blob: 062b92858c4f58116beb0c15f3e919896a37e2ac [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#include <linux/irq.h>
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/io.h>
17#include <linux/interrupt.h>
18#include <linux/platform_device.h>
19#include <linux/dma-mapping.h>
20#include <linux/slimbus/slimbus.h>
21#include <linux/delay.h>
22#include <linux/kthread.h>
23#include <linux/clk.h>
Sagar Dharia45ee38a2011-08-03 17:01:31 -060024#include <linux/pm_runtime.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include <mach/sps.h>
26
27/* Per spec.max 40 bytes per received message */
28#define SLIM_RX_MSGQ_BUF_LEN 40
29
30#define SLIM_USR_MC_GENERIC_ACK 0x25
31#define SLIM_USR_MC_MASTER_CAPABILITY 0x0
32#define SLIM_USR_MC_REPORT_SATELLITE 0x1
33#define SLIM_USR_MC_ADDR_QUERY 0xD
34#define SLIM_USR_MC_ADDR_REPLY 0xE
35#define SLIM_USR_MC_DEFINE_CHAN 0x20
36#define SLIM_USR_MC_DEF_ACT_CHAN 0x21
37#define SLIM_USR_MC_CHAN_CTRL 0x23
38#define SLIM_USR_MC_RECONFIG_NOW 0x24
39#define SLIM_USR_MC_REQ_BW 0x28
40#define SLIM_USR_MC_CONNECT_SRC 0x2C
41#define SLIM_USR_MC_CONNECT_SINK 0x2D
42#define SLIM_USR_MC_DISCONNECT_PORT 0x2E
43
44/* MSM Slimbus peripheral settings */
45#define MSM_SLIM_PERF_SUMM_THRESHOLD 0x8000
46#define MSM_SLIM_NCHANS 32
47#define MSM_SLIM_NPORTS 24
Sagar Dharia45ee38a2011-08-03 17:01:31 -060048#define MSM_SLIM_AUTOSUSPEND MSEC_PER_SEC
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049
50/*
51 * Need enough descriptors to receive present messages from slaves
52 * if received simultaneously. Present message needs 3 descriptors
53 * and this size will ensure around 10 simultaneous reports.
54 */
55#define MSM_SLIM_DESC_NUM 32
56
57#define SLIM_MSG_ASM_FIRST_WORD(l, mt, mc, dt, ad) \
58 ((l) | ((mt) << 5) | ((mc) << 8) | ((dt) << 15) | ((ad) << 16))
59
60#define MSM_SLIM_NAME "msm_slim_ctrl"
61#define SLIM_ROOT_FREQ 24576000
62
63#define MSM_CONCUR_MSG 8
64#define SAT_CONCUR_MSG 8
65#define DEF_WATERMARK (8 << 1)
66#define DEF_ALIGN 0
67#define DEF_PACK (1 << 6)
68#define ENABLE_PORT 1
69
70#define DEF_BLKSZ 0
71#define DEF_TRANSZ 0
72
73#define SAT_MAGIC_LSB 0xD9
74#define SAT_MAGIC_MSB 0xC5
75#define SAT_MSG_VER 0x1
76#define SAT_MSG_PROT 0x1
77#define MSM_SAT_SUCCSS 0x20
78
79#define QC_MFGID_LSB 0x2
80#define QC_MFGID_MSB 0x17
81#define QC_CHIPID_SL 0x10
82#define QC_DEVID_SAT1 0x3
83#define QC_DEVID_SAT2 0x4
84#define QC_DEVID_PGD 0x5
Sagar Dharia45ee38a2011-08-03 17:01:31 -060085#define QC_MSM_DEVS 5
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070086
87/* Component registers */
88enum comp_reg {
89 COMP_CFG = 0,
90 COMP_TRUST_CFG = 0x14,
91};
92
93/* Manager registers */
94enum mgr_reg {
95 MGR_CFG = 0x200,
96 MGR_STATUS = 0x204,
97 MGR_RX_MSGQ_CFG = 0x208,
98 MGR_INT_EN = 0x210,
99 MGR_INT_STAT = 0x214,
100 MGR_INT_CLR = 0x218,
101 MGR_TX_MSG = 0x230,
102 MGR_RX_MSG = 0x270,
103 MGR_VE_STAT = 0x300,
104};
105
106enum msg_cfg {
107 MGR_CFG_ENABLE = 1,
108 MGR_CFG_RX_MSGQ_EN = 1 << 1,
109 MGR_CFG_TX_MSGQ_EN_HIGH = 1 << 2,
110 MGR_CFG_TX_MSGQ_EN_LOW = 1 << 3,
111};
112/* Message queue types */
113enum msm_slim_msgq_type {
114 MSGQ_RX = 0,
115 MSGQ_TX_LOW = 1,
116 MSGQ_TX_HIGH = 2,
117};
118/* Framer registers */
119enum frm_reg {
120 FRM_CFG = 0x400,
121 FRM_STAT = 0x404,
122 FRM_INT_EN = 0x410,
123 FRM_INT_STAT = 0x414,
124 FRM_INT_CLR = 0x418,
125 FRM_WAKEUP = 0x41C,
126 FRM_CLKCTL_DONE = 0x420,
127 FRM_IE_STAT = 0x430,
128 FRM_VE_STAT = 0x440,
129};
130
131/* Interface registers */
132enum intf_reg {
133 INTF_CFG = 0x600,
134 INTF_STAT = 0x604,
135 INTF_INT_EN = 0x610,
136 INTF_INT_STAT = 0x614,
137 INTF_INT_CLR = 0x618,
138 INTF_IE_STAT = 0x630,
139 INTF_VE_STAT = 0x640,
140};
141
142/* Manager PGD registers */
143enum pgd_reg {
144 PGD_CFG = 0x1000,
145 PGD_STAT = 0x1004,
146 PGD_INT_EN = 0x1010,
147 PGD_INT_STAT = 0x1014,
148 PGD_INT_CLR = 0x1018,
149 PGD_OWN_EEn = 0x1020,
150 PGD_PORT_INT_EN_EEn = 0x1030,
151 PGD_PORT_INT_ST_EEn = 0x1034,
152 PGD_PORT_INT_CL_EEn = 0x1038,
153 PGD_PORT_CFGn = 0x1080,
154 PGD_PORT_STATn = 0x1084,
155 PGD_PORT_PARAMn = 0x1088,
156 PGD_PORT_BLKn = 0x108C,
157 PGD_PORT_TRANn = 0x1090,
158 PGD_PORT_MCHANn = 0x1094,
159 PGD_PORT_PSHPLLn = 0x1098,
160 PGD_PORT_PC_CFGn = 0x1600,
161 PGD_PORT_PC_VALn = 0x1604,
162 PGD_PORT_PC_VFR_TSn = 0x1608,
163 PGD_PORT_PC_VFR_STn = 0x160C,
164 PGD_PORT_PC_VFR_CLn = 0x1610,
165 PGD_IE_STAT = 0x1700,
166 PGD_VE_STAT = 0x1710,
167};
168
169enum rsc_grp {
170 EE_MGR_RSC_GRP = 1 << 10,
171 EE_NGD_2 = 2 << 6,
172 EE_NGD_1 = 0,
173};
174
175enum mgr_intr {
176 MGR_INT_RECFG_DONE = 1 << 24,
177 MGR_INT_TX_NACKED_2 = 1 << 25,
178 MGR_INT_MSG_BUF_CONTE = 1 << 26,
179 MGR_INT_RX_MSG_RCVD = 1 << 30,
180 MGR_INT_TX_MSG_SENT = 1 << 31,
181};
182
183enum frm_cfg {
184 FRM_ACTIVE = 1,
185 CLK_GEAR = 7,
186 ROOT_FREQ = 11,
187 REF_CLK_GEAR = 15,
188};
189
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600190enum msm_ctrl_state {
191 MSM_CTRL_AWAKE,
192 MSM_CTRL_SLEEPING,
193 MSM_CTRL_ASLEEP,
194};
195
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196struct msm_slim_sps_bam {
197 u32 hdl;
198 void __iomem *base;
199 int irq;
200};
201
202struct msm_slim_endp {
203 struct sps_pipe *sps;
204 struct sps_connect config;
205 struct sps_register_event event;
206 struct sps_mem_buffer buf;
207 struct completion *xcomp;
208 bool connected;
209};
210
211struct msm_slim_ctrl {
212 struct slim_controller ctrl;
213 struct slim_framer framer;
214 struct device *dev;
215 void __iomem *base;
Sagar Dhariacc969452011-09-19 10:34:30 -0600216 struct resource *slew_mem;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217 u32 curr_bw;
218 u8 msg_cnt;
219 u32 tx_buf[10];
220 u8 rx_msgs[MSM_CONCUR_MSG][SLIM_RX_MSGQ_BUF_LEN];
221 spinlock_t rx_lock;
222 int head;
223 int tail;
224 int irq;
225 int err;
226 int ee;
227 struct completion *wr_comp;
228 struct msm_slim_sat *satd;
229 struct msm_slim_endp pipes[7];
230 struct msm_slim_sps_bam bam;
231 struct msm_slim_endp rx_msgq;
232 struct completion rx_msgq_notify;
233 struct task_struct *rx_msgq_thread;
234 struct clk *rclk;
235 struct mutex tx_lock;
236 u8 pgdla;
237 bool use_rx_msgqs;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 int pipe_b;
239 struct completion reconf;
240 bool reconf_busy;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600241 bool chan_active;
242 enum msm_ctrl_state state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243};
244
245struct msm_slim_sat {
246 struct slim_device satcl;
247 struct msm_slim_ctrl *dev;
248 struct workqueue_struct *wq;
249 struct work_struct wd;
250 u8 sat_msgs[SAT_CONCUR_MSG][40];
251 u16 *satch;
252 u8 nsatch;
253 bool sent_capability;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600254 bool pending_reconf;
255 bool pending_capability;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256 int shead;
257 int stail;
258 spinlock_t lock;
259};
260
261static int msm_slim_rx_enqueue(struct msm_slim_ctrl *dev, u32 *buf, u8 len)
262{
263 spin_lock(&dev->rx_lock);
264 if ((dev->tail + 1) % MSM_CONCUR_MSG == dev->head) {
265 spin_unlock(&dev->rx_lock);
266 dev_err(dev->dev, "RX QUEUE full!");
267 return -EXFULL;
268 }
269 memcpy((u8 *)dev->rx_msgs[dev->tail], (u8 *)buf, len);
270 dev->tail = (dev->tail + 1) % MSM_CONCUR_MSG;
271 spin_unlock(&dev->rx_lock);
272 return 0;
273}
274
275static int msm_slim_rx_dequeue(struct msm_slim_ctrl *dev, u8 *buf)
276{
277 unsigned long flags;
278 spin_lock_irqsave(&dev->rx_lock, flags);
279 if (dev->tail == dev->head) {
280 spin_unlock_irqrestore(&dev->rx_lock, flags);
281 return -ENODATA;
282 }
283 memcpy(buf, (u8 *)dev->rx_msgs[dev->head], 40);
284 dev->head = (dev->head + 1) % MSM_CONCUR_MSG;
285 spin_unlock_irqrestore(&dev->rx_lock, flags);
286 return 0;
287}
288
289static int msm_sat_enqueue(struct msm_slim_sat *sat, u32 *buf, u8 len)
290{
291 struct msm_slim_ctrl *dev = sat->dev;
292 spin_lock(&sat->lock);
293 if ((sat->stail + 1) % SAT_CONCUR_MSG == sat->shead) {
294 spin_unlock(&sat->lock);
295 dev_err(dev->dev, "SAT QUEUE full!");
296 return -EXFULL;
297 }
298 memcpy(sat->sat_msgs[sat->stail], (u8 *)buf, len);
299 sat->stail = (sat->stail + 1) % SAT_CONCUR_MSG;
300 spin_unlock(&sat->lock);
301 return 0;
302}
303
304static int msm_sat_dequeue(struct msm_slim_sat *sat, u8 *buf)
305{
306 unsigned long flags;
307 spin_lock_irqsave(&sat->lock, flags);
308 if (sat->stail == sat->shead) {
309 spin_unlock_irqrestore(&sat->lock, flags);
310 return -ENODATA;
311 }
312 memcpy(buf, sat->sat_msgs[sat->shead], 40);
313 sat->shead = (sat->shead + 1) % SAT_CONCUR_MSG;
314 spin_unlock_irqrestore(&sat->lock, flags);
315 return 0;
316}
317
318static void msm_get_eaddr(u8 *e_addr, u32 *buffer)
319{
320 e_addr[0] = (buffer[1] >> 24) & 0xff;
321 e_addr[1] = (buffer[1] >> 16) & 0xff;
322 e_addr[2] = (buffer[1] >> 8) & 0xff;
323 e_addr[3] = buffer[1] & 0xff;
324 e_addr[4] = (buffer[0] >> 24) & 0xff;
325 e_addr[5] = (buffer[0] >> 16) & 0xff;
326}
327
328static bool msm_is_sat_dev(u8 *e_addr)
329{
330 if (e_addr[5] == QC_MFGID_LSB && e_addr[4] == QC_MFGID_MSB &&
331 e_addr[2] != QC_CHIPID_SL &&
332 (e_addr[1] == QC_DEVID_SAT1 || e_addr[1] == QC_DEVID_SAT2))
333 return true;
334 return false;
335}
336
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700337static int msm_slim_get_ctrl(struct msm_slim_ctrl *dev)
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600338{
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700339 int ref;
340 int ret = pm_runtime_get_sync(dev->dev);
341 if (ret >= 0) {
342 ref = atomic_read(&dev->dev->power.usage_count);
343 if (ref <= 0) {
344 dev_err(dev->dev, "reference count -ve:%d", ref);
345 ret = -ENODEV;
346 }
347 }
348 return ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600349}
350static void msm_slim_put_ctrl(struct msm_slim_ctrl *dev)
351{
352 pm_runtime_mark_last_busy(dev->dev);
353 pm_runtime_put(dev->dev);
354}
355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356static irqreturn_t msm_slim_interrupt(int irq, void *d)
357{
358 struct msm_slim_ctrl *dev = d;
359 u32 pstat;
360 u32 stat = readl_relaxed(dev->base + MGR_INT_STAT);
361
362 if (stat & MGR_INT_TX_MSG_SENT || stat & MGR_INT_TX_NACKED_2) {
363 if (stat & MGR_INT_TX_MSG_SENT)
364 writel_relaxed(MGR_INT_TX_MSG_SENT,
365 dev->base + MGR_INT_CLR);
366 else {
367 writel_relaxed(MGR_INT_TX_NACKED_2,
368 dev->base + MGR_INT_CLR);
369 dev->err = -EIO;
370 }
371 /*
372 * Guarantee that interrupt clear bit write goes through before
373 * signalling completion/exiting ISR
374 */
375 mb();
376 if (dev->wr_comp)
377 complete(dev->wr_comp);
378 }
379 if (stat & MGR_INT_RX_MSG_RCVD) {
380 u32 rx_buf[10];
381 u32 mc, mt;
382 u8 len, i;
383 rx_buf[0] = readl_relaxed(dev->base + MGR_RX_MSG);
384 len = rx_buf[0] & 0x1F;
385 for (i = 1; i < ((len + 3) >> 2); i++) {
386 rx_buf[i] = readl_relaxed(dev->base + MGR_RX_MSG +
387 (4 * i));
388 dev_dbg(dev->dev, "reading data: %x\n", rx_buf[i]);
389 }
390 mt = (rx_buf[0] >> 5) & 0x7;
391 mc = (rx_buf[0] >> 8) & 0xff;
392 dev_dbg(dev->dev, "MC: %x, MT: %x\n", mc, mt);
393 if (mt == SLIM_MSG_MT_DEST_REFERRED_USER ||
394 mt == SLIM_MSG_MT_SRC_REFERRED_USER) {
395 struct msm_slim_sat *sat = dev->satd;
396 msm_sat_enqueue(sat, rx_buf, len);
397 writel_relaxed(MGR_INT_RX_MSG_RCVD,
398 dev->base + MGR_INT_CLR);
399 /*
400 * Guarantee that CLR bit write goes through before
401 * queuing work
402 */
403 mb();
404 queue_work(sat->wq, &sat->wd);
405 } else if (mt == SLIM_MSG_MT_CORE &&
406 mc == SLIM_MSG_MC_REPORT_PRESENT) {
407 u8 e_addr[6];
408 msm_get_eaddr(e_addr, rx_buf);
409 if (msm_is_sat_dev(e_addr)) {
410 /*
411 * Consider possibility that this device may
412 * be reporting more than once?
413 */
414 struct msm_slim_sat *sat = dev->satd;
415 msm_sat_enqueue(sat, rx_buf, len);
416 writel_relaxed(MGR_INT_RX_MSG_RCVD, dev->base +
417 MGR_INT_CLR);
418 /*
419 * Guarantee that CLR bit write goes through
420 * before queuing work
421 */
422 mb();
423 queue_work(sat->wq, &sat->wd);
424 } else {
425 msm_slim_rx_enqueue(dev, rx_buf, len);
426 writel_relaxed(MGR_INT_RX_MSG_RCVD, dev->base +
427 MGR_INT_CLR);
428 /*
429 * Guarantee that CLR bit write goes through
430 * before signalling completion
431 */
432 mb();
433 complete(&dev->rx_msgq_notify);
434 }
435 } else if (mc == SLIM_MSG_MC_REPLY_INFORMATION ||
436 mc == SLIM_MSG_MC_REPLY_VALUE) {
437 msm_slim_rx_enqueue(dev, rx_buf, len);
438 writel_relaxed(MGR_INT_RX_MSG_RCVD, dev->base +
439 MGR_INT_CLR);
440 /*
441 * Guarantee that CLR bit write goes through
442 * before signalling completion
443 */
444 mb();
445 complete(&dev->rx_msgq_notify);
Sagar Dharia144e5e02011-08-08 17:30:11 -0600446 } else if (mc == SLIM_MSG_MC_REPORT_INFORMATION) {
447 u8 *buf = (u8 *)rx_buf;
448 u8 l_addr = buf[2];
449 u16 ele = (u16)buf[4] << 4;
450 ele |= ((buf[3] & 0xf0) >> 4);
451 dev_err(dev->dev, "Slim-dev:%d report inf element:0x%x",
452 l_addr, ele);
453 for (i = 0; i < len - 5; i++)
454 dev_err(dev->dev, "offset:0x%x:bit mask:%x",
455 i, buf[i+5]);
456 writel_relaxed(MGR_INT_RX_MSG_RCVD, dev->base +
457 MGR_INT_CLR);
458 /*
459 * Guarantee that CLR bit write goes through
460 * before exiting
461 */
462 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463 } else {
464 dev_err(dev->dev, "Unexpected MC,%x MT:%x, len:%d",
465 mc, mt, len);
466 for (i = 0; i < ((len + 3) >> 2); i++)
467 dev_err(dev->dev, "error msg: %x", rx_buf[i]);
468 writel_relaxed(MGR_INT_RX_MSG_RCVD, dev->base +
469 MGR_INT_CLR);
470 /*
471 * Guarantee that CLR bit write goes through
472 * before exiting
473 */
474 mb();
475 }
476 }
477 if (stat & MGR_INT_RECFG_DONE) {
478 writel_relaxed(MGR_INT_RECFG_DONE, dev->base + MGR_INT_CLR);
479 /*
480 * Guarantee that CLR bit write goes through
481 * before exiting ISR
482 */
483 mb();
484 complete(&dev->reconf);
485 }
486 pstat = readl_relaxed(dev->base + PGD_PORT_INT_ST_EEn + (16 * dev->ee));
487 if (pstat != 0) {
488 int i = 0;
489 for (i = dev->pipe_b; i < MSM_SLIM_NPORTS; i++) {
490 if (pstat & 1 << i) {
491 u32 val = readl_relaxed(dev->base +
492 PGD_PORT_STATn + (i * 32));
493 if (val & (1 << 19)) {
494 dev->ctrl.ports[i].err =
495 SLIM_P_DISCONNECT;
496 dev->pipes[i-dev->pipe_b].connected =
497 false;
498 /*
499 * SPS will call completion since
500 * ERROR flags are registered
501 */
502 } else if (val & (1 << 2))
503 dev->ctrl.ports[i].err =
504 SLIM_P_OVERFLOW;
505 else if (val & (1 << 3))
506 dev->ctrl.ports[i].err =
507 SLIM_P_UNDERFLOW;
508 }
509 writel_relaxed(1, dev->base + PGD_PORT_INT_CL_EEn +
510 (dev->ee * 16));
511 }
512 /*
513 * Guarantee that port interrupt bit(s) clearing writes go
514 * through before exiting ISR
515 */
516 mb();
517 }
518
519 return IRQ_HANDLED;
520}
521
522static int
523msm_slim_init_endpoint(struct msm_slim_ctrl *dev, struct msm_slim_endp *ep)
524{
525 int ret;
526 struct sps_pipe *endpoint;
527 struct sps_connect *config = &ep->config;
528
529 /* Allocate the endpoint */
530 endpoint = sps_alloc_endpoint();
531 if (!endpoint) {
532 dev_err(dev->dev, "sps_alloc_endpoint failed\n");
533 return -ENOMEM;
534 }
535
536 /* Get default connection configuration for an endpoint */
537 ret = sps_get_config(endpoint, config);
538 if (ret) {
539 dev_err(dev->dev, "sps_get_config failed 0x%x\n", ret);
540 goto sps_config_failed;
541 }
542
543 ep->sps = endpoint;
544 return 0;
545
546sps_config_failed:
547 sps_free_endpoint(endpoint);
548 return ret;
549}
550
551static void
552msm_slim_free_endpoint(struct msm_slim_endp *ep)
553{
554 sps_free_endpoint(ep->sps);
555 ep->sps = NULL;
556}
557
558static int msm_slim_sps_mem_alloc(
559 struct msm_slim_ctrl *dev, struct sps_mem_buffer *mem, u32 len)
560{
561 dma_addr_t phys;
562
563 mem->size = len;
564 mem->min_size = 0;
565 mem->base = dma_alloc_coherent(dev->dev, mem->size, &phys, GFP_KERNEL);
566
567 if (!mem->base) {
568 dev_err(dev->dev, "dma_alloc_coherent(%d) failed\n", len);
569 return -ENOMEM;
570 }
571
572 mem->phys_base = phys;
573 memset(mem->base, 0x00, mem->size);
574 return 0;
575}
576
577static void
578msm_slim_sps_mem_free(struct msm_slim_ctrl *dev, struct sps_mem_buffer *mem)
579{
580 dma_free_coherent(dev->dev, mem->size, mem->base, mem->phys_base);
581 mem->size = 0;
582 mem->base = NULL;
583 mem->phys_base = 0;
584}
585
586static void msm_hw_set_port(struct msm_slim_ctrl *dev, u8 pn)
587{
588 u32 set_cfg = DEF_WATERMARK | DEF_ALIGN | DEF_PACK | ENABLE_PORT;
589 u32 int_port = readl_relaxed(dev->base + PGD_PORT_INT_EN_EEn +
590 (dev->ee * 16));
591 writel_relaxed(set_cfg, dev->base + PGD_PORT_CFGn + (pn * 32));
592 writel_relaxed(DEF_BLKSZ, dev->base + PGD_PORT_BLKn + (pn * 32));
593 writel_relaxed(DEF_TRANSZ, dev->base + PGD_PORT_TRANn + (pn * 32));
594 writel_relaxed((int_port | 1 << pn) , dev->base + PGD_PORT_INT_EN_EEn +
595 (dev->ee * 16));
596 /* Make sure that port registers are updated before returning */
597 mb();
598}
599
600static int msm_slim_connect_pipe_port(struct msm_slim_ctrl *dev, u8 pn)
601{
602 struct msm_slim_endp *endpoint = &dev->pipes[pn];
603 struct sps_connect *cfg = &endpoint->config;
604 u32 stat;
605 int ret = sps_get_config(dev->pipes[pn].sps, cfg);
606 if (ret) {
607 dev_err(dev->dev, "sps pipe-port get config error%x\n", ret);
608 return ret;
609 }
610 cfg->options = SPS_O_DESC_DONE | SPS_O_ERROR |
611 SPS_O_ACK_TRANSFERS | SPS_O_AUTO_ENABLE;
612
613 if (dev->pipes[pn].connected) {
614 ret = sps_set_config(dev->pipes[pn].sps, cfg);
615 if (ret) {
616 dev_err(dev->dev, "sps pipe-port set config erro:%x\n",
617 ret);
618 return ret;
619 }
620 }
621
622 stat = readl_relaxed(dev->base + PGD_PORT_STATn +
623 (32 * (pn + dev->pipe_b)));
624 if (dev->ctrl.ports[pn].flow == SLIM_SRC) {
625 cfg->destination = dev->bam.hdl;
626 cfg->source = SPS_DEV_HANDLE_MEM;
627 cfg->dest_pipe_index = ((stat & (0xFF << 4)) >> 4);
628 cfg->src_pipe_index = 0;
629 dev_dbg(dev->dev, "flow src:pipe num:%d",
630 cfg->dest_pipe_index);
631 cfg->mode = SPS_MODE_DEST;
632 } else {
633 cfg->source = dev->bam.hdl;
634 cfg->destination = SPS_DEV_HANDLE_MEM;
635 cfg->src_pipe_index = ((stat & (0xFF << 4)) >> 4);
636 cfg->dest_pipe_index = 0;
637 dev_dbg(dev->dev, "flow dest:pipe num:%d",
638 cfg->src_pipe_index);
639 cfg->mode = SPS_MODE_SRC;
640 }
641 /* Space for desciptor FIFOs */
642 cfg->desc.size = MSM_SLIM_DESC_NUM * sizeof(struct sps_iovec);
643 cfg->config = SPS_CONFIG_DEFAULT;
644 ret = sps_connect(dev->pipes[pn].sps, cfg);
645 if (!ret) {
646 dev->pipes[pn].connected = true;
647 msm_hw_set_port(dev, pn + dev->pipe_b);
648 }
649 return ret;
650}
651
652static u32 *msm_get_msg_buf(struct slim_controller *ctrl, int len)
653{
654 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
655 /*
656 * Currently we block a transaction until the current one completes.
657 * In case we need multiple transactions, use message Q
658 */
659 return dev->tx_buf;
660}
661
662static int msm_send_msg_buf(struct slim_controller *ctrl, u32 *buf, u8 len)
663{
664 int i;
665 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
666 for (i = 0; i < (len + 3) >> 2; i++) {
667 dev_dbg(dev->dev, "TX data:0x%x\n", buf[i]);
668 writel_relaxed(buf[i], dev->base + MGR_TX_MSG + (i * 4));
669 }
670 /* Guarantee that message is sent before returning */
671 mb();
672 return 0;
673}
674
675static int msm_xfer_msg(struct slim_controller *ctrl, struct slim_msg_txn *txn)
676{
677 DECLARE_COMPLETION_ONSTACK(done);
678 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
679 u32 *pbuf;
680 u8 *puc;
681 int timeout;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700682 int msgv = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683 u8 la = txn->la;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600684 u8 mc = (u8)(txn->mc & 0xFF);
685 /*
686 * Voting for runtime PM: Slimbus has 2 possible use cases:
687 * 1. messaging
688 * 2. Data channels
689 * Messaging case goes through messaging slots and data channels
690 * use their own slots
691 * This "get" votes for messaging bandwidth
692 */
693 if (!(txn->mc & SLIM_MSG_CLK_PAUSE_SEQ_FLG))
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700694 msgv = msm_slim_get_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695 mutex_lock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700696 if (dev->state == MSM_CTRL_ASLEEP ||
697 ((!(txn->mc & SLIM_MSG_CLK_PAUSE_SEQ_FLG)) &&
698 dev->state == MSM_CTRL_SLEEPING)) {
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600699 dev_err(dev->dev, "runtime or system PM suspended state");
700 mutex_unlock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700701 if (msgv >= 0)
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600702 msm_slim_put_ctrl(dev);
703 return -EBUSY;
704 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705 if (txn->mt == SLIM_MSG_MT_CORE &&
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600706 mc == SLIM_MSG_MC_BEGIN_RECONFIGURATION) {
707 if (dev->reconf_busy) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708 wait_for_completion(&dev->reconf);
709 dev->reconf_busy = false;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600710 }
711 /* This "get" votes for data channels */
712 if (dev->ctrl.sched.usedslots != 0 &&
713 !dev->chan_active) {
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700714 int chv = msm_slim_get_ctrl(dev);
715 if (chv >= 0)
716 dev->chan_active = true;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600717 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 }
719 txn->rl--;
720 pbuf = msm_get_msg_buf(ctrl, txn->rl);
721 dev->wr_comp = NULL;
722 dev->err = 0;
723
724 if (txn->dt == SLIM_MSG_DEST_ENUMADDR) {
725 mutex_unlock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700726 if (msgv >= 0)
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600727 msm_slim_put_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728 return -EPROTONOSUPPORT;
729 }
730 if (txn->mt == SLIM_MSG_MT_CORE && txn->la == 0xFF &&
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600731 (mc == SLIM_MSG_MC_CONNECT_SOURCE ||
732 mc == SLIM_MSG_MC_CONNECT_SINK ||
733 mc == SLIM_MSG_MC_DISCONNECT_PORT))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 la = dev->pgdla;
735 if (txn->dt == SLIM_MSG_DEST_LOGICALADDR)
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600736 *pbuf = SLIM_MSG_ASM_FIRST_WORD(txn->rl, txn->mt, mc, 0, la);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 else
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600738 *pbuf = SLIM_MSG_ASM_FIRST_WORD(txn->rl, txn->mt, mc, 1, la);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 if (txn->dt == SLIM_MSG_DEST_LOGICALADDR)
740 puc = ((u8 *)pbuf) + 3;
741 else
742 puc = ((u8 *)pbuf) + 2;
743 if (txn->rbuf)
744 *(puc++) = txn->tid;
745 if ((txn->mt == SLIM_MSG_MT_CORE) &&
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600746 ((mc >= SLIM_MSG_MC_REQUEST_INFORMATION &&
747 mc <= SLIM_MSG_MC_REPORT_INFORMATION) ||
748 (mc >= SLIM_MSG_MC_REQUEST_VALUE &&
749 mc <= SLIM_MSG_MC_CHANGE_VALUE))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750 *(puc++) = (txn->ec & 0xFF);
751 *(puc++) = (txn->ec >> 8)&0xFF;
752 }
753 if (txn->wbuf)
754 memcpy(puc, txn->wbuf, txn->len);
755 if (txn->mt == SLIM_MSG_MT_CORE && txn->la == 0xFF &&
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600756 (mc == SLIM_MSG_MC_CONNECT_SOURCE ||
757 mc == SLIM_MSG_MC_CONNECT_SINK ||
758 mc == SLIM_MSG_MC_DISCONNECT_PORT)) {
759 if (mc != SLIM_MSG_MC_DISCONNECT_PORT)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700760 dev->err = msm_slim_connect_pipe_port(dev, *puc);
761 else {
762 struct msm_slim_endp *endpoint = &dev->pipes[*puc];
763 struct sps_register_event sps_event;
764 memset(&sps_event, 0, sizeof(sps_event));
765 sps_register_event(endpoint->sps, &sps_event);
766 sps_disconnect(endpoint->sps);
767 /*
768 * Remove channel disconnects master-side ports from
769 * channel. No need to send that again on the bus
770 */
771 dev->pipes[*puc].connected = false;
772 mutex_unlock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700773 if (msgv >= 0)
774 msm_slim_put_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 return 0;
776 }
777 if (dev->err) {
778 dev_err(dev->dev, "pipe-port connect err:%d", dev->err);
779 mutex_unlock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700780 if (msgv >= 0)
781 msm_slim_put_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 return dev->err;
783 }
784 *(puc) = *(puc) + dev->pipe_b;
785 }
786 if (txn->mt == SLIM_MSG_MT_CORE &&
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600787 mc == SLIM_MSG_MC_BEGIN_RECONFIGURATION)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 dev->reconf_busy = true;
789 dev->wr_comp = &done;
790 msm_send_msg_buf(ctrl, pbuf, txn->rl);
791 timeout = wait_for_completion_timeout(&done, HZ);
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600792
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700793 if (mc == SLIM_MSG_MC_RECONFIGURE_NOW) {
794 if ((txn->mc == (SLIM_MSG_MC_RECONFIGURE_NOW |
795 SLIM_MSG_CLK_PAUSE_SEQ_FLG)) &&
796 timeout) {
797 timeout = wait_for_completion_timeout(&dev->reconf, HZ);
798 dev->reconf_busy = false;
799 if (timeout) {
800 clk_disable(dev->rclk);
801 disable_irq(dev->irq);
802 }
803 }
804 if ((txn->mc == (SLIM_MSG_MC_RECONFIGURE_NOW |
805 SLIM_MSG_CLK_PAUSE_SEQ_FLG)) &&
806 !timeout) {
807 dev->reconf_busy = false;
808 dev_err(dev->dev, "clock pause failed");
809 mutex_unlock(&dev->tx_lock);
810 return -ETIMEDOUT;
811 }
812 if (txn->mt == SLIM_MSG_MT_CORE &&
813 txn->mc == SLIM_MSG_MC_RECONFIGURE_NOW) {
814 if (dev->ctrl.sched.usedslots == 0 &&
815 dev->chan_active) {
816 dev->chan_active = false;
817 msm_slim_put_ctrl(dev);
818 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600819 }
820 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600821 mutex_unlock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700822 if (msgv >= 0)
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600823 msm_slim_put_ctrl(dev);
824
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 if (!timeout)
826 dev_err(dev->dev, "TX timed out:MC:0x%x,mt:0x%x", txn->mc,
827 txn->mt);
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600828
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 return timeout ? dev->err : -ETIMEDOUT;
830}
831
832static int msm_set_laddr(struct slim_controller *ctrl, const u8 *ea,
833 u8 elen, u8 laddr)
834{
835 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
836 DECLARE_COMPLETION_ONSTACK(done);
837 int timeout;
838 u32 *buf;
839 mutex_lock(&dev->tx_lock);
840 buf = msm_get_msg_buf(ctrl, 9);
841 buf[0] = SLIM_MSG_ASM_FIRST_WORD(9, SLIM_MSG_MT_CORE,
842 SLIM_MSG_MC_ASSIGN_LOGICAL_ADDRESS,
843 SLIM_MSG_DEST_LOGICALADDR,
844 ea[5] | ea[4] << 8);
845 buf[1] = ea[3] | (ea[2] << 8) | (ea[1] << 16) | (ea[0] << 24);
846 buf[2] = laddr;
847
848 dev->wr_comp = &done;
849 msm_send_msg_buf(ctrl, buf, 9);
850 timeout = wait_for_completion_timeout(&done, HZ);
851 mutex_unlock(&dev->tx_lock);
852 return timeout ? dev->err : -ETIMEDOUT;
853}
854
Sagar Dharia144e5e02011-08-08 17:30:11 -0600855static int msm_clk_pause_wakeup(struct slim_controller *ctrl)
856{
857 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600858 enable_irq(dev->irq);
Sagar Dharia144e5e02011-08-08 17:30:11 -0600859 clk_enable(dev->rclk);
860 writel_relaxed(1, dev->base + FRM_WAKEUP);
861 /* Make sure framer wakeup write goes through before exiting function */
862 mb();
863 /*
864 * Workaround: Currently, slave is reporting lost-sync messages
865 * after slimbus comes out of clock pause.
866 * Transaction with slave fail before slave reports that message
867 * Give some time for that report to come
868 * Slimbus wakes up in clock gear 10 at 24.576MHz. With each superframe
869 * being 250 usecs, we wait for 20 superframes here to ensure
870 * we get the message
871 */
872 usleep_range(5000, 5000);
873 return 0;
874}
875
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876static int msm_config_port(struct slim_controller *ctrl, u8 pn)
877{
878 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
879 struct msm_slim_endp *endpoint;
880 int ret = 0;
881 if (ctrl->ports[pn].req == SLIM_REQ_HALF_DUP ||
882 ctrl->ports[pn].req == SLIM_REQ_MULTI_CH)
883 return -EPROTONOSUPPORT;
884 if (pn >= (MSM_SLIM_NPORTS - dev->pipe_b))
885 return -ENODEV;
886
887 endpoint = &dev->pipes[pn];
888 ret = msm_slim_init_endpoint(dev, endpoint);
889 dev_dbg(dev->dev, "sps register bam error code:%x\n", ret);
890 return ret;
891}
892
893static enum slim_port_err msm_slim_port_xfer_status(struct slim_controller *ctr,
894 u8 pn, u8 **done_buf, u32 *done_len)
895{
896 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctr);
897 struct sps_iovec sio;
898 int ret;
899 if (done_len)
900 *done_len = 0;
901 if (done_buf)
902 *done_buf = NULL;
903 if (!dev->pipes[pn].connected)
904 return SLIM_P_DISCONNECT;
905 ret = sps_get_iovec(dev->pipes[pn].sps, &sio);
906 if (!ret) {
907 if (done_len)
908 *done_len = sio.size;
909 if (done_buf)
910 *done_buf = (u8 *)sio.addr;
911 }
912 dev_dbg(dev->dev, "get iovec returned %d\n", ret);
913 return SLIM_P_INPROGRESS;
914}
915
916static int msm_slim_port_xfer(struct slim_controller *ctrl, u8 pn, u8 *iobuf,
917 u32 len, struct completion *comp)
918{
919 struct sps_register_event sreg;
920 int ret;
921 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
Sagar Dhariae77961f2011-09-27 14:03:50 -0600922 if (pn >= 7)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923 return -ENODEV;
924
925
926 ctrl->ports[pn].xcomp = comp;
927 sreg.options = (SPS_EVENT_DESC_DONE|SPS_EVENT_ERROR);
928 sreg.mode = SPS_TRIGGER_WAIT;
929 sreg.xfer_done = comp;
930 sreg.callback = NULL;
931 sreg.user = &ctrl->ports[pn];
932 ret = sps_register_event(dev->pipes[pn].sps, &sreg);
933 if (ret) {
934 dev_dbg(dev->dev, "sps register event error:%x\n", ret);
935 return ret;
936 }
937 ret = sps_transfer_one(dev->pipes[pn].sps, (u32)iobuf, len, NULL,
938 SPS_IOVEC_FLAG_INT);
939 dev_dbg(dev->dev, "sps submit xfer error code:%x\n", ret);
940
941 return ret;
942}
943
944static int msm_sat_define_ch(struct msm_slim_sat *sat, u8 *buf, u8 len, u8 mc)
945{
946 struct msm_slim_ctrl *dev = sat->dev;
947 enum slim_ch_control oper;
948 int i;
949 int ret = 0;
950 if (mc == SLIM_USR_MC_CHAN_CTRL) {
951 u16 chanh = sat->satch[buf[5]];
952 oper = ((buf[3] & 0xC0) >> 6);
953 /* part of grp. activating/removing 1 will take care of rest */
954 ret = slim_control_ch(&sat->satcl, chanh, oper, false);
955 } else {
956 u16 chh[40];
957 struct slim_ch prop;
958 u32 exp;
959 u8 coeff, cc;
960 u8 prrate = buf[6];
961 for (i = 8; i < len; i++)
962 chh[i-8] = sat->satch[buf[i]];
963 prop.dataf = (enum slim_ch_dataf)((buf[3] & 0xE0) >> 5);
964 prop.auxf = (enum slim_ch_auxf)((buf[4] & 0xC0) >> 5);
965 prop.baser = SLIM_RATE_4000HZ;
966 if (prrate & 0x8)
967 prop.baser = SLIM_RATE_11025HZ;
968 else
969 prop.baser = SLIM_RATE_4000HZ;
970 prop.prot = (enum slim_ch_proto)(buf[5] & 0x0F);
971 prop.sampleszbits = (buf[4] & 0x1F)*SLIM_CL_PER_SL;
972 exp = (u32)((buf[5] & 0xF0) >> 4);
973 coeff = (buf[4] & 0x20) >> 5;
974 cc = (coeff ? 3 : 1);
975 prop.ratem = cc * (1 << exp);
976 if (i > 9)
977 ret = slim_define_ch(&sat->satcl, &prop, chh, len - 8,
978 true, &sat->satch[buf[8]]);
979 else
980 ret = slim_define_ch(&sat->satcl, &prop,
981 &sat->satch[buf[8]], 1, false,
982 NULL);
983 dev_dbg(dev->dev, "define sat grp returned:%d", ret);
984
985 /* part of group so activating 1 will take care of rest */
986 if (mc == SLIM_USR_MC_DEF_ACT_CHAN)
987 ret = slim_control_ch(&sat->satcl,
988 sat->satch[buf[8]],
989 SLIM_CH_ACTIVATE, false);
990 }
991 return ret;
992}
993
994static void msm_slim_rxwq(struct msm_slim_ctrl *dev)
995{
996 u8 buf[40];
997 u8 mc, mt, len;
998 int i, ret;
999 if ((msm_slim_rx_dequeue(dev, (u8 *)buf)) != -ENODATA) {
1000 len = buf[0] & 0x1F;
1001 mt = (buf[0] >> 5) & 0x7;
1002 mc = buf[1];
1003 if (mt == SLIM_MSG_MT_CORE &&
1004 mc == SLIM_MSG_MC_REPORT_PRESENT) {
1005 u8 laddr;
1006 u8 e_addr[6];
1007 for (i = 0; i < 6; i++)
1008 e_addr[i] = buf[7-i];
1009
1010 ret = slim_assign_laddr(&dev->ctrl, e_addr, 6, &laddr);
1011 /* Is this Qualcomm ported generic device? */
1012 if (!ret && e_addr[5] == QC_MFGID_LSB &&
1013 e_addr[4] == QC_MFGID_MSB &&
1014 e_addr[1] == QC_DEVID_PGD &&
1015 e_addr[2] != QC_CHIPID_SL)
1016 dev->pgdla = laddr;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001017 if (!ret && !pm_runtime_enabled(dev->dev) &&
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001018 laddr == (QC_MSM_DEVS - 1))
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001019 pm_runtime_enable(dev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020
1021 } else if (mc == SLIM_MSG_MC_REPLY_INFORMATION ||
1022 mc == SLIM_MSG_MC_REPLY_VALUE) {
1023 u8 tid = buf[3];
1024 dev_dbg(dev->dev, "tid:%d, len:%d\n", tid, len - 4);
1025 slim_msg_response(&dev->ctrl, &buf[4], tid,
1026 len - 4);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001027 pm_runtime_mark_last_busy(dev->dev);
Sagar Dharia144e5e02011-08-08 17:30:11 -06001028 } else if (mc == SLIM_MSG_MC_REPORT_INFORMATION) {
1029 u8 l_addr = buf[2];
1030 u16 ele = (u16)buf[4] << 4;
1031 ele |= ((buf[3] & 0xf0) >> 4);
1032 dev_err(dev->dev, "Slim-dev:%d report inf element:0x%x",
1033 l_addr, ele);
1034 for (i = 0; i < len - 5; i++)
1035 dev_err(dev->dev, "offset:0x%x:bit mask:%x",
1036 i, buf[i+5]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037 } else {
1038 dev_err(dev->dev, "unexpected message:mc:%x, mt:%x",
1039 mc, mt);
1040 for (i = 0; i < len; i++)
1041 dev_err(dev->dev, "error msg: %x", buf[i]);
1042
1043 }
1044 } else
1045 dev_err(dev->dev, "rxwq called and no dequeue");
1046}
1047
1048static void slim_sat_rxprocess(struct work_struct *work)
1049{
1050 struct msm_slim_sat *sat = container_of(work, struct msm_slim_sat, wd);
1051 struct msm_slim_ctrl *dev = sat->dev;
1052 u8 buf[40];
1053
1054 while ((msm_sat_dequeue(sat, buf)) != -ENODATA) {
1055 struct slim_msg_txn txn;
1056 int i;
1057 u8 len, mc, mt;
1058 u32 bw_sl;
1059 int ret = 0;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001060 int satv = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061 bool gen_ack = false;
1062 u8 tid;
1063 u8 wbuf[8];
1064 txn.mt = SLIM_MSG_MT_SRC_REFERRED_USER;
1065 txn.dt = SLIM_MSG_DEST_LOGICALADDR;
1066 txn.ec = 0;
1067 txn.rbuf = NULL;
1068 txn.la = sat->satcl.laddr;
1069 /* satellite handling */
1070 len = buf[0] & 0x1F;
1071 mc = buf[1];
1072 mt = (buf[0] >> 5) & 0x7;
1073
1074 if (mt == SLIM_MSG_MT_CORE &&
1075 mc == SLIM_MSG_MC_REPORT_PRESENT) {
1076 u8 laddr;
1077 u8 e_addr[6];
1078 for (i = 0; i < 6; i++)
1079 e_addr[i] = buf[7-i];
1080
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001081 if (pm_runtime_enabled(dev->dev)) {
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001082 satv = msm_slim_get_ctrl(dev);
1083 if (satv >= 0)
1084 sat->pending_capability = true;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001085 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086 slim_assign_laddr(&dev->ctrl, e_addr, 6, &laddr);
1087 sat->satcl.laddr = laddr;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001088 } else if (mt != SLIM_MSG_MT_CORE &&
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001089 mc != SLIM_MSG_MC_REPORT_PRESENT) {
1090 satv = msm_slim_get_ctrl(dev);
1091 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 switch (mc) {
1093 case SLIM_MSG_MC_REPORT_PRESENT:
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001094 /* Remove runtime_pm vote once satellite acks */
1095 if (mt != SLIM_MSG_MT_CORE) {
1096 if (pm_runtime_enabled(dev->dev) &&
1097 sat->pending_capability) {
1098 msm_slim_put_ctrl(dev);
1099 sat->pending_capability = false;
1100 }
1101 continue;
1102 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001103 /* send a Manager capability msg */
1104 if (sat->sent_capability)
1105 continue;
1106 ret = slim_add_device(&dev->ctrl, &sat->satcl);
1107 if (ret) {
1108 dev_err(dev->dev,
1109 "Satellite-init failed");
1110 continue;
1111 }
1112 /* Satellite owns first 21 channels */
1113 sat->satch = kzalloc(21 * sizeof(u16), GFP_KERNEL);
1114 sat->nsatch = 20;
1115 /* alloc all sat chans */
1116 for (i = 0; i < 21; i++)
1117 slim_alloc_ch(&sat->satcl, &sat->satch[i]);
1118 txn.mc = SLIM_USR_MC_MASTER_CAPABILITY;
1119 txn.mt = SLIM_MSG_MT_SRC_REFERRED_USER;
1120 txn.la = sat->satcl.laddr;
1121 txn.rl = 8;
1122 wbuf[0] = SAT_MAGIC_LSB;
1123 wbuf[1] = SAT_MAGIC_MSB;
1124 wbuf[2] = SAT_MSG_VER;
1125 wbuf[3] = SAT_MSG_PROT;
1126 txn.wbuf = wbuf;
1127 txn.len = 4;
1128 sat->sent_capability = true;
1129 msm_xfer_msg(&dev->ctrl, &txn);
1130 break;
1131 case SLIM_USR_MC_ADDR_QUERY:
1132 memcpy(&wbuf[1], &buf[4], 6);
1133 ret = slim_get_logical_addr(&sat->satcl,
1134 &wbuf[1], 6, &wbuf[7]);
1135 if (ret)
1136 memset(&wbuf[1], 0, 6);
1137 wbuf[0] = buf[3];
1138 txn.mc = SLIM_USR_MC_ADDR_REPLY;
1139 txn.rl = 12;
1140 txn.len = 8;
1141 txn.wbuf = wbuf;
1142 msm_xfer_msg(&dev->ctrl, &txn);
1143 break;
1144 case SLIM_USR_MC_DEFINE_CHAN:
1145 case SLIM_USR_MC_DEF_ACT_CHAN:
1146 case SLIM_USR_MC_CHAN_CTRL:
1147 if (mc != SLIM_USR_MC_CHAN_CTRL)
1148 tid = buf[7];
1149 else
1150 tid = buf[4];
1151 gen_ack = true;
1152 ret = msm_sat_define_ch(sat, buf, len, mc);
1153 if (ret) {
1154 dev_err(dev->dev,
1155 "SAT define_ch returned:%d",
1156 ret);
1157 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001158 if (!sat->pending_reconf) {
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001159 int chv = msm_slim_get_ctrl(dev);
1160 if (chv >= 0)
1161 sat->pending_reconf = true;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001162 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001163 break;
1164 case SLIM_USR_MC_RECONFIG_NOW:
1165 tid = buf[3];
1166 gen_ack = true;
1167 ret = slim_reconfigure_now(&sat->satcl);
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001168 if (sat->pending_reconf) {
1169 msm_slim_put_ctrl(dev);
1170 sat->pending_reconf = false;
1171 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001172 break;
1173 case SLIM_USR_MC_REQ_BW:
1174 /* what we get is in SLOTS */
1175 bw_sl = (u32)buf[4] << 3 |
1176 ((buf[3] & 0xE0) >> 5);
1177 sat->satcl.pending_msgsl = bw_sl;
1178 tid = buf[5];
1179 gen_ack = true;
1180 break;
1181 case SLIM_USR_MC_CONNECT_SRC:
1182 case SLIM_USR_MC_CONNECT_SINK:
1183 if (mc == SLIM_USR_MC_CONNECT_SRC)
1184 txn.mc = SLIM_MSG_MC_CONNECT_SOURCE;
1185 else
1186 txn.mc = SLIM_MSG_MC_CONNECT_SINK;
1187 wbuf[0] = buf[4] & 0x1F;
1188 wbuf[1] = buf[5];
1189 tid = buf[6];
1190 txn.la = buf[3];
1191 txn.mt = SLIM_MSG_MT_CORE;
1192 txn.rl = 6;
1193 txn.len = 2;
1194 txn.wbuf = wbuf;
1195 gen_ack = true;
1196 ret = msm_xfer_msg(&dev->ctrl, &txn);
1197 break;
1198 case SLIM_USR_MC_DISCONNECT_PORT:
1199 txn.mc = SLIM_MSG_MC_DISCONNECT_PORT;
1200 wbuf[0] = buf[4] & 0x1F;
1201 tid = buf[5];
1202 txn.la = buf[3];
1203 txn.rl = 5;
1204 txn.len = 1;
1205 txn.mt = SLIM_MSG_MT_CORE;
1206 txn.wbuf = wbuf;
1207 gen_ack = true;
1208 ret = msm_xfer_msg(&dev->ctrl, &txn);
1209 default:
1210 break;
1211 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001212 if (!gen_ack) {
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001213 if (mc != SLIM_MSG_MC_REPORT_PRESENT && satv >= 0)
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001214 msm_slim_put_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215 continue;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001216 }
1217
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218 wbuf[0] = tid;
1219 if (!ret)
1220 wbuf[1] = MSM_SAT_SUCCSS;
1221 else
1222 wbuf[1] = 0;
1223 txn.mc = SLIM_USR_MC_GENERIC_ACK;
1224 txn.la = sat->satcl.laddr;
1225 txn.rl = 6;
1226 txn.len = 2;
1227 txn.wbuf = wbuf;
1228 txn.mt = SLIM_MSG_MT_SRC_REFERRED_USER;
1229 msm_xfer_msg(&dev->ctrl, &txn);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001230 if (satv >= 0)
1231 msm_slim_put_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001232 }
1233}
1234
1235static void
1236msm_slim_rx_msgq_event(struct msm_slim_ctrl *dev, struct sps_event_notify *ev)
1237{
1238 u32 *buf = ev->data.transfer.user;
1239 struct sps_iovec *iovec = &ev->data.transfer.iovec;
1240
1241 /*
1242 * Note the virtual address needs to be offset by the same index
1243 * as the physical address or just pass in the actual virtual address
1244 * if the sps_mem_buffer is not needed. Note that if completion is
1245 * used, the virtual address won't be available and will need to be
1246 * calculated based on the offset of the physical address
1247 */
1248 if (ev->event_id == SPS_EVENT_DESC_DONE) {
1249
1250 pr_debug("buf = 0x%p, data = 0x%x\n", buf, *buf);
1251
1252 pr_debug("iovec = (0x%x 0x%x 0x%x)\n",
1253 iovec->addr, iovec->size, iovec->flags);
1254
1255 } else {
1256 dev_err(dev->dev, "%s: unknown event %d\n",
1257 __func__, ev->event_id);
1258 }
1259}
1260
1261static void msm_slim_rx_msgq_cb(struct sps_event_notify *notify)
1262{
1263 struct msm_slim_ctrl *dev = (struct msm_slim_ctrl *)notify->user;
1264 msm_slim_rx_msgq_event(dev, notify);
1265}
1266
1267/* Queue up Rx message buffer */
1268static inline int
1269msm_slim_post_rx_msgq(struct msm_slim_ctrl *dev, int ix)
1270{
1271 int ret;
1272 u32 flags = SPS_IOVEC_FLAG_INT;
1273 struct msm_slim_endp *endpoint = &dev->rx_msgq;
1274 struct sps_mem_buffer *mem = &endpoint->buf;
1275 struct sps_pipe *pipe = endpoint->sps;
1276
1277 /* Rx message queue buffers are 4 bytes in length */
1278 u8 *virt_addr = mem->base + (4 * ix);
1279 u32 phys_addr = mem->phys_base + (4 * ix);
1280
1281 pr_debug("index:%d, phys:0x%x, virt:0x%p\n", ix, phys_addr, virt_addr);
1282
1283 ret = sps_transfer_one(pipe, phys_addr, 4, virt_addr, flags);
1284 if (ret)
1285 dev_err(dev->dev, "transfer_one() failed 0x%x, %d\n", ret, ix);
1286
1287 return ret;
1288}
1289
1290static inline int
1291msm_slim_rx_msgq_get(struct msm_slim_ctrl *dev, u32 *data, int offset)
1292{
1293 struct msm_slim_endp *endpoint = &dev->rx_msgq;
1294 struct sps_mem_buffer *mem = &endpoint->buf;
1295 struct sps_pipe *pipe = endpoint->sps;
1296 struct sps_iovec iovec;
1297 int index;
1298 int ret;
1299
1300 ret = sps_get_iovec(pipe, &iovec);
1301 if (ret) {
1302 dev_err(dev->dev, "sps_get_iovec() failed 0x%x\n", ret);
1303 goto err_exit;
1304 }
1305
1306 pr_debug("iovec = (0x%x 0x%x 0x%x)\n",
1307 iovec.addr, iovec.size, iovec.flags);
1308 BUG_ON(iovec.addr < mem->phys_base);
1309 BUG_ON(iovec.addr >= mem->phys_base + mem->size);
1310
1311 /* Calculate buffer index */
1312 index = (iovec.addr - mem->phys_base) / 4;
1313 *(data + offset) = *((u32 *)mem->base + index);
1314
1315 pr_debug("buf = 0x%p, data = 0x%x\n", (u32 *)mem->base + index, *data);
1316
1317 /* Add buffer back to the queue */
1318 (void)msm_slim_post_rx_msgq(dev, index);
1319
1320err_exit:
1321 return ret;
1322}
1323
1324static int msm_slim_rx_msgq_thread(void *data)
1325{
1326 struct msm_slim_ctrl *dev = (struct msm_slim_ctrl *)data;
1327 struct completion *notify = &dev->rx_msgq_notify;
1328 struct msm_slim_sat *sat = NULL;
1329 u32 mc = 0;
1330 u32 mt = 0;
1331 u32 buffer[10];
1332 int index = 0;
1333 u8 msg_len = 0;
1334 int ret;
1335
1336 dev_dbg(dev->dev, "rx thread started");
1337
1338 while (!kthread_should_stop()) {
1339 set_current_state(TASK_INTERRUPTIBLE);
1340 ret = wait_for_completion_interruptible(notify);
1341
1342 if (ret)
1343 dev_err(dev->dev, "rx thread wait error:%d", ret);
1344
1345 /* 1 irq notification per message */
1346 if (!dev->use_rx_msgqs) {
1347 msm_slim_rxwq(dev);
1348 continue;
1349 }
1350
1351 ret = msm_slim_rx_msgq_get(dev, buffer, index);
1352 if (ret) {
1353 dev_err(dev->dev, "rx_msgq_get() failed 0x%x\n", ret);
1354 continue;
1355 }
1356
1357 pr_debug("message[%d] = 0x%x\n", index, *buffer);
1358
1359 /* Decide if we use generic RX or satellite RX */
1360 if (index++ == 0) {
1361 msg_len = *buffer & 0x1F;
1362 pr_debug("Start of new message, len = %d\n", msg_len);
1363 mt = (buffer[0] >> 5) & 0x7;
1364 mc = (buffer[0] >> 8) & 0xff;
1365 dev_dbg(dev->dev, "MC: %x, MT: %x\n", mc, mt);
1366 if (mt == SLIM_MSG_MT_DEST_REFERRED_USER ||
1367 mt == SLIM_MSG_MT_SRC_REFERRED_USER)
1368 sat = dev->satd;
1369
1370 } else if ((index * 4) >= msg_len) {
1371 index = 0;
1372 if (mt == SLIM_MSG_MT_CORE &&
1373 mc == SLIM_MSG_MC_REPORT_PRESENT) {
1374 u8 e_addr[6];
1375 msm_get_eaddr(e_addr, buffer);
1376 if (msm_is_sat_dev(e_addr))
1377 sat = dev->satd;
1378 }
1379 if (sat) {
1380 msm_sat_enqueue(sat, buffer, msg_len);
1381 queue_work(sat->wq, &sat->wd);
1382 sat = NULL;
1383 } else {
1384 msm_slim_rx_enqueue(dev, buffer, msg_len);
1385 msm_slim_rxwq(dev);
1386 }
1387 }
1388 }
1389
1390 return 0;
1391}
1392
1393static int __devinit msm_slim_init_rx_msgq(struct msm_slim_ctrl *dev)
1394{
1395 int i, ret;
1396 u32 pipe_offset;
1397 struct msm_slim_endp *endpoint = &dev->rx_msgq;
1398 struct sps_connect *config = &endpoint->config;
1399 struct sps_mem_buffer *descr = &config->desc;
1400 struct sps_mem_buffer *mem = &endpoint->buf;
1401 struct completion *notify = &dev->rx_msgq_notify;
1402
1403 struct sps_register_event sps_error_event; /* SPS_ERROR */
1404 struct sps_register_event sps_descr_event; /* DESCR_DONE */
1405
1406 /* Allocate the endpoint */
1407 ret = msm_slim_init_endpoint(dev, endpoint);
1408 if (ret) {
1409 dev_err(dev->dev, "init_endpoint failed 0x%x\n", ret);
1410 goto sps_init_endpoint_failed;
1411 }
1412
1413 /* Get the pipe indices for the message queues */
1414 pipe_offset = (readl_relaxed(dev->base + MGR_STATUS) & 0xfc) >> 2;
1415 dev_dbg(dev->dev, "Message queue pipe offset %d\n", pipe_offset);
1416
1417 config->mode = SPS_MODE_SRC;
1418 config->source = dev->bam.hdl;
1419 config->destination = SPS_DEV_HANDLE_MEM;
1420 config->src_pipe_index = pipe_offset;
1421 config->options = SPS_O_DESC_DONE | SPS_O_ERROR |
1422 SPS_O_ACK_TRANSFERS | SPS_O_AUTO_ENABLE;
1423
1424 /* Allocate memory for the FIFO descriptors */
1425 ret = msm_slim_sps_mem_alloc(dev, descr,
1426 MSM_SLIM_DESC_NUM * sizeof(struct sps_iovec));
1427 if (ret) {
1428 dev_err(dev->dev, "unable to allocate SPS descriptors\n");
1429 goto alloc_descr_failed;
1430 }
1431
1432 ret = sps_connect(endpoint->sps, config);
1433 if (ret) {
1434 dev_err(dev->dev, "sps_connect failed 0x%x\n", ret);
1435 goto sps_connect_failed;
1436 }
1437
1438 /* Register completion for DESC_DONE */
1439 init_completion(notify);
1440 memset(&sps_descr_event, 0x00, sizeof(sps_descr_event));
1441
1442 sps_descr_event.mode = SPS_TRIGGER_CALLBACK;
1443 sps_descr_event.options = SPS_O_DESC_DONE;
1444 sps_descr_event.user = (void *)dev;
1445 sps_descr_event.xfer_done = notify;
1446
1447 ret = sps_register_event(endpoint->sps, &sps_descr_event);
1448 if (ret) {
1449 dev_err(dev->dev, "sps_connect() failed 0x%x\n", ret);
1450 goto sps_reg_event_failed;
1451 }
1452
1453 /* Register callback for errors */
1454 memset(&sps_error_event, 0x00, sizeof(sps_error_event));
1455 sps_error_event.mode = SPS_TRIGGER_CALLBACK;
1456 sps_error_event.options = SPS_O_ERROR;
1457 sps_error_event.user = (void *)dev;
1458 sps_error_event.callback = msm_slim_rx_msgq_cb;
1459
1460 ret = sps_register_event(endpoint->sps, &sps_error_event);
1461 if (ret) {
1462 dev_err(dev->dev, "sps_connect() failed 0x%x\n", ret);
1463 goto sps_reg_event_failed;
1464 }
1465
1466 /* Allocate memory for the message buffer(s), N descrs, 4-byte mesg */
1467 ret = msm_slim_sps_mem_alloc(dev, mem, MSM_SLIM_DESC_NUM * 4);
1468 if (ret) {
1469 dev_err(dev->dev, "dma_alloc_coherent failed\n");
1470 goto alloc_buffer_failed;
1471 }
1472
1473 /*
1474 * Call transfer_one for each 4-byte buffer
1475 * Use (buf->size/4) - 1 for the number of buffer to post
1476 */
1477
1478 /* Setup the transfer */
1479 for (i = 0; i < (MSM_SLIM_DESC_NUM - 1); i++) {
1480 ret = msm_slim_post_rx_msgq(dev, i);
1481 if (ret) {
1482 dev_err(dev->dev, "post_rx_msgq() failed 0x%x\n", ret);
1483 goto sps_transfer_failed;
1484 }
1485 }
1486
1487 /* Fire up the Rx message queue thread */
1488 dev->rx_msgq_thread = kthread_run(msm_slim_rx_msgq_thread, dev,
1489 MSM_SLIM_NAME "_rx_msgq_thread");
1490 if (!dev->rx_msgq_thread) {
1491 dev_err(dev->dev, "Failed to start Rx message queue thread\n");
1492 ret = -EIO;
1493 } else
1494 return 0;
1495
1496sps_transfer_failed:
1497 msm_slim_sps_mem_free(dev, mem);
1498alloc_buffer_failed:
1499 memset(&sps_error_event, 0x00, sizeof(sps_error_event));
1500 sps_register_event(endpoint->sps, &sps_error_event);
1501sps_reg_event_failed:
1502 sps_disconnect(endpoint->sps);
1503sps_connect_failed:
1504 msm_slim_sps_mem_free(dev, descr);
1505alloc_descr_failed:
1506 msm_slim_free_endpoint(endpoint);
1507sps_init_endpoint_failed:
1508 return ret;
1509}
1510
1511/* Registers BAM h/w resource with SPS driver and initializes msgq endpoints */
1512static int __devinit
1513msm_slim_sps_init(struct msm_slim_ctrl *dev, struct resource *bam_mem)
1514{
1515 int i, ret;
1516 u32 bam_handle;
1517 struct sps_bam_props bam_props = {0};
1518
1519 static struct sps_bam_sec_config_props sec_props = {
1520 .ees = {
1521 [0] = { /* LPASS */
1522 .vmid = 0,
1523 .pipe_mask = 0xFFFF98,
1524 },
1525 [1] = { /* Krait Apps */
1526 .vmid = 1,
1527 .pipe_mask = 0x3F000007,
1528 },
1529 [2] = { /* Modem */
1530 .vmid = 2,
1531 .pipe_mask = 0x00000060,
1532 },
1533 },
1534 };
1535
1536 bam_props.ee = dev->ee;
1537 bam_props.virt_addr = dev->bam.base;
1538 bam_props.phys_addr = bam_mem->start;
1539 bam_props.irq = dev->bam.irq;
1540 bam_props.manage = SPS_BAM_MGR_LOCAL;
1541 bam_props.summing_threshold = MSM_SLIM_PERF_SUMM_THRESHOLD;
1542
1543 bam_props.sec_config = SPS_BAM_SEC_DO_CONFIG;
1544 bam_props.p_sec_config_props = &sec_props;
1545
1546 bam_props.options = SPS_O_DESC_DONE | SPS_O_ERROR |
1547 SPS_O_ACK_TRANSFERS | SPS_O_AUTO_ENABLE;
1548
1549 /* First 7 bits are for message Qs */
1550 for (i = 7; i < 32; i++) {
1551 /* Check what pipes are owned by Apps. */
1552 if ((sec_props.ees[dev->ee].pipe_mask >> i) & 0x1)
1553 break;
1554 }
1555 dev->pipe_b = i - 7;
1556
1557 /* Register the BAM device with the SPS driver */
1558 ret = sps_register_bam_device(&bam_props, &bam_handle);
1559 if (ret) {
1560 dev_err(dev->dev, "sps_register_bam_device failed 0x%x\n", ret);
1561 return ret;
1562 }
1563 dev->bam.hdl = bam_handle;
1564 dev_dbg(dev->dev, "SLIM BAM registered, handle = 0x%x\n", bam_handle);
1565
1566 ret = msm_slim_init_rx_msgq(dev);
1567 if (ret) {
1568 dev_err(dev->dev, "msm_slim_init_rx_msgq failed 0x%x\n", ret);
1569 goto rx_msgq_init_failed;
1570 }
1571
1572 return 0;
1573rx_msgq_init_failed:
1574 sps_deregister_bam_device(bam_handle);
1575 dev->bam.hdl = 0L;
1576 return ret;
1577}
1578
1579static void msm_slim_sps_exit(struct msm_slim_ctrl *dev)
1580{
1581 if (dev->use_rx_msgqs) {
1582 struct msm_slim_endp *endpoint = &dev->rx_msgq;
1583 struct sps_connect *config = &endpoint->config;
1584 struct sps_mem_buffer *descr = &config->desc;
1585 struct sps_mem_buffer *mem = &endpoint->buf;
1586 struct sps_register_event sps_event;
1587 memset(&sps_event, 0x00, sizeof(sps_event));
1588 msm_slim_sps_mem_free(dev, mem);
1589 sps_register_event(endpoint->sps, &sps_event);
1590 sps_disconnect(endpoint->sps);
1591 msm_slim_sps_mem_free(dev, descr);
1592 msm_slim_free_endpoint(endpoint);
1593 }
1594 sps_deregister_bam_device(dev->bam.hdl);
1595}
1596
Sagar Dhariacc969452011-09-19 10:34:30 -06001597static void msm_slim_prg_slew(struct platform_device *pdev,
1598 struct msm_slim_ctrl *dev)
1599{
1600 struct resource *slew_io;
1601 void __iomem *slew_reg;
1602 /* SLEW RATE register for this slimbus */
1603 dev->slew_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1604 "slimbus_slew_reg");
1605 if (!dev->slew_mem) {
1606 dev_dbg(&pdev->dev, "no slimbus slew resource\n");
1607 return;
1608 }
1609 slew_io = request_mem_region(dev->slew_mem->start,
1610 resource_size(dev->slew_mem), pdev->name);
1611 if (!slew_io) {
1612 dev_dbg(&pdev->dev, "slimbus-slew mem claimed\n");
1613 dev->slew_mem = NULL;
1614 return;
1615 }
1616
1617 slew_reg = ioremap(dev->slew_mem->start, resource_size(dev->slew_mem));
1618 if (!slew_reg) {
1619 dev_dbg(dev->dev, "slew register mapping failed");
1620 release_mem_region(dev->slew_mem->start,
1621 resource_size(dev->slew_mem));
1622 dev->slew_mem = NULL;
1623 return;
1624 }
1625 writel_relaxed(1, slew_reg);
1626 /* Make sure slimbus-slew rate enabling goes through */
1627 wmb();
1628 iounmap(slew_reg);
1629}
1630
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001631static int __devinit msm_slim_probe(struct platform_device *pdev)
1632{
1633 struct msm_slim_ctrl *dev;
1634 int ret;
1635 struct resource *bam_mem, *bam_io;
1636 struct resource *slim_mem, *slim_io;
1637 struct resource *irq, *bam_irq;
1638 slim_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1639 "slimbus_physical");
1640 if (!slim_mem) {
1641 dev_err(&pdev->dev, "no slimbus physical memory resource\n");
1642 return -ENODEV;
1643 }
1644 slim_io = request_mem_region(slim_mem->start, resource_size(slim_mem),
1645 pdev->name);
1646 if (!slim_io) {
1647 dev_err(&pdev->dev, "slimbus memory already claimed\n");
1648 return -EBUSY;
1649 }
1650
1651 bam_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1652 "slimbus_bam_physical");
1653 if (!bam_mem) {
1654 dev_err(&pdev->dev, "no slimbus BAM memory resource\n");
1655 ret = -ENODEV;
1656 goto err_get_res_bam_failed;
1657 }
1658 bam_io = request_mem_region(bam_mem->start, resource_size(bam_mem),
1659 pdev->name);
1660 if (!bam_io) {
1661 release_mem_region(slim_mem->start, resource_size(slim_mem));
1662 dev_err(&pdev->dev, "slimbus BAM memory already claimed\n");
1663 ret = -EBUSY;
1664 goto err_get_res_bam_failed;
1665 }
1666 irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1667 "slimbus_irq");
1668 if (!irq) {
1669 dev_err(&pdev->dev, "no slimbus IRQ resource\n");
1670 ret = -ENODEV;
1671 goto err_get_res_failed;
1672 }
1673 bam_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1674 "slimbus_bam_irq");
1675 if (!bam_irq) {
1676 dev_err(&pdev->dev, "no slimbus BAM IRQ resource\n");
1677 ret = -ENODEV;
1678 goto err_get_res_failed;
1679 }
1680
1681 dev = kzalloc(sizeof(struct msm_slim_ctrl), GFP_KERNEL);
1682 if (!dev) {
1683 dev_err(&pdev->dev, "no memory for MSM slimbus controller\n");
1684 ret = -ENOMEM;
1685 goto err_get_res_failed;
1686 }
1687 dev->dev = &pdev->dev;
1688 platform_set_drvdata(pdev, dev);
1689 slim_set_ctrldata(&dev->ctrl, dev);
1690 dev->base = ioremap(slim_mem->start, resource_size(slim_mem));
1691 if (!dev->base) {
1692 dev_err(&pdev->dev, "IOremap failed\n");
1693 ret = -ENOMEM;
1694 goto err_ioremap_failed;
1695 }
1696 dev->bam.base = ioremap(bam_mem->start, resource_size(bam_mem));
1697 if (!dev->bam.base) {
1698 dev_err(&pdev->dev, "BAM IOremap failed\n");
1699 ret = -ENOMEM;
1700 goto err_ioremap_bam_failed;
1701 }
1702 dev->ctrl.nr = pdev->id;
1703 dev->ctrl.nchans = MSM_SLIM_NCHANS;
1704 dev->ctrl.nports = MSM_SLIM_NPORTS;
1705 dev->ctrl.set_laddr = msm_set_laddr;
1706 dev->ctrl.xfer_msg = msm_xfer_msg;
Sagar Dharia144e5e02011-08-08 17:30:11 -06001707 dev->ctrl.wakeup = msm_clk_pause_wakeup;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001708 dev->ctrl.config_port = msm_config_port;
1709 dev->ctrl.port_xfer = msm_slim_port_xfer;
1710 dev->ctrl.port_xfer_status = msm_slim_port_xfer_status;
1711 /* Reserve some messaging BW for satellite-apps driver communication */
1712 dev->ctrl.sched.pending_msgsl = 30;
1713
1714 init_completion(&dev->reconf);
1715 mutex_init(&dev->tx_lock);
1716 spin_lock_init(&dev->rx_lock);
1717 dev->ee = 1;
1718 dev->use_rx_msgqs = 1;
1719 dev->irq = irq->start;
1720 dev->bam.irq = bam_irq->start;
1721
1722 ret = msm_slim_sps_init(dev, bam_mem);
1723 if (ret != 0) {
1724 dev_err(dev->dev, "error SPS init\n");
1725 goto err_sps_init_failed;
1726 }
1727
1728
1729 dev->rclk = clk_get(dev->dev, "audio_slimbus_clk");
Sagar Dhariacc969452011-09-19 10:34:30 -06001730 if (!dev->rclk) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001731 dev_err(dev->dev, "slimbus clock not found");
1732 goto err_clk_get_failed;
1733 }
1734 dev->framer.rootfreq = SLIM_ROOT_FREQ >> 3;
1735 dev->framer.superfreq =
1736 dev->framer.rootfreq / SLIM_CL_PER_SUPERFRAME_DIV8;
1737 dev->ctrl.a_framer = &dev->framer;
1738 dev->ctrl.clkgear = SLIM_MAX_CLK_GEAR;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001739 dev->ctrl.dev.parent = &pdev->dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001740
1741 ret = request_irq(dev->irq, msm_slim_interrupt, IRQF_TRIGGER_HIGH,
1742 "msm_slim_irq", dev);
1743 if (ret) {
1744 dev_err(&pdev->dev, "request IRQ failed\n");
1745 goto err_request_irq_failed;
1746 }
1747
1748 dev->satd = kzalloc(sizeof(struct msm_slim_sat), GFP_KERNEL);
1749 if (!dev->satd) {
1750 ret = -ENOMEM;
1751 goto err_sat_failed;
1752 }
Sagar Dhariacc969452011-09-19 10:34:30 -06001753
1754 msm_slim_prg_slew(pdev, dev);
1755 clk_set_rate(dev->rclk, SLIM_ROOT_FREQ);
1756 clk_enable(dev->rclk);
1757
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001758 dev->satd->dev = dev;
1759 dev->satd->satcl.name = "msm_sat_dev";
1760 spin_lock_init(&dev->satd->lock);
1761 INIT_WORK(&dev->satd->wd, slim_sat_rxprocess);
1762 dev->satd->wq = create_singlethread_workqueue("msm_slim_sat");
1763 /* Component register initialization */
1764 writel_relaxed(1, dev->base + COMP_CFG);
1765 writel_relaxed((EE_MGR_RSC_GRP | EE_NGD_2 | EE_NGD_1),
1766 dev->base + COMP_TRUST_CFG);
1767
1768 /*
1769 * Manager register initialization
1770 * If RX msg Q is used, disable RX_MSG_RCVD interrupt
1771 */
1772 if (dev->use_rx_msgqs)
1773 writel_relaxed((MGR_INT_RECFG_DONE | MGR_INT_TX_NACKED_2 |
1774 MGR_INT_MSG_BUF_CONTE | /* MGR_INT_RX_MSG_RCVD | */
1775 MGR_INT_TX_MSG_SENT), dev->base + MGR_INT_EN);
1776 else
1777 writel_relaxed((MGR_INT_RECFG_DONE | MGR_INT_TX_NACKED_2 |
1778 MGR_INT_MSG_BUF_CONTE | MGR_INT_RX_MSG_RCVD |
1779 MGR_INT_TX_MSG_SENT), dev->base + MGR_INT_EN);
1780 writel_relaxed(1, dev->base + MGR_CFG);
1781 /*
1782 * Framer registers are beyond 1K memory region after Manager and/or
1783 * component registers. Make sure those writes are ordered
1784 * before framer register writes
1785 */
1786 wmb();
1787
Sagar Dharia72007922011-12-13 21:14:26 -07001788 /* Register with framework before enabling frame, clock */
1789 ret = slim_add_numbered_controller(&dev->ctrl);
1790 if (ret) {
1791 dev_err(dev->dev, "error adding controller\n");
1792 goto err_ctrl_failed;
1793 }
1794
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001795 /* Framer register initialization */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001796 writel_relaxed((0xA << REF_CLK_GEAR) | (0xA << CLK_GEAR) |
1797 (1 << ROOT_FREQ) | (1 << FRM_ACTIVE) | 1,
1798 dev->base + FRM_CFG);
1799 /*
1800 * Make sure that framer wake-up and enabling writes go through
1801 * before any other component is enabled. Framer is responsible for
1802 * clocking the bus and enabling framer first will ensure that other
1803 * devices can report presence when they are enabled
1804 */
1805 mb();
1806
1807 /* Enable RX msg Q */
1808 if (dev->use_rx_msgqs)
1809 writel_relaxed(MGR_CFG_ENABLE | MGR_CFG_RX_MSGQ_EN,
1810 dev->base + MGR_CFG);
1811 else
1812 writel_relaxed(MGR_CFG_ENABLE, dev->base + MGR_CFG);
1813 /*
1814 * Make sure that manager-enable is written through before interface
1815 * device is enabled
1816 */
1817 mb();
1818 writel_relaxed(1, dev->base + INTF_CFG);
1819 /*
1820 * Make sure that interface-enable is written through before enabling
1821 * ported generic device inside MSM manager
1822 */
1823 mb();
1824 writel_relaxed(1, dev->base + PGD_CFG);
1825 writel_relaxed(0x3F<<17, dev->base + (PGD_OWN_EEn + (4 * dev->ee)));
1826 /*
1827 * Make sure that ported generic device is enabled and port-EE settings
1828 * are written through before finally enabling the component
1829 */
1830 mb();
1831
1832 writel_relaxed(1, dev->base + COMP_CFG);
1833 /*
1834 * Make sure that all writes have gone through before exiting this
1835 * function
1836 */
1837 mb();
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001838 pm_runtime_use_autosuspend(&pdev->dev);
1839 pm_runtime_set_autosuspend_delay(&pdev->dev, MSM_SLIM_AUTOSUSPEND);
1840 pm_runtime_set_active(&pdev->dev);
1841
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842 dev_dbg(dev->dev, "MSM SB controller is up!\n");
1843 return 0;
1844
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001845err_ctrl_failed:
1846 writel_relaxed(0, dev->base + COMP_CFG);
1847 kfree(dev->satd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001848err_sat_failed:
1849 free_irq(dev->irq, dev);
1850err_request_irq_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001851 clk_disable(dev->rclk);
1852 clk_put(dev->rclk);
1853err_clk_get_failed:
1854 msm_slim_sps_exit(dev);
1855err_sps_init_failed:
1856 iounmap(dev->bam.base);
1857err_ioremap_bam_failed:
1858 iounmap(dev->base);
1859err_ioremap_failed:
1860 kfree(dev);
1861err_get_res_failed:
1862 release_mem_region(bam_mem->start, resource_size(bam_mem));
1863err_get_res_bam_failed:
1864 release_mem_region(slim_mem->start, resource_size(slim_mem));
1865 return ret;
1866}
1867
1868static int __devexit msm_slim_remove(struct platform_device *pdev)
1869{
1870 struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
1871 struct resource *bam_mem;
1872 struct resource *slim_mem;
Sagar Dhariacc969452011-09-19 10:34:30 -06001873 struct resource *slew_mem = dev->slew_mem;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001874 struct msm_slim_sat *sat = dev->satd;
1875 slim_remove_device(&sat->satcl);
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001876 pm_runtime_disable(&pdev->dev);
1877 pm_runtime_set_suspended(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001878 kfree(sat->satch);
1879 destroy_workqueue(sat->wq);
1880 kfree(sat);
1881 free_irq(dev->irq, dev);
1882 slim_del_controller(&dev->ctrl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001883 clk_put(dev->rclk);
1884 msm_slim_sps_exit(dev);
1885 kthread_stop(dev->rx_msgq_thread);
1886 iounmap(dev->bam.base);
1887 iounmap(dev->base);
1888 kfree(dev);
1889 bam_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1890 "slimbus_bam_physical");
Sagar Dhariae77961f2011-09-27 14:03:50 -06001891 if (bam_mem)
1892 release_mem_region(bam_mem->start, resource_size(bam_mem));
Sagar Dhariacc969452011-09-19 10:34:30 -06001893 if (slew_mem)
1894 release_mem_region(slew_mem->start, resource_size(slew_mem));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001895 slim_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1896 "slimbus_physical");
Sagar Dhariae77961f2011-09-27 14:03:50 -06001897 if (slim_mem)
1898 release_mem_region(slim_mem->start, resource_size(slim_mem));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001899 return 0;
1900}
1901
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001902#ifdef CONFIG_PM_RUNTIME
1903static int msm_slim_runtime_idle(struct device *device)
1904{
1905 dev_dbg(device, "pm_runtime: idle...\n");
1906 pm_request_autosuspend(device);
1907 return -EAGAIN;
1908}
1909#endif
1910
1911/*
1912 * If PM_RUNTIME is not defined, these 2 functions become helper
1913 * functions to be called from system suspend/resume. So they are not
1914 * inside ifdef CONFIG_PM_RUNTIME
1915 */
1916static int msm_slim_runtime_suspend(struct device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001917{
1918 struct platform_device *pdev = to_platform_device(device);
1919 struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001920 int ret;
1921 dev_dbg(device, "pm_runtime: suspending...\n");
1922 dev->state = MSM_CTRL_SLEEPING;
1923 ret = slim_ctrl_clk_pause(&dev->ctrl, false, SLIM_CLK_UNSPECIFIED);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001924 if (ret) {
1925 dev_err(device, "clk pause not entered:%d", ret);
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001926 dev->state = MSM_CTRL_AWAKE;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001927 } else {
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001928 dev->state = MSM_CTRL_ASLEEP;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001929 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001930 return ret;
1931}
1932
1933static int msm_slim_runtime_resume(struct device *device)
1934{
1935 struct platform_device *pdev = to_platform_device(device);
1936 struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
1937 int ret = 0;
1938 dev_dbg(device, "pm_runtime: resuming...\n");
1939 if (dev->state == MSM_CTRL_ASLEEP)
1940 ret = slim_ctrl_clk_pause(&dev->ctrl, true, 0);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001941 if (ret) {
1942 dev_err(device, "clk pause not exited:%d", ret);
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001943 dev->state = MSM_CTRL_ASLEEP;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001944 } else {
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001945 dev->state = MSM_CTRL_AWAKE;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001946 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001947 return ret;
1948}
1949
1950#ifdef CONFIG_PM_SLEEP
1951static int msm_slim_suspend(struct device *dev)
1952{
1953 int ret = 0;
1954 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
1955 dev_dbg(dev, "system suspend");
1956 ret = msm_slim_runtime_suspend(dev);
Sagar Dharia6b559e02011-08-03 17:01:31 -06001957 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001958 if (ret == -EBUSY) {
Sagar Dharia144e5e02011-08-08 17:30:11 -06001959 /*
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001960 * If the clock pause failed due to active channels, there is
1961 * a possibility that some audio stream is active during suspend
1962 * We dont want to return suspend failure in that case so that
1963 * display and relevant components can still go to suspend.
1964 * If there is some other error, then it should be passed-on
1965 * to system level suspend
1966 */
Sagar Dharia144e5e02011-08-08 17:30:11 -06001967 ret = 0;
1968 }
1969 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001970}
1971
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001972static int msm_slim_resume(struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001973{
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001974 /* If runtime_pm is enabled, this resume shouldn't do anything */
1975 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
1976 int ret;
1977 dev_dbg(dev, "system resume");
1978 ret = msm_slim_runtime_resume(dev);
1979 if (!ret) {
1980 pm_runtime_mark_last_busy(dev);
1981 pm_request_autosuspend(dev);
1982 }
1983 return ret;
1984
Sagar Dharia144e5e02011-08-08 17:30:11 -06001985 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001986 return 0;
1987}
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001988#endif /* CONFIG_PM_SLEEP */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001989
1990static const struct dev_pm_ops msm_slim_dev_pm_ops = {
1991 SET_SYSTEM_SLEEP_PM_OPS(
1992 msm_slim_suspend,
1993 msm_slim_resume
1994 )
1995 SET_RUNTIME_PM_OPS(
1996 msm_slim_runtime_suspend,
1997 msm_slim_runtime_resume,
1998 msm_slim_runtime_idle
1999 )
2000};
2001
2002static struct platform_driver msm_slim_driver = {
2003 .probe = msm_slim_probe,
2004 .remove = msm_slim_remove,
2005 .driver = {
2006 .name = MSM_SLIM_NAME,
2007 .owner = THIS_MODULE,
2008 .pm = &msm_slim_dev_pm_ops,
2009 },
2010};
2011
2012static int msm_slim_init(void)
2013{
2014 return platform_driver_register(&msm_slim_driver);
2015}
2016subsys_initcall(msm_slim_init);
2017
2018static void msm_slim_exit(void)
2019{
2020 platform_driver_unregister(&msm_slim_driver);
2021}
2022module_exit(msm_slim_exit);
2023
2024MODULE_LICENSE("GPL v2");
2025MODULE_VERSION("0.1");
2026MODULE_DESCRIPTION("MSM Slimbus controller");
2027MODULE_ALIAS("platform:msm-slim");