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