blob: 76094854ff0a77698bd4d24ec1de88c2b7b2850f [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__);
Jeff Hugo96cb7482011-12-07 13:28:31 -0700385 kfree(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387 return rc;
388 }
389 pkt->skb = (struct sk_buff *)(data);
390 pkt->len = len;
391 pkt->dma_address = dma_address;
392 pkt->is_cmd = 1;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600393 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700394 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600395 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700396 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
398 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600399 if (rc) {
400 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700401 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600402 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700403 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700404 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600405 kfree(pkt);
406 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600408 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409 return rc;
410}
411
412static void bam_mux_write_done(struct work_struct *work)
413{
414 struct sk_buff *skb;
415 struct bam_mux_hdr *hdr;
416 struct tx_pkt_info *info;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600417 unsigned long event_data;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600418 struct list_head *node;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700419 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600421 if (in_global_reset)
422 return;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700423 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600424 node = bam_tx_pool.next;
425 list_del(node);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700426 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427 info = container_of(work, struct tx_pkt_info, work);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600428 if (info->is_cmd) {
429 kfree(info->skb);
430 kfree(info);
431 return;
432 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433 skb = info->skb;
434 kfree(info);
435 hdr = (struct bam_mux_hdr *)skb->data;
436 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600437 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700438 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
439 bam_ch[hdr->ch_id].num_tx_pkts--;
440 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600441 if (bam_ch[hdr->ch_id].notify)
442 bam_ch[hdr->ch_id].notify(
443 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
444 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445 else
446 dev_kfree_skb_any(skb);
447}
448
449int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
450{
451 int rc = 0;
452 struct bam_mux_hdr *hdr;
453 unsigned long flags;
454 struct sk_buff *new_skb = NULL;
455 dma_addr_t dma_address;
456 struct tx_pkt_info *pkt;
457
458 if (id >= BAM_DMUX_NUM_CHANNELS)
459 return -EINVAL;
460 if (!skb)
461 return -EINVAL;
462 if (!bam_mux_initialized)
463 return -ENODEV;
464
465 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
466 spin_lock_irqsave(&bam_ch[id].lock, flags);
467 if (!bam_ch_is_open(id)) {
468 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
469 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
470 return -ENODEV;
471 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700472
473 if (bam_ch[id].use_wm &&
474 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
475 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
476 pr_err("%s: watermark exceeded: %d\n", __func__, id);
477 return -EAGAIN;
478 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
480
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600481 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600482 if (!bam_is_connected) {
483 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600484 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600485 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600486 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600487 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600488
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489 /* if skb do not have any tailroom for padding,
490 copy the skb into a new expanded skb */
491 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
492 /* revisit, probably dev_alloc_skb and memcpy is effecient */
493 new_skb = skb_copy_expand(skb, skb_headroom(skb),
494 4 - (skb->len & 0x3), GFP_ATOMIC);
495 if (new_skb == NULL) {
496 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600497 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 }
499 dev_kfree_skb_any(skb);
500 skb = new_skb;
501 DBG_INC_WRITE_CPY(skb->len);
502 }
503
504 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
505
506 /* caller should allocate for hdr and padding
507 hdr is fine, padding is tricky */
508 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
509 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
510 hdr->reserved = 0;
511 hdr->ch_id = id;
512 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
513 if (skb->len & 0x3)
514 skb_put(skb, 4 - (skb->len & 0x3));
515
516 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
517
518 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
519 __func__, skb->data, skb->tail, skb->len,
520 hdr->pkt_len, hdr->pad_len);
521
522 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
523 if (pkt == NULL) {
524 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600525 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 }
527
528 dma_address = dma_map_single(NULL, skb->data, skb->len,
529 DMA_TO_DEVICE);
530 if (!dma_address) {
531 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600532 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 }
534 pkt->skb = skb;
535 pkt->dma_address = dma_address;
536 pkt->is_cmd = 0;
537 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700538 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600539 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700540 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
542 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600543 if (rc) {
544 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700545 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600546 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700547 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700548 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600549 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700550 if (new_skb)
551 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700552 } else {
553 spin_lock_irqsave(&bam_ch[id].lock, flags);
554 bam_ch[id].num_tx_pkts++;
555 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600556 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600557 ul_packet_written = 1;
558 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600560
561write_fail3:
562 kfree(pkt);
563write_fail2:
564 if (new_skb)
565 dev_kfree_skb_any(new_skb);
566write_fail:
567 read_unlock(&ul_wakeup_lock);
568 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569}
570
571int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600572 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573{
574 struct bam_mux_hdr *hdr;
575 unsigned long flags;
576 int rc = 0;
577
578 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700579 if (!bam_mux_initialized) {
580 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700582 }
583 if (id >= BAM_DMUX_NUM_CHANNELS) {
584 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700586 }
587 if (notify == NULL) {
588 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600589 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700590 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591
592 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
593 if (hdr == NULL) {
594 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
595 return -ENOMEM;
596 }
597 spin_lock_irqsave(&bam_ch[id].lock, flags);
598 if (bam_ch_is_open(id)) {
599 DBG("%s: Already opened %d\n", __func__, id);
600 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
601 kfree(hdr);
602 goto open_done;
603 }
604 if (!bam_ch_is_remote_open(id)) {
605 DBG("%s: Remote not open; ch: %d\n", __func__, id);
606 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
607 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700608 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609 }
610
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600611 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612 bam_ch[id].priv = priv;
613 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700614 bam_ch[id].num_tx_pkts = 0;
615 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
617
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600618 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600619 if (!bam_is_connected) {
620 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600621 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600622 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600623 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600624 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600625
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
627 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
628 hdr->reserved = 0;
629 hdr->ch_id = id;
630 hdr->pkt_len = 0;
631 hdr->pad_len = 0;
632
633 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600634 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635
636open_done:
637 DBG("%s: opened ch %d\n", __func__, id);
638 return rc;
639}
640
641int msm_bam_dmux_close(uint32_t id)
642{
643 struct bam_mux_hdr *hdr;
644 unsigned long flags;
645 int rc;
646
647 if (id >= BAM_DMUX_NUM_CHANNELS)
648 return -EINVAL;
649 DBG("%s: closing ch %d\n", __func__, id);
650 if (!bam_mux_initialized)
651 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600653 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600654 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600655 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600656 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600657 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600658 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600659 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600660
Jeff Hugo061ce672011-10-21 17:15:32 -0600661 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600662 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663 bam_ch[id].priv = NULL;
664 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
665 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
666
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600667 if (bam_ch_is_in_reset(id)) {
668 read_unlock(&ul_wakeup_lock);
669 bam_ch[id].status &= ~BAM_CH_IN_RESET;
670 return 0;
671 }
672
Jeff Hugobb5802f2011-11-02 17:10:29 -0600673 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674 if (hdr == NULL) {
675 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600676 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677 return -ENOMEM;
678 }
679 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
680 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
681 hdr->reserved = 0;
682 hdr->ch_id = id;
683 hdr->pkt_len = 0;
684 hdr->pad_len = 0;
685
686 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600687 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700688
689 DBG("%s: closed ch %d\n", __func__, id);
690 return rc;
691}
692
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700693int msm_bam_dmux_is_ch_full(uint32_t id)
694{
695 unsigned long flags;
696 int ret;
697
698 if (id >= BAM_DMUX_NUM_CHANNELS)
699 return -EINVAL;
700
701 spin_lock_irqsave(&bam_ch[id].lock, flags);
702 bam_ch[id].use_wm = 1;
703 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
704 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
705 id, bam_ch[id].num_tx_pkts, ret);
706 if (!bam_ch_is_local_open(id)) {
707 ret = -ENODEV;
708 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
709 }
710 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
711
712 return ret;
713}
714
715int msm_bam_dmux_is_ch_low(uint32_t id)
716{
717 int ret;
718
719 if (id >= BAM_DMUX_NUM_CHANNELS)
720 return -EINVAL;
721
722 bam_ch[id].use_wm = 1;
723 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
724 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
725 id, bam_ch[id].num_tx_pkts, ret);
726 if (!bam_ch_is_local_open(id)) {
727 ret = -ENODEV;
728 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
729 }
730
731 return ret;
732}
733
Jeff Hugo949080a2011-08-30 11:58:56 -0600734static void rx_timer_work_func(struct work_struct *work)
735{
736 struct sps_iovec iov;
737 struct list_head *node;
738 struct rx_pkt_info *info;
739 int inactive_cycles = 0;
740 int ret;
741 struct sps_connect cur_rx_conn;
742
743 while (1) { /* timer loop */
744 ++inactive_cycles;
745 while (1) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600746 if (in_global_reset)
747 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600748 sps_get_iovec(bam_rx_pipe, &iov);
749 if (iov.addr == 0)
750 break;
751 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600752 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600753 node = bam_rx_pool.next;
754 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600755 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600756 info = container_of(node, struct rx_pkt_info,
757 list_node);
758 handle_bam_mux_cmd(&info->work);
759 }
760
761 if (inactive_cycles == POLLING_INACTIVITY) {
762 /*
763 * attempt to enable interrupts in this pipe
764 * if enabling interrupts fails, continue polling
765 */
766 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
767 if (ret) {
768 pr_err("%s: sps_get_config() failed, interrupts"
769 " not enabled\n", __func__);
770 queue_work(bam_mux_rx_workqueue,
771 &rx_timer_work);
772 return;
773 } else {
774 rx_register_event.options = SPS_O_EOT;
775 /* should check return value */
776 sps_register_event(bam_rx_pipe,
777 &rx_register_event);
778 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
779 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
780 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
781 if (ret) {
782 pr_err("%s: sps_set_config() failed, "
783 "interrupts not enabled\n",
784 __func__);
785 queue_work(bam_mux_rx_workqueue,
786 &rx_timer_work);
787 return;
788 }
789 polling_mode = 0;
790 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600791 if (in_global_reset)
792 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600793 /* handle race condition - missed packet? */
794 sps_get_iovec(bam_rx_pipe, &iov);
795 if (iov.addr == 0)
796 return;
797 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600798 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600799 node = bam_rx_pool.next;
800 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600801 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600802 info = container_of(node, struct rx_pkt_info,
803 list_node);
804 handle_bam_mux_cmd(&info->work);
805 return;
806 }
807
808 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
809 }
810}
811
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812static void bam_mux_tx_notify(struct sps_event_notify *notify)
813{
814 struct tx_pkt_info *pkt;
815
816 DBG("%s: event %d notified\n", __func__, notify->event_id);
817
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600818 if (in_global_reset)
819 return;
820
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700821 switch (notify->event_id) {
822 case SPS_EVENT_EOT:
823 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600824 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 dma_unmap_single(NULL, pkt->dma_address,
826 pkt->skb->len,
827 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600828 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 dma_unmap_single(NULL, pkt->dma_address,
830 pkt->len,
831 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600832 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 break;
834 default:
835 pr_err("%s: recieved unexpected event id %d\n", __func__,
836 notify->event_id);
837 }
838}
839
Jeff Hugo33dbc002011-08-25 15:52:53 -0600840static void bam_mux_rx_notify(struct sps_event_notify *notify)
841{
Jeff Hugo949080a2011-08-30 11:58:56 -0600842 int ret;
843 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600844
845 DBG("%s: event %d notified\n", __func__, notify->event_id);
846
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600847 if (in_global_reset)
848 return;
849
Jeff Hugo33dbc002011-08-25 15:52:53 -0600850 switch (notify->event_id) {
851 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -0600852 /* attempt to disable interrupts in this pipe */
853 if (!polling_mode) {
854 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
855 if (ret) {
856 pr_err("%s: sps_get_config() failed, interrupts"
857 " not disabled\n", __func__);
858 break;
859 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -0700860 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -0600861 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
862 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
863 if (ret) {
864 pr_err("%s: sps_set_config() failed, interrupts"
865 " not disabled\n", __func__);
866 break;
867 }
868 polling_mode = 1;
869 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
870 }
Jeff Hugo33dbc002011-08-25 15:52:53 -0600871 break;
872 default:
873 pr_err("%s: recieved unexpected event id %d\n", __func__,
874 notify->event_id);
875 }
876}
877
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878#ifdef CONFIG_DEBUG_FS
879
880static int debug_tbl(char *buf, int max)
881{
882 int i = 0;
883 int j;
884
885 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
886 i += scnprintf(buf + i, max - i,
887 "ch%02d local open=%s remote open=%s\n",
888 j, bam_ch_is_local_open(j) ? "Y" : "N",
889 bam_ch_is_remote_open(j) ? "Y" : "N");
890 }
891
892 return i;
893}
894
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700895static int debug_ul_pkt_cnt(char *buf, int max)
896{
897 struct list_head *p;
898 unsigned long flags;
899 int n = 0;
900
901 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
902 __list_for_each(p, &bam_tx_pool) {
903 ++n;
904 }
905 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
906
907 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
908}
909
910static int debug_stats(char *buf, int max)
911{
912 int i = 0;
913
914 i += scnprintf(buf + i, max - i,
915 "skb copy cnt: %u\n"
916 "skb copy bytes: %u\n"
917 "sps tx failures: %u\n",
918 bam_dmux_write_cpy_cnt,
919 bam_dmux_write_cpy_bytes,
920 bam_dmux_tx_sps_failure_cnt
921 );
922
923 return i;
924}
925
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926#define DEBUG_BUFMAX 4096
927static char debug_buffer[DEBUG_BUFMAX];
928
929static ssize_t debug_read(struct file *file, char __user *buf,
930 size_t count, loff_t *ppos)
931{
932 int (*fill)(char *buf, int max) = file->private_data;
933 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
934 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
935}
936
937static int debug_open(struct inode *inode, struct file *file)
938{
939 file->private_data = inode->i_private;
940 return 0;
941}
942
943
944static const struct file_operations debug_ops = {
945 .read = debug_read,
946 .open = debug_open,
947};
948
949static void debug_create(const char *name, mode_t mode,
950 struct dentry *dent,
951 int (*fill)(char *buf, int max))
952{
953 debugfs_create_file(name, mode, dent, fill, &debug_ops);
954}
955
956#endif
957
Jeff Hugod98b1082011-10-24 10:30:23 -0600958static void notify_all(int event, unsigned long data)
959{
960 int i;
961
962 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
963 if (bam_ch_is_open(i))
964 bam_ch[i].notify(bam_ch[i].priv, event, data);
965 }
966}
967
968static void kickoff_ul_wakeup_func(struct work_struct *work)
969{
970 read_lock(&ul_wakeup_lock);
971 if (!bam_is_connected) {
972 read_unlock(&ul_wakeup_lock);
973 ul_wakeup();
974 read_lock(&ul_wakeup_lock);
975 ul_packet_written = 1;
976 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
977 }
978 read_unlock(&ul_wakeup_lock);
979}
980
981void msm_bam_dmux_kickoff_ul_wakeup(void)
982{
983 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
984}
985
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600986static void ul_timeout(struct work_struct *work)
987{
Jeff Hugoc040a5b2011-11-15 14:26:01 -0700988 unsigned long flags;
989 int ret;
990
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600991 if (in_global_reset)
992 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -0700993 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
994 if (!ret) { /* failed to grab lock, reschedule and bail */
995 schedule_delayed_work(&ul_timeout_work,
996 msecs_to_jiffies(UL_TIMEOUT_DELAY));
997 return;
998 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600999 if (ul_packet_written) {
1000 ul_packet_written = 0;
1001 schedule_delayed_work(&ul_timeout_work,
1002 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1003 } else {
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001004 wait_for_ack = 1;
1005 INIT_COMPLETION(ul_wakeup_ack_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001006 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1007 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -06001008 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001009 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001010 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001011}
1012static void ul_wakeup(void)
1013{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001014 int ret;
1015
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001016 mutex_lock(&wakeup_lock);
1017 if (bam_is_connected) { /* bam got connected before lock grabbed */
1018 mutex_unlock(&wakeup_lock);
1019 return;
1020 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001021 /*
1022 * must wait for the previous power down request to have been acked
1023 * chances are it already came in and this will just fall through
1024 * instead of waiting
1025 */
1026 if (wait_for_ack) {
1027 ret = wait_for_completion_interruptible_timeout(
1028 &ul_wakeup_ack_completion, HZ);
1029 BUG_ON(ret == 0);
1030 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001031 INIT_COMPLETION(ul_wakeup_ack_completion);
1032 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001033 ret = wait_for_completion_interruptible_timeout(
1034 &ul_wakeup_ack_completion, HZ);
1035 BUG_ON(ret == 0);
1036 ret = wait_for_completion_interruptible_timeout(
1037 &bam_connection_completion, HZ);
1038 BUG_ON(ret == 0);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001039
1040 bam_is_connected = 1;
1041 schedule_delayed_work(&ul_timeout_work,
1042 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1043 mutex_unlock(&wakeup_lock);
1044}
1045
1046static void reconnect_to_bam(void)
1047{
1048 int i;
1049
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001050 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001051 vote_dfab();
1052 i = sps_device_reset(a2_device_handle);
1053 if (i)
1054 pr_err("%s: device reset failed rc = %d\n", __func__, i);
1055 i = sps_connect(bam_tx_pipe, &tx_connection);
1056 if (i)
1057 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
1058 i = sps_connect(bam_rx_pipe, &rx_connection);
1059 if (i)
1060 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
1061 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1062 if (i)
1063 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
1064 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1065 if (i)
1066 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
1067 for (i = 0; i < NUM_BUFFERS; ++i)
1068 queue_rx();
1069 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001070 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001071 complete_all(&bam_connection_completion);
1072}
1073
1074static void disconnect_to_bam(void)
1075{
1076 struct list_head *node;
1077 struct rx_pkt_info *info;
1078
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001079 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001080 INIT_COMPLETION(bam_connection_completion);
1081 sps_disconnect(bam_tx_pipe);
1082 sps_disconnect(bam_rx_pipe);
1083 unvote_dfab();
1084 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1085 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1086 while (!list_empty(&bam_rx_pool)) {
1087 node = bam_rx_pool.next;
1088 list_del(node);
1089 info = container_of(node, struct rx_pkt_info, list_node);
1090 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1091 DMA_FROM_DEVICE);
1092 dev_kfree_skb_any(info->skb);
1093 kfree(info);
1094 }
1095}
1096
1097static void vote_dfab(void)
1098{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001099 int rc;
1100
1101 rc = clk_enable(dfab_clk);
1102 if (rc)
1103 pr_err("bam_dmux vote for dfab failed rc = %d\n", rc);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001104}
1105
1106static void unvote_dfab(void)
1107{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001108 clk_disable(dfab_clk);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001109}
1110
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001111static int restart_notifier_cb(struct notifier_block *this,
1112 unsigned long code,
1113 void *data)
1114{
1115 int i;
1116 struct list_head *node;
1117 struct tx_pkt_info *info;
1118 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001119 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001120
1121 if (code != SUBSYS_AFTER_SHUTDOWN)
1122 return NOTIFY_DONE;
1123
1124 in_global_reset = 1;
1125 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1126 temp_remote_status = bam_ch_is_remote_open(i);
1127 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001128 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001129 if (bam_ch_is_local_open(i))
1130 bam_ch[i].status |= BAM_CH_IN_RESET;
1131 if (temp_remote_status) {
1132 platform_device_unregister(bam_ch[i].pdev);
1133 bam_ch[i].pdev = platform_device_alloc(
1134 bam_ch[i].name, 2);
1135 }
1136 }
1137 /*cleanup UL*/
Jeff Hugo626303bf2011-11-21 11:43:28 -07001138 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001139 while (!list_empty(&bam_tx_pool)) {
1140 node = bam_tx_pool.next;
1141 list_del(node);
1142 info = container_of(node, struct tx_pkt_info,
1143 list_node);
1144 if (!info->is_cmd) {
1145 dma_unmap_single(NULL, info->dma_address,
1146 info->skb->len,
1147 DMA_TO_DEVICE);
1148 dev_kfree_skb_any(info->skb);
1149 } else {
1150 dma_unmap_single(NULL, info->dma_address,
1151 info->len,
1152 DMA_TO_DEVICE);
1153 kfree(info->skb);
1154 }
1155 kfree(info);
1156 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001157 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001158 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1159
1160 return NOTIFY_DONE;
1161}
1162
Jeff Hugoade1f842011-08-03 15:53:59 -06001163static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164{
1165 u32 h;
1166 dma_addr_t dma_addr;
1167 int ret;
1168 void *a2_virt_addr;
1169 int i;
1170
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001171 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001172 /* init BAM */
1173 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1174 if (!a2_virt_addr) {
1175 pr_err("%s: ioremap failed\n", __func__);
1176 ret = -ENOMEM;
1177 goto register_bam_failed;
1178 }
1179 a2_props.phys_addr = A2_PHYS_BASE;
1180 a2_props.virt_addr = a2_virt_addr;
1181 a2_props.virt_size = A2_PHYS_SIZE;
1182 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001183 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001184 a2_props.num_pipes = A2_NUM_PIPES;
1185 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001186 if (cpu_is_msm9615())
1187 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001188 /* need to free on tear down */
1189 ret = sps_register_bam_device(&a2_props, &h);
1190 if (ret < 0) {
1191 pr_err("%s: register bam error %d\n", __func__, ret);
1192 goto register_bam_failed;
1193 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001194 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001195
1196 bam_tx_pipe = sps_alloc_endpoint();
1197 if (bam_tx_pipe == NULL) {
1198 pr_err("%s: tx alloc endpoint failed\n", __func__);
1199 ret = -ENOMEM;
1200 goto register_bam_failed;
1201 }
1202 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1203 if (ret) {
1204 pr_err("%s: tx get config failed %d\n", __func__, ret);
1205 goto tx_get_config_failed;
1206 }
1207
1208 tx_connection.source = SPS_DEV_HANDLE_MEM;
1209 tx_connection.src_pipe_index = 0;
1210 tx_connection.destination = h;
1211 tx_connection.dest_pipe_index = 4;
1212 tx_connection.mode = SPS_MODE_DEST;
1213 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1214 tx_desc_mem_buf.size = 0x800; /* 2k */
1215 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1216 &dma_addr, 0);
1217 if (tx_desc_mem_buf.base == NULL) {
1218 pr_err("%s: tx memory alloc failed\n", __func__);
1219 ret = -ENOMEM;
1220 goto tx_mem_failed;
1221 }
1222 tx_desc_mem_buf.phys_base = dma_addr;
1223 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1224 tx_connection.desc = tx_desc_mem_buf;
1225 tx_connection.event_thresh = 0x10;
1226
1227 ret = sps_connect(bam_tx_pipe, &tx_connection);
1228 if (ret < 0) {
1229 pr_err("%s: tx connect error %d\n", __func__, ret);
1230 goto tx_connect_failed;
1231 }
1232
1233 bam_rx_pipe = sps_alloc_endpoint();
1234 if (bam_rx_pipe == NULL) {
1235 pr_err("%s: rx alloc endpoint failed\n", __func__);
1236 ret = -ENOMEM;
1237 goto tx_connect_failed;
1238 }
1239 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1240 if (ret) {
1241 pr_err("%s: rx get config failed %d\n", __func__, ret);
1242 goto rx_get_config_failed;
1243 }
1244
1245 rx_connection.source = h;
1246 rx_connection.src_pipe_index = 5;
1247 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1248 rx_connection.dest_pipe_index = 1;
1249 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001250 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1251 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001252 rx_desc_mem_buf.size = 0x800; /* 2k */
1253 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1254 &dma_addr, 0);
1255 if (rx_desc_mem_buf.base == NULL) {
1256 pr_err("%s: rx memory alloc failed\n", __func__);
1257 ret = -ENOMEM;
1258 goto rx_mem_failed;
1259 }
1260 rx_desc_mem_buf.phys_base = dma_addr;
1261 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1262 rx_connection.desc = rx_desc_mem_buf;
1263 rx_connection.event_thresh = 0x10;
1264
1265 ret = sps_connect(bam_rx_pipe, &rx_connection);
1266 if (ret < 0) {
1267 pr_err("%s: rx connect error %d\n", __func__, ret);
1268 goto rx_connect_failed;
1269 }
1270
1271 tx_register_event.options = SPS_O_EOT;
1272 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1273 tx_register_event.xfer_done = NULL;
1274 tx_register_event.callback = bam_mux_tx_notify;
1275 tx_register_event.user = NULL;
1276 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1277 if (ret < 0) {
1278 pr_err("%s: tx register event error %d\n", __func__, ret);
1279 goto rx_event_reg_failed;
1280 }
1281
Jeff Hugo33dbc002011-08-25 15:52:53 -06001282 rx_register_event.options = SPS_O_EOT;
1283 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1284 rx_register_event.xfer_done = NULL;
1285 rx_register_event.callback = bam_mux_rx_notify;
1286 rx_register_event.user = NULL;
1287 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1288 if (ret < 0) {
1289 pr_err("%s: tx register event error %d\n", __func__, ret);
1290 goto rx_event_reg_failed;
1291 }
1292
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293 bam_mux_initialized = 1;
1294 for (i = 0; i < NUM_BUFFERS; ++i)
1295 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001296 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001297 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001298 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001299 return;
1300
1301rx_event_reg_failed:
1302 sps_disconnect(bam_rx_pipe);
1303rx_connect_failed:
1304 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1305 rx_desc_mem_buf.phys_base);
1306rx_mem_failed:
1307 sps_disconnect(bam_tx_pipe);
1308rx_get_config_failed:
1309 sps_free_endpoint(bam_rx_pipe);
1310tx_connect_failed:
1311 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1312 tx_desc_mem_buf.phys_base);
1313tx_get_config_failed:
1314 sps_free_endpoint(bam_tx_pipe);
1315tx_mem_failed:
1316 sps_deregister_bam_device(h);
1317register_bam_failed:
1318 /*destroy_workqueue(bam_mux_workqueue);*/
1319 /*return ret;*/
1320 return;
1321}
Jeff Hugoade1f842011-08-03 15:53:59 -06001322
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001323static void toggle_apps_ack(void)
1324{
1325 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
1326 smsm_change_state(SMSM_APPS_STATE,
1327 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1328 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1329 clear_bit = ~clear_bit;
1330}
1331
Jeff Hugoade1f842011-08-03 15:53:59 -06001332static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1333{
1334 DBG("%s: smsm activity\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001335 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
1336 wake_lock(&bam_wakelock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001337 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001338 } else if (bam_mux_initialized &&
1339 !(new_state & SMSM_A2_POWER_CONTROL)) {
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001340 disconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001341 wake_unlock(&bam_wakelock);
1342 } else if (new_state & SMSM_A2_POWER_CONTROL) {
1343 wake_lock(&bam_wakelock);
Jeff Hugoade1f842011-08-03 15:53:59 -06001344 bam_init();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001345 } else {
Jeff Hugoade1f842011-08-03 15:53:59 -06001346 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001347 }
Jeff Hugoade1f842011-08-03 15:53:59 -06001348
1349}
1350
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001351static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1352 uint32_t new_state)
1353{
1354 complete_all(&ul_wakeup_ack_completion);
1355}
1356
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001357static int bam_dmux_probe(struct platform_device *pdev)
1358{
1359 int rc;
1360
1361 DBG("%s probe called\n", __func__);
1362 if (bam_mux_initialized)
1363 return 0;
1364
Stephen Boyd1c51a492011-10-26 12:11:47 -07001365 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001366 if (IS_ERR(dfab_clk)) {
1367 pr_err("%s: did not get dfab clock\n", __func__);
1368 return -EFAULT;
1369 }
1370
1371 rc = clk_set_rate(dfab_clk, 64000000);
1372 if (rc)
1373 pr_err("%s: unable to set dfab clock rate\n", __func__);
1374
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001375 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1376 if (!bam_mux_rx_workqueue)
1377 return -ENOMEM;
1378
1379 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1380 if (!bam_mux_tx_workqueue) {
1381 destroy_workqueue(bam_mux_rx_workqueue);
1382 return -ENOMEM;
1383 }
1384
Jeff Hugo7960abd2011-08-02 15:39:38 -06001385 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001386 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001387 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1388 "bam_dmux_ch_%d", rc);
1389 /* bus 2, ie a2 stream 2 */
1390 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1391 if (!bam_ch[rc].pdev) {
1392 pr_err("%s: platform device alloc failed\n", __func__);
1393 destroy_workqueue(bam_mux_rx_workqueue);
1394 destroy_workqueue(bam_mux_tx_workqueue);
1395 return -ENOMEM;
1396 }
1397 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001398
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001399 init_completion(&ul_wakeup_ack_completion);
1400 init_completion(&bam_connection_completion);
1401 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001402 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001403
Jeff Hugoade1f842011-08-03 15:53:59 -06001404 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1405 bam_dmux_smsm_cb, NULL);
1406
1407 if (rc) {
1408 destroy_workqueue(bam_mux_rx_workqueue);
1409 destroy_workqueue(bam_mux_tx_workqueue);
1410 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1411 return -ENOMEM;
1412 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001413
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001414 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1415 bam_dmux_smsm_ack_cb, NULL);
1416
1417 if (rc) {
1418 destroy_workqueue(bam_mux_rx_workqueue);
1419 destroy_workqueue(bam_mux_tx_workqueue);
1420 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1421 SMSM_A2_POWER_CONTROL,
1422 bam_dmux_smsm_cb, NULL);
1423 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1424 rc);
1425 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1426 platform_device_put(bam_ch[rc].pdev);
1427 return -ENOMEM;
1428 }
1429
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07001430 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
1431 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
1432
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433 return 0;
1434}
1435
1436static struct platform_driver bam_dmux_driver = {
1437 .probe = bam_dmux_probe,
1438 .driver = {
1439 .name = "BAM_RMNT",
1440 .owner = THIS_MODULE,
1441 },
1442};
1443
1444static int __init bam_dmux_init(void)
1445{
1446#ifdef CONFIG_DEBUG_FS
1447 struct dentry *dent;
1448
1449 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001450 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001451 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001452 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
1453 debug_create("stats", 0444, dent, debug_stats);
1454 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001455#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001456 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001457 return platform_driver_register(&bam_dmux_driver);
1458}
1459
Jeff Hugoade1f842011-08-03 15:53:59 -06001460late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001461MODULE_DESCRIPTION("MSM BAM DMUX");
1462MODULE_LICENSE("GPL v2");