blob: afcb3bc41edbf684bd18afaec4453cb2a41d2b12 [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;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700419 struct tx_pkt_info *info_expected;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600420 unsigned long event_data;
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;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700425
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 info = container_of(work, struct tx_pkt_info, work);
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700427
428 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
429 info_expected = list_first_entry(&bam_tx_pool,
430 struct tx_pkt_info, list_node);
431 if (unlikely(info != info_expected)) {
432 struct list_head *node;
433
434 pr_err("%s: bam_tx_pool mismatch .next=%p, list_node=%p\n",
435 __func__, bam_tx_pool.next, &info->list_node);
436 list_for_each(node, &bam_tx_pool)
437 pr_err("%s: node=%p\n", __func__, node);
438 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
439 BUG();
440 }
441 list_del(&info->list_node);
442 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
443
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600444 if (info->is_cmd) {
445 kfree(info->skb);
446 kfree(info);
447 return;
448 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449 skb = info->skb;
450 kfree(info);
451 hdr = (struct bam_mux_hdr *)skb->data;
452 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600453 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700454 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
455 bam_ch[hdr->ch_id].num_tx_pkts--;
456 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600457 if (bam_ch[hdr->ch_id].notify)
458 bam_ch[hdr->ch_id].notify(
459 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
460 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461 else
462 dev_kfree_skb_any(skb);
463}
464
465int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
466{
467 int rc = 0;
468 struct bam_mux_hdr *hdr;
469 unsigned long flags;
470 struct sk_buff *new_skb = NULL;
471 dma_addr_t dma_address;
472 struct tx_pkt_info *pkt;
473
474 if (id >= BAM_DMUX_NUM_CHANNELS)
475 return -EINVAL;
476 if (!skb)
477 return -EINVAL;
478 if (!bam_mux_initialized)
479 return -ENODEV;
480
481 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
482 spin_lock_irqsave(&bam_ch[id].lock, flags);
483 if (!bam_ch_is_open(id)) {
484 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
485 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
486 return -ENODEV;
487 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700488
489 if (bam_ch[id].use_wm &&
490 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
491 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
492 pr_err("%s: watermark exceeded: %d\n", __func__, id);
493 return -EAGAIN;
494 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
496
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600497 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600498 if (!bam_is_connected) {
499 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600500 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600501 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600502 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600503 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600504
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 /* if skb do not have any tailroom for padding,
506 copy the skb into a new expanded skb */
507 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
508 /* revisit, probably dev_alloc_skb and memcpy is effecient */
509 new_skb = skb_copy_expand(skb, skb_headroom(skb),
510 4 - (skb->len & 0x3), GFP_ATOMIC);
511 if (new_skb == NULL) {
512 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600513 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 }
515 dev_kfree_skb_any(skb);
516 skb = new_skb;
517 DBG_INC_WRITE_CPY(skb->len);
518 }
519
520 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
521
522 /* caller should allocate for hdr and padding
523 hdr is fine, padding is tricky */
524 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
525 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
526 hdr->reserved = 0;
527 hdr->ch_id = id;
528 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
529 if (skb->len & 0x3)
530 skb_put(skb, 4 - (skb->len & 0x3));
531
532 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
533
534 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
535 __func__, skb->data, skb->tail, skb->len,
536 hdr->pkt_len, hdr->pad_len);
537
538 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
539 if (pkt == NULL) {
540 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600541 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542 }
543
544 dma_address = dma_map_single(NULL, skb->data, skb->len,
545 DMA_TO_DEVICE);
546 if (!dma_address) {
547 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600548 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549 }
550 pkt->skb = skb;
551 pkt->dma_address = dma_address;
552 pkt->is_cmd = 0;
553 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700554 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600555 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700556 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
558 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600559 if (rc) {
560 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700561 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600562 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700563 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700564 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600565 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700566 if (new_skb)
567 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700568 } else {
569 spin_lock_irqsave(&bam_ch[id].lock, flags);
570 bam_ch[id].num_tx_pkts++;
571 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600572 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600573 ul_packet_written = 1;
574 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600576
577write_fail3:
578 kfree(pkt);
579write_fail2:
580 if (new_skb)
581 dev_kfree_skb_any(new_skb);
582write_fail:
583 read_unlock(&ul_wakeup_lock);
584 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585}
586
587int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600588 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589{
590 struct bam_mux_hdr *hdr;
591 unsigned long flags;
592 int rc = 0;
593
594 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700595 if (!bam_mux_initialized) {
596 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700598 }
599 if (id >= BAM_DMUX_NUM_CHANNELS) {
600 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700602 }
603 if (notify == NULL) {
604 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600605 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700606 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607
608 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
609 if (hdr == NULL) {
610 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
611 return -ENOMEM;
612 }
613 spin_lock_irqsave(&bam_ch[id].lock, flags);
614 if (bam_ch_is_open(id)) {
615 DBG("%s: Already opened %d\n", __func__, id);
616 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
617 kfree(hdr);
618 goto open_done;
619 }
620 if (!bam_ch_is_remote_open(id)) {
621 DBG("%s: Remote not open; ch: %d\n", __func__, id);
622 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
623 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700624 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 }
626
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600627 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628 bam_ch[id].priv = priv;
629 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700630 bam_ch[id].num_tx_pkts = 0;
631 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
633
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600634 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600635 if (!bam_is_connected) {
636 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600637 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600638 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600639 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600640 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600641
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
643 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
644 hdr->reserved = 0;
645 hdr->ch_id = id;
646 hdr->pkt_len = 0;
647 hdr->pad_len = 0;
648
649 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600650 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651
652open_done:
653 DBG("%s: opened ch %d\n", __func__, id);
654 return rc;
655}
656
657int msm_bam_dmux_close(uint32_t id)
658{
659 struct bam_mux_hdr *hdr;
660 unsigned long flags;
661 int rc;
662
663 if (id >= BAM_DMUX_NUM_CHANNELS)
664 return -EINVAL;
665 DBG("%s: closing ch %d\n", __func__, id);
666 if (!bam_mux_initialized)
667 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600669 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600670 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600671 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600672 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600673 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600674 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600675 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600676
Jeff Hugo061ce672011-10-21 17:15:32 -0600677 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600678 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679 bam_ch[id].priv = NULL;
680 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
681 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
682
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600683 if (bam_ch_is_in_reset(id)) {
684 read_unlock(&ul_wakeup_lock);
685 bam_ch[id].status &= ~BAM_CH_IN_RESET;
686 return 0;
687 }
688
Jeff Hugobb5802f2011-11-02 17:10:29 -0600689 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 if (hdr == NULL) {
691 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600692 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693 return -ENOMEM;
694 }
695 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
696 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
697 hdr->reserved = 0;
698 hdr->ch_id = id;
699 hdr->pkt_len = 0;
700 hdr->pad_len = 0;
701
702 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600703 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700704
705 DBG("%s: closed ch %d\n", __func__, id);
706 return rc;
707}
708
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700709int msm_bam_dmux_is_ch_full(uint32_t id)
710{
711 unsigned long flags;
712 int ret;
713
714 if (id >= BAM_DMUX_NUM_CHANNELS)
715 return -EINVAL;
716
717 spin_lock_irqsave(&bam_ch[id].lock, flags);
718 bam_ch[id].use_wm = 1;
719 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
720 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
721 id, bam_ch[id].num_tx_pkts, ret);
722 if (!bam_ch_is_local_open(id)) {
723 ret = -ENODEV;
724 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
725 }
726 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
727
728 return ret;
729}
730
731int msm_bam_dmux_is_ch_low(uint32_t id)
732{
733 int ret;
734
735 if (id >= BAM_DMUX_NUM_CHANNELS)
736 return -EINVAL;
737
738 bam_ch[id].use_wm = 1;
739 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
740 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
741 id, bam_ch[id].num_tx_pkts, ret);
742 if (!bam_ch_is_local_open(id)) {
743 ret = -ENODEV;
744 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
745 }
746
747 return ret;
748}
749
Jeff Hugo949080a2011-08-30 11:58:56 -0600750static void rx_timer_work_func(struct work_struct *work)
751{
752 struct sps_iovec iov;
753 struct list_head *node;
754 struct rx_pkt_info *info;
755 int inactive_cycles = 0;
756 int ret;
757 struct sps_connect cur_rx_conn;
758
759 while (1) { /* timer loop */
760 ++inactive_cycles;
761 while (1) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600762 if (in_global_reset)
763 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600764 sps_get_iovec(bam_rx_pipe, &iov);
765 if (iov.addr == 0)
766 break;
767 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600768 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600769 node = bam_rx_pool.next;
770 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600771 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600772 info = container_of(node, struct rx_pkt_info,
773 list_node);
774 handle_bam_mux_cmd(&info->work);
775 }
776
777 if (inactive_cycles == POLLING_INACTIVITY) {
778 /*
779 * attempt to enable interrupts in this pipe
780 * if enabling interrupts fails, continue polling
781 */
782 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
783 if (ret) {
784 pr_err("%s: sps_get_config() failed, interrupts"
785 " not enabled\n", __func__);
786 queue_work(bam_mux_rx_workqueue,
787 &rx_timer_work);
788 return;
789 } else {
790 rx_register_event.options = SPS_O_EOT;
791 /* should check return value */
792 sps_register_event(bam_rx_pipe,
793 &rx_register_event);
794 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
795 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
796 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
797 if (ret) {
798 pr_err("%s: sps_set_config() failed, "
799 "interrupts not enabled\n",
800 __func__);
801 queue_work(bam_mux_rx_workqueue,
802 &rx_timer_work);
803 return;
804 }
805 polling_mode = 0;
806 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600807 if (in_global_reset)
808 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600809 /* handle race condition - missed packet? */
810 sps_get_iovec(bam_rx_pipe, &iov);
811 if (iov.addr == 0)
812 return;
813 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600814 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600815 node = bam_rx_pool.next;
816 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600817 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600818 info = container_of(node, struct rx_pkt_info,
819 list_node);
820 handle_bam_mux_cmd(&info->work);
821 return;
822 }
823
824 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
825 }
826}
827
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828static void bam_mux_tx_notify(struct sps_event_notify *notify)
829{
830 struct tx_pkt_info *pkt;
831
832 DBG("%s: event %d notified\n", __func__, notify->event_id);
833
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600834 if (in_global_reset)
835 return;
836
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837 switch (notify->event_id) {
838 case SPS_EVENT_EOT:
839 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600840 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841 dma_unmap_single(NULL, pkt->dma_address,
842 pkt->skb->len,
843 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600844 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 dma_unmap_single(NULL, pkt->dma_address,
846 pkt->len,
847 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600848 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849 break;
850 default:
851 pr_err("%s: recieved unexpected event id %d\n", __func__,
852 notify->event_id);
853 }
854}
855
Jeff Hugo33dbc002011-08-25 15:52:53 -0600856static void bam_mux_rx_notify(struct sps_event_notify *notify)
857{
Jeff Hugo949080a2011-08-30 11:58:56 -0600858 int ret;
859 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600860
861 DBG("%s: event %d notified\n", __func__, notify->event_id);
862
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600863 if (in_global_reset)
864 return;
865
Jeff Hugo33dbc002011-08-25 15:52:53 -0600866 switch (notify->event_id) {
867 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -0600868 /* attempt to disable interrupts in this pipe */
869 if (!polling_mode) {
870 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
871 if (ret) {
872 pr_err("%s: sps_get_config() failed, interrupts"
873 " not disabled\n", __func__);
874 break;
875 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -0700876 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -0600877 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
878 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
879 if (ret) {
880 pr_err("%s: sps_set_config() failed, interrupts"
881 " not disabled\n", __func__);
882 break;
883 }
884 polling_mode = 1;
885 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
886 }
Jeff Hugo33dbc002011-08-25 15:52:53 -0600887 break;
888 default:
889 pr_err("%s: recieved unexpected event id %d\n", __func__,
890 notify->event_id);
891 }
892}
893
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894#ifdef CONFIG_DEBUG_FS
895
896static int debug_tbl(char *buf, int max)
897{
898 int i = 0;
899 int j;
900
901 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
902 i += scnprintf(buf + i, max - i,
903 "ch%02d local open=%s remote open=%s\n",
904 j, bam_ch_is_local_open(j) ? "Y" : "N",
905 bam_ch_is_remote_open(j) ? "Y" : "N");
906 }
907
908 return i;
909}
910
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700911static int debug_ul_pkt_cnt(char *buf, int max)
912{
913 struct list_head *p;
914 unsigned long flags;
915 int n = 0;
916
917 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
918 __list_for_each(p, &bam_tx_pool) {
919 ++n;
920 }
921 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
922
923 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
924}
925
926static int debug_stats(char *buf, int max)
927{
928 int i = 0;
929
930 i += scnprintf(buf + i, max - i,
931 "skb copy cnt: %u\n"
932 "skb copy bytes: %u\n"
933 "sps tx failures: %u\n",
934 bam_dmux_write_cpy_cnt,
935 bam_dmux_write_cpy_bytes,
936 bam_dmux_tx_sps_failure_cnt
937 );
938
939 return i;
940}
941
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942#define DEBUG_BUFMAX 4096
943static char debug_buffer[DEBUG_BUFMAX];
944
945static ssize_t debug_read(struct file *file, char __user *buf,
946 size_t count, loff_t *ppos)
947{
948 int (*fill)(char *buf, int max) = file->private_data;
949 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
950 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
951}
952
953static int debug_open(struct inode *inode, struct file *file)
954{
955 file->private_data = inode->i_private;
956 return 0;
957}
958
959
960static const struct file_operations debug_ops = {
961 .read = debug_read,
962 .open = debug_open,
963};
964
965static void debug_create(const char *name, mode_t mode,
966 struct dentry *dent,
967 int (*fill)(char *buf, int max))
968{
969 debugfs_create_file(name, mode, dent, fill, &debug_ops);
970}
971
972#endif
973
Jeff Hugod98b1082011-10-24 10:30:23 -0600974static void notify_all(int event, unsigned long data)
975{
976 int i;
977
978 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
979 if (bam_ch_is_open(i))
980 bam_ch[i].notify(bam_ch[i].priv, event, data);
981 }
982}
983
984static void kickoff_ul_wakeup_func(struct work_struct *work)
985{
986 read_lock(&ul_wakeup_lock);
987 if (!bam_is_connected) {
988 read_unlock(&ul_wakeup_lock);
989 ul_wakeup();
990 read_lock(&ul_wakeup_lock);
991 ul_packet_written = 1;
992 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
993 }
994 read_unlock(&ul_wakeup_lock);
995}
996
997void msm_bam_dmux_kickoff_ul_wakeup(void)
998{
999 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1000}
1001
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001002static void ul_timeout(struct work_struct *work)
1003{
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001004 unsigned long flags;
1005 int ret;
1006
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001007 if (in_global_reset)
1008 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001009 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
1010 if (!ret) { /* failed to grab lock, reschedule and bail */
1011 schedule_delayed_work(&ul_timeout_work,
1012 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1013 return;
1014 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001015 if (ul_packet_written) {
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001016 pr_info("%s: packet written\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001017 ul_packet_written = 0;
1018 schedule_delayed_work(&ul_timeout_work,
1019 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1020 } else {
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001021 pr_info("%s: powerdown\n", __func__);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001022 wait_for_ack = 1;
1023 INIT_COMPLETION(ul_wakeup_ack_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001024 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1025 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -06001026 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001027 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001028 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001029}
1030static void ul_wakeup(void)
1031{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001032 int ret;
1033
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001034 mutex_lock(&wakeup_lock);
1035 if (bam_is_connected) { /* bam got connected before lock grabbed */
1036 mutex_unlock(&wakeup_lock);
1037 return;
1038 }
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001039 pr_info("%s\n", __func__);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001040 /*
1041 * must wait for the previous power down request to have been acked
1042 * chances are it already came in and this will just fall through
1043 * instead of waiting
1044 */
1045 if (wait_for_ack) {
1046 ret = wait_for_completion_interruptible_timeout(
1047 &ul_wakeup_ack_completion, HZ);
1048 BUG_ON(ret == 0);
1049 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001050 INIT_COMPLETION(ul_wakeup_ack_completion);
1051 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001052 ret = wait_for_completion_interruptible_timeout(
1053 &ul_wakeup_ack_completion, HZ);
1054 BUG_ON(ret == 0);
1055 ret = wait_for_completion_interruptible_timeout(
1056 &bam_connection_completion, HZ);
1057 BUG_ON(ret == 0);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001058
1059 bam_is_connected = 1;
1060 schedule_delayed_work(&ul_timeout_work,
1061 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1062 mutex_unlock(&wakeup_lock);
1063}
1064
1065static void reconnect_to_bam(void)
1066{
1067 int i;
1068
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001069 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001070 vote_dfab();
1071 i = sps_device_reset(a2_device_handle);
1072 if (i)
1073 pr_err("%s: device reset failed rc = %d\n", __func__, i);
1074 i = sps_connect(bam_tx_pipe, &tx_connection);
1075 if (i)
1076 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
1077 i = sps_connect(bam_rx_pipe, &rx_connection);
1078 if (i)
1079 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
1080 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1081 if (i)
1082 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
1083 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1084 if (i)
1085 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
1086 for (i = 0; i < NUM_BUFFERS; ++i)
1087 queue_rx();
1088 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001089 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001090 complete_all(&bam_connection_completion);
1091}
1092
1093static void disconnect_to_bam(void)
1094{
1095 struct list_head *node;
1096 struct rx_pkt_info *info;
1097
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001098 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001099 INIT_COMPLETION(bam_connection_completion);
1100 sps_disconnect(bam_tx_pipe);
1101 sps_disconnect(bam_rx_pipe);
1102 unvote_dfab();
1103 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1104 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1105 while (!list_empty(&bam_rx_pool)) {
1106 node = bam_rx_pool.next;
1107 list_del(node);
1108 info = container_of(node, struct rx_pkt_info, list_node);
1109 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1110 DMA_FROM_DEVICE);
1111 dev_kfree_skb_any(info->skb);
1112 kfree(info);
1113 }
1114}
1115
1116static void vote_dfab(void)
1117{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001118 int rc;
1119
1120 rc = clk_enable(dfab_clk);
1121 if (rc)
1122 pr_err("bam_dmux vote for dfab failed rc = %d\n", rc);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001123}
1124
1125static void unvote_dfab(void)
1126{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001127 clk_disable(dfab_clk);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001128}
1129
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001130static int restart_notifier_cb(struct notifier_block *this,
1131 unsigned long code,
1132 void *data)
1133{
1134 int i;
1135 struct list_head *node;
1136 struct tx_pkt_info *info;
1137 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001138 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001139
1140 if (code != SUBSYS_AFTER_SHUTDOWN)
1141 return NOTIFY_DONE;
1142
1143 in_global_reset = 1;
1144 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1145 temp_remote_status = bam_ch_is_remote_open(i);
1146 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001147 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001148 if (bam_ch_is_local_open(i))
1149 bam_ch[i].status |= BAM_CH_IN_RESET;
1150 if (temp_remote_status) {
1151 platform_device_unregister(bam_ch[i].pdev);
1152 bam_ch[i].pdev = platform_device_alloc(
1153 bam_ch[i].name, 2);
1154 }
1155 }
1156 /*cleanup UL*/
Jeff Hugo626303bf2011-11-21 11:43:28 -07001157 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001158 while (!list_empty(&bam_tx_pool)) {
1159 node = bam_tx_pool.next;
1160 list_del(node);
1161 info = container_of(node, struct tx_pkt_info,
1162 list_node);
1163 if (!info->is_cmd) {
1164 dma_unmap_single(NULL, info->dma_address,
1165 info->skb->len,
1166 DMA_TO_DEVICE);
1167 dev_kfree_skb_any(info->skb);
1168 } else {
1169 dma_unmap_single(NULL, info->dma_address,
1170 info->len,
1171 DMA_TO_DEVICE);
1172 kfree(info->skb);
1173 }
1174 kfree(info);
1175 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001176 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001177 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1178
1179 return NOTIFY_DONE;
1180}
1181
Jeff Hugoade1f842011-08-03 15:53:59 -06001182static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183{
1184 u32 h;
1185 dma_addr_t dma_addr;
1186 int ret;
1187 void *a2_virt_addr;
1188 int i;
1189
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001190 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001191 /* init BAM */
1192 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1193 if (!a2_virt_addr) {
1194 pr_err("%s: ioremap failed\n", __func__);
1195 ret = -ENOMEM;
1196 goto register_bam_failed;
1197 }
1198 a2_props.phys_addr = A2_PHYS_BASE;
1199 a2_props.virt_addr = a2_virt_addr;
1200 a2_props.virt_size = A2_PHYS_SIZE;
1201 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001202 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001203 a2_props.num_pipes = A2_NUM_PIPES;
1204 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001205 if (cpu_is_msm9615())
1206 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 /* need to free on tear down */
1208 ret = sps_register_bam_device(&a2_props, &h);
1209 if (ret < 0) {
1210 pr_err("%s: register bam error %d\n", __func__, ret);
1211 goto register_bam_failed;
1212 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001213 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001214
1215 bam_tx_pipe = sps_alloc_endpoint();
1216 if (bam_tx_pipe == NULL) {
1217 pr_err("%s: tx alloc endpoint failed\n", __func__);
1218 ret = -ENOMEM;
1219 goto register_bam_failed;
1220 }
1221 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1222 if (ret) {
1223 pr_err("%s: tx get config failed %d\n", __func__, ret);
1224 goto tx_get_config_failed;
1225 }
1226
1227 tx_connection.source = SPS_DEV_HANDLE_MEM;
1228 tx_connection.src_pipe_index = 0;
1229 tx_connection.destination = h;
1230 tx_connection.dest_pipe_index = 4;
1231 tx_connection.mode = SPS_MODE_DEST;
1232 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1233 tx_desc_mem_buf.size = 0x800; /* 2k */
1234 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1235 &dma_addr, 0);
1236 if (tx_desc_mem_buf.base == NULL) {
1237 pr_err("%s: tx memory alloc failed\n", __func__);
1238 ret = -ENOMEM;
1239 goto tx_mem_failed;
1240 }
1241 tx_desc_mem_buf.phys_base = dma_addr;
1242 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1243 tx_connection.desc = tx_desc_mem_buf;
1244 tx_connection.event_thresh = 0x10;
1245
1246 ret = sps_connect(bam_tx_pipe, &tx_connection);
1247 if (ret < 0) {
1248 pr_err("%s: tx connect error %d\n", __func__, ret);
1249 goto tx_connect_failed;
1250 }
1251
1252 bam_rx_pipe = sps_alloc_endpoint();
1253 if (bam_rx_pipe == NULL) {
1254 pr_err("%s: rx alloc endpoint failed\n", __func__);
1255 ret = -ENOMEM;
1256 goto tx_connect_failed;
1257 }
1258 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1259 if (ret) {
1260 pr_err("%s: rx get config failed %d\n", __func__, ret);
1261 goto rx_get_config_failed;
1262 }
1263
1264 rx_connection.source = h;
1265 rx_connection.src_pipe_index = 5;
1266 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1267 rx_connection.dest_pipe_index = 1;
1268 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001269 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1270 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271 rx_desc_mem_buf.size = 0x800; /* 2k */
1272 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1273 &dma_addr, 0);
1274 if (rx_desc_mem_buf.base == NULL) {
1275 pr_err("%s: rx memory alloc failed\n", __func__);
1276 ret = -ENOMEM;
1277 goto rx_mem_failed;
1278 }
1279 rx_desc_mem_buf.phys_base = dma_addr;
1280 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1281 rx_connection.desc = rx_desc_mem_buf;
1282 rx_connection.event_thresh = 0x10;
1283
1284 ret = sps_connect(bam_rx_pipe, &rx_connection);
1285 if (ret < 0) {
1286 pr_err("%s: rx connect error %d\n", __func__, ret);
1287 goto rx_connect_failed;
1288 }
1289
1290 tx_register_event.options = SPS_O_EOT;
1291 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1292 tx_register_event.xfer_done = NULL;
1293 tx_register_event.callback = bam_mux_tx_notify;
1294 tx_register_event.user = NULL;
1295 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1296 if (ret < 0) {
1297 pr_err("%s: tx register event error %d\n", __func__, ret);
1298 goto rx_event_reg_failed;
1299 }
1300
Jeff Hugo33dbc002011-08-25 15:52:53 -06001301 rx_register_event.options = SPS_O_EOT;
1302 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1303 rx_register_event.xfer_done = NULL;
1304 rx_register_event.callback = bam_mux_rx_notify;
1305 rx_register_event.user = NULL;
1306 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1307 if (ret < 0) {
1308 pr_err("%s: tx register event error %d\n", __func__, ret);
1309 goto rx_event_reg_failed;
1310 }
1311
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312 bam_mux_initialized = 1;
1313 for (i = 0; i < NUM_BUFFERS; ++i)
1314 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001315 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001316 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001317 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318 return;
1319
1320rx_event_reg_failed:
1321 sps_disconnect(bam_rx_pipe);
1322rx_connect_failed:
1323 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1324 rx_desc_mem_buf.phys_base);
1325rx_mem_failed:
1326 sps_disconnect(bam_tx_pipe);
1327rx_get_config_failed:
1328 sps_free_endpoint(bam_rx_pipe);
1329tx_connect_failed:
1330 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1331 tx_desc_mem_buf.phys_base);
1332tx_get_config_failed:
1333 sps_free_endpoint(bam_tx_pipe);
1334tx_mem_failed:
1335 sps_deregister_bam_device(h);
1336register_bam_failed:
1337 /*destroy_workqueue(bam_mux_workqueue);*/
1338 /*return ret;*/
1339 return;
1340}
Jeff Hugoade1f842011-08-03 15:53:59 -06001341
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001342static void toggle_apps_ack(void)
1343{
1344 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001345 pr_info("%s: clear bit: %d\n", __func__, clear_bit);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001346 smsm_change_state(SMSM_APPS_STATE,
1347 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1348 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1349 clear_bit = ~clear_bit;
1350}
1351
Jeff Hugoade1f842011-08-03 15:53:59 -06001352static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1353{
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001354 pr_info("%s: smsm activity 0x%08x -> 0x%08x\n", __func__, old_state,
1355 new_state);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001356 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001357 pr_info("%s: reconnect\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001358 wake_lock(&bam_wakelock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001359 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001360 } else if (bam_mux_initialized &&
1361 !(new_state & SMSM_A2_POWER_CONTROL)) {
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001362 pr_info("%s: disconnect\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001363 disconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001364 wake_unlock(&bam_wakelock);
1365 } else if (new_state & SMSM_A2_POWER_CONTROL) {
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001366 pr_info("%s: init\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001367 wake_lock(&bam_wakelock);
Jeff Hugoade1f842011-08-03 15:53:59 -06001368 bam_init();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001369 } else {
Jeff Hugoade1f842011-08-03 15:53:59 -06001370 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001371 }
Jeff Hugoade1f842011-08-03 15:53:59 -06001372
1373}
1374
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001375static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1376 uint32_t new_state)
1377{
Jeff Hugoecf91ad92011-12-21 11:31:03 -07001378 pr_info("%s: 0x%08x -> 0x%08x\n", __func__, old_state, new_state);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001379 complete_all(&ul_wakeup_ack_completion);
1380}
1381
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001382static int bam_dmux_probe(struct platform_device *pdev)
1383{
1384 int rc;
1385
1386 DBG("%s probe called\n", __func__);
1387 if (bam_mux_initialized)
1388 return 0;
1389
Stephen Boyd1c51a492011-10-26 12:11:47 -07001390 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001391 if (IS_ERR(dfab_clk)) {
1392 pr_err("%s: did not get dfab clock\n", __func__);
1393 return -EFAULT;
1394 }
1395
1396 rc = clk_set_rate(dfab_clk, 64000000);
1397 if (rc)
1398 pr_err("%s: unable to set dfab clock rate\n", __func__);
1399
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001400 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1401 if (!bam_mux_rx_workqueue)
1402 return -ENOMEM;
1403
1404 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1405 if (!bam_mux_tx_workqueue) {
1406 destroy_workqueue(bam_mux_rx_workqueue);
1407 return -ENOMEM;
1408 }
1409
Jeff Hugo7960abd2011-08-02 15:39:38 -06001410 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001411 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001412 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1413 "bam_dmux_ch_%d", rc);
1414 /* bus 2, ie a2 stream 2 */
1415 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1416 if (!bam_ch[rc].pdev) {
1417 pr_err("%s: platform device alloc failed\n", __func__);
1418 destroy_workqueue(bam_mux_rx_workqueue);
1419 destroy_workqueue(bam_mux_tx_workqueue);
1420 return -ENOMEM;
1421 }
1422 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001423
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001424 init_completion(&ul_wakeup_ack_completion);
1425 init_completion(&bam_connection_completion);
1426 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001427 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001428
Jeff Hugoade1f842011-08-03 15:53:59 -06001429 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1430 bam_dmux_smsm_cb, NULL);
1431
1432 if (rc) {
1433 destroy_workqueue(bam_mux_rx_workqueue);
1434 destroy_workqueue(bam_mux_tx_workqueue);
1435 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1436 return -ENOMEM;
1437 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001438
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001439 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1440 bam_dmux_smsm_ack_cb, NULL);
1441
1442 if (rc) {
1443 destroy_workqueue(bam_mux_rx_workqueue);
1444 destroy_workqueue(bam_mux_tx_workqueue);
1445 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1446 SMSM_A2_POWER_CONTROL,
1447 bam_dmux_smsm_cb, NULL);
1448 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1449 rc);
1450 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1451 platform_device_put(bam_ch[rc].pdev);
1452 return -ENOMEM;
1453 }
1454
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07001455 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
1456 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
1457
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001458 return 0;
1459}
1460
1461static struct platform_driver bam_dmux_driver = {
1462 .probe = bam_dmux_probe,
1463 .driver = {
1464 .name = "BAM_RMNT",
1465 .owner = THIS_MODULE,
1466 },
1467};
1468
1469static int __init bam_dmux_init(void)
1470{
1471#ifdef CONFIG_DEBUG_FS
1472 struct dentry *dent;
1473
1474 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001475 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001476 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001477 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
1478 debug_create("stats", 0444, dent, debug_stats);
1479 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001480#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001481 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001482 return platform_driver_register(&bam_dmux_driver);
1483}
1484
Jeff Hugoade1f842011-08-03 15:53:59 -06001485late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001486MODULE_DESCRIPTION("MSM BAM DMUX");
1487MODULE_LICENSE("GPL v2");