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