blob: 84ba5f98514ad2dfb9ada9804dacb9fe7c7926d3 [file] [log] [blame]
Sagar Dharia790cfd02011-09-25 17:56:24 -06001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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>
Sagar Dhariaf8f603b2012-03-21 15:25:17 -060025#include <linux/of.h>
26#include <linux/of_slimbus.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#include <mach/sps.h>
28
29/* Per spec.max 40 bytes per received message */
30#define SLIM_RX_MSGQ_BUF_LEN 40
31
32#define SLIM_USR_MC_GENERIC_ACK 0x25
33#define SLIM_USR_MC_MASTER_CAPABILITY 0x0
34#define SLIM_USR_MC_REPORT_SATELLITE 0x1
35#define SLIM_USR_MC_ADDR_QUERY 0xD
36#define SLIM_USR_MC_ADDR_REPLY 0xE
37#define SLIM_USR_MC_DEFINE_CHAN 0x20
38#define SLIM_USR_MC_DEF_ACT_CHAN 0x21
39#define SLIM_USR_MC_CHAN_CTRL 0x23
40#define SLIM_USR_MC_RECONFIG_NOW 0x24
41#define SLIM_USR_MC_REQ_BW 0x28
42#define SLIM_USR_MC_CONNECT_SRC 0x2C
43#define SLIM_USR_MC_CONNECT_SINK 0x2D
44#define SLIM_USR_MC_DISCONNECT_PORT 0x2E
45
46/* MSM Slimbus peripheral settings */
47#define MSM_SLIM_PERF_SUMM_THRESHOLD 0x8000
48#define MSM_SLIM_NCHANS 32
49#define MSM_SLIM_NPORTS 24
Sagar Dharia45ee38a2011-08-03 17:01:31 -060050#define MSM_SLIM_AUTOSUSPEND MSEC_PER_SEC
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051
52/*
53 * Need enough descriptors to receive present messages from slaves
54 * if received simultaneously. Present message needs 3 descriptors
55 * and this size will ensure around 10 simultaneous reports.
56 */
57#define MSM_SLIM_DESC_NUM 32
58
59#define SLIM_MSG_ASM_FIRST_WORD(l, mt, mc, dt, ad) \
60 ((l) | ((mt) << 5) | ((mc) << 8) | ((dt) << 15) | ((ad) << 16))
61
62#define MSM_SLIM_NAME "msm_slim_ctrl"
63#define SLIM_ROOT_FREQ 24576000
64
65#define MSM_CONCUR_MSG 8
66#define SAT_CONCUR_MSG 8
67#define DEF_WATERMARK (8 << 1)
68#define DEF_ALIGN 0
69#define DEF_PACK (1 << 6)
70#define ENABLE_PORT 1
71
72#define DEF_BLKSZ 0
73#define DEF_TRANSZ 0
74
75#define SAT_MAGIC_LSB 0xD9
76#define SAT_MAGIC_MSB 0xC5
77#define SAT_MSG_VER 0x1
78#define SAT_MSG_PROT 0x1
79#define MSM_SAT_SUCCSS 0x20
Sagar Dharia790cfd02011-09-25 17:56:24 -060080#define MSM_MAX_NSATS 2
Sagar Dharia0ffdca12011-09-25 18:55:53 -060081#define MSM_MAX_SATCH 32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082
83#define QC_MFGID_LSB 0x2
84#define QC_MFGID_MSB 0x17
85#define QC_CHIPID_SL 0x10
86#define QC_DEVID_SAT1 0x3
87#define QC_DEVID_SAT2 0x4
88#define QC_DEVID_PGD 0x5
Sagar Dharia45ee38a2011-08-03 17:01:31 -060089#define QC_MSM_DEVS 5
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090
Sagar Dharia82e516f2012-03-16 16:01:23 -060091#define PGD_THIS_EE(r, v) ((v) ? PGD_THIS_EE_V2(r) : PGD_THIS_EE_V1(r))
92#define PGD_PORT(r, p, v) ((v) ? PGD_PORT_V2(r, p) : PGD_PORT_V1(r, p))
93#define CFG_PORT(r, v) ((v) ? CFG_PORT_V2(r) : CFG_PORT_V1(r))
94
95#define PGD_THIS_EE_V2(r) (dev->base + (r ## _V2) + (dev->ee * 0x1000))
96#define PGD_PORT_V2(r, p) (dev->base + (r ## _V2) + ((p) * 0x1000))
97#define CFG_PORT_V2(r) ((r ## _V2))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098/* Component registers */
Sagar Dharia82e516f2012-03-16 16:01:23 -060099enum comp_reg_v2 {
100 COMP_CFG_V2 = 4,
101 COMP_TRUST_CFG_V2 = 0x3000,
102};
103
104/* Manager PGD registers */
105enum pgd_reg_v2 {
106 PGD_CFG_V2 = 0x800,
107 PGD_STAT_V2 = 0x804,
108 PGD_INT_EN_V2 = 0x810,
109 PGD_INT_STAT_V2 = 0x814,
110 PGD_INT_CLR_V2 = 0x818,
111 PGD_OWN_EEn_V2 = 0x300C,
112 PGD_PORT_INT_EN_EEn_V2 = 0x5000,
113 PGD_PORT_INT_ST_EEn_V2 = 0x5004,
114 PGD_PORT_INT_CL_EEn_V2 = 0x5008,
115 PGD_PORT_CFGn_V2 = 0x14000,
116 PGD_PORT_STATn_V2 = 0x14004,
117 PGD_PORT_PARAMn_V2 = 0x14008,
118 PGD_PORT_BLKn_V2 = 0x1400C,
119 PGD_PORT_TRANn_V2 = 0x14010,
120 PGD_PORT_MCHANn_V2 = 0x14014,
121 PGD_PORT_PSHPLLn_V2 = 0x14018,
122 PGD_PORT_PC_CFGn_V2 = 0x8000,
123 PGD_PORT_PC_VALn_V2 = 0x8004,
124 PGD_PORT_PC_VFR_TSn_V2 = 0x8008,
125 PGD_PORT_PC_VFR_STn_V2 = 0x800C,
126 PGD_PORT_PC_VFR_CLn_V2 = 0x8010,
127 PGD_IE_STAT_V2 = 0x820,
128 PGD_VE_STAT_V2 = 0x830,
129};
130
131#define PGD_THIS_EE_V1(r) (dev->base + (r ## _V1) + (dev->ee * 16))
132#define PGD_PORT_V1(r, p) (dev->base + (r ## _V1) + ((p) * 32))
133#define CFG_PORT_V1(r) ((r ## _V1))
134/* Component registers */
135enum comp_reg_v1 {
136 COMP_CFG_V1 = 0,
137 COMP_TRUST_CFG_V1 = 0x14,
138};
139
140/* Manager PGD registers */
141enum pgd_reg_v1 {
142 PGD_CFG_V1 = 0x1000,
143 PGD_STAT_V1 = 0x1004,
144 PGD_INT_EN_V1 = 0x1010,
145 PGD_INT_STAT_V1 = 0x1014,
146 PGD_INT_CLR_V1 = 0x1018,
147 PGD_OWN_EEn_V1 = 0x1020,
148 PGD_PORT_INT_EN_EEn_V1 = 0x1030,
149 PGD_PORT_INT_ST_EEn_V1 = 0x1034,
150 PGD_PORT_INT_CL_EEn_V1 = 0x1038,
151 PGD_PORT_CFGn_V1 = 0x1080,
152 PGD_PORT_STATn_V1 = 0x1084,
153 PGD_PORT_PARAMn_V1 = 0x1088,
154 PGD_PORT_BLKn_V1 = 0x108C,
155 PGD_PORT_TRANn_V1 = 0x1090,
156 PGD_PORT_MCHANn_V1 = 0x1094,
157 PGD_PORT_PSHPLLn_V1 = 0x1098,
158 PGD_PORT_PC_CFGn_V1 = 0x1600,
159 PGD_PORT_PC_VALn_V1 = 0x1604,
160 PGD_PORT_PC_VFR_TSn_V1 = 0x1608,
161 PGD_PORT_PC_VFR_STn_V1 = 0x160C,
162 PGD_PORT_PC_VFR_CLn_V1 = 0x1610,
163 PGD_IE_STAT_V1 = 0x1700,
164 PGD_VE_STAT_V1 = 0x1710,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165};
166
167/* Manager registers */
168enum mgr_reg {
169 MGR_CFG = 0x200,
170 MGR_STATUS = 0x204,
171 MGR_RX_MSGQ_CFG = 0x208,
172 MGR_INT_EN = 0x210,
173 MGR_INT_STAT = 0x214,
174 MGR_INT_CLR = 0x218,
175 MGR_TX_MSG = 0x230,
176 MGR_RX_MSG = 0x270,
177 MGR_VE_STAT = 0x300,
178};
179
180enum msg_cfg {
181 MGR_CFG_ENABLE = 1,
182 MGR_CFG_RX_MSGQ_EN = 1 << 1,
183 MGR_CFG_TX_MSGQ_EN_HIGH = 1 << 2,
184 MGR_CFG_TX_MSGQ_EN_LOW = 1 << 3,
185};
186/* Message queue types */
187enum msm_slim_msgq_type {
188 MSGQ_RX = 0,
189 MSGQ_TX_LOW = 1,
190 MSGQ_TX_HIGH = 2,
191};
192/* Framer registers */
193enum frm_reg {
194 FRM_CFG = 0x400,
195 FRM_STAT = 0x404,
196 FRM_INT_EN = 0x410,
197 FRM_INT_STAT = 0x414,
198 FRM_INT_CLR = 0x418,
199 FRM_WAKEUP = 0x41C,
200 FRM_CLKCTL_DONE = 0x420,
201 FRM_IE_STAT = 0x430,
202 FRM_VE_STAT = 0x440,
203};
204
205/* Interface registers */
206enum intf_reg {
207 INTF_CFG = 0x600,
208 INTF_STAT = 0x604,
209 INTF_INT_EN = 0x610,
210 INTF_INT_STAT = 0x614,
211 INTF_INT_CLR = 0x618,
212 INTF_IE_STAT = 0x630,
213 INTF_VE_STAT = 0x640,
214};
215
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216enum rsc_grp {
217 EE_MGR_RSC_GRP = 1 << 10,
218 EE_NGD_2 = 2 << 6,
219 EE_NGD_1 = 0,
220};
221
222enum mgr_intr {
223 MGR_INT_RECFG_DONE = 1 << 24,
224 MGR_INT_TX_NACKED_2 = 1 << 25,
225 MGR_INT_MSG_BUF_CONTE = 1 << 26,
226 MGR_INT_RX_MSG_RCVD = 1 << 30,
227 MGR_INT_TX_MSG_SENT = 1 << 31,
228};
229
230enum frm_cfg {
231 FRM_ACTIVE = 1,
232 CLK_GEAR = 7,
233 ROOT_FREQ = 11,
234 REF_CLK_GEAR = 15,
235};
236
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600237enum msm_ctrl_state {
238 MSM_CTRL_AWAKE,
239 MSM_CTRL_SLEEPING,
240 MSM_CTRL_ASLEEP,
241};
242
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243struct msm_slim_sps_bam {
244 u32 hdl;
245 void __iomem *base;
246 int irq;
247};
248
249struct msm_slim_endp {
250 struct sps_pipe *sps;
251 struct sps_connect config;
252 struct sps_register_event event;
253 struct sps_mem_buffer buf;
254 struct completion *xcomp;
255 bool connected;
256};
257
258struct msm_slim_ctrl {
259 struct slim_controller ctrl;
260 struct slim_framer framer;
261 struct device *dev;
262 void __iomem *base;
Sagar Dhariacc969452011-09-19 10:34:30 -0600263 struct resource *slew_mem;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264 u32 curr_bw;
265 u8 msg_cnt;
266 u32 tx_buf[10];
267 u8 rx_msgs[MSM_CONCUR_MSG][SLIM_RX_MSGQ_BUF_LEN];
268 spinlock_t rx_lock;
269 int head;
270 int tail;
271 int irq;
272 int err;
273 int ee;
274 struct completion *wr_comp;
Sagar Dharia790cfd02011-09-25 17:56:24 -0600275 struct msm_slim_sat *satd[MSM_MAX_NSATS];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700276 struct msm_slim_endp pipes[7];
277 struct msm_slim_sps_bam bam;
278 struct msm_slim_endp rx_msgq;
279 struct completion rx_msgq_notify;
280 struct task_struct *rx_msgq_thread;
281 struct clk *rclk;
282 struct mutex tx_lock;
283 u8 pgdla;
284 bool use_rx_msgqs;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700285 int pipe_b;
286 struct completion reconf;
287 bool reconf_busy;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600288 bool chan_active;
289 enum msm_ctrl_state state;
Sagar Dharia790cfd02011-09-25 17:56:24 -0600290 int nsats;
Sagar Dharia82e516f2012-03-16 16:01:23 -0600291 u32 ver;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292};
293
Sagar Dharia0ffdca12011-09-25 18:55:53 -0600294struct msm_sat_chan {
295 u8 chan;
296 u16 chanh;
297 int req_rem;
298 int req_def;
299};
300
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301struct msm_slim_sat {
302 struct slim_device satcl;
303 struct msm_slim_ctrl *dev;
304 struct workqueue_struct *wq;
305 struct work_struct wd;
306 u8 sat_msgs[SAT_CONCUR_MSG][40];
Sagar Dharia0ffdca12011-09-25 18:55:53 -0600307 struct msm_sat_chan *satch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308 u8 nsatch;
309 bool sent_capability;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600310 bool pending_reconf;
311 bool pending_capability;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700312 int shead;
313 int stail;
314 spinlock_t lock;
315};
316
Sagar Dharia790cfd02011-09-25 17:56:24 -0600317static struct msm_slim_sat *msm_slim_alloc_sat(struct msm_slim_ctrl *dev);
318
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319static int msm_slim_rx_enqueue(struct msm_slim_ctrl *dev, u32 *buf, u8 len)
320{
321 spin_lock(&dev->rx_lock);
322 if ((dev->tail + 1) % MSM_CONCUR_MSG == dev->head) {
323 spin_unlock(&dev->rx_lock);
324 dev_err(dev->dev, "RX QUEUE full!");
325 return -EXFULL;
326 }
327 memcpy((u8 *)dev->rx_msgs[dev->tail], (u8 *)buf, len);
328 dev->tail = (dev->tail + 1) % MSM_CONCUR_MSG;
329 spin_unlock(&dev->rx_lock);
330 return 0;
331}
332
333static int msm_slim_rx_dequeue(struct msm_slim_ctrl *dev, u8 *buf)
334{
335 unsigned long flags;
336 spin_lock_irqsave(&dev->rx_lock, flags);
337 if (dev->tail == dev->head) {
338 spin_unlock_irqrestore(&dev->rx_lock, flags);
339 return -ENODATA;
340 }
341 memcpy(buf, (u8 *)dev->rx_msgs[dev->head], 40);
342 dev->head = (dev->head + 1) % MSM_CONCUR_MSG;
343 spin_unlock_irqrestore(&dev->rx_lock, flags);
344 return 0;
345}
346
347static int msm_sat_enqueue(struct msm_slim_sat *sat, u32 *buf, u8 len)
348{
349 struct msm_slim_ctrl *dev = sat->dev;
350 spin_lock(&sat->lock);
351 if ((sat->stail + 1) % SAT_CONCUR_MSG == sat->shead) {
352 spin_unlock(&sat->lock);
353 dev_err(dev->dev, "SAT QUEUE full!");
354 return -EXFULL;
355 }
356 memcpy(sat->sat_msgs[sat->stail], (u8 *)buf, len);
357 sat->stail = (sat->stail + 1) % SAT_CONCUR_MSG;
358 spin_unlock(&sat->lock);
359 return 0;
360}
361
362static int msm_sat_dequeue(struct msm_slim_sat *sat, u8 *buf)
363{
364 unsigned long flags;
365 spin_lock_irqsave(&sat->lock, flags);
366 if (sat->stail == sat->shead) {
367 spin_unlock_irqrestore(&sat->lock, flags);
368 return -ENODATA;
369 }
370 memcpy(buf, sat->sat_msgs[sat->shead], 40);
371 sat->shead = (sat->shead + 1) % SAT_CONCUR_MSG;
372 spin_unlock_irqrestore(&sat->lock, flags);
373 return 0;
374}
375
376static void msm_get_eaddr(u8 *e_addr, u32 *buffer)
377{
378 e_addr[0] = (buffer[1] >> 24) & 0xff;
379 e_addr[1] = (buffer[1] >> 16) & 0xff;
380 e_addr[2] = (buffer[1] >> 8) & 0xff;
381 e_addr[3] = buffer[1] & 0xff;
382 e_addr[4] = (buffer[0] >> 24) & 0xff;
383 e_addr[5] = (buffer[0] >> 16) & 0xff;
384}
385
386static bool msm_is_sat_dev(u8 *e_addr)
387{
388 if (e_addr[5] == QC_MFGID_LSB && e_addr[4] == QC_MFGID_MSB &&
389 e_addr[2] != QC_CHIPID_SL &&
390 (e_addr[1] == QC_DEVID_SAT1 || e_addr[1] == QC_DEVID_SAT2))
391 return true;
392 return false;
393}
394
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700395static int msm_slim_get_ctrl(struct msm_slim_ctrl *dev)
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600396{
Sagar Dharia45e77912012-01-10 09:55:18 -0700397#ifdef CONFIG_PM_RUNTIME
398 int ref = 0;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700399 int ret = pm_runtime_get_sync(dev->dev);
400 if (ret >= 0) {
401 ref = atomic_read(&dev->dev->power.usage_count);
402 if (ref <= 0) {
403 dev_err(dev->dev, "reference count -ve:%d", ref);
404 ret = -ENODEV;
405 }
406 }
407 return ret;
Sagar Dharia45e77912012-01-10 09:55:18 -0700408#else
409 return -ENODEV;
410#endif
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600411}
412static void msm_slim_put_ctrl(struct msm_slim_ctrl *dev)
413{
Sagar Dharia45e77912012-01-10 09:55:18 -0700414#ifdef CONFIG_PM_RUNTIME
Sagar Dharia38fd1872012-02-06 18:36:38 -0700415 int ref;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600416 pm_runtime_mark_last_busy(dev->dev);
Sagar Dharia38fd1872012-02-06 18:36:38 -0700417 ref = atomic_read(&dev->dev->power.usage_count);
418 if (ref <= 0)
419 dev_err(dev->dev, "reference count mismatch:%d", ref);
420 else
421 pm_runtime_put(dev->dev);
Sagar Dharia45e77912012-01-10 09:55:18 -0700422#endif
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600423}
424
Sagar Dharia790cfd02011-09-25 17:56:24 -0600425static struct msm_slim_sat *addr_to_sat(struct msm_slim_ctrl *dev, u8 laddr)
426{
427 struct msm_slim_sat *sat = NULL;
428 int i = 0;
429 while (!sat && i < dev->nsats) {
430 if (laddr == dev->satd[i]->satcl.laddr)
431 sat = dev->satd[i];
432 i++;
433 }
434 return sat;
435}
436
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437static irqreturn_t msm_slim_interrupt(int irq, void *d)
438{
439 struct msm_slim_ctrl *dev = d;
440 u32 pstat;
441 u32 stat = readl_relaxed(dev->base + MGR_INT_STAT);
442
443 if (stat & MGR_INT_TX_MSG_SENT || stat & MGR_INT_TX_NACKED_2) {
444 if (stat & MGR_INT_TX_MSG_SENT)
445 writel_relaxed(MGR_INT_TX_MSG_SENT,
446 dev->base + MGR_INT_CLR);
447 else {
448 writel_relaxed(MGR_INT_TX_NACKED_2,
449 dev->base + MGR_INT_CLR);
450 dev->err = -EIO;
451 }
452 /*
453 * Guarantee that interrupt clear bit write goes through before
454 * signalling completion/exiting ISR
455 */
456 mb();
457 if (dev->wr_comp)
458 complete(dev->wr_comp);
459 }
460 if (stat & MGR_INT_RX_MSG_RCVD) {
461 u32 rx_buf[10];
462 u32 mc, mt;
463 u8 len, i;
464 rx_buf[0] = readl_relaxed(dev->base + MGR_RX_MSG);
465 len = rx_buf[0] & 0x1F;
466 for (i = 1; i < ((len + 3) >> 2); i++) {
467 rx_buf[i] = readl_relaxed(dev->base + MGR_RX_MSG +
468 (4 * i));
469 dev_dbg(dev->dev, "reading data: %x\n", rx_buf[i]);
470 }
471 mt = (rx_buf[0] >> 5) & 0x7;
472 mc = (rx_buf[0] >> 8) & 0xff;
473 dev_dbg(dev->dev, "MC: %x, MT: %x\n", mc, mt);
474 if (mt == SLIM_MSG_MT_DEST_REFERRED_USER ||
475 mt == SLIM_MSG_MT_SRC_REFERRED_USER) {
Sagar Dharia790cfd02011-09-25 17:56:24 -0600476 u8 laddr = (u8)((rx_buf[0] >> 16) & 0xFF);
477 struct msm_slim_sat *sat = addr_to_sat(dev, laddr);
478 if (sat)
479 msm_sat_enqueue(sat, rx_buf, len);
480 else
481 dev_err(dev->dev, "unknown sat:%d message",
482 laddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483 writel_relaxed(MGR_INT_RX_MSG_RCVD,
484 dev->base + MGR_INT_CLR);
485 /*
486 * Guarantee that CLR bit write goes through before
487 * queuing work
488 */
489 mb();
Sagar Dharia790cfd02011-09-25 17:56:24 -0600490 if (sat)
491 queue_work(sat->wq, &sat->wd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 } else if (mt == SLIM_MSG_MT_CORE &&
493 mc == SLIM_MSG_MC_REPORT_PRESENT) {
494 u8 e_addr[6];
495 msm_get_eaddr(e_addr, rx_buf);
Sagar Dharia790cfd02011-09-25 17:56:24 -0600496 msm_slim_rx_enqueue(dev, rx_buf, len);
497 writel_relaxed(MGR_INT_RX_MSG_RCVD, dev->base +
498 MGR_INT_CLR);
499 /*
500 * Guarantee that CLR bit write goes through
501 * before signalling completion
502 */
503 mb();
504 complete(&dev->rx_msgq_notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 } else if (mc == SLIM_MSG_MC_REPLY_INFORMATION ||
506 mc == SLIM_MSG_MC_REPLY_VALUE) {
507 msm_slim_rx_enqueue(dev, rx_buf, len);
508 writel_relaxed(MGR_INT_RX_MSG_RCVD, dev->base +
509 MGR_INT_CLR);
510 /*
511 * Guarantee that CLR bit write goes through
512 * before signalling completion
513 */
514 mb();
515 complete(&dev->rx_msgq_notify);
Sagar Dharia144e5e02011-08-08 17:30:11 -0600516 } else if (mc == SLIM_MSG_MC_REPORT_INFORMATION) {
517 u8 *buf = (u8 *)rx_buf;
518 u8 l_addr = buf[2];
519 u16 ele = (u16)buf[4] << 4;
520 ele |= ((buf[3] & 0xf0) >> 4);
521 dev_err(dev->dev, "Slim-dev:%d report inf element:0x%x",
522 l_addr, ele);
523 for (i = 0; i < len - 5; i++)
524 dev_err(dev->dev, "offset:0x%x:bit mask:%x",
525 i, buf[i+5]);
526 writel_relaxed(MGR_INT_RX_MSG_RCVD, dev->base +
527 MGR_INT_CLR);
528 /*
529 * Guarantee that CLR bit write goes through
530 * before exiting
531 */
532 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 } else {
534 dev_err(dev->dev, "Unexpected MC,%x MT:%x, len:%d",
535 mc, mt, len);
536 for (i = 0; i < ((len + 3) >> 2); i++)
537 dev_err(dev->dev, "error msg: %x", rx_buf[i]);
538 writel_relaxed(MGR_INT_RX_MSG_RCVD, dev->base +
539 MGR_INT_CLR);
540 /*
541 * Guarantee that CLR bit write goes through
542 * before exiting
543 */
544 mb();
545 }
546 }
547 if (stat & MGR_INT_RECFG_DONE) {
548 writel_relaxed(MGR_INT_RECFG_DONE, dev->base + MGR_INT_CLR);
549 /*
550 * Guarantee that CLR bit write goes through
551 * before exiting ISR
552 */
553 mb();
554 complete(&dev->reconf);
555 }
Sagar Dharia82e516f2012-03-16 16:01:23 -0600556 pstat = readl_relaxed(PGD_THIS_EE(PGD_PORT_INT_ST_EEn, dev->ver));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 if (pstat != 0) {
558 int i = 0;
559 for (i = dev->pipe_b; i < MSM_SLIM_NPORTS; i++) {
560 if (pstat & 1 << i) {
Sagar Dharia82e516f2012-03-16 16:01:23 -0600561 u32 val = readl_relaxed(PGD_PORT(PGD_PORT_STATn,
562 i, dev->ver));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563 if (val & (1 << 19)) {
564 dev->ctrl.ports[i].err =
565 SLIM_P_DISCONNECT;
566 dev->pipes[i-dev->pipe_b].connected =
567 false;
568 /*
569 * SPS will call completion since
570 * ERROR flags are registered
571 */
572 } else if (val & (1 << 2))
573 dev->ctrl.ports[i].err =
574 SLIM_P_OVERFLOW;
575 else if (val & (1 << 3))
576 dev->ctrl.ports[i].err =
577 SLIM_P_UNDERFLOW;
578 }
Sagar Dharia82e516f2012-03-16 16:01:23 -0600579 writel_relaxed(1, PGD_THIS_EE(PGD_PORT_INT_CL_EEn,
580 dev->ver));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581 }
582 /*
583 * Guarantee that port interrupt bit(s) clearing writes go
584 * through before exiting ISR
585 */
586 mb();
587 }
588
589 return IRQ_HANDLED;
590}
591
592static int
593msm_slim_init_endpoint(struct msm_slim_ctrl *dev, struct msm_slim_endp *ep)
594{
595 int ret;
596 struct sps_pipe *endpoint;
597 struct sps_connect *config = &ep->config;
598
599 /* Allocate the endpoint */
600 endpoint = sps_alloc_endpoint();
601 if (!endpoint) {
602 dev_err(dev->dev, "sps_alloc_endpoint failed\n");
603 return -ENOMEM;
604 }
605
606 /* Get default connection configuration for an endpoint */
607 ret = sps_get_config(endpoint, config);
608 if (ret) {
609 dev_err(dev->dev, "sps_get_config failed 0x%x\n", ret);
610 goto sps_config_failed;
611 }
612
613 ep->sps = endpoint;
614 return 0;
615
616sps_config_failed:
617 sps_free_endpoint(endpoint);
618 return ret;
619}
620
621static void
622msm_slim_free_endpoint(struct msm_slim_endp *ep)
623{
624 sps_free_endpoint(ep->sps);
625 ep->sps = NULL;
626}
627
628static int msm_slim_sps_mem_alloc(
629 struct msm_slim_ctrl *dev, struct sps_mem_buffer *mem, u32 len)
630{
631 dma_addr_t phys;
632
633 mem->size = len;
634 mem->min_size = 0;
635 mem->base = dma_alloc_coherent(dev->dev, mem->size, &phys, GFP_KERNEL);
636
637 if (!mem->base) {
638 dev_err(dev->dev, "dma_alloc_coherent(%d) failed\n", len);
639 return -ENOMEM;
640 }
641
642 mem->phys_base = phys;
643 memset(mem->base, 0x00, mem->size);
644 return 0;
645}
646
647static void
648msm_slim_sps_mem_free(struct msm_slim_ctrl *dev, struct sps_mem_buffer *mem)
649{
650 dma_free_coherent(dev->dev, mem->size, mem->base, mem->phys_base);
651 mem->size = 0;
652 mem->base = NULL;
653 mem->phys_base = 0;
654}
655
656static void msm_hw_set_port(struct msm_slim_ctrl *dev, u8 pn)
657{
658 u32 set_cfg = DEF_WATERMARK | DEF_ALIGN | DEF_PACK | ENABLE_PORT;
Sagar Dharia82e516f2012-03-16 16:01:23 -0600659 u32 int_port = readl_relaxed(PGD_THIS_EE(PGD_PORT_INT_EN_EEn,
660 dev->ver));
661 writel_relaxed(set_cfg, PGD_PORT(PGD_PORT_CFGn, pn, dev->ver));
662 writel_relaxed(DEF_BLKSZ, PGD_PORT(PGD_PORT_BLKn, pn, dev->ver));
663 writel_relaxed(DEF_TRANSZ, PGD_PORT(PGD_PORT_TRANn, pn, dev->ver));
664 writel_relaxed((int_port | 1 << pn) , PGD_THIS_EE(PGD_PORT_INT_EN_EEn,
665 dev->ver));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666 /* Make sure that port registers are updated before returning */
667 mb();
668}
669
670static int msm_slim_connect_pipe_port(struct msm_slim_ctrl *dev, u8 pn)
671{
672 struct msm_slim_endp *endpoint = &dev->pipes[pn];
673 struct sps_connect *cfg = &endpoint->config;
674 u32 stat;
675 int ret = sps_get_config(dev->pipes[pn].sps, cfg);
676 if (ret) {
677 dev_err(dev->dev, "sps pipe-port get config error%x\n", ret);
678 return ret;
679 }
680 cfg->options = SPS_O_DESC_DONE | SPS_O_ERROR |
681 SPS_O_ACK_TRANSFERS | SPS_O_AUTO_ENABLE;
682
683 if (dev->pipes[pn].connected) {
684 ret = sps_set_config(dev->pipes[pn].sps, cfg);
685 if (ret) {
686 dev_err(dev->dev, "sps pipe-port set config erro:%x\n",
687 ret);
688 return ret;
689 }
690 }
691
Sagar Dharia82e516f2012-03-16 16:01:23 -0600692 stat = readl_relaxed(PGD_PORT(PGD_PORT_STATn, (pn + dev->pipe_b),
693 dev->ver));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694 if (dev->ctrl.ports[pn].flow == SLIM_SRC) {
695 cfg->destination = dev->bam.hdl;
696 cfg->source = SPS_DEV_HANDLE_MEM;
697 cfg->dest_pipe_index = ((stat & (0xFF << 4)) >> 4);
698 cfg->src_pipe_index = 0;
699 dev_dbg(dev->dev, "flow src:pipe num:%d",
700 cfg->dest_pipe_index);
701 cfg->mode = SPS_MODE_DEST;
702 } else {
703 cfg->source = dev->bam.hdl;
704 cfg->destination = SPS_DEV_HANDLE_MEM;
705 cfg->src_pipe_index = ((stat & (0xFF << 4)) >> 4);
706 cfg->dest_pipe_index = 0;
707 dev_dbg(dev->dev, "flow dest:pipe num:%d",
708 cfg->src_pipe_index);
709 cfg->mode = SPS_MODE_SRC;
710 }
711 /* Space for desciptor FIFOs */
712 cfg->desc.size = MSM_SLIM_DESC_NUM * sizeof(struct sps_iovec);
713 cfg->config = SPS_CONFIG_DEFAULT;
714 ret = sps_connect(dev->pipes[pn].sps, cfg);
715 if (!ret) {
716 dev->pipes[pn].connected = true;
717 msm_hw_set_port(dev, pn + dev->pipe_b);
718 }
719 return ret;
720}
721
722static u32 *msm_get_msg_buf(struct slim_controller *ctrl, int len)
723{
724 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
725 /*
726 * Currently we block a transaction until the current one completes.
727 * In case we need multiple transactions, use message Q
728 */
729 return dev->tx_buf;
730}
731
732static int msm_send_msg_buf(struct slim_controller *ctrl, u32 *buf, u8 len)
733{
734 int i;
735 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
736 for (i = 0; i < (len + 3) >> 2; i++) {
737 dev_dbg(dev->dev, "TX data:0x%x\n", buf[i]);
738 writel_relaxed(buf[i], dev->base + MGR_TX_MSG + (i * 4));
739 }
740 /* Guarantee that message is sent before returning */
741 mb();
742 return 0;
743}
744
745static int msm_xfer_msg(struct slim_controller *ctrl, struct slim_msg_txn *txn)
746{
747 DECLARE_COMPLETION_ONSTACK(done);
748 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
749 u32 *pbuf;
750 u8 *puc;
751 int timeout;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700752 int msgv = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 u8 la = txn->la;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600754 u8 mc = (u8)(txn->mc & 0xFF);
755 /*
756 * Voting for runtime PM: Slimbus has 2 possible use cases:
757 * 1. messaging
758 * 2. Data channels
759 * Messaging case goes through messaging slots and data channels
760 * use their own slots
761 * This "get" votes for messaging bandwidth
762 */
763 if (!(txn->mc & SLIM_MSG_CLK_PAUSE_SEQ_FLG))
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700764 msgv = msm_slim_get_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 mutex_lock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700766 if (dev->state == MSM_CTRL_ASLEEP ||
767 ((!(txn->mc & SLIM_MSG_CLK_PAUSE_SEQ_FLG)) &&
768 dev->state == MSM_CTRL_SLEEPING)) {
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600769 dev_err(dev->dev, "runtime or system PM suspended state");
770 mutex_unlock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700771 if (msgv >= 0)
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600772 msm_slim_put_ctrl(dev);
773 return -EBUSY;
774 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 if (txn->mt == SLIM_MSG_MT_CORE &&
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600776 mc == SLIM_MSG_MC_BEGIN_RECONFIGURATION) {
777 if (dev->reconf_busy) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778 wait_for_completion(&dev->reconf);
779 dev->reconf_busy = false;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600780 }
781 /* This "get" votes for data channels */
782 if (dev->ctrl.sched.usedslots != 0 &&
783 !dev->chan_active) {
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700784 int chv = msm_slim_get_ctrl(dev);
785 if (chv >= 0)
786 dev->chan_active = true;
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600787 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 }
789 txn->rl--;
790 pbuf = msm_get_msg_buf(ctrl, txn->rl);
791 dev->wr_comp = NULL;
792 dev->err = 0;
793
794 if (txn->dt == SLIM_MSG_DEST_ENUMADDR) {
795 mutex_unlock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700796 if (msgv >= 0)
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600797 msm_slim_put_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 return -EPROTONOSUPPORT;
799 }
800 if (txn->mt == SLIM_MSG_MT_CORE && txn->la == 0xFF &&
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600801 (mc == SLIM_MSG_MC_CONNECT_SOURCE ||
802 mc == SLIM_MSG_MC_CONNECT_SINK ||
803 mc == SLIM_MSG_MC_DISCONNECT_PORT))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 la = dev->pgdla;
805 if (txn->dt == SLIM_MSG_DEST_LOGICALADDR)
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600806 *pbuf = SLIM_MSG_ASM_FIRST_WORD(txn->rl, txn->mt, mc, 0, la);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 else
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600808 *pbuf = SLIM_MSG_ASM_FIRST_WORD(txn->rl, txn->mt, mc, 1, la);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809 if (txn->dt == SLIM_MSG_DEST_LOGICALADDR)
810 puc = ((u8 *)pbuf) + 3;
811 else
812 puc = ((u8 *)pbuf) + 2;
813 if (txn->rbuf)
814 *(puc++) = txn->tid;
815 if ((txn->mt == SLIM_MSG_MT_CORE) &&
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600816 ((mc >= SLIM_MSG_MC_REQUEST_INFORMATION &&
817 mc <= SLIM_MSG_MC_REPORT_INFORMATION) ||
818 (mc >= SLIM_MSG_MC_REQUEST_VALUE &&
819 mc <= SLIM_MSG_MC_CHANGE_VALUE))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 *(puc++) = (txn->ec & 0xFF);
821 *(puc++) = (txn->ec >> 8)&0xFF;
822 }
823 if (txn->wbuf)
824 memcpy(puc, txn->wbuf, txn->len);
825 if (txn->mt == SLIM_MSG_MT_CORE && txn->la == 0xFF &&
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600826 (mc == SLIM_MSG_MC_CONNECT_SOURCE ||
827 mc == SLIM_MSG_MC_CONNECT_SINK ||
828 mc == SLIM_MSG_MC_DISCONNECT_PORT)) {
829 if (mc != SLIM_MSG_MC_DISCONNECT_PORT)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 dev->err = msm_slim_connect_pipe_port(dev, *puc);
831 else {
832 struct msm_slim_endp *endpoint = &dev->pipes[*puc];
833 struct sps_register_event sps_event;
834 memset(&sps_event, 0, sizeof(sps_event));
835 sps_register_event(endpoint->sps, &sps_event);
836 sps_disconnect(endpoint->sps);
837 /*
838 * Remove channel disconnects master-side ports from
839 * channel. No need to send that again on the bus
840 */
841 dev->pipes[*puc].connected = false;
842 mutex_unlock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700843 if (msgv >= 0)
844 msm_slim_put_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 return 0;
846 }
847 if (dev->err) {
848 dev_err(dev->dev, "pipe-port connect err:%d", dev->err);
849 mutex_unlock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700850 if (msgv >= 0)
851 msm_slim_put_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852 return dev->err;
853 }
854 *(puc) = *(puc) + dev->pipe_b;
855 }
856 if (txn->mt == SLIM_MSG_MT_CORE &&
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600857 mc == SLIM_MSG_MC_BEGIN_RECONFIGURATION)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858 dev->reconf_busy = true;
859 dev->wr_comp = &done;
860 msm_send_msg_buf(ctrl, pbuf, txn->rl);
861 timeout = wait_for_completion_timeout(&done, HZ);
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600862
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700863 if (mc == SLIM_MSG_MC_RECONFIGURE_NOW) {
864 if ((txn->mc == (SLIM_MSG_MC_RECONFIGURE_NOW |
865 SLIM_MSG_CLK_PAUSE_SEQ_FLG)) &&
866 timeout) {
867 timeout = wait_for_completion_timeout(&dev->reconf, HZ);
868 dev->reconf_busy = false;
869 if (timeout) {
Sagar Dharia9acf7f42012-03-08 09:45:30 -0700870 clk_disable_unprepare(dev->rclk);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700871 disable_irq(dev->irq);
872 }
873 }
874 if ((txn->mc == (SLIM_MSG_MC_RECONFIGURE_NOW |
875 SLIM_MSG_CLK_PAUSE_SEQ_FLG)) &&
876 !timeout) {
877 dev->reconf_busy = false;
878 dev_err(dev->dev, "clock pause failed");
879 mutex_unlock(&dev->tx_lock);
880 return -ETIMEDOUT;
881 }
882 if (txn->mt == SLIM_MSG_MT_CORE &&
883 txn->mc == SLIM_MSG_MC_RECONFIGURE_NOW) {
884 if (dev->ctrl.sched.usedslots == 0 &&
885 dev->chan_active) {
886 dev->chan_active = false;
887 msm_slim_put_ctrl(dev);
888 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600889 }
890 }
ehgrace.kim1f6cbba2012-08-03 16:05:34 -0700891 if (!timeout) {
892 dev_err(dev->dev, "TX timed out:MC:0x%x,mt:0x%x",
893 txn->mc, txn->mt);
894 dev->wr_comp = NULL;
895 }
896
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600897 mutex_unlock(&dev->tx_lock);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -0700898 if (msgv >= 0)
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600899 msm_slim_put_ctrl(dev);
900
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 return timeout ? dev->err : -ETIMEDOUT;
902}
903
904static int msm_set_laddr(struct slim_controller *ctrl, const u8 *ea,
905 u8 elen, u8 laddr)
906{
907 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
908 DECLARE_COMPLETION_ONSTACK(done);
909 int timeout;
910 u32 *buf;
911 mutex_lock(&dev->tx_lock);
912 buf = msm_get_msg_buf(ctrl, 9);
913 buf[0] = SLIM_MSG_ASM_FIRST_WORD(9, SLIM_MSG_MT_CORE,
914 SLIM_MSG_MC_ASSIGN_LOGICAL_ADDRESS,
915 SLIM_MSG_DEST_LOGICALADDR,
916 ea[5] | ea[4] << 8);
917 buf[1] = ea[3] | (ea[2] << 8) | (ea[1] << 16) | (ea[0] << 24);
918 buf[2] = laddr;
919
920 dev->wr_comp = &done;
921 msm_send_msg_buf(ctrl, buf, 9);
922 timeout = wait_for_completion_timeout(&done, HZ);
ehgrace.kim1f6cbba2012-08-03 16:05:34 -0700923 if (!timeout)
924 dev->wr_comp = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 mutex_unlock(&dev->tx_lock);
926 return timeout ? dev->err : -ETIMEDOUT;
927}
928
Sagar Dharia144e5e02011-08-08 17:30:11 -0600929static int msm_clk_pause_wakeup(struct slim_controller *ctrl)
930{
931 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600932 enable_irq(dev->irq);
Sagar Dharia9acf7f42012-03-08 09:45:30 -0700933 clk_prepare_enable(dev->rclk);
Sagar Dharia144e5e02011-08-08 17:30:11 -0600934 writel_relaxed(1, dev->base + FRM_WAKEUP);
935 /* Make sure framer wakeup write goes through before exiting function */
936 mb();
937 /*
938 * Workaround: Currently, slave is reporting lost-sync messages
939 * after slimbus comes out of clock pause.
940 * Transaction with slave fail before slave reports that message
941 * Give some time for that report to come
942 * Slimbus wakes up in clock gear 10 at 24.576MHz. With each superframe
943 * being 250 usecs, we wait for 20 superframes here to ensure
944 * we get the message
945 */
946 usleep_range(5000, 5000);
947 return 0;
948}
949
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950static int msm_config_port(struct slim_controller *ctrl, u8 pn)
951{
952 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
953 struct msm_slim_endp *endpoint;
954 int ret = 0;
955 if (ctrl->ports[pn].req == SLIM_REQ_HALF_DUP ||
956 ctrl->ports[pn].req == SLIM_REQ_MULTI_CH)
957 return -EPROTONOSUPPORT;
958 if (pn >= (MSM_SLIM_NPORTS - dev->pipe_b))
959 return -ENODEV;
960
961 endpoint = &dev->pipes[pn];
962 ret = msm_slim_init_endpoint(dev, endpoint);
963 dev_dbg(dev->dev, "sps register bam error code:%x\n", ret);
964 return ret;
965}
966
967static enum slim_port_err msm_slim_port_xfer_status(struct slim_controller *ctr,
968 u8 pn, u8 **done_buf, u32 *done_len)
969{
970 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctr);
971 struct sps_iovec sio;
972 int ret;
973 if (done_len)
974 *done_len = 0;
975 if (done_buf)
976 *done_buf = NULL;
977 if (!dev->pipes[pn].connected)
978 return SLIM_P_DISCONNECT;
979 ret = sps_get_iovec(dev->pipes[pn].sps, &sio);
980 if (!ret) {
981 if (done_len)
982 *done_len = sio.size;
983 if (done_buf)
984 *done_buf = (u8 *)sio.addr;
985 }
986 dev_dbg(dev->dev, "get iovec returned %d\n", ret);
987 return SLIM_P_INPROGRESS;
988}
989
990static int msm_slim_port_xfer(struct slim_controller *ctrl, u8 pn, u8 *iobuf,
991 u32 len, struct completion *comp)
992{
993 struct sps_register_event sreg;
994 int ret;
995 struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
Sagar Dhariae77961f2011-09-27 14:03:50 -0600996 if (pn >= 7)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997 return -ENODEV;
998
999
1000 ctrl->ports[pn].xcomp = comp;
1001 sreg.options = (SPS_EVENT_DESC_DONE|SPS_EVENT_ERROR);
1002 sreg.mode = SPS_TRIGGER_WAIT;
1003 sreg.xfer_done = comp;
1004 sreg.callback = NULL;
1005 sreg.user = &ctrl->ports[pn];
1006 ret = sps_register_event(dev->pipes[pn].sps, &sreg);
1007 if (ret) {
1008 dev_dbg(dev->dev, "sps register event error:%x\n", ret);
1009 return ret;
1010 }
1011 ret = sps_transfer_one(dev->pipes[pn].sps, (u32)iobuf, len, NULL,
1012 SPS_IOVEC_FLAG_INT);
1013 dev_dbg(dev->dev, "sps submit xfer error code:%x\n", ret);
1014
1015 return ret;
1016}
1017
1018static int msm_sat_define_ch(struct msm_slim_sat *sat, u8 *buf, u8 len, u8 mc)
1019{
1020 struct msm_slim_ctrl *dev = sat->dev;
1021 enum slim_ch_control oper;
1022 int i;
1023 int ret = 0;
1024 if (mc == SLIM_USR_MC_CHAN_CTRL) {
Sagar Dharia0ffdca12011-09-25 18:55:53 -06001025 for (i = 0; i < sat->nsatch; i++) {
1026 if (buf[5] == sat->satch[i].chan)
1027 break;
1028 }
1029 if (i >= sat->nsatch)
1030 return -ENOTCONN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031 oper = ((buf[3] & 0xC0) >> 6);
1032 /* part of grp. activating/removing 1 will take care of rest */
Sagar Dharia0ffdca12011-09-25 18:55:53 -06001033 ret = slim_control_ch(&sat->satcl, sat->satch[i].chanh, oper,
1034 false);
1035 if (!ret) {
1036 for (i = 5; i < len; i++) {
1037 int j;
1038 for (j = 0; j < sat->nsatch; j++) {
1039 if (buf[i] == sat->satch[j].chan) {
1040 if (oper == SLIM_CH_REMOVE)
1041 sat->satch[j].req_rem++;
1042 else
1043 sat->satch[j].req_def++;
1044 break;
1045 }
1046 }
1047 }
1048 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049 } else {
1050 u16 chh[40];
1051 struct slim_ch prop;
1052 u32 exp;
1053 u8 coeff, cc;
1054 u8 prrate = buf[6];
Sagar Dharia0ffdca12011-09-25 18:55:53 -06001055 if (len <= 8)
1056 return -EINVAL;
1057 for (i = 8; i < len; i++) {
1058 int j = 0;
1059 for (j = 0; j < sat->nsatch; j++) {
1060 if (sat->satch[j].chan == buf[i]) {
1061 chh[i - 8] = sat->satch[j].chanh;
1062 break;
1063 }
1064 }
1065 if (j < sat->nsatch) {
1066 u16 dummy;
1067 ret = slim_query_ch(&sat->satcl, buf[i],
1068 &dummy);
1069 if (ret)
1070 return ret;
1071 if (mc == SLIM_USR_MC_DEF_ACT_CHAN)
1072 sat->satch[j].req_def++;
1073 continue;
1074 }
1075 if (sat->nsatch >= MSM_MAX_SATCH)
1076 return -EXFULL;
1077 ret = slim_query_ch(&sat->satcl, buf[i], &chh[i - 8]);
1078 if (ret)
1079 return ret;
1080 sat->satch[j].chan = buf[i];
1081 sat->satch[j].chanh = chh[i - 8];
1082 if (mc == SLIM_USR_MC_DEF_ACT_CHAN)
1083 sat->satch[j].req_def++;
1084 sat->nsatch++;
1085 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086 prop.dataf = (enum slim_ch_dataf)((buf[3] & 0xE0) >> 5);
1087 prop.auxf = (enum slim_ch_auxf)((buf[4] & 0xC0) >> 5);
1088 prop.baser = SLIM_RATE_4000HZ;
1089 if (prrate & 0x8)
1090 prop.baser = SLIM_RATE_11025HZ;
1091 else
1092 prop.baser = SLIM_RATE_4000HZ;
1093 prop.prot = (enum slim_ch_proto)(buf[5] & 0x0F);
1094 prop.sampleszbits = (buf[4] & 0x1F)*SLIM_CL_PER_SL;
1095 exp = (u32)((buf[5] & 0xF0) >> 4);
1096 coeff = (buf[4] & 0x20) >> 5;
1097 cc = (coeff ? 3 : 1);
1098 prop.ratem = cc * (1 << exp);
1099 if (i > 9)
1100 ret = slim_define_ch(&sat->satcl, &prop, chh, len - 8,
Sagar Dharia0ffdca12011-09-25 18:55:53 -06001101 true, &chh[0]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001102 else
1103 ret = slim_define_ch(&sat->satcl, &prop,
Sagar Dharia0ffdca12011-09-25 18:55:53 -06001104 &chh[0], 1, false, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105 dev_dbg(dev->dev, "define sat grp returned:%d", ret);
Sagar Dharia0ffdca12011-09-25 18:55:53 -06001106 if (ret)
1107 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108
1109 /* part of group so activating 1 will take care of rest */
1110 if (mc == SLIM_USR_MC_DEF_ACT_CHAN)
1111 ret = slim_control_ch(&sat->satcl,
Sagar Dharia0ffdca12011-09-25 18:55:53 -06001112 chh[0],
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001113 SLIM_CH_ACTIVATE, false);
1114 }
1115 return ret;
1116}
1117
1118static void msm_slim_rxwq(struct msm_slim_ctrl *dev)
1119{
1120 u8 buf[40];
1121 u8 mc, mt, len;
1122 int i, ret;
1123 if ((msm_slim_rx_dequeue(dev, (u8 *)buf)) != -ENODATA) {
1124 len = buf[0] & 0x1F;
1125 mt = (buf[0] >> 5) & 0x7;
1126 mc = buf[1];
1127 if (mt == SLIM_MSG_MT_CORE &&
1128 mc == SLIM_MSG_MC_REPORT_PRESENT) {
1129 u8 laddr;
1130 u8 e_addr[6];
1131 for (i = 0; i < 6; i++)
1132 e_addr[i] = buf[7-i];
1133
1134 ret = slim_assign_laddr(&dev->ctrl, e_addr, 6, &laddr);
1135 /* Is this Qualcomm ported generic device? */
1136 if (!ret && e_addr[5] == QC_MFGID_LSB &&
1137 e_addr[4] == QC_MFGID_MSB &&
1138 e_addr[1] == QC_DEVID_PGD &&
1139 e_addr[2] != QC_CHIPID_SL)
1140 dev->pgdla = laddr;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001141 if (!ret && !pm_runtime_enabled(dev->dev) &&
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001142 laddr == (QC_MSM_DEVS - 1))
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001143 pm_runtime_enable(dev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144
Sagar Dharia790cfd02011-09-25 17:56:24 -06001145 if (!ret && msm_is_sat_dev(e_addr)) {
1146 struct msm_slim_sat *sat = addr_to_sat(dev,
1147 laddr);
1148 if (!sat)
1149 sat = msm_slim_alloc_sat(dev);
1150 if (!sat)
1151 return;
1152
1153 sat->satcl.laddr = laddr;
1154 msm_sat_enqueue(sat, (u32 *)buf, len);
1155 queue_work(sat->wq, &sat->wd);
1156 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157 } else if (mc == SLIM_MSG_MC_REPLY_INFORMATION ||
1158 mc == SLIM_MSG_MC_REPLY_VALUE) {
1159 u8 tid = buf[3];
1160 dev_dbg(dev->dev, "tid:%d, len:%d\n", tid, len - 4);
1161 slim_msg_response(&dev->ctrl, &buf[4], tid,
1162 len - 4);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001163 pm_runtime_mark_last_busy(dev->dev);
Sagar Dharia144e5e02011-08-08 17:30:11 -06001164 } else if (mc == SLIM_MSG_MC_REPORT_INFORMATION) {
1165 u8 l_addr = buf[2];
1166 u16 ele = (u16)buf[4] << 4;
1167 ele |= ((buf[3] & 0xf0) >> 4);
1168 dev_err(dev->dev, "Slim-dev:%d report inf element:0x%x",
1169 l_addr, ele);
1170 for (i = 0; i < len - 5; i++)
1171 dev_err(dev->dev, "offset:0x%x:bit mask:%x",
1172 i, buf[i+5]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173 } else {
1174 dev_err(dev->dev, "unexpected message:mc:%x, mt:%x",
1175 mc, mt);
1176 for (i = 0; i < len; i++)
1177 dev_err(dev->dev, "error msg: %x", buf[i]);
1178
1179 }
1180 } else
1181 dev_err(dev->dev, "rxwq called and no dequeue");
1182}
1183
1184static void slim_sat_rxprocess(struct work_struct *work)
1185{
1186 struct msm_slim_sat *sat = container_of(work, struct msm_slim_sat, wd);
1187 struct msm_slim_ctrl *dev = sat->dev;
1188 u8 buf[40];
1189
1190 while ((msm_sat_dequeue(sat, buf)) != -ENODATA) {
1191 struct slim_msg_txn txn;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001192 u8 len, mc, mt;
1193 u32 bw_sl;
1194 int ret = 0;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001195 int satv = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196 bool gen_ack = false;
1197 u8 tid;
1198 u8 wbuf[8];
Sagar Dharia0ffdca12011-09-25 18:55:53 -06001199 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 txn.mt = SLIM_MSG_MT_SRC_REFERRED_USER;
1201 txn.dt = SLIM_MSG_DEST_LOGICALADDR;
1202 txn.ec = 0;
1203 txn.rbuf = NULL;
1204 txn.la = sat->satcl.laddr;
1205 /* satellite handling */
1206 len = buf[0] & 0x1F;
1207 mc = buf[1];
1208 mt = (buf[0] >> 5) & 0x7;
1209
1210 if (mt == SLIM_MSG_MT_CORE &&
1211 mc == SLIM_MSG_MC_REPORT_PRESENT) {
1212 u8 laddr;
1213 u8 e_addr[6];
1214 for (i = 0; i < 6; i++)
1215 e_addr[i] = buf[7-i];
1216
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001217 if (pm_runtime_enabled(dev->dev)) {
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001218 satv = msm_slim_get_ctrl(dev);
1219 if (satv >= 0)
1220 sat->pending_capability = true;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001221 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001222 slim_assign_laddr(&dev->ctrl, e_addr, 6, &laddr);
1223 sat->satcl.laddr = laddr;
Sagar Dharia69bf5572012-02-21 14:45:35 -07001224 /*
1225 * Since capability message is already sent, present
1226 * message will indicate subsystem hosting this
1227 * satellite has restarted.
1228 * Remove all active channels of this satellite
1229 * when this is detected
1230 */
1231 if (sat->sent_capability) {
1232 for (i = 0; i < sat->nsatch; i++) {
1233 enum slim_ch_state chs =
1234 slim_get_ch_state(&sat->satcl,
1235 sat->satch[i].chanh);
1236 pr_err("Slim-SSR, sat:%d, rm chan:%d",
1237 laddr,
1238 sat->satch[i].chan);
1239 if (chs == SLIM_CH_ACTIVE)
1240 slim_control_ch(&sat->satcl,
1241 sat->satch[i].chanh,
1242 SLIM_CH_REMOVE, true);
1243 }
1244 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001245 } else if (mt != SLIM_MSG_MT_CORE &&
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001246 mc != SLIM_MSG_MC_REPORT_PRESENT) {
1247 satv = msm_slim_get_ctrl(dev);
1248 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001249 switch (mc) {
1250 case SLIM_MSG_MC_REPORT_PRESENT:
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001251 /* Remove runtime_pm vote once satellite acks */
1252 if (mt != SLIM_MSG_MT_CORE) {
1253 if (pm_runtime_enabled(dev->dev) &&
1254 sat->pending_capability) {
1255 msm_slim_put_ctrl(dev);
1256 sat->pending_capability = false;
1257 }
1258 continue;
1259 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260 /* send a Manager capability msg */
Sagar Dharia790cfd02011-09-25 17:56:24 -06001261 if (sat->sent_capability) {
1262 if (mt == SLIM_MSG_MT_CORE)
1263 goto send_capability;
1264 else
1265 continue;
1266 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001267 ret = slim_add_device(&dev->ctrl, &sat->satcl);
1268 if (ret) {
1269 dev_err(dev->dev,
1270 "Satellite-init failed");
1271 continue;
1272 }
Sagar Dharia0ffdca12011-09-25 18:55:53 -06001273 /* Satellite-channels */
1274 sat->satch = kzalloc(MSM_MAX_SATCH *
1275 sizeof(struct msm_sat_chan),
1276 GFP_KERNEL);
Sagar Dharia790cfd02011-09-25 17:56:24 -06001277send_capability:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001278 txn.mc = SLIM_USR_MC_MASTER_CAPABILITY;
1279 txn.mt = SLIM_MSG_MT_SRC_REFERRED_USER;
1280 txn.la = sat->satcl.laddr;
1281 txn.rl = 8;
1282 wbuf[0] = SAT_MAGIC_LSB;
1283 wbuf[1] = SAT_MAGIC_MSB;
1284 wbuf[2] = SAT_MSG_VER;
1285 wbuf[3] = SAT_MSG_PROT;
1286 txn.wbuf = wbuf;
1287 txn.len = 4;
1288 sat->sent_capability = true;
1289 msm_xfer_msg(&dev->ctrl, &txn);
1290 break;
1291 case SLIM_USR_MC_ADDR_QUERY:
1292 memcpy(&wbuf[1], &buf[4], 6);
1293 ret = slim_get_logical_addr(&sat->satcl,
1294 &wbuf[1], 6, &wbuf[7]);
1295 if (ret)
1296 memset(&wbuf[1], 0, 6);
1297 wbuf[0] = buf[3];
1298 txn.mc = SLIM_USR_MC_ADDR_REPLY;
1299 txn.rl = 12;
1300 txn.len = 8;
1301 txn.wbuf = wbuf;
1302 msm_xfer_msg(&dev->ctrl, &txn);
1303 break;
1304 case SLIM_USR_MC_DEFINE_CHAN:
1305 case SLIM_USR_MC_DEF_ACT_CHAN:
1306 case SLIM_USR_MC_CHAN_CTRL:
1307 if (mc != SLIM_USR_MC_CHAN_CTRL)
1308 tid = buf[7];
1309 else
1310 tid = buf[4];
1311 gen_ack = true;
1312 ret = msm_sat_define_ch(sat, buf, len, mc);
1313 if (ret) {
1314 dev_err(dev->dev,
1315 "SAT define_ch returned:%d",
1316 ret);
1317 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001318 if (!sat->pending_reconf) {
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001319 int chv = msm_slim_get_ctrl(dev);
1320 if (chv >= 0)
1321 sat->pending_reconf = true;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001322 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001323 break;
1324 case SLIM_USR_MC_RECONFIG_NOW:
1325 tid = buf[3];
1326 gen_ack = true;
1327 ret = slim_reconfigure_now(&sat->satcl);
Sagar Dharia0ffdca12011-09-25 18:55:53 -06001328 for (i = 0; i < sat->nsatch; i++) {
1329 struct msm_sat_chan *sch = &sat->satch[i];
1330 if (sch->req_rem) {
1331 if (!ret)
1332 slim_dealloc_ch(&sat->satcl,
1333 sch->chanh);
1334 sch->req_rem--;
1335 } else if (sch->req_def) {
1336 if (ret)
1337 slim_dealloc_ch(&sat->satcl,
1338 sch->chanh);
1339 sch->req_def--;
1340 }
1341 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001342 if (sat->pending_reconf) {
1343 msm_slim_put_ctrl(dev);
1344 sat->pending_reconf = false;
1345 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001346 break;
1347 case SLIM_USR_MC_REQ_BW:
1348 /* what we get is in SLOTS */
1349 bw_sl = (u32)buf[4] << 3 |
1350 ((buf[3] & 0xE0) >> 5);
1351 sat->satcl.pending_msgsl = bw_sl;
1352 tid = buf[5];
1353 gen_ack = true;
1354 break;
1355 case SLIM_USR_MC_CONNECT_SRC:
1356 case SLIM_USR_MC_CONNECT_SINK:
1357 if (mc == SLIM_USR_MC_CONNECT_SRC)
1358 txn.mc = SLIM_MSG_MC_CONNECT_SOURCE;
1359 else
1360 txn.mc = SLIM_MSG_MC_CONNECT_SINK;
1361 wbuf[0] = buf[4] & 0x1F;
1362 wbuf[1] = buf[5];
1363 tid = buf[6];
1364 txn.la = buf[3];
1365 txn.mt = SLIM_MSG_MT_CORE;
1366 txn.rl = 6;
1367 txn.len = 2;
1368 txn.wbuf = wbuf;
1369 gen_ack = true;
1370 ret = msm_xfer_msg(&dev->ctrl, &txn);
1371 break;
1372 case SLIM_USR_MC_DISCONNECT_PORT:
1373 txn.mc = SLIM_MSG_MC_DISCONNECT_PORT;
1374 wbuf[0] = buf[4] & 0x1F;
1375 tid = buf[5];
1376 txn.la = buf[3];
1377 txn.rl = 5;
1378 txn.len = 1;
1379 txn.mt = SLIM_MSG_MT_CORE;
1380 txn.wbuf = wbuf;
1381 gen_ack = true;
1382 ret = msm_xfer_msg(&dev->ctrl, &txn);
1383 default:
1384 break;
1385 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001386 if (!gen_ack) {
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001387 if (mc != SLIM_MSG_MC_REPORT_PRESENT && satv >= 0)
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001388 msm_slim_put_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001389 continue;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001390 }
1391
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001392 wbuf[0] = tid;
1393 if (!ret)
1394 wbuf[1] = MSM_SAT_SUCCSS;
1395 else
1396 wbuf[1] = 0;
1397 txn.mc = SLIM_USR_MC_GENERIC_ACK;
1398 txn.la = sat->satcl.laddr;
1399 txn.rl = 6;
1400 txn.len = 2;
1401 txn.wbuf = wbuf;
1402 txn.mt = SLIM_MSG_MT_SRC_REFERRED_USER;
1403 msm_xfer_msg(&dev->ctrl, &txn);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07001404 if (satv >= 0)
1405 msm_slim_put_ctrl(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001406 }
1407}
1408
Sagar Dharia790cfd02011-09-25 17:56:24 -06001409static struct msm_slim_sat *msm_slim_alloc_sat(struct msm_slim_ctrl *dev)
1410{
1411 struct msm_slim_sat *sat;
1412 char *name;
1413 if (dev->nsats >= MSM_MAX_NSATS)
1414 return NULL;
1415
1416 sat = kzalloc(sizeof(struct msm_slim_sat), GFP_KERNEL);
1417 if (!sat) {
1418 dev_err(dev->dev, "no memory for satellite");
1419 return NULL;
1420 }
1421 name = kzalloc(SLIMBUS_NAME_SIZE, GFP_KERNEL);
1422 if (!name) {
1423 dev_err(dev->dev, "no memory for satellite name");
1424 kfree(sat);
1425 return NULL;
1426 }
1427 dev->satd[dev->nsats] = sat;
1428 sat->dev = dev;
1429 snprintf(name, SLIMBUS_NAME_SIZE, "msm_sat%d", dev->nsats);
1430 sat->satcl.name = name;
1431 spin_lock_init(&sat->lock);
1432 INIT_WORK(&sat->wd, slim_sat_rxprocess);
1433 sat->wq = create_singlethread_workqueue(sat->satcl.name);
1434 if (!sat->wq) {
1435 kfree(name);
1436 kfree(sat);
1437 return NULL;
1438 }
1439 /*
1440 * Both sats will be allocated from RX thread and RX thread will
1441 * process messages sequentially. No synchronization necessary
1442 */
1443 dev->nsats++;
1444 return sat;
1445}
1446
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001447static void
1448msm_slim_rx_msgq_event(struct msm_slim_ctrl *dev, struct sps_event_notify *ev)
1449{
1450 u32 *buf = ev->data.transfer.user;
1451 struct sps_iovec *iovec = &ev->data.transfer.iovec;
1452
1453 /*
1454 * Note the virtual address needs to be offset by the same index
1455 * as the physical address or just pass in the actual virtual address
1456 * if the sps_mem_buffer is not needed. Note that if completion is
1457 * used, the virtual address won't be available and will need to be
1458 * calculated based on the offset of the physical address
1459 */
1460 if (ev->event_id == SPS_EVENT_DESC_DONE) {
1461
1462 pr_debug("buf = 0x%p, data = 0x%x\n", buf, *buf);
1463
1464 pr_debug("iovec = (0x%x 0x%x 0x%x)\n",
1465 iovec->addr, iovec->size, iovec->flags);
1466
1467 } else {
1468 dev_err(dev->dev, "%s: unknown event %d\n",
1469 __func__, ev->event_id);
1470 }
1471}
1472
1473static void msm_slim_rx_msgq_cb(struct sps_event_notify *notify)
1474{
1475 struct msm_slim_ctrl *dev = (struct msm_slim_ctrl *)notify->user;
1476 msm_slim_rx_msgq_event(dev, notify);
1477}
1478
1479/* Queue up Rx message buffer */
1480static inline int
1481msm_slim_post_rx_msgq(struct msm_slim_ctrl *dev, int ix)
1482{
1483 int ret;
1484 u32 flags = SPS_IOVEC_FLAG_INT;
1485 struct msm_slim_endp *endpoint = &dev->rx_msgq;
1486 struct sps_mem_buffer *mem = &endpoint->buf;
1487 struct sps_pipe *pipe = endpoint->sps;
1488
1489 /* Rx message queue buffers are 4 bytes in length */
1490 u8 *virt_addr = mem->base + (4 * ix);
1491 u32 phys_addr = mem->phys_base + (4 * ix);
1492
1493 pr_debug("index:%d, phys:0x%x, virt:0x%p\n", ix, phys_addr, virt_addr);
1494
1495 ret = sps_transfer_one(pipe, phys_addr, 4, virt_addr, flags);
1496 if (ret)
1497 dev_err(dev->dev, "transfer_one() failed 0x%x, %d\n", ret, ix);
1498
1499 return ret;
1500}
1501
1502static inline int
1503msm_slim_rx_msgq_get(struct msm_slim_ctrl *dev, u32 *data, int offset)
1504{
1505 struct msm_slim_endp *endpoint = &dev->rx_msgq;
1506 struct sps_mem_buffer *mem = &endpoint->buf;
1507 struct sps_pipe *pipe = endpoint->sps;
1508 struct sps_iovec iovec;
1509 int index;
1510 int ret;
1511
1512 ret = sps_get_iovec(pipe, &iovec);
1513 if (ret) {
1514 dev_err(dev->dev, "sps_get_iovec() failed 0x%x\n", ret);
1515 goto err_exit;
1516 }
1517
1518 pr_debug("iovec = (0x%x 0x%x 0x%x)\n",
1519 iovec.addr, iovec.size, iovec.flags);
1520 BUG_ON(iovec.addr < mem->phys_base);
1521 BUG_ON(iovec.addr >= mem->phys_base + mem->size);
1522
1523 /* Calculate buffer index */
1524 index = (iovec.addr - mem->phys_base) / 4;
1525 *(data + offset) = *((u32 *)mem->base + index);
1526
1527 pr_debug("buf = 0x%p, data = 0x%x\n", (u32 *)mem->base + index, *data);
1528
1529 /* Add buffer back to the queue */
1530 (void)msm_slim_post_rx_msgq(dev, index);
1531
1532err_exit:
1533 return ret;
1534}
1535
1536static int msm_slim_rx_msgq_thread(void *data)
1537{
1538 struct msm_slim_ctrl *dev = (struct msm_slim_ctrl *)data;
1539 struct completion *notify = &dev->rx_msgq_notify;
1540 struct msm_slim_sat *sat = NULL;
1541 u32 mc = 0;
1542 u32 mt = 0;
1543 u32 buffer[10];
1544 int index = 0;
1545 u8 msg_len = 0;
1546 int ret;
1547
1548 dev_dbg(dev->dev, "rx thread started");
1549
1550 while (!kthread_should_stop()) {
1551 set_current_state(TASK_INTERRUPTIBLE);
1552 ret = wait_for_completion_interruptible(notify);
1553
1554 if (ret)
1555 dev_err(dev->dev, "rx thread wait error:%d", ret);
1556
1557 /* 1 irq notification per message */
1558 if (!dev->use_rx_msgqs) {
1559 msm_slim_rxwq(dev);
1560 continue;
1561 }
1562
1563 ret = msm_slim_rx_msgq_get(dev, buffer, index);
1564 if (ret) {
1565 dev_err(dev->dev, "rx_msgq_get() failed 0x%x\n", ret);
1566 continue;
1567 }
1568
1569 pr_debug("message[%d] = 0x%x\n", index, *buffer);
1570
1571 /* Decide if we use generic RX or satellite RX */
1572 if (index++ == 0) {
1573 msg_len = *buffer & 0x1F;
1574 pr_debug("Start of new message, len = %d\n", msg_len);
1575 mt = (buffer[0] >> 5) & 0x7;
1576 mc = (buffer[0] >> 8) & 0xff;
1577 dev_dbg(dev->dev, "MC: %x, MT: %x\n", mc, mt);
1578 if (mt == SLIM_MSG_MT_DEST_REFERRED_USER ||
Sagar Dharia790cfd02011-09-25 17:56:24 -06001579 mt == SLIM_MSG_MT_SRC_REFERRED_USER) {
1580 u8 laddr;
1581 laddr = (u8)((buffer[0] >> 16) & 0xff);
1582 sat = addr_to_sat(dev, laddr);
1583 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001584 } else if ((index * 4) >= msg_len) {
1585 index = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001586 if (sat) {
1587 msm_sat_enqueue(sat, buffer, msg_len);
1588 queue_work(sat->wq, &sat->wd);
1589 sat = NULL;
1590 } else {
1591 msm_slim_rx_enqueue(dev, buffer, msg_len);
1592 msm_slim_rxwq(dev);
1593 }
1594 }
1595 }
1596
1597 return 0;
1598}
1599
1600static int __devinit msm_slim_init_rx_msgq(struct msm_slim_ctrl *dev)
1601{
1602 int i, ret;
1603 u32 pipe_offset;
1604 struct msm_slim_endp *endpoint = &dev->rx_msgq;
1605 struct sps_connect *config = &endpoint->config;
1606 struct sps_mem_buffer *descr = &config->desc;
1607 struct sps_mem_buffer *mem = &endpoint->buf;
1608 struct completion *notify = &dev->rx_msgq_notify;
1609
1610 struct sps_register_event sps_error_event; /* SPS_ERROR */
1611 struct sps_register_event sps_descr_event; /* DESCR_DONE */
1612
Sagar Dharia31ac5812012-01-04 11:38:59 -07001613 init_completion(notify);
1614 if (!dev->use_rx_msgqs)
1615 goto rx_thread_create;
1616
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001617 /* Allocate the endpoint */
1618 ret = msm_slim_init_endpoint(dev, endpoint);
1619 if (ret) {
1620 dev_err(dev->dev, "init_endpoint failed 0x%x\n", ret);
1621 goto sps_init_endpoint_failed;
1622 }
1623
1624 /* Get the pipe indices for the message queues */
1625 pipe_offset = (readl_relaxed(dev->base + MGR_STATUS) & 0xfc) >> 2;
1626 dev_dbg(dev->dev, "Message queue pipe offset %d\n", pipe_offset);
1627
1628 config->mode = SPS_MODE_SRC;
1629 config->source = dev->bam.hdl;
1630 config->destination = SPS_DEV_HANDLE_MEM;
1631 config->src_pipe_index = pipe_offset;
1632 config->options = SPS_O_DESC_DONE | SPS_O_ERROR |
1633 SPS_O_ACK_TRANSFERS | SPS_O_AUTO_ENABLE;
1634
1635 /* Allocate memory for the FIFO descriptors */
1636 ret = msm_slim_sps_mem_alloc(dev, descr,
1637 MSM_SLIM_DESC_NUM * sizeof(struct sps_iovec));
1638 if (ret) {
1639 dev_err(dev->dev, "unable to allocate SPS descriptors\n");
1640 goto alloc_descr_failed;
1641 }
1642
1643 ret = sps_connect(endpoint->sps, config);
1644 if (ret) {
1645 dev_err(dev->dev, "sps_connect failed 0x%x\n", ret);
1646 goto sps_connect_failed;
1647 }
1648
1649 /* Register completion for DESC_DONE */
1650 init_completion(notify);
1651 memset(&sps_descr_event, 0x00, sizeof(sps_descr_event));
1652
1653 sps_descr_event.mode = SPS_TRIGGER_CALLBACK;
1654 sps_descr_event.options = SPS_O_DESC_DONE;
1655 sps_descr_event.user = (void *)dev;
1656 sps_descr_event.xfer_done = notify;
1657
1658 ret = sps_register_event(endpoint->sps, &sps_descr_event);
1659 if (ret) {
1660 dev_err(dev->dev, "sps_connect() failed 0x%x\n", ret);
1661 goto sps_reg_event_failed;
1662 }
1663
1664 /* Register callback for errors */
1665 memset(&sps_error_event, 0x00, sizeof(sps_error_event));
1666 sps_error_event.mode = SPS_TRIGGER_CALLBACK;
1667 sps_error_event.options = SPS_O_ERROR;
1668 sps_error_event.user = (void *)dev;
1669 sps_error_event.callback = msm_slim_rx_msgq_cb;
1670
1671 ret = sps_register_event(endpoint->sps, &sps_error_event);
1672 if (ret) {
1673 dev_err(dev->dev, "sps_connect() failed 0x%x\n", ret);
1674 goto sps_reg_event_failed;
1675 }
1676
1677 /* Allocate memory for the message buffer(s), N descrs, 4-byte mesg */
1678 ret = msm_slim_sps_mem_alloc(dev, mem, MSM_SLIM_DESC_NUM * 4);
1679 if (ret) {
1680 dev_err(dev->dev, "dma_alloc_coherent failed\n");
1681 goto alloc_buffer_failed;
1682 }
1683
1684 /*
1685 * Call transfer_one for each 4-byte buffer
1686 * Use (buf->size/4) - 1 for the number of buffer to post
1687 */
1688
1689 /* Setup the transfer */
1690 for (i = 0; i < (MSM_SLIM_DESC_NUM - 1); i++) {
1691 ret = msm_slim_post_rx_msgq(dev, i);
1692 if (ret) {
1693 dev_err(dev->dev, "post_rx_msgq() failed 0x%x\n", ret);
1694 goto sps_transfer_failed;
1695 }
1696 }
1697
Sagar Dharia31ac5812012-01-04 11:38:59 -07001698rx_thread_create:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001699 /* Fire up the Rx message queue thread */
1700 dev->rx_msgq_thread = kthread_run(msm_slim_rx_msgq_thread, dev,
1701 MSM_SLIM_NAME "_rx_msgq_thread");
1702 if (!dev->rx_msgq_thread) {
1703 dev_err(dev->dev, "Failed to start Rx message queue thread\n");
Sagar Dharia31ac5812012-01-04 11:38:59 -07001704 /* Tear-down BAMs or return? */
1705 if (!dev->use_rx_msgqs)
1706 return -EIO;
1707 else
1708 ret = -EIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001709 } else
1710 return 0;
1711
1712sps_transfer_failed:
1713 msm_slim_sps_mem_free(dev, mem);
1714alloc_buffer_failed:
1715 memset(&sps_error_event, 0x00, sizeof(sps_error_event));
1716 sps_register_event(endpoint->sps, &sps_error_event);
1717sps_reg_event_failed:
1718 sps_disconnect(endpoint->sps);
1719sps_connect_failed:
1720 msm_slim_sps_mem_free(dev, descr);
1721alloc_descr_failed:
1722 msm_slim_free_endpoint(endpoint);
1723sps_init_endpoint_failed:
Sagar Dharia31ac5812012-01-04 11:38:59 -07001724 dev->use_rx_msgqs = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001725 return ret;
1726}
1727
1728/* Registers BAM h/w resource with SPS driver and initializes msgq endpoints */
1729static int __devinit
1730msm_slim_sps_init(struct msm_slim_ctrl *dev, struct resource *bam_mem)
1731{
1732 int i, ret;
1733 u32 bam_handle;
1734 struct sps_bam_props bam_props = {0};
1735
1736 static struct sps_bam_sec_config_props sec_props = {
1737 .ees = {
1738 [0] = { /* LPASS */
1739 .vmid = 0,
1740 .pipe_mask = 0xFFFF98,
1741 },
1742 [1] = { /* Krait Apps */
1743 .vmid = 1,
1744 .pipe_mask = 0x3F000007,
1745 },
1746 [2] = { /* Modem */
1747 .vmid = 2,
1748 .pipe_mask = 0x00000060,
1749 },
1750 },
1751 };
1752
Sagar Dharia31ac5812012-01-04 11:38:59 -07001753 if (!dev->use_rx_msgqs)
1754 goto init_rx_msgq;
1755
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001756 bam_props.ee = dev->ee;
1757 bam_props.virt_addr = dev->bam.base;
1758 bam_props.phys_addr = bam_mem->start;
1759 bam_props.irq = dev->bam.irq;
1760 bam_props.manage = SPS_BAM_MGR_LOCAL;
1761 bam_props.summing_threshold = MSM_SLIM_PERF_SUMM_THRESHOLD;
1762
1763 bam_props.sec_config = SPS_BAM_SEC_DO_CONFIG;
1764 bam_props.p_sec_config_props = &sec_props;
1765
1766 bam_props.options = SPS_O_DESC_DONE | SPS_O_ERROR |
1767 SPS_O_ACK_TRANSFERS | SPS_O_AUTO_ENABLE;
1768
1769 /* First 7 bits are for message Qs */
1770 for (i = 7; i < 32; i++) {
1771 /* Check what pipes are owned by Apps. */
1772 if ((sec_props.ees[dev->ee].pipe_mask >> i) & 0x1)
1773 break;
1774 }
1775 dev->pipe_b = i - 7;
1776
1777 /* Register the BAM device with the SPS driver */
1778 ret = sps_register_bam_device(&bam_props, &bam_handle);
1779 if (ret) {
Sagar Dharia31ac5812012-01-04 11:38:59 -07001780 dev_err(dev->dev, "disabling BAM: reg-bam failed 0x%x\n", ret);
1781 dev->use_rx_msgqs = 0;
1782 goto init_rx_msgq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001783 }
1784 dev->bam.hdl = bam_handle;
1785 dev_dbg(dev->dev, "SLIM BAM registered, handle = 0x%x\n", bam_handle);
1786
Sagar Dharia31ac5812012-01-04 11:38:59 -07001787init_rx_msgq:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001788 ret = msm_slim_init_rx_msgq(dev);
Sagar Dharia31ac5812012-01-04 11:38:59 -07001789 if (ret)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790 dev_err(dev->dev, "msm_slim_init_rx_msgq failed 0x%x\n", ret);
Sagar Dharia31ac5812012-01-04 11:38:59 -07001791 if (!dev->use_rx_msgqs && bam_handle) {
1792 sps_deregister_bam_device(bam_handle);
1793 dev->bam.hdl = 0L;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001794 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001795 return ret;
1796}
1797
1798static void msm_slim_sps_exit(struct msm_slim_ctrl *dev)
1799{
1800 if (dev->use_rx_msgqs) {
1801 struct msm_slim_endp *endpoint = &dev->rx_msgq;
1802 struct sps_connect *config = &endpoint->config;
1803 struct sps_mem_buffer *descr = &config->desc;
1804 struct sps_mem_buffer *mem = &endpoint->buf;
1805 struct sps_register_event sps_event;
1806 memset(&sps_event, 0x00, sizeof(sps_event));
1807 msm_slim_sps_mem_free(dev, mem);
1808 sps_register_event(endpoint->sps, &sps_event);
1809 sps_disconnect(endpoint->sps);
1810 msm_slim_sps_mem_free(dev, descr);
1811 msm_slim_free_endpoint(endpoint);
Sagar Dharia31ac5812012-01-04 11:38:59 -07001812 sps_deregister_bam_device(dev->bam.hdl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001813 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001814}
1815
Sagar Dhariacc969452011-09-19 10:34:30 -06001816static void msm_slim_prg_slew(struct platform_device *pdev,
1817 struct msm_slim_ctrl *dev)
1818{
1819 struct resource *slew_io;
1820 void __iomem *slew_reg;
1821 /* SLEW RATE register for this slimbus */
1822 dev->slew_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1823 "slimbus_slew_reg");
1824 if (!dev->slew_mem) {
1825 dev_dbg(&pdev->dev, "no slimbus slew resource\n");
1826 return;
1827 }
1828 slew_io = request_mem_region(dev->slew_mem->start,
1829 resource_size(dev->slew_mem), pdev->name);
1830 if (!slew_io) {
1831 dev_dbg(&pdev->dev, "slimbus-slew mem claimed\n");
1832 dev->slew_mem = NULL;
1833 return;
1834 }
1835
1836 slew_reg = ioremap(dev->slew_mem->start, resource_size(dev->slew_mem));
1837 if (!slew_reg) {
1838 dev_dbg(dev->dev, "slew register mapping failed");
1839 release_mem_region(dev->slew_mem->start,
1840 resource_size(dev->slew_mem));
1841 dev->slew_mem = NULL;
1842 return;
1843 }
1844 writel_relaxed(1, slew_reg);
1845 /* Make sure slimbus-slew rate enabling goes through */
1846 wmb();
1847 iounmap(slew_reg);
1848}
1849
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001850static int __devinit msm_slim_probe(struct platform_device *pdev)
1851{
1852 struct msm_slim_ctrl *dev;
1853 int ret;
1854 struct resource *bam_mem, *bam_io;
1855 struct resource *slim_mem, *slim_io;
1856 struct resource *irq, *bam_irq;
1857 slim_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1858 "slimbus_physical");
1859 if (!slim_mem) {
1860 dev_err(&pdev->dev, "no slimbus physical memory resource\n");
1861 return -ENODEV;
1862 }
1863 slim_io = request_mem_region(slim_mem->start, resource_size(slim_mem),
1864 pdev->name);
1865 if (!slim_io) {
1866 dev_err(&pdev->dev, "slimbus memory already claimed\n");
1867 return -EBUSY;
1868 }
1869
1870 bam_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1871 "slimbus_bam_physical");
1872 if (!bam_mem) {
1873 dev_err(&pdev->dev, "no slimbus BAM memory resource\n");
1874 ret = -ENODEV;
1875 goto err_get_res_bam_failed;
1876 }
1877 bam_io = request_mem_region(bam_mem->start, resource_size(bam_mem),
1878 pdev->name);
1879 if (!bam_io) {
1880 release_mem_region(slim_mem->start, resource_size(slim_mem));
1881 dev_err(&pdev->dev, "slimbus BAM memory already claimed\n");
1882 ret = -EBUSY;
1883 goto err_get_res_bam_failed;
1884 }
1885 irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1886 "slimbus_irq");
1887 if (!irq) {
1888 dev_err(&pdev->dev, "no slimbus IRQ resource\n");
1889 ret = -ENODEV;
1890 goto err_get_res_failed;
1891 }
1892 bam_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1893 "slimbus_bam_irq");
1894 if (!bam_irq) {
1895 dev_err(&pdev->dev, "no slimbus BAM IRQ resource\n");
1896 ret = -ENODEV;
1897 goto err_get_res_failed;
1898 }
1899
1900 dev = kzalloc(sizeof(struct msm_slim_ctrl), GFP_KERNEL);
1901 if (!dev) {
1902 dev_err(&pdev->dev, "no memory for MSM slimbus controller\n");
1903 ret = -ENOMEM;
1904 goto err_get_res_failed;
1905 }
1906 dev->dev = &pdev->dev;
1907 platform_set_drvdata(pdev, dev);
1908 slim_set_ctrldata(&dev->ctrl, dev);
1909 dev->base = ioremap(slim_mem->start, resource_size(slim_mem));
1910 if (!dev->base) {
1911 dev_err(&pdev->dev, "IOremap failed\n");
1912 ret = -ENOMEM;
1913 goto err_ioremap_failed;
1914 }
1915 dev->bam.base = ioremap(bam_mem->start, resource_size(bam_mem));
1916 if (!dev->bam.base) {
1917 dev_err(&pdev->dev, "BAM IOremap failed\n");
1918 ret = -ENOMEM;
1919 goto err_ioremap_bam_failed;
1920 }
Sagar Dhariaf8f603b2012-03-21 15:25:17 -06001921 if (pdev->dev.of_node) {
1922
1923 ret = of_property_read_u32(pdev->dev.of_node, "cell-index",
1924 &dev->ctrl.nr);
1925 if (ret) {
1926 dev_err(&pdev->dev, "Cell index not specified:%d", ret);
1927 goto err_of_init_failed;
1928 }
1929 /* Optional properties */
1930 ret = of_property_read_u32(pdev->dev.of_node,
1931 "qcom,min-clk-gear", &dev->ctrl.min_cg);
1932 ret = of_property_read_u32(pdev->dev.of_node,
1933 "qcom,max-clk-gear", &dev->ctrl.max_cg);
1934 pr_err("min_cg:%d, max_cg:%d, ret:%d", dev->ctrl.min_cg,
1935 dev->ctrl.max_cg, ret);
1936 } else {
1937 dev->ctrl.nr = pdev->id;
1938 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001939 dev->ctrl.nchans = MSM_SLIM_NCHANS;
1940 dev->ctrl.nports = MSM_SLIM_NPORTS;
1941 dev->ctrl.set_laddr = msm_set_laddr;
1942 dev->ctrl.xfer_msg = msm_xfer_msg;
Sagar Dharia144e5e02011-08-08 17:30:11 -06001943 dev->ctrl.wakeup = msm_clk_pause_wakeup;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001944 dev->ctrl.config_port = msm_config_port;
1945 dev->ctrl.port_xfer = msm_slim_port_xfer;
1946 dev->ctrl.port_xfer_status = msm_slim_port_xfer_status;
1947 /* Reserve some messaging BW for satellite-apps driver communication */
1948 dev->ctrl.sched.pending_msgsl = 30;
1949
1950 init_completion(&dev->reconf);
1951 mutex_init(&dev->tx_lock);
1952 spin_lock_init(&dev->rx_lock);
1953 dev->ee = 1;
1954 dev->use_rx_msgqs = 1;
1955 dev->irq = irq->start;
1956 dev->bam.irq = bam_irq->start;
1957
1958 ret = msm_slim_sps_init(dev, bam_mem);
1959 if (ret != 0) {
1960 dev_err(dev->dev, "error SPS init\n");
1961 goto err_sps_init_failed;
1962 }
1963
1964
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001965 dev->framer.rootfreq = SLIM_ROOT_FREQ >> 3;
1966 dev->framer.superfreq =
1967 dev->framer.rootfreq / SLIM_CL_PER_SUPERFRAME_DIV8;
1968 dev->ctrl.a_framer = &dev->framer;
1969 dev->ctrl.clkgear = SLIM_MAX_CLK_GEAR;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001970 dev->ctrl.dev.parent = &pdev->dev;
Sagar Dhariaf8f603b2012-03-21 15:25:17 -06001971 dev->ctrl.dev.of_node = pdev->dev.of_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001972
1973 ret = request_irq(dev->irq, msm_slim_interrupt, IRQF_TRIGGER_HIGH,
1974 "msm_slim_irq", dev);
1975 if (ret) {
1976 dev_err(&pdev->dev, "request IRQ failed\n");
1977 goto err_request_irq_failed;
1978 }
1979
Sagar Dhariacc969452011-09-19 10:34:30 -06001980 msm_slim_prg_slew(pdev, dev);
Sagar Dhariab1c0acf2012-02-06 18:16:58 -07001981
1982 /* Register with framework before enabling frame, clock */
1983 ret = slim_add_numbered_controller(&dev->ctrl);
1984 if (ret) {
1985 dev_err(dev->dev, "error adding controller\n");
1986 goto err_ctrl_failed;
1987 }
1988
1989
Tianyi Gou44a81b02012-02-06 17:49:07 -08001990 dev->rclk = clk_get(dev->dev, "core_clk");
Sagar Dhariab1c0acf2012-02-06 18:16:58 -07001991 if (!dev->rclk) {
1992 dev_err(dev->dev, "slimbus clock not found");
1993 goto err_clk_get_failed;
1994 }
Sagar Dhariacc969452011-09-19 10:34:30 -06001995 clk_set_rate(dev->rclk, SLIM_ROOT_FREQ);
Sagar Dharia9acf7f42012-03-08 09:45:30 -07001996 clk_prepare_enable(dev->rclk);
Sagar Dhariacc969452011-09-19 10:34:30 -06001997
Sagar Dharia82e516f2012-03-16 16:01:23 -06001998 dev->ver = readl_relaxed(dev->base);
1999 /* Version info in 16 MSbits */
2000 dev->ver >>= 16;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002001 /* Component register initialization */
Sagar Dharia82e516f2012-03-16 16:01:23 -06002002 writel_relaxed(1, dev->base + CFG_PORT(COMP_CFG, dev->ver));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002003 writel_relaxed((EE_MGR_RSC_GRP | EE_NGD_2 | EE_NGD_1),
Sagar Dharia82e516f2012-03-16 16:01:23 -06002004 dev->base + CFG_PORT(COMP_TRUST_CFG, dev->ver));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002005
2006 /*
2007 * Manager register initialization
2008 * If RX msg Q is used, disable RX_MSG_RCVD interrupt
2009 */
2010 if (dev->use_rx_msgqs)
2011 writel_relaxed((MGR_INT_RECFG_DONE | MGR_INT_TX_NACKED_2 |
2012 MGR_INT_MSG_BUF_CONTE | /* MGR_INT_RX_MSG_RCVD | */
2013 MGR_INT_TX_MSG_SENT), dev->base + MGR_INT_EN);
2014 else
2015 writel_relaxed((MGR_INT_RECFG_DONE | MGR_INT_TX_NACKED_2 |
2016 MGR_INT_MSG_BUF_CONTE | MGR_INT_RX_MSG_RCVD |
2017 MGR_INT_TX_MSG_SENT), dev->base + MGR_INT_EN);
2018 writel_relaxed(1, dev->base + MGR_CFG);
2019 /*
2020 * Framer registers are beyond 1K memory region after Manager and/or
2021 * component registers. Make sure those writes are ordered
2022 * before framer register writes
2023 */
2024 wmb();
2025
2026 /* Framer register initialization */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002027 writel_relaxed((0xA << REF_CLK_GEAR) | (0xA << CLK_GEAR) |
2028 (1 << ROOT_FREQ) | (1 << FRM_ACTIVE) | 1,
2029 dev->base + FRM_CFG);
2030 /*
2031 * Make sure that framer wake-up and enabling writes go through
2032 * before any other component is enabled. Framer is responsible for
2033 * clocking the bus and enabling framer first will ensure that other
2034 * devices can report presence when they are enabled
2035 */
2036 mb();
2037
2038 /* Enable RX msg Q */
2039 if (dev->use_rx_msgqs)
2040 writel_relaxed(MGR_CFG_ENABLE | MGR_CFG_RX_MSGQ_EN,
2041 dev->base + MGR_CFG);
2042 else
2043 writel_relaxed(MGR_CFG_ENABLE, dev->base + MGR_CFG);
2044 /*
2045 * Make sure that manager-enable is written through before interface
2046 * device is enabled
2047 */
2048 mb();
2049 writel_relaxed(1, dev->base + INTF_CFG);
2050 /*
2051 * Make sure that interface-enable is written through before enabling
2052 * ported generic device inside MSM manager
2053 */
2054 mb();
Sagar Dharia82e516f2012-03-16 16:01:23 -06002055 writel_relaxed(1, dev->base + CFG_PORT(PGD_CFG, dev->ver));
2056 writel_relaxed(0x3F<<17, dev->base + CFG_PORT(PGD_OWN_EEn, dev->ver) +
2057 (4 * dev->ee));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002058 /*
2059 * Make sure that ported generic device is enabled and port-EE settings
2060 * are written through before finally enabling the component
2061 */
2062 mb();
2063
Sagar Dharia82e516f2012-03-16 16:01:23 -06002064 writel_relaxed(1, dev->base + CFG_PORT(COMP_CFG, dev->ver));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002065 /*
2066 * Make sure that all writes have gone through before exiting this
2067 * function
2068 */
2069 mb();
Sagar Dhariaf8f603b2012-03-21 15:25:17 -06002070 if (pdev->dev.of_node)
2071 of_register_slim_devices(&dev->ctrl);
2072
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002073 pm_runtime_use_autosuspend(&pdev->dev);
2074 pm_runtime_set_autosuspend_delay(&pdev->dev, MSM_SLIM_AUTOSUSPEND);
2075 pm_runtime_set_active(&pdev->dev);
2076
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002077 dev_dbg(dev->dev, "MSM SB controller is up!\n");
2078 return 0;
2079
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002080err_ctrl_failed:
Sagar Dharia82e516f2012-03-16 16:01:23 -06002081 writel_relaxed(0, dev->base + CFG_PORT(COMP_CFG, dev->ver));
Sagar Dhariab1c0acf2012-02-06 18:16:58 -07002082err_clk_get_failed:
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002083 kfree(dev->satd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002084err_request_irq_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002085 msm_slim_sps_exit(dev);
2086err_sps_init_failed:
Sagar Dhariaf8f603b2012-03-21 15:25:17 -06002087err_of_init_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002088 iounmap(dev->bam.base);
2089err_ioremap_bam_failed:
2090 iounmap(dev->base);
2091err_ioremap_failed:
2092 kfree(dev);
2093err_get_res_failed:
2094 release_mem_region(bam_mem->start, resource_size(bam_mem));
2095err_get_res_bam_failed:
2096 release_mem_region(slim_mem->start, resource_size(slim_mem));
2097 return ret;
2098}
2099
2100static int __devexit msm_slim_remove(struct platform_device *pdev)
2101{
2102 struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
2103 struct resource *bam_mem;
2104 struct resource *slim_mem;
Sagar Dhariacc969452011-09-19 10:34:30 -06002105 struct resource *slew_mem = dev->slew_mem;
Sagar Dharia790cfd02011-09-25 17:56:24 -06002106 int i;
2107 for (i = 0; i < dev->nsats; i++) {
2108 struct msm_slim_sat *sat = dev->satd[i];
Sagar Dharia0ffdca12011-09-25 18:55:53 -06002109 int j;
2110 for (j = 0; j < sat->nsatch; j++)
2111 slim_dealloc_ch(&sat->satcl, sat->satch[j].chanh);
Sagar Dharia790cfd02011-09-25 17:56:24 -06002112 slim_remove_device(&sat->satcl);
2113 kfree(sat->satch);
2114 destroy_workqueue(sat->wq);
2115 kfree(sat->satcl.name);
2116 kfree(sat);
2117 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002118 pm_runtime_disable(&pdev->dev);
2119 pm_runtime_set_suspended(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002120 free_irq(dev->irq, dev);
2121 slim_del_controller(&dev->ctrl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002122 clk_put(dev->rclk);
2123 msm_slim_sps_exit(dev);
2124 kthread_stop(dev->rx_msgq_thread);
2125 iounmap(dev->bam.base);
2126 iounmap(dev->base);
2127 kfree(dev);
2128 bam_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2129 "slimbus_bam_physical");
Sagar Dhariae77961f2011-09-27 14:03:50 -06002130 if (bam_mem)
2131 release_mem_region(bam_mem->start, resource_size(bam_mem));
Sagar Dhariacc969452011-09-19 10:34:30 -06002132 if (slew_mem)
2133 release_mem_region(slew_mem->start, resource_size(slew_mem));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002134 slim_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2135 "slimbus_physical");
Sagar Dhariae77961f2011-09-27 14:03:50 -06002136 if (slim_mem)
2137 release_mem_region(slim_mem->start, resource_size(slim_mem));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002138 return 0;
2139}
2140
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002141#ifdef CONFIG_PM_RUNTIME
2142static int msm_slim_runtime_idle(struct device *device)
2143{
2144 dev_dbg(device, "pm_runtime: idle...\n");
2145 pm_request_autosuspend(device);
2146 return -EAGAIN;
2147}
2148#endif
2149
2150/*
2151 * If PM_RUNTIME is not defined, these 2 functions become helper
2152 * functions to be called from system suspend/resume. So they are not
2153 * inside ifdef CONFIG_PM_RUNTIME
2154 */
Sagar Dharia45e77912012-01-10 09:55:18 -07002155#ifdef CONFIG_PM_SLEEP
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002156static int msm_slim_runtime_suspend(struct device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002157{
2158 struct platform_device *pdev = to_platform_device(device);
2159 struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002160 int ret;
2161 dev_dbg(device, "pm_runtime: suspending...\n");
2162 dev->state = MSM_CTRL_SLEEPING;
2163 ret = slim_ctrl_clk_pause(&dev->ctrl, false, SLIM_CLK_UNSPECIFIED);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07002164 if (ret) {
2165 dev_err(device, "clk pause not entered:%d", ret);
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002166 dev->state = MSM_CTRL_AWAKE;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07002167 } else {
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002168 dev->state = MSM_CTRL_ASLEEP;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07002169 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002170 return ret;
2171}
2172
2173static int msm_slim_runtime_resume(struct device *device)
2174{
2175 struct platform_device *pdev = to_platform_device(device);
2176 struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
2177 int ret = 0;
2178 dev_dbg(device, "pm_runtime: resuming...\n");
2179 if (dev->state == MSM_CTRL_ASLEEP)
2180 ret = slim_ctrl_clk_pause(&dev->ctrl, true, 0);
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07002181 if (ret) {
2182 dev_err(device, "clk pause not exited:%d", ret);
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002183 dev->state = MSM_CTRL_ASLEEP;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07002184 } else {
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002185 dev->state = MSM_CTRL_AWAKE;
Sagar Dhariad3ef30a2011-12-09 14:30:45 -07002186 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002187 return ret;
2188}
2189
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002190static int msm_slim_suspend(struct device *dev)
2191{
2192 int ret = 0;
2193 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2194 dev_dbg(dev, "system suspend");
2195 ret = msm_slim_runtime_suspend(dev);
Sagar Dharia6b559e02011-08-03 17:01:31 -06002196 }
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002197 if (ret == -EBUSY) {
Sagar Dharia144e5e02011-08-08 17:30:11 -06002198 /*
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002199 * If the clock pause failed due to active channels, there is
2200 * a possibility that some audio stream is active during suspend
2201 * We dont want to return suspend failure in that case so that
2202 * display and relevant components can still go to suspend.
2203 * If there is some other error, then it should be passed-on
2204 * to system level suspend
2205 */
Sagar Dharia144e5e02011-08-08 17:30:11 -06002206 ret = 0;
2207 }
2208 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002209}
2210
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002211static int msm_slim_resume(struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002212{
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002213 /* If runtime_pm is enabled, this resume shouldn't do anything */
2214 if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
2215 int ret;
2216 dev_dbg(dev, "system resume");
2217 ret = msm_slim_runtime_resume(dev);
2218 if (!ret) {
2219 pm_runtime_mark_last_busy(dev);
2220 pm_request_autosuspend(dev);
2221 }
2222 return ret;
2223
Sagar Dharia144e5e02011-08-08 17:30:11 -06002224 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002225 return 0;
2226}
Sagar Dharia45ee38a2011-08-03 17:01:31 -06002227#endif /* CONFIG_PM_SLEEP */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002228
2229static const struct dev_pm_ops msm_slim_dev_pm_ops = {
2230 SET_SYSTEM_SLEEP_PM_OPS(
2231 msm_slim_suspend,
2232 msm_slim_resume
2233 )
2234 SET_RUNTIME_PM_OPS(
2235 msm_slim_runtime_suspend,
2236 msm_slim_runtime_resume,
2237 msm_slim_runtime_idle
2238 )
2239};
2240
Sagar Dhariaf8f603b2012-03-21 15:25:17 -06002241static struct of_device_id msm_slim_dt_match[] = {
2242 {
2243 .compatible = "qcom,slim-msm",
2244 },
2245 {}
2246};
2247
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002248static struct platform_driver msm_slim_driver = {
2249 .probe = msm_slim_probe,
2250 .remove = msm_slim_remove,
2251 .driver = {
2252 .name = MSM_SLIM_NAME,
2253 .owner = THIS_MODULE,
2254 .pm = &msm_slim_dev_pm_ops,
Sagar Dhariaf8f603b2012-03-21 15:25:17 -06002255 .of_match_table = msm_slim_dt_match,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002256 },
2257};
2258
2259static int msm_slim_init(void)
2260{
2261 return platform_driver_register(&msm_slim_driver);
2262}
2263subsys_initcall(msm_slim_init);
2264
2265static void msm_slim_exit(void)
2266{
2267 platform_driver_unregister(&msm_slim_driver);
2268}
2269module_exit(msm_slim_exit);
2270
2271MODULE_LICENSE("GPL v2");
2272MODULE_VERSION("0.1");
2273MODULE_DESCRIPTION("MSM Slimbus controller");
2274MODULE_ALIAS("platform:msm-slim");