blob: afd9b30b513f92b60e74c8419f239e2712ba6958 [file] [log] [blame]
Sukumar Ghoraide941242010-09-18 20:32:33 -07001/*
2 * (C) Copyright 2008
3 * Texas Instruments, <www.ti.com>
4 * Sukumar Ghorai <s-ghorai@ti.com>
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
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
12 * the License.
13 *
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.
18 *
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,
22 * MA 02111-1307 USA
23 */
24
25#include <config.h>
26#include <common.h>
27#include <mmc.h>
28#include <part.h>
29#include <i2c.h>
30#include <twl4030.h>
Balaji T K14fa2dd2011-09-08 06:34:57 +000031#include <twl6030.h>
Balaji T Kdd23e592012-03-12 02:25:49 +000032#include <twl6035.h>
Sukumar Ghoraide941242010-09-18 20:32:33 -070033#include <asm/io.h>
34#include <asm/arch/mmc_host_def.h>
Dirk Behme96e0e7b2011-05-15 09:04:47 +000035#include <asm/arch/sys_proto.h>
Sukumar Ghoraide941242010-09-18 20:32:33 -070036
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +000037/* common definitions for all OMAPs */
38#define SYSCTL_SRC (1 << 25)
39#define SYSCTL_SRD (1 << 26)
40
Nishanth Menoneb9a28f2010-11-19 11:18:12 -050041/* If we fail after 1 second wait, something is really bad */
42#define MAX_RETRY_MS 1000
43
Sricharan933efe62011-11-15 09:49:53 -050044static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
45static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
46 unsigned int siz);
Sukumar Ghoraide941242010-09-18 20:32:33 -070047static struct mmc hsmmc_dev[2];
Balaji T K14fa2dd2011-09-08 06:34:57 +000048
49#if defined(CONFIG_OMAP44XX) && defined(CONFIG_TWL6030_POWER)
50static void omap4_vmmc_pbias_config(struct mmc *mmc)
51{
52 u32 value = 0;
SRICHARAN R002a2c02012-03-12 02:25:42 +000053 struct omap_sys_ctrl_regs *const ctrl =
54 (struct omap_sys_ctrl_regs *) SYSCTRL_GENERAL_CORE_BASE;
Balaji T K14fa2dd2011-09-08 06:34:57 +000055
56
57 value = readl(&ctrl->control_pbiaslite);
58 value &= ~(MMC1_PBIASLITE_PWRDNZ | MMC1_PWRDNZ);
59 writel(value, &ctrl->control_pbiaslite);
60 /* set VMMC to 3V */
61 twl6030_power_mmc_init();
62 value = readl(&ctrl->control_pbiaslite);
63 value |= MMC1_PBIASLITE_VMODE | MMC1_PBIASLITE_PWRDNZ | MMC1_PWRDNZ;
64 writel(value, &ctrl->control_pbiaslite);
65}
66#endif
67
Balaji T Kdd23e592012-03-12 02:25:49 +000068#if defined(CONFIG_OMAP54XX) && defined(CONFIG_TWL6035_POWER)
69static void omap5_pbias_config(struct mmc *mmc)
70{
71 u32 value = 0;
72 struct omap_sys_ctrl_regs *const ctrl =
73 (struct omap_sys_ctrl_regs *) SYSCTRL_GENERAL_CORE_BASE;
74
75 value = readl(&ctrl->control_pbias);
76 value &= ~(SDCARD_PWRDNZ | SDCARD_BIAS_PWRDNZ);
77 value |= SDCARD_BIAS_HIZ_MODE;
78 writel(value, &ctrl->control_pbias);
79
80 twl6035_mmc1_poweron_ldo();
81
82 value = readl(&ctrl->control_pbias);
83 value &= ~SDCARD_BIAS_HIZ_MODE;
84 value |= SDCARD_PBIASLITE_VMODE | SDCARD_PWRDNZ | SDCARD_BIAS_PWRDNZ;
85 writel(value, &ctrl->control_pbias);
86
87 value = readl(&ctrl->control_pbias);
88 if (value & (1 << 23)) {
89 value &= ~(SDCARD_PWRDNZ | SDCARD_BIAS_PWRDNZ);
90 value |= SDCARD_BIAS_HIZ_MODE;
91 writel(value, &ctrl->control_pbias);
92 }
93}
94#endif
95
Balaji T K14fa2dd2011-09-08 06:34:57 +000096unsigned char mmc_board_init(struct mmc *mmc)
Sukumar Ghoraide941242010-09-18 20:32:33 -070097{
Sukumar Ghoraide941242010-09-18 20:32:33 -070098#if defined(CONFIG_OMAP34XX)
99 t2_t *t2_base = (t2_t *)T2_BASE;
100 struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
Grazvydas Ignotasb1e725f2012-03-19 03:50:53 +0000101 u32 pbias_lite;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700102
Grazvydas Ignotasb1e725f2012-03-19 03:50:53 +0000103 pbias_lite = readl(&t2_base->pbias_lite);
104 pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
105 writel(pbias_lite, &t2_base->pbias_lite);
106#endif
107#if defined(CONFIG_TWL4030_POWER)
108 twl4030_power_mmc_init();
109 mdelay(100); /* ramp-up delay from Linux code */
110#endif
111#if defined(CONFIG_OMAP34XX)
112 writel(pbias_lite | PBIASLITEPWRDNZ1 |
Sukumar Ghoraide941242010-09-18 20:32:33 -0700113 PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
114 &t2_base->pbias_lite);
115
116 writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
117 &t2_base->devconf0);
118
119 writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
120 &t2_base->devconf1);
121
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +0000122 /* Change from default of 52MHz to 26MHz if necessary */
123 if (!(mmc->host_caps & MMC_MODE_HS_52MHz))
124 writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
125 &t2_base->ctl_prog_io1);
126
Sukumar Ghoraide941242010-09-18 20:32:33 -0700127 writel(readl(&prcm_base->fclken1_core) |
128 EN_MMC1 | EN_MMC2 | EN_MMC3,
129 &prcm_base->fclken1_core);
130
131 writel(readl(&prcm_base->iclken1_core) |
132 EN_MMC1 | EN_MMC2 | EN_MMC3,
133 &prcm_base->iclken1_core);
134#endif
135
Balaji T K14fa2dd2011-09-08 06:34:57 +0000136#if defined(CONFIG_OMAP44XX) && defined(CONFIG_TWL6030_POWER)
137 /* PBIAS config needed for MMC1 only */
138 if (mmc->block_dev.dev == 0)
139 omap4_vmmc_pbias_config(mmc);
140#endif
Balaji T Kdd23e592012-03-12 02:25:49 +0000141#if defined(CONFIG_OMAP54XX) && defined(CONFIG_TWL6035_POWER)
142 if (mmc->block_dev.dev == 0)
143 omap5_pbias_config(mmc);
144#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700145
146 return 0;
147}
148
Sricharan933efe62011-11-15 09:49:53 -0500149void mmc_init_stream(struct hsmmc *mmc_base)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700150{
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500151 ulong start;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700152
153 writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
154
155 writel(MMC_CMD0, &mmc_base->cmd);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500156 start = get_timer(0);
157 while (!(readl(&mmc_base->stat) & CC_MASK)) {
158 if (get_timer(0) - start > MAX_RETRY_MS) {
159 printf("%s: timedout waiting for cc!\n", __func__);
160 return;
161 }
162 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700163 writel(CC_MASK, &mmc_base->stat)
164 ;
165 writel(MMC_CMD0, &mmc_base->cmd)
166 ;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500167 start = get_timer(0);
168 while (!(readl(&mmc_base->stat) & CC_MASK)) {
169 if (get_timer(0) - start > MAX_RETRY_MS) {
170 printf("%s: timedout waiting for cc2!\n", __func__);
171 return;
172 }
173 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700174 writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
175}
176
177
178static int mmc_init_setup(struct mmc *mmc)
179{
Sricharan933efe62011-11-15 09:49:53 -0500180 struct hsmmc *mmc_base = (struct hsmmc *)mmc->priv;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700181 unsigned int reg_val;
182 unsigned int dsor;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500183 ulong start;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700184
Balaji T K14fa2dd2011-09-08 06:34:57 +0000185 mmc_board_init(mmc);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700186
187 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
188 &mmc_base->sysconfig);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500189 start = get_timer(0);
190 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
191 if (get_timer(0) - start > MAX_RETRY_MS) {
192 printf("%s: timedout waiting for cc2!\n", __func__);
193 return TIMEOUT;
194 }
195 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700196 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500197 start = get_timer(0);
198 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
199 if (get_timer(0) - start > MAX_RETRY_MS) {
200 printf("%s: timedout waiting for softresetall!\n",
201 __func__);
202 return TIMEOUT;
203 }
204 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700205 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
206 writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
207 &mmc_base->capa);
208
209 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
210
211 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
212 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
213 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
214
215 dsor = 240;
216 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
217 (ICE_STOP | DTO_15THDTO | CEN_DISABLE));
218 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
219 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500220 start = get_timer(0);
221 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
222 if (get_timer(0) - start > MAX_RETRY_MS) {
223 printf("%s: timedout waiting for ics!\n", __func__);
224 return TIMEOUT;
225 }
226 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700227 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
228
229 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
230
231 writel(IE_BADA | IE_CERR | IE_DEB | IE_DCRC | IE_DTO | IE_CIE |
232 IE_CEB | IE_CCRC | IE_CTO | IE_BRR | IE_BWR | IE_TC | IE_CC,
233 &mmc_base->ie);
234
235 mmc_init_stream(mmc_base);
236
237 return 0;
238}
239
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000240/*
241 * MMC controller internal finite state machine reset
242 *
243 * Used to reset command or data internal state machines, using respectively
244 * SRC or SRD bit of SYSCTL register
245 */
246static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
247{
248 ulong start;
249
250 mmc_reg_out(&mmc_base->sysctl, bit, bit);
251
252 start = get_timer(0);
253 while ((readl(&mmc_base->sysctl) & bit) != 0) {
254 if (get_timer(0) - start > MAX_RETRY_MS) {
255 printf("%s: timedout waiting for sysctl %x to clear\n",
256 __func__, bit);
257 return;
258 }
259 }
260}
Sukumar Ghoraide941242010-09-18 20:32:33 -0700261
262static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
263 struct mmc_data *data)
264{
Sricharan933efe62011-11-15 09:49:53 -0500265 struct hsmmc *mmc_base = (struct hsmmc *)mmc->priv;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700266 unsigned int flags, mmc_stat;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500267 ulong start;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700268
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500269 start = get_timer(0);
Tom Rinia7778f82012-01-30 11:22:25 +0000270 while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500271 if (get_timer(0) - start > MAX_RETRY_MS) {
Tom Rinia7778f82012-01-30 11:22:25 +0000272 printf("%s: timedout waiting on cmd inhibit to clear\n",
273 __func__);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500274 return TIMEOUT;
275 }
276 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700277 writel(0xFFFFFFFF, &mmc_base->stat);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500278 start = get_timer(0);
279 while (readl(&mmc_base->stat)) {
280 if (get_timer(0) - start > MAX_RETRY_MS) {
Grazvydas Ignotas15ceb1d2012-03-19 12:11:43 +0000281 printf("%s: timedout waiting for STAT (%x) to clear\n",
282 __func__, readl(&mmc_base->stat));
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500283 return TIMEOUT;
284 }
285 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700286 /*
287 * CMDREG
288 * CMDIDX[13:8] : Command index
289 * DATAPRNT[5] : Data Present Select
290 * ENCMDIDX[4] : Command Index Check Enable
291 * ENCMDCRC[3] : Command CRC Check Enable
292 * RSPTYP[1:0]
293 * 00 = No Response
294 * 01 = Length 136
295 * 10 = Length 48
296 * 11 = Length 48 Check busy after response
297 */
298 /* Delay added before checking the status of frq change
299 * retry not supported by mmc.c(core file)
300 */
301 if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
302 udelay(50000); /* wait 50 ms */
303
304 if (!(cmd->resp_type & MMC_RSP_PRESENT))
305 flags = 0;
306 else if (cmd->resp_type & MMC_RSP_136)
307 flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
308 else if (cmd->resp_type & MMC_RSP_BUSY)
309 flags = RSP_TYPE_LGHT48B;
310 else
311 flags = RSP_TYPE_LGHT48;
312
313 /* enable default flags */
314 flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
315 MSBS_SGLEBLK | ACEN_DISABLE | BCE_DISABLE | DE_DISABLE);
316
317 if (cmd->resp_type & MMC_RSP_CRC)
318 flags |= CCCE_CHECK;
319 if (cmd->resp_type & MMC_RSP_OPCODE)
320 flags |= CICE_CHECK;
321
322 if (data) {
323 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
324 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
325 flags |= (MSBS_MULTIBLK | BCE_ENABLE);
326 data->blocksize = 512;
327 writel(data->blocksize | (data->blocks << 16),
328 &mmc_base->blk);
329 } else
330 writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
331
332 if (data->flags & MMC_DATA_READ)
333 flags |= (DP_DATA | DDIR_READ);
334 else
335 flags |= (DP_DATA | DDIR_WRITE);
336 }
337
338 writel(cmd->cmdarg, &mmc_base->arg);
339 writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
340
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500341 start = get_timer(0);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700342 do {
343 mmc_stat = readl(&mmc_base->stat);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500344 if (get_timer(0) - start > MAX_RETRY_MS) {
345 printf("%s : timeout: No status update\n", __func__);
346 return TIMEOUT;
347 }
348 } while (!mmc_stat);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700349
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000350 if ((mmc_stat & IE_CTO) != 0) {
351 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700352 return TIMEOUT;
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000353 } else if ((mmc_stat & ERRI_MASK) != 0)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700354 return -1;
355
356 if (mmc_stat & CC_MASK) {
357 writel(CC_MASK, &mmc_base->stat);
358 if (cmd->resp_type & MMC_RSP_PRESENT) {
359 if (cmd->resp_type & MMC_RSP_136) {
360 /* response type 2 */
361 cmd->response[3] = readl(&mmc_base->rsp10);
362 cmd->response[2] = readl(&mmc_base->rsp32);
363 cmd->response[1] = readl(&mmc_base->rsp54);
364 cmd->response[0] = readl(&mmc_base->rsp76);
365 } else
366 /* response types 1, 1b, 3, 4, 5, 6 */
367 cmd->response[0] = readl(&mmc_base->rsp10);
368 }
369 }
370
371 if (data && (data->flags & MMC_DATA_READ)) {
372 mmc_read_data(mmc_base, data->dest,
373 data->blocksize * data->blocks);
374 } else if (data && (data->flags & MMC_DATA_WRITE)) {
375 mmc_write_data(mmc_base, data->src,
376 data->blocksize * data->blocks);
377 }
378 return 0;
379}
380
Sricharan933efe62011-11-15 09:49:53 -0500381static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700382{
383 unsigned int *output_buf = (unsigned int *)buf;
384 unsigned int mmc_stat;
385 unsigned int count;
386
387 /*
388 * Start Polled Read
389 */
390 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
391 count /= 4;
392
393 while (size) {
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500394 ulong start = get_timer(0);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700395 do {
396 mmc_stat = readl(&mmc_base->stat);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500397 if (get_timer(0) - start > MAX_RETRY_MS) {
398 printf("%s: timedout waiting for status!\n",
399 __func__);
400 return TIMEOUT;
401 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700402 } while (mmc_stat == 0);
403
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000404 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
405 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
406
Sukumar Ghoraide941242010-09-18 20:32:33 -0700407 if ((mmc_stat & ERRI_MASK) != 0)
408 return 1;
409
410 if (mmc_stat & BRR_MASK) {
411 unsigned int k;
412
413 writel(readl(&mmc_base->stat) | BRR_MASK,
414 &mmc_base->stat);
415 for (k = 0; k < count; k++) {
416 *output_buf = readl(&mmc_base->data);
417 output_buf++;
418 }
419 size -= (count*4);
420 }
421
422 if (mmc_stat & BWR_MASK)
423 writel(readl(&mmc_base->stat) | BWR_MASK,
424 &mmc_base->stat);
425
426 if (mmc_stat & TC_MASK) {
427 writel(readl(&mmc_base->stat) | TC_MASK,
428 &mmc_base->stat);
429 break;
430 }
431 }
432 return 0;
433}
434
Sricharan933efe62011-11-15 09:49:53 -0500435static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
436 unsigned int size)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700437{
438 unsigned int *input_buf = (unsigned int *)buf;
439 unsigned int mmc_stat;
440 unsigned int count;
441
442 /*
443 * Start Polled Read
444 */
445 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
446 count /= 4;
447
448 while (size) {
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500449 ulong start = get_timer(0);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700450 do {
451 mmc_stat = readl(&mmc_base->stat);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500452 if (get_timer(0) - start > MAX_RETRY_MS) {
453 printf("%s: timedout waiting for status!\n",
454 __func__);
455 return TIMEOUT;
456 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700457 } while (mmc_stat == 0);
458
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000459 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
460 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
461
Sukumar Ghoraide941242010-09-18 20:32:33 -0700462 if ((mmc_stat & ERRI_MASK) != 0)
463 return 1;
464
465 if (mmc_stat & BWR_MASK) {
466 unsigned int k;
467
468 writel(readl(&mmc_base->stat) | BWR_MASK,
469 &mmc_base->stat);
470 for (k = 0; k < count; k++) {
471 writel(*input_buf, &mmc_base->data);
472 input_buf++;
473 }
474 size -= (count*4);
475 }
476
477 if (mmc_stat & BRR_MASK)
478 writel(readl(&mmc_base->stat) | BRR_MASK,
479 &mmc_base->stat);
480
481 if (mmc_stat & TC_MASK) {
482 writel(readl(&mmc_base->stat) | TC_MASK,
483 &mmc_base->stat);
484 break;
485 }
486 }
487 return 0;
488}
489
490static void mmc_set_ios(struct mmc *mmc)
491{
Sricharan933efe62011-11-15 09:49:53 -0500492 struct hsmmc *mmc_base = (struct hsmmc *)mmc->priv;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700493 unsigned int dsor = 0;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500494 ulong start;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700495
496 /* configue bus width */
497 switch (mmc->bus_width) {
498 case 8:
499 writel(readl(&mmc_base->con) | DTW_8_BITMODE,
500 &mmc_base->con);
501 break;
502
503 case 4:
504 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
505 &mmc_base->con);
506 writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
507 &mmc_base->hctl);
508 break;
509
510 case 1:
511 default:
512 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
513 &mmc_base->con);
514 writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
515 &mmc_base->hctl);
516 break;
517 }
518
519 /* configure clock with 96Mhz system clock.
520 */
521 if (mmc->clock != 0) {
522 dsor = (MMC_CLOCK_REFERENCE * 1000000 / mmc->clock);
523 if ((MMC_CLOCK_REFERENCE * 1000000) / dsor > mmc->clock)
524 dsor++;
525 }
526
527 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
528 (ICE_STOP | DTO_15THDTO | CEN_DISABLE));
529
530 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
531 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
532
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500533 start = get_timer(0);
534 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
535 if (get_timer(0) - start > MAX_RETRY_MS) {
536 printf("%s: timedout waiting for ics!\n", __func__);
537 return;
538 }
539 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700540 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
541}
542
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +0000543int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700544{
545 struct mmc *mmc;
546
547 mmc = &hsmmc_dev[dev_index];
548
549 sprintf(mmc->name, "OMAP SD/MMC");
550 mmc->send_cmd = mmc_send_cmd;
551 mmc->set_ios = mmc_set_ios;
552 mmc->init = mmc_init_setup;
Thierry Reding48972d92012-01-02 01:15:37 +0000553 mmc->getcd = NULL;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700554
555 switch (dev_index) {
556 case 0:
Sricharan933efe62011-11-15 09:49:53 -0500557 mmc->priv = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700558 break;
Tom Rini1037d582011-10-12 06:20:50 +0000559#ifdef OMAP_HSMMC2_BASE
Sukumar Ghoraide941242010-09-18 20:32:33 -0700560 case 1:
Sricharan933efe62011-11-15 09:49:53 -0500561 mmc->priv = (struct hsmmc *)OMAP_HSMMC2_BASE;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700562 break;
Tom Rini1037d582011-10-12 06:20:50 +0000563#endif
564#ifdef OMAP_HSMMC3_BASE
Sukumar Ghoraide941242010-09-18 20:32:33 -0700565 case 2:
Sricharan933efe62011-11-15 09:49:53 -0500566 mmc->priv = (struct hsmmc *)OMAP_HSMMC3_BASE;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700567 break;
Tom Rini1037d582011-10-12 06:20:50 +0000568#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700569 default:
Sricharan933efe62011-11-15 09:49:53 -0500570 mmc->priv = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700571 return 1;
572 }
573 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +0000574 mmc->host_caps = (MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS |
575 MMC_MODE_HC) & ~host_caps_mask;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700576
577 mmc->f_min = 400000;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +0000578
579 if (f_max != 0)
580 mmc->f_max = f_max;
581 else {
582 if (mmc->host_caps & MMC_MODE_HS) {
583 if (mmc->host_caps & MMC_MODE_HS_52MHz)
584 mmc->f_max = 52000000;
585 else
586 mmc->f_max = 26000000;
587 } else
588 mmc->f_max = 20000000;
589 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700590
John Rigby8feafcc2011-04-18 05:50:08 +0000591 mmc->b_max = 0;
592
John Rigby4ca92442011-04-19 05:48:14 +0000593#if defined(CONFIG_OMAP34XX)
594 /*
595 * Silicon revs 2.1 and older do not support multiblock transfers.
596 */
597 if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
598 mmc->b_max = 1;
599#endif
600
Sukumar Ghoraide941242010-09-18 20:32:33 -0700601 mmc_register(mmc);
602
603 return 0;
604}