blob: 0cd8a6e0c8dbd9b29ab0d4c353644eb2619590c5 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14/*
15 * BAM DMUX module.
16 */
17
18#define DEBUG
19
20#include <linux/delay.h>
21#include <linux/module.h>
22#include <linux/netdevice.h>
23#include <linux/platform_device.h>
24#include <linux/sched.h>
25#include <linux/skbuff.h>
26#include <linux/debugfs.h>
Jeff Hugoaab7ebc2011-09-07 16:46:04 -060027#include <linux/clk.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
29#include <mach/sps.h>
30#include <mach/bam_dmux.h>
Jeff Hugoade1f842011-08-03 15:53:59 -060031#include <mach/msm_smsm.h>
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060032#include <mach/subsystem_notif.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34#define BAM_CH_LOCAL_OPEN 0x1
35#define BAM_CH_REMOTE_OPEN 0x2
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060036#define BAM_CH_IN_RESET 0x4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
38#define BAM_MUX_HDR_MAGIC_NO 0x33fc
39
40#define BAM_MUX_HDR_CMD_DATA 0
41#define BAM_MUX_HDR_CMD_OPEN 1
42#define BAM_MUX_HDR_CMD_CLOSE 2
43
Jeff Hugo949080a2011-08-30 11:58:56 -060044#define POLLING_MIN_SLEEP 950 /* 0.95 ms */
45#define POLLING_MAX_SLEEP 1050 /* 1.05 ms */
46#define POLLING_INACTIVITY 40 /* cycles before switch to intr mode */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047
48
49static int msm_bam_dmux_debug_enable;
50module_param_named(debug_enable, msm_bam_dmux_debug_enable,
51 int, S_IRUGO | S_IWUSR | S_IWGRP);
52
53#if defined(DEBUG)
54static uint32_t bam_dmux_read_cnt;
55static uint32_t bam_dmux_write_cnt;
56static uint32_t bam_dmux_write_cpy_cnt;
57static uint32_t bam_dmux_write_cpy_bytes;
58
59#define DBG(x...) do { \
60 if (msm_bam_dmux_debug_enable) \
61 pr_debug(x); \
62 } while (0)
63
64#define DBG_INC_READ_CNT(x) do { \
65 bam_dmux_read_cnt += (x); \
66 if (msm_bam_dmux_debug_enable) \
67 pr_debug("%s: total read bytes %u\n", \
68 __func__, bam_dmux_read_cnt); \
69 } while (0)
70
71#define DBG_INC_WRITE_CNT(x) do { \
72 bam_dmux_write_cnt += (x); \
73 if (msm_bam_dmux_debug_enable) \
74 pr_debug("%s: total written bytes %u\n", \
75 __func__, bam_dmux_write_cnt); \
76 } while (0)
77
78#define DBG_INC_WRITE_CPY(x) do { \
79 bam_dmux_write_cpy_bytes += (x); \
80 bam_dmux_write_cpy_cnt++; \
81 if (msm_bam_dmux_debug_enable) \
82 pr_debug("%s: total write copy cnt %u, bytes %u\n", \
83 __func__, bam_dmux_write_cpy_cnt, \
84 bam_dmux_write_cpy_bytes); \
85 } while (0)
86#else
87#define DBG(x...) do { } while (0)
88#define DBG_INC_READ_CNT(x...) do { } while (0)
89#define DBG_INC_WRITE_CNT(x...) do { } while (0)
90#define DBG_INC_WRITE_CPY(x...) do { } while (0)
91#endif
92
93struct bam_ch_info {
94 uint32_t status;
Jeff Hugo1c4531c2011-08-02 14:55:37 -060095 void (*notify)(void *, int, unsigned long);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 void *priv;
97 spinlock_t lock;
Jeff Hugo7960abd2011-08-02 15:39:38 -060098 struct platform_device *pdev;
99 char name[BAM_DMUX_CH_NAME_MAX_LEN];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100};
101
102struct tx_pkt_info {
103 struct sk_buff *skb;
104 dma_addr_t dma_address;
105 char is_cmd;
106 uint32_t len;
107 struct work_struct work;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600108 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109};
110
111struct rx_pkt_info {
112 struct sk_buff *skb;
113 dma_addr_t dma_address;
114 struct work_struct work;
Jeff Hugo949080a2011-08-30 11:58:56 -0600115 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116};
117
118#define A2_NUM_PIPES 6
119#define A2_SUMMING_THRESHOLD 4096
120#define A2_DEFAULT_DESCRIPTORS 32
121#define A2_PHYS_BASE 0x124C2000
122#define A2_PHYS_SIZE 0x2000
123#define BUFFER_SIZE 2048
124#define NUM_BUFFERS 32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125static struct sps_bam_props a2_props;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600126static u32 a2_device_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127static struct sps_pipe *bam_tx_pipe;
128static struct sps_pipe *bam_rx_pipe;
129static struct sps_connect tx_connection;
130static struct sps_connect rx_connection;
131static struct sps_mem_buffer tx_desc_mem_buf;
132static struct sps_mem_buffer rx_desc_mem_buf;
133static struct sps_register_event tx_register_event;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600134static struct sps_register_event rx_register_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135
136static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS];
137static int bam_mux_initialized;
138
Jeff Hugo949080a2011-08-30 11:58:56 -0600139static int polling_mode;
140
141static LIST_HEAD(bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600142static DEFINE_MUTEX(bam_rx_pool_mutexlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600143static LIST_HEAD(bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600144static DEFINE_SPINLOCK(bam_tx_pool_spinlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600145
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146struct bam_mux_hdr {
147 uint16_t magic_num;
148 uint8_t reserved;
149 uint8_t cmd;
150 uint8_t pad_len;
151 uint8_t ch_id;
152 uint16_t pkt_len;
153};
154
Jeff Hugod98b1082011-10-24 10:30:23 -0600155static void notify_all(int event, unsigned long data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700156static void bam_mux_write_done(struct work_struct *work);
157static void handle_bam_mux_cmd(struct work_struct *work);
Jeff Hugo949080a2011-08-30 11:58:56 -0600158static void rx_timer_work_func(struct work_struct *work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159
Jeff Hugo949080a2011-08-30 11:58:56 -0600160static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161
162static struct workqueue_struct *bam_mux_rx_workqueue;
163static struct workqueue_struct *bam_mux_tx_workqueue;
164
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600165/* A2 power collaspe */
166#define UL_TIMEOUT_DELAY 1000 /* in ms */
167static void toggle_apps_ack(void);
168static void reconnect_to_bam(void);
169static void disconnect_to_bam(void);
170static void ul_wakeup(void);
171static void ul_timeout(struct work_struct *work);
172static void vote_dfab(void);
173static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600174static void kickoff_ul_wakeup_func(struct work_struct *work);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600175
176static int bam_is_connected;
177static DEFINE_MUTEX(wakeup_lock);
178static struct completion ul_wakeup_ack_completion;
179static struct completion bam_connection_completion;
180static struct delayed_work ul_timeout_work;
181static int ul_packet_written;
182static struct clk *dfab_clk;
183static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600184static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600185static int bam_connection_is_active;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600186/* End A2 power collaspe */
187
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600188/* subsystem restart */
189static int restart_notifier_cb(struct notifier_block *this,
190 unsigned long code,
191 void *data);
192
193static struct notifier_block restart_notifier = {
194 .notifier_call = restart_notifier_cb,
195};
196static int in_global_reset;
197/* end subsystem restart */
198
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199#define bam_ch_is_open(x) \
200 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
201
202#define bam_ch_is_local_open(x) \
203 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
204
205#define bam_ch_is_remote_open(x) \
206 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
207
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600208#define bam_ch_is_in_reset(x) \
209 (bam_ch[(x)].status & BAM_CH_IN_RESET)
210
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211static void queue_rx(void)
212{
213 void *ptr;
214 struct rx_pkt_info *info;
215
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600216 if (in_global_reset)
217 return;
218
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219 info = kmalloc(sizeof(struct rx_pkt_info), GFP_KERNEL);
220 if (!info)
221 return; /*need better way to handle this */
222
223 INIT_WORK(&info->work, handle_bam_mux_cmd);
224
225 info->skb = __dev_alloc_skb(BUFFER_SIZE, GFP_KERNEL);
226 ptr = skb_put(info->skb, BUFFER_SIZE);
Jeff Hugo949080a2011-08-30 11:58:56 -0600227
Jeff Hugoc9749932011-11-02 17:50:40 -0600228 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600229 list_add_tail(&info->list_node, &bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600230 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600231
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232 /* need a way to handle error case */
233 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
234 DMA_FROM_DEVICE);
235 sps_transfer_one(bam_rx_pipe, info->dma_address,
Jeff Hugo33dbc002011-08-25 15:52:53 -0600236 BUFFER_SIZE, info,
237 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238}
239
240static void bam_mux_process_data(struct sk_buff *rx_skb)
241{
242 unsigned long flags;
243 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600244 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245
246 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
247
248 rx_skb->data = (unsigned char *)(rx_hdr + 1);
249 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
250 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600251 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600253 event_data = (unsigned long)(rx_skb);
254
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600256 if (bam_ch[rx_hdr->ch_id].notify)
257 bam_ch[rx_hdr->ch_id].notify(
258 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
259 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 else
261 dev_kfree_skb_any(rx_skb);
262 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
263
264 queue_rx();
265}
266
267static void handle_bam_mux_cmd(struct work_struct *work)
268{
269 unsigned long flags;
270 struct bam_mux_hdr *rx_hdr;
271 struct rx_pkt_info *info;
272 struct sk_buff *rx_skb;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600273 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274
275 info = container_of(work, struct rx_pkt_info, work);
276 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600277 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 kfree(info);
279
280 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
281
282 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
283 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
284 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
285 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
286 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
287 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
288 " pad %d ch %d len %d\n", __func__,
289 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
290 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
291 dev_kfree_skb_any(rx_skb);
292 queue_rx();
293 return;
294 }
295 switch (rx_hdr->cmd) {
296 case BAM_MUX_HDR_CMD_DATA:
297 DBG_INC_READ_CNT(rx_hdr->pkt_len);
298 bam_mux_process_data(rx_skb);
299 break;
300 case BAM_MUX_HDR_CMD_OPEN:
301 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
302 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
303 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600305 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
306 if (ret)
307 pr_err("%s: platform_device_add() error: %d\n",
308 __func__, ret);
Eric Holmberge779dba2011-11-04 18:22:01 -0600309 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 break;
311 case BAM_MUX_HDR_CMD_CLOSE:
312 /* probably should drop pending write */
313 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
314 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
315 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600317 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
318 bam_ch[rx_hdr->ch_id].pdev =
319 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
320 if (!bam_ch[rx_hdr->ch_id].pdev)
321 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberge779dba2011-11-04 18:22:01 -0600322 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323 break;
324 default:
325 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
326 " pad %d ch %d len %d\n", __func__,
327 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
328 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
329 dev_kfree_skb_any(rx_skb);
330 queue_rx();
331 return;
332 }
333}
334
335static int bam_mux_write_cmd(void *data, uint32_t len)
336{
337 int rc;
338 struct tx_pkt_info *pkt;
339 dma_addr_t dma_address;
340
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600341 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 if (pkt == NULL) {
343 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
344 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345 return rc;
346 }
347
348 dma_address = dma_map_single(NULL, data, len,
349 DMA_TO_DEVICE);
350 if (!dma_address) {
351 pr_err("%s: dma_map_single() failed\n", __func__);
352 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 return rc;
354 }
355 pkt->skb = (struct sk_buff *)(data);
356 pkt->len = len;
357 pkt->dma_address = dma_address;
358 pkt->is_cmd = 1;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600359 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugoc9749932011-11-02 17:50:40 -0600360 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600361 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600362 spin_unlock(&bam_tx_pool_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
364 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600365 if (rc) {
366 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
367 spin_lock(&bam_tx_pool_spinlock);
368 list_del(&pkt->list_node);
369 spin_unlock(&bam_tx_pool_spinlock);
370 kfree(pkt);
371 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600373 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700374 return rc;
375}
376
377static void bam_mux_write_done(struct work_struct *work)
378{
379 struct sk_buff *skb;
380 struct bam_mux_hdr *hdr;
381 struct tx_pkt_info *info;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600382 unsigned long event_data;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600383 struct list_head *node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600385 if (in_global_reset)
386 return;
Jeff Hugoc9749932011-11-02 17:50:40 -0600387 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600388 node = bam_tx_pool.next;
389 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600390 spin_unlock(&bam_tx_pool_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391 info = container_of(work, struct tx_pkt_info, work);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600392 if (info->is_cmd) {
393 kfree(info->skb);
394 kfree(info);
395 return;
396 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397 skb = info->skb;
398 kfree(info);
399 hdr = (struct bam_mux_hdr *)skb->data;
400 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600401 event_data = (unsigned long)(skb);
402 if (bam_ch[hdr->ch_id].notify)
403 bam_ch[hdr->ch_id].notify(
404 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
405 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406 else
407 dev_kfree_skb_any(skb);
408}
409
410int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
411{
412 int rc = 0;
413 struct bam_mux_hdr *hdr;
414 unsigned long flags;
415 struct sk_buff *new_skb = NULL;
416 dma_addr_t dma_address;
417 struct tx_pkt_info *pkt;
418
419 if (id >= BAM_DMUX_NUM_CHANNELS)
420 return -EINVAL;
421 if (!skb)
422 return -EINVAL;
423 if (!bam_mux_initialized)
424 return -ENODEV;
425
426 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
427 spin_lock_irqsave(&bam_ch[id].lock, flags);
428 if (!bam_ch_is_open(id)) {
429 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
430 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
431 return -ENODEV;
432 }
433 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
434
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600435 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600436 if (!bam_is_connected) {
437 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600438 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600439 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600440 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600441 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600442
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 /* if skb do not have any tailroom for padding,
444 copy the skb into a new expanded skb */
445 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
446 /* revisit, probably dev_alloc_skb and memcpy is effecient */
447 new_skb = skb_copy_expand(skb, skb_headroom(skb),
448 4 - (skb->len & 0x3), GFP_ATOMIC);
449 if (new_skb == NULL) {
450 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600451 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 }
453 dev_kfree_skb_any(skb);
454 skb = new_skb;
455 DBG_INC_WRITE_CPY(skb->len);
456 }
457
458 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
459
460 /* caller should allocate for hdr and padding
461 hdr is fine, padding is tricky */
462 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
463 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
464 hdr->reserved = 0;
465 hdr->ch_id = id;
466 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
467 if (skb->len & 0x3)
468 skb_put(skb, 4 - (skb->len & 0x3));
469
470 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
471
472 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
473 __func__, skb->data, skb->tail, skb->len,
474 hdr->pkt_len, hdr->pad_len);
475
476 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
477 if (pkt == NULL) {
478 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600479 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 }
481
482 dma_address = dma_map_single(NULL, skb->data, skb->len,
483 DMA_TO_DEVICE);
484 if (!dma_address) {
485 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600486 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 }
488 pkt->skb = skb;
489 pkt->dma_address = dma_address;
490 pkt->is_cmd = 0;
491 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugoc9749932011-11-02 17:50:40 -0600492 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600493 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600494 spin_unlock(&bam_tx_pool_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
496 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600497 if (rc) {
498 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
499 spin_lock(&bam_tx_pool_spinlock);
500 list_del(&pkt->list_node);
501 spin_unlock(&bam_tx_pool_spinlock);
502 kfree(pkt);
503 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600504 ul_packet_written = 1;
505 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600507
508write_fail3:
509 kfree(pkt);
510write_fail2:
511 if (new_skb)
512 dev_kfree_skb_any(new_skb);
513write_fail:
514 read_unlock(&ul_wakeup_lock);
515 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516}
517
518int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600519 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520{
521 struct bam_mux_hdr *hdr;
522 unsigned long flags;
523 int rc = 0;
524
525 DBG("%s: opening ch %d\n", __func__, id);
526 if (!bam_mux_initialized)
527 return -ENODEV;
528 if (id >= BAM_DMUX_NUM_CHANNELS)
529 return -EINVAL;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600530 if (notify == NULL)
531 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532
533 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
534 if (hdr == NULL) {
535 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
536 return -ENOMEM;
537 }
538 spin_lock_irqsave(&bam_ch[id].lock, flags);
539 if (bam_ch_is_open(id)) {
540 DBG("%s: Already opened %d\n", __func__, id);
541 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
542 kfree(hdr);
543 goto open_done;
544 }
545 if (!bam_ch_is_remote_open(id)) {
546 DBG("%s: Remote not open; ch: %d\n", __func__, id);
547 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
548 kfree(hdr);
549 rc = -ENODEV;
550 goto open_done;
551 }
552
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600553 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 bam_ch[id].priv = priv;
555 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
556 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
557
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600558 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600559 if (!bam_is_connected) {
560 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600561 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600562 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600563 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600564 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600565
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
567 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
568 hdr->reserved = 0;
569 hdr->ch_id = id;
570 hdr->pkt_len = 0;
571 hdr->pad_len = 0;
572
573 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600574 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575
576open_done:
577 DBG("%s: opened ch %d\n", __func__, id);
578 return rc;
579}
580
581int msm_bam_dmux_close(uint32_t id)
582{
583 struct bam_mux_hdr *hdr;
584 unsigned long flags;
585 int rc;
586
587 if (id >= BAM_DMUX_NUM_CHANNELS)
588 return -EINVAL;
589 DBG("%s: closing ch %d\n", __func__, id);
590 if (!bam_mux_initialized)
591 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600593 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600594 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600595 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600596 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600597 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600598 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600599 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600600
Jeff Hugo061ce672011-10-21 17:15:32 -0600601 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600602 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 bam_ch[id].priv = NULL;
604 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
605 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
606
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600607 if (bam_ch_is_in_reset(id)) {
608 read_unlock(&ul_wakeup_lock);
609 bam_ch[id].status &= ~BAM_CH_IN_RESET;
610 return 0;
611 }
612
Jeff Hugobb5802f2011-11-02 17:10:29 -0600613 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614 if (hdr == NULL) {
615 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600616 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617 return -ENOMEM;
618 }
619 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
620 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
621 hdr->reserved = 0;
622 hdr->ch_id = id;
623 hdr->pkt_len = 0;
624 hdr->pad_len = 0;
625
626 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600627 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628
629 DBG("%s: closed ch %d\n", __func__, id);
630 return rc;
631}
632
Jeff Hugo949080a2011-08-30 11:58:56 -0600633static void rx_timer_work_func(struct work_struct *work)
634{
635 struct sps_iovec iov;
636 struct list_head *node;
637 struct rx_pkt_info *info;
638 int inactive_cycles = 0;
639 int ret;
640 struct sps_connect cur_rx_conn;
641
642 while (1) { /* timer loop */
643 ++inactive_cycles;
644 while (1) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600645 if (in_global_reset)
646 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600647 sps_get_iovec(bam_rx_pipe, &iov);
648 if (iov.addr == 0)
649 break;
650 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600651 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600652 node = bam_rx_pool.next;
653 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600654 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600655 info = container_of(node, struct rx_pkt_info,
656 list_node);
657 handle_bam_mux_cmd(&info->work);
658 }
659
660 if (inactive_cycles == POLLING_INACTIVITY) {
661 /*
662 * attempt to enable interrupts in this pipe
663 * if enabling interrupts fails, continue polling
664 */
665 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
666 if (ret) {
667 pr_err("%s: sps_get_config() failed, interrupts"
668 " not enabled\n", __func__);
669 queue_work(bam_mux_rx_workqueue,
670 &rx_timer_work);
671 return;
672 } else {
673 rx_register_event.options = SPS_O_EOT;
674 /* should check return value */
675 sps_register_event(bam_rx_pipe,
676 &rx_register_event);
677 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
678 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
679 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
680 if (ret) {
681 pr_err("%s: sps_set_config() failed, "
682 "interrupts not enabled\n",
683 __func__);
684 queue_work(bam_mux_rx_workqueue,
685 &rx_timer_work);
686 return;
687 }
688 polling_mode = 0;
689 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600690 if (in_global_reset)
691 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600692 /* handle race condition - missed packet? */
693 sps_get_iovec(bam_rx_pipe, &iov);
694 if (iov.addr == 0)
695 return;
696 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600697 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600698 node = bam_rx_pool.next;
699 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600700 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600701 info = container_of(node, struct rx_pkt_info,
702 list_node);
703 handle_bam_mux_cmd(&info->work);
704 return;
705 }
706
707 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
708 }
709}
710
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711static void bam_mux_tx_notify(struct sps_event_notify *notify)
712{
713 struct tx_pkt_info *pkt;
714
715 DBG("%s: event %d notified\n", __func__, notify->event_id);
716
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600717 if (in_global_reset)
718 return;
719
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 switch (notify->event_id) {
721 case SPS_EVENT_EOT:
722 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600723 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724 dma_unmap_single(NULL, pkt->dma_address,
725 pkt->skb->len,
726 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600727 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728 dma_unmap_single(NULL, pkt->dma_address,
729 pkt->len,
730 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600731 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732 break;
733 default:
734 pr_err("%s: recieved unexpected event id %d\n", __func__,
735 notify->event_id);
736 }
737}
738
Jeff Hugo33dbc002011-08-25 15:52:53 -0600739static void bam_mux_rx_notify(struct sps_event_notify *notify)
740{
Jeff Hugo949080a2011-08-30 11:58:56 -0600741 int ret;
742 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600743
744 DBG("%s: event %d notified\n", __func__, notify->event_id);
745
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600746 if (in_global_reset)
747 return;
748
Jeff Hugo33dbc002011-08-25 15:52:53 -0600749 switch (notify->event_id) {
750 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -0600751 /* attempt to disable interrupts in this pipe */
752 if (!polling_mode) {
753 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
754 if (ret) {
755 pr_err("%s: sps_get_config() failed, interrupts"
756 " not disabled\n", __func__);
757 break;
758 }
759 rx_register_event.options = 0;
760 ret = sps_register_event(bam_rx_pipe,
761 &rx_register_event);
762 if (ret) {
763 pr_err("%s: sps_register_event ret = %d\n",
764 __func__, ret);
765 break;
766 }
767 cur_rx_conn.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
768 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
769 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
770 if (ret) {
771 pr_err("%s: sps_set_config() failed, interrupts"
772 " not disabled\n", __func__);
773 break;
774 }
775 polling_mode = 1;
776 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
777 }
Jeff Hugo33dbc002011-08-25 15:52:53 -0600778 break;
779 default:
780 pr_err("%s: recieved unexpected event id %d\n", __func__,
781 notify->event_id);
782 }
783}
784
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785#ifdef CONFIG_DEBUG_FS
786
787static int debug_tbl(char *buf, int max)
788{
789 int i = 0;
790 int j;
791
792 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
793 i += scnprintf(buf + i, max - i,
794 "ch%02d local open=%s remote open=%s\n",
795 j, bam_ch_is_local_open(j) ? "Y" : "N",
796 bam_ch_is_remote_open(j) ? "Y" : "N");
797 }
798
799 return i;
800}
801
802#define DEBUG_BUFMAX 4096
803static char debug_buffer[DEBUG_BUFMAX];
804
805static ssize_t debug_read(struct file *file, char __user *buf,
806 size_t count, loff_t *ppos)
807{
808 int (*fill)(char *buf, int max) = file->private_data;
809 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
810 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
811}
812
813static int debug_open(struct inode *inode, struct file *file)
814{
815 file->private_data = inode->i_private;
816 return 0;
817}
818
819
820static const struct file_operations debug_ops = {
821 .read = debug_read,
822 .open = debug_open,
823};
824
825static void debug_create(const char *name, mode_t mode,
826 struct dentry *dent,
827 int (*fill)(char *buf, int max))
828{
829 debugfs_create_file(name, mode, dent, fill, &debug_ops);
830}
831
832#endif
833
Jeff Hugod98b1082011-10-24 10:30:23 -0600834static void notify_all(int event, unsigned long data)
835{
836 int i;
837
838 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
839 if (bam_ch_is_open(i))
840 bam_ch[i].notify(bam_ch[i].priv, event, data);
841 }
842}
843
844static void kickoff_ul_wakeup_func(struct work_struct *work)
845{
846 read_lock(&ul_wakeup_lock);
847 if (!bam_is_connected) {
848 read_unlock(&ul_wakeup_lock);
849 ul_wakeup();
850 read_lock(&ul_wakeup_lock);
851 ul_packet_written = 1;
852 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
853 }
854 read_unlock(&ul_wakeup_lock);
855}
856
857void msm_bam_dmux_kickoff_ul_wakeup(void)
858{
859 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
860}
861
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600862static void ul_timeout(struct work_struct *work)
863{
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600864 if (in_global_reset)
865 return;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600866 write_lock(&ul_wakeup_lock);
867 if (ul_packet_written) {
868 ul_packet_written = 0;
869 schedule_delayed_work(&ul_timeout_work,
870 msecs_to_jiffies(UL_TIMEOUT_DELAY));
871 } else {
872 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
873 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -0600874 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600875 }
876 write_unlock(&ul_wakeup_lock);
877}
878static void ul_wakeup(void)
879{
880 mutex_lock(&wakeup_lock);
881 if (bam_is_connected) { /* bam got connected before lock grabbed */
882 mutex_unlock(&wakeup_lock);
883 return;
884 }
885 INIT_COMPLETION(ul_wakeup_ack_completion);
886 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
887 wait_for_completion_interruptible_timeout(&ul_wakeup_ack_completion,
888 HZ);
889 wait_for_completion_interruptible_timeout(&bam_connection_completion,
890 HZ);
891
892 bam_is_connected = 1;
893 schedule_delayed_work(&ul_timeout_work,
894 msecs_to_jiffies(UL_TIMEOUT_DELAY));
895 mutex_unlock(&wakeup_lock);
896}
897
898static void reconnect_to_bam(void)
899{
900 int i;
901
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600902 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600903 vote_dfab();
904 i = sps_device_reset(a2_device_handle);
905 if (i)
906 pr_err("%s: device reset failed rc = %d\n", __func__, i);
907 i = sps_connect(bam_tx_pipe, &tx_connection);
908 if (i)
909 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
910 i = sps_connect(bam_rx_pipe, &rx_connection);
911 if (i)
912 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
913 i = sps_register_event(bam_tx_pipe, &tx_register_event);
914 if (i)
915 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
916 i = sps_register_event(bam_rx_pipe, &rx_register_event);
917 if (i)
918 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
919 for (i = 0; i < NUM_BUFFERS; ++i)
920 queue_rx();
921 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600922 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600923 complete_all(&bam_connection_completion);
924}
925
926static void disconnect_to_bam(void)
927{
928 struct list_head *node;
929 struct rx_pkt_info *info;
930
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600931 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600932 INIT_COMPLETION(bam_connection_completion);
933 sps_disconnect(bam_tx_pipe);
934 sps_disconnect(bam_rx_pipe);
935 unvote_dfab();
936 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
937 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
938 while (!list_empty(&bam_rx_pool)) {
939 node = bam_rx_pool.next;
940 list_del(node);
941 info = container_of(node, struct rx_pkt_info, list_node);
942 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
943 DMA_FROM_DEVICE);
944 dev_kfree_skb_any(info->skb);
945 kfree(info);
946 }
947}
948
949static void vote_dfab(void)
950{
951 int rc;
952
953 rc = clk_enable(dfab_clk);
954 if (rc)
955 pr_err("bam_dmux vote for dfab failed rc = %d\n", rc);
956}
957
958static void unvote_dfab(void)
959{
960 clk_disable(dfab_clk);
961}
962
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600963static int restart_notifier_cb(struct notifier_block *this,
964 unsigned long code,
965 void *data)
966{
967 int i;
968 struct list_head *node;
969 struct tx_pkt_info *info;
970 int temp_remote_status;
971
972 if (code != SUBSYS_AFTER_SHUTDOWN)
973 return NOTIFY_DONE;
974
975 in_global_reset = 1;
976 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
977 temp_remote_status = bam_ch_is_remote_open(i);
978 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
979 if (bam_ch_is_local_open(i))
980 bam_ch[i].status |= BAM_CH_IN_RESET;
981 if (temp_remote_status) {
982 platform_device_unregister(bam_ch[i].pdev);
983 bam_ch[i].pdev = platform_device_alloc(
984 bam_ch[i].name, 2);
985 }
986 }
987 /*cleanup UL*/
Jeff Hugoc9749932011-11-02 17:50:40 -0600988 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600989 while (!list_empty(&bam_tx_pool)) {
990 node = bam_tx_pool.next;
991 list_del(node);
992 info = container_of(node, struct tx_pkt_info,
993 list_node);
994 if (!info->is_cmd) {
995 dma_unmap_single(NULL, info->dma_address,
996 info->skb->len,
997 DMA_TO_DEVICE);
998 dev_kfree_skb_any(info->skb);
999 } else {
1000 dma_unmap_single(NULL, info->dma_address,
1001 info->len,
1002 DMA_TO_DEVICE);
1003 kfree(info->skb);
1004 }
1005 kfree(info);
1006 }
Jeff Hugoc9749932011-11-02 17:50:40 -06001007 spin_unlock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001008 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1009
1010 return NOTIFY_DONE;
1011}
1012
Jeff Hugoade1f842011-08-03 15:53:59 -06001013static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014{
1015 u32 h;
1016 dma_addr_t dma_addr;
1017 int ret;
1018 void *a2_virt_addr;
1019 int i;
1020
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001021 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022 /* init BAM */
1023 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1024 if (!a2_virt_addr) {
1025 pr_err("%s: ioremap failed\n", __func__);
1026 ret = -ENOMEM;
1027 goto register_bam_failed;
1028 }
1029 a2_props.phys_addr = A2_PHYS_BASE;
1030 a2_props.virt_addr = a2_virt_addr;
1031 a2_props.virt_size = A2_PHYS_SIZE;
1032 a2_props.irq = A2_BAM_IRQ;
1033 a2_props.num_pipes = A2_NUM_PIPES;
1034 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
1035 /* need to free on tear down */
1036 ret = sps_register_bam_device(&a2_props, &h);
1037 if (ret < 0) {
1038 pr_err("%s: register bam error %d\n", __func__, ret);
1039 goto register_bam_failed;
1040 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001041 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042
1043 bam_tx_pipe = sps_alloc_endpoint();
1044 if (bam_tx_pipe == NULL) {
1045 pr_err("%s: tx alloc endpoint failed\n", __func__);
1046 ret = -ENOMEM;
1047 goto register_bam_failed;
1048 }
1049 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1050 if (ret) {
1051 pr_err("%s: tx get config failed %d\n", __func__, ret);
1052 goto tx_get_config_failed;
1053 }
1054
1055 tx_connection.source = SPS_DEV_HANDLE_MEM;
1056 tx_connection.src_pipe_index = 0;
1057 tx_connection.destination = h;
1058 tx_connection.dest_pipe_index = 4;
1059 tx_connection.mode = SPS_MODE_DEST;
1060 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1061 tx_desc_mem_buf.size = 0x800; /* 2k */
1062 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1063 &dma_addr, 0);
1064 if (tx_desc_mem_buf.base == NULL) {
1065 pr_err("%s: tx memory alloc failed\n", __func__);
1066 ret = -ENOMEM;
1067 goto tx_mem_failed;
1068 }
1069 tx_desc_mem_buf.phys_base = dma_addr;
1070 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1071 tx_connection.desc = tx_desc_mem_buf;
1072 tx_connection.event_thresh = 0x10;
1073
1074 ret = sps_connect(bam_tx_pipe, &tx_connection);
1075 if (ret < 0) {
1076 pr_err("%s: tx connect error %d\n", __func__, ret);
1077 goto tx_connect_failed;
1078 }
1079
1080 bam_rx_pipe = sps_alloc_endpoint();
1081 if (bam_rx_pipe == NULL) {
1082 pr_err("%s: rx alloc endpoint failed\n", __func__);
1083 ret = -ENOMEM;
1084 goto tx_connect_failed;
1085 }
1086 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1087 if (ret) {
1088 pr_err("%s: rx get config failed %d\n", __func__, ret);
1089 goto rx_get_config_failed;
1090 }
1091
1092 rx_connection.source = h;
1093 rx_connection.src_pipe_index = 5;
1094 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1095 rx_connection.dest_pipe_index = 1;
1096 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001097 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1098 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099 rx_desc_mem_buf.size = 0x800; /* 2k */
1100 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1101 &dma_addr, 0);
1102 if (rx_desc_mem_buf.base == NULL) {
1103 pr_err("%s: rx memory alloc failed\n", __func__);
1104 ret = -ENOMEM;
1105 goto rx_mem_failed;
1106 }
1107 rx_desc_mem_buf.phys_base = dma_addr;
1108 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1109 rx_connection.desc = rx_desc_mem_buf;
1110 rx_connection.event_thresh = 0x10;
1111
1112 ret = sps_connect(bam_rx_pipe, &rx_connection);
1113 if (ret < 0) {
1114 pr_err("%s: rx connect error %d\n", __func__, ret);
1115 goto rx_connect_failed;
1116 }
1117
1118 tx_register_event.options = SPS_O_EOT;
1119 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1120 tx_register_event.xfer_done = NULL;
1121 tx_register_event.callback = bam_mux_tx_notify;
1122 tx_register_event.user = NULL;
1123 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1124 if (ret < 0) {
1125 pr_err("%s: tx register event error %d\n", __func__, ret);
1126 goto rx_event_reg_failed;
1127 }
1128
Jeff Hugo33dbc002011-08-25 15:52:53 -06001129 rx_register_event.options = SPS_O_EOT;
1130 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1131 rx_register_event.xfer_done = NULL;
1132 rx_register_event.callback = bam_mux_rx_notify;
1133 rx_register_event.user = NULL;
1134 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1135 if (ret < 0) {
1136 pr_err("%s: tx register event error %d\n", __func__, ret);
1137 goto rx_event_reg_failed;
1138 }
1139
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140 bam_mux_initialized = 1;
1141 for (i = 0; i < NUM_BUFFERS; ++i)
1142 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001143 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001144 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001145 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001146 return;
1147
1148rx_event_reg_failed:
1149 sps_disconnect(bam_rx_pipe);
1150rx_connect_failed:
1151 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1152 rx_desc_mem_buf.phys_base);
1153rx_mem_failed:
1154 sps_disconnect(bam_tx_pipe);
1155rx_get_config_failed:
1156 sps_free_endpoint(bam_rx_pipe);
1157tx_connect_failed:
1158 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1159 tx_desc_mem_buf.phys_base);
1160tx_get_config_failed:
1161 sps_free_endpoint(bam_tx_pipe);
1162tx_mem_failed:
1163 sps_deregister_bam_device(h);
1164register_bam_failed:
1165 /*destroy_workqueue(bam_mux_workqueue);*/
1166 /*return ret;*/
1167 return;
1168}
Jeff Hugoade1f842011-08-03 15:53:59 -06001169
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001170static void toggle_apps_ack(void)
1171{
1172 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
1173 smsm_change_state(SMSM_APPS_STATE,
1174 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1175 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1176 clear_bit = ~clear_bit;
1177}
1178
Jeff Hugoade1f842011-08-03 15:53:59 -06001179static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1180{
1181 DBG("%s: smsm activity\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001182 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL)
1183 reconnect_to_bam();
1184 else if (bam_mux_initialized && !(new_state & SMSM_A2_POWER_CONTROL))
1185 disconnect_to_bam();
Jeff Hugoade1f842011-08-03 15:53:59 -06001186 else if (new_state & SMSM_A2_POWER_CONTROL)
1187 bam_init();
1188 else
1189 pr_err("%s: unsupported state change\n", __func__);
1190
1191}
1192
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001193static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1194 uint32_t new_state)
1195{
1196 complete_all(&ul_wakeup_ack_completion);
1197}
1198
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001199static int bam_dmux_probe(struct platform_device *pdev)
1200{
1201 int rc;
1202
1203 DBG("%s probe called\n", __func__);
1204 if (bam_mux_initialized)
1205 return 0;
1206
Stephen Boyd1c51a492011-10-26 12:11:47 -07001207 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001208 if (IS_ERR(dfab_clk)) {
1209 pr_err("%s: did not get dfab clock\n", __func__);
1210 return -EFAULT;
1211 }
1212
1213 rc = clk_set_rate(dfab_clk, 64000000);
1214 if (rc)
1215 pr_err("%s: unable to set dfab clock rate\n", __func__);
1216
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001217 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1218 if (!bam_mux_rx_workqueue)
1219 return -ENOMEM;
1220
1221 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1222 if (!bam_mux_tx_workqueue) {
1223 destroy_workqueue(bam_mux_rx_workqueue);
1224 return -ENOMEM;
1225 }
1226
Jeff Hugo7960abd2011-08-02 15:39:38 -06001227 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001229 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1230 "bam_dmux_ch_%d", rc);
1231 /* bus 2, ie a2 stream 2 */
1232 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1233 if (!bam_ch[rc].pdev) {
1234 pr_err("%s: platform device alloc failed\n", __func__);
1235 destroy_workqueue(bam_mux_rx_workqueue);
1236 destroy_workqueue(bam_mux_tx_workqueue);
1237 return -ENOMEM;
1238 }
1239 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001240
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001241 init_completion(&ul_wakeup_ack_completion);
1242 init_completion(&bam_connection_completion);
1243 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
1244
Jeff Hugoade1f842011-08-03 15:53:59 -06001245 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1246 bam_dmux_smsm_cb, NULL);
1247
1248 if (rc) {
1249 destroy_workqueue(bam_mux_rx_workqueue);
1250 destroy_workqueue(bam_mux_tx_workqueue);
1251 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1252 return -ENOMEM;
1253 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001254
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001255 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1256 bam_dmux_smsm_ack_cb, NULL);
1257
1258 if (rc) {
1259 destroy_workqueue(bam_mux_rx_workqueue);
1260 destroy_workqueue(bam_mux_tx_workqueue);
1261 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1262 SMSM_A2_POWER_CONTROL,
1263 bam_dmux_smsm_cb, NULL);
1264 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1265 rc);
1266 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1267 platform_device_put(bam_ch[rc].pdev);
1268 return -ENOMEM;
1269 }
1270
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271 return 0;
1272}
1273
1274static struct platform_driver bam_dmux_driver = {
1275 .probe = bam_dmux_probe,
1276 .driver = {
1277 .name = "BAM_RMNT",
1278 .owner = THIS_MODULE,
1279 },
1280};
1281
1282static int __init bam_dmux_init(void)
1283{
1284#ifdef CONFIG_DEBUG_FS
1285 struct dentry *dent;
1286
1287 dent = debugfs_create_dir("bam_dmux", 0);
1288 if (!IS_ERR(dent))
1289 debug_create("tbl", 0444, dent, debug_tbl);
1290#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001291 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001292 return platform_driver_register(&bam_dmux_driver);
1293}
1294
Jeff Hugoade1f842011-08-03 15:53:59 -06001295late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001296MODULE_DESCRIPTION("MSM BAM DMUX");
1297MODULE_LICENSE("GPL v2");