blob: b865f24c19603558c99a3262e367d15fd4e8ad30 [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) {
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001002 pr_info("%s: packet written\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001003 ul_packet_written = 0;
1004 schedule_delayed_work(&ul_timeout_work,
1005 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1006 } else {
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001007 pr_info("%s: powerdown\n", __func__);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001008 wait_for_ack = 1;
1009 INIT_COMPLETION(ul_wakeup_ack_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001010 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1011 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -06001012 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001013 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001014 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001015}
1016static void ul_wakeup(void)
1017{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001018 int ret;
1019
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001020 mutex_lock(&wakeup_lock);
1021 if (bam_is_connected) { /* bam got connected before lock grabbed */
1022 mutex_unlock(&wakeup_lock);
1023 return;
1024 }
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001025 pr_info("%s\n", __func__);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001026 /*
1027 * must wait for the previous power down request to have been acked
1028 * chances are it already came in and this will just fall through
1029 * instead of waiting
1030 */
1031 if (wait_for_ack) {
1032 ret = wait_for_completion_interruptible_timeout(
1033 &ul_wakeup_ack_completion, HZ);
1034 BUG_ON(ret == 0);
1035 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001036 INIT_COMPLETION(ul_wakeup_ack_completion);
1037 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001038 ret = wait_for_completion_interruptible_timeout(
1039 &ul_wakeup_ack_completion, HZ);
1040 BUG_ON(ret == 0);
1041 ret = wait_for_completion_interruptible_timeout(
1042 &bam_connection_completion, HZ);
1043 BUG_ON(ret == 0);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001044
1045 bam_is_connected = 1;
1046 schedule_delayed_work(&ul_timeout_work,
1047 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1048 mutex_unlock(&wakeup_lock);
1049}
1050
1051static void reconnect_to_bam(void)
1052{
1053 int i;
1054
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001055 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001056 vote_dfab();
1057 i = sps_device_reset(a2_device_handle);
1058 if (i)
1059 pr_err("%s: device reset failed rc = %d\n", __func__, i);
1060 i = sps_connect(bam_tx_pipe, &tx_connection);
1061 if (i)
1062 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
1063 i = sps_connect(bam_rx_pipe, &rx_connection);
1064 if (i)
1065 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
1066 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1067 if (i)
1068 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
1069 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1070 if (i)
1071 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
1072 for (i = 0; i < NUM_BUFFERS; ++i)
1073 queue_rx();
1074 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001075 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001076 complete_all(&bam_connection_completion);
1077}
1078
1079static void disconnect_to_bam(void)
1080{
1081 struct list_head *node;
1082 struct rx_pkt_info *info;
1083
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001084 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001085 INIT_COMPLETION(bam_connection_completion);
1086 sps_disconnect(bam_tx_pipe);
1087 sps_disconnect(bam_rx_pipe);
1088 unvote_dfab();
1089 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1090 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1091 while (!list_empty(&bam_rx_pool)) {
1092 node = bam_rx_pool.next;
1093 list_del(node);
1094 info = container_of(node, struct rx_pkt_info, list_node);
1095 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1096 DMA_FROM_DEVICE);
1097 dev_kfree_skb_any(info->skb);
1098 kfree(info);
1099 }
1100}
1101
1102static void vote_dfab(void)
1103{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001104 int rc;
1105
1106 rc = clk_enable(dfab_clk);
1107 if (rc)
1108 pr_err("bam_dmux vote for dfab failed rc = %d\n", rc);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001109}
1110
1111static void unvote_dfab(void)
1112{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001113 clk_disable(dfab_clk);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001114}
1115
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001116static int restart_notifier_cb(struct notifier_block *this,
1117 unsigned long code,
1118 void *data)
1119{
1120 int i;
1121 struct list_head *node;
1122 struct tx_pkt_info *info;
1123 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001124 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001125
1126 if (code != SUBSYS_AFTER_SHUTDOWN)
1127 return NOTIFY_DONE;
1128
1129 in_global_reset = 1;
1130 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1131 temp_remote_status = bam_ch_is_remote_open(i);
1132 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001133 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001134 if (bam_ch_is_local_open(i))
1135 bam_ch[i].status |= BAM_CH_IN_RESET;
1136 if (temp_remote_status) {
1137 platform_device_unregister(bam_ch[i].pdev);
1138 bam_ch[i].pdev = platform_device_alloc(
1139 bam_ch[i].name, 2);
1140 }
1141 }
1142 /*cleanup UL*/
Jeff Hugo626303bf2011-11-21 11:43:28 -07001143 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001144 while (!list_empty(&bam_tx_pool)) {
1145 node = bam_tx_pool.next;
1146 list_del(node);
1147 info = container_of(node, struct tx_pkt_info,
1148 list_node);
1149 if (!info->is_cmd) {
1150 dma_unmap_single(NULL, info->dma_address,
1151 info->skb->len,
1152 DMA_TO_DEVICE);
1153 dev_kfree_skb_any(info->skb);
1154 } else {
1155 dma_unmap_single(NULL, info->dma_address,
1156 info->len,
1157 DMA_TO_DEVICE);
1158 kfree(info->skb);
1159 }
1160 kfree(info);
1161 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001162 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001163 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1164
1165 return NOTIFY_DONE;
1166}
1167
Jeff Hugoade1f842011-08-03 15:53:59 -06001168static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169{
1170 u32 h;
1171 dma_addr_t dma_addr;
1172 int ret;
1173 void *a2_virt_addr;
1174 int i;
1175
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001176 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 /* init BAM */
1178 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1179 if (!a2_virt_addr) {
1180 pr_err("%s: ioremap failed\n", __func__);
1181 ret = -ENOMEM;
1182 goto register_bam_failed;
1183 }
1184 a2_props.phys_addr = A2_PHYS_BASE;
1185 a2_props.virt_addr = a2_virt_addr;
1186 a2_props.virt_size = A2_PHYS_SIZE;
1187 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001188 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189 a2_props.num_pipes = A2_NUM_PIPES;
1190 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001191 if (cpu_is_msm9615())
1192 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193 /* need to free on tear down */
1194 ret = sps_register_bam_device(&a2_props, &h);
1195 if (ret < 0) {
1196 pr_err("%s: register bam error %d\n", __func__, ret);
1197 goto register_bam_failed;
1198 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001199 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200
1201 bam_tx_pipe = sps_alloc_endpoint();
1202 if (bam_tx_pipe == NULL) {
1203 pr_err("%s: tx alloc endpoint failed\n", __func__);
1204 ret = -ENOMEM;
1205 goto register_bam_failed;
1206 }
1207 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1208 if (ret) {
1209 pr_err("%s: tx get config failed %d\n", __func__, ret);
1210 goto tx_get_config_failed;
1211 }
1212
1213 tx_connection.source = SPS_DEV_HANDLE_MEM;
1214 tx_connection.src_pipe_index = 0;
1215 tx_connection.destination = h;
1216 tx_connection.dest_pipe_index = 4;
1217 tx_connection.mode = SPS_MODE_DEST;
1218 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1219 tx_desc_mem_buf.size = 0x800; /* 2k */
1220 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1221 &dma_addr, 0);
1222 if (tx_desc_mem_buf.base == NULL) {
1223 pr_err("%s: tx memory alloc failed\n", __func__);
1224 ret = -ENOMEM;
1225 goto tx_mem_failed;
1226 }
1227 tx_desc_mem_buf.phys_base = dma_addr;
1228 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1229 tx_connection.desc = tx_desc_mem_buf;
1230 tx_connection.event_thresh = 0x10;
1231
1232 ret = sps_connect(bam_tx_pipe, &tx_connection);
1233 if (ret < 0) {
1234 pr_err("%s: tx connect error %d\n", __func__, ret);
1235 goto tx_connect_failed;
1236 }
1237
1238 bam_rx_pipe = sps_alloc_endpoint();
1239 if (bam_rx_pipe == NULL) {
1240 pr_err("%s: rx alloc endpoint failed\n", __func__);
1241 ret = -ENOMEM;
1242 goto tx_connect_failed;
1243 }
1244 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1245 if (ret) {
1246 pr_err("%s: rx get config failed %d\n", __func__, ret);
1247 goto rx_get_config_failed;
1248 }
1249
1250 rx_connection.source = h;
1251 rx_connection.src_pipe_index = 5;
1252 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1253 rx_connection.dest_pipe_index = 1;
1254 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001255 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1256 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001257 rx_desc_mem_buf.size = 0x800; /* 2k */
1258 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1259 &dma_addr, 0);
1260 if (rx_desc_mem_buf.base == NULL) {
1261 pr_err("%s: rx memory alloc failed\n", __func__);
1262 ret = -ENOMEM;
1263 goto rx_mem_failed;
1264 }
1265 rx_desc_mem_buf.phys_base = dma_addr;
1266 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1267 rx_connection.desc = rx_desc_mem_buf;
1268 rx_connection.event_thresh = 0x10;
1269
1270 ret = sps_connect(bam_rx_pipe, &rx_connection);
1271 if (ret < 0) {
1272 pr_err("%s: rx connect error %d\n", __func__, ret);
1273 goto rx_connect_failed;
1274 }
1275
1276 tx_register_event.options = SPS_O_EOT;
1277 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1278 tx_register_event.xfer_done = NULL;
1279 tx_register_event.callback = bam_mux_tx_notify;
1280 tx_register_event.user = NULL;
1281 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1282 if (ret < 0) {
1283 pr_err("%s: tx register event error %d\n", __func__, ret);
1284 goto rx_event_reg_failed;
1285 }
1286
Jeff Hugo33dbc002011-08-25 15:52:53 -06001287 rx_register_event.options = SPS_O_EOT;
1288 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1289 rx_register_event.xfer_done = NULL;
1290 rx_register_event.callback = bam_mux_rx_notify;
1291 rx_register_event.user = NULL;
1292 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1293 if (ret < 0) {
1294 pr_err("%s: tx register event error %d\n", __func__, ret);
1295 goto rx_event_reg_failed;
1296 }
1297
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001298 bam_mux_initialized = 1;
1299 for (i = 0; i < NUM_BUFFERS; ++i)
1300 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001301 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001302 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001303 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304 return;
1305
1306rx_event_reg_failed:
1307 sps_disconnect(bam_rx_pipe);
1308rx_connect_failed:
1309 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1310 rx_desc_mem_buf.phys_base);
1311rx_mem_failed:
1312 sps_disconnect(bam_tx_pipe);
1313rx_get_config_failed:
1314 sps_free_endpoint(bam_rx_pipe);
1315tx_connect_failed:
1316 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1317 tx_desc_mem_buf.phys_base);
1318tx_get_config_failed:
1319 sps_free_endpoint(bam_tx_pipe);
1320tx_mem_failed:
1321 sps_deregister_bam_device(h);
1322register_bam_failed:
1323 /*destroy_workqueue(bam_mux_workqueue);*/
1324 /*return ret;*/
1325 return;
1326}
Jeff Hugoade1f842011-08-03 15:53:59 -06001327
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001328static void toggle_apps_ack(void)
1329{
1330 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001331 pr_info("%s: clear bit: %d\n", __func__, clear_bit);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001332 smsm_change_state(SMSM_APPS_STATE,
1333 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1334 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1335 clear_bit = ~clear_bit;
1336}
1337
Jeff Hugoade1f842011-08-03 15:53:59 -06001338static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1339{
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001340 pr_info("%s: smsm activity 0x%08x -> 0x%08x\n", __func__, old_state,
1341 new_state);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001342 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001343 pr_info("%s: reconnect\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001344 wake_lock(&bam_wakelock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001345 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001346 } else if (bam_mux_initialized &&
1347 !(new_state & SMSM_A2_POWER_CONTROL)) {
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001348 pr_info("%s: disconnect\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001349 disconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001350 wake_unlock(&bam_wakelock);
1351 } else if (new_state & SMSM_A2_POWER_CONTROL) {
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001352 pr_info("%s: init\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001353 wake_lock(&bam_wakelock);
Jeff Hugoade1f842011-08-03 15:53:59 -06001354 bam_init();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001355 } else {
Jeff Hugoade1f842011-08-03 15:53:59 -06001356 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001357 }
Jeff Hugoade1f842011-08-03 15:53:59 -06001358
1359}
1360
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001361static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1362 uint32_t new_state)
1363{
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001364 pr_info("%s: 0x%08x -> 0x%08x\n", __func__, old_state, new_state);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001365 complete_all(&ul_wakeup_ack_completion);
1366}
1367
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001368static int bam_dmux_probe(struct platform_device *pdev)
1369{
1370 int rc;
1371
1372 DBG("%s probe called\n", __func__);
1373 if (bam_mux_initialized)
1374 return 0;
1375
Stephen Boyd1c51a492011-10-26 12:11:47 -07001376 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001377 if (IS_ERR(dfab_clk)) {
1378 pr_err("%s: did not get dfab clock\n", __func__);
1379 return -EFAULT;
1380 }
1381
1382 rc = clk_set_rate(dfab_clk, 64000000);
1383 if (rc)
1384 pr_err("%s: unable to set dfab clock rate\n", __func__);
1385
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001386 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1387 if (!bam_mux_rx_workqueue)
1388 return -ENOMEM;
1389
1390 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1391 if (!bam_mux_tx_workqueue) {
1392 destroy_workqueue(bam_mux_rx_workqueue);
1393 return -ENOMEM;
1394 }
1395
Jeff Hugo7960abd2011-08-02 15:39:38 -06001396 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001397 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001398 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1399 "bam_dmux_ch_%d", rc);
1400 /* bus 2, ie a2 stream 2 */
1401 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1402 if (!bam_ch[rc].pdev) {
1403 pr_err("%s: platform device alloc failed\n", __func__);
1404 destroy_workqueue(bam_mux_rx_workqueue);
1405 destroy_workqueue(bam_mux_tx_workqueue);
1406 return -ENOMEM;
1407 }
1408 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001409
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001410 init_completion(&ul_wakeup_ack_completion);
1411 init_completion(&bam_connection_completion);
1412 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001413 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001414
Jeff Hugoade1f842011-08-03 15:53:59 -06001415 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1416 bam_dmux_smsm_cb, NULL);
1417
1418 if (rc) {
1419 destroy_workqueue(bam_mux_rx_workqueue);
1420 destroy_workqueue(bam_mux_tx_workqueue);
1421 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1422 return -ENOMEM;
1423 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001424
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001425 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1426 bam_dmux_smsm_ack_cb, NULL);
1427
1428 if (rc) {
1429 destroy_workqueue(bam_mux_rx_workqueue);
1430 destroy_workqueue(bam_mux_tx_workqueue);
1431 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1432 SMSM_A2_POWER_CONTROL,
1433 bam_dmux_smsm_cb, NULL);
1434 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1435 rc);
1436 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1437 platform_device_put(bam_ch[rc].pdev);
1438 return -ENOMEM;
1439 }
1440
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07001441 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
1442 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
1443
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001444 return 0;
1445}
1446
1447static struct platform_driver bam_dmux_driver = {
1448 .probe = bam_dmux_probe,
1449 .driver = {
1450 .name = "BAM_RMNT",
1451 .owner = THIS_MODULE,
1452 },
1453};
1454
1455static int __init bam_dmux_init(void)
1456{
1457#ifdef CONFIG_DEBUG_FS
1458 struct dentry *dent;
1459
1460 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001461 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001462 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001463 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
1464 debug_create("stats", 0444, dent, debug_stats);
1465 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001466#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001467 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001468 return platform_driver_register(&bam_dmux_driver);
1469}
1470
Jeff Hugoade1f842011-08-03 15:53:59 -06001471late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001472MODULE_DESCRIPTION("MSM BAM DMUX");
1473MODULE_LICENSE("GPL v2");