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