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