3 * Texas Instruments, <www.ti.com>
4 * Sukumar Ghorai <s-ghorai@ti.com>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation's version 2 of
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 #include <asm/arch/mmc_host_def.h>
35 #include <asm/arch/sys_proto.h>
37 /* common definitions for all OMAPs */
38 #define SYSCTL_SRC (1 << 25)
39 #define SYSCTL_SRD (1 << 26)
41 struct omap_hsmmc_data {
42 struct hsmmc *base_addr;
45 /* If we fail after 1 second wait, something is really bad */
46 #define MAX_RETRY_MS 1000
48 static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
49 static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
51 static struct mmc hsmmc_dev[3];
52 static struct omap_hsmmc_data hsmmc_dev_data[3];
54 #if defined(CONFIG_OMAP44XX) && defined(CONFIG_TWL6030_POWER)
55 static void omap4_vmmc_pbias_config(struct mmc *mmc)
58 struct omap_sys_ctrl_regs *const ctrl =
59 (struct omap_sys_ctrl_regs *) SYSCTRL_GENERAL_CORE_BASE;
62 value = readl(&ctrl->control_pbiaslite);
63 value &= ~(MMC1_PBIASLITE_PWRDNZ | MMC1_PWRDNZ);
64 writel(value, &ctrl->control_pbiaslite);
66 twl6030_power_mmc_init();
67 value = readl(&ctrl->control_pbiaslite);
68 value |= MMC1_PBIASLITE_VMODE | MMC1_PBIASLITE_PWRDNZ | MMC1_PWRDNZ;
69 writel(value, &ctrl->control_pbiaslite);
73 #if defined(CONFIG_OMAP54XX) && defined(CONFIG_TWL6035_POWER)
74 static void omap5_pbias_config(struct mmc *mmc)
77 struct omap_sys_ctrl_regs *const ctrl =
78 (struct omap_sys_ctrl_regs *) SYSCTRL_GENERAL_CORE_BASE;
80 value = readl(&ctrl->control_pbias);
81 value &= ~(SDCARD_PWRDNZ | SDCARD_BIAS_PWRDNZ);
82 value |= SDCARD_BIAS_HIZ_MODE;
83 writel(value, &ctrl->control_pbias);
85 twl6035_mmc1_poweron_ldo();
87 value = readl(&ctrl->control_pbias);
88 value &= ~SDCARD_BIAS_HIZ_MODE;
89 value |= SDCARD_PBIASLITE_VMODE | SDCARD_PWRDNZ | SDCARD_BIAS_PWRDNZ;
90 writel(value, &ctrl->control_pbias);
92 value = readl(&ctrl->control_pbias);
93 if (value & (1 << 23)) {
94 value &= ~(SDCARD_PWRDNZ | SDCARD_BIAS_PWRDNZ);
95 value |= SDCARD_BIAS_HIZ_MODE;
96 writel(value, &ctrl->control_pbias);
101 unsigned char mmc_board_init(struct mmc *mmc)
103 #if defined(CONFIG_OMAP34XX)
104 t2_t *t2_base = (t2_t *)T2_BASE;
105 struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
108 pbias_lite = readl(&t2_base->pbias_lite);
109 pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
110 writel(pbias_lite, &t2_base->pbias_lite);
112 #if defined(CONFIG_TWL4030_POWER)
113 twl4030_power_mmc_init();
114 mdelay(100); /* ramp-up delay from Linux code */
116 #if defined(CONFIG_OMAP34XX)
117 writel(pbias_lite | PBIASLITEPWRDNZ1 |
118 PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
119 &t2_base->pbias_lite);
121 writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
124 writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
127 /* Change from default of 52MHz to 26MHz if necessary */
128 if (!(mmc->host_caps & MMC_MODE_HS_52MHz))
129 writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
130 &t2_base->ctl_prog_io1);
132 writel(readl(&prcm_base->fclken1_core) |
133 EN_MMC1 | EN_MMC2 | EN_MMC3,
134 &prcm_base->fclken1_core);
136 writel(readl(&prcm_base->iclken1_core) |
137 EN_MMC1 | EN_MMC2 | EN_MMC3,
138 &prcm_base->iclken1_core);
141 #if defined(CONFIG_OMAP44XX) && defined(CONFIG_TWL6030_POWER)
142 /* PBIAS config needed for MMC1 only */
143 if (mmc->block_dev.dev == 0)
144 omap4_vmmc_pbias_config(mmc);
146 #if defined(CONFIG_OMAP54XX) && defined(CONFIG_TWL6035_POWER)
147 if (mmc->block_dev.dev == 0)
148 omap5_pbias_config(mmc);
154 void mmc_init_stream(struct hsmmc *mmc_base)
158 writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
160 writel(MMC_CMD0, &mmc_base->cmd);
161 start = get_timer(0);
162 while (!(readl(&mmc_base->stat) & CC_MASK)) {
163 if (get_timer(0) - start > MAX_RETRY_MS) {
164 printf("%s: timedout waiting for cc!\n", __func__);
168 writel(CC_MASK, &mmc_base->stat)
170 writel(MMC_CMD0, &mmc_base->cmd)
172 start = get_timer(0);
173 while (!(readl(&mmc_base->stat) & CC_MASK)) {
174 if (get_timer(0) - start > MAX_RETRY_MS) {
175 printf("%s: timedout waiting for cc2!\n", __func__);
179 writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
183 static int mmc_init_setup(struct mmc *mmc)
185 struct hsmmc *mmc_base;
186 unsigned int reg_val;
190 mmc_base = ((struct omap_hsmmc_data *)mmc->priv)->base_addr;
193 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
194 &mmc_base->sysconfig);
195 start = get_timer(0);
196 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
197 if (get_timer(0) - start > MAX_RETRY_MS) {
198 printf("%s: timedout waiting for cc2!\n", __func__);
202 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
203 start = get_timer(0);
204 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
205 if (get_timer(0) - start > MAX_RETRY_MS) {
206 printf("%s: timedout waiting for softresetall!\n",
211 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
212 writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
215 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
217 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
218 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
219 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
222 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
223 (ICE_STOP | DTO_15THDTO | CEN_DISABLE));
224 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
225 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
226 start = get_timer(0);
227 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
228 if (get_timer(0) - start > MAX_RETRY_MS) {
229 printf("%s: timedout waiting for ics!\n", __func__);
233 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
235 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
237 writel(IE_BADA | IE_CERR | IE_DEB | IE_DCRC | IE_DTO | IE_CIE |
238 IE_CEB | IE_CCRC | IE_CTO | IE_BRR | IE_BWR | IE_TC | IE_CC,
241 mmc_init_stream(mmc_base);
247 * MMC controller internal finite state machine reset
249 * Used to reset command or data internal state machines, using respectively
250 * SRC or SRD bit of SYSCTL register
252 static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
256 mmc_reg_out(&mmc_base->sysctl, bit, bit);
258 start = get_timer(0);
259 while ((readl(&mmc_base->sysctl) & bit) != 0) {
260 if (get_timer(0) - start > MAX_RETRY_MS) {
261 printf("%s: timedout waiting for sysctl %x to clear\n",
268 static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
269 struct mmc_data *data)
271 struct hsmmc *mmc_base;
272 unsigned int flags, mmc_stat;
275 mmc_base = ((struct omap_hsmmc_data *)mmc->priv)->base_addr;
276 start = get_timer(0);
277 while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
278 if (get_timer(0) - start > MAX_RETRY_MS) {
279 printf("%s: timedout waiting on cmd inhibit to clear\n",
284 writel(0xFFFFFFFF, &mmc_base->stat);
285 start = get_timer(0);
286 while (readl(&mmc_base->stat)) {
287 if (get_timer(0) - start > MAX_RETRY_MS) {
288 printf("%s: timedout waiting for STAT (%x) to clear\n",
289 __func__, readl(&mmc_base->stat));
295 * CMDIDX[13:8] : Command index
296 * DATAPRNT[5] : Data Present Select
297 * ENCMDIDX[4] : Command Index Check Enable
298 * ENCMDCRC[3] : Command CRC Check Enable
303 * 11 = Length 48 Check busy after response
305 /* Delay added before checking the status of frq change
306 * retry not supported by mmc.c(core file)
308 if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
309 udelay(50000); /* wait 50 ms */
311 if (!(cmd->resp_type & MMC_RSP_PRESENT))
313 else if (cmd->resp_type & MMC_RSP_136)
314 flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
315 else if (cmd->resp_type & MMC_RSP_BUSY)
316 flags = RSP_TYPE_LGHT48B;
318 flags = RSP_TYPE_LGHT48;
320 /* enable default flags */
321 flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
322 MSBS_SGLEBLK | ACEN_DISABLE | BCE_DISABLE | DE_DISABLE);
324 if (cmd->resp_type & MMC_RSP_CRC)
326 if (cmd->resp_type & MMC_RSP_OPCODE)
330 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
331 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
332 flags |= (MSBS_MULTIBLK | BCE_ENABLE);
333 data->blocksize = 512;
334 writel(data->blocksize | (data->blocks << 16),
337 writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
339 if (data->flags & MMC_DATA_READ)
340 flags |= (DP_DATA | DDIR_READ);
342 flags |= (DP_DATA | DDIR_WRITE);
345 writel(cmd->cmdarg, &mmc_base->arg);
346 writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
348 start = get_timer(0);
350 mmc_stat = readl(&mmc_base->stat);
351 if (get_timer(0) - start > MAX_RETRY_MS) {
352 printf("%s : timeout: No status update\n", __func__);
357 if ((mmc_stat & IE_CTO) != 0) {
358 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
360 } else if ((mmc_stat & ERRI_MASK) != 0)
363 if (mmc_stat & CC_MASK) {
364 writel(CC_MASK, &mmc_base->stat);
365 if (cmd->resp_type & MMC_RSP_PRESENT) {
366 if (cmd->resp_type & MMC_RSP_136) {
367 /* response type 2 */
368 cmd->response[3] = readl(&mmc_base->rsp10);
369 cmd->response[2] = readl(&mmc_base->rsp32);
370 cmd->response[1] = readl(&mmc_base->rsp54);
371 cmd->response[0] = readl(&mmc_base->rsp76);
373 /* response types 1, 1b, 3, 4, 5, 6 */
374 cmd->response[0] = readl(&mmc_base->rsp10);
378 if (data && (data->flags & MMC_DATA_READ)) {
379 mmc_read_data(mmc_base, data->dest,
380 data->blocksize * data->blocks);
381 } else if (data && (data->flags & MMC_DATA_WRITE)) {
382 mmc_write_data(mmc_base, data->src,
383 data->blocksize * data->blocks);
388 static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
390 unsigned int *output_buf = (unsigned int *)buf;
391 unsigned int mmc_stat;
397 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
401 ulong start = get_timer(0);
403 mmc_stat = readl(&mmc_base->stat);
404 if (get_timer(0) - start > MAX_RETRY_MS) {
405 printf("%s: timedout waiting for status!\n",
409 } while (mmc_stat == 0);
411 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
412 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
414 if ((mmc_stat & ERRI_MASK) != 0)
417 if (mmc_stat & BRR_MASK) {
420 writel(readl(&mmc_base->stat) | BRR_MASK,
422 for (k = 0; k < count; k++) {
423 *output_buf = readl(&mmc_base->data);
429 if (mmc_stat & BWR_MASK)
430 writel(readl(&mmc_base->stat) | BWR_MASK,
433 if (mmc_stat & TC_MASK) {
434 writel(readl(&mmc_base->stat) | TC_MASK,
442 static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
445 unsigned int *input_buf = (unsigned int *)buf;
446 unsigned int mmc_stat;
452 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
456 ulong start = get_timer(0);
458 mmc_stat = readl(&mmc_base->stat);
459 if (get_timer(0) - start > MAX_RETRY_MS) {
460 printf("%s: timedout waiting for status!\n",
464 } while (mmc_stat == 0);
466 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
467 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
469 if ((mmc_stat & ERRI_MASK) != 0)
472 if (mmc_stat & BWR_MASK) {
475 writel(readl(&mmc_base->stat) | BWR_MASK,
477 for (k = 0; k < count; k++) {
478 writel(*input_buf, &mmc_base->data);
484 if (mmc_stat & BRR_MASK)
485 writel(readl(&mmc_base->stat) | BRR_MASK,
488 if (mmc_stat & TC_MASK) {
489 writel(readl(&mmc_base->stat) | TC_MASK,
497 static void mmc_set_ios(struct mmc *mmc)
499 struct hsmmc *mmc_base;
500 unsigned int dsor = 0;
503 mmc_base = ((struct omap_hsmmc_data *)mmc->priv)->base_addr;
504 /* configue bus width */
505 switch (mmc->bus_width) {
507 writel(readl(&mmc_base->con) | DTW_8_BITMODE,
512 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
514 writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
520 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
522 writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
527 /* configure clock with 96Mhz system clock.
529 if (mmc->clock != 0) {
530 dsor = (MMC_CLOCK_REFERENCE * 1000000 / mmc->clock);
531 if ((MMC_CLOCK_REFERENCE * 1000000) / dsor > mmc->clock)
535 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
536 (ICE_STOP | DTO_15THDTO | CEN_DISABLE));
538 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
539 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
541 start = get_timer(0);
542 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
543 if (get_timer(0) - start > MAX_RETRY_MS) {
544 printf("%s: timedout waiting for ics!\n", __func__);
548 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
551 int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max)
553 struct mmc *mmc = &hsmmc_dev[dev_index];
554 struct omap_hsmmc_data *priv_data = &hsmmc_dev_data[dev_index];
556 sprintf(mmc->name, "OMAP SD/MMC");
557 mmc->send_cmd = mmc_send_cmd;
558 mmc->set_ios = mmc_set_ios;
559 mmc->init = mmc_init_setup;
561 mmc->priv = priv_data;
565 priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
567 #ifdef OMAP_HSMMC2_BASE
569 priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
572 #ifdef OMAP_HSMMC3_BASE
574 priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
578 priv_data->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
581 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
582 mmc->host_caps = (MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS |
583 MMC_MODE_HC) & ~host_caps_mask;
590 if (mmc->host_caps & MMC_MODE_HS) {
591 if (mmc->host_caps & MMC_MODE_HS_52MHz)
592 mmc->f_max = 52000000;
594 mmc->f_max = 26000000;
596 mmc->f_max = 20000000;
601 #if defined(CONFIG_OMAP34XX)
603 * Silicon revs 2.1 and older do not support multiblock transfers.
605 if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))