blob: 45f10334ffb4cd745d755c7903cdc3286e58e0e2 [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);
241 ptr = skb_put(info->skb, BUFFER_SIZE);
Jeff Hugo949080a2011-08-30 11:58:56 -0600242
Jeff Hugoc9749932011-11-02 17:50:40 -0600243 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600244 list_add_tail(&info->list_node, &bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600245 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600246
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247 /* need a way to handle error case */
248 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
249 DMA_FROM_DEVICE);
250 sps_transfer_one(bam_rx_pipe, info->dma_address,
Jeff Hugo33dbc002011-08-25 15:52:53 -0600251 BUFFER_SIZE, info,
252 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253}
254
255static void bam_mux_process_data(struct sk_buff *rx_skb)
256{
257 unsigned long flags;
258 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600259 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260
261 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
262
263 rx_skb->data = (unsigned char *)(rx_hdr + 1);
264 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
265 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600266 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600268 event_data = (unsigned long)(rx_skb);
269
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600271 if (bam_ch[rx_hdr->ch_id].notify)
272 bam_ch[rx_hdr->ch_id].notify(
273 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
274 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275 else
276 dev_kfree_skb_any(rx_skb);
277 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
278
279 queue_rx();
280}
281
282static void handle_bam_mux_cmd(struct work_struct *work)
283{
284 unsigned long flags;
285 struct bam_mux_hdr *rx_hdr;
286 struct rx_pkt_info *info;
287 struct sk_buff *rx_skb;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600288 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289
290 info = container_of(work, struct rx_pkt_info, work);
291 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600292 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 kfree(info);
294
295 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
296
297 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
298 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
299 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
300 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
301 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
302 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
303 " 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 dev_kfree_skb_any(rx_skb);
307 queue_rx();
308 return;
309 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700310
311 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
312 pr_err("%s: dropping invalid LCID %d reserved %d cmd %d"
313 " pad %d ch %d len %d\n", __func__,
314 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
315 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
316 dev_kfree_skb_any(rx_skb);
317 queue_rx();
318 return;
319 }
320
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321 switch (rx_hdr->cmd) {
322 case BAM_MUX_HDR_CMD_DATA:
323 DBG_INC_READ_CNT(rx_hdr->pkt_len);
324 bam_mux_process_data(rx_skb);
325 break;
326 case BAM_MUX_HDR_CMD_OPEN:
327 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
328 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700329 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700330 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600332 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
333 if (ret)
334 pr_err("%s: platform_device_add() error: %d\n",
335 __func__, ret);
Eric Holmberge779dba2011-11-04 18:22:01 -0600336 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 break;
338 case BAM_MUX_HDR_CMD_CLOSE:
339 /* probably should drop pending write */
340 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
341 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
342 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600344 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
345 bam_ch[rx_hdr->ch_id].pdev =
346 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
347 if (!bam_ch[rx_hdr->ch_id].pdev)
348 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberge779dba2011-11-04 18:22:01 -0600349 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350 break;
351 default:
352 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
353 " pad %d ch %d len %d\n", __func__,
354 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
355 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
356 dev_kfree_skb_any(rx_skb);
357 queue_rx();
358 return;
359 }
360}
361
362static int bam_mux_write_cmd(void *data, uint32_t len)
363{
364 int rc;
365 struct tx_pkt_info *pkt;
366 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700367 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600369 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370 if (pkt == NULL) {
371 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
372 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700373 return rc;
374 }
375
376 dma_address = dma_map_single(NULL, data, len,
377 DMA_TO_DEVICE);
378 if (!dma_address) {
379 pr_err("%s: dma_map_single() failed\n", __func__);
380 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 return rc;
382 }
383 pkt->skb = (struct sk_buff *)(data);
384 pkt->len = len;
385 pkt->dma_address = dma_address;
386 pkt->is_cmd = 1;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600387 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700388 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600389 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700390 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
392 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600393 if (rc) {
394 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700395 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600396 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700397 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700398 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600399 kfree(pkt);
400 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600402 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403 return rc;
404}
405
406static void bam_mux_write_done(struct work_struct *work)
407{
408 struct sk_buff *skb;
409 struct bam_mux_hdr *hdr;
410 struct tx_pkt_info *info;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600411 unsigned long event_data;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600412 struct list_head *node;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700413 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600415 if (in_global_reset)
416 return;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700417 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600418 node = bam_tx_pool.next;
419 list_del(node);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700420 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421 info = container_of(work, struct tx_pkt_info, work);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600422 if (info->is_cmd) {
423 kfree(info->skb);
424 kfree(info);
425 return;
426 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427 skb = info->skb;
428 kfree(info);
429 hdr = (struct bam_mux_hdr *)skb->data;
430 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600431 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700432 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
433 bam_ch[hdr->ch_id].num_tx_pkts--;
434 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600435 if (bam_ch[hdr->ch_id].notify)
436 bam_ch[hdr->ch_id].notify(
437 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
438 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439 else
440 dev_kfree_skb_any(skb);
441}
442
443int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
444{
445 int rc = 0;
446 struct bam_mux_hdr *hdr;
447 unsigned long flags;
448 struct sk_buff *new_skb = NULL;
449 dma_addr_t dma_address;
450 struct tx_pkt_info *pkt;
451
452 if (id >= BAM_DMUX_NUM_CHANNELS)
453 return -EINVAL;
454 if (!skb)
455 return -EINVAL;
456 if (!bam_mux_initialized)
457 return -ENODEV;
458
459 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
460 spin_lock_irqsave(&bam_ch[id].lock, flags);
461 if (!bam_ch_is_open(id)) {
462 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
463 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
464 return -ENODEV;
465 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700466
467 if (bam_ch[id].use_wm &&
468 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
469 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
470 pr_err("%s: watermark exceeded: %d\n", __func__, id);
471 return -EAGAIN;
472 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
474
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600475 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600476 if (!bam_is_connected) {
477 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600478 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600479 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600480 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600481 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600482
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483 /* if skb do not have any tailroom for padding,
484 copy the skb into a new expanded skb */
485 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
486 /* revisit, probably dev_alloc_skb and memcpy is effecient */
487 new_skb = skb_copy_expand(skb, skb_headroom(skb),
488 4 - (skb->len & 0x3), GFP_ATOMIC);
489 if (new_skb == NULL) {
490 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600491 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 }
493 dev_kfree_skb_any(skb);
494 skb = new_skb;
495 DBG_INC_WRITE_CPY(skb->len);
496 }
497
498 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
499
500 /* caller should allocate for hdr and padding
501 hdr is fine, padding is tricky */
502 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
503 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
504 hdr->reserved = 0;
505 hdr->ch_id = id;
506 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
507 if (skb->len & 0x3)
508 skb_put(skb, 4 - (skb->len & 0x3));
509
510 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
511
512 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
513 __func__, skb->data, skb->tail, skb->len,
514 hdr->pkt_len, hdr->pad_len);
515
516 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
517 if (pkt == NULL) {
518 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600519 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 }
521
522 dma_address = dma_map_single(NULL, skb->data, skb->len,
523 DMA_TO_DEVICE);
524 if (!dma_address) {
525 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600526 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527 }
528 pkt->skb = skb;
529 pkt->dma_address = dma_address;
530 pkt->is_cmd = 0;
531 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700532 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600533 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700534 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
536 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600537 if (rc) {
538 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700539 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600540 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700541 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700542 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600543 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700544 if (new_skb)
545 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700546 } else {
547 spin_lock_irqsave(&bam_ch[id].lock, flags);
548 bam_ch[id].num_tx_pkts++;
549 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600550 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600551 ul_packet_written = 1;
552 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600554
555write_fail3:
556 kfree(pkt);
557write_fail2:
558 if (new_skb)
559 dev_kfree_skb_any(new_skb);
560write_fail:
561 read_unlock(&ul_wakeup_lock);
562 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563}
564
565int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600566 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567{
568 struct bam_mux_hdr *hdr;
569 unsigned long flags;
570 int rc = 0;
571
572 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700573 if (!bam_mux_initialized) {
574 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700576 }
577 if (id >= BAM_DMUX_NUM_CHANNELS) {
578 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700580 }
581 if (notify == NULL) {
582 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600583 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700584 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585
586 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
587 if (hdr == NULL) {
588 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
589 return -ENOMEM;
590 }
591 spin_lock_irqsave(&bam_ch[id].lock, flags);
592 if (bam_ch_is_open(id)) {
593 DBG("%s: Already opened %d\n", __func__, id);
594 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
595 kfree(hdr);
596 goto open_done;
597 }
598 if (!bam_ch_is_remote_open(id)) {
599 DBG("%s: Remote not open; ch: %d\n", __func__, id);
600 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
601 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700602 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 }
604
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600605 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606 bam_ch[id].priv = priv;
607 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700608 bam_ch[id].num_tx_pkts = 0;
609 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
611
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600612 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600613 if (!bam_is_connected) {
614 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600615 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600616 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600617 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600618 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600619
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
621 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
622 hdr->reserved = 0;
623 hdr->ch_id = id;
624 hdr->pkt_len = 0;
625 hdr->pad_len = 0;
626
627 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600628 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700629
630open_done:
631 DBG("%s: opened ch %d\n", __func__, id);
632 return rc;
633}
634
635int msm_bam_dmux_close(uint32_t id)
636{
637 struct bam_mux_hdr *hdr;
638 unsigned long flags;
639 int rc;
640
641 if (id >= BAM_DMUX_NUM_CHANNELS)
642 return -EINVAL;
643 DBG("%s: closing ch %d\n", __func__, id);
644 if (!bam_mux_initialized)
645 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600647 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600648 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600649 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600650 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600651 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600652 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600653 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600654
Jeff Hugo061ce672011-10-21 17:15:32 -0600655 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600656 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 bam_ch[id].priv = NULL;
658 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
659 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
660
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600661 if (bam_ch_is_in_reset(id)) {
662 read_unlock(&ul_wakeup_lock);
663 bam_ch[id].status &= ~BAM_CH_IN_RESET;
664 return 0;
665 }
666
Jeff Hugobb5802f2011-11-02 17:10:29 -0600667 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668 if (hdr == NULL) {
669 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600670 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700671 return -ENOMEM;
672 }
673 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
674 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
675 hdr->reserved = 0;
676 hdr->ch_id = id;
677 hdr->pkt_len = 0;
678 hdr->pad_len = 0;
679
680 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600681 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682
683 DBG("%s: closed ch %d\n", __func__, id);
684 return rc;
685}
686
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700687int msm_bam_dmux_is_ch_full(uint32_t id)
688{
689 unsigned long flags;
690 int ret;
691
692 if (id >= BAM_DMUX_NUM_CHANNELS)
693 return -EINVAL;
694
695 spin_lock_irqsave(&bam_ch[id].lock, flags);
696 bam_ch[id].use_wm = 1;
697 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
698 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
699 id, bam_ch[id].num_tx_pkts, ret);
700 if (!bam_ch_is_local_open(id)) {
701 ret = -ENODEV;
702 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
703 }
704 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
705
706 return ret;
707}
708
709int msm_bam_dmux_is_ch_low(uint32_t id)
710{
711 int ret;
712
713 if (id >= BAM_DMUX_NUM_CHANNELS)
714 return -EINVAL;
715
716 bam_ch[id].use_wm = 1;
717 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
718 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
719 id, bam_ch[id].num_tx_pkts, ret);
720 if (!bam_ch_is_local_open(id)) {
721 ret = -ENODEV;
722 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
723 }
724
725 return ret;
726}
727
Jeff Hugo949080a2011-08-30 11:58:56 -0600728static void rx_timer_work_func(struct work_struct *work)
729{
730 struct sps_iovec iov;
731 struct list_head *node;
732 struct rx_pkt_info *info;
733 int inactive_cycles = 0;
734 int ret;
735 struct sps_connect cur_rx_conn;
736
737 while (1) { /* timer loop */
738 ++inactive_cycles;
739 while (1) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600740 if (in_global_reset)
741 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600742 sps_get_iovec(bam_rx_pipe, &iov);
743 if (iov.addr == 0)
744 break;
745 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600746 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600747 node = bam_rx_pool.next;
748 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600749 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600750 info = container_of(node, struct rx_pkt_info,
751 list_node);
752 handle_bam_mux_cmd(&info->work);
753 }
754
755 if (inactive_cycles == POLLING_INACTIVITY) {
756 /*
757 * attempt to enable interrupts in this pipe
758 * if enabling interrupts fails, continue polling
759 */
760 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
761 if (ret) {
762 pr_err("%s: sps_get_config() failed, interrupts"
763 " not enabled\n", __func__);
764 queue_work(bam_mux_rx_workqueue,
765 &rx_timer_work);
766 return;
767 } else {
768 rx_register_event.options = SPS_O_EOT;
769 /* should check return value */
770 sps_register_event(bam_rx_pipe,
771 &rx_register_event);
772 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
773 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
774 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
775 if (ret) {
776 pr_err("%s: sps_set_config() failed, "
777 "interrupts not enabled\n",
778 __func__);
779 queue_work(bam_mux_rx_workqueue,
780 &rx_timer_work);
781 return;
782 }
783 polling_mode = 0;
784 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600785 if (in_global_reset)
786 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600787 /* handle race condition - missed packet? */
788 sps_get_iovec(bam_rx_pipe, &iov);
789 if (iov.addr == 0)
790 return;
791 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600792 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600793 node = bam_rx_pool.next;
794 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600795 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600796 info = container_of(node, struct rx_pkt_info,
797 list_node);
798 handle_bam_mux_cmd(&info->work);
799 return;
800 }
801
802 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
803 }
804}
805
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806static void bam_mux_tx_notify(struct sps_event_notify *notify)
807{
808 struct tx_pkt_info *pkt;
809
810 DBG("%s: event %d notified\n", __func__, notify->event_id);
811
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600812 if (in_global_reset)
813 return;
814
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 switch (notify->event_id) {
816 case SPS_EVENT_EOT:
817 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600818 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 dma_unmap_single(NULL, pkt->dma_address,
820 pkt->skb->len,
821 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600822 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 dma_unmap_single(NULL, pkt->dma_address,
824 pkt->len,
825 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600826 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827 break;
828 default:
829 pr_err("%s: recieved unexpected event id %d\n", __func__,
830 notify->event_id);
831 }
832}
833
Jeff Hugo33dbc002011-08-25 15:52:53 -0600834static void bam_mux_rx_notify(struct sps_event_notify *notify)
835{
Jeff Hugo949080a2011-08-30 11:58:56 -0600836 int ret;
837 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600838
839 DBG("%s: event %d notified\n", __func__, notify->event_id);
840
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600841 if (in_global_reset)
842 return;
843
Jeff Hugo33dbc002011-08-25 15:52:53 -0600844 switch (notify->event_id) {
845 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -0600846 /* attempt to disable interrupts in this pipe */
847 if (!polling_mode) {
848 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
849 if (ret) {
850 pr_err("%s: sps_get_config() failed, interrupts"
851 " not disabled\n", __func__);
852 break;
853 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -0700854 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -0600855 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
856 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
857 if (ret) {
858 pr_err("%s: sps_set_config() failed, interrupts"
859 " not disabled\n", __func__);
860 break;
861 }
862 polling_mode = 1;
863 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
864 }
Jeff Hugo33dbc002011-08-25 15:52:53 -0600865 break;
866 default:
867 pr_err("%s: recieved unexpected event id %d\n", __func__,
868 notify->event_id);
869 }
870}
871
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700872#ifdef CONFIG_DEBUG_FS
873
874static int debug_tbl(char *buf, int max)
875{
876 int i = 0;
877 int j;
878
879 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
880 i += scnprintf(buf + i, max - i,
881 "ch%02d local open=%s remote open=%s\n",
882 j, bam_ch_is_local_open(j) ? "Y" : "N",
883 bam_ch_is_remote_open(j) ? "Y" : "N");
884 }
885
886 return i;
887}
888
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700889static int debug_ul_pkt_cnt(char *buf, int max)
890{
891 struct list_head *p;
892 unsigned long flags;
893 int n = 0;
894
895 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
896 __list_for_each(p, &bam_tx_pool) {
897 ++n;
898 }
899 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
900
901 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
902}
903
904static int debug_stats(char *buf, int max)
905{
906 int i = 0;
907
908 i += scnprintf(buf + i, max - i,
909 "skb copy cnt: %u\n"
910 "skb copy bytes: %u\n"
911 "sps tx failures: %u\n",
912 bam_dmux_write_cpy_cnt,
913 bam_dmux_write_cpy_bytes,
914 bam_dmux_tx_sps_failure_cnt
915 );
916
917 return i;
918}
919
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920#define DEBUG_BUFMAX 4096
921static char debug_buffer[DEBUG_BUFMAX];
922
923static ssize_t debug_read(struct file *file, char __user *buf,
924 size_t count, loff_t *ppos)
925{
926 int (*fill)(char *buf, int max) = file->private_data;
927 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
928 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
929}
930
931static int debug_open(struct inode *inode, struct file *file)
932{
933 file->private_data = inode->i_private;
934 return 0;
935}
936
937
938static const struct file_operations debug_ops = {
939 .read = debug_read,
940 .open = debug_open,
941};
942
943static void debug_create(const char *name, mode_t mode,
944 struct dentry *dent,
945 int (*fill)(char *buf, int max))
946{
947 debugfs_create_file(name, mode, dent, fill, &debug_ops);
948}
949
950#endif
951
Jeff Hugod98b1082011-10-24 10:30:23 -0600952static void notify_all(int event, unsigned long data)
953{
954 int i;
955
956 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
957 if (bam_ch_is_open(i))
958 bam_ch[i].notify(bam_ch[i].priv, event, data);
959 }
960}
961
962static void kickoff_ul_wakeup_func(struct work_struct *work)
963{
964 read_lock(&ul_wakeup_lock);
965 if (!bam_is_connected) {
966 read_unlock(&ul_wakeup_lock);
967 ul_wakeup();
968 read_lock(&ul_wakeup_lock);
969 ul_packet_written = 1;
970 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
971 }
972 read_unlock(&ul_wakeup_lock);
973}
974
975void msm_bam_dmux_kickoff_ul_wakeup(void)
976{
977 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
978}
979
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600980static void ul_timeout(struct work_struct *work)
981{
Jeff Hugoc040a5b2011-11-15 14:26:01 -0700982 unsigned long flags;
983 int ret;
984
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600985 if (in_global_reset)
986 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -0700987 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
988 if (!ret) { /* failed to grab lock, reschedule and bail */
989 schedule_delayed_work(&ul_timeout_work,
990 msecs_to_jiffies(UL_TIMEOUT_DELAY));
991 return;
992 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600993 if (ul_packet_written) {
994 ul_packet_written = 0;
995 schedule_delayed_work(&ul_timeout_work,
996 msecs_to_jiffies(UL_TIMEOUT_DELAY));
997 } else {
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700998 wait_for_ack = 1;
999 INIT_COMPLETION(ul_wakeup_ack_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001000 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1001 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -06001002 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001003 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001004 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001005}
1006static void ul_wakeup(void)
1007{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001008 int ret;
1009
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001010 mutex_lock(&wakeup_lock);
1011 if (bam_is_connected) { /* bam got connected before lock grabbed */
1012 mutex_unlock(&wakeup_lock);
1013 return;
1014 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001015 /*
1016 * must wait for the previous power down request to have been acked
1017 * chances are it already came in and this will just fall through
1018 * instead of waiting
1019 */
1020 if (wait_for_ack) {
1021 ret = wait_for_completion_interruptible_timeout(
1022 &ul_wakeup_ack_completion, HZ);
1023 BUG_ON(ret == 0);
1024 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001025 INIT_COMPLETION(ul_wakeup_ack_completion);
1026 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001027 ret = wait_for_completion_interruptible_timeout(
1028 &ul_wakeup_ack_completion, HZ);
1029 BUG_ON(ret == 0);
1030 ret = wait_for_completion_interruptible_timeout(
1031 &bam_connection_completion, HZ);
1032 BUG_ON(ret == 0);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001033
1034 bam_is_connected = 1;
1035 schedule_delayed_work(&ul_timeout_work,
1036 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1037 mutex_unlock(&wakeup_lock);
1038}
1039
1040static void reconnect_to_bam(void)
1041{
1042 int i;
1043
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001044 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001045 vote_dfab();
1046 i = sps_device_reset(a2_device_handle);
1047 if (i)
1048 pr_err("%s: device reset failed rc = %d\n", __func__, i);
1049 i = sps_connect(bam_tx_pipe, &tx_connection);
1050 if (i)
1051 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
1052 i = sps_connect(bam_rx_pipe, &rx_connection);
1053 if (i)
1054 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
1055 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1056 if (i)
1057 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
1058 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1059 if (i)
1060 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
1061 for (i = 0; i < NUM_BUFFERS; ++i)
1062 queue_rx();
1063 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001064 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001065 complete_all(&bam_connection_completion);
1066}
1067
1068static void disconnect_to_bam(void)
1069{
1070 struct list_head *node;
1071 struct rx_pkt_info *info;
1072
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001073 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001074 INIT_COMPLETION(bam_connection_completion);
1075 sps_disconnect(bam_tx_pipe);
1076 sps_disconnect(bam_rx_pipe);
1077 unvote_dfab();
1078 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1079 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1080 while (!list_empty(&bam_rx_pool)) {
1081 node = bam_rx_pool.next;
1082 list_del(node);
1083 info = container_of(node, struct rx_pkt_info, list_node);
1084 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1085 DMA_FROM_DEVICE);
1086 dev_kfree_skb_any(info->skb);
1087 kfree(info);
1088 }
1089}
1090
1091static void vote_dfab(void)
1092{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001093 int rc;
1094
1095 rc = clk_enable(dfab_clk);
1096 if (rc)
1097 pr_err("bam_dmux vote for dfab failed rc = %d\n", rc);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001098}
1099
1100static void unvote_dfab(void)
1101{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001102 clk_disable(dfab_clk);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001103}
1104
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001105static int restart_notifier_cb(struct notifier_block *this,
1106 unsigned long code,
1107 void *data)
1108{
1109 int i;
1110 struct list_head *node;
1111 struct tx_pkt_info *info;
1112 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001113 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001114
1115 if (code != SUBSYS_AFTER_SHUTDOWN)
1116 return NOTIFY_DONE;
1117
1118 in_global_reset = 1;
1119 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1120 temp_remote_status = bam_ch_is_remote_open(i);
1121 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001122 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001123 if (bam_ch_is_local_open(i))
1124 bam_ch[i].status |= BAM_CH_IN_RESET;
1125 if (temp_remote_status) {
1126 platform_device_unregister(bam_ch[i].pdev);
1127 bam_ch[i].pdev = platform_device_alloc(
1128 bam_ch[i].name, 2);
1129 }
1130 }
1131 /*cleanup UL*/
Jeff Hugo626303bf2011-11-21 11:43:28 -07001132 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001133 while (!list_empty(&bam_tx_pool)) {
1134 node = bam_tx_pool.next;
1135 list_del(node);
1136 info = container_of(node, struct tx_pkt_info,
1137 list_node);
1138 if (!info->is_cmd) {
1139 dma_unmap_single(NULL, info->dma_address,
1140 info->skb->len,
1141 DMA_TO_DEVICE);
1142 dev_kfree_skb_any(info->skb);
1143 } else {
1144 dma_unmap_single(NULL, info->dma_address,
1145 info->len,
1146 DMA_TO_DEVICE);
1147 kfree(info->skb);
1148 }
1149 kfree(info);
1150 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001151 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001152 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1153
1154 return NOTIFY_DONE;
1155}
1156
Jeff Hugoade1f842011-08-03 15:53:59 -06001157static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158{
1159 u32 h;
1160 dma_addr_t dma_addr;
1161 int ret;
1162 void *a2_virt_addr;
1163 int i;
1164
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001165 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001166 /* init BAM */
1167 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1168 if (!a2_virt_addr) {
1169 pr_err("%s: ioremap failed\n", __func__);
1170 ret = -ENOMEM;
1171 goto register_bam_failed;
1172 }
1173 a2_props.phys_addr = A2_PHYS_BASE;
1174 a2_props.virt_addr = a2_virt_addr;
1175 a2_props.virt_size = A2_PHYS_SIZE;
1176 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001177 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 a2_props.num_pipes = A2_NUM_PIPES;
1179 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001180 if (cpu_is_msm9615())
1181 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182 /* need to free on tear down */
1183 ret = sps_register_bam_device(&a2_props, &h);
1184 if (ret < 0) {
1185 pr_err("%s: register bam error %d\n", __func__, ret);
1186 goto register_bam_failed;
1187 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001188 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189
1190 bam_tx_pipe = sps_alloc_endpoint();
1191 if (bam_tx_pipe == NULL) {
1192 pr_err("%s: tx alloc endpoint failed\n", __func__);
1193 ret = -ENOMEM;
1194 goto register_bam_failed;
1195 }
1196 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1197 if (ret) {
1198 pr_err("%s: tx get config failed %d\n", __func__, ret);
1199 goto tx_get_config_failed;
1200 }
1201
1202 tx_connection.source = SPS_DEV_HANDLE_MEM;
1203 tx_connection.src_pipe_index = 0;
1204 tx_connection.destination = h;
1205 tx_connection.dest_pipe_index = 4;
1206 tx_connection.mode = SPS_MODE_DEST;
1207 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1208 tx_desc_mem_buf.size = 0x800; /* 2k */
1209 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1210 &dma_addr, 0);
1211 if (tx_desc_mem_buf.base == NULL) {
1212 pr_err("%s: tx memory alloc failed\n", __func__);
1213 ret = -ENOMEM;
1214 goto tx_mem_failed;
1215 }
1216 tx_desc_mem_buf.phys_base = dma_addr;
1217 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1218 tx_connection.desc = tx_desc_mem_buf;
1219 tx_connection.event_thresh = 0x10;
1220
1221 ret = sps_connect(bam_tx_pipe, &tx_connection);
1222 if (ret < 0) {
1223 pr_err("%s: tx connect error %d\n", __func__, ret);
1224 goto tx_connect_failed;
1225 }
1226
1227 bam_rx_pipe = sps_alloc_endpoint();
1228 if (bam_rx_pipe == NULL) {
1229 pr_err("%s: rx alloc endpoint failed\n", __func__);
1230 ret = -ENOMEM;
1231 goto tx_connect_failed;
1232 }
1233 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1234 if (ret) {
1235 pr_err("%s: rx get config failed %d\n", __func__, ret);
1236 goto rx_get_config_failed;
1237 }
1238
1239 rx_connection.source = h;
1240 rx_connection.src_pipe_index = 5;
1241 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1242 rx_connection.dest_pipe_index = 1;
1243 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001244 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1245 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246 rx_desc_mem_buf.size = 0x800; /* 2k */
1247 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1248 &dma_addr, 0);
1249 if (rx_desc_mem_buf.base == NULL) {
1250 pr_err("%s: rx memory alloc failed\n", __func__);
1251 ret = -ENOMEM;
1252 goto rx_mem_failed;
1253 }
1254 rx_desc_mem_buf.phys_base = dma_addr;
1255 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1256 rx_connection.desc = rx_desc_mem_buf;
1257 rx_connection.event_thresh = 0x10;
1258
1259 ret = sps_connect(bam_rx_pipe, &rx_connection);
1260 if (ret < 0) {
1261 pr_err("%s: rx connect error %d\n", __func__, ret);
1262 goto rx_connect_failed;
1263 }
1264
1265 tx_register_event.options = SPS_O_EOT;
1266 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1267 tx_register_event.xfer_done = NULL;
1268 tx_register_event.callback = bam_mux_tx_notify;
1269 tx_register_event.user = NULL;
1270 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1271 if (ret < 0) {
1272 pr_err("%s: tx register event error %d\n", __func__, ret);
1273 goto rx_event_reg_failed;
1274 }
1275
Jeff Hugo33dbc002011-08-25 15:52:53 -06001276 rx_register_event.options = SPS_O_EOT;
1277 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1278 rx_register_event.xfer_done = NULL;
1279 rx_register_event.callback = bam_mux_rx_notify;
1280 rx_register_event.user = NULL;
1281 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1282 if (ret < 0) {
1283 pr_err("%s: tx register event error %d\n", __func__, ret);
1284 goto rx_event_reg_failed;
1285 }
1286
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001287 bam_mux_initialized = 1;
1288 for (i = 0; i < NUM_BUFFERS; ++i)
1289 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001290 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001291 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001292 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293 return;
1294
1295rx_event_reg_failed:
1296 sps_disconnect(bam_rx_pipe);
1297rx_connect_failed:
1298 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1299 rx_desc_mem_buf.phys_base);
1300rx_mem_failed:
1301 sps_disconnect(bam_tx_pipe);
1302rx_get_config_failed:
1303 sps_free_endpoint(bam_rx_pipe);
1304tx_connect_failed:
1305 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1306 tx_desc_mem_buf.phys_base);
1307tx_get_config_failed:
1308 sps_free_endpoint(bam_tx_pipe);
1309tx_mem_failed:
1310 sps_deregister_bam_device(h);
1311register_bam_failed:
1312 /*destroy_workqueue(bam_mux_workqueue);*/
1313 /*return ret;*/
1314 return;
1315}
Jeff Hugoade1f842011-08-03 15:53:59 -06001316
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001317static void toggle_apps_ack(void)
1318{
1319 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
1320 smsm_change_state(SMSM_APPS_STATE,
1321 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1322 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1323 clear_bit = ~clear_bit;
1324}
1325
Jeff Hugoade1f842011-08-03 15:53:59 -06001326static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1327{
1328 DBG("%s: smsm activity\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001329 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
1330 wake_lock(&bam_wakelock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001331 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001332 } else if (bam_mux_initialized &&
1333 !(new_state & SMSM_A2_POWER_CONTROL)) {
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001334 disconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001335 wake_unlock(&bam_wakelock);
1336 } else if (new_state & SMSM_A2_POWER_CONTROL) {
1337 wake_lock(&bam_wakelock);
Jeff Hugoade1f842011-08-03 15:53:59 -06001338 bam_init();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001339 } else {
Jeff Hugoade1f842011-08-03 15:53:59 -06001340 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001341 }
Jeff Hugoade1f842011-08-03 15:53:59 -06001342
1343}
1344
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001345static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1346 uint32_t new_state)
1347{
1348 complete_all(&ul_wakeup_ack_completion);
1349}
1350
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001351static int bam_dmux_probe(struct platform_device *pdev)
1352{
1353 int rc;
1354
1355 DBG("%s probe called\n", __func__);
1356 if (bam_mux_initialized)
1357 return 0;
1358
Stephen Boyd1c51a492011-10-26 12:11:47 -07001359 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001360 if (IS_ERR(dfab_clk)) {
1361 pr_err("%s: did not get dfab clock\n", __func__);
1362 return -EFAULT;
1363 }
1364
1365 rc = clk_set_rate(dfab_clk, 64000000);
1366 if (rc)
1367 pr_err("%s: unable to set dfab clock rate\n", __func__);
1368
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001369 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1370 if (!bam_mux_rx_workqueue)
1371 return -ENOMEM;
1372
1373 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1374 if (!bam_mux_tx_workqueue) {
1375 destroy_workqueue(bam_mux_rx_workqueue);
1376 return -ENOMEM;
1377 }
1378
Jeff Hugo7960abd2011-08-02 15:39:38 -06001379 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001380 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001381 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1382 "bam_dmux_ch_%d", rc);
1383 /* bus 2, ie a2 stream 2 */
1384 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1385 if (!bam_ch[rc].pdev) {
1386 pr_err("%s: platform device alloc failed\n", __func__);
1387 destroy_workqueue(bam_mux_rx_workqueue);
1388 destroy_workqueue(bam_mux_tx_workqueue);
1389 return -ENOMEM;
1390 }
1391 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001392
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001393 init_completion(&ul_wakeup_ack_completion);
1394 init_completion(&bam_connection_completion);
1395 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001396 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001397
Jeff Hugoade1f842011-08-03 15:53:59 -06001398 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1399 bam_dmux_smsm_cb, NULL);
1400
1401 if (rc) {
1402 destroy_workqueue(bam_mux_rx_workqueue);
1403 destroy_workqueue(bam_mux_tx_workqueue);
1404 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1405 return -ENOMEM;
1406 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001407
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001408 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1409 bam_dmux_smsm_ack_cb, NULL);
1410
1411 if (rc) {
1412 destroy_workqueue(bam_mux_rx_workqueue);
1413 destroy_workqueue(bam_mux_tx_workqueue);
1414 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1415 SMSM_A2_POWER_CONTROL,
1416 bam_dmux_smsm_cb, NULL);
1417 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1418 rc);
1419 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1420 platform_device_put(bam_ch[rc].pdev);
1421 return -ENOMEM;
1422 }
1423
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07001424 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
1425 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
1426
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001427 return 0;
1428}
1429
1430static struct platform_driver bam_dmux_driver = {
1431 .probe = bam_dmux_probe,
1432 .driver = {
1433 .name = "BAM_RMNT",
1434 .owner = THIS_MODULE,
1435 },
1436};
1437
1438static int __init bam_dmux_init(void)
1439{
1440#ifdef CONFIG_DEBUG_FS
1441 struct dentry *dent;
1442
1443 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001444 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001445 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001446 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
1447 debug_create("stats", 0444, dent, debug_stats);
1448 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001449#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001450 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001451 return platform_driver_register(&bam_dmux_driver);
1452}
1453
Jeff Hugoade1f842011-08-03 15:53:59 -06001454late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001455MODULE_DESCRIPTION("MSM BAM DMUX");
1456MODULE_LICENSE("GPL v2");