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
29 #include <linux/delay.h>
30 #include <linux/device.h>
31 #include <linux/dmaengine.h>
32 #include <linux/highmem.h>
33 #include <linux/interrupt.h>
35 #include <linux/irq.h>
36 #include <linux/mfd/core.h>
37 #include <linux/mfd/tmio.h>
38 #include <linux/mmc/host.h>
39 #include <linux/module.h>
40 #include <linux/pagemap.h>
41 #include <linux/scatterlist.h>
42 #include <linux/workqueue.h>
43 #include <linux/spinlock.h>
45 #define CTL_SD_CMD 0x00
46 #define CTL_ARG_REG 0x04
47 #define CTL_STOP_INTERNAL_ACTION 0x08
48 #define CTL_XFER_BLK_COUNT 0xa
49 #define CTL_RESPONSE 0x0c
50 #define CTL_STATUS 0x1c
51 #define CTL_IRQ_MASK 0x20
52 #define CTL_SD_CARD_CLK_CTL 0x24
53 #define CTL_SD_XFER_LEN 0x26
54 #define CTL_SD_MEM_CARD_OPT 0x28
55 #define CTL_SD_ERROR_DETAIL_STATUS 0x2c
56 #define CTL_SD_DATA_PORT 0x30
57 #define CTL_TRANSACTION_CTL 0x34
58 #define CTL_SDIO_STATUS 0x36
59 #define CTL_SDIO_IRQ_MASK 0x38
60 #define CTL_RESET_SD 0xe0
61 #define CTL_SDIO_REGS 0x100
62 #define CTL_CLK_AND_WAIT_CTL 0x138
63 #define CTL_RESET_SDIO 0x1e0
65 /* Definitions for values the CTRL_STATUS register can take. */
66 #define TMIO_STAT_CMDRESPEND 0x00000001
67 #define TMIO_STAT_DATAEND 0x00000004
68 #define TMIO_STAT_CARD_REMOVE 0x00000008
69 #define TMIO_STAT_CARD_INSERT 0x00000010
70 #define TMIO_STAT_SIGSTATE 0x00000020
71 #define TMIO_STAT_WRPROTECT 0x00000080
72 #define TMIO_STAT_CARD_REMOVE_A 0x00000100
73 #define TMIO_STAT_CARD_INSERT_A 0x00000200
74 #define TMIO_STAT_SIGSTATE_A 0x00000400
75 #define TMIO_STAT_CMD_IDX_ERR 0x00010000
76 #define TMIO_STAT_CRCFAIL 0x00020000
77 #define TMIO_STAT_STOPBIT_ERR 0x00040000
78 #define TMIO_STAT_DATATIMEOUT 0x00080000
79 #define TMIO_STAT_RXOVERFLOW 0x00100000
80 #define TMIO_STAT_TXUNDERRUN 0x00200000
81 #define TMIO_STAT_CMDTIMEOUT 0x00400000
82 #define TMIO_STAT_RXRDY 0x01000000
83 #define TMIO_STAT_TXRQ 0x02000000
84 #define TMIO_STAT_ILL_FUNC 0x20000000
85 #define TMIO_STAT_CMD_BUSY 0x40000000
86 #define TMIO_STAT_ILL_ACCESS 0x80000000
88 /* Definitions for values the CTRL_SDIO_STATUS register can take. */
89 #define TMIO_SDIO_STAT_IOIRQ 0x0001
90 #define TMIO_SDIO_STAT_EXPUB52 0x4000
91 #define TMIO_SDIO_STAT_EXWT 0x8000
92 #define TMIO_SDIO_MASK_ALL 0xc007
94 /* Define some IRQ masks */
95 /* This is the mask used at reset by the chip */
96 #define TMIO_MASK_ALL 0x837f031d
97 #define TMIO_MASK_READOP (TMIO_STAT_RXRDY | TMIO_STAT_DATAEND)
98 #define TMIO_MASK_WRITEOP (TMIO_STAT_TXRQ | TMIO_STAT_DATAEND)
99 #define TMIO_MASK_CMD (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT | \
100 TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT)
101 #define TMIO_MASK_IRQ (TMIO_MASK_READOP | TMIO_MASK_WRITEOP | TMIO_MASK_CMD)
103 #define enable_mmc_irqs(host, i) \
106 mask = sd_ctrl_read32((host), CTL_IRQ_MASK); \
107 mask &= ~((i) & TMIO_MASK_IRQ); \
108 sd_ctrl_write32((host), CTL_IRQ_MASK, mask); \
111 #define disable_mmc_irqs(host, i) \
114 mask = sd_ctrl_read32((host), CTL_IRQ_MASK); \
115 mask |= ((i) & TMIO_MASK_IRQ); \
116 sd_ctrl_write32((host), CTL_IRQ_MASK, mask); \
119 #define ack_mmc_irqs(host, i) \
121 sd_ctrl_write32((host), CTL_STATUS, ~(i)); \
124 /* This is arbitrary, just noone needed any higher alignment yet */
127 struct tmio_mmc_host
{
129 unsigned long bus_shift
;
130 struct mmc_command
*cmd
;
131 struct mmc_request
*mrq
;
132 struct mmc_data
*data
;
133 struct mmc_host
*mmc
;
135 unsigned int sdio_irq_enabled
;
137 /* Callbacks for clock / power control */
138 void (*set_pwr
)(struct platform_device
*host
, int state
);
139 void (*set_clk_div
)(struct platform_device
*host
, int state
);
141 /* pio related stuff */
142 struct scatterlist
*sg_ptr
;
143 struct scatterlist
*sg_orig
;
147 struct platform_device
*pdev
;
150 struct dma_chan
*chan_rx
;
151 struct dma_chan
*chan_tx
;
152 struct tasklet_struct dma_complete
;
153 struct tasklet_struct dma_issue
;
154 #ifdef CONFIG_TMIO_MMC_DMA
155 unsigned int dma_sglen
;
156 u8 bounce_buf
[PAGE_CACHE_SIZE
] __attribute__((aligned(MAX_ALIGN
)));
157 struct scatterlist bounce_sg
;
160 /* Track lost interrupts */
161 struct delayed_work delayed_reset_work
;
163 unsigned long last_req_ts
;
166 static void tmio_check_bounce_buffer(struct tmio_mmc_host
*host
);
168 static u16
sd_ctrl_read16(struct tmio_mmc_host
*host
, int addr
)
170 return readw(host
->ctl
+ (addr
<< host
->bus_shift
));
173 static void sd_ctrl_read16_rep(struct tmio_mmc_host
*host
, int addr
,
176 readsw(host
->ctl
+ (addr
<< host
->bus_shift
), buf
, count
);
179 static u32
sd_ctrl_read32(struct tmio_mmc_host
*host
, int addr
)
181 return readw(host
->ctl
+ (addr
<< host
->bus_shift
)) |
182 readw(host
->ctl
+ ((addr
+ 2) << host
->bus_shift
)) << 16;
185 static void sd_ctrl_write16(struct tmio_mmc_host
*host
, int addr
, u16 val
)
187 writew(val
, host
->ctl
+ (addr
<< host
->bus_shift
));
190 static void sd_ctrl_write16_rep(struct tmio_mmc_host
*host
, int addr
,
193 writesw(host
->ctl
+ (addr
<< host
->bus_shift
), buf
, count
);
196 static void sd_ctrl_write32(struct tmio_mmc_host
*host
, int addr
, u32 val
)
198 writew(val
, host
->ctl
+ (addr
<< host
->bus_shift
));
199 writew(val
>> 16, host
->ctl
+ ((addr
+ 2) << host
->bus_shift
));
202 static void tmio_mmc_init_sg(struct tmio_mmc_host
*host
, struct mmc_data
*data
)
204 host
->sg_len
= data
->sg_len
;
205 host
->sg_ptr
= data
->sg
;
206 host
->sg_orig
= data
->sg
;
210 static int tmio_mmc_next_sg(struct tmio_mmc_host
*host
)
212 host
->sg_ptr
= sg_next(host
->sg_ptr
);
214 return --host
->sg_len
;
217 static char *tmio_mmc_kmap_atomic(struct scatterlist
*sg
, unsigned long *flags
)
219 local_irq_save(*flags
);
220 return kmap_atomic(sg_page(sg
), KM_BIO_SRC_IRQ
) + sg
->offset
;
223 static void tmio_mmc_kunmap_atomic(void *virt
, unsigned long *flags
)
225 kunmap_atomic(virt
, KM_BIO_SRC_IRQ
);
226 local_irq_restore(*flags
);
229 #ifdef CONFIG_MMC_DEBUG
231 #define STATUS_TO_TEXT(a) \
233 if (status & TMIO_STAT_##a) \
237 void pr_debug_status(u32 status
)
239 printk(KERN_DEBUG
"status: %08x = ", status
);
240 STATUS_TO_TEXT(CARD_REMOVE
);
241 STATUS_TO_TEXT(CARD_INSERT
);
242 STATUS_TO_TEXT(SIGSTATE
);
243 STATUS_TO_TEXT(WRPROTECT
);
244 STATUS_TO_TEXT(CARD_REMOVE_A
);
245 STATUS_TO_TEXT(CARD_INSERT_A
);
246 STATUS_TO_TEXT(SIGSTATE_A
);
247 STATUS_TO_TEXT(CMD_IDX_ERR
);
248 STATUS_TO_TEXT(STOPBIT_ERR
);
249 STATUS_TO_TEXT(ILL_FUNC
);
250 STATUS_TO_TEXT(CMD_BUSY
);
251 STATUS_TO_TEXT(CMDRESPEND
);
252 STATUS_TO_TEXT(DATAEND
);
253 STATUS_TO_TEXT(CRCFAIL
);
254 STATUS_TO_TEXT(DATATIMEOUT
);
255 STATUS_TO_TEXT(CMDTIMEOUT
);
256 STATUS_TO_TEXT(RXOVERFLOW
);
257 STATUS_TO_TEXT(TXUNDERRUN
);
258 STATUS_TO_TEXT(RXRDY
);
259 STATUS_TO_TEXT(TXRQ
);
260 STATUS_TO_TEXT(ILL_ACCESS
);
265 #define pr_debug_status(s) do { } while (0)
268 static void tmio_mmc_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
270 struct tmio_mmc_host
*host
= mmc_priv(mmc
);
273 host
->sdio_irq_enabled
= 1;
274 sd_ctrl_write16(host
, CTL_TRANSACTION_CTL
, 0x0001);
275 sd_ctrl_write16(host
, CTL_SDIO_IRQ_MASK
,
276 (TMIO_SDIO_MASK_ALL
& ~TMIO_SDIO_STAT_IOIRQ
));
278 sd_ctrl_write16(host
, CTL_SDIO_IRQ_MASK
, TMIO_SDIO_MASK_ALL
);
279 sd_ctrl_write16(host
, CTL_TRANSACTION_CTL
, 0x0000);
280 host
->sdio_irq_enabled
= 0;
284 static void tmio_mmc_set_clock(struct tmio_mmc_host
*host
, int new_clock
)
289 for (clock
= host
->mmc
->f_min
, clk
= 0x80000080;
290 new_clock
>= (clock
<<1); clk
>>= 1)
295 if (host
->set_clk_div
)
296 host
->set_clk_div(host
->pdev
, (clk
>>22) & 1);
298 sd_ctrl_write16(host
, CTL_SD_CARD_CLK_CTL
, clk
& 0x1ff);
301 static void tmio_mmc_clk_stop(struct tmio_mmc_host
*host
)
303 struct mfd_cell
*cell
= host
->pdev
->dev
.platform_data
;
304 struct tmio_mmc_data
*pdata
= cell
->driver_data
;
307 * Testing on sh-mobile showed that SDIO IRQs are unmasked when
308 * CTL_CLK_AND_WAIT_CTL gets written, so we have to disable the
309 * device IRQ here and restore the SDIO IRQ mask before
310 * re-enabling the device IRQ.
312 if (pdata
->flags
& TMIO_MMC_SDIO_IRQ
)
313 disable_irq(host
->irq
);
314 sd_ctrl_write16(host
, CTL_CLK_AND_WAIT_CTL
, 0x0000);
316 if (pdata
->flags
& TMIO_MMC_SDIO_IRQ
) {
317 tmio_mmc_enable_sdio_irq(host
->mmc
, host
->sdio_irq_enabled
);
318 enable_irq(host
->irq
);
320 sd_ctrl_write16(host
, CTL_SD_CARD_CLK_CTL
, ~0x0100 &
321 sd_ctrl_read16(host
, CTL_SD_CARD_CLK_CTL
));
325 static void tmio_mmc_clk_start(struct tmio_mmc_host
*host
)
327 struct mfd_cell
*cell
= host
->pdev
->dev
.platform_data
;
328 struct tmio_mmc_data
*pdata
= cell
->driver_data
;
330 sd_ctrl_write16(host
, CTL_SD_CARD_CLK_CTL
, 0x0100 |
331 sd_ctrl_read16(host
, CTL_SD_CARD_CLK_CTL
));
333 /* see comment in tmio_mmc_clk_stop above */
334 if (pdata
->flags
& TMIO_MMC_SDIO_IRQ
)
335 disable_irq(host
->irq
);
336 sd_ctrl_write16(host
, CTL_CLK_AND_WAIT_CTL
, 0x0100);
338 if (pdata
->flags
& TMIO_MMC_SDIO_IRQ
) {
339 tmio_mmc_enable_sdio_irq(host
->mmc
, host
->sdio_irq_enabled
);
340 enable_irq(host
->irq
);
344 static void reset(struct tmio_mmc_host
*host
)
346 /* FIXME - should we set stop clock reg here */
347 sd_ctrl_write16(host
, CTL_RESET_SD
, 0x0000);
348 sd_ctrl_write16(host
, CTL_RESET_SDIO
, 0x0000);
350 sd_ctrl_write16(host
, CTL_RESET_SD
, 0x0001);
351 sd_ctrl_write16(host
, CTL_RESET_SDIO
, 0x0001);
355 static void tmio_mmc_reset_work(struct work_struct
*work
)
357 struct tmio_mmc_host
*host
= container_of(work
, struct tmio_mmc_host
,
358 delayed_reset_work
.work
);
359 struct mmc_request
*mrq
;
362 spin_lock_irqsave(&host
->lock
, flags
);
365 /* request already finished */
367 || time_is_after_jiffies(host
->last_req_ts
+
368 msecs_to_jiffies(2000))) {
369 spin_unlock_irqrestore(&host
->lock
, flags
);
373 dev_warn(&host
->pdev
->dev
,
374 "timeout waiting for hardware interrupt (CMD%u)\n",
378 host
->data
->error
= -ETIMEDOUT
;
380 host
->cmd
->error
= -ETIMEDOUT
;
382 mrq
->cmd
->error
= -ETIMEDOUT
;
388 spin_unlock_irqrestore(&host
->lock
, flags
);
392 mmc_request_done(host
->mmc
, mrq
);
396 tmio_mmc_finish_request(struct tmio_mmc_host
*host
)
398 struct mmc_request
*mrq
= host
->mrq
;
407 cancel_delayed_work(&host
->delayed_reset_work
);
409 mmc_request_done(host
->mmc
, mrq
);
412 /* These are the bitmasks the tmio chip requires to implement the MMC response
413 * types. Note that R1 and R6 are the same in this scheme. */
414 #define APP_CMD 0x0040
415 #define RESP_NONE 0x0300
416 #define RESP_R1 0x0400
417 #define RESP_R1B 0x0500
418 #define RESP_R2 0x0600
419 #define RESP_R3 0x0700
420 #define DATA_PRESENT 0x0800
421 #define TRANSFER_READ 0x1000
422 #define TRANSFER_MULTI 0x2000
423 #define SECURITY_CMD 0x4000
426 tmio_mmc_start_command(struct tmio_mmc_host
*host
, struct mmc_command
*cmd
)
428 struct mmc_data
*data
= host
->data
;
431 /* Command 12 is handled by hardware */
432 if (cmd
->opcode
== 12 && !cmd
->arg
) {
433 sd_ctrl_write16(host
, CTL_STOP_INTERNAL_ACTION
, 0x001);
437 switch (mmc_resp_type(cmd
)) {
438 case MMC_RSP_NONE
: c
|= RESP_NONE
; break;
439 case MMC_RSP_R1
: c
|= RESP_R1
; break;
440 case MMC_RSP_R1B
: c
|= RESP_R1B
; break;
441 case MMC_RSP_R2
: c
|= RESP_R2
; break;
442 case MMC_RSP_R3
: c
|= RESP_R3
; break;
444 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd
));
450 /* FIXME - this seems to be ok commented out but the spec suggest this bit
451 * should be set when issuing app commands.
452 * if(cmd->flags & MMC_FLAG_ACMD)
457 if (data
->blocks
> 1) {
458 sd_ctrl_write16(host
, CTL_STOP_INTERNAL_ACTION
, 0x100);
461 if (data
->flags
& MMC_DATA_READ
)
465 enable_mmc_irqs(host
, TMIO_MASK_CMD
);
467 /* Fire off the command */
468 sd_ctrl_write32(host
, CTL_ARG_REG
, cmd
->arg
);
469 sd_ctrl_write16(host
, CTL_SD_CMD
, c
);
475 * This chip always returns (at least?) as much data as you ask for.
476 * I'm unsure what happens if you ask for less than a block. This should be
477 * looked into to ensure that a funny length read doesnt hose the controller.
479 static void tmio_mmc_pio_irq(struct tmio_mmc_host
*host
)
481 struct mmc_data
*data
= host
->data
;
488 pr_debug("Spurious PIO IRQ\n");
492 sg_virt
= tmio_mmc_kmap_atomic(host
->sg_ptr
, &flags
);
493 buf
= (unsigned short *)(sg_virt
+ host
->sg_off
);
495 count
= host
->sg_ptr
->length
- host
->sg_off
;
496 if (count
> data
->blksz
)
499 pr_debug("count: %08x offset: %08x flags %08x\n",
500 count
, host
->sg_off
, data
->flags
);
502 /* Transfer the data */
503 if (data
->flags
& MMC_DATA_READ
)
504 sd_ctrl_read16_rep(host
, CTL_SD_DATA_PORT
, buf
, count
>> 1);
506 sd_ctrl_write16_rep(host
, CTL_SD_DATA_PORT
, buf
, count
>> 1);
508 host
->sg_off
+= count
;
510 tmio_mmc_kunmap_atomic(sg_virt
, &flags
);
512 if (host
->sg_off
== host
->sg_ptr
->length
)
513 tmio_mmc_next_sg(host
);
518 /* needs to be called with host->lock held */
519 static void tmio_mmc_do_data_irq(struct tmio_mmc_host
*host
)
521 struct mmc_data
*data
= host
->data
;
522 struct mmc_command
*stop
;
527 dev_warn(&host
->pdev
->dev
, "Spurious data end IRQ\n");
532 /* FIXME - return correct transfer count on errors */
534 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
536 data
->bytes_xfered
= 0;
538 pr_debug("Completed data request\n");
541 * FIXME: other drivers allow an optional stop command of any given type
542 * which we dont do, as the chip can auto generate them.
543 * Perhaps we can be smarter about when to use auto CMD12 and
544 * only issue the auto request when we know this is the desired
545 * stop command, allowing fallback to the stop command the
546 * upper layers expect. For now, we do what works.
549 if (data
->flags
& MMC_DATA_READ
) {
551 disable_mmc_irqs(host
, TMIO_MASK_READOP
);
553 tmio_check_bounce_buffer(host
);
554 dev_dbg(&host
->pdev
->dev
, "Complete Rx request %p\n",
558 disable_mmc_irqs(host
, TMIO_MASK_WRITEOP
);
559 dev_dbg(&host
->pdev
->dev
, "Complete Tx request %p\n",
564 if (stop
->opcode
== 12 && !stop
->arg
)
565 sd_ctrl_write16(host
, CTL_STOP_INTERNAL_ACTION
, 0x000);
570 tmio_mmc_finish_request(host
);
573 static void tmio_mmc_data_irq(struct tmio_mmc_host
*host
)
575 struct mmc_data
*data
;
576 spin_lock(&host
->lock
);
582 if (host
->chan_tx
&& (data
->flags
& MMC_DATA_WRITE
)) {
584 * Has all data been written out yet? Testing on SuperH showed,
585 * that in most cases the first interrupt comes already with the
586 * BUSY status bit clear, but on some operations, like mount or
587 * in the beginning of a write / sync / umount, there is one
588 * DATAEND interrupt with the BUSY bit set, in this cases
589 * waiting for one more interrupt fixes the problem.
591 if (!(sd_ctrl_read32(host
, CTL_STATUS
) & TMIO_STAT_CMD_BUSY
)) {
592 disable_mmc_irqs(host
, TMIO_STAT_DATAEND
);
593 tasklet_schedule(&host
->dma_complete
);
595 } else if (host
->chan_rx
&& (data
->flags
& MMC_DATA_READ
)) {
596 disable_mmc_irqs(host
, TMIO_STAT_DATAEND
);
597 tasklet_schedule(&host
->dma_complete
);
599 tmio_mmc_do_data_irq(host
);
602 spin_unlock(&host
->lock
);
605 static void tmio_mmc_cmd_irq(struct tmio_mmc_host
*host
,
608 struct mmc_command
*cmd
= host
->cmd
;
611 spin_lock(&host
->lock
);
614 pr_debug("Spurious CMD irq\n");
620 /* This controller is sicker than the PXA one. Not only do we need to
621 * drop the top 8 bits of the first response word, we also need to
622 * modify the order of the response for short response command types.
625 for (i
= 3, addr
= CTL_RESPONSE
; i
>= 0 ; i
--, addr
+= 4)
626 cmd
->resp
[i
] = sd_ctrl_read32(host
, addr
);
628 if (cmd
->flags
& MMC_RSP_136
) {
629 cmd
->resp
[0] = (cmd
->resp
[0] << 8) | (cmd
->resp
[1] >> 24);
630 cmd
->resp
[1] = (cmd
->resp
[1] << 8) | (cmd
->resp
[2] >> 24);
631 cmd
->resp
[2] = (cmd
->resp
[2] << 8) | (cmd
->resp
[3] >> 24);
633 } else if (cmd
->flags
& MMC_RSP_R3
) {
634 cmd
->resp
[0] = cmd
->resp
[3];
637 if (stat
& TMIO_STAT_CMDTIMEOUT
)
638 cmd
->error
= -ETIMEDOUT
;
639 else if (stat
& TMIO_STAT_CRCFAIL
&& cmd
->flags
& MMC_RSP_CRC
)
640 cmd
->error
= -EILSEQ
;
642 /* If there is data to handle we enable data IRQs here, and
643 * we will ultimatley finish the request in the data_end handler.
644 * If theres no data or we encountered an error, finish now.
646 if (host
->data
&& !cmd
->error
) {
647 if (host
->data
->flags
& MMC_DATA_READ
) {
649 enable_mmc_irqs(host
, TMIO_MASK_READOP
);
652 enable_mmc_irqs(host
, TMIO_MASK_WRITEOP
);
654 tasklet_schedule(&host
->dma_issue
);
657 tmio_mmc_finish_request(host
);
661 spin_unlock(&host
->lock
);
666 static irqreturn_t
tmio_mmc_irq(int irq
, void *devid
)
668 struct tmio_mmc_host
*host
= devid
;
669 struct mfd_cell
*cell
= host
->pdev
->dev
.platform_data
;
670 struct tmio_mmc_data
*pdata
= cell
->driver_data
;
671 unsigned int ireg
, irq_mask
, status
;
672 unsigned int sdio_ireg
, sdio_irq_mask
, sdio_status
;
674 pr_debug("MMC IRQ begin\n");
676 status
= sd_ctrl_read32(host
, CTL_STATUS
);
677 irq_mask
= sd_ctrl_read32(host
, CTL_IRQ_MASK
);
678 ireg
= status
& TMIO_MASK_IRQ
& ~irq_mask
;
681 if (!ireg
&& pdata
->flags
& TMIO_MMC_SDIO_IRQ
) {
682 sdio_status
= sd_ctrl_read16(host
, CTL_SDIO_STATUS
);
683 sdio_irq_mask
= sd_ctrl_read16(host
, CTL_SDIO_IRQ_MASK
);
684 sdio_ireg
= sdio_status
& TMIO_SDIO_MASK_ALL
& ~sdio_irq_mask
;
686 sd_ctrl_write16(host
, CTL_SDIO_STATUS
, sdio_status
& ~TMIO_SDIO_MASK_ALL
);
688 if (sdio_ireg
&& !host
->sdio_irq_enabled
) {
689 pr_warning("tmio_mmc: Spurious SDIO IRQ, disabling! 0x%04x 0x%04x 0x%04x\n",
690 sdio_status
, sdio_irq_mask
, sdio_ireg
);
691 tmio_mmc_enable_sdio_irq(host
->mmc
, 0);
695 if (host
->mmc
->caps
& MMC_CAP_SDIO_IRQ
&&
696 sdio_ireg
& TMIO_SDIO_STAT_IOIRQ
)
697 mmc_signal_sdio_irq(host
->mmc
);
703 pr_debug_status(status
);
704 pr_debug_status(ireg
);
707 disable_mmc_irqs(host
, status
& ~irq_mask
);
709 pr_warning("tmio_mmc: Spurious irq, disabling! "
710 "0x%08x 0x%08x 0x%08x\n", status
, irq_mask
, ireg
);
711 pr_debug_status(status
);
717 /* Card insert / remove attempts */
718 if (ireg
& (TMIO_STAT_CARD_INSERT
| TMIO_STAT_CARD_REMOVE
)) {
719 ack_mmc_irqs(host
, TMIO_STAT_CARD_INSERT
|
720 TMIO_STAT_CARD_REMOVE
);
721 mmc_detect_change(host
->mmc
, msecs_to_jiffies(100));
724 /* CRC and other errors */
725 /* if (ireg & TMIO_STAT_ERR_IRQ)
726 * handled |= tmio_error_irq(host, irq, stat);
729 /* Command completion */
730 if (ireg
& (TMIO_STAT_CMDRESPEND
| TMIO_STAT_CMDTIMEOUT
)) {
732 TMIO_STAT_CMDRESPEND
|
733 TMIO_STAT_CMDTIMEOUT
);
734 tmio_mmc_cmd_irq(host
, status
);
738 if (ireg
& (TMIO_STAT_RXRDY
| TMIO_STAT_TXRQ
)) {
739 ack_mmc_irqs(host
, TMIO_STAT_RXRDY
| TMIO_STAT_TXRQ
);
740 tmio_mmc_pio_irq(host
);
743 /* Data transfer completion */
744 if (ireg
& TMIO_STAT_DATAEND
) {
745 ack_mmc_irqs(host
, TMIO_STAT_DATAEND
);
746 tmio_mmc_data_irq(host
);
749 /* Check status - keep going until we've handled it all */
750 status
= sd_ctrl_read32(host
, CTL_STATUS
);
751 irq_mask
= sd_ctrl_read32(host
, CTL_IRQ_MASK
);
752 ireg
= status
& TMIO_MASK_IRQ
& ~irq_mask
;
754 pr_debug("Status at end of loop: %08x\n", status
);
755 pr_debug_status(status
);
757 pr_debug("MMC IRQ end\n");
763 #ifdef CONFIG_TMIO_MMC_DMA
764 static void tmio_check_bounce_buffer(struct tmio_mmc_host
*host
)
766 if (host
->sg_ptr
== &host
->bounce_sg
) {
768 void *sg_vaddr
= tmio_mmc_kmap_atomic(host
->sg_orig
, &flags
);
769 memcpy(sg_vaddr
, host
->bounce_buf
, host
->bounce_sg
.length
);
770 tmio_mmc_kunmap_atomic(sg_vaddr
, &flags
);
774 static void tmio_mmc_enable_dma(struct tmio_mmc_host
*host
, bool enable
)
776 #if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
777 /* Switch DMA mode on or off - SuperH specific? */
778 sd_ctrl_write16(host
, 0xd8, enable
? 2 : 0);
782 static void tmio_dma_complete(void *arg
)
784 struct tmio_mmc_host
*host
= arg
;
786 dev_dbg(&host
->pdev
->dev
, "Command completed\n");
789 dev_warn(&host
->pdev
->dev
, "NULL data in DMA completion!\n");
791 enable_mmc_irqs(host
, TMIO_STAT_DATAEND
);
794 static void tmio_mmc_start_dma_rx(struct tmio_mmc_host
*host
)
796 struct scatterlist
*sg
= host
->sg_ptr
, *sg_tmp
;
797 struct dma_async_tx_descriptor
*desc
= NULL
;
798 struct dma_chan
*chan
= host
->chan_rx
;
799 struct mfd_cell
*cell
= host
->pdev
->dev
.platform_data
;
800 struct tmio_mmc_data
*pdata
= cell
->driver_data
;
803 bool aligned
= true, multiple
= true;
804 unsigned int align
= (1 << pdata
->dma
->alignment_shift
) - 1;
806 for_each_sg(sg
, sg_tmp
, host
->sg_len
, i
) {
807 if (sg_tmp
->offset
& align
)
809 if (sg_tmp
->length
& align
) {
815 if ((!aligned
&& (host
->sg_len
> 1 || sg
->length
> PAGE_CACHE_SIZE
||
816 align
>= MAX_ALIGN
)) || !multiple
) {
821 /* The only sg element can be unaligned, use our bounce buffer then */
823 sg_init_one(&host
->bounce_sg
, host
->bounce_buf
, sg
->length
);
824 host
->sg_ptr
= &host
->bounce_sg
;
828 ret
= dma_map_sg(&host
->pdev
->dev
, sg
, host
->sg_len
, DMA_FROM_DEVICE
);
830 host
->dma_sglen
= ret
;
831 desc
= chan
->device
->device_prep_slave_sg(chan
, sg
, ret
,
832 DMA_FROM_DEVICE
, DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
836 desc
->callback
= tmio_dma_complete
;
837 desc
->callback_param
= host
;
838 cookie
= desc
->tx_submit(desc
);
843 chan
->device
->device_issue_pending(chan
);
846 dev_dbg(&host
->pdev
->dev
, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
847 __func__
, host
->sg_len
, ret
, cookie
, host
->mrq
);
851 /* DMA failed, fall back to PIO */
854 host
->chan_rx
= NULL
;
855 dma_release_channel(chan
);
856 /* Free the Tx channel too */
857 chan
= host
->chan_tx
;
859 host
->chan_tx
= NULL
;
860 dma_release_channel(chan
);
862 dev_warn(&host
->pdev
->dev
,
863 "DMA failed: %d, falling back to PIO\n", ret
);
864 tmio_mmc_enable_dma(host
, false);
867 dev_dbg(&host
->pdev
->dev
, "%s(): desc %p, cookie %d, sg[%d]\n", __func__
,
868 desc
, cookie
, host
->sg_len
);
871 static void tmio_mmc_start_dma_tx(struct tmio_mmc_host
*host
)
873 struct scatterlist
*sg
= host
->sg_ptr
, *sg_tmp
;
874 struct dma_async_tx_descriptor
*desc
= NULL
;
875 struct dma_chan
*chan
= host
->chan_tx
;
876 struct mfd_cell
*cell
= host
->pdev
->dev
.platform_data
;
877 struct tmio_mmc_data
*pdata
= cell
->driver_data
;
880 bool aligned
= true, multiple
= true;
881 unsigned int align
= (1 << pdata
->dma
->alignment_shift
) - 1;
883 for_each_sg(sg
, sg_tmp
, host
->sg_len
, i
) {
884 if (sg_tmp
->offset
& align
)
886 if (sg_tmp
->length
& align
) {
892 if ((!aligned
&& (host
->sg_len
> 1 || sg
->length
> PAGE_CACHE_SIZE
||
893 align
>= MAX_ALIGN
)) || !multiple
) {
898 /* The only sg element can be unaligned, use our bounce buffer then */
901 void *sg_vaddr
= tmio_mmc_kmap_atomic(sg
, &flags
);
902 sg_init_one(&host
->bounce_sg
, host
->bounce_buf
, sg
->length
);
903 memcpy(host
->bounce_buf
, sg_vaddr
, host
->bounce_sg
.length
);
904 tmio_mmc_kunmap_atomic(sg_vaddr
, &flags
);
905 host
->sg_ptr
= &host
->bounce_sg
;
909 ret
= dma_map_sg(&host
->pdev
->dev
, sg
, host
->sg_len
, DMA_TO_DEVICE
);
911 host
->dma_sglen
= ret
;
912 desc
= chan
->device
->device_prep_slave_sg(chan
, sg
, ret
,
913 DMA_TO_DEVICE
, DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
917 desc
->callback
= tmio_dma_complete
;
918 desc
->callback_param
= host
;
919 cookie
= desc
->tx_submit(desc
);
925 dev_dbg(&host
->pdev
->dev
, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
926 __func__
, host
->sg_len
, ret
, cookie
, host
->mrq
);
930 /* DMA failed, fall back to PIO */
933 host
->chan_tx
= NULL
;
934 dma_release_channel(chan
);
935 /* Free the Rx channel too */
936 chan
= host
->chan_rx
;
938 host
->chan_rx
= NULL
;
939 dma_release_channel(chan
);
941 dev_warn(&host
->pdev
->dev
,
942 "DMA failed: %d, falling back to PIO\n", ret
);
943 tmio_mmc_enable_dma(host
, false);
946 dev_dbg(&host
->pdev
->dev
, "%s(): desc %p, cookie %d\n", __func__
,
950 static void tmio_mmc_start_dma(struct tmio_mmc_host
*host
,
951 struct mmc_data
*data
)
953 if (data
->flags
& MMC_DATA_READ
) {
955 tmio_mmc_start_dma_rx(host
);
958 tmio_mmc_start_dma_tx(host
);
962 static void tmio_issue_tasklet_fn(unsigned long priv
)
964 struct tmio_mmc_host
*host
= (struct tmio_mmc_host
*)priv
;
965 struct dma_chan
*chan
= host
->chan_tx
;
967 chan
->device
->device_issue_pending(chan
);
970 static void tmio_tasklet_fn(unsigned long arg
)
972 struct tmio_mmc_host
*host
= (struct tmio_mmc_host
*)arg
;
975 spin_lock_irqsave(&host
->lock
, flags
);
980 if (host
->data
->flags
& MMC_DATA_READ
)
981 dma_unmap_sg(&host
->pdev
->dev
, host
->sg_ptr
, host
->dma_sglen
,
984 dma_unmap_sg(&host
->pdev
->dev
, host
->sg_ptr
, host
->dma_sglen
,
987 tmio_mmc_do_data_irq(host
);
989 spin_unlock_irqrestore(&host
->lock
, flags
);
992 /* It might be necessary to make filter MFD specific */
993 static bool tmio_mmc_filter(struct dma_chan
*chan
, void *arg
)
995 dev_dbg(chan
->device
->dev
, "%s: slave data %p\n", __func__
, arg
);
1000 static void tmio_mmc_request_dma(struct tmio_mmc_host
*host
,
1001 struct tmio_mmc_data
*pdata
)
1003 /* We can only either use DMA for both Tx and Rx or not use it at all */
1005 dma_cap_mask_t mask
;
1008 dma_cap_set(DMA_SLAVE
, mask
);
1010 host
->chan_tx
= dma_request_channel(mask
, tmio_mmc_filter
,
1011 pdata
->dma
->chan_priv_tx
);
1012 dev_dbg(&host
->pdev
->dev
, "%s: TX: got channel %p\n", __func__
,
1018 host
->chan_rx
= dma_request_channel(mask
, tmio_mmc_filter
,
1019 pdata
->dma
->chan_priv_rx
);
1020 dev_dbg(&host
->pdev
->dev
, "%s: RX: got channel %p\n", __func__
,
1023 if (!host
->chan_rx
) {
1024 dma_release_channel(host
->chan_tx
);
1025 host
->chan_tx
= NULL
;
1029 tasklet_init(&host
->dma_complete
, tmio_tasklet_fn
, (unsigned long)host
);
1030 tasklet_init(&host
->dma_issue
, tmio_issue_tasklet_fn
, (unsigned long)host
);
1032 tmio_mmc_enable_dma(host
, true);
1036 static void tmio_mmc_release_dma(struct tmio_mmc_host
*host
)
1038 if (host
->chan_tx
) {
1039 struct dma_chan
*chan
= host
->chan_tx
;
1040 host
->chan_tx
= NULL
;
1041 dma_release_channel(chan
);
1043 if (host
->chan_rx
) {
1044 struct dma_chan
*chan
= host
->chan_rx
;
1045 host
->chan_rx
= NULL
;
1046 dma_release_channel(chan
);
1050 static void tmio_check_bounce_buffer(struct tmio_mmc_host
*host
)
1054 static void tmio_mmc_start_dma(struct tmio_mmc_host
*host
,
1055 struct mmc_data
*data
)
1059 static void tmio_mmc_request_dma(struct tmio_mmc_host
*host
,
1060 struct tmio_mmc_data
*pdata
)
1062 host
->chan_tx
= NULL
;
1063 host
->chan_rx
= NULL
;
1066 static void tmio_mmc_release_dma(struct tmio_mmc_host
*host
)
1071 static int tmio_mmc_start_data(struct tmio_mmc_host
*host
,
1072 struct mmc_data
*data
)
1074 struct mfd_cell
*cell
= host
->pdev
->dev
.platform_data
;
1075 struct tmio_mmc_data
*pdata
= cell
->driver_data
;
1077 pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
1078 data
->blksz
, data
->blocks
);
1080 /* Some hardware cannot perform 2 byte requests in 4 bit mode */
1081 if (host
->mmc
->ios
.bus_width
== MMC_BUS_WIDTH_4
) {
1082 int blksz_2bytes
= pdata
->flags
& TMIO_MMC_BLKSZ_2BYTES
;
1084 if (data
->blksz
< 2 || (data
->blksz
< 4 && !blksz_2bytes
)) {
1085 pr_err("%s: %d byte block unsupported in 4 bit mode\n",
1086 mmc_hostname(host
->mmc
), data
->blksz
);
1091 tmio_mmc_init_sg(host
, data
);
1094 /* Set transfer length / blocksize */
1095 sd_ctrl_write16(host
, CTL_SD_XFER_LEN
, data
->blksz
);
1096 sd_ctrl_write16(host
, CTL_XFER_BLK_COUNT
, data
->blocks
);
1098 tmio_mmc_start_dma(host
, data
);
1103 /* Process requests from the MMC layer */
1104 static void tmio_mmc_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1106 struct tmio_mmc_host
*host
= mmc_priv(mmc
);
1110 pr_debug("request not null\n");
1112 host
->last_req_ts
= jiffies
;
1117 ret
= tmio_mmc_start_data(host
, mrq
->data
);
1122 ret
= tmio_mmc_start_command(host
, mrq
->cmd
);
1124 schedule_delayed_work(&host
->delayed_reset_work
,
1125 msecs_to_jiffies(2000));
1131 mrq
->cmd
->error
= ret
;
1132 mmc_request_done(mmc
, mrq
);
1135 /* Set MMC clock / power.
1136 * Note: This controller uses a simple divider scheme therefore it cannot
1137 * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
1138 * MMC wont run that fast, it has to be clocked at 12MHz which is the next
1141 static void tmio_mmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1143 struct tmio_mmc_host
*host
= mmc_priv(mmc
);
1146 tmio_mmc_set_clock(host
, ios
->clock
);
1148 /* Power sequence - OFF -> ON -> UP */
1149 switch (ios
->power_mode
) {
1150 case MMC_POWER_OFF
: /* power down SD bus */
1152 host
->set_pwr(host
->pdev
, 0);
1153 tmio_mmc_clk_stop(host
);
1155 case MMC_POWER_ON
: /* power up SD bus */
1157 host
->set_pwr(host
->pdev
, 1);
1159 case MMC_POWER_UP
: /* start bus clock */
1160 tmio_mmc_clk_start(host
);
1164 switch (ios
->bus_width
) {
1165 case MMC_BUS_WIDTH_1
:
1166 sd_ctrl_write16(host
, CTL_SD_MEM_CARD_OPT
, 0x80e0);
1168 case MMC_BUS_WIDTH_4
:
1169 sd_ctrl_write16(host
, CTL_SD_MEM_CARD_OPT
, 0x00e0);
1173 /* Let things settle. delay taken from winCE driver */
1177 static int tmio_mmc_get_ro(struct mmc_host
*mmc
)
1179 struct tmio_mmc_host
*host
= mmc_priv(mmc
);
1180 struct mfd_cell
*cell
= host
->pdev
->dev
.platform_data
;
1181 struct tmio_mmc_data
*pdata
= cell
->driver_data
;
1183 return ((pdata
->flags
& TMIO_MMC_WRPROTECT_DISABLE
) ||
1184 (sd_ctrl_read32(host
, CTL_STATUS
) & TMIO_STAT_WRPROTECT
)) ? 0 : 1;
1187 static int tmio_mmc_get_cd(struct mmc_host
*mmc
)
1189 struct tmio_mmc_host
*host
= mmc_priv(mmc
);
1190 struct mfd_cell
*cell
= host
->pdev
->dev
.platform_data
;
1191 struct tmio_mmc_data
*pdata
= cell
->driver_data
;
1196 return pdata
->get_cd(host
->pdev
);
1199 static const struct mmc_host_ops tmio_mmc_ops
= {
1200 .request
= tmio_mmc_request
,
1201 .set_ios
= tmio_mmc_set_ios
,
1202 .get_ro
= tmio_mmc_get_ro
,
1203 .get_cd
= tmio_mmc_get_cd
,
1204 .enable_sdio_irq
= tmio_mmc_enable_sdio_irq
,
1208 static int tmio_mmc_suspend(struct platform_device
*dev
, pm_message_t state
)
1210 struct mfd_cell
*cell
= (struct mfd_cell
*)dev
->dev
.platform_data
;
1211 struct mmc_host
*mmc
= platform_get_drvdata(dev
);
1214 ret
= mmc_suspend_host(mmc
);
1216 /* Tell MFD core it can disable us now.*/
1217 if (!ret
&& cell
->disable
)
1223 static int tmio_mmc_resume(struct platform_device
*dev
)
1225 struct mfd_cell
*cell
= (struct mfd_cell
*)dev
->dev
.platform_data
;
1226 struct mmc_host
*mmc
= platform_get_drvdata(dev
);
1229 /* Tell the MFD core we are ready to be enabled */
1231 ret
= cell
->resume(dev
);
1236 mmc_resume_host(mmc
);
1242 #define tmio_mmc_suspend NULL
1243 #define tmio_mmc_resume NULL
1246 static int __devinit
tmio_mmc_probe(struct platform_device
*dev
)
1248 struct mfd_cell
*cell
= (struct mfd_cell
*)dev
->dev
.platform_data
;
1249 struct tmio_mmc_data
*pdata
;
1250 struct resource
*res_ctl
;
1251 struct tmio_mmc_host
*host
;
1252 struct mmc_host
*mmc
;
1254 u32 irq_mask
= TMIO_MASK_CMD
;
1256 if (dev
->num_resources
!= 2)
1259 res_ctl
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
1263 pdata
= cell
->driver_data
;
1264 if (!pdata
|| !pdata
->hclk
)
1269 mmc
= mmc_alloc_host(sizeof(struct tmio_mmc_host
), &dev
->dev
);
1273 host
= mmc_priv(mmc
);
1276 platform_set_drvdata(dev
, mmc
);
1278 host
->set_pwr
= pdata
->set_pwr
;
1279 host
->set_clk_div
= pdata
->set_clk_div
;
1281 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
1282 host
->bus_shift
= resource_size(res_ctl
) >> 10;
1284 host
->ctl
= ioremap(res_ctl
->start
, resource_size(res_ctl
));
1288 mmc
->ops
= &tmio_mmc_ops
;
1289 mmc
->caps
= MMC_CAP_4_BIT_DATA
| pdata
->capabilities
;
1290 mmc
->f_max
= pdata
->hclk
;
1291 mmc
->f_min
= mmc
->f_max
/ 512;
1293 mmc
->max_blk_size
= 512;
1294 mmc
->max_blk_count
= (PAGE_CACHE_SIZE
/ mmc
->max_blk_size
) *
1296 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
1297 mmc
->max_seg_size
= mmc
->max_req_size
;
1298 if (pdata
->ocr_mask
)
1299 mmc
->ocr_avail
= pdata
->ocr_mask
;
1301 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
1303 /* Tell the MFD core we are ready to be enabled */
1305 ret
= cell
->enable(dev
);
1310 tmio_mmc_clk_stop(host
);
1313 ret
= platform_get_irq(dev
, 0);
1319 disable_mmc_irqs(host
, TMIO_MASK_ALL
);
1320 if (pdata
->flags
& TMIO_MMC_SDIO_IRQ
)
1321 tmio_mmc_enable_sdio_irq(mmc
, 0);
1323 ret
= request_irq(host
->irq
, tmio_mmc_irq
, IRQF_DISABLED
|
1324 IRQF_TRIGGER_FALLING
, dev_name(&dev
->dev
), host
);
1328 spin_lock_init(&host
->lock
);
1330 /* Init delayed work for request timeouts */
1331 INIT_DELAYED_WORK(&host
->delayed_reset_work
, tmio_mmc_reset_work
);
1333 /* See if we also get DMA */
1334 tmio_mmc_request_dma(host
, pdata
);
1338 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host
->mmc
),
1339 (unsigned long)host
->ctl
, host
->irq
);
1341 /* Unmask the IRQs we want to know about */
1343 irq_mask
|= TMIO_MASK_READOP
;
1345 irq_mask
|= TMIO_MASK_WRITEOP
;
1346 enable_mmc_irqs(host
, irq_mask
);
1361 static int __devexit
tmio_mmc_remove(struct platform_device
*dev
)
1363 struct mfd_cell
*cell
= (struct mfd_cell
*)dev
->dev
.platform_data
;
1364 struct mmc_host
*mmc
= platform_get_drvdata(dev
);
1366 platform_set_drvdata(dev
, NULL
);
1369 struct tmio_mmc_host
*host
= mmc_priv(mmc
);
1370 mmc_remove_host(mmc
);
1371 cancel_delayed_work_sync(&host
->delayed_reset_work
);
1372 tmio_mmc_release_dma(host
);
1373 free_irq(host
->irq
, host
);
1383 /* ------------------- device registration ----------------------- */
1385 static struct platform_driver tmio_mmc_driver
= {
1388 .owner
= THIS_MODULE
,
1390 .probe
= tmio_mmc_probe
,
1391 .remove
= __devexit_p(tmio_mmc_remove
),
1392 .suspend
= tmio_mmc_suspend
,
1393 .resume
= tmio_mmc_resume
,
1397 static int __init
tmio_mmc_init(void)
1399 return platform_driver_register(&tmio_mmc_driver
);
1402 static void __exit
tmio_mmc_exit(void)
1404 platform_driver_unregister(&tmio_mmc_driver
);
1407 module_init(tmio_mmc_init
);
1408 module_exit(tmio_mmc_exit
);
1410 MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
1411 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
1412 MODULE_LICENSE("GPL v2");
1413 MODULE_ALIAS("platform:tmio-mmc");