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