blob: c70e1b52cd64e885584a6290b2a15e49d49b3b31 [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 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700295
296 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
297 pr_err("%s: dropping invalid LCID %d reserved %d cmd %d"
298 " pad %d ch %d len %d\n", __func__,
299 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
300 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
301 dev_kfree_skb_any(rx_skb);
302 queue_rx();
303 return;
304 }
305
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 switch (rx_hdr->cmd) {
307 case BAM_MUX_HDR_CMD_DATA:
308 DBG_INC_READ_CNT(rx_hdr->pkt_len);
309 bam_mux_process_data(rx_skb);
310 break;
311 case BAM_MUX_HDR_CMD_OPEN:
312 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
313 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
314 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700315 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600316 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
317 if (ret)
318 pr_err("%s: platform_device_add() error: %d\n",
319 __func__, ret);
Eric Holmberge779dba2011-11-04 18:22:01 -0600320 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321 break;
322 case BAM_MUX_HDR_CMD_CLOSE:
323 /* probably should drop pending write */
324 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
325 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
326 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600328 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
329 bam_ch[rx_hdr->ch_id].pdev =
330 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
331 if (!bam_ch[rx_hdr->ch_id].pdev)
332 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberge779dba2011-11-04 18:22:01 -0600333 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334 break;
335 default:
336 pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d"
337 " pad %d ch %d len %d\n", __func__,
338 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
339 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
340 dev_kfree_skb_any(rx_skb);
341 queue_rx();
342 return;
343 }
344}
345
346static int bam_mux_write_cmd(void *data, uint32_t len)
347{
348 int rc;
349 struct tx_pkt_info *pkt;
350 dma_addr_t dma_address;
351
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600352 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 if (pkt == NULL) {
354 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
355 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356 return rc;
357 }
358
359 dma_address = dma_map_single(NULL, data, len,
360 DMA_TO_DEVICE);
361 if (!dma_address) {
362 pr_err("%s: dma_map_single() failed\n", __func__);
363 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364 return rc;
365 }
366 pkt->skb = (struct sk_buff *)(data);
367 pkt->len = len;
368 pkt->dma_address = dma_address;
369 pkt->is_cmd = 1;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600370 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugoc9749932011-11-02 17:50:40 -0600371 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600372 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600373 spin_unlock(&bam_tx_pool_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700374 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
375 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600376 if (rc) {
377 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
378 spin_lock(&bam_tx_pool_spinlock);
379 list_del(&pkt->list_node);
380 spin_unlock(&bam_tx_pool_spinlock);
381 kfree(pkt);
382 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600384 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385 return rc;
386}
387
388static void bam_mux_write_done(struct work_struct *work)
389{
390 struct sk_buff *skb;
391 struct bam_mux_hdr *hdr;
392 struct tx_pkt_info *info;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600393 unsigned long event_data;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600394 struct list_head *node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600396 if (in_global_reset)
397 return;
Jeff Hugoc9749932011-11-02 17:50:40 -0600398 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600399 node = bam_tx_pool.next;
400 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600401 spin_unlock(&bam_tx_pool_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402 info = container_of(work, struct tx_pkt_info, work);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600403 if (info->is_cmd) {
404 kfree(info->skb);
405 kfree(info);
406 return;
407 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 skb = info->skb;
409 kfree(info);
410 hdr = (struct bam_mux_hdr *)skb->data;
411 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600412 event_data = (unsigned long)(skb);
413 if (bam_ch[hdr->ch_id].notify)
414 bam_ch[hdr->ch_id].notify(
415 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
416 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417 else
418 dev_kfree_skb_any(skb);
419}
420
421int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
422{
423 int rc = 0;
424 struct bam_mux_hdr *hdr;
425 unsigned long flags;
426 struct sk_buff *new_skb = NULL;
427 dma_addr_t dma_address;
428 struct tx_pkt_info *pkt;
429
430 if (id >= BAM_DMUX_NUM_CHANNELS)
431 return -EINVAL;
432 if (!skb)
433 return -EINVAL;
434 if (!bam_mux_initialized)
435 return -ENODEV;
436
437 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
438 spin_lock_irqsave(&bam_ch[id].lock, flags);
439 if (!bam_ch_is_open(id)) {
440 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
441 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
442 return -ENODEV;
443 }
444 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
445
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600446 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600447 if (!bam_is_connected) {
448 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600449 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600450 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600451 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600452 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600453
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 /* if skb do not have any tailroom for padding,
455 copy the skb into a new expanded skb */
456 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
457 /* revisit, probably dev_alloc_skb and memcpy is effecient */
458 new_skb = skb_copy_expand(skb, skb_headroom(skb),
459 4 - (skb->len & 0x3), GFP_ATOMIC);
460 if (new_skb == NULL) {
461 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600462 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463 }
464 dev_kfree_skb_any(skb);
465 skb = new_skb;
466 DBG_INC_WRITE_CPY(skb->len);
467 }
468
469 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
470
471 /* caller should allocate for hdr and padding
472 hdr is fine, padding is tricky */
473 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
474 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
475 hdr->reserved = 0;
476 hdr->ch_id = id;
477 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
478 if (skb->len & 0x3)
479 skb_put(skb, 4 - (skb->len & 0x3));
480
481 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
482
483 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
484 __func__, skb->data, skb->tail, skb->len,
485 hdr->pkt_len, hdr->pad_len);
486
487 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
488 if (pkt == NULL) {
489 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600490 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491 }
492
493 dma_address = dma_map_single(NULL, skb->data, skb->len,
494 DMA_TO_DEVICE);
495 if (!dma_address) {
496 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600497 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 }
499 pkt->skb = skb;
500 pkt->dma_address = dma_address;
501 pkt->is_cmd = 0;
502 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugoc9749932011-11-02 17:50:40 -0600503 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600504 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600505 spin_unlock(&bam_tx_pool_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
507 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600508 if (rc) {
509 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
510 spin_lock(&bam_tx_pool_spinlock);
511 list_del(&pkt->list_node);
512 spin_unlock(&bam_tx_pool_spinlock);
513 kfree(pkt);
514 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600515 ul_packet_written = 1;
516 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600518
519write_fail3:
520 kfree(pkt);
521write_fail2:
522 if (new_skb)
523 dev_kfree_skb_any(new_skb);
524write_fail:
525 read_unlock(&ul_wakeup_lock);
526 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527}
528
529int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600530 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531{
532 struct bam_mux_hdr *hdr;
533 unsigned long flags;
534 int rc = 0;
535
536 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700537 if (!bam_mux_initialized) {
538 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700540 }
541 if (id >= BAM_DMUX_NUM_CHANNELS) {
542 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700544 }
545 if (notify == NULL) {
546 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600547 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700548 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549
550 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
551 if (hdr == NULL) {
552 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
553 return -ENOMEM;
554 }
555 spin_lock_irqsave(&bam_ch[id].lock, flags);
556 if (bam_ch_is_open(id)) {
557 DBG("%s: Already opened %d\n", __func__, id);
558 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
559 kfree(hdr);
560 goto open_done;
561 }
562 if (!bam_ch_is_remote_open(id)) {
563 DBG("%s: Remote not open; ch: %d\n", __func__, id);
564 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
565 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700566 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567 }
568
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600569 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 bam_ch[id].priv = priv;
571 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
572 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
573
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600574 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600575 if (!bam_is_connected) {
576 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600577 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600578 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600579 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600580 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600581
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
583 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
584 hdr->reserved = 0;
585 hdr->ch_id = id;
586 hdr->pkt_len = 0;
587 hdr->pad_len = 0;
588
589 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600590 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591
592open_done:
593 DBG("%s: opened ch %d\n", __func__, id);
594 return rc;
595}
596
597int msm_bam_dmux_close(uint32_t id)
598{
599 struct bam_mux_hdr *hdr;
600 unsigned long flags;
601 int rc;
602
603 if (id >= BAM_DMUX_NUM_CHANNELS)
604 return -EINVAL;
605 DBG("%s: closing ch %d\n", __func__, id);
606 if (!bam_mux_initialized)
607 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600609 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600610 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600611 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600612 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600613 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600614 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600615 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600616
Jeff Hugo061ce672011-10-21 17:15:32 -0600617 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600618 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619 bam_ch[id].priv = NULL;
620 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
621 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
622
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600623 if (bam_ch_is_in_reset(id)) {
624 read_unlock(&ul_wakeup_lock);
625 bam_ch[id].status &= ~BAM_CH_IN_RESET;
626 return 0;
627 }
628
Jeff Hugobb5802f2011-11-02 17:10:29 -0600629 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630 if (hdr == NULL) {
631 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600632 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633 return -ENOMEM;
634 }
635 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
636 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
637 hdr->reserved = 0;
638 hdr->ch_id = id;
639 hdr->pkt_len = 0;
640 hdr->pad_len = 0;
641
642 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600643 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644
645 DBG("%s: closed ch %d\n", __func__, id);
646 return rc;
647}
648
Jeff Hugo949080a2011-08-30 11:58:56 -0600649static void rx_timer_work_func(struct work_struct *work)
650{
651 struct sps_iovec iov;
652 struct list_head *node;
653 struct rx_pkt_info *info;
654 int inactive_cycles = 0;
655 int ret;
656 struct sps_connect cur_rx_conn;
657
658 while (1) { /* timer loop */
659 ++inactive_cycles;
660 while (1) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600661 if (in_global_reset)
662 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600663 sps_get_iovec(bam_rx_pipe, &iov);
664 if (iov.addr == 0)
665 break;
666 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600667 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600668 node = bam_rx_pool.next;
669 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600670 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600671 info = container_of(node, struct rx_pkt_info,
672 list_node);
673 handle_bam_mux_cmd(&info->work);
674 }
675
676 if (inactive_cycles == POLLING_INACTIVITY) {
677 /*
678 * attempt to enable interrupts in this pipe
679 * if enabling interrupts fails, continue polling
680 */
681 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
682 if (ret) {
683 pr_err("%s: sps_get_config() failed, interrupts"
684 " not enabled\n", __func__);
685 queue_work(bam_mux_rx_workqueue,
686 &rx_timer_work);
687 return;
688 } else {
689 rx_register_event.options = SPS_O_EOT;
690 /* should check return value */
691 sps_register_event(bam_rx_pipe,
692 &rx_register_event);
693 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
694 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
695 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
696 if (ret) {
697 pr_err("%s: sps_set_config() failed, "
698 "interrupts not enabled\n",
699 __func__);
700 queue_work(bam_mux_rx_workqueue,
701 &rx_timer_work);
702 return;
703 }
704 polling_mode = 0;
705 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600706 if (in_global_reset)
707 return;
Jeff Hugo949080a2011-08-30 11:58:56 -0600708 /* handle race condition - missed packet? */
709 sps_get_iovec(bam_rx_pipe, &iov);
710 if (iov.addr == 0)
711 return;
712 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600713 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600714 node = bam_rx_pool.next;
715 list_del(node);
Jeff Hugoc9749932011-11-02 17:50:40 -0600716 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600717 info = container_of(node, struct rx_pkt_info,
718 list_node);
719 handle_bam_mux_cmd(&info->work);
720 return;
721 }
722
723 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
724 }
725}
726
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727static void bam_mux_tx_notify(struct sps_event_notify *notify)
728{
729 struct tx_pkt_info *pkt;
730
731 DBG("%s: event %d notified\n", __func__, notify->event_id);
732
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600733 if (in_global_reset)
734 return;
735
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736 switch (notify->event_id) {
737 case SPS_EVENT_EOT:
738 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600739 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 dma_unmap_single(NULL, pkt->dma_address,
741 pkt->skb->len,
742 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600743 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 dma_unmap_single(NULL, pkt->dma_address,
745 pkt->len,
746 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600747 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 break;
749 default:
750 pr_err("%s: recieved unexpected event id %d\n", __func__,
751 notify->event_id);
752 }
753}
754
Jeff Hugo33dbc002011-08-25 15:52:53 -0600755static void bam_mux_rx_notify(struct sps_event_notify *notify)
756{
Jeff Hugo949080a2011-08-30 11:58:56 -0600757 int ret;
758 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600759
760 DBG("%s: event %d notified\n", __func__, notify->event_id);
761
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600762 if (in_global_reset)
763 return;
764
Jeff Hugo33dbc002011-08-25 15:52:53 -0600765 switch (notify->event_id) {
766 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -0600767 /* attempt to disable interrupts in this pipe */
768 if (!polling_mode) {
769 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
770 if (ret) {
771 pr_err("%s: sps_get_config() failed, interrupts"
772 " not disabled\n", __func__);
773 break;
774 }
775 rx_register_event.options = 0;
776 ret = sps_register_event(bam_rx_pipe,
777 &rx_register_event);
778 if (ret) {
779 pr_err("%s: sps_register_event ret = %d\n",
780 __func__, ret);
781 break;
782 }
783 cur_rx_conn.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
784 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
785 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
786 if (ret) {
787 pr_err("%s: sps_set_config() failed, interrupts"
788 " not disabled\n", __func__);
789 break;
790 }
791 polling_mode = 1;
792 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
793 }
Jeff Hugo33dbc002011-08-25 15:52:53 -0600794 break;
795 default:
796 pr_err("%s: recieved unexpected event id %d\n", __func__,
797 notify->event_id);
798 }
799}
800
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801#ifdef CONFIG_DEBUG_FS
802
803static int debug_tbl(char *buf, int max)
804{
805 int i = 0;
806 int j;
807
808 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
809 i += scnprintf(buf + i, max - i,
810 "ch%02d local open=%s remote open=%s\n",
811 j, bam_ch_is_local_open(j) ? "Y" : "N",
812 bam_ch_is_remote_open(j) ? "Y" : "N");
813 }
814
815 return i;
816}
817
818#define DEBUG_BUFMAX 4096
819static char debug_buffer[DEBUG_BUFMAX];
820
821static ssize_t debug_read(struct file *file, char __user *buf,
822 size_t count, loff_t *ppos)
823{
824 int (*fill)(char *buf, int max) = file->private_data;
825 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
826 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
827}
828
829static int debug_open(struct inode *inode, struct file *file)
830{
831 file->private_data = inode->i_private;
832 return 0;
833}
834
835
836static const struct file_operations debug_ops = {
837 .read = debug_read,
838 .open = debug_open,
839};
840
841static void debug_create(const char *name, mode_t mode,
842 struct dentry *dent,
843 int (*fill)(char *buf, int max))
844{
845 debugfs_create_file(name, mode, dent, fill, &debug_ops);
846}
847
848#endif
849
Jeff Hugod98b1082011-10-24 10:30:23 -0600850static void notify_all(int event, unsigned long data)
851{
852 int i;
853
854 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
855 if (bam_ch_is_open(i))
856 bam_ch[i].notify(bam_ch[i].priv, event, data);
857 }
858}
859
860static void kickoff_ul_wakeup_func(struct work_struct *work)
861{
862 read_lock(&ul_wakeup_lock);
863 if (!bam_is_connected) {
864 read_unlock(&ul_wakeup_lock);
865 ul_wakeup();
866 read_lock(&ul_wakeup_lock);
867 ul_packet_written = 1;
868 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
869 }
870 read_unlock(&ul_wakeup_lock);
871}
872
873void msm_bam_dmux_kickoff_ul_wakeup(void)
874{
875 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
876}
877
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600878static void ul_timeout(struct work_struct *work)
879{
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600880 if (in_global_reset)
881 return;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600882 write_lock(&ul_wakeup_lock);
883 if (ul_packet_written) {
884 ul_packet_written = 0;
885 schedule_delayed_work(&ul_timeout_work,
886 msecs_to_jiffies(UL_TIMEOUT_DELAY));
887 } else {
888 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
889 bam_is_connected = 0;
Jeff Hugod98b1082011-10-24 10:30:23 -0600890 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600891 }
892 write_unlock(&ul_wakeup_lock);
893}
894static void ul_wakeup(void)
895{
896 mutex_lock(&wakeup_lock);
897 if (bam_is_connected) { /* bam got connected before lock grabbed */
898 mutex_unlock(&wakeup_lock);
899 return;
900 }
901 INIT_COMPLETION(ul_wakeup_ack_completion);
902 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
903 wait_for_completion_interruptible_timeout(&ul_wakeup_ack_completion,
904 HZ);
905 wait_for_completion_interruptible_timeout(&bam_connection_completion,
906 HZ);
907
908 bam_is_connected = 1;
909 schedule_delayed_work(&ul_timeout_work,
910 msecs_to_jiffies(UL_TIMEOUT_DELAY));
911 mutex_unlock(&wakeup_lock);
912}
913
914static void reconnect_to_bam(void)
915{
916 int i;
917
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600918 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600919 vote_dfab();
920 i = sps_device_reset(a2_device_handle);
921 if (i)
922 pr_err("%s: device reset failed rc = %d\n", __func__, i);
923 i = sps_connect(bam_tx_pipe, &tx_connection);
924 if (i)
925 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
926 i = sps_connect(bam_rx_pipe, &rx_connection);
927 if (i)
928 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
929 i = sps_register_event(bam_tx_pipe, &tx_register_event);
930 if (i)
931 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
932 i = sps_register_event(bam_rx_pipe, &rx_register_event);
933 if (i)
934 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
935 for (i = 0; i < NUM_BUFFERS; ++i)
936 queue_rx();
937 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600938 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600939 complete_all(&bam_connection_completion);
940}
941
942static void disconnect_to_bam(void)
943{
944 struct list_head *node;
945 struct rx_pkt_info *info;
946
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600947 bam_connection_is_active = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600948 INIT_COMPLETION(bam_connection_completion);
949 sps_disconnect(bam_tx_pipe);
950 sps_disconnect(bam_rx_pipe);
951 unvote_dfab();
952 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
953 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
954 while (!list_empty(&bam_rx_pool)) {
955 node = bam_rx_pool.next;
956 list_del(node);
957 info = container_of(node, struct rx_pkt_info, list_node);
958 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
959 DMA_FROM_DEVICE);
960 dev_kfree_skb_any(info->skb);
961 kfree(info);
962 }
963}
964
965static void vote_dfab(void)
966{
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600967}
968
969static void unvote_dfab(void)
970{
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600971}
972
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600973static int restart_notifier_cb(struct notifier_block *this,
974 unsigned long code,
975 void *data)
976{
977 int i;
978 struct list_head *node;
979 struct tx_pkt_info *info;
980 int temp_remote_status;
981
982 if (code != SUBSYS_AFTER_SHUTDOWN)
983 return NOTIFY_DONE;
984
985 in_global_reset = 1;
986 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
987 temp_remote_status = bam_ch_is_remote_open(i);
988 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
989 if (bam_ch_is_local_open(i))
990 bam_ch[i].status |= BAM_CH_IN_RESET;
991 if (temp_remote_status) {
992 platform_device_unregister(bam_ch[i].pdev);
993 bam_ch[i].pdev = platform_device_alloc(
994 bam_ch[i].name, 2);
995 }
996 }
997 /*cleanup UL*/
Jeff Hugoc9749932011-11-02 17:50:40 -0600998 spin_lock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600999 while (!list_empty(&bam_tx_pool)) {
1000 node = bam_tx_pool.next;
1001 list_del(node);
1002 info = container_of(node, struct tx_pkt_info,
1003 list_node);
1004 if (!info->is_cmd) {
1005 dma_unmap_single(NULL, info->dma_address,
1006 info->skb->len,
1007 DMA_TO_DEVICE);
1008 dev_kfree_skb_any(info->skb);
1009 } else {
1010 dma_unmap_single(NULL, info->dma_address,
1011 info->len,
1012 DMA_TO_DEVICE);
1013 kfree(info->skb);
1014 }
1015 kfree(info);
1016 }
Jeff Hugoc9749932011-11-02 17:50:40 -06001017 spin_unlock(&bam_tx_pool_spinlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001018 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1019
1020 return NOTIFY_DONE;
1021}
1022
Jeff Hugoade1f842011-08-03 15:53:59 -06001023static void bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024{
1025 u32 h;
1026 dma_addr_t dma_addr;
1027 int ret;
1028 void *a2_virt_addr;
1029 int i;
1030
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001031 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032 /* init BAM */
1033 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1034 if (!a2_virt_addr) {
1035 pr_err("%s: ioremap failed\n", __func__);
1036 ret = -ENOMEM;
1037 goto register_bam_failed;
1038 }
1039 a2_props.phys_addr = A2_PHYS_BASE;
1040 a2_props.virt_addr = a2_virt_addr;
1041 a2_props.virt_size = A2_PHYS_SIZE;
1042 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001043 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044 a2_props.num_pipes = A2_NUM_PIPES;
1045 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
1046 /* need to free on tear down */
1047 ret = sps_register_bam_device(&a2_props, &h);
1048 if (ret < 0) {
1049 pr_err("%s: register bam error %d\n", __func__, ret);
1050 goto register_bam_failed;
1051 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001052 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001053
1054 bam_tx_pipe = sps_alloc_endpoint();
1055 if (bam_tx_pipe == NULL) {
1056 pr_err("%s: tx alloc endpoint failed\n", __func__);
1057 ret = -ENOMEM;
1058 goto register_bam_failed;
1059 }
1060 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1061 if (ret) {
1062 pr_err("%s: tx get config failed %d\n", __func__, ret);
1063 goto tx_get_config_failed;
1064 }
1065
1066 tx_connection.source = SPS_DEV_HANDLE_MEM;
1067 tx_connection.src_pipe_index = 0;
1068 tx_connection.destination = h;
1069 tx_connection.dest_pipe_index = 4;
1070 tx_connection.mode = SPS_MODE_DEST;
1071 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1072 tx_desc_mem_buf.size = 0x800; /* 2k */
1073 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1074 &dma_addr, 0);
1075 if (tx_desc_mem_buf.base == NULL) {
1076 pr_err("%s: tx memory alloc failed\n", __func__);
1077 ret = -ENOMEM;
1078 goto tx_mem_failed;
1079 }
1080 tx_desc_mem_buf.phys_base = dma_addr;
1081 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1082 tx_connection.desc = tx_desc_mem_buf;
1083 tx_connection.event_thresh = 0x10;
1084
1085 ret = sps_connect(bam_tx_pipe, &tx_connection);
1086 if (ret < 0) {
1087 pr_err("%s: tx connect error %d\n", __func__, ret);
1088 goto tx_connect_failed;
1089 }
1090
1091 bam_rx_pipe = sps_alloc_endpoint();
1092 if (bam_rx_pipe == NULL) {
1093 pr_err("%s: rx alloc endpoint failed\n", __func__);
1094 ret = -ENOMEM;
1095 goto tx_connect_failed;
1096 }
1097 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1098 if (ret) {
1099 pr_err("%s: rx get config failed %d\n", __func__, ret);
1100 goto rx_get_config_failed;
1101 }
1102
1103 rx_connection.source = h;
1104 rx_connection.src_pipe_index = 5;
1105 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1106 rx_connection.dest_pipe_index = 1;
1107 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001108 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1109 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110 rx_desc_mem_buf.size = 0x800; /* 2k */
1111 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1112 &dma_addr, 0);
1113 if (rx_desc_mem_buf.base == NULL) {
1114 pr_err("%s: rx memory alloc failed\n", __func__);
1115 ret = -ENOMEM;
1116 goto rx_mem_failed;
1117 }
1118 rx_desc_mem_buf.phys_base = dma_addr;
1119 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1120 rx_connection.desc = rx_desc_mem_buf;
1121 rx_connection.event_thresh = 0x10;
1122
1123 ret = sps_connect(bam_rx_pipe, &rx_connection);
1124 if (ret < 0) {
1125 pr_err("%s: rx connect error %d\n", __func__, ret);
1126 goto rx_connect_failed;
1127 }
1128
1129 tx_register_event.options = SPS_O_EOT;
1130 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1131 tx_register_event.xfer_done = NULL;
1132 tx_register_event.callback = bam_mux_tx_notify;
1133 tx_register_event.user = NULL;
1134 ret = sps_register_event(bam_tx_pipe, &tx_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
Jeff Hugo33dbc002011-08-25 15:52:53 -06001140 rx_register_event.options = SPS_O_EOT;
1141 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1142 rx_register_event.xfer_done = NULL;
1143 rx_register_event.callback = bam_mux_rx_notify;
1144 rx_register_event.user = NULL;
1145 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1146 if (ret < 0) {
1147 pr_err("%s: tx register event error %d\n", __func__, ret);
1148 goto rx_event_reg_failed;
1149 }
1150
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001151 bam_mux_initialized = 1;
1152 for (i = 0; i < NUM_BUFFERS; ++i)
1153 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001154 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001155 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001156 complete_all(&bam_connection_completion);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157 return;
1158
1159rx_event_reg_failed:
1160 sps_disconnect(bam_rx_pipe);
1161rx_connect_failed:
1162 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1163 rx_desc_mem_buf.phys_base);
1164rx_mem_failed:
1165 sps_disconnect(bam_tx_pipe);
1166rx_get_config_failed:
1167 sps_free_endpoint(bam_rx_pipe);
1168tx_connect_failed:
1169 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1170 tx_desc_mem_buf.phys_base);
1171tx_get_config_failed:
1172 sps_free_endpoint(bam_tx_pipe);
1173tx_mem_failed:
1174 sps_deregister_bam_device(h);
1175register_bam_failed:
1176 /*destroy_workqueue(bam_mux_workqueue);*/
1177 /*return ret;*/
1178 return;
1179}
Jeff Hugoade1f842011-08-03 15:53:59 -06001180
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001181static void toggle_apps_ack(void)
1182{
1183 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
1184 smsm_change_state(SMSM_APPS_STATE,
1185 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1186 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1187 clear_bit = ~clear_bit;
1188}
1189
Jeff Hugoade1f842011-08-03 15:53:59 -06001190static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1191{
1192 DBG("%s: smsm activity\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001193 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL)
1194 reconnect_to_bam();
1195 else if (bam_mux_initialized && !(new_state & SMSM_A2_POWER_CONTROL))
1196 disconnect_to_bam();
Jeff Hugoade1f842011-08-03 15:53:59 -06001197 else if (new_state & SMSM_A2_POWER_CONTROL)
1198 bam_init();
1199 else
1200 pr_err("%s: unsupported state change\n", __func__);
1201
1202}
1203
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001204static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1205 uint32_t new_state)
1206{
1207 complete_all(&ul_wakeup_ack_completion);
1208}
1209
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210static int bam_dmux_probe(struct platform_device *pdev)
1211{
1212 int rc;
1213
1214 DBG("%s probe called\n", __func__);
1215 if (bam_mux_initialized)
1216 return 0;
1217
Stephen Boyd1c51a492011-10-26 12:11:47 -07001218 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001219 if (IS_ERR(dfab_clk)) {
1220 pr_err("%s: did not get dfab clock\n", __func__);
1221 return -EFAULT;
1222 }
1223
1224 rc = clk_set_rate(dfab_clk, 64000000);
1225 if (rc)
1226 pr_err("%s: unable to set dfab clock rate\n", __func__);
1227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1229 if (!bam_mux_rx_workqueue)
1230 return -ENOMEM;
1231
1232 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1233 if (!bam_mux_tx_workqueue) {
1234 destroy_workqueue(bam_mux_rx_workqueue);
1235 return -ENOMEM;
1236 }
1237
Jeff Hugo7960abd2011-08-02 15:39:38 -06001238 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001239 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001240 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1241 "bam_dmux_ch_%d", rc);
1242 /* bus 2, ie a2 stream 2 */
1243 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1244 if (!bam_ch[rc].pdev) {
1245 pr_err("%s: platform device alloc failed\n", __func__);
1246 destroy_workqueue(bam_mux_rx_workqueue);
1247 destroy_workqueue(bam_mux_tx_workqueue);
1248 return -ENOMEM;
1249 }
1250 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001251
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001252 init_completion(&ul_wakeup_ack_completion);
1253 init_completion(&bam_connection_completion);
1254 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
1255
Jeff Hugoade1f842011-08-03 15:53:59 -06001256 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1257 bam_dmux_smsm_cb, NULL);
1258
1259 if (rc) {
1260 destroy_workqueue(bam_mux_rx_workqueue);
1261 destroy_workqueue(bam_mux_tx_workqueue);
1262 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1263 return -ENOMEM;
1264 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001265
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001266 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1267 bam_dmux_smsm_ack_cb, NULL);
1268
1269 if (rc) {
1270 destroy_workqueue(bam_mux_rx_workqueue);
1271 destroy_workqueue(bam_mux_tx_workqueue);
1272 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1273 SMSM_A2_POWER_CONTROL,
1274 bam_dmux_smsm_cb, NULL);
1275 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1276 rc);
1277 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1278 platform_device_put(bam_ch[rc].pdev);
1279 return -ENOMEM;
1280 }
1281
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07001282 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
1283 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
1284
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001285 return 0;
1286}
1287
1288static struct platform_driver bam_dmux_driver = {
1289 .probe = bam_dmux_probe,
1290 .driver = {
1291 .name = "BAM_RMNT",
1292 .owner = THIS_MODULE,
1293 },
1294};
1295
1296static int __init bam_dmux_init(void)
1297{
1298#ifdef CONFIG_DEBUG_FS
1299 struct dentry *dent;
1300
1301 dent = debugfs_create_dir("bam_dmux", 0);
1302 if (!IS_ERR(dent))
1303 debug_create("tbl", 0444, dent, debug_tbl);
1304#endif
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001305 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306 return platform_driver_register(&bam_dmux_driver);
1307}
1308
Jeff Hugoade1f842011-08-03 15:53:59 -06001309late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310MODULE_DESCRIPTION("MSM BAM DMUX");
1311MODULE_LICENSE("GPL v2");