blob: 644a6662f4d12185410857ffc3568bd401cfcc30 [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);
Jeff Hugoe05bc222011-12-07 13:57:23 -0700235 if (!info) {
236 pr_err("%s: unable to alloc rx_pkt_info\n", __func__);
237 return;
238 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239
240 INIT_WORK(&info->work, handle_bam_mux_cmd);
241
242 info->skb = __dev_alloc_skb(BUFFER_SIZE, GFP_KERNEL);
Jeff Hugo4ba22f92011-12-07 12:42:47 -0700243 if (info->skb == NULL) {
244 pr_err("%s: unable to alloc skb\n", __func__);
245 kfree(info);
246 return;
247 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 ptr = skb_put(info->skb, BUFFER_SIZE);
Jeff Hugo949080a2011-08-30 11:58:56 -0600249
Jeff Hugoc9749932011-11-02 17:50:40 -0600250 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600251 list_add_tail(&info->list_node, &bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600252 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600253
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 /* need a way to handle error case */
255 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
256 DMA_FROM_DEVICE);
257 sps_transfer_one(bam_rx_pipe, info->dma_address,
Jeff Hugo33dbc002011-08-25 15:52:53 -0600258 BUFFER_SIZE, info,
259 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260}
261
262static void bam_mux_process_data(struct sk_buff *rx_skb)
263{
264 unsigned long flags;
265 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600266 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267
268 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
269
270 rx_skb->data = (unsigned char *)(rx_hdr + 1);
271 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
272 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600273 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600275 event_data = (unsigned long)(rx_skb);
276
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600278 if (bam_ch[rx_hdr->ch_id].notify)
279 bam_ch[rx_hdr->ch_id].notify(
280 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
281 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282 else
283 dev_kfree_skb_any(rx_skb);
284 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
285
286 queue_rx();
287}
288
289static void handle_bam_mux_cmd(struct work_struct *work)
290{
291 unsigned long flags;
292 struct bam_mux_hdr *rx_hdr;
293 struct rx_pkt_info *info;
294 struct sk_buff *rx_skb;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600295 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296
297 info = container_of(work, struct rx_pkt_info, work);
298 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600299 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 kfree(info);
301
302 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
303
304 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
305 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
306 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
307 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
308 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
309 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
310 " pad %d ch %d len %d\n", __func__,
311 rx_hdr->magic_num, 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 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700317
318 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
319 pr_err("%s: dropping invalid LCID %d reserved %d cmd %d"
320 " pad %d ch %d len %d\n", __func__,
321 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
322 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
323 dev_kfree_skb_any(rx_skb);
324 queue_rx();
325 return;
326 }
327
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 switch (rx_hdr->cmd) {
329 case BAM_MUX_HDR_CMD_DATA:
330 DBG_INC_READ_CNT(rx_hdr->pkt_len);
331 bam_mux_process_data(rx_skb);
332 break;
333 case BAM_MUX_HDR_CMD_OPEN:
334 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
335 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700336 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600339 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
340 if (ret)
341 pr_err("%s: platform_device_add() error: %d\n",
342 __func__, ret);
Eric Holmberge779dba2011-11-04 18:22:01 -0600343 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344 break;
345 case BAM_MUX_HDR_CMD_CLOSE:
346 /* probably should drop pending write */
347 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
348 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
349 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600351 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
352 bam_ch[rx_hdr->ch_id].pdev =
353 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
354 if (!bam_ch[rx_hdr->ch_id].pdev)
355 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberge779dba2011-11-04 18:22:01 -0600356 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357 break;
358 default:
359 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
360 " pad %d ch %d len %d\n", __func__,
361 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
362 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
363 dev_kfree_skb_any(rx_skb);
364 queue_rx();
365 return;
366 }
367}
368
369static int bam_mux_write_cmd(void *data, uint32_t len)
370{
371 int rc;
372 struct tx_pkt_info *pkt;
373 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700374 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600376 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377 if (pkt == NULL) {
378 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
379 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700380 return rc;
381 }
382
383 dma_address = dma_map_single(NULL, data, len,
384 DMA_TO_DEVICE);
385 if (!dma_address) {
386 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugo96cb7482011-12-07 13:28:31 -0700387 kfree(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389 return rc;
390 }
391 pkt->skb = (struct sk_buff *)(data);
392 pkt->len = len;
393 pkt->dma_address = dma_address;
394 pkt->is_cmd = 1;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600395 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700396 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600397 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700398 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
400 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600401 if (rc) {
402 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700403 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600404 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700405 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700406 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600407 kfree(pkt);
408 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600410 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411 return rc;
412}
413
414static void bam_mux_write_done(struct work_struct *work)
415{
416 struct sk_buff *skb;
417 struct bam_mux_hdr *hdr;
418 struct tx_pkt_info *info;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600419 unsigned long event_data;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600420 struct list_head *node;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700421 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600423 if (in_global_reset)
424 return;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700425 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600426 node = bam_tx_pool.next;
427 list_del(node);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700428 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429 info = container_of(work, struct tx_pkt_info, work);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600430 if (info->is_cmd) {
431 kfree(info->skb);
432 kfree(info);
433 return;
434 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 skb = info->skb;
436 kfree(info);
437 hdr = (struct bam_mux_hdr *)skb->data;
438 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600439 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700440 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
441 bam_ch[hdr->ch_id].num_tx_pkts--;
442 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600443 if (bam_ch[hdr->ch_id].notify)
444 bam_ch[hdr->ch_id].notify(
445 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
446 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447 else
448 dev_kfree_skb_any(skb);
449}
450
451int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
452{
453 int rc = 0;
454 struct bam_mux_hdr *hdr;
455 unsigned long flags;
456 struct sk_buff *new_skb = NULL;
457 dma_addr_t dma_address;
458 struct tx_pkt_info *pkt;
459
460 if (id >= BAM_DMUX_NUM_CHANNELS)
461 return -EINVAL;
462 if (!skb)
463 return -EINVAL;
464 if (!bam_mux_initialized)
465 return -ENODEV;
466
467 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
468 spin_lock_irqsave(&bam_ch[id].lock, flags);
469 if (!bam_ch_is_open(id)) {
470 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
471 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
472 return -ENODEV;
473 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700474
475 if (bam_ch[id].use_wm &&
476 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
477 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
478 pr_err("%s: watermark exceeded: %d\n", __func__, id);
479 return -EAGAIN;
480 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
482
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600483 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600484 if (!bam_is_connected) {
485 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600486 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600487 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600488 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600489 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600490
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491 /* if skb do not have any tailroom for padding,
492 copy the skb into a new expanded skb */
493 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
494 /* revisit, probably dev_alloc_skb and memcpy is effecient */
495 new_skb = skb_copy_expand(skb, skb_headroom(skb),
496 4 - (skb->len & 0x3), GFP_ATOMIC);
497 if (new_skb == NULL) {
498 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600499 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 }
501 dev_kfree_skb_any(skb);
502 skb = new_skb;
503 DBG_INC_WRITE_CPY(skb->len);
504 }
505
506 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
507
508 /* caller should allocate for hdr and padding
509 hdr is fine, padding is tricky */
510 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
511 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
512 hdr->reserved = 0;
513 hdr->ch_id = id;
514 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
515 if (skb->len & 0x3)
516 skb_put(skb, 4 - (skb->len & 0x3));
517
518 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
519
520 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
521 __func__, skb->data, skb->tail, skb->len,
522 hdr->pkt_len, hdr->pad_len);
523
524 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
525 if (pkt == NULL) {
526 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600527 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528 }
529
530 dma_address = dma_map_single(NULL, skb->data, skb->len,
531 DMA_TO_DEVICE);
532 if (!dma_address) {
533 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600534 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 }
536 pkt->skb = skb;
537 pkt->dma_address = dma_address;
538 pkt->is_cmd = 0;
539 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700540 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600541 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700542 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
544 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600545 if (rc) {
546 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700547 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600548 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700549 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700550 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600551 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700552 if (new_skb)
553 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700554 } else {
555 spin_lock_irqsave(&bam_ch[id].lock, flags);
556 bam_ch[id].num_tx_pkts++;
557 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600558 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600559 ul_packet_written = 1;
560 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600562
563write_fail3:
564 kfree(pkt);
565write_fail2:
566 if (new_skb)
567 dev_kfree_skb_any(new_skb);
568write_fail:
569 read_unlock(&ul_wakeup_lock);
570 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571}
572
573int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600574 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575{
576 struct bam_mux_hdr *hdr;
577 unsigned long flags;
578 int rc = 0;
579
580 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700581 if (!bam_mux_initialized) {
582 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700584 }
585 if (id >= BAM_DMUX_NUM_CHANNELS) {
586 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700588 }
589 if (notify == NULL) {
590 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600591 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700592 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593
594 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
595 if (hdr == NULL) {
596 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
597 return -ENOMEM;
598 }
599 spin_lock_irqsave(&bam_ch[id].lock, flags);
600 if (bam_ch_is_open(id)) {
601 DBG("%s: Already opened %d\n", __func__, id);
602 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
603 kfree(hdr);
604 goto open_done;
605 }
606 if (!bam_ch_is_remote_open(id)) {
607 DBG("%s: Remote not open; ch: %d\n", __func__, id);
608 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
609 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700610 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611 }
612
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600613 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614 bam_ch[id].priv = priv;
615 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700616 bam_ch[id].num_tx_pkts = 0;
617 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
619
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600620 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600621 if (!bam_is_connected) {
622 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600623 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600624 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600625 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600626 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600627
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
629 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
630 hdr->reserved = 0;
631 hdr->ch_id = id;
632 hdr->pkt_len = 0;
633 hdr->pad_len = 0;
634
635 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600636 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637
638open_done:
639 DBG("%s: opened ch %d\n", __func__, id);
640 return rc;
641}
642
643int msm_bam_dmux_close(uint32_t id)
644{
645 struct bam_mux_hdr *hdr;
646 unsigned long flags;
647 int rc;
648
649 if (id >= BAM_DMUX_NUM_CHANNELS)
650 return -EINVAL;
651 DBG("%s: closing ch %d\n", __func__, id);
652 if (!bam_mux_initialized)
653 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600655 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600656 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600657 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600658 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600659 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600660 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600661 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600662
Jeff Hugo061ce672011-10-21 17:15:32 -0600663 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600664 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665 bam_ch[id].priv = NULL;
666 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
667 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
668
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600669 if (bam_ch_is_in_reset(id)) {
670 read_unlock(&ul_wakeup_lock);
671 bam_ch[id].status &= ~BAM_CH_IN_RESET;
672 return 0;
673 }
674
Jeff Hugobb5802f2011-11-02 17:10:29 -0600675 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676 if (hdr == NULL) {
677 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600678 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679 return -ENOMEM;
680 }
681 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
682 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
683 hdr->reserved = 0;
684 hdr->ch_id = id;
685 hdr->pkt_len = 0;
686 hdr->pad_len = 0;
687
688 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600689 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690
691 DBG("%s: closed ch %d\n", __func__, id);
692 return rc;
693}
694
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700695int msm_bam_dmux_is_ch_full(uint32_t id)
696{
697 unsigned long flags;
698 int ret;
699
700 if (id >= BAM_DMUX_NUM_CHANNELS)
701 return -EINVAL;
702
703 spin_lock_irqsave(&bam_ch[id].lock, flags);
704 bam_ch[id].use_wm = 1;
705 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
706 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
707 id, bam_ch[id].num_tx_pkts, ret);
708 if (!bam_ch_is_local_open(id)) {
709 ret = -ENODEV;
710 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
711 }
712 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
713
714 return ret;
715}
716
717int msm_bam_dmux_is_ch_low(uint32_t id)
718{
719 int ret;
720
721 if (id >= BAM_DMUX_NUM_CHANNELS)
722 return -EINVAL;
723
724 bam_ch[id].use_wm = 1;
725 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
726 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
727 id, bam_ch[id].num_tx_pkts, ret);
728 if (!bam_ch_is_local_open(id)) {
729 ret = -ENODEV;
730 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
731 }
732
733 return ret;
734}
735
Jeff Hugo949080a2011-08-30 11:58:56 -0600736static void rx_timer_work_func(struct work_struct *work)
737{
738 struct sps_iovec iov;
739 struct list_head *node;
740 struct rx_pkt_info *info;
741 int inactive_cycles = 0;
742 int ret;
743 struct sps_connect cur_rx_conn;
744
745 while (1) { /* timer loop */
746 ++inactive_cycles;
747 while (1) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600748 if (in_global_reset)
749 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600750 sps_get_iovec(bam_rx_pipe, &iov);
751 if (iov.addr == 0)
752 break;
753 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600754 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600755 node = bam_rx_pool.next;
756 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600757 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600758 info = container_of(node, struct rx_pkt_info,
759 list_node);
760 handle_bam_mux_cmd(&info->work);
761 }
762
763 if (inactive_cycles == POLLING_INACTIVITY) {
764 /*
765 * attempt to enable interrupts in this pipe
766 * if enabling interrupts fails, continue polling
767 */
768 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
769 if (ret) {
770 pr_err("%s: sps_get_config() failed, interrupts"
771 " not enabled\n", __func__);
772 queue_work(bam_mux_rx_workqueue,
773 &rx_timer_work);
774 return;
775 } else {
776 rx_register_event.options = SPS_O_EOT;
777 /* should check return value */
778 sps_register_event(bam_rx_pipe,
779 &rx_register_event);
780 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
781 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
782 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
783 if (ret) {
784 pr_err("%s: sps_set_config() failed, "
785 "interrupts not enabled\n",
786 __func__);
787 queue_work(bam_mux_rx_workqueue,
788 &rx_timer_work);
789 return;
790 }
791 polling_mode = 0;
792 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600793 if (in_global_reset)
794 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600795 /* handle race condition - missed packet? */
796 sps_get_iovec(bam_rx_pipe, &iov);
797 if (iov.addr == 0)
798 return;
799 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600800 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600801 node = bam_rx_pool.next;
802 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600803 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600804 info = container_of(node, struct rx_pkt_info,
805 list_node);
806 handle_bam_mux_cmd(&info->work);
807 return;
808 }
809
810 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
811 }
812}
813
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814static void bam_mux_tx_notify(struct sps_event_notify *notify)
815{
816 struct tx_pkt_info *pkt;
817
818 DBG("%s: event %d notified\n", __func__, notify->event_id);
819
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600820 if (in_global_reset)
821 return;
822
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 switch (notify->event_id) {
824 case SPS_EVENT_EOT:
825 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600826 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827 dma_unmap_single(NULL, pkt->dma_address,
828 pkt->skb->len,
829 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600830 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 dma_unmap_single(NULL, pkt->dma_address,
832 pkt->len,
833 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600834 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835 break;
836 default:
837 pr_err("%s: recieved unexpected event id %d\n", __func__,
838 notify->event_id);
839 }
840}
841
Jeff Hugo33dbc002011-08-25 15:52:53 -0600842static void bam_mux_rx_notify(struct sps_event_notify *notify)
843{
Jeff Hugo949080a2011-08-30 11:58:56 -0600844 int ret;
845 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600846
847 DBG("%s: event %d notified\n", __func__, notify->event_id);
848
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600849 if (in_global_reset)
850 return;
851
Jeff Hugo33dbc002011-08-25 15:52:53 -0600852 switch (notify->event_id) {
853 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -0600854 /* attempt to disable interrupts in this pipe */
855 if (!polling_mode) {
856 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
857 if (ret) {
858 pr_err("%s: sps_get_config() failed, interrupts"
859 " not disabled\n", __func__);
860 break;
861 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -0700862 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -0600863 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
864 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
865 if (ret) {
866 pr_err("%s: sps_set_config() failed, interrupts"
867 " not disabled\n", __func__);
868 break;
869 }
870 polling_mode = 1;
871 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
872 }
Jeff Hugo33dbc002011-08-25 15:52:53 -0600873 break;
874 default:
875 pr_err("%s: recieved unexpected event id %d\n", __func__,
876 notify->event_id);
877 }
878}
879
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880#ifdef CONFIG_DEBUG_FS
881
882static int debug_tbl(char *buf, int max)
883{
884 int i = 0;
885 int j;
886
887 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
888 i += scnprintf(buf + i, max - i,
889 "ch%02d local open=%s remote open=%s\n",
890 j, bam_ch_is_local_open(j) ? "Y" : "N",
891 bam_ch_is_remote_open(j) ? "Y" : "N");
892 }
893
894 return i;
895}
896
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700897static int debug_ul_pkt_cnt(char *buf, int max)
898{
899 struct list_head *p;
900 unsigned long flags;
901 int n = 0;
902
903 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
904 __list_for_each(p, &bam_tx_pool) {
905 ++n;
906 }
907 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
908
909 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
910}
911
912static int debug_stats(char *buf, int max)
913{
914 int i = 0;
915
916 i += scnprintf(buf + i, max - i,
917 "skb copy cnt: %u\n"
918 "skb copy bytes: %u\n"
919 "sps tx failures: %u\n",
920 bam_dmux_write_cpy_cnt,
921 bam_dmux_write_cpy_bytes,
922 bam_dmux_tx_sps_failure_cnt
923 );
924
925 return i;
926}
927
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928#define DEBUG_BUFMAX 4096
929static char debug_buffer[DEBUG_BUFMAX];
930
931static ssize_t debug_read(struct file *file, char __user *buf,
932 size_t count, loff_t *ppos)
933{
934 int (*fill)(char *buf, int max) = file->private_data;
935 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
936 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
937}
938
939static int debug_open(struct inode *inode, struct file *file)
940{
941 file->private_data = inode->i_private;
942 return 0;
943}
944
945
946static const struct file_operations debug_ops = {
947 .read = debug_read,
948 .open = debug_open,
949};
950
951static void debug_create(const char *name, mode_t mode,
952 struct dentry *dent,
953 int (*fill)(char *buf, int max))
954{
955 debugfs_create_file(name, mode, dent, fill, &debug_ops);
956}
957
958#endif
959
Jeff Hugod98b1082011-10-24 10:30:23 -0600960static void notify_all(int event, unsigned long data)
961{
962 int i;
963
964 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
965 if (bam_ch_is_open(i))
966 bam_ch[i].notify(bam_ch[i].priv, event, data);
967 }
968}
969
970static void kickoff_ul_wakeup_func(struct work_struct *work)
971{
972 read_lock(&ul_wakeup_lock);
973 if (!bam_is_connected) {
974 read_unlock(&ul_wakeup_lock);
975 ul_wakeup();
976 read_lock(&ul_wakeup_lock);
977 ul_packet_written = 1;
978 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
979 }
980 read_unlock(&ul_wakeup_lock);
981}
982
983void msm_bam_dmux_kickoff_ul_wakeup(void)
984{
985 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
986}
987
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600988static void ul_timeout(struct work_struct *work)
989{
Jeff Hugoc040a5b2011-11-15 14:26:01 -0700990 unsigned long flags;
991 int ret;
992
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600993 if (in_global_reset)
994 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -0700995 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
996 if (!ret) { /* failed to grab lock, reschedule and bail */
997 schedule_delayed_work(&ul_timeout_work,
998 msecs_to_jiffies(UL_TIMEOUT_DELAY));
999 return;
1000 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001001 if (ul_packet_written) {
1002 ul_packet_written = 0;
1003 schedule_delayed_work(&ul_timeout_work,
1004 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1005 } else {
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001006 wait_for_ack = 1;
1007 INIT_COMPLETION(ul_wakeup_ack_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001008 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1009 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -06001010 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001011 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001012 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001013}
1014static void ul_wakeup(void)
1015{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001016 int ret;
1017
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001018 mutex_lock(&wakeup_lock);
1019 if (bam_is_connected) { /* bam got connected before lock grabbed */
1020 mutex_unlock(&wakeup_lock);
1021 return;
1022 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001023 /*
1024 * must wait for the previous power down request to have been acked
1025 * chances are it already came in and this will just fall through
1026 * instead of waiting
1027 */
1028 if (wait_for_ack) {
1029 ret = wait_for_completion_interruptible_timeout(
1030 &ul_wakeup_ack_completion, HZ);
1031 BUG_ON(ret == 0);
1032 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001033 INIT_COMPLETION(ul_wakeup_ack_completion);
1034 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001035 ret = wait_for_completion_interruptible_timeout(
1036 &ul_wakeup_ack_completion, HZ);
1037 BUG_ON(ret == 0);
1038 ret = wait_for_completion_interruptible_timeout(
1039 &bam_connection_completion, HZ);
1040 BUG_ON(ret == 0);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001041
1042 bam_is_connected = 1;
1043 schedule_delayed_work(&ul_timeout_work,
1044 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1045 mutex_unlock(&wakeup_lock);
1046}
1047
1048static void reconnect_to_bam(void)
1049{
1050 int i;
1051
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001052 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001053 vote_dfab();
1054 i = sps_device_reset(a2_device_handle);
1055 if (i)
1056 pr_err("%s: device reset failed rc = %d\n", __func__, i);
1057 i = sps_connect(bam_tx_pipe, &tx_connection);
1058 if (i)
1059 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
1060 i = sps_connect(bam_rx_pipe, &rx_connection);
1061 if (i)
1062 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
1063 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1064 if (i)
1065 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
1066 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1067 if (i)
1068 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
1069 for (i = 0; i < NUM_BUFFERS; ++i)
1070 queue_rx();
1071 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001072 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001073 complete_all(&bam_connection_completion);
1074}
1075
1076static void disconnect_to_bam(void)
1077{
1078 struct list_head *node;
1079 struct rx_pkt_info *info;
1080
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001081 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001082 INIT_COMPLETION(bam_connection_completion);
1083 sps_disconnect(bam_tx_pipe);
1084 sps_disconnect(bam_rx_pipe);
1085 unvote_dfab();
1086 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1087 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1088 while (!list_empty(&bam_rx_pool)) {
1089 node = bam_rx_pool.next;
1090 list_del(node);
1091 info = container_of(node, struct rx_pkt_info, list_node);
1092 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1093 DMA_FROM_DEVICE);
1094 dev_kfree_skb_any(info->skb);
1095 kfree(info);
1096 }
1097}
1098
1099static void vote_dfab(void)
1100{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001101 int rc;
1102
1103 rc = clk_enable(dfab_clk);
1104 if (rc)
1105 pr_err("bam_dmux vote for dfab failed rc = %d\n", rc);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001106}
1107
1108static void unvote_dfab(void)
1109{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001110 clk_disable(dfab_clk);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001111}
1112
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001113static int restart_notifier_cb(struct notifier_block *this,
1114 unsigned long code,
1115 void *data)
1116{
1117 int i;
1118 struct list_head *node;
1119 struct tx_pkt_info *info;
1120 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001121 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001122
1123 if (code != SUBSYS_AFTER_SHUTDOWN)
1124 return NOTIFY_DONE;
1125
1126 in_global_reset = 1;
1127 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1128 temp_remote_status = bam_ch_is_remote_open(i);
1129 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001130 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001131 if (bam_ch_is_local_open(i))
1132 bam_ch[i].status |= BAM_CH_IN_RESET;
1133 if (temp_remote_status) {
1134 platform_device_unregister(bam_ch[i].pdev);
1135 bam_ch[i].pdev = platform_device_alloc(
1136 bam_ch[i].name, 2);
1137 }
1138 }
1139 /*cleanup UL*/
Jeff Hugo626303bf2011-11-21 11:43:28 -07001140 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001141 while (!list_empty(&bam_tx_pool)) {
1142 node = bam_tx_pool.next;
1143 list_del(node);
1144 info = container_of(node, struct tx_pkt_info,
1145 list_node);
1146 if (!info->is_cmd) {
1147 dma_unmap_single(NULL, info->dma_address,
1148 info->skb->len,
1149 DMA_TO_DEVICE);
1150 dev_kfree_skb_any(info->skb);
1151 } else {
1152 dma_unmap_single(NULL, info->dma_address,
1153 info->len,
1154 DMA_TO_DEVICE);
1155 kfree(info->skb);
1156 }
1157 kfree(info);
1158 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001159 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001160 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1161
1162 return NOTIFY_DONE;
1163}
1164
Jeff Hugoade1f842011-08-03 15:53:59 -06001165static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001166{
1167 u32 h;
1168 dma_addr_t dma_addr;
1169 int ret;
1170 void *a2_virt_addr;
1171 int i;
1172
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001173 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174 /* init BAM */
1175 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1176 if (!a2_virt_addr) {
1177 pr_err("%s: ioremap failed\n", __func__);
1178 ret = -ENOMEM;
1179 goto register_bam_failed;
1180 }
1181 a2_props.phys_addr = A2_PHYS_BASE;
1182 a2_props.virt_addr = a2_virt_addr;
1183 a2_props.virt_size = A2_PHYS_SIZE;
1184 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001185 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 a2_props.num_pipes = A2_NUM_PIPES;
1187 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001188 if (cpu_is_msm9615())
1189 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001190 /* need to free on tear down */
1191 ret = sps_register_bam_device(&a2_props, &h);
1192 if (ret < 0) {
1193 pr_err("%s: register bam error %d\n", __func__, ret);
1194 goto register_bam_failed;
1195 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001196 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001197
1198 bam_tx_pipe = sps_alloc_endpoint();
1199 if (bam_tx_pipe == NULL) {
1200 pr_err("%s: tx alloc endpoint failed\n", __func__);
1201 ret = -ENOMEM;
1202 goto register_bam_failed;
1203 }
1204 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1205 if (ret) {
1206 pr_err("%s: tx get config failed %d\n", __func__, ret);
1207 goto tx_get_config_failed;
1208 }
1209
1210 tx_connection.source = SPS_DEV_HANDLE_MEM;
1211 tx_connection.src_pipe_index = 0;
1212 tx_connection.destination = h;
1213 tx_connection.dest_pipe_index = 4;
1214 tx_connection.mode = SPS_MODE_DEST;
1215 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1216 tx_desc_mem_buf.size = 0x800; /* 2k */
1217 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1218 &dma_addr, 0);
1219 if (tx_desc_mem_buf.base == NULL) {
1220 pr_err("%s: tx memory alloc failed\n", __func__);
1221 ret = -ENOMEM;
1222 goto tx_mem_failed;
1223 }
1224 tx_desc_mem_buf.phys_base = dma_addr;
1225 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1226 tx_connection.desc = tx_desc_mem_buf;
1227 tx_connection.event_thresh = 0x10;
1228
1229 ret = sps_connect(bam_tx_pipe, &tx_connection);
1230 if (ret < 0) {
1231 pr_err("%s: tx connect error %d\n", __func__, ret);
1232 goto tx_connect_failed;
1233 }
1234
1235 bam_rx_pipe = sps_alloc_endpoint();
1236 if (bam_rx_pipe == NULL) {
1237 pr_err("%s: rx alloc endpoint failed\n", __func__);
1238 ret = -ENOMEM;
1239 goto tx_connect_failed;
1240 }
1241 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1242 if (ret) {
1243 pr_err("%s: rx get config failed %d\n", __func__, ret);
1244 goto rx_get_config_failed;
1245 }
1246
1247 rx_connection.source = h;
1248 rx_connection.src_pipe_index = 5;
1249 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1250 rx_connection.dest_pipe_index = 1;
1251 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001252 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1253 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001254 rx_desc_mem_buf.size = 0x800; /* 2k */
1255 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1256 &dma_addr, 0);
1257 if (rx_desc_mem_buf.base == NULL) {
1258 pr_err("%s: rx memory alloc failed\n", __func__);
1259 ret = -ENOMEM;
1260 goto rx_mem_failed;
1261 }
1262 rx_desc_mem_buf.phys_base = dma_addr;
1263 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1264 rx_connection.desc = rx_desc_mem_buf;
1265 rx_connection.event_thresh = 0x10;
1266
1267 ret = sps_connect(bam_rx_pipe, &rx_connection);
1268 if (ret < 0) {
1269 pr_err("%s: rx connect error %d\n", __func__, ret);
1270 goto rx_connect_failed;
1271 }
1272
1273 tx_register_event.options = SPS_O_EOT;
1274 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1275 tx_register_event.xfer_done = NULL;
1276 tx_register_event.callback = bam_mux_tx_notify;
1277 tx_register_event.user = NULL;
1278 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1279 if (ret < 0) {
1280 pr_err("%s: tx register event error %d\n", __func__, ret);
1281 goto rx_event_reg_failed;
1282 }
1283
Jeff Hugo33dbc002011-08-25 15:52:53 -06001284 rx_register_event.options = SPS_O_EOT;
1285 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1286 rx_register_event.xfer_done = NULL;
1287 rx_register_event.callback = bam_mux_rx_notify;
1288 rx_register_event.user = NULL;
1289 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1290 if (ret < 0) {
1291 pr_err("%s: tx register event error %d\n", __func__, ret);
1292 goto rx_event_reg_failed;
1293 }
1294
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295 bam_mux_initialized = 1;
1296 for (i = 0; i < NUM_BUFFERS; ++i)
1297 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001298 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001299 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001300 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301 return;
1302
1303rx_event_reg_failed:
1304 sps_disconnect(bam_rx_pipe);
1305rx_connect_failed:
1306 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1307 rx_desc_mem_buf.phys_base);
1308rx_mem_failed:
1309 sps_disconnect(bam_tx_pipe);
1310rx_get_config_failed:
1311 sps_free_endpoint(bam_rx_pipe);
1312tx_connect_failed:
1313 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1314 tx_desc_mem_buf.phys_base);
1315tx_get_config_failed:
1316 sps_free_endpoint(bam_tx_pipe);
1317tx_mem_failed:
1318 sps_deregister_bam_device(h);
1319register_bam_failed:
1320 /*destroy_workqueue(bam_mux_workqueue);*/
1321 /*return ret;*/
1322 return;
1323}
Jeff Hugoade1f842011-08-03 15:53:59 -06001324
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001325static void toggle_apps_ack(void)
1326{
1327 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
1328 smsm_change_state(SMSM_APPS_STATE,
1329 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1330 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1331 clear_bit = ~clear_bit;
1332}
1333
Jeff Hugoade1f842011-08-03 15:53:59 -06001334static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1335{
1336 DBG("%s: smsm activity\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001337 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
1338 wake_lock(&bam_wakelock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001339 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001340 } else if (bam_mux_initialized &&
1341 !(new_state & SMSM_A2_POWER_CONTROL)) {
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001342 disconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001343 wake_unlock(&bam_wakelock);
1344 } else if (new_state & SMSM_A2_POWER_CONTROL) {
1345 wake_lock(&bam_wakelock);
Jeff Hugoade1f842011-08-03 15:53:59 -06001346 bam_init();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001347 } else {
Jeff Hugoade1f842011-08-03 15:53:59 -06001348 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001349 }
Jeff Hugoade1f842011-08-03 15:53:59 -06001350
1351}
1352
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001353static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1354 uint32_t new_state)
1355{
1356 complete_all(&ul_wakeup_ack_completion);
1357}
1358
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001359static int bam_dmux_probe(struct platform_device *pdev)
1360{
1361 int rc;
1362
1363 DBG("%s probe called\n", __func__);
1364 if (bam_mux_initialized)
1365 return 0;
1366
Stephen Boyd1c51a492011-10-26 12:11:47 -07001367 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001368 if (IS_ERR(dfab_clk)) {
1369 pr_err("%s: did not get dfab clock\n", __func__);
1370 return -EFAULT;
1371 }
1372
1373 rc = clk_set_rate(dfab_clk, 64000000);
1374 if (rc)
1375 pr_err("%s: unable to set dfab clock rate\n", __func__);
1376
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001377 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1378 if (!bam_mux_rx_workqueue)
1379 return -ENOMEM;
1380
1381 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1382 if (!bam_mux_tx_workqueue) {
1383 destroy_workqueue(bam_mux_rx_workqueue);
1384 return -ENOMEM;
1385 }
1386
Jeff Hugo7960abd2011-08-02 15:39:38 -06001387 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001388 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001389 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1390 "bam_dmux_ch_%d", rc);
1391 /* bus 2, ie a2 stream 2 */
1392 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1393 if (!bam_ch[rc].pdev) {
1394 pr_err("%s: platform device alloc failed\n", __func__);
1395 destroy_workqueue(bam_mux_rx_workqueue);
1396 destroy_workqueue(bam_mux_tx_workqueue);
1397 return -ENOMEM;
1398 }
1399 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001400
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001401 init_completion(&ul_wakeup_ack_completion);
1402 init_completion(&bam_connection_completion);
1403 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001404 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001405
Jeff Hugoade1f842011-08-03 15:53:59 -06001406 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1407 bam_dmux_smsm_cb, NULL);
1408
1409 if (rc) {
1410 destroy_workqueue(bam_mux_rx_workqueue);
1411 destroy_workqueue(bam_mux_tx_workqueue);
1412 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1413 return -ENOMEM;
1414 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001415
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001416 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1417 bam_dmux_smsm_ack_cb, NULL);
1418
1419 if (rc) {
1420 destroy_workqueue(bam_mux_rx_workqueue);
1421 destroy_workqueue(bam_mux_tx_workqueue);
1422 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1423 SMSM_A2_POWER_CONTROL,
1424 bam_dmux_smsm_cb, NULL);
1425 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1426 rc);
1427 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1428 platform_device_put(bam_ch[rc].pdev);
1429 return -ENOMEM;
1430 }
1431
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07001432 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
1433 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
1434
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001435 return 0;
1436}
1437
1438static struct platform_driver bam_dmux_driver = {
1439 .probe = bam_dmux_probe,
1440 .driver = {
1441 .name = "BAM_RMNT",
1442 .owner = THIS_MODULE,
1443 },
1444};
1445
1446static int __init bam_dmux_init(void)
1447{
1448#ifdef CONFIG_DEBUG_FS
1449 struct dentry *dent;
1450
1451 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001452 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001453 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001454 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
1455 debug_create("stats", 0444, dent, debug_stats);
1456 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001457#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001458 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001459 return platform_driver_register(&bam_dmux_driver);
1460}
1461
Jeff Hugoade1f842011-08-03 15:53:59 -06001462late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001463MODULE_DESCRIPTION("MSM BAM DMUX");
1464MODULE_LICENSE("GPL v2");