5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <asm/errno.h>
28 #include <asm/arch/hardware.h>
33 extern int fat_register_device(block_dev_desc_t * dev_desc, int part_no);
35 static block_dev_desc_t mmc_dev;
37 block_dev_desc_t *mmc_get_dev(int dev)
39 return ((block_dev_desc_t *) & mmc_dev);
43 * FIXME needs to read cid and csd info to determine block size
44 * and other parameters
46 static uchar mmc_buf[MMC_BLOCK_SIZE];
47 static uchar spec_ver;
48 static int mmc_ready = 0;
52 /****************************************************/
53 mmc_cmd(ushort cmd, ushort argh, ushort argl, ushort cmdat)
54 /****************************************************/
56 static uint32_t resp[4], a, b, c;
60 debug("mmc_cmd %u 0x%04x 0x%04x 0x%04x\n", cmd, argh, argl,
62 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
63 MMC_I_MASK = ~MMC_I_MASK_CLK_IS_OFF;
64 while (!(MMC_I_REG & MMC_I_REG_CLK_IS_OFF)) ;
68 MMC_CMDAT = cmdat | wide;
69 MMC_I_MASK = ~MMC_I_MASK_END_CMD_RES;
70 MMC_STRPCL = MMC_STRPCL_START_CLK;
71 while (!(MMC_I_REG & MMC_I_REG_END_CMD_RES)) ;
74 debug("MMC status 0x%08x\n", status);
75 if (status & MMC_STAT_TIME_OUT_RESPONSE) {
80 * Did I mention this is Sick. We always need to
81 * discard the upper 8 bits of the first 16-bit word.
83 a = (MMC_RES & 0xffff);
84 for (i = 0; i < 4; i++) {
85 b = (MMC_RES & 0xffff);
86 c = (MMC_RES & 0xffff);
87 resp[i] = (a << 24) | (b << 8) | (c >> 8);
89 debug("MMC resp[%d] = %#08x\n", i, resp[i]);
96 /****************************************************/
97 mmc_block_read(uchar * dst, ulong src, ulong len)
98 /****************************************************/
107 debug("mmc_block_rd dst %lx src %lx len %d\n", (ulong) dst, src, len);
113 mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
115 /* send read command */
118 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
122 mmc_cmd(MMC_CMD_READ_BLOCK, argh, argl,
123 MMC_CMDAT_R1 | MMC_CMDAT_READ | MMC_CMDAT_BLOCK |
126 MMC_I_MASK = ~MMC_I_MASK_RXFIFO_RD_REQ;
128 if (MMC_I_REG & MMC_I_REG_RXFIFO_RD_REQ) {
131 for (i = min(len, 32); i; i--) {
132 *dst++ = *((volatile uchar *)&MMC_RXFIFO);
141 if (status & MMC_STAT_ERRORS) {
142 printf("MMC_STAT error %lx\n", status);
146 MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
147 while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE)) ;
149 if (status & MMC_STAT_ERRORS) {
150 printf("MMC_STAT error %lx\n", status);
157 /****************************************************/
158 mmc_block_write(ulong dst, uchar * src, int len)
159 /****************************************************/
168 debug("mmc_block_wr dst %lx src %lx len %d\n", dst, (ulong) src, len);
174 mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
176 /* send write command */
179 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
182 mmc_cmd(MMC_CMD_WRITE_BLOCK, argh, argl,
183 MMC_CMDAT_R1 | MMC_CMDAT_WRITE | MMC_CMDAT_BLOCK |
186 MMC_I_MASK = ~MMC_I_MASK_TXFIFO_WR_REQ;
188 if (MMC_I_REG & MMC_I_REG_TXFIFO_WR_REQ) {
189 int i, bytes = min(32, len);
191 for (i = 0; i < bytes; i++) {
195 MMC_PRTBUF = MMC_PRTBUF_BUF_PART_FULL;
200 if (status & MMC_STAT_ERRORS) {
201 printf("MMC_STAT error %lx\n", status);
205 MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
206 while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE)) ;
207 MMC_I_MASK = ~MMC_I_MASK_PRG_DONE;
208 while (!(MMC_I_REG & MMC_I_REG_PRG_DONE)) ;
210 if (status & MMC_STAT_ERRORS) {
211 printf("MMC_STAT error %lx\n", status);
218 /****************************************************/
219 mmc_read(ulong src, uchar * dst, int size)
220 /****************************************************/
222 ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
223 ulong mmc_block_size, mmc_block_address;
230 printf("Please initial the MMC first\n");
234 mmc_block_size = MMC_BLOCK_SIZE;
235 mmc_block_address = ~(mmc_block_size - 1);
239 part_start = ~mmc_block_address & src;
240 part_end = ~mmc_block_address & end;
241 aligned_start = mmc_block_address & src;
242 aligned_end = mmc_block_address & end;
244 /* all block aligned accesses */
246 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
247 src, (ulong) dst, end, part_start, part_end, aligned_start,
250 part_len = mmc_block_size - part_start;
252 ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
253 src, (ulong) dst, end, part_start, part_end, aligned_start,
255 if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) <
259 memcpy(dst, mmc_buf + part_start, part_len);
264 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
265 src, (ulong) dst, end, part_start, part_end, aligned_start,
267 for (; src < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
269 ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
270 src, (ulong) dst, end, part_start, part_end, aligned_start,
272 if ((mmc_block_read((uchar *) (dst), src, mmc_block_size)) < 0) {
277 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
278 src, (ulong) dst, end, part_start, part_end, aligned_start,
280 if (part_end && src < end) {
282 ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
283 src, (ulong) dst, end, part_start, part_end, aligned_start,
285 if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
288 memcpy(dst, mmc_buf, part_end);
294 /****************************************************/
295 mmc_write(uchar * src, ulong dst, int size)
296 /****************************************************/
298 ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
299 ulong mmc_block_size, mmc_block_address;
306 printf("Please initial the MMC first\n");
310 mmc_block_size = MMC_BLOCK_SIZE;
311 mmc_block_address = ~(mmc_block_size - 1);
315 part_start = ~mmc_block_address & dst;
316 part_end = ~mmc_block_address & end;
317 aligned_start = mmc_block_address & dst;
318 aligned_end = mmc_block_address & end;
320 /* all block aligned accesses */
322 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
323 src, (ulong) dst, end, part_start, part_end, aligned_start,
326 part_len = mmc_block_size - part_start;
328 ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
329 (ulong) src, dst, end, part_start, part_end, aligned_start,
331 if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) <
335 memcpy(mmc_buf + part_start, src, part_len);
336 if ((mmc_block_write(aligned_start, mmc_buf, mmc_block_size)) <
344 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
345 src, (ulong) dst, end, part_start, part_end, aligned_start,
347 for (; dst < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
349 ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
350 src, (ulong) dst, end, part_start, part_end, aligned_start,
352 if ((mmc_block_write(dst, (uchar *) src, mmc_block_size)) < 0) {
357 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
358 src, (ulong) dst, end, part_start, part_end, aligned_start,
360 if (part_end && dst < end) {
362 ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
363 src, (ulong) dst, end, part_start, part_end, aligned_start,
365 if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
368 memcpy(mmc_buf, src, part_end);
369 if ((mmc_block_write(aligned_end, mmc_buf, mmc_block_size)) < 0) {
377 /****************************************************/
378 mmc_bread(int dev_num, ulong blknr, lbaint_t blkcnt, void *dst)
379 /****************************************************/
381 int mmc_block_size = MMC_BLOCK_SIZE;
382 ulong src = blknr * mmc_block_size + CFG_MMC_BASE;
384 mmc_read(src, (uchar *) dst, blkcnt * mmc_block_size);
389 #define likely(x) __builtin_expect(!!(x), 1)
390 #define unlikely(x) __builtin_expect(!!(x), 0)
392 #define likely(x) (x)
393 #define unlikely(x) (x)
396 #define UNSTUFF_BITS(resp,start,size) \
398 const int __size = size; \
399 const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1; \
400 const int32_t __off = 3 - ((start) / 32); \
401 const int32_t __shft = (start) & 31; \
404 __res = resp[__off] >> __shft; \
405 if (__size + __shft > 32) \
406 __res |= resp[__off-1] << ((32 - __shft) % 32); \
411 * Given the decoded CSD structure, decode the raw CID to our CID structure.
413 static void mmc_decode_cid(uint32_t * resp)
415 if (IF_TYPE_SD == mmc_dev.if_type) {
417 * SD doesn't currently have a version field so we will
418 * have to assume we can parse this.
420 sprintf((char *)mmc_dev.vendor,
421 "Man %02x OEM %c%c \"%c%c%c%c%c\" Date %02u/%04u",
422 UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp, 112, 8),
423 UNSTUFF_BITS(resp, 104, 8), UNSTUFF_BITS(resp, 96, 8),
424 UNSTUFF_BITS(resp, 88, 8), UNSTUFF_BITS(resp, 80, 8),
425 UNSTUFF_BITS(resp, 72, 8), UNSTUFF_BITS(resp, 64, 8),
426 UNSTUFF_BITS(resp, 8, 4), UNSTUFF_BITS(resp, 12,
428 sprintf((char *)mmc_dev.revision, "%d.%d",
429 UNSTUFF_BITS(resp, 60, 4), UNSTUFF_BITS(resp, 56, 4));
430 sprintf((char *)mmc_dev.product, "%u",
431 UNSTUFF_BITS(resp, 24, 32));
434 * The selection of the format here is based upon published
435 * specs from sandisk and from what people have reported.
438 case 0: /* MMC v1.0 - v1.2 */
439 case 1: /* MMC v1.4 */
440 sprintf((char *)mmc_dev.vendor,
441 "Man %02x%02x%02x \"%c%c%c%c%c%c%c\" Date %02u/%04u",
442 UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp,
445 UNSTUFF_BITS(resp, 104, 8), UNSTUFF_BITS(resp,
447 UNSTUFF_BITS(resp, 88, 8), UNSTUFF_BITS(resp,
449 UNSTUFF_BITS(resp, 72, 8), UNSTUFF_BITS(resp,
451 UNSTUFF_BITS(resp, 56, 8), UNSTUFF_BITS(resp,
453 UNSTUFF_BITS(resp, 12, 4), UNSTUFF_BITS(resp, 8,
456 sprintf((char *)mmc_dev.revision, "%d.%d",
457 UNSTUFF_BITS(resp, 44, 4), UNSTUFF_BITS(resp,
459 sprintf((char *)mmc_dev.product, "%u",
460 UNSTUFF_BITS(resp, 16, 24));
463 case 2: /* MMC v2.0 - v2.2 */
464 case 3: /* MMC v3.1 - v3.3 */
466 sprintf((char *)mmc_dev.vendor,
467 "Man %02x OEM %04x \"%c%c%c%c%c%c\" Date %02u/%04u",
468 UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp,
471 UNSTUFF_BITS(resp, 96, 8), UNSTUFF_BITS(resp,
473 UNSTUFF_BITS(resp, 80, 8), UNSTUFF_BITS(resp,
475 UNSTUFF_BITS(resp, 64, 8), UNSTUFF_BITS(resp,
477 UNSTUFF_BITS(resp, 12, 4), UNSTUFF_BITS(resp, 8,
480 sprintf((char *)mmc_dev.product, "%u",
481 UNSTUFF_BITS(resp, 16, 32));
482 sprintf((char *)mmc_dev.revision, "N/A");
486 printf("MMC card has unknown MMCA version %d\n",
491 printf("%s card.\nVendor: %s\nProduct: %s\nRevision: %s\n",
492 (IF_TYPE_SD == mmc_dev.if_type) ? "SD" : "MMC", mmc_dev.vendor,
493 mmc_dev.product, mmc_dev.revision);
497 * Given a 128-bit response, decode to our card CSD structure.
499 static void mmc_decode_csd(uint32_t * resp)
501 unsigned int mult, csd_struct;
503 if (IF_TYPE_SD == mmc_dev.if_type) {
504 csd_struct = UNSTUFF_BITS(resp, 126, 2);
505 if (csd_struct != 0) {
506 printf("SD: unrecognised CSD structure version %d\n",
512 * We only understand CSD structure v1.1 and v1.2.
513 * v1.2 has extra information in bits 15, 11 and 10.
515 csd_struct = UNSTUFF_BITS(resp, 126, 2);
516 if (csd_struct != 1 && csd_struct != 2) {
517 printf("MMC: unrecognised CSD structure version %d\n",
522 spec_ver = UNSTUFF_BITS(resp, 122, 4);
523 mmc_dev.if_type = IF_TYPE_MMC;
526 mult = 1 << (UNSTUFF_BITS(resp, 47, 3) + 2);
527 mmc_dev.lba = (1 + UNSTUFF_BITS(resp, 62, 12)) * mult;
528 mmc_dev.blksz = 1 << UNSTUFF_BITS(resp, 80, 4);
530 /* FIXME: The following just makes assumes that's the partition type -- should really read it */
531 mmc_dev.part_type = PART_TYPE_DOS;
534 mmc_dev.type = DEV_TYPE_HARDDISK;
535 mmc_dev.removable = 0;
536 mmc_dev.block_read = mmc_bread;
538 printf("Detected: %u blocks of %u bytes (%uMB) ", mmc_dev.lba,
539 mmc_dev.blksz, mmc_dev.lba * mmc_dev.blksz / (1024 * 1024));
543 /****************************************************/
544 mmc_init(int verbose)
545 /****************************************************/
547 int retries, rc = -ENODEV;
548 uint32_t cid_resp[4];
552 /* Reset device interface type */
553 mmc_dev.if_type = IF_TYPE_UNKNOWN;
555 #if defined (CONFIG_LUBBOCK) || (defined (CONFIG_GUMSTIX) && !defined(CONFIG_PXA27X))
556 set_GPIO_mode(GPIO6_MMCCLK_MD);
557 set_GPIO_mode(GPIO8_MMCCS0_MD);
559 CKEN |= CKEN12_MMC; /* enable MMC unit clock */
560 #if defined(CONFIG_ADSVIX)
561 /* turn on the power */
562 GPCR(114) = GPIO_bit(114);
566 MMC_CLKRT = MMC_CLKRT_0_3125MHZ;
567 MMC_RESTO = MMC_RES_TO_MAX;
568 MMC_SPI = MMC_SPI_DISABLE;
571 mmc_cmd(MMC_CMD_RESET, 0, 0, MMC_CMDAT_INIT | MMC_CMDAT_R0);
575 resp = mmc_cmd(MMC_CMD_APP_CMD, 0, 0, MMC_CMDAT_R1);
576 if (!(resp[0] & 0x00000020)) { /* Card does not support APP_CMD */
577 debug("Card does not support APP_CMD\n");
581 resp = mmc_cmd(SD_CMD_APP_OP_COND, 0x0020, 0, MMC_CMDAT_R3 | (retries < 2 ? 0 : MMC_CMDAT_INIT)); /* Select 3.2-3.3 and 3.3-3.4V */
582 if (resp[0] & 0x80000000) {
583 mmc_dev.if_type = IF_TYPE_SD;
584 debug("Detected SD card\n");
594 if (retries <= 0 || !(IF_TYPE_SD == mmc_dev.if_type)) {
595 debug("Failed to detect SD Card, trying MMC\n");
597 mmc_cmd(MMC_CMD_SEND_OP_COND, 0x00ff, 0x8000, MMC_CMDAT_R3);
600 while (retries-- && resp && !(resp[0] & 0x80000000)) {
607 mmc_cmd(MMC_CMD_SEND_OP_COND, 0x00ff, 0x8000,
612 /* try to get card id */
614 mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, 0, MMC_CMDAT_R2 | MMC_CMDAT_BUSY);
616 memcpy(cid_resp, resp, sizeof(cid_resp));
618 /* MMC exists, get CSD too */
619 resp = mmc_cmd(MMC_CMD_SET_RCA, 0, 0, MMC_CMDAT_R1);
620 if (IF_TYPE_SD == mmc_dev.if_type)
621 rca = ((resp[0] & 0xffff0000) >> 16);
622 resp = mmc_cmd(MMC_CMD_SEND_CSD, rca, 0, MMC_CMDAT_R2);
624 mmc_decode_csd(resp);
629 mmc_decode_cid(cid_resp);
632 MMC_CLKRT = 0; /* 20 MHz */
633 resp = mmc_cmd(MMC_CMD_SELECT_CARD, rca, 0, MMC_CMDAT_R1);
636 if (IF_TYPE_SD == mmc_dev.if_type) {
637 resp = mmc_cmd(MMC_CMD_APP_CMD, rca, 0, MMC_CMDAT_R1);
638 resp = mmc_cmd(SD_CMD_APP_SET_BUS_WIDTH, 0, 2, MMC_CMDAT_R1);
639 wide = MMC_CMDAT_SD_4DAT;
643 fat_register_device(&mmc_dev, 1); /* partitions start counting with 1 */
648 int mmc_ident(block_dev_desc_t * dev)
653 int mmc2info(ulong addr)
655 if (addr >= CFG_MMC_BASE
656 && addr < CFG_MMC_BASE + (mmc_dev.lba * mmc_dev.blksz)) {
662 #endif /* CONFIG_MMC */