blob: 15d41e55bd1606b05ddc925f3c90eb3f68d47a90 [file] [log] [blame]
Dirk Behmeb1c3bf92008-12-14 09:47:17 +01001/*
2 * (C) Copyright 2008
3 * Texas Instruments, <www.ti.com>
4 * Syed Mohammed Khasim <khasim@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 <fat.h>
28#include <mmc.h>
29#include <part.h>
30#include <i2c.h>
Tom Rixfccc0fc2009-06-28 12:52:31 -050031#include <twl4030.h>
Dirk Behmeaba45c82009-02-20 17:51:28 +010032#include <asm/io.h>
Steve Sakomanbec3dc72010-06-09 12:54:58 -070033
34#include "omap3_mmc.h"
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010035
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -070036static const unsigned short mmc_transspeed_val[15][4] = {
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010037 {CLKD(10, 1), CLKD(10, 10), CLKD(10, 100), CLKD(10, 1000)},
38 {CLKD(12, 1), CLKD(12, 10), CLKD(12, 100), CLKD(12, 1000)},
39 {CLKD(13, 1), CLKD(13, 10), CLKD(13, 100), CLKD(13, 1000)},
40 {CLKD(15, 1), CLKD(15, 10), CLKD(15, 100), CLKD(15, 1000)},
41 {CLKD(20, 1), CLKD(20, 10), CLKD(20, 100), CLKD(20, 1000)},
42 {CLKD(26, 1), CLKD(26, 10), CLKD(26, 100), CLKD(26, 1000)},
43 {CLKD(30, 1), CLKD(30, 10), CLKD(30, 100), CLKD(30, 1000)},
44 {CLKD(35, 1), CLKD(35, 10), CLKD(35, 100), CLKD(35, 1000)},
45 {CLKD(40, 1), CLKD(40, 10), CLKD(40, 100), CLKD(40, 1000)},
46 {CLKD(45, 1), CLKD(45, 10), CLKD(45, 100), CLKD(45, 1000)},
47 {CLKD(52, 1), CLKD(52, 10), CLKD(52, 100), CLKD(52, 1000)},
48 {CLKD(55, 1), CLKD(55, 10), CLKD(55, 100), CLKD(55, 1000)},
49 {CLKD(60, 1), CLKD(60, 10), CLKD(60, 100), CLKD(60, 1000)},
50 {CLKD(70, 1), CLKD(70, 10), CLKD(70, 100), CLKD(70, 1000)},
51 {CLKD(80, 1), CLKD(80, 10), CLKD(80, 100), CLKD(80, 1000)}
52};
53
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -070054static mmc_card_data cur_card_data;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010055static block_dev_desc_t mmc_blk_dev;
Steve Sakoman8f223272010-04-29 10:28:14 -070056static hsmmc_t *mmc_base = (hsmmc_t *)OMAP_HSMMC1_BASE;
57
Steve Sakoman89716962010-07-01 12:12:42 -070058int mmc_set_dev(int dev_num)
Steve Sakoman8f223272010-04-29 10:28:14 -070059{
Steve Sakoman89716962010-07-01 12:12:42 -070060 switch (dev_num) {
Steve Sakoman8f223272010-04-29 10:28:14 -070061 case 1:
62 mmc_base = (hsmmc_t *)OMAP_HSMMC1_BASE;
63 break;
64 case 2:
65 mmc_base = (hsmmc_t *)OMAP_HSMMC2_BASE;
66 break;
67 case 3:
68 mmc_base = (hsmmc_t *)OMAP_HSMMC3_BASE;
69 break;
70 default:
71 mmc_base = (hsmmc_t *)OMAP_HSMMC1_BASE;
72 return 1;
73 }
74
75 return 0;
76}
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010077
78block_dev_desc_t *mmc_get_dev(int dev)
79{
80 return (block_dev_desc_t *) &mmc_blk_dev;
81}
82
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -070083static unsigned char mmc_board_init(void)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010084{
Vaibhav Hiremath7c151212009-11-23 16:36:05 +053085#if defined(CONFIG_TWL4030_POWER)
Tom Rixfccc0fc2009-06-28 12:52:31 -050086 twl4030_power_mmc_init();
Vaibhav Hiremath7c151212009-11-23 16:36:05 +053087#endif
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010088
Steve Sakomanbec3dc72010-06-09 12:54:58 -070089#if defined(CONFIG_OMAP34XX)
90 t2_t *t2_base = (t2_t *)T2_BASE;
91 struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
92
Dirk Behmeaba45c82009-02-20 17:51:28 +010093 writel(readl(&t2_base->pbias_lite) | PBIASLITEPWRDNZ1 |
94 PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
95 &t2_base->pbias_lite);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010096
Dirk Behmeaba45c82009-02-20 17:51:28 +010097 writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
98 &t2_base->devconf0);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010099
Steve Sakoman8f223272010-04-29 10:28:14 -0700100 writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
101 &t2_base->devconf1);
102
103 writel(readl(&prcm_base->fclken1_core) |
104 EN_MMC1 | EN_MMC2 | EN_MMC3,
105 &prcm_base->fclken1_core);
106
107 writel(readl(&prcm_base->iclken1_core) |
108 EN_MMC1 | EN_MMC2 | EN_MMC3,
109 &prcm_base->iclken1_core);
Steve Sakomanbec3dc72010-06-09 12:54:58 -0700110#endif
111
112/* TODO add appropriate OMAP4 init */
Steve Sakoman8f223272010-04-29 10:28:14 -0700113
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100114 return 1;
115}
116
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -0700117static void mmc_init_stream(void)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100118{
Dirk Behmeaba45c82009-02-20 17:51:28 +0100119 writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100120
Dirk Behmeaba45c82009-02-20 17:51:28 +0100121 writel(MMC_CMD0, &mmc_base->cmd);
122 while (!(readl(&mmc_base->stat) & CC_MASK));
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100123
Dirk Behmeaba45c82009-02-20 17:51:28 +0100124 writel(CC_MASK, &mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100125
Dirk Behmeaba45c82009-02-20 17:51:28 +0100126 writel(MMC_CMD0, &mmc_base->cmd);
127 while (!(readl(&mmc_base->stat) & CC_MASK));
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100128
Dirk Behmeaba45c82009-02-20 17:51:28 +0100129 writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100130}
131
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -0700132static unsigned char mmc_clock_config(unsigned int iclk, unsigned short clk_div)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100133{
134 unsigned int val;
135
Dirk Behmeaba45c82009-02-20 17:51:28 +0100136 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
137 (ICE_STOP | DTO_15THDTO | CEN_DISABLE));
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100138
139 switch (iclk) {
140 case CLK_INITSEQ:
141 val = MMC_INIT_SEQ_CLK / 2;
142 break;
143 case CLK_400KHZ:
144 val = MMC_400kHz_CLK;
145 break;
146 case CLK_MISC:
147 val = clk_div;
148 break;
149 default:
150 return 0;
151 }
Dirk Behmeaba45c82009-02-20 17:51:28 +0100152 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
153 (val << CLKD_OFFSET) | ICE_OSCILLATE);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100154
Dirk Behmeaba45c82009-02-20 17:51:28 +0100155 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100156
Dirk Behmeaba45c82009-02-20 17:51:28 +0100157 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100158 return 1;
159}
160
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -0700161static unsigned char mmc_init_setup(void)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100162{
163 unsigned int reg_val;
164
165 mmc_board_init();
166
Dirk Behmeaba45c82009-02-20 17:51:28 +0100167 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
168 &mmc_base->sysconfig);
169 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100170
Dirk Behmeaba45c82009-02-20 17:51:28 +0100171 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
172 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100173
Dirk Behmeaba45c82009-02-20 17:51:28 +0100174 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
175 writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
176 &mmc_base->capa);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100177
Dirk Behmeaba45c82009-02-20 17:51:28 +0100178 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100179
Dirk Behmeaba45c82009-02-20 17:51:28 +0100180 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
181 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
182 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100183
184 mmc_clock_config(CLK_INITSEQ, 0);
Dirk Behmeaba45c82009-02-20 17:51:28 +0100185 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100186
Dirk Behmeaba45c82009-02-20 17:51:28 +0100187 writel(IE_BADA | IE_CERR | IE_DEB | IE_DCRC | IE_DTO | IE_CIE |
188 IE_CEB | IE_CCRC | IE_CTO | IE_BRR | IE_BWR | IE_TC | IE_CC,
189 &mmc_base->ie);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100190
191 mmc_init_stream();
192 return 1;
193}
194
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -0700195static unsigned char mmc_send_cmd(unsigned int cmd, unsigned int arg,
Dirk Behmeaba45c82009-02-20 17:51:28 +0100196 unsigned int *response)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100197{
Dirk Behmeaba45c82009-02-20 17:51:28 +0100198 unsigned int mmc_stat;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100199
Dirk Behmeaba45c82009-02-20 17:51:28 +0100200 while ((readl(&mmc_base->pstate) & DATI_MASK) == DATI_CMDDIS);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100201
Dirk Behmeaba45c82009-02-20 17:51:28 +0100202 writel(BLEN_512BYTESLEN | NBLK_STPCNT, &mmc_base->blk);
203 writel(0xFFFFFFFF, &mmc_base->stat);
204 writel(arg, &mmc_base->arg);
205 writel(cmd | CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
206 MSBS_SGLEBLK | ACEN_DISABLE | BCE_DISABLE | DE_DISABLE,
207 &mmc_base->cmd);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100208
209 while (1) {
210 do {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100211 mmc_stat = readl(&mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100212 } while (mmc_stat == 0);
213
214 if ((mmc_stat & ERRI_MASK) != 0)
215 return (unsigned char) mmc_stat;
216
217 if (mmc_stat & CC_MASK) {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100218 writel(CC_MASK, &mmc_base->stat);
219 response[0] = readl(&mmc_base->rsp10);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100220 if ((cmd & RSP_TYPE_MASK) == RSP_TYPE_LGHT136) {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100221 response[1] = readl(&mmc_base->rsp32);
222 response[2] = readl(&mmc_base->rsp54);
223 response[3] = readl(&mmc_base->rsp76);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100224 }
225 break;
226 }
227 }
228 return 1;
229}
230
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -0700231static unsigned char mmc_read_data(unsigned int *output_buf)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100232{
Dirk Behmeaba45c82009-02-20 17:51:28 +0100233 unsigned int mmc_stat;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100234 unsigned int read_count = 0;
235
236 /*
237 * Start Polled Read
238 */
239 while (1) {
240 do {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100241 mmc_stat = readl(&mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100242 } while (mmc_stat == 0);
243
244 if ((mmc_stat & ERRI_MASK) != 0)
245 return (unsigned char) mmc_stat;
246
247 if (mmc_stat & BRR_MASK) {
248 unsigned int k;
249
Dirk Behmeaba45c82009-02-20 17:51:28 +0100250 writel(readl(&mmc_base->stat) | BRR_MASK,
251 &mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100252 for (k = 0; k < MMCSD_SECTOR_SIZE / 4; k++) {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100253 *output_buf = readl(&mmc_base->data);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100254 output_buf++;
255 read_count += 4;
256 }
257 }
258
259 if (mmc_stat & BWR_MASK)
Dirk Behmeaba45c82009-02-20 17:51:28 +0100260 writel(readl(&mmc_base->stat) | BWR_MASK,
261 &mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100262
263 if (mmc_stat & TC_MASK) {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100264 writel(readl(&mmc_base->stat) | TC_MASK,
265 &mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100266 break;
267 }
268 }
269 return 1;
270}
271
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -0700272static unsigned char mmc_detect_card(mmc_card_data *mmc_card_cur)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100273{
274 unsigned char err;
275 unsigned int argument = 0;
276 unsigned int ocr_value, ocr_recvd, ret_cmd41, hcs_val;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100277 unsigned short retry_cnt = 2000;
Dirk Behme9de02122009-09-28 08:17:50 -0400278 mmc_resp_t mmc_resp;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100279
280 /* Set to Initialization Clock */
281 err = mmc_clock_config(CLK_400KHZ, 0);
282 if (err != 1)
283 return err;
284
285 mmc_card_cur->RCA = MMC_RELATIVE_CARD_ADDRESS;
286 argument = 0x00000000;
287
288 ocr_value = (0x1FF << 15);
Dirk Behme9de02122009-09-28 08:17:50 -0400289 err = mmc_send_cmd(MMC_CMD0, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100290 if (err != 1)
291 return err;
292
293 argument = SD_CMD8_CHECK_PATTERN | SD_CMD8_2_7_3_6_V_RANGE;
Dirk Behme9de02122009-09-28 08:17:50 -0400294 err = mmc_send_cmd(MMC_SDCMD8, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100295 hcs_val = (err == 1) ?
296 MMC_OCR_REG_HOST_CAPACITY_SUPPORT_SECTOR :
297 MMC_OCR_REG_HOST_CAPACITY_SUPPORT_BYTE;
298
299 argument = 0x0000 << 16;
Dirk Behme9de02122009-09-28 08:17:50 -0400300 err = mmc_send_cmd(MMC_CMD55, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100301 if (err == 1) {
302 mmc_card_cur->card_type = SD_CARD;
303 ocr_value |= hcs_val;
304 ret_cmd41 = MMC_ACMD41;
305 } else {
306 mmc_card_cur->card_type = MMC_CARD;
307 ocr_value |= MMC_OCR_REG_ACCESS_MODE_SECTOR;
308 ret_cmd41 = MMC_CMD1;
Dirk Behmeaba45c82009-02-20 17:51:28 +0100309 writel(readl(&mmc_base->con) & ~OD, &mmc_base->con);
310 writel(readl(&mmc_base->con) | OPENDRAIN, &mmc_base->con);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100311 }
312
313 argument = ocr_value;
Dirk Behme9de02122009-09-28 08:17:50 -0400314 err = mmc_send_cmd(ret_cmd41, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100315 if (err != 1)
316 return err;
317
Dirk Behme9de02122009-09-28 08:17:50 -0400318 ocr_recvd = mmc_resp.r3.ocr;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100319
320 while (!(ocr_recvd & (0x1 << 31)) && (retry_cnt > 0)) {
321 retry_cnt--;
322 if (mmc_card_cur->card_type == SD_CARD) {
323 argument = 0x0000 << 16;
Dirk Behme9de02122009-09-28 08:17:50 -0400324 err = mmc_send_cmd(MMC_CMD55, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100325 }
326
327 argument = ocr_value;
Dirk Behme9de02122009-09-28 08:17:50 -0400328 err = mmc_send_cmd(ret_cmd41, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100329 if (err != 1)
330 return err;
Dirk Behme9de02122009-09-28 08:17:50 -0400331 ocr_recvd = mmc_resp.r3.ocr;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100332 }
333
334 if (!(ocr_recvd & (0x1 << 31)))
335 return 0;
336
337 if (mmc_card_cur->card_type == MMC_CARD) {
338 if ((ocr_recvd & MMC_OCR_REG_ACCESS_MODE_MASK) ==
339 MMC_OCR_REG_ACCESS_MODE_SECTOR) {
340 mmc_card_cur->mode = SECTOR_MODE;
341 } else {
342 mmc_card_cur->mode = BYTE_MODE;
343 }
344
345 ocr_recvd &= ~MMC_OCR_REG_ACCESS_MODE_MASK;
346 } else {
347 if ((ocr_recvd & MMC_OCR_REG_HOST_CAPACITY_SUPPORT_MASK)
348 == MMC_OCR_REG_HOST_CAPACITY_SUPPORT_SECTOR) {
349 mmc_card_cur->mode = SECTOR_MODE;
350 } else {
351 mmc_card_cur->mode = BYTE_MODE;
352 }
353 ocr_recvd &= ~MMC_OCR_REG_HOST_CAPACITY_SUPPORT_MASK;
354 }
355
356 ocr_recvd &= ~(0x1 << 31);
357 if (!(ocr_recvd & ocr_value))
358 return 0;
359
Dirk Behme9de02122009-09-28 08:17:50 -0400360 err = mmc_send_cmd(MMC_CMD2, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100361 if (err != 1)
362 return err;
363
364 if (mmc_card_cur->card_type == MMC_CARD) {
365 argument = mmc_card_cur->RCA << 16;
Dirk Behme9de02122009-09-28 08:17:50 -0400366 err = mmc_send_cmd(MMC_CMD3, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100367 if (err != 1)
368 return err;
369 } else {
370 argument = 0x00000000;
Dirk Behme9de02122009-09-28 08:17:50 -0400371 err = mmc_send_cmd(MMC_SDCMD3, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100372 if (err != 1)
373 return err;
374
Dirk Behme9de02122009-09-28 08:17:50 -0400375 mmc_card_cur->RCA = mmc_resp.r6.newpublishedrca;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100376 }
377
Dirk Behmeaba45c82009-02-20 17:51:28 +0100378 writel(readl(&mmc_base->con) & ~OD, &mmc_base->con);
379 writel(readl(&mmc_base->con) | NOOPENDRAIN, &mmc_base->con);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100380 return 1;
381}
382
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -0700383static unsigned char mmc_read_cardsize(mmc_card_data *mmc_dev_data,
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100384 mmc_csd_reg_t *cur_csd)
385{
386 mmc_extended_csd_reg_t ext_csd;
387 unsigned int size, count, blk_len, blk_no, card_size, argument;
388 unsigned char err;
389 unsigned int resp[4];
390
391 if (mmc_dev_data->mode == SECTOR_MODE) {
392 if (mmc_dev_data->card_type == SD_CARD) {
393 card_size =
394 (((mmc_sd2_csd_reg_t *) cur_csd)->
395 c_size_lsb & MMC_SD2_CSD_C_SIZE_LSB_MASK) |
396 ((((mmc_sd2_csd_reg_t *) cur_csd)->
397 c_size_msb & MMC_SD2_CSD_C_SIZE_MSB_MASK)
398 << MMC_SD2_CSD_C_SIZE_MSB_OFFSET);
399 mmc_dev_data->size = card_size * 1024;
400 if (mmc_dev_data->size == 0)
401 return 0;
402 } else {
403 argument = 0x00000000;
404 err = mmc_send_cmd(MMC_CMD8, argument, resp);
405 if (err != 1)
406 return err;
407 err = mmc_read_data((unsigned int *) &ext_csd);
408 if (err != 1)
409 return err;
410 mmc_dev_data->size = ext_csd.sectorcount;
411
412 if (mmc_dev_data->size == 0)
413 mmc_dev_data->size = 8388608;
414 }
415 } else {
416 if (cur_csd->c_size_mult >= 8)
417 return 0;
418
419 if (cur_csd->read_bl_len >= 12)
420 return 0;
421
422 /* Compute size */
423 count = 1 << (cur_csd->c_size_mult + 2);
424 card_size = (cur_csd->c_size_lsb & MMC_CSD_C_SIZE_LSB_MASK) |
425 ((cur_csd->c_size_msb & MMC_CSD_C_SIZE_MSB_MASK)
426 << MMC_CSD_C_SIZE_MSB_OFFSET);
427 blk_no = (card_size + 1) * count;
428 blk_len = 1 << cur_csd->read_bl_len;
429 size = blk_no * blk_len;
430 mmc_dev_data->size = size / MMCSD_SECTOR_SIZE;
431 if (mmc_dev_data->size == 0)
432 return 0;
433 }
434 return 1;
435}
436
Grazvydas Ignotasdc41b8b2010-08-11 15:56:03 -0700437static unsigned long mmc_bread(int dev_num, unsigned long blknr,
438 lbaint_t blkcnt, void *dst)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100439{
440 unsigned char err;
441 unsigned int argument;
442 unsigned int resp[4];
Grazvydas Ignotasdc41b8b2010-08-11 15:56:03 -0700443 unsigned int *output_buf = dst;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100444 unsigned int sec_inc_val;
Grazvydas Ignotasdc41b8b2010-08-11 15:56:03 -0700445 lbaint_t i;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100446
Grazvydas Ignotasdc41b8b2010-08-11 15:56:03 -0700447 if (blkcnt == 0)
448 return 0;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100449
Grazvydas Ignotasdc41b8b2010-08-11 15:56:03 -0700450 if (cur_card_data.mode == SECTOR_MODE) {
451 argument = blknr;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100452 sec_inc_val = 1;
453 } else {
Grazvydas Ignotasdc41b8b2010-08-11 15:56:03 -0700454 argument = blknr * MMCSD_SECTOR_SIZE;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100455 sec_inc_val = MMCSD_SECTOR_SIZE;
456 }
457
Grazvydas Ignotasdc41b8b2010-08-11 15:56:03 -0700458 for (i = 0; i < blkcnt; i++) {
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100459 err = mmc_send_cmd(MMC_CMD17, argument, resp);
Grazvydas Ignotasdc41b8b2010-08-11 15:56:03 -0700460 if (err != 1) {
461 printf("mmc: CMD17 failed, status = %08x\n", err);
462 break;
463 }
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100464
Grazvydas Ignotasdc41b8b2010-08-11 15:56:03 -0700465 err = mmc_read_data(output_buf);
466 if (err != 1) {
467 printf("mmc: read failed, status = %08x\n", err);
468 break;
469 }
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100470
471 output_buf += (MMCSD_SECTOR_SIZE / 4);
472 argument += sec_inc_val;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100473 }
Grazvydas Ignotasdc41b8b2010-08-11 15:56:03 -0700474
475 return i;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100476}
477
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -0700478static unsigned char configure_mmc(mmc_card_data *mmc_card_cur)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100479{
480 unsigned char ret_val;
481 unsigned int argument;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100482 unsigned int trans_clk, trans_fact, trans_unit, retries = 2;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100483 unsigned char trans_speed;
Dirk Behme9de02122009-09-28 08:17:50 -0400484 mmc_resp_t mmc_resp;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100485
486 ret_val = mmc_init_setup();
487
488 if (ret_val != 1)
489 return ret_val;
490
491 do {
492 ret_val = mmc_detect_card(mmc_card_cur);
493 retries--;
494 } while ((retries > 0) && (ret_val != 1));
495
496 argument = mmc_card_cur->RCA << 16;
Dirk Behme9de02122009-09-28 08:17:50 -0400497 ret_val = mmc_send_cmd(MMC_CMD9, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100498 if (ret_val != 1)
499 return ret_val;
500
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100501 if (mmc_card_cur->card_type == MMC_CARD)
Dirk Behme9de02122009-09-28 08:17:50 -0400502 mmc_card_cur->version = mmc_resp.Card_CSD.spec_vers;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100503
Dirk Behme9de02122009-09-28 08:17:50 -0400504 trans_speed = mmc_resp.Card_CSD.tran_speed;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100505
Dirk Behme9de02122009-09-28 08:17:50 -0400506 ret_val = mmc_send_cmd(MMC_CMD4, MMC_DSR_DEFAULT << 16, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100507 if (ret_val != 1)
508 return ret_val;
509
510 trans_unit = trans_speed & MMC_CSD_TRAN_SPEED_UNIT_MASK;
511 trans_fact = trans_speed & MMC_CSD_TRAN_SPEED_FACTOR_MASK;
512
513 if (trans_unit > MMC_CSD_TRAN_SPEED_UNIT_100MHZ)
514 return 0;
515
516 if ((trans_fact < MMC_CSD_TRAN_SPEED_FACTOR_1_0) ||
517 (trans_fact > MMC_CSD_TRAN_SPEED_FACTOR_8_0))
518 return 0;
519
520 trans_unit >>= 0;
521 trans_fact >>= 3;
522
523 trans_clk = mmc_transspeed_val[trans_fact - 1][trans_unit] * 2;
524 ret_val = mmc_clock_config(CLK_MISC, trans_clk);
525
526 if (ret_val != 1)
527 return ret_val;
528
529 argument = mmc_card_cur->RCA << 16;
Dirk Behme9de02122009-09-28 08:17:50 -0400530 ret_val = mmc_send_cmd(MMC_CMD7_SELECT, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100531 if (ret_val != 1)
532 return ret_val;
533
534 /* Configure the block length to 512 bytes */
535 argument = MMCSD_SECTOR_SIZE;
Dirk Behme9de02122009-09-28 08:17:50 -0400536 ret_val = mmc_send_cmd(MMC_CMD16, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100537 if (ret_val != 1)
538 return ret_val;
539
540 /* get the card size in sectors */
Dirk Behme9de02122009-09-28 08:17:50 -0400541 ret_val = mmc_read_cardsize(mmc_card_cur, &mmc_resp.Card_CSD);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100542 if (ret_val != 1)
543 return ret_val;
544
545 return 1;
546}
Grazvydas Ignotas8bbf4302010-08-11 15:51:12 -0700547
Steve Sakoman8f223272010-04-29 10:28:14 -0700548int mmc_legacy_init(int dev)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100549{
Steve Sakoman8f223272010-04-29 10:28:14 -0700550 if (mmc_set_dev(dev) != 0)
551 return 1;
552
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100553 if (configure_mmc(&cur_card_data) != 1)
554 return 1;
555
556 mmc_blk_dev.if_type = IF_TYPE_MMC;
557 mmc_blk_dev.part_type = PART_TYPE_DOS;
558 mmc_blk_dev.dev = 0;
559 mmc_blk_dev.lun = 0;
560 mmc_blk_dev.type = 0;
561
562 /* FIXME fill in the correct size (is set to 32MByte) */
563 mmc_blk_dev.blksz = MMCSD_SECTOR_SIZE;
564 mmc_blk_dev.lba = 0x10000;
565 mmc_blk_dev.removable = 0;
566 mmc_blk_dev.block_read = mmc_bread;
567
568 fat_register_device(&mmc_blk_dev, 1);
569 return 0;
570}