On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / mmc / host / tmio_mmc.c
blobf233867f54bb5a8805ef7f3d06f3791e275a50d2
1 /*
2 * linux/drivers/mmc/tmio_mmc.c
4 * Copyright (C) 2004 Ian Molton
5 * Copyright (C) 2007 Ian Molton
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Driver for the MMC / SD / SDIO cell found in:
13 * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
15 * This driver draws mainly on scattered spec sheets, Reverse engineering
16 * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit
17 * support). (Further 4 bit support from a later datasheet).
19 * TODO:
20 * Investigate using a workqueue for PIO transfers
21 * Eliminate FIXMEs
22 * SDIO support
23 * Better Power management
24 * Handle MMC errors better
25 * double buffer support
28 #include <linux/module.h>
29 #include <linux/irq.h>
30 #include <linux/device.h>
31 #include <linux/delay.h>
32 #include <linux/mmc/host.h>
33 #include <linux/mfd/core.h>
34 #include <linux/mfd/tmio.h>
36 #include "tmio_mmc.h"
38 static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
40 u32 clk = 0, clock;
42 if (new_clock) {
43 for (clock = host->mmc->f_min, clk = 0x80000080;
44 new_clock >= (clock<<1); clk >>= 1)
45 clock <<= 1;
47 /* Round the clock to the closest available. This is required
48 * for some fussy cards that dont like to initialise below 400kHz
50 if (new_clock - clock >= (clock << 1) - new_clock) {
51 clk >>= 1; clock <<= 1;
54 /* Clock enable */
55 clk |= 0x100;
58 if (host->set_no_clk_div)
59 host->set_no_clk_div(host->pdev, (clk>>22) & 1);
61 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
64 static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
66 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
67 msleep(10);
68 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
69 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
70 msleep(10);
73 static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
75 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
76 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
77 msleep(10);
78 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
79 msleep(10);
82 static void reset(struct tmio_mmc_host *host)
84 /* FIXME - should we set stop clock reg here */
85 sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
86 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
87 msleep(10);
88 sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
89 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
90 msleep(10);
93 static void
94 tmio_mmc_finish_request(struct tmio_mmc_host *host)
96 struct mmc_request *mrq = host->mrq;
98 host->mrq = NULL;
99 host->cmd = NULL;
100 host->data = NULL;
102 mmc_request_done(host->mmc, mrq);
105 /* These are the bitmasks the tmio chip requires to implement the MMC response
106 * types. Note that R1 and R6 are the same in this scheme. */
107 #define APP_CMD 0x0040
108 #define RESP_NONE 0x0300
109 #define RESP_R1 0x0400
110 #define RESP_R1B 0x0500
111 #define RESP_R2 0x0600
112 #define RESP_R3 0x0700
113 #define DATA_PRESENT 0x0800
114 #define TRANSFER_READ 0x1000
115 #define TRANSFER_MULTI 0x2000
116 #define SECURITY_CMD 0x4000
118 static int
119 tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
121 struct mmc_data *data = host->data;
122 int c = cmd->opcode;
124 /* Command 12 is handled by hardware */
125 if (cmd->opcode == 12 && !cmd->arg) {
126 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
127 return 0;
130 switch (mmc_resp_type(cmd)) {
131 case MMC_RSP_NONE: c |= RESP_NONE; break;
132 case MMC_RSP_R1: c |= RESP_R1; break;
133 case MMC_RSP_R1B: c |= RESP_R1B; break;
134 case MMC_RSP_R2: c |= RESP_R2; break;
135 case MMC_RSP_R3: c |= RESP_R3; break;
136 default:
137 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
138 return -EINVAL;
141 host->cmd = cmd;
143 /* FIXME - this seems to be ok comented out but the spec suggest this bit should
144 * be set when issuing app commands.
145 * if(cmd->flags & MMC_FLAG_ACMD)
146 * c |= APP_CMD;
148 if (data) {
149 c |= DATA_PRESENT;
150 if (data->blocks > 1) {
151 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
152 c |= TRANSFER_MULTI;
154 if (data->flags & MMC_DATA_READ)
155 c |= TRANSFER_READ;
158 enable_mmc_irqs(host, TMIO_MASK_CMD);
160 /* Fire off the command */
161 sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
162 sd_ctrl_write16(host, CTL_SD_CMD, c);
164 return 0;
167 /* This chip always returns (at least?) as much data as you ask for.
168 * I'm unsure what happens if you ask for less than a block. This should be
169 * looked into to ensure that a funny length read doesnt hose the controller.
172 static inline void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
174 struct mmc_data *data = host->data;
175 unsigned short *buf;
176 unsigned int count;
177 unsigned long flags;
179 if (!data) {
180 pr_debug("Spurious PIO IRQ\n");
181 return;
184 buf = (unsigned short *)(tmio_mmc_kmap_atomic(host, &flags) +
185 host->sg_off);
187 count = host->sg_ptr->length - host->sg_off;
188 if (count > data->blksz)
189 count = data->blksz;
191 pr_debug("count: %08x offset: %08x flags %08x\n",
192 count, host->sg_off, data->flags);
194 /* Transfer the data */
195 if (data->flags & MMC_DATA_READ)
196 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
197 else
198 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
200 host->sg_off += count;
202 tmio_mmc_kunmap_atomic(host, &flags);
204 if (host->sg_off == host->sg_ptr->length)
205 tmio_mmc_next_sg(host);
207 return;
210 static inline void tmio_mmc_data_irq(struct tmio_mmc_host *host)
212 struct mmc_data *data = host->data;
213 struct mmc_command *stop;
215 host->data = NULL;
217 if (!data) {
218 pr_debug("Spurious data end IRQ\n");
219 return;
221 stop = data->stop;
223 /* FIXME - return correct transfer count on errors */
224 if (!data->error)
225 data->bytes_xfered = data->blocks * data->blksz;
226 else
227 data->bytes_xfered = 0;
229 pr_debug("Completed data request\n");
231 /*FIXME - other drivers allow an optional stop command of any given type
232 * which we dont do, as the chip can auto generate them.
233 * Perhaps we can be smarter about when to use auto CMD12 and
234 * only issue the auto request when we know this is the desired
235 * stop command, allowing fallback to the stop command the
236 * upper layers expect. For now, we do what works.
239 if (data->flags & MMC_DATA_READ)
240 disable_mmc_irqs(host, TMIO_MASK_READOP);
241 else
242 disable_mmc_irqs(host, TMIO_MASK_WRITEOP);
244 if (stop) {
245 if (stop->opcode == 12 && !stop->arg)
246 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
247 else
248 BUG();
251 tmio_mmc_finish_request(host);
254 static inline void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
255 unsigned int stat)
257 struct mmc_command *cmd = host->cmd;
258 int i, addr;
260 if (!host->cmd) {
261 pr_debug("Spurious CMD irq\n");
262 return;
265 host->cmd = NULL;
267 /* This controller is sicker than the PXA one. Not only do we need to
268 * drop the top 8 bits of the first response word, we also need to
269 * modify the order of the response for short response command types.
272 for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
273 cmd->resp[i] = sd_ctrl_read32(host, addr);
275 if (cmd->flags & MMC_RSP_136) {
276 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
277 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
278 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
279 cmd->resp[3] <<= 8;
280 } else if (cmd->flags & MMC_RSP_R3) {
281 cmd->resp[0] = cmd->resp[3];
284 if (stat & TMIO_STAT_CMDTIMEOUT)
285 cmd->error = -ETIMEDOUT;
286 else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
287 cmd->error = -EILSEQ;
289 /* If there is data to handle we enable data IRQs here, and
290 * we will ultimatley finish the request in the data_end handler.
291 * If theres no data or we encountered an error, finish now.
293 if (host->data && !cmd->error) {
294 if (host->data->flags & MMC_DATA_READ)
295 enable_mmc_irqs(host, TMIO_MASK_READOP);
296 else
297 enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
298 } else {
299 tmio_mmc_finish_request(host);
302 return;
306 static irqreturn_t tmio_mmc_irq(int irq, void *devid)
308 struct tmio_mmc_host *host = devid;
309 unsigned int ireg, irq_mask, status;
311 pr_debug("MMC IRQ begin\n");
313 status = sd_ctrl_read32(host, CTL_STATUS);
314 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
315 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
317 pr_debug_status(status);
318 pr_debug_status(ireg);
320 if (!ireg) {
321 disable_mmc_irqs(host, status & ~irq_mask);
323 pr_debug("tmio_mmc: Spurious irq, disabling! "
324 "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
325 pr_debug_status(status);
327 goto out;
330 while (ireg) {
331 /* Card insert / remove attempts */
332 if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
333 ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
334 TMIO_STAT_CARD_REMOVE);
335 mmc_detect_change(host->mmc, 0);
338 /* CRC and other errors */
339 /* if (ireg & TMIO_STAT_ERR_IRQ)
340 * handled |= tmio_error_irq(host, irq, stat);
343 /* Command completion */
344 if (ireg & TMIO_MASK_CMD) {
345 ack_mmc_irqs(host, TMIO_MASK_CMD);
346 tmio_mmc_cmd_irq(host, status);
349 /* Data transfer */
350 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
351 ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
352 tmio_mmc_pio_irq(host);
355 /* Data transfer completion */
356 if (ireg & TMIO_STAT_DATAEND) {
357 ack_mmc_irqs(host, TMIO_STAT_DATAEND);
358 tmio_mmc_data_irq(host);
361 /* Check status - keep going until we've handled it all */
362 status = sd_ctrl_read32(host, CTL_STATUS);
363 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
364 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
366 pr_debug("Status at end of loop: %08x\n", status);
367 pr_debug_status(status);
369 pr_debug("MMC IRQ end\n");
371 out:
372 return IRQ_HANDLED;
375 static int tmio_mmc_start_data(struct tmio_mmc_host *host,
376 struct mmc_data *data)
378 pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
379 data->blksz, data->blocks);
381 /* Hardware cannot perform 1 and 2 byte requests in 4 bit mode */
382 if (data->blksz < 4 && host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
383 printk(KERN_ERR "%s: %d byte block unsupported in 4 bit mode\n",
384 mmc_hostname(host->mmc), data->blksz);
385 return -EINVAL;
388 tmio_mmc_init_sg(host, data);
389 host->data = data;
391 /* Set transfer length / blocksize */
392 sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
393 sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
395 return 0;
398 /* Process requests from the MMC layer */
399 static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
401 struct tmio_mmc_host *host = mmc_priv(mmc);
402 int ret;
404 if (host->mrq)
405 pr_debug("request not null\n");
407 host->mrq = mrq;
409 if (mrq->data) {
410 ret = tmio_mmc_start_data(host, mrq->data);
411 if (ret)
412 goto fail;
415 ret = tmio_mmc_start_command(host, mrq->cmd);
417 if (!ret)
418 return;
420 fail:
421 mrq->cmd->error = ret;
422 mmc_request_done(mmc, mrq);
425 /* Set MMC clock / power.
426 * Note: This controller uses a simple divider scheme therefore it cannot
427 * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
428 * MMC wont run that fast, it has to be clocked at 12MHz which is the next
429 * slowest setting.
431 static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
433 struct tmio_mmc_host *host = mmc_priv(mmc);
435 if (ios->clock)
436 tmio_mmc_set_clock(host, ios->clock);
438 /* Power sequence - OFF -> UP -> ON */
439 switch (ios->power_mode) {
440 case MMC_POWER_OFF: /* power down SD bus */
441 if (host->set_pwr)
442 host->set_pwr(host->pdev, 0);
443 tmio_mmc_clk_stop(host);
444 break;
445 case MMC_POWER_UP: /* power up SD bus */
446 if (host->set_pwr)
447 host->set_pwr(host->pdev, 1);
448 break;
449 case MMC_POWER_ON: /* enable bus clock */
450 tmio_mmc_clk_start(host);
451 break;
454 switch (ios->bus_width) {
455 case MMC_BUS_WIDTH_1:
456 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
457 break;
458 case MMC_BUS_WIDTH_4:
459 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
460 break;
463 /* Let things settle. delay taken from winCE driver */
464 udelay(140);
467 static int tmio_mmc_get_ro(struct mmc_host *mmc)
469 struct tmio_mmc_host *host = mmc_priv(mmc);
471 return (sd_ctrl_read16(host, CTL_STATUS) & TMIO_STAT_WRPROTECT) ? 0 : 1;
474 static struct mmc_host_ops tmio_mmc_ops = {
475 .request = tmio_mmc_request,
476 .set_ios = tmio_mmc_set_ios,
477 .get_ro = tmio_mmc_get_ro,
480 #ifdef CONFIG_PM
481 static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state)
483 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
484 struct mmc_host *mmc = platform_get_drvdata(dev);
485 int ret;
487 ret = mmc_suspend_host(mmc, state);
489 /* Tell MFD core it can disable us now.*/
490 if (!ret && cell->suspend)
491 cell->suspend(dev);
493 return ret;
496 static int tmio_mmc_resume(struct platform_device *dev)
498 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
499 struct mmc_host *mmc = platform_get_drvdata(dev);
500 int ret = 0;
502 /* Tell the MFD core we are ready to be enabled */
503 if (cell->resume) {
504 ret = cell->resume(dev);
505 if (ret)
506 goto out;
509 mmc_resume_host(mmc);
511 out:
512 return ret;
514 #else
515 #define tmio_mmc_suspend NULL
516 #define tmio_mmc_resume NULL
517 #endif
519 static int __devinit tmio_mmc_probe(struct platform_device *dev)
521 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
522 struct tmio_mmc_data *pdata;
523 struct resource *res_ctl;
524 struct tmio_mmc_host *host;
525 struct mmc_host *mmc;
526 int ret = -EINVAL;
528 if (dev->num_resources != 2)
529 goto out;
531 res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0);
532 if (!res_ctl)
533 goto out;
535 pdata = cell->driver_data;
536 if (!pdata || !pdata->hclk)
537 goto out;
539 ret = -ENOMEM;
541 mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev);
542 if (!mmc)
543 goto out;
545 host = mmc_priv(mmc);
546 host->mmc = mmc;
547 host->pdev = dev;
548 platform_set_drvdata(dev, mmc);
550 host->set_pwr = pdata->set_pwr;
551 host->set_no_clk_div = pdata->set_no_clk_div;
553 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
554 host->bus_shift = resource_size(res_ctl) >> 10;
556 host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
557 if (!host->ctl)
558 goto host_free;
560 mmc->ops = &tmio_mmc_ops;
561 mmc->caps = MMC_CAP_4_BIT_DATA;
562 mmc->f_max = pdata->hclk;
563 mmc->f_min = mmc->f_max / 512;
564 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
566 /* Tell the MFD core we are ready to be enabled */
567 if (cell->enable) {
568 ret = cell->enable(dev);
569 if (ret)
570 goto unmap_ctl;
573 tmio_mmc_clk_stop(host);
574 reset(host);
576 ret = platform_get_irq(dev, 0);
577 if (ret >= 0)
578 host->irq = ret;
579 else
580 goto unmap_ctl;
582 disable_mmc_irqs(host, TMIO_MASK_ALL);
584 ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED |
585 IRQF_TRIGGER_FALLING, "tmio-mmc", host);
586 if (ret)
587 goto unmap_ctl;
589 mmc_add_host(mmc);
591 printk(KERN_INFO "%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
592 (unsigned long)host->ctl, host->irq);
594 /* Unmask the IRQs we want to know about */
595 enable_mmc_irqs(host, TMIO_MASK_IRQ);
597 return 0;
599 unmap_ctl:
600 iounmap(host->ctl);
601 host_free:
602 mmc_free_host(mmc);
603 out:
604 return ret;
607 static int __devexit tmio_mmc_remove(struct platform_device *dev)
609 struct mmc_host *mmc = platform_get_drvdata(dev);
611 platform_set_drvdata(dev, NULL);
613 if (mmc) {
614 struct tmio_mmc_host *host = mmc_priv(mmc);
615 mmc_remove_host(mmc);
616 free_irq(host->irq, host);
617 iounmap(host->ctl);
618 mmc_free_host(mmc);
621 return 0;
624 /* ------------------- device registration ----------------------- */
626 static struct platform_driver tmio_mmc_driver = {
627 .driver = {
628 .name = "tmio-mmc",
629 .owner = THIS_MODULE,
631 .probe = tmio_mmc_probe,
632 .remove = __devexit_p(tmio_mmc_remove),
633 .suspend = tmio_mmc_suspend,
634 .resume = tmio_mmc_resume,
638 static int __init tmio_mmc_init(void)
640 return platform_driver_register(&tmio_mmc_driver);
643 static void __exit tmio_mmc_exit(void)
645 platform_driver_unregister(&tmio_mmc_driver);
648 module_init(tmio_mmc_init);
649 module_exit(tmio_mmc_exit);
651 MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
652 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
653 MODULE_LICENSE("GPL v2");
654 MODULE_ALIAS("platform:tmio-mmc");