blob: 42970ab6e803d72e36473294f32d9775e9c624ac [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>
Guennadi Liakhovetskic8be24c2012-02-09 22:57:09 +010037#include <linux/mmc/cd-gpio.h>
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010038#include <linux/mmc/host.h>
Simon Hormancba179a2011-03-24 09:48:36 +010039#include <linux/mmc/tmio.h>
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010040#include <linux/module.h>
41#include <linux/pagemap.h>
42#include <linux/platform_device.h>
Rafael J. Wysockic419e612012-03-13 01:01:51 +010043#include <linux/pm_qos.h>
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +000044#include <linux/pm_runtime.h>
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010045#include <linux/scatterlist.h>
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010046#include <linux/spinlock.h>
Guennadi Liakhovetskie3de2be2012-01-06 13:06:51 +010047#include <linux/workqueue.h>
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010048
49#include "tmio_mmc.h"
50
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010051void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
52{
Simon Horman54680fe2011-08-25 10:27:25 +090053 host->sdcard_irq_mask &= ~(i & TMIO_MASK_IRQ);
54 sd_ctrl_write32(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010055}
56
57void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
58{
Simon Horman54680fe2011-08-25 10:27:25 +090059 host->sdcard_irq_mask |= (i & TMIO_MASK_IRQ);
60 sd_ctrl_write32(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010061}
62
63static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i)
64{
65 sd_ctrl_write32(host, CTL_STATUS, ~i);
66}
67
68static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
69{
70 host->sg_len = data->sg_len;
71 host->sg_ptr = data->sg;
72 host->sg_orig = data->sg;
73 host->sg_off = 0;
74}
75
76static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
77{
78 host->sg_ptr = sg_next(host->sg_ptr);
79 host->sg_off = 0;
80 return --host->sg_len;
81}
82
83#ifdef CONFIG_MMC_DEBUG
84
85#define STATUS_TO_TEXT(a, status, i) \
86 do { \
87 if (status & TMIO_STAT_##a) { \
88 if (i++) \
89 printk(" | "); \
90 printk(#a); \
91 } \
92 } while (0)
93
94static void pr_debug_status(u32 status)
95{
96 int i = 0;
Girish K Sa3c76eb2011-10-11 11:44:09 +053097 pr_debug("status: %08x = ", status);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010098 STATUS_TO_TEXT(CARD_REMOVE, status, i);
99 STATUS_TO_TEXT(CARD_INSERT, status, i);
100 STATUS_TO_TEXT(SIGSTATE, status, i);
101 STATUS_TO_TEXT(WRPROTECT, status, i);
102 STATUS_TO_TEXT(CARD_REMOVE_A, status, i);
103 STATUS_TO_TEXT(CARD_INSERT_A, status, i);
104 STATUS_TO_TEXT(SIGSTATE_A, status, i);
105 STATUS_TO_TEXT(CMD_IDX_ERR, status, i);
106 STATUS_TO_TEXT(STOPBIT_ERR, status, i);
107 STATUS_TO_TEXT(ILL_FUNC, status, i);
108 STATUS_TO_TEXT(CMD_BUSY, status, i);
109 STATUS_TO_TEXT(CMDRESPEND, status, i);
110 STATUS_TO_TEXT(DATAEND, status, i);
111 STATUS_TO_TEXT(CRCFAIL, status, i);
112 STATUS_TO_TEXT(DATATIMEOUT, status, i);
113 STATUS_TO_TEXT(CMDTIMEOUT, status, i);
114 STATUS_TO_TEXT(RXOVERFLOW, status, i);
115 STATUS_TO_TEXT(TXUNDERRUN, status, i);
116 STATUS_TO_TEXT(RXRDY, status, i);
117 STATUS_TO_TEXT(TXRQ, status, i);
118 STATUS_TO_TEXT(ILL_ACCESS, status, i);
119 printk("\n");
120}
121
122#else
123#define pr_debug_status(s) do { } while (0)
124#endif
125
126static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
127{
128 struct tmio_mmc_host *host = mmc_priv(mmc);
129
130 if (enable) {
131 host->sdio_irq_enabled = 1;
Simon Horman54680fe2011-08-25 10:27:25 +0900132 host->sdio_irq_mask = TMIO_SDIO_MASK_ALL &
133 ~TMIO_SDIO_STAT_IOIRQ;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100134 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
Simon Horman54680fe2011-08-25 10:27:25 +0900135 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100136 } else {
Simon Horman54680fe2011-08-25 10:27:25 +0900137 host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
138 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100139 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
140 host->sdio_irq_enabled = 0;
141 }
142}
143
144static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
145{
146 u32 clk = 0, clock;
147
148 if (new_clock) {
149 for (clock = host->mmc->f_min, clk = 0x80000080;
150 new_clock >= (clock<<1); clk >>= 1)
151 clock <<= 1;
152 clk |= 0x100;
153 }
154
155 if (host->set_clk_div)
156 host->set_clk_div(host->pdev, (clk>>22) & 1);
157
158 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
159}
160
161static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
162{
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100163 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100164
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100165 /* implicit BUG_ON(!res) */
166 if (resource_size(res) > 0x100) {
167 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
168 msleep(10);
169 }
Guennadi Liakhovetskid9b03422011-03-10 18:43:07 +0100170
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100171 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
172 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
173 msleep(10);
174}
175
176static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
177{
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100178 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100179
180 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
181 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
182 msleep(10);
Guennadi Liakhovetskid9b03422011-03-10 18:43:07 +0100183
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100184 /* implicit BUG_ON(!res) */
185 if (resource_size(res) > 0x100) {
186 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
187 msleep(10);
188 }
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100189}
190
191static void tmio_mmc_reset(struct tmio_mmc_host *host)
192{
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100193 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
194
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100195 /* FIXME - should we set stop clock reg here */
196 sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100197 /* implicit BUG_ON(!res) */
198 if (resource_size(res) > 0x100)
199 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100200 msleep(10);
201 sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100202 if (resource_size(res) > 0x100)
203 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100204 msleep(10);
205}
206
207static void tmio_mmc_reset_work(struct work_struct *work)
208{
209 struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
210 delayed_reset_work.work);
211 struct mmc_request *mrq;
212 unsigned long flags;
213
214 spin_lock_irqsave(&host->lock, flags);
215 mrq = host->mrq;
216
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000217 /*
218 * is request already finished? Since we use a non-blocking
219 * cancel_delayed_work(), it can happen, that a .set_ios() call preempts
220 * us, so, have to check for IS_ERR(host->mrq)
221 */
222 if (IS_ERR_OR_NULL(mrq)
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100223 || time_is_after_jiffies(host->last_req_ts +
224 msecs_to_jiffies(2000))) {
225 spin_unlock_irqrestore(&host->lock, flags);
226 return;
227 }
228
229 dev_warn(&host->pdev->dev,
230 "timeout waiting for hardware interrupt (CMD%u)\n",
231 mrq->cmd->opcode);
232
233 if (host->data)
234 host->data->error = -ETIMEDOUT;
235 else if (host->cmd)
236 host->cmd->error = -ETIMEDOUT;
237 else
238 mrq->cmd->error = -ETIMEDOUT;
239
240 host->cmd = NULL;
241 host->data = NULL;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100242 host->force_pio = false;
243
244 spin_unlock_irqrestore(&host->lock, flags);
245
246 tmio_mmc_reset(host);
247
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000248 /* Ready for new calls */
249 host->mrq = NULL;
250
Guennadi Liakhovetskie3de2be2012-01-06 13:06:51 +0100251 tmio_mmc_abort_dma(host);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100252 mmc_request_done(host->mmc, mrq);
253}
254
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000255/* called with host->lock held, interrupts disabled */
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100256static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
257{
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200258 struct mmc_request *mrq;
259 unsigned long flags;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100260
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200261 spin_lock_irqsave(&host->lock, flags);
262
263 mrq = host->mrq;
264 if (IS_ERR_OR_NULL(mrq)) {
265 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100266 return;
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200267 }
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100268
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100269 host->cmd = NULL;
270 host->data = NULL;
271 host->force_pio = false;
272
273 cancel_delayed_work(&host->delayed_reset_work);
274
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000275 host->mrq = NULL;
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200276 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000277
Guennadi Liakhovetskie3de2be2012-01-06 13:06:51 +0100278 if (mrq->cmd->error || (mrq->data && mrq->data->error))
279 tmio_mmc_abort_dma(host);
280
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100281 mmc_request_done(host->mmc, mrq);
282}
283
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200284static void tmio_mmc_done_work(struct work_struct *work)
285{
286 struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
287 done);
288 tmio_mmc_finish_request(host);
289}
290
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100291/* These are the bitmasks the tmio chip requires to implement the MMC response
292 * types. Note that R1 and R6 are the same in this scheme. */
293#define APP_CMD 0x0040
294#define RESP_NONE 0x0300
295#define RESP_R1 0x0400
296#define RESP_R1B 0x0500
297#define RESP_R2 0x0600
298#define RESP_R3 0x0700
299#define DATA_PRESENT 0x0800
300#define TRANSFER_READ 0x1000
301#define TRANSFER_MULTI 0x2000
302#define SECURITY_CMD 0x4000
303
304static int tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
305{
306 struct mmc_data *data = host->data;
307 int c = cmd->opcode;
Guennadi Liakhovetskie23cd532012-02-22 13:16:09 +0100308 u32 irq_mask = TMIO_MASK_CMD;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100309
310 /* Command 12 is handled by hardware */
311 if (cmd->opcode == 12 && !cmd->arg) {
312 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
313 return 0;
314 }
315
316 switch (mmc_resp_type(cmd)) {
317 case MMC_RSP_NONE: c |= RESP_NONE; break;
318 case MMC_RSP_R1: c |= RESP_R1; break;
319 case MMC_RSP_R1B: c |= RESP_R1B; break;
320 case MMC_RSP_R2: c |= RESP_R2; break;
321 case MMC_RSP_R3: c |= RESP_R3; break;
322 default:
323 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
324 return -EINVAL;
325 }
326
327 host->cmd = cmd;
328
329/* FIXME - this seems to be ok commented out but the spec suggest this bit
330 * should be set when issuing app commands.
331 * if(cmd->flags & MMC_FLAG_ACMD)
332 * c |= APP_CMD;
333 */
334 if (data) {
335 c |= DATA_PRESENT;
336 if (data->blocks > 1) {
337 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
338 c |= TRANSFER_MULTI;
339 }
340 if (data->flags & MMC_DATA_READ)
341 c |= TRANSFER_READ;
342 }
343
Guennadi Liakhovetskie23cd532012-02-22 13:16:09 +0100344 if (!host->native_hotplug)
345 irq_mask &= ~(TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
346 tmio_mmc_enable_mmc_irqs(host, irq_mask);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100347
348 /* Fire off the command */
349 sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
350 sd_ctrl_write16(host, CTL_SD_CMD, c);
351
352 return 0;
353}
354
355/*
356 * This chip always returns (at least?) as much data as you ask for.
357 * I'm unsure what happens if you ask for less than a block. This should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300358 * looked into to ensure that a funny length read doesn't hose the controller.
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100359 */
360static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
361{
362 struct mmc_data *data = host->data;
363 void *sg_virt;
364 unsigned short *buf;
365 unsigned int count;
366 unsigned long flags;
367
368 if ((host->chan_tx || host->chan_rx) && !host->force_pio) {
369 pr_err("PIO IRQ in DMA mode!\n");
370 return;
371 } else if (!data) {
372 pr_debug("Spurious PIO IRQ\n");
373 return;
374 }
375
376 sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
377 buf = (unsigned short *)(sg_virt + host->sg_off);
378
379 count = host->sg_ptr->length - host->sg_off;
380 if (count > data->blksz)
381 count = data->blksz;
382
383 pr_debug("count: %08x offset: %08x flags %08x\n",
384 count, host->sg_off, data->flags);
385
386 /* Transfer the data */
387 if (data->flags & MMC_DATA_READ)
388 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
389 else
390 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
391
392 host->sg_off += count;
393
394 tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
395
396 if (host->sg_off == host->sg_ptr->length)
397 tmio_mmc_next_sg(host);
398
399 return;
400}
401
402static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
403{
404 if (host->sg_ptr == &host->bounce_sg) {
405 unsigned long flags;
406 void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
407 memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
408 tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
409 }
410}
411
412/* needs to be called with host->lock held */
413void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
414{
415 struct mmc_data *data = host->data;
416 struct mmc_command *stop;
417
418 host->data = NULL;
419
420 if (!data) {
421 dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
422 return;
423 }
424 stop = data->stop;
425
426 /* FIXME - return correct transfer count on errors */
427 if (!data->error)
428 data->bytes_xfered = data->blocks * data->blksz;
429 else
430 data->bytes_xfered = 0;
431
432 pr_debug("Completed data request\n");
433
434 /*
435 * FIXME: other drivers allow an optional stop command of any given type
436 * which we dont do, as the chip can auto generate them.
437 * Perhaps we can be smarter about when to use auto CMD12 and
438 * only issue the auto request when we know this is the desired
439 * stop command, allowing fallback to the stop command the
440 * upper layers expect. For now, we do what works.
441 */
442
443 if (data->flags & MMC_DATA_READ) {
444 if (host->chan_rx && !host->force_pio)
445 tmio_mmc_check_bounce_buffer(host);
446 dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
447 host->mrq);
448 } else {
449 dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
450 host->mrq);
451 }
452
453 if (stop) {
454 if (stop->opcode == 12 && !stop->arg)
455 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
456 else
457 BUG();
458 }
459
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200460 schedule_work(&host->done);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100461}
462
463static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
464{
465 struct mmc_data *data;
466 spin_lock(&host->lock);
467 data = host->data;
468
469 if (!data)
470 goto out;
471
472 if (host->chan_tx && (data->flags & MMC_DATA_WRITE) && !host->force_pio) {
473 /*
474 * Has all data been written out yet? Testing on SuperH showed,
475 * that in most cases the first interrupt comes already with the
476 * BUSY status bit clear, but on some operations, like mount or
477 * in the beginning of a write / sync / umount, there is one
478 * DATAEND interrupt with the BUSY bit set, in this cases
479 * waiting for one more interrupt fixes the problem.
480 */
481 if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
482 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
483 tasklet_schedule(&host->dma_complete);
484 }
485 } else if (host->chan_rx && (data->flags & MMC_DATA_READ) && !host->force_pio) {
486 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
487 tasklet_schedule(&host->dma_complete);
488 } else {
489 tmio_mmc_do_data_irq(host);
490 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
491 }
492out:
493 spin_unlock(&host->lock);
494}
495
496static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
497 unsigned int stat)
498{
499 struct mmc_command *cmd = host->cmd;
500 int i, addr;
501
502 spin_lock(&host->lock);
503
504 if (!host->cmd) {
505 pr_debug("Spurious CMD irq\n");
506 goto out;
507 }
508
509 host->cmd = NULL;
510
511 /* This controller is sicker than the PXA one. Not only do we need to
512 * drop the top 8 bits of the first response word, we also need to
513 * modify the order of the response for short response command types.
514 */
515
516 for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
517 cmd->resp[i] = sd_ctrl_read32(host, addr);
518
519 if (cmd->flags & MMC_RSP_136) {
520 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
521 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
522 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
523 cmd->resp[3] <<= 8;
524 } else if (cmd->flags & MMC_RSP_R3) {
525 cmd->resp[0] = cmd->resp[3];
526 }
527
528 if (stat & TMIO_STAT_CMDTIMEOUT)
529 cmd->error = -ETIMEDOUT;
530 else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
531 cmd->error = -EILSEQ;
532
533 /* If there is data to handle we enable data IRQs here, and
534 * we will ultimatley finish the request in the data_end handler.
535 * If theres no data or we encountered an error, finish now.
536 */
537 if (host->data && !cmd->error) {
538 if (host->data->flags & MMC_DATA_READ) {
539 if (host->force_pio || !host->chan_rx)
540 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
541 else
542 tasklet_schedule(&host->dma_issue);
543 } else {
544 if (host->force_pio || !host->chan_tx)
545 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
546 else
547 tasklet_schedule(&host->dma_issue);
548 }
549 } else {
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200550 schedule_work(&host->done);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100551 }
552
553out:
554 spin_unlock(&host->lock);
555}
556
Simon Horman7729c7a2011-08-25 10:27:26 +0900557static void tmio_mmc_card_irq_status(struct tmio_mmc_host *host,
558 int *ireg, int *status)
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100559{
Simon Horman7729c7a2011-08-25 10:27:26 +0900560 *status = sd_ctrl_read32(host, CTL_STATUS);
561 *ireg = *status & TMIO_MASK_IRQ & ~host->sdcard_irq_mask;
562
563 pr_debug_status(*status);
564 pr_debug_status(*ireg);
565}
566
567static bool __tmio_mmc_card_detect_irq(struct tmio_mmc_host *host,
568 int ireg, int status)
569{
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200570 struct mmc_host *mmc = host->mmc;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100571
Paul Parsonse312eb12011-05-15 13:24:41 +0000572 /* Card insert / remove attempts */
573 if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
574 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
575 TMIO_STAT_CARD_REMOVE);
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200576 if ((((ireg & TMIO_STAT_CARD_REMOVE) && mmc->card) ||
577 ((ireg & TMIO_STAT_CARD_INSERT) && !mmc->card)) &&
578 !work_pending(&mmc->detect.work))
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200579 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
Simon Horman7729c7a2011-08-25 10:27:26 +0900580 return true;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100581 }
582
Simon Horman7729c7a2011-08-25 10:27:26 +0900583 return false;
584}
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100585
Simon Horman7729c7a2011-08-25 10:27:26 +0900586irqreturn_t tmio_mmc_card_detect_irq(int irq, void *devid)
587{
588 unsigned int ireg, status;
589 struct tmio_mmc_host *host = devid;
590
591 tmio_mmc_card_irq_status(host, &ireg, &status);
592 __tmio_mmc_card_detect_irq(host, ireg, status);
593
594 return IRQ_HANDLED;
595}
596EXPORT_SYMBOL(tmio_mmc_card_detect_irq);
597
598static bool __tmio_mmc_sdcard_irq(struct tmio_mmc_host *host,
599 int ireg, int status)
600{
Paul Parsonse312eb12011-05-15 13:24:41 +0000601 /* Command completion */
602 if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
603 tmio_mmc_ack_mmc_irqs(host,
604 TMIO_STAT_CMDRESPEND |
605 TMIO_STAT_CMDTIMEOUT);
606 tmio_mmc_cmd_irq(host, status);
Simon Horman7729c7a2011-08-25 10:27:26 +0900607 return true;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100608 }
Paul Parsonse312eb12011-05-15 13:24:41 +0000609
610 /* Data transfer */
611 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
612 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
613 tmio_mmc_pio_irq(host);
Simon Horman7729c7a2011-08-25 10:27:26 +0900614 return true;
Paul Parsonse312eb12011-05-15 13:24:41 +0000615 }
616
617 /* Data transfer completion */
618 if (ireg & TMIO_STAT_DATAEND) {
619 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
620 tmio_mmc_data_irq(host);
Simon Horman7729c7a2011-08-25 10:27:26 +0900621 return true;
Paul Parsonse312eb12011-05-15 13:24:41 +0000622 }
623
Simon Horman7729c7a2011-08-25 10:27:26 +0900624 return false;
625}
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100626
Simon Horman7729c7a2011-08-25 10:27:26 +0900627irqreturn_t tmio_mmc_sdcard_irq(int irq, void *devid)
628{
629 unsigned int ireg, status;
630 struct tmio_mmc_host *host = devid;
631
632 tmio_mmc_card_irq_status(host, &ireg, &status);
633 __tmio_mmc_sdcard_irq(host, ireg, status);
634
635 return IRQ_HANDLED;
636}
637EXPORT_SYMBOL(tmio_mmc_sdcard_irq);
638
639irqreturn_t tmio_mmc_sdio_irq(int irq, void *devid)
640{
641 struct tmio_mmc_host *host = devid;
642 struct mmc_host *mmc = host->mmc;
643 struct tmio_mmc_data *pdata = host->pdata;
644 unsigned int ireg, status;
645
646 if (!(pdata->flags & TMIO_MMC_SDIO_IRQ))
647 return IRQ_HANDLED;
648
649 status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
650 ireg = status & TMIO_SDIO_MASK_ALL & ~host->sdcard_irq_mask;
651
652 sd_ctrl_write16(host, CTL_SDIO_STATUS, status & ~TMIO_SDIO_MASK_ALL);
653
654 if (mmc->caps & MMC_CAP_SDIO_IRQ && ireg & TMIO_SDIO_STAT_IOIRQ)
655 mmc_signal_sdio_irq(mmc);
656
657 return IRQ_HANDLED;
658}
659EXPORT_SYMBOL(tmio_mmc_sdio_irq);
660
661irqreturn_t tmio_mmc_irq(int irq, void *devid)
662{
663 struct tmio_mmc_host *host = devid;
664 unsigned int ireg, status;
665
666 pr_debug("MMC IRQ begin\n");
667
668 tmio_mmc_card_irq_status(host, &ireg, &status);
669 if (__tmio_mmc_card_detect_irq(host, ireg, status))
670 return IRQ_HANDLED;
671 if (__tmio_mmc_sdcard_irq(host, ireg, status))
672 return IRQ_HANDLED;
673
674 tmio_mmc_sdio_irq(irq, devid);
675
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100676 return IRQ_HANDLED;
677}
Magnus Damm8e7bfdb2011-05-06 11:02:33 +0000678EXPORT_SYMBOL(tmio_mmc_irq);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100679
680static int tmio_mmc_start_data(struct tmio_mmc_host *host,
681 struct mmc_data *data)
682{
683 struct tmio_mmc_data *pdata = host->pdata;
684
685 pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
686 data->blksz, data->blocks);
687
688 /* Some hardware cannot perform 2 byte requests in 4 bit mode */
689 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
690 int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
691
692 if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
693 pr_err("%s: %d byte block unsupported in 4 bit mode\n",
694 mmc_hostname(host->mmc), data->blksz);
695 return -EINVAL;
696 }
697 }
698
699 tmio_mmc_init_sg(host, data);
700 host->data = data;
701
702 /* Set transfer length / blocksize */
703 sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
704 sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
705
706 tmio_mmc_start_dma(host, data);
707
708 return 0;
709}
710
711/* Process requests from the MMC layer */
712static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
713{
714 struct tmio_mmc_host *host = mmc_priv(mmc);
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000715 unsigned long flags;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100716 int ret;
717
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000718 spin_lock_irqsave(&host->lock, flags);
719
720 if (host->mrq) {
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100721 pr_debug("request not null\n");
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000722 if (IS_ERR(host->mrq)) {
723 spin_unlock_irqrestore(&host->lock, flags);
724 mrq->cmd->error = -EAGAIN;
725 mmc_request_done(mmc, mrq);
726 return;
727 }
728 }
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100729
730 host->last_req_ts = jiffies;
731 wmb();
732 host->mrq = mrq;
733
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000734 spin_unlock_irqrestore(&host->lock, flags);
735
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100736 if (mrq->data) {
737 ret = tmio_mmc_start_data(host, mrq->data);
738 if (ret)
739 goto fail;
740 }
741
742 ret = tmio_mmc_start_command(host, mrq->cmd);
743 if (!ret) {
744 schedule_delayed_work(&host->delayed_reset_work,
745 msecs_to_jiffies(2000));
746 return;
747 }
748
749fail:
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100750 host->force_pio = false;
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000751 host->mrq = NULL;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100752 mrq->cmd->error = ret;
753 mmc_request_done(mmc, mrq);
754}
755
756/* Set MMC clock / power.
757 * Note: This controller uses a simple divider scheme therefore it cannot
758 * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
759 * MMC wont run that fast, it has to be clocked at 12MHz which is the next
760 * slowest setting.
761 */
762static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
763{
764 struct tmio_mmc_host *host = mmc_priv(mmc);
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +0000765 struct tmio_mmc_data *pdata = host->pdata;
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000766 unsigned long flags;
767
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200768 mutex_lock(&host->ios_lock);
769
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000770 spin_lock_irqsave(&host->lock, flags);
771 if (host->mrq) {
772 if (IS_ERR(host->mrq)) {
773 dev_dbg(&host->pdev->dev,
774 "%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
775 current->comm, task_pid_nr(current),
776 ios->clock, ios->power_mode);
777 host->mrq = ERR_PTR(-EINTR);
778 } else {
779 dev_dbg(&host->pdev->dev,
780 "%s.%d: CMD%u active since %lu, now %lu!\n",
781 current->comm, task_pid_nr(current),
782 host->mrq->cmd->opcode, host->last_req_ts, jiffies);
783 }
784 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200785
786 mutex_unlock(&host->ios_lock);
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000787 return;
788 }
789
790 host->mrq = ERR_PTR(-EBUSY);
791
792 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100793
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200794 /*
Guennadi Liakhovetskic8be24c2012-02-09 22:57:09 +0100795 * pdata->power toggles between false and true in both cases - either
796 * or not the controller can be runtime-suspended during inactivity.
797 * But if the controller has to be kept on, the runtime-pm usage_count
798 * is kept positive, so no suspending actually takes place.
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200799 */
800 if (ios->power_mode == MMC_POWER_ON && ios->clock) {
801 if (!pdata->power) {
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +0000802 pm_runtime_get_sync(&host->pdev->dev);
803 pdata->power = true;
804 }
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200805 tmio_mmc_set_clock(host, ios->clock);
Guennadi Liakhovetskic919c2a2011-04-21 09:09:59 +0200806 /* power up SD bus */
807 if (host->set_pwr)
808 host->set_pwr(host->pdev, 1);
Guennadi Liakhovetski5fd01572011-03-09 14:45:44 +0100809 /* start bus clock */
810 tmio_mmc_clk_start(host);
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200811 } else if (ios->power_mode != MMC_POWER_UP) {
Guennadi Liakhovetskif6b8b522011-11-16 10:11:45 +0100812 if (host->set_pwr && ios->power_mode == MMC_POWER_OFF)
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200813 host->set_pwr(host->pdev, 0);
Bastian Hechtcbb18b32011-12-23 23:03:13 +0100814 if (pdata->power) {
Guennadi Liakhovetski71d111c2011-07-14 12:16:59 +0200815 pdata->power = false;
816 pm_runtime_put(&host->pdev->dev);
817 }
818 tmio_mmc_clk_stop(host);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100819 }
820
821 switch (ios->bus_width) {
822 case MMC_BUS_WIDTH_1:
823 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
824 break;
825 case MMC_BUS_WIDTH_4:
826 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
827 break;
828 }
829
830 /* Let things settle. delay taken from winCE driver */
831 udelay(140);
Guennadi Liakhovetskidf3ef2d2011-04-21 07:20:16 +0000832 if (PTR_ERR(host->mrq) == -EINTR)
833 dev_dbg(&host->pdev->dev,
834 "%s.%d: IOS interrupted: clk %u, mode %u",
835 current->comm, task_pid_nr(current),
836 ios->clock, ios->power_mode);
837 host->mrq = NULL;
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200838
839 mutex_unlock(&host->ios_lock);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100840}
841
842static int tmio_mmc_get_ro(struct mmc_host *mmc)
843{
844 struct tmio_mmc_host *host = mmc_priv(mmc);
845 struct tmio_mmc_data *pdata = host->pdata;
846
Guennadi Liakhovetski7d8b4c22011-06-20 16:51:10 +0200847 return !((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
848 (sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT));
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100849}
850
851static int tmio_mmc_get_cd(struct mmc_host *mmc)
852{
853 struct tmio_mmc_host *host = mmc_priv(mmc);
854 struct tmio_mmc_data *pdata = host->pdata;
855
856 if (!pdata->get_cd)
857 return -ENOSYS;
858 else
859 return pdata->get_cd(host->pdev);
860}
861
862static const struct mmc_host_ops tmio_mmc_ops = {
863 .request = tmio_mmc_request,
864 .set_ios = tmio_mmc_set_ios,
865 .get_ro = tmio_mmc_get_ro,
866 .get_cd = tmio_mmc_get_cd,
867 .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
868};
869
870int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host,
871 struct platform_device *pdev,
872 struct tmio_mmc_data *pdata)
873{
874 struct tmio_mmc_host *_host;
875 struct mmc_host *mmc;
876 struct resource *res_ctl;
877 int ret;
878 u32 irq_mask = TMIO_MASK_CMD;
879
880 res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0);
881 if (!res_ctl)
882 return -EINVAL;
883
884 mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
885 if (!mmc)
886 return -ENOMEM;
887
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +0000888 pdata->dev = &pdev->dev;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100889 _host = mmc_priv(mmc);
890 _host->pdata = pdata;
891 _host->mmc = mmc;
892 _host->pdev = pdev;
893 platform_set_drvdata(pdev, mmc);
894
895 _host->set_pwr = pdata->set_pwr;
896 _host->set_clk_div = pdata->set_clk_div;
897
898 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
899 _host->bus_shift = resource_size(res_ctl) >> 10;
900
901 _host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
902 if (!_host->ctl) {
903 ret = -ENOMEM;
904 goto host_free;
905 }
906
907 mmc->ops = &tmio_mmc_ops;
908 mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
909 mmc->f_max = pdata->hclk;
910 mmc->f_min = mmc->f_max / 512;
911 mmc->max_segs = 32;
912 mmc->max_blk_size = 512;
913 mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
914 mmc->max_segs;
915 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
916 mmc->max_seg_size = mmc->max_req_size;
917 if (pdata->ocr_mask)
918 mmc->ocr_avail = pdata->ocr_mask;
919 else
920 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
921
Guennadi Liakhovetskic8be24c2012-02-09 22:57:09 +0100922 _host->native_hotplug = !(pdata->flags & TMIO_MMC_USE_GPIO_CD ||
Guennadi Liakhovetski2b1ac5c2012-02-09 22:57:08 +0100923 mmc->caps & MMC_CAP_NEEDS_POLL ||
924 mmc->caps & MMC_CAP_NONREMOVABLE);
925
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +0000926 pdata->power = false;
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000927 pm_runtime_enable(&pdev->dev);
928 ret = pm_runtime_resume(&pdev->dev);
929 if (ret < 0)
930 goto pm_disable;
931
Bastian Hechtcbb18b32011-12-23 23:03:13 +0100932 /*
933 * There are 4 different scenarios for the card detection:
934 * 1) an external gpio irq handles the cd (best for power savings)
935 * 2) internal sdhi irq handles the cd
936 * 3) a worker thread polls the sdhi - indicated by MMC_CAP_NEEDS_POLL
937 * 4) the medium is non-removable - indicated by MMC_CAP_NONREMOVABLE
938 *
Guennadi Liakhovetskic8be24c2012-02-09 22:57:09 +0100939 * While we increment the runtime PM counter for all scenarios when
940 * the mmc core activates us by calling an appropriate set_ios(), we
941 * must additionally ensure that in case 2) the tmio mmc hardware stays
Bastian Hechtcbb18b32011-12-23 23:03:13 +0100942 * additionally ensure that in case 2) the tmio mmc hardware stays
943 * powered on during runtime for the card detection to work.
944 */
Guennadi Liakhovetski2b1ac5c2012-02-09 22:57:08 +0100945 if (_host->native_hotplug)
Bastian Hechtcbb18b32011-12-23 23:03:13 +0100946 pm_runtime_get_noresume(&pdev->dev);
947
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100948 tmio_mmc_clk_stop(_host);
949 tmio_mmc_reset(_host);
950
Simon Horman54680fe2011-08-25 10:27:25 +0900951 _host->sdcard_irq_mask = sd_ctrl_read32(_host, CTL_IRQ_MASK);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100952 tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
953 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
954 tmio_mmc_enable_sdio_irq(mmc, 0);
955
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100956 spin_lock_init(&_host->lock);
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200957 mutex_init(&_host->ios_lock);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100958
959 /* Init delayed work for request timeouts */
960 INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +0200961 INIT_WORK(&_host->done, tmio_mmc_done_work);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100962
963 /* See if we also get DMA */
964 tmio_mmc_request_dma(_host, pdata);
965
966 mmc_add_host(mmc);
967
Rafael J. Wysockic419e612012-03-13 01:01:51 +0100968 dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
969
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100970 /* Unmask the IRQs we want to know about */
971 if (!_host->chan_rx)
972 irq_mask |= TMIO_MASK_READOP;
973 if (!_host->chan_tx)
974 irq_mask |= TMIO_MASK_WRITEOP;
Guennadi Liakhovetskie23cd532012-02-22 13:16:09 +0100975 if (!_host->native_hotplug)
976 irq_mask &= ~(TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100977
978 tmio_mmc_enable_mmc_irqs(_host, irq_mask);
979
Guennadi Liakhovetskic8be24c2012-02-09 22:57:09 +0100980 if (pdata->flags & TMIO_MMC_USE_GPIO_CD) {
981 ret = mmc_cd_gpio_request(mmc, pdata->cd_gpio);
982 if (ret < 0) {
983 tmio_mmc_host_remove(_host);
984 return ret;
985 }
986 }
987
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100988 *host = _host;
989
990 return 0;
991
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000992pm_disable:
993 pm_runtime_disable(&pdev->dev);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100994 iounmap(_host->ctl);
995host_free:
996 mmc_free_host(mmc);
997
998 return ret;
999}
1000EXPORT_SYMBOL(tmio_mmc_host_probe);
1001
1002void tmio_mmc_host_remove(struct tmio_mmc_host *host)
1003{
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001004 struct platform_device *pdev = host->pdev;
Guennadi Liakhovetskic8be24c2012-02-09 22:57:09 +01001005 struct tmio_mmc_data *pdata = host->pdata;
1006 struct mmc_host *mmc = host->mmc;
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001007
Guennadi Liakhovetskic8be24c2012-02-09 22:57:09 +01001008 if (pdata->flags & TMIO_MMC_USE_GPIO_CD)
1009 /*
1010 * This means we can miss a card-eject, but this is anyway
1011 * possible, because of delayed processing of hotplug events.
1012 */
1013 mmc_cd_gpio_free(mmc);
1014
Guennadi Liakhovetski2b1ac5c2012-02-09 22:57:08 +01001015 if (!host->native_hotplug)
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001016 pm_runtime_get_sync(&pdev->dev);
1017
Rafael J. Wysockic419e612012-03-13 01:01:51 +01001018 dev_pm_qos_hide_latency_limit(&pdev->dev);
1019
Guennadi Liakhovetskic8be24c2012-02-09 22:57:09 +01001020 mmc_remove_host(mmc);
Guennadi Liakhovetskib9269fd2011-07-14 12:12:38 +02001021 cancel_work_sync(&host->done);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +01001022 cancel_delayed_work_sync(&host->delayed_reset_work);
1023 tmio_mmc_release_dma(host);
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001024
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001025 pm_runtime_put_sync(&pdev->dev);
1026 pm_runtime_disable(&pdev->dev);
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001027
1028 iounmap(host->ctl);
Guennadi Liakhovetskic8be24c2012-02-09 22:57:09 +01001029 mmc_free_host(mmc);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +01001030}
1031EXPORT_SYMBOL(tmio_mmc_host_remove);
1032
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001033#ifdef CONFIG_PM
1034int tmio_mmc_host_suspend(struct device *dev)
1035{
1036 struct mmc_host *mmc = dev_get_drvdata(dev);
1037 struct tmio_mmc_host *host = mmc_priv(mmc);
1038 int ret = mmc_suspend_host(mmc);
1039
1040 if (!ret)
1041 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
1042
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001043 return ret;
1044}
1045EXPORT_SYMBOL(tmio_mmc_host_suspend);
1046
1047int tmio_mmc_host_resume(struct device *dev)
1048{
1049 struct mmc_host *mmc = dev_get_drvdata(dev);
1050 struct tmio_mmc_host *host = mmc_priv(mmc);
1051
Guennadi Liakhovetskic8be24c2012-02-09 22:57:09 +01001052 tmio_mmc_reset(host);
1053 tmio_mmc_enable_dma(host, true);
1054
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001055 /* The MMC core will perform the complete set up */
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +00001056 return mmc_resume_host(mmc);
1057}
1058EXPORT_SYMBOL(tmio_mmc_host_resume);
1059
1060#endif /* CONFIG_PM */
1061
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001062int tmio_mmc_host_runtime_suspend(struct device *dev)
1063{
1064 return 0;
1065}
1066EXPORT_SYMBOL(tmio_mmc_host_runtime_suspend);
1067
1068int tmio_mmc_host_runtime_resume(struct device *dev)
1069{
1070 struct mmc_host *mmc = dev_get_drvdata(dev);
1071 struct tmio_mmc_host *host = mmc_priv(mmc);
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001072
1073 tmio_mmc_reset(host);
Guennadi Liakhovetski162f43e2011-07-14 18:39:10 +02001074 tmio_mmc_enable_dma(host, true);
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001075
Guennadi Liakhovetski7311bef2011-05-11 16:51:11 +00001076 return 0;
1077}
1078EXPORT_SYMBOL(tmio_mmc_host_runtime_resume);
1079
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +01001080MODULE_LICENSE("GPL v2");