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