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,
33 #include <asm/arch/mmc_host_def.h>
34 #include <asm/arch/sys_proto.h>
36 /* common definitions for all OMAPs */
37 #define SYSCTL_SRC (1 << 25)
38 #define SYSCTL_SRD (1 << 26)
40 /* If we fail after 1 second wait, something is really bad */
41 #define MAX_RETRY_MS 1000
43 static int mmc_read_data(struct hsmmc
*mmc_base
, char *buf
, unsigned int size
);
44 static int mmc_write_data(struct hsmmc
*mmc_base
, const char *buf
,
46 static struct mmc hsmmc_dev
[2];
48 #if defined(CONFIG_OMAP44XX) && defined(CONFIG_TWL6030_POWER)
49 static void omap4_vmmc_pbias_config(struct mmc
*mmc
)
52 struct omap4_sys_ctrl_regs
*const ctrl
=
53 (struct omap4_sys_ctrl_regs
*)SYSCTRL_GENERAL_CORE_BASE
;
56 value
= readl(&ctrl
->control_pbiaslite
);
57 value
&= ~(MMC1_PBIASLITE_PWRDNZ
| MMC1_PWRDNZ
);
58 writel(value
, &ctrl
->control_pbiaslite
);
60 twl6030_power_mmc_init();
61 value
= readl(&ctrl
->control_pbiaslite
);
62 value
|= MMC1_PBIASLITE_VMODE
| MMC1_PBIASLITE_PWRDNZ
| MMC1_PWRDNZ
;
63 writel(value
, &ctrl
->control_pbiaslite
);
67 unsigned char mmc_board_init(struct mmc
*mmc
)
69 #if defined(CONFIG_OMAP34XX)
70 t2_t
*t2_base
= (t2_t
*)T2_BASE
;
71 struct prcm
*prcm_base
= (struct prcm
*)PRCM_BASE
;
74 pbias_lite
= readl(&t2_base
->pbias_lite
);
75 pbias_lite
&= ~(PBIASLITEPWRDNZ1
| PBIASLITEPWRDNZ0
);
76 writel(pbias_lite
, &t2_base
->pbias_lite
);
78 #if defined(CONFIG_TWL4030_POWER)
79 twl4030_power_mmc_init();
80 mdelay(100); /* ramp-up delay from Linux code */
82 #if defined(CONFIG_OMAP34XX)
83 writel(pbias_lite
| PBIASLITEPWRDNZ1
|
84 PBIASSPEEDCTRL0
| PBIASLITEPWRDNZ0
,
85 &t2_base
->pbias_lite
);
87 writel(readl(&t2_base
->devconf0
) | MMCSDIO1ADPCLKISEL
,
90 writel(readl(&t2_base
->devconf1
) | MMCSDIO2ADPCLKISEL
,
93 writel(readl(&prcm_base
->fclken1_core
) |
94 EN_MMC1
| EN_MMC2
| EN_MMC3
,
95 &prcm_base
->fclken1_core
);
97 writel(readl(&prcm_base
->iclken1_core
) |
98 EN_MMC1
| EN_MMC2
| EN_MMC3
,
99 &prcm_base
->iclken1_core
);
102 #if defined(CONFIG_OMAP44XX) && defined(CONFIG_TWL6030_POWER)
103 /* PBIAS config needed for MMC1 only */
104 if (mmc
->block_dev
.dev
== 0)
105 omap4_vmmc_pbias_config(mmc
);
111 void mmc_init_stream(struct hsmmc
*mmc_base
)
115 writel(readl(&mmc_base
->con
) | INIT_INITSTREAM
, &mmc_base
->con
);
117 writel(MMC_CMD0
, &mmc_base
->cmd
);
118 start
= get_timer(0);
119 while (!(readl(&mmc_base
->stat
) & CC_MASK
)) {
120 if (get_timer(0) - start
> MAX_RETRY_MS
) {
121 printf("%s: timedout waiting for cc!\n", __func__
);
125 writel(CC_MASK
, &mmc_base
->stat
)
127 writel(MMC_CMD0
, &mmc_base
->cmd
)
129 start
= get_timer(0);
130 while (!(readl(&mmc_base
->stat
) & CC_MASK
)) {
131 if (get_timer(0) - start
> MAX_RETRY_MS
) {
132 printf("%s: timedout waiting for cc2!\n", __func__
);
136 writel(readl(&mmc_base
->con
) & ~INIT_INITSTREAM
, &mmc_base
->con
);
140 static int mmc_init_setup(struct mmc
*mmc
)
142 struct hsmmc
*mmc_base
= (struct hsmmc
*)mmc
->priv
;
143 unsigned int reg_val
;
149 writel(readl(&mmc_base
->sysconfig
) | MMC_SOFTRESET
,
150 &mmc_base
->sysconfig
);
151 start
= get_timer(0);
152 while ((readl(&mmc_base
->sysstatus
) & RESETDONE
) == 0) {
153 if (get_timer(0) - start
> MAX_RETRY_MS
) {
154 printf("%s: timedout waiting for cc2!\n", __func__
);
158 writel(readl(&mmc_base
->sysctl
) | SOFTRESETALL
, &mmc_base
->sysctl
);
159 start
= get_timer(0);
160 while ((readl(&mmc_base
->sysctl
) & SOFTRESETALL
) != 0x0) {
161 if (get_timer(0) - start
> MAX_RETRY_MS
) {
162 printf("%s: timedout waiting for softresetall!\n",
167 writel(DTW_1_BITMODE
| SDBP_PWROFF
| SDVS_3V0
, &mmc_base
->hctl
);
168 writel(readl(&mmc_base
->capa
) | VS30_3V0SUP
| VS18_1V8SUP
,
171 reg_val
= readl(&mmc_base
->con
) & RESERVED_MASK
;
173 writel(CTPL_MMC_SD
| reg_val
| WPP_ACTIVEHIGH
| CDP_ACTIVEHIGH
|
174 MIT_CTO
| DW8_1_4BITMODE
| MODE_FUNC
| STR_BLOCK
|
175 HR_NOHOSTRESP
| INIT_NOINIT
| NOOPENDRAIN
, &mmc_base
->con
);
178 mmc_reg_out(&mmc_base
->sysctl
, (ICE_MASK
| DTO_MASK
| CEN_MASK
),
179 (ICE_STOP
| DTO_15THDTO
| CEN_DISABLE
));
180 mmc_reg_out(&mmc_base
->sysctl
, ICE_MASK
| CLKD_MASK
,
181 (dsor
<< CLKD_OFFSET
) | ICE_OSCILLATE
);
182 start
= get_timer(0);
183 while ((readl(&mmc_base
->sysctl
) & ICS_MASK
) == ICS_NOTREADY
) {
184 if (get_timer(0) - start
> MAX_RETRY_MS
) {
185 printf("%s: timedout waiting for ics!\n", __func__
);
189 writel(readl(&mmc_base
->sysctl
) | CEN_ENABLE
, &mmc_base
->sysctl
);
191 writel(readl(&mmc_base
->hctl
) | SDBP_PWRON
, &mmc_base
->hctl
);
193 writel(IE_BADA
| IE_CERR
| IE_DEB
| IE_DCRC
| IE_DTO
| IE_CIE
|
194 IE_CEB
| IE_CCRC
| IE_CTO
| IE_BRR
| IE_BWR
| IE_TC
| IE_CC
,
197 mmc_init_stream(mmc_base
);
203 * MMC controller internal finite state machine reset
205 * Used to reset command or data internal state machines, using respectively
206 * SRC or SRD bit of SYSCTL register
208 static void mmc_reset_controller_fsm(struct hsmmc
*mmc_base
, u32 bit
)
212 mmc_reg_out(&mmc_base
->sysctl
, bit
, bit
);
214 start
= get_timer(0);
215 while ((readl(&mmc_base
->sysctl
) & bit
) != 0) {
216 if (get_timer(0) - start
> MAX_RETRY_MS
) {
217 printf("%s: timedout waiting for sysctl %x to clear\n",
224 static int mmc_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
,
225 struct mmc_data
*data
)
227 struct hsmmc
*mmc_base
= (struct hsmmc
*)mmc
->priv
;
228 unsigned int flags
, mmc_stat
;
231 start
= get_timer(0);
232 while ((readl(&mmc_base
->pstate
) & (DATI_MASK
| CMDI_MASK
)) != 0) {
233 if (get_timer(0) - start
> MAX_RETRY_MS
) {
234 printf("%s: timedout waiting on cmd inhibit to clear\n",
239 writel(0xFFFFFFFF, &mmc_base
->stat
);
240 start
= get_timer(0);
241 while (readl(&mmc_base
->stat
)) {
242 if (get_timer(0) - start
> MAX_RETRY_MS
) {
243 printf("%s: timedout waiting for STAT (%x) to clear\n",
244 __func__
, readl(&mmc_base
->stat
));
250 * CMDIDX[13:8] : Command index
251 * DATAPRNT[5] : Data Present Select
252 * ENCMDIDX[4] : Command Index Check Enable
253 * ENCMDCRC[3] : Command CRC Check Enable
258 * 11 = Length 48 Check busy after response
260 /* Delay added before checking the status of frq change
261 * retry not supported by mmc.c(core file)
263 if (cmd
->cmdidx
== SD_CMD_APP_SEND_SCR
)
264 udelay(50000); /* wait 50 ms */
266 if (!(cmd
->resp_type
& MMC_RSP_PRESENT
))
268 else if (cmd
->resp_type
& MMC_RSP_136
)
269 flags
= RSP_TYPE_LGHT136
| CICE_NOCHECK
;
270 else if (cmd
->resp_type
& MMC_RSP_BUSY
)
271 flags
= RSP_TYPE_LGHT48B
;
273 flags
= RSP_TYPE_LGHT48
;
275 /* enable default flags */
276 flags
= flags
| (CMD_TYPE_NORMAL
| CICE_NOCHECK
| CCCE_NOCHECK
|
277 MSBS_SGLEBLK
| ACEN_DISABLE
| BCE_DISABLE
| DE_DISABLE
);
279 if (cmd
->resp_type
& MMC_RSP_CRC
)
281 if (cmd
->resp_type
& MMC_RSP_OPCODE
)
285 if ((cmd
->cmdidx
== MMC_CMD_READ_MULTIPLE_BLOCK
) ||
286 (cmd
->cmdidx
== MMC_CMD_WRITE_MULTIPLE_BLOCK
)) {
287 flags
|= (MSBS_MULTIBLK
| BCE_ENABLE
);
288 data
->blocksize
= 512;
289 writel(data
->blocksize
| (data
->blocks
<< 16),
292 writel(data
->blocksize
| NBLK_STPCNT
, &mmc_base
->blk
);
294 if (data
->flags
& MMC_DATA_READ
)
295 flags
|= (DP_DATA
| DDIR_READ
);
297 flags
|= (DP_DATA
| DDIR_WRITE
);
300 writel(cmd
->cmdarg
, &mmc_base
->arg
);
301 writel((cmd
->cmdidx
<< 24) | flags
, &mmc_base
->cmd
);
303 start
= get_timer(0);
305 mmc_stat
= readl(&mmc_base
->stat
);
306 if (get_timer(0) - start
> MAX_RETRY_MS
) {
307 printf("%s : timeout: No status update\n", __func__
);
312 if ((mmc_stat
& IE_CTO
) != 0) {
313 mmc_reset_controller_fsm(mmc_base
, SYSCTL_SRC
);
315 } else if ((mmc_stat
& ERRI_MASK
) != 0)
318 if (mmc_stat
& CC_MASK
) {
319 writel(CC_MASK
, &mmc_base
->stat
);
320 if (cmd
->resp_type
& MMC_RSP_PRESENT
) {
321 if (cmd
->resp_type
& MMC_RSP_136
) {
322 /* response type 2 */
323 cmd
->response
[3] = readl(&mmc_base
->rsp10
);
324 cmd
->response
[2] = readl(&mmc_base
->rsp32
);
325 cmd
->response
[1] = readl(&mmc_base
->rsp54
);
326 cmd
->response
[0] = readl(&mmc_base
->rsp76
);
328 /* response types 1, 1b, 3, 4, 5, 6 */
329 cmd
->response
[0] = readl(&mmc_base
->rsp10
);
333 if (data
&& (data
->flags
& MMC_DATA_READ
)) {
334 mmc_read_data(mmc_base
, data
->dest
,
335 data
->blocksize
* data
->blocks
);
336 } else if (data
&& (data
->flags
& MMC_DATA_WRITE
)) {
337 mmc_write_data(mmc_base
, data
->src
,
338 data
->blocksize
* data
->blocks
);
343 static int mmc_read_data(struct hsmmc
*mmc_base
, char *buf
, unsigned int size
)
345 unsigned int *output_buf
= (unsigned int *)buf
;
346 unsigned int mmc_stat
;
352 count
= (size
> MMCSD_SECTOR_SIZE
) ? MMCSD_SECTOR_SIZE
: size
;
356 ulong start
= get_timer(0);
358 mmc_stat
= readl(&mmc_base
->stat
);
359 if (get_timer(0) - start
> MAX_RETRY_MS
) {
360 printf("%s: timedout waiting for status!\n",
364 } while (mmc_stat
== 0);
366 if ((mmc_stat
& (IE_DTO
| IE_DCRC
| IE_DEB
)) != 0)
367 mmc_reset_controller_fsm(mmc_base
, SYSCTL_SRD
);
369 if ((mmc_stat
& ERRI_MASK
) != 0)
372 if (mmc_stat
& BRR_MASK
) {
375 writel(readl(&mmc_base
->stat
) | BRR_MASK
,
377 for (k
= 0; k
< count
; k
++) {
378 *output_buf
= readl(&mmc_base
->data
);
384 if (mmc_stat
& BWR_MASK
)
385 writel(readl(&mmc_base
->stat
) | BWR_MASK
,
388 if (mmc_stat
& TC_MASK
) {
389 writel(readl(&mmc_base
->stat
) | TC_MASK
,
397 static int mmc_write_data(struct hsmmc
*mmc_base
, const char *buf
,
400 unsigned int *input_buf
= (unsigned int *)buf
;
401 unsigned int mmc_stat
;
407 count
= (size
> MMCSD_SECTOR_SIZE
) ? MMCSD_SECTOR_SIZE
: size
;
411 ulong start
= get_timer(0);
413 mmc_stat
= readl(&mmc_base
->stat
);
414 if (get_timer(0) - start
> MAX_RETRY_MS
) {
415 printf("%s: timedout waiting for status!\n",
419 } while (mmc_stat
== 0);
421 if ((mmc_stat
& (IE_DTO
| IE_DCRC
| IE_DEB
)) != 0)
422 mmc_reset_controller_fsm(mmc_base
, SYSCTL_SRD
);
424 if ((mmc_stat
& ERRI_MASK
) != 0)
427 if (mmc_stat
& BWR_MASK
) {
430 writel(readl(&mmc_base
->stat
) | BWR_MASK
,
432 for (k
= 0; k
< count
; k
++) {
433 writel(*input_buf
, &mmc_base
->data
);
439 if (mmc_stat
& BRR_MASK
)
440 writel(readl(&mmc_base
->stat
) | BRR_MASK
,
443 if (mmc_stat
& TC_MASK
) {
444 writel(readl(&mmc_base
->stat
) | TC_MASK
,
452 static void mmc_set_ios(struct mmc
*mmc
)
454 struct hsmmc
*mmc_base
= (struct hsmmc
*)mmc
->priv
;
455 unsigned int dsor
= 0;
458 /* configue bus width */
459 switch (mmc
->bus_width
) {
461 writel(readl(&mmc_base
->con
) | DTW_8_BITMODE
,
466 writel(readl(&mmc_base
->con
) & ~DTW_8_BITMODE
,
468 writel(readl(&mmc_base
->hctl
) | DTW_4_BITMODE
,
474 writel(readl(&mmc_base
->con
) & ~DTW_8_BITMODE
,
476 writel(readl(&mmc_base
->hctl
) & ~DTW_4_BITMODE
,
481 /* configure clock with 96Mhz system clock.
483 if (mmc
->clock
!= 0) {
484 dsor
= (MMC_CLOCK_REFERENCE
* 1000000 / mmc
->clock
);
485 if ((MMC_CLOCK_REFERENCE
* 1000000) / dsor
> mmc
->clock
)
489 mmc_reg_out(&mmc_base
->sysctl
, (ICE_MASK
| DTO_MASK
| CEN_MASK
),
490 (ICE_STOP
| DTO_15THDTO
| CEN_DISABLE
));
492 mmc_reg_out(&mmc_base
->sysctl
, ICE_MASK
| CLKD_MASK
,
493 (dsor
<< CLKD_OFFSET
) | ICE_OSCILLATE
);
495 start
= get_timer(0);
496 while ((readl(&mmc_base
->sysctl
) & ICS_MASK
) == ICS_NOTREADY
) {
497 if (get_timer(0) - start
> MAX_RETRY_MS
) {
498 printf("%s: timedout waiting for ics!\n", __func__
);
502 writel(readl(&mmc_base
->sysctl
) | CEN_ENABLE
, &mmc_base
->sysctl
);
505 int omap_mmc_init(int dev_index
)
509 mmc
= &hsmmc_dev
[dev_index
];
511 sprintf(mmc
->name
, "OMAP SD/MMC");
512 mmc
->send_cmd
= mmc_send_cmd
;
513 mmc
->set_ios
= mmc_set_ios
;
514 mmc
->init
= mmc_init_setup
;
519 mmc
->priv
= (struct hsmmc
*)OMAP_HSMMC1_BASE
;
521 #ifdef OMAP_HSMMC2_BASE
523 mmc
->priv
= (struct hsmmc
*)OMAP_HSMMC2_BASE
;
526 #ifdef OMAP_HSMMC3_BASE
528 mmc
->priv
= (struct hsmmc
*)OMAP_HSMMC3_BASE
;
532 mmc
->priv
= (struct hsmmc
*)OMAP_HSMMC1_BASE
;
535 mmc
->voltages
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
536 mmc
->host_caps
= MMC_MODE_4BIT
| MMC_MODE_HS_52MHz
| MMC_MODE_HS
|
540 mmc
->f_max
= 52000000;
544 #if defined(CONFIG_OMAP34XX)
546 * Silicon revs 2.1 and older do not support multiblock transfers.
548 if ((get_cpu_family() == CPU_OMAP34XX
) && (get_cpu_rev() <= CPU_3XX_ES21
))