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/kernel.h>
21 #include <linux/debugfs.h>
22 #include <linux/dmaengine.h>
23 #include <linux/seq_file.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/platform_device.h>
28 #include <linux/timer.h>
29 #include <linux/clk.h>
31 #include <linux/of_gpio.h>
32 #include <linux/of_device.h>
33 #include <linux/omap-dma.h>
34 #include <linux/mmc/host.h>
35 #include <linux/mmc/core.h>
36 #include <linux/mmc/mmc.h>
38 #include <linux/gpio.h>
39 #include <linux/regulator/consumer.h>
40 #include <linux/pinctrl/consumer.h>
41 #include <linux/pm_runtime.h>
42 #include <linux/platform_data/mmc-omap.h>
44 /* OMAP HSMMC Host Controller Registers */
45 #define OMAP_HSMMC_SYSSTATUS 0x0014
46 #define OMAP_HSMMC_CON 0x002C
47 #define OMAP_HSMMC_BLK 0x0104
48 #define OMAP_HSMMC_ARG 0x0108
49 #define OMAP_HSMMC_CMD 0x010C
50 #define OMAP_HSMMC_RSP10 0x0110
51 #define OMAP_HSMMC_RSP32 0x0114
52 #define OMAP_HSMMC_RSP54 0x0118
53 #define OMAP_HSMMC_RSP76 0x011C
54 #define OMAP_HSMMC_DATA 0x0120
55 #define OMAP_HSMMC_HCTL 0x0128
56 #define OMAP_HSMMC_SYSCTL 0x012C
57 #define OMAP_HSMMC_STAT 0x0130
58 #define OMAP_HSMMC_IE 0x0134
59 #define OMAP_HSMMC_ISE 0x0138
60 #define OMAP_HSMMC_CAPA 0x0140
62 #define VS18 (1 << 26)
63 #define VS30 (1 << 25)
65 #define SDVS18 (0x5 << 9)
66 #define SDVS30 (0x6 << 9)
67 #define SDVS33 (0x7 << 9)
68 #define SDVS_MASK 0x00000E00
69 #define SDVSCLR 0xFFFFF1FF
70 #define SDVSDET 0x00000400
77 #define CLKD_MASK 0x0000FFC0
79 #define DTO_MASK 0x000F0000
81 #define INIT_STREAM (1 << 1)
82 #define DP_SELECT (1 << 21)
87 #define FOUR_BIT (1 << 1)
92 #define STAT_CLEAR 0xFFFFFFFF
93 #define INIT_STREAM_CMD 0x00000000
94 #define DUAL_VOLT_OCR_BIT 7
97 #define SOFTRESET (1 << 1)
98 #define RESETDONE (1 << 0)
100 /* Interrupt masks for IE and ISE register */
101 #define CC_EN (1 << 0)
102 #define TC_EN (1 << 1)
103 #define BWR_EN (1 << 4)
104 #define BRR_EN (1 << 5)
105 #define ERR_EN (1 << 15)
106 #define CTO_EN (1 << 16)
107 #define CCRC_EN (1 << 17)
108 #define CEB_EN (1 << 18)
109 #define CIE_EN (1 << 19)
110 #define DTO_EN (1 << 20)
111 #define DCRC_EN (1 << 21)
112 #define DEB_EN (1 << 22)
113 #define CERR_EN (1 << 28)
114 #define BADA_EN (1 << 29)
116 #define INT_EN_MASK (BADA_EN | CERR_EN | DEB_EN | DCRC_EN |\
117 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
118 BRR_EN | BWR_EN | TC_EN | CC_EN)
120 #define MMC_AUTOSUSPEND_DELAY 100
121 #define MMC_TIMEOUT_MS 20
122 #define OMAP_MMC_MIN_CLOCK 400000
123 #define OMAP_MMC_MAX_CLOCK 52000000
124 #define DRIVER_NAME "omap_hsmmc"
127 * One controller can have multiple slots, like on some omap boards using
128 * omap.c controller driver. Luckily this is not currently done on any known
129 * omap_hsmmc.c device.
131 #define mmc_slot(host) (host->pdata->slots[host->slot_id])
134 * MMC Host controller read/write API's
136 #define OMAP_HSMMC_READ(base, reg) \
137 __raw_readl((base) + OMAP_HSMMC_##reg)
139 #define OMAP_HSMMC_WRITE(base, reg, val) \
140 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
142 struct omap_hsmmc_next
{
143 unsigned int dma_len
;
147 struct omap_hsmmc_host
{
149 struct mmc_host
*mmc
;
150 struct mmc_request
*mrq
;
151 struct mmc_command
*cmd
;
152 struct mmc_data
*data
;
156 * vcc == configured supply
157 * vcc_aux == optional
158 * - MMC1, supply for DAT4..DAT7
159 * - MMC2/MMC2, external level shifter voltage supply, for
160 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
162 struct regulator
*vcc
;
163 struct regulator
*vcc_aux
;
166 resource_size_t mapbase
;
167 spinlock_t irq_lock
; /* Prevent races with irq handler */
168 unsigned int dma_len
;
169 unsigned int dma_sg_idx
;
170 unsigned char bus_mode
;
171 unsigned char power_mode
;
175 struct dma_chan
*tx_chan
;
176 struct dma_chan
*rx_chan
;
184 struct omap_hsmmc_next next_data
;
186 struct omap_mmc_platform_data
*pdata
;
189 static int omap_hsmmc_card_detect(struct device
*dev
, int slot
)
191 struct omap_hsmmc_host
*host
= dev_get_drvdata(dev
);
192 struct omap_mmc_platform_data
*mmc
= host
->pdata
;
194 /* NOTE: assumes card detect signal is active-low */
195 return !gpio_get_value_cansleep(mmc
->slots
[0].switch_pin
);
198 static int omap_hsmmc_get_wp(struct device
*dev
, int slot
)
200 struct omap_hsmmc_host
*host
= dev_get_drvdata(dev
);
201 struct omap_mmc_platform_data
*mmc
= host
->pdata
;
203 /* NOTE: assumes write protect signal is active-high */
204 return gpio_get_value_cansleep(mmc
->slots
[0].gpio_wp
);
207 static int omap_hsmmc_get_cover_state(struct device
*dev
, int slot
)
209 struct omap_hsmmc_host
*host
= dev_get_drvdata(dev
);
210 struct omap_mmc_platform_data
*mmc
= host
->pdata
;
212 /* NOTE: assumes card detect signal is active-low */
213 return !gpio_get_value_cansleep(mmc
->slots
[0].switch_pin
);
218 static int omap_hsmmc_suspend_cdirq(struct device
*dev
, int slot
)
220 struct omap_hsmmc_host
*host
= dev_get_drvdata(dev
);
221 struct omap_mmc_platform_data
*mmc
= host
->pdata
;
223 disable_irq(mmc
->slots
[0].card_detect_irq
);
227 static int omap_hsmmc_resume_cdirq(struct device
*dev
, int slot
)
229 struct omap_hsmmc_host
*host
= dev_get_drvdata(dev
);
230 struct omap_mmc_platform_data
*mmc
= host
->pdata
;
232 enable_irq(mmc
->slots
[0].card_detect_irq
);
238 #define omap_hsmmc_suspend_cdirq NULL
239 #define omap_hsmmc_resume_cdirq NULL
243 #ifdef CONFIG_REGULATOR
245 static int omap_hsmmc_set_power(struct device
*dev
, int slot
, int power_on
,
248 struct omap_hsmmc_host
*host
=
249 platform_get_drvdata(to_platform_device(dev
));
253 * If we don't see a Vcc regulator, assume it's a fixed
254 * voltage always-on regulator.
259 * With DT, never turn OFF the regulator for MMC1. This is because
260 * the pbias cell programming support is still missing when
261 * booting with Device tree
263 if (host
->pbias_disable
&& !vdd
)
266 if (mmc_slot(host
).before_set_reg
)
267 mmc_slot(host
).before_set_reg(dev
, slot
, power_on
, vdd
);
270 * Assume Vcc regulator is used only to power the card ... OMAP
271 * VDDS is used to power the pins, optionally with a transceiver to
272 * support cards using voltages other than VDDS (1.8V nominal). When a
273 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
275 * In some cases this regulator won't support enable/disable;
276 * e.g. it's a fixed rail for a WLAN chip.
278 * In other cases vcc_aux switches interface power. Example, for
279 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
280 * chips/cards need an interface voltage rail too.
283 ret
= mmc_regulator_set_ocr(host
->mmc
, host
->vcc
, vdd
);
284 /* Enable interface voltage rail, if needed */
285 if (ret
== 0 && host
->vcc_aux
) {
286 ret
= regulator_enable(host
->vcc_aux
);
288 ret
= mmc_regulator_set_ocr(host
->mmc
,
292 /* Shut down the rail */
294 ret
= regulator_disable(host
->vcc_aux
);
296 /* Then proceed to shut down the local regulator */
297 ret
= mmc_regulator_set_ocr(host
->mmc
,
302 if (mmc_slot(host
).after_set_reg
)
303 mmc_slot(host
).after_set_reg(dev
, slot
, power_on
, vdd
);
308 static int omap_hsmmc_reg_get(struct omap_hsmmc_host
*host
)
310 struct regulator
*reg
;
313 reg
= regulator_get(host
->dev
, "vmmc");
315 dev_err(host
->dev
, "vmmc regulator missing\n");
318 mmc_slot(host
).set_power
= omap_hsmmc_set_power
;
320 ocr_value
= mmc_regulator_get_ocrmask(reg
);
321 if (!mmc_slot(host
).ocr_mask
) {
322 mmc_slot(host
).ocr_mask
= ocr_value
;
324 if (!(mmc_slot(host
).ocr_mask
& ocr_value
)) {
325 dev_err(host
->dev
, "ocrmask %x is not supported\n",
326 mmc_slot(host
).ocr_mask
);
327 mmc_slot(host
).ocr_mask
= 0;
332 /* Allow an aux regulator */
333 reg
= regulator_get(host
->dev
, "vmmc_aux");
334 host
->vcc_aux
= IS_ERR(reg
) ? NULL
: reg
;
336 /* For eMMC do not power off when not in sleep state */
337 if (mmc_slot(host
).no_regulator_off_init
)
340 * UGLY HACK: workaround regulator framework bugs.
341 * When the bootloader leaves a supply active, it's
342 * initialized with zero usecount ... and we can't
343 * disable it without first enabling it. Until the
344 * framework is fixed, we need a workaround like this
345 * (which is safe for MMC, but not in general).
347 if (regulator_is_enabled(host
->vcc
) > 0 ||
348 (host
->vcc_aux
&& regulator_is_enabled(host
->vcc_aux
))) {
349 int vdd
= ffs(mmc_slot(host
).ocr_mask
) - 1;
351 mmc_slot(host
).set_power(host
->dev
, host
->slot_id
,
353 mmc_slot(host
).set_power(host
->dev
, host
->slot_id
,
361 static void omap_hsmmc_reg_put(struct omap_hsmmc_host
*host
)
363 regulator_put(host
->vcc
);
364 regulator_put(host
->vcc_aux
);
365 mmc_slot(host
).set_power
= NULL
;
368 static inline int omap_hsmmc_have_reg(void)
375 static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host
*host
)
380 static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host
*host
)
384 static inline int omap_hsmmc_have_reg(void)
391 static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data
*pdata
)
395 if (gpio_is_valid(pdata
->slots
[0].switch_pin
)) {
396 if (pdata
->slots
[0].cover
)
397 pdata
->slots
[0].get_cover_state
=
398 omap_hsmmc_get_cover_state
;
400 pdata
->slots
[0].card_detect
= omap_hsmmc_card_detect
;
401 pdata
->slots
[0].card_detect_irq
=
402 gpio_to_irq(pdata
->slots
[0].switch_pin
);
403 ret
= gpio_request(pdata
->slots
[0].switch_pin
, "mmc_cd");
406 ret
= gpio_direction_input(pdata
->slots
[0].switch_pin
);
410 pdata
->slots
[0].switch_pin
= -EINVAL
;
412 if (gpio_is_valid(pdata
->slots
[0].gpio_wp
)) {
413 pdata
->slots
[0].get_ro
= omap_hsmmc_get_wp
;
414 ret
= gpio_request(pdata
->slots
[0].gpio_wp
, "mmc_wp");
417 ret
= gpio_direction_input(pdata
->slots
[0].gpio_wp
);
421 pdata
->slots
[0].gpio_wp
= -EINVAL
;
426 gpio_free(pdata
->slots
[0].gpio_wp
);
428 if (gpio_is_valid(pdata
->slots
[0].switch_pin
))
430 gpio_free(pdata
->slots
[0].switch_pin
);
434 static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data
*pdata
)
436 if (gpio_is_valid(pdata
->slots
[0].gpio_wp
))
437 gpio_free(pdata
->slots
[0].gpio_wp
);
438 if (gpio_is_valid(pdata
->slots
[0].switch_pin
))
439 gpio_free(pdata
->slots
[0].switch_pin
);
443 * Start clock to the card
445 static void omap_hsmmc_start_clock(struct omap_hsmmc_host
*host
)
447 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
,
448 OMAP_HSMMC_READ(host
->base
, SYSCTL
) | CEN
);
452 * Stop clock to the card
454 static void omap_hsmmc_stop_clock(struct omap_hsmmc_host
*host
)
456 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
,
457 OMAP_HSMMC_READ(host
->base
, SYSCTL
) & ~CEN
);
458 if ((OMAP_HSMMC_READ(host
->base
, SYSCTL
) & CEN
) != 0x0)
459 dev_dbg(mmc_dev(host
->mmc
), "MMC Clock is not stopped\n");
462 static void omap_hsmmc_enable_irq(struct omap_hsmmc_host
*host
,
463 struct mmc_command
*cmd
)
465 unsigned int irq_mask
;
468 irq_mask
= INT_EN_MASK
& ~(BRR_EN
| BWR_EN
);
470 irq_mask
= INT_EN_MASK
;
472 /* Disable timeout for erases */
473 if (cmd
->opcode
== MMC_ERASE
)
476 OMAP_HSMMC_WRITE(host
->base
, STAT
, STAT_CLEAR
);
477 OMAP_HSMMC_WRITE(host
->base
, ISE
, irq_mask
);
478 OMAP_HSMMC_WRITE(host
->base
, IE
, irq_mask
);
481 static void omap_hsmmc_disable_irq(struct omap_hsmmc_host
*host
)
483 OMAP_HSMMC_WRITE(host
->base
, ISE
, 0);
484 OMAP_HSMMC_WRITE(host
->base
, IE
, 0);
485 OMAP_HSMMC_WRITE(host
->base
, STAT
, STAT_CLEAR
);
488 /* Calculate divisor for the given clock frequency */
489 static u16
calc_divisor(struct omap_hsmmc_host
*host
, struct mmc_ios
*ios
)
494 dsor
= DIV_ROUND_UP(clk_get_rate(host
->fclk
), ios
->clock
);
502 static void omap_hsmmc_set_clock(struct omap_hsmmc_host
*host
)
504 struct mmc_ios
*ios
= &host
->mmc
->ios
;
505 unsigned long regval
;
506 unsigned long timeout
;
507 unsigned long clkdiv
;
509 dev_vdbg(mmc_dev(host
->mmc
), "Set clock to %uHz\n", ios
->clock
);
511 omap_hsmmc_stop_clock(host
);
513 regval
= OMAP_HSMMC_READ(host
->base
, SYSCTL
);
514 regval
= regval
& ~(CLKD_MASK
| DTO_MASK
);
515 clkdiv
= calc_divisor(host
, ios
);
516 regval
= regval
| (clkdiv
<< 6) | (DTO
<< 16);
517 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
, regval
);
518 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
,
519 OMAP_HSMMC_READ(host
->base
, SYSCTL
) | ICE
);
521 /* Wait till the ICS bit is set */
522 timeout
= jiffies
+ msecs_to_jiffies(MMC_TIMEOUT_MS
);
523 while ((OMAP_HSMMC_READ(host
->base
, SYSCTL
) & ICS
) != ICS
524 && time_before(jiffies
, timeout
))
528 * Enable High-Speed Support
530 * - Controller should support High-Speed-Enable Bit
531 * - Controller should not be using DDR Mode
532 * - Controller should advertise that it supports High Speed
533 * in capabilities register
534 * - MMC/SD clock coming out of controller > 25MHz
536 if ((mmc_slot(host
).features
& HSMMC_HAS_HSPE_SUPPORT
) &&
537 (ios
->timing
!= MMC_TIMING_UHS_DDR50
) &&
538 ((OMAP_HSMMC_READ(host
->base
, CAPA
) & HSS
) == HSS
)) {
539 regval
= OMAP_HSMMC_READ(host
->base
, HCTL
);
540 if (clkdiv
&& (clk_get_rate(host
->fclk
)/clkdiv
) > 25000000)
545 OMAP_HSMMC_WRITE(host
->base
, HCTL
, regval
);
548 omap_hsmmc_start_clock(host
);
551 static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host
*host
)
553 struct mmc_ios
*ios
= &host
->mmc
->ios
;
556 con
= OMAP_HSMMC_READ(host
->base
, CON
);
557 if (ios
->timing
== MMC_TIMING_UHS_DDR50
)
558 con
|= DDR
; /* configure in DDR mode */
561 switch (ios
->bus_width
) {
562 case MMC_BUS_WIDTH_8
:
563 OMAP_HSMMC_WRITE(host
->base
, CON
, con
| DW8
);
565 case MMC_BUS_WIDTH_4
:
566 OMAP_HSMMC_WRITE(host
->base
, CON
, con
& ~DW8
);
567 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
568 OMAP_HSMMC_READ(host
->base
, HCTL
) | FOUR_BIT
);
570 case MMC_BUS_WIDTH_1
:
571 OMAP_HSMMC_WRITE(host
->base
, CON
, con
& ~DW8
);
572 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
573 OMAP_HSMMC_READ(host
->base
, HCTL
) & ~FOUR_BIT
);
578 static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host
*host
)
580 struct mmc_ios
*ios
= &host
->mmc
->ios
;
583 con
= OMAP_HSMMC_READ(host
->base
, CON
);
584 if (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
)
585 OMAP_HSMMC_WRITE(host
->base
, CON
, con
| OD
);
587 OMAP_HSMMC_WRITE(host
->base
, CON
, con
& ~OD
);
593 * Restore the MMC host context, if it was lost as result of a
594 * power state change.
596 static int omap_hsmmc_context_restore(struct omap_hsmmc_host
*host
)
598 struct mmc_ios
*ios
= &host
->mmc
->ios
;
599 struct omap_mmc_platform_data
*pdata
= host
->pdata
;
600 int context_loss
= 0;
602 unsigned long timeout
;
604 if (pdata
->get_context_loss_count
) {
605 context_loss
= pdata
->get_context_loss_count(host
->dev
);
606 if (context_loss
< 0)
610 dev_dbg(mmc_dev(host
->mmc
), "context was %slost\n",
611 context_loss
== host
->context_loss
? "not " : "");
612 if (host
->context_loss
== context_loss
)
615 if (!OMAP_HSMMC_READ(host
->base
, SYSSTATUS
) & RESETDONE
)
618 if (host
->pdata
->controller_flags
& OMAP_HSMMC_SUPPORTS_DUAL_VOLT
) {
619 if (host
->power_mode
!= MMC_POWER_OFF
&&
620 (1 << ios
->vdd
) <= MMC_VDD_23_24
)
630 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
631 OMAP_HSMMC_READ(host
->base
, HCTL
) | hctl
);
633 OMAP_HSMMC_WRITE(host
->base
, CAPA
,
634 OMAP_HSMMC_READ(host
->base
, CAPA
) | capa
);
636 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
637 OMAP_HSMMC_READ(host
->base
, HCTL
) | SDBP
);
639 timeout
= jiffies
+ msecs_to_jiffies(MMC_TIMEOUT_MS
);
640 while ((OMAP_HSMMC_READ(host
->base
, HCTL
) & SDBP
) != SDBP
641 && time_before(jiffies
, timeout
))
644 omap_hsmmc_disable_irq(host
);
646 /* Do not initialize card-specific things if the power is off */
647 if (host
->power_mode
== MMC_POWER_OFF
)
650 omap_hsmmc_set_bus_width(host
);
652 omap_hsmmc_set_clock(host
);
654 omap_hsmmc_set_bus_mode(host
);
657 host
->context_loss
= context_loss
;
659 dev_dbg(mmc_dev(host
->mmc
), "context is restored\n");
664 * Save the MMC host context (store the number of power state changes so far).
666 static void omap_hsmmc_context_save(struct omap_hsmmc_host
*host
)
668 struct omap_mmc_platform_data
*pdata
= host
->pdata
;
671 if (pdata
->get_context_loss_count
) {
672 context_loss
= pdata
->get_context_loss_count(host
->dev
);
673 if (context_loss
< 0)
675 host
->context_loss
= context_loss
;
681 static int omap_hsmmc_context_restore(struct omap_hsmmc_host
*host
)
686 static void omap_hsmmc_context_save(struct omap_hsmmc_host
*host
)
693 * Send init stream sequence to card
694 * before sending IDLE command
696 static void send_init_stream(struct omap_hsmmc_host
*host
)
699 unsigned long timeout
;
701 if (host
->protect_card
)
704 disable_irq(host
->irq
);
706 OMAP_HSMMC_WRITE(host
->base
, IE
, INT_EN_MASK
);
707 OMAP_HSMMC_WRITE(host
->base
, CON
,
708 OMAP_HSMMC_READ(host
->base
, CON
) | INIT_STREAM
);
709 OMAP_HSMMC_WRITE(host
->base
, CMD
, INIT_STREAM_CMD
);
711 timeout
= jiffies
+ msecs_to_jiffies(MMC_TIMEOUT_MS
);
712 while ((reg
!= CC_EN
) && time_before(jiffies
, timeout
))
713 reg
= OMAP_HSMMC_READ(host
->base
, STAT
) & CC_EN
;
715 OMAP_HSMMC_WRITE(host
->base
, CON
,
716 OMAP_HSMMC_READ(host
->base
, CON
) & ~INIT_STREAM
);
718 OMAP_HSMMC_WRITE(host
->base
, STAT
, STAT_CLEAR
);
719 OMAP_HSMMC_READ(host
->base
, STAT
);
721 enable_irq(host
->irq
);
725 int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host
*host
)
729 if (mmc_slot(host
).get_cover_state
)
730 r
= mmc_slot(host
).get_cover_state(host
->dev
, host
->slot_id
);
735 omap_hsmmc_show_cover_switch(struct device
*dev
, struct device_attribute
*attr
,
738 struct mmc_host
*mmc
= container_of(dev
, struct mmc_host
, class_dev
);
739 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
741 return sprintf(buf
, "%s\n",
742 omap_hsmmc_cover_is_closed(host
) ? "closed" : "open");
745 static DEVICE_ATTR(cover_switch
, S_IRUGO
, omap_hsmmc_show_cover_switch
, NULL
);
748 omap_hsmmc_show_slot_name(struct device
*dev
, struct device_attribute
*attr
,
751 struct mmc_host
*mmc
= container_of(dev
, struct mmc_host
, class_dev
);
752 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
754 return sprintf(buf
, "%s\n", mmc_slot(host
).name
);
757 static DEVICE_ATTR(slot_name
, S_IRUGO
, omap_hsmmc_show_slot_name
, NULL
);
760 * Configure the response type and send the cmd.
763 omap_hsmmc_start_command(struct omap_hsmmc_host
*host
, struct mmc_command
*cmd
,
764 struct mmc_data
*data
)
766 int cmdreg
= 0, resptype
= 0, cmdtype
= 0;
768 dev_vdbg(mmc_dev(host
->mmc
), "%s: CMD%d, argument 0x%08x\n",
769 mmc_hostname(host
->mmc
), cmd
->opcode
, cmd
->arg
);
772 omap_hsmmc_enable_irq(host
, cmd
);
774 host
->response_busy
= 0;
775 if (cmd
->flags
& MMC_RSP_PRESENT
) {
776 if (cmd
->flags
& MMC_RSP_136
)
778 else if (cmd
->flags
& MMC_RSP_BUSY
) {
780 host
->response_busy
= 1;
786 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
787 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
788 * a val of 0x3, rest 0x0.
790 if (cmd
== host
->mrq
->stop
)
793 cmdreg
= (cmd
->opcode
<< 24) | (resptype
<< 16) | (cmdtype
<< 22);
796 cmdreg
|= DP_SELECT
| MSBS
| BCE
;
797 if (data
->flags
& MMC_DATA_READ
)
806 host
->req_in_progress
= 1;
808 OMAP_HSMMC_WRITE(host
->base
, ARG
, cmd
->arg
);
809 OMAP_HSMMC_WRITE(host
->base
, CMD
, cmdreg
);
813 omap_hsmmc_get_dma_dir(struct omap_hsmmc_host
*host
, struct mmc_data
*data
)
815 if (data
->flags
& MMC_DATA_WRITE
)
816 return DMA_TO_DEVICE
;
818 return DMA_FROM_DEVICE
;
821 static struct dma_chan
*omap_hsmmc_get_dma_chan(struct omap_hsmmc_host
*host
,
822 struct mmc_data
*data
)
824 return data
->flags
& MMC_DATA_WRITE
? host
->tx_chan
: host
->rx_chan
;
827 static void omap_hsmmc_request_done(struct omap_hsmmc_host
*host
, struct mmc_request
*mrq
)
832 spin_lock_irqsave(&host
->irq_lock
, flags
);
833 host
->req_in_progress
= 0;
834 dma_ch
= host
->dma_ch
;
835 spin_unlock_irqrestore(&host
->irq_lock
, flags
);
837 omap_hsmmc_disable_irq(host
);
838 /* Do not complete the request if DMA is still in progress */
839 if (mrq
->data
&& host
->use_dma
&& dma_ch
!= -1)
842 mmc_request_done(host
->mmc
, mrq
);
846 * Notify the transfer complete to MMC core
849 omap_hsmmc_xfer_done(struct omap_hsmmc_host
*host
, struct mmc_data
*data
)
852 struct mmc_request
*mrq
= host
->mrq
;
854 /* TC before CC from CMD6 - don't know why, but it happens */
855 if (host
->cmd
&& host
->cmd
->opcode
== 6 &&
856 host
->response_busy
) {
857 host
->response_busy
= 0;
861 omap_hsmmc_request_done(host
, mrq
);
868 data
->bytes_xfered
+= data
->blocks
* (data
->blksz
);
870 data
->bytes_xfered
= 0;
873 omap_hsmmc_request_done(host
, data
->mrq
);
876 omap_hsmmc_start_command(host
, data
->stop
, NULL
);
880 * Notify the core about command completion
883 omap_hsmmc_cmd_done(struct omap_hsmmc_host
*host
, struct mmc_command
*cmd
)
887 if (cmd
->flags
& MMC_RSP_PRESENT
) {
888 if (cmd
->flags
& MMC_RSP_136
) {
889 /* response type 2 */
890 cmd
->resp
[3] = OMAP_HSMMC_READ(host
->base
, RSP10
);
891 cmd
->resp
[2] = OMAP_HSMMC_READ(host
->base
, RSP32
);
892 cmd
->resp
[1] = OMAP_HSMMC_READ(host
->base
, RSP54
);
893 cmd
->resp
[0] = OMAP_HSMMC_READ(host
->base
, RSP76
);
895 /* response types 1, 1b, 3, 4, 5, 6 */
896 cmd
->resp
[0] = OMAP_HSMMC_READ(host
->base
, RSP10
);
899 if ((host
->data
== NULL
&& !host
->response_busy
) || cmd
->error
)
900 omap_hsmmc_request_done(host
, cmd
->mrq
);
904 * DMA clean up for command errors
906 static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host
*host
, int errno
)
911 host
->data
->error
= errno
;
913 spin_lock_irqsave(&host
->irq_lock
, flags
);
914 dma_ch
= host
->dma_ch
;
916 spin_unlock_irqrestore(&host
->irq_lock
, flags
);
918 if (host
->use_dma
&& dma_ch
!= -1) {
919 struct dma_chan
*chan
= omap_hsmmc_get_dma_chan(host
, host
->data
);
921 dmaengine_terminate_all(chan
);
922 dma_unmap_sg(chan
->device
->dev
,
923 host
->data
->sg
, host
->data
->sg_len
,
924 omap_hsmmc_get_dma_dir(host
, host
->data
));
926 host
->data
->host_cookie
= 0;
932 * Readable error output
934 #ifdef CONFIG_MMC_DEBUG
935 static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host
*host
, u32 status
)
937 /* --- means reserved bit without definition at documentation */
938 static const char *omap_hsmmc_status_bits
[] = {
939 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
940 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
941 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
942 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
948 len
= sprintf(buf
, "MMC IRQ 0x%x :", status
);
951 for (i
= 0; i
< ARRAY_SIZE(omap_hsmmc_status_bits
); i
++)
952 if (status
& (1 << i
)) {
953 len
= sprintf(buf
, " %s", omap_hsmmc_status_bits
[i
]);
957 dev_vdbg(mmc_dev(host
->mmc
), "%s\n", res
);
960 static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host
*host
,
964 #endif /* CONFIG_MMC_DEBUG */
967 * MMC controller internal state machines reset
969 * Used to reset command or data internal state machines, using respectively
970 * SRC or SRD bit of SYSCTL register
971 * Can be called from interrupt context
973 static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host
*host
,
977 unsigned long limit
= (loops_per_jiffy
*
978 msecs_to_jiffies(MMC_TIMEOUT_MS
));
980 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
,
981 OMAP_HSMMC_READ(host
->base
, SYSCTL
) | bit
);
984 * OMAP4 ES2 and greater has an updated reset logic.
985 * Monitor a 0->1 transition first
987 if (mmc_slot(host
).features
& HSMMC_HAS_UPDATED_RESET
) {
988 while ((!(OMAP_HSMMC_READ(host
->base
, SYSCTL
) & bit
))
994 while ((OMAP_HSMMC_READ(host
->base
, SYSCTL
) & bit
) &&
998 if (OMAP_HSMMC_READ(host
->base
, SYSCTL
) & bit
)
999 dev_err(mmc_dev(host
->mmc
),
1000 "Timeout waiting on controller reset in %s\n",
1004 static void hsmmc_command_incomplete(struct omap_hsmmc_host
*host
,
1005 int err
, int end_cmd
)
1008 omap_hsmmc_reset_controller_fsm(host
, SRC
);
1010 host
->cmd
->error
= err
;
1014 omap_hsmmc_reset_controller_fsm(host
, SRD
);
1015 omap_hsmmc_dma_cleanup(host
, err
);
1016 } else if (host
->mrq
&& host
->mrq
->cmd
)
1017 host
->mrq
->cmd
->error
= err
;
1020 static void omap_hsmmc_do_irq(struct omap_hsmmc_host
*host
, int status
)
1022 struct mmc_data
*data
;
1023 int end_cmd
= 0, end_trans
= 0;
1026 dev_vdbg(mmc_dev(host
->mmc
), "IRQ Status is %x\n", status
);
1028 if (status
& ERR_EN
) {
1029 omap_hsmmc_dbg_report_irq(host
, status
);
1031 if (status
& (CTO_EN
| CCRC_EN
))
1033 if (status
& (CTO_EN
| DTO_EN
))
1034 hsmmc_command_incomplete(host
, -ETIMEDOUT
, end_cmd
);
1035 else if (status
& (CCRC_EN
| DCRC_EN
))
1036 hsmmc_command_incomplete(host
, -EILSEQ
, end_cmd
);
1038 if (host
->data
|| host
->response_busy
) {
1039 end_trans
= !end_cmd
;
1040 host
->response_busy
= 0;
1044 if (end_cmd
|| ((status
& CC_EN
) && host
->cmd
))
1045 omap_hsmmc_cmd_done(host
, host
->cmd
);
1046 if ((end_trans
|| (status
& TC_EN
)) && host
->mrq
)
1047 omap_hsmmc_xfer_done(host
, data
);
1051 * MMC controller IRQ handler
1053 static irqreturn_t
omap_hsmmc_irq(int irq
, void *dev_id
)
1055 struct omap_hsmmc_host
*host
= dev_id
;
1058 status
= OMAP_HSMMC_READ(host
->base
, STAT
);
1059 while (status
& INT_EN_MASK
&& host
->req_in_progress
) {
1060 omap_hsmmc_do_irq(host
, status
);
1062 /* Flush posted write */
1063 OMAP_HSMMC_WRITE(host
->base
, STAT
, status
);
1064 status
= OMAP_HSMMC_READ(host
->base
, STAT
);
1070 static void set_sd_bus_power(struct omap_hsmmc_host
*host
)
1074 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
1075 OMAP_HSMMC_READ(host
->base
, HCTL
) | SDBP
);
1076 for (i
= 0; i
< loops_per_jiffy
; i
++) {
1077 if (OMAP_HSMMC_READ(host
->base
, HCTL
) & SDBP
)
1084 * Switch MMC interface voltage ... only relevant for MMC1.
1086 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1087 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1088 * Some chips, like eMMC ones, use internal transceivers.
1090 static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host
*host
, int vdd
)
1095 /* Disable the clocks */
1096 pm_runtime_put_sync(host
->dev
);
1098 clk_disable_unprepare(host
->dbclk
);
1100 /* Turn the power off */
1101 ret
= mmc_slot(host
).set_power(host
->dev
, host
->slot_id
, 0, 0);
1103 /* Turn the power ON with given VDD 1.8 or 3.0v */
1105 ret
= mmc_slot(host
).set_power(host
->dev
, host
->slot_id
, 1,
1107 pm_runtime_get_sync(host
->dev
);
1109 clk_prepare_enable(host
->dbclk
);
1114 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
1115 OMAP_HSMMC_READ(host
->base
, HCTL
) & SDVSCLR
);
1116 reg_val
= OMAP_HSMMC_READ(host
->base
, HCTL
);
1119 * If a MMC dual voltage card is detected, the set_ios fn calls
1120 * this fn with VDD bit set for 1.8V. Upon card removal from the
1121 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
1123 * Cope with a bit of slop in the range ... per data sheets:
1124 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1125 * but recommended values are 1.71V to 1.89V
1126 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1127 * but recommended values are 2.7V to 3.3V
1129 * Board setup code shouldn't permit anything very out-of-range.
1130 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1131 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
1133 if ((1 << vdd
) <= MMC_VDD_23_24
)
1138 OMAP_HSMMC_WRITE(host
->base
, HCTL
, reg_val
);
1139 set_sd_bus_power(host
);
1143 dev_err(mmc_dev(host
->mmc
), "Unable to switch operating voltage\n");
1147 /* Protect the card while the cover is open */
1148 static void omap_hsmmc_protect_card(struct omap_hsmmc_host
*host
)
1150 if (!mmc_slot(host
).get_cover_state
)
1153 host
->reqs_blocked
= 0;
1154 if (mmc_slot(host
).get_cover_state(host
->dev
, host
->slot_id
)) {
1155 if (host
->protect_card
) {
1156 dev_info(host
->dev
, "%s: cover is closed, "
1157 "card is now accessible\n",
1158 mmc_hostname(host
->mmc
));
1159 host
->protect_card
= 0;
1162 if (!host
->protect_card
) {
1163 dev_info(host
->dev
, "%s: cover is open, "
1164 "card is now inaccessible\n",
1165 mmc_hostname(host
->mmc
));
1166 host
->protect_card
= 1;
1172 * irq handler to notify the core about card insertion/removal
1174 static irqreturn_t
omap_hsmmc_detect(int irq
, void *dev_id
)
1176 struct omap_hsmmc_host
*host
= dev_id
;
1177 struct omap_mmc_slot_data
*slot
= &mmc_slot(host
);
1180 if (host
->suspended
)
1183 sysfs_notify(&host
->mmc
->class_dev
.kobj
, NULL
, "cover_switch");
1185 if (slot
->card_detect
)
1186 carddetect
= slot
->card_detect(host
->dev
, host
->slot_id
);
1188 omap_hsmmc_protect_card(host
);
1189 carddetect
= -ENOSYS
;
1193 mmc_detect_change(host
->mmc
, (HZ
* 200) / 1000);
1195 mmc_detect_change(host
->mmc
, (HZ
* 50) / 1000);
1199 static void omap_hsmmc_dma_callback(void *param
)
1201 struct omap_hsmmc_host
*host
= param
;
1202 struct dma_chan
*chan
;
1203 struct mmc_data
*data
;
1204 int req_in_progress
;
1206 spin_lock_irq(&host
->irq_lock
);
1207 if (host
->dma_ch
< 0) {
1208 spin_unlock_irq(&host
->irq_lock
);
1212 data
= host
->mrq
->data
;
1213 chan
= omap_hsmmc_get_dma_chan(host
, data
);
1214 if (!data
->host_cookie
)
1215 dma_unmap_sg(chan
->device
->dev
,
1216 data
->sg
, data
->sg_len
,
1217 omap_hsmmc_get_dma_dir(host
, data
));
1219 req_in_progress
= host
->req_in_progress
;
1221 spin_unlock_irq(&host
->irq_lock
);
1223 /* If DMA has finished after TC, complete the request */
1224 if (!req_in_progress
) {
1225 struct mmc_request
*mrq
= host
->mrq
;
1228 mmc_request_done(host
->mmc
, mrq
);
1232 static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host
*host
,
1233 struct mmc_data
*data
,
1234 struct omap_hsmmc_next
*next
,
1235 struct dma_chan
*chan
)
1239 if (!next
&& data
->host_cookie
&&
1240 data
->host_cookie
!= host
->next_data
.cookie
) {
1241 dev_warn(host
->dev
, "[%s] invalid cookie: data->host_cookie %d"
1242 " host->next_data.cookie %d\n",
1243 __func__
, data
->host_cookie
, host
->next_data
.cookie
);
1244 data
->host_cookie
= 0;
1247 /* Check if next job is already prepared */
1249 (!next
&& data
->host_cookie
!= host
->next_data
.cookie
)) {
1250 dma_len
= dma_map_sg(chan
->device
->dev
, data
->sg
, data
->sg_len
,
1251 omap_hsmmc_get_dma_dir(host
, data
));
1254 dma_len
= host
->next_data
.dma_len
;
1255 host
->next_data
.dma_len
= 0;
1263 next
->dma_len
= dma_len
;
1264 data
->host_cookie
= ++next
->cookie
< 0 ? 1 : next
->cookie
;
1266 host
->dma_len
= dma_len
;
1272 * Routine to configure and start DMA for the MMC card
1274 static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host
*host
,
1275 struct mmc_request
*req
)
1277 struct dma_slave_config cfg
;
1278 struct dma_async_tx_descriptor
*tx
;
1280 struct mmc_data
*data
= req
->data
;
1281 struct dma_chan
*chan
;
1283 /* Sanity check: all the SG entries must be aligned by block size. */
1284 for (i
= 0; i
< data
->sg_len
; i
++) {
1285 struct scatterlist
*sgl
;
1288 if (sgl
->length
% data
->blksz
)
1291 if ((data
->blksz
% 4) != 0)
1292 /* REVISIT: The MMC buffer increments only when MSB is written.
1293 * Return error for blksz which is non multiple of four.
1297 BUG_ON(host
->dma_ch
!= -1);
1299 chan
= omap_hsmmc_get_dma_chan(host
, data
);
1301 cfg
.src_addr
= host
->mapbase
+ OMAP_HSMMC_DATA
;
1302 cfg
.dst_addr
= host
->mapbase
+ OMAP_HSMMC_DATA
;
1303 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
1304 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
1305 cfg
.src_maxburst
= data
->blksz
/ 4;
1306 cfg
.dst_maxburst
= data
->blksz
/ 4;
1308 ret
= dmaengine_slave_config(chan
, &cfg
);
1312 ret
= omap_hsmmc_pre_dma_transfer(host
, data
, NULL
, chan
);
1316 tx
= dmaengine_prep_slave_sg(chan
, data
->sg
, data
->sg_len
,
1317 data
->flags
& MMC_DATA_WRITE
? DMA_MEM_TO_DEV
: DMA_DEV_TO_MEM
,
1318 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1320 dev_err(mmc_dev(host
->mmc
), "prep_slave_sg() failed\n");
1321 /* FIXME: cleanup */
1325 tx
->callback
= omap_hsmmc_dma_callback
;
1326 tx
->callback_param
= host
;
1329 dmaengine_submit(tx
);
1333 dma_async_issue_pending(chan
);
1338 static void set_data_timeout(struct omap_hsmmc_host
*host
,
1339 unsigned int timeout_ns
,
1340 unsigned int timeout_clks
)
1342 unsigned int timeout
, cycle_ns
;
1343 uint32_t reg
, clkd
, dto
= 0;
1345 reg
= OMAP_HSMMC_READ(host
->base
, SYSCTL
);
1346 clkd
= (reg
& CLKD_MASK
) >> CLKD_SHIFT
;
1350 cycle_ns
= 1000000000 / (clk_get_rate(host
->fclk
) / clkd
);
1351 timeout
= timeout_ns
/ cycle_ns
;
1352 timeout
+= timeout_clks
;
1354 while ((timeout
& 0x80000000) == 0) {
1371 reg
|= dto
<< DTO_SHIFT
;
1372 OMAP_HSMMC_WRITE(host
->base
, SYSCTL
, reg
);
1376 * Configure block length for MMC/SD cards and initiate the transfer.
1379 omap_hsmmc_prepare_data(struct omap_hsmmc_host
*host
, struct mmc_request
*req
)
1382 host
->data
= req
->data
;
1384 if (req
->data
== NULL
) {
1385 OMAP_HSMMC_WRITE(host
->base
, BLK
, 0);
1387 * Set an arbitrary 100ms data timeout for commands with
1390 if (req
->cmd
->flags
& MMC_RSP_BUSY
)
1391 set_data_timeout(host
, 100000000U, 0);
1395 OMAP_HSMMC_WRITE(host
->base
, BLK
, (req
->data
->blksz
)
1396 | (req
->data
->blocks
<< 16));
1397 set_data_timeout(host
, req
->data
->timeout_ns
, req
->data
->timeout_clks
);
1399 if (host
->use_dma
) {
1400 ret
= omap_hsmmc_start_dma_transfer(host
, req
);
1402 dev_err(mmc_dev(host
->mmc
), "MMC start dma failure\n");
1409 static void omap_hsmmc_post_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
,
1412 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
1413 struct mmc_data
*data
= mrq
->data
;
1415 if (host
->use_dma
&& data
->host_cookie
) {
1416 struct dma_chan
*c
= omap_hsmmc_get_dma_chan(host
, data
);
1418 dma_unmap_sg(c
->device
->dev
, data
->sg
, data
->sg_len
,
1419 omap_hsmmc_get_dma_dir(host
, data
));
1420 data
->host_cookie
= 0;
1424 static void omap_hsmmc_pre_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
,
1427 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
1429 if (mrq
->data
->host_cookie
) {
1430 mrq
->data
->host_cookie
= 0;
1434 if (host
->use_dma
) {
1435 struct dma_chan
*c
= omap_hsmmc_get_dma_chan(host
, mrq
->data
);
1437 if (omap_hsmmc_pre_dma_transfer(host
, mrq
->data
,
1438 &host
->next_data
, c
))
1439 mrq
->data
->host_cookie
= 0;
1444 * Request function. for read/write operation
1446 static void omap_hsmmc_request(struct mmc_host
*mmc
, struct mmc_request
*req
)
1448 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
1451 BUG_ON(host
->req_in_progress
);
1452 BUG_ON(host
->dma_ch
!= -1);
1453 if (host
->protect_card
) {
1454 if (host
->reqs_blocked
< 3) {
1456 * Ensure the controller is left in a consistent
1457 * state by resetting the command and data state
1460 omap_hsmmc_reset_controller_fsm(host
, SRD
);
1461 omap_hsmmc_reset_controller_fsm(host
, SRC
);
1462 host
->reqs_blocked
+= 1;
1464 req
->cmd
->error
= -EBADF
;
1466 req
->data
->error
= -EBADF
;
1467 req
->cmd
->retries
= 0;
1468 mmc_request_done(mmc
, req
);
1470 } else if (host
->reqs_blocked
)
1471 host
->reqs_blocked
= 0;
1472 WARN_ON(host
->mrq
!= NULL
);
1474 err
= omap_hsmmc_prepare_data(host
, req
);
1476 req
->cmd
->error
= err
;
1478 req
->data
->error
= err
;
1480 mmc_request_done(mmc
, req
);
1484 omap_hsmmc_start_command(host
, req
->cmd
, req
->data
);
1487 /* Routine to configure clock values. Exposed API to core */
1488 static void omap_hsmmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1490 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
1491 int do_send_init_stream
= 0;
1493 pm_runtime_get_sync(host
->dev
);
1495 if (ios
->power_mode
!= host
->power_mode
) {
1496 switch (ios
->power_mode
) {
1498 mmc_slot(host
).set_power(host
->dev
, host
->slot_id
,
1502 mmc_slot(host
).set_power(host
->dev
, host
->slot_id
,
1506 do_send_init_stream
= 1;
1509 host
->power_mode
= ios
->power_mode
;
1512 /* FIXME: set registers based only on changes to ios */
1514 omap_hsmmc_set_bus_width(host
);
1516 if (host
->pdata
->controller_flags
& OMAP_HSMMC_SUPPORTS_DUAL_VOLT
) {
1517 /* Only MMC1 can interface at 3V without some flavor
1518 * of external transceiver; but they all handle 1.8V.
1520 if ((OMAP_HSMMC_READ(host
->base
, HCTL
) & SDVSDET
) &&
1521 (ios
->vdd
== DUAL_VOLT_OCR_BIT
) &&
1523 * With pbias cell programming missing, this
1524 * can't be allowed on MMC1 when booting with device
1527 !host
->pbias_disable
) {
1529 * The mmc_select_voltage fn of the core does
1530 * not seem to set the power_mode to
1531 * MMC_POWER_UP upon recalculating the voltage.
1534 if (omap_hsmmc_switch_opcond(host
, ios
->vdd
) != 0)
1535 dev_dbg(mmc_dev(host
->mmc
),
1536 "Switch operation failed\n");
1540 omap_hsmmc_set_clock(host
);
1542 if (do_send_init_stream
)
1543 send_init_stream(host
);
1545 omap_hsmmc_set_bus_mode(host
);
1547 pm_runtime_put_autosuspend(host
->dev
);
1550 static int omap_hsmmc_get_cd(struct mmc_host
*mmc
)
1552 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
1554 if (!mmc_slot(host
).card_detect
)
1556 return mmc_slot(host
).card_detect(host
->dev
, host
->slot_id
);
1559 static int omap_hsmmc_get_ro(struct mmc_host
*mmc
)
1561 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
1563 if (!mmc_slot(host
).get_ro
)
1565 return mmc_slot(host
).get_ro(host
->dev
, 0);
1568 static void omap_hsmmc_init_card(struct mmc_host
*mmc
, struct mmc_card
*card
)
1570 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
1572 if (mmc_slot(host
).init_card
)
1573 mmc_slot(host
).init_card(card
);
1576 static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host
*host
)
1578 u32 hctl
, capa
, value
;
1580 /* Only MMC1 supports 3.0V */
1581 if (host
->pdata
->controller_flags
& OMAP_HSMMC_SUPPORTS_DUAL_VOLT
) {
1589 value
= OMAP_HSMMC_READ(host
->base
, HCTL
) & ~SDVS_MASK
;
1590 OMAP_HSMMC_WRITE(host
->base
, HCTL
, value
| hctl
);
1592 value
= OMAP_HSMMC_READ(host
->base
, CAPA
);
1593 OMAP_HSMMC_WRITE(host
->base
, CAPA
, value
| capa
);
1595 /* Set SD bus power bit */
1596 set_sd_bus_power(host
);
1599 static int omap_hsmmc_enable_fclk(struct mmc_host
*mmc
)
1601 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
1603 pm_runtime_get_sync(host
->dev
);
1608 static int omap_hsmmc_disable_fclk(struct mmc_host
*mmc
)
1610 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
1612 pm_runtime_mark_last_busy(host
->dev
);
1613 pm_runtime_put_autosuspend(host
->dev
);
1618 static const struct mmc_host_ops omap_hsmmc_ops
= {
1619 .enable
= omap_hsmmc_enable_fclk
,
1620 .disable
= omap_hsmmc_disable_fclk
,
1621 .post_req
= omap_hsmmc_post_req
,
1622 .pre_req
= omap_hsmmc_pre_req
,
1623 .request
= omap_hsmmc_request
,
1624 .set_ios
= omap_hsmmc_set_ios
,
1625 .get_cd
= omap_hsmmc_get_cd
,
1626 .get_ro
= omap_hsmmc_get_ro
,
1627 .init_card
= omap_hsmmc_init_card
,
1628 /* NYET -- enable_sdio_irq */
1631 #ifdef CONFIG_DEBUG_FS
1633 static int omap_hsmmc_regs_show(struct seq_file
*s
, void *data
)
1635 struct mmc_host
*mmc
= s
->private;
1636 struct omap_hsmmc_host
*host
= mmc_priv(mmc
);
1637 int context_loss
= 0;
1639 if (host
->pdata
->get_context_loss_count
)
1640 context_loss
= host
->pdata
->get_context_loss_count(host
->dev
);
1642 seq_printf(s
, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1643 mmc
->index
, host
->context_loss
, context_loss
);
1645 if (host
->suspended
) {
1646 seq_printf(s
, "host suspended, can't read registers\n");
1650 pm_runtime_get_sync(host
->dev
);
1652 seq_printf(s
, "CON:\t\t0x%08x\n",
1653 OMAP_HSMMC_READ(host
->base
, CON
));
1654 seq_printf(s
, "HCTL:\t\t0x%08x\n",
1655 OMAP_HSMMC_READ(host
->base
, HCTL
));
1656 seq_printf(s
, "SYSCTL:\t\t0x%08x\n",
1657 OMAP_HSMMC_READ(host
->base
, SYSCTL
));
1658 seq_printf(s
, "IE:\t\t0x%08x\n",
1659 OMAP_HSMMC_READ(host
->base
, IE
));
1660 seq_printf(s
, "ISE:\t\t0x%08x\n",
1661 OMAP_HSMMC_READ(host
->base
, ISE
));
1662 seq_printf(s
, "CAPA:\t\t0x%08x\n",
1663 OMAP_HSMMC_READ(host
->base
, CAPA
));
1665 pm_runtime_mark_last_busy(host
->dev
);
1666 pm_runtime_put_autosuspend(host
->dev
);
1671 static int omap_hsmmc_regs_open(struct inode
*inode
, struct file
*file
)
1673 return single_open(file
, omap_hsmmc_regs_show
, inode
->i_private
);
1676 static const struct file_operations mmc_regs_fops
= {
1677 .open
= omap_hsmmc_regs_open
,
1679 .llseek
= seq_lseek
,
1680 .release
= single_release
,
1683 static void omap_hsmmc_debugfs(struct mmc_host
*mmc
)
1685 if (mmc
->debugfs_root
)
1686 debugfs_create_file("regs", S_IRUSR
, mmc
->debugfs_root
,
1687 mmc
, &mmc_regs_fops
);
1692 static void omap_hsmmc_debugfs(struct mmc_host
*mmc
)
1699 static u16 omap4_reg_offset
= 0x100;
1701 static const struct of_device_id omap_mmc_of_match
[] = {
1703 .compatible
= "ti,omap2-hsmmc",
1706 .compatible
= "ti,omap3-hsmmc",
1709 .compatible
= "ti,omap4-hsmmc",
1710 .data
= &omap4_reg_offset
,
1714 MODULE_DEVICE_TABLE(of
, omap_mmc_of_match
);
1716 static struct omap_mmc_platform_data
*of_get_hsmmc_pdata(struct device
*dev
)
1718 struct omap_mmc_platform_data
*pdata
;
1719 struct device_node
*np
= dev
->of_node
;
1720 u32 bus_width
, max_freq
;
1721 int cd_gpio
, wp_gpio
;
1723 cd_gpio
= of_get_named_gpio(np
, "cd-gpios", 0);
1724 wp_gpio
= of_get_named_gpio(np
, "wp-gpios", 0);
1725 if (cd_gpio
== -EPROBE_DEFER
|| wp_gpio
== -EPROBE_DEFER
)
1726 return ERR_PTR(-EPROBE_DEFER
);
1728 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
1730 return NULL
; /* out of memory */
1732 if (of_find_property(np
, "ti,dual-volt", NULL
))
1733 pdata
->controller_flags
|= OMAP_HSMMC_SUPPORTS_DUAL_VOLT
;
1735 /* This driver only supports 1 slot */
1736 pdata
->nr_slots
= 1;
1737 pdata
->slots
[0].switch_pin
= cd_gpio
;
1738 pdata
->slots
[0].gpio_wp
= wp_gpio
;
1740 if (of_find_property(np
, "ti,non-removable", NULL
)) {
1741 pdata
->slots
[0].nonremovable
= true;
1742 pdata
->slots
[0].no_regulator_off_init
= true;
1744 of_property_read_u32(np
, "bus-width", &bus_width
);
1746 pdata
->slots
[0].caps
|= MMC_CAP_4_BIT_DATA
;
1747 else if (bus_width
== 8)
1748 pdata
->slots
[0].caps
|= MMC_CAP_8_BIT_DATA
;
1750 if (of_find_property(np
, "ti,needs-special-reset", NULL
))
1751 pdata
->slots
[0].features
|= HSMMC_HAS_UPDATED_RESET
;
1753 if (!of_property_read_u32(np
, "max-frequency", &max_freq
))
1754 pdata
->max_freq
= max_freq
;
1756 if (of_find_property(np
, "ti,needs-special-hs-handling", NULL
))
1757 pdata
->slots
[0].features
|= HSMMC_HAS_HSPE_SUPPORT
;
1762 static inline struct omap_mmc_platform_data
1763 *of_get_hsmmc_pdata(struct device
*dev
)
1769 static int omap_hsmmc_probe(struct platform_device
*pdev
)
1771 struct omap_mmc_platform_data
*pdata
= pdev
->dev
.platform_data
;
1772 struct mmc_host
*mmc
;
1773 struct omap_hsmmc_host
*host
= NULL
;
1774 struct resource
*res
;
1776 const struct of_device_id
*match
;
1777 dma_cap_mask_t mask
;
1778 unsigned tx_req
, rx_req
;
1779 struct pinctrl
*pinctrl
;
1781 match
= of_match_device(of_match_ptr(omap_mmc_of_match
), &pdev
->dev
);
1783 pdata
= of_get_hsmmc_pdata(&pdev
->dev
);
1786 return PTR_ERR(pdata
);
1789 const u16
*offsetp
= match
->data
;
1790 pdata
->reg_offset
= *offsetp
;
1794 if (pdata
== NULL
) {
1795 dev_err(&pdev
->dev
, "Platform Data is missing\n");
1799 if (pdata
->nr_slots
== 0) {
1800 dev_err(&pdev
->dev
, "No Slots\n");
1804 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1805 irq
= platform_get_irq(pdev
, 0);
1806 if (res
== NULL
|| irq
< 0)
1809 res
= request_mem_region(res
->start
, resource_size(res
), pdev
->name
);
1813 ret
= omap_hsmmc_gpio_init(pdata
);
1817 mmc
= mmc_alloc_host(sizeof(struct omap_hsmmc_host
), &pdev
->dev
);
1823 host
= mmc_priv(mmc
);
1825 host
->pdata
= pdata
;
1826 host
->dev
= &pdev
->dev
;
1831 host
->mapbase
= res
->start
+ pdata
->reg_offset
;
1832 host
->base
= ioremap(host
->mapbase
, SZ_4K
);
1833 host
->power_mode
= MMC_POWER_OFF
;
1834 host
->next_data
.cookie
= 1;
1836 platform_set_drvdata(pdev
, host
);
1838 mmc
->ops
= &omap_hsmmc_ops
;
1841 * If regulator_disable can only put vcc_aux to sleep then there is
1844 if (mmc_slot(host
).vcc_aux_disable_is_sleep
)
1845 mmc_slot(host
).no_off
= 1;
1847 mmc
->f_min
= OMAP_MMC_MIN_CLOCK
;
1849 if (pdata
->max_freq
> 0)
1850 mmc
->f_max
= pdata
->max_freq
;
1852 mmc
->f_max
= OMAP_MMC_MAX_CLOCK
;
1854 spin_lock_init(&host
->irq_lock
);
1856 host
->fclk
= clk_get(&pdev
->dev
, "fck");
1857 if (IS_ERR(host
->fclk
)) {
1858 ret
= PTR_ERR(host
->fclk
);
1863 if (host
->pdata
->controller_flags
& OMAP_HSMMC_BROKEN_MULTIBLOCK_READ
) {
1864 dev_info(&pdev
->dev
, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1865 mmc
->caps2
|= MMC_CAP2_NO_MULTI_READ
;
1868 pm_runtime_enable(host
->dev
);
1869 pm_runtime_get_sync(host
->dev
);
1870 pm_runtime_set_autosuspend_delay(host
->dev
, MMC_AUTOSUSPEND_DELAY
);
1871 pm_runtime_use_autosuspend(host
->dev
);
1873 omap_hsmmc_context_save(host
);
1875 /* This can be removed once we support PBIAS with DT */
1876 if (host
->dev
->of_node
&& host
->mapbase
== 0x4809c000)
1877 host
->pbias_disable
= 1;
1879 host
->dbclk
= clk_get(&pdev
->dev
, "mmchsdb_fck");
1881 * MMC can still work without debounce clock.
1883 if (IS_ERR(host
->dbclk
)) {
1885 } else if (clk_prepare_enable(host
->dbclk
) != 0) {
1886 dev_warn(mmc_dev(host
->mmc
), "Failed to enable debounce clk\n");
1887 clk_put(host
->dbclk
);
1891 /* Since we do only SG emulation, we can have as many segs
1893 mmc
->max_segs
= 1024;
1895 mmc
->max_blk_size
= 512; /* Block Length at max can be 1024 */
1896 mmc
->max_blk_count
= 0xFFFF; /* No. of Blocks is 16 bits */
1897 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
1898 mmc
->max_seg_size
= mmc
->max_req_size
;
1900 mmc
->caps
|= MMC_CAP_MMC_HIGHSPEED
| MMC_CAP_SD_HIGHSPEED
|
1901 MMC_CAP_WAIT_WHILE_BUSY
| MMC_CAP_ERASE
;
1903 mmc
->caps
|= mmc_slot(host
).caps
;
1904 if (mmc
->caps
& MMC_CAP_8_BIT_DATA
)
1905 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1907 if (mmc_slot(host
).nonremovable
)
1908 mmc
->caps
|= MMC_CAP_NONREMOVABLE
;
1910 mmc
->pm_caps
= mmc_slot(host
).pm_caps
;
1912 omap_hsmmc_conf_bus_power(host
);
1914 if (!pdev
->dev
.of_node
) {
1915 res
= platform_get_resource_byname(pdev
, IORESOURCE_DMA
, "tx");
1917 dev_err(mmc_dev(host
->mmc
), "cannot get DMA TX channel\n");
1921 tx_req
= res
->start
;
1923 res
= platform_get_resource_byname(pdev
, IORESOURCE_DMA
, "rx");
1925 dev_err(mmc_dev(host
->mmc
), "cannot get DMA RX channel\n");
1929 rx_req
= res
->start
;
1933 dma_cap_set(DMA_SLAVE
, mask
);
1936 dma_request_slave_channel_compat(mask
, omap_dma_filter_fn
,
1937 &rx_req
, &pdev
->dev
, "rx");
1939 if (!host
->rx_chan
) {
1940 dev_err(mmc_dev(host
->mmc
), "unable to obtain RX DMA engine channel %u\n", rx_req
);
1946 dma_request_slave_channel_compat(mask
, omap_dma_filter_fn
,
1947 &tx_req
, &pdev
->dev
, "tx");
1949 if (!host
->tx_chan
) {
1950 dev_err(mmc_dev(host
->mmc
), "unable to obtain TX DMA engine channel %u\n", tx_req
);
1955 /* Request IRQ for MMC operations */
1956 ret
= request_irq(host
->irq
, omap_hsmmc_irq
, 0,
1957 mmc_hostname(mmc
), host
);
1959 dev_err(mmc_dev(host
->mmc
), "Unable to grab HSMMC IRQ\n");
1963 if (pdata
->init
!= NULL
) {
1964 if (pdata
->init(&pdev
->dev
) != 0) {
1965 dev_err(mmc_dev(host
->mmc
),
1966 "Unable to configure MMC IRQs\n");
1967 goto err_irq_cd_init
;
1971 if (omap_hsmmc_have_reg() && !mmc_slot(host
).set_power
) {
1972 ret
= omap_hsmmc_reg_get(host
);
1978 mmc
->ocr_avail
= mmc_slot(host
).ocr_mask
;
1980 /* Request IRQ for card detect */
1981 if ((mmc_slot(host
).card_detect_irq
)) {
1982 ret
= request_threaded_irq(mmc_slot(host
).card_detect_irq
,
1985 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
1986 mmc_hostname(mmc
), host
);
1988 dev_err(mmc_dev(host
->mmc
),
1989 "Unable to grab MMC CD IRQ\n");
1992 pdata
->suspend
= omap_hsmmc_suspend_cdirq
;
1993 pdata
->resume
= omap_hsmmc_resume_cdirq
;
1996 omap_hsmmc_disable_irq(host
);
1998 pinctrl
= devm_pinctrl_get_select_default(&pdev
->dev
);
1999 if (IS_ERR(pinctrl
))
2000 dev_warn(&pdev
->dev
,
2001 "pins are not configured from the driver\n");
2003 omap_hsmmc_protect_card(host
);
2007 if (mmc_slot(host
).name
!= NULL
) {
2008 ret
= device_create_file(&mmc
->class_dev
, &dev_attr_slot_name
);
2012 if (mmc_slot(host
).card_detect_irq
&& mmc_slot(host
).get_cover_state
) {
2013 ret
= device_create_file(&mmc
->class_dev
,
2014 &dev_attr_cover_switch
);
2019 omap_hsmmc_debugfs(mmc
);
2020 pm_runtime_mark_last_busy(host
->dev
);
2021 pm_runtime_put_autosuspend(host
->dev
);
2026 mmc_remove_host(mmc
);
2027 free_irq(mmc_slot(host
).card_detect_irq
, host
);
2030 omap_hsmmc_reg_put(host
);
2032 if (host
->pdata
->cleanup
)
2033 host
->pdata
->cleanup(&pdev
->dev
);
2035 free_irq(host
->irq
, host
);
2038 dma_release_channel(host
->tx_chan
);
2040 dma_release_channel(host
->rx_chan
);
2041 pm_runtime_put_sync(host
->dev
);
2042 pm_runtime_disable(host
->dev
);
2043 clk_put(host
->fclk
);
2045 clk_disable_unprepare(host
->dbclk
);
2046 clk_put(host
->dbclk
);
2049 iounmap(host
->base
);
2052 omap_hsmmc_gpio_free(pdata
);
2054 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2056 release_mem_region(res
->start
, resource_size(res
));
2060 static int omap_hsmmc_remove(struct platform_device
*pdev
)
2062 struct omap_hsmmc_host
*host
= platform_get_drvdata(pdev
);
2063 struct resource
*res
;
2065 pm_runtime_get_sync(host
->dev
);
2066 mmc_remove_host(host
->mmc
);
2068 omap_hsmmc_reg_put(host
);
2069 if (host
->pdata
->cleanup
)
2070 host
->pdata
->cleanup(&pdev
->dev
);
2071 free_irq(host
->irq
, host
);
2072 if (mmc_slot(host
).card_detect_irq
)
2073 free_irq(mmc_slot(host
).card_detect_irq
, host
);
2076 dma_release_channel(host
->tx_chan
);
2078 dma_release_channel(host
->rx_chan
);
2080 pm_runtime_put_sync(host
->dev
);
2081 pm_runtime_disable(host
->dev
);
2082 clk_put(host
->fclk
);
2084 clk_disable_unprepare(host
->dbclk
);
2085 clk_put(host
->dbclk
);
2088 omap_hsmmc_gpio_free(host
->pdata
);
2089 iounmap(host
->base
);
2090 mmc_free_host(host
->mmc
);
2092 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2094 release_mem_region(res
->start
, resource_size(res
));
2100 static int omap_hsmmc_prepare(struct device
*dev
)
2102 struct omap_hsmmc_host
*host
= dev_get_drvdata(dev
);
2104 if (host
->pdata
->suspend
)
2105 return host
->pdata
->suspend(dev
, host
->slot_id
);
2110 static void omap_hsmmc_complete(struct device
*dev
)
2112 struct omap_hsmmc_host
*host
= dev_get_drvdata(dev
);
2114 if (host
->pdata
->resume
)
2115 host
->pdata
->resume(dev
, host
->slot_id
);
2119 static int omap_hsmmc_suspend(struct device
*dev
)
2122 struct omap_hsmmc_host
*host
= dev_get_drvdata(dev
);
2127 if (host
&& host
->suspended
)
2130 pm_runtime_get_sync(host
->dev
);
2131 host
->suspended
= 1;
2132 ret
= mmc_suspend_host(host
->mmc
);
2135 host
->suspended
= 0;
2139 if (!(host
->mmc
->pm_flags
& MMC_PM_KEEP_POWER
)) {
2140 omap_hsmmc_disable_irq(host
);
2141 OMAP_HSMMC_WRITE(host
->base
, HCTL
,
2142 OMAP_HSMMC_READ(host
->base
, HCTL
) & ~SDBP
);
2146 clk_disable_unprepare(host
->dbclk
);
2148 pm_runtime_put_sync(host
->dev
);
2152 /* Routine to resume the MMC device */
2153 static int omap_hsmmc_resume(struct device
*dev
)
2156 struct omap_hsmmc_host
*host
= dev_get_drvdata(dev
);
2161 if (host
&& !host
->suspended
)
2164 pm_runtime_get_sync(host
->dev
);
2167 clk_prepare_enable(host
->dbclk
);
2169 if (!(host
->mmc
->pm_flags
& MMC_PM_KEEP_POWER
))
2170 omap_hsmmc_conf_bus_power(host
);
2172 omap_hsmmc_protect_card(host
);
2174 /* Notify the core to resume the host */
2175 ret
= mmc_resume_host(host
->mmc
);
2177 host
->suspended
= 0;
2179 pm_runtime_mark_last_busy(host
->dev
);
2180 pm_runtime_put_autosuspend(host
->dev
);
2187 #define omap_hsmmc_prepare NULL
2188 #define omap_hsmmc_complete NULL
2189 #define omap_hsmmc_suspend NULL
2190 #define omap_hsmmc_resume NULL
2193 static int omap_hsmmc_runtime_suspend(struct device
*dev
)
2195 struct omap_hsmmc_host
*host
;
2197 host
= platform_get_drvdata(to_platform_device(dev
));
2198 omap_hsmmc_context_save(host
);
2199 dev_dbg(dev
, "disabled\n");
2204 static int omap_hsmmc_runtime_resume(struct device
*dev
)
2206 struct omap_hsmmc_host
*host
;
2208 host
= platform_get_drvdata(to_platform_device(dev
));
2209 omap_hsmmc_context_restore(host
);
2210 dev_dbg(dev
, "enabled\n");
2215 static struct dev_pm_ops omap_hsmmc_dev_pm_ops
= {
2216 .suspend
= omap_hsmmc_suspend
,
2217 .resume
= omap_hsmmc_resume
,
2218 .prepare
= omap_hsmmc_prepare
,
2219 .complete
= omap_hsmmc_complete
,
2220 .runtime_suspend
= omap_hsmmc_runtime_suspend
,
2221 .runtime_resume
= omap_hsmmc_runtime_resume
,
2224 static struct platform_driver omap_hsmmc_driver
= {
2225 .probe
= omap_hsmmc_probe
,
2226 .remove
= omap_hsmmc_remove
,
2228 .name
= DRIVER_NAME
,
2229 .owner
= THIS_MODULE
,
2230 .pm
= &omap_hsmmc_dev_pm_ops
,
2231 .of_match_table
= of_match_ptr(omap_mmc_of_match
),
2235 module_platform_driver(omap_hsmmc_driver
);
2236 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2237 MODULE_LICENSE("GPL");
2238 MODULE_ALIAS("platform:" DRIVER_NAME
);
2239 MODULE_AUTHOR("Texas Instruments Inc");