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