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