blob: 2720774daccec808027e63d5fb0cf30f6c001772 [file] [log] [blame]
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14/*
15 * BAM DMUX module.
16 */
17
18#define DEBUG
19
20#include <linux/delay.h>
21#include <linux/module.h>
22#include <linux/netdevice.h>
23#include <linux/platform_device.h>
24#include <linux/sched.h>
25#include <linux/skbuff.h>
26#include <linux/debugfs.h>
Jeff Hugoaab7ebc2011-09-07 16:46:04 -060027#include <linux/clk.h>
Jeff Hugoae3a85e2011-12-02 17:10:18 -070028#include <linux/wakelock.h>
Eric Holmberg878923a2012-01-10 14:28:19 -070029#include <linux/kfifo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31#include <mach/sps.h>
32#include <mach/bam_dmux.h>
Jeff Hugoade1f842011-08-03 15:53:59 -060033#include <mach/msm_smsm.h>
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060034#include <mach/subsystem_notif.h>
Jeff Hugo75913c82011-12-05 15:59:01 -070035#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036
37#define BAM_CH_LOCAL_OPEN 0x1
38#define BAM_CH_REMOTE_OPEN 0x2
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060039#define BAM_CH_IN_RESET 0x4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040
41#define BAM_MUX_HDR_MAGIC_NO 0x33fc
42
Eric Holmberg006057d2012-01-11 10:10:42 -070043#define BAM_MUX_HDR_CMD_DATA 0
44#define BAM_MUX_HDR_CMD_OPEN 1
45#define BAM_MUX_HDR_CMD_CLOSE 2
46#define BAM_MUX_HDR_CMD_STATUS 3 /* unused */
47#define BAM_MUX_HDR_CMD_OPEN_NO_A2_PC 4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048
Jeff Hugo949080a2011-08-30 11:58:56 -060049#define POLLING_MIN_SLEEP 950 /* 0.95 ms */
50#define POLLING_MAX_SLEEP 1050 /* 1.05 ms */
51#define POLLING_INACTIVITY 40 /* cycles before switch to intr mode */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -070053#define LOW_WATERMARK 2
54#define HIGH_WATERMARK 4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055
56static int msm_bam_dmux_debug_enable;
57module_param_named(debug_enable, msm_bam_dmux_debug_enable,
58 int, S_IRUGO | S_IWUSR | S_IWGRP);
59
60#if defined(DEBUG)
61static uint32_t bam_dmux_read_cnt;
62static uint32_t bam_dmux_write_cnt;
63static uint32_t bam_dmux_write_cpy_cnt;
64static uint32_t bam_dmux_write_cpy_bytes;
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070065static uint32_t bam_dmux_tx_sps_failure_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066
67#define DBG(x...) do { \
68 if (msm_bam_dmux_debug_enable) \
69 pr_debug(x); \
70 } while (0)
71
72#define DBG_INC_READ_CNT(x) do { \
73 bam_dmux_read_cnt += (x); \
74 if (msm_bam_dmux_debug_enable) \
75 pr_debug("%s: total read bytes %u\n", \
76 __func__, bam_dmux_read_cnt); \
77 } while (0)
78
79#define DBG_INC_WRITE_CNT(x) do { \
80 bam_dmux_write_cnt += (x); \
81 if (msm_bam_dmux_debug_enable) \
82 pr_debug("%s: total written bytes %u\n", \
83 __func__, bam_dmux_write_cnt); \
84 } while (0)
85
86#define DBG_INC_WRITE_CPY(x) do { \
87 bam_dmux_write_cpy_bytes += (x); \
88 bam_dmux_write_cpy_cnt++; \
89 if (msm_bam_dmux_debug_enable) \
90 pr_debug("%s: total write copy cnt %u, bytes %u\n", \
91 __func__, bam_dmux_write_cpy_cnt, \
92 bam_dmux_write_cpy_bytes); \
93 } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070094
95#define DBG_INC_TX_SPS_FAILURE_CNT() do { \
96 bam_dmux_tx_sps_failure_cnt++; \
97} while (0)
98
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099#else
100#define DBG(x...) do { } while (0)
101#define DBG_INC_READ_CNT(x...) do { } while (0)
102#define DBG_INC_WRITE_CNT(x...) do { } while (0)
103#define DBG_INC_WRITE_CPY(x...) do { } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700104#define DBG_INC_TX_SPS_FAILURE_CNT() do { } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105#endif
106
107struct bam_ch_info {
108 uint32_t status;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600109 void (*notify)(void *, int, unsigned long);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110 void *priv;
111 spinlock_t lock;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600112 struct platform_device *pdev;
113 char name[BAM_DMUX_CH_NAME_MAX_LEN];
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700114 int num_tx_pkts;
115 int use_wm;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116};
117
118struct tx_pkt_info {
119 struct sk_buff *skb;
120 dma_addr_t dma_address;
121 char is_cmd;
122 uint32_t len;
123 struct work_struct work;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600124 struct list_head list_node;
Eric Holmberg878923a2012-01-10 14:28:19 -0700125 unsigned ts_sec;
126 unsigned long ts_nsec;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127};
128
129struct rx_pkt_info {
130 struct sk_buff *skb;
131 dma_addr_t dma_address;
132 struct work_struct work;
Jeff Hugo949080a2011-08-30 11:58:56 -0600133 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134};
135
136#define A2_NUM_PIPES 6
137#define A2_SUMMING_THRESHOLD 4096
138#define A2_DEFAULT_DESCRIPTORS 32
139#define A2_PHYS_BASE 0x124C2000
140#define A2_PHYS_SIZE 0x2000
141#define BUFFER_SIZE 2048
142#define NUM_BUFFERS 32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143static struct sps_bam_props a2_props;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600144static u32 a2_device_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145static struct sps_pipe *bam_tx_pipe;
146static struct sps_pipe *bam_rx_pipe;
147static struct sps_connect tx_connection;
148static struct sps_connect rx_connection;
149static struct sps_mem_buffer tx_desc_mem_buf;
150static struct sps_mem_buffer rx_desc_mem_buf;
151static struct sps_register_event tx_register_event;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600152static struct sps_register_event rx_register_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153
154static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS];
155static int bam_mux_initialized;
156
Jeff Hugo949080a2011-08-30 11:58:56 -0600157static int polling_mode;
158
159static LIST_HEAD(bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600160static DEFINE_MUTEX(bam_rx_pool_mutexlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600161static LIST_HEAD(bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600162static DEFINE_SPINLOCK(bam_tx_pool_spinlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600163
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164struct bam_mux_hdr {
165 uint16_t magic_num;
166 uint8_t reserved;
167 uint8_t cmd;
168 uint8_t pad_len;
169 uint8_t ch_id;
170 uint16_t pkt_len;
171};
172
Jeff Hugod98b1082011-10-24 10:30:23 -0600173static void notify_all(int event, unsigned long data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174static void bam_mux_write_done(struct work_struct *work);
175static void handle_bam_mux_cmd(struct work_struct *work);
Jeff Hugo949080a2011-08-30 11:58:56 -0600176static void rx_timer_work_func(struct work_struct *work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177
Jeff Hugo949080a2011-08-30 11:58:56 -0600178static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179
180static struct workqueue_struct *bam_mux_rx_workqueue;
181static struct workqueue_struct *bam_mux_tx_workqueue;
182
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600183/* A2 power collaspe */
184#define UL_TIMEOUT_DELAY 1000 /* in ms */
185static void toggle_apps_ack(void);
186static void reconnect_to_bam(void);
187static void disconnect_to_bam(void);
188static void ul_wakeup(void);
189static void ul_timeout(struct work_struct *work);
190static void vote_dfab(void);
191static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600192static void kickoff_ul_wakeup_func(struct work_struct *work);
Eric Holmberg006057d2012-01-11 10:10:42 -0700193static void grab_wakelock(void);
194static void release_wakelock(void);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600195
196static int bam_is_connected;
197static DEFINE_MUTEX(wakeup_lock);
198static struct completion ul_wakeup_ack_completion;
199static struct completion bam_connection_completion;
200static struct delayed_work ul_timeout_work;
201static int ul_packet_written;
202static struct clk *dfab_clk;
203static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600204static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600205static int bam_connection_is_active;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700206static int wait_for_ack;
Jeff Hugoae3a85e2011-12-02 17:10:18 -0700207static struct wake_lock bam_wakelock;
Eric Holmberg006057d2012-01-11 10:10:42 -0700208static int a2_pc_disabled;
209static DEFINE_MUTEX(dfab_status_lock);
210static int dfab_is_on;
211static int wait_for_dfab;
212static struct completion dfab_unvote_completion;
213static DEFINE_SPINLOCK(wakelock_reference_lock);
214static int wakelock_reference_count;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600215/* End A2 power collaspe */
216
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600217/* subsystem restart */
218static int restart_notifier_cb(struct notifier_block *this,
219 unsigned long code,
220 void *data);
221
222static struct notifier_block restart_notifier = {
223 .notifier_call = restart_notifier_cb,
224};
225static int in_global_reset;
226/* end subsystem restart */
227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228#define bam_ch_is_open(x) \
229 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
230
231#define bam_ch_is_local_open(x) \
232 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
233
234#define bam_ch_is_remote_open(x) \
235 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
236
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600237#define bam_ch_is_in_reset(x) \
238 (bam_ch[(x)].status & BAM_CH_IN_RESET)
239
Eric Holmberg878923a2012-01-10 14:28:19 -0700240#define LOG_MESSAGE_MAX_SIZE 80
241struct kfifo bam_dmux_state_log;
242static uint32_t bam_dmux_state_logging_disabled;
243static DEFINE_SPINLOCK(bam_dmux_logging_spinlock);
244static int bam_dmux_uplink_vote;
245static int bam_dmux_power_state;
246
247
248#define DMUX_LOG_KERR(fmt...) \
249do { \
250 bam_dmux_log(fmt); \
251 pr_err(fmt); \
252} while (0)
253
254/**
255 * Log a state change along with a small message.
256 *
257 * Complete size of messsage is limited to @todo.
258 */
259static void bam_dmux_log(const char *fmt, ...)
260{
261 char buff[LOG_MESSAGE_MAX_SIZE];
262 unsigned long flags;
263 va_list arg_list;
264 unsigned long long t_now;
265 unsigned long nanosec_rem;
266 int len = 0;
267
268 if (bam_dmux_state_logging_disabled)
269 return;
270
271 t_now = sched_clock();
272 nanosec_rem = do_div(t_now, 1000000000U);
273
274 /*
275 * States
Eric Holmberg006057d2012-01-11 10:10:42 -0700276 * D: 1 = Power collapse disabled
Eric Holmberg878923a2012-01-10 14:28:19 -0700277 * R: 1 = in global reset
278 * P: 1 = BAM is powered up
279 * A: 1 = BAM initialized and ready for data
280 *
281 * V: 1 = Uplink vote for power
282 * U: 1 = Uplink active
283 * W: 1 = Uplink Wait-for-ack
284 * A: 1 = Uplink ACK received
285 */
286 len += scnprintf(buff, sizeof(buff),
Eric Holmberg006057d2012-01-11 10:10:42 -0700287 "<DMUX> %u.%09lu %c%c%c%c %c%c%c%c ",
Eric Holmberg878923a2012-01-10 14:28:19 -0700288 (unsigned)t_now, nanosec_rem,
Eric Holmberg006057d2012-01-11 10:10:42 -0700289 a2_pc_disabled ? 'D' : 'd',
Eric Holmberg878923a2012-01-10 14:28:19 -0700290 in_global_reset ? 'R' : 'r',
291 bam_dmux_power_state ? 'P' : 'p',
292 bam_connection_is_active ? 'A' : 'a',
293 bam_dmux_uplink_vote ? 'V' : 'v',
294 bam_is_connected ? 'U' : 'u',
295 wait_for_ack ? 'W' : 'w',
296 ul_wakeup_ack_completion.done ? 'A' : 'a'
297 );
298
299 va_start(arg_list, fmt);
300 len += vscnprintf(buff + len, sizeof(buff) - len, fmt, arg_list);
301 va_end(arg_list);
302 memset(buff + len, 0x0, sizeof(buff) - len);
303
304 spin_lock_irqsave(&bam_dmux_logging_spinlock, flags);
305 if (kfifo_avail(&bam_dmux_state_log) < LOG_MESSAGE_MAX_SIZE) {
306 char junk[LOG_MESSAGE_MAX_SIZE];
307 int ret;
308
309 ret = kfifo_out(&bam_dmux_state_log, junk, sizeof(junk));
310 if (ret != LOG_MESSAGE_MAX_SIZE) {
311 pr_err("%s: unable to empty log %d\n", __func__, ret);
312 spin_unlock_irqrestore(&bam_dmux_logging_spinlock,
313 flags);
314 return;
315 }
316 }
317 kfifo_in(&bam_dmux_state_log, buff, sizeof(buff));
318 spin_unlock_irqrestore(&bam_dmux_logging_spinlock, flags);
319}
320
321static inline void set_tx_timestamp(struct tx_pkt_info *pkt)
322{
323 unsigned long long t_now;
324
325 t_now = sched_clock();
326 pkt->ts_nsec = do_div(t_now, 1000000000U);
327 pkt->ts_sec = (unsigned)t_now;
328}
329
330static inline void verify_tx_queue_is_empty(const char *func)
331{
332 unsigned long flags;
333 struct tx_pkt_info *info;
334 int reported = 0;
335
336 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
337 list_for_each_entry(info, &bam_tx_pool, list_node) {
338 if (!reported) {
Eric Holmberg454d9da2012-01-12 09:37:14 -0700339 bam_dmux_log("%s: tx pool not empty\n", func);
340 if (!in_global_reset)
341 pr_err("%s: tx pool not empty\n", func);
Eric Holmberg878923a2012-01-10 14:28:19 -0700342 reported = 1;
343 }
Eric Holmberg454d9da2012-01-12 09:37:14 -0700344 bam_dmux_log("%s: node=%p ts=%u.%09lu\n", __func__,
345 &info->list_node, info->ts_sec, info->ts_nsec);
346 if (!in_global_reset)
347 pr_err("%s: node=%p ts=%u.%09lu\n", __func__,
348 &info->list_node, info->ts_sec, info->ts_nsec);
Eric Holmberg878923a2012-01-10 14:28:19 -0700349 }
350 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
351}
352
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353static void queue_rx(void)
354{
355 void *ptr;
356 struct rx_pkt_info *info;
357
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600358 if (in_global_reset)
359 return;
360
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361 info = kmalloc(sizeof(struct rx_pkt_info), GFP_KERNEL);
Jeff Hugoe05bc222011-12-07 13:57:23 -0700362 if (!info) {
363 pr_err("%s: unable to alloc rx_pkt_info\n", __func__);
364 return;
365 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366
367 INIT_WORK(&info->work, handle_bam_mux_cmd);
368
369 info->skb = __dev_alloc_skb(BUFFER_SIZE, GFP_KERNEL);
Jeff Hugo4ba22f92011-12-07 12:42:47 -0700370 if (info->skb == NULL) {
371 pr_err("%s: unable to alloc skb\n", __func__);
372 kfree(info);
373 return;
374 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375 ptr = skb_put(info->skb, BUFFER_SIZE);
Jeff Hugo949080a2011-08-30 11:58:56 -0600376
Jeff Hugoc9749932011-11-02 17:50:40 -0600377 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600378 list_add_tail(&info->list_node, &bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600379 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600380
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 /* need a way to handle error case */
382 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
383 DMA_FROM_DEVICE);
384 sps_transfer_one(bam_rx_pipe, info->dma_address,
Jeff Hugo33dbc002011-08-25 15:52:53 -0600385 BUFFER_SIZE, info,
386 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387}
388
389static void bam_mux_process_data(struct sk_buff *rx_skb)
390{
391 unsigned long flags;
392 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600393 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394
395 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
396
397 rx_skb->data = (unsigned char *)(rx_hdr + 1);
398 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
399 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600400 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600402 event_data = (unsigned long)(rx_skb);
403
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600405 if (bam_ch[rx_hdr->ch_id].notify)
406 bam_ch[rx_hdr->ch_id].notify(
407 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
408 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409 else
410 dev_kfree_skb_any(rx_skb);
411 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
412
413 queue_rx();
414}
415
Eric Holmberg006057d2012-01-11 10:10:42 -0700416static inline void handle_bam_mux_cmd_open(struct bam_mux_hdr *rx_hdr)
417{
418 unsigned long flags;
419 int ret;
420
421 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
422 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
423 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
424 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
425 queue_rx();
426 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
427 if (ret)
428 pr_err("%s: platform_device_add() error: %d\n",
429 __func__, ret);
430}
431
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432static void handle_bam_mux_cmd(struct work_struct *work)
433{
434 unsigned long flags;
435 struct bam_mux_hdr *rx_hdr;
436 struct rx_pkt_info *info;
437 struct sk_buff *rx_skb;
438
439 info = container_of(work, struct rx_pkt_info, work);
440 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600441 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442 kfree(info);
443
444 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
445
446 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
447 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
448 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
449 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
450 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700451 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
452 " reserved %d cmd %d"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 " pad %d ch %d len %d\n", __func__,
454 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
455 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
456 dev_kfree_skb_any(rx_skb);
457 queue_rx();
458 return;
459 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700460
461 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700462 DMUX_LOG_KERR("%s: dropping invalid LCID %d"
463 " reserved %d cmd %d"
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700464 " pad %d ch %d len %d\n", __func__,
465 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
466 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
467 dev_kfree_skb_any(rx_skb);
468 queue_rx();
469 return;
470 }
471
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472 switch (rx_hdr->cmd) {
473 case BAM_MUX_HDR_CMD_DATA:
474 DBG_INC_READ_CNT(rx_hdr->pkt_len);
475 bam_mux_process_data(rx_skb);
476 break;
477 case BAM_MUX_HDR_CMD_OPEN:
Eric Holmberg006057d2012-01-11 10:10:42 -0700478 bam_dmux_log("%s: opening cid %d PC enabled\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700479 rx_hdr->ch_id);
Eric Holmberg006057d2012-01-11 10:10:42 -0700480 handle_bam_mux_cmd_open(rx_hdr);
481 dev_kfree_skb_any(rx_skb);
482 break;
483 case BAM_MUX_HDR_CMD_OPEN_NO_A2_PC:
484 bam_dmux_log("%s: opening cid %d PC disabled\n", __func__,
485 rx_hdr->ch_id);
486
487 if (!a2_pc_disabled) {
488 a2_pc_disabled = 1;
489 schedule_delayed_work(&ul_timeout_work,
490 msecs_to_jiffies(UL_TIMEOUT_DELAY));
491 }
492
493 handle_bam_mux_cmd_open(rx_hdr);
Eric Holmberge779dba2011-11-04 18:22:01 -0600494 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 break;
496 case BAM_MUX_HDR_CMD_CLOSE:
497 /* probably should drop pending write */
Eric Holmberg878923a2012-01-10 14:28:19 -0700498 bam_dmux_log("%s: closing cid %d\n", __func__,
499 rx_hdr->ch_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
501 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
502 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600504 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
505 bam_ch[rx_hdr->ch_id].pdev =
506 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
507 if (!bam_ch[rx_hdr->ch_id].pdev)
508 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberge779dba2011-11-04 18:22:01 -0600509 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 break;
511 default:
Eric Holmberg878923a2012-01-10 14:28:19 -0700512 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
513 " reserved %d cmd %d pad %d ch %d len %d\n",
514 __func__, rx_hdr->magic_num, rx_hdr->reserved,
515 rx_hdr->cmd, rx_hdr->pad_len, rx_hdr->ch_id,
516 rx_hdr->pkt_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 dev_kfree_skb_any(rx_skb);
518 queue_rx();
519 return;
520 }
521}
522
523static int bam_mux_write_cmd(void *data, uint32_t len)
524{
525 int rc;
526 struct tx_pkt_info *pkt;
527 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700528 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600530 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531 if (pkt == NULL) {
532 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
533 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534 return rc;
535 }
536
537 dma_address = dma_map_single(NULL, data, len,
538 DMA_TO_DEVICE);
539 if (!dma_address) {
540 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugo96cb7482011-12-07 13:28:31 -0700541 kfree(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 return rc;
544 }
545 pkt->skb = (struct sk_buff *)(data);
546 pkt->len = len;
547 pkt->dma_address = dma_address;
548 pkt->is_cmd = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -0700549 set_tx_timestamp(pkt);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600550 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700551 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600552 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700553 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
555 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600556 if (rc) {
557 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700558 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600559 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700560 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700561 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600562 kfree(pkt);
563 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600565 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 return rc;
567}
568
569static void bam_mux_write_done(struct work_struct *work)
570{
571 struct sk_buff *skb;
572 struct bam_mux_hdr *hdr;
573 struct tx_pkt_info *info;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700574 struct tx_pkt_info *info_expected;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600575 unsigned long event_data;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700576 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600578 if (in_global_reset)
579 return;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700580
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581 info = container_of(work, struct tx_pkt_info, work);
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700582
583 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
584 info_expected = list_first_entry(&bam_tx_pool,
585 struct tx_pkt_info, list_node);
586 if (unlikely(info != info_expected)) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700587 struct tx_pkt_info *errant_pkt;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700588
Eric Holmberg878923a2012-01-10 14:28:19 -0700589 DMUX_LOG_KERR("%s: bam_tx_pool mismatch .next=%p,"
590 " list_node=%p, ts=%u.%09lu\n",
591 __func__, bam_tx_pool.next, &info->list_node,
592 info->ts_sec, info->ts_nsec
593 );
594
595 list_for_each_entry(errant_pkt, &bam_tx_pool, list_node) {
596 DMUX_LOG_KERR("%s: node=%p ts=%u.%09lu\n", __func__,
597 &errant_pkt->list_node, errant_pkt->ts_sec,
598 errant_pkt->ts_nsec);
599
600 }
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700601 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
602 BUG();
603 }
604 list_del(&info->list_node);
605 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
606
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600607 if (info->is_cmd) {
608 kfree(info->skb);
609 kfree(info);
610 return;
611 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612 skb = info->skb;
613 kfree(info);
614 hdr = (struct bam_mux_hdr *)skb->data;
615 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600616 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700617 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
618 bam_ch[hdr->ch_id].num_tx_pkts--;
619 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600620 if (bam_ch[hdr->ch_id].notify)
621 bam_ch[hdr->ch_id].notify(
622 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
623 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624 else
625 dev_kfree_skb_any(skb);
626}
627
628int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
629{
630 int rc = 0;
631 struct bam_mux_hdr *hdr;
632 unsigned long flags;
633 struct sk_buff *new_skb = NULL;
634 dma_addr_t dma_address;
635 struct tx_pkt_info *pkt;
636
637 if (id >= BAM_DMUX_NUM_CHANNELS)
638 return -EINVAL;
639 if (!skb)
640 return -EINVAL;
641 if (!bam_mux_initialized)
642 return -ENODEV;
643
644 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
645 spin_lock_irqsave(&bam_ch[id].lock, flags);
646 if (!bam_ch_is_open(id)) {
647 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
648 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
649 return -ENODEV;
650 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700651
652 if (bam_ch[id].use_wm &&
653 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
654 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
655 pr_err("%s: watermark exceeded: %d\n", __func__, id);
656 return -EAGAIN;
657 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700658 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
659
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600660 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600661 if (!bam_is_connected) {
662 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600663 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600664 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600665 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600666 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600667
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668 /* if skb do not have any tailroom for padding,
669 copy the skb into a new expanded skb */
670 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
671 /* revisit, probably dev_alloc_skb and memcpy is effecient */
672 new_skb = skb_copy_expand(skb, skb_headroom(skb),
673 4 - (skb->len & 0x3), GFP_ATOMIC);
674 if (new_skb == NULL) {
675 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600676 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677 }
678 dev_kfree_skb_any(skb);
679 skb = new_skb;
680 DBG_INC_WRITE_CPY(skb->len);
681 }
682
683 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
684
685 /* caller should allocate for hdr and padding
686 hdr is fine, padding is tricky */
687 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
688 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
689 hdr->reserved = 0;
690 hdr->ch_id = id;
691 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
692 if (skb->len & 0x3)
693 skb_put(skb, 4 - (skb->len & 0x3));
694
695 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
696
697 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
698 __func__, skb->data, skb->tail, skb->len,
699 hdr->pkt_len, hdr->pad_len);
700
701 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
702 if (pkt == NULL) {
703 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600704 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705 }
706
707 dma_address = dma_map_single(NULL, skb->data, skb->len,
708 DMA_TO_DEVICE);
709 if (!dma_address) {
710 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600711 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712 }
713 pkt->skb = skb;
714 pkt->dma_address = dma_address;
715 pkt->is_cmd = 0;
Eric Holmberg878923a2012-01-10 14:28:19 -0700716 set_tx_timestamp(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700718 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600719 list_add_tail(&pkt->list_node, &bam_tx_pool);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700720 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
722 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600723 if (rc) {
724 DBG("%s sps_transfer_one failed rc=%d\n", __func__, rc);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700725 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600726 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700727 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700728 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600729 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700730 if (new_skb)
731 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700732 } else {
733 spin_lock_irqsave(&bam_ch[id].lock, flags);
734 bam_ch[id].num_tx_pkts++;
735 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600736 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600737 ul_packet_written = 1;
738 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600740
741write_fail3:
742 kfree(pkt);
743write_fail2:
744 if (new_skb)
745 dev_kfree_skb_any(new_skb);
746write_fail:
747 read_unlock(&ul_wakeup_lock);
748 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749}
750
751int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600752 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753{
754 struct bam_mux_hdr *hdr;
755 unsigned long flags;
756 int rc = 0;
757
758 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700759 if (!bam_mux_initialized) {
760 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700762 }
763 if (id >= BAM_DMUX_NUM_CHANNELS) {
764 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700766 }
767 if (notify == NULL) {
768 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600769 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700770 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771
772 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
773 if (hdr == NULL) {
774 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
775 return -ENOMEM;
776 }
777 spin_lock_irqsave(&bam_ch[id].lock, flags);
778 if (bam_ch_is_open(id)) {
779 DBG("%s: Already opened %d\n", __func__, id);
780 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
781 kfree(hdr);
782 goto open_done;
783 }
784 if (!bam_ch_is_remote_open(id)) {
785 DBG("%s: Remote not open; ch: %d\n", __func__, id);
786 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
787 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700788 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700789 }
790
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600791 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792 bam_ch[id].priv = priv;
793 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700794 bam_ch[id].num_tx_pkts = 0;
795 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
797
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600798 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600799 if (!bam_is_connected) {
800 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600801 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600802 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600803 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600804 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600805
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
807 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
808 hdr->reserved = 0;
809 hdr->ch_id = id;
810 hdr->pkt_len = 0;
811 hdr->pad_len = 0;
812
813 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600814 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815
816open_done:
817 DBG("%s: opened ch %d\n", __func__, id);
818 return rc;
819}
820
821int msm_bam_dmux_close(uint32_t id)
822{
823 struct bam_mux_hdr *hdr;
824 unsigned long flags;
825 int rc;
826
827 if (id >= BAM_DMUX_NUM_CHANNELS)
828 return -EINVAL;
829 DBG("%s: closing ch %d\n", __func__, id);
830 if (!bam_mux_initialized)
831 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600833 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600834 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600835 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600836 ul_wakeup();
Jeff Hugo061ce672011-10-21 17:15:32 -0600837 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600838 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600839 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600840
Jeff Hugo061ce672011-10-21 17:15:32 -0600841 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600842 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843 bam_ch[id].priv = NULL;
844 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
845 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
846
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600847 if (bam_ch_is_in_reset(id)) {
848 read_unlock(&ul_wakeup_lock);
849 bam_ch[id].status &= ~BAM_CH_IN_RESET;
850 return 0;
851 }
852
Jeff Hugobb5802f2011-11-02 17:10:29 -0600853 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 if (hdr == NULL) {
855 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600856 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 return -ENOMEM;
858 }
859 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
860 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
861 hdr->reserved = 0;
862 hdr->ch_id = id;
863 hdr->pkt_len = 0;
864 hdr->pad_len = 0;
865
866 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600867 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700868
869 DBG("%s: closed ch %d\n", __func__, id);
870 return rc;
871}
872
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700873int msm_bam_dmux_is_ch_full(uint32_t id)
874{
875 unsigned long flags;
876 int ret;
877
878 if (id >= BAM_DMUX_NUM_CHANNELS)
879 return -EINVAL;
880
881 spin_lock_irqsave(&bam_ch[id].lock, flags);
882 bam_ch[id].use_wm = 1;
883 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
884 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
885 id, bam_ch[id].num_tx_pkts, ret);
886 if (!bam_ch_is_local_open(id)) {
887 ret = -ENODEV;
888 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
889 }
890 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
891
892 return ret;
893}
894
895int msm_bam_dmux_is_ch_low(uint32_t id)
896{
897 int ret;
898
899 if (id >= BAM_DMUX_NUM_CHANNELS)
900 return -EINVAL;
901
902 bam_ch[id].use_wm = 1;
903 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
904 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
905 id, bam_ch[id].num_tx_pkts, ret);
906 if (!bam_ch_is_local_open(id)) {
907 ret = -ENODEV;
908 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
909 }
910
911 return ret;
912}
913
Eric Holmberg8df0cdb2012-01-04 17:40:46 -0700914static void rx_switch_to_interrupt_mode(void)
915{
916 struct sps_connect cur_rx_conn;
917 struct sps_iovec iov;
918 struct rx_pkt_info *info;
919 int ret;
920
921 /*
922 * Attempt to enable interrupts - if this fails,
923 * continue polling and we will retry later.
924 */
925 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
926 if (ret) {
927 pr_err("%s: sps_get_config() failed %d\n", __func__, ret);
928 goto fail;
929 }
930
931 rx_register_event.options = SPS_O_EOT;
932 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
933 if (ret) {
934 pr_err("%s: sps_register_event() failed %d\n", __func__, ret);
935 goto fail;
936 }
937
938 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
939 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
940 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
941 if (ret) {
942 pr_err("%s: sps_set_config() failed %d\n", __func__, ret);
943 goto fail;
944 }
945 polling_mode = 0;
Eric Holmberg006057d2012-01-11 10:10:42 -0700946 release_wakelock();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -0700947
948 /* handle any rx packets before interrupt was enabled */
949 while (bam_connection_is_active && !polling_mode) {
950 ret = sps_get_iovec(bam_rx_pipe, &iov);
951 if (ret) {
952 pr_err("%s: sps_get_iovec failed %d\n",
953 __func__, ret);
954 break;
955 }
956 if (iov.addr == 0)
957 break;
958
959 mutex_lock(&bam_rx_pool_mutexlock);
960 if (unlikely(list_empty(&bam_rx_pool))) {
961 mutex_unlock(&bam_rx_pool_mutexlock);
962 continue;
963 }
964 info = list_first_entry(&bam_rx_pool, struct rx_pkt_info,
965 list_node);
966 list_del(&info->list_node);
967 mutex_unlock(&bam_rx_pool_mutexlock);
968 handle_bam_mux_cmd(&info->work);
969 }
970 return;
971
972fail:
973 pr_err("%s: reverting to polling\n", __func__);
974 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
975}
976
Jeff Hugo949080a2011-08-30 11:58:56 -0600977static void rx_timer_work_func(struct work_struct *work)
978{
979 struct sps_iovec iov;
Jeff Hugo949080a2011-08-30 11:58:56 -0600980 struct rx_pkt_info *info;
981 int inactive_cycles = 0;
982 int ret;
Jeff Hugo949080a2011-08-30 11:58:56 -0600983
Eric Holmberg8df0cdb2012-01-04 17:40:46 -0700984 while (bam_connection_is_active) { /* timer loop */
Jeff Hugo949080a2011-08-30 11:58:56 -0600985 ++inactive_cycles;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -0700986 while (bam_connection_is_active) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600987 if (in_global_reset)
988 return;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -0700989
990 ret = sps_get_iovec(bam_rx_pipe, &iov);
991 if (ret) {
992 pr_err("%s: sps_get_iovec failed %d\n",
993 __func__, ret);
994 break;
995 }
Jeff Hugo949080a2011-08-30 11:58:56 -0600996 if (iov.addr == 0)
997 break;
998 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -0600999 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001000 if (unlikely(list_empty(&bam_rx_pool))) {
1001 mutex_unlock(&bam_rx_pool_mutexlock);
1002 continue;
1003 }
1004 info = list_first_entry(&bam_rx_pool,
1005 struct rx_pkt_info, list_node);
1006 list_del(&info->list_node);
Jeff Hugoc9749932011-11-02 17:50:40 -06001007 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -06001008 handle_bam_mux_cmd(&info->work);
1009 }
1010
1011 if (inactive_cycles == POLLING_INACTIVITY) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001012 rx_switch_to_interrupt_mode();
1013 break;
Jeff Hugo949080a2011-08-30 11:58:56 -06001014 }
1015
1016 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
1017 }
1018}
1019
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020static void bam_mux_tx_notify(struct sps_event_notify *notify)
1021{
1022 struct tx_pkt_info *pkt;
1023
1024 DBG("%s: event %d notified\n", __func__, notify->event_id);
1025
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001026 if (in_global_reset)
1027 return;
1028
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029 switch (notify->event_id) {
1030 case SPS_EVENT_EOT:
1031 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001032 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001033 dma_unmap_single(NULL, pkt->dma_address,
1034 pkt->skb->len,
1035 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001036 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037 dma_unmap_single(NULL, pkt->dma_address,
1038 pkt->len,
1039 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001040 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041 break;
1042 default:
1043 pr_err("%s: recieved unexpected event id %d\n", __func__,
1044 notify->event_id);
1045 }
1046}
1047
Jeff Hugo33dbc002011-08-25 15:52:53 -06001048static void bam_mux_rx_notify(struct sps_event_notify *notify)
1049{
Jeff Hugo949080a2011-08-30 11:58:56 -06001050 int ret;
1051 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -06001052
1053 DBG("%s: event %d notified\n", __func__, notify->event_id);
1054
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001055 if (in_global_reset)
1056 return;
1057
Jeff Hugo33dbc002011-08-25 15:52:53 -06001058 switch (notify->event_id) {
1059 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -06001060 /* attempt to disable interrupts in this pipe */
1061 if (!polling_mode) {
1062 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1063 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001064 pr_err("%s: sps_get_config() failed %d, interrupts"
1065 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001066 break;
1067 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -07001068 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -06001069 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
1070 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1071 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001072 pr_err("%s: sps_set_config() failed %d, interrupts"
1073 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001074 break;
1075 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001076 grab_wakelock();
Jeff Hugo949080a2011-08-30 11:58:56 -06001077 polling_mode = 1;
1078 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
1079 }
Jeff Hugo33dbc002011-08-25 15:52:53 -06001080 break;
1081 default:
1082 pr_err("%s: recieved unexpected event id %d\n", __func__,
1083 notify->event_id);
1084 }
1085}
1086
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001087#ifdef CONFIG_DEBUG_FS
1088
1089static int debug_tbl(char *buf, int max)
1090{
1091 int i = 0;
1092 int j;
1093
1094 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
1095 i += scnprintf(buf + i, max - i,
1096 "ch%02d local open=%s remote open=%s\n",
1097 j, bam_ch_is_local_open(j) ? "Y" : "N",
1098 bam_ch_is_remote_open(j) ? "Y" : "N");
1099 }
1100
1101 return i;
1102}
1103
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001104static int debug_ul_pkt_cnt(char *buf, int max)
1105{
1106 struct list_head *p;
1107 unsigned long flags;
1108 int n = 0;
1109
1110 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
1111 __list_for_each(p, &bam_tx_pool) {
1112 ++n;
1113 }
1114 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
1115
1116 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
1117}
1118
1119static int debug_stats(char *buf, int max)
1120{
1121 int i = 0;
1122
1123 i += scnprintf(buf + i, max - i,
1124 "skb copy cnt: %u\n"
1125 "skb copy bytes: %u\n"
1126 "sps tx failures: %u\n",
1127 bam_dmux_write_cpy_cnt,
1128 bam_dmux_write_cpy_bytes,
1129 bam_dmux_tx_sps_failure_cnt
1130 );
1131
1132 return i;
1133}
1134
Eric Holmberg878923a2012-01-10 14:28:19 -07001135static int debug_log(char *buff, int max, loff_t *ppos)
1136{
1137 unsigned long flags;
1138 int i = 0;
1139
1140 if (bam_dmux_state_logging_disabled) {
1141 i += scnprintf(buff - i, max - i, "Logging disabled\n");
1142 return i;
1143 }
1144
1145 if (*ppos == 0) {
1146 i += scnprintf(buff - i, max - i,
1147 "<DMUX> timestamp FLAGS [Message]\n"
1148 "FLAGS:\n"
Eric Holmberg006057d2012-01-11 10:10:42 -07001149 "\tD: 1 = Power collapse disabled\n"
Eric Holmberg878923a2012-01-10 14:28:19 -07001150 "\tR: 1 = in global reset\n"
1151 "\tP: 1 = BAM is powered up\n"
1152 "\tA: 1 = BAM initialized and ready for data\n"
1153 "\n"
1154 "\tV: 1 = Uplink vote for power\n"
1155 "\tU: 1 = Uplink active\n"
1156 "\tW: 1 = Uplink Wait-for-ack\n"
1157 "\tA: 1 = Uplink ACK received\n"
1158 );
1159 buff += i;
1160 }
1161
1162 spin_lock_irqsave(&bam_dmux_logging_spinlock, flags);
1163 while (kfifo_len(&bam_dmux_state_log)
1164 && (i + LOG_MESSAGE_MAX_SIZE) < max) {
1165 int k_len;
1166 k_len = kfifo_out(&bam_dmux_state_log,
1167 buff, LOG_MESSAGE_MAX_SIZE);
1168 if (k_len != LOG_MESSAGE_MAX_SIZE) {
1169 pr_err("%s: retrieve failure %d\n", __func__, k_len);
1170 break;
1171 }
1172
1173 /* keep non-null portion of string and add line break */
1174 k_len = strnlen(buff, LOG_MESSAGE_MAX_SIZE);
1175 buff += k_len;
1176 i += k_len;
1177 if (k_len && *(buff - 1) != '\n') {
1178 *buff++ = '\n';
1179 ++i;
1180 }
1181 }
1182 spin_unlock_irqrestore(&bam_dmux_logging_spinlock, flags);
1183
1184 return i;
1185}
1186
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001187#define DEBUG_BUFMAX 4096
1188static char debug_buffer[DEBUG_BUFMAX];
1189
1190static ssize_t debug_read(struct file *file, char __user *buf,
1191 size_t count, loff_t *ppos)
1192{
1193 int (*fill)(char *buf, int max) = file->private_data;
1194 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1195 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1196}
1197
Eric Holmberg878923a2012-01-10 14:28:19 -07001198static ssize_t debug_read_multiple(struct file *file, char __user *buff,
1199 size_t count, loff_t *ppos)
1200{
1201 int (*util_func)(char *buf, int max, loff_t *) = file->private_data;
1202 char *buffer;
1203 int bsize;
1204
1205 buffer = kmalloc(count, GFP_KERNEL);
1206 if (!buffer)
1207 return -ENOMEM;
1208
1209 bsize = util_func(buffer, count, ppos);
1210
1211 if (bsize >= 0) {
1212 if (copy_to_user(buff, buffer, bsize)) {
1213 kfree(buffer);
1214 return -EFAULT;
1215 }
1216 *ppos += bsize;
1217 }
1218 kfree(buffer);
1219 return bsize;
1220}
1221
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001222static int debug_open(struct inode *inode, struct file *file)
1223{
1224 file->private_data = inode->i_private;
1225 return 0;
1226}
1227
1228
1229static const struct file_operations debug_ops = {
1230 .read = debug_read,
1231 .open = debug_open,
1232};
1233
Eric Holmberg878923a2012-01-10 14:28:19 -07001234static const struct file_operations debug_ops_multiple = {
1235 .read = debug_read_multiple,
1236 .open = debug_open,
1237};
1238
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001239static void debug_create(const char *name, mode_t mode,
1240 struct dentry *dent,
1241 int (*fill)(char *buf, int max))
1242{
1243 debugfs_create_file(name, mode, dent, fill, &debug_ops);
1244}
1245
1246#endif
1247
Jeff Hugod98b1082011-10-24 10:30:23 -06001248static void notify_all(int event, unsigned long data)
1249{
1250 int i;
1251
1252 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001253 if (bam_ch_is_open(i)) {
Jeff Hugod98b1082011-10-24 10:30:23 -06001254 bam_ch[i].notify(bam_ch[i].priv, event, data);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001255 bam_dmux_log("%s: cid=%d, event=%d, data=%lu\n",
1256 __func__, i, event, data);
1257 }
Jeff Hugod98b1082011-10-24 10:30:23 -06001258 }
1259}
1260
1261static void kickoff_ul_wakeup_func(struct work_struct *work)
1262{
1263 read_lock(&ul_wakeup_lock);
1264 if (!bam_is_connected) {
1265 read_unlock(&ul_wakeup_lock);
1266 ul_wakeup();
1267 read_lock(&ul_wakeup_lock);
1268 ul_packet_written = 1;
1269 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
1270 }
1271 read_unlock(&ul_wakeup_lock);
1272}
1273
1274void msm_bam_dmux_kickoff_ul_wakeup(void)
1275{
1276 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1277}
1278
Eric Holmberg878923a2012-01-10 14:28:19 -07001279static void power_vote(int vote)
1280{
1281 bam_dmux_log("%s: curr=%d, vote=%d\n", __func__,
1282 bam_dmux_uplink_vote, vote);
1283
1284 if (bam_dmux_uplink_vote == vote)
1285 bam_dmux_log("%s: warning - duplicate power vote\n", __func__);
1286
1287 bam_dmux_uplink_vote = vote;
1288 if (vote)
1289 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
1290 else
1291 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1292}
1293
Eric Holmberg454d9da2012-01-12 09:37:14 -07001294/*
1295 * @note: Must be called with ul_wakeup_lock locked.
1296 */
1297static inline void ul_powerdown(void)
1298{
1299 bam_dmux_log("%s: powerdown\n", __func__);
1300 verify_tx_queue_is_empty(__func__);
1301
1302 if (a2_pc_disabled) {
1303 wait_for_dfab = 1;
1304 INIT_COMPLETION(dfab_unvote_completion);
1305 release_wakelock();
1306 } else {
1307 wait_for_ack = 1;
1308 INIT_COMPLETION(ul_wakeup_ack_completion);
1309 power_vote(0);
1310 }
1311 bam_is_connected = 0;
1312 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
1313}
1314
1315static inline void ul_powerdown_finish(void)
1316{
1317 if (a2_pc_disabled && wait_for_dfab) {
1318 unvote_dfab();
1319 complete_all(&dfab_unvote_completion);
1320 wait_for_dfab = 0;
1321 }
1322}
1323
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001324static void ul_timeout(struct work_struct *work)
1325{
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001326 unsigned long flags;
1327 int ret;
1328
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001329 if (in_global_reset)
1330 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001331 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
1332 if (!ret) { /* failed to grab lock, reschedule and bail */
1333 schedule_delayed_work(&ul_timeout_work,
1334 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1335 return;
1336 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001337 if (bam_is_connected) {
1338 if (ul_packet_written) {
1339 bam_dmux_log("%s: packet written\n", __func__);
1340 ul_packet_written = 0;
1341 schedule_delayed_work(&ul_timeout_work,
1342 msecs_to_jiffies(UL_TIMEOUT_DELAY));
Eric Holmberg006057d2012-01-11 10:10:42 -07001343 } else {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001344 ul_powerdown();
Eric Holmberg006057d2012-01-11 10:10:42 -07001345 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001346 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001347 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001348 ul_powerdown_finish();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001349}
1350static void ul_wakeup(void)
1351{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001352 int ret;
Eric Holmberg006057d2012-01-11 10:10:42 -07001353 static int called_before;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001354
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001355 mutex_lock(&wakeup_lock);
1356 if (bam_is_connected) { /* bam got connected before lock grabbed */
Eric Holmberg878923a2012-01-10 14:28:19 -07001357 bam_dmux_log("%s Already awake\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001358 mutex_unlock(&wakeup_lock);
1359 return;
1360 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001361
Eric Holmberg006057d2012-01-11 10:10:42 -07001362 if (a2_pc_disabled) {
1363 /*
1364 * don't grab the wakelock the first time because it is
1365 * already grabbed when a2 powers on
1366 */
1367 if (likely(called_before))
1368 grab_wakelock();
1369 else
1370 called_before = 1;
1371 if (wait_for_dfab) {
1372 ret = wait_for_completion_interruptible_timeout(
1373 &dfab_unvote_completion, HZ);
1374 BUG_ON(ret == 0);
1375 }
1376 vote_dfab();
1377 schedule_delayed_work(&ul_timeout_work,
1378 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1379 bam_is_connected = 1;
1380 mutex_unlock(&wakeup_lock);
1381 return;
1382 }
1383
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001384 /*
1385 * must wait for the previous power down request to have been acked
1386 * chances are it already came in and this will just fall through
1387 * instead of waiting
1388 */
1389 if (wait_for_ack) {
Eric Holmberg878923a2012-01-10 14:28:19 -07001390 bam_dmux_log("%s waiting for previous ack\n", __func__);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001391 ret = wait_for_completion_interruptible_timeout(
1392 &ul_wakeup_ack_completion, HZ);
1393 BUG_ON(ret == 0);
Eric Holmberg006057d2012-01-11 10:10:42 -07001394 wait_for_ack = 0;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001395 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001396 INIT_COMPLETION(ul_wakeup_ack_completion);
Eric Holmberg878923a2012-01-10 14:28:19 -07001397 power_vote(1);
1398 bam_dmux_log("%s waiting for wakeup ack\n", __func__);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001399 ret = wait_for_completion_interruptible_timeout(
1400 &ul_wakeup_ack_completion, HZ);
1401 BUG_ON(ret == 0);
Eric Holmberg878923a2012-01-10 14:28:19 -07001402 bam_dmux_log("%s waiting completion\n", __func__);
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001403 ret = wait_for_completion_interruptible_timeout(
1404 &bam_connection_completion, HZ);
1405 BUG_ON(ret == 0);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001406
1407 bam_is_connected = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -07001408 bam_dmux_log("%s complete\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001409 schedule_delayed_work(&ul_timeout_work,
1410 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1411 mutex_unlock(&wakeup_lock);
1412}
1413
1414static void reconnect_to_bam(void)
1415{
1416 int i;
1417
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001418 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001419 vote_dfab();
1420 i = sps_device_reset(a2_device_handle);
1421 if (i)
1422 pr_err("%s: device reset failed rc = %d\n", __func__, i);
1423 i = sps_connect(bam_tx_pipe, &tx_connection);
1424 if (i)
1425 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
1426 i = sps_connect(bam_rx_pipe, &rx_connection);
1427 if (i)
1428 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
1429 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1430 if (i)
1431 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
1432 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1433 if (i)
1434 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001435
1436 bam_connection_is_active = 1;
1437
1438 if (polling_mode)
1439 rx_switch_to_interrupt_mode();
1440
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001441 for (i = 0; i < NUM_BUFFERS; ++i)
1442 queue_rx();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001443
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001444 toggle_apps_ack();
1445 complete_all(&bam_connection_completion);
1446}
1447
1448static void disconnect_to_bam(void)
1449{
1450 struct list_head *node;
1451 struct rx_pkt_info *info;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001452 unsigned long flags;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001453
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001454 bam_connection_is_active = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001455
1456 /* handle disconnect during active UL */
1457 write_lock_irqsave(&ul_wakeup_lock, flags);
1458 if (bam_is_connected) {
1459 bam_dmux_log("%s: UL active - forcing powerdown\n", __func__);
1460 ul_powerdown();
1461 }
1462 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1463 ul_powerdown_finish();
1464
1465 /* tear down BAM connection */
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001466 INIT_COMPLETION(bam_connection_completion);
1467 sps_disconnect(bam_tx_pipe);
1468 sps_disconnect(bam_rx_pipe);
1469 unvote_dfab();
1470 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1471 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001472
1473 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001474 while (!list_empty(&bam_rx_pool)) {
1475 node = bam_rx_pool.next;
1476 list_del(node);
1477 info = container_of(node, struct rx_pkt_info, list_node);
1478 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1479 DMA_FROM_DEVICE);
1480 dev_kfree_skb_any(info->skb);
1481 kfree(info);
1482 }
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001483 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmberg878923a2012-01-10 14:28:19 -07001484
1485 verify_tx_queue_is_empty(__func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001486}
1487
1488static void vote_dfab(void)
1489{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001490 int rc;
1491
Eric Holmberg006057d2012-01-11 10:10:42 -07001492 bam_dmux_log("%s\n", __func__);
1493 mutex_lock(&dfab_status_lock);
1494 if (dfab_is_on) {
1495 bam_dmux_log("%s: dfab is already on\n", __func__);
1496 mutex_unlock(&dfab_status_lock);
1497 return;
1498 }
Jeff Hugoca0caa82011-12-05 16:05:23 -07001499 rc = clk_enable(dfab_clk);
1500 if (rc)
Eric Holmberg006057d2012-01-11 10:10:42 -07001501 DMUX_LOG_KERR("bam_dmux vote for dfab failed rc = %d\n", rc);
1502 dfab_is_on = 1;
1503 mutex_unlock(&dfab_status_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001504}
1505
1506static void unvote_dfab(void)
1507{
Eric Holmberg006057d2012-01-11 10:10:42 -07001508 bam_dmux_log("%s\n", __func__);
1509 mutex_lock(&dfab_status_lock);
1510 if (!dfab_is_on) {
1511 DMUX_LOG_KERR("%s: dfab is already off\n", __func__);
1512 dump_stack();
1513 mutex_unlock(&dfab_status_lock);
1514 return;
1515 }
Jeff Hugoca0caa82011-12-05 16:05:23 -07001516 clk_disable(dfab_clk);
Eric Holmberg006057d2012-01-11 10:10:42 -07001517 dfab_is_on = 0;
1518 mutex_unlock(&dfab_status_lock);
1519}
1520
1521/* reference counting wrapper around wakelock */
1522static void grab_wakelock(void)
1523{
1524 unsigned long flags;
1525
1526 spin_lock_irqsave(&wakelock_reference_lock, flags);
1527 bam_dmux_log("%s: ref count = %d\n", __func__,
1528 wakelock_reference_count);
1529 if (wakelock_reference_count == 0)
1530 wake_lock(&bam_wakelock);
1531 ++wakelock_reference_count;
1532 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1533}
1534
1535static void release_wakelock(void)
1536{
1537 unsigned long flags;
1538
1539 spin_lock_irqsave(&wakelock_reference_lock, flags);
1540 if (wakelock_reference_count == 0) {
1541 DMUX_LOG_KERR("%s: bam_dmux wakelock not locked\n", __func__);
1542 dump_stack();
1543 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1544 return;
1545 }
1546 bam_dmux_log("%s: ref count = %d\n", __func__,
1547 wakelock_reference_count);
1548 --wakelock_reference_count;
1549 if (wakelock_reference_count == 0)
1550 wake_unlock(&bam_wakelock);
1551 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001552}
1553
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001554static int restart_notifier_cb(struct notifier_block *this,
1555 unsigned long code,
1556 void *data)
1557{
1558 int i;
1559 struct list_head *node;
1560 struct tx_pkt_info *info;
1561 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001562 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001563
1564 if (code != SUBSYS_AFTER_SHUTDOWN)
1565 return NOTIFY_DONE;
1566
Eric Holmberg878923a2012-01-10 14:28:19 -07001567 bam_dmux_log("%s: begin\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001568 in_global_reset = 1;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001569
1570 /* Handle uplink Powerdown */
1571 write_lock_irqsave(&ul_wakeup_lock, flags);
1572 if (bam_is_connected) {
1573 ul_powerdown();
1574 wait_for_ack = 0;
1575 }
1576 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1577 ul_powerdown_finish();
Eric Holmberg006057d2012-01-11 10:10:42 -07001578 a2_pc_disabled = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001579
1580 /* Cleanup Channel States */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001581 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1582 temp_remote_status = bam_ch_is_remote_open(i);
1583 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001584 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001585 if (bam_ch_is_local_open(i))
1586 bam_ch[i].status |= BAM_CH_IN_RESET;
1587 if (temp_remote_status) {
1588 platform_device_unregister(bam_ch[i].pdev);
1589 bam_ch[i].pdev = platform_device_alloc(
1590 bam_ch[i].name, 2);
1591 }
1592 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001593
1594 /* Cleanup pending UL data */
Jeff Hugo626303bf2011-11-21 11:43:28 -07001595 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001596 while (!list_empty(&bam_tx_pool)) {
1597 node = bam_tx_pool.next;
1598 list_del(node);
1599 info = container_of(node, struct tx_pkt_info,
1600 list_node);
1601 if (!info->is_cmd) {
1602 dma_unmap_single(NULL, info->dma_address,
1603 info->skb->len,
1604 DMA_TO_DEVICE);
1605 dev_kfree_skb_any(info->skb);
1606 } else {
1607 dma_unmap_single(NULL, info->dma_address,
1608 info->len,
1609 DMA_TO_DEVICE);
1610 kfree(info->skb);
1611 }
1612 kfree(info);
1613 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001614 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001615
Eric Holmberg878923a2012-01-10 14:28:19 -07001616 bam_dmux_log("%s: complete\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001617 return NOTIFY_DONE;
1618}
1619
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001620static int bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001621{
1622 u32 h;
1623 dma_addr_t dma_addr;
1624 int ret;
1625 void *a2_virt_addr;
1626 int i;
1627
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001628 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001629 /* init BAM */
1630 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1631 if (!a2_virt_addr) {
1632 pr_err("%s: ioremap failed\n", __func__);
1633 ret = -ENOMEM;
Jeff Hugo994a92d2012-01-05 13:25:21 -07001634 goto ioremap_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001635 }
1636 a2_props.phys_addr = A2_PHYS_BASE;
1637 a2_props.virt_addr = a2_virt_addr;
1638 a2_props.virt_size = A2_PHYS_SIZE;
1639 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001640 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001641 a2_props.num_pipes = A2_NUM_PIPES;
1642 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001643 if (cpu_is_msm9615())
1644 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001645 /* need to free on tear down */
1646 ret = sps_register_bam_device(&a2_props, &h);
1647 if (ret < 0) {
1648 pr_err("%s: register bam error %d\n", __func__, ret);
1649 goto register_bam_failed;
1650 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001651 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001652
1653 bam_tx_pipe = sps_alloc_endpoint();
1654 if (bam_tx_pipe == NULL) {
1655 pr_err("%s: tx alloc endpoint failed\n", __func__);
1656 ret = -ENOMEM;
1657 goto register_bam_failed;
1658 }
1659 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1660 if (ret) {
1661 pr_err("%s: tx get config failed %d\n", __func__, ret);
1662 goto tx_get_config_failed;
1663 }
1664
1665 tx_connection.source = SPS_DEV_HANDLE_MEM;
1666 tx_connection.src_pipe_index = 0;
1667 tx_connection.destination = h;
1668 tx_connection.dest_pipe_index = 4;
1669 tx_connection.mode = SPS_MODE_DEST;
1670 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1671 tx_desc_mem_buf.size = 0x800; /* 2k */
1672 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1673 &dma_addr, 0);
1674 if (tx_desc_mem_buf.base == NULL) {
1675 pr_err("%s: tx memory alloc failed\n", __func__);
1676 ret = -ENOMEM;
1677 goto tx_mem_failed;
1678 }
1679 tx_desc_mem_buf.phys_base = dma_addr;
1680 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1681 tx_connection.desc = tx_desc_mem_buf;
1682 tx_connection.event_thresh = 0x10;
1683
1684 ret = sps_connect(bam_tx_pipe, &tx_connection);
1685 if (ret < 0) {
1686 pr_err("%s: tx connect error %d\n", __func__, ret);
1687 goto tx_connect_failed;
1688 }
1689
1690 bam_rx_pipe = sps_alloc_endpoint();
1691 if (bam_rx_pipe == NULL) {
1692 pr_err("%s: rx alloc endpoint failed\n", __func__);
1693 ret = -ENOMEM;
1694 goto tx_connect_failed;
1695 }
1696 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1697 if (ret) {
1698 pr_err("%s: rx get config failed %d\n", __func__, ret);
1699 goto rx_get_config_failed;
1700 }
1701
1702 rx_connection.source = h;
1703 rx_connection.src_pipe_index = 5;
1704 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1705 rx_connection.dest_pipe_index = 1;
1706 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001707 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1708 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001709 rx_desc_mem_buf.size = 0x800; /* 2k */
1710 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1711 &dma_addr, 0);
1712 if (rx_desc_mem_buf.base == NULL) {
1713 pr_err("%s: rx memory alloc failed\n", __func__);
1714 ret = -ENOMEM;
1715 goto rx_mem_failed;
1716 }
1717 rx_desc_mem_buf.phys_base = dma_addr;
1718 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1719 rx_connection.desc = rx_desc_mem_buf;
1720 rx_connection.event_thresh = 0x10;
1721
1722 ret = sps_connect(bam_rx_pipe, &rx_connection);
1723 if (ret < 0) {
1724 pr_err("%s: rx connect error %d\n", __func__, ret);
1725 goto rx_connect_failed;
1726 }
1727
1728 tx_register_event.options = SPS_O_EOT;
1729 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1730 tx_register_event.xfer_done = NULL;
1731 tx_register_event.callback = bam_mux_tx_notify;
1732 tx_register_event.user = NULL;
1733 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1734 if (ret < 0) {
1735 pr_err("%s: tx register event error %d\n", __func__, ret);
1736 goto rx_event_reg_failed;
1737 }
1738
Jeff Hugo33dbc002011-08-25 15:52:53 -06001739 rx_register_event.options = SPS_O_EOT;
1740 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1741 rx_register_event.xfer_done = NULL;
1742 rx_register_event.callback = bam_mux_rx_notify;
1743 rx_register_event.user = NULL;
1744 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1745 if (ret < 0) {
1746 pr_err("%s: tx register event error %d\n", __func__, ret);
1747 goto rx_event_reg_failed;
1748 }
1749
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001750 bam_mux_initialized = 1;
1751 for (i = 0; i < NUM_BUFFERS; ++i)
1752 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001753 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001754 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001755 complete_all(&bam_connection_completion);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001756 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001757
1758rx_event_reg_failed:
1759 sps_disconnect(bam_rx_pipe);
1760rx_connect_failed:
1761 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1762 rx_desc_mem_buf.phys_base);
1763rx_mem_failed:
1764 sps_disconnect(bam_tx_pipe);
1765rx_get_config_failed:
1766 sps_free_endpoint(bam_rx_pipe);
1767tx_connect_failed:
1768 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1769 tx_desc_mem_buf.phys_base);
1770tx_get_config_failed:
1771 sps_free_endpoint(bam_tx_pipe);
1772tx_mem_failed:
1773 sps_deregister_bam_device(h);
1774register_bam_failed:
Jeff Hugo994a92d2012-01-05 13:25:21 -07001775ioremap_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776 /*destroy_workqueue(bam_mux_workqueue);*/
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001777 return ret;
1778}
1779
1780static int bam_init_fallback(void)
1781{
1782 u32 h;
1783 int ret;
1784 void *a2_virt_addr;
1785
1786 unvote_dfab();
1787 /* init BAM */
1788 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1789 if (!a2_virt_addr) {
1790 pr_err("%s: ioremap failed\n", __func__);
1791 ret = -ENOMEM;
1792 goto ioremap_failed;
1793 }
1794 a2_props.phys_addr = A2_PHYS_BASE;
1795 a2_props.virt_addr = a2_virt_addr;
1796 a2_props.virt_size = A2_PHYS_SIZE;
1797 a2_props.irq = A2_BAM_IRQ;
1798 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
1799 a2_props.num_pipes = A2_NUM_PIPES;
1800 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
1801 if (cpu_is_msm9615())
1802 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
1803 ret = sps_register_bam_device(&a2_props, &h);
1804 if (ret < 0) {
1805 pr_err("%s: register bam error %d\n", __func__, ret);
1806 goto register_bam_failed;
1807 }
1808 a2_device_handle = h;
1809
1810 return 0;
1811
1812register_bam_failed:
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001813ioremap_failed:
1814 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001815}
Jeff Hugoade1f842011-08-03 15:53:59 -06001816
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001817static void toggle_apps_ack(void)
1818{
1819 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
Eric Holmberg878923a2012-01-10 14:28:19 -07001820
1821 bam_dmux_log("%s: apps ack %d->%d\n", __func__,
1822 clear_bit & 0x1, ~clear_bit & 0x1);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001823 smsm_change_state(SMSM_APPS_STATE,
1824 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1825 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1826 clear_bit = ~clear_bit;
1827}
1828
Jeff Hugoade1f842011-08-03 15:53:59 -06001829static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1830{
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001831 int ret = 0;
Eric Holmberg878923a2012-01-10 14:28:19 -07001832
1833 bam_dmux_power_state = new_state & SMSM_A2_POWER_CONTROL ? 1 : 0;
1834 bam_dmux_log("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
1835 new_state);
1836
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001837 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
Eric Holmberg878923a2012-01-10 14:28:19 -07001838 bam_dmux_log("%s: reconnect\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001839 grab_wakelock();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001840 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001841 } else if (bam_mux_initialized &&
1842 !(new_state & SMSM_A2_POWER_CONTROL)) {
Eric Holmberg878923a2012-01-10 14:28:19 -07001843 bam_dmux_log("%s: disconnect\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001844 disconnect_to_bam();
Eric Holmberg006057d2012-01-11 10:10:42 -07001845 release_wakelock();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001846 } else if (new_state & SMSM_A2_POWER_CONTROL) {
Eric Holmberg878923a2012-01-10 14:28:19 -07001847 bam_dmux_log("%s: init\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001848 grab_wakelock();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001849 ret = bam_init();
1850 if (ret) {
1851 ret = bam_init_fallback();
1852 if (ret)
1853 pr_err("%s: bam init fallback failed: %d",
1854 __func__, ret);
1855 }
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001856 } else {
Eric Holmberg878923a2012-01-10 14:28:19 -07001857 bam_dmux_log("%s: bad state change\n", __func__);
Jeff Hugoade1f842011-08-03 15:53:59 -06001858 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001859 }
Jeff Hugoade1f842011-08-03 15:53:59 -06001860
1861}
1862
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001863static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
1864 uint32_t new_state)
1865{
Eric Holmberg878923a2012-01-10 14:28:19 -07001866 bam_dmux_log("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
1867 new_state);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001868 complete_all(&ul_wakeup_ack_completion);
1869}
1870
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001871static int bam_dmux_probe(struct platform_device *pdev)
1872{
1873 int rc;
1874
1875 DBG("%s probe called\n", __func__);
1876 if (bam_mux_initialized)
1877 return 0;
1878
Stephen Boyd1c51a492011-10-26 12:11:47 -07001879 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001880 if (IS_ERR(dfab_clk)) {
1881 pr_err("%s: did not get dfab clock\n", __func__);
1882 return -EFAULT;
1883 }
1884
1885 rc = clk_set_rate(dfab_clk, 64000000);
1886 if (rc)
1887 pr_err("%s: unable to set dfab clock rate\n", __func__);
1888
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001889 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
1890 if (!bam_mux_rx_workqueue)
1891 return -ENOMEM;
1892
1893 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
1894 if (!bam_mux_tx_workqueue) {
1895 destroy_workqueue(bam_mux_rx_workqueue);
1896 return -ENOMEM;
1897 }
1898
Jeff Hugo7960abd2011-08-02 15:39:38 -06001899 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001900 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06001901 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
1902 "bam_dmux_ch_%d", rc);
1903 /* bus 2, ie a2 stream 2 */
1904 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
1905 if (!bam_ch[rc].pdev) {
1906 pr_err("%s: platform device alloc failed\n", __func__);
1907 destroy_workqueue(bam_mux_rx_workqueue);
1908 destroy_workqueue(bam_mux_tx_workqueue);
1909 return -ENOMEM;
1910 }
1911 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001912
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001913 init_completion(&ul_wakeup_ack_completion);
1914 init_completion(&bam_connection_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07001915 init_completion(&dfab_unvote_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001916 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001917 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001918
Jeff Hugoade1f842011-08-03 15:53:59 -06001919 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
1920 bam_dmux_smsm_cb, NULL);
1921
1922 if (rc) {
1923 destroy_workqueue(bam_mux_rx_workqueue);
1924 destroy_workqueue(bam_mux_tx_workqueue);
1925 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
1926 return -ENOMEM;
1927 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001928
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001929 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
1930 bam_dmux_smsm_ack_cb, NULL);
1931
1932 if (rc) {
1933 destroy_workqueue(bam_mux_rx_workqueue);
1934 destroy_workqueue(bam_mux_tx_workqueue);
1935 smsm_state_cb_deregister(SMSM_MODEM_STATE,
1936 SMSM_A2_POWER_CONTROL,
1937 bam_dmux_smsm_cb, NULL);
1938 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
1939 rc);
1940 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
1941 platform_device_put(bam_ch[rc].pdev);
1942 return -ENOMEM;
1943 }
1944
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07001945 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
1946 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
1947
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001948 return 0;
1949}
1950
1951static struct platform_driver bam_dmux_driver = {
1952 .probe = bam_dmux_probe,
1953 .driver = {
1954 .name = "BAM_RMNT",
1955 .owner = THIS_MODULE,
1956 },
1957};
1958
1959static int __init bam_dmux_init(void)
1960{
Eric Holmberg878923a2012-01-10 14:28:19 -07001961 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001962#ifdef CONFIG_DEBUG_FS
1963 struct dentry *dent;
1964
1965 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001966 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001967 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001968 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
1969 debug_create("stats", 0444, dent, debug_stats);
Eric Holmberg878923a2012-01-10 14:28:19 -07001970 debugfs_create_file("log", 0444, dent, debug_log,
1971 &debug_ops_multiple);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001972 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001973#endif
Eric Holmberg878923a2012-01-10 14:28:19 -07001974 ret = kfifo_alloc(&bam_dmux_state_log, PAGE_SIZE, GFP_KERNEL);
1975 if (ret) {
1976 pr_err("%s: failed to allocate log %d\n", __func__, ret);
1977 bam_dmux_state_logging_disabled = 1;
1978 }
1979
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001980 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001981 return platform_driver_register(&bam_dmux_driver);
1982}
1983
Jeff Hugoade1f842011-08-03 15:53:59 -06001984late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001985MODULE_DESCRIPTION("MSM BAM DMUX");
1986MODULE_LICENSE("GPL v2");