blob: cb0406f31311ff64e86cd001d23a13ae5586cd3b [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);
Eric Holmberg5d775432011-11-09 10:23:35 -0700526 if (!bam_mux_initialized) {
527 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700529 }
530 if (id >= BAM_DMUX_NUM_CHANNELS) {
531 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700533 }
534 if (notify == NULL) {
535 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600536 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700537 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538
539 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
540 if (hdr == NULL) {
541 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
542 return -ENOMEM;
543 }
544 spin_lock_irqsave(&bam_ch[id].lock, flags);
545 if (bam_ch_is_open(id)) {
546 DBG("%s: Already opened %d\n", __func__, id);
547 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
548 kfree(hdr);
549 goto open_done;
550 }
551 if (!bam_ch_is_remote_open(id)) {
552 DBG("%s: Remote not open; ch: %d\n", __func__, id);
553 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
554 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700555 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556 }
557
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600558 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 bam_ch[id].priv = priv;
560 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
561 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
562
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600563 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600564 if (!bam_is_connected) {
565 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600566 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600567 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600568 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600569 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600570
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
572 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
573 hdr->reserved = 0;
574 hdr->ch_id = id;
575 hdr->pkt_len = 0;
576 hdr->pad_len = 0;
577
578 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600579 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580
581open_done:
582 DBG("%s: opened ch %d\n", __func__, id);
583 return rc;
584}
585
586int msm_bam_dmux_close(uint32_t id)
587{
588 struct bam_mux_hdr *hdr;
589 unsigned long flags;
590 int rc;
591
592 if (id >= BAM_DMUX_NUM_CHANNELS)
593 return -EINVAL;
594 DBG("%s: closing ch %d\n", __func__, id);
595 if (!bam_mux_initialized)
596 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600598 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600599 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600600 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600601 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600602 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600603 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600604 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600605
Jeff Hugo061ce672011-10-21 17:15:32 -0600606 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600607 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608 bam_ch[id].priv = NULL;
609 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
610 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
611
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600612 if (bam_ch_is_in_reset(id)) {
613 read_unlock(&ul_wakeup_lock);
614 bam_ch[id].status &= ~BAM_CH_IN_RESET;
615 return 0;
616 }
617
Jeff Hugobb5802f2011-11-02 17:10:29 -0600618 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619 if (hdr == NULL) {
620 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600621 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622 return -ENOMEM;
623 }
624 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
625 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
626 hdr->reserved = 0;
627 hdr->ch_id = id;
628 hdr->pkt_len = 0;
629 hdr->pad_len = 0;
630
631 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600632 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633
634 DBG("%s: closed ch %d\n", __func__, id);
635 return rc;
636}
637
Jeff Hugo949080a2011-08-30 11:58:56 -0600638static void rx_timer_work_func(struct work_struct *work)
639{
640 struct sps_iovec iov;
641 struct list_head *node;
642 struct rx_pkt_info *info;
643 int inactive_cycles = 0;
644 int ret;
645 struct sps_connect cur_rx_conn;
646
647 while (1) { /* timer loop */
648 ++inactive_cycles;
649 while (1) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600650 if (in_global_reset)
651 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600652 sps_get_iovec(bam_rx_pipe, &iov);
653 if (iov.addr == 0)
654 break;
655 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600656 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600657 node = bam_rx_pool.next;
658 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600659 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600660 info = container_of(node, struct rx_pkt_info,
661 list_node);
662 handle_bam_mux_cmd(&info->work);
663 }
664
665 if (inactive_cycles == POLLING_INACTIVITY) {
666 /*
667 * attempt to enable interrupts in this pipe
668 * if enabling interrupts fails, continue polling
669 */
670 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
671 if (ret) {
672 pr_err("%s: sps_get_config() failed, interrupts"
673 " not enabled\n", __func__);
674 queue_work(bam_mux_rx_workqueue,
675 &rx_timer_work);
676 return;
677 } else {
678 rx_register_event.options = SPS_O_EOT;
679 /* should check return value */
680 sps_register_event(bam_rx_pipe,
681 &rx_register_event);
682 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
683 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
684 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
685 if (ret) {
686 pr_err("%s: sps_set_config() failed, "
687 "interrupts not enabled\n",
688 __func__);
689 queue_work(bam_mux_rx_workqueue,
690 &rx_timer_work);
691 return;
692 }
693 polling_mode = 0;
694 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600695 if (in_global_reset)
696 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600697 /* handle race condition - missed packet? */
698 sps_get_iovec(bam_rx_pipe, &iov);
699 if (iov.addr == 0)
700 return;
701 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600702 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600703 node = bam_rx_pool.next;
704 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600705 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600706 info = container_of(node, struct rx_pkt_info,
707 list_node);
708 handle_bam_mux_cmd(&info->work);
709 return;
710 }
711
712 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
713 }
714}
715
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716static void bam_mux_tx_notify(struct sps_event_notify *notify)
717{
718 struct tx_pkt_info *pkt;
719
720 DBG("%s: event %d notified\n", __func__, notify->event_id);
721
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600722 if (in_global_reset)
723 return;
724
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 switch (notify->event_id) {
726 case SPS_EVENT_EOT:
727 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600728 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729 dma_unmap_single(NULL, pkt->dma_address,
730 pkt->skb->len,
731 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600732 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733 dma_unmap_single(NULL, pkt->dma_address,
734 pkt->len,
735 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600736 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 break;
738 default:
739 pr_err("%s: recieved unexpected event id %d\n", __func__,
740 notify->event_id);
741 }
742}
743
Jeff Hugo33dbc002011-08-25 15:52:53 -0600744static void bam_mux_rx_notify(struct sps_event_notify *notify)
745{
Jeff Hugo949080a2011-08-30 11:58:56 -0600746 int ret;
747 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600748
749 DBG("%s: event %d notified\n", __func__, notify->event_id);
750
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600751 if (in_global_reset)
752 return;
753
Jeff Hugo33dbc002011-08-25 15:52:53 -0600754 switch (notify->event_id) {
755 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -0600756 /* attempt to disable interrupts in this pipe */
757 if (!polling_mode) {
758 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
759 if (ret) {
760 pr_err("%s: sps_get_config() failed, interrupts"
761 " not disabled\n", __func__);
762 break;
763 }
764 rx_register_event.options = 0;
765 ret = sps_register_event(bam_rx_pipe,
766 &rx_register_event);
767 if (ret) {
768 pr_err("%s: sps_register_event ret = %d\n",
769 __func__, ret);
770 break;
771 }
772 cur_rx_conn.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
773 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
774 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
775 if (ret) {
776 pr_err("%s: sps_set_config() failed, interrupts"
777 " not disabled\n", __func__);
778 break;
779 }
780 polling_mode = 1;
781 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
782 }
Jeff Hugo33dbc002011-08-25 15:52:53 -0600783 break;
784 default:
785 pr_err("%s: recieved unexpected event id %d\n", __func__,
786 notify->event_id);
787 }
788}
789
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790#ifdef CONFIG_DEBUG_FS
791
792static int debug_tbl(char *buf, int max)
793{
794 int i = 0;
795 int j;
796
797 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
798 i += scnprintf(buf + i, max - i,
799 "ch%02d local open=%s remote open=%s\n",
800 j, bam_ch_is_local_open(j) ? "Y" : "N",
801 bam_ch_is_remote_open(j) ? "Y" : "N");
802 }
803
804 return i;
805}
806
807#define DEBUG_BUFMAX 4096
808static char debug_buffer[DEBUG_BUFMAX];
809
810static ssize_t debug_read(struct file *file, char __user *buf,
811 size_t count, loff_t *ppos)
812{
813 int (*fill)(char *buf, int max) = file->private_data;
814 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
815 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
816}
817
818static int debug_open(struct inode *inode, struct file *file)
819{
820 file->private_data = inode->i_private;
821 return 0;
822}
823
824
825static const struct file_operations debug_ops = {
826 .read = debug_read,
827 .open = debug_open,
828};
829
830static void debug_create(const char *name, mode_t mode,
831 struct dentry *dent,
832 int (*fill)(char *buf, int max))
833{
834 debugfs_create_file(name, mode, dent, fill, &debug_ops);
835}
836
837#endif
838
Jeff Hugod98b1082011-10-24 10:30:23 -0600839static void notify_all(int event, unsigned long data)
840{
841 int i;
842
843 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
844 if (bam_ch_is_open(i))
845 bam_ch[i].notify(bam_ch[i].priv, event, data);
846 }
847}
848
849static void kickoff_ul_wakeup_func(struct work_struct *work)
850{
851 read_lock(&ul_wakeup_lock);
852 if (!bam_is_connected) {
853 read_unlock(&ul_wakeup_lock);
854 ul_wakeup();
855 read_lock(&ul_wakeup_lock);
856 ul_packet_written = 1;
857 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
858 }
859 read_unlock(&ul_wakeup_lock);
860}
861
862void msm_bam_dmux_kickoff_ul_wakeup(void)
863{
864 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
865}
866
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600867static void ul_timeout(struct work_struct *work)
868{
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600869 if (in_global_reset)
870 return;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600871 write_lock(&ul_wakeup_lock);
872 if (ul_packet_written) {
873 ul_packet_written = 0;
874 schedule_delayed_work(&ul_timeout_work,
875 msecs_to_jiffies(UL_TIMEOUT_DELAY));
876 } else {
877 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
878 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -0600879 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600880 }
881 write_unlock(&ul_wakeup_lock);
882}
883static void ul_wakeup(void)
884{
885 mutex_lock(&wakeup_lock);
886 if (bam_is_connected) { /* bam got connected before lock grabbed */
887 mutex_unlock(&wakeup_lock);
888 return;
889 }
890 INIT_COMPLETION(ul_wakeup_ack_completion);
891 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
892 wait_for_completion_interruptible_timeout(&ul_wakeup_ack_completion,
893 HZ);
894 wait_for_completion_interruptible_timeout(&bam_connection_completion,
895 HZ);
896
897 bam_is_connected = 1;
898 schedule_delayed_work(&ul_timeout_work,
899 msecs_to_jiffies(UL_TIMEOUT_DELAY));
900 mutex_unlock(&wakeup_lock);
901}
902
903static void reconnect_to_bam(void)
904{
905 int i;
906
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600907 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600908 vote_dfab();
909 i = sps_device_reset(a2_device_handle);
910 if (i)
911 pr_err("%s: device reset failed rc = %d\n", __func__, i);
912 i = sps_connect(bam_tx_pipe, &tx_connection);
913 if (i)
914 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
915 i = sps_connect(bam_rx_pipe, &rx_connection);
916 if (i)
917 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
918 i = sps_register_event(bam_tx_pipe, &tx_register_event);
919 if (i)
920 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
921 i = sps_register_event(bam_rx_pipe, &rx_register_event);
922 if (i)
923 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
924 for (i = 0; i < NUM_BUFFERS; ++i)
925 queue_rx();
926 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600927 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600928 complete_all(&bam_connection_completion);
929}
930
931static void disconnect_to_bam(void)
932{
933 struct list_head *node;
934 struct rx_pkt_info *info;
935
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600936 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600937 INIT_COMPLETION(bam_connection_completion);
938 sps_disconnect(bam_tx_pipe);
939 sps_disconnect(bam_rx_pipe);
940 unvote_dfab();
941 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
942 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
943 while (!list_empty(&bam_rx_pool)) {
944 node = bam_rx_pool.next;
945 list_del(node);
946 info = container_of(node, struct rx_pkt_info, list_node);
947 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
948 DMA_FROM_DEVICE);
949 dev_kfree_skb_any(info->skb);
950 kfree(info);
951 }
952}
953
954static void vote_dfab(void)
955{
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600956}
957
958static void unvote_dfab(void)
959{
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600960}
961
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600962static int restart_notifier_cb(struct notifier_block *this,
963 unsigned long code,
964 void *data)
965{
966 int i;
967 struct list_head *node;
968 struct tx_pkt_info *info;
969 int temp_remote_status;
970
971 if (code != SUBSYS_AFTER_SHUTDOWN)
972 return NOTIFY_DONE;
973
974 in_global_reset = 1;
975 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
976 temp_remote_status = bam_ch_is_remote_open(i);
977 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
978 if (bam_ch_is_local_open(i))
979 bam_ch[i].status |= BAM_CH_IN_RESET;
980 if (temp_remote_status) {
981 platform_device_unregister(bam_ch[i].pdev);
982 bam_ch[i].pdev = platform_device_alloc(
983 bam_ch[i].name, 2);
984 }
985 }
986 /*cleanup UL*/
Jeff Hugoc9749932011-11-02 17:50:40 -0600987 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600988 while (!list_empty(&bam_tx_pool)) {
989 node = bam_tx_pool.next;
990 list_del(node);
991 info = container_of(node, struct tx_pkt_info,
992 list_node);
993 if (!info->is_cmd) {
994 dma_unmap_single(NULL, info->dma_address,
995 info->skb->len,
996 DMA_TO_DEVICE);
997 dev_kfree_skb_any(info->skb);
998 } else {
999 dma_unmap_single(NULL, info->dma_address,
1000 info->len,
1001 DMA_TO_DEVICE);
1002 kfree(info->skb);
1003 }
1004 kfree(info);
1005 }
Jeff Hugoc9749932011-11-02 17:50:40 -06001006 spin_unlock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001007 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1008
1009 return NOTIFY_DONE;
1010}
1011
Jeff Hugoade1f842011-08-03 15:53:59 -06001012static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013{
1014 u32 h;
1015 dma_addr_t dma_addr;
1016 int ret;
1017 void *a2_virt_addr;
1018 int i;
1019
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001020 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 /* init BAM */
1022 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1023 if (!a2_virt_addr) {
1024 pr_err("%s: ioremap failed\n", __func__);
1025 ret = -ENOMEM;
1026 goto register_bam_failed;
1027 }
1028 a2_props.phys_addr = A2_PHYS_BASE;
1029 a2_props.virt_addr = a2_virt_addr;
1030 a2_props.virt_size = A2_PHYS_SIZE;
1031 a2_props.irq = A2_BAM_IRQ;
1032 a2_props.num_pipes = A2_NUM_PIPES;
1033 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
1034 /* need to free on tear down */
1035 ret = sps_register_bam_device(&a2_props, &h);
1036 if (ret < 0) {
1037 pr_err("%s: register bam error %d\n", __func__, ret);
1038 goto register_bam_failed;
1039 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001040 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041
1042 bam_tx_pipe = sps_alloc_endpoint();
1043 if (bam_tx_pipe == NULL) {
1044 pr_err("%s: tx alloc endpoint failed\n", __func__);
1045 ret = -ENOMEM;
1046 goto register_bam_failed;
1047 }
1048 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1049 if (ret) {
1050 pr_err("%s: tx get config failed %d\n", __func__, ret);
1051 goto tx_get_config_failed;
1052 }
1053
1054 tx_connection.source = SPS_DEV_HANDLE_MEM;
1055 tx_connection.src_pipe_index = 0;
1056 tx_connection.destination = h;
1057 tx_connection.dest_pipe_index = 4;
1058 tx_connection.mode = SPS_MODE_DEST;
1059 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1060 tx_desc_mem_buf.size = 0x800; /* 2k */
1061 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1062 &dma_addr, 0);
1063 if (tx_desc_mem_buf.base == NULL) {
1064 pr_err("%s: tx memory alloc failed\n", __func__);
1065 ret = -ENOMEM;
1066 goto tx_mem_failed;
1067 }
1068 tx_desc_mem_buf.phys_base = dma_addr;
1069 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1070 tx_connection.desc = tx_desc_mem_buf;
1071 tx_connection.event_thresh = 0x10;
1072
1073 ret = sps_connect(bam_tx_pipe, &tx_connection);
1074 if (ret < 0) {
1075 pr_err("%s: tx connect error %d\n", __func__, ret);
1076 goto tx_connect_failed;
1077 }
1078
1079 bam_rx_pipe = sps_alloc_endpoint();
1080 if (bam_rx_pipe == NULL) {
1081 pr_err("%s: rx alloc endpoint failed\n", __func__);
1082 ret = -ENOMEM;
1083 goto tx_connect_failed;
1084 }
1085 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1086 if (ret) {
1087 pr_err("%s: rx get config failed %d\n", __func__, ret);
1088 goto rx_get_config_failed;
1089 }
1090
1091 rx_connection.source = h;
1092 rx_connection.src_pipe_index = 5;
1093 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1094 rx_connection.dest_pipe_index = 1;
1095 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001096 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1097 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 rx_desc_mem_buf.size = 0x800; /* 2k */
1099 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1100 &dma_addr, 0);
1101 if (rx_desc_mem_buf.base == NULL) {
1102 pr_err("%s: rx memory alloc failed\n", __func__);
1103 ret = -ENOMEM;
1104 goto rx_mem_failed;
1105 }
1106 rx_desc_mem_buf.phys_base = dma_addr;
1107 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1108 rx_connection.desc = rx_desc_mem_buf;
1109 rx_connection.event_thresh = 0x10;
1110
1111 ret = sps_connect(bam_rx_pipe, &rx_connection);
1112 if (ret < 0) {
1113 pr_err("%s: rx connect error %d\n", __func__, ret);
1114 goto rx_connect_failed;
1115 }
1116
1117 tx_register_event.options = SPS_O_EOT;
1118 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1119 tx_register_event.xfer_done = NULL;
1120 tx_register_event.callback = bam_mux_tx_notify;
1121 tx_register_event.user = NULL;
1122 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1123 if (ret < 0) {
1124 pr_err("%s: tx register event error %d\n", __func__, ret);
1125 goto rx_event_reg_failed;
1126 }
1127
Jeff Hugo33dbc002011-08-25 15:52:53 -06001128 rx_register_event.options = SPS_O_EOT;
1129 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1130 rx_register_event.xfer_done = NULL;
1131 rx_register_event.callback = bam_mux_rx_notify;
1132 rx_register_event.user = NULL;
1133 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1134 if (ret < 0) {
1135 pr_err("%s: tx register event error %d\n", __func__, ret);
1136 goto rx_event_reg_failed;
1137 }
1138
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001139 bam_mux_initialized = 1;
1140 for (i = 0; i < NUM_BUFFERS; ++i)
1141 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001142 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001143 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001144 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001145 return;
1146
1147rx_event_reg_failed:
1148 sps_disconnect(bam_rx_pipe);
1149rx_connect_failed:
1150 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1151 rx_desc_mem_buf.phys_base);
1152rx_mem_failed:
1153 sps_disconnect(bam_tx_pipe);
1154rx_get_config_failed:
1155 sps_free_endpoint(bam_rx_pipe);
1156tx_connect_failed:
1157 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1158 tx_desc_mem_buf.phys_base);
1159tx_get_config_failed:
1160 sps_free_endpoint(bam_tx_pipe);
1161tx_mem_failed:
1162 sps_deregister_bam_device(h);
1163register_bam_failed:
1164 /*destroy_workqueue(bam_mux_workqueue);*/
1165 /*return ret;*/
1166 return;
1167}
Jeff Hugoade1f842011-08-03 15:53:59 -06001168
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001169static void toggle_apps_ack(void)
1170{
1171 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
1172 smsm_change_state(SMSM_APPS_STATE,
1173 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1174 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1175 clear_bit = ~clear_bit;
1176}
1177
Jeff Hugoade1f842011-08-03 15:53:59 -06001178static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1179{
1180 DBG("%s: smsm activity\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001181 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL)
1182 reconnect_to_bam();
1183 else if (bam_mux_initialized && !(new_state & SMSM_A2_POWER_CONTROL))
1184 disconnect_to_bam();
Jeff Hugoade1f842011-08-03 15:53:59 -06001185 else if (new_state & SMSM_A2_POWER_CONTROL)
1186 bam_init();
1187 else
1188 pr_err("%s: unsupported state change\n", __func__);
1189
1190}
1191
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001192static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1193 uint32_t new_state)
1194{
1195 complete_all(&ul_wakeup_ack_completion);
1196}
1197
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198static int bam_dmux_probe(struct platform_device *pdev)
1199{
1200 int rc;
1201
1202 DBG("%s probe called\n", __func__);
1203 if (bam_mux_initialized)
1204 return 0;
1205
Stephen Boyd1c51a492011-10-26 12:11:47 -07001206 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001207 if (IS_ERR(dfab_clk)) {
1208 pr_err("%s: did not get dfab clock\n", __func__);
1209 return -EFAULT;
1210 }
1211
1212 rc = clk_set_rate(dfab_clk, 64000000);
1213 if (rc)
1214 pr_err("%s: unable to set dfab clock rate\n", __func__);
1215
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1217 if (!bam_mux_rx_workqueue)
1218 return -ENOMEM;
1219
1220 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1221 if (!bam_mux_tx_workqueue) {
1222 destroy_workqueue(bam_mux_rx_workqueue);
1223 return -ENOMEM;
1224 }
1225
Jeff Hugo7960abd2011-08-02 15:39:38 -06001226 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001228 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1229 "bam_dmux_ch_%d", rc);
1230 /* bus 2, ie a2 stream 2 */
1231 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1232 if (!bam_ch[rc].pdev) {
1233 pr_err("%s: platform device alloc failed\n", __func__);
1234 destroy_workqueue(bam_mux_rx_workqueue);
1235 destroy_workqueue(bam_mux_tx_workqueue);
1236 return -ENOMEM;
1237 }
1238 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001239
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001240 init_completion(&ul_wakeup_ack_completion);
1241 init_completion(&bam_connection_completion);
1242 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
1243
Jeff Hugoade1f842011-08-03 15:53:59 -06001244 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1245 bam_dmux_smsm_cb, NULL);
1246
1247 if (rc) {
1248 destroy_workqueue(bam_mux_rx_workqueue);
1249 destroy_workqueue(bam_mux_tx_workqueue);
1250 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1251 return -ENOMEM;
1252 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001253
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001254 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1255 bam_dmux_smsm_ack_cb, NULL);
1256
1257 if (rc) {
1258 destroy_workqueue(bam_mux_rx_workqueue);
1259 destroy_workqueue(bam_mux_tx_workqueue);
1260 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1261 SMSM_A2_POWER_CONTROL,
1262 bam_dmux_smsm_cb, NULL);
1263 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1264 rc);
1265 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1266 platform_device_put(bam_ch[rc].pdev);
1267 return -ENOMEM;
1268 }
1269
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001270 return 0;
1271}
1272
1273static struct platform_driver bam_dmux_driver = {
1274 .probe = bam_dmux_probe,
1275 .driver = {
1276 .name = "BAM_RMNT",
1277 .owner = THIS_MODULE,
1278 },
1279};
1280
1281static int __init bam_dmux_init(void)
1282{
1283#ifdef CONFIG_DEBUG_FS
1284 struct dentry *dent;
1285
1286 dent = debugfs_create_dir("bam_dmux", 0);
1287 if (!IS_ERR(dent))
1288 debug_create("tbl", 0444, dent, debug_tbl);
1289#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001290 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001291 return platform_driver_register(&bam_dmux_driver);
1292}
1293
Jeff Hugoade1f842011-08-03 15:53:59 -06001294late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295MODULE_DESCRIPTION("MSM BAM DMUX");
1296MODULE_LICENSE("GPL v2");