Documentation: configfs examples crash fix
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mmc / host / tmio_mmc_pio.c
blobad6347bb02ddd9ee20a6085185fb6ca03924d666
1 /*
2 * linux/drivers/mmc/host/tmio_mmc_pio.c
4 * Copyright (C) 2011 Guennadi Liakhovetski
5 * Copyright (C) 2007 Ian Molton
6 * Copyright (C) 2004 Ian Molton
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.
12 * Driver for the MMC / SD / SDIO IP found in:
14 * TC6393XB, TC6391XB, TC6387XB, T7L66XB, ASIC3, SH-Mobile SoCs
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).
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
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>
38 #include <linux/mmc/tmio.h>
39 #include <linux/module.h>
40 #include <linux/pagemap.h>
41 #include <linux/platform_device.h>
42 #include <linux/pm_runtime.h>
43 #include <linux/scatterlist.h>
44 #include <linux/workqueue.h>
45 #include <linux/spinlock.h>
47 #include "tmio_mmc.h"
49 static u16 sd_ctrl_read16(struct tmio_mmc_host *host, int addr)
51 return readw(host->ctl + (addr << host->bus_shift));
54 static void sd_ctrl_read16_rep(struct tmio_mmc_host *host, int addr,
55 u16 *buf, int count)
57 readsw(host->ctl + (addr << host->bus_shift), buf, count);
60 static u32 sd_ctrl_read32(struct tmio_mmc_host *host, int addr)
62 return readw(host->ctl + (addr << host->bus_shift)) |
63 readw(host->ctl + ((addr + 2) << host->bus_shift)) << 16;
66 static void sd_ctrl_write16(struct tmio_mmc_host *host, int addr, u16 val)
68 writew(val, host->ctl + (addr << host->bus_shift));
71 static void sd_ctrl_write16_rep(struct tmio_mmc_host *host, int addr,
72 u16 *buf, int count)
74 writesw(host->ctl + (addr << host->bus_shift), buf, count);
77 static void sd_ctrl_write32(struct tmio_mmc_host *host, int addr, u32 val)
79 writew(val, host->ctl + (addr << host->bus_shift));
80 writew(val >> 16, host->ctl + ((addr + 2) << host->bus_shift));
83 void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
85 u32 mask = sd_ctrl_read32(host, CTL_IRQ_MASK) & ~(i & TMIO_MASK_IRQ);
86 sd_ctrl_write32(host, CTL_IRQ_MASK, mask);
89 void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
91 u32 mask = sd_ctrl_read32(host, CTL_IRQ_MASK) | (i & TMIO_MASK_IRQ);
92 sd_ctrl_write32(host, CTL_IRQ_MASK, mask);
95 static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i)
97 sd_ctrl_write32(host, CTL_STATUS, ~i);
100 static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
102 host->sg_len = data->sg_len;
103 host->sg_ptr = data->sg;
104 host->sg_orig = data->sg;
105 host->sg_off = 0;
108 static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
110 host->sg_ptr = sg_next(host->sg_ptr);
111 host->sg_off = 0;
112 return --host->sg_len;
115 #ifdef CONFIG_MMC_DEBUG
117 #define STATUS_TO_TEXT(a, status, i) \
118 do { \
119 if (status & TMIO_STAT_##a) { \
120 if (i++) \
121 printk(" | "); \
122 printk(#a); \
124 } while (0)
126 static void pr_debug_status(u32 status)
128 int i = 0;
129 printk(KERN_DEBUG "status: %08x = ", status);
130 STATUS_TO_TEXT(CARD_REMOVE, status, i);
131 STATUS_TO_TEXT(CARD_INSERT, status, i);
132 STATUS_TO_TEXT(SIGSTATE, status, i);
133 STATUS_TO_TEXT(WRPROTECT, status, i);
134 STATUS_TO_TEXT(CARD_REMOVE_A, status, i);
135 STATUS_TO_TEXT(CARD_INSERT_A, status, i);
136 STATUS_TO_TEXT(SIGSTATE_A, status, i);
137 STATUS_TO_TEXT(CMD_IDX_ERR, status, i);
138 STATUS_TO_TEXT(STOPBIT_ERR, status, i);
139 STATUS_TO_TEXT(ILL_FUNC, status, i);
140 STATUS_TO_TEXT(CMD_BUSY, status, i);
141 STATUS_TO_TEXT(CMDRESPEND, status, i);
142 STATUS_TO_TEXT(DATAEND, status, i);
143 STATUS_TO_TEXT(CRCFAIL, status, i);
144 STATUS_TO_TEXT(DATATIMEOUT, status, i);
145 STATUS_TO_TEXT(CMDTIMEOUT, status, i);
146 STATUS_TO_TEXT(RXOVERFLOW, status, i);
147 STATUS_TO_TEXT(TXUNDERRUN, status, i);
148 STATUS_TO_TEXT(RXRDY, status, i);
149 STATUS_TO_TEXT(TXRQ, status, i);
150 STATUS_TO_TEXT(ILL_ACCESS, status, i);
151 printk("\n");
154 #else
155 #define pr_debug_status(s) do { } while (0)
156 #endif
158 static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
160 struct tmio_mmc_host *host = mmc_priv(mmc);
162 if (enable) {
163 host->sdio_irq_enabled = 1;
164 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
165 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK,
166 (TMIO_SDIO_MASK_ALL & ~TMIO_SDIO_STAT_IOIRQ));
167 } else {
168 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, TMIO_SDIO_MASK_ALL);
169 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
170 host->sdio_irq_enabled = 0;
174 static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
176 u32 clk = 0, clock;
178 if (new_clock) {
179 for (clock = host->mmc->f_min, clk = 0x80000080;
180 new_clock >= (clock<<1); clk >>= 1)
181 clock <<= 1;
182 clk |= 0x100;
185 if (host->set_clk_div)
186 host->set_clk_div(host->pdev, (clk>>22) & 1);
188 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
191 static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
193 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
195 /* implicit BUG_ON(!res) */
196 if (resource_size(res) > 0x100) {
197 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
198 msleep(10);
201 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
202 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
203 msleep(10);
206 static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
208 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
210 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
211 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
212 msleep(10);
214 /* implicit BUG_ON(!res) */
215 if (resource_size(res) > 0x100) {
216 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
217 msleep(10);
221 static void tmio_mmc_reset(struct tmio_mmc_host *host)
223 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
225 /* FIXME - should we set stop clock reg here */
226 sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
227 /* implicit BUG_ON(!res) */
228 if (resource_size(res) > 0x100)
229 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
230 msleep(10);
231 sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
232 if (resource_size(res) > 0x100)
233 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
234 msleep(10);
237 static void tmio_mmc_reset_work(struct work_struct *work)
239 struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
240 delayed_reset_work.work);
241 struct mmc_request *mrq;
242 unsigned long flags;
244 spin_lock_irqsave(&host->lock, flags);
245 mrq = host->mrq;
248 * is request already finished? Since we use a non-blocking
249 * cancel_delayed_work(), it can happen, that a .set_ios() call preempts
250 * us, so, have to check for IS_ERR(host->mrq)
252 if (IS_ERR_OR_NULL(mrq)
253 || time_is_after_jiffies(host->last_req_ts +
254 msecs_to_jiffies(2000))) {
255 spin_unlock_irqrestore(&host->lock, flags);
256 return;
259 dev_warn(&host->pdev->dev,
260 "timeout waiting for hardware interrupt (CMD%u)\n",
261 mrq->cmd->opcode);
263 if (host->data)
264 host->data->error = -ETIMEDOUT;
265 else if (host->cmd)
266 host->cmd->error = -ETIMEDOUT;
267 else
268 mrq->cmd->error = -ETIMEDOUT;
270 host->cmd = NULL;
271 host->data = NULL;
272 host->force_pio = false;
274 spin_unlock_irqrestore(&host->lock, flags);
276 tmio_mmc_reset(host);
278 /* Ready for new calls */
279 host->mrq = NULL;
281 mmc_request_done(host->mmc, mrq);
284 /* called with host->lock held, interrupts disabled */
285 static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
287 struct mmc_request *mrq = host->mrq;
289 if (!mrq)
290 return;
292 host->cmd = NULL;
293 host->data = NULL;
294 host->force_pio = false;
296 cancel_delayed_work(&host->delayed_reset_work);
298 host->mrq = NULL;
300 /* FIXME: mmc_request_done() can schedule! */
301 mmc_request_done(host->mmc, mrq);
304 /* These are the bitmasks the tmio chip requires to implement the MMC response
305 * types. Note that R1 and R6 are the same in this scheme. */
306 #define APP_CMD 0x0040
307 #define RESP_NONE 0x0300
308 #define RESP_R1 0x0400
309 #define RESP_R1B 0x0500
310 #define RESP_R2 0x0600
311 #define RESP_R3 0x0700
312 #define DATA_PRESENT 0x0800
313 #define TRANSFER_READ 0x1000
314 #define TRANSFER_MULTI 0x2000
315 #define SECURITY_CMD 0x4000
317 static int tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
319 struct mmc_data *data = host->data;
320 int c = cmd->opcode;
322 /* Command 12 is handled by hardware */
323 if (cmd->opcode == 12 && !cmd->arg) {
324 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
325 return 0;
328 switch (mmc_resp_type(cmd)) {
329 case MMC_RSP_NONE: c |= RESP_NONE; break;
330 case MMC_RSP_R1: c |= RESP_R1; break;
331 case MMC_RSP_R1B: c |= RESP_R1B; break;
332 case MMC_RSP_R2: c |= RESP_R2; break;
333 case MMC_RSP_R3: c |= RESP_R3; break;
334 default:
335 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
336 return -EINVAL;
339 host->cmd = cmd;
341 /* FIXME - this seems to be ok commented out but the spec suggest this bit
342 * should be set when issuing app commands.
343 * if(cmd->flags & MMC_FLAG_ACMD)
344 * c |= APP_CMD;
346 if (data) {
347 c |= DATA_PRESENT;
348 if (data->blocks > 1) {
349 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
350 c |= TRANSFER_MULTI;
352 if (data->flags & MMC_DATA_READ)
353 c |= TRANSFER_READ;
356 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_CMD);
358 /* Fire off the command */
359 sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
360 sd_ctrl_write16(host, CTL_SD_CMD, c);
362 return 0;
366 * This chip always returns (at least?) as much data as you ask for.
367 * I'm unsure what happens if you ask for less than a block. This should be
368 * looked into to ensure that a funny length read doesn't hose the controller.
370 static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
372 struct mmc_data *data = host->data;
373 void *sg_virt;
374 unsigned short *buf;
375 unsigned int count;
376 unsigned long flags;
378 if ((host->chan_tx || host->chan_rx) && !host->force_pio) {
379 pr_err("PIO IRQ in DMA mode!\n");
380 return;
381 } else if (!data) {
382 pr_debug("Spurious PIO IRQ\n");
383 return;
386 sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
387 buf = (unsigned short *)(sg_virt + host->sg_off);
389 count = host->sg_ptr->length - host->sg_off;
390 if (count > data->blksz)
391 count = data->blksz;
393 pr_debug("count: %08x offset: %08x flags %08x\n",
394 count, host->sg_off, data->flags);
396 /* Transfer the data */
397 if (data->flags & MMC_DATA_READ)
398 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
399 else
400 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
402 host->sg_off += count;
404 tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
406 if (host->sg_off == host->sg_ptr->length)
407 tmio_mmc_next_sg(host);
409 return;
412 static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
414 if (host->sg_ptr == &host->bounce_sg) {
415 unsigned long flags;
416 void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
417 memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
418 tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
422 /* needs to be called with host->lock held */
423 void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
425 struct mmc_data *data = host->data;
426 struct mmc_command *stop;
428 host->data = NULL;
430 if (!data) {
431 dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
432 return;
434 stop = data->stop;
436 /* FIXME - return correct transfer count on errors */
437 if (!data->error)
438 data->bytes_xfered = data->blocks * data->blksz;
439 else
440 data->bytes_xfered = 0;
442 pr_debug("Completed data request\n");
445 * FIXME: other drivers allow an optional stop command of any given type
446 * which we dont do, as the chip can auto generate them.
447 * Perhaps we can be smarter about when to use auto CMD12 and
448 * only issue the auto request when we know this is the desired
449 * stop command, allowing fallback to the stop command the
450 * upper layers expect. For now, we do what works.
453 if (data->flags & MMC_DATA_READ) {
454 if (host->chan_rx && !host->force_pio)
455 tmio_mmc_check_bounce_buffer(host);
456 dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
457 host->mrq);
458 } else {
459 dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
460 host->mrq);
463 if (stop) {
464 if (stop->opcode == 12 && !stop->arg)
465 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
466 else
467 BUG();
470 tmio_mmc_finish_request(host);
473 static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
475 struct mmc_data *data;
476 spin_lock(&host->lock);
477 data = host->data;
479 if (!data)
480 goto out;
482 if (host->chan_tx && (data->flags & MMC_DATA_WRITE) && !host->force_pio) {
484 * Has all data been written out yet? Testing on SuperH showed,
485 * that in most cases the first interrupt comes already with the
486 * BUSY status bit clear, but on some operations, like mount or
487 * in the beginning of a write / sync / umount, there is one
488 * DATAEND interrupt with the BUSY bit set, in this cases
489 * waiting for one more interrupt fixes the problem.
491 if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
492 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
493 tasklet_schedule(&host->dma_complete);
495 } else if (host->chan_rx && (data->flags & MMC_DATA_READ) && !host->force_pio) {
496 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
497 tasklet_schedule(&host->dma_complete);
498 } else {
499 tmio_mmc_do_data_irq(host);
500 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
502 out:
503 spin_unlock(&host->lock);
506 static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
507 unsigned int stat)
509 struct mmc_command *cmd = host->cmd;
510 int i, addr;
512 spin_lock(&host->lock);
514 if (!host->cmd) {
515 pr_debug("Spurious CMD irq\n");
516 goto out;
519 host->cmd = NULL;
521 /* This controller is sicker than the PXA one. Not only do we need to
522 * drop the top 8 bits of the first response word, we also need to
523 * modify the order of the response for short response command types.
526 for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
527 cmd->resp[i] = sd_ctrl_read32(host, addr);
529 if (cmd->flags & MMC_RSP_136) {
530 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
531 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
532 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
533 cmd->resp[3] <<= 8;
534 } else if (cmd->flags & MMC_RSP_R3) {
535 cmd->resp[0] = cmd->resp[3];
538 if (stat & TMIO_STAT_CMDTIMEOUT)
539 cmd->error = -ETIMEDOUT;
540 else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
541 cmd->error = -EILSEQ;
543 /* If there is data to handle we enable data IRQs here, and
544 * we will ultimatley finish the request in the data_end handler.
545 * If theres no data or we encountered an error, finish now.
547 if (host->data && !cmd->error) {
548 if (host->data->flags & MMC_DATA_READ) {
549 if (host->force_pio || !host->chan_rx)
550 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
551 else
552 tasklet_schedule(&host->dma_issue);
553 } else {
554 if (host->force_pio || !host->chan_tx)
555 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
556 else
557 tasklet_schedule(&host->dma_issue);
559 } else {
560 tmio_mmc_finish_request(host);
563 out:
564 spin_unlock(&host->lock);
567 irqreturn_t tmio_mmc_irq(int irq, void *devid)
569 struct tmio_mmc_host *host = devid;
570 struct tmio_mmc_data *pdata = host->pdata;
571 unsigned int ireg, irq_mask, status;
572 unsigned int sdio_ireg, sdio_irq_mask, sdio_status;
574 pr_debug("MMC IRQ begin\n");
576 status = sd_ctrl_read32(host, CTL_STATUS);
577 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
578 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
580 sdio_ireg = 0;
581 if (!ireg && pdata->flags & TMIO_MMC_SDIO_IRQ) {
582 sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
583 sdio_irq_mask = sd_ctrl_read16(host, CTL_SDIO_IRQ_MASK);
584 sdio_ireg = sdio_status & TMIO_SDIO_MASK_ALL & ~sdio_irq_mask;
586 sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status & ~TMIO_SDIO_MASK_ALL);
588 if (sdio_ireg && !host->sdio_irq_enabled) {
589 pr_warning("tmio_mmc: Spurious SDIO IRQ, disabling! 0x%04x 0x%04x 0x%04x\n",
590 sdio_status, sdio_irq_mask, sdio_ireg);
591 tmio_mmc_enable_sdio_irq(host->mmc, 0);
592 goto out;
595 if (host->mmc->caps & MMC_CAP_SDIO_IRQ &&
596 sdio_ireg & TMIO_SDIO_STAT_IOIRQ)
597 mmc_signal_sdio_irq(host->mmc);
599 if (sdio_ireg)
600 goto out;
603 pr_debug_status(status);
604 pr_debug_status(ireg);
606 if (!ireg) {
607 tmio_mmc_disable_mmc_irqs(host, status & ~irq_mask);
609 pr_warning("tmio_mmc: Spurious irq, disabling! "
610 "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
611 pr_debug_status(status);
613 goto out;
616 while (ireg) {
617 /* Card insert / remove attempts */
618 if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
619 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
620 TMIO_STAT_CARD_REMOVE);
621 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
624 /* CRC and other errors */
625 /* if (ireg & TMIO_STAT_ERR_IRQ)
626 * handled |= tmio_error_irq(host, irq, stat);
629 /* Command completion */
630 if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
631 tmio_mmc_ack_mmc_irqs(host,
632 TMIO_STAT_CMDRESPEND |
633 TMIO_STAT_CMDTIMEOUT);
634 tmio_mmc_cmd_irq(host, status);
637 /* Data transfer */
638 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
639 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
640 tmio_mmc_pio_irq(host);
643 /* Data transfer completion */
644 if (ireg & TMIO_STAT_DATAEND) {
645 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
646 tmio_mmc_data_irq(host);
649 /* Check status - keep going until we've handled it all */
650 status = sd_ctrl_read32(host, CTL_STATUS);
651 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
652 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
654 pr_debug("Status at end of loop: %08x\n", status);
655 pr_debug_status(status);
657 pr_debug("MMC IRQ end\n");
659 out:
660 return IRQ_HANDLED;
662 EXPORT_SYMBOL(tmio_mmc_irq);
664 static int tmio_mmc_start_data(struct tmio_mmc_host *host,
665 struct mmc_data *data)
667 struct tmio_mmc_data *pdata = host->pdata;
669 pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
670 data->blksz, data->blocks);
672 /* Some hardware cannot perform 2 byte requests in 4 bit mode */
673 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
674 int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
676 if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
677 pr_err("%s: %d byte block unsupported in 4 bit mode\n",
678 mmc_hostname(host->mmc), data->blksz);
679 return -EINVAL;
683 tmio_mmc_init_sg(host, data);
684 host->data = data;
686 /* Set transfer length / blocksize */
687 sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
688 sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
690 tmio_mmc_start_dma(host, data);
692 return 0;
695 /* Process requests from the MMC layer */
696 static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
698 struct tmio_mmc_host *host = mmc_priv(mmc);
699 unsigned long flags;
700 int ret;
702 spin_lock_irqsave(&host->lock, flags);
704 if (host->mrq) {
705 pr_debug("request not null\n");
706 if (IS_ERR(host->mrq)) {
707 spin_unlock_irqrestore(&host->lock, flags);
708 mrq->cmd->error = -EAGAIN;
709 mmc_request_done(mmc, mrq);
710 return;
714 host->last_req_ts = jiffies;
715 wmb();
716 host->mrq = mrq;
718 spin_unlock_irqrestore(&host->lock, flags);
720 if (mrq->data) {
721 ret = tmio_mmc_start_data(host, mrq->data);
722 if (ret)
723 goto fail;
726 ret = tmio_mmc_start_command(host, mrq->cmd);
727 if (!ret) {
728 schedule_delayed_work(&host->delayed_reset_work,
729 msecs_to_jiffies(2000));
730 return;
733 fail:
734 host->force_pio = false;
735 host->mrq = NULL;
736 mrq->cmd->error = ret;
737 mmc_request_done(mmc, mrq);
740 /* Set MMC clock / power.
741 * Note: This controller uses a simple divider scheme therefore it cannot
742 * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
743 * MMC wont run that fast, it has to be clocked at 12MHz which is the next
744 * slowest setting.
746 static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
748 struct tmio_mmc_host *host = mmc_priv(mmc);
749 struct tmio_mmc_data *pdata = host->pdata;
750 unsigned long flags;
752 spin_lock_irqsave(&host->lock, flags);
753 if (host->mrq) {
754 if (IS_ERR(host->mrq)) {
755 dev_dbg(&host->pdev->dev,
756 "%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
757 current->comm, task_pid_nr(current),
758 ios->clock, ios->power_mode);
759 host->mrq = ERR_PTR(-EINTR);
760 } else {
761 dev_dbg(&host->pdev->dev,
762 "%s.%d: CMD%u active since %lu, now %lu!\n",
763 current->comm, task_pid_nr(current),
764 host->mrq->cmd->opcode, host->last_req_ts, jiffies);
766 spin_unlock_irqrestore(&host->lock, flags);
767 return;
770 host->mrq = ERR_PTR(-EBUSY);
772 spin_unlock_irqrestore(&host->lock, flags);
774 if (ios->clock)
775 tmio_mmc_set_clock(host, ios->clock);
777 /* Power sequence - OFF -> UP -> ON */
778 if (ios->power_mode == MMC_POWER_UP) {
779 if ((pdata->flags & TMIO_MMC_HAS_COLD_CD) && !pdata->power) {
780 pm_runtime_get_sync(&host->pdev->dev);
781 pdata->power = true;
783 /* power up SD bus */
784 if (host->set_pwr)
785 host->set_pwr(host->pdev, 1);
786 } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
787 /* power down SD bus */
788 if (ios->power_mode == MMC_POWER_OFF) {
789 if (host->set_pwr)
790 host->set_pwr(host->pdev, 0);
791 if ((pdata->flags & TMIO_MMC_HAS_COLD_CD) &&
792 pdata->power) {
793 pdata->power = false;
794 pm_runtime_put(&host->pdev->dev);
797 tmio_mmc_clk_stop(host);
798 } else {
799 /* start bus clock */
800 tmio_mmc_clk_start(host);
803 switch (ios->bus_width) {
804 case MMC_BUS_WIDTH_1:
805 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
806 break;
807 case MMC_BUS_WIDTH_4:
808 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
809 break;
812 /* Let things settle. delay taken from winCE driver */
813 udelay(140);
814 if (PTR_ERR(host->mrq) == -EINTR)
815 dev_dbg(&host->pdev->dev,
816 "%s.%d: IOS interrupted: clk %u, mode %u",
817 current->comm, task_pid_nr(current),
818 ios->clock, ios->power_mode);
819 host->mrq = NULL;
822 static int tmio_mmc_get_ro(struct mmc_host *mmc)
824 struct tmio_mmc_host *host = mmc_priv(mmc);
825 struct tmio_mmc_data *pdata = host->pdata;
827 return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
828 !(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT));
831 static int tmio_mmc_get_cd(struct mmc_host *mmc)
833 struct tmio_mmc_host *host = mmc_priv(mmc);
834 struct tmio_mmc_data *pdata = host->pdata;
836 if (!pdata->get_cd)
837 return -ENOSYS;
838 else
839 return pdata->get_cd(host->pdev);
842 static const struct mmc_host_ops tmio_mmc_ops = {
843 .request = tmio_mmc_request,
844 .set_ios = tmio_mmc_set_ios,
845 .get_ro = tmio_mmc_get_ro,
846 .get_cd = tmio_mmc_get_cd,
847 .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
850 int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host,
851 struct platform_device *pdev,
852 struct tmio_mmc_data *pdata)
854 struct tmio_mmc_host *_host;
855 struct mmc_host *mmc;
856 struct resource *res_ctl;
857 int ret;
858 u32 irq_mask = TMIO_MASK_CMD;
860 res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0);
861 if (!res_ctl)
862 return -EINVAL;
864 mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
865 if (!mmc)
866 return -ENOMEM;
868 pdata->dev = &pdev->dev;
869 _host = mmc_priv(mmc);
870 _host->pdata = pdata;
871 _host->mmc = mmc;
872 _host->pdev = pdev;
873 platform_set_drvdata(pdev, mmc);
875 _host->set_pwr = pdata->set_pwr;
876 _host->set_clk_div = pdata->set_clk_div;
878 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
879 _host->bus_shift = resource_size(res_ctl) >> 10;
881 _host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
882 if (!_host->ctl) {
883 ret = -ENOMEM;
884 goto host_free;
887 mmc->ops = &tmio_mmc_ops;
888 mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
889 mmc->f_max = pdata->hclk;
890 mmc->f_min = mmc->f_max / 512;
891 mmc->max_segs = 32;
892 mmc->max_blk_size = 512;
893 mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
894 mmc->max_segs;
895 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
896 mmc->max_seg_size = mmc->max_req_size;
897 if (pdata->ocr_mask)
898 mmc->ocr_avail = pdata->ocr_mask;
899 else
900 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
902 pdata->power = false;
903 pm_runtime_enable(&pdev->dev);
904 ret = pm_runtime_resume(&pdev->dev);
905 if (ret < 0)
906 goto pm_disable;
908 tmio_mmc_clk_stop(_host);
909 tmio_mmc_reset(_host);
911 tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
912 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
913 tmio_mmc_enable_sdio_irq(mmc, 0);
915 spin_lock_init(&_host->lock);
917 /* Init delayed work for request timeouts */
918 INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
920 /* See if we also get DMA */
921 tmio_mmc_request_dma(_host, pdata);
923 /* We have to keep the device powered for its card detection to work */
924 if (!(pdata->flags & TMIO_MMC_HAS_COLD_CD))
925 pm_runtime_get_noresume(&pdev->dev);
927 mmc_add_host(mmc);
929 /* Unmask the IRQs we want to know about */
930 if (!_host->chan_rx)
931 irq_mask |= TMIO_MASK_READOP;
932 if (!_host->chan_tx)
933 irq_mask |= TMIO_MASK_WRITEOP;
935 tmio_mmc_enable_mmc_irqs(_host, irq_mask);
937 *host = _host;
939 return 0;
941 pm_disable:
942 pm_runtime_disable(&pdev->dev);
943 iounmap(_host->ctl);
944 host_free:
945 mmc_free_host(mmc);
947 return ret;
949 EXPORT_SYMBOL(tmio_mmc_host_probe);
951 void tmio_mmc_host_remove(struct tmio_mmc_host *host)
953 struct platform_device *pdev = host->pdev;
956 * We don't have to manipulate pdata->power here: if there is a card in
957 * the slot, the runtime PM is active and our .runtime_resume() will not
958 * be run. If there is no card in the slot and the platform can suspend
959 * the controller, the runtime PM is suspended and pdata->power == false,
960 * so, our .runtime_resume() will not try to detect a card in the slot.
962 if (host->pdata->flags & TMIO_MMC_HAS_COLD_CD)
963 pm_runtime_get_sync(&pdev->dev);
965 mmc_remove_host(host->mmc);
966 cancel_delayed_work_sync(&host->delayed_reset_work);
967 tmio_mmc_release_dma(host);
969 pm_runtime_put_sync(&pdev->dev);
970 pm_runtime_disable(&pdev->dev);
972 iounmap(host->ctl);
973 mmc_free_host(host->mmc);
975 EXPORT_SYMBOL(tmio_mmc_host_remove);
977 #ifdef CONFIG_PM
978 int tmio_mmc_host_suspend(struct device *dev)
980 struct mmc_host *mmc = dev_get_drvdata(dev);
981 struct tmio_mmc_host *host = mmc_priv(mmc);
982 int ret = mmc_suspend_host(mmc);
984 if (!ret)
985 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
987 host->pm_error = pm_runtime_put_sync(dev);
989 return ret;
991 EXPORT_SYMBOL(tmio_mmc_host_suspend);
993 int tmio_mmc_host_resume(struct device *dev)
995 struct mmc_host *mmc = dev_get_drvdata(dev);
996 struct tmio_mmc_host *host = mmc_priv(mmc);
998 /* The MMC core will perform the complete set up */
999 host->pdata->power = false;
1001 if (!host->pm_error)
1002 pm_runtime_get_sync(dev);
1004 tmio_mmc_reset(mmc_priv(mmc));
1005 tmio_mmc_request_dma(host, host->pdata);
1007 return mmc_resume_host(mmc);
1009 EXPORT_SYMBOL(tmio_mmc_host_resume);
1011 #endif /* CONFIG_PM */
1013 int tmio_mmc_host_runtime_suspend(struct device *dev)
1015 return 0;
1017 EXPORT_SYMBOL(tmio_mmc_host_runtime_suspend);
1019 int tmio_mmc_host_runtime_resume(struct device *dev)
1021 struct mmc_host *mmc = dev_get_drvdata(dev);
1022 struct tmio_mmc_host *host = mmc_priv(mmc);
1023 struct tmio_mmc_data *pdata = host->pdata;
1025 tmio_mmc_reset(host);
1027 if (pdata->power) {
1028 /* Only entered after a card-insert interrupt */
1029 tmio_mmc_set_ios(mmc, &mmc->ios);
1030 mmc_detect_change(mmc, msecs_to_jiffies(100));
1033 return 0;
1035 EXPORT_SYMBOL(tmio_mmc_host_runtime_resume);
1037 MODULE_LICENSE("GPL v2");