2 * linux/arch/arm/mach-omap2/hsmmc.c
4 * Copyright (C) 2007-2008 Texas Instruments
5 * Copyright (C) 2008 Nokia Corporation
6 * Author: Texas Instruments
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/delay.h>
16 #include <linux/gpio.h>
17 #include <mach/hardware.h>
18 #include <linux/platform_data/gpio-omap.h>
21 #include <plat/omap-pm.h>
23 #include <plat/omap_device.h>
29 #if defined(CONFIG_MMC_OMAP_HS) || defined(CONFIG_MMC_OMAP_HS_MODULE)
31 static u16 control_pbias_offset;
32 static u16 control_devconf1_offset;
33 static u16 control_mmc1;
35 #define HSMMC_NAME_LEN 9
37 #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
39 static int hsmmc_get_context_loss(struct device *dev)
41 return omap_pm_get_dev_context_loss_count(dev);
45 #define hsmmc_get_context_loss NULL
48 static void omap_hsmmc1_before_set_reg(struct device *dev, int slot,
49 int power_on, int vdd)
52 struct omap_mmc_platform_data *mmc = dev->platform_data;
54 if (mmc->slots[0].remux)
55 mmc->slots[0].remux(dev, slot, power_on);
58 * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
59 * card with Vcc regulator (from twl4030 or whatever). OMAP has both
60 * 1.8V and 3.0V modes, controlled by the PBIAS register.
62 * In 8-bit modes, OMAP VMMC1A (for DAT4..7) needs a supply, which
63 * is most naturally TWL VSIM; those pins also use PBIAS.
65 * FIXME handle VMMC1A as needed ...
68 if (cpu_is_omap2430()) {
69 reg = omap_ctrl_readl(OMAP243X_CONTROL_DEVCONF1);
70 if ((1 << vdd) >= MMC_VDD_30_31)
71 reg |= OMAP243X_MMC1_ACTIVE_OVERWRITE;
73 reg &= ~OMAP243X_MMC1_ACTIVE_OVERWRITE;
74 omap_ctrl_writel(reg, OMAP243X_CONTROL_DEVCONF1);
77 if (mmc->slots[0].internal_clock) {
78 reg = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0);
79 reg |= OMAP2_MMCSDIO1ADPCLKISEL;
80 omap_ctrl_writel(reg, OMAP2_CONTROL_DEVCONF0);
83 reg = omap_ctrl_readl(control_pbias_offset);
84 if (cpu_is_omap3630()) {
85 /* Set MMC I/O to 52Mhz */
86 prog_io = omap_ctrl_readl(OMAP343X_CONTROL_PROG_IO1);
87 prog_io |= OMAP3630_PRG_SDMMC1_SPEEDCTRL;
88 omap_ctrl_writel(prog_io, OMAP343X_CONTROL_PROG_IO1);
90 reg |= OMAP2_PBIASSPEEDCTRL0;
92 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
93 omap_ctrl_writel(reg, control_pbias_offset);
95 reg = omap_ctrl_readl(control_pbias_offset);
96 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
97 omap_ctrl_writel(reg, control_pbias_offset);
101 static void omap_hsmmc1_after_set_reg(struct device *dev, int slot,
102 int power_on, int vdd)
106 /* 100ms delay required for PBIAS configuration */
110 reg = omap_ctrl_readl(control_pbias_offset);
111 reg |= (OMAP2_PBIASLITEPWRDNZ0 | OMAP2_PBIASSPEEDCTRL0);
112 if ((1 << vdd) <= MMC_VDD_165_195)
113 reg &= ~OMAP2_PBIASLITEVMODE0;
115 reg |= OMAP2_PBIASLITEVMODE0;
116 omap_ctrl_writel(reg, control_pbias_offset);
118 reg = omap_ctrl_readl(control_pbias_offset);
119 reg |= (OMAP2_PBIASSPEEDCTRL0 | OMAP2_PBIASLITEPWRDNZ0 |
120 OMAP2_PBIASLITEVMODE0);
121 omap_ctrl_writel(reg, control_pbias_offset);
125 static void omap4_hsmmc1_before_set_reg(struct device *dev, int slot,
126 int power_on, int vdd)
131 * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
132 * card with Vcc regulator (from twl4030 or whatever). OMAP has both
133 * 1.8V and 3.0V modes, controlled by the PBIAS register.
135 reg = omap4_ctrl_pad_readl(control_pbias_offset);
136 reg &= ~(OMAP4_MMC1_PBIASLITE_PWRDNZ_MASK |
137 OMAP4_MMC1_PWRDNZ_MASK |
138 OMAP4_MMC1_PBIASLITE_VMODE_MASK);
139 omap4_ctrl_pad_writel(reg, control_pbias_offset);
142 static void omap4_hsmmc1_after_set_reg(struct device *dev, int slot,
143 int power_on, int vdd)
146 unsigned long timeout;
149 reg = omap4_ctrl_pad_readl(control_pbias_offset);
150 reg |= OMAP4_MMC1_PBIASLITE_PWRDNZ_MASK;
151 if ((1 << vdd) <= MMC_VDD_165_195)
152 reg &= ~OMAP4_MMC1_PBIASLITE_VMODE_MASK;
154 reg |= OMAP4_MMC1_PBIASLITE_VMODE_MASK;
155 reg |= (OMAP4_MMC1_PBIASLITE_PWRDNZ_MASK |
156 OMAP4_MMC1_PWRDNZ_MASK);
157 omap4_ctrl_pad_writel(reg, control_pbias_offset);
159 timeout = jiffies + msecs_to_jiffies(5);
161 reg = omap4_ctrl_pad_readl(control_pbias_offset);
162 if (!(reg & OMAP4_MMC1_PBIASLITE_VMODE_ERROR_MASK))
164 usleep_range(100, 200);
165 } while (!time_after(jiffies, timeout));
167 if (reg & OMAP4_MMC1_PBIASLITE_VMODE_ERROR_MASK) {
168 pr_err("Pbias Voltage is not same as LDO\n");
169 /* Caution : On VMODE_ERROR Power Down MMC IO */
170 reg &= ~(OMAP4_MMC1_PWRDNZ_MASK);
171 omap4_ctrl_pad_writel(reg, control_pbias_offset);
176 static void hsmmc2_select_input_clk_src(struct omap_mmc_platform_data *mmc)
180 reg = omap_ctrl_readl(control_devconf1_offset);
181 if (mmc->slots[0].internal_clock)
182 reg |= OMAP2_MMCSDIO2ADPCLKISEL;
184 reg &= ~OMAP2_MMCSDIO2ADPCLKISEL;
185 omap_ctrl_writel(reg, control_devconf1_offset);
188 static void hsmmc2_before_set_reg(struct device *dev, int slot,
189 int power_on, int vdd)
191 struct omap_mmc_platform_data *mmc = dev->platform_data;
193 if (mmc->slots[0].remux)
194 mmc->slots[0].remux(dev, slot, power_on);
197 hsmmc2_select_input_clk_src(mmc);
200 static int am35x_hsmmc2_set_power(struct device *dev, int slot,
201 int power_on, int vdd)
203 struct omap_mmc_platform_data *mmc = dev->platform_data;
206 hsmmc2_select_input_clk_src(mmc);
211 static int nop_mmc_set_power(struct device *dev, int slot, int power_on,
217 static inline void omap_hsmmc_mux(struct omap_mmc_platform_data *mmc_controller,
220 if (gpio_is_valid(mmc_controller->slots[0].switch_pin) &&
221 (mmc_controller->slots[0].switch_pin < OMAP_MAX_GPIO_LINES))
222 omap_mux_init_gpio(mmc_controller->slots[0].switch_pin,
223 OMAP_PIN_INPUT_PULLUP);
224 if (gpio_is_valid(mmc_controller->slots[0].gpio_wp) &&
225 (mmc_controller->slots[0].gpio_wp < OMAP_MAX_GPIO_LINES))
226 omap_mux_init_gpio(mmc_controller->slots[0].gpio_wp,
227 OMAP_PIN_INPUT_PULLUP);
228 if (cpu_is_omap34xx()) {
229 if (controller_nr == 0) {
230 omap_mux_init_signal("sdmmc1_clk",
231 OMAP_PIN_INPUT_PULLUP);
232 omap_mux_init_signal("sdmmc1_cmd",
233 OMAP_PIN_INPUT_PULLUP);
234 omap_mux_init_signal("sdmmc1_dat0",
235 OMAP_PIN_INPUT_PULLUP);
236 if (mmc_controller->slots[0].caps &
237 (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)) {
238 omap_mux_init_signal("sdmmc1_dat1",
239 OMAP_PIN_INPUT_PULLUP);
240 omap_mux_init_signal("sdmmc1_dat2",
241 OMAP_PIN_INPUT_PULLUP);
242 omap_mux_init_signal("sdmmc1_dat3",
243 OMAP_PIN_INPUT_PULLUP);
245 if (mmc_controller->slots[0].caps &
246 MMC_CAP_8_BIT_DATA) {
247 omap_mux_init_signal("sdmmc1_dat4",
248 OMAP_PIN_INPUT_PULLUP);
249 omap_mux_init_signal("sdmmc1_dat5",
250 OMAP_PIN_INPUT_PULLUP);
251 omap_mux_init_signal("sdmmc1_dat6",
252 OMAP_PIN_INPUT_PULLUP);
253 omap_mux_init_signal("sdmmc1_dat7",
254 OMAP_PIN_INPUT_PULLUP);
257 if (controller_nr == 1) {
259 omap_mux_init_signal("sdmmc2_clk",
260 OMAP_PIN_INPUT_PULLUP);
261 omap_mux_init_signal("sdmmc2_cmd",
262 OMAP_PIN_INPUT_PULLUP);
263 omap_mux_init_signal("sdmmc2_dat0",
264 OMAP_PIN_INPUT_PULLUP);
267 * For 8 wire configurations, Lines DAT4, 5, 6 and 7
268 * need to be muxed in the board-*.c files
270 if (mmc_controller->slots[0].caps &
271 (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)) {
272 omap_mux_init_signal("sdmmc2_dat1",
273 OMAP_PIN_INPUT_PULLUP);
274 omap_mux_init_signal("sdmmc2_dat2",
275 OMAP_PIN_INPUT_PULLUP);
276 omap_mux_init_signal("sdmmc2_dat3",
277 OMAP_PIN_INPUT_PULLUP);
279 if (mmc_controller->slots[0].caps &
280 MMC_CAP_8_BIT_DATA) {
281 omap_mux_init_signal("sdmmc2_dat4.sdmmc2_dat4",
282 OMAP_PIN_INPUT_PULLUP);
283 omap_mux_init_signal("sdmmc2_dat5.sdmmc2_dat5",
284 OMAP_PIN_INPUT_PULLUP);
285 omap_mux_init_signal("sdmmc2_dat6.sdmmc2_dat6",
286 OMAP_PIN_INPUT_PULLUP);
287 omap_mux_init_signal("sdmmc2_dat7.sdmmc2_dat7",
288 OMAP_PIN_INPUT_PULLUP);
293 * For MMC3 the pins need to be muxed in the board-*.c files
298 static int __init omap_hsmmc_pdata_init(struct omap2_hsmmc_info *c,
299 struct omap_mmc_platform_data *mmc)
303 hc_name = kzalloc(sizeof(char) * (HSMMC_NAME_LEN + 1), GFP_KERNEL);
305 pr_err("Cannot allocate memory for controller slot name\n");
311 strncpy(hc_name, c->name, HSMMC_NAME_LEN);
313 snprintf(hc_name, (HSMMC_NAME_LEN + 1), "mmc%islot%i",
315 mmc->slots[0].name = hc_name;
317 mmc->slots[0].caps = c->caps;
318 mmc->slots[0].pm_caps = c->pm_caps;
319 mmc->slots[0].internal_clock = !c->ext_clock;
320 mmc->max_freq = c->max_freq;
321 if (cpu_is_omap44xx())
322 mmc->reg_offset = OMAP4_MMC_REG_OFFSET;
326 mmc->get_context_loss_count = hsmmc_get_context_loss;
328 mmc->slots[0].switch_pin = c->gpio_cd;
329 mmc->slots[0].gpio_wp = c->gpio_wp;
331 mmc->slots[0].remux = c->remux;
332 mmc->slots[0].init_card = c->init_card;
335 mmc->slots[0].cover = 1;
338 mmc->slots[0].nonremovable = 1;
341 mmc->slots[0].power_saving = 1;
344 mmc->slots[0].no_off = 1;
347 mmc->slots[0].no_regulator_off_init = c->no_off_init;
349 if (c->vcc_aux_disable_is_sleep)
350 mmc->slots[0].vcc_aux_disable_is_sleep = 1;
353 * NOTE: MMC slots should have a Vcc regulator set up.
354 * This may be from a TWL4030-family chip, another
355 * controllable regulator, or a fixed supply.
357 * temporary HACK: ocr_mask instead of fixed supply
360 mmc->slots[0].ocr_mask = MMC_VDD_165_195 |
367 mmc->slots[0].ocr_mask = c->ocr_mask;
369 if (!soc_is_am35xx())
370 mmc->slots[0].features |= HSMMC_HAS_PBIAS;
372 if (cpu_is_omap44xx() && (omap_rev() > OMAP4430_REV_ES1_0))
373 mmc->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
377 if (mmc->slots[0].features & HSMMC_HAS_PBIAS) {
378 /* on-chip level shifting via PBIAS0/PBIAS1 */
379 if (cpu_is_omap44xx()) {
380 mmc->slots[0].before_set_reg =
381 omap4_hsmmc1_before_set_reg;
382 mmc->slots[0].after_set_reg =
383 omap4_hsmmc1_after_set_reg;
385 mmc->slots[0].before_set_reg =
386 omap_hsmmc1_before_set_reg;
387 mmc->slots[0].after_set_reg =
388 omap_hsmmc1_after_set_reg;
393 mmc->slots[0].set_power = nop_mmc_set_power;
395 /* OMAP3630 HSMMC1 supports only 4-bit */
396 if (cpu_is_omap3630() &&
397 (c->caps & MMC_CAP_8_BIT_DATA)) {
398 c->caps &= ~MMC_CAP_8_BIT_DATA;
399 c->caps |= MMC_CAP_4_BIT_DATA;
400 mmc->slots[0].caps = c->caps;
405 mmc->slots[0].set_power = am35x_hsmmc2_set_power;
409 if (c->transceiver && (c->caps & MMC_CAP_8_BIT_DATA)) {
410 c->caps &= ~MMC_CAP_8_BIT_DATA;
411 c->caps |= MMC_CAP_4_BIT_DATA;
413 if (mmc->slots[0].features & HSMMC_HAS_PBIAS) {
414 /* off-chip level shifting, or none */
415 mmc->slots[0].before_set_reg = hsmmc2_before_set_reg;
416 mmc->slots[0].after_set_reg = NULL;
422 mmc->slots[0].before_set_reg = NULL;
423 mmc->slots[0].after_set_reg = NULL;
426 pr_err("MMC%d configuration not supported!\n", c->mmc);
433 static int omap_hsmmc_done;
435 void omap_hsmmc_late_init(struct omap2_hsmmc_info *c)
437 struct platform_device *pdev;
438 struct omap_mmc_platform_data *mmc_pdata;
441 if (omap_hsmmc_done != 1)
446 for (; c->mmc; c++) {
454 mmc_pdata = pdev->dev.platform_data;
458 mmc_pdata->slots[0].switch_pin = c->gpio_cd;
459 mmc_pdata->slots[0].gpio_wp = c->gpio_wp;
461 res = omap_device_register(pdev);
463 pr_err("Could not late init MMC %s\n",
468 #define MAX_OMAP_MMC_HWMOD_NAME_LEN 16
470 static void __init omap_hsmmc_init_one(struct omap2_hsmmc_info *hsmmcinfo,
473 struct omap_hwmod *oh;
474 struct omap_hwmod *ohs[1];
475 struct omap_device *od;
476 struct platform_device *pdev;
477 char oh_name[MAX_OMAP_MMC_HWMOD_NAME_LEN];
478 struct omap_mmc_platform_data *mmc_data;
479 struct omap_mmc_dev_attr *mmc_dev_attr;
483 mmc_data = kzalloc(sizeof(struct omap_mmc_platform_data), GFP_KERNEL);
485 pr_err("Cannot allocate memory for mmc device!\n");
489 res = omap_hsmmc_pdata_init(hsmmcinfo, mmc_data);
493 omap_hsmmc_mux(mmc_data, (ctrl_nr - 1));
496 res = snprintf(oh_name, MAX_OMAP_MMC_HWMOD_NAME_LEN,
498 WARN(res >= MAX_OMAP_MMC_HWMOD_NAME_LEN,
499 "String buffer overflow in MMC%d device setup\n", ctrl_nr);
501 oh = omap_hwmod_lookup(oh_name);
503 pr_err("Could not look up %s\n", oh_name);
507 if (oh->dev_attr != NULL) {
508 mmc_dev_attr = oh->dev_attr;
509 mmc_data->controller_flags = mmc_dev_attr->flags;
511 * erratum 2.1.1.128 doesn't apply if board has
512 * a transceiver is attached
514 if (hsmmcinfo->transceiver)
515 mmc_data->controller_flags &=
516 ~OMAP_HSMMC_BROKEN_MULTIBLOCK_READ;
519 pdev = platform_device_alloc(name, ctrl_nr - 1);
521 pr_err("Could not allocate pdev for %s\n", name);
524 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
526 od = omap_device_alloc(pdev, ohs, 1, NULL, 0);
528 pr_err("Could not allocate od for %s\n", name);
532 res = platform_device_add_data(pdev, mmc_data,
533 sizeof(struct omap_mmc_platform_data));
535 pr_err("Could not add pdata for %s\n", name);
539 hsmmcinfo->pdev = pdev;
541 if (hsmmcinfo->deferred)
544 res = omap_device_register(pdev);
546 pr_err("Could not register od for %s\n", name);
553 omap_device_delete(od);
556 platform_device_put(pdev);
559 kfree(mmc_data->slots[0].name);
565 void __init omap_hsmmc_init(struct omap2_hsmmc_info *controllers)
574 if (!cpu_is_omap44xx()) {
575 if (cpu_is_omap2430()) {
576 control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
577 control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
579 control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
580 control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
583 control_pbias_offset =
584 OMAP4_CTRL_MODULE_PAD_CORE_CONTROL_PBIASLITE;
585 control_mmc1 = OMAP4_CTRL_MODULE_PAD_CORE_CONTROL_MMC1;
586 reg = omap4_ctrl_pad_readl(control_mmc1);
587 reg |= (OMAP4_SDMMC1_PUSTRENGTH_GRP0_MASK |
588 OMAP4_SDMMC1_PUSTRENGTH_GRP1_MASK);
589 reg &= ~(OMAP4_SDMMC1_PUSTRENGTH_GRP2_MASK |
590 OMAP4_SDMMC1_PUSTRENGTH_GRP3_MASK);
591 reg |= (OMAP4_SDMMC1_DR0_SPEEDCTRL_MASK |
592 OMAP4_SDMMC1_DR1_SPEEDCTRL_MASK |
593 OMAP4_SDMMC1_DR2_SPEEDCTRL_MASK);
594 omap4_ctrl_pad_writel(reg, control_mmc1);
597 for (; controllers->mmc; controllers++)
598 omap_hsmmc_init_one(controllers, controllers->mmc);