blob: 30d3fe63103c288717a48eded1d1a3f707aa139d [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 */
13
14/*
15 * BAM DMUX module.
16 */
17
18#define DEBUG
19
20#include <linux/delay.h>
21#include <linux/module.h>
22#include <linux/netdevice.h>
23#include <linux/platform_device.h>
24#include <linux/sched.h>
25#include <linux/skbuff.h>
26#include <linux/debugfs.h>
Jeff Hugoaab7ebc2011-09-07 16:46:04 -060027#include <linux/clk.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
29#include <mach/sps.h>
30#include <mach/bam_dmux.h>
Jeff Hugoade1f842011-08-03 15:53:59 -060031#include <mach/msm_smsm.h>
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060032#include <mach/subsystem_notif.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34#define BAM_CH_LOCAL_OPEN 0x1
35#define BAM_CH_REMOTE_OPEN 0x2
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060036#define BAM_CH_IN_RESET 0x4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
38#define BAM_MUX_HDR_MAGIC_NO 0x33fc
39
40#define BAM_MUX_HDR_CMD_DATA 0
41#define BAM_MUX_HDR_CMD_OPEN 1
42#define BAM_MUX_HDR_CMD_CLOSE 2
43
Jeff Hugo949080a2011-08-30 11:58:56 -060044#define POLLING_MIN_SLEEP 950 /* 0.95 ms */
45#define POLLING_MAX_SLEEP 1050 /* 1.05 ms */
46#define POLLING_INACTIVITY 40 /* cycles before switch to intr mode */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047
48
49static int msm_bam_dmux_debug_enable;
50module_param_named(debug_enable, msm_bam_dmux_debug_enable,
51 int, S_IRUGO | S_IWUSR | S_IWGRP);
52
53#if defined(DEBUG)
54static uint32_t bam_dmux_read_cnt;
55static uint32_t bam_dmux_write_cnt;
56static uint32_t bam_dmux_write_cpy_cnt;
57static uint32_t bam_dmux_write_cpy_bytes;
58
59#define DBG(x...) do { \
60 if (msm_bam_dmux_debug_enable) \
61 pr_debug(x); \
62 } while (0)
63
64#define DBG_INC_READ_CNT(x) do { \
65 bam_dmux_read_cnt += (x); \
66 if (msm_bam_dmux_debug_enable) \
67 pr_debug("%s: total read bytes %u\n", \
68 __func__, bam_dmux_read_cnt); \
69 } while (0)
70
71#define DBG_INC_WRITE_CNT(x) do { \
72 bam_dmux_write_cnt += (x); \
73 if (msm_bam_dmux_debug_enable) \
74 pr_debug("%s: total written bytes %u\n", \
75 __func__, bam_dmux_write_cnt); \
76 } while (0)
77
78#define DBG_INC_WRITE_CPY(x) do { \
79 bam_dmux_write_cpy_bytes += (x); \
80 bam_dmux_write_cpy_cnt++; \
81 if (msm_bam_dmux_debug_enable) \
82 pr_debug("%s: total write copy cnt %u, bytes %u\n", \
83 __func__, bam_dmux_write_cpy_cnt, \
84 bam_dmux_write_cpy_bytes); \
85 } while (0)
86#else
87#define DBG(x...) do { } while (0)
88#define DBG_INC_READ_CNT(x...) do { } while (0)
89#define DBG_INC_WRITE_CNT(x...) do { } while (0)
90#define DBG_INC_WRITE_CPY(x...) do { } while (0)
91#endif
92
93struct bam_ch_info {
94 uint32_t status;
Jeff Hugo1c4531c2011-08-02 14:55:37 -060095 void (*notify)(void *, int, unsigned long);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 void *priv;
97 spinlock_t lock;
Jeff Hugo7960abd2011-08-02 15:39:38 -060098 struct platform_device *pdev;
99 char name[BAM_DMUX_CH_NAME_MAX_LEN];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100};
101
102struct tx_pkt_info {
103 struct sk_buff *skb;
104 dma_addr_t dma_address;
105 char is_cmd;
106 uint32_t len;
107 struct work_struct work;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600108 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109};
110
111struct rx_pkt_info {
112 struct sk_buff *skb;
113 dma_addr_t dma_address;
114 struct work_struct work;
Jeff Hugo949080a2011-08-30 11:58:56 -0600115 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116};
117
118#define A2_NUM_PIPES 6
119#define A2_SUMMING_THRESHOLD 4096
120#define A2_DEFAULT_DESCRIPTORS 32
121#define A2_PHYS_BASE 0x124C2000
122#define A2_PHYS_SIZE 0x2000
123#define BUFFER_SIZE 2048
124#define NUM_BUFFERS 32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125static struct sps_bam_props a2_props;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600126static u32 a2_device_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127static struct sps_pipe *bam_tx_pipe;
128static struct sps_pipe *bam_rx_pipe;
129static struct sps_connect tx_connection;
130static struct sps_connect rx_connection;
131static struct sps_mem_buffer tx_desc_mem_buf;
132static struct sps_mem_buffer rx_desc_mem_buf;
133static struct sps_register_event tx_register_event;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600134static struct sps_register_event rx_register_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135
136static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS];
137static int bam_mux_initialized;
138
Jeff Hugo949080a2011-08-30 11:58:56 -0600139static int polling_mode;
140
141static LIST_HEAD(bam_rx_pool);
142static DEFINE_MUTEX(bam_rx_pool_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600143static LIST_HEAD(bam_tx_pool);
144static DEFINE_MUTEX(bam_tx_pool_lock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600145
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146struct bam_mux_hdr {
147 uint16_t magic_num;
148 uint8_t reserved;
149 uint8_t cmd;
150 uint8_t pad_len;
151 uint8_t ch_id;
152 uint16_t pkt_len;
153};
154
Jeff Hugod98b1082011-10-24 10:30:23 -0600155static void notify_all(int event, unsigned long data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700156static void bam_mux_write_done(struct work_struct *work);
157static void handle_bam_mux_cmd(struct work_struct *work);
Jeff Hugo949080a2011-08-30 11:58:56 -0600158static void rx_timer_work_func(struct work_struct *work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159
160static DEFINE_MUTEX(bam_mux_lock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600161static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162
163static struct workqueue_struct *bam_mux_rx_workqueue;
164static struct workqueue_struct *bam_mux_tx_workqueue;
165
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600166/* A2 power collaspe */
167#define UL_TIMEOUT_DELAY 1000 /* in ms */
168static void toggle_apps_ack(void);
169static void reconnect_to_bam(void);
170static void disconnect_to_bam(void);
171static void ul_wakeup(void);
172static void ul_timeout(struct work_struct *work);
173static void vote_dfab(void);
174static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600175static void kickoff_ul_wakeup_func(struct work_struct *work);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600176
177static int bam_is_connected;
178static DEFINE_MUTEX(wakeup_lock);
179static struct completion ul_wakeup_ack_completion;
180static struct completion bam_connection_completion;
181static struct delayed_work ul_timeout_work;
182static int ul_packet_written;
183static struct clk *dfab_clk;
184static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600185static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600186static int bam_connection_is_active;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600187/* End A2 power collaspe */
188
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600189/* subsystem restart */
190static int restart_notifier_cb(struct notifier_block *this,
191 unsigned long code,
192 void *data);
193
194static struct notifier_block restart_notifier = {
195 .notifier_call = restart_notifier_cb,
196};
197static int in_global_reset;
198/* end subsystem restart */
199
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200#define bam_ch_is_open(x) \
201 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
202
203#define bam_ch_is_local_open(x) \
204 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
205
206#define bam_ch_is_remote_open(x) \
207 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
208
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600209#define bam_ch_is_in_reset(x) \
210 (bam_ch[(x)].status & BAM_CH_IN_RESET)
211
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212static void queue_rx(void)
213{
214 void *ptr;
215 struct rx_pkt_info *info;
216
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600217 if (in_global_reset)
218 return;
219
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 info = kmalloc(sizeof(struct rx_pkt_info), GFP_KERNEL);
221 if (!info)
222 return; /*need better way to handle this */
223
224 INIT_WORK(&info->work, handle_bam_mux_cmd);
225
226 info->skb = __dev_alloc_skb(BUFFER_SIZE, GFP_KERNEL);
227 ptr = skb_put(info->skb, BUFFER_SIZE);
Jeff Hugo949080a2011-08-30 11:58:56 -0600228
229 mutex_lock(&bam_rx_pool_lock);
230 list_add_tail(&info->list_node, &bam_rx_pool);
231 mutex_unlock(&bam_rx_pool_lock);
232
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 /* need a way to handle error case */
234 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
235 DMA_FROM_DEVICE);
236 sps_transfer_one(bam_rx_pipe, info->dma_address,
Jeff Hugo33dbc002011-08-25 15:52:53 -0600237 BUFFER_SIZE, info,
238 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239}
240
241static void bam_mux_process_data(struct sk_buff *rx_skb)
242{
243 unsigned long flags;
244 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600245 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246
247 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
248
249 rx_skb->data = (unsigned char *)(rx_hdr + 1);
250 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
251 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600252 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600254 event_data = (unsigned long)(rx_skb);
255
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600257 if (bam_ch[rx_hdr->ch_id].notify)
258 bam_ch[rx_hdr->ch_id].notify(
259 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
260 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 else
262 dev_kfree_skb_any(rx_skb);
263 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
264
265 queue_rx();
266}
267
268static void handle_bam_mux_cmd(struct work_struct *work)
269{
270 unsigned long flags;
271 struct bam_mux_hdr *rx_hdr;
272 struct rx_pkt_info *info;
273 struct sk_buff *rx_skb;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600274 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275
276 info = container_of(work, struct rx_pkt_info, work);
277 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600278 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 kfree(info);
280
281 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
282
283 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
284 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
285 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
286 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
287 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
288 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
289 " pad %d ch %d len %d\n", __func__,
290 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
291 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
292 dev_kfree_skb_any(rx_skb);
293 queue_rx();
294 return;
295 }
296 switch (rx_hdr->cmd) {
297 case BAM_MUX_HDR_CMD_DATA:
298 DBG_INC_READ_CNT(rx_hdr->pkt_len);
299 bam_mux_process_data(rx_skb);
300 break;
301 case BAM_MUX_HDR_CMD_OPEN:
302 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
303 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
304 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
305 dev_kfree_skb_any(rx_skb);
306 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600307 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
308 if (ret)
309 pr_err("%s: platform_device_add() error: %d\n",
310 __func__, ret);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 break;
312 case BAM_MUX_HDR_CMD_CLOSE:
313 /* probably should drop pending write */
314 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
315 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
316 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
317 dev_kfree_skb_any(rx_skb);
318 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600319 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
320 bam_ch[rx_hdr->ch_id].pdev =
321 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
322 if (!bam_ch[rx_hdr->ch_id].pdev)
323 pr_err("%s: platform_device_alloc failed\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324 break;
325 default:
326 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
327 " pad %d ch %d len %d\n", __func__,
328 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
329 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
330 dev_kfree_skb_any(rx_skb);
331 queue_rx();
332 return;
333 }
334}
335
336static int bam_mux_write_cmd(void *data, uint32_t len)
337{
338 int rc;
339 struct tx_pkt_info *pkt;
340 dma_addr_t dma_address;
341
342 mutex_lock(&bam_mux_lock);
343 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_KERNEL);
344 if (pkt == NULL) {
345 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
346 rc = -ENOMEM;
347 mutex_unlock(&bam_mux_lock);
348 return rc;
349 }
350
351 dma_address = dma_map_single(NULL, data, len,
352 DMA_TO_DEVICE);
353 if (!dma_address) {
354 pr_err("%s: dma_map_single() failed\n", __func__);
355 rc = -ENOMEM;
356 mutex_unlock(&bam_mux_lock);
357 return rc;
358 }
359 pkt->skb = (struct sk_buff *)(data);
360 pkt->len = len;
361 pkt->dma_address = dma_address;
362 pkt->is_cmd = 1;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600363 INIT_WORK(&pkt->work, bam_mux_write_done);
364 mutex_lock(&bam_tx_pool_lock);
365 list_add_tail(&pkt->list_node, &bam_tx_pool);
366 mutex_unlock(&bam_tx_pool_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
368 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
369
370 mutex_unlock(&bam_mux_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600371 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372 return rc;
373}
374
375static void bam_mux_write_done(struct work_struct *work)
376{
377 struct sk_buff *skb;
378 struct bam_mux_hdr *hdr;
379 struct tx_pkt_info *info;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600380 unsigned long event_data;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600381 struct list_head *node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600383 if (in_global_reset)
384 return;
385 mutex_lock(&bam_tx_pool_lock);
386 node = bam_tx_pool.next;
387 list_del(node);
388 mutex_unlock(&bam_tx_pool_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389 info = container_of(work, struct tx_pkt_info, work);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600390 if (info->is_cmd) {
391 kfree(info->skb);
392 kfree(info);
393 return;
394 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395 skb = info->skb;
396 kfree(info);
397 hdr = (struct bam_mux_hdr *)skb->data;
398 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600399 event_data = (unsigned long)(skb);
400 if (bam_ch[hdr->ch_id].notify)
401 bam_ch[hdr->ch_id].notify(
402 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
403 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404 else
405 dev_kfree_skb_any(skb);
406}
407
408int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
409{
410 int rc = 0;
411 struct bam_mux_hdr *hdr;
412 unsigned long flags;
413 struct sk_buff *new_skb = NULL;
414 dma_addr_t dma_address;
415 struct tx_pkt_info *pkt;
416
417 if (id >= BAM_DMUX_NUM_CHANNELS)
418 return -EINVAL;
419 if (!skb)
420 return -EINVAL;
421 if (!bam_mux_initialized)
422 return -ENODEV;
423
424 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
425 spin_lock_irqsave(&bam_ch[id].lock, flags);
426 if (!bam_ch_is_open(id)) {
427 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
428 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
429 return -ENODEV;
430 }
431 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
432
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600433 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600434 if (!bam_is_connected) {
435 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600436 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600437 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600438 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600439 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600440
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441 /* if skb do not have any tailroom for padding,
442 copy the skb into a new expanded skb */
443 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
444 /* revisit, probably dev_alloc_skb and memcpy is effecient */
445 new_skb = skb_copy_expand(skb, skb_headroom(skb),
446 4 - (skb->len & 0x3), GFP_ATOMIC);
447 if (new_skb == NULL) {
448 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600449 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450 }
451 dev_kfree_skb_any(skb);
452 skb = new_skb;
453 DBG_INC_WRITE_CPY(skb->len);
454 }
455
456 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
457
458 /* caller should allocate for hdr and padding
459 hdr is fine, padding is tricky */
460 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
461 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
462 hdr->reserved = 0;
463 hdr->ch_id = id;
464 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
465 if (skb->len & 0x3)
466 skb_put(skb, 4 - (skb->len & 0x3));
467
468 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
469
470 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
471 __func__, skb->data, skb->tail, skb->len,
472 hdr->pkt_len, hdr->pad_len);
473
474 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
475 if (pkt == NULL) {
476 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600477 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 }
479
480 dma_address = dma_map_single(NULL, skb->data, skb->len,
481 DMA_TO_DEVICE);
482 if (!dma_address) {
483 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600484 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485 }
486 pkt->skb = skb;
487 pkt->dma_address = dma_address;
488 pkt->is_cmd = 0;
489 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600490 mutex_lock(&bam_tx_pool_lock);
491 list_add_tail(&pkt->list_node, &bam_tx_pool);
492 mutex_unlock(&bam_tx_pool_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
494 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600495 ul_packet_written = 1;
496 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600498
499write_fail3:
500 kfree(pkt);
501write_fail2:
502 if (new_skb)
503 dev_kfree_skb_any(new_skb);
504write_fail:
505 read_unlock(&ul_wakeup_lock);
506 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507}
508
509int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600510 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511{
512 struct bam_mux_hdr *hdr;
513 unsigned long flags;
514 int rc = 0;
515
516 DBG("%s: opening ch %d\n", __func__, id);
517 if (!bam_mux_initialized)
518 return -ENODEV;
519 if (id >= BAM_DMUX_NUM_CHANNELS)
520 return -EINVAL;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600521 if (notify == NULL)
522 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523
524 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
525 if (hdr == NULL) {
526 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
527 return -ENOMEM;
528 }
529 spin_lock_irqsave(&bam_ch[id].lock, flags);
530 if (bam_ch_is_open(id)) {
531 DBG("%s: Already opened %d\n", __func__, id);
532 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
533 kfree(hdr);
534 goto open_done;
535 }
536 if (!bam_ch_is_remote_open(id)) {
537 DBG("%s: Remote not open; ch: %d\n", __func__, id);
538 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
539 kfree(hdr);
540 rc = -ENODEV;
541 goto open_done;
542 }
543
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600544 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 bam_ch[id].priv = priv;
546 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
547 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
548
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600549 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600550 if (!bam_is_connected) {
551 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600552 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600553 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600554 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600555 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600556
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
558 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
559 hdr->reserved = 0;
560 hdr->ch_id = id;
561 hdr->pkt_len = 0;
562 hdr->pad_len = 0;
563
564 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600565 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566
567open_done:
568 DBG("%s: opened ch %d\n", __func__, id);
569 return rc;
570}
571
572int msm_bam_dmux_close(uint32_t id)
573{
574 struct bam_mux_hdr *hdr;
575 unsigned long flags;
576 int rc;
577
578 if (id >= BAM_DMUX_NUM_CHANNELS)
579 return -EINVAL;
580 DBG("%s: closing ch %d\n", __func__, id);
581 if (!bam_mux_initialized)
582 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600584 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600585 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600586 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600587 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600588 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600589 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600590 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600591
Jeff Hugo061ce672011-10-21 17:15:32 -0600592 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600593 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 bam_ch[id].priv = NULL;
595 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
596 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
597
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600598 if (bam_ch_is_in_reset(id)) {
599 read_unlock(&ul_wakeup_lock);
600 bam_ch[id].status &= ~BAM_CH_IN_RESET;
601 return 0;
602 }
603
Jeff Hugobb5802f2011-11-02 17:10:29 -0600604 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605 if (hdr == NULL) {
606 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600607 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608 return -ENOMEM;
609 }
610 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
611 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
612 hdr->reserved = 0;
613 hdr->ch_id = id;
614 hdr->pkt_len = 0;
615 hdr->pad_len = 0;
616
617 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600618 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619
620 DBG("%s: closed ch %d\n", __func__, id);
621 return rc;
622}
623
Jeff Hugo949080a2011-08-30 11:58:56 -0600624static void rx_timer_work_func(struct work_struct *work)
625{
626 struct sps_iovec iov;
627 struct list_head *node;
628 struct rx_pkt_info *info;
629 int inactive_cycles = 0;
630 int ret;
631 struct sps_connect cur_rx_conn;
632
633 while (1) { /* timer loop */
634 ++inactive_cycles;
635 while (1) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600636 if (in_global_reset)
637 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600638 sps_get_iovec(bam_rx_pipe, &iov);
639 if (iov.addr == 0)
640 break;
641 inactive_cycles = 0;
642 mutex_lock(&bam_rx_pool_lock);
643 node = bam_rx_pool.next;
644 list_del(node);
645 mutex_unlock(&bam_rx_pool_lock);
646 info = container_of(node, struct rx_pkt_info,
647 list_node);
648 handle_bam_mux_cmd(&info->work);
649 }
650
651 if (inactive_cycles == POLLING_INACTIVITY) {
652 /*
653 * attempt to enable interrupts in this pipe
654 * if enabling interrupts fails, continue polling
655 */
656 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
657 if (ret) {
658 pr_err("%s: sps_get_config() failed, interrupts"
659 " not enabled\n", __func__);
660 queue_work(bam_mux_rx_workqueue,
661 &rx_timer_work);
662 return;
663 } else {
664 rx_register_event.options = SPS_O_EOT;
665 /* should check return value */
666 sps_register_event(bam_rx_pipe,
667 &rx_register_event);
668 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
669 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
670 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
671 if (ret) {
672 pr_err("%s: sps_set_config() failed, "
673 "interrupts not enabled\n",
674 __func__);
675 queue_work(bam_mux_rx_workqueue,
676 &rx_timer_work);
677 return;
678 }
679 polling_mode = 0;
680 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600681 if (in_global_reset)
682 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600683 /* handle race condition - missed packet? */
684 sps_get_iovec(bam_rx_pipe, &iov);
685 if (iov.addr == 0)
686 return;
687 inactive_cycles = 0;
688 mutex_lock(&bam_rx_pool_lock);
689 node = bam_rx_pool.next;
690 list_del(node);
691 mutex_unlock(&bam_rx_pool_lock);
692 info = container_of(node, struct rx_pkt_info,
693 list_node);
694 handle_bam_mux_cmd(&info->work);
695 return;
696 }
697
698 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
699 }
700}
701
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702static void bam_mux_tx_notify(struct sps_event_notify *notify)
703{
704 struct tx_pkt_info *pkt;
705
706 DBG("%s: event %d notified\n", __func__, notify->event_id);
707
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600708 if (in_global_reset)
709 return;
710
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711 switch (notify->event_id) {
712 case SPS_EVENT_EOT:
713 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600714 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 dma_unmap_single(NULL, pkt->dma_address,
716 pkt->skb->len,
717 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600718 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719 dma_unmap_single(NULL, pkt->dma_address,
720 pkt->len,
721 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600722 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 break;
724 default:
725 pr_err("%s: recieved unexpected event id %d\n", __func__,
726 notify->event_id);
727 }
728}
729
Jeff Hugo33dbc002011-08-25 15:52:53 -0600730static void bam_mux_rx_notify(struct sps_event_notify *notify)
731{
Jeff Hugo949080a2011-08-30 11:58:56 -0600732 int ret;
733 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600734
735 DBG("%s: event %d notified\n", __func__, notify->event_id);
736
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600737 if (in_global_reset)
738 return;
739
Jeff Hugo33dbc002011-08-25 15:52:53 -0600740 switch (notify->event_id) {
741 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -0600742 /* attempt to disable interrupts in this pipe */
743 if (!polling_mode) {
744 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
745 if (ret) {
746 pr_err("%s: sps_get_config() failed, interrupts"
747 " not disabled\n", __func__);
748 break;
749 }
750 rx_register_event.options = 0;
751 ret = sps_register_event(bam_rx_pipe,
752 &rx_register_event);
753 if (ret) {
754 pr_err("%s: sps_register_event ret = %d\n",
755 __func__, ret);
756 break;
757 }
758 cur_rx_conn.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
759 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
760 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
761 if (ret) {
762 pr_err("%s: sps_set_config() failed, interrupts"
763 " not disabled\n", __func__);
764 break;
765 }
766 polling_mode = 1;
767 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
768 }
Jeff Hugo33dbc002011-08-25 15:52:53 -0600769 break;
770 default:
771 pr_err("%s: recieved unexpected event id %d\n", __func__,
772 notify->event_id);
773 }
774}
775
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776#ifdef CONFIG_DEBUG_FS
777
778static int debug_tbl(char *buf, int max)
779{
780 int i = 0;
781 int j;
782
783 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
784 i += scnprintf(buf + i, max - i,
785 "ch%02d local open=%s remote open=%s\n",
786 j, bam_ch_is_local_open(j) ? "Y" : "N",
787 bam_ch_is_remote_open(j) ? "Y" : "N");
788 }
789
790 return i;
791}
792
793#define DEBUG_BUFMAX 4096
794static char debug_buffer[DEBUG_BUFMAX];
795
796static ssize_t debug_read(struct file *file, char __user *buf,
797 size_t count, loff_t *ppos)
798{
799 int (*fill)(char *buf, int max) = file->private_data;
800 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
801 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
802}
803
804static int debug_open(struct inode *inode, struct file *file)
805{
806 file->private_data = inode->i_private;
807 return 0;
808}
809
810
811static const struct file_operations debug_ops = {
812 .read = debug_read,
813 .open = debug_open,
814};
815
816static void debug_create(const char *name, mode_t mode,
817 struct dentry *dent,
818 int (*fill)(char *buf, int max))
819{
820 debugfs_create_file(name, mode, dent, fill, &debug_ops);
821}
822
823#endif
824
Jeff Hugod98b1082011-10-24 10:30:23 -0600825static void notify_all(int event, unsigned long data)
826{
827 int i;
828
829 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
830 if (bam_ch_is_open(i))
831 bam_ch[i].notify(bam_ch[i].priv, event, data);
832 }
833}
834
835static void kickoff_ul_wakeup_func(struct work_struct *work)
836{
837 read_lock(&ul_wakeup_lock);
838 if (!bam_is_connected) {
839 read_unlock(&ul_wakeup_lock);
840 ul_wakeup();
841 read_lock(&ul_wakeup_lock);
842 ul_packet_written = 1;
843 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
844 }
845 read_unlock(&ul_wakeup_lock);
846}
847
848void msm_bam_dmux_kickoff_ul_wakeup(void)
849{
850 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
851}
852
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600853static void ul_timeout(struct work_struct *work)
854{
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600855 if (in_global_reset)
856 return;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600857 write_lock(&ul_wakeup_lock);
858 if (ul_packet_written) {
859 ul_packet_written = 0;
860 schedule_delayed_work(&ul_timeout_work,
861 msecs_to_jiffies(UL_TIMEOUT_DELAY));
862 } else {
863 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
864 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -0600865 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600866 }
867 write_unlock(&ul_wakeup_lock);
868}
869static void ul_wakeup(void)
870{
871 mutex_lock(&wakeup_lock);
872 if (bam_is_connected) { /* bam got connected before lock grabbed */
873 mutex_unlock(&wakeup_lock);
874 return;
875 }
876 INIT_COMPLETION(ul_wakeup_ack_completion);
877 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
878 wait_for_completion_interruptible_timeout(&ul_wakeup_ack_completion,
879 HZ);
880 wait_for_completion_interruptible_timeout(&bam_connection_completion,
881 HZ);
882
883 bam_is_connected = 1;
884 schedule_delayed_work(&ul_timeout_work,
885 msecs_to_jiffies(UL_TIMEOUT_DELAY));
886 mutex_unlock(&wakeup_lock);
887}
888
889static void reconnect_to_bam(void)
890{
891 int i;
892
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600893 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600894 vote_dfab();
895 i = sps_device_reset(a2_device_handle);
896 if (i)
897 pr_err("%s: device reset failed rc = %d\n", __func__, i);
898 i = sps_connect(bam_tx_pipe, &tx_connection);
899 if (i)
900 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
901 i = sps_connect(bam_rx_pipe, &rx_connection);
902 if (i)
903 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
904 i = sps_register_event(bam_tx_pipe, &tx_register_event);
905 if (i)
906 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
907 i = sps_register_event(bam_rx_pipe, &rx_register_event);
908 if (i)
909 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
910 for (i = 0; i < NUM_BUFFERS; ++i)
911 queue_rx();
912 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600913 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600914 complete_all(&bam_connection_completion);
915}
916
917static void disconnect_to_bam(void)
918{
919 struct list_head *node;
920 struct rx_pkt_info *info;
921
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600922 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600923 INIT_COMPLETION(bam_connection_completion);
924 sps_disconnect(bam_tx_pipe);
925 sps_disconnect(bam_rx_pipe);
926 unvote_dfab();
927 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
928 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
929 while (!list_empty(&bam_rx_pool)) {
930 node = bam_rx_pool.next;
931 list_del(node);
932 info = container_of(node, struct rx_pkt_info, list_node);
933 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
934 DMA_FROM_DEVICE);
935 dev_kfree_skb_any(info->skb);
936 kfree(info);
937 }
938}
939
940static void vote_dfab(void)
941{
942 int rc;
943
944 rc = clk_enable(dfab_clk);
945 if (rc)
946 pr_err("bam_dmux vote for dfab failed rc = %d\n", rc);
947}
948
949static void unvote_dfab(void)
950{
951 clk_disable(dfab_clk);
952}
953
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600954static int restart_notifier_cb(struct notifier_block *this,
955 unsigned long code,
956 void *data)
957{
958 int i;
959 struct list_head *node;
960 struct tx_pkt_info *info;
961 int temp_remote_status;
962
963 if (code != SUBSYS_AFTER_SHUTDOWN)
964 return NOTIFY_DONE;
965
966 in_global_reset = 1;
967 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
968 temp_remote_status = bam_ch_is_remote_open(i);
969 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
970 if (bam_ch_is_local_open(i))
971 bam_ch[i].status |= BAM_CH_IN_RESET;
972 if (temp_remote_status) {
973 platform_device_unregister(bam_ch[i].pdev);
974 bam_ch[i].pdev = platform_device_alloc(
975 bam_ch[i].name, 2);
976 }
977 }
978 /*cleanup UL*/
979 mutex_lock(&bam_tx_pool_lock);
980 while (!list_empty(&bam_tx_pool)) {
981 node = bam_tx_pool.next;
982 list_del(node);
983 info = container_of(node, struct tx_pkt_info,
984 list_node);
985 if (!info->is_cmd) {
986 dma_unmap_single(NULL, info->dma_address,
987 info->skb->len,
988 DMA_TO_DEVICE);
989 dev_kfree_skb_any(info->skb);
990 } else {
991 dma_unmap_single(NULL, info->dma_address,
992 info->len,
993 DMA_TO_DEVICE);
994 kfree(info->skb);
995 }
996 kfree(info);
997 }
998 mutex_unlock(&bam_tx_pool_lock);
999 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1000
1001 return NOTIFY_DONE;
1002}
1003
Jeff Hugoade1f842011-08-03 15:53:59 -06001004static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005{
1006 u32 h;
1007 dma_addr_t dma_addr;
1008 int ret;
1009 void *a2_virt_addr;
1010 int i;
1011
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001012 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 /* init BAM */
1014 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1015 if (!a2_virt_addr) {
1016 pr_err("%s: ioremap failed\n", __func__);
1017 ret = -ENOMEM;
1018 goto register_bam_failed;
1019 }
1020 a2_props.phys_addr = A2_PHYS_BASE;
1021 a2_props.virt_addr = a2_virt_addr;
1022 a2_props.virt_size = A2_PHYS_SIZE;
1023 a2_props.irq = A2_BAM_IRQ;
1024 a2_props.num_pipes = A2_NUM_PIPES;
1025 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
1026 /* need to free on tear down */
1027 ret = sps_register_bam_device(&a2_props, &h);
1028 if (ret < 0) {
1029 pr_err("%s: register bam error %d\n", __func__, ret);
1030 goto register_bam_failed;
1031 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001032 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001033
1034 bam_tx_pipe = sps_alloc_endpoint();
1035 if (bam_tx_pipe == NULL) {
1036 pr_err("%s: tx alloc endpoint failed\n", __func__);
1037 ret = -ENOMEM;
1038 goto register_bam_failed;
1039 }
1040 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1041 if (ret) {
1042 pr_err("%s: tx get config failed %d\n", __func__, ret);
1043 goto tx_get_config_failed;
1044 }
1045
1046 tx_connection.source = SPS_DEV_HANDLE_MEM;
1047 tx_connection.src_pipe_index = 0;
1048 tx_connection.destination = h;
1049 tx_connection.dest_pipe_index = 4;
1050 tx_connection.mode = SPS_MODE_DEST;
1051 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1052 tx_desc_mem_buf.size = 0x800; /* 2k */
1053 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1054 &dma_addr, 0);
1055 if (tx_desc_mem_buf.base == NULL) {
1056 pr_err("%s: tx memory alloc failed\n", __func__);
1057 ret = -ENOMEM;
1058 goto tx_mem_failed;
1059 }
1060 tx_desc_mem_buf.phys_base = dma_addr;
1061 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1062 tx_connection.desc = tx_desc_mem_buf;
1063 tx_connection.event_thresh = 0x10;
1064
1065 ret = sps_connect(bam_tx_pipe, &tx_connection);
1066 if (ret < 0) {
1067 pr_err("%s: tx connect error %d\n", __func__, ret);
1068 goto tx_connect_failed;
1069 }
1070
1071 bam_rx_pipe = sps_alloc_endpoint();
1072 if (bam_rx_pipe == NULL) {
1073 pr_err("%s: rx alloc endpoint failed\n", __func__);
1074 ret = -ENOMEM;
1075 goto tx_connect_failed;
1076 }
1077 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1078 if (ret) {
1079 pr_err("%s: rx get config failed %d\n", __func__, ret);
1080 goto rx_get_config_failed;
1081 }
1082
1083 rx_connection.source = h;
1084 rx_connection.src_pipe_index = 5;
1085 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1086 rx_connection.dest_pipe_index = 1;
1087 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001088 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1089 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 rx_desc_mem_buf.size = 0x800; /* 2k */
1091 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1092 &dma_addr, 0);
1093 if (rx_desc_mem_buf.base == NULL) {
1094 pr_err("%s: rx memory alloc failed\n", __func__);
1095 ret = -ENOMEM;
1096 goto rx_mem_failed;
1097 }
1098 rx_desc_mem_buf.phys_base = dma_addr;
1099 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1100 rx_connection.desc = rx_desc_mem_buf;
1101 rx_connection.event_thresh = 0x10;
1102
1103 ret = sps_connect(bam_rx_pipe, &rx_connection);
1104 if (ret < 0) {
1105 pr_err("%s: rx connect error %d\n", __func__, ret);
1106 goto rx_connect_failed;
1107 }
1108
1109 tx_register_event.options = SPS_O_EOT;
1110 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1111 tx_register_event.xfer_done = NULL;
1112 tx_register_event.callback = bam_mux_tx_notify;
1113 tx_register_event.user = NULL;
1114 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1115 if (ret < 0) {
1116 pr_err("%s: tx register event error %d\n", __func__, ret);
1117 goto rx_event_reg_failed;
1118 }
1119
Jeff Hugo33dbc002011-08-25 15:52:53 -06001120 rx_register_event.options = SPS_O_EOT;
1121 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1122 rx_register_event.xfer_done = NULL;
1123 rx_register_event.callback = bam_mux_rx_notify;
1124 rx_register_event.user = NULL;
1125 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1126 if (ret < 0) {
1127 pr_err("%s: tx register event error %d\n", __func__, ret);
1128 goto rx_event_reg_failed;
1129 }
1130
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001131 bam_mux_initialized = 1;
1132 for (i = 0; i < NUM_BUFFERS; ++i)
1133 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001134 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001135 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001136 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001137 return;
1138
1139rx_event_reg_failed:
1140 sps_disconnect(bam_rx_pipe);
1141rx_connect_failed:
1142 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1143 rx_desc_mem_buf.phys_base);
1144rx_mem_failed:
1145 sps_disconnect(bam_tx_pipe);
1146rx_get_config_failed:
1147 sps_free_endpoint(bam_rx_pipe);
1148tx_connect_failed:
1149 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1150 tx_desc_mem_buf.phys_base);
1151tx_get_config_failed:
1152 sps_free_endpoint(bam_tx_pipe);
1153tx_mem_failed:
1154 sps_deregister_bam_device(h);
1155register_bam_failed:
1156 /*destroy_workqueue(bam_mux_workqueue);*/
1157 /*return ret;*/
1158 return;
1159}
Jeff Hugoade1f842011-08-03 15:53:59 -06001160
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001161static void toggle_apps_ack(void)
1162{
1163 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
1164 smsm_change_state(SMSM_APPS_STATE,
1165 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1166 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1167 clear_bit = ~clear_bit;
1168}
1169
Jeff Hugoade1f842011-08-03 15:53:59 -06001170static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1171{
1172 DBG("%s: smsm activity\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001173 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL)
1174 reconnect_to_bam();
1175 else if (bam_mux_initialized && !(new_state & SMSM_A2_POWER_CONTROL))
1176 disconnect_to_bam();
Jeff Hugoade1f842011-08-03 15:53:59 -06001177 else if (new_state & SMSM_A2_POWER_CONTROL)
1178 bam_init();
1179 else
1180 pr_err("%s: unsupported state change\n", __func__);
1181
1182}
1183
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001184static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1185 uint32_t new_state)
1186{
1187 complete_all(&ul_wakeup_ack_completion);
1188}
1189
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001190static int bam_dmux_probe(struct platform_device *pdev)
1191{
1192 int rc;
1193
1194 DBG("%s probe called\n", __func__);
1195 if (bam_mux_initialized)
1196 return 0;
1197
Stephen Boyd1c51a492011-10-26 12:11:47 -07001198 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001199 if (IS_ERR(dfab_clk)) {
1200 pr_err("%s: did not get dfab clock\n", __func__);
1201 return -EFAULT;
1202 }
1203
1204 rc = clk_set_rate(dfab_clk, 64000000);
1205 if (rc)
1206 pr_err("%s: unable to set dfab clock rate\n", __func__);
1207
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1209 if (!bam_mux_rx_workqueue)
1210 return -ENOMEM;
1211
1212 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1213 if (!bam_mux_tx_workqueue) {
1214 destroy_workqueue(bam_mux_rx_workqueue);
1215 return -ENOMEM;
1216 }
1217
Jeff Hugo7960abd2011-08-02 15:39:38 -06001218 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001219 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001220 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1221 "bam_dmux_ch_%d", rc);
1222 /* bus 2, ie a2 stream 2 */
1223 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1224 if (!bam_ch[rc].pdev) {
1225 pr_err("%s: platform device alloc failed\n", __func__);
1226 destroy_workqueue(bam_mux_rx_workqueue);
1227 destroy_workqueue(bam_mux_tx_workqueue);
1228 return -ENOMEM;
1229 }
1230 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001232 init_completion(&ul_wakeup_ack_completion);
1233 init_completion(&bam_connection_completion);
1234 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
1235
Jeff Hugoade1f842011-08-03 15:53:59 -06001236 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1237 bam_dmux_smsm_cb, NULL);
1238
1239 if (rc) {
1240 destroy_workqueue(bam_mux_rx_workqueue);
1241 destroy_workqueue(bam_mux_tx_workqueue);
1242 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1243 return -ENOMEM;
1244 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001245
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001246 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1247 bam_dmux_smsm_ack_cb, NULL);
1248
1249 if (rc) {
1250 destroy_workqueue(bam_mux_rx_workqueue);
1251 destroy_workqueue(bam_mux_tx_workqueue);
1252 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1253 SMSM_A2_POWER_CONTROL,
1254 bam_dmux_smsm_cb, NULL);
1255 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1256 rc);
1257 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1258 platform_device_put(bam_ch[rc].pdev);
1259 return -ENOMEM;
1260 }
1261
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001262 return 0;
1263}
1264
1265static struct platform_driver bam_dmux_driver = {
1266 .probe = bam_dmux_probe,
1267 .driver = {
1268 .name = "BAM_RMNT",
1269 .owner = THIS_MODULE,
1270 },
1271};
1272
1273static int __init bam_dmux_init(void)
1274{
1275#ifdef CONFIG_DEBUG_FS
1276 struct dentry *dent;
1277
1278 dent = debugfs_create_dir("bam_dmux", 0);
1279 if (!IS_ERR(dent))
1280 debug_create("tbl", 0444, dent, debug_tbl);
1281#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001282 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001283 return platform_driver_register(&bam_dmux_driver);
1284}
1285
Jeff Hugoade1f842011-08-03 15:53:59 -06001286late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001287MODULE_DESCRIPTION("MSM BAM DMUX");
1288MODULE_LICENSE("GPL v2");