blob: 399c57b70545bec8f757fceef60a9a23ca385e22 [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 Rixe5e88c32009-02-19 16:45:43 -060031#include <asm/arch/mmc.h>
Dirk Behmeb1c3bf92008-12-14 09:47:17 +010032
33const unsigned short mmc_transspeed_val[15][4] = {
34 {CLKD(10, 1), CLKD(10, 10), CLKD(10, 100), CLKD(10, 1000)},
35 {CLKD(12, 1), CLKD(12, 10), CLKD(12, 100), CLKD(12, 1000)},
36 {CLKD(13, 1), CLKD(13, 10), CLKD(13, 100), CLKD(13, 1000)},
37 {CLKD(15, 1), CLKD(15, 10), CLKD(15, 100), CLKD(15, 1000)},
38 {CLKD(20, 1), CLKD(20, 10), CLKD(20, 100), CLKD(20, 1000)},
39 {CLKD(26, 1), CLKD(26, 10), CLKD(26, 100), CLKD(26, 1000)},
40 {CLKD(30, 1), CLKD(30, 10), CLKD(30, 100), CLKD(30, 1000)},
41 {CLKD(35, 1), CLKD(35, 10), CLKD(35, 100), CLKD(35, 1000)},
42 {CLKD(40, 1), CLKD(40, 10), CLKD(40, 100), CLKD(40, 1000)},
43 {CLKD(45, 1), CLKD(45, 10), CLKD(45, 100), CLKD(45, 1000)},
44 {CLKD(52, 1), CLKD(52, 10), CLKD(52, 100), CLKD(52, 1000)},
45 {CLKD(55, 1), CLKD(55, 10), CLKD(55, 100), CLKD(55, 1000)},
46 {CLKD(60, 1), CLKD(60, 10), CLKD(60, 100), CLKD(60, 1000)},
47 {CLKD(70, 1), CLKD(70, 10), CLKD(70, 100), CLKD(70, 1000)},
48 {CLKD(80, 1), CLKD(80, 10), CLKD(80, 100), CLKD(80, 1000)}
49};
50
51mmc_card_data cur_card_data;
52static block_dev_desc_t mmc_blk_dev;
53
54block_dev_desc_t *mmc_get_dev(int dev)
55{
56 return (block_dev_desc_t *) &mmc_blk_dev;
57}
58
59void twl4030_mmc_config(void)
60{
61 unsigned char data;
62
63 data = 0x20;
64 i2c_write(0x4B, 0x82, 1, &data, 1);
65 data = 0x2;
66 i2c_write(0x4B, 0x85, 1, &data, 1);
67}
68
69unsigned char mmc_board_init(void)
70{
71 unsigned int value = 0;
72
73 twl4030_mmc_config();
74
75 value = CONTROL_PBIAS_LITE;
76 CONTROL_PBIAS_LITE = value | (1 << 2) | (1 << 1) | (1 << 9);
77
78 value = CONTROL_DEV_CONF0;
79 CONTROL_DEV_CONF0 = value | (1 << 24);
80
81 return 1;
82}
83
84void mmc_init_stream(void)
85{
86 volatile unsigned int mmc_stat;
87
88 OMAP_HSMMC_CON |= INIT_INITSTREAM;
89
90 OMAP_HSMMC_CMD = MMC_CMD0;
91 do {
92 mmc_stat = OMAP_HSMMC_STAT;
93 } while (!(mmc_stat & CC_MASK));
94
95 OMAP_HSMMC_STAT = CC_MASK;
96
97 OMAP_HSMMC_CMD = MMC_CMD0;
98 do {
99 mmc_stat = OMAP_HSMMC_STAT;
100 } while (!(mmc_stat & CC_MASK));
101
102 OMAP_HSMMC_STAT = OMAP_HSMMC_STAT;
103 OMAP_HSMMC_CON &= ~INIT_INITSTREAM;
104}
105
106unsigned char mmc_clock_config(unsigned int iclk, unsigned short clk_div)
107{
108 unsigned int val;
109
110 mmc_reg_out(OMAP_HSMMC_SYSCTL, (ICE_MASK | DTO_MASK | CEN_MASK),
111 (ICE_STOP | DTO_15THDTO | CEN_DISABLE));
112
113 switch (iclk) {
114 case CLK_INITSEQ:
115 val = MMC_INIT_SEQ_CLK / 2;
116 break;
117 case CLK_400KHZ:
118 val = MMC_400kHz_CLK;
119 break;
120 case CLK_MISC:
121 val = clk_div;
122 break;
123 default:
124 return 0;
125 }
126 mmc_reg_out(OMAP_HSMMC_SYSCTL,
127 ICE_MASK | CLKD_MASK, (val << CLKD_OFFSET) | ICE_OSCILLATE);
128
129 while ((OMAP_HSMMC_SYSCTL & ICS_MASK) == ICS_NOTREADY) ;
130
131 OMAP_HSMMC_SYSCTL |= CEN_ENABLE;
132 return 1;
133}
134
135unsigned char mmc_init_setup(void)
136{
137 unsigned int reg_val;
138
139 mmc_board_init();
140
141 OMAP_HSMMC_SYSCONFIG |= MMC_SOFTRESET;
142 while ((OMAP_HSMMC_SYSSTATUS & RESETDONE) == 0) ;
143
144 OMAP_HSMMC_SYSCTL |= SOFTRESETALL;
145 while ((OMAP_HSMMC_SYSCTL & SOFTRESETALL) != 0x0) ;
146
147 OMAP_HSMMC_HCTL = DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0;
148 OMAP_HSMMC_CAPA |= VS30_3V0SUP | VS18_1V8SUP;
149
150 reg_val = OMAP_HSMMC_CON & RESERVED_MASK;
151
152 OMAP_HSMMC_CON = CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH |
153 CDP_ACTIVEHIGH | MIT_CTO | DW8_1_4BITMODE | MODE_FUNC |
154 STR_BLOCK | HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN;
155
156 mmc_clock_config(CLK_INITSEQ, 0);
157 OMAP_HSMMC_HCTL |= SDBP_PWRON;
158
159 OMAP_HSMMC_IE = 0x307f0033;
160
161 mmc_init_stream();
162 return 1;
163}
164
165unsigned char mmc_send_cmd(unsigned int cmd, unsigned int arg,
166 unsigned int *response)
167{
168 volatile unsigned int mmc_stat;
169
170 while ((OMAP_HSMMC_PSTATE & DATI_MASK) == DATI_CMDDIS) ;
171
172 OMAP_HSMMC_BLK = BLEN_512BYTESLEN | NBLK_STPCNT;
173 OMAP_HSMMC_STAT = 0xFFFFFFFF;
174 OMAP_HSMMC_ARG = arg;
175 OMAP_HSMMC_CMD = cmd | CMD_TYPE_NORMAL | CICE_NOCHECK |
176 CCCE_NOCHECK | MSBS_SGLEBLK | ACEN_DISABLE | BCE_DISABLE |
177 DE_DISABLE;
178
179 while (1) {
180 do {
181 mmc_stat = OMAP_HSMMC_STAT;
182 } while (mmc_stat == 0);
183
184 if ((mmc_stat & ERRI_MASK) != 0)
185 return (unsigned char) mmc_stat;
186
187 if (mmc_stat & CC_MASK) {
188 OMAP_HSMMC_STAT = CC_MASK;
189 response[0] = OMAP_HSMMC_RSP10;
190 if ((cmd & RSP_TYPE_MASK) == RSP_TYPE_LGHT136) {
191 response[1] = OMAP_HSMMC_RSP32;
192 response[2] = OMAP_HSMMC_RSP54;
193 response[3] = OMAP_HSMMC_RSP76;
194 }
195 break;
196 }
197 }
198 return 1;
199}
200
201unsigned char mmc_read_data(unsigned int *output_buf)
202{
203 volatile unsigned int mmc_stat;
204 unsigned int read_count = 0;
205
206 /*
207 * Start Polled Read
208 */
209 while (1) {
210 do {
211 mmc_stat = OMAP_HSMMC_STAT;
212 } while (mmc_stat == 0);
213
214 if ((mmc_stat & ERRI_MASK) != 0)
215 return (unsigned char) mmc_stat;
216
217 if (mmc_stat & BRR_MASK) {
218 unsigned int k;
219
220 OMAP_HSMMC_STAT |= BRR_MASK;
221 for (k = 0; k < MMCSD_SECTOR_SIZE / 4; k++) {
222 *output_buf = OMAP_HSMMC_DATA;
223 output_buf++;
224 read_count += 4;
225 }
226 }
227
228 if (mmc_stat & BWR_MASK)
229 OMAP_HSMMC_STAT |= BWR_MASK;
230
231 if (mmc_stat & TC_MASK) {
232 OMAP_HSMMC_STAT |= TC_MASK;
233 break;
234 }
235 }
236 return 1;
237}
238
239unsigned char mmc_detect_card(mmc_card_data *mmc_card_cur)
240{
241 unsigned char err;
242 unsigned int argument = 0;
243 unsigned int ocr_value, ocr_recvd, ret_cmd41, hcs_val;
244 unsigned int resp[4];
245 unsigned short retry_cnt = 2000;
246
247 /* Set to Initialization Clock */
248 err = mmc_clock_config(CLK_400KHZ, 0);
249 if (err != 1)
250 return err;
251
252 mmc_card_cur->RCA = MMC_RELATIVE_CARD_ADDRESS;
253 argument = 0x00000000;
254
255 ocr_value = (0x1FF << 15);
256 err = mmc_send_cmd(MMC_CMD0, argument, resp);
257 if (err != 1)
258 return err;
259
260 argument = SD_CMD8_CHECK_PATTERN | SD_CMD8_2_7_3_6_V_RANGE;
261 err = mmc_send_cmd(MMC_SDCMD8, argument, resp);
262 hcs_val = (err == 1) ?
263 MMC_OCR_REG_HOST_CAPACITY_SUPPORT_SECTOR :
264 MMC_OCR_REG_HOST_CAPACITY_SUPPORT_BYTE;
265
266 argument = 0x0000 << 16;
267 err = mmc_send_cmd(MMC_CMD55, argument, resp);
268 if (err == 1) {
269 mmc_card_cur->card_type = SD_CARD;
270 ocr_value |= hcs_val;
271 ret_cmd41 = MMC_ACMD41;
272 } else {
273 mmc_card_cur->card_type = MMC_CARD;
274 ocr_value |= MMC_OCR_REG_ACCESS_MODE_SECTOR;
275 ret_cmd41 = MMC_CMD1;
276 OMAP_HSMMC_CON &= ~OD;
277 OMAP_HSMMC_CON |= OPENDRAIN;
278 }
279
280 argument = ocr_value;
281 err = mmc_send_cmd(ret_cmd41, argument, resp);
282 if (err != 1)
283 return err;
284
285 ocr_recvd = ((mmc_resp_r3 *) resp)->ocr;
286
287 while (!(ocr_recvd & (0x1 << 31)) && (retry_cnt > 0)) {
288 retry_cnt--;
289 if (mmc_card_cur->card_type == SD_CARD) {
290 argument = 0x0000 << 16;
291 err = mmc_send_cmd(MMC_CMD55, argument, resp);
292 }
293
294 argument = ocr_value;
295 err = mmc_send_cmd(ret_cmd41, argument, resp);
296 if (err != 1)
297 return err;
298 ocr_recvd = ((mmc_resp_r3 *) resp)->ocr;
299 }
300
301 if (!(ocr_recvd & (0x1 << 31)))
302 return 0;
303
304 if (mmc_card_cur->card_type == MMC_CARD) {
305 if ((ocr_recvd & MMC_OCR_REG_ACCESS_MODE_MASK) ==
306 MMC_OCR_REG_ACCESS_MODE_SECTOR) {
307 mmc_card_cur->mode = SECTOR_MODE;
308 } else {
309 mmc_card_cur->mode = BYTE_MODE;
310 }
311
312 ocr_recvd &= ~MMC_OCR_REG_ACCESS_MODE_MASK;
313 } else {
314 if ((ocr_recvd & MMC_OCR_REG_HOST_CAPACITY_SUPPORT_MASK)
315 == MMC_OCR_REG_HOST_CAPACITY_SUPPORT_SECTOR) {
316 mmc_card_cur->mode = SECTOR_MODE;
317 } else {
318 mmc_card_cur->mode = BYTE_MODE;
319 }
320 ocr_recvd &= ~MMC_OCR_REG_HOST_CAPACITY_SUPPORT_MASK;
321 }
322
323 ocr_recvd &= ~(0x1 << 31);
324 if (!(ocr_recvd & ocr_value))
325 return 0;
326
327 err = mmc_send_cmd(MMC_CMD2, argument, resp);
328 if (err != 1)
329 return err;
330
331 if (mmc_card_cur->card_type == MMC_CARD) {
332 argument = mmc_card_cur->RCA << 16;
333 err = mmc_send_cmd(MMC_CMD3, argument, resp);
334 if (err != 1)
335 return err;
336 } else {
337 argument = 0x00000000;
338 err = mmc_send_cmd(MMC_SDCMD3, argument, resp);
339 if (err != 1)
340 return err;
341
342 mmc_card_cur->RCA = ((mmc_resp_r6 *) resp)->newpublishedrca;
343 }
344
345 OMAP_HSMMC_CON &= ~OD;
346 OMAP_HSMMC_CON |= NOOPENDRAIN;
347 return 1;
348}
349
350unsigned char mmc_read_cardsize(mmc_card_data *mmc_dev_data,
351 mmc_csd_reg_t *cur_csd)
352{
353 mmc_extended_csd_reg_t ext_csd;
354 unsigned int size, count, blk_len, blk_no, card_size, argument;
355 unsigned char err;
356 unsigned int resp[4];
357
358 if (mmc_dev_data->mode == SECTOR_MODE) {
359 if (mmc_dev_data->card_type == SD_CARD) {
360 card_size =
361 (((mmc_sd2_csd_reg_t *) cur_csd)->
362 c_size_lsb & MMC_SD2_CSD_C_SIZE_LSB_MASK) |
363 ((((mmc_sd2_csd_reg_t *) cur_csd)->
364 c_size_msb & MMC_SD2_CSD_C_SIZE_MSB_MASK)
365 << MMC_SD2_CSD_C_SIZE_MSB_OFFSET);
366 mmc_dev_data->size = card_size * 1024;
367 if (mmc_dev_data->size == 0)
368 return 0;
369 } else {
370 argument = 0x00000000;
371 err = mmc_send_cmd(MMC_CMD8, argument, resp);
372 if (err != 1)
373 return err;
374 err = mmc_read_data((unsigned int *) &ext_csd);
375 if (err != 1)
376 return err;
377 mmc_dev_data->size = ext_csd.sectorcount;
378
379 if (mmc_dev_data->size == 0)
380 mmc_dev_data->size = 8388608;
381 }
382 } else {
383 if (cur_csd->c_size_mult >= 8)
384 return 0;
385
386 if (cur_csd->read_bl_len >= 12)
387 return 0;
388
389 /* Compute size */
390 count = 1 << (cur_csd->c_size_mult + 2);
391 card_size = (cur_csd->c_size_lsb & MMC_CSD_C_SIZE_LSB_MASK) |
392 ((cur_csd->c_size_msb & MMC_CSD_C_SIZE_MSB_MASK)
393 << MMC_CSD_C_SIZE_MSB_OFFSET);
394 blk_no = (card_size + 1) * count;
395 blk_len = 1 << cur_csd->read_bl_len;
396 size = blk_no * blk_len;
397 mmc_dev_data->size = size / MMCSD_SECTOR_SIZE;
398 if (mmc_dev_data->size == 0)
399 return 0;
400 }
401 return 1;
402}
403
404unsigned char omap_mmc_read_sect(unsigned int start_sec, unsigned int num_bytes,
405 mmc_card_data *mmc_c,
406 unsigned long *output_buf)
407{
408 unsigned char err;
409 unsigned int argument;
410 unsigned int resp[4];
411 unsigned int num_sec_val =
412 (num_bytes + (MMCSD_SECTOR_SIZE - 1)) / MMCSD_SECTOR_SIZE;
413 unsigned int sec_inc_val;
414
415 if (num_sec_val == 0)
416 return 1;
417
418 if (mmc_c->mode == SECTOR_MODE) {
419 argument = start_sec;
420 sec_inc_val = 1;
421 } else {
422 argument = start_sec * MMCSD_SECTOR_SIZE;
423 sec_inc_val = MMCSD_SECTOR_SIZE;
424 }
425
426 while (num_sec_val) {
427 err = mmc_send_cmd(MMC_CMD17, argument, resp);
428 if (err != 1)
429 return err;
430
431 err = mmc_read_data((unsigned int *) output_buf);
432 if (err != 1)
433 return err;
434
435 output_buf += (MMCSD_SECTOR_SIZE / 4);
436 argument += sec_inc_val;
437 num_sec_val--;
438 }
439 return 1;
440}
441
442unsigned char configure_mmc(mmc_card_data *mmc_card_cur)
443{
444 unsigned char ret_val;
445 unsigned int argument;
446 unsigned int resp[4];
447 unsigned int trans_clk, trans_fact, trans_unit, retries = 2;
448 mmc_csd_reg_t Card_CSD;
449 unsigned char trans_speed;
450
451 ret_val = mmc_init_setup();
452
453 if (ret_val != 1)
454 return ret_val;
455
456 do {
457 ret_val = mmc_detect_card(mmc_card_cur);
458 retries--;
459 } while ((retries > 0) && (ret_val != 1));
460
461 argument = mmc_card_cur->RCA << 16;
462 ret_val = mmc_send_cmd(MMC_CMD9, argument, resp);
463 if (ret_val != 1)
464 return ret_val;
465
466 ((unsigned int *) &Card_CSD)[3] = resp[3];
467 ((unsigned int *) &Card_CSD)[2] = resp[2];
468 ((unsigned int *) &Card_CSD)[1] = resp[1];
469 ((unsigned int *) &Card_CSD)[0] = resp[0];
470
471 if (mmc_card_cur->card_type == MMC_CARD)
472 mmc_card_cur->version = Card_CSD.spec_vers;
473
474 trans_speed = Card_CSD.tran_speed;
475
476 ret_val = mmc_send_cmd(MMC_CMD4, MMC_DSR_DEFAULT << 16, resp);
477 if (ret_val != 1)
478 return ret_val;
479
480 trans_unit = trans_speed & MMC_CSD_TRAN_SPEED_UNIT_MASK;
481 trans_fact = trans_speed & MMC_CSD_TRAN_SPEED_FACTOR_MASK;
482
483 if (trans_unit > MMC_CSD_TRAN_SPEED_UNIT_100MHZ)
484 return 0;
485
486 if ((trans_fact < MMC_CSD_TRAN_SPEED_FACTOR_1_0) ||
487 (trans_fact > MMC_CSD_TRAN_SPEED_FACTOR_8_0))
488 return 0;
489
490 trans_unit >>= 0;
491 trans_fact >>= 3;
492
493 trans_clk = mmc_transspeed_val[trans_fact - 1][trans_unit] * 2;
494 ret_val = mmc_clock_config(CLK_MISC, trans_clk);
495
496 if (ret_val != 1)
497 return ret_val;
498
499 argument = mmc_card_cur->RCA << 16;
500 ret_val = mmc_send_cmd(MMC_CMD7_SELECT, argument, resp);
501 if (ret_val != 1)
502 return ret_val;
503
504 /* Configure the block length to 512 bytes */
505 argument = MMCSD_SECTOR_SIZE;
506 ret_val = mmc_send_cmd(MMC_CMD16, argument, resp);
507 if (ret_val != 1)
508 return ret_val;
509
510 /* get the card size in sectors */
511 ret_val = mmc_read_cardsize(mmc_card_cur, &Card_CSD);
512 if (ret_val != 1)
513 return ret_val;
514
515 return 1;
516}
517unsigned long mmc_bread(int dev_num, unsigned long blknr, lbaint_t blkcnt,
518 void *dst)
519{
520 omap_mmc_read_sect(blknr, (blkcnt * MMCSD_SECTOR_SIZE), &cur_card_data,
521 (unsigned long *) dst);
522 return 1;
523}
524
Tom Rixe5e88c32009-02-19 16:45:43 -0600525int mmc_legacy_init(int verbose)
Dirk Behmeb1c3bf92008-12-14 09:47:17 +0100526{
527 if (configure_mmc(&cur_card_data) != 1)
528 return 1;
529
530 mmc_blk_dev.if_type = IF_TYPE_MMC;
531 mmc_blk_dev.part_type = PART_TYPE_DOS;
532 mmc_blk_dev.dev = 0;
533 mmc_blk_dev.lun = 0;
534 mmc_blk_dev.type = 0;
535
536 /* FIXME fill in the correct size (is set to 32MByte) */
537 mmc_blk_dev.blksz = MMCSD_SECTOR_SIZE;
538 mmc_blk_dev.lba = 0x10000;
539 mmc_blk_dev.removable = 0;
540 mmc_blk_dev.block_read = mmc_bread;
541
542 fat_register_device(&mmc_blk_dev, 1);
543 return 0;
544}