blob: 25baedb0929811dcd5c9acecd14d4f02e8f57f61 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010-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 * SDIO-Abstraction-Layer Module.
15 *
16 * To be used with Qualcomm's SDIO-Client connected to this host.
17 */
18#include <sdio_al_private.h>
19
20#include <linux/module.h>
21#include <linux/scatterlist.h>
22#include <linux/workqueue.h>
23#include <linux/wait.h>
24#include <linux/delay.h>
25#include <linux/fs.h>
26#include <linux/slab.h>
27#include <linux/wakelock.h>
28#include <linux/mmc/core.h>
29#include <linux/mmc/card.h>
30#include <linux/mmc/host.h>
31#include <linux/mmc/mmc.h>
32#include <linux/mmc/sdio.h>
33#include <linux/mmc/sdio_func.h>
34#include <linux/mmc/sdio_ids.h>
35#include <linux/gpio.h>
36#include <linux/dma-mapping.h>
37#include <linux/earlysuspend.h>
38#include <linux/debugfs.h>
39#include <linux/uaccess.h>
40#include <linux/syscalls.h>
41
42#include <mach/dma.h>
43#include <mach/gpio.h>
44#include <mach/subsystem_notif.h>
45
46#include "../../../drivers/mmc/host/msm_sdcc.h"
47
48/**
49 * Func#0 has SDIO standard registers
50 * Func#1 is for Mailbox.
51 * Functions 2..7 are for channels.
52 * Currently only functions 2..5 are active due to SDIO-Client
53 * number of pipes.
54 *
55 */
56#define SDIO_AL_MAX_CHANNELS 6
57
58/** Func 1..5 */
59#define SDIO_AL_MAX_FUNCS (SDIO_AL_MAX_CHANNELS+1)
60#define SDIO_AL_WAKEUP_FUNC 6
61
62/** Number of SDIO-Client pipes */
63#define SDIO_AL_MAX_PIPES 16
64#define SDIO_AL_ACTIVE_PIPES 8
65
66/** CMD53/CMD54 Block size */
Yaniv Gardie5370b82011-07-27 11:46:51 +030067#define SDIO_AL_BLOCK_SIZE 256
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068
69/** Func#1 hardware Mailbox base address */
70#define HW_MAILBOX_ADDR 0x1000
71
72/** Func#1 peer sdioc software version.
73 * The header is duplicated also to the mailbox of the other
74 * functions. It can be used before other functions are enabled. */
75#define SDIOC_SW_HEADER_ADDR 0x0400
76
77/** Func#2..7 software Mailbox base address at 16K */
78#define SDIOC_SW_MAILBOX_ADDR 0x4000
79
80/** Some Mailbox registers address, written by host for
81 control */
82#define PIPES_THRESHOLD_ADDR 0x01000
83
84#define PIPES_0_7_IRQ_MASK_ADDR 0x01048
85
86#define PIPES_8_15_IRQ_MASK_ADDR 0x0104C
87
88#define FUNC_1_4_MASK_IRQ_ADDR 0x01040
89#define FUNC_5_7_MASK_IRQ_ADDR 0x01044
90#define FUNC_1_4_USER_IRQ_ADDR 0x01050
91#define FUNC_5_7_USER_IRQ_ADDR 0x01054
92
93#define EOT_PIPES_ENABLE 0x00
94
95/** Maximum read/write data available is SDIO-Client limitation */
96#define MAX_DATA_AVAILABLE (16*1024)
97#define INVALID_DATA_AVAILABLE (0x8000)
98
99/** SDIO-Client HW threshold to generate interrupt to the
100 * SDIO-Host on write available bytes.
101 */
102#define DEFAULT_WRITE_THRESHOLD (1024)
103
104/** SDIO-Client HW threshold to generate interrupt to the
105 * SDIO-Host on read available bytes, for streaming (non
106 * packet) rx data.
107 */
108#define DEFAULT_READ_THRESHOLD (1024)
109
Maya Erez8ed0a9a2011-07-19 14:46:53 +0300110/* Extra bytes to ensure getting the rx threshold interrupt on stream channels
111 when restoring the threshold after sleep */
112#define THRESHOLD_CHANGE_EXTRA_BYTES (100)
113
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114/** SW threshold to trigger reading the mailbox. */
115#define DEFAULT_MIN_WRITE_THRESHOLD (1024)
116#define DEFAULT_MIN_WRITE_THRESHOLD_STREAMING (1600)
117
118#define THRESHOLD_DISABLE_VAL (0xFFFFFFFF)
119
120
121/** Mailbox polling time for packet channels */
122#define DEFAULT_POLL_DELAY_MSEC 10
123/** Mailbox polling time for streaming channels */
124#define DEFAULT_POLL_DELAY_NOPACKET_MSEC 30
125
126/** The SDIO-Client prepares N buffers of size X per Tx pipe.
127 * Even when the transfer fills a partial buffer,
128 * that buffer becomes unusable for the next transfer. */
129#define DEFAULT_PEER_TX_BUF_SIZE (128)
130
131#define ROUND_UP(x, n) (((x + n - 1) / n) * n)
132
133/** Func#2..7 FIFOs are r/w via
134 sdio_readsb() & sdio_writesb(),when inc_addr=0 */
135#define PIPE_RX_FIFO_ADDR 0x00
136#define PIPE_TX_FIFO_ADDR 0x00
137
138/** Inactivity time to go to sleep in mseconds */
139#define INACTIVITY_TIME_MSEC 30
140#define INITIAL_INACTIVITY_TIME_MSEC 5000
141
142/** Context validity check */
143#define SDIO_AL_SIGNATURE 0xAABBCCDD
144
145/* Vendor Specific Command */
146#define SD_IO_RW_EXTENDED_QCOM 54
147
148#define TIME_TO_WAIT_US 500
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300149#define SDIO_CLOSE_FLUSH_TIMEOUT_MSEC (10000)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150
151#define SDIO_TEST_POSTFIX "_TEST"
152
153#define DATA_DEBUG(x...) if (sdio_al->debug.debug_data_on) pr_info(x)
154#define LPM_DEBUG(x...) if (sdio_al->debug.debug_lpm_on) pr_info(x)
155
156
157/* The index of the SDIO card used for the sdio_al_dloader */
158#define SDIO_BOOTLOADER_CARD_INDEX 1
159
160
161/* SDIO card state machine */
162enum sdio_al_device_state {
163 CARD_INSERTED,
164 CARD_REMOVED,
165 MODEM_RESTART
166};
167
168struct sdio_al_debug {
169
170 u8 debug_lpm_on;
171 u8 debug_data_on;
172 struct dentry *sdio_al_debug_root;
173 struct dentry *sdio_al_debug_lpm_on;
174 struct dentry *sdio_al_debug_data_on;
175 struct dentry *sdio_al_debug_info;
176};
177
178/* Polling time for the inactivity timer for devices that doesn't have
179 * a streaming channel
180 */
181#define SDIO_AL_POLL_TIME_NO_STREAMING 30
182
183#define CHAN_TO_FUNC(x) ((x) + 2 - 1)
184
185/**
186 * Mailbox structure.
187 * The Mailbox is located on the SDIO-Client Function#1.
188 * The mailbox size is 128 bytes, which is one block.
189 * The mailbox allows the host ton:
190 * 1. Get the number of available bytes on the pipes.
191 * 2. Enable/Disable SDIO-Client interrupt, related to pipes.
192 * 3. Set the Threshold for generating interrupt.
193 *
194 */
195struct sdio_mailbox {
196 u32 pipe_bytes_threshold[SDIO_AL_MAX_PIPES]; /* Addr 0x1000 */
197
198 /* Mask USER interrupts generated towards host - Addr 0x1040 */
199 u32 mask_irq_func_1:8; /* LSB */
200 u32 mask_irq_func_2:8;
201 u32 mask_irq_func_3:8;
202 u32 mask_irq_func_4:8;
203
204 u32 mask_irq_func_5:8;
205 u32 mask_irq_func_6:8;
206 u32 mask_irq_func_7:8;
207 u32 mask_mutex_irq:8;
208
209 /* Mask PIPE interrupts generated towards host - Addr 0x1048 */
210 u32 mask_eot_pipe_0_7:8;
211 u32 mask_thresh_above_limit_pipe_0_7:8;
212 u32 mask_overflow_pipe_0_7:8;
213 u32 mask_underflow_pipe_0_7:8;
214
215 u32 mask_eot_pipe_8_15:8;
216 u32 mask_thresh_above_limit_pipe_8_15:8;
217 u32 mask_overflow_pipe_8_15:8;
218 u32 mask_underflow_pipe_8_15:8;
219
220 /* Status of User interrupts generated towards host - Addr 0x1050 */
221 u32 user_irq_func_1:8;
222 u32 user_irq_func_2:8;
223 u32 user_irq_func_3:8;
224 u32 user_irq_func_4:8;
225
226 u32 user_irq_func_5:8;
227 u32 user_irq_func_6:8;
228 u32 user_irq_func_7:8;
229 u32 user_mutex_irq:8;
230
231 /* Status of PIPE interrupts generated towards host */
232 /* Note: All sources are cleared once they read. - Addr 0x1058 */
233 u32 eot_pipe_0_7:8;
234 u32 thresh_above_limit_pipe_0_7:8;
235 u32 overflow_pipe_0_7:8;
236 u32 underflow_pipe_0_7:8;
237
238 u32 eot_pipe_8_15:8;
239 u32 thresh_above_limit_pipe_8_15:8;
240 u32 overflow_pipe_8_15:8;
241 u32 underflow_pipe_8_15:8;
242
243 u16 pipe_bytes_avail[SDIO_AL_MAX_PIPES];
244};
245
246/** Track pending Rx Packet size */
247struct rx_packet_size {
248 u32 size; /* in bytes */
249 struct list_head list;
250};
251
252#define PEER_SDIOC_SW_MAILBOX_SIGNATURE 0xFACECAFE
253#define PEER_SDIOC_SW_MAILBOX_UT_SIGNATURE 0x5D107E57
254#define PEER_SDIOC_SW_MAILBOX_BOOT_SIGNATURE 0xDEADBEEF
255
256/* Allow support in old sdio version */
257#define PEER_SDIOC_OLD_VERSION_MAJOR 0x0002
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258#define INVALID_SDIO_CHAN 0xFF
259
260/**
261 * Peer SDIO-Client software header.
262 */
263struct peer_sdioc_sw_header {
264 u32 signature;
265 u32 version;
266 u32 max_channels;
267 char channel_names[SDIO_AL_MAX_CHANNELS][PEER_CHANNEL_NAME_SIZE];
268 u32 reserved[23];
269};
270
271struct peer_sdioc_boot_sw_header {
272 u32 signature;
273 u32 version;
274 u32 boot_ch_num;
275 u32 reserved[29]; /* 32 - previous fields */
276};
277
278/**
279 * Peer SDIO-Client software mailbox.
280 */
281struct peer_sdioc_sw_mailbox {
282 struct peer_sdioc_sw_header sw_header;
283 struct peer_sdioc_channel_config ch_config[SDIO_AL_MAX_CHANNELS];
284};
285
286/**
287 * SDIO Abstraction Layer driver context.
288 *
289 * @pdata -
290 * @debug -
291 * @devices - an array of the the devices claimed by sdio_al
292 * @unittest_mode - a flag to indicate if sdio_al is in
293 * unittest mode
294 * @bootloader_dev - the device which is used for the
295 * bootloader
296 * @subsys_notif_handle - handle for modem restart
297 * notifications
298 *
299 */
300struct sdio_al {
301 struct sdio_al_platform_data *pdata;
302 struct sdio_al_debug debug;
303 struct sdio_al_device *devices[MAX_NUM_OF_SDIO_DEVICES];
304 int unittest_mode;
305 struct sdio_al_device *bootloader_dev;
306 void *subsys_notif_handle;
307 int sdioc_major;
308};
309
310struct sdio_al_work {
311 struct work_struct work;
312 struct sdio_al_device *sdio_al_dev;
313};
314
315
316/**
317 * SDIO Abstraction Layer device context.
318 *
319 * @card - card claimed.
320 *
321 * @mailbox - A shadow of the SDIO-Client mailbox.
322 *
323 * @channel - Channels context.
324 *
325 * @workqueue - workqueue to read the mailbox and handle
326 * pending requests. Reading the mailbox should not happen
327 * in interrupt context.
328 *
329 * @work - work to submit to workqueue.
330 *
331 * @is_ready - driver is ready.
332 *
333 * @ask_mbox - Flag to request reading the mailbox,
334 * for different reasons.
335 *
336 * @wake_lock - Lock when can't sleep.
337 *
338 * @lpm_chan - Channel to use for LPM (low power mode)
339 * communication.
340 *
341 * @is_ok_to_sleep - Mark if driver is OK with going to sleep
342 * (no pending transactions).
343 *
344 * @inactivity_time - time allowed to be in inactivity before
345 * going to sleep
346 *
347 * @timer - timer to use for polling the mailbox.
348 *
349 * @poll_delay_msec - timer delay for polling the mailbox.
350 *
351 * @is_err - error detected.
352 *
353 * @signature - Context Validity Check.
354 *
355 * @flashless_boot_on - flag to indicate if sdio_al is in
356 * flshless boot mode
357 *
358 */
359struct sdio_al_device {
360 struct mmc_card *card;
361 struct sdio_mailbox *mailbox;
362 struct sdio_channel channel[SDIO_AL_MAX_CHANNELS];
363
364 struct peer_sdioc_sw_header *sdioc_sw_header;
365 struct peer_sdioc_boot_sw_header *sdioc_boot_sw_header;
366
367 struct workqueue_struct *workqueue;
368 struct sdio_al_work sdio_al_work;
369 struct sdio_al_work boot_work;
370
371 int is_ready;
372
373 wait_queue_head_t wait_mbox;
374 int ask_mbox;
375 int bootloader_done;
376
377 struct wake_lock wake_lock;
378 int lpm_chan;
379 int is_ok_to_sleep;
380 unsigned long inactivity_time;
381
382 struct timer_list timer;
383 u32 poll_delay_msec;
384 int is_timer_initialized;
385
386 int is_err;
387
388 u32 signature;
389
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390 unsigned int is_suspended;
391
392 int flashless_boot_on;
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300393 int ch_close_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394 int state;
395 int (*lpm_callback)(void *, int);
Maya Erez7b1ebd22011-08-20 20:53:24 +0300396
397 int print_after_interrupt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398};
399
400/*
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300401 * Host operation:
402 * lower 16bits are operation code
403 * upper 16bits are operation state
404 */
405#define PEER_OPERATION(op_code , op_state) ((op_code) | ((op_state) << 16))
406#define GET_PEER_OPERATION_CODE(op) ((op) & 0xffff)
407#define GET_PEER_OPERATION_STATE(op) ((op) >> 16)
408
409enum peer_op_code {
410 PEER_OP_CODE_CLOSE = 1
411};
412
413enum peer_op_state {
414 PEER_OP_STATE_INIT = 0,
415 PEER_OP_STATE_START = 1
416};
417
418
419/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420 * On the kernel command line specify
421 * sdio_al.debug_lpm_on=1 to enable the LPM debug messages
422 * By default the LPM debug messages are turned off
423 */
424static int debug_lpm_on;
425module_param(debug_lpm_on, int, 0);
426
427/*
428 * On the kernel command line specify
429 * sdio_al.debug_data_on=1 to enable the DATA debug messages
430 * By default the DATA debug messages are turned off
431 */
432static int debug_data_on;
433module_param(debug_data_on, int, 0);
434
435/** The driver context */
436static struct sdio_al *sdio_al;
437
438/* Static functions declaration */
439static int enable_eot_interrupt(struct sdio_al_device *sdio_al_dev,
440 int pipe_index, int enable);
441static int enable_threshold_interrupt(struct sdio_al_device *sdio_al_dev,
442 int pipe_index, int enable);
443static void sdio_func_irq(struct sdio_func *func);
444static void sdio_al_timer_handler(unsigned long data);
445static int get_min_poll_time_msec(struct sdio_al_device *sdio_al_dev);
446static u32 check_pending_rx_packet(struct sdio_channel *ch, u32 eot);
447static u32 remove_handled_rx_packet(struct sdio_channel *ch);
448static int set_pipe_threshold(struct sdio_al_device *sdio_al_dev,
449 int pipe_index, int threshold);
450static int sdio_al_wake_up(struct sdio_al_device *sdio_al_dev,
Maya Erez7b1ebd22011-08-20 20:53:24 +0300451 u32 not_from_int, struct sdio_channel *ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452static int sdio_al_client_setup(struct sdio_al_device *sdio_al_dev);
453static int enable_mask_irq(struct sdio_al_device *sdio_al_dev,
454 int func_num, int enable, u8 bit_offset);
455static int sdio_al_enable_func_retry(struct sdio_func *func, const char *name);
456static void sdio_al_print_info(void);
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300457static int sdio_read_internal(struct sdio_channel *ch, void *data, int len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458
459#define SDIO_AL_ERR(func) \
460 do { \
461 printk_once(KERN_ERR MODULE_NAME \
462 ":In Error state, ignore %s\n", \
463 func); \
464 sdio_al_print_info(); \
465 } while (0)
466
467#ifdef CONFIG_DEBUG_FS
468static int debug_info_open(struct inode *inode, struct file *file)
469{
470 file->private_data = inode->i_private;
471 return 0;
472}
473
474static ssize_t debug_info_write(struct file *file,
475 const char __user *buf, size_t count, loff_t *ppos)
476{
477 sdio_al_print_info();
478 return 1;
479}
480
481const struct file_operations debug_info_ops = {
482 .open = debug_info_open,
483 .write = debug_info_write,
484};
485
486/*
487*
488* Trigger on/off for debug messages
489* for trigger off the data messages debug level use:
490* echo 0 > /sys/kernel/debugfs/sdio_al/debug_data_on
491* for trigger on the data messages debug level use:
492* echo 1 > /sys/kernel/debugfs/sdio_al/debug_data_on
493* for trigger off the lpm messages debug level use:
494* echo 0 > /sys/kernel/debugfs/sdio_al/debug_lpm_on
495* for trigger on the lpm messages debug level use:
496* echo 1 > /sys/kernel/debugfs/sdio_al/debug_lpm_on
497*/
498static int sdio_al_debugfs_init(void)
499{
500 sdio_al->debug.sdio_al_debug_root = debugfs_create_dir("sdio_al", NULL);
501 if (!sdio_al->debug.sdio_al_debug_root)
502 return -ENOENT;
503
504 sdio_al->debug.sdio_al_debug_lpm_on = debugfs_create_u8("debug_lpm_on",
505 S_IRUGO | S_IWUGO,
506 sdio_al->debug.sdio_al_debug_root,
507 &sdio_al->debug.debug_lpm_on);
508
509 sdio_al->debug.sdio_al_debug_data_on = debugfs_create_u8(
510 "debug_data_on",
511 S_IRUGO | S_IWUGO,
512 sdio_al->debug.sdio_al_debug_root,
513 &sdio_al->debug.debug_data_on);
514
515 sdio_al->debug.sdio_al_debug_info = debugfs_create_file(
516 "sdio_debug_info",
517 S_IRUGO | S_IWUGO,
518 sdio_al->debug.sdio_al_debug_root,
519 NULL,
520 &debug_info_ops);
521
522 if ((!sdio_al->debug.sdio_al_debug_data_on) &&
523 (!sdio_al->debug.sdio_al_debug_lpm_on) &&
524 (!sdio_al->debug.sdio_al_debug_info)
525 ) {
526 debugfs_remove(sdio_al->debug.sdio_al_debug_root);
527 sdio_al->debug.sdio_al_debug_root = NULL;
528 return -ENOENT;
529 }
530 return 0;
531}
532
533static void sdio_al_debugfs_cleanup(void)
534{
535 debugfs_remove(sdio_al->debug.sdio_al_debug_lpm_on);
536 debugfs_remove(sdio_al->debug.sdio_al_debug_data_on);
537 debugfs_remove(sdio_al->debug.sdio_al_debug_info);
538 debugfs_remove(sdio_al->debug.sdio_al_debug_root);
539}
540#endif
541
542static int sdio_al_verify_func1(struct sdio_al_device *sdio_al_dev,
543 char const *func)
544{
545 if (sdio_al_dev == NULL) {
546 pr_err(MODULE_NAME ": %s: NULL sdio_al_dev\n", func);
547 return -ENODEV;
548 }
549 if (!sdio_al_dev->card) {
550 pr_err(MODULE_NAME ": %s: NULL card\n", func);
551 return -ENODEV;
552 }
553 if (!sdio_al_dev->card->sdio_func[0]) {
554 pr_err(MODULE_NAME ": %s: NULL func1\n", func);
555 return -ENODEV;
556 }
557 return 0;
558}
559
560
561static int sdio_al_verify_dev(struct sdio_al_device *sdio_al_dev,
562 char const *func)
563{
564 int ret;
565
566 ret = sdio_al_verify_func1(sdio_al_dev, func);
567 if (ret)
568 return ret;
569
570 if ((sdio_al_dev->state == MODEM_RESTART) ||
571 (sdio_al_dev->state == CARD_REMOVED)) {
572 pr_err(MODULE_NAME ": %s: device state %d\n", func,
573 sdio_al_dev->state);
574 return -ENODEV;
575 }
576 return 0;
577}
578
579static void sdio_al_get_into_err_state(struct sdio_al_device *sdio_al_dev)
580{
581 if ((!sdio_al) || (!sdio_al_dev))
582 return;
583
584 sdio_al_dev->is_err = true;
585 sdio_al->debug.debug_data_on = 0;
586 sdio_al->debug.debug_lpm_on = 0;
587 sdio_al_print_info();
588}
589
590void sdio_al_register_lpm_cb(void *device_handle,
591 int(*lpm_callback)(void *, int))
592{
593 struct sdio_al_device *sdio_al_dev =
594 (struct sdio_al_device *) device_handle;
595
596 if (!sdio_al_dev) {
597 pr_err(MODULE_NAME ": %s - device_handle is NULL\n",
598 __func__);
599 return;
600 }
601
602 if (lpm_callback) {
603 sdio_al_dev->lpm_callback = lpm_callback;
604 lpm_callback((void *)sdio_al_dev,
605 sdio_al_dev->is_ok_to_sleep);
606 }
607 LPM_DEBUG(MODULE_NAME ": %s - device %d registered for wakeup "
608 "callback\n", __func__, sdio_al_dev->card->host->index);
609}
610
611void sdio_al_unregister_lpm_cb(void *device_handle)
612{
613 struct sdio_al_device *sdio_al_dev =
614 (struct sdio_al_device *) device_handle;
615
616 if (!sdio_al_dev) {
617 pr_err(MODULE_NAME ": %s - device_handle is NULL\n",
618 __func__);
619 return;
620 }
621
622 sdio_al_dev->lpm_callback = NULL;
623 LPM_DEBUG(MODULE_NAME ": %s - device %d unregister for wakeup "
624 "callback\n", __func__, sdio_al_dev->card->host->index);
625}
626
627static void sdio_al_vote_for_sleep(struct sdio_al_device *sdio_al_dev,
628 int is_vote_for_sleep)
629{
630 pr_debug(MODULE_NAME ": %s()", __func__);
631
Yaniv Gardi3e327762011-07-27 11:11:04 +0300632 if (!sdio_al_dev) {
633 pr_err(MODULE_NAME ": %s - sdio_al_dev is NULL\n", __func__);
634 return;
635 }
636
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637 if (is_vote_for_sleep) {
Maya Erez7b1ebd22011-08-20 20:53:24 +0300638 pr_debug(MODULE_NAME ": %s - sdio vote for Sleep", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639 wake_unlock(&sdio_al_dev->wake_lock);
640 } else {
Maya Erez7b1ebd22011-08-20 20:53:24 +0300641 pr_debug(MODULE_NAME ": %s - sdio vote against sleep",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642 __func__);
643 wake_lock(&sdio_al_dev->wake_lock);
644 }
645
646 if (sdio_al_dev->lpm_callback != NULL) {
647 LPM_DEBUG(MODULE_NAME ": %s - is_vote_for_sleep=%d for "
648 "card#%d, calling callback...",
649 __func__,
650 is_vote_for_sleep,
651 sdio_al_dev->card->host->index);
652 sdio_al_dev->lpm_callback((void *)sdio_al_dev,
653 is_vote_for_sleep);
654 }
655}
656
657/**
658 * Write SDIO-Client lpm information
659 * Should only be called with host claimed.
660 */
661static int write_lpm_info(struct sdio_al_device *sdio_al_dev)
662{
663 struct sdio_func *lpm_func =
664 sdio_al_dev->card->sdio_func[sdio_al_dev->lpm_chan+1];
665 int offset = offsetof(struct peer_sdioc_sw_mailbox, ch_config)+
666 sizeof(struct peer_sdioc_channel_config) *
667 sdio_al_dev->lpm_chan+
668 offsetof(struct peer_sdioc_channel_config, is_host_ok_to_sleep);
669 int ret;
670
671 if (sdio_al_dev->lpm_chan == INVALID_SDIO_CHAN) {
672 pr_err(MODULE_NAME ":Invalid lpm_chan for card %d\n",
673 sdio_al_dev->card->host->index);
674 return -EINVAL;
675 }
676
677 pr_debug(MODULE_NAME ":write_lpm_info is_ok_to_sleep=%d, device %d\n",
678 sdio_al_dev->is_ok_to_sleep,
679 sdio_al_dev->card->host->index);
680
681 ret = sdio_memcpy_toio(lpm_func, SDIOC_SW_MAILBOX_ADDR+offset,
682 &sdio_al_dev->is_ok_to_sleep, sizeof(u32));
683 if (ret) {
684 pr_err(MODULE_NAME ":failed to write lpm info for card %d\n",
685 sdio_al_dev->card->host->index);
686 return ret;
687 }
688
689 return 0;
690}
691
692/* Set inactivity counter to intial value to allow clients come up */
693static inline void start_inactive_time(struct sdio_al_device *sdio_al_dev)
694{
695 sdio_al_dev->inactivity_time = jiffies +
696 msecs_to_jiffies(INITIAL_INACTIVITY_TIME_MSEC);
697}
698
699static inline void restart_inactive_time(struct sdio_al_device *sdio_al_dev)
700{
701 sdio_al_dev->inactivity_time = jiffies +
702 msecs_to_jiffies(INACTIVITY_TIME_MSEC);
703}
704
705static inline int is_inactive_time_expired(struct sdio_al_device *sdio_al_dev)
706{
707 return time_after(jiffies, sdio_al_dev->inactivity_time);
708}
709
710
711static int is_user_irq_enabled(struct sdio_al_device *sdio_al_dev,
712 int func_num)
713{
714 int ret = 0;
715 struct sdio_func *func1;
716 u32 user_irq = 0;
717 u32 addr = 0;
718 u32 offset = 0;
719 u32 masked_user_irq = 0;
720
721 if (sdio_al_verify_dev(sdio_al_dev, __func__))
722 return 0;
723 func1 = sdio_al_dev->card->sdio_func[0];
724
725 if (func_num < 4) {
726 addr = FUNC_1_4_USER_IRQ_ADDR;
727 offset = func_num * 8;
728 } else {
729 addr = FUNC_5_7_USER_IRQ_ADDR;
730 offset = (func_num - 4) * 8;
731 }
732
733 user_irq = sdio_readl(func1, addr, &ret);
734 if (ret) {
735 pr_debug(MODULE_NAME ":read_user_irq fail\n");
736 return 0;
737 }
738
739 masked_user_irq = (user_irq >> offset) && 0xFF;
740 if (masked_user_irq == 0x1) {
741 pr_info(MODULE_NAME ":user_irq enabled\n");
742 return 1;
743 }
744
745 return 0;
746}
747
748static void sdio_al_sleep(struct sdio_al_device *sdio_al_dev,
749 struct mmc_host *host)
750{
751 int i;
752
753 /* Go to sleep */
Maya Erez7b1ebd22011-08-20 20:53:24 +0300754 pr_debug(MODULE_NAME ":Inactivity timer expired."
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755 " Going to sleep\n");
756 /* Stop mailbox timer */
757 sdio_al_dev->poll_delay_msec = 0;
758 del_timer_sync(&sdio_al_dev->timer);
759 /* Make sure we get interrupt for non-packet-mode right away */
760 for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
761 struct sdio_channel *ch = &sdio_al_dev->channel[i];
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300762 if (ch->state != SDIO_CHANNEL_STATE_OPEN) {
763 pr_debug(MODULE_NAME ":continue for channel %s in"
764 " state %d\n", ch->name, ch->state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 continue;
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300766 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767 if (ch->is_packet_mode == false) {
768 ch->read_threshold = 1;
769 set_pipe_threshold(sdio_al_dev,
770 ch->rx_pipe_index,
771 ch->read_threshold);
772 }
773 }
Maya Erezfd915312011-07-14 13:45:34 +0300774 /* Prevent modem to go to sleep until we get the PROG_DONE on
775 the dummy CMD52 */
776 msmsdcc_set_pwrsave(sdio_al_dev->card->host, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 /* Mark HOST_OK_TOSLEEP */
778 sdio_al_dev->is_ok_to_sleep = 1;
779 write_lpm_info(sdio_al_dev);
780
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781 msmsdcc_lpm_enable(host);
782 LPM_DEBUG(MODULE_NAME ":Finished sleep sequence for card %d. "
783 "Sleep now.\n",
784 sdio_al_dev->card->host->index);
785 /* Release wakelock */
786 sdio_al_vote_for_sleep(sdio_al_dev, 1);
787}
788
789
790/**
791 * Read SDIO-Client Mailbox from Function#1.thresh_pipe
792 *
793 * The mailbox contain the bytes available per pipe,
794 * and the End-Of-Transfer indication per pipe (if available).
795 *
796 * WARNING: Each time the Mailbox is read from the client, the
797 * read_bytes_avail is incremented with another pending
798 * transfer. Therefore, a pending rx-packet should be added to a
799 * list before the next read of the mailbox.
800 *
801 * This function should run from a workqueue context since it
802 * notifies the clients.
803 *
804 * This function assumes that sdio_claim_host was called before
805 * calling it.
806 *
807 */
808static int read_mailbox(struct sdio_al_device *sdio_al_dev, int from_isr)
809{
810 int ret;
811 struct sdio_func *func1 = sdio_al_dev->card->sdio_func[0];
812 struct sdio_mailbox *mailbox = sdio_al_dev->mailbox;
813 struct mmc_host *host = func1->card->host;
814 u32 new_write_avail = 0;
815 u32 old_write_avail = 0;
816 u32 any_read_avail = 0;
817 u32 any_write_pending = 0;
818 int i;
819 u32 rx_notify_bitmask = 0;
820 u32 tx_notify_bitmask = 0;
821 u32 eot_pipe = 0;
822 u32 thresh_pipe = 0;
823 u32 overflow_pipe = 0;
824 u32 underflow_pipe = 0;
825 u32 thresh_intr_mask = 0;
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300826 int is_closing = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827
828 if (sdio_al_dev->is_err) {
829 SDIO_AL_ERR(__func__);
830 return 0;
831 }
832
833 pr_debug(MODULE_NAME ":start %s from_isr = %d for card %d.\n"
834 , __func__, from_isr, sdio_al_dev->card->host->index);
835
836 pr_debug(MODULE_NAME ":before sdio_memcpy_fromio.\n");
Maya Erez320a7ca2011-08-03 09:41:27 +0300837 memset(mailbox, 0, sizeof(struct sdio_mailbox));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838 ret = sdio_memcpy_fromio(func1, mailbox,
839 HW_MAILBOX_ADDR, sizeof(*mailbox));
840 pr_debug(MODULE_NAME ":after sdio_memcpy_fromio.\n");
Maya Erez320a7ca2011-08-03 09:41:27 +0300841 if (ret) {
842 pr_err(MODULE_NAME ":Fail to read Mailbox for card %d,"
843 " goto error state\n",
844 sdio_al_dev->card->host->index);
845 sdio_al_get_into_err_state(sdio_al_dev);
846 /* Stop the timer to stop reading the mailbox */
847 sdio_al_dev->poll_delay_msec = 0;
848 goto exit_err;
849 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850
851 eot_pipe = (mailbox->eot_pipe_0_7) |
852 (mailbox->eot_pipe_8_15<<8);
853 thresh_pipe = (mailbox->thresh_above_limit_pipe_0_7) |
854 (mailbox->thresh_above_limit_pipe_8_15<<8);
855
856 overflow_pipe = (mailbox->overflow_pipe_0_7) |
857 (mailbox->overflow_pipe_8_15<<8);
858 underflow_pipe = mailbox->underflow_pipe_0_7 |
859 (mailbox->underflow_pipe_8_15<<8);
860 thresh_intr_mask =
861 (mailbox->mask_thresh_above_limit_pipe_0_7) |
862 (mailbox->mask_thresh_above_limit_pipe_8_15<<8);
863
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864 if (overflow_pipe || underflow_pipe)
865 pr_err(MODULE_NAME ":Mailbox ERROR "
866 "overflow=0x%x, underflow=0x%x\n",
867 overflow_pipe, underflow_pipe);
868
869 /* In case of modem reset we would like to read the daya from the modem
870 to clear the interrupts but do not process it */
871 if (sdio_al_dev->state != CARD_INSERTED) {
872 pr_err(MODULE_NAME ":sdio_al_device (card %d) is in invalid "
873 "state %d\n",
874 sdio_al_dev->card->host->index,
875 sdio_al_dev->state);
876 return -ENODEV;
877 }
878
879 pr_debug(MODULE_NAME ":card %d: eot=0x%x, thresh=0x%x\n",
880 sdio_al_dev->card->host->index,
881 eot_pipe, thresh_pipe);
882
883 /* Scan for Rx Packets available and update read available bytes */
884 for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
885 struct sdio_channel *ch = &sdio_al_dev->channel[i];
886 u32 old_read_avail;
887 u32 read_avail;
888 u32 new_packet_size = 0;
889
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300890 if (ch->state == SDIO_CHANNEL_STATE_CLOSING)
891 is_closing = true; /* used to prevent sleep */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892
893 old_read_avail = ch->read_avail;
894 read_avail = mailbox->pipe_bytes_avail[ch->rx_pipe_index];
895
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300896 if ((ch->state == SDIO_CHANNEL_STATE_CLOSED) &&
897 (read_avail > 0)) {
898 /* Stop the timer to stop reading the mailbox */
899 sdio_al_dev->poll_delay_msec = 0;
900 pr_err(MODULE_NAME
901 ":Invalid read_avail 0x%x, old_read_avail=0x%x for CLOSED ch %s\n",
902 read_avail, old_read_avail, ch->name);
903 sdio_al_get_into_err_state(sdio_al_dev);
904 goto exit_err;
905 }
906 if ((ch->state != SDIO_CHANNEL_STATE_OPEN) &&
907 (ch->state != SDIO_CHANNEL_STATE_CLOSING))
908 continue;
909
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910 if (read_avail > INVALID_DATA_AVAILABLE) {
911 pr_err(MODULE_NAME
912 ":Invalid read_avail 0x%x for pipe %d\n",
913 read_avail, ch->rx_pipe_index);
914 continue;
915 }
916 any_read_avail |= read_avail | old_read_avail;
917 ch->statistics.last_any_read_avail = any_read_avail;
918 ch->statistics.last_read_avail = read_avail;
919 ch->statistics.last_old_read_avail = old_read_avail;
920
Maya Erez8ed0a9a2011-07-19 14:46:53 +0300921 if (ch->is_packet_mode) {
Maya Erez7b1ebd22011-08-20 20:53:24 +0300922 if ((eot_pipe & (1<<ch->rx_pipe_index)) &&
923 sdio_al_dev->print_after_interrupt) {
924 LPM_DEBUG(MODULE_NAME ":Interrupt on ch %s, "
925 "card %d", ch->name,
926 sdio_al_dev->card->host->index);
927 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928 new_packet_size = check_pending_rx_packet(ch, eot_pipe);
Maya Erez8ed0a9a2011-07-19 14:46:53 +0300929 } else {
Maya Erez7b1ebd22011-08-20 20:53:24 +0300930 if ((thresh_pipe & (1<<ch->rx_pipe_index)) &&
931 sdio_al_dev->print_after_interrupt) {
932 LPM_DEBUG(MODULE_NAME ":Interrupt on ch %s, "
933 "card %d", ch->name,
934 sdio_al_dev->card->host->index);
935 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936 ch->read_avail = read_avail;
Maya Erez8ed0a9a2011-07-19 14:46:53 +0300937 /* Restore default thresh for non packet channels */
938 if ((ch->read_threshold != ch->def_read_threshold) &&
939 (read_avail >= ch->threshold_change_cnt)) {
940 ch->read_threshold = ch->def_read_threshold;
941 set_pipe_threshold(sdio_al_dev,
942 ch->rx_pipe_index,
943 ch->read_threshold);
944 }
945 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946
947 if ((ch->is_packet_mode) && (new_packet_size > 0)) {
948 rx_notify_bitmask |= (1<<ch->num);
949 ch->statistics.total_notifs++;
950 }
951
952 if ((!ch->is_packet_mode) && (ch->read_avail > 0) &&
953 (old_read_avail == 0)) {
954 rx_notify_bitmask |= (1<<ch->num);
955 ch->statistics.total_notifs++;
956 }
957 }
Maya Erez7b1ebd22011-08-20 20:53:24 +0300958 sdio_al_dev->print_after_interrupt = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959
960 /* Update Write available */
961 for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
962 struct sdio_channel *ch = &sdio_al_dev->channel[i];
963
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300964 if ((ch->state != SDIO_CHANNEL_STATE_OPEN) &&
965 (ch->state != SDIO_CHANNEL_STATE_CLOSING))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966 continue;
967
968 new_write_avail = mailbox->pipe_bytes_avail[ch->tx_pipe_index];
969
970 if (new_write_avail > INVALID_DATA_AVAILABLE) {
971 pr_err(MODULE_NAME
972 ":Invalid write_avail 0x%x for pipe %d\n",
973 new_write_avail, ch->tx_pipe_index);
974 continue;
975 }
976
977 old_write_avail = ch->write_avail;
978 ch->write_avail = new_write_avail;
979
980 if ((old_write_avail <= ch->min_write_avail) &&
981 (new_write_avail >= ch->min_write_avail))
982 tx_notify_bitmask |= (1<<ch->num);
983
984 /* There is not enough write avail for this channel.
985 We need to keep reading mailbox to wait for the appropriate
986 write avail and cannot sleep. Ignore SMEM channel that has
987 only one direction. */
988 if (strcmp(ch->name, "SDIO_SMEM"))
989 any_write_pending |=
990 (new_write_avail < ch->ch_config.max_tx_threshold);
991 }
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300992 /* notify clients */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
994 struct sdio_channel *ch = &sdio_al_dev->channel[i];
995
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +0300996 if ((ch->state != SDIO_CHANNEL_STATE_OPEN) ||
997 (ch->notify == NULL))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998 continue;
999
1000 if (rx_notify_bitmask & (1<<ch->num))
1001 ch->notify(ch->priv,
1002 SDIO_EVENT_DATA_READ_AVAIL);
1003
1004 if (tx_notify_bitmask & (1<<ch->num))
1005 ch->notify(ch->priv,
1006 SDIO_EVENT_DATA_WRITE_AVAIL);
1007 }
1008
1009
1010 if ((rx_notify_bitmask == 0) && (tx_notify_bitmask == 0) &&
1011 !any_read_avail && !any_write_pending) {
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03001012 DATA_DEBUG(MODULE_NAME ":Nothing to Notify for card %d,"
1013 " is_closing=%d\n",
1014 sdio_al_dev->card->host->index, is_closing);
1015 if (is_closing)
1016 restart_inactive_time(sdio_al_dev);
1017 else if (is_inactive_time_expired(sdio_al_dev))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018 sdio_al_sleep(sdio_al_dev, host);
1019 } else {
1020 DATA_DEBUG(MODULE_NAME ":Notify bitmask for card %d "
1021 "rx=0x%x, tx=0x%x.\n",
1022 sdio_al_dev->card->host->index, rx_notify_bitmask,
1023 tx_notify_bitmask);
1024 /* Restart inactivity timer if any activity on the channel */
1025 restart_inactive_time(sdio_al_dev);
1026 }
1027
1028 pr_debug(MODULE_NAME ":end %s.\n", __func__);
1029
1030exit_err:
1031 return ret;
1032}
1033
1034/**
1035 * Check pending rx packet when reading the mailbox.
1036 */
1037static u32 check_pending_rx_packet(struct sdio_channel *ch, u32 eot)
1038{
1039 u32 rx_pending;
1040 u32 rx_avail;
1041 u32 new_packet_size = 0;
1042 struct sdio_al_device *sdio_al_dev = ch->sdio_al_dev;
1043
1044
1045 if (sdio_al_dev == NULL) {
1046 pr_err(MODULE_NAME ": NULL sdio_al_dev for channel %s\n",
1047 ch->name);
1048 return -EINVAL;
1049 }
1050
1051 mutex_lock(&ch->ch_lock);
1052
1053 rx_pending = ch->rx_pending_bytes;
1054 rx_avail = sdio_al_dev->mailbox->pipe_bytes_avail[ch->rx_pipe_index];
1055
1056 pr_debug(MODULE_NAME ":pipe %d of card %d rx_avail=0x%x, "
1057 "rx_pending=0x%x\n",
1058 ch->rx_pipe_index, sdio_al_dev->card->host->index, rx_avail,
1059 rx_pending);
1060
1061
1062 /* new packet detected */
1063 if (eot & (1<<ch->rx_pipe_index)) {
1064 struct rx_packet_size *p = NULL;
1065 new_packet_size = rx_avail - rx_pending;
1066
1067 if ((rx_avail <= rx_pending)) {
1068 pr_err(MODULE_NAME ":Invalid new packet size."
1069 " rx_avail=%d.\n", rx_avail);
1070 new_packet_size = 0;
1071 goto exit_err;
1072 }
1073
1074 p = kzalloc(sizeof(*p), GFP_KERNEL);
Maya Erezd9cc2292011-08-04 09:20:31 +03001075 if (p == NULL) {
1076 pr_err(MODULE_NAME ":failed to allocate item for "
1077 "rx_pending list. rx_avail=%d, rx_pending=%d.\n",
1078 rx_avail, rx_pending);
1079 new_packet_size = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080 goto exit_err;
Maya Erezd9cc2292011-08-04 09:20:31 +03001081 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 p->size = new_packet_size;
1083 /* Add new packet as last */
1084 list_add_tail(&p->list, &ch->rx_size_list_head);
1085 ch->rx_pending_bytes += new_packet_size;
1086
1087 if (ch->read_avail == 0)
1088 ch->read_avail = new_packet_size;
1089 }
1090
1091exit_err:
1092 mutex_unlock(&ch->ch_lock);
1093
1094 return new_packet_size;
1095}
1096
1097
1098
1099/**
1100 * Remove first pending packet from the list.
1101 */
1102static u32 remove_handled_rx_packet(struct sdio_channel *ch)
1103{
1104 struct rx_packet_size *p = NULL;
1105
1106 mutex_lock(&ch->ch_lock);
1107
1108 ch->rx_pending_bytes -= ch->read_avail;
1109
1110 if (!list_empty(&ch->rx_size_list_head)) {
1111 p = list_first_entry(&ch->rx_size_list_head,
1112 struct rx_packet_size, list);
1113 list_del(&p->list);
1114 kfree(p);
Maya Erezd9cc2292011-08-04 09:20:31 +03001115 } else {
1116 pr_err(MODULE_NAME ":%s: ch %s: unexpected empty list!!\n",
1117 __func__, ch->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118 }
1119
1120 if (list_empty(&ch->rx_size_list_head)) {
1121 ch->read_avail = 0;
1122 } else {
1123 p = list_first_entry(&ch->rx_size_list_head,
1124 struct rx_packet_size, list);
1125 ch->read_avail = p->size;
1126 }
1127
1128 mutex_unlock(&ch->ch_lock);
1129
1130 return ch->read_avail;
1131}
1132
1133
1134/**
1135 * Bootloader worker function.
1136 *
1137 * @note: clear the bootloader_done flag only after reading the
1138 * mailbox, to ignore more requests while reading the mailbox.
1139 */
1140static void boot_worker(struct work_struct *work)
1141{
1142 int ret = 0;
1143 int func_num = 0;
1144 int i;
1145 struct sdio_al_device *sdio_al_dev = NULL;
1146 struct sdio_al_work *sdio_al_work = container_of(work,
1147 struct sdio_al_work,
1148 work);
1149
1150 if (sdio_al_work == NULL) {
1151 pr_err(MODULE_NAME ": %s: NULL sdio_al_work\n", __func__);
1152 return;
1153 }
1154
1155 sdio_al_dev = sdio_al_work->sdio_al_dev;
1156 if (sdio_al_dev == NULL) {
1157 pr_err(MODULE_NAME ": %s: NULL sdio_al_dev\n", __func__);
1158 return;
1159 }
1160 pr_info(MODULE_NAME ":Bootloader Worker Started, "
1161 "wait for bootloader_done event..\n");
1162 wait_event(sdio_al_dev->wait_mbox,
1163 sdio_al_dev->bootloader_done);
1164 pr_info(MODULE_NAME ":Got bootloader_done event..\n");
1165 /* Do polling until MDM is up */
1166 for (i = 0; i < 5000; ++i) {
1167 if (sdio_al_verify_dev(sdio_al_dev, __func__))
1168 return;
1169 sdio_claim_host(sdio_al_dev->card->sdio_func[0]);
1170 if (is_user_irq_enabled(sdio_al_dev, func_num)) {
1171 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
1172 sdio_al_dev->bootloader_done = 0;
1173 ret = sdio_al_client_setup(sdio_al_dev);
1174 if (ret) {
1175 pr_err(MODULE_NAME ":"
1176 "sdio_al_client_setup failed, "
1177 "for card %d ret=%d\n",
1178 sdio_al_dev->card->host->index,
1179 ret);
1180 sdio_al_get_into_err_state(sdio_al_dev);
1181 }
1182 goto done;
1183 }
1184 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
1185 msleep(100);
1186 }
1187 pr_err(MODULE_NAME ":Timeout waiting for user_irq for card %d\n",
1188 sdio_al_dev->card->host->index);
1189 sdio_al_get_into_err_state(sdio_al_dev);
1190
1191done:
1192 pr_debug(MODULE_NAME ":Boot Worker for card %d Exit!\n",
1193 sdio_al_dev->card->host->index);
1194}
1195
1196/**
1197 * Worker function.
1198 *
1199 * @note: clear the ask_mbox flag only after
1200 * reading the mailbox, to ignore more requests while
1201 * reading the mailbox.
1202 */
1203static void worker(struct work_struct *work)
1204{
1205 int ret = 0;
1206 struct sdio_al_device *sdio_al_dev = NULL;
1207 struct sdio_al_work *sdio_al_work = container_of(work,
1208 struct sdio_al_work,
1209 work);
1210 if (sdio_al_work == NULL) {
1211 pr_err(MODULE_NAME ": worker: NULL sdio_al_work\n");
1212 return;
1213 }
1214
1215 sdio_al_dev = sdio_al_work->sdio_al_dev;
1216 if (sdio_al_dev == NULL) {
1217 pr_err(MODULE_NAME ": worker: NULL sdio_al_dev\n");
1218 return;
1219 }
1220 pr_debug(MODULE_NAME ":Worker Started..\n");
1221 while ((sdio_al_dev->is_ready) && (ret == 0)) {
1222 pr_debug(MODULE_NAME ":Wait for read mailbox request..\n");
1223 wait_event(sdio_al_dev->wait_mbox, sdio_al_dev->ask_mbox);
1224 if (sdio_al_verify_dev(sdio_al_dev, __func__))
1225 break;
1226 if (!sdio_al_dev->is_ready)
1227 break;
1228 sdio_claim_host(sdio_al_dev->card->sdio_func[0]);
1229 if (sdio_al_dev->is_ok_to_sleep) {
Maya Erez7b1ebd22011-08-20 20:53:24 +03001230 ret = sdio_al_wake_up(sdio_al_dev, 1, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231 if (ret) {
1232 sdio_release_host(
1233 sdio_al_dev->card->sdio_func[0]);
1234 return;
1235 }
1236 }
1237 ret = read_mailbox(sdio_al_dev, false);
1238 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
1239 sdio_al_dev->ask_mbox = false;
1240 }
1241 pr_debug(MODULE_NAME ":Worker Exit!\n");
1242}
1243
1244/**
1245 * Write command using CMD54 rather than CMD53.
1246 * Writing with CMD54 generate EOT interrupt at the
1247 * SDIO-Client.
1248 * Based on mmc_io_rw_extended()
1249 */
1250static int sdio_write_cmd54(struct mmc_card *card, unsigned fn,
1251 unsigned addr, const u8 *buf,
1252 unsigned blocks, unsigned blksz)
1253{
1254 struct mmc_request mrq;
1255 struct mmc_command cmd;
1256 struct mmc_data data;
1257 struct scatterlist sg;
1258 int incr_addr = 1; /* MUST */
1259 int write = 1;
1260
1261 BUG_ON(!card);
1262 BUG_ON(fn > 7);
1263 BUG_ON(blocks == 1 && blksz > 512);
1264 WARN_ON(blocks == 0);
1265 WARN_ON(blksz == 0);
1266
1267 write = true;
1268 pr_debug(MODULE_NAME ":sdio_write_cmd54()"
1269 "fn=%d,buf=0x%x,blocks=%d,blksz=%d\n",
1270 fn, (u32) buf, blocks, blksz);
1271
1272 memset(&mrq, 0, sizeof(struct mmc_request));
1273 memset(&cmd, 0, sizeof(struct mmc_command));
1274 memset(&data, 0, sizeof(struct mmc_data));
1275
1276 mrq.cmd = &cmd;
1277 mrq.data = &data;
1278
1279 cmd.opcode = SD_IO_RW_EXTENDED_QCOM;
1280
1281 cmd.arg = write ? 0x80000000 : 0x00000000;
1282 cmd.arg |= fn << 28;
1283 cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
1284 cmd.arg |= addr << 9;
1285 if (blocks == 1 && blksz <= 512)
1286 cmd.arg |= (blksz == 512) ? 0 : blksz; /* byte mode */
1287 else
1288 cmd.arg |= 0x08000000 | blocks; /* block mode */
1289 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
1290
1291 data.blksz = blksz;
1292 data.blocks = blocks;
1293 data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
1294 data.sg = &sg;
1295 data.sg_len = 1;
1296
1297 sg_init_one(&sg, buf, blksz * blocks);
1298
1299 mmc_set_data_timeout(&data, card);
1300
1301 mmc_wait_for_req(card->host, &mrq);
1302
1303 if (cmd.error)
1304 return cmd.error;
1305 if (data.error)
1306 return data.error;
1307
1308 if (mmc_host_is_spi(card->host)) {
1309 /* host driver already reported errors */
1310 } else {
1311 if (cmd.resp[0] & R5_ERROR) {
1312 pr_err(MODULE_NAME ":%s: R5_ERROR", __func__);
1313 return -EIO;
1314 }
1315 if (cmd.resp[0] & R5_FUNCTION_NUMBER) {
1316 pr_err(MODULE_NAME ":%s: R5_FUNCTION_NUMBER", __func__);
1317 return -EINVAL;
1318 }
1319 if (cmd.resp[0] & R5_OUT_OF_RANGE) {
1320 pr_err(MODULE_NAME ":%s: R5_OUT_OF_RANGE", __func__);
1321 return -ERANGE;
1322 }
1323 }
1324
1325 return 0;
1326}
1327
1328
1329/**
1330 * Write data to channel.
1331 * Handle different data size types.
1332 *
1333 */
1334static int sdio_ch_write(struct sdio_channel *ch, const u8 *buf, u32 len)
1335{
1336 int ret = 0;
1337 unsigned blksz = ch->func->cur_blksize;
1338 int blocks = len / blksz;
1339 int remain_bytes = len % blksz;
1340 struct mmc_card *card = NULL;
1341 u32 fn = ch->func->num;
1342
1343 if (len == 0) {
1344 pr_err(MODULE_NAME ":channel %s trying to write 0 bytes\n",
1345 ch->name);
1346 return -EINVAL;
1347 }
1348
1349 card = ch->func->card;
1350
1351 if (remain_bytes) {
1352 /* CMD53 */
1353 if (blocks) {
1354 ret = sdio_memcpy_toio(ch->func, PIPE_TX_FIFO_ADDR,
1355 (void *) buf, blocks*blksz);
1356 if (ret != 0) {
1357 pr_err(MODULE_NAME ":%s: sdio_memcpy_toio "
1358 "failed for channel %s\n",
1359 __func__, ch->name);
1360 ch->sdio_al_dev->is_err = true;
1361 return ret;
1362 }
1363 }
1364
1365 buf += (blocks*blksz);
1366
1367 ret = sdio_write_cmd54(card, fn, PIPE_TX_FIFO_ADDR,
1368 buf, 1, remain_bytes);
1369 } else {
1370 ret = sdio_write_cmd54(card, fn, PIPE_TX_FIFO_ADDR,
1371 buf, blocks, blksz);
1372 }
1373
1374 if (ret != 0) {
1375 pr_err(MODULE_NAME ":%s: sdio_write_cmd54 "
1376 "failed for channel %s\n",
1377 __func__, ch->name);
1378 ch->sdio_al_dev->is_err = true;
1379 return ret;
1380 }
1381
1382 return ret;
1383}
1384
1385static int sdio_al_bootloader_completed(void)
1386{
1387 int i;
1388
1389 pr_debug(MODULE_NAME ":sdio_al_bootloader_completed was called\n");
1390
1391 for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; ++i) {
1392 struct sdio_al_device *dev = NULL;
1393 if (sdio_al->devices[i] == NULL)
1394 continue;
1395 dev = sdio_al->devices[i];
1396 dev->bootloader_done = 1;
1397 wake_up(&dev->wait_mbox);
1398 }
1399
1400 return 0;
1401}
1402
1403static int sdio_al_wait_for_bootloader_comp(struct sdio_al_device *sdio_al_dev)
1404{
1405 int ret = 0;
1406
1407 struct sdio_func *func1;
1408
1409 if (sdio_al_verify_dev(sdio_al_dev, __func__))
1410 return -ENODEV;
1411 func1 = sdio_al_dev->card->sdio_func[0];
1412
1413 sdio_claim_host(func1);
1414 /*
1415 * Enable function 0 interrupt mask to allow 9k to raise this interrupt
1416 * in power-up. When sdio_downloader will notify its completion
1417 * we will poll on this interrupt to wait for 9k power-up
1418 */
1419 ret = enable_mask_irq(sdio_al_dev, 0, 1, 0);
1420 if (ret) {
1421 pr_err(MODULE_NAME ":Enable_mask_irq for card %d failed, "
1422 "ret=%d\n",
1423 sdio_al_dev->card->host->index, ret);
1424 sdio_release_host(func1);
1425 return ret;
1426 }
1427
1428 sdio_release_host(func1);
1429
1430 /*
1431 * Start bootloader worker that will wait for the bootloader
1432 * completion
1433 */
1434 sdio_al_dev->boot_work.sdio_al_dev = sdio_al_dev;
1435 INIT_WORK(&sdio_al_dev->boot_work.work, boot_worker);
1436 sdio_al_dev->bootloader_done = 0;
1437 queue_work(sdio_al_dev->workqueue, &sdio_al_dev->boot_work.work);
1438
1439 return 0;
1440}
1441
1442static int sdio_al_bootloader_setup(void)
1443{
1444 int ret = 0;
1445 struct sdio_func *func1;
1446 struct sdio_al_device *bootloader_dev = sdio_al->bootloader_dev;
1447
1448 if (bootloader_dev == NULL) {
1449 pr_err(MODULE_NAME ":No bootloader_dev\n");
1450 return -ENODEV;
1451 }
1452
1453
1454 if (bootloader_dev->flashless_boot_on) {
1455 pr_info(MODULE_NAME ":Already in boot process.\n");
1456 return 0;
1457 }
1458
1459 func1 = bootloader_dev->card->sdio_func[0];
1460 if (!func1) {
1461 pr_err(MODULE_NAME ": %s: NULL func1\n", __func__);
1462 return -ENODEV;
1463 }
1464
1465 bootloader_dev->sdioc_boot_sw_header
1466 = kzalloc(sizeof(*bootloader_dev->sdioc_boot_sw_header),
1467 GFP_KERNEL);
1468 if (bootloader_dev->sdioc_boot_sw_header == NULL) {
1469 pr_err(MODULE_NAME ":fail to allocate sdioc boot sw header.\n");
1470 return -ENOMEM;
1471 }
1472
1473 sdio_claim_host(func1);
1474
1475 ret = sdio_memcpy_fromio(func1,
1476 bootloader_dev->sdioc_boot_sw_header,
1477 SDIOC_SW_HEADER_ADDR,
1478 sizeof(struct peer_sdioc_boot_sw_header));
1479 if (ret) {
1480 pr_err(MODULE_NAME ":fail to read sdioc boot sw header.\n");
1481 sdio_release_host(func1);
1482 goto exit_err;
1483 }
1484
1485 if (bootloader_dev->sdioc_boot_sw_header->signature !=
1486 (u32) PEER_SDIOC_SW_MAILBOX_BOOT_SIGNATURE) {
1487 pr_err(MODULE_NAME ":invalid mailbox signature 0x%x.\n",
1488 bootloader_dev->sdioc_boot_sw_header->signature);
1489 sdio_release_host(func1);
1490 ret = -EINVAL;
1491 goto exit_err;
1492 }
1493
1494 /* Upper byte has to be equal - no backward compatibility for unequal */
1495 if ((bootloader_dev->sdioc_boot_sw_header->version >> 16) !=
1496 (sdio_al->pdata->peer_sdioc_boot_version_major)) {
1497 pr_err(MODULE_NAME ": HOST(0x%x) and CLIENT(0x%x) SDIO_AL BOOT "
1498 "VERSION don't match\n",
1499 ((sdio_al->pdata->peer_sdioc_boot_version_major<<16)+
1500 sdio_al->pdata->peer_sdioc_boot_version_minor),
1501 bootloader_dev->sdioc_boot_sw_header->version);
1502 sdio_release_host(func1);
1503 ret = -EIO;
1504 goto exit_err;
1505 }
1506
1507 pr_info(MODULE_NAME ": SDIOC BOOT SW version 0x%x\n",
1508 bootloader_dev->sdioc_boot_sw_header->version);
1509
1510 bootloader_dev->flashless_boot_on = true;
1511
1512 sdio_release_host(func1);
1513
1514 ret = sdio_al_wait_for_bootloader_comp(bootloader_dev);
1515 if (ret) {
1516 pr_err(MODULE_NAME ":sdio_al_wait_for_bootloader_comp failed, "
1517 "err=%d\n", ret);
1518 goto exit_err;
1519 }
1520
1521 ret = sdio_downloader_setup(bootloader_dev->card, 1,
1522 bootloader_dev->sdioc_boot_sw_header->boot_ch_num,
1523 sdio_al_bootloader_completed);
1524
1525 if (ret) {
1526 pr_err(MODULE_NAME ":sdio_downloader_setup failed, err=%d\n",
1527 ret);
1528 goto exit_err;
1529 }
1530
1531 pr_info(MODULE_NAME ":In Flashless boot, waiting for its "
1532 "completion\n");
1533
1534
1535exit_err:
1536 pr_info(MODULE_NAME ":free sdioc_boot_sw_header.\n");
1537 kfree(bootloader_dev->sdioc_boot_sw_header);
1538 bootloader_dev->sdioc_boot_sw_header = NULL;
1539 bootloader_dev = NULL;
1540
1541 return ret;
1542}
1543
1544
1545/**
1546 * Read SDIO-Client software header
1547 *
1548 */
1549static int read_sdioc_software_header(struct sdio_al_device *sdio_al_dev,
1550 struct peer_sdioc_sw_header *header)
1551{
1552 int ret;
1553 int i;
1554 int test_version = 0;
1555 int sdioc_test_version = 0;
1556
1557 pr_debug(MODULE_NAME ":reading sdioc sw header.\n");
1558
1559 if (sdio_al_verify_dev(sdio_al_dev, __func__))
1560 return -ENODEV;
1561
1562 ret = sdio_memcpy_fromio(sdio_al_dev->card->sdio_func[0], header,
1563 SDIOC_SW_HEADER_ADDR, sizeof(*header));
1564 if (ret) {
1565 pr_err(MODULE_NAME ":fail to read sdioc sw header.\n");
1566 goto exit_err;
1567 }
1568
1569 if (header->signature == (u32)PEER_SDIOC_SW_MAILBOX_UT_SIGNATURE) {
1570 pr_info(MODULE_NAME ":SDIOC SW unittest signature. 0x%x\n",
1571 header->signature);
1572 sdio_al->unittest_mode = true;
1573 /* Verify test code compatibility with the modem */
1574 sdioc_test_version = (header->version & 0xFF00) >> 8;
1575 test_version = sdio_al->pdata->peer_sdioc_version_minor >> 8;
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03001576 if (test_version != sdioc_test_version) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001577 pr_err(MODULE_NAME ":HOST(0x%x) and CLIENT(0x%x) "
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03001578 "testing VERSION don't match\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001579 test_version,
1580 sdioc_test_version);
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03001581 msleep(500);
1582 BUG();
1583 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001584 }
1585
1586 if ((header->signature != (u32) PEER_SDIOC_SW_MAILBOX_SIGNATURE) &&
1587 (header->signature != (u32) PEER_SDIOC_SW_MAILBOX_UT_SIGNATURE)) {
1588 pr_err(MODULE_NAME ":SDIOC SW invalid signature. 0x%x\n",
1589 header->signature);
1590 goto exit_err;
1591 }
1592 /* Upper byte has to be equal - no backward compatibility for unequal */
1593 sdio_al->sdioc_major = header->version >> 16;
1594 if (sdio_al->pdata->allow_sdioc_version_major_2) {
1595 if ((sdio_al->sdioc_major !=
1596 sdio_al->pdata->peer_sdioc_version_major) &&
1597 (sdio_al->sdioc_major != PEER_SDIOC_OLD_VERSION_MAJOR)) {
1598 pr_err(MODULE_NAME ": HOST(0x%x) and CLIENT(0x%x) "
1599 "SDIO_AL VERSION don't match\n",
1600 ((sdio_al->pdata->peer_sdioc_version_major<<16)+
1601 sdio_al->pdata->peer_sdioc_version_minor),
1602 header->version);
1603 goto exit_err;
1604 }
1605 } else {
1606 if (sdio_al->sdioc_major !=
1607 sdio_al->pdata->peer_sdioc_version_major) {
1608 pr_err(MODULE_NAME ": HOST(0x%x) and CLIENT(0x%x) "
1609 "SDIO_AL VERSION don't match\n",
1610 ((sdio_al->pdata->peer_sdioc_version_major<<16)+
1611 sdio_al->pdata->peer_sdioc_version_minor),
1612 header->version);
1613 goto exit_err;
1614 }
1615 }
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03001616 sdio_al_dev->ch_close_supported = (header->version & 0x000F) >=
1617 (sdio_al->pdata->peer_sdioc_version_minor & 0xF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001618
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03001619 pr_info(MODULE_NAME ":SDIOC SW version 0x%x, sdio_al major 0x%x"
1620 " minor 0x%x\n", header->version,
1621 sdio_al->sdioc_major,
1622 sdio_al->pdata->peer_sdioc_version_minor);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623
1624 sdio_al_dev->flashless_boot_on = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001625 for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
1626 struct sdio_channel *ch = &sdio_al_dev->channel[i];
1627
1628 /* Set default values */
1629 ch->read_threshold = DEFAULT_READ_THRESHOLD;
1630 ch->write_threshold = DEFAULT_WRITE_THRESHOLD;
1631 ch->min_write_avail = DEFAULT_MIN_WRITE_THRESHOLD;
1632 ch->is_packet_mode = true;
1633 ch->peer_tx_buf_size = DEFAULT_PEER_TX_BUF_SIZE;
1634 ch->poll_delay_msec = 0;
1635
1636 ch->num = i;
1637
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03001638 ch->func = sdio_al_dev->card->sdio_func[ch->num+1];
1639 ch->rx_pipe_index = ch->num*2;
1640 ch->tx_pipe_index = ch->num*2+1;
1641
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001642 memset(ch->name, 0, sizeof(ch->name));
1643
1644 if (header->channel_names[i][0]) {
1645 memcpy(ch->name, SDIO_PREFIX,
1646 strlen(SDIO_PREFIX));
1647 memcpy(ch->name + strlen(SDIO_PREFIX),
1648 header->channel_names[i],
1649 PEER_CHANNEL_NAME_SIZE);
1650
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03001651 ch->state = SDIO_CHANNEL_STATE_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001652 ch->sdio_al_dev = sdio_al_dev;
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03001653 } else {
1654 ch->state = SDIO_CHANNEL_STATE_INVALID;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001655 }
1656
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03001657 pr_info(MODULE_NAME ":Channel=%s, state=%d\n", ch->name,
1658 ch->state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001659 }
1660
1661 return 0;
1662
1663exit_err:
1664 sdio_al_get_into_err_state(sdio_al_dev);
1665 memset(header, 0, sizeof(*header));
1666
1667 return -EIO;
1668}
1669
1670/**
1671 * Read SDIO-Client channel configuration
1672 *
1673 */
1674static int read_sdioc_channel_config(struct sdio_channel *ch)
1675{
1676 int ret;
1677 struct peer_sdioc_sw_mailbox *sw_mailbox = NULL;
1678 struct peer_sdioc_channel_config *ch_config = NULL;
1679 struct sdio_al_device *sdio_al_dev = ch->sdio_al_dev;
1680
1681 if (sdio_al_dev == NULL) {
1682 pr_err(MODULE_NAME ": NULL sdio_al_dev for channel %s\n",
1683 ch->name);
1684 return -EINVAL;
1685 }
1686
1687 if (sdio_al_dev->sdioc_sw_header->version == 0)
1688 return -1;
1689
1690 pr_debug(MODULE_NAME ":reading sw mailbox %s channel.\n", ch->name);
1691
1692 sw_mailbox = kzalloc(sizeof(*sw_mailbox), GFP_KERNEL);
1693 if (sw_mailbox == NULL)
1694 return -ENOMEM;
1695
1696 ret = sdio_memcpy_fromio(ch->func, sw_mailbox,
1697 SDIOC_SW_MAILBOX_ADDR, sizeof(*sw_mailbox));
1698 if (ret) {
1699 pr_err(MODULE_NAME ":fail to read sw mailbox.\n");
1700 goto exit_err;
1701 }
1702
1703 ch_config = &sw_mailbox->ch_config[ch->num];
1704 memcpy(&ch->ch_config, ch_config,
1705 sizeof(struct peer_sdioc_channel_config));
1706
1707 if (!ch_config->is_ready) {
1708 pr_err(MODULE_NAME ":sw mailbox channel not ready.\n");
1709 goto exit_err;
1710 }
1711
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001712 /* Aggregation up to 90% of the maximum size */
1713 ch->read_threshold = (ch_config->max_rx_threshold * 9) / 10;
1714 /* Threshold on 50% of the maximum size , sdioc uses double-buffer */
1715 ch->write_threshold = (ch_config->max_tx_threshold * 5) / 10;
Maya Erez8ed0a9a2011-07-19 14:46:53 +03001716 ch->threshold_change_cnt = ch->ch_config.max_rx_threshold -
1717 ch->read_threshold + THRESHOLD_CHANGE_EXTRA_BYTES;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001718
1719 ch->def_read_threshold = ch->read_threshold;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001720 ch->is_packet_mode = ch_config->is_packet_mode;
1721 if (!ch->is_packet_mode) {
1722 ch->poll_delay_msec = DEFAULT_POLL_DELAY_NOPACKET_MSEC;
1723 ch->min_write_avail = DEFAULT_MIN_WRITE_THRESHOLD_STREAMING;
1724 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001725 /* The max_packet_size is set by the modem in version 3 and on */
1726 if (sdio_al->sdioc_major > PEER_SDIOC_OLD_VERSION_MAJOR)
1727 ch->min_write_avail = ch_config->max_packet_size;
1728
1729 if (ch->min_write_avail > ch->write_threshold)
1730 ch->min_write_avail = ch->write_threshold;
1731
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03001732 pr_info(MODULE_NAME ":ch %s read_threshold=%d, write_threshold=%d,"
1733 " min_write_avail=%d, max_rx_threshold=%d,"
1734 " max_tx_threshold=%d\n", ch->name, ch->read_threshold,
1735 ch->write_threshold, ch->min_write_avail,
1736 ch_config->max_rx_threshold,
1737 ch_config->max_tx_threshold);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001738
1739 ch->peer_tx_buf_size = ch_config->tx_buf_size;
1740
1741 kfree(sw_mailbox);
1742
1743 return 0;
1744
1745exit_err:
1746 pr_info(MODULE_NAME ":Reading SW Mailbox error.\n");
1747 kfree(sw_mailbox);
1748
1749 return -1;
1750}
1751
1752
1753/**
1754 * Enable/Disable EOT interrupt of a pipe.
1755 *
1756 */
1757static int enable_eot_interrupt(struct sdio_al_device *sdio_al_dev,
1758 int pipe_index, int enable)
1759{
1760 int ret = 0;
1761 struct sdio_func *func1;
1762 u32 mask;
1763 u32 pipe_mask;
1764 u32 addr;
1765
1766 if (sdio_al_verify_dev(sdio_al_dev, __func__))
1767 return -ENODEV;
1768 func1 = sdio_al_dev->card->sdio_func[0];
1769
1770 if (pipe_index < 8) {
1771 addr = PIPES_0_7_IRQ_MASK_ADDR;
1772 pipe_mask = (1<<pipe_index);
1773 } else {
1774 addr = PIPES_8_15_IRQ_MASK_ADDR;
1775 pipe_mask = (1<<(pipe_index-8));
1776 }
1777
1778 mask = sdio_readl(func1, addr, &ret);
1779 if (ret) {
1780 pr_debug(MODULE_NAME ":enable_eot_interrupt fail\n");
1781 goto exit_err;
1782 }
1783
1784 if (enable)
1785 mask &= (~pipe_mask); /* 0 = enable */
1786 else
1787 mask |= (pipe_mask); /* 1 = disable */
1788
1789 sdio_writel(func1, mask, addr, &ret);
1790
1791exit_err:
1792 return ret;
1793}
1794
1795
1796/**
1797 * Enable/Disable mask interrupt of a function.
1798 *
1799 */
1800static int enable_mask_irq(struct sdio_al_device *sdio_al_dev,
1801 int func_num, int enable, u8 bit_offset)
1802{
1803 int ret = 0;
1804 struct sdio_func *func1 = NULL;
1805 u32 mask = 0;
1806 u32 func_mask = 0;
1807 u32 addr = 0;
1808 u32 offset = 0;
1809
1810 if (sdio_al_verify_dev(sdio_al_dev, __func__))
1811 return -ENODEV;
1812 func1 = sdio_al_dev->card->sdio_func[0];
1813
1814 if (func_num < 4) {
1815 addr = FUNC_1_4_MASK_IRQ_ADDR;
1816 offset = func_num * 8 + bit_offset;
1817 } else {
1818 addr = FUNC_5_7_MASK_IRQ_ADDR;
1819 offset = (func_num - 4) * 8 + bit_offset;
1820 }
1821
1822 func_mask = 1<<offset;
1823
1824 mask = sdio_readl(func1, addr, &ret);
1825 if (ret) {
1826 pr_err(MODULE_NAME ":enable_mask_irq fail\n");
1827 goto exit_err;
1828 }
1829
1830 if (enable)
1831 mask &= (~func_mask); /* 0 = enable */
1832 else
1833 mask |= (func_mask); /* 1 = disable */
1834
1835 pr_debug(MODULE_NAME ":enable_mask_irq, writing mask = 0x%x\n", mask);
1836
1837 sdio_writel(func1, mask, addr, &ret);
1838
1839exit_err:
1840 return ret;
1841}
1842
1843/**
1844 * Enable/Disable Threshold interrupt of a pipe.
1845 *
1846 */
1847static int enable_threshold_interrupt(struct sdio_al_device *sdio_al_dev,
1848 int pipe_index, int enable)
1849{
1850 int ret = 0;
1851 struct sdio_func *func1;
1852 u32 mask;
1853 u32 pipe_mask;
1854 u32 addr;
1855
1856 if (sdio_al_verify_dev(sdio_al_dev, __func__))
1857 return -ENODEV;
1858 func1 = sdio_al_dev->card->sdio_func[0];
1859
1860 if (pipe_index < 8) {
1861 addr = PIPES_0_7_IRQ_MASK_ADDR;
1862 pipe_mask = (1<<pipe_index);
1863 } else {
1864 addr = PIPES_8_15_IRQ_MASK_ADDR;
1865 pipe_mask = (1<<(pipe_index-8));
1866 }
1867
1868 mask = sdio_readl(func1, addr, &ret);
1869 if (ret) {
1870 pr_debug(MODULE_NAME ":enable_threshold_interrupt fail\n");
1871 goto exit_err;
1872 }
1873
1874 pipe_mask = pipe_mask<<8; /* Threshold bits 8..15 */
1875 if (enable)
1876 mask &= (~pipe_mask); /* 0 = enable */
1877 else
1878 mask |= (pipe_mask); /* 1 = disable */
1879
1880 sdio_writel(func1, mask, addr, &ret);
1881
1882exit_err:
1883 return ret;
1884}
1885
1886/**
1887 * Set the threshold to trigger interrupt from SDIO-Card on
1888 * pipe available bytes.
1889 *
1890 */
1891static int set_pipe_threshold(struct sdio_al_device *sdio_al_dev,
1892 int pipe_index, int threshold)
1893{
1894 int ret = 0;
1895 struct sdio_func *func1;
1896
1897 if (sdio_al_verify_dev(sdio_al_dev, __func__))
1898 return -ENODEV;
1899 func1 = sdio_al_dev->card->sdio_func[0];
1900
1901 sdio_writel(func1, threshold,
1902 PIPES_THRESHOLD_ADDR+pipe_index*4, &ret);
1903 if (ret)
1904 pr_err(MODULE_NAME ":set_pipe_threshold err=%d\n", -ret);
1905
1906 return ret;
1907}
1908
1909/**
1910 * Enable func w/ retries
1911 *
1912 */
1913static int sdio_al_enable_func_retry(struct sdio_func *func, const char *name)
1914{
1915 int ret, i;
1916 for (i = 0; i < 200; i++) {
1917 ret = sdio_enable_func(func);
1918 if (ret) {
1919 pr_debug(MODULE_NAME ":retry enable %s func#%d "
1920 "ret=%d\n",
1921 name, func->num, ret);
1922 msleep(10);
1923 } else
1924 break;
1925 }
1926
1927 return ret;
1928}
1929
1930/**
1931 * Open Channel
1932 *
1933 * 1. Init Channel Context.
1934 * 2. Init the Channel SDIO-Function.
1935 * 3. Init the Channel Pipes on Mailbox.
1936 */
1937static int open_channel(struct sdio_channel *ch)
1938{
1939 int ret = 0;
1940 struct sdio_al_device *sdio_al_dev = ch->sdio_al_dev;
1941
1942 if (sdio_al_dev == NULL) {
1943 pr_err(MODULE_NAME ": NULL sdio_al_dev for channel %s\n",
1944 ch->name);
1945 return -EINVAL;
1946 }
1947
1948 /* Init channel Context */
1949 /** Func#1 is reserved for mailbox */
1950 ch->func = sdio_al_dev->card->sdio_func[ch->num+1];
1951 ch->rx_pipe_index = ch->num*2;
1952 ch->tx_pipe_index = ch->num*2+1;
1953 ch->signature = SDIO_AL_SIGNATURE;
1954
1955 ch->total_rx_bytes = 0;
1956 ch->total_tx_bytes = 0;
1957
1958 ch->write_avail = 0;
1959 ch->read_avail = 0;
1960 ch->rx_pending_bytes = 0;
1961
1962 mutex_init(&ch->ch_lock);
1963
1964 pr_debug(MODULE_NAME ":open_channel %s func#%d\n",
1965 ch->name, ch->func->num);
1966
1967 INIT_LIST_HEAD(&(ch->rx_size_list_head));
1968
1969 /* Init SDIO Function */
1970 ret = sdio_al_enable_func_retry(ch->func, ch->name);
1971 if (ret) {
1972 pr_err(MODULE_NAME ":sdio_enable_func() err=%d\n", -ret);
1973 goto exit_err;
1974 }
1975
1976 /* Note: Patch Func CIS tuple issue */
1977 ret = sdio_set_block_size(ch->func, SDIO_AL_BLOCK_SIZE);
1978 if (ret) {
1979 pr_err(MODULE_NAME ":sdio_set_block_size()failed, err=%d\n",
1980 -ret);
1981 goto exit_err;
1982 }
1983
1984 ch->func->max_blksize = SDIO_AL_BLOCK_SIZE;
1985
1986 sdio_set_drvdata(ch->func, ch);
1987
1988 /* Get channel parameters from the peer SDIO-Client */
1989 read_sdioc_channel_config(ch);
1990
1991 /* Set Pipes Threshold on Mailbox */
1992 ret = set_pipe_threshold(sdio_al_dev,
1993 ch->rx_pipe_index, ch->read_threshold);
1994 if (ret)
1995 goto exit_err;
1996 ret = set_pipe_threshold(sdio_al_dev,
1997 ch->tx_pipe_index, ch->write_threshold);
1998 if (ret)
1999 goto exit_err;
2000
2001 /* Set flag before interrupts are enabled to allow notify */
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002002 ch->state = SDIO_CHANNEL_STATE_OPEN;
2003 pr_debug(MODULE_NAME ":channel %s is in OPEN state now\n", ch->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002004
2005 sdio_al_dev->poll_delay_msec = get_min_poll_time_msec(sdio_al_dev);
2006
2007 /* lpm mechanism lives under the assumption there is always a timer */
2008 /* Check if need to start the timer */
2009 if ((sdio_al_dev->poll_delay_msec) &&
2010 (sdio_al_dev->is_timer_initialized == false)) {
2011
2012 init_timer(&sdio_al_dev->timer);
2013 sdio_al_dev->timer.data = (unsigned long) sdio_al_dev;
2014 sdio_al_dev->timer.function = sdio_al_timer_handler;
2015 sdio_al_dev->timer.expires = jiffies +
2016 msecs_to_jiffies(sdio_al_dev->poll_delay_msec);
2017 add_timer(&sdio_al_dev->timer);
2018 sdio_al_dev->is_timer_initialized = true;
2019 }
2020
2021 /* Enable Pipes Interrupts */
2022 enable_eot_interrupt(sdio_al_dev, ch->rx_pipe_index, true);
2023 enable_eot_interrupt(sdio_al_dev, ch->tx_pipe_index, true);
2024
2025 enable_threshold_interrupt(sdio_al_dev, ch->rx_pipe_index, true);
2026 enable_threshold_interrupt(sdio_al_dev, ch->tx_pipe_index, true);
2027
2028exit_err:
2029
2030 return ret;
2031}
2032
2033/**
2034 * Ask the worker to read the mailbox.
2035 */
2036static void ask_reading_mailbox(struct sdio_al_device *sdio_al_dev)
2037{
2038 if (!sdio_al_dev->ask_mbox) {
2039 pr_debug(MODULE_NAME ":ask_reading_mailbox for card %d\n",
2040 sdio_al_dev->card->host->index);
2041 sdio_al_dev->ask_mbox = true;
2042 wake_up(&sdio_al_dev->wait_mbox);
2043 }
2044}
2045
2046/**
2047 * Start the timer
2048 */
2049static void start_timer(struct sdio_al_device *sdio_al_dev)
2050{
2051 if ((sdio_al_dev->poll_delay_msec) &&
2052 (sdio_al_dev->state == CARD_INSERTED)) {
2053 sdio_al_dev->timer.expires = jiffies +
2054 msecs_to_jiffies(sdio_al_dev->poll_delay_msec);
2055 add_timer(&sdio_al_dev->timer);
2056 }
2057}
2058
2059/**
2060 * Restart(postpone) the already working timer
2061 */
2062static void restart_timer(struct sdio_al_device *sdio_al_dev)
2063{
2064 if ((sdio_al_dev->poll_delay_msec) &&
2065 (sdio_al_dev->state == CARD_INSERTED)) {
2066 ulong expires = jiffies +
2067 msecs_to_jiffies(sdio_al_dev->poll_delay_msec);
2068 mod_timer(&sdio_al_dev->timer, expires);
2069 }
2070}
2071
2072/**
2073 * Do the wakup sequence.
2074 * This function should be called after claiming the host!
2075 * The caller is responsible for releasing the host.
2076 *
2077 * Wake up sequence
2078 * 1. Get lock
2079 * 2. Enable wake up function if needed
2080 * 3. Mark NOT OK to sleep and write it
2081 * 4. Restore default thresholds
2082 * 5. Start the mailbox and inactivity timer again
2083 */
2084static int sdio_al_wake_up(struct sdio_al_device *sdio_al_dev,
Maya Erez7b1ebd22011-08-20 20:53:24 +03002085 u32 not_from_int, struct sdio_channel *ch)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002086{
Maya Erez8ed0a9a2011-07-19 14:46:53 +03002087 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002088 struct sdio_func *wk_func =
2089 sdio_al_dev->card->sdio_func[SDIO_AL_WAKEUP_FUNC-1];
2090 unsigned long time_to_wait;
2091 struct mmc_host *host = wk_func->card->host;
2092
2093 if (sdio_al_dev->is_err) {
2094 SDIO_AL_ERR(__func__);
2095 return -ENODEV;
2096 }
2097
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002098 if (!sdio_al_dev->is_ok_to_sleep) {
2099 LPM_DEBUG(MODULE_NAME ":card %d already awake, "
2100 "no need to wake up\n",
2101 sdio_al_dev->card->host->index);
2102 return 0;
2103 }
Maya Erez7b1ebd22011-08-20 20:53:24 +03002104
2105 /* Wake up sequence */
2106 if (not_from_int) {
2107 if (ch) {
2108 LPM_DEBUG(MODULE_NAME ": Wake up card %d (not by "
2109 "interrupt), ch %s",
2110 sdio_al_dev->card->host->index, ch->name);
2111 } else {
2112 LPM_DEBUG(MODULE_NAME ": Wake up card %d (not "
2113 "by interrupt)",
2114 sdio_al_dev->card->host->index);
2115 }
2116 } else {
2117 LPM_DEBUG(MODULE_NAME ": Wake up card %d by interrupt",
2118 sdio_al_dev->card->host->index);
2119 sdio_al_dev->print_after_interrupt = 1;
2120 }
2121
Yaniv Gardi3e327762011-07-27 11:11:04 +03002122 sdio_al_vote_for_sleep(sdio_al_dev, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002123
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002124 msmsdcc_lpm_disable(host);
2125 msmsdcc_set_pwrsave(sdio_al_dev->card->host, 0);
2126 /* Poll the GPIO */
2127 time_to_wait = jiffies + msecs_to_jiffies(1000);
2128 while (time_before(jiffies, time_to_wait)) {
2129 if (sdio_al->pdata->get_mdm2ap_status())
2130 break;
2131 udelay(TIME_TO_WAIT_US);
2132 }
Yaniv Gardi3e327762011-07-27 11:11:04 +03002133
Maya Erez7b1ebd22011-08-20 20:53:24 +03002134 pr_debug(MODULE_NAME ":GPIO mdm2ap_status=%d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002135 sdio_al->pdata->get_mdm2ap_status());
2136
2137 /* Here get_mdm2ap_status() returning 0 is not an error condition */
2138 if (sdio_al->pdata->get_mdm2ap_status() == 0)
2139 LPM_DEBUG(MODULE_NAME ": get_mdm2ap_status() is 0\n");
2140
2141 /* Enable Wake up Function */
2142 ret = sdio_al_enable_func_retry(wk_func, "wakeup func");
2143 if (ret) {
2144 pr_err(MODULE_NAME ":sdio_enable_func() err=%d\n",
2145 -ret);
2146 goto error_exit;
2147 }
2148 /* Mark NOT OK_TOSLEEP */
2149 sdio_al_dev->is_ok_to_sleep = 0;
2150 ret = write_lpm_info(sdio_al_dev);
2151 if (ret) {
2152 pr_err(MODULE_NAME ":write_lpm_info() failed, err=%d\n",
2153 -ret);
2154 sdio_al_dev->is_ok_to_sleep = 1;
2155 sdio_disable_func(wk_func);
2156 goto error_exit;
2157 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002158 sdio_disable_func(wk_func);
2159
2160 /* Start the timer again*/
2161 restart_inactive_time(sdio_al_dev);
2162 sdio_al_dev->poll_delay_msec = get_min_poll_time_msec(sdio_al_dev);
2163 start_timer(sdio_al_dev);
2164
2165 LPM_DEBUG(MODULE_NAME "Finished Wake up sequence for card %d",
2166 sdio_al_dev->card->host->index);
2167
2168 msmsdcc_set_pwrsave(sdio_al_dev->card->host, 1);
2169 pr_debug(MODULE_NAME ":Turn clock off\n");
2170
2171 return ret;
2172error_exit:
2173 sdio_al_vote_for_sleep(sdio_al_dev, 1);
2174 msmsdcc_set_pwrsave(sdio_al_dev->card->host, 1);
2175 WARN_ON(ret);
2176 sdio_al_get_into_err_state(sdio_al_dev);
2177 return ret;
2178}
2179
2180
2181/**
2182 * SDIO Function Interrupt handler.
2183 *
2184 * Interrupt shall be triggered by SDIO-Client when:
2185 * 1. End-Of-Transfer (EOT) detected in packet mode.
2186 * 2. Bytes-available reached the threshold.
2187 *
2188 * Reading the mailbox clears the EOT/Threshold interrupt
2189 * source.
2190 * The interrupt source should be cleared before this ISR
2191 * returns. This ISR is called from IRQ Thread and not
2192 * interrupt, so it may sleep.
2193 *
2194 */
2195static void sdio_func_irq(struct sdio_func *func)
2196{
2197 struct sdio_al_device *sdio_al_dev = sdio_get_drvdata(func);
2198
2199 pr_debug(MODULE_NAME ":start %s.\n", __func__);
2200
2201 if (sdio_al_dev == NULL) {
2202 pr_err(MODULE_NAME ": NULL sdio_al_dev for card %d\n",
2203 func->card->host->index);
2204 return;
2205 }
2206
2207 if (sdio_al_dev->is_ok_to_sleep)
Maya Erez7b1ebd22011-08-20 20:53:24 +03002208 sdio_al_wake_up(sdio_al_dev, 0, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002209 else
2210 restart_timer(sdio_al_dev);
2211
2212 read_mailbox(sdio_al_dev, true);
2213
2214 pr_debug(MODULE_NAME ":end %s.\n", __func__);
2215}
2216
2217/**
2218 * Timer Expire Handler
2219 *
2220 */
2221static void sdio_al_timer_handler(unsigned long data)
2222{
2223 struct sdio_al_device *sdio_al_dev = (struct sdio_al_device *)data;
2224 if (sdio_al_dev == NULL) {
2225 pr_err(MODULE_NAME ": NULL sdio_al_dev for data %lu\n",
2226 data);
2227 return;
2228 }
2229 if (sdio_al_dev->state != CARD_INSERTED) {
2230 pr_err(MODULE_NAME ": sdio_al_dev is in invalid state %d\n",
2231 sdio_al_dev->state);
2232 return;
2233 }
2234 pr_debug(MODULE_NAME " Timer Expired\n");
2235
2236 ask_reading_mailbox(sdio_al_dev);
2237
2238 restart_timer(sdio_al_dev);
2239}
2240
2241/**
2242 * Driver Setup.
2243 *
2244 */
2245static int sdio_al_setup(struct sdio_al_device *sdio_al_dev)
2246{
2247 int ret = 0;
2248 struct mmc_card *card = sdio_al_dev->card;
2249 struct sdio_func *func1 = NULL;
2250 int i = 0;
2251 int fn = 0;
2252
2253 if (card == NULL) {
2254 pr_err(MODULE_NAME ":sdio_al_setup: No Card detected\n");
2255 return -ENODEV;
2256 }
2257
2258
2259 pr_info(MODULE_NAME ":sdio_al_setup for card %d\n",
2260 sdio_al_dev->card->host->index);
2261
2262 func1 = card->sdio_func[0];
2263
2264 ret = sdio_al->pdata->config_mdm2ap_status(1);
2265 if (ret) {
2266 pr_err(MODULE_NAME "Could not request GPIO\n");
2267 return ret;
2268 }
2269
2270 INIT_WORK(&sdio_al_dev->sdio_al_work.work, worker);
2271 /* disable all pipes interrupts before claim irq.
2272 since all are enabled by default. */
2273 for (i = 0 ; i < SDIO_AL_MAX_PIPES; i++) {
2274 enable_eot_interrupt(sdio_al_dev, i, false);
2275 enable_threshold_interrupt(sdio_al_dev, i, false);
2276 }
2277
2278 /* Disable all SDIO Functions before claim irq. */
2279 for (fn = 1 ; fn <= card->sdio_funcs; fn++)
2280 sdio_disable_func(card->sdio_func[fn-1]);
2281
2282 sdio_set_drvdata(func1, sdio_al_dev);
2283 pr_info(MODULE_NAME ":claim IRQ for card %d\n",
2284 card->host->index);
2285
2286 ret = sdio_claim_irq(func1, sdio_func_irq);
2287 if (ret) {
2288 pr_err(MODULE_NAME ":Fail to claim IRQ for card %d\n",
2289 card->host->index);
2290 goto exit_err;
2291 }
2292
2293 sdio_al_dev->is_ready = true;
2294
2295 /* Start worker before interrupt might happen */
2296 queue_work(sdio_al_dev->workqueue, &sdio_al_dev->sdio_al_work.work);
2297
2298 start_inactive_time(sdio_al_dev);
2299
2300 pr_debug(MODULE_NAME ":Ready.\n");
2301
2302 return 0;
2303
2304exit_err:
2305 sdio_release_host(func1);
2306 pr_err(MODULE_NAME ":Setup Failure.\n");
2307
2308 return ret;
2309}
2310
2311/**
2312 * Driver Tear-Down.
2313 *
2314 */
2315static void sdio_al_tear_down(void)
2316{
2317 int i;
2318 struct sdio_al_device *sdio_al_dev = NULL;
2319 struct sdio_func *func1;
2320
2321 for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; ++i) {
2322 if (sdio_al->devices[i] == NULL)
2323 continue;
2324 sdio_al_dev = sdio_al->devices[i];
2325
2326 if (sdio_al_dev->is_ready) {
2327 sdio_al_dev->is_ready = false; /* Flag worker to exit */
2328 sdio_al_dev->ask_mbox = false;
2329 ask_reading_mailbox(sdio_al_dev); /* Wakeup worker */
2330 /* allow gracefully exit of the worker thread */
2331 msleep(100);
2332
2333 flush_workqueue(sdio_al_dev->workqueue);
2334 destroy_workqueue(sdio_al_dev->workqueue);
2335
2336 sdio_al_vote_for_sleep(sdio_al_dev, 1);
2337
2338 if (sdio_al_verify_func1(sdio_al_dev, __func__)) {
2339 pr_err(MODULE_NAME ": %s: Invalid func1",
2340 __func__);
2341 return;
2342 }
2343 func1 = sdio_al_dev->card->sdio_func[0];
2344
2345 sdio_claim_host(func1);
2346 sdio_release_irq(func1);
2347 sdio_disable_func(func1);
2348 sdio_release_host(func1);
2349 }
2350 }
2351
2352 sdio_al->pdata->config_mdm2ap_status(0);
2353}
2354
2355/**
2356 * Find channel by name.
2357 *
2358 */
2359static struct sdio_channel *find_channel_by_name(const char *name)
2360{
2361 struct sdio_channel *ch = NULL;
2362 int i, j;
2363 struct sdio_al_device *sdio_al_dev = NULL;
2364
2365 for (j = 0; j < MAX_NUM_OF_SDIO_DEVICES; ++j) {
2366 if (sdio_al->devices[j] == NULL)
2367 continue;
2368 sdio_al_dev = sdio_al->devices[j];
2369 for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002370 if (sdio_al_dev->channel[i].state ==
2371 SDIO_CHANNEL_STATE_INVALID)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002372 continue;
2373 if (strcmp(sdio_al_dev->channel[i].name, name) == 0) {
2374 ch = &sdio_al_dev->channel[i];
2375 break;
2376 }
2377 }
2378 if (ch != NULL)
2379 break;
2380 }
2381
2382 return ch;
2383}
2384
2385/**
2386 * Find the minimal poll time.
2387 *
2388 */
2389static int get_min_poll_time_msec(struct sdio_al_device *sdio_sl_dev)
2390{
2391 int i;
2392 int poll_delay_msec = 0x0FFFFFFF;
2393
2394 for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++)
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002395 if ((sdio_sl_dev->channel[i].state ==
2396 SDIO_CHANNEL_STATE_OPEN) &&
2397 (sdio_sl_dev->channel[i].poll_delay_msec > 0) &&
2398 (sdio_sl_dev->channel[i].poll_delay_msec < poll_delay_msec))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002399 poll_delay_msec =
2400 sdio_sl_dev->channel[i].poll_delay_msec;
2401
2402 if (poll_delay_msec == 0x0FFFFFFF)
2403 poll_delay_msec = SDIO_AL_POLL_TIME_NO_STREAMING;
2404
2405 pr_debug(MODULE_NAME ":poll delay time is %d msec\n", poll_delay_msec);
2406
2407 return poll_delay_msec;
2408}
2409
2410/**
2411 * Open SDIO Channel.
2412 *
2413 * Enable the channel.
2414 * Set the channel context.
2415 * Trigger reading the mailbox to check available bytes.
2416 *
2417 */
2418int sdio_open(const char *name, struct sdio_channel **ret_ch, void *priv,
2419 void (*notify)(void *priv, unsigned ch_event))
2420{
2421 int ret = 0;
2422 struct sdio_channel *ch = NULL;
2423 struct sdio_al_device *sdio_al_dev = NULL;
2424
2425 *ret_ch = NULL; /* default */
2426
2427 ch = find_channel_by_name(name);
2428 if (ch == NULL) {
2429 pr_err(MODULE_NAME ":Can't find channel name %s\n", name);
2430 return -EINVAL;
2431 }
2432
2433 sdio_al_dev = ch->sdio_al_dev;
2434 if (sdio_al_verify_dev(sdio_al_dev, __func__))
2435 return -ENODEV;
2436
2437 sdio_claim_host(sdio_al_dev->card->sdio_func[0]);
2438
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002439 if ((ch->state != SDIO_CHANNEL_STATE_IDLE) &&
2440 (ch->state != SDIO_CHANNEL_STATE_CLOSED)) {
2441 pr_err(MODULE_NAME ":Wrong ch %s state %d\n", name, ch->state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002442 ret = -EPERM;
2443 goto exit_err;
2444 }
2445
2446 if (sdio_al_dev->state != CARD_INSERTED) {
2447 pr_err(MODULE_NAME ":%s: sdio_al_dev is in invalid state %d\n",
2448 __func__, sdio_al_dev->state);
2449 ret = -ENODEV;
2450 goto exit_err;
2451 }
2452
2453 if (sdio_al_dev->is_err) {
2454 SDIO_AL_ERR(__func__);
2455 ret = -ENODEV;
2456 goto exit_err;
2457 }
2458
Maya Erez7b1ebd22011-08-20 20:53:24 +03002459 ret = sdio_al_wake_up(sdio_al_dev, 1, ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002460 if (ret)
2461 goto exit_err;
2462
2463 ch->notify = notify;
2464 ch->priv = priv;
2465
2466 /* Note: Set caller returned context before interrupts are enabled */
2467 *ret_ch = ch;
2468
2469 ret = open_channel(ch);
2470 if (ret) {
2471 pr_err(MODULE_NAME ":sdio_open %s err=%d\n", name, -ret);
2472 goto exit_err;
2473 }
2474
2475 pr_info(MODULE_NAME ":sdio_open %s completed OK\n", name);
2476 if (sdio_al_dev->lpm_chan == INVALID_SDIO_CHAN) {
2477 if (sdio_al->sdioc_major == PEER_SDIOC_OLD_VERSION_MAJOR) {
2478 if (!ch->is_packet_mode) {
2479 pr_info(MODULE_NAME ":setting channel %s as "
2480 "lpm_chan\n", name);
2481 sdio_al_dev->lpm_chan = ch->num;
2482 }
2483 } else {
2484 pr_info(MODULE_NAME ":setting channel %s as lpm_chan\n",
2485 name);
2486 sdio_al_dev->lpm_chan = ch->num;
2487 }
2488 }
2489
2490exit_err:
2491 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2492 return ret;
2493}
2494EXPORT_SYMBOL(sdio_open);
2495
2496/**
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002497 * Request peer operation
2498 * note: sanity checks of parameters done by caller
2499 * called under bus locked
2500 */
2501static int peer_set_operation(u32 opcode,
2502 struct sdio_al_device *sdio_al_dev,
2503 struct sdio_channel *ch)
2504{
2505 int ret;
2506 int offset;
2507 struct sdio_func *wk_func;
2508 u32 peer_operation;
2509 int loop_count = 0;
2510
2511 wk_func = sdio_al_dev->card->sdio_func[SDIO_AL_WAKEUP_FUNC-1];
2512 if (!wk_func) {
2513 pr_err(MODULE_NAME ":%s: NULL wakeup func:%d\n",
2514 __func__, SDIO_AL_WAKEUP_FUNC);
2515 ret = -ENODEV;
2516 goto exit;
2517 }
2518 /* calculate offset of peer_operation field in sw mailbox struct */
2519 offset = offsetof(struct peer_sdioc_sw_mailbox, ch_config) +
2520 sizeof(struct peer_sdioc_channel_config) * ch->num +
2521 offsetof(struct peer_sdioc_channel_config, peer_operation);
2522
Maya Erez7b1ebd22011-08-20 20:53:24 +03002523 ret = sdio_al_wake_up(sdio_al_dev, 1, ch);
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002524 if (ret) {
2525 pr_err(MODULE_NAME ":Fail to wake up\n");
2526 goto exit;
2527 }
2528 /* request operation from MDM peer */
2529 peer_operation = PEER_OPERATION(opcode, PEER_OP_STATE_INIT);
2530 ret = sdio_memcpy_toio(ch->func, SDIOC_SW_MAILBOX_ADDR+offset,
2531 &peer_operation, sizeof(u32));
2532 if (ret) {
2533 pr_err(MODULE_NAME ":failed to request close operation\n");
2534 goto exit;
2535 }
2536 ret = sdio_al_enable_func_retry(wk_func, "wk_func");
2537 if (ret) {
2538 pr_err(MODULE_NAME ":Fail to enable Func#%d\n", wk_func->num);
2539 goto exit;
2540 }
2541 pr_debug(MODULE_NAME ":%s: wk_func enabled on ch %s\n",
2542 __func__, ch->name);
2543 /* send "start" operation to MDM */
2544 peer_operation = PEER_OPERATION(opcode, PEER_OP_STATE_START);
2545 ret = sdio_memcpy_toio(ch->func, SDIOC_SW_MAILBOX_ADDR+offset,
2546 &peer_operation, sizeof(u32));
2547 if (ret) {
2548 pr_err(MODULE_NAME ":failed to send start close operation\n");
2549 goto exit;
2550 }
2551 ret = sdio_disable_func(wk_func);
2552 if (ret) {
2553 pr_err(MODULE_NAME ":Fail to disable Func#%d\n", wk_func->num);
2554 goto exit;
2555 }
2556 /* poll for peer operation ack */
2557 while (peer_operation != 0) {
2558 ret = sdio_memcpy_fromio(ch->func,
2559 &peer_operation,
2560 SDIOC_SW_MAILBOX_ADDR+offset,
2561 sizeof(u32));
2562 if (ret) {
2563 pr_err(MODULE_NAME ":failed to request ack on close"
2564 " operation, loop_count = %d\n",
2565 loop_count);
2566 goto exit;
2567 }
2568 loop_count++;
2569 if (loop_count > 10) {
2570 pr_info(MODULE_NAME ":%s: peer_operation=0x%x wait loop"
2571 " %d on ch %s\n", __func__,
2572 peer_operation, loop_count, ch->name);
2573 }
2574 }
2575exit:
2576 return ret;
2577}
2578
2579/**
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002580 * Close SDIO Channel.
2581 *
2582 */
2583int sdio_close(struct sdio_channel *ch)
2584{
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002585 int ret;
2586 struct sdio_al_device *sdio_al_dev = NULL;
2587 void *temp_buf = NULL;
2588 int flush_len;
2589 ulong flush_expires;
2590
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002591 if (!ch) {
2592 pr_err(MODULE_NAME ":%s: NULL channel\n", __func__);
2593 return -ENODEV;
2594 }
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002595 sdio_al_dev = ch->sdio_al_dev;
2596 if (sdio_al_verify_dev(sdio_al_dev, __func__))
2597 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002598
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002599 if (!sdio_al_dev->ch_close_supported || !ch->is_packet_mode) {
2600 pr_info(MODULE_NAME ":%s: Not supported by mdm, ch %s\n",
2601 __func__, ch->name);
2602 return -ENOTSUPP;
2603 }
2604
2605 if (!ch->func) {
2606 pr_err(MODULE_NAME ":%s: NULL func on channel:%d\n",
2607 __func__, ch->num);
2608 return -ENODEV;
2609 }
2610 sdio_claim_host(sdio_al_dev->card->sdio_func[0]);
2611
2612 if (sdio_al_dev->is_err) {
2613 SDIO_AL_ERR(__func__);
2614 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2615 return -ENODEV;
2616 }
2617 if (sdio_al_dev->state != CARD_INSERTED) {
2618 pr_err(MODULE_NAME ":%s: sdio_al_dev is in invalid state %d\n",
2619 __func__, sdio_al_dev->state);
2620 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2621 return -ENODEV;
2622 }
2623 ch->state = SDIO_CHANNEL_STATE_CLOSING;
2624 ret = peer_set_operation(PEER_OP_CODE_CLOSE, sdio_al_dev, ch);
2625 if (ret) {
2626 pr_err(MODULE_NAME ":%s: peer_set_operation() failed: %d\n",
2627 __func__, ret);
2628 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2629 return -ENODEV;
2630 }
2631 /* udate poll time for opened channels */
2632 if (ch->poll_delay_msec > 0) {
2633 sdio_al_dev->poll_delay_msec =
2634 get_min_poll_time_msec(sdio_al_dev);
2635 }
2636 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2637
2638 flush_expires = jiffies +
2639 msecs_to_jiffies(SDIO_CLOSE_FLUSH_TIMEOUT_MSEC);
2640 /* flush rx packets of the channel */
2641 do {
2642 while (ch->read_avail > 0) {
2643 flush_len = ch->read_avail;
2644 temp_buf = kzalloc(flush_len, GFP_KERNEL);
2645 if (temp_buf == NULL) {
2646 pr_err(MODULE_NAME ":%s failed to allocate"
2647 " %d bytes during flush\n",
2648 __func__, flush_len);
2649 return -ENOMEM;
2650 }
2651 ret = sdio_read_internal(ch, temp_buf, flush_len);
2652 kfree(temp_buf);
2653 if (ret) {
2654 pr_err(MODULE_NAME ":%s failed to"
2655 " sdio_read: %d, ch %s\n",
2656 __func__, ret, ch->name);
2657 return ret;
2658 }
Yaniv Gardic4663632011-08-31 19:55:38 +03002659
2660 if (time_after(jiffies, flush_expires) != 0) {
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002661 pr_err(MODULE_NAME ":%s flush rx packets"
Yaniv Gardic4663632011-08-31 19:55:38 +03002662 " timeout: ch %s\n",
2663 __func__, ch->name);
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002664 sdio_al_get_into_err_state(sdio_al_dev);
2665 return -EBUSY;
2666 }
2667 }
2668 msleep(100);
2669 if (ch->signature != SDIO_AL_SIGNATURE) {
2670 pr_err(MODULE_NAME ":%s: after sleep, invalid signature"
2671 " 0x%x\n", __func__, ch->signature);
2672 return -ENODEV;
2673 }
2674 } while (ch->read_avail > 0);
2675
2676 sdio_claim_host(sdio_al_dev->card->sdio_func[0]);
2677 /* disable channel interrupts */
2678 enable_eot_interrupt(ch->sdio_al_dev, ch->rx_pipe_index, false);
2679 enable_eot_interrupt(ch->sdio_al_dev, ch->tx_pipe_index, false);
2680 enable_threshold_interrupt(ch->sdio_al_dev, ch->rx_pipe_index, false);
2681 enable_threshold_interrupt(ch->sdio_al_dev, ch->tx_pipe_index, false);
2682
2683 /* disable function to be able to open the channel again */
2684 ret = sdio_disable_func(ch->func);
2685 if (ret) {
2686 pr_err(MODULE_NAME ":Fail to disable Func#%d\n", ch->func->num);
2687 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2688 return ret;
2689 }
2690 ch->state = SDIO_CHANNEL_STATE_CLOSED;
2691 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2692
2693 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002694}
2695EXPORT_SYMBOL(sdio_close);
2696
2697/**
2698 * Get the number of available bytes to write.
2699 *
2700 */
2701int sdio_write_avail(struct sdio_channel *ch)
2702{
2703 if (!ch) {
2704 pr_err(MODULE_NAME ":%s: NULL channel\n", __func__);
2705 return -ENODEV;
2706 }
2707 if (ch->signature != SDIO_AL_SIGNATURE) {
2708 pr_err(MODULE_NAME ":%s: Invalid signature 0x%x\n", __func__,
2709 ch->signature);
2710 return -ENODEV;
2711 }
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002712 if (ch->state != SDIO_CHANNEL_STATE_OPEN) {
2713 pr_err(MODULE_NAME ":%s: channel %s state is not open (%d)\n",
2714 __func__, ch->name, ch->state);
2715 return -ENODEV;
2716 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002717 pr_debug(MODULE_NAME ":sdio_write_avail %s 0x%x\n",
2718 ch->name, ch->write_avail);
2719
2720 return ch->write_avail;
2721}
2722EXPORT_SYMBOL(sdio_write_avail);
2723
2724/**
2725 * Get the number of available bytes to read.
2726 *
2727 */
2728int sdio_read_avail(struct sdio_channel *ch)
2729{
2730 if (!ch) {
2731 pr_err(MODULE_NAME ":%s: NULL channel\n", __func__);
2732 return -ENODEV;
2733 }
2734 if (ch->signature != SDIO_AL_SIGNATURE) {
2735 pr_err(MODULE_NAME ":%s: Invalid signature 0x%x\n", __func__,
2736 ch->signature);
2737 return -ENODEV;
2738 }
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002739 if (ch->state != SDIO_CHANNEL_STATE_OPEN) {
2740 pr_err(MODULE_NAME ":%s: channel %s state is not open (%d)\n",
2741 __func__, ch->name, ch->state);
2742 return -ENODEV;
2743 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002744 pr_debug(MODULE_NAME ":sdio_read_avail %s 0x%x\n",
2745 ch->name, ch->read_avail);
2746
2747 return ch->read_avail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002748}
2749EXPORT_SYMBOL(sdio_read_avail);
2750
2751/**
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002752 * Internal read from SDIO Channel.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002753 *
2754 * Reading from the pipe will trigger interrupt if there are
2755 * other pending packets on the SDIO-Client.
2756 *
2757 */
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002758static int sdio_read_internal(struct sdio_channel *ch, void *data, int len)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002759{
2760 int ret = 0;
2761 struct sdio_al_device *sdio_al_dev = NULL;
2762
2763 if (!ch) {
2764 pr_err(MODULE_NAME ":%s: NULL channel\n", __func__);
2765 return -ENODEV;
2766 }
2767 if (!data) {
2768 pr_err(MODULE_NAME ":%s: NULL data\n", __func__);
2769 return -ENODEV;
2770 }
2771 if (len == 0) {
2772 pr_err(MODULE_NAME ":channel %s trying to read 0 bytes\n",
2773 ch->name);
2774 return -EINVAL;
2775 }
2776
2777 if (ch->signature != SDIO_AL_SIGNATURE) {
2778 pr_err(MODULE_NAME ":%s: Invalid signature 0x%x\n", __func__,
2779 ch->signature);
2780 return -ENODEV;
2781 }
2782
2783 sdio_al_dev = ch->sdio_al_dev;
2784 if (sdio_al_verify_dev(sdio_al_dev, __func__))
2785 return -ENODEV;
2786
2787 sdio_claim_host(sdio_al_dev->card->sdio_func[0]);
2788
2789 if (sdio_al_dev->is_err) {
2790 SDIO_AL_ERR(__func__);
2791 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2792 return -ENODEV;
2793 }
2794
2795 if (sdio_al_dev->state != CARD_INSERTED) {
2796 pr_err(MODULE_NAME ":%s: sdio_al_dev is in invalid state %d\n",
2797 __func__, sdio_al_dev->state);
2798 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2799 return -ENODEV;
2800 }
2801
2802 /* lpm policy says we can't go to sleep when we have pending rx data,
2803 so either we had rx interrupt and woken up, or we never went to
2804 sleep */
2805 if (sdio_al_dev->is_ok_to_sleep) {
2806 pr_err(MODULE_NAME ":%s: called when is_ok_to_sleep is set "
2807 "for ch %s, len=%d, last_any_read_avail=%d,"
2808 "last_read_avail=%d, last_old_read_avail=%d",
2809 __func__, ch->name, len,
2810 ch->statistics.last_any_read_avail,
2811 ch->statistics.last_read_avail,
2812 ch->statistics.last_old_read_avail);
2813 }
2814 BUG_ON(sdio_al_dev->is_ok_to_sleep);
2815
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002816 if ((ch->state != SDIO_CHANNEL_STATE_OPEN) &&
2817 (ch->state != SDIO_CHANNEL_STATE_CLOSING)) {
2818 pr_err(MODULE_NAME ":%s wrong channel %s state %d\n",
2819 __func__, ch->name, ch->state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002820 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2821 return -EINVAL;
2822 }
2823
2824 DATA_DEBUG(MODULE_NAME ":start ch %s read %d avail %d.\n",
2825 ch->name, len, ch->read_avail);
2826
2827 restart_inactive_time(sdio_al_dev);
2828
2829 if ((ch->is_packet_mode) && (len != ch->read_avail)) {
2830 pr_err(MODULE_NAME ":sdio_read ch %s len != read_avail\n",
2831 ch->name);
2832 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2833 return -EINVAL;
2834 }
2835
2836 if (len > ch->read_avail) {
2837 pr_err(MODULE_NAME ":ERR ch %s: reading more bytes (%d) than"
2838 " the avail(%d).\n",
2839 ch->name, len, ch->read_avail);
2840 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2841 return -ENOMEM;
2842 }
2843
2844 ret = sdio_memcpy_fromio(ch->func, data, PIPE_RX_FIFO_ADDR, len);
2845
2846 if (ret) {
Maya Erezd9cc2292011-08-04 09:20:31 +03002847 pr_err(MODULE_NAME ":ch %s: sdio_read err=%d, len=%d, "
2848 "read_avail=%d, last_read_avail=%d, last_old_read_avail=%d\n",
2849 ch->name, -ret, len, ch->read_avail,
2850 ch->statistics.last_read_avail,
2851 ch->statistics.last_old_read_avail);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002852 sdio_al_dev->is_err = true;
2853 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2854 return ret;
2855 }
2856
2857 ch->statistics.total_read_times++;
2858
2859 /* Remove handled packet from the list regardless if ret is ok */
2860 if (ch->is_packet_mode)
2861 remove_handled_rx_packet(ch);
2862 else
2863 ch->read_avail -= len;
2864
2865 ch->total_rx_bytes += len;
2866 DATA_DEBUG(MODULE_NAME ":end ch %s read %d avail %d total %d.\n",
2867 ch->name, len, ch->read_avail, ch->total_rx_bytes);
2868
2869 if ((ch->read_avail == 0) && !(ch->is_packet_mode))
2870 ask_reading_mailbox(sdio_al_dev);
2871
2872 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2873
2874 return ret;
2875}
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002876
2877/**
2878 * Read from SDIO Channel.
2879 *
2880 * Reading from the pipe will trigger interrupt if there are
2881 * other pending packets on the SDIO-Client.
2882 *
2883 */
2884int sdio_read(struct sdio_channel *ch, void *data, int len)
2885{
2886 if (!ch) {
2887 pr_err(MODULE_NAME ":%s: NULL channel\n", __func__);
2888 return -ENODEV;
2889 }
2890 if (ch->signature != SDIO_AL_SIGNATURE) {
2891 pr_err(MODULE_NAME ":%s: Invalid signature 0x%x\n", __func__,
2892 ch->signature);
2893 return -ENODEV;
2894 }
2895 if (ch->state == SDIO_CHANNEL_STATE_OPEN) {
2896 return sdio_read_internal(ch, data, len);
2897 } else {
2898 pr_err(MODULE_NAME ":%s: Invalid channel %s state %d\n",
2899 __func__, ch->name, ch->state);
2900 }
2901 return -ENODEV;
2902}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002903EXPORT_SYMBOL(sdio_read);
2904
2905/**
2906 * Write to SDIO Channel.
2907 *
2908 */
2909int sdio_write(struct sdio_channel *ch, const void *data, int len)
2910{
2911 int ret = 0;
2912 struct sdio_al_device *sdio_al_dev = NULL;
2913
2914 if (!ch) {
2915 pr_err(MODULE_NAME ":%s: NULL channel\n", __func__);
2916 return -ENODEV;
2917 }
2918 if (!data) {
2919 pr_err(MODULE_NAME ":%s: NULL data\n", __func__);
2920 return -ENODEV;
2921 }
2922 if (len == 0) {
2923 pr_err(MODULE_NAME ":channel %s trying to write 0 bytes\n",
2924 ch->name);
2925 return -EINVAL;
2926 }
2927
2928 if (ch->signature != SDIO_AL_SIGNATURE) {
2929 pr_err(MODULE_NAME ":%s: Invalid signature 0x%x\n", __func__,
2930 ch->signature);
2931 return -ENODEV;
2932 }
2933
2934 sdio_al_dev = ch->sdio_al_dev;
2935 if (sdio_al_verify_dev(sdio_al_dev, __func__))
2936 return -ENODEV;
2937
2938 sdio_claim_host(sdio_al_dev->card->sdio_func[0]);
2939
2940
2941 if (sdio_al_dev->state != CARD_INSERTED) {
2942 pr_err(MODULE_NAME ":%s: sdio_al_dev is in invalid state %d\n",
2943 __func__, sdio_al_dev->state);
2944 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2945 return -ENODEV;
2946 }
2947 WARN_ON(len > ch->write_avail);
2948
2949 if (sdio_al_dev->is_err) {
2950 SDIO_AL_ERR(__func__);
2951 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2952 return -ENODEV;
2953 }
2954
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03002955 if (ch->state != SDIO_CHANNEL_STATE_OPEN) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002956 pr_err(MODULE_NAME ":writing to closed channel %s\n",
2957 ch->name);
2958 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2959 return -EINVAL;
2960 }
2961
2962 if (sdio_al_dev->is_ok_to_sleep) {
Maya Erez7b1ebd22011-08-20 20:53:24 +03002963 ret = sdio_al_wake_up(sdio_al_dev, 1, ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002964 if (ret) {
2965 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2966 return ret;
2967 }
2968 } else {
2969 restart_inactive_time(sdio_al_dev);
2970 }
2971
2972 DATA_DEBUG(MODULE_NAME ":start ch %s write %d avail %d.\n",
2973 ch->name, len, ch->write_avail);
2974
2975 if (len > ch->write_avail) {
2976 pr_err(MODULE_NAME ":ERR ch %s: write more bytes (%d) than "
2977 " available %d.\n",
2978 ch->name, len, ch->write_avail);
2979 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2980 return -ENOMEM;
2981 }
2982
2983 ret = sdio_ch_write(ch, data, len);
2984 if (ret) {
2985 pr_err(MODULE_NAME ":sdio_write on channel %s err=%d\n",
2986 ch->name, -ret);
2987 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
2988 return ret;
2989 }
2990
2991 ch->total_tx_bytes += len;
2992 DATA_DEBUG(MODULE_NAME ":end ch %s write %d avail %d total %d.\n",
2993 ch->name, len, ch->write_avail, ch->total_tx_bytes);
2994
2995 /* Round up to whole buffer size */
2996 len = ROUND_UP(len, ch->peer_tx_buf_size);
2997 /* Protect from wraparound */
2998 len = min(len, (int) ch->write_avail);
2999 ch->write_avail -= len;
3000
3001 if (ch->write_avail < ch->min_write_avail)
3002 ask_reading_mailbox(sdio_al_dev);
3003
3004 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
3005
3006 return ret;
3007}
3008EXPORT_SYMBOL(sdio_write);
3009
3010static int __devinit msm_sdio_al_probe(struct platform_device *pdev)
3011{
3012 if (!sdio_al) {
3013 pr_err(MODULE_NAME ": %s: NULL sdio_al\n", __func__);
3014 return -ENODEV;
3015 }
3016
3017 sdio_al->pdata = pdev->dev.platform_data;
3018 return 0;
3019}
3020
3021static int __devexit msm_sdio_al_remove(struct platform_device *pdev)
3022{
3023 return 0;
3024}
3025
Maya Erez6862b142011-08-22 09:07:07 +03003026static void msm_sdio_al_shutdown(struct platform_device *pdev)
3027{
3028 int i, j;
3029 struct sdio_func *func1 = NULL;
3030 int ret;
3031
3032 pr_info("Initiating msm_sdio_al_shutdown...");
3033
3034 for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; i++) {
3035 struct sdio_al_device *sdio_al_dev = NULL;
3036 if (sdio_al->devices[i] == NULL) {
3037 pr_debug(MODULE_NAME ": %s: NULL device in index %d",
3038 __func__, i);
3039 continue;
3040 }
3041 sdio_al_dev = sdio_al->devices[i];
3042 if (sdio_al_dev->state == CARD_REMOVED) {
3043 pr_info(MODULE_NAME ": %s: card %d is already removed",
3044 __func__, sdio_al_dev->card->host->index);
3045 continue;
3046 }
3047 if (sdio_al_dev->state == MODEM_RESTART) {
3048 pr_info(MODULE_NAME ": %s: card %d was already "
3049 "notified for modem reset",
3050 __func__, sdio_al_dev->card->host->index);
3051 continue;
3052 }
3053
3054 pr_info(MODULE_NAME ": %s: Set the state to MODEM_RESTART"
3055 " for card %d",
3056 __func__, sdio_al_dev->card->host->index);
3057 sdio_al_dev->state = MODEM_RESTART;
3058 sdio_al_dev->is_ready = false;
3059
3060 /* Stop mailbox timer */
3061 if (sdio_al_dev->is_timer_initialized) {
3062 pr_debug(MODULE_NAME ": %s: Stop timer for card %d",
3063 __func__, sdio_al_dev->card->host->index);
3064 sdio_al_dev->poll_delay_msec = 0;
3065 del_timer_sync(&sdio_al_dev->timer);
3066 }
3067 }
3068
3069 for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; i++) {
3070 struct sdio_al_device *sdio_al_dev;
3071 if (sdio_al->devices[i] == NULL) {
3072 pr_debug(MODULE_NAME ": %s: NULL device in index %d",
3073 __func__, i);
3074 continue;
3075 }
3076 sdio_al_dev = sdio_al->devices[i];
3077
3078 if (!sdio_al_verify_func1(sdio_al_dev, __func__)) {
3079 func1 = sdio_al_dev->card->sdio_func[0];
3080 sdio_claim_host(func1);
3081
3082 if ((sdio_al_dev->is_ok_to_sleep) &&
3083 (!sdio_al_dev->is_err)) {
3084 pr_debug(MODULE_NAME ": %s: wakeup modem for "
3085 "card %d", __func__,
3086 sdio_al_dev->card->host->index);
3087 ret = sdio_al_wake_up(sdio_al_dev, 1, NULL);
3088 if (ret == 0) {
3089 pr_info(MODULE_NAME ": %s: "
3090 "sdio_release_irq"
3091 " for card %d",
3092 __func__,
3093 sdio_al_dev->card->host->index);
3094 sdio_release_irq(func1);
3095 }
3096 } else {
3097 pr_debug(MODULE_NAME ": %s: sdio_release_irq"
3098 " for card %d",
3099 __func__,
3100 sdio_al_dev->card->host->index);
3101 sdio_release_irq(func1);
3102 }
3103 }
3104
3105 pr_debug(MODULE_NAME ": %s: Notifying SDIO clients for card %d",
3106 __func__, sdio_al_dev->card->host->index);
Maya Erez8afd564f2011-08-24 15:57:06 +03003107 for (j = 0; j < SDIO_AL_MAX_CHANNELS; j++) {
3108 if (sdio_al_dev->channel[j].state ==
3109 SDIO_CHANNEL_STATE_INVALID)
3110 continue;
3111 platform_device_unregister(
3112 sdio_al_dev->channel[j].pdev);
3113 sdio_al_dev->channel[i].signature = 0x0;
3114 }
Maya Erez6862b142011-08-22 09:07:07 +03003115
3116 if (!sdio_al_verify_func1(sdio_al_dev, __func__))
3117 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
3118
3119 pr_debug(MODULE_NAME ": %s: Allows sleep for card %d", __func__,
3120 sdio_al_dev->card->host->index);
3121 sdio_al_vote_for_sleep(sdio_al_dev, 1);
3122 }
3123 pr_info("msm_sdio_al_shutdown complete.");
3124}
3125
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003126static struct platform_driver msm_sdio_al_driver = {
3127 .probe = msm_sdio_al_probe,
3128 .remove = __exit_p(msm_sdio_al_remove),
Maya Erez6862b142011-08-22 09:07:07 +03003129 .shutdown = msm_sdio_al_shutdown,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003130 .driver = {
3131 .name = "msm_sdio_al",
3132 },
3133};
3134
3135/**
3136 * Initialize SDIO_AL channels.
3137 *
3138 */
3139static int init_channels(struct sdio_al_device *sdio_al_dev)
3140{
3141 int ret = 0;
3142 int i;
3143
3144 if (sdio_al_verify_dev(sdio_al_dev, __func__))
3145 return -ENODEV;
3146
3147 sdio_claim_host(sdio_al_dev->card->sdio_func[0]);
3148
3149 ret = read_sdioc_software_header(sdio_al_dev,
3150 sdio_al_dev->sdioc_sw_header);
3151 if (ret)
3152 goto exit;
3153
3154 ret = sdio_al_setup(sdio_al_dev);
3155 if (ret)
3156 goto exit;
3157
3158 for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
3159 int ch_name_size;
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03003160 if (sdio_al_dev->channel[i].state == SDIO_CHANNEL_STATE_INVALID)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003161 continue;
3162 if (sdio_al->unittest_mode) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003163 memset(sdio_al_dev->channel[i].ch_test_name, 0,
3164 sizeof(sdio_al_dev->channel[i].ch_test_name));
3165 ch_name_size = strnlen(sdio_al_dev->channel[i].name,
3166 CHANNEL_NAME_SIZE);
3167 strncpy(sdio_al_dev->channel[i].ch_test_name,
3168 sdio_al_dev->channel[i].name,
3169 ch_name_size);
3170 strncat(sdio_al_dev->channel[i].ch_test_name +
3171 ch_name_size,
3172 SDIO_TEST_POSTFIX,
3173 SDIO_TEST_POSTFIX_SIZE);
3174 pr_debug(MODULE_NAME ":pdev.name = %s\n",
3175 sdio_al_dev->channel[i].ch_test_name);
3176 sdio_al_dev->channel[i].pdev = platform_device_alloc(
3177 sdio_al_dev->channel[i].ch_test_name, -1);
3178 } else {
3179 pr_debug(MODULE_NAME ":pdev.name = %s\n",
3180 sdio_al_dev->channel[i].name);
3181 sdio_al_dev->channel[i].pdev = platform_device_alloc(
3182 sdio_al_dev->channel[i].name, -1);
3183 }
3184 if (!sdio_al_dev->channel[i].pdev) {
3185 pr_err(MODULE_NAME ":NULL platform device for ch %s",
3186 sdio_al_dev->channel[i].name);
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03003187 sdio_al_dev->channel[i].state =
3188 SDIO_CHANNEL_STATE_INVALID;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003189 continue;
3190 }
3191 ret = platform_device_add(sdio_al_dev->channel[i].pdev);
3192 if (ret) {
3193 pr_err(MODULE_NAME ":platform_device_add failed, "
3194 "ret=%d\n", ret);
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03003195 sdio_al_dev->channel[i].state =
3196 SDIO_CHANNEL_STATE_INVALID;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003197 }
3198 }
3199
3200exit:
3201 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
3202 return ret;
3203}
3204
3205/**
3206 * Initialize SDIO_AL channels according to the client setup.
3207 * This function also check if the client is in boot mode and
3208 * flashless boot is required to be activated or the client is
3209 * up and running.
3210 *
3211 */
3212static int sdio_al_client_setup(struct sdio_al_device *sdio_al_dev)
3213{
3214 int ret = 0;
3215 struct sdio_func *func1;
3216 int signature = 0;
3217
3218 if (sdio_al_verify_dev(sdio_al_dev, __func__))
3219 return -ENODEV;
3220 func1 = sdio_al_dev->card->sdio_func[0];
3221
3222 sdio_claim_host(func1);
3223
3224 /* Read the header signature to determine the status of the MDM
3225 * SDIO Client
3226 */
3227 signature = sdio_readl(func1, SDIOC_SW_HEADER_ADDR, &ret);
3228 sdio_release_host(func1);
3229 if (ret) {
3230 pr_err(MODULE_NAME ":fail to read signature from sw header.\n");
3231 return ret;
3232 }
3233
3234 switch (signature) {
3235 case PEER_SDIOC_SW_MAILBOX_BOOT_SIGNATURE:
3236 if (sdio_al_dev == sdio_al->bootloader_dev) {
3237 pr_info(MODULE_NAME ":setup bootloader on card %d\n",
3238 sdio_al_dev->card->host->index);
3239 return sdio_al_bootloader_setup();
3240 } else {
3241 pr_info(MODULE_NAME ":wait for bootloader completion "
3242 "on card %d\n",
3243 sdio_al_dev->card->host->index);
3244 return sdio_al_wait_for_bootloader_comp(sdio_al_dev);
3245 }
3246 case PEER_SDIOC_SW_MAILBOX_SIGNATURE:
3247 case PEER_SDIOC_SW_MAILBOX_UT_SIGNATURE:
3248 return init_channels(sdio_al_dev);
3249 default:
3250 pr_err(MODULE_NAME ":Invalid signature 0x%x\n", signature);
3251 return -EINVAL;
3252 }
3253
3254 return 0;
3255}
3256
3257/*
3258 * SDIO driver functions
3259 */
3260static int sdio_al_sdio_probe(struct sdio_func *func,
3261 const struct sdio_device_id *sdio_dev_id)
3262{
3263 int ret = 0;
3264 struct sdio_al_device *sdio_al_dev = NULL;
3265 int i;
3266 struct mmc_card *card = NULL;
3267
3268 if (!func) {
3269 pr_err(MODULE_NAME ": %s: NULL func\n", __func__);
3270 return -ENODEV;
3271 }
3272 card = func->card;
3273
3274 if (!card) {
3275 pr_err(MODULE_NAME ": %s: NULL card\n", __func__);
3276 return -ENODEV;
3277 }
3278
3279 if (card->sdio_funcs < SDIO_AL_MAX_FUNCS) {
3280 dev_info(&card->dev,
3281 "SDIO-functions# %d less than expected.\n",
3282 card->sdio_funcs);
3283 return -ENODEV;
3284 }
3285
3286 /* Check if there is already a device for this card */
3287 for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; ++i) {
3288 if (sdio_al->devices[i] == NULL)
3289 continue;
3290 if (sdio_al->devices[i]->card == card)
3291 return 0;
3292 }
3293
3294 dev_info(&card->dev, "SDIO Card claimed.\n");
3295
3296 sdio_al_dev = kzalloc(sizeof(struct sdio_al_device), GFP_KERNEL);
3297 if (sdio_al_dev == NULL)
3298 return -ENOMEM;
3299
3300 sdio_al_dev->state = CARD_INSERTED;
3301
3302 if (card->host->index == SDIO_BOOTLOADER_CARD_INDEX)
3303 sdio_al->bootloader_dev = sdio_al_dev;
3304
3305 for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES ; ++i)
3306 if (sdio_al->devices[i] == NULL) {
3307 sdio_al->devices[i] = sdio_al_dev;
3308 break;
3309 }
3310 if (i == MAX_NUM_OF_SDIO_DEVICES) {
3311 pr_err(MODULE_NAME ":No space in devices array for the "
3312 "device\n");
3313 return -ENOMEM;
3314 }
3315
3316 sdio_al_dev->is_ready = false;
3317
3318 sdio_al_dev->signature = SDIO_AL_SIGNATURE;
3319
3320 sdio_al_dev->is_suspended = 0;
3321 sdio_al_dev->is_timer_initialized = false;
3322
3323 sdio_al_dev->lpm_chan = INVALID_SDIO_CHAN;
3324
3325 sdio_al_dev->card = card;
3326
3327 sdio_al_dev->mailbox = kzalloc(sizeof(struct sdio_mailbox), GFP_KERNEL);
3328 if (sdio_al_dev->mailbox == NULL)
3329 return -ENOMEM;
3330
3331 sdio_al_dev->sdioc_sw_header
3332 = kzalloc(sizeof(*sdio_al_dev->sdioc_sw_header), GFP_KERNEL);
3333 if (sdio_al_dev->sdioc_sw_header == NULL)
3334 return -ENOMEM;
3335
3336 sdio_al_dev->timer.data = (unsigned long)sdio_al_dev;
3337
3338 wake_lock_init(&sdio_al_dev->wake_lock, WAKE_LOCK_SUSPEND, MODULE_NAME);
3339 /* Don't allow sleep until all required clients register */
3340 sdio_al_vote_for_sleep(sdio_al_dev, 0);
3341
3342 sdio_claim_host(card->sdio_func[0]);
3343
3344 /* Init Func#1 */
3345 ret = sdio_enable_func(card->sdio_func[0]);
3346 if (ret) {
3347 pr_err(MODULE_NAME ":Fail to enable Func#%d\n",
3348 card->sdio_func[0]->num);
3349 goto exit;
3350 }
3351
3352 /* Patch Func CIS tuple issue */
3353 ret = sdio_set_block_size(card->sdio_func[0], SDIO_AL_BLOCK_SIZE);
3354 if (ret) {
3355 pr_err(MODULE_NAME ":Fail to set block size, Func#%d\n",
3356 card->sdio_func[0]->num);
3357 goto exit;
3358 }
3359 sdio_al_dev->card->sdio_func[0]->max_blksize = SDIO_AL_BLOCK_SIZE;
3360
3361 sdio_al_dev->workqueue = create_singlethread_workqueue("sdio_al_wq");
3362 sdio_al_dev->sdio_al_work.sdio_al_dev = sdio_al_dev;
3363 init_waitqueue_head(&sdio_al_dev->wait_mbox);
3364
3365 ret = sdio_al_client_setup(sdio_al_dev);
3366
3367exit:
3368 sdio_release_host(card->sdio_func[0]);
3369 return ret;
3370}
3371
3372static void sdio_al_sdio_remove(struct sdio_func *func)
3373{
3374 struct sdio_al_device *sdio_al_dev = NULL;
3375 int i;
3376 int state;
3377 struct mmc_card *card = NULL;
3378
3379 if (!func) {
3380 pr_err(MODULE_NAME ": %s: NULL func\n", __func__);
3381 return;
3382 }
3383 card = func->card;
3384
3385 if (!card) {
3386 pr_err(MODULE_NAME ": %s: NULL card\n", __func__);
3387 return;
3388 }
3389
3390 /* Find the sdio_al_device of this card */
3391 for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; ++i) {
3392 if (sdio_al->devices[i] == NULL)
3393 continue;
3394 if (sdio_al->devices[i]->card == card) {
3395 sdio_al_dev = sdio_al->devices[i];
3396 sdio_al->devices[i] = NULL;
3397 break;
3398 }
3399 }
3400 if (sdio_al_dev == NULL) {
3401 pr_debug(MODULE_NAME ":%s :NULL sdio_al_dev for card %d\n",
3402 __func__, card->host->index);
3403 return;
3404 }
3405
3406 pr_info(MODULE_NAME ":%s for card %d\n",
3407 __func__, card->host->index);
3408
3409 if (card->sdio_func[0])
3410 sdio_claim_host(card->sdio_func[0]);
3411 else
3412 pr_err(MODULE_NAME ":%s: NULL func1 for card %d\n",
3413 __func__, card->host->index);
3414
3415 if (sdio_al_dev->state == CARD_REMOVED)
3416 return;
3417
3418 state = sdio_al_dev->state;
3419 sdio_al_dev->state = CARD_REMOVED;
3420
3421 for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++)
3422 sdio_al_dev->channel[i].signature = 0x0;
3423
3424 pr_info(MODULE_NAME ":%s: ask_reading_mailbox for card %d\n",
3425 __func__, card->host->index);
3426 sdio_al_dev->is_ready = false; /* Flag worker to exit */
3427 sdio_al_dev->ask_mbox = false;
3428 ask_reading_mailbox(sdio_al_dev); /* Wakeup worker */
3429
3430 if (state != MODEM_RESTART) {
3431 if (sdio_al_dev->is_timer_initialized) {
3432 pr_info(MODULE_NAME ": %s: Stop timer for card %d",
3433 __func__, sdio_al_dev->card->host->index);
3434 sdio_al_dev->poll_delay_msec = 0;
3435 del_timer_sync(&sdio_al_dev->timer);
3436 }
3437
Maya Erez8afd564f2011-08-24 15:57:06 +03003438 pr_info(MODULE_NAME ":%s: notifying clients for "
3439 "card %d\n",
3440 __func__, card->host->index);
3441 for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
3442 if (sdio_al_dev->channel[i].state ==
3443 SDIO_CHANNEL_STATE_INVALID)
3444 continue;
3445 platform_device_unregister(
3446 sdio_al_dev->channel[i].pdev);
3447 sdio_al_dev->channel[i].signature = 0x0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003448 }
3449 }
3450 if (card->sdio_func[0])
3451 sdio_release_host(card->sdio_func[0]);
3452
3453 pr_info(MODULE_NAME ":%s: vote for sleep for card %d\n",
3454 __func__, card->host->index);
3455 sdio_al_vote_for_sleep(sdio_al_dev, 1);
3456
3457 pr_info(MODULE_NAME ":%s: flush_workqueue for card %d\n",
3458 __func__, card->host->index);
3459 flush_workqueue(sdio_al_dev->workqueue);
3460 destroy_workqueue(sdio_al_dev->workqueue);
3461 wake_lock_destroy(&sdio_al_dev->wake_lock);
3462
3463 pr_info(MODULE_NAME ":%s: delete data structures for card %d\n",
3464 __func__, card->host->index);
3465 kfree(sdio_al_dev->sdioc_sw_header);
3466 kfree(sdio_al_dev->mailbox);
3467 kfree(sdio_al_dev);
3468
3469 pr_info(MODULE_NAME ":%s: sdio card %d removed.\n", __func__,
3470 card->host->index);
3471}
3472
3473static void sdio_print_mailbox(char *prefix_str, struct sdio_mailbox *mailbox)
3474{
3475 int k = 0;
3476 char buf[256];
3477 char buf1[10];
3478
3479 if (!mailbox) {
3480 pr_err(MODULE_NAME ": mailbox is NULL\n");
3481 return;
3482 }
3483
3484 pr_err(MODULE_NAME ": %s: pipes 0_7: eot=0x%x, "
3485 "thresh=0x%x, overflow=0x%x, "
3486 "underflow=0x%x, mask_thresh=0x%x\n",
3487 prefix_str, mailbox->eot_pipe_0_7,
3488 mailbox->thresh_above_limit_pipe_0_7,
3489 mailbox->overflow_pipe_0_7,
3490 mailbox->underflow_pipe_0_7,
3491 mailbox->mask_thresh_above_limit_pipe_0_7);
3492
3493 memset(buf, 0, sizeof(buf));
3494 strncat(buf, ": bytes_avail:", sizeof(buf));
3495
3496 for (k = 0 ; k < SDIO_AL_ACTIVE_PIPES ; ++k) {
3497 snprintf(buf1, sizeof(buf1), "%d, ",
3498 mailbox->pipe_bytes_avail[k]);
3499 strncat(buf, buf1, sizeof(buf));
3500 }
3501
3502 pr_err(MODULE_NAME "%s", buf);
3503}
3504
3505static void sdio_al_print_info(void)
3506{
3507 int i = 0;
3508 int j = 0;
3509 int ret = 0;
3510 struct sdio_mailbox *mailbox = NULL;
3511 struct sdio_mailbox *hw_mailbox = NULL;
3512 struct peer_sdioc_channel_config *ch_config = NULL;
3513 struct sdio_func *func1 = NULL;
3514 struct sdio_func *lpm_func = NULL;
3515 int offset = 0;
3516 int is_ok_to_sleep = 0;
3517 static atomic_t first_time;
3518 char buf[50];
3519
3520 if (atomic_read(&first_time) == 1)
3521 return;
3522
3523 atomic_set(&first_time, 1);
3524
3525 pr_err(MODULE_NAME ": %s - SDIO DEBUG INFO\n", __func__);
3526
3527 if (!sdio_al) {
3528 pr_err(MODULE_NAME ": %s - ERROR - sdio_al is NULL\n",
3529 __func__);
3530 return;
3531 }
3532
3533 pr_err(MODULE_NAME ": GPIO mdm2ap_status=%d\n",
3534 sdio_al->pdata->get_mdm2ap_status());
3535
3536 for (j = 0 ; j < MAX_NUM_OF_SDIO_DEVICES ; ++j) {
3537 struct sdio_al_device *sdio_al_dev = sdio_al->devices[j];
3538
3539 if (sdio_al_dev == NULL) {
3540 continue;
3541 }
3542
3543 if (!sdio_al_dev->card && !sdio_al_dev->card->host) {
3544 pr_err(MODULE_NAME ": Card or Host fields "
3545 "are NULL\n);");
3546 continue;
3547 }
3548
3549 snprintf(buf, sizeof(buf), "Card#%d: Shadow HW MB",
3550 sdio_al_dev->card->host->index);
3551
3552 /* printing Shadowing HW Mailbox*/
3553 mailbox = sdio_al_dev->mailbox;
3554 sdio_print_mailbox(buf, mailbox);
3555
3556 pr_err(MODULE_NAME ": Card#%d: "
3557 "is_ok_to_sleep=%d\n",
3558 sdio_al_dev->card->host->index,
3559 sdio_al_dev->is_ok_to_sleep);
3560
3561
3562 pr_err(MODULE_NAME ": Card#%d: "
3563 "Shadow channels SW MB:",
3564 sdio_al_dev->card->host->index);
3565
3566 /* printing Shadowing SW Mailbox per channel*/
3567 for (i = 0 ; i < SDIO_AL_MAX_CHANNELS ; ++i) {
3568 struct sdio_channel *ch = &sdio_al_dev->channel[i];
3569
3570 if (ch == NULL) {
3571 continue;
3572 }
3573
Konstantin Dorfmanee2e3082011-08-16 15:12:01 +03003574 if (ch->state == SDIO_CHANNEL_STATE_INVALID)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003575 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003576
3577 ch_config = &sdio_al_dev->channel[i].ch_config;
3578
3579 pr_err(MODULE_NAME ": Ch %s: max_rx_thres=0x%x, "
3580 "max_tx_thres=0x%x, tx_buf=0x%x, "
3581 "is_packet_mode=%d, "
3582 "max_packet=0x%x, min_write=0x%x",
3583 ch->name, ch_config->max_rx_threshold,
3584 ch_config->max_tx_threshold,
3585 ch_config->tx_buf_size,
3586 ch_config->is_packet_mode,
3587 ch_config->max_packet_size,
3588 ch->min_write_avail);
3589
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003590 pr_err(MODULE_NAME ": total_rx=0x%x, "
3591 "total_tx=0x%x, "
3592 "read_avail=0x%x, "
3593 "write_avail=0x%x, rx_pending=0x%x, "
3594 "num_reads=0x%x, num_notifs=0x%x",
3595 ch->total_rx_bytes, ch->total_tx_bytes,
3596 ch->read_avail, ch->write_avail,
3597 ch->rx_pending_bytes,
3598 ch->statistics.total_read_times,
3599 ch->statistics.total_notifs);
3600 } /* end loop over all channels */
3601
3602 } /* end loop over all devices */
3603
3604 /* reading from client and printing is_host_ok_to_sleep per device */
3605 for (j = 0 ; j < MAX_NUM_OF_SDIO_DEVICES ; ++j) {
3606 struct sdio_al_device *sdio_al_dev = sdio_al->devices[j];
3607
3608 if (sdio_al_verify_dev(sdio_al_dev, __func__))
3609 return;
3610
3611 if (!sdio_al_dev->card->host) {
3612 pr_err(MODULE_NAME ": Host is NULL");
3613 continue;
3614 }
3615
3616 if (sdio_al_dev->lpm_chan == INVALID_SDIO_CHAN) {
3617 pr_err(MODULE_NAME ": %s - for "
3618 "Card#%d, is lpm_chan=="
3619 "INVALID_SDIO_CHAN. continuing...",
3620 __func__, sdio_al_dev->card->host->index);
3621 continue;
3622 }
3623
3624 offset = offsetof(struct peer_sdioc_sw_mailbox, ch_config)+
3625 sizeof(struct peer_sdioc_channel_config) *
3626 sdio_al_dev->lpm_chan+
3627 offsetof(struct peer_sdioc_channel_config, is_host_ok_to_sleep);
3628
3629 lpm_func = sdio_al_dev->card->sdio_func[sdio_al_dev->
3630 lpm_chan+1];
3631 if (!lpm_func) {
3632 pr_err(MODULE_NAME ": %s - lpm_func is NULL for card#%d"
3633 " continuing...\n", __func__,
3634 sdio_al_dev->card->host->index);
3635 continue;
3636 }
3637
3638 sdio_claim_host(sdio_al_dev->card->sdio_func[0]);
3639 ret = sdio_memcpy_fromio(lpm_func,
3640 &is_ok_to_sleep,
3641 SDIOC_SW_MAILBOX_ADDR+offset,
3642 sizeof(int));
Maya Erez1dd658a2011-08-09 10:14:47 +03003643 if (sdio_al_verify_dev(sdio_al_dev, __func__))
3644 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003645 sdio_release_host(sdio_al_dev->card->sdio_func[0]);
3646
3647 if (ret)
3648 pr_err(MODULE_NAME ": %s - fail to read "
3649 "is_HOST_ok_to_sleep from mailbox for card %d",
3650 __func__, sdio_al_dev->card->host->index);
3651 else
3652 pr_err(MODULE_NAME ": Card#%d: "
3653 "is_HOST_ok_to_sleep=%d\n",
3654 sdio_al_dev->card->host->index,
3655 is_ok_to_sleep);
3656 }
3657
3658 for (j = 0 ; j < MAX_NUM_OF_SDIO_DEVICES ; ++j) {
3659 struct sdio_al_device *sdio_al_dev = sdio_al->devices[j];
3660
3661 if (sdio_al_verify_dev(sdio_al_dev, __func__))
3662 return;
3663
3664 if (!sdio_al_dev->card->host) {
3665 pr_err(MODULE_NAME ": Host is NULL");
3666 continue;
3667 }
3668
3669 /* Reading HW Mailbox */
3670 hw_mailbox = sdio_al_dev->mailbox;
3671 func1 = sdio_al_dev->card->sdio_func[0];
3672
3673 sdio_claim_host(func1);
3674 ret = sdio_memcpy_fromio(func1, hw_mailbox,
3675 HW_MAILBOX_ADDR, sizeof(*hw_mailbox));
Maya Erez1dd658a2011-08-09 10:14:47 +03003676 if (sdio_al_verify_dev(sdio_al_dev, __func__))
3677 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003678 sdio_release_host(func1);
3679
3680 if (ret) {
3681 pr_err(MODULE_NAME ": fail to read "
3682 "mailbox for card#%d. "
3683 "continuing...\n",
3684 sdio_al_dev->card->host->index);
3685 continue;
3686 }
3687
3688 snprintf(buf, sizeof(buf), "Card#%d: Current HW MB",
3689 sdio_al_dev->card->host->index);
3690
3691 /* Printing HW Mailbox */
3692 sdio_print_mailbox(buf, hw_mailbox);
3693 }
3694}
3695
3696static struct sdio_device_id sdio_al_sdioid[] = {
3697 {.class = 0, .vendor = 0x70, .device = 0x2460},
3698 {.class = 0, .vendor = 0x70, .device = 0x0460},
3699 {.class = 0, .vendor = 0x70, .device = 0x23F1},
3700 {.class = 0, .vendor = 0x70, .device = 0x23F0},
3701 {}
3702};
3703
3704static struct sdio_driver sdio_al_sdiofn_driver = {
3705 .name = "sdio_al_sdiofn",
3706 .id_table = sdio_al_sdioid,
3707 .probe = sdio_al_sdio_probe,
3708 .remove = sdio_al_sdio_remove,
3709};
3710
3711#ifdef CONFIG_MSM_SUBSYSTEM_RESTART
3712/*
3713 * Callback for notifications from restart mudule.
3714 * This function handles only the BEFORE_RESTART notification.
3715 * Stop all the activity on the card and notify our clients.
3716 */
3717static int sdio_al_subsys_notifier_cb(struct notifier_block *this,
3718 unsigned long notif_type,
3719 void *data)
3720{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003721 if (notif_type != SUBSYS_BEFORE_SHUTDOWN) {
3722 pr_info(MODULE_NAME ": %s: got notification %ld",
3723 __func__, notif_type);
3724 return NOTIFY_DONE;
3725 }
3726
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003727
Maya Erez6862b142011-08-22 09:07:07 +03003728 msm_sdio_al_shutdown(NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003729 return NOTIFY_OK;
3730}
3731
3732static struct notifier_block sdio_al_nb = {
3733 .notifier_call = sdio_al_subsys_notifier_cb,
3734};
3735#endif
3736
3737/**
3738 * Module Init.
3739 *
3740 * @warn: allocate sdio_al context before registering driver.
3741 *
3742 */
3743static int __init sdio_al_init(void)
3744{
3745 int ret = 0;
3746 int i;
3747
3748 pr_debug(MODULE_NAME ":sdio_al_init\n");
3749
3750 pr_info(MODULE_NAME ":SDIO-AL SW version %s\n",
3751 DRV_VERSION);
3752
3753 sdio_al = kzalloc(sizeof(struct sdio_al), GFP_KERNEL);
3754 if (sdio_al == NULL)
3755 return -ENOMEM;
3756
3757 for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES ; ++i)
3758 sdio_al->devices[i] = NULL;
3759
3760 sdio_al->unittest_mode = false;
3761
3762 sdio_al->debug.debug_lpm_on = debug_lpm_on;
3763 sdio_al->debug.debug_data_on = debug_data_on;
3764
3765#ifdef CONFIG_DEBUG_FS
3766 sdio_al_debugfs_init();
3767#endif
3768
3769
3770#ifdef CONFIG_MSM_SUBSYSTEM_RESTART
3771 sdio_al->subsys_notif_handle = subsys_notif_register_notifier(
3772 "external_modem", &sdio_al_nb);
3773#endif
3774
3775 ret = platform_driver_register(&msm_sdio_al_driver);
3776 if (ret) {
3777 pr_err(MODULE_NAME ": platform_driver_register failed: %d\n",
3778 ret);
3779 goto exit;
3780 }
3781
3782 sdio_register_driver(&sdio_al_sdiofn_driver);
3783exit:
3784 if (ret)
3785 kfree(sdio_al);
3786 return ret;
3787}
3788
3789/**
3790 * Module Exit.
3791 *
3792 * Free allocated memory.
3793 * Disable SDIO-Card.
3794 * Unregister driver.
3795 *
3796 */
3797static void __exit sdio_al_exit(void)
3798{
3799 if (sdio_al == NULL)
3800 return;
3801
3802 pr_debug(MODULE_NAME ":sdio_al_exit\n");
3803
3804#ifdef CONFIG_MSM_SUBSYSTEM_RESTART
3805 subsys_notif_unregister_notifier(
3806 sdio_al->subsys_notif_handle, &sdio_al_nb);
3807#endif
3808
3809 sdio_al_tear_down();
3810
3811 sdio_unregister_driver(&sdio_al_sdiofn_driver);
3812
3813 kfree(sdio_al);
3814
3815#ifdef CONFIG_DEBUG_FS
3816 sdio_al_debugfs_cleanup();
3817#endif
3818
3819 platform_driver_unregister(&msm_sdio_al_driver);
3820
3821 pr_debug(MODULE_NAME ":sdio_al_exit complete\n");
3822}
3823
3824module_init(sdio_al_init);
3825module_exit(sdio_al_exit);
3826
3827MODULE_LICENSE("GPL v2");
3828MODULE_DESCRIPTION("SDIO Abstraction Layer");
3829MODULE_AUTHOR("Amir Samuelov <amirs@codeaurora.org>");
3830MODULE_VERSION(DRV_VERSION);
3831