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