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).
20 * Investigate using a workqueue for PIO transfers
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>
38 static void tmio_mmc_set_clock(struct tmio_mmc_host
*host
, int new_clock
)
43 for (clock
= host
->mmc
->f_min
, clk
= 0x80000080;
44 new_clock
>= (clock
<<1); clk
>>= 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;
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);
68 sd_ctrl_write16(host
, CTL_SD_CARD_CLK_CTL
, ~0x0100 &
69 sd_ctrl_read16(host
, CTL_SD_CARD_CLK_CTL
));
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
));
78 sd_ctrl_write16(host
, CTL_CLK_AND_WAIT_CTL
, 0x0100);
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);
88 sd_ctrl_write16(host
, CTL_RESET_SD
, 0x0001);
89 sd_ctrl_write16(host
, CTL_RESET_SDIO
, 0x0001);
94 tmio_mmc_finish_request(struct tmio_mmc_host
*host
)
96 struct mmc_request
*mrq
= host
->mrq
;
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
119 tmio_mmc_start_command(struct tmio_mmc_host
*host
, struct mmc_command
*cmd
)
121 struct mmc_data
*data
= host
->data
;
124 /* Command 12 is handled by hardware */
125 if (cmd
->opcode
== 12 && !cmd
->arg
) {
126 sd_ctrl_write16(host
, CTL_STOP_INTERNAL_ACTION
, 0x001);
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;
137 pr_debug("Unknown response type %d\n", mmc_resp_type(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)
150 if (data
->blocks
> 1) {
151 sd_ctrl_write16(host
, CTL_STOP_INTERNAL_ACTION
, 0x100);
154 if (data
->flags
& MMC_DATA_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
);
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
;
180 pr_debug("Spurious PIO IRQ\n");
184 buf
= (unsigned short *)(tmio_mmc_kmap_atomic(host
, &flags
) +
187 count
= host
->sg_ptr
->length
- host
->sg_off
;
188 if (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);
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
);
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
;
218 pr_debug("Spurious data end IRQ\n");
223 /* FIXME - return correct transfer count on errors */
225 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
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
);
242 disable_mmc_irqs(host
, TMIO_MASK_WRITEOP
);
245 if (stop
->opcode
== 12 && !stop
->arg
)
246 sd_ctrl_write16(host
, CTL_STOP_INTERNAL_ACTION
, 0x000);
251 tmio_mmc_finish_request(host
);
254 static inline void tmio_mmc_cmd_irq(struct tmio_mmc_host
*host
,
257 struct mmc_command
*cmd
= host
->cmd
;
261 pr_debug("Spurious CMD irq\n");
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);
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
);
297 enable_mmc_irqs(host
, TMIO_MASK_WRITEOP
);
299 tmio_mmc_finish_request(host
);
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
);
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
);
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
);
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");
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
);
388 tmio_mmc_init_sg(host
, 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
);
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
);
405 pr_debug("request not null\n");
410 ret
= tmio_mmc_start_data(host
, mrq
->data
);
415 ret
= tmio_mmc_start_command(host
, mrq
->cmd
);
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
431 static void tmio_mmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
433 struct tmio_mmc_host
*host
= mmc_priv(mmc
);
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 */
442 host
->set_pwr(host
->pdev
, 0);
443 tmio_mmc_clk_stop(host
);
445 case MMC_POWER_UP
: /* power up SD bus */
447 host
->set_pwr(host
->pdev
, 1);
449 case MMC_POWER_ON
: /* enable bus clock */
450 tmio_mmc_clk_start(host
);
454 switch (ios
->bus_width
) {
455 case MMC_BUS_WIDTH_1
:
456 sd_ctrl_write16(host
, CTL_SD_MEM_CARD_OPT
, 0x80e0);
458 case MMC_BUS_WIDTH_4
:
459 sd_ctrl_write16(host
, CTL_SD_MEM_CARD_OPT
, 0x00e0);
463 /* Let things settle. delay taken from winCE driver */
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
,
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
);
487 ret
= mmc_suspend_host(mmc
, state
);
489 /* Tell MFD core it can disable us now.*/
490 if (!ret
&& cell
->suspend
)
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
);
502 /* Tell the MFD core we are ready to be enabled */
504 ret
= cell
->resume(dev
);
509 mmc_resume_host(mmc
);
515 #define tmio_mmc_suspend NULL
516 #define tmio_mmc_resume NULL
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
;
528 if (dev
->num_resources
!= 2)
531 res_ctl
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
535 pdata
= cell
->driver_data
;
536 if (!pdata
|| !pdata
->hclk
)
541 mmc
= mmc_alloc_host(sizeof(struct tmio_mmc_host
), &dev
->dev
);
545 host
= mmc_priv(mmc
);
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
));
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 */
568 ret
= cell
->enable(dev
);
573 tmio_mmc_clk_stop(host
);
576 ret
= platform_get_irq(dev
, 0);
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
);
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
);
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
);
614 struct tmio_mmc_host
*host
= mmc_priv(mmc
);
615 mmc_remove_host(mmc
);
616 free_irq(host
->irq
, host
);
624 /* ------------------- device registration ----------------------- */
626 static struct platform_driver tmio_mmc_driver
= {
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");