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