blob: b9183d27e03c1805f5c73f025afea0f01397b153 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14/*
15 * BAM DMUX module.
16 */
17
18#define DEBUG
19
20#include <linux/delay.h>
21#include <linux/module.h>
22#include <linux/netdevice.h>
23#include <linux/platform_device.h>
24#include <linux/sched.h>
25#include <linux/skbuff.h>
26#include <linux/debugfs.h>
Jeff Hugoaab7ebc2011-09-07 16:46:04 -060027#include <linux/clk.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
29#include <mach/sps.h>
30#include <mach/bam_dmux.h>
Jeff Hugoade1f842011-08-03 15:53:59 -060031#include <mach/msm_smsm.h>
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060032#include <mach/subsystem_notif.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34#define BAM_CH_LOCAL_OPEN 0x1
35#define BAM_CH_REMOTE_OPEN 0x2
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060036#define BAM_CH_IN_RESET 0x4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
38#define BAM_MUX_HDR_MAGIC_NO 0x33fc
39
40#define BAM_MUX_HDR_CMD_DATA 0
41#define BAM_MUX_HDR_CMD_OPEN 1
42#define BAM_MUX_HDR_CMD_CLOSE 2
43
Jeff Hugo949080a2011-08-30 11:58:56 -060044#define POLLING_MIN_SLEEP 950 /* 0.95 ms */
45#define POLLING_MAX_SLEEP 1050 /* 1.05 ms */
46#define POLLING_INACTIVITY 40 /* cycles before switch to intr mode */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -070048#define LOW_WATERMARK 2
49#define HIGH_WATERMARK 4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050
51static int msm_bam_dmux_debug_enable;
52module_param_named(debug_enable, msm_bam_dmux_debug_enable,
53 int, S_IRUGO | S_IWUSR | S_IWGRP);
54
55#if defined(DEBUG)
56static uint32_t bam_dmux_read_cnt;
57static uint32_t bam_dmux_write_cnt;
58static uint32_t bam_dmux_write_cpy_cnt;
59static uint32_t bam_dmux_write_cpy_bytes;
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070060static uint32_t bam_dmux_tx_sps_failure_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061
62#define DBG(x...) do { \
63 if (msm_bam_dmux_debug_enable) \
64 pr_debug(x); \
65 } while (0)
66
67#define DBG_INC_READ_CNT(x) do { \
68 bam_dmux_read_cnt += (x); \
69 if (msm_bam_dmux_debug_enable) \
70 pr_debug("%s: total read bytes %u\n", \
71 __func__, bam_dmux_read_cnt); \
72 } while (0)
73
74#define DBG_INC_WRITE_CNT(x) do { \
75 bam_dmux_write_cnt += (x); \
76 if (msm_bam_dmux_debug_enable) \
77 pr_debug("%s: total written bytes %u\n", \
78 __func__, bam_dmux_write_cnt); \
79 } while (0)
80
81#define DBG_INC_WRITE_CPY(x) do { \
82 bam_dmux_write_cpy_bytes += (x); \
83 bam_dmux_write_cpy_cnt++; \
84 if (msm_bam_dmux_debug_enable) \
85 pr_debug("%s: total write copy cnt %u, bytes %u\n", \
86 __func__, bam_dmux_write_cpy_cnt, \
87 bam_dmux_write_cpy_bytes); \
88 } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070089
90#define DBG_INC_TX_SPS_FAILURE_CNT() do { \
91 bam_dmux_tx_sps_failure_cnt++; \
92} while (0)
93
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094#else
95#define DBG(x...) do { } while (0)
96#define DBG_INC_READ_CNT(x...) do { } while (0)
97#define DBG_INC_WRITE_CNT(x...) do { } while (0)
98#define DBG_INC_WRITE_CPY(x...) do { } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070099#define DBG_INC_TX_SPS_FAILURE_CNT() do { } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100#endif
101
102struct bam_ch_info {
103 uint32_t status;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600104 void (*notify)(void *, int, unsigned long);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105 void *priv;
106 spinlock_t lock;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600107 struct platform_device *pdev;
108 char name[BAM_DMUX_CH_NAME_MAX_LEN];
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700109 int num_tx_pkts;
110 int use_wm;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111};
112
113struct tx_pkt_info {
114 struct sk_buff *skb;
115 dma_addr_t dma_address;
116 char is_cmd;
117 uint32_t len;
118 struct work_struct work;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600119 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120};
121
122struct rx_pkt_info {
123 struct sk_buff *skb;
124 dma_addr_t dma_address;
125 struct work_struct work;
Jeff Hugo949080a2011-08-30 11:58:56 -0600126 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127};
128
129#define A2_NUM_PIPES 6
130#define A2_SUMMING_THRESHOLD 4096
131#define A2_DEFAULT_DESCRIPTORS 32
132#define A2_PHYS_BASE 0x124C2000
133#define A2_PHYS_SIZE 0x2000
134#define BUFFER_SIZE 2048
135#define NUM_BUFFERS 32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136static struct sps_bam_props a2_props;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600137static u32 a2_device_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138static struct sps_pipe *bam_tx_pipe;
139static struct sps_pipe *bam_rx_pipe;
140static struct sps_connect tx_connection;
141static struct sps_connect rx_connection;
142static struct sps_mem_buffer tx_desc_mem_buf;
143static struct sps_mem_buffer rx_desc_mem_buf;
144static struct sps_register_event tx_register_event;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600145static struct sps_register_event rx_register_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146
147static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS];
148static int bam_mux_initialized;
149
Jeff Hugo949080a2011-08-30 11:58:56 -0600150static int polling_mode;
151
152static LIST_HEAD(bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600153static DEFINE_MUTEX(bam_rx_pool_mutexlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600154static LIST_HEAD(bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600155static DEFINE_SPINLOCK(bam_tx_pool_spinlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600156
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157struct bam_mux_hdr {
158 uint16_t magic_num;
159 uint8_t reserved;
160 uint8_t cmd;
161 uint8_t pad_len;
162 uint8_t ch_id;
163 uint16_t pkt_len;
164};
165
Jeff Hugod98b1082011-10-24 10:30:23 -0600166static void notify_all(int event, unsigned long data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167static void bam_mux_write_done(struct work_struct *work);
168static void handle_bam_mux_cmd(struct work_struct *work);
Jeff Hugo949080a2011-08-30 11:58:56 -0600169static void rx_timer_work_func(struct work_struct *work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170
Jeff Hugo949080a2011-08-30 11:58:56 -0600171static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172
173static struct workqueue_struct *bam_mux_rx_workqueue;
174static struct workqueue_struct *bam_mux_tx_workqueue;
175
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600176/* A2 power collaspe */
177#define UL_TIMEOUT_DELAY 1000 /* in ms */
178static void toggle_apps_ack(void);
179static void reconnect_to_bam(void);
180static void disconnect_to_bam(void);
181static void ul_wakeup(void);
182static void ul_timeout(struct work_struct *work);
183static void vote_dfab(void);
184static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600185static void kickoff_ul_wakeup_func(struct work_struct *work);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600186
187static int bam_is_connected;
188static DEFINE_MUTEX(wakeup_lock);
189static struct completion ul_wakeup_ack_completion;
190static struct completion bam_connection_completion;
191static struct delayed_work ul_timeout_work;
192static int ul_packet_written;
193static struct clk *dfab_clk;
194static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600195static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600196static int bam_connection_is_active;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700197static int wait_for_ack;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600198/* End A2 power collaspe */
199
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600200/* subsystem restart */
201static int restart_notifier_cb(struct notifier_block *this,
202 unsigned long code,
203 void *data);
204
205static struct notifier_block restart_notifier = {
206 .notifier_call = restart_notifier_cb,
207};
208static int in_global_reset;
209/* end subsystem restart */
210
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211#define bam_ch_is_open(x) \
212 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
213
214#define bam_ch_is_local_open(x) \
215 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
216
217#define bam_ch_is_remote_open(x) \
218 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
219
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600220#define bam_ch_is_in_reset(x) \
221 (bam_ch[(x)].status & BAM_CH_IN_RESET)
222
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223static void queue_rx(void)
224{
225 void *ptr;
226 struct rx_pkt_info *info;
227
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600228 if (in_global_reset)
229 return;
230
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231 info = kmalloc(sizeof(struct rx_pkt_info), GFP_KERNEL);
232 if (!info)
233 return; /*need better way to handle this */
234
235 INIT_WORK(&info->work, handle_bam_mux_cmd);
236
237 info->skb = __dev_alloc_skb(BUFFER_SIZE, GFP_KERNEL);
238 ptr = skb_put(info->skb, BUFFER_SIZE);
Jeff Hugo949080a2011-08-30 11:58:56 -0600239
Jeff Hugoc9749932011-11-02 17:50:40 -0600240 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600241 list_add_tail(&info->list_node, &bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600242 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600243
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 /* need a way to handle error case */
245 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
246 DMA_FROM_DEVICE);
247 sps_transfer_one(bam_rx_pipe, info->dma_address,
Jeff Hugo33dbc002011-08-25 15:52:53 -0600248 BUFFER_SIZE, info,
249 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250}
251
252static void bam_mux_process_data(struct sk_buff *rx_skb)
253{
254 unsigned long flags;
255 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600256 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257
258 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
259
260 rx_skb->data = (unsigned char *)(rx_hdr + 1);
261 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
262 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600263 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600265 event_data = (unsigned long)(rx_skb);
266
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600268 if (bam_ch[rx_hdr->ch_id].notify)
269 bam_ch[rx_hdr->ch_id].notify(
270 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
271 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272 else
273 dev_kfree_skb_any(rx_skb);
274 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
275
276 queue_rx();
277}
278
279static void handle_bam_mux_cmd(struct work_struct *work)
280{
281 unsigned long flags;
282 struct bam_mux_hdr *rx_hdr;
283 struct rx_pkt_info *info;
284 struct sk_buff *rx_skb;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600285 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286
287 info = container_of(work, struct rx_pkt_info, work);
288 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600289 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290 kfree(info);
291
292 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
293
294 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
295 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
296 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
297 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
298 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
299 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
300 " pad %d ch %d len %d\n", __func__,
301 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
302 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
303 dev_kfree_skb_any(rx_skb);
304 queue_rx();
305 return;
306 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700307
308 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
309 pr_err("%s: dropping invalid LCID %d reserved %d cmd %d"
310 " pad %d ch %d len %d\n", __func__,
311 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
312 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
313 dev_kfree_skb_any(rx_skb);
314 queue_rx();
315 return;
316 }
317
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318 switch (rx_hdr->cmd) {
319 case BAM_MUX_HDR_CMD_DATA:
320 DBG_INC_READ_CNT(rx_hdr->pkt_len);
321 bam_mux_process_data(rx_skb);
322 break;
323 case BAM_MUX_HDR_CMD_OPEN:
324 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
325 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700326 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600329 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
330 if (ret)
331 pr_err("%s: platform_device_add() error: %d\n",
332 __func__, ret);
Eric Holmberge779dba2011-11-04 18:22:01 -0600333 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334 break;
335 case BAM_MUX_HDR_CMD_CLOSE:
336 /* probably should drop pending write */
337 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
338 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
339 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600341 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
342 bam_ch[rx_hdr->ch_id].pdev =
343 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
344 if (!bam_ch[rx_hdr->ch_id].pdev)
345 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberge779dba2011-11-04 18:22:01 -0600346 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347 break;
348 default:
349 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
350 " pad %d ch %d len %d\n", __func__,
351 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
352 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
353 dev_kfree_skb_any(rx_skb);
354 queue_rx();
355 return;
356 }
357}
358
359static int bam_mux_write_cmd(void *data, uint32_t len)
360{
361 int rc;
362 struct tx_pkt_info *pkt;
363 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700364 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600366 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367 if (pkt == NULL) {
368 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
369 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370 return rc;
371 }
372
373 dma_address = dma_map_single(NULL, data, len,
374 DMA_TO_DEVICE);
375 if (!dma_address) {
376 pr_err("%s: dma_map_single() failed\n", __func__);
377 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378 return rc;
379 }
380 pkt->skb = (struct sk_buff *)(data);
381 pkt->len = len;
382 pkt->dma_address = dma_address;
383 pkt->is_cmd = 1;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600384 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700385 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600386 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700387 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
389 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600390 if (rc) {
391 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700392 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600393 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700394 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700395 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600396 kfree(pkt);
397 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600399 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400 return rc;
401}
402
403static void bam_mux_write_done(struct work_struct *work)
404{
405 struct sk_buff *skb;
406 struct bam_mux_hdr *hdr;
407 struct tx_pkt_info *info;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600408 unsigned long event_data;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600409 struct list_head *node;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700410 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600412 if (in_global_reset)
413 return;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700414 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600415 node = bam_tx_pool.next;
416 list_del(node);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700417 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700418 info = container_of(work, struct tx_pkt_info, work);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600419 if (info->is_cmd) {
420 kfree(info->skb);
421 kfree(info);
422 return;
423 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424 skb = info->skb;
425 kfree(info);
426 hdr = (struct bam_mux_hdr *)skb->data;
427 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600428 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700429 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
430 bam_ch[hdr->ch_id].num_tx_pkts--;
431 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600432 if (bam_ch[hdr->ch_id].notify)
433 bam_ch[hdr->ch_id].notify(
434 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
435 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436 else
437 dev_kfree_skb_any(skb);
438}
439
440int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
441{
442 int rc = 0;
443 struct bam_mux_hdr *hdr;
444 unsigned long flags;
445 struct sk_buff *new_skb = NULL;
446 dma_addr_t dma_address;
447 struct tx_pkt_info *pkt;
448
449 if (id >= BAM_DMUX_NUM_CHANNELS)
450 return -EINVAL;
451 if (!skb)
452 return -EINVAL;
453 if (!bam_mux_initialized)
454 return -ENODEV;
455
456 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
457 spin_lock_irqsave(&bam_ch[id].lock, flags);
458 if (!bam_ch_is_open(id)) {
459 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
460 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
461 return -ENODEV;
462 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700463
464 if (bam_ch[id].use_wm &&
465 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
466 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
467 pr_err("%s: watermark exceeded: %d\n", __func__, id);
468 return -EAGAIN;
469 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
471
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600472 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600473 if (!bam_is_connected) {
474 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600475 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600476 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600477 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600478 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600479
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 /* if skb do not have any tailroom for padding,
481 copy the skb into a new expanded skb */
482 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
483 /* revisit, probably dev_alloc_skb and memcpy is effecient */
484 new_skb = skb_copy_expand(skb, skb_headroom(skb),
485 4 - (skb->len & 0x3), GFP_ATOMIC);
486 if (new_skb == NULL) {
487 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600488 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489 }
490 dev_kfree_skb_any(skb);
491 skb = new_skb;
492 DBG_INC_WRITE_CPY(skb->len);
493 }
494
495 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
496
497 /* caller should allocate for hdr and padding
498 hdr is fine, padding is tricky */
499 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
500 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
501 hdr->reserved = 0;
502 hdr->ch_id = id;
503 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
504 if (skb->len & 0x3)
505 skb_put(skb, 4 - (skb->len & 0x3));
506
507 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
508
509 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
510 __func__, skb->data, skb->tail, skb->len,
511 hdr->pkt_len, hdr->pad_len);
512
513 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
514 if (pkt == NULL) {
515 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600516 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 }
518
519 dma_address = dma_map_single(NULL, skb->data, skb->len,
520 DMA_TO_DEVICE);
521 if (!dma_address) {
522 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600523 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 }
525 pkt->skb = skb;
526 pkt->dma_address = dma_address;
527 pkt->is_cmd = 0;
528 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700529 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600530 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700531 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
533 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600534 if (rc) {
535 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700536 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600537 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700538 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700539 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600540 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700541 if (new_skb)
542 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700543 } else {
544 spin_lock_irqsave(&bam_ch[id].lock, flags);
545 bam_ch[id].num_tx_pkts++;
546 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600547 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600548 ul_packet_written = 1;
549 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600551
552write_fail3:
553 kfree(pkt);
554write_fail2:
555 if (new_skb)
556 dev_kfree_skb_any(new_skb);
557write_fail:
558 read_unlock(&ul_wakeup_lock);
559 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560}
561
562int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600563 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564{
565 struct bam_mux_hdr *hdr;
566 unsigned long flags;
567 int rc = 0;
568
569 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700570 if (!bam_mux_initialized) {
571 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700573 }
574 if (id >= BAM_DMUX_NUM_CHANNELS) {
575 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700577 }
578 if (notify == NULL) {
579 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600580 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700581 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582
583 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
584 if (hdr == NULL) {
585 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
586 return -ENOMEM;
587 }
588 spin_lock_irqsave(&bam_ch[id].lock, flags);
589 if (bam_ch_is_open(id)) {
590 DBG("%s: Already opened %d\n", __func__, id);
591 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
592 kfree(hdr);
593 goto open_done;
594 }
595 if (!bam_ch_is_remote_open(id)) {
596 DBG("%s: Remote not open; ch: %d\n", __func__, id);
597 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
598 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700599 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 }
601
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600602 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 bam_ch[id].priv = priv;
604 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700605 bam_ch[id].num_tx_pkts = 0;
606 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
608
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600609 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600610 if (!bam_is_connected) {
611 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600612 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600613 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600614 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600615 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600616
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
618 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
619 hdr->reserved = 0;
620 hdr->ch_id = id;
621 hdr->pkt_len = 0;
622 hdr->pad_len = 0;
623
624 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600625 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626
627open_done:
628 DBG("%s: opened ch %d\n", __func__, id);
629 return rc;
630}
631
632int msm_bam_dmux_close(uint32_t id)
633{
634 struct bam_mux_hdr *hdr;
635 unsigned long flags;
636 int rc;
637
638 if (id >= BAM_DMUX_NUM_CHANNELS)
639 return -EINVAL;
640 DBG("%s: closing ch %d\n", __func__, id);
641 if (!bam_mux_initialized)
642 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600644 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600645 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600646 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600647 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600648 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600649 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600650 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600651
Jeff Hugo061ce672011-10-21 17:15:32 -0600652 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600653 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654 bam_ch[id].priv = NULL;
655 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
656 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
657
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600658 if (bam_ch_is_in_reset(id)) {
659 read_unlock(&ul_wakeup_lock);
660 bam_ch[id].status &= ~BAM_CH_IN_RESET;
661 return 0;
662 }
663
Jeff Hugobb5802f2011-11-02 17:10:29 -0600664 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665 if (hdr == NULL) {
666 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600667 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668 return -ENOMEM;
669 }
670 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
671 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
672 hdr->reserved = 0;
673 hdr->ch_id = id;
674 hdr->pkt_len = 0;
675 hdr->pad_len = 0;
676
677 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600678 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679
680 DBG("%s: closed ch %d\n", __func__, id);
681 return rc;
682}
683
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700684int msm_bam_dmux_is_ch_full(uint32_t id)
685{
686 unsigned long flags;
687 int ret;
688
689 if (id >= BAM_DMUX_NUM_CHANNELS)
690 return -EINVAL;
691
692 spin_lock_irqsave(&bam_ch[id].lock, flags);
693 bam_ch[id].use_wm = 1;
694 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
695 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
696 id, bam_ch[id].num_tx_pkts, ret);
697 if (!bam_ch_is_local_open(id)) {
698 ret = -ENODEV;
699 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
700 }
701 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
702
703 return ret;
704}
705
706int msm_bam_dmux_is_ch_low(uint32_t id)
707{
708 int ret;
709
710 if (id >= BAM_DMUX_NUM_CHANNELS)
711 return -EINVAL;
712
713 bam_ch[id].use_wm = 1;
714 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
715 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
716 id, bam_ch[id].num_tx_pkts, ret);
717 if (!bam_ch_is_local_open(id)) {
718 ret = -ENODEV;
719 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
720 }
721
722 return ret;
723}
724
Jeff Hugo949080a2011-08-30 11:58:56 -0600725static void rx_timer_work_func(struct work_struct *work)
726{
727 struct sps_iovec iov;
728 struct list_head *node;
729 struct rx_pkt_info *info;
730 int inactive_cycles = 0;
731 int ret;
732 struct sps_connect cur_rx_conn;
733
734 while (1) { /* timer loop */
735 ++inactive_cycles;
736 while (1) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600737 if (in_global_reset)
738 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600739 sps_get_iovec(bam_rx_pipe, &iov);
740 if (iov.addr == 0)
741 break;
742 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600743 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600744 node = bam_rx_pool.next;
745 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600746 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600747 info = container_of(node, struct rx_pkt_info,
748 list_node);
749 handle_bam_mux_cmd(&info->work);
750 }
751
752 if (inactive_cycles == POLLING_INACTIVITY) {
753 /*
754 * attempt to enable interrupts in this pipe
755 * if enabling interrupts fails, continue polling
756 */
757 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
758 if (ret) {
759 pr_err("%s: sps_get_config() failed, interrupts"
760 " not enabled\n", __func__);
761 queue_work(bam_mux_rx_workqueue,
762 &rx_timer_work);
763 return;
764 } else {
765 rx_register_event.options = SPS_O_EOT;
766 /* should check return value */
767 sps_register_event(bam_rx_pipe,
768 &rx_register_event);
769 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
770 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
771 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
772 if (ret) {
773 pr_err("%s: sps_set_config() failed, "
774 "interrupts not enabled\n",
775 __func__);
776 queue_work(bam_mux_rx_workqueue,
777 &rx_timer_work);
778 return;
779 }
780 polling_mode = 0;
781 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600782 if (in_global_reset)
783 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600784 /* handle race condition - missed packet? */
785 sps_get_iovec(bam_rx_pipe, &iov);
786 if (iov.addr == 0)
787 return;
788 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600789 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600790 node = bam_rx_pool.next;
791 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600792 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600793 info = container_of(node, struct rx_pkt_info,
794 list_node);
795 handle_bam_mux_cmd(&info->work);
796 return;
797 }
798
799 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
800 }
801}
802
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803static void bam_mux_tx_notify(struct sps_event_notify *notify)
804{
805 struct tx_pkt_info *pkt;
806
807 DBG("%s: event %d notified\n", __func__, notify->event_id);
808
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600809 if (in_global_reset)
810 return;
811
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812 switch (notify->event_id) {
813 case SPS_EVENT_EOT:
814 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600815 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 dma_unmap_single(NULL, pkt->dma_address,
817 pkt->skb->len,
818 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600819 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 dma_unmap_single(NULL, pkt->dma_address,
821 pkt->len,
822 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600823 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824 break;
825 default:
826 pr_err("%s: recieved unexpected event id %d\n", __func__,
827 notify->event_id);
828 }
829}
830
Jeff Hugo33dbc002011-08-25 15:52:53 -0600831static void bam_mux_rx_notify(struct sps_event_notify *notify)
832{
Jeff Hugo949080a2011-08-30 11:58:56 -0600833 int ret;
834 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600835
836 DBG("%s: event %d notified\n", __func__, notify->event_id);
837
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600838 if (in_global_reset)
839 return;
840
Jeff Hugo33dbc002011-08-25 15:52:53 -0600841 switch (notify->event_id) {
842 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -0600843 /* attempt to disable interrupts in this pipe */
844 if (!polling_mode) {
845 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
846 if (ret) {
847 pr_err("%s: sps_get_config() failed, interrupts"
848 " not disabled\n", __func__);
849 break;
850 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -0700851 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -0600852 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
853 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
854 if (ret) {
855 pr_err("%s: sps_set_config() failed, interrupts"
856 " not disabled\n", __func__);
857 break;
858 }
859 polling_mode = 1;
860 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
861 }
Jeff Hugo33dbc002011-08-25 15:52:53 -0600862 break;
863 default:
864 pr_err("%s: recieved unexpected event id %d\n", __func__,
865 notify->event_id);
866 }
867}
868
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869#ifdef CONFIG_DEBUG_FS
870
871static int debug_tbl(char *buf, int max)
872{
873 int i = 0;
874 int j;
875
876 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
877 i += scnprintf(buf + i, max - i,
878 "ch%02d local open=%s remote open=%s\n",
879 j, bam_ch_is_local_open(j) ? "Y" : "N",
880 bam_ch_is_remote_open(j) ? "Y" : "N");
881 }
882
883 return i;
884}
885
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700886static int debug_ul_pkt_cnt(char *buf, int max)
887{
888 struct list_head *p;
889 unsigned long flags;
890 int n = 0;
891
892 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
893 __list_for_each(p, &bam_tx_pool) {
894 ++n;
895 }
896 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
897
898 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
899}
900
901static int debug_stats(char *buf, int max)
902{
903 int i = 0;
904
905 i += scnprintf(buf + i, max - i,
906 "skb copy cnt: %u\n"
907 "skb copy bytes: %u\n"
908 "sps tx failures: %u\n",
909 bam_dmux_write_cpy_cnt,
910 bam_dmux_write_cpy_bytes,
911 bam_dmux_tx_sps_failure_cnt
912 );
913
914 return i;
915}
916
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917#define DEBUG_BUFMAX 4096
918static char debug_buffer[DEBUG_BUFMAX];
919
920static ssize_t debug_read(struct file *file, char __user *buf,
921 size_t count, loff_t *ppos)
922{
923 int (*fill)(char *buf, int max) = file->private_data;
924 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
925 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
926}
927
928static int debug_open(struct inode *inode, struct file *file)
929{
930 file->private_data = inode->i_private;
931 return 0;
932}
933
934
935static const struct file_operations debug_ops = {
936 .read = debug_read,
937 .open = debug_open,
938};
939
940static void debug_create(const char *name, mode_t mode,
941 struct dentry *dent,
942 int (*fill)(char *buf, int max))
943{
944 debugfs_create_file(name, mode, dent, fill, &debug_ops);
945}
946
947#endif
948
Jeff Hugod98b1082011-10-24 10:30:23 -0600949static void notify_all(int event, unsigned long data)
950{
951 int i;
952
953 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
954 if (bam_ch_is_open(i))
955 bam_ch[i].notify(bam_ch[i].priv, event, data);
956 }
957}
958
959static void kickoff_ul_wakeup_func(struct work_struct *work)
960{
961 read_lock(&ul_wakeup_lock);
962 if (!bam_is_connected) {
963 read_unlock(&ul_wakeup_lock);
964 ul_wakeup();
965 read_lock(&ul_wakeup_lock);
966 ul_packet_written = 1;
967 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
968 }
969 read_unlock(&ul_wakeup_lock);
970}
971
972void msm_bam_dmux_kickoff_ul_wakeup(void)
973{
974 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
975}
976
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600977static void ul_timeout(struct work_struct *work)
978{
Jeff Hugoc040a5b2011-11-15 14:26:01 -0700979 unsigned long flags;
980 int ret;
981
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600982 if (in_global_reset)
983 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -0700984 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
985 if (!ret) { /* failed to grab lock, reschedule and bail */
986 schedule_delayed_work(&ul_timeout_work,
987 msecs_to_jiffies(UL_TIMEOUT_DELAY));
988 return;
989 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600990 if (ul_packet_written) {
991 ul_packet_written = 0;
992 schedule_delayed_work(&ul_timeout_work,
993 msecs_to_jiffies(UL_TIMEOUT_DELAY));
994 } else {
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700995 wait_for_ack = 1;
996 INIT_COMPLETION(ul_wakeup_ack_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600997 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
998 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -0600999 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001000 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001001 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001002}
1003static void ul_wakeup(void)
1004{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001005 int ret;
1006
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001007 mutex_lock(&wakeup_lock);
1008 if (bam_is_connected) { /* bam got connected before lock grabbed */
1009 mutex_unlock(&wakeup_lock);
1010 return;
1011 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001012 /*
1013 * must wait for the previous power down request to have been acked
1014 * chances are it already came in and this will just fall through
1015 * instead of waiting
1016 */
1017 if (wait_for_ack) {
1018 ret = wait_for_completion_interruptible_timeout(
1019 &ul_wakeup_ack_completion, HZ);
1020 BUG_ON(ret == 0);
1021 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001022 INIT_COMPLETION(ul_wakeup_ack_completion);
1023 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001024 ret = wait_for_completion_interruptible_timeout(
1025 &ul_wakeup_ack_completion, HZ);
1026 BUG_ON(ret == 0);
1027 ret = wait_for_completion_interruptible_timeout(
1028 &bam_connection_completion, HZ);
1029 BUG_ON(ret == 0);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001030
1031 bam_is_connected = 1;
1032 schedule_delayed_work(&ul_timeout_work,
1033 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1034 mutex_unlock(&wakeup_lock);
1035}
1036
1037static void reconnect_to_bam(void)
1038{
1039 int i;
1040
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001041 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001042 vote_dfab();
1043 i = sps_device_reset(a2_device_handle);
1044 if (i)
1045 pr_err("%s: device reset failed rc = %d\n", __func__, i);
1046 i = sps_connect(bam_tx_pipe, &tx_connection);
1047 if (i)
1048 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
1049 i = sps_connect(bam_rx_pipe, &rx_connection);
1050 if (i)
1051 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
1052 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1053 if (i)
1054 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
1055 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1056 if (i)
1057 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
1058 for (i = 0; i < NUM_BUFFERS; ++i)
1059 queue_rx();
1060 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001061 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001062 complete_all(&bam_connection_completion);
1063}
1064
1065static void disconnect_to_bam(void)
1066{
1067 struct list_head *node;
1068 struct rx_pkt_info *info;
1069
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001070 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001071 INIT_COMPLETION(bam_connection_completion);
1072 sps_disconnect(bam_tx_pipe);
1073 sps_disconnect(bam_rx_pipe);
1074 unvote_dfab();
1075 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1076 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1077 while (!list_empty(&bam_rx_pool)) {
1078 node = bam_rx_pool.next;
1079 list_del(node);
1080 info = container_of(node, struct rx_pkt_info, list_node);
1081 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1082 DMA_FROM_DEVICE);
1083 dev_kfree_skb_any(info->skb);
1084 kfree(info);
1085 }
1086}
1087
1088static void vote_dfab(void)
1089{
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001090}
1091
1092static void unvote_dfab(void)
1093{
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001094}
1095
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001096static int restart_notifier_cb(struct notifier_block *this,
1097 unsigned long code,
1098 void *data)
1099{
1100 int i;
1101 struct list_head *node;
1102 struct tx_pkt_info *info;
1103 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001104 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001105
1106 if (code != SUBSYS_AFTER_SHUTDOWN)
1107 return NOTIFY_DONE;
1108
1109 in_global_reset = 1;
1110 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1111 temp_remote_status = bam_ch_is_remote_open(i);
1112 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001113 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001114 if (bam_ch_is_local_open(i))
1115 bam_ch[i].status |= BAM_CH_IN_RESET;
1116 if (temp_remote_status) {
1117 platform_device_unregister(bam_ch[i].pdev);
1118 bam_ch[i].pdev = platform_device_alloc(
1119 bam_ch[i].name, 2);
1120 }
1121 }
1122 /*cleanup UL*/
Jeff Hugo626303bf2011-11-21 11:43:28 -07001123 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001124 while (!list_empty(&bam_tx_pool)) {
1125 node = bam_tx_pool.next;
1126 list_del(node);
1127 info = container_of(node, struct tx_pkt_info,
1128 list_node);
1129 if (!info->is_cmd) {
1130 dma_unmap_single(NULL, info->dma_address,
1131 info->skb->len,
1132 DMA_TO_DEVICE);
1133 dev_kfree_skb_any(info->skb);
1134 } else {
1135 dma_unmap_single(NULL, info->dma_address,
1136 info->len,
1137 DMA_TO_DEVICE);
1138 kfree(info->skb);
1139 }
1140 kfree(info);
1141 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001142 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001143 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1144
1145 return NOTIFY_DONE;
1146}
1147
Jeff Hugoade1f842011-08-03 15:53:59 -06001148static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149{
1150 u32 h;
1151 dma_addr_t dma_addr;
1152 int ret;
1153 void *a2_virt_addr;
1154 int i;
1155
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001156 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157 /* init BAM */
1158 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1159 if (!a2_virt_addr) {
1160 pr_err("%s: ioremap failed\n", __func__);
1161 ret = -ENOMEM;
1162 goto register_bam_failed;
1163 }
1164 a2_props.phys_addr = A2_PHYS_BASE;
1165 a2_props.virt_addr = a2_virt_addr;
1166 a2_props.virt_size = A2_PHYS_SIZE;
1167 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001168 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169 a2_props.num_pipes = A2_NUM_PIPES;
1170 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
1171 /* need to free on tear down */
1172 ret = sps_register_bam_device(&a2_props, &h);
1173 if (ret < 0) {
1174 pr_err("%s: register bam error %d\n", __func__, ret);
1175 goto register_bam_failed;
1176 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001177 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178
1179 bam_tx_pipe = sps_alloc_endpoint();
1180 if (bam_tx_pipe == NULL) {
1181 pr_err("%s: tx alloc endpoint failed\n", __func__);
1182 ret = -ENOMEM;
1183 goto register_bam_failed;
1184 }
1185 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1186 if (ret) {
1187 pr_err("%s: tx get config failed %d\n", __func__, ret);
1188 goto tx_get_config_failed;
1189 }
1190
1191 tx_connection.source = SPS_DEV_HANDLE_MEM;
1192 tx_connection.src_pipe_index = 0;
1193 tx_connection.destination = h;
1194 tx_connection.dest_pipe_index = 4;
1195 tx_connection.mode = SPS_MODE_DEST;
1196 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1197 tx_desc_mem_buf.size = 0x800; /* 2k */
1198 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1199 &dma_addr, 0);
1200 if (tx_desc_mem_buf.base == NULL) {
1201 pr_err("%s: tx memory alloc failed\n", __func__);
1202 ret = -ENOMEM;
1203 goto tx_mem_failed;
1204 }
1205 tx_desc_mem_buf.phys_base = dma_addr;
1206 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1207 tx_connection.desc = tx_desc_mem_buf;
1208 tx_connection.event_thresh = 0x10;
1209
1210 ret = sps_connect(bam_tx_pipe, &tx_connection);
1211 if (ret < 0) {
1212 pr_err("%s: tx connect error %d\n", __func__, ret);
1213 goto tx_connect_failed;
1214 }
1215
1216 bam_rx_pipe = sps_alloc_endpoint();
1217 if (bam_rx_pipe == NULL) {
1218 pr_err("%s: rx alloc endpoint failed\n", __func__);
1219 ret = -ENOMEM;
1220 goto tx_connect_failed;
1221 }
1222 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1223 if (ret) {
1224 pr_err("%s: rx get config failed %d\n", __func__, ret);
1225 goto rx_get_config_failed;
1226 }
1227
1228 rx_connection.source = h;
1229 rx_connection.src_pipe_index = 5;
1230 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1231 rx_connection.dest_pipe_index = 1;
1232 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001233 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1234 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001235 rx_desc_mem_buf.size = 0x800; /* 2k */
1236 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1237 &dma_addr, 0);
1238 if (rx_desc_mem_buf.base == NULL) {
1239 pr_err("%s: rx memory alloc failed\n", __func__);
1240 ret = -ENOMEM;
1241 goto rx_mem_failed;
1242 }
1243 rx_desc_mem_buf.phys_base = dma_addr;
1244 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1245 rx_connection.desc = rx_desc_mem_buf;
1246 rx_connection.event_thresh = 0x10;
1247
1248 ret = sps_connect(bam_rx_pipe, &rx_connection);
1249 if (ret < 0) {
1250 pr_err("%s: rx connect error %d\n", __func__, ret);
1251 goto rx_connect_failed;
1252 }
1253
1254 tx_register_event.options = SPS_O_EOT;
1255 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1256 tx_register_event.xfer_done = NULL;
1257 tx_register_event.callback = bam_mux_tx_notify;
1258 tx_register_event.user = NULL;
1259 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1260 if (ret < 0) {
1261 pr_err("%s: tx register event error %d\n", __func__, ret);
1262 goto rx_event_reg_failed;
1263 }
1264
Jeff Hugo33dbc002011-08-25 15:52:53 -06001265 rx_register_event.options = SPS_O_EOT;
1266 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1267 rx_register_event.xfer_done = NULL;
1268 rx_register_event.callback = bam_mux_rx_notify;
1269 rx_register_event.user = NULL;
1270 ret = sps_register_event(bam_rx_pipe, &rx_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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001276 bam_mux_initialized = 1;
1277 for (i = 0; i < NUM_BUFFERS; ++i)
1278 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001279 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001280 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001281 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001282 return;
1283
1284rx_event_reg_failed:
1285 sps_disconnect(bam_rx_pipe);
1286rx_connect_failed:
1287 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1288 rx_desc_mem_buf.phys_base);
1289rx_mem_failed:
1290 sps_disconnect(bam_tx_pipe);
1291rx_get_config_failed:
1292 sps_free_endpoint(bam_rx_pipe);
1293tx_connect_failed:
1294 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1295 tx_desc_mem_buf.phys_base);
1296tx_get_config_failed:
1297 sps_free_endpoint(bam_tx_pipe);
1298tx_mem_failed:
1299 sps_deregister_bam_device(h);
1300register_bam_failed:
1301 /*destroy_workqueue(bam_mux_workqueue);*/
1302 /*return ret;*/
1303 return;
1304}
Jeff Hugoade1f842011-08-03 15:53:59 -06001305
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001306static void toggle_apps_ack(void)
1307{
1308 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
1309 smsm_change_state(SMSM_APPS_STATE,
1310 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1311 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1312 clear_bit = ~clear_bit;
1313}
1314
Jeff Hugoade1f842011-08-03 15:53:59 -06001315static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1316{
1317 DBG("%s: smsm activity\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001318 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL)
1319 reconnect_to_bam();
1320 else if (bam_mux_initialized && !(new_state & SMSM_A2_POWER_CONTROL))
1321 disconnect_to_bam();
Jeff Hugoade1f842011-08-03 15:53:59 -06001322 else if (new_state & SMSM_A2_POWER_CONTROL)
1323 bam_init();
1324 else
1325 pr_err("%s: unsupported state change\n", __func__);
1326
1327}
1328
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001329static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1330 uint32_t new_state)
1331{
1332 complete_all(&ul_wakeup_ack_completion);
1333}
1334
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001335static int bam_dmux_probe(struct platform_device *pdev)
1336{
1337 int rc;
1338
1339 DBG("%s probe called\n", __func__);
1340 if (bam_mux_initialized)
1341 return 0;
1342
Stephen Boyd1c51a492011-10-26 12:11:47 -07001343 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001344 if (IS_ERR(dfab_clk)) {
1345 pr_err("%s: did not get dfab clock\n", __func__);
1346 return -EFAULT;
1347 }
1348
1349 rc = clk_set_rate(dfab_clk, 64000000);
1350 if (rc)
1351 pr_err("%s: unable to set dfab clock rate\n", __func__);
1352
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001353 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1354 if (!bam_mux_rx_workqueue)
1355 return -ENOMEM;
1356
1357 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1358 if (!bam_mux_tx_workqueue) {
1359 destroy_workqueue(bam_mux_rx_workqueue);
1360 return -ENOMEM;
1361 }
1362
Jeff Hugo7960abd2011-08-02 15:39:38 -06001363 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001364 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001365 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1366 "bam_dmux_ch_%d", rc);
1367 /* bus 2, ie a2 stream 2 */
1368 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1369 if (!bam_ch[rc].pdev) {
1370 pr_err("%s: platform device alloc failed\n", __func__);
1371 destroy_workqueue(bam_mux_rx_workqueue);
1372 destroy_workqueue(bam_mux_tx_workqueue);
1373 return -ENOMEM;
1374 }
1375 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001376
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001377 init_completion(&ul_wakeup_ack_completion);
1378 init_completion(&bam_connection_completion);
1379 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
1380
Jeff Hugoade1f842011-08-03 15:53:59 -06001381 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1382 bam_dmux_smsm_cb, NULL);
1383
1384 if (rc) {
1385 destroy_workqueue(bam_mux_rx_workqueue);
1386 destroy_workqueue(bam_mux_tx_workqueue);
1387 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1388 return -ENOMEM;
1389 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001390
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001391 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1392 bam_dmux_smsm_ack_cb, NULL);
1393
1394 if (rc) {
1395 destroy_workqueue(bam_mux_rx_workqueue);
1396 destroy_workqueue(bam_mux_tx_workqueue);
1397 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1398 SMSM_A2_POWER_CONTROL,
1399 bam_dmux_smsm_cb, NULL);
1400 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1401 rc);
1402 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1403 platform_device_put(bam_ch[rc].pdev);
1404 return -ENOMEM;
1405 }
1406
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07001407 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
1408 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
1409
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001410 return 0;
1411}
1412
1413static struct platform_driver bam_dmux_driver = {
1414 .probe = bam_dmux_probe,
1415 .driver = {
1416 .name = "BAM_RMNT",
1417 .owner = THIS_MODULE,
1418 },
1419};
1420
1421static int __init bam_dmux_init(void)
1422{
1423#ifdef CONFIG_DEBUG_FS
1424 struct dentry *dent;
1425
1426 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001427 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001428 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001429 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
1430 debug_create("stats", 0444, dent, debug_stats);
1431 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001432#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001433 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001434 return platform_driver_register(&bam_dmux_driver);
1435}
1436
Jeff Hugoade1f842011-08-03 15:53:59 -06001437late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001438MODULE_DESCRIPTION("MSM BAM DMUX");
1439MODULE_LICENSE("GPL v2");