blob: 513dd25b0d6d1a1c74961c7d261ae0129c381716 [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>
Tom Rixe5e88c32009-02-19 16:45:43 -060033#include <asm/arch/mmc.h>
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010034
35const unsigned short mmc_transspeed_val[15][4] = {
36 {CLKD(10, 1), CLKD(10, 10), CLKD(10, 100), CLKD(10, 1000)},
37 {CLKD(12, 1), CLKD(12, 10), CLKD(12, 100), CLKD(12, 1000)},
38 {CLKD(13, 1), CLKD(13, 10), CLKD(13, 100), CLKD(13, 1000)},
39 {CLKD(15, 1), CLKD(15, 10), CLKD(15, 100), CLKD(15, 1000)},
40 {CLKD(20, 1), CLKD(20, 10), CLKD(20, 100), CLKD(20, 1000)},
41 {CLKD(26, 1), CLKD(26, 10), CLKD(26, 100), CLKD(26, 1000)},
42 {CLKD(30, 1), CLKD(30, 10), CLKD(30, 100), CLKD(30, 1000)},
43 {CLKD(35, 1), CLKD(35, 10), CLKD(35, 100), CLKD(35, 1000)},
44 {CLKD(40, 1), CLKD(40, 10), CLKD(40, 100), CLKD(40, 1000)},
45 {CLKD(45, 1), CLKD(45, 10), CLKD(45, 100), CLKD(45, 1000)},
46 {CLKD(52, 1), CLKD(52, 10), CLKD(52, 100), CLKD(52, 1000)},
47 {CLKD(55, 1), CLKD(55, 10), CLKD(55, 100), CLKD(55, 1000)},
48 {CLKD(60, 1), CLKD(60, 10), CLKD(60, 100), CLKD(60, 1000)},
49 {CLKD(70, 1), CLKD(70, 10), CLKD(70, 100), CLKD(70, 1000)},
50 {CLKD(80, 1), CLKD(80, 10), CLKD(80, 100), CLKD(80, 1000)}
51};
52
53mmc_card_data cur_card_data;
54static block_dev_desc_t mmc_blk_dev;
Dirk Behmeaba45c82009-02-20 17:51:28 +010055static hsmmc_t *mmc_base = (hsmmc_t *)OMAP_HSMMC_BASE;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010056
57block_dev_desc_t *mmc_get_dev(int dev)
58{
59 return (block_dev_desc_t *) &mmc_blk_dev;
60}
61
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010062unsigned char mmc_board_init(void)
63{
Dirk Behmeaba45c82009-02-20 17:51:28 +010064 t2_t *t2_base = (t2_t *)T2_BASE;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010065
Tom Rixfccc0fc2009-06-28 12:52:31 -050066 twl4030_power_mmc_init();
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010067
Dirk Behmeaba45c82009-02-20 17:51:28 +010068 writel(readl(&t2_base->pbias_lite) | PBIASLITEPWRDNZ1 |
69 PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
70 &t2_base->pbias_lite);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010071
Dirk Behmeaba45c82009-02-20 17:51:28 +010072 writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
73 &t2_base->devconf0);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010074
75 return 1;
76}
77
78void mmc_init_stream(void)
79{
Dirk Behmeaba45c82009-02-20 17:51:28 +010080 writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010081
Dirk Behmeaba45c82009-02-20 17:51:28 +010082 writel(MMC_CMD0, &mmc_base->cmd);
83 while (!(readl(&mmc_base->stat) & CC_MASK));
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010084
Dirk Behmeaba45c82009-02-20 17:51:28 +010085 writel(CC_MASK, &mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010086
Dirk Behmeaba45c82009-02-20 17:51:28 +010087 writel(MMC_CMD0, &mmc_base->cmd);
88 while (!(readl(&mmc_base->stat) & CC_MASK));
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010089
Dirk Behmeaba45c82009-02-20 17:51:28 +010090 writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010091}
92
93unsigned char mmc_clock_config(unsigned int iclk, unsigned short clk_div)
94{
95 unsigned int val;
96
Dirk Behmeaba45c82009-02-20 17:51:28 +010097 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
98 (ICE_STOP | DTO_15THDTO | CEN_DISABLE));
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010099
100 switch (iclk) {
101 case CLK_INITSEQ:
102 val = MMC_INIT_SEQ_CLK / 2;
103 break;
104 case CLK_400KHZ:
105 val = MMC_400kHz_CLK;
106 break;
107 case CLK_MISC:
108 val = clk_div;
109 break;
110 default:
111 return 0;
112 }
Dirk Behmeaba45c82009-02-20 17:51:28 +0100113 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
114 (val << CLKD_OFFSET) | ICE_OSCILLATE);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100115
Dirk Behmeaba45c82009-02-20 17:51:28 +0100116 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100117
Dirk Behmeaba45c82009-02-20 17:51:28 +0100118 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100119 return 1;
120}
121
122unsigned char mmc_init_setup(void)
123{
124 unsigned int reg_val;
125
126 mmc_board_init();
127
Dirk Behmeaba45c82009-02-20 17:51:28 +0100128 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
129 &mmc_base->sysconfig);
130 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100131
Dirk Behmeaba45c82009-02-20 17:51:28 +0100132 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
133 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100134
Dirk Behmeaba45c82009-02-20 17:51:28 +0100135 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
136 writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
137 &mmc_base->capa);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100138
Dirk Behmeaba45c82009-02-20 17:51:28 +0100139 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100140
Dirk Behmeaba45c82009-02-20 17:51:28 +0100141 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
142 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
143 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100144
145 mmc_clock_config(CLK_INITSEQ, 0);
Dirk Behmeaba45c82009-02-20 17:51:28 +0100146 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100147
Dirk Behmeaba45c82009-02-20 17:51:28 +0100148 writel(IE_BADA | IE_CERR | IE_DEB | IE_DCRC | IE_DTO | IE_CIE |
149 IE_CEB | IE_CCRC | IE_CTO | IE_BRR | IE_BWR | IE_TC | IE_CC,
150 &mmc_base->ie);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100151
152 mmc_init_stream();
153 return 1;
154}
155
156unsigned char mmc_send_cmd(unsigned int cmd, unsigned int arg,
Dirk Behmeaba45c82009-02-20 17:51:28 +0100157 unsigned int *response)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100158{
Dirk Behmeaba45c82009-02-20 17:51:28 +0100159 unsigned int mmc_stat;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100160
Dirk Behmeaba45c82009-02-20 17:51:28 +0100161 while ((readl(&mmc_base->pstate) & DATI_MASK) == DATI_CMDDIS);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100162
Dirk Behmeaba45c82009-02-20 17:51:28 +0100163 writel(BLEN_512BYTESLEN | NBLK_STPCNT, &mmc_base->blk);
164 writel(0xFFFFFFFF, &mmc_base->stat);
165 writel(arg, &mmc_base->arg);
166 writel(cmd | CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
167 MSBS_SGLEBLK | ACEN_DISABLE | BCE_DISABLE | DE_DISABLE,
168 &mmc_base->cmd);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100169
170 while (1) {
171 do {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100172 mmc_stat = readl(&mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100173 } while (mmc_stat == 0);
174
175 if ((mmc_stat & ERRI_MASK) != 0)
176 return (unsigned char) mmc_stat;
177
178 if (mmc_stat & CC_MASK) {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100179 writel(CC_MASK, &mmc_base->stat);
180 response[0] = readl(&mmc_base->rsp10);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100181 if ((cmd & RSP_TYPE_MASK) == RSP_TYPE_LGHT136) {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100182 response[1] = readl(&mmc_base->rsp32);
183 response[2] = readl(&mmc_base->rsp54);
184 response[3] = readl(&mmc_base->rsp76);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100185 }
186 break;
187 }
188 }
189 return 1;
190}
191
192unsigned char mmc_read_data(unsigned int *output_buf)
193{
Dirk Behmeaba45c82009-02-20 17:51:28 +0100194 unsigned int mmc_stat;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100195 unsigned int read_count = 0;
196
197 /*
198 * Start Polled Read
199 */
200 while (1) {
201 do {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100202 mmc_stat = readl(&mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100203 } while (mmc_stat == 0);
204
205 if ((mmc_stat & ERRI_MASK) != 0)
206 return (unsigned char) mmc_stat;
207
208 if (mmc_stat & BRR_MASK) {
209 unsigned int k;
210
Dirk Behmeaba45c82009-02-20 17:51:28 +0100211 writel(readl(&mmc_base->stat) | BRR_MASK,
212 &mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100213 for (k = 0; k < MMCSD_SECTOR_SIZE / 4; k++) {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100214 *output_buf = readl(&mmc_base->data);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100215 output_buf++;
216 read_count += 4;
217 }
218 }
219
220 if (mmc_stat & BWR_MASK)
Dirk Behmeaba45c82009-02-20 17:51:28 +0100221 writel(readl(&mmc_base->stat) | BWR_MASK,
222 &mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100223
224 if (mmc_stat & TC_MASK) {
Dirk Behmeaba45c82009-02-20 17:51:28 +0100225 writel(readl(&mmc_base->stat) | TC_MASK,
226 &mmc_base->stat);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100227 break;
228 }
229 }
230 return 1;
231}
232
233unsigned char mmc_detect_card(mmc_card_data *mmc_card_cur)
234{
235 unsigned char err;
236 unsigned int argument = 0;
237 unsigned int ocr_value, ocr_recvd, ret_cmd41, hcs_val;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100238 unsigned short retry_cnt = 2000;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400239 mmc_resp_t mmc_resp;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100240
241 /* Set to Initialization Clock */
242 err = mmc_clock_config(CLK_400KHZ, 0);
243 if (err != 1)
244 return err;
245
246 mmc_card_cur->RCA = MMC_RELATIVE_CARD_ADDRESS;
247 argument = 0x00000000;
248
249 ocr_value = (0x1FF << 15);
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400250 err = mmc_send_cmd(MMC_CMD0, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100251 if (err != 1)
252 return err;
253
254 argument = SD_CMD8_CHECK_PATTERN | SD_CMD8_2_7_3_6_V_RANGE;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400255 err = mmc_send_cmd(MMC_SDCMD8, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100256 hcs_val = (err == 1) ?
257 MMC_OCR_REG_HOST_CAPACITY_SUPPORT_SECTOR :
258 MMC_OCR_REG_HOST_CAPACITY_SUPPORT_BYTE;
259
260 argument = 0x0000 << 16;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400261 err = mmc_send_cmd(MMC_CMD55, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100262 if (err == 1) {
263 mmc_card_cur->card_type = SD_CARD;
264 ocr_value |= hcs_val;
265 ret_cmd41 = MMC_ACMD41;
266 } else {
267 mmc_card_cur->card_type = MMC_CARD;
268 ocr_value |= MMC_OCR_REG_ACCESS_MODE_SECTOR;
269 ret_cmd41 = MMC_CMD1;
Dirk Behmeaba45c82009-02-20 17:51:28 +0100270 writel(readl(&mmc_base->con) & ~OD, &mmc_base->con);
271 writel(readl(&mmc_base->con) | OPENDRAIN, &mmc_base->con);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100272 }
273
274 argument = ocr_value;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400275 err = mmc_send_cmd(ret_cmd41, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100276 if (err != 1)
277 return err;
278
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400279 ocr_recvd = mmc_resp.r3.ocr;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100280
281 while (!(ocr_recvd & (0x1 << 31)) && (retry_cnt > 0)) {
282 retry_cnt--;
283 if (mmc_card_cur->card_type == SD_CARD) {
284 argument = 0x0000 << 16;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400285 err = mmc_send_cmd(MMC_CMD55, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100286 }
287
288 argument = ocr_value;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400289 err = mmc_send_cmd(ret_cmd41, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100290 if (err != 1)
291 return err;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400292 ocr_recvd = mmc_resp.r3.ocr;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100293 }
294
295 if (!(ocr_recvd & (0x1 << 31)))
296 return 0;
297
298 if (mmc_card_cur->card_type == MMC_CARD) {
299 if ((ocr_recvd & MMC_OCR_REG_ACCESS_MODE_MASK) ==
300 MMC_OCR_REG_ACCESS_MODE_SECTOR) {
301 mmc_card_cur->mode = SECTOR_MODE;
302 } else {
303 mmc_card_cur->mode = BYTE_MODE;
304 }
305
306 ocr_recvd &= ~MMC_OCR_REG_ACCESS_MODE_MASK;
307 } else {
308 if ((ocr_recvd & MMC_OCR_REG_HOST_CAPACITY_SUPPORT_MASK)
309 == MMC_OCR_REG_HOST_CAPACITY_SUPPORT_SECTOR) {
310 mmc_card_cur->mode = SECTOR_MODE;
311 } else {
312 mmc_card_cur->mode = BYTE_MODE;
313 }
314 ocr_recvd &= ~MMC_OCR_REG_HOST_CAPACITY_SUPPORT_MASK;
315 }
316
317 ocr_recvd &= ~(0x1 << 31);
318 if (!(ocr_recvd & ocr_value))
319 return 0;
320
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400321 err = mmc_send_cmd(MMC_CMD2, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100322 if (err != 1)
323 return err;
324
325 if (mmc_card_cur->card_type == MMC_CARD) {
326 argument = mmc_card_cur->RCA << 16;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400327 err = mmc_send_cmd(MMC_CMD3, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100328 if (err != 1)
329 return err;
330 } else {
331 argument = 0x00000000;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400332 err = mmc_send_cmd(MMC_SDCMD3, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100333 if (err != 1)
334 return err;
335
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400336 mmc_card_cur->RCA = mmc_resp.r6.newpublishedrca;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100337 }
338
Dirk Behmeaba45c82009-02-20 17:51:28 +0100339 writel(readl(&mmc_base->con) & ~OD, &mmc_base->con);
340 writel(readl(&mmc_base->con) | NOOPENDRAIN, &mmc_base->con);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100341 return 1;
342}
343
344unsigned char mmc_read_cardsize(mmc_card_data *mmc_dev_data,
345 mmc_csd_reg_t *cur_csd)
346{
347 mmc_extended_csd_reg_t ext_csd;
348 unsigned int size, count, blk_len, blk_no, card_size, argument;
349 unsigned char err;
350 unsigned int resp[4];
351
352 if (mmc_dev_data->mode == SECTOR_MODE) {
353 if (mmc_dev_data->card_type == SD_CARD) {
354 card_size =
355 (((mmc_sd2_csd_reg_t *) cur_csd)->
356 c_size_lsb & MMC_SD2_CSD_C_SIZE_LSB_MASK) |
357 ((((mmc_sd2_csd_reg_t *) cur_csd)->
358 c_size_msb & MMC_SD2_CSD_C_SIZE_MSB_MASK)
359 << MMC_SD2_CSD_C_SIZE_MSB_OFFSET);
360 mmc_dev_data->size = card_size * 1024;
361 if (mmc_dev_data->size == 0)
362 return 0;
363 } else {
364 argument = 0x00000000;
365 err = mmc_send_cmd(MMC_CMD8, argument, resp);
366 if (err != 1)
367 return err;
368 err = mmc_read_data((unsigned int *) &ext_csd);
369 if (err != 1)
370 return err;
371 mmc_dev_data->size = ext_csd.sectorcount;
372
373 if (mmc_dev_data->size == 0)
374 mmc_dev_data->size = 8388608;
375 }
376 } else {
377 if (cur_csd->c_size_mult >= 8)
378 return 0;
379
380 if (cur_csd->read_bl_len >= 12)
381 return 0;
382
383 /* Compute size */
384 count = 1 << (cur_csd->c_size_mult + 2);
385 card_size = (cur_csd->c_size_lsb & MMC_CSD_C_SIZE_LSB_MASK) |
386 ((cur_csd->c_size_msb & MMC_CSD_C_SIZE_MSB_MASK)
387 << MMC_CSD_C_SIZE_MSB_OFFSET);
388 blk_no = (card_size + 1) * count;
389 blk_len = 1 << cur_csd->read_bl_len;
390 size = blk_no * blk_len;
391 mmc_dev_data->size = size / MMCSD_SECTOR_SIZE;
392 if (mmc_dev_data->size == 0)
393 return 0;
394 }
395 return 1;
396}
397
398unsigned char omap_mmc_read_sect(unsigned int start_sec, unsigned int num_bytes,
399 mmc_card_data *mmc_c,
400 unsigned long *output_buf)
401{
402 unsigned char err;
403 unsigned int argument;
404 unsigned int resp[4];
405 unsigned int num_sec_val =
406 (num_bytes + (MMCSD_SECTOR_SIZE - 1)) / MMCSD_SECTOR_SIZE;
407 unsigned int sec_inc_val;
408
409 if (num_sec_val == 0)
410 return 1;
411
412 if (mmc_c->mode == SECTOR_MODE) {
413 argument = start_sec;
414 sec_inc_val = 1;
415 } else {
416 argument = start_sec * MMCSD_SECTOR_SIZE;
417 sec_inc_val = MMCSD_SECTOR_SIZE;
418 }
419
420 while (num_sec_val) {
421 err = mmc_send_cmd(MMC_CMD17, argument, resp);
422 if (err != 1)
423 return err;
424
425 err = mmc_read_data((unsigned int *) output_buf);
426 if (err != 1)
427 return err;
428
429 output_buf += (MMCSD_SECTOR_SIZE / 4);
430 argument += sec_inc_val;
431 num_sec_val--;
432 }
433 return 1;
434}
435
436unsigned char configure_mmc(mmc_card_data *mmc_card_cur)
437{
438 unsigned char ret_val;
439 unsigned int argument;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100440 unsigned int trans_clk, trans_fact, trans_unit, retries = 2;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100441 unsigned char trans_speed;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400442 mmc_resp_t mmc_resp;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100443
444 ret_val = mmc_init_setup();
445
446 if (ret_val != 1)
447 return ret_val;
448
449 do {
450 ret_val = mmc_detect_card(mmc_card_cur);
451 retries--;
452 } while ((retries > 0) && (ret_val != 1));
453
454 argument = mmc_card_cur->RCA << 16;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400455 ret_val = mmc_send_cmd(MMC_CMD9, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100456 if (ret_val != 1)
457 return ret_val;
458
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100459 if (mmc_card_cur->card_type == MMC_CARD)
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400460 mmc_card_cur->version = mmc_resp.Card_CSD.spec_vers;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100461
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400462 trans_speed = mmc_resp.Card_CSD.tran_speed;
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100463
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400464 ret_val = mmc_send_cmd(MMC_CMD4, MMC_DSR_DEFAULT << 16, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100465 if (ret_val != 1)
466 return ret_val;
467
468 trans_unit = trans_speed & MMC_CSD_TRAN_SPEED_UNIT_MASK;
469 trans_fact = trans_speed & MMC_CSD_TRAN_SPEED_FACTOR_MASK;
470
471 if (trans_unit > MMC_CSD_TRAN_SPEED_UNIT_100MHZ)
472 return 0;
473
474 if ((trans_fact < MMC_CSD_TRAN_SPEED_FACTOR_1_0) ||
475 (trans_fact > MMC_CSD_TRAN_SPEED_FACTOR_8_0))
476 return 0;
477
478 trans_unit >>= 0;
479 trans_fact >>= 3;
480
481 trans_clk = mmc_transspeed_val[trans_fact - 1][trans_unit] * 2;
482 ret_val = mmc_clock_config(CLK_MISC, trans_clk);
483
484 if (ret_val != 1)
485 return ret_val;
486
487 argument = mmc_card_cur->RCA << 16;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400488 ret_val = mmc_send_cmd(MMC_CMD7_SELECT, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100489 if (ret_val != 1)
490 return ret_val;
491
492 /* Configure the block length to 512 bytes */
493 argument = MMCSD_SECTOR_SIZE;
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400494 ret_val = mmc_send_cmd(MMC_CMD16, argument, mmc_resp.resp);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100495 if (ret_val != 1)
496 return ret_val;
497
498 /* get the card size in sectors */
Dirk Behmeb3ff5662009-09-28 08:17:50 -0400499 ret_val = mmc_read_cardsize(mmc_card_cur, &mmc_resp.Card_CSD);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100500 if (ret_val != 1)
501 return ret_val;
502
503 return 1;
504}
505unsigned long mmc_bread(int dev_num, unsigned long blknr, lbaint_t blkcnt,
506 void *dst)
507{
508 omap_mmc_read_sect(blknr, (blkcnt * MMCSD_SECTOR_SIZE), &cur_card_data,
Dirk Behmeaba45c82009-02-20 17:51:28 +0100509 (unsigned long *) dst);
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100510 return 1;
511}
512
Tom Rixe5e88c32009-02-19 16:45:43 -0600513int mmc_legacy_init(int verbose)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100514{
515 if (configure_mmc(&cur_card_data) != 1)
516 return 1;
517
518 mmc_blk_dev.if_type = IF_TYPE_MMC;
519 mmc_blk_dev.part_type = PART_TYPE_DOS;
520 mmc_blk_dev.dev = 0;
521 mmc_blk_dev.lun = 0;
522 mmc_blk_dev.type = 0;
523
524 /* FIXME fill in the correct size (is set to 32MByte) */
525 mmc_blk_dev.blksz = MMCSD_SECTOR_SIZE;
526 mmc_blk_dev.lba = 0x10000;
527 mmc_blk_dev.removable = 0;
528 mmc_blk_dev.block_read = mmc_bread;
529
530 fat_register_device(&mmc_blk_dev, 1);
531 return 0;
532}