blob: d82a6bf346e1dd6c7bf08532d06c082b999cc777 [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);
Jeff Hugoc9749932011-11-02 17:50:40 -0600142static DEFINE_MUTEX(bam_rx_pool_mutexlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600143static LIST_HEAD(bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600144static DEFINE_SPINLOCK(bam_tx_pool_spinlock);
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
Jeff Hugo949080a2011-08-30 11:58:56 -0600160static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161
162static struct workqueue_struct *bam_mux_rx_workqueue;
163static struct workqueue_struct *bam_mux_tx_workqueue;
164
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600165/* A2 power collaspe */
166#define UL_TIMEOUT_DELAY 1000 /* in ms */
167static void toggle_apps_ack(void);
168static void reconnect_to_bam(void);
169static void disconnect_to_bam(void);
170static void ul_wakeup(void);
171static void ul_timeout(struct work_struct *work);
172static void vote_dfab(void);
173static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600174static void kickoff_ul_wakeup_func(struct work_struct *work);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600175
176static int bam_is_connected;
177static DEFINE_MUTEX(wakeup_lock);
178static struct completion ul_wakeup_ack_completion;
179static struct completion bam_connection_completion;
180static struct delayed_work ul_timeout_work;
181static int ul_packet_written;
182static struct clk *dfab_clk;
183static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600184static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600185static int bam_connection_is_active;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600186/* End A2 power collaspe */
187
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600188/* subsystem restart */
189static int restart_notifier_cb(struct notifier_block *this,
190 unsigned long code,
191 void *data);
192
193static struct notifier_block restart_notifier = {
194 .notifier_call = restart_notifier_cb,
195};
196static int in_global_reset;
197/* end subsystem restart */
198
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199#define bam_ch_is_open(x) \
200 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
201
202#define bam_ch_is_local_open(x) \
203 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
204
205#define bam_ch_is_remote_open(x) \
206 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
207
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600208#define bam_ch_is_in_reset(x) \
209 (bam_ch[(x)].status & BAM_CH_IN_RESET)
210
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211static void queue_rx(void)
212{
213 void *ptr;
214 struct rx_pkt_info *info;
215
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600216 if (in_global_reset)
217 return;
218
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219 info = kmalloc(sizeof(struct rx_pkt_info), GFP_KERNEL);
220 if (!info)
221 return; /*need better way to handle this */
222
223 INIT_WORK(&info->work, handle_bam_mux_cmd);
224
225 info->skb = __dev_alloc_skb(BUFFER_SIZE, GFP_KERNEL);
226 ptr = skb_put(info->skb, BUFFER_SIZE);
Jeff Hugo949080a2011-08-30 11:58:56 -0600227
Jeff Hugoc9749932011-11-02 17:50:40 -0600228 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600229 list_add_tail(&info->list_node, &bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600230 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600231
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232 /* need a way to handle error case */
233 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
234 DMA_FROM_DEVICE);
235 sps_transfer_one(bam_rx_pipe, info->dma_address,
Jeff Hugo33dbc002011-08-25 15:52:53 -0600236 BUFFER_SIZE, info,
237 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238}
239
240static void bam_mux_process_data(struct sk_buff *rx_skb)
241{
242 unsigned long flags;
243 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600244 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245
246 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
247
248 rx_skb->data = (unsigned char *)(rx_hdr + 1);
249 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
250 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600251 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600253 event_data = (unsigned long)(rx_skb);
254
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600256 if (bam_ch[rx_hdr->ch_id].notify)
257 bam_ch[rx_hdr->ch_id].notify(
258 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
259 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 else
261 dev_kfree_skb_any(rx_skb);
262 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
263
264 queue_rx();
265}
266
267static void handle_bam_mux_cmd(struct work_struct *work)
268{
269 unsigned long flags;
270 struct bam_mux_hdr *rx_hdr;
271 struct rx_pkt_info *info;
272 struct sk_buff *rx_skb;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600273 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274
275 info = container_of(work, struct rx_pkt_info, work);
276 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600277 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 kfree(info);
279
280 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
281
282 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
283 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
284 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
285 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
286 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
287 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
288 " pad %d ch %d len %d\n", __func__,
289 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
290 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
291 dev_kfree_skb_any(rx_skb);
292 queue_rx();
293 return;
294 }
295 switch (rx_hdr->cmd) {
296 case BAM_MUX_HDR_CMD_DATA:
297 DBG_INC_READ_CNT(rx_hdr->pkt_len);
298 bam_mux_process_data(rx_skb);
299 break;
300 case BAM_MUX_HDR_CMD_OPEN:
301 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
302 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
303 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600305 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
306 if (ret)
307 pr_err("%s: platform_device_add() error: %d\n",
308 __func__, ret);
Eric Holmberge779dba2011-11-04 18:22:01 -0600309 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 break;
311 case BAM_MUX_HDR_CMD_CLOSE:
312 /* probably should drop pending write */
313 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
314 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
315 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600317 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
318 bam_ch[rx_hdr->ch_id].pdev =
319 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
320 if (!bam_ch[rx_hdr->ch_id].pdev)
321 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberge779dba2011-11-04 18:22:01 -0600322 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323 break;
324 default:
325 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
326 " pad %d ch %d len %d\n", __func__,
327 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
328 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
329 dev_kfree_skb_any(rx_skb);
330 queue_rx();
331 return;
332 }
333}
334
335static int bam_mux_write_cmd(void *data, uint32_t len)
336{
337 int rc;
338 struct tx_pkt_info *pkt;
339 dma_addr_t dma_address;
340
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600341 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 if (pkt == NULL) {
343 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
344 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345 return rc;
346 }
347
348 dma_address = dma_map_single(NULL, data, len,
349 DMA_TO_DEVICE);
350 if (!dma_address) {
351 pr_err("%s: dma_map_single() failed\n", __func__);
352 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 return rc;
354 }
355 pkt->skb = (struct sk_buff *)(data);
356 pkt->len = len;
357 pkt->dma_address = dma_address;
358 pkt->is_cmd = 1;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600359 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugoc9749932011-11-02 17:50:40 -0600360 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600361 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600362 spin_unlock(&bam_tx_pool_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
364 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600365 if (rc) {
366 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
367 spin_lock(&bam_tx_pool_spinlock);
368 list_del(&pkt->list_node);
369 spin_unlock(&bam_tx_pool_spinlock);
370 kfree(pkt);
371 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600373 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700374 return rc;
375}
376
377static void bam_mux_write_done(struct work_struct *work)
378{
379 struct sk_buff *skb;
380 struct bam_mux_hdr *hdr;
381 struct tx_pkt_info *info;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600382 unsigned long event_data;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600383 struct list_head *node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600385 if (in_global_reset)
386 return;
Jeff Hugoc9749932011-11-02 17:50:40 -0600387 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600388 node = bam_tx_pool.next;
389 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600390 spin_unlock(&bam_tx_pool_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391 info = container_of(work, struct tx_pkt_info, work);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600392 if (info->is_cmd) {
393 kfree(info->skb);
394 kfree(info);
395 return;
396 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397 skb = info->skb;
398 kfree(info);
399 hdr = (struct bam_mux_hdr *)skb->data;
400 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600401 event_data = (unsigned long)(skb);
402 if (bam_ch[hdr->ch_id].notify)
403 bam_ch[hdr->ch_id].notify(
404 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
405 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406 else
407 dev_kfree_skb_any(skb);
408}
409
410int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
411{
412 int rc = 0;
413 struct bam_mux_hdr *hdr;
414 unsigned long flags;
415 struct sk_buff *new_skb = NULL;
416 dma_addr_t dma_address;
417 struct tx_pkt_info *pkt;
418
419 if (id >= BAM_DMUX_NUM_CHANNELS)
420 return -EINVAL;
421 if (!skb)
422 return -EINVAL;
423 if (!bam_mux_initialized)
424 return -ENODEV;
425
426 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
427 spin_lock_irqsave(&bam_ch[id].lock, flags);
428 if (!bam_ch_is_open(id)) {
429 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
430 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
431 return -ENODEV;
432 }
433 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
434
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600435 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600436 if (!bam_is_connected) {
437 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600438 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600439 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600440 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600441 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600442
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 /* if skb do not have any tailroom for padding,
444 copy the skb into a new expanded skb */
445 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
446 /* revisit, probably dev_alloc_skb and memcpy is effecient */
447 new_skb = skb_copy_expand(skb, skb_headroom(skb),
448 4 - (skb->len & 0x3), GFP_ATOMIC);
449 if (new_skb == NULL) {
450 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600451 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 }
453 dev_kfree_skb_any(skb);
454 skb = new_skb;
455 DBG_INC_WRITE_CPY(skb->len);
456 }
457
458 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
459
460 /* caller should allocate for hdr and padding
461 hdr is fine, padding is tricky */
462 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
463 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
464 hdr->reserved = 0;
465 hdr->ch_id = id;
466 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
467 if (skb->len & 0x3)
468 skb_put(skb, 4 - (skb->len & 0x3));
469
470 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
471
472 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
473 __func__, skb->data, skb->tail, skb->len,
474 hdr->pkt_len, hdr->pad_len);
475
476 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
477 if (pkt == NULL) {
478 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600479 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 }
481
482 dma_address = dma_map_single(NULL, skb->data, skb->len,
483 DMA_TO_DEVICE);
484 if (!dma_address) {
485 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600486 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 }
488 pkt->skb = skb;
489 pkt->dma_address = dma_address;
490 pkt->is_cmd = 0;
491 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugoc9749932011-11-02 17:50:40 -0600492 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600493 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600494 spin_unlock(&bam_tx_pool_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
496 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600497 if (rc) {
498 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
499 spin_lock(&bam_tx_pool_spinlock);
500 list_del(&pkt->list_node);
501 spin_unlock(&bam_tx_pool_spinlock);
502 kfree(pkt);
503 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600504 ul_packet_written = 1;
505 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600507
508write_fail3:
509 kfree(pkt);
510write_fail2:
511 if (new_skb)
512 dev_kfree_skb_any(new_skb);
513write_fail:
514 read_unlock(&ul_wakeup_lock);
515 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516}
517
518int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600519 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520{
521 struct bam_mux_hdr *hdr;
522 unsigned long flags;
523 int rc = 0;
524
525 DBG("%s: opening ch %d\n", __func__, id);
526 if (!bam_mux_initialized)
527 return -ENODEV;
528 if (id >= BAM_DMUX_NUM_CHANNELS)
529 return -EINVAL;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600530 if (notify == NULL)
531 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532
533 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
534 if (hdr == NULL) {
535 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
536 return -ENOMEM;
537 }
538 spin_lock_irqsave(&bam_ch[id].lock, flags);
539 if (bam_ch_is_open(id)) {
540 DBG("%s: Already opened %d\n", __func__, id);
541 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
542 kfree(hdr);
543 goto open_done;
544 }
545 if (!bam_ch_is_remote_open(id)) {
546 DBG("%s: Remote not open; ch: %d\n", __func__, id);
547 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
548 kfree(hdr);
549 rc = -ENODEV;
550 goto open_done;
551 }
552
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600553 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 bam_ch[id].priv = priv;
555 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
556 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
557
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600558 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600559 if (!bam_is_connected) {
560 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600561 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600562 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600563 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600564 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600565
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
567 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
568 hdr->reserved = 0;
569 hdr->ch_id = id;
570 hdr->pkt_len = 0;
571 hdr->pad_len = 0;
572
573 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600574 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575
576open_done:
577 DBG("%s: opened ch %d\n", __func__, id);
578 return rc;
579}
580
581int msm_bam_dmux_close(uint32_t id)
582{
583 struct bam_mux_hdr *hdr;
584 unsigned long flags;
585 int rc;
586
587 if (id >= BAM_DMUX_NUM_CHANNELS)
588 return -EINVAL;
589 DBG("%s: closing ch %d\n", __func__, id);
590 if (!bam_mux_initialized)
591 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600593 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600594 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600595 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600596 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600597 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600598 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600599 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600600
Jeff Hugo061ce672011-10-21 17:15:32 -0600601 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600602 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 bam_ch[id].priv = NULL;
604 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
605 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
606
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600607 if (bam_ch_is_in_reset(id)) {
608 read_unlock(&ul_wakeup_lock);
609 bam_ch[id].status &= ~BAM_CH_IN_RESET;
610 return 0;
611 }
612
Jeff Hugobb5802f2011-11-02 17:10:29 -0600613 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614 if (hdr == NULL) {
615 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600616 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617 return -ENOMEM;
618 }
619 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
620 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
621 hdr->reserved = 0;
622 hdr->ch_id = id;
623 hdr->pkt_len = 0;
624 hdr->pad_len = 0;
625
626 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600627 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628
629 DBG("%s: closed ch %d\n", __func__, id);
630 return rc;
631}
632
Jeff Hugo949080a2011-08-30 11:58:56 -0600633static void rx_timer_work_func(struct work_struct *work)
634{
635 struct sps_iovec iov;
636 struct list_head *node;
637 struct rx_pkt_info *info;
638 int inactive_cycles = 0;
639 int ret;
640 struct sps_connect cur_rx_conn;
641
642 while (1) { /* timer loop */
643 ++inactive_cycles;
644 while (1) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600645 if (in_global_reset)
646 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600647 sps_get_iovec(bam_rx_pipe, &iov);
648 if (iov.addr == 0)
649 break;
650 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600651 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600652 node = bam_rx_pool.next;
653 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600654 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600655 info = container_of(node, struct rx_pkt_info,
656 list_node);
657 handle_bam_mux_cmd(&info->work);
658 }
659
660 if (inactive_cycles == POLLING_INACTIVITY) {
661 /*
662 * attempt to enable interrupts in this pipe
663 * if enabling interrupts fails, continue polling
664 */
665 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
666 if (ret) {
667 pr_err("%s: sps_get_config() failed, interrupts"
668 " not enabled\n", __func__);
669 queue_work(bam_mux_rx_workqueue,
670 &rx_timer_work);
671 return;
672 } else {
673 rx_register_event.options = SPS_O_EOT;
674 /* should check return value */
675 sps_register_event(bam_rx_pipe,
676 &rx_register_event);
677 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
678 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
679 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
680 if (ret) {
681 pr_err("%s: sps_set_config() failed, "
682 "interrupts not enabled\n",
683 __func__);
684 queue_work(bam_mux_rx_workqueue,
685 &rx_timer_work);
686 return;
687 }
688 polling_mode = 0;
689 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600690 if (in_global_reset)
691 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600692 /* handle race condition - missed packet? */
693 sps_get_iovec(bam_rx_pipe, &iov);
694 if (iov.addr == 0)
695 return;
696 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600697 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600698 node = bam_rx_pool.next;
699 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600700 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600701 info = container_of(node, struct rx_pkt_info,
702 list_node);
703 handle_bam_mux_cmd(&info->work);
704 return;
705 }
706
707 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
708 }
709}
710
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711static void bam_mux_tx_notify(struct sps_event_notify *notify)
712{
713 struct tx_pkt_info *pkt;
714
715 DBG("%s: event %d notified\n", __func__, notify->event_id);
716
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600717 if (in_global_reset)
718 return;
719
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 switch (notify->event_id) {
721 case SPS_EVENT_EOT:
722 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600723 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724 dma_unmap_single(NULL, pkt->dma_address,
725 pkt->skb->len,
726 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600727 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728 dma_unmap_single(NULL, pkt->dma_address,
729 pkt->len,
730 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600731 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732 break;
733 default:
734 pr_err("%s: recieved unexpected event id %d\n", __func__,
735 notify->event_id);
736 }
737}
738
Jeff Hugo33dbc002011-08-25 15:52:53 -0600739static void bam_mux_rx_notify(struct sps_event_notify *notify)
740{
Jeff Hugo949080a2011-08-30 11:58:56 -0600741 int ret;
742 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600743
744 DBG("%s: event %d notified\n", __func__, notify->event_id);
745
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600746 if (in_global_reset)
747 return;
748
Jeff Hugo33dbc002011-08-25 15:52:53 -0600749 switch (notify->event_id) {
750 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -0600751 /* attempt to disable interrupts in this pipe */
752 if (!polling_mode) {
753 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
754 if (ret) {
755 pr_err("%s: sps_get_config() failed, interrupts"
756 " not disabled\n", __func__);
757 break;
758 }
759 rx_register_event.options = 0;
760 ret = sps_register_event(bam_rx_pipe,
761 &rx_register_event);
762 if (ret) {
763 pr_err("%s: sps_register_event ret = %d\n",
764 __func__, ret);
765 break;
766 }
767 cur_rx_conn.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
768 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
769 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
770 if (ret) {
771 pr_err("%s: sps_set_config() failed, interrupts"
772 " not disabled\n", __func__);
773 break;
774 }
775 polling_mode = 1;
776 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
777 }
Jeff Hugo33dbc002011-08-25 15:52:53 -0600778 break;
779 default:
780 pr_err("%s: recieved unexpected event id %d\n", __func__,
781 notify->event_id);
782 }
783}
784
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785#ifdef CONFIG_DEBUG_FS
786
787static int debug_tbl(char *buf, int max)
788{
789 int i = 0;
790 int j;
791
792 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
793 i += scnprintf(buf + i, max - i,
794 "ch%02d local open=%s remote open=%s\n",
795 j, bam_ch_is_local_open(j) ? "Y" : "N",
796 bam_ch_is_remote_open(j) ? "Y" : "N");
797 }
798
799 return i;
800}
801
802#define DEBUG_BUFMAX 4096
803static char debug_buffer[DEBUG_BUFMAX];
804
805static ssize_t debug_read(struct file *file, char __user *buf,
806 size_t count, loff_t *ppos)
807{
808 int (*fill)(char *buf, int max) = file->private_data;
809 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
810 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
811}
812
813static int debug_open(struct inode *inode, struct file *file)
814{
815 file->private_data = inode->i_private;
816 return 0;
817}
818
819
820static const struct file_operations debug_ops = {
821 .read = debug_read,
822 .open = debug_open,
823};
824
825static void debug_create(const char *name, mode_t mode,
826 struct dentry *dent,
827 int (*fill)(char *buf, int max))
828{
829 debugfs_create_file(name, mode, dent, fill, &debug_ops);
830}
831
832#endif
833
Jeff Hugod98b1082011-10-24 10:30:23 -0600834static void notify_all(int event, unsigned long data)
835{
836 int i;
837
838 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
839 if (bam_ch_is_open(i))
840 bam_ch[i].notify(bam_ch[i].priv, event, data);
841 }
842}
843
844static void kickoff_ul_wakeup_func(struct work_struct *work)
845{
846 read_lock(&ul_wakeup_lock);
847 if (!bam_is_connected) {
848 read_unlock(&ul_wakeup_lock);
849 ul_wakeup();
850 read_lock(&ul_wakeup_lock);
851 ul_packet_written = 1;
852 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
853 }
854 read_unlock(&ul_wakeup_lock);
855}
856
857void msm_bam_dmux_kickoff_ul_wakeup(void)
858{
859 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
860}
861
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600862static void ul_timeout(struct work_struct *work)
863{
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600864 if (in_global_reset)
865 return;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600866 write_lock(&ul_wakeup_lock);
867 if (ul_packet_written) {
868 ul_packet_written = 0;
869 schedule_delayed_work(&ul_timeout_work,
870 msecs_to_jiffies(UL_TIMEOUT_DELAY));
871 } else {
872 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
873 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -0600874 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600875 }
876 write_unlock(&ul_wakeup_lock);
877}
878static void ul_wakeup(void)
879{
880 mutex_lock(&wakeup_lock);
881 if (bam_is_connected) { /* bam got connected before lock grabbed */
882 mutex_unlock(&wakeup_lock);
883 return;
884 }
885 INIT_COMPLETION(ul_wakeup_ack_completion);
886 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
887 wait_for_completion_interruptible_timeout(&ul_wakeup_ack_completion,
888 HZ);
889 wait_for_completion_interruptible_timeout(&bam_connection_completion,
890 HZ);
891
892 bam_is_connected = 1;
893 schedule_delayed_work(&ul_timeout_work,
894 msecs_to_jiffies(UL_TIMEOUT_DELAY));
895 mutex_unlock(&wakeup_lock);
896}
897
898static void reconnect_to_bam(void)
899{
900 int i;
901
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600902 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600903 vote_dfab();
904 i = sps_device_reset(a2_device_handle);
905 if (i)
906 pr_err("%s: device reset failed rc = %d\n", __func__, i);
907 i = sps_connect(bam_tx_pipe, &tx_connection);
908 if (i)
909 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
910 i = sps_connect(bam_rx_pipe, &rx_connection);
911 if (i)
912 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
913 i = sps_register_event(bam_tx_pipe, &tx_register_event);
914 if (i)
915 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
916 i = sps_register_event(bam_rx_pipe, &rx_register_event);
917 if (i)
918 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
919 for (i = 0; i < NUM_BUFFERS; ++i)
920 queue_rx();
921 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600922 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600923 complete_all(&bam_connection_completion);
924}
925
926static void disconnect_to_bam(void)
927{
928 struct list_head *node;
929 struct rx_pkt_info *info;
930
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600931 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600932 INIT_COMPLETION(bam_connection_completion);
933 sps_disconnect(bam_tx_pipe);
934 sps_disconnect(bam_rx_pipe);
935 unvote_dfab();
936 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
937 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
938 while (!list_empty(&bam_rx_pool)) {
939 node = bam_rx_pool.next;
940 list_del(node);
941 info = container_of(node, struct rx_pkt_info, list_node);
942 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
943 DMA_FROM_DEVICE);
944 dev_kfree_skb_any(info->skb);
945 kfree(info);
946 }
947}
948
949static void vote_dfab(void)
950{
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600951}
952
953static void unvote_dfab(void)
954{
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600955}
956
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600957static int restart_notifier_cb(struct notifier_block *this,
958 unsigned long code,
959 void *data)
960{
961 int i;
962 struct list_head *node;
963 struct tx_pkt_info *info;
964 int temp_remote_status;
965
966 if (code != SUBSYS_AFTER_SHUTDOWN)
967 return NOTIFY_DONE;
968
969 in_global_reset = 1;
970 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
971 temp_remote_status = bam_ch_is_remote_open(i);
972 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
973 if (bam_ch_is_local_open(i))
974 bam_ch[i].status |= BAM_CH_IN_RESET;
975 if (temp_remote_status) {
976 platform_device_unregister(bam_ch[i].pdev);
977 bam_ch[i].pdev = platform_device_alloc(
978 bam_ch[i].name, 2);
979 }
980 }
981 /*cleanup UL*/
Jeff Hugoc9749932011-11-02 17:50:40 -0600982 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600983 while (!list_empty(&bam_tx_pool)) {
984 node = bam_tx_pool.next;
985 list_del(node);
986 info = container_of(node, struct tx_pkt_info,
987 list_node);
988 if (!info->is_cmd) {
989 dma_unmap_single(NULL, info->dma_address,
990 info->skb->len,
991 DMA_TO_DEVICE);
992 dev_kfree_skb_any(info->skb);
993 } else {
994 dma_unmap_single(NULL, info->dma_address,
995 info->len,
996 DMA_TO_DEVICE);
997 kfree(info->skb);
998 }
999 kfree(info);
1000 }
Jeff Hugoc9749932011-11-02 17:50:40 -06001001 spin_unlock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001002 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1003
1004 return NOTIFY_DONE;
1005}
1006
Jeff Hugoade1f842011-08-03 15:53:59 -06001007static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008{
1009 u32 h;
1010 dma_addr_t dma_addr;
1011 int ret;
1012 void *a2_virt_addr;
1013 int i;
1014
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001015 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016 /* init BAM */
1017 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1018 if (!a2_virt_addr) {
1019 pr_err("%s: ioremap failed\n", __func__);
1020 ret = -ENOMEM;
1021 goto register_bam_failed;
1022 }
1023 a2_props.phys_addr = A2_PHYS_BASE;
1024 a2_props.virt_addr = a2_virt_addr;
1025 a2_props.virt_size = A2_PHYS_SIZE;
1026 a2_props.irq = A2_BAM_IRQ;
1027 a2_props.num_pipes = A2_NUM_PIPES;
1028 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
1029 /* need to free on tear down */
1030 ret = sps_register_bam_device(&a2_props, &h);
1031 if (ret < 0) {
1032 pr_err("%s: register bam error %d\n", __func__, ret);
1033 goto register_bam_failed;
1034 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001035 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001036
1037 bam_tx_pipe = sps_alloc_endpoint();
1038 if (bam_tx_pipe == NULL) {
1039 pr_err("%s: tx alloc endpoint failed\n", __func__);
1040 ret = -ENOMEM;
1041 goto register_bam_failed;
1042 }
1043 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1044 if (ret) {
1045 pr_err("%s: tx get config failed %d\n", __func__, ret);
1046 goto tx_get_config_failed;
1047 }
1048
1049 tx_connection.source = SPS_DEV_HANDLE_MEM;
1050 tx_connection.src_pipe_index = 0;
1051 tx_connection.destination = h;
1052 tx_connection.dest_pipe_index = 4;
1053 tx_connection.mode = SPS_MODE_DEST;
1054 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1055 tx_desc_mem_buf.size = 0x800; /* 2k */
1056 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1057 &dma_addr, 0);
1058 if (tx_desc_mem_buf.base == NULL) {
1059 pr_err("%s: tx memory alloc failed\n", __func__);
1060 ret = -ENOMEM;
1061 goto tx_mem_failed;
1062 }
1063 tx_desc_mem_buf.phys_base = dma_addr;
1064 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1065 tx_connection.desc = tx_desc_mem_buf;
1066 tx_connection.event_thresh = 0x10;
1067
1068 ret = sps_connect(bam_tx_pipe, &tx_connection);
1069 if (ret < 0) {
1070 pr_err("%s: tx connect error %d\n", __func__, ret);
1071 goto tx_connect_failed;
1072 }
1073
1074 bam_rx_pipe = sps_alloc_endpoint();
1075 if (bam_rx_pipe == NULL) {
1076 pr_err("%s: rx alloc endpoint failed\n", __func__);
1077 ret = -ENOMEM;
1078 goto tx_connect_failed;
1079 }
1080 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1081 if (ret) {
1082 pr_err("%s: rx get config failed %d\n", __func__, ret);
1083 goto rx_get_config_failed;
1084 }
1085
1086 rx_connection.source = h;
1087 rx_connection.src_pipe_index = 5;
1088 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1089 rx_connection.dest_pipe_index = 1;
1090 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001091 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1092 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093 rx_desc_mem_buf.size = 0x800; /* 2k */
1094 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1095 &dma_addr, 0);
1096 if (rx_desc_mem_buf.base == NULL) {
1097 pr_err("%s: rx memory alloc failed\n", __func__);
1098 ret = -ENOMEM;
1099 goto rx_mem_failed;
1100 }
1101 rx_desc_mem_buf.phys_base = dma_addr;
1102 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1103 rx_connection.desc = rx_desc_mem_buf;
1104 rx_connection.event_thresh = 0x10;
1105
1106 ret = sps_connect(bam_rx_pipe, &rx_connection);
1107 if (ret < 0) {
1108 pr_err("%s: rx connect error %d\n", __func__, ret);
1109 goto rx_connect_failed;
1110 }
1111
1112 tx_register_event.options = SPS_O_EOT;
1113 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1114 tx_register_event.xfer_done = NULL;
1115 tx_register_event.callback = bam_mux_tx_notify;
1116 tx_register_event.user = NULL;
1117 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1118 if (ret < 0) {
1119 pr_err("%s: tx register event error %d\n", __func__, ret);
1120 goto rx_event_reg_failed;
1121 }
1122
Jeff Hugo33dbc002011-08-25 15:52:53 -06001123 rx_register_event.options = SPS_O_EOT;
1124 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1125 rx_register_event.xfer_done = NULL;
1126 rx_register_event.callback = bam_mux_rx_notify;
1127 rx_register_event.user = NULL;
1128 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1129 if (ret < 0) {
1130 pr_err("%s: tx register event error %d\n", __func__, ret);
1131 goto rx_event_reg_failed;
1132 }
1133
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001134 bam_mux_initialized = 1;
1135 for (i = 0; i < NUM_BUFFERS; ++i)
1136 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001137 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001138 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001139 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140 return;
1141
1142rx_event_reg_failed:
1143 sps_disconnect(bam_rx_pipe);
1144rx_connect_failed:
1145 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1146 rx_desc_mem_buf.phys_base);
1147rx_mem_failed:
1148 sps_disconnect(bam_tx_pipe);
1149rx_get_config_failed:
1150 sps_free_endpoint(bam_rx_pipe);
1151tx_connect_failed:
1152 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1153 tx_desc_mem_buf.phys_base);
1154tx_get_config_failed:
1155 sps_free_endpoint(bam_tx_pipe);
1156tx_mem_failed:
1157 sps_deregister_bam_device(h);
1158register_bam_failed:
1159 /*destroy_workqueue(bam_mux_workqueue);*/
1160 /*return ret;*/
1161 return;
1162}
Jeff Hugoade1f842011-08-03 15:53:59 -06001163
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001164static void toggle_apps_ack(void)
1165{
1166 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
1167 smsm_change_state(SMSM_APPS_STATE,
1168 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1169 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1170 clear_bit = ~clear_bit;
1171}
1172
Jeff Hugoade1f842011-08-03 15:53:59 -06001173static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1174{
1175 DBG("%s: smsm activity\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001176 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL)
1177 reconnect_to_bam();
1178 else if (bam_mux_initialized && !(new_state & SMSM_A2_POWER_CONTROL))
1179 disconnect_to_bam();
Jeff Hugoade1f842011-08-03 15:53:59 -06001180 else if (new_state & SMSM_A2_POWER_CONTROL)
1181 bam_init();
1182 else
1183 pr_err("%s: unsupported state change\n", __func__);
1184
1185}
1186
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001187static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1188 uint32_t new_state)
1189{
1190 complete_all(&ul_wakeup_ack_completion);
1191}
1192
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193static int bam_dmux_probe(struct platform_device *pdev)
1194{
1195 int rc;
1196
1197 DBG("%s probe called\n", __func__);
1198 if (bam_mux_initialized)
1199 return 0;
1200
Stephen Boyd1c51a492011-10-26 12:11:47 -07001201 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001202 if (IS_ERR(dfab_clk)) {
1203 pr_err("%s: did not get dfab clock\n", __func__);
1204 return -EFAULT;
1205 }
1206
1207 rc = clk_set_rate(dfab_clk, 64000000);
1208 if (rc)
1209 pr_err("%s: unable to set dfab clock rate\n", __func__);
1210
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1212 if (!bam_mux_rx_workqueue)
1213 return -ENOMEM;
1214
1215 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1216 if (!bam_mux_tx_workqueue) {
1217 destroy_workqueue(bam_mux_rx_workqueue);
1218 return -ENOMEM;
1219 }
1220
Jeff Hugo7960abd2011-08-02 15:39:38 -06001221 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001222 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001223 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1224 "bam_dmux_ch_%d", rc);
1225 /* bus 2, ie a2 stream 2 */
1226 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1227 if (!bam_ch[rc].pdev) {
1228 pr_err("%s: platform device alloc failed\n", __func__);
1229 destroy_workqueue(bam_mux_rx_workqueue);
1230 destroy_workqueue(bam_mux_tx_workqueue);
1231 return -ENOMEM;
1232 }
1233 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001234
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001235 init_completion(&ul_wakeup_ack_completion);
1236 init_completion(&bam_connection_completion);
1237 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
1238
Jeff Hugoade1f842011-08-03 15:53:59 -06001239 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1240 bam_dmux_smsm_cb, NULL);
1241
1242 if (rc) {
1243 destroy_workqueue(bam_mux_rx_workqueue);
1244 destroy_workqueue(bam_mux_tx_workqueue);
1245 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1246 return -ENOMEM;
1247 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001248
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001249 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1250 bam_dmux_smsm_ack_cb, NULL);
1251
1252 if (rc) {
1253 destroy_workqueue(bam_mux_rx_workqueue);
1254 destroy_workqueue(bam_mux_tx_workqueue);
1255 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1256 SMSM_A2_POWER_CONTROL,
1257 bam_dmux_smsm_cb, NULL);
1258 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1259 rc);
1260 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1261 platform_device_put(bam_ch[rc].pdev);
1262 return -ENOMEM;
1263 }
1264
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001265 return 0;
1266}
1267
1268static struct platform_driver bam_dmux_driver = {
1269 .probe = bam_dmux_probe,
1270 .driver = {
1271 .name = "BAM_RMNT",
1272 .owner = THIS_MODULE,
1273 },
1274};
1275
1276static int __init bam_dmux_init(void)
1277{
1278#ifdef CONFIG_DEBUG_FS
1279 struct dentry *dent;
1280
1281 dent = debugfs_create_dir("bam_dmux", 0);
1282 if (!IS_ERR(dent))
1283 debug_create("tbl", 0444, dent, debug_tbl);
1284#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001285 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286 return platform_driver_register(&bam_dmux_driver);
1287}
1288
Jeff Hugoade1f842011-08-03 15:53:59 -06001289late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290MODULE_DESCRIPTION("MSM BAM DMUX");
1291MODULE_LICENSE("GPL v2");