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