blob: d0bf0cdb33ea3c3646e2c4a12a8923b98398cdd6 [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>
Jeff Hugoae3a85e2011-12-02 17:10:18 -070028#include <linux/wakelock.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029
30#include <mach/sps.h>
31#include <mach/bam_dmux.h>
Jeff Hugoade1f842011-08-03 15:53:59 -060032#include <mach/msm_smsm.h>
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060033#include <mach/subsystem_notif.h>
Jeff Hugo75913c82011-12-05 15:59:01 -070034#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035
36#define BAM_CH_LOCAL_OPEN 0x1
37#define BAM_CH_REMOTE_OPEN 0x2
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060038#define BAM_CH_IN_RESET 0x4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
40#define BAM_MUX_HDR_MAGIC_NO 0x33fc
41
42#define BAM_MUX_HDR_CMD_DATA 0
43#define BAM_MUX_HDR_CMD_OPEN 1
44#define BAM_MUX_HDR_CMD_CLOSE 2
45
Jeff Hugo949080a2011-08-30 11:58:56 -060046#define POLLING_MIN_SLEEP 950 /* 0.95 ms */
47#define POLLING_MAX_SLEEP 1050 /* 1.05 ms */
48#define POLLING_INACTIVITY 40 /* cycles before switch to intr mode */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -070050#define LOW_WATERMARK 2
51#define HIGH_WATERMARK 4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052
53static int msm_bam_dmux_debug_enable;
54module_param_named(debug_enable, msm_bam_dmux_debug_enable,
55 int, S_IRUGO | S_IWUSR | S_IWGRP);
56
57#if defined(DEBUG)
58static uint32_t bam_dmux_read_cnt;
59static uint32_t bam_dmux_write_cnt;
60static uint32_t bam_dmux_write_cpy_cnt;
61static uint32_t bam_dmux_write_cpy_bytes;
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070062static uint32_t bam_dmux_tx_sps_failure_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063
64#define DBG(x...) do { \
65 if (msm_bam_dmux_debug_enable) \
66 pr_debug(x); \
67 } while (0)
68
69#define DBG_INC_READ_CNT(x) do { \
70 bam_dmux_read_cnt += (x); \
71 if (msm_bam_dmux_debug_enable) \
72 pr_debug("%s: total read bytes %u\n", \
73 __func__, bam_dmux_read_cnt); \
74 } while (0)
75
76#define DBG_INC_WRITE_CNT(x) do { \
77 bam_dmux_write_cnt += (x); \
78 if (msm_bam_dmux_debug_enable) \
79 pr_debug("%s: total written bytes %u\n", \
80 __func__, bam_dmux_write_cnt); \
81 } while (0)
82
83#define DBG_INC_WRITE_CPY(x) do { \
84 bam_dmux_write_cpy_bytes += (x); \
85 bam_dmux_write_cpy_cnt++; \
86 if (msm_bam_dmux_debug_enable) \
87 pr_debug("%s: total write copy cnt %u, bytes %u\n", \
88 __func__, bam_dmux_write_cpy_cnt, \
89 bam_dmux_write_cpy_bytes); \
90 } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070091
92#define DBG_INC_TX_SPS_FAILURE_CNT() do { \
93 bam_dmux_tx_sps_failure_cnt++; \
94} while (0)
95
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096#else
97#define DBG(x...) do { } while (0)
98#define DBG_INC_READ_CNT(x...) do { } while (0)
99#define DBG_INC_WRITE_CNT(x...) do { } while (0)
100#define DBG_INC_WRITE_CPY(x...) do { } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700101#define DBG_INC_TX_SPS_FAILURE_CNT() do { } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102#endif
103
104struct bam_ch_info {
105 uint32_t status;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600106 void (*notify)(void *, int, unsigned long);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 void *priv;
108 spinlock_t lock;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600109 struct platform_device *pdev;
110 char name[BAM_DMUX_CH_NAME_MAX_LEN];
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700111 int num_tx_pkts;
112 int use_wm;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113};
114
115struct tx_pkt_info {
116 struct sk_buff *skb;
117 dma_addr_t dma_address;
118 char is_cmd;
119 uint32_t len;
120 struct work_struct work;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600121 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122};
123
124struct rx_pkt_info {
125 struct sk_buff *skb;
126 dma_addr_t dma_address;
127 struct work_struct work;
Jeff Hugo949080a2011-08-30 11:58:56 -0600128 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129};
130
131#define A2_NUM_PIPES 6
132#define A2_SUMMING_THRESHOLD 4096
133#define A2_DEFAULT_DESCRIPTORS 32
134#define A2_PHYS_BASE 0x124C2000
135#define A2_PHYS_SIZE 0x2000
136#define BUFFER_SIZE 2048
137#define NUM_BUFFERS 32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138static struct sps_bam_props a2_props;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600139static u32 a2_device_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140static struct sps_pipe *bam_tx_pipe;
141static struct sps_pipe *bam_rx_pipe;
142static struct sps_connect tx_connection;
143static struct sps_connect rx_connection;
144static struct sps_mem_buffer tx_desc_mem_buf;
145static struct sps_mem_buffer rx_desc_mem_buf;
146static struct sps_register_event tx_register_event;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600147static struct sps_register_event rx_register_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700148
149static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS];
150static int bam_mux_initialized;
151
Jeff Hugo949080a2011-08-30 11:58:56 -0600152static int polling_mode;
153
154static LIST_HEAD(bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600155static DEFINE_MUTEX(bam_rx_pool_mutexlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600156static LIST_HEAD(bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600157static DEFINE_SPINLOCK(bam_tx_pool_spinlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600158
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159struct bam_mux_hdr {
160 uint16_t magic_num;
161 uint8_t reserved;
162 uint8_t cmd;
163 uint8_t pad_len;
164 uint8_t ch_id;
165 uint16_t pkt_len;
166};
167
Jeff Hugod98b1082011-10-24 10:30:23 -0600168static void notify_all(int event, unsigned long data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169static void bam_mux_write_done(struct work_struct *work);
170static void handle_bam_mux_cmd(struct work_struct *work);
Jeff Hugo949080a2011-08-30 11:58:56 -0600171static void rx_timer_work_func(struct work_struct *work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172
Jeff Hugo949080a2011-08-30 11:58:56 -0600173static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174
175static struct workqueue_struct *bam_mux_rx_workqueue;
176static struct workqueue_struct *bam_mux_tx_workqueue;
177
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600178/* A2 power collaspe */
179#define UL_TIMEOUT_DELAY 1000 /* in ms */
180static void toggle_apps_ack(void);
181static void reconnect_to_bam(void);
182static void disconnect_to_bam(void);
183static void ul_wakeup(void);
184static void ul_timeout(struct work_struct *work);
185static void vote_dfab(void);
186static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600187static void kickoff_ul_wakeup_func(struct work_struct *work);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600188
189static int bam_is_connected;
190static DEFINE_MUTEX(wakeup_lock);
191static struct completion ul_wakeup_ack_completion;
192static struct completion bam_connection_completion;
193static struct delayed_work ul_timeout_work;
194static int ul_packet_written;
195static struct clk *dfab_clk;
196static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600197static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600198static int bam_connection_is_active;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700199static int wait_for_ack;
Jeff Hugoae3a85e2011-12-02 17:10:18 -0700200static struct wake_lock bam_wakelock;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600201/* End A2 power collaspe */
202
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600203/* subsystem restart */
204static int restart_notifier_cb(struct notifier_block *this,
205 unsigned long code,
206 void *data);
207
208static struct notifier_block restart_notifier = {
209 .notifier_call = restart_notifier_cb,
210};
211static int in_global_reset;
212/* end subsystem restart */
213
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214#define bam_ch_is_open(x) \
215 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
216
217#define bam_ch_is_local_open(x) \
218 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
219
220#define bam_ch_is_remote_open(x) \
221 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
222
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600223#define bam_ch_is_in_reset(x) \
224 (bam_ch[(x)].status & BAM_CH_IN_RESET)
225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226static void queue_rx(void)
227{
228 void *ptr;
229 struct rx_pkt_info *info;
230
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600231 if (in_global_reset)
232 return;
233
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 info = kmalloc(sizeof(struct rx_pkt_info), GFP_KERNEL);
235 if (!info)
236 return; /*need better way to handle this */
237
238 INIT_WORK(&info->work, handle_bam_mux_cmd);
239
240 info->skb = __dev_alloc_skb(BUFFER_SIZE, GFP_KERNEL);
Jeff Hugo4ba22f92011-12-07 12:42:47 -0700241 if (info->skb == NULL) {
242 pr_err("%s: unable to alloc skb\n", __func__);
243 kfree(info);
244 return;
245 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246 ptr = skb_put(info->skb, BUFFER_SIZE);
Jeff Hugo949080a2011-08-30 11:58:56 -0600247
Jeff Hugoc9749932011-11-02 17:50:40 -0600248 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600249 list_add_tail(&info->list_node, &bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600250 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600251
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 /* need a way to handle error case */
253 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
254 DMA_FROM_DEVICE);
255 sps_transfer_one(bam_rx_pipe, info->dma_address,
Jeff Hugo33dbc002011-08-25 15:52:53 -0600256 BUFFER_SIZE, info,
257 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258}
259
260static void bam_mux_process_data(struct sk_buff *rx_skb)
261{
262 unsigned long flags;
263 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600264 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265
266 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
267
268 rx_skb->data = (unsigned char *)(rx_hdr + 1);
269 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
270 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600271 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600273 event_data = (unsigned long)(rx_skb);
274
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600276 if (bam_ch[rx_hdr->ch_id].notify)
277 bam_ch[rx_hdr->ch_id].notify(
278 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
279 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 else
281 dev_kfree_skb_any(rx_skb);
282 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
283
284 queue_rx();
285}
286
287static void handle_bam_mux_cmd(struct work_struct *work)
288{
289 unsigned long flags;
290 struct bam_mux_hdr *rx_hdr;
291 struct rx_pkt_info *info;
292 struct sk_buff *rx_skb;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600293 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294
295 info = container_of(work, struct rx_pkt_info, work);
296 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600297 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 kfree(info);
299
300 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
301
302 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
303 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
304 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
305 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
306 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
307 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
308 " pad %d ch %d len %d\n", __func__,
309 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
310 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
311 dev_kfree_skb_any(rx_skb);
312 queue_rx();
313 return;
314 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700315
316 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
317 pr_err("%s: dropping invalid LCID %d reserved %d cmd %d"
318 " pad %d ch %d len %d\n", __func__,
319 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
320 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
321 dev_kfree_skb_any(rx_skb);
322 queue_rx();
323 return;
324 }
325
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700326 switch (rx_hdr->cmd) {
327 case BAM_MUX_HDR_CMD_DATA:
328 DBG_INC_READ_CNT(rx_hdr->pkt_len);
329 bam_mux_process_data(rx_skb);
330 break;
331 case BAM_MUX_HDR_CMD_OPEN:
332 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
333 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700334 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700336 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600337 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
338 if (ret)
339 pr_err("%s: platform_device_add() error: %d\n",
340 __func__, ret);
Eric Holmberge779dba2011-11-04 18:22:01 -0600341 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 break;
343 case BAM_MUX_HDR_CMD_CLOSE:
344 /* probably should drop pending write */
345 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
346 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
347 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600349 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
350 bam_ch[rx_hdr->ch_id].pdev =
351 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
352 if (!bam_ch[rx_hdr->ch_id].pdev)
353 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberge779dba2011-11-04 18:22:01 -0600354 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355 break;
356 default:
357 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
358 " pad %d ch %d len %d\n", __func__,
359 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
360 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
361 dev_kfree_skb_any(rx_skb);
362 queue_rx();
363 return;
364 }
365}
366
367static int bam_mux_write_cmd(void *data, uint32_t len)
368{
369 int rc;
370 struct tx_pkt_info *pkt;
371 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700372 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700373
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600374 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375 if (pkt == NULL) {
376 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
377 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378 return rc;
379 }
380
381 dma_address = dma_map_single(NULL, data, len,
382 DMA_TO_DEVICE);
383 if (!dma_address) {
384 pr_err("%s: dma_map_single() failed\n", __func__);
385 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386 return rc;
387 }
388 pkt->skb = (struct sk_buff *)(data);
389 pkt->len = len;
390 pkt->dma_address = dma_address;
391 pkt->is_cmd = 1;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600392 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700393 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600394 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700395 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
397 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600398 if (rc) {
399 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700400 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600401 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700402 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700403 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600404 kfree(pkt);
405 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600407 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 return rc;
409}
410
411static void bam_mux_write_done(struct work_struct *work)
412{
413 struct sk_buff *skb;
414 struct bam_mux_hdr *hdr;
415 struct tx_pkt_info *info;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600416 unsigned long event_data;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600417 struct list_head *node;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700418 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600420 if (in_global_reset)
421 return;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700422 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600423 node = bam_tx_pool.next;
424 list_del(node);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700425 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 info = container_of(work, struct tx_pkt_info, work);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600427 if (info->is_cmd) {
428 kfree(info->skb);
429 kfree(info);
430 return;
431 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432 skb = info->skb;
433 kfree(info);
434 hdr = (struct bam_mux_hdr *)skb->data;
435 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600436 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700437 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
438 bam_ch[hdr->ch_id].num_tx_pkts--;
439 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600440 if (bam_ch[hdr->ch_id].notify)
441 bam_ch[hdr->ch_id].notify(
442 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
443 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444 else
445 dev_kfree_skb_any(skb);
446}
447
448int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
449{
450 int rc = 0;
451 struct bam_mux_hdr *hdr;
452 unsigned long flags;
453 struct sk_buff *new_skb = NULL;
454 dma_addr_t dma_address;
455 struct tx_pkt_info *pkt;
456
457 if (id >= BAM_DMUX_NUM_CHANNELS)
458 return -EINVAL;
459 if (!skb)
460 return -EINVAL;
461 if (!bam_mux_initialized)
462 return -ENODEV;
463
464 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
465 spin_lock_irqsave(&bam_ch[id].lock, flags);
466 if (!bam_ch_is_open(id)) {
467 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
468 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
469 return -ENODEV;
470 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700471
472 if (bam_ch[id].use_wm &&
473 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
474 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
475 pr_err("%s: watermark exceeded: %d\n", __func__, id);
476 return -EAGAIN;
477 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
479
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600480 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600481 if (!bam_is_connected) {
482 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600483 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600484 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600485 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600486 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600487
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488 /* if skb do not have any tailroom for padding,
489 copy the skb into a new expanded skb */
490 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
491 /* revisit, probably dev_alloc_skb and memcpy is effecient */
492 new_skb = skb_copy_expand(skb, skb_headroom(skb),
493 4 - (skb->len & 0x3), GFP_ATOMIC);
494 if (new_skb == NULL) {
495 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600496 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 }
498 dev_kfree_skb_any(skb);
499 skb = new_skb;
500 DBG_INC_WRITE_CPY(skb->len);
501 }
502
503 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
504
505 /* caller should allocate for hdr and padding
506 hdr is fine, padding is tricky */
507 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
508 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
509 hdr->reserved = 0;
510 hdr->ch_id = id;
511 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
512 if (skb->len & 0x3)
513 skb_put(skb, 4 - (skb->len & 0x3));
514
515 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
516
517 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
518 __func__, skb->data, skb->tail, skb->len,
519 hdr->pkt_len, hdr->pad_len);
520
521 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
522 if (pkt == NULL) {
523 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600524 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525 }
526
527 dma_address = dma_map_single(NULL, skb->data, skb->len,
528 DMA_TO_DEVICE);
529 if (!dma_address) {
530 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600531 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 }
533 pkt->skb = skb;
534 pkt->dma_address = dma_address;
535 pkt->is_cmd = 0;
536 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700537 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600538 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700539 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
541 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600542 if (rc) {
543 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700544 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600545 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700546 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700547 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600548 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700549 if (new_skb)
550 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700551 } else {
552 spin_lock_irqsave(&bam_ch[id].lock, flags);
553 bam_ch[id].num_tx_pkts++;
554 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600555 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600556 ul_packet_written = 1;
557 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600559
560write_fail3:
561 kfree(pkt);
562write_fail2:
563 if (new_skb)
564 dev_kfree_skb_any(new_skb);
565write_fail:
566 read_unlock(&ul_wakeup_lock);
567 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568}
569
570int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600571 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572{
573 struct bam_mux_hdr *hdr;
574 unsigned long flags;
575 int rc = 0;
576
577 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700578 if (!bam_mux_initialized) {
579 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700581 }
582 if (id >= BAM_DMUX_NUM_CHANNELS) {
583 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700585 }
586 if (notify == NULL) {
587 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600588 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700589 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590
591 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
592 if (hdr == NULL) {
593 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
594 return -ENOMEM;
595 }
596 spin_lock_irqsave(&bam_ch[id].lock, flags);
597 if (bam_ch_is_open(id)) {
598 DBG("%s: Already opened %d\n", __func__, id);
599 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
600 kfree(hdr);
601 goto open_done;
602 }
603 if (!bam_ch_is_remote_open(id)) {
604 DBG("%s: Remote not open; ch: %d\n", __func__, id);
605 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
606 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700607 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608 }
609
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600610 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611 bam_ch[id].priv = priv;
612 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700613 bam_ch[id].num_tx_pkts = 0;
614 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
616
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600617 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600618 if (!bam_is_connected) {
619 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600620 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600621 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600622 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600623 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600624
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
626 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
627 hdr->reserved = 0;
628 hdr->ch_id = id;
629 hdr->pkt_len = 0;
630 hdr->pad_len = 0;
631
632 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600633 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634
635open_done:
636 DBG("%s: opened ch %d\n", __func__, id);
637 return rc;
638}
639
640int msm_bam_dmux_close(uint32_t id)
641{
642 struct bam_mux_hdr *hdr;
643 unsigned long flags;
644 int rc;
645
646 if (id >= BAM_DMUX_NUM_CHANNELS)
647 return -EINVAL;
648 DBG("%s: closing ch %d\n", __func__, id);
649 if (!bam_mux_initialized)
650 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600652 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600653 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600654 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600655 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600656 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600657 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600658 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600659
Jeff Hugo061ce672011-10-21 17:15:32 -0600660 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600661 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700662 bam_ch[id].priv = NULL;
663 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
664 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
665
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600666 if (bam_ch_is_in_reset(id)) {
667 read_unlock(&ul_wakeup_lock);
668 bam_ch[id].status &= ~BAM_CH_IN_RESET;
669 return 0;
670 }
671
Jeff Hugobb5802f2011-11-02 17:10:29 -0600672 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673 if (hdr == NULL) {
674 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600675 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676 return -ENOMEM;
677 }
678 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
679 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
680 hdr->reserved = 0;
681 hdr->ch_id = id;
682 hdr->pkt_len = 0;
683 hdr->pad_len = 0;
684
685 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600686 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687
688 DBG("%s: closed ch %d\n", __func__, id);
689 return rc;
690}
691
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700692int msm_bam_dmux_is_ch_full(uint32_t id)
693{
694 unsigned long flags;
695 int ret;
696
697 if (id >= BAM_DMUX_NUM_CHANNELS)
698 return -EINVAL;
699
700 spin_lock_irqsave(&bam_ch[id].lock, flags);
701 bam_ch[id].use_wm = 1;
702 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
703 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
704 id, bam_ch[id].num_tx_pkts, ret);
705 if (!bam_ch_is_local_open(id)) {
706 ret = -ENODEV;
707 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
708 }
709 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
710
711 return ret;
712}
713
714int msm_bam_dmux_is_ch_low(uint32_t id)
715{
716 int ret;
717
718 if (id >= BAM_DMUX_NUM_CHANNELS)
719 return -EINVAL;
720
721 bam_ch[id].use_wm = 1;
722 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
723 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
724 id, bam_ch[id].num_tx_pkts, ret);
725 if (!bam_ch_is_local_open(id)) {
726 ret = -ENODEV;
727 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
728 }
729
730 return ret;
731}
732
Jeff Hugo949080a2011-08-30 11:58:56 -0600733static void rx_timer_work_func(struct work_struct *work)
734{
735 struct sps_iovec iov;
736 struct list_head *node;
737 struct rx_pkt_info *info;
738 int inactive_cycles = 0;
739 int ret;
740 struct sps_connect cur_rx_conn;
741
742 while (1) { /* timer loop */
743 ++inactive_cycles;
744 while (1) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600745 if (in_global_reset)
746 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600747 sps_get_iovec(bam_rx_pipe, &iov);
748 if (iov.addr == 0)
749 break;
750 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600751 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600752 node = bam_rx_pool.next;
753 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600754 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600755 info = container_of(node, struct rx_pkt_info,
756 list_node);
757 handle_bam_mux_cmd(&info->work);
758 }
759
760 if (inactive_cycles == POLLING_INACTIVITY) {
761 /*
762 * attempt to enable interrupts in this pipe
763 * if enabling interrupts fails, continue polling
764 */
765 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
766 if (ret) {
767 pr_err("%s: sps_get_config() failed, interrupts"
768 " not enabled\n", __func__);
769 queue_work(bam_mux_rx_workqueue,
770 &rx_timer_work);
771 return;
772 } else {
773 rx_register_event.options = SPS_O_EOT;
774 /* should check return value */
775 sps_register_event(bam_rx_pipe,
776 &rx_register_event);
777 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
778 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
779 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
780 if (ret) {
781 pr_err("%s: sps_set_config() failed, "
782 "interrupts not enabled\n",
783 __func__);
784 queue_work(bam_mux_rx_workqueue,
785 &rx_timer_work);
786 return;
787 }
788 polling_mode = 0;
789 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600790 if (in_global_reset)
791 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600792 /* handle race condition - missed packet? */
793 sps_get_iovec(bam_rx_pipe, &iov);
794 if (iov.addr == 0)
795 return;
796 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600797 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600798 node = bam_rx_pool.next;
799 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600800 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600801 info = container_of(node, struct rx_pkt_info,
802 list_node);
803 handle_bam_mux_cmd(&info->work);
804 return;
805 }
806
807 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
808 }
809}
810
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811static void bam_mux_tx_notify(struct sps_event_notify *notify)
812{
813 struct tx_pkt_info *pkt;
814
815 DBG("%s: event %d notified\n", __func__, notify->event_id);
816
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600817 if (in_global_reset)
818 return;
819
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 switch (notify->event_id) {
821 case SPS_EVENT_EOT:
822 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600823 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824 dma_unmap_single(NULL, pkt->dma_address,
825 pkt->skb->len,
826 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600827 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828 dma_unmap_single(NULL, pkt->dma_address,
829 pkt->len,
830 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600831 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832 break;
833 default:
834 pr_err("%s: recieved unexpected event id %d\n", __func__,
835 notify->event_id);
836 }
837}
838
Jeff Hugo33dbc002011-08-25 15:52:53 -0600839static void bam_mux_rx_notify(struct sps_event_notify *notify)
840{
Jeff Hugo949080a2011-08-30 11:58:56 -0600841 int ret;
842 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600843
844 DBG("%s: event %d notified\n", __func__, notify->event_id);
845
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600846 if (in_global_reset)
847 return;
848
Jeff Hugo33dbc002011-08-25 15:52:53 -0600849 switch (notify->event_id) {
850 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -0600851 /* attempt to disable interrupts in this pipe */
852 if (!polling_mode) {
853 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
854 if (ret) {
855 pr_err("%s: sps_get_config() failed, interrupts"
856 " not disabled\n", __func__);
857 break;
858 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -0700859 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -0600860 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
861 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
862 if (ret) {
863 pr_err("%s: sps_set_config() failed, interrupts"
864 " not disabled\n", __func__);
865 break;
866 }
867 polling_mode = 1;
868 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
869 }
Jeff Hugo33dbc002011-08-25 15:52:53 -0600870 break;
871 default:
872 pr_err("%s: recieved unexpected event id %d\n", __func__,
873 notify->event_id);
874 }
875}
876
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877#ifdef CONFIG_DEBUG_FS
878
879static int debug_tbl(char *buf, int max)
880{
881 int i = 0;
882 int j;
883
884 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
885 i += scnprintf(buf + i, max - i,
886 "ch%02d local open=%s remote open=%s\n",
887 j, bam_ch_is_local_open(j) ? "Y" : "N",
888 bam_ch_is_remote_open(j) ? "Y" : "N");
889 }
890
891 return i;
892}
893
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700894static int debug_ul_pkt_cnt(char *buf, int max)
895{
896 struct list_head *p;
897 unsigned long flags;
898 int n = 0;
899
900 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
901 __list_for_each(p, &bam_tx_pool) {
902 ++n;
903 }
904 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
905
906 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
907}
908
909static int debug_stats(char *buf, int max)
910{
911 int i = 0;
912
913 i += scnprintf(buf + i, max - i,
914 "skb copy cnt: %u\n"
915 "skb copy bytes: %u\n"
916 "sps tx failures: %u\n",
917 bam_dmux_write_cpy_cnt,
918 bam_dmux_write_cpy_bytes,
919 bam_dmux_tx_sps_failure_cnt
920 );
921
922 return i;
923}
924
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925#define DEBUG_BUFMAX 4096
926static char debug_buffer[DEBUG_BUFMAX];
927
928static ssize_t debug_read(struct file *file, char __user *buf,
929 size_t count, loff_t *ppos)
930{
931 int (*fill)(char *buf, int max) = file->private_data;
932 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
933 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
934}
935
936static int debug_open(struct inode *inode, struct file *file)
937{
938 file->private_data = inode->i_private;
939 return 0;
940}
941
942
943static const struct file_operations debug_ops = {
944 .read = debug_read,
945 .open = debug_open,
946};
947
948static void debug_create(const char *name, mode_t mode,
949 struct dentry *dent,
950 int (*fill)(char *buf, int max))
951{
952 debugfs_create_file(name, mode, dent, fill, &debug_ops);
953}
954
955#endif
956
Jeff Hugod98b1082011-10-24 10:30:23 -0600957static void notify_all(int event, unsigned long data)
958{
959 int i;
960
961 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
962 if (bam_ch_is_open(i))
963 bam_ch[i].notify(bam_ch[i].priv, event, data);
964 }
965}
966
967static void kickoff_ul_wakeup_func(struct work_struct *work)
968{
969 read_lock(&ul_wakeup_lock);
970 if (!bam_is_connected) {
971 read_unlock(&ul_wakeup_lock);
972 ul_wakeup();
973 read_lock(&ul_wakeup_lock);
974 ul_packet_written = 1;
975 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
976 }
977 read_unlock(&ul_wakeup_lock);
978}
979
980void msm_bam_dmux_kickoff_ul_wakeup(void)
981{
982 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
983}
984
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600985static void ul_timeout(struct work_struct *work)
986{
Jeff Hugoc040a5b2011-11-15 14:26:01 -0700987 unsigned long flags;
988 int ret;
989
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600990 if (in_global_reset)
991 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -0700992 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
993 if (!ret) { /* failed to grab lock, reschedule and bail */
994 schedule_delayed_work(&ul_timeout_work,
995 msecs_to_jiffies(UL_TIMEOUT_DELAY));
996 return;
997 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600998 if (ul_packet_written) {
999 ul_packet_written = 0;
1000 schedule_delayed_work(&ul_timeout_work,
1001 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1002 } else {
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001003 wait_for_ack = 1;
1004 INIT_COMPLETION(ul_wakeup_ack_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001005 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1006 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -06001007 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001008 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001009 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001010}
1011static void ul_wakeup(void)
1012{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001013 int ret;
1014
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001015 mutex_lock(&wakeup_lock);
1016 if (bam_is_connected) { /* bam got connected before lock grabbed */
1017 mutex_unlock(&wakeup_lock);
1018 return;
1019 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001020 /*
1021 * must wait for the previous power down request to have been acked
1022 * chances are it already came in and this will just fall through
1023 * instead of waiting
1024 */
1025 if (wait_for_ack) {
1026 ret = wait_for_completion_interruptible_timeout(
1027 &ul_wakeup_ack_completion, HZ);
1028 BUG_ON(ret == 0);
1029 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001030 INIT_COMPLETION(ul_wakeup_ack_completion);
1031 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001032 ret = wait_for_completion_interruptible_timeout(
1033 &ul_wakeup_ack_completion, HZ);
1034 BUG_ON(ret == 0);
1035 ret = wait_for_completion_interruptible_timeout(
1036 &bam_connection_completion, HZ);
1037 BUG_ON(ret == 0);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001038
1039 bam_is_connected = 1;
1040 schedule_delayed_work(&ul_timeout_work,
1041 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1042 mutex_unlock(&wakeup_lock);
1043}
1044
1045static void reconnect_to_bam(void)
1046{
1047 int i;
1048
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001049 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001050 vote_dfab();
1051 i = sps_device_reset(a2_device_handle);
1052 if (i)
1053 pr_err("%s: device reset failed rc = %d\n", __func__, i);
1054 i = sps_connect(bam_tx_pipe, &tx_connection);
1055 if (i)
1056 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
1057 i = sps_connect(bam_rx_pipe, &rx_connection);
1058 if (i)
1059 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
1060 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1061 if (i)
1062 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
1063 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1064 if (i)
1065 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
1066 for (i = 0; i < NUM_BUFFERS; ++i)
1067 queue_rx();
1068 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001069 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001070 complete_all(&bam_connection_completion);
1071}
1072
1073static void disconnect_to_bam(void)
1074{
1075 struct list_head *node;
1076 struct rx_pkt_info *info;
1077
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001078 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001079 INIT_COMPLETION(bam_connection_completion);
1080 sps_disconnect(bam_tx_pipe);
1081 sps_disconnect(bam_rx_pipe);
1082 unvote_dfab();
1083 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1084 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1085 while (!list_empty(&bam_rx_pool)) {
1086 node = bam_rx_pool.next;
1087 list_del(node);
1088 info = container_of(node, struct rx_pkt_info, list_node);
1089 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1090 DMA_FROM_DEVICE);
1091 dev_kfree_skb_any(info->skb);
1092 kfree(info);
1093 }
1094}
1095
1096static void vote_dfab(void)
1097{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001098 int rc;
1099
1100 rc = clk_enable(dfab_clk);
1101 if (rc)
1102 pr_err("bam_dmux vote for dfab failed rc = %d\n", rc);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001103}
1104
1105static void unvote_dfab(void)
1106{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001107 clk_disable(dfab_clk);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001108}
1109
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001110static int restart_notifier_cb(struct notifier_block *this,
1111 unsigned long code,
1112 void *data)
1113{
1114 int i;
1115 struct list_head *node;
1116 struct tx_pkt_info *info;
1117 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001118 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001119
1120 if (code != SUBSYS_AFTER_SHUTDOWN)
1121 return NOTIFY_DONE;
1122
1123 in_global_reset = 1;
1124 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1125 temp_remote_status = bam_ch_is_remote_open(i);
1126 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001127 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001128 if (bam_ch_is_local_open(i))
1129 bam_ch[i].status |= BAM_CH_IN_RESET;
1130 if (temp_remote_status) {
1131 platform_device_unregister(bam_ch[i].pdev);
1132 bam_ch[i].pdev = platform_device_alloc(
1133 bam_ch[i].name, 2);
1134 }
1135 }
1136 /*cleanup UL*/
Jeff Hugo626303bf2011-11-21 11:43:28 -07001137 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001138 while (!list_empty(&bam_tx_pool)) {
1139 node = bam_tx_pool.next;
1140 list_del(node);
1141 info = container_of(node, struct tx_pkt_info,
1142 list_node);
1143 if (!info->is_cmd) {
1144 dma_unmap_single(NULL, info->dma_address,
1145 info->skb->len,
1146 DMA_TO_DEVICE);
1147 dev_kfree_skb_any(info->skb);
1148 } else {
1149 dma_unmap_single(NULL, info->dma_address,
1150 info->len,
1151 DMA_TO_DEVICE);
1152 kfree(info->skb);
1153 }
1154 kfree(info);
1155 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001156 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001157 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1158
1159 return NOTIFY_DONE;
1160}
1161
Jeff Hugoade1f842011-08-03 15:53:59 -06001162static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001163{
1164 u32 h;
1165 dma_addr_t dma_addr;
1166 int ret;
1167 void *a2_virt_addr;
1168 int i;
1169
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001170 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001171 /* init BAM */
1172 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1173 if (!a2_virt_addr) {
1174 pr_err("%s: ioremap failed\n", __func__);
1175 ret = -ENOMEM;
1176 goto register_bam_failed;
1177 }
1178 a2_props.phys_addr = A2_PHYS_BASE;
1179 a2_props.virt_addr = a2_virt_addr;
1180 a2_props.virt_size = A2_PHYS_SIZE;
1181 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001182 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183 a2_props.num_pipes = A2_NUM_PIPES;
1184 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001185 if (cpu_is_msm9615())
1186 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001187 /* need to free on tear down */
1188 ret = sps_register_bam_device(&a2_props, &h);
1189 if (ret < 0) {
1190 pr_err("%s: register bam error %d\n", __func__, ret);
1191 goto register_bam_failed;
1192 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001193 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194
1195 bam_tx_pipe = sps_alloc_endpoint();
1196 if (bam_tx_pipe == NULL) {
1197 pr_err("%s: tx alloc endpoint failed\n", __func__);
1198 ret = -ENOMEM;
1199 goto register_bam_failed;
1200 }
1201 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1202 if (ret) {
1203 pr_err("%s: tx get config failed %d\n", __func__, ret);
1204 goto tx_get_config_failed;
1205 }
1206
1207 tx_connection.source = SPS_DEV_HANDLE_MEM;
1208 tx_connection.src_pipe_index = 0;
1209 tx_connection.destination = h;
1210 tx_connection.dest_pipe_index = 4;
1211 tx_connection.mode = SPS_MODE_DEST;
1212 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1213 tx_desc_mem_buf.size = 0x800; /* 2k */
1214 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1215 &dma_addr, 0);
1216 if (tx_desc_mem_buf.base == NULL) {
1217 pr_err("%s: tx memory alloc failed\n", __func__);
1218 ret = -ENOMEM;
1219 goto tx_mem_failed;
1220 }
1221 tx_desc_mem_buf.phys_base = dma_addr;
1222 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1223 tx_connection.desc = tx_desc_mem_buf;
1224 tx_connection.event_thresh = 0x10;
1225
1226 ret = sps_connect(bam_tx_pipe, &tx_connection);
1227 if (ret < 0) {
1228 pr_err("%s: tx connect error %d\n", __func__, ret);
1229 goto tx_connect_failed;
1230 }
1231
1232 bam_rx_pipe = sps_alloc_endpoint();
1233 if (bam_rx_pipe == NULL) {
1234 pr_err("%s: rx alloc endpoint failed\n", __func__);
1235 ret = -ENOMEM;
1236 goto tx_connect_failed;
1237 }
1238 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1239 if (ret) {
1240 pr_err("%s: rx get config failed %d\n", __func__, ret);
1241 goto rx_get_config_failed;
1242 }
1243
1244 rx_connection.source = h;
1245 rx_connection.src_pipe_index = 5;
1246 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1247 rx_connection.dest_pipe_index = 1;
1248 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001249 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1250 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001251 rx_desc_mem_buf.size = 0x800; /* 2k */
1252 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1253 &dma_addr, 0);
1254 if (rx_desc_mem_buf.base == NULL) {
1255 pr_err("%s: rx memory alloc failed\n", __func__);
1256 ret = -ENOMEM;
1257 goto rx_mem_failed;
1258 }
1259 rx_desc_mem_buf.phys_base = dma_addr;
1260 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1261 rx_connection.desc = rx_desc_mem_buf;
1262 rx_connection.event_thresh = 0x10;
1263
1264 ret = sps_connect(bam_rx_pipe, &rx_connection);
1265 if (ret < 0) {
1266 pr_err("%s: rx connect error %d\n", __func__, ret);
1267 goto rx_connect_failed;
1268 }
1269
1270 tx_register_event.options = SPS_O_EOT;
1271 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1272 tx_register_event.xfer_done = NULL;
1273 tx_register_event.callback = bam_mux_tx_notify;
1274 tx_register_event.user = NULL;
1275 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1276 if (ret < 0) {
1277 pr_err("%s: tx register event error %d\n", __func__, ret);
1278 goto rx_event_reg_failed;
1279 }
1280
Jeff Hugo33dbc002011-08-25 15:52:53 -06001281 rx_register_event.options = SPS_O_EOT;
1282 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1283 rx_register_event.xfer_done = NULL;
1284 rx_register_event.callback = bam_mux_rx_notify;
1285 rx_register_event.user = NULL;
1286 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1287 if (ret < 0) {
1288 pr_err("%s: tx register event error %d\n", __func__, ret);
1289 goto rx_event_reg_failed;
1290 }
1291
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001292 bam_mux_initialized = 1;
1293 for (i = 0; i < NUM_BUFFERS; ++i)
1294 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001295 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001296 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001297 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001298 return;
1299
1300rx_event_reg_failed:
1301 sps_disconnect(bam_rx_pipe);
1302rx_connect_failed:
1303 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1304 rx_desc_mem_buf.phys_base);
1305rx_mem_failed:
1306 sps_disconnect(bam_tx_pipe);
1307rx_get_config_failed:
1308 sps_free_endpoint(bam_rx_pipe);
1309tx_connect_failed:
1310 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1311 tx_desc_mem_buf.phys_base);
1312tx_get_config_failed:
1313 sps_free_endpoint(bam_tx_pipe);
1314tx_mem_failed:
1315 sps_deregister_bam_device(h);
1316register_bam_failed:
1317 /*destroy_workqueue(bam_mux_workqueue);*/
1318 /*return ret;*/
1319 return;
1320}
Jeff Hugoade1f842011-08-03 15:53:59 -06001321
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001322static void toggle_apps_ack(void)
1323{
1324 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
1325 smsm_change_state(SMSM_APPS_STATE,
1326 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1327 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1328 clear_bit = ~clear_bit;
1329}
1330
Jeff Hugoade1f842011-08-03 15:53:59 -06001331static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1332{
1333 DBG("%s: smsm activity\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001334 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
1335 wake_lock(&bam_wakelock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001336 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001337 } else if (bam_mux_initialized &&
1338 !(new_state & SMSM_A2_POWER_CONTROL)) {
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001339 disconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001340 wake_unlock(&bam_wakelock);
1341 } else if (new_state & SMSM_A2_POWER_CONTROL) {
1342 wake_lock(&bam_wakelock);
Jeff Hugoade1f842011-08-03 15:53:59 -06001343 bam_init();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001344 } else {
Jeff Hugoade1f842011-08-03 15:53:59 -06001345 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001346 }
Jeff Hugoade1f842011-08-03 15:53:59 -06001347
1348}
1349
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001350static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1351 uint32_t new_state)
1352{
1353 complete_all(&ul_wakeup_ack_completion);
1354}
1355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001356static int bam_dmux_probe(struct platform_device *pdev)
1357{
1358 int rc;
1359
1360 DBG("%s probe called\n", __func__);
1361 if (bam_mux_initialized)
1362 return 0;
1363
Stephen Boyd1c51a492011-10-26 12:11:47 -07001364 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001365 if (IS_ERR(dfab_clk)) {
1366 pr_err("%s: did not get dfab clock\n", __func__);
1367 return -EFAULT;
1368 }
1369
1370 rc = clk_set_rate(dfab_clk, 64000000);
1371 if (rc)
1372 pr_err("%s: unable to set dfab clock rate\n", __func__);
1373
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001374 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1375 if (!bam_mux_rx_workqueue)
1376 return -ENOMEM;
1377
1378 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1379 if (!bam_mux_tx_workqueue) {
1380 destroy_workqueue(bam_mux_rx_workqueue);
1381 return -ENOMEM;
1382 }
1383
Jeff Hugo7960abd2011-08-02 15:39:38 -06001384 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001386 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1387 "bam_dmux_ch_%d", rc);
1388 /* bus 2, ie a2 stream 2 */
1389 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1390 if (!bam_ch[rc].pdev) {
1391 pr_err("%s: platform device alloc failed\n", __func__);
1392 destroy_workqueue(bam_mux_rx_workqueue);
1393 destroy_workqueue(bam_mux_tx_workqueue);
1394 return -ENOMEM;
1395 }
1396 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001397
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001398 init_completion(&ul_wakeup_ack_completion);
1399 init_completion(&bam_connection_completion);
1400 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001401 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001402
Jeff Hugoade1f842011-08-03 15:53:59 -06001403 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1404 bam_dmux_smsm_cb, NULL);
1405
1406 if (rc) {
1407 destroy_workqueue(bam_mux_rx_workqueue);
1408 destroy_workqueue(bam_mux_tx_workqueue);
1409 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1410 return -ENOMEM;
1411 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001412
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001413 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1414 bam_dmux_smsm_ack_cb, NULL);
1415
1416 if (rc) {
1417 destroy_workqueue(bam_mux_rx_workqueue);
1418 destroy_workqueue(bam_mux_tx_workqueue);
1419 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1420 SMSM_A2_POWER_CONTROL,
1421 bam_dmux_smsm_cb, NULL);
1422 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1423 rc);
1424 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1425 platform_device_put(bam_ch[rc].pdev);
1426 return -ENOMEM;
1427 }
1428
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07001429 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
1430 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
1431
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001432 return 0;
1433}
1434
1435static struct platform_driver bam_dmux_driver = {
1436 .probe = bam_dmux_probe,
1437 .driver = {
1438 .name = "BAM_RMNT",
1439 .owner = THIS_MODULE,
1440 },
1441};
1442
1443static int __init bam_dmux_init(void)
1444{
1445#ifdef CONFIG_DEBUG_FS
1446 struct dentry *dent;
1447
1448 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001449 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001450 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001451 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
1452 debug_create("stats", 0444, dent, debug_stats);
1453 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001454#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001455 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001456 return platform_driver_register(&bam_dmux_driver);
1457}
1458
Jeff Hugoade1f842011-08-03 15:53:59 -06001459late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460MODULE_DESCRIPTION("MSM BAM DMUX");
1461MODULE_LICENSE("GPL v2");