2 * linux/drivers/mmc/host/omap.c
4 * Copyright (C) 2004 Nokia Corporation
5 * Written by Tuukka Tikkanen and Juha Yrjölä<juha.yrjola@nokia.com>
6 * Misc hacks here and there by Tony Lindgren <tony@atomide.com>
7 * Other hacks (DMA, SD, etc) by David Brownell
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/ioport.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/delay.h>
22 #include <linux/spinlock.h>
23 #include <linux/timer.h>
24 #include <linux/mmc/host.h>
25 #include <linux/mmc/card.h>
26 #include <linux/clk.h>
27 #include <linux/scatterlist.h>
28 #include <linux/i2c/tps65010.h>
32 #include <asm/mach-types.h>
34 #include <asm/arch/board.h>
35 #include <asm/arch/gpio.h>
36 #include <asm/arch/dma.h>
37 #include <asm/arch/mux.h>
38 #include <asm/arch/fpga.h>
40 #define OMAP_MMC_REG_CMD 0x00
41 #define OMAP_MMC_REG_ARGL 0x04
42 #define OMAP_MMC_REG_ARGH 0x08
43 #define OMAP_MMC_REG_CON 0x0c
44 #define OMAP_MMC_REG_STAT 0x10
45 #define OMAP_MMC_REG_IE 0x14
46 #define OMAP_MMC_REG_CTO 0x18
47 #define OMAP_MMC_REG_DTO 0x1c
48 #define OMAP_MMC_REG_DATA 0x20
49 #define OMAP_MMC_REG_BLEN 0x24
50 #define OMAP_MMC_REG_NBLK 0x28
51 #define OMAP_MMC_REG_BUF 0x2c
52 #define OMAP_MMC_REG_SDIO 0x34
53 #define OMAP_MMC_REG_REV 0x3c
54 #define OMAP_MMC_REG_RSP0 0x40
55 #define OMAP_MMC_REG_RSP1 0x44
56 #define OMAP_MMC_REG_RSP2 0x48
57 #define OMAP_MMC_REG_RSP3 0x4c
58 #define OMAP_MMC_REG_RSP4 0x50
59 #define OMAP_MMC_REG_RSP5 0x54
60 #define OMAP_MMC_REG_RSP6 0x58
61 #define OMAP_MMC_REG_RSP7 0x5c
62 #define OMAP_MMC_REG_IOSR 0x60
63 #define OMAP_MMC_REG_SYSC 0x64
64 #define OMAP_MMC_REG_SYSS 0x68
66 #define OMAP_MMC_STAT_CARD_ERR (1 << 14)
67 #define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
68 #define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
69 #define OMAP_MMC_STAT_A_EMPTY (1 << 11)
70 #define OMAP_MMC_STAT_A_FULL (1 << 10)
71 #define OMAP_MMC_STAT_CMD_CRC (1 << 8)
72 #define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
73 #define OMAP_MMC_STAT_DATA_CRC (1 << 6)
74 #define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
75 #define OMAP_MMC_STAT_END_BUSY (1 << 4)
76 #define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
77 #define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
78 #define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
80 #define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG_##reg)
81 #define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG_##reg)
86 #define OMAP_MMC_CMDTYPE_BC 0
87 #define OMAP_MMC_CMDTYPE_BCR 1
88 #define OMAP_MMC_CMDTYPE_AC 2
89 #define OMAP_MMC_CMDTYPE_ADTC 3
92 #define DRIVER_NAME "mmci-omap"
94 /* Specifies how often in millisecs to poll for card status changes
95 * when the cover switch is open */
96 #define OMAP_MMC_SWITCH_POLL_DELAY 500
98 struct mmc_omap_host
{
101 struct mmc_request
* mrq
;
102 struct mmc_command
* cmd
;
103 struct mmc_data
* data
;
104 struct mmc_host
* mmc
;
106 unsigned char id
; /* 16xx chips have 2 MMC blocks */
109 struct resource
*mem_res
;
110 void __iomem
*virt_base
;
111 unsigned int phys_base
;
113 unsigned char bus_mode
;
114 unsigned char hw_bus_mode
;
119 u32 buffer_bytes_left
;
120 u32 total_bytes_left
;
123 unsigned brs_received
:1, dma_done
:1;
124 unsigned dma_is_read
:1;
125 unsigned dma_in_use
:1;
128 struct timer_list dma_timer
;
134 struct work_struct switch_work
;
135 struct timer_list switch_timer
;
136 int switch_last_state
;
140 mmc_omap_start_command(struct mmc_omap_host
*host
, struct mmc_command
*cmd
)
151 /* Our hardware needs to know exact type */
152 switch (mmc_resp_type(cmd
)) {
157 /* resp 1, 1b, 6, 7 */
167 dev_err(mmc_dev(host
->mmc
), "Invalid response type: %04x\n", mmc_resp_type(cmd
));
171 if (mmc_cmd_type(cmd
) == MMC_CMD_ADTC
) {
172 cmdtype
= OMAP_MMC_CMDTYPE_ADTC
;
173 } else if (mmc_cmd_type(cmd
) == MMC_CMD_BC
) {
174 cmdtype
= OMAP_MMC_CMDTYPE_BC
;
175 } else if (mmc_cmd_type(cmd
) == MMC_CMD_BCR
) {
176 cmdtype
= OMAP_MMC_CMDTYPE_BCR
;
178 cmdtype
= OMAP_MMC_CMDTYPE_AC
;
181 cmdreg
= cmd
->opcode
| (resptype
<< 8) | (cmdtype
<< 12);
183 if (host
->bus_mode
== MMC_BUSMODE_OPENDRAIN
)
186 if (cmd
->flags
& MMC_RSP_BUSY
)
189 if (host
->data
&& !(host
->data
->flags
& MMC_DATA_WRITE
))
192 clk_enable(host
->fclk
);
194 OMAP_MMC_WRITE(host
, CTO
, 200);
195 OMAP_MMC_WRITE(host
, ARGL
, cmd
->arg
& 0xffff);
196 OMAP_MMC_WRITE(host
, ARGH
, cmd
->arg
>> 16);
197 OMAP_MMC_WRITE(host
, IE
,
198 OMAP_MMC_STAT_A_EMPTY
| OMAP_MMC_STAT_A_FULL
|
199 OMAP_MMC_STAT_CMD_CRC
| OMAP_MMC_STAT_CMD_TOUT
|
200 OMAP_MMC_STAT_DATA_CRC
| OMAP_MMC_STAT_DATA_TOUT
|
201 OMAP_MMC_STAT_END_OF_CMD
| OMAP_MMC_STAT_CARD_ERR
|
202 OMAP_MMC_STAT_END_OF_DATA
);
203 OMAP_MMC_WRITE(host
, CMD
, cmdreg
);
207 mmc_omap_xfer_done(struct mmc_omap_host
*host
, struct mmc_data
*data
)
209 if (host
->dma_in_use
) {
210 enum dma_data_direction dma_data_dir
;
212 BUG_ON(host
->dma_ch
< 0);
214 omap_stop_dma(host
->dma_ch
);
215 /* Release DMA channel lazily */
216 mod_timer(&host
->dma_timer
, jiffies
+ HZ
);
217 if (data
->flags
& MMC_DATA_WRITE
)
218 dma_data_dir
= DMA_TO_DEVICE
;
220 dma_data_dir
= DMA_FROM_DEVICE
;
221 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
, host
->sg_len
,
226 clk_disable(host
->fclk
);
228 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
229 * dozens of requests until the card finishes writing data.
230 * It'd be cheaper to just wait till an EOFB interrupt arrives...
235 mmc_request_done(host
->mmc
, data
->mrq
);
239 mmc_omap_start_command(host
, data
->stop
);
243 mmc_omap_end_of_data(struct mmc_omap_host
*host
, struct mmc_data
*data
)
248 if (!host
->dma_in_use
) {
249 mmc_omap_xfer_done(host
, data
);
253 spin_lock_irqsave(&host
->dma_lock
, flags
);
257 host
->brs_received
= 1;
258 spin_unlock_irqrestore(&host
->dma_lock
, flags
);
260 mmc_omap_xfer_done(host
, data
);
264 mmc_omap_dma_timer(unsigned long data
)
266 struct mmc_omap_host
*host
= (struct mmc_omap_host
*) data
;
268 BUG_ON(host
->dma_ch
< 0);
269 omap_free_dma(host
->dma_ch
);
274 mmc_omap_dma_done(struct mmc_omap_host
*host
, struct mmc_data
*data
)
280 spin_lock_irqsave(&host
->dma_lock
, flags
);
281 if (host
->brs_received
)
285 spin_unlock_irqrestore(&host
->dma_lock
, flags
);
287 mmc_omap_xfer_done(host
, data
);
291 mmc_omap_cmd_done(struct mmc_omap_host
*host
, struct mmc_command
*cmd
)
295 if (cmd
->flags
& MMC_RSP_PRESENT
) {
296 if (cmd
->flags
& MMC_RSP_136
) {
297 /* response type 2 */
299 OMAP_MMC_READ(host
, RSP0
) |
300 (OMAP_MMC_READ(host
, RSP1
) << 16);
302 OMAP_MMC_READ(host
, RSP2
) |
303 (OMAP_MMC_READ(host
, RSP3
) << 16);
305 OMAP_MMC_READ(host
, RSP4
) |
306 (OMAP_MMC_READ(host
, RSP5
) << 16);
308 OMAP_MMC_READ(host
, RSP6
) |
309 (OMAP_MMC_READ(host
, RSP7
) << 16);
311 /* response types 1, 1b, 3, 4, 5, 6 */
313 OMAP_MMC_READ(host
, RSP6
) |
314 (OMAP_MMC_READ(host
, RSP7
) << 16);
318 if (host
->data
== NULL
|| cmd
->error
) {
320 clk_disable(host
->fclk
);
321 mmc_request_done(host
->mmc
, cmd
->mrq
);
327 mmc_omap_sg_to_buf(struct mmc_omap_host
*host
)
329 struct scatterlist
*sg
;
331 sg
= host
->data
->sg
+ host
->sg_idx
;
332 host
->buffer_bytes_left
= sg
->length
;
333 host
->buffer
= sg_virt(sg
);
334 if (host
->buffer_bytes_left
> host
->total_bytes_left
)
335 host
->buffer_bytes_left
= host
->total_bytes_left
;
340 mmc_omap_xfer_data(struct mmc_omap_host
*host
, int write
)
344 if (host
->buffer_bytes_left
== 0) {
346 BUG_ON(host
->sg_idx
== host
->sg_len
);
347 mmc_omap_sg_to_buf(host
);
350 if (n
> host
->buffer_bytes_left
)
351 n
= host
->buffer_bytes_left
;
352 host
->buffer_bytes_left
-= n
;
353 host
->total_bytes_left
-= n
;
354 host
->data
->bytes_xfered
+= n
;
357 __raw_writesw(host
->virt_base
+ OMAP_MMC_REG_DATA
, host
->buffer
, n
);
359 __raw_readsw(host
->virt_base
+ OMAP_MMC_REG_DATA
, host
->buffer
, n
);
363 static inline void mmc_omap_report_irq(u16 status
)
365 static const char *mmc_omap_status_bits
[] = {
366 "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
367 "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
371 for (i
= 0; i
< ARRAY_SIZE(mmc_omap_status_bits
); i
++)
372 if (status
& (1 << i
)) {
375 printk("%s", mmc_omap_status_bits
[i
]);
380 static irqreturn_t
mmc_omap_irq(int irq
, void *dev_id
)
382 struct mmc_omap_host
* host
= (struct mmc_omap_host
*)dev_id
;
388 if (host
->cmd
== NULL
&& host
->data
== NULL
) {
389 status
= OMAP_MMC_READ(host
, STAT
);
390 dev_info(mmc_dev(host
->mmc
),"spurious irq 0x%04x\n", status
);
392 OMAP_MMC_WRITE(host
, STAT
, status
);
393 OMAP_MMC_WRITE(host
, IE
, 0);
402 while ((status
= OMAP_MMC_READ(host
, STAT
)) != 0) {
403 OMAP_MMC_WRITE(host
, STAT
, status
);
404 #ifdef CONFIG_MMC_DEBUG
405 dev_dbg(mmc_dev(host
->mmc
), "MMC IRQ %04x (CMD %d): ",
406 status
, host
->cmd
!= NULL
? host
->cmd
->opcode
: -1);
407 mmc_omap_report_irq(status
);
410 if (host
->total_bytes_left
) {
411 if ((status
& OMAP_MMC_STAT_A_FULL
) ||
412 (status
& OMAP_MMC_STAT_END_OF_DATA
))
413 mmc_omap_xfer_data(host
, 0);
414 if (status
& OMAP_MMC_STAT_A_EMPTY
)
415 mmc_omap_xfer_data(host
, 1);
418 if (status
& OMAP_MMC_STAT_END_OF_DATA
) {
422 if (status
& OMAP_MMC_STAT_DATA_TOUT
) {
423 dev_dbg(mmc_dev(host
->mmc
), "data timeout\n");
425 host
->data
->error
= -ETIMEDOUT
;
430 if (status
& OMAP_MMC_STAT_DATA_CRC
) {
432 host
->data
->error
= -EILSEQ
;
433 dev_dbg(mmc_dev(host
->mmc
),
434 "data CRC error, bytes left %d\n",
435 host
->total_bytes_left
);
438 dev_dbg(mmc_dev(host
->mmc
), "data CRC error\n");
442 if (status
& OMAP_MMC_STAT_CMD_TOUT
) {
443 /* Timeouts are routine with some commands */
445 dev_err(mmc_dev(host
->mmc
),
446 "command timeout, CMD %d\n",
448 host
->cmd
->error
= -ETIMEDOUT
;
453 if (status
& OMAP_MMC_STAT_CMD_CRC
) {
455 dev_err(mmc_dev(host
->mmc
),
456 "command CRC error (CMD%d, arg 0x%08x)\n",
457 host
->cmd
->opcode
, host
->cmd
->arg
);
458 host
->cmd
->error
= -EILSEQ
;
461 dev_err(mmc_dev(host
->mmc
),
462 "command CRC error without cmd?\n");
465 if (status
& OMAP_MMC_STAT_CARD_ERR
) {
466 dev_dbg(mmc_dev(host
->mmc
),
467 "ignoring card status error (CMD%d)\n",
473 * NOTE: On 1610 the END_OF_CMD may come too early when
476 if ((status
& OMAP_MMC_STAT_END_OF_CMD
) &&
477 (!(status
& OMAP_MMC_STAT_A_EMPTY
))) {
483 mmc_omap_cmd_done(host
, host
->cmd
);
486 mmc_omap_xfer_done(host
, host
->data
);
487 else if (end_transfer
)
488 mmc_omap_end_of_data(host
, host
->data
);
493 /* Prepare to transfer the next segment of a scatterlist */
495 mmc_omap_prepare_dma(struct mmc_omap_host
*host
, struct mmc_data
*data
)
497 int dma_ch
= host
->dma_ch
;
498 unsigned long data_addr
;
501 struct scatterlist
*sg
= &data
->sg
[host
->sg_idx
];
506 data_addr
= host
->phys_base
+ OMAP_MMC_REG_DATA
;
508 count
= sg_dma_len(sg
);
510 if ((data
->blocks
== 1) && (count
> data
->blksz
))
513 host
->dma_len
= count
;
515 /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
516 * Use 16 or 32 word frames when the blocksize is at least that large.
517 * Blocksize is usually 512 bytes; but not for some SD reads.
519 if (cpu_is_omap15xx() && frame
> 32)
526 if (!(data
->flags
& MMC_DATA_WRITE
)) {
527 buf
= 0x800f | ((frame
- 1) << 8);
529 if (cpu_class_is_omap1()) {
530 src_port
= OMAP_DMA_PORT_TIPB
;
531 dst_port
= OMAP_DMA_PORT_EMIFF
;
533 if (cpu_is_omap24xx())
534 sync_dev
= OMAP24XX_DMA_MMC1_RX
;
536 omap_set_dma_src_params(dma_ch
, src_port
,
537 OMAP_DMA_AMODE_CONSTANT
,
539 omap_set_dma_dest_params(dma_ch
, dst_port
,
540 OMAP_DMA_AMODE_POST_INC
,
541 sg_dma_address(sg
), 0, 0);
542 omap_set_dma_dest_data_pack(dma_ch
, 1);
543 omap_set_dma_dest_burst_mode(dma_ch
, OMAP_DMA_DATA_BURST_4
);
545 buf
= 0x0f80 | ((frame
- 1) << 0);
547 if (cpu_class_is_omap1()) {
548 src_port
= OMAP_DMA_PORT_EMIFF
;
549 dst_port
= OMAP_DMA_PORT_TIPB
;
551 if (cpu_is_omap24xx())
552 sync_dev
= OMAP24XX_DMA_MMC1_TX
;
554 omap_set_dma_dest_params(dma_ch
, dst_port
,
555 OMAP_DMA_AMODE_CONSTANT
,
557 omap_set_dma_src_params(dma_ch
, src_port
,
558 OMAP_DMA_AMODE_POST_INC
,
559 sg_dma_address(sg
), 0, 0);
560 omap_set_dma_src_data_pack(dma_ch
, 1);
561 omap_set_dma_src_burst_mode(dma_ch
, OMAP_DMA_DATA_BURST_4
);
564 /* Max limit for DMA frame count is 0xffff */
565 BUG_ON(count
> 0xffff);
567 OMAP_MMC_WRITE(host
, BUF
, buf
);
568 omap_set_dma_transfer_params(dma_ch
, OMAP_DMA_DATA_TYPE_S16
,
569 frame
, count
, OMAP_DMA_SYNC_FRAME
,
573 /* A scatterlist segment completed */
574 static void mmc_omap_dma_cb(int lch
, u16 ch_status
, void *data
)
576 struct mmc_omap_host
*host
= (struct mmc_omap_host
*) data
;
577 struct mmc_data
*mmcdat
= host
->data
;
579 if (unlikely(host
->dma_ch
< 0)) {
580 dev_err(mmc_dev(host
->mmc
),
581 "DMA callback while DMA not enabled\n");
584 /* FIXME: We really should do something to _handle_ the errors */
585 if (ch_status
& OMAP1_DMA_TOUT_IRQ
) {
586 dev_err(mmc_dev(host
->mmc
),"DMA timeout\n");
589 if (ch_status
& OMAP_DMA_DROP_IRQ
) {
590 dev_err(mmc_dev(host
->mmc
), "DMA sync error\n");
593 if (!(ch_status
& OMAP_DMA_BLOCK_IRQ
)) {
596 mmcdat
->bytes_xfered
+= host
->dma_len
;
598 if (host
->sg_idx
< host
->sg_len
) {
599 mmc_omap_prepare_dma(host
, host
->data
);
600 omap_start_dma(host
->dma_ch
);
602 mmc_omap_dma_done(host
, host
->data
);
605 static int mmc_omap_get_dma_channel(struct mmc_omap_host
*host
, struct mmc_data
*data
)
607 const char *dev_name
;
608 int sync_dev
, dma_ch
, is_read
, r
;
610 is_read
= !(data
->flags
& MMC_DATA_WRITE
);
611 del_timer_sync(&host
->dma_timer
);
612 if (host
->dma_ch
>= 0) {
613 if (is_read
== host
->dma_is_read
)
615 omap_free_dma(host
->dma_ch
);
621 sync_dev
= OMAP_DMA_MMC_RX
;
622 dev_name
= "MMC1 read";
624 sync_dev
= OMAP_DMA_MMC2_RX
;
625 dev_name
= "MMC2 read";
629 sync_dev
= OMAP_DMA_MMC_TX
;
630 dev_name
= "MMC1 write";
632 sync_dev
= OMAP_DMA_MMC2_TX
;
633 dev_name
= "MMC2 write";
636 r
= omap_request_dma(sync_dev
, dev_name
, mmc_omap_dma_cb
,
639 dev_dbg(mmc_dev(host
->mmc
), "omap_request_dma() failed with %d\n", r
);
642 host
->dma_ch
= dma_ch
;
643 host
->dma_is_read
= is_read
;
648 static inline void set_cmd_timeout(struct mmc_omap_host
*host
, struct mmc_request
*req
)
652 reg
= OMAP_MMC_READ(host
, SDIO
);
654 OMAP_MMC_WRITE(host
, SDIO
, reg
);
655 /* Set maximum timeout */
656 OMAP_MMC_WRITE(host
, CTO
, 0xff);
659 static inline void set_data_timeout(struct mmc_omap_host
*host
, struct mmc_request
*req
)
664 /* Convert ns to clock cycles by assuming 20MHz frequency
665 * 1 cycle at 20MHz = 500 ns
667 timeout
= req
->data
->timeout_clks
+ req
->data
->timeout_ns
/ 500;
669 /* Check if we need to use timeout multiplier register */
670 reg
= OMAP_MMC_READ(host
, SDIO
);
671 if (timeout
> 0xffff) {
676 OMAP_MMC_WRITE(host
, SDIO
, reg
);
677 OMAP_MMC_WRITE(host
, DTO
, timeout
);
681 mmc_omap_prepare_data(struct mmc_omap_host
*host
, struct mmc_request
*req
)
683 struct mmc_data
*data
= req
->data
;
684 int i
, use_dma
, block_size
;
689 OMAP_MMC_WRITE(host
, BLEN
, 0);
690 OMAP_MMC_WRITE(host
, NBLK
, 0);
691 OMAP_MMC_WRITE(host
, BUF
, 0);
692 host
->dma_in_use
= 0;
693 set_cmd_timeout(host
, req
);
697 block_size
= data
->blksz
;
699 OMAP_MMC_WRITE(host
, NBLK
, data
->blocks
- 1);
700 OMAP_MMC_WRITE(host
, BLEN
, block_size
- 1);
701 set_data_timeout(host
, req
);
703 /* cope with calling layer confusion; it issues "single
704 * block" writes using multi-block scatterlists.
706 sg_len
= (data
->blocks
== 1) ? 1 : data
->sg_len
;
708 /* Only do DMA for entire blocks */
709 use_dma
= host
->use_dma
;
711 for (i
= 0; i
< sg_len
; i
++) {
712 if ((data
->sg
[i
].length
% block_size
) != 0) {
721 if (mmc_omap_get_dma_channel(host
, data
) == 0) {
722 enum dma_data_direction dma_data_dir
;
724 if (data
->flags
& MMC_DATA_WRITE
)
725 dma_data_dir
= DMA_TO_DEVICE
;
727 dma_data_dir
= DMA_FROM_DEVICE
;
729 host
->sg_len
= dma_map_sg(mmc_dev(host
->mmc
), data
->sg
,
730 sg_len
, dma_data_dir
);
731 host
->total_bytes_left
= 0;
732 mmc_omap_prepare_dma(host
, req
->data
);
733 host
->brs_received
= 0;
735 host
->dma_in_use
= 1;
742 OMAP_MMC_WRITE(host
, BUF
, 0x1f1f);
743 host
->total_bytes_left
= data
->blocks
* block_size
;
744 host
->sg_len
= sg_len
;
745 mmc_omap_sg_to_buf(host
);
746 host
->dma_in_use
= 0;
750 static void mmc_omap_request(struct mmc_host
*mmc
, struct mmc_request
*req
)
752 struct mmc_omap_host
*host
= mmc_priv(mmc
);
754 WARN_ON(host
->mrq
!= NULL
);
758 /* only touch fifo AFTER the controller readies it */
759 mmc_omap_prepare_data(host
, req
);
760 mmc_omap_start_command(host
, req
->cmd
);
761 if (host
->dma_in_use
)
762 omap_start_dma(host
->dma_ch
);
765 static void innovator_fpga_socket_power(int on
)
767 #if defined(CONFIG_MACH_OMAP_INNOVATOR) && defined(CONFIG_ARCH_OMAP15XX)
769 fpga_write(fpga_read(OMAP1510_FPGA_POWER
) | (1 << 3),
770 OMAP1510_FPGA_POWER
);
772 fpga_write(fpga_read(OMAP1510_FPGA_POWER
) & ~(1 << 3),
773 OMAP1510_FPGA_POWER
);
779 * Turn the socket power on/off. Innovator uses FPGA, most boards
782 static void mmc_omap_power(struct mmc_omap_host
*host
, int on
)
785 if (machine_is_omap_innovator())
786 innovator_fpga_socket_power(1);
787 else if (machine_is_omap_h2())
788 tps65010_set_gpio_out_value(GPIO3
, HIGH
);
789 else if (machine_is_omap_h3())
790 /* GPIO 4 of TPS65010 sends SD_EN signal */
791 tps65010_set_gpio_out_value(GPIO4
, HIGH
);
792 else if (cpu_is_omap24xx()) {
793 u16 reg
= OMAP_MMC_READ(host
, CON
);
794 OMAP_MMC_WRITE(host
, CON
, reg
| (1 << 11));
796 if (host
->power_pin
>= 0)
797 omap_set_gpio_dataout(host
->power_pin
, 1);
799 if (machine_is_omap_innovator())
800 innovator_fpga_socket_power(0);
801 else if (machine_is_omap_h2())
802 tps65010_set_gpio_out_value(GPIO3
, LOW
);
803 else if (machine_is_omap_h3())
804 tps65010_set_gpio_out_value(GPIO4
, LOW
);
805 else if (cpu_is_omap24xx()) {
806 u16 reg
= OMAP_MMC_READ(host
, CON
);
807 OMAP_MMC_WRITE(host
, CON
, reg
& ~(1 << 11));
809 if (host
->power_pin
>= 0)
810 omap_set_gpio_dataout(host
->power_pin
, 0);
814 static int mmc_omap_calc_divisor(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
816 struct mmc_omap_host
*host
= mmc_priv(mmc
);
817 int func_clk_rate
= clk_get_rate(host
->fclk
);
823 dsor
= func_clk_rate
/ ios
->clock
;
827 if (func_clk_rate
/ dsor
> ios
->clock
)
833 if (ios
->bus_width
== MMC_BUS_WIDTH_4
)
839 static void mmc_omap_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
841 struct mmc_omap_host
*host
= mmc_priv(mmc
);
845 dsor
= mmc_omap_calc_divisor(mmc
, ios
);
846 host
->bus_mode
= ios
->bus_mode
;
847 host
->hw_bus_mode
= host
->bus_mode
;
849 switch (ios
->power_mode
) {
851 mmc_omap_power(host
, 0);
854 /* Cannot touch dsor yet, just power up MMC */
855 mmc_omap_power(host
, 1);
862 clk_enable(host
->fclk
);
864 /* On insanely high arm_per frequencies something sometimes
865 * goes somehow out of sync, and the POW bit is not being set,
866 * which results in the while loop below getting stuck.
867 * Writing to the CON register twice seems to do the trick. */
868 for (i
= 0; i
< 2; i
++)
869 OMAP_MMC_WRITE(host
, CON
, dsor
);
870 if (ios
->power_mode
== MMC_POWER_ON
) {
871 /* Send clock cycles, poll completion */
872 OMAP_MMC_WRITE(host
, IE
, 0);
873 OMAP_MMC_WRITE(host
, STAT
, 0xffff);
874 OMAP_MMC_WRITE(host
, CMD
, 1 << 7);
875 while ((OMAP_MMC_READ(host
, STAT
) & 1) == 0);
876 OMAP_MMC_WRITE(host
, STAT
, 1);
878 clk_disable(host
->fclk
);
881 static int mmc_omap_get_ro(struct mmc_host
*mmc
)
883 struct mmc_omap_host
*host
= mmc_priv(mmc
);
885 return host
->wp_pin
&& omap_get_gpio_datain(host
->wp_pin
);
888 static const struct mmc_host_ops mmc_omap_ops
= {
889 .request
= mmc_omap_request
,
890 .set_ios
= mmc_omap_set_ios
,
891 .get_ro
= mmc_omap_get_ro
,
894 static int __init
mmc_omap_probe(struct platform_device
*pdev
)
896 struct omap_mmc_conf
*minfo
= pdev
->dev
.platform_data
;
897 struct mmc_host
*mmc
;
898 struct mmc_omap_host
*host
= NULL
;
899 struct resource
*res
;
904 dev_err(&pdev
->dev
, "platform data missing\n");
908 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
909 irq
= platform_get_irq(pdev
, 0);
910 if (res
== NULL
|| irq
< 0)
913 res
= request_mem_region(res
->start
, res
->end
- res
->start
+ 1,
918 mmc
= mmc_alloc_host(sizeof(struct mmc_omap_host
), &pdev
->dev
);
921 goto err_free_mem_region
;
924 host
= mmc_priv(mmc
);
927 spin_lock_init(&host
->dma_lock
);
928 init_timer(&host
->dma_timer
);
929 host
->dma_timer
.function
= mmc_omap_dma_timer
;
930 host
->dma_timer
.data
= (unsigned long) host
;
936 if (cpu_is_omap24xx()) {
937 host
->iclk
= clk_get(&pdev
->dev
, "mmc_ick");
938 if (IS_ERR(host
->iclk
))
939 goto err_free_mmc_host
;
940 clk_enable(host
->iclk
);
943 if (!cpu_is_omap24xx())
944 host
->fclk
= clk_get(&pdev
->dev
, "mmc_ck");
946 host
->fclk
= clk_get(&pdev
->dev
, "mmc_fck");
948 if (IS_ERR(host
->fclk
)) {
949 ret
= PTR_ERR(host
->fclk
);
954 * Also, use minfo->cover to decide how to manage
955 * the card detect sensing.
957 host
->power_pin
= minfo
->power_pin
;
958 host
->wp_pin
= minfo
->wp_pin
;
963 host
->phys_base
= host
->mem_res
->start
;
964 host
->virt_base
= (void __iomem
*) IO_ADDRESS(host
->phys_base
);
966 mmc
->ops
= &mmc_omap_ops
;
968 mmc
->f_max
= 24000000;
969 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
970 mmc
->caps
= MMC_CAP_MULTIWRITE
;
973 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
975 /* Use scatterlist DMA to reduce per-transfer costs.
976 * NOTE max_seg_size assumption that small blocks aren't
977 * normally used (except e.g. for reading SD registers).
979 mmc
->max_phys_segs
= 32;
980 mmc
->max_hw_segs
= 32;
981 mmc
->max_blk_size
= 2048; /* BLEN is 11 bits (+1) */
982 mmc
->max_blk_count
= 2048; /* NBLK is 11 bits (+1) */
983 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
984 mmc
->max_seg_size
= mmc
->max_req_size
;
986 if (host
->power_pin
>= 0) {
987 if ((ret
= omap_request_gpio(host
->power_pin
)) != 0) {
988 dev_err(mmc_dev(host
->mmc
),
989 "Unable to get GPIO pin for MMC power\n");
992 omap_set_gpio_direction(host
->power_pin
, 0);
995 ret
= request_irq(host
->irq
, mmc_omap_irq
, 0, DRIVER_NAME
, host
);
997 goto err_free_power_gpio
;
999 host
->dev
= &pdev
->dev
;
1000 platform_set_drvdata(pdev
, host
);
1006 /* FIXME: Free other resources too. */
1008 if (host
->iclk
&& !IS_ERR(host
->iclk
))
1009 clk_put(host
->iclk
);
1010 if (host
->fclk
&& !IS_ERR(host
->fclk
))
1011 clk_put(host
->fclk
);
1012 mmc_free_host(host
->mmc
);
1014 err_free_power_gpio
:
1015 if (host
->power_pin
>= 0)
1016 omap_free_gpio(host
->power_pin
);
1018 clk_put(host
->fclk
);
1020 if (host
->iclk
!= NULL
) {
1021 clk_disable(host
->iclk
);
1022 clk_put(host
->iclk
);
1025 mmc_free_host(host
->mmc
);
1026 err_free_mem_region
:
1027 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
1031 static int mmc_omap_remove(struct platform_device
*pdev
)
1033 struct mmc_omap_host
*host
= platform_get_drvdata(pdev
);
1035 platform_set_drvdata(pdev
, NULL
);
1037 BUG_ON(host
== NULL
);
1039 mmc_remove_host(host
->mmc
);
1040 free_irq(host
->irq
, host
);
1042 if (host
->power_pin
>= 0)
1043 omap_free_gpio(host
->power_pin
);
1044 if (host
->iclk
&& !IS_ERR(host
->iclk
))
1045 clk_put(host
->iclk
);
1046 if (host
->fclk
&& !IS_ERR(host
->fclk
))
1047 clk_put(host
->fclk
);
1049 release_mem_region(pdev
->resource
[0].start
,
1050 pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1);
1052 mmc_free_host(host
->mmc
);
1058 static int mmc_omap_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
1061 struct mmc_omap_host
*host
= platform_get_drvdata(pdev
);
1063 if (host
&& host
->suspended
)
1067 ret
= mmc_suspend_host(host
->mmc
, mesg
);
1069 host
->suspended
= 1;
1074 static int mmc_omap_resume(struct platform_device
*pdev
)
1077 struct mmc_omap_host
*host
= platform_get_drvdata(pdev
);
1079 if (host
&& !host
->suspended
)
1083 ret
= mmc_resume_host(host
->mmc
);
1085 host
->suspended
= 0;
1091 #define mmc_omap_suspend NULL
1092 #define mmc_omap_resume NULL
1095 static struct platform_driver mmc_omap_driver
= {
1096 .probe
= mmc_omap_probe
,
1097 .remove
= mmc_omap_remove
,
1098 .suspend
= mmc_omap_suspend
,
1099 .resume
= mmc_omap_resume
,
1101 .name
= DRIVER_NAME
,
1102 .owner
= THIS_MODULE
,
1106 static int __init
mmc_omap_init(void)
1108 return platform_driver_register(&mmc_omap_driver
);
1111 static void __exit
mmc_omap_exit(void)
1113 platform_driver_unregister(&mmc_omap_driver
);
1116 module_init(mmc_omap_init
);
1117 module_exit(mmc_omap_exit
);
1119 MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1120 MODULE_LICENSE("GPL");
1121 MODULE_ALIAS("platform:" DRIVER_NAME
);
1122 MODULE_AUTHOR("Juha Yrjölä");