blob: fc3eb1a7fe246e86ff240165f77bf3dea78803dc [file] [log] [blame]
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +01001/*
2 * linux/drivers/mmc/host/tmio_mmc_pio.c
3 *
4 * Copyright (C) 2011 Guennadi Liakhovetski
5 * Copyright (C) 2007 Ian Molton
6 * Copyright (C) 2004 Ian Molton
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Driver for the MMC / SD / SDIO IP found in:
13 *
14 * TC6393XB, TC6391XB, TC6387XB, T7L66XB, ASIC3, SH-Mobile SoCs
15 *
16 * This driver draws mainly on scattered spec sheets, Reverse engineering
17 * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit
18 * support). (Further 4 bit support from a later datasheet).
19 *
20 * TODO:
21 * Investigate using a workqueue for PIO transfers
22 * Eliminate FIXMEs
23 * SDIO support
24 * Better Power management
25 * Handle MMC errors better
26 * double buffer support
27 *
28 */
29
30#include <linux/delay.h>
31#include <linux/device.h>
32#include <linux/highmem.h>
33#include <linux/interrupt.h>
34#include <linux/io.h>
35#include <linux/irq.h>
36#include <linux/mfd/tmio.h>
37#include <linux/mmc/host.h>
Simon Hormancba179a2011-03-24 09:48:36 +010038#include <linux/mmc/tmio.h>
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010039#include <linux/module.h>
40#include <linux/pagemap.h>
41#include <linux/platform_device.h>
Rafael J. Wysockic419e612012-03-13 01:01:51 +010042#include <linux/pm_qos.h>
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +000043#include <linux/pm_runtime.h>
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010044#include <linux/scatterlist.h>
45#include <linux/workqueue.h>
46#include <linux/spinlock.h>
47
48#include "tmio_mmc.h"
49
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010050void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
51{
Simon Horman54680fe2011-08-25 10:27:25 +090052 host->sdcard_irq_mask &= ~(i & TMIO_MASK_IRQ);
53 sd_ctrl_write32(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010054}
55
56void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
57{
Simon Horman54680fe2011-08-25 10:27:25 +090058 host->sdcard_irq_mask |= (i & TMIO_MASK_IRQ);
59 sd_ctrl_write32(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010060}
61
62static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i)
63{
64 sd_ctrl_write32(host, CTL_STATUS, ~i);
65}
66
67static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
68{
69 host->sg_len = data->sg_len;
70 host->sg_ptr = data->sg;
71 host->sg_orig = data->sg;
72 host->sg_off = 0;
73}
74
75static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
76{
77 host->sg_ptr = sg_next(host->sg_ptr);
78 host->sg_off = 0;
79 return --host->sg_len;
80}
81
82#ifdef CONFIG_MMC_DEBUG
83
84#define STATUS_TO_TEXT(a, status, i) \
85 do { \
86 if (status & TMIO_STAT_##a) { \
87 if (i++) \
88 printk(" | "); \
89 printk(#a); \
90 } \
91 } while (0)
92
93static void pr_debug_status(u32 status)
94{
95 int i = 0;
Girish K Sa3c76eb2011-10-11 11:44:09 +053096 pr_debug("status: %08x = ", status);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010097 STATUS_TO_TEXT(CARD_REMOVE, status, i);
98 STATUS_TO_TEXT(CARD_INSERT, status, i);
99 STATUS_TO_TEXT(SIGSTATE, status, i);
100 STATUS_TO_TEXT(WRPROTECT, status, i);
101 STATUS_TO_TEXT(CARD_REMOVE_A, status, i);
102 STATUS_TO_TEXT(CARD_INSERT_A, status, i);
103 STATUS_TO_TEXT(SIGSTATE_A, status, i);
104 STATUS_TO_TEXT(CMD_IDX_ERR, status, i);
105 STATUS_TO_TEXT(STOPBIT_ERR, status, i);
106 STATUS_TO_TEXT(ILL_FUNC, status, i);
107 STATUS_TO_TEXT(CMD_BUSY, status, i);
108 STATUS_TO_TEXT(CMDRESPEND, status, i);
109 STATUS_TO_TEXT(DATAEND, status, i);
110 STATUS_TO_TEXT(CRCFAIL, status, i);
111 STATUS_TO_TEXT(DATATIMEOUT, status, i);
112 STATUS_TO_TEXT(CMDTIMEOUT, status, i);
113 STATUS_TO_TEXT(RXOVERFLOW, status, i);
114 STATUS_TO_TEXT(TXUNDERRUN, status, i);
115 STATUS_TO_TEXT(RXRDY, status, i);
116 STATUS_TO_TEXT(TXRQ, status, i);
117 STATUS_TO_TEXT(ILL_ACCESS, status, i);
118 printk("\n");
119}
120
121#else
122#define pr_debug_status(s) do { } while (0)
123#endif
124
125static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
126{
127 struct tmio_mmc_host *host = mmc_priv(mmc);
128
129 if (enable) {
130 host->sdio_irq_enabled = 1;
Simon Horman54680fe2011-08-25 10:27:25 +0900131 host->sdio_irq_mask = TMIO_SDIO_MASK_ALL &
132 ~TMIO_SDIO_STAT_IOIRQ;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100133 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
Simon Horman54680fe2011-08-25 10:27:25 +0900134 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100135 } else {
Simon Horman54680fe2011-08-25 10:27:25 +0900136 host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
137 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100138 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
139 host->sdio_irq_enabled = 0;
140 }
141}
142
143static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
144{
145 u32 clk = 0, clock;
146
147 if (new_clock) {
148 for (clock = host->mmc->f_min, clk = 0x80000080;
149 new_clock >= (clock<<1); clk >>= 1)
150 clock <<= 1;
151 clk |= 0x100;
152 }
153
154 if (host->set_clk_div)
155 host->set_clk_div(host->pdev, (clk>>22) & 1);
156
157 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
158}
159
160static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
161{
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100162 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100163
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100164 /* implicit BUG_ON(!res) */
165 if (resource_size(res) > 0x100) {
166 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
167 msleep(10);
168 }
Guennadi Liakhovetskid9b03422011-03-10 18:43:07 +0100169
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100170 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
171 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
172 msleep(10);
173}
174
175static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
176{
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100177 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100178
179 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
180 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
181 msleep(10);
Guennadi Liakhovetskid9b03422011-03-10 18:43:07 +0100182
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100183 /* implicit BUG_ON(!res) */
184 if (resource_size(res) > 0x100) {
185 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
186 msleep(10);
187 }
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100188}
189
190static void tmio_mmc_reset(struct tmio_mmc_host *host)
191{
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100192 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
193
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100194 /* FIXME - should we set stop clock reg here */
195 sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100196 /* implicit BUG_ON(!res) */
197 if (resource_size(res) > 0x100)
198 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100199 msleep(10);
200 sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100201 if (resource_size(res) > 0x100)
202 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100203 msleep(10);
204}
205
206static void tmio_mmc_reset_work(struct work_struct *work)
207{
208 struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
209 delayed_reset_work.work);
210 struct mmc_request *mrq;
211 unsigned long flags;
212
213 spin_lock_irqsave(&host->lock, flags);
214 mrq = host->mrq;
215
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000216 /*
217 * is request already finished? Since we use a non-blocking
218 * cancel_delayed_work(), it can happen, that a .set_ios() call preempts
219 * us, so, have to check for IS_ERR(host->mrq)
220 */
221 if (IS_ERR_OR_NULL(mrq)
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100222 || time_is_after_jiffies(host->last_req_ts +
223 msecs_to_jiffies(2000))) {
224 spin_unlock_irqrestore(&host->lock, flags);
225 return;
226 }
227
228 dev_warn(&host->pdev->dev,
229 "timeout waiting for hardware interrupt (CMD%u)\n",
230 mrq->cmd->opcode);
231
232 if (host->data)
233 host->data->error = -ETIMEDOUT;
234 else if (host->cmd)
235 host->cmd->error = -ETIMEDOUT;
236 else
237 mrq->cmd->error = -ETIMEDOUT;
238
239 host->cmd = NULL;
240 host->data = NULL;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100241 host->force_pio = false;
242
243 spin_unlock_irqrestore(&host->lock, flags);
244
245 tmio_mmc_reset(host);
246
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000247 /* Ready for new calls */
248 host->mrq = NULL;
249
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100250 mmc_request_done(host->mmc, mrq);
251}
252
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000253/* called with host->lock held, interrupts disabled */
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100254static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
255{
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200256 struct mmc_request *mrq;
257 unsigned long flags;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100258
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200259 spin_lock_irqsave(&host->lock, flags);
260
261 mrq = host->mrq;
262 if (IS_ERR_OR_NULL(mrq)) {
263 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100264 return;
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200265 }
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100266
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100267 host->cmd = NULL;
268 host->data = NULL;
269 host->force_pio = false;
270
271 cancel_delayed_work(&host->delayed_reset_work);
272
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000273 host->mrq = NULL;
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200274 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000275
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100276 mmc_request_done(host->mmc, mrq);
277}
278
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200279static void tmio_mmc_done_work(struct work_struct *work)
280{
281 struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
282 done);
283 tmio_mmc_finish_request(host);
284}
285
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100286/* These are the bitmasks the tmio chip requires to implement the MMC response
287 * types. Note that R1 and R6 are the same in this scheme. */
288#define APP_CMD 0x0040
289#define RESP_NONE 0x0300
290#define RESP_R1 0x0400
291#define RESP_R1B 0x0500
292#define RESP_R2 0x0600
293#define RESP_R3 0x0700
294#define DATA_PRESENT 0x0800
295#define TRANSFER_READ 0x1000
296#define TRANSFER_MULTI 0x2000
297#define SECURITY_CMD 0x4000
298
299static int tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
300{
301 struct mmc_data *data = host->data;
302 int c = cmd->opcode;
303
304 /* Command 12 is handled by hardware */
305 if (cmd->opcode == 12 && !cmd->arg) {
306 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
307 return 0;
308 }
309
310 switch (mmc_resp_type(cmd)) {
311 case MMC_RSP_NONE: c |= RESP_NONE; break;
312 case MMC_RSP_R1: c |= RESP_R1; break;
313 case MMC_RSP_R1B: c |= RESP_R1B; break;
314 case MMC_RSP_R2: c |= RESP_R2; break;
315 case MMC_RSP_R3: c |= RESP_R3; break;
316 default:
317 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
318 return -EINVAL;
319 }
320
321 host->cmd = cmd;
322
323/* FIXME - this seems to be ok commented out but the spec suggest this bit
324 * should be set when issuing app commands.
325 * if(cmd->flags & MMC_FLAG_ACMD)
326 * c |= APP_CMD;
327 */
328 if (data) {
329 c |= DATA_PRESENT;
330 if (data->blocks > 1) {
331 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
332 c |= TRANSFER_MULTI;
333 }
334 if (data->flags & MMC_DATA_READ)
335 c |= TRANSFER_READ;
336 }
337
338 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_CMD);
339
340 /* Fire off the command */
341 sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
342 sd_ctrl_write16(host, CTL_SD_CMD, c);
343
344 return 0;
345}
346
347/*
348 * This chip always returns (at least?) as much data as you ask for.
349 * I'm unsure what happens if you ask for less than a block. This should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300350 * looked into to ensure that a funny length read doesn't hose the controller.
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100351 */
352static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
353{
354 struct mmc_data *data = host->data;
355 void *sg_virt;
356 unsigned short *buf;
357 unsigned int count;
358 unsigned long flags;
359
360 if ((host->chan_tx || host->chan_rx) && !host->force_pio) {
361 pr_err("PIO IRQ in DMA mode!\n");
362 return;
363 } else if (!data) {
364 pr_debug("Spurious PIO IRQ\n");
365 return;
366 }
367
368 sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
369 buf = (unsigned short *)(sg_virt + host->sg_off);
370
371 count = host->sg_ptr->length - host->sg_off;
372 if (count > data->blksz)
373 count = data->blksz;
374
375 pr_debug("count: %08x offset: %08x flags %08x\n",
376 count, host->sg_off, data->flags);
377
378 /* Transfer the data */
379 if (data->flags & MMC_DATA_READ)
380 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
381 else
382 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
383
384 host->sg_off += count;
385
386 tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
387
388 if (host->sg_off == host->sg_ptr->length)
389 tmio_mmc_next_sg(host);
390
391 return;
392}
393
394static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
395{
396 if (host->sg_ptr == &host->bounce_sg) {
397 unsigned long flags;
398 void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
399 memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
400 tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
401 }
402}
403
404/* needs to be called with host->lock held */
405void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
406{
407 struct mmc_data *data = host->data;
408 struct mmc_command *stop;
409
410 host->data = NULL;
411
412 if (!data) {
413 dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
414 return;
415 }
416 stop = data->stop;
417
418 /* FIXME - return correct transfer count on errors */
419 if (!data->error)
420 data->bytes_xfered = data->blocks * data->blksz;
421 else
422 data->bytes_xfered = 0;
423
424 pr_debug("Completed data request\n");
425
426 /*
427 * FIXME: other drivers allow an optional stop command of any given type
428 * which we dont do, as the chip can auto generate them.
429 * Perhaps we can be smarter about when to use auto CMD12 and
430 * only issue the auto request when we know this is the desired
431 * stop command, allowing fallback to the stop command the
432 * upper layers expect. For now, we do what works.
433 */
434
435 if (data->flags & MMC_DATA_READ) {
436 if (host->chan_rx && !host->force_pio)
437 tmio_mmc_check_bounce_buffer(host);
438 dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
439 host->mrq);
440 } else {
441 dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
442 host->mrq);
443 }
444
445 if (stop) {
446 if (stop->opcode == 12 && !stop->arg)
447 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
448 else
449 BUG();
450 }
451
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200452 schedule_work(&host->done);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100453}
454
455static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
456{
457 struct mmc_data *data;
458 spin_lock(&host->lock);
459 data = host->data;
460
461 if (!data)
462 goto out;
463
464 if (host->chan_tx && (data->flags & MMC_DATA_WRITE) && !host->force_pio) {
465 /*
466 * Has all data been written out yet? Testing on SuperH showed,
467 * that in most cases the first interrupt comes already with the
468 * BUSY status bit clear, but on some operations, like mount or
469 * in the beginning of a write / sync / umount, there is one
470 * DATAEND interrupt with the BUSY bit set, in this cases
471 * waiting for one more interrupt fixes the problem.
472 */
473 if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
474 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
475 tasklet_schedule(&host->dma_complete);
476 }
477 } else if (host->chan_rx && (data->flags & MMC_DATA_READ) && !host->force_pio) {
478 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
479 tasklet_schedule(&host->dma_complete);
480 } else {
481 tmio_mmc_do_data_irq(host);
482 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
483 }
484out:
485 spin_unlock(&host->lock);
486}
487
488static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
489 unsigned int stat)
490{
491 struct mmc_command *cmd = host->cmd;
492 int i, addr;
493
494 spin_lock(&host->lock);
495
496 if (!host->cmd) {
497 pr_debug("Spurious CMD irq\n");
498 goto out;
499 }
500
501 host->cmd = NULL;
502
503 /* This controller is sicker than the PXA one. Not only do we need to
504 * drop the top 8 bits of the first response word, we also need to
505 * modify the order of the response for short response command types.
506 */
507
508 for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
509 cmd->resp[i] = sd_ctrl_read32(host, addr);
510
511 if (cmd->flags & MMC_RSP_136) {
512 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
513 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
514 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
515 cmd->resp[3] <<= 8;
516 } else if (cmd->flags & MMC_RSP_R3) {
517 cmd->resp[0] = cmd->resp[3];
518 }
519
520 if (stat & TMIO_STAT_CMDTIMEOUT)
521 cmd->error = -ETIMEDOUT;
522 else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
523 cmd->error = -EILSEQ;
524
525 /* If there is data to handle we enable data IRQs here, and
526 * we will ultimatley finish the request in the data_end handler.
527 * If theres no data or we encountered an error, finish now.
528 */
529 if (host->data && !cmd->error) {
530 if (host->data->flags & MMC_DATA_READ) {
531 if (host->force_pio || !host->chan_rx)
532 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
533 else
534 tasklet_schedule(&host->dma_issue);
535 } else {
536 if (host->force_pio || !host->chan_tx)
537 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
538 else
539 tasklet_schedule(&host->dma_issue);
540 }
541 } else {
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200542 schedule_work(&host->done);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100543 }
544
545out:
546 spin_unlock(&host->lock);
547}
548
Simon Horman7729c7a2011-08-25 10:27:26 +0900549static void tmio_mmc_card_irq_status(struct tmio_mmc_host *host,
550 int *ireg, int *status)
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100551{
Simon Horman7729c7a2011-08-25 10:27:26 +0900552 *status = sd_ctrl_read32(host, CTL_STATUS);
553 *ireg = *status & TMIO_MASK_IRQ & ~host->sdcard_irq_mask;
554
555 pr_debug_status(*status);
556 pr_debug_status(*ireg);
557}
558
559static bool __tmio_mmc_card_detect_irq(struct tmio_mmc_host *host,
560 int ireg, int status)
561{
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200562 struct mmc_host *mmc = host->mmc;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100563
Paul Parsonse312eb12011-05-15 13:24:41 +0000564 /* Card insert / remove attempts */
565 if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
566 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
567 TMIO_STAT_CARD_REMOVE);
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200568 if ((((ireg & TMIO_STAT_CARD_REMOVE) && mmc->card) ||
569 ((ireg & TMIO_STAT_CARD_INSERT) && !mmc->card)) &&
570 !work_pending(&mmc->detect.work))
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200571 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
Simon Horman7729c7a2011-08-25 10:27:26 +0900572 return true;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100573 }
574
Simon Horman7729c7a2011-08-25 10:27:26 +0900575 return false;
576}
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100577
Simon Horman7729c7a2011-08-25 10:27:26 +0900578irqreturn_t tmio_mmc_card_detect_irq(int irq, void *devid)
579{
580 unsigned int ireg, status;
581 struct tmio_mmc_host *host = devid;
582
583 tmio_mmc_card_irq_status(host, &ireg, &status);
584 __tmio_mmc_card_detect_irq(host, ireg, status);
585
586 return IRQ_HANDLED;
587}
588EXPORT_SYMBOL(tmio_mmc_card_detect_irq);
589
590static bool __tmio_mmc_sdcard_irq(struct tmio_mmc_host *host,
591 int ireg, int status)
592{
Paul Parsonse312eb12011-05-15 13:24:41 +0000593 /* Command completion */
594 if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
595 tmio_mmc_ack_mmc_irqs(host,
596 TMIO_STAT_CMDRESPEND |
597 TMIO_STAT_CMDTIMEOUT);
598 tmio_mmc_cmd_irq(host, status);
Simon Horman7729c7a2011-08-25 10:27:26 +0900599 return true;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100600 }
Paul Parsonse312eb12011-05-15 13:24:41 +0000601
602 /* Data transfer */
603 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
604 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
605 tmio_mmc_pio_irq(host);
Simon Horman7729c7a2011-08-25 10:27:26 +0900606 return true;
Paul Parsonse312eb12011-05-15 13:24:41 +0000607 }
608
609 /* Data transfer completion */
610 if (ireg & TMIO_STAT_DATAEND) {
611 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
612 tmio_mmc_data_irq(host);
Simon Horman7729c7a2011-08-25 10:27:26 +0900613 return true;
Paul Parsonse312eb12011-05-15 13:24:41 +0000614 }
615
Simon Horman7729c7a2011-08-25 10:27:26 +0900616 return false;
617}
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100618
Simon Horman7729c7a2011-08-25 10:27:26 +0900619irqreturn_t tmio_mmc_sdcard_irq(int irq, void *devid)
620{
621 unsigned int ireg, status;
622 struct tmio_mmc_host *host = devid;
623
624 tmio_mmc_card_irq_status(host, &ireg, &status);
625 __tmio_mmc_sdcard_irq(host, ireg, status);
626
627 return IRQ_HANDLED;
628}
629EXPORT_SYMBOL(tmio_mmc_sdcard_irq);
630
631irqreturn_t tmio_mmc_sdio_irq(int irq, void *devid)
632{
633 struct tmio_mmc_host *host = devid;
634 struct mmc_host *mmc = host->mmc;
635 struct tmio_mmc_data *pdata = host->pdata;
636 unsigned int ireg, status;
637
638 if (!(pdata->flags & TMIO_MMC_SDIO_IRQ))
639 return IRQ_HANDLED;
640
641 status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
642 ireg = status & TMIO_SDIO_MASK_ALL & ~host->sdcard_irq_mask;
643
644 sd_ctrl_write16(host, CTL_SDIO_STATUS, status & ~TMIO_SDIO_MASK_ALL);
645
646 if (mmc->caps & MMC_CAP_SDIO_IRQ && ireg & TMIO_SDIO_STAT_IOIRQ)
647 mmc_signal_sdio_irq(mmc);
648
649 return IRQ_HANDLED;
650}
651EXPORT_SYMBOL(tmio_mmc_sdio_irq);
652
653irqreturn_t tmio_mmc_irq(int irq, void *devid)
654{
655 struct tmio_mmc_host *host = devid;
656 unsigned int ireg, status;
657
658 pr_debug("MMC IRQ begin\n");
659
660 tmio_mmc_card_irq_status(host, &ireg, &status);
661 if (__tmio_mmc_card_detect_irq(host, ireg, status))
662 return IRQ_HANDLED;
663 if (__tmio_mmc_sdcard_irq(host, ireg, status))
664 return IRQ_HANDLED;
665
666 tmio_mmc_sdio_irq(irq, devid);
667
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100668 return IRQ_HANDLED;
669}
Magnus Damm8e7bfdb2011-05-06 11:02:33 +0000670EXPORT_SYMBOL(tmio_mmc_irq);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100671
672static int tmio_mmc_start_data(struct tmio_mmc_host *host,
673 struct mmc_data *data)
674{
675 struct tmio_mmc_data *pdata = host->pdata;
676
677 pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
678 data->blksz, data->blocks);
679
680 /* Some hardware cannot perform 2 byte requests in 4 bit mode */
681 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
682 int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
683
684 if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
685 pr_err("%s: %d byte block unsupported in 4 bit mode\n",
686 mmc_hostname(host->mmc), data->blksz);
687 return -EINVAL;
688 }
689 }
690
691 tmio_mmc_init_sg(host, data);
692 host->data = data;
693
694 /* Set transfer length / blocksize */
695 sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
696 sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
697
698 tmio_mmc_start_dma(host, data);
699
700 return 0;
701}
702
703/* Process requests from the MMC layer */
704static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
705{
706 struct tmio_mmc_host *host = mmc_priv(mmc);
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000707 unsigned long flags;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100708 int ret;
709
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000710 spin_lock_irqsave(&host->lock, flags);
711
712 if (host->mrq) {
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100713 pr_debug("request not null\n");
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000714 if (IS_ERR(host->mrq)) {
715 spin_unlock_irqrestore(&host->lock, flags);
716 mrq->cmd->error = -EAGAIN;
717 mmc_request_done(mmc, mrq);
718 return;
719 }
720 }
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100721
722 host->last_req_ts = jiffies;
723 wmb();
724 host->mrq = mrq;
725
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000726 spin_unlock_irqrestore(&host->lock, flags);
727
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100728 if (mrq->data) {
729 ret = tmio_mmc_start_data(host, mrq->data);
730 if (ret)
731 goto fail;
732 }
733
734 ret = tmio_mmc_start_command(host, mrq->cmd);
735 if (!ret) {
736 schedule_delayed_work(&host->delayed_reset_work,
737 msecs_to_jiffies(2000));
738 return;
739 }
740
741fail:
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100742 host->force_pio = false;
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000743 host->mrq = NULL;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100744 mrq->cmd->error = ret;
745 mmc_request_done(mmc, mrq);
746}
747
748/* Set MMC clock / power.
749 * Note: This controller uses a simple divider scheme therefore it cannot
750 * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
751 * MMC wont run that fast, it has to be clocked at 12MHz which is the next
752 * slowest setting.
753 */
754static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
755{
756 struct tmio_mmc_host *host = mmc_priv(mmc);
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +0000757 struct tmio_mmc_data *pdata = host->pdata;
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000758 unsigned long flags;
759
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200760 mutex_lock(&host->ios_lock);
761
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000762 spin_lock_irqsave(&host->lock, flags);
763 if (host->mrq) {
764 if (IS_ERR(host->mrq)) {
765 dev_dbg(&host->pdev->dev,
766 "%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
767 current->comm, task_pid_nr(current),
768 ios->clock, ios->power_mode);
769 host->mrq = ERR_PTR(-EINTR);
770 } else {
771 dev_dbg(&host->pdev->dev,
772 "%s.%d: CMD%u active since %lu, now %lu!\n",
773 current->comm, task_pid_nr(current),
774 host->mrq->cmd->opcode, host->last_req_ts, jiffies);
775 }
776 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200777
778 mutex_unlock(&host->ios_lock);
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000779 return;
780 }
781
782 host->mrq = ERR_PTR(-EBUSY);
783
784 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100785
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200786 /*
787 * pdata->power == false only if COLD_CD is available, otherwise only
788 * in short time intervals during probing or resuming
789 */
790 if (ios->power_mode == MMC_POWER_ON && ios->clock) {
791 if (!pdata->power) {
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +0000792 pm_runtime_get_sync(&host->pdev->dev);
793 pdata->power = true;
794 }
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200795 tmio_mmc_set_clock(host, ios->clock);
Guennadi Liakhovetskic919c2a2011-04-21 09:09:59 +0200796 /* power up SD bus */
797 if (host->set_pwr)
798 host->set_pwr(host->pdev, 1);
Guennadi Liakhovetski5fd01572011-03-09 14:45:44 +0100799 /* start bus clock */
800 tmio_mmc_clk_start(host);
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200801 } else if (ios->power_mode != MMC_POWER_UP) {
Guennadi Liakhovetskif6b8b522011-11-16 10:11:45 +0100802 if (host->set_pwr && ios->power_mode == MMC_POWER_OFF)
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200803 host->set_pwr(host->pdev, 0);
Bastian Hechtcbb18b32011-12-23 23:03:13 +0100804 if (pdata->power) {
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200805 pdata->power = false;
806 pm_runtime_put(&host->pdev->dev);
807 }
808 tmio_mmc_clk_stop(host);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100809 }
810
811 switch (ios->bus_width) {
812 case MMC_BUS_WIDTH_1:
813 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
814 break;
815 case MMC_BUS_WIDTH_4:
816 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
817 break;
818 }
819
820 /* Let things settle. delay taken from winCE driver */
821 udelay(140);
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000822 if (PTR_ERR(host->mrq) == -EINTR)
823 dev_dbg(&host->pdev->dev,
824 "%s.%d: IOS interrupted: clk %u, mode %u",
825 current->comm, task_pid_nr(current),
826 ios->clock, ios->power_mode);
827 host->mrq = NULL;
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200828
829 mutex_unlock(&host->ios_lock);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100830}
831
832static int tmio_mmc_get_ro(struct mmc_host *mmc)
833{
834 struct tmio_mmc_host *host = mmc_priv(mmc);
835 struct tmio_mmc_data *pdata = host->pdata;
836
Guennadi Liakhovetski7d8b4c22011-06-20 16:51:10 +0200837 return !((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
838 (sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT));
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100839}
840
841static int tmio_mmc_get_cd(struct mmc_host *mmc)
842{
843 struct tmio_mmc_host *host = mmc_priv(mmc);
844 struct tmio_mmc_data *pdata = host->pdata;
845
846 if (!pdata->get_cd)
847 return -ENOSYS;
848 else
849 return pdata->get_cd(host->pdev);
850}
851
852static const struct mmc_host_ops tmio_mmc_ops = {
853 .request = tmio_mmc_request,
854 .set_ios = tmio_mmc_set_ios,
855 .get_ro = tmio_mmc_get_ro,
856 .get_cd = tmio_mmc_get_cd,
857 .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
858};
859
860int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host,
861 struct platform_device *pdev,
862 struct tmio_mmc_data *pdata)
863{
864 struct tmio_mmc_host *_host;
865 struct mmc_host *mmc;
866 struct resource *res_ctl;
867 int ret;
868 u32 irq_mask = TMIO_MASK_CMD;
869
870 res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0);
871 if (!res_ctl)
872 return -EINVAL;
873
874 mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
875 if (!mmc)
876 return -ENOMEM;
877
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +0000878 pdata->dev = &pdev->dev;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100879 _host = mmc_priv(mmc);
880 _host->pdata = pdata;
881 _host->mmc = mmc;
882 _host->pdev = pdev;
883 platform_set_drvdata(pdev, mmc);
884
885 _host->set_pwr = pdata->set_pwr;
886 _host->set_clk_div = pdata->set_clk_div;
887
888 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
889 _host->bus_shift = resource_size(res_ctl) >> 10;
890
891 _host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
892 if (!_host->ctl) {
893 ret = -ENOMEM;
894 goto host_free;
895 }
896
897 mmc->ops = &tmio_mmc_ops;
898 mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
899 mmc->f_max = pdata->hclk;
900 mmc->f_min = mmc->f_max / 512;
901 mmc->max_segs = 32;
902 mmc->max_blk_size = 512;
903 mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
904 mmc->max_segs;
905 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
906 mmc->max_seg_size = mmc->max_req_size;
907 if (pdata->ocr_mask)
908 mmc->ocr_avail = pdata->ocr_mask;
909 else
910 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
911
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +0000912 pdata->power = false;
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000913 pm_runtime_enable(&pdev->dev);
914 ret = pm_runtime_resume(&pdev->dev);
915 if (ret < 0)
916 goto pm_disable;
917
Bastian Hechtcbb18b32011-12-23 23:03:13 +0100918 /*
919 * There are 4 different scenarios for the card detection:
920 * 1) an external gpio irq handles the cd (best for power savings)
921 * 2) internal sdhi irq handles the cd
922 * 3) a worker thread polls the sdhi - indicated by MMC_CAP_NEEDS_POLL
923 * 4) the medium is non-removable - indicated by MMC_CAP_NONREMOVABLE
924 *
925 * While we increment the rtpm counter for all scenarios when the mmc
926 * core activates us by calling an appropriate set_ios(), we must
927 * additionally ensure that in case 2) the tmio mmc hardware stays
928 * powered on during runtime for the card detection to work.
929 */
930 if (!(pdata->flags & TMIO_MMC_HAS_COLD_CD
931 || mmc->caps & MMC_CAP_NEEDS_POLL
932 || mmc->caps & MMC_CAP_NONREMOVABLE))
933 pm_runtime_get_noresume(&pdev->dev);
934
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100935 tmio_mmc_clk_stop(_host);
936 tmio_mmc_reset(_host);
937
Simon Horman54680fe2011-08-25 10:27:25 +0900938 _host->sdcard_irq_mask = sd_ctrl_read32(_host, CTL_IRQ_MASK);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100939 tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
940 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
941 tmio_mmc_enable_sdio_irq(mmc, 0);
942
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100943 spin_lock_init(&_host->lock);
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200944 mutex_init(&_host->ios_lock);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100945
946 /* Init delayed work for request timeouts */
947 INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200948 INIT_WORK(&_host->done, tmio_mmc_done_work);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100949
950 /* See if we also get DMA */
951 tmio_mmc_request_dma(_host, pdata);
952
953 mmc_add_host(mmc);
954
Rafael J. Wysockic419e612012-03-13 01:01:51 +0100955 dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
956
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100957 /* Unmask the IRQs we want to know about */
958 if (!_host->chan_rx)
959 irq_mask |= TMIO_MASK_READOP;
960 if (!_host->chan_tx)
961 irq_mask |= TMIO_MASK_WRITEOP;
962
963 tmio_mmc_enable_mmc_irqs(_host, irq_mask);
964
965 *host = _host;
966
967 return 0;
968
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000969pm_disable:
970 pm_runtime_disable(&pdev->dev);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100971 iounmap(_host->ctl);
972host_free:
973 mmc_free_host(mmc);
974
975 return ret;
976}
977EXPORT_SYMBOL(tmio_mmc_host_probe);
978
979void tmio_mmc_host_remove(struct tmio_mmc_host *host)
980{
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000981 struct platform_device *pdev = host->pdev;
982
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +0000983 /*
984 * We don't have to manipulate pdata->power here: if there is a card in
985 * the slot, the runtime PM is active and our .runtime_resume() will not
986 * be run. If there is no card in the slot and the platform can suspend
987 * the controller, the runtime PM is suspended and pdata->power == false,
988 * so, our .runtime_resume() will not try to detect a card in the slot.
989 */
Bastian Hechtcbb18b32011-12-23 23:03:13 +0100990 if (host->pdata->flags & TMIO_MMC_HAS_COLD_CD
991 || host->mmc->caps & MMC_CAP_NEEDS_POLL
992 || host->mmc->caps & MMC_CAP_NONREMOVABLE)
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +0000993 pm_runtime_get_sync(&pdev->dev);
994
Rafael J. Wysockic419e612012-03-13 01:01:51 +0100995 dev_pm_qos_hide_latency_limit(&pdev->dev);
996
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100997 mmc_remove_host(host->mmc);
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200998 cancel_work_sync(&host->done);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100999 cancel_delayed_work_sync(&host->delayed_reset_work);
1000 tmio_mmc_release_dma(host);
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001001
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001002 pm_runtime_put_sync(&pdev->dev);
1003 pm_runtime_disable(&pdev->dev);
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001004
1005 iounmap(host->ctl);
1006 mmc_free_host(host->mmc);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +01001007}
1008EXPORT_SYMBOL(tmio_mmc_host_remove);
1009
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001010#ifdef CONFIG_PM
1011int tmio_mmc_host_suspend(struct device *dev)
1012{
1013 struct mmc_host *mmc = dev_get_drvdata(dev);
1014 struct tmio_mmc_host *host = mmc_priv(mmc);
1015 int ret = mmc_suspend_host(mmc);
1016
1017 if (!ret)
1018 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
1019
1020 host->pm_error = pm_runtime_put_sync(dev);
1021
1022 return ret;
1023}
1024EXPORT_SYMBOL(tmio_mmc_host_suspend);
1025
1026int tmio_mmc_host_resume(struct device *dev)
1027{
1028 struct mmc_host *mmc = dev_get_drvdata(dev);
1029 struct tmio_mmc_host *host = mmc_priv(mmc);
1030
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001031 /* The MMC core will perform the complete set up */
1032 host->pdata->power = false;
1033
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +02001034 host->pm_global = true;
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001035 if (!host->pm_error)
1036 pm_runtime_get_sync(dev);
1037
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +02001038 if (host->pm_global) {
1039 /* Runtime PM resume callback didn't run */
1040 tmio_mmc_reset(host);
Guennadi Liakhovetski162f43e2011-07-14 18:39:10 +02001041 tmio_mmc_enable_dma(host, true);
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +02001042 host->pm_global = false;
1043 }
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001044
1045 return mmc_resume_host(mmc);
1046}
1047EXPORT_SYMBOL(tmio_mmc_host_resume);
1048
1049#endif /* CONFIG_PM */
1050
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001051int tmio_mmc_host_runtime_suspend(struct device *dev)
1052{
1053 return 0;
1054}
1055EXPORT_SYMBOL(tmio_mmc_host_runtime_suspend);
1056
1057int tmio_mmc_host_runtime_resume(struct device *dev)
1058{
1059 struct mmc_host *mmc = dev_get_drvdata(dev);
1060 struct tmio_mmc_host *host = mmc_priv(mmc);
1061 struct tmio_mmc_data *pdata = host->pdata;
1062
1063 tmio_mmc_reset(host);
Guennadi Liakhovetski162f43e2011-07-14 18:39:10 +02001064 tmio_mmc_enable_dma(host, true);
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001065
1066 if (pdata->power) {
1067 /* Only entered after a card-insert interrupt */
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +02001068 if (!mmc->card)
1069 tmio_mmc_set_ios(mmc, &mmc->ios);
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001070 mmc_detect_change(mmc, msecs_to_jiffies(100));
1071 }
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +02001072 host->pm_global = false;
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001073
1074 return 0;
1075}
1076EXPORT_SYMBOL(tmio_mmc_host_runtime_resume);
1077
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +01001078MODULE_LICENSE("GPL v2");