2 * drivers/mmc/host/omap_hsmmc.c
4 * Driver for OMAP2430/3430 MMC controller.
6 * Copyright (C) 2007 Texas Instruments.
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/platform_device.h>
24 #include <linux/workqueue.h>
25 #include <linux/timer.h>
26 #include <linux/clk.h>
27 #include <linux/mmc/host.h>
29 #include <linux/semaphore.h>
31 #include <mach/hardware.h>
32 #include <mach/board.h>
36 /* OMAP HSMMC Host Controller Registers */
37 #define OMAP_HSMMC_SYSCONFIG 0x0010
38 #define OMAP_HSMMC_CON 0x002C
39 #define OMAP_HSMMC_BLK 0x0104
40 #define OMAP_HSMMC_ARG 0x0108
41 #define OMAP_HSMMC_CMD 0x010C
42 #define OMAP_HSMMC_RSP10 0x0110
43 #define OMAP_HSMMC_RSP32 0x0114
44 #define OMAP_HSMMC_RSP54 0x0118
45 #define OMAP_HSMMC_RSP76 0x011C
46 #define OMAP_HSMMC_DATA 0x0120
47 #define OMAP_HSMMC_HCTL 0x0128
48 #define OMAP_HSMMC_SYSCTL 0x012C
49 #define OMAP_HSMMC_STAT 0x0130
50 #define OMAP_HSMMC_IE 0x0134
51 #define OMAP_HSMMC_ISE 0x0138
52 #define OMAP_HSMMC_CAPA 0x0140
54 #define VS18 (1 << 26)
55 #define VS30 (1 << 25)
56 #define SDVS18 (0x5 << 9)
57 #define SDVS30 (0x6 << 9)
58 #define SDVS33 (0x7 << 9)
59 #define SDVS_MASK 0x00000E00
60 #define SDVSCLR 0xFFFFF1FF
61 #define SDVSDET 0x00000400
68 #define CLKD_MASK 0x0000FFC0
70 #define DTO_MASK 0x000F0000
72 #define INT_EN_MASK 0x307F0033
73 #define BWR_ENABLE (1 << 4)
74 #define BRR_ENABLE (1 << 5)
75 #define INIT_STREAM (1 << 1)
76 #define DP_SELECT (1 << 21)
81 #define FOUR_BIT (1 << 1)
87 #define CMD_TIMEOUT (1 << 16)
88 #define DATA_TIMEOUT (1 << 20)
89 #define CMD_CRC (1 << 17)
90 #define DATA_CRC (1 << 21)
91 #define CARD_ERR (1 << 28)
92 #define STAT_CLEAR 0xFFFFFFFF
93 #define INIT_STREAM_CMD 0x00000000
94 #define DUAL_VOLT_OCR_BIT 7
99 * FIXME: Most likely all the data using these _DEVID defines should come
100 * from the platform_data, or implemented in controller and slot specific
103 #define OMAP_MMC1_DEVID 0
104 #define OMAP_MMC2_DEVID 1
105 #define OMAP_MMC3_DEVID 2
107 #define MMC_TIMEOUT_MS 20
108 #define OMAP_MMC_MASTER_CLOCK 96000000
109 #define DRIVER_NAME "mmci-omap-hs"
112 * One controller can have multiple slots, like on some omap boards using
113 * omap.c controller driver. Luckily this is not currently done on any known
114 * omap_hsmmc.c device.
116 #define mmc_slot(host) (host->pdata->slots[host->slot_id])
119 * MMC Host controller read/write API's
121 #define OMAP_HSMMC_READ(base, reg) \
122 __raw_readl((base) + OMAP_HSMMC_##reg)
124 #define OMAP_HSMMC_WRITE(base, reg, val) \
125 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
127 struct mmc_omap_host
{
129 struct mmc_host
*mmc
;
130 struct mmc_request
*mrq
;
131 struct mmc_command
*cmd
;
132 struct mmc_data
*data
;
136 struct semaphore sem
;
137 struct work_struct mmc_carddetect_work
;
139 resource_size_t mapbase
;
141 unsigned int dma_len
;
142 unsigned int dma_sg_idx
;
143 unsigned char bus_mode
;
150 int dma_line_tx
, dma_line_rx
;
154 struct omap_mmc_platform_data
*pdata
;
158 * Stop clock to the card
160 static void omap_mmc_stop_clock(struct mmc_omap_host
*host
)
162 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
,
163 OMAP_HSMMC_READ(host
->base
, SYSCTL
) & ~CEN
);
164 if ((OMAP_HSMMC_READ(host
->base
, SYSCTL
) & CEN
) != 0x0)
165 dev_dbg(mmc_dev(host
->mmc
), "MMC Clock is not stoped\n");
169 * Send init stream sequence to card
170 * before sending IDLE command
172 static void send_init_stream(struct mmc_omap_host
*host
)
175 unsigned long timeout
;
177 disable_irq(host
->irq
);
178 OMAP_HSMMC_WRITE(host
->base
, CON
,
179 OMAP_HSMMC_READ(host
->base
, CON
) | INIT_STREAM
);
180 OMAP_HSMMC_WRITE(host
->base
, CMD
, INIT_STREAM_CMD
);
182 timeout
= jiffies
+ msecs_to_jiffies(MMC_TIMEOUT_MS
);
183 while ((reg
!= CC
) && time_before(jiffies
, timeout
))
184 reg
= OMAP_HSMMC_READ(host
->base
, STAT
) & CC
;
186 OMAP_HSMMC_WRITE(host
->base
, CON
,
187 OMAP_HSMMC_READ(host
->base
, CON
) & ~INIT_STREAM
);
188 enable_irq(host
->irq
);
192 int mmc_omap_cover_is_closed(struct mmc_omap_host
*host
)
196 if (host
->pdata
->slots
[host
->slot_id
].get_cover_state
)
197 r
= host
->pdata
->slots
[host
->slot_id
].get_cover_state(host
->dev
,
203 mmc_omap_show_cover_switch(struct device
*dev
, struct device_attribute
*attr
,
206 struct mmc_host
*mmc
= container_of(dev
, struct mmc_host
, class_dev
);
207 struct mmc_omap_host
*host
= mmc_priv(mmc
);
209 return sprintf(buf
, "%s\n", mmc_omap_cover_is_closed(host
) ? "closed" :
213 static DEVICE_ATTR(cover_switch
, S_IRUGO
, mmc_omap_show_cover_switch
, NULL
);
216 mmc_omap_show_slot_name(struct device
*dev
, struct device_attribute
*attr
,
219 struct mmc_host
*mmc
= container_of(dev
, struct mmc_host
, class_dev
);
220 struct mmc_omap_host
*host
= mmc_priv(mmc
);
221 struct omap_mmc_slot_data slot
= host
->pdata
->slots
[host
->slot_id
];
223 return sprintf(buf
, "%s\n", slot
.name
);
226 static DEVICE_ATTR(slot_name
, S_IRUGO
, mmc_omap_show_slot_name
, NULL
);
229 * Configure the response type and send the cmd.
232 mmc_omap_start_command(struct mmc_omap_host
*host
, struct mmc_command
*cmd
,
233 struct mmc_data
*data
)
235 int cmdreg
= 0, resptype
= 0, cmdtype
= 0;
237 dev_dbg(mmc_dev(host
->mmc
), "%s: CMD%d, argument 0x%08x\n",
238 mmc_hostname(host
->mmc
), cmd
->opcode
, cmd
->arg
);
242 * Clear status bits and enable interrupts
244 OMAP_HSMMC_WRITE(host
->base
, STAT
, STAT_CLEAR
);
245 OMAP_HSMMC_WRITE(host
->base
, ISE
, INT_EN_MASK
);
248 OMAP_HSMMC_WRITE(host
->base
, IE
,
249 INT_EN_MASK
& ~(BRR_ENABLE
| BWR_ENABLE
));
251 OMAP_HSMMC_WRITE(host
->base
, IE
, INT_EN_MASK
);
253 host
->response_busy
= 0;
254 if (cmd
->flags
& MMC_RSP_PRESENT
) {
255 if (cmd
->flags
& MMC_RSP_136
)
257 else if (cmd
->flags
& MMC_RSP_BUSY
) {
259 host
->response_busy
= 1;
265 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
266 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
267 * a val of 0x3, rest 0x0.
269 if (cmd
== host
->mrq
->stop
)
272 cmdreg
= (cmd
->opcode
<< 24) | (resptype
<< 16) | (cmdtype
<< 22);
275 cmdreg
|= DP_SELECT
| MSBS
| BCE
;
276 if (data
->flags
& MMC_DATA_READ
)
285 OMAP_HSMMC_WRITE(host
->base
, ARG
, cmd
->arg
);
286 OMAP_HSMMC_WRITE(host
->base
, CMD
, cmdreg
);
290 mmc_omap_get_dma_dir(struct mmc_omap_host
*host
, struct mmc_data
*data
)
292 if (data
->flags
& MMC_DATA_WRITE
)
293 return DMA_TO_DEVICE
;
295 return DMA_FROM_DEVICE
;
299 * Notify the transfer complete to MMC core
302 mmc_omap_xfer_done(struct mmc_omap_host
*host
, struct mmc_data
*data
)
305 struct mmc_request
*mrq
= host
->mrq
;
308 mmc_request_done(host
->mmc
, mrq
);
314 if (host
->use_dma
&& host
->dma_ch
!= -1)
315 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
, host
->dma_len
,
316 mmc_omap_get_dma_dir(host
, data
));
319 data
->bytes_xfered
+= data
->blocks
* (data
->blksz
);
321 data
->bytes_xfered
= 0;
325 mmc_request_done(host
->mmc
, data
->mrq
);
328 mmc_omap_start_command(host
, data
->stop
, NULL
);
332 * Notify the core about command completion
335 mmc_omap_cmd_done(struct mmc_omap_host
*host
, struct mmc_command
*cmd
)
339 if (cmd
->flags
& MMC_RSP_PRESENT
) {
340 if (cmd
->flags
& MMC_RSP_136
) {
341 /* response type 2 */
342 cmd
->resp
[3] = OMAP_HSMMC_READ(host
->base
, RSP10
);
343 cmd
->resp
[2] = OMAP_HSMMC_READ(host
->base
, RSP32
);
344 cmd
->resp
[1] = OMAP_HSMMC_READ(host
->base
, RSP54
);
345 cmd
->resp
[0] = OMAP_HSMMC_READ(host
->base
, RSP76
);
347 /* response types 1, 1b, 3, 4, 5, 6 */
348 cmd
->resp
[0] = OMAP_HSMMC_READ(host
->base
, RSP10
);
351 if ((host
->data
== NULL
&& !host
->response_busy
) || cmd
->error
) {
353 mmc_request_done(host
->mmc
, cmd
->mrq
);
358 * DMA clean up for command errors
360 static void mmc_dma_cleanup(struct mmc_omap_host
*host
, int errno
)
362 host
->data
->error
= errno
;
364 if (host
->use_dma
&& host
->dma_ch
!= -1) {
365 dma_unmap_sg(mmc_dev(host
->mmc
), host
->data
->sg
, host
->dma_len
,
366 mmc_omap_get_dma_dir(host
, host
->data
));
367 omap_free_dma(host
->dma_ch
);
375 * Readable error output
377 #ifdef CONFIG_MMC_DEBUG
378 static void mmc_omap_report_irq(struct mmc_omap_host
*host
, u32 status
)
380 /* --- means reserved bit without definition at documentation */
381 static const char *mmc_omap_status_bits
[] = {
382 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
383 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
384 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
385 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
391 len
= sprintf(buf
, "MMC IRQ 0x%x :", status
);
394 for (i
= 0; i
< ARRAY_SIZE(mmc_omap_status_bits
); i
++)
395 if (status
& (1 << i
)) {
396 len
= sprintf(buf
, " %s", mmc_omap_status_bits
[i
]);
400 dev_dbg(mmc_dev(host
->mmc
), "%s\n", res
);
402 #endif /* CONFIG_MMC_DEBUG */
405 * MMC controller internal state machines reset
407 * Used to reset command or data internal state machines, using respectively
408 * SRC or SRD bit of SYSCTL register
409 * Can be called from interrupt context
411 static inline void mmc_omap_reset_controller_fsm(struct mmc_omap_host
*host
,
415 unsigned long limit
= (loops_per_jiffy
*
416 msecs_to_jiffies(MMC_TIMEOUT_MS
));
418 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
,
419 OMAP_HSMMC_READ(host
->base
, SYSCTL
) | bit
);
421 while ((OMAP_HSMMC_READ(host
->base
, SYSCTL
) & bit
) &&
425 if (OMAP_HSMMC_READ(host
->base
, SYSCTL
) & bit
)
426 dev_err(mmc_dev(host
->mmc
),
427 "Timeout waiting on controller reset in %s\n",
432 * MMC controller IRQ handler
434 static irqreturn_t
mmc_omap_irq(int irq
, void *dev_id
)
436 struct mmc_omap_host
*host
= dev_id
;
437 struct mmc_data
*data
;
438 int end_cmd
= 0, end_trans
= 0, status
;
440 if (host
->mrq
== NULL
) {
441 OMAP_HSMMC_WRITE(host
->base
, STAT
,
442 OMAP_HSMMC_READ(host
->base
, STAT
));
443 /* Flush posted write */
444 OMAP_HSMMC_READ(host
->base
, STAT
);
449 status
= OMAP_HSMMC_READ(host
->base
, STAT
);
450 dev_dbg(mmc_dev(host
->mmc
), "IRQ Status is %x\n", status
);
453 #ifdef CONFIG_MMC_DEBUG
454 mmc_omap_report_irq(host
, status
);
456 if ((status
& CMD_TIMEOUT
) ||
457 (status
& CMD_CRC
)) {
459 if (status
& CMD_TIMEOUT
) {
460 mmc_omap_reset_controller_fsm(host
, SRC
);
461 host
->cmd
->error
= -ETIMEDOUT
;
463 host
->cmd
->error
= -EILSEQ
;
467 if (host
->data
|| host
->response_busy
) {
469 mmc_dma_cleanup(host
, -ETIMEDOUT
);
470 host
->response_busy
= 0;
471 mmc_omap_reset_controller_fsm(host
, SRD
);
474 if ((status
& DATA_TIMEOUT
) ||
475 (status
& DATA_CRC
)) {
476 if (host
->data
|| host
->response_busy
) {
477 int err
= (status
& DATA_TIMEOUT
) ?
478 -ETIMEDOUT
: -EILSEQ
;
481 mmc_dma_cleanup(host
, err
);
483 host
->mrq
->cmd
->error
= err
;
484 host
->response_busy
= 0;
485 mmc_omap_reset_controller_fsm(host
, SRD
);
489 if (status
& CARD_ERR
) {
490 dev_dbg(mmc_dev(host
->mmc
),
491 "Ignoring card err CMD%d\n", host
->cmd
->opcode
);
499 OMAP_HSMMC_WRITE(host
->base
, STAT
, status
);
500 /* Flush posted write */
501 OMAP_HSMMC_READ(host
->base
, STAT
);
503 if (end_cmd
|| ((status
& CC
) && host
->cmd
))
504 mmc_omap_cmd_done(host
, host
->cmd
);
505 if (end_trans
|| (status
& TC
))
506 mmc_omap_xfer_done(host
, data
);
511 static void set_sd_bus_power(struct mmc_omap_host
*host
)
515 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
516 OMAP_HSMMC_READ(host
->base
, HCTL
) | SDBP
);
517 for (i
= 0; i
< loops_per_jiffy
; i
++) {
518 if (OMAP_HSMMC_READ(host
->base
, HCTL
) & SDBP
)
525 * Switch MMC interface voltage ... only relevant for MMC1.
527 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
528 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
529 * Some chips, like eMMC ones, use internal transceivers.
531 static int omap_mmc_switch_opcond(struct mmc_omap_host
*host
, int vdd
)
536 /* Disable the clocks */
537 clk_disable(host
->fclk
);
538 clk_disable(host
->iclk
);
539 clk_disable(host
->dbclk
);
541 /* Turn the power off */
542 ret
= mmc_slot(host
).set_power(host
->dev
, host
->slot_id
, 0, 0);
546 /* Turn the power ON with given VDD 1.8 or 3.0v */
547 ret
= mmc_slot(host
).set_power(host
->dev
, host
->slot_id
, 1, vdd
);
551 clk_enable(host
->fclk
);
552 clk_enable(host
->iclk
);
553 clk_enable(host
->dbclk
);
555 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
556 OMAP_HSMMC_READ(host
->base
, HCTL
) & SDVSCLR
);
557 reg_val
= OMAP_HSMMC_READ(host
->base
, HCTL
);
560 * If a MMC dual voltage card is detected, the set_ios fn calls
561 * this fn with VDD bit set for 1.8V. Upon card removal from the
562 * slot, omap_mmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
564 * Cope with a bit of slop in the range ... per data sheets:
565 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
566 * but recommended values are 1.71V to 1.89V
567 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
568 * but recommended values are 2.7V to 3.3V
570 * Board setup code shouldn't permit anything very out-of-range.
571 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
572 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
574 if ((1 << vdd
) <= MMC_VDD_23_24
)
579 OMAP_HSMMC_WRITE(host
->base
, HCTL
, reg_val
);
580 set_sd_bus_power(host
);
584 dev_dbg(mmc_dev(host
->mmc
), "Unable to switch operating voltage\n");
589 * Work Item to notify the core about card insertion/removal
591 static void mmc_omap_detect(struct work_struct
*work
)
593 struct mmc_omap_host
*host
= container_of(work
, struct mmc_omap_host
,
594 mmc_carddetect_work
);
595 struct omap_mmc_slot_data
*slot
= &mmc_slot(host
);
597 if (mmc_slot(host
).card_detect
)
598 host
->carddetect
= slot
->card_detect(slot
->card_detect_irq
);
600 host
->carddetect
= -ENOSYS
;
602 sysfs_notify(&host
->mmc
->class_dev
.kobj
, NULL
, "cover_switch");
603 if (host
->carddetect
) {
604 mmc_detect_change(host
->mmc
, (HZ
* 200) / 1000);
606 mmc_omap_reset_controller_fsm(host
, SRD
);
607 mmc_detect_change(host
->mmc
, (HZ
* 50) / 1000);
612 * ISR for handling card insertion and removal
614 static irqreturn_t
omap_mmc_cd_handler(int irq
, void *dev_id
)
616 struct mmc_omap_host
*host
= (struct mmc_omap_host
*)dev_id
;
618 schedule_work(&host
->mmc_carddetect_work
);
623 static int mmc_omap_get_dma_sync_dev(struct mmc_omap_host
*host
,
624 struct mmc_data
*data
)
628 if (data
->flags
& MMC_DATA_WRITE
)
629 sync_dev
= host
->dma_line_tx
;
631 sync_dev
= host
->dma_line_rx
;
635 static void mmc_omap_config_dma_params(struct mmc_omap_host
*host
,
636 struct mmc_data
*data
,
637 struct scatterlist
*sgl
)
639 int blksz
, nblk
, dma_ch
;
641 dma_ch
= host
->dma_ch
;
642 if (data
->flags
& MMC_DATA_WRITE
) {
643 omap_set_dma_dest_params(dma_ch
, 0, OMAP_DMA_AMODE_CONSTANT
,
644 (host
->mapbase
+ OMAP_HSMMC_DATA
), 0, 0);
645 omap_set_dma_src_params(dma_ch
, 0, OMAP_DMA_AMODE_POST_INC
,
646 sg_dma_address(sgl
), 0, 0);
648 omap_set_dma_src_params(dma_ch
, 0, OMAP_DMA_AMODE_CONSTANT
,
649 (host
->mapbase
+ OMAP_HSMMC_DATA
), 0, 0);
650 omap_set_dma_dest_params(dma_ch
, 0, OMAP_DMA_AMODE_POST_INC
,
651 sg_dma_address(sgl
), 0, 0);
654 blksz
= host
->data
->blksz
;
655 nblk
= sg_dma_len(sgl
) / blksz
;
657 omap_set_dma_transfer_params(dma_ch
, OMAP_DMA_DATA_TYPE_S32
,
658 blksz
/ 4, nblk
, OMAP_DMA_SYNC_FRAME
,
659 mmc_omap_get_dma_sync_dev(host
, data
),
660 !(data
->flags
& MMC_DATA_WRITE
));
662 omap_start_dma(dma_ch
);
666 * DMA call back function
668 static void mmc_omap_dma_cb(int lch
, u16 ch_status
, void *data
)
670 struct mmc_omap_host
*host
= data
;
672 if (ch_status
& OMAP2_DMA_MISALIGNED_ERR_IRQ
)
673 dev_dbg(mmc_dev(host
->mmc
), "MISALIGNED_ADRS_ERR\n");
675 if (host
->dma_ch
< 0)
679 if (host
->dma_sg_idx
< host
->dma_len
) {
680 /* Fire up the next transfer. */
681 mmc_omap_config_dma_params(host
, host
->data
,
682 host
->data
->sg
+ host
->dma_sg_idx
);
686 omap_free_dma(host
->dma_ch
);
689 * DMA Callback: run in interrupt context.
690 * mutex_unlock will throw a kernel warning if used.
696 * Routine to configure and start DMA for the MMC card
699 mmc_omap_start_dma_transfer(struct mmc_omap_host
*host
, struct mmc_request
*req
)
701 int dma_ch
= 0, ret
= 0, err
= 1, i
;
702 struct mmc_data
*data
= req
->data
;
704 /* Sanity check: all the SG entries must be aligned by block size. */
705 for (i
= 0; i
< host
->dma_len
; i
++) {
706 struct scatterlist
*sgl
;
709 if (sgl
->length
% data
->blksz
)
712 if ((data
->blksz
% 4) != 0)
713 /* REVISIT: The MMC buffer increments only when MSB is written.
714 * Return error for blksz which is non multiple of four.
719 * If for some reason the DMA transfer is still active,
720 * we wait for timeout period and free the dma
722 if (host
->dma_ch
!= -1) {
723 set_current_state(TASK_UNINTERRUPTIBLE
);
724 schedule_timeout(100);
725 if (down_trylock(&host
->sem
)) {
726 omap_free_dma(host
->dma_ch
);
732 if (down_trylock(&host
->sem
))
736 ret
= omap_request_dma(mmc_omap_get_dma_sync_dev(host
, data
), "MMC/SD",
737 mmc_omap_dma_cb
,host
, &dma_ch
);
739 dev_err(mmc_dev(host
->mmc
),
740 "%s: omap_request_dma() failed with %d\n",
741 mmc_hostname(host
->mmc
), ret
);
745 host
->dma_len
= dma_map_sg(mmc_dev(host
->mmc
), data
->sg
,
746 data
->sg_len
, mmc_omap_get_dma_dir(host
, data
));
747 host
->dma_ch
= dma_ch
;
748 host
->dma_sg_idx
= 0;
750 mmc_omap_config_dma_params(host
, data
, data
->sg
);
755 static void set_data_timeout(struct mmc_omap_host
*host
,
756 struct mmc_request
*req
)
758 unsigned int timeout
, cycle_ns
;
759 uint32_t reg
, clkd
, dto
= 0;
761 reg
= OMAP_HSMMC_READ(host
->base
, SYSCTL
);
762 clkd
= (reg
& CLKD_MASK
) >> CLKD_SHIFT
;
766 cycle_ns
= 1000000000 / (clk_get_rate(host
->fclk
) / clkd
);
767 timeout
= req
->data
->timeout_ns
/ cycle_ns
;
768 timeout
+= req
->data
->timeout_clks
;
770 while ((timeout
& 0x80000000) == 0) {
787 reg
|= dto
<< DTO_SHIFT
;
788 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
, reg
);
792 * Configure block length for MMC/SD cards and initiate the transfer.
795 mmc_omap_prepare_data(struct mmc_omap_host
*host
, struct mmc_request
*req
)
798 host
->data
= req
->data
;
800 if (req
->data
== NULL
) {
801 OMAP_HSMMC_WRITE(host
->base
, BLK
, 0);
805 OMAP_HSMMC_WRITE(host
->base
, BLK
, (req
->data
->blksz
)
806 | (req
->data
->blocks
<< 16));
807 set_data_timeout(host
, req
);
810 ret
= mmc_omap_start_dma_transfer(host
, req
);
812 dev_dbg(mmc_dev(host
->mmc
), "MMC start dma failure\n");
820 * Request function. for read/write operation
822 static void omap_mmc_request(struct mmc_host
*mmc
, struct mmc_request
*req
)
824 struct mmc_omap_host
*host
= mmc_priv(mmc
);
826 WARN_ON(host
->mrq
!= NULL
);
828 mmc_omap_prepare_data(host
, req
);
829 mmc_omap_start_command(host
, req
->cmd
, req
->data
);
833 /* Routine to configure clock values. Exposed API to core */
834 static void omap_mmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
836 struct mmc_omap_host
*host
= mmc_priv(mmc
);
838 unsigned long regval
;
839 unsigned long timeout
;
842 switch (ios
->power_mode
) {
844 mmc_slot(host
).set_power(host
->dev
, host
->slot_id
, 0, 0);
847 mmc_slot(host
).set_power(host
->dev
, host
->slot_id
, 1, ios
->vdd
);
851 con
= OMAP_HSMMC_READ(host
->base
, CON
);
852 switch (mmc
->ios
.bus_width
) {
853 case MMC_BUS_WIDTH_8
:
854 OMAP_HSMMC_WRITE(host
->base
, CON
, con
| DW8
);
856 case MMC_BUS_WIDTH_4
:
857 OMAP_HSMMC_WRITE(host
->base
, CON
, con
& ~DW8
);
858 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
859 OMAP_HSMMC_READ(host
->base
, HCTL
) | FOUR_BIT
);
861 case MMC_BUS_WIDTH_1
:
862 OMAP_HSMMC_WRITE(host
->base
, CON
, con
& ~DW8
);
863 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
864 OMAP_HSMMC_READ(host
->base
, HCTL
) & ~FOUR_BIT
);
868 if (host
->id
== OMAP_MMC1_DEVID
) {
869 /* Only MMC1 can interface at 3V without some flavor
870 * of external transceiver; but they all handle 1.8V.
872 if ((OMAP_HSMMC_READ(host
->base
, HCTL
) & SDVSDET
) &&
873 (ios
->vdd
== DUAL_VOLT_OCR_BIT
)) {
875 * The mmc_select_voltage fn of the core does
876 * not seem to set the power_mode to
877 * MMC_POWER_UP upon recalculating the voltage.
880 if (omap_mmc_switch_opcond(host
, ios
->vdd
) != 0)
881 dev_dbg(mmc_dev(host
->mmc
),
882 "Switch operation failed\n");
887 dsor
= OMAP_MMC_MASTER_CLOCK
/ ios
->clock
;
891 if (OMAP_MMC_MASTER_CLOCK
/ dsor
> ios
->clock
)
897 omap_mmc_stop_clock(host
);
898 regval
= OMAP_HSMMC_READ(host
->base
, SYSCTL
);
899 regval
= regval
& ~(CLKD_MASK
);
900 regval
= regval
| (dsor
<< 6) | (DTO
<< 16);
901 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
, regval
);
902 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
,
903 OMAP_HSMMC_READ(host
->base
, SYSCTL
) | ICE
);
905 /* Wait till the ICS bit is set */
906 timeout
= jiffies
+ msecs_to_jiffies(MMC_TIMEOUT_MS
);
907 while ((OMAP_HSMMC_READ(host
->base
, SYSCTL
) & ICS
) != 0x2
908 && time_before(jiffies
, timeout
))
911 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
,
912 OMAP_HSMMC_READ(host
->base
, SYSCTL
) | CEN
);
914 if (ios
->power_mode
== MMC_POWER_ON
)
915 send_init_stream(host
);
917 if (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
)
918 OMAP_HSMMC_WRITE(host
->base
, CON
,
919 OMAP_HSMMC_READ(host
->base
, CON
) | OD
);
922 static int omap_hsmmc_get_cd(struct mmc_host
*mmc
)
924 struct mmc_omap_host
*host
= mmc_priv(mmc
);
925 struct omap_mmc_platform_data
*pdata
= host
->pdata
;
927 if (!pdata
->slots
[0].card_detect
)
929 return pdata
->slots
[0].card_detect(pdata
->slots
[0].card_detect_irq
);
932 static int omap_hsmmc_get_ro(struct mmc_host
*mmc
)
934 struct mmc_omap_host
*host
= mmc_priv(mmc
);
935 struct omap_mmc_platform_data
*pdata
= host
->pdata
;
937 if (!pdata
->slots
[0].get_ro
)
939 return pdata
->slots
[0].get_ro(host
->dev
, 0);
942 static void omap_hsmmc_init(struct mmc_omap_host
*host
)
944 u32 hctl
, capa
, value
;
946 /* Only MMC1 supports 3.0V */
947 if (host
->id
== OMAP_MMC1_DEVID
) {
955 value
= OMAP_HSMMC_READ(host
->base
, HCTL
) & ~SDVS_MASK
;
956 OMAP_HSMMC_WRITE(host
->base
, HCTL
, value
| hctl
);
958 value
= OMAP_HSMMC_READ(host
->base
, CAPA
);
959 OMAP_HSMMC_WRITE(host
->base
, CAPA
, value
| capa
);
961 /* Set the controller to AUTO IDLE mode */
962 value
= OMAP_HSMMC_READ(host
->base
, SYSCONFIG
);
963 OMAP_HSMMC_WRITE(host
->base
, SYSCONFIG
, value
| AUTOIDLE
);
965 /* Set SD bus power bit */
966 set_sd_bus_power(host
);
969 static struct mmc_host_ops mmc_omap_ops
= {
970 .request
= omap_mmc_request
,
971 .set_ios
= omap_mmc_set_ios
,
972 .get_cd
= omap_hsmmc_get_cd
,
973 .get_ro
= omap_hsmmc_get_ro
,
974 /* NYET -- enable_sdio_irq */
977 static int __init
omap_mmc_probe(struct platform_device
*pdev
)
979 struct omap_mmc_platform_data
*pdata
= pdev
->dev
.platform_data
;
980 struct mmc_host
*mmc
;
981 struct mmc_omap_host
*host
= NULL
;
982 struct resource
*res
;
986 dev_err(&pdev
->dev
, "Platform Data is missing\n");
990 if (pdata
->nr_slots
== 0) {
991 dev_err(&pdev
->dev
, "No Slots\n");
995 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
996 irq
= platform_get_irq(pdev
, 0);
997 if (res
== NULL
|| irq
< 0)
1000 res
= request_mem_region(res
->start
, res
->end
- res
->start
+ 1,
1005 mmc
= mmc_alloc_host(sizeof(struct mmc_omap_host
), &pdev
->dev
);
1011 host
= mmc_priv(mmc
);
1013 host
->pdata
= pdata
;
1014 host
->dev
= &pdev
->dev
;
1016 host
->dev
->dma_mask
= &pdata
->dma_mask
;
1019 host
->id
= pdev
->id
;
1021 host
->mapbase
= res
->start
;
1022 host
->base
= ioremap(host
->mapbase
, SZ_4K
);
1024 platform_set_drvdata(pdev
, host
);
1025 INIT_WORK(&host
->mmc_carddetect_work
, mmc_omap_detect
);
1027 mmc
->ops
= &mmc_omap_ops
;
1028 mmc
->f_min
= 400000;
1029 mmc
->f_max
= 52000000;
1031 sema_init(&host
->sem
, 1);
1033 host
->iclk
= clk_get(&pdev
->dev
, "ick");
1034 if (IS_ERR(host
->iclk
)) {
1035 ret
= PTR_ERR(host
->iclk
);
1039 host
->fclk
= clk_get(&pdev
->dev
, "fck");
1040 if (IS_ERR(host
->fclk
)) {
1041 ret
= PTR_ERR(host
->fclk
);
1043 clk_put(host
->iclk
);
1047 if (clk_enable(host
->fclk
) != 0) {
1048 clk_put(host
->iclk
);
1049 clk_put(host
->fclk
);
1053 if (clk_enable(host
->iclk
) != 0) {
1054 clk_disable(host
->fclk
);
1055 clk_put(host
->iclk
);
1056 clk_put(host
->fclk
);
1060 host
->dbclk
= clk_get(&pdev
->dev
, "mmchsdb_fck");
1062 * MMC can still work without debounce clock.
1064 if (IS_ERR(host
->dbclk
))
1065 dev_warn(mmc_dev(host
->mmc
), "Failed to get debounce clock\n");
1067 if (clk_enable(host
->dbclk
) != 0)
1068 dev_dbg(mmc_dev(host
->mmc
), "Enabling debounce"
1071 host
->dbclk_enabled
= 1;
1073 /* Since we do only SG emulation, we can have as many segs
1075 mmc
->max_phys_segs
= 1024;
1076 mmc
->max_hw_segs
= 1024;
1078 mmc
->max_blk_size
= 512; /* Block Length at max can be 1024 */
1079 mmc
->max_blk_count
= 0xFFFF; /* No. of Blocks is 16 bits */
1080 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
1081 mmc
->max_seg_size
= mmc
->max_req_size
;
1083 mmc
->caps
|= MMC_CAP_MMC_HIGHSPEED
| MMC_CAP_SD_HIGHSPEED
;
1085 if (pdata
->slots
[host
->slot_id
].wires
>= 8)
1086 mmc
->caps
|= MMC_CAP_8_BIT_DATA
;
1087 else if (pdata
->slots
[host
->slot_id
].wires
>= 4)
1088 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1090 omap_hsmmc_init(host
);
1092 /* Select DMA lines */
1094 case OMAP_MMC1_DEVID
:
1095 host
->dma_line_tx
= OMAP24XX_DMA_MMC1_TX
;
1096 host
->dma_line_rx
= OMAP24XX_DMA_MMC1_RX
;
1098 case OMAP_MMC2_DEVID
:
1099 host
->dma_line_tx
= OMAP24XX_DMA_MMC2_TX
;
1100 host
->dma_line_rx
= OMAP24XX_DMA_MMC2_RX
;
1102 case OMAP_MMC3_DEVID
:
1103 host
->dma_line_tx
= OMAP34XX_DMA_MMC3_TX
;
1104 host
->dma_line_rx
= OMAP34XX_DMA_MMC3_RX
;
1107 dev_err(mmc_dev(host
->mmc
), "Invalid MMC id\n");
1111 /* Request IRQ for MMC operations */
1112 ret
= request_irq(host
->irq
, mmc_omap_irq
, IRQF_DISABLED
,
1113 mmc_hostname(mmc
), host
);
1115 dev_dbg(mmc_dev(host
->mmc
), "Unable to grab HSMMC IRQ\n");
1119 /* initialize power supplies, gpios, etc */
1120 if (pdata
->init
!= NULL
) {
1121 if (pdata
->init(&pdev
->dev
) != 0) {
1122 dev_dbg(mmc_dev(host
->mmc
), "late init error\n");
1123 goto err_irq_cd_init
;
1126 mmc
->ocr_avail
= mmc_slot(host
).ocr_mask
;
1128 /* Request IRQ for card detect */
1129 if ((mmc_slot(host
).card_detect_irq
)) {
1130 ret
= request_irq(mmc_slot(host
).card_detect_irq
,
1131 omap_mmc_cd_handler
,
1132 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
1134 mmc_hostname(mmc
), host
);
1136 dev_dbg(mmc_dev(host
->mmc
),
1137 "Unable to grab MMC CD IRQ\n");
1142 OMAP_HSMMC_WRITE(host
->base
, ISE
, INT_EN_MASK
);
1143 OMAP_HSMMC_WRITE(host
->base
, IE
, INT_EN_MASK
);
1147 if (host
->pdata
->slots
[host
->slot_id
].name
!= NULL
) {
1148 ret
= device_create_file(&mmc
->class_dev
, &dev_attr_slot_name
);
1152 if (mmc_slot(host
).card_detect_irq
&&
1153 host
->pdata
->slots
[host
->slot_id
].get_cover_state
) {
1154 ret
= device_create_file(&mmc
->class_dev
,
1155 &dev_attr_cover_switch
);
1157 goto err_cover_switch
;
1163 device_remove_file(&mmc
->class_dev
, &dev_attr_cover_switch
);
1165 mmc_remove_host(mmc
);
1167 free_irq(mmc_slot(host
).card_detect_irq
, host
);
1169 free_irq(host
->irq
, host
);
1171 clk_disable(host
->fclk
);
1172 clk_disable(host
->iclk
);
1173 clk_put(host
->fclk
);
1174 clk_put(host
->iclk
);
1175 if (host
->dbclk_enabled
) {
1176 clk_disable(host
->dbclk
);
1177 clk_put(host
->dbclk
);
1181 iounmap(host
->base
);
1183 dev_dbg(mmc_dev(host
->mmc
), "Probe Failed\n");
1184 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
1190 static int omap_mmc_remove(struct platform_device
*pdev
)
1192 struct mmc_omap_host
*host
= platform_get_drvdata(pdev
);
1193 struct resource
*res
;
1196 mmc_remove_host(host
->mmc
);
1197 if (host
->pdata
->cleanup
)
1198 host
->pdata
->cleanup(&pdev
->dev
);
1199 free_irq(host
->irq
, host
);
1200 if (mmc_slot(host
).card_detect_irq
)
1201 free_irq(mmc_slot(host
).card_detect_irq
, host
);
1202 flush_scheduled_work();
1204 clk_disable(host
->fclk
);
1205 clk_disable(host
->iclk
);
1206 clk_put(host
->fclk
);
1207 clk_put(host
->iclk
);
1208 if (host
->dbclk_enabled
) {
1209 clk_disable(host
->dbclk
);
1210 clk_put(host
->dbclk
);
1213 mmc_free_host(host
->mmc
);
1214 iounmap(host
->base
);
1217 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1219 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
1220 platform_set_drvdata(pdev
, NULL
);
1226 static int omap_mmc_suspend(struct platform_device
*pdev
, pm_message_t state
)
1229 struct mmc_omap_host
*host
= platform_get_drvdata(pdev
);
1231 if (host
&& host
->suspended
)
1235 ret
= mmc_suspend_host(host
->mmc
, state
);
1237 host
->suspended
= 1;
1239 OMAP_HSMMC_WRITE(host
->base
, ISE
, 0);
1240 OMAP_HSMMC_WRITE(host
->base
, IE
, 0);
1242 if (host
->pdata
->suspend
) {
1243 ret
= host
->pdata
->suspend(&pdev
->dev
,
1246 dev_dbg(mmc_dev(host
->mmc
),
1247 "Unable to handle MMC board"
1248 " level suspend\n");
1251 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
1252 OMAP_HSMMC_READ(host
->base
, HCTL
) & ~SDBP
);
1253 clk_disable(host
->fclk
);
1254 clk_disable(host
->iclk
);
1255 clk_disable(host
->dbclk
);
1262 /* Routine to resume the MMC device */
1263 static int omap_mmc_resume(struct platform_device
*pdev
)
1266 struct mmc_omap_host
*host
= platform_get_drvdata(pdev
);
1268 if (host
&& !host
->suspended
)
1273 ret
= clk_enable(host
->fclk
);
1277 ret
= clk_enable(host
->iclk
);
1279 clk_disable(host
->fclk
);
1280 clk_put(host
->fclk
);
1284 if (clk_enable(host
->dbclk
) != 0)
1285 dev_dbg(mmc_dev(host
->mmc
),
1286 "Enabling debounce clk failed\n");
1288 omap_hsmmc_init(host
);
1290 if (host
->pdata
->resume
) {
1291 ret
= host
->pdata
->resume(&pdev
->dev
, host
->slot_id
);
1293 dev_dbg(mmc_dev(host
->mmc
),
1294 "Unmask interrupt failed\n");
1297 /* Notify the core to resume the host */
1298 ret
= mmc_resume_host(host
->mmc
);
1300 host
->suspended
= 0;
1306 dev_dbg(mmc_dev(host
->mmc
),
1307 "Failed to enable MMC clocks during resume\n");
1312 #define omap_mmc_suspend NULL
1313 #define omap_mmc_resume NULL
1316 static struct platform_driver omap_mmc_driver
= {
1317 .probe
= omap_mmc_probe
,
1318 .remove
= omap_mmc_remove
,
1319 .suspend
= omap_mmc_suspend
,
1320 .resume
= omap_mmc_resume
,
1322 .name
= DRIVER_NAME
,
1323 .owner
= THIS_MODULE
,
1327 static int __init
omap_mmc_init(void)
1329 /* Register the MMC driver */
1330 return platform_driver_register(&omap_mmc_driver
);
1333 static void __exit
omap_mmc_cleanup(void)
1335 /* Unregister MMC driver */
1336 platform_driver_unregister(&omap_mmc_driver
);
1339 module_init(omap_mmc_init
);
1340 module_exit(omap_mmc_cleanup
);
1342 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
1343 MODULE_LICENSE("GPL");
1344 MODULE_ALIAS("platform:" DRIVER_NAME
);
1345 MODULE_AUTHOR("Texas Instruments Inc");