3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
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>
35 extern int fat_register_device(block_dev_desc_t * dev_desc, int part_no);
37 static block_dev_desc_t mmc_dev;
39 block_dev_desc_t *mmc_get_dev(int dev)
41 return ((block_dev_desc_t *) & mmc_dev);
45 * FIXME needs to read cid and csd info to determine block size
46 * and other parameters
48 static uchar mmc_buf[MMC_BLOCK_SIZE];
49 static uchar spec_ver;
50 static int mmc_ready = 0;
54 /****************************************************/
55 mmc_cmd(ushort cmd, ushort argh, ushort argl, ushort cmdat)
56 /****************************************************/
58 static uint32_t resp[4], a, b, c;
62 debug("mmc_cmd %u 0x%04x 0x%04x 0x%04x\n", cmd, argh, argl,
64 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
65 MMC_I_MASK = ~MMC_I_MASK_CLK_IS_OFF;
66 while (!(MMC_I_REG & MMC_I_REG_CLK_IS_OFF)) ;
70 MMC_CMDAT = cmdat | wide;
71 MMC_I_MASK = ~MMC_I_MASK_END_CMD_RES;
72 MMC_STRPCL = MMC_STRPCL_START_CLK;
73 while (!(MMC_I_REG & MMC_I_REG_END_CMD_RES)) ;
76 debug("MMC status 0x%08x\n", status);
77 if (status & MMC_STAT_TIME_OUT_RESPONSE) {
82 * Did I mention this is Sick. We always need to
83 * discard the upper 8 bits of the first 16-bit word.
85 a = (MMC_RES & 0xffff);
86 for (i = 0; i < 4; i++) {
87 b = (MMC_RES & 0xffff);
88 c = (MMC_RES & 0xffff);
89 resp[i] = (a << 24) | (b << 8) | (c >> 8);
91 debug("MMC resp[%d] = %#08x\n", i, resp[i]);
98 /****************************************************/
99 mmc_block_read(uchar * dst, ulong src, ulong len)
100 /****************************************************/
109 debug("mmc_block_rd dst %lx src %lx len %d\n", (ulong) dst, src, len);
115 mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
117 /* send read command */
120 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
124 mmc_cmd(MMC_CMD_READ_SINGLE_BLOCK, argh, argl,
125 MMC_CMDAT_R1 | MMC_CMDAT_READ | MMC_CMDAT_BLOCK |
128 MMC_I_MASK = ~MMC_I_MASK_RXFIFO_RD_REQ;
130 if (MMC_I_REG & MMC_I_REG_RXFIFO_RD_REQ) {
133 for (i = min(len, 32); i; i--) {
134 *dst++ = *((volatile uchar *)&MMC_RXFIFO);
143 if (status & MMC_STAT_ERRORS) {
144 printf("MMC_STAT error %lx\n", status);
148 MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
149 while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE)) ;
151 if (status & MMC_STAT_ERRORS) {
152 printf("MMC_STAT error %lx\n", status);
159 /****************************************************/
160 mmc_block_write(ulong dst, uchar * src, int len)
161 /****************************************************/
170 debug("mmc_block_wr dst %lx src %lx len %d\n", dst, (ulong) src, len);
176 mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
178 /* send write command */
181 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
184 mmc_cmd(MMC_CMD_WRITE_BLOCK, argh, argl,
185 MMC_CMDAT_R1 | MMC_CMDAT_WRITE | MMC_CMDAT_BLOCK |
188 MMC_I_MASK = ~MMC_I_MASK_TXFIFO_WR_REQ;
190 if (MMC_I_REG & MMC_I_REG_TXFIFO_WR_REQ) {
191 int i, bytes = min(32, len);
193 for (i = 0; i < bytes; i++) {
197 MMC_PRTBUF = MMC_PRTBUF_BUF_PART_FULL;
202 if (status & MMC_STAT_ERRORS) {
203 printf("MMC_STAT error %lx\n", status);
207 MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
208 while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE)) ;
209 MMC_I_MASK = ~MMC_I_MASK_PRG_DONE;
210 while (!(MMC_I_REG & MMC_I_REG_PRG_DONE)) ;
212 if (status & MMC_STAT_ERRORS) {
213 printf("MMC_STAT error %lx\n", status);
220 /****************************************************/
221 mmc_read(ulong src, uchar * dst, int size)
222 /****************************************************/
224 ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
225 ulong mmc_block_size, mmc_block_address;
232 printf("Please initial the MMC first\n");
236 mmc_block_size = MMC_BLOCK_SIZE;
237 mmc_block_address = ~(mmc_block_size - 1);
239 src -= CONFIG_SYS_MMC_BASE;
241 part_start = ~mmc_block_address & src;
242 part_end = ~mmc_block_address & end;
243 aligned_start = mmc_block_address & src;
244 aligned_end = mmc_block_address & end;
246 /* all block aligned accesses */
248 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
249 src, (ulong) dst, end, part_start, part_end, aligned_start,
252 part_len = mmc_block_size - part_start;
254 ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
255 src, (ulong) dst, end, part_start, part_end, aligned_start,
257 if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) <
261 memcpy(dst, mmc_buf + part_start, part_len);
266 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
267 src, (ulong) dst, end, part_start, part_end, aligned_start,
269 for (; src < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
271 ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
272 src, (ulong) dst, end, part_start, part_end, aligned_start,
274 if ((mmc_block_read((uchar *) (dst), src, mmc_block_size)) < 0) {
279 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
280 src, (ulong) dst, end, part_start, part_end, aligned_start,
282 if (part_end && src < end) {
284 ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
285 src, (ulong) dst, end, part_start, part_end, aligned_start,
287 if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
290 memcpy(dst, mmc_buf, part_end);
296 /****************************************************/
297 mmc_write(uchar * src, ulong dst, int size)
298 /****************************************************/
300 ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
301 ulong mmc_block_size, mmc_block_address;
308 printf("Please initial the MMC first\n");
312 mmc_block_size = MMC_BLOCK_SIZE;
313 mmc_block_address = ~(mmc_block_size - 1);
315 dst -= CONFIG_SYS_MMC_BASE;
317 part_start = ~mmc_block_address & dst;
318 part_end = ~mmc_block_address & end;
319 aligned_start = mmc_block_address & dst;
320 aligned_end = mmc_block_address & end;
322 /* all block aligned accesses */
324 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
325 src, (ulong) dst, end, part_start, part_end, aligned_start,
328 part_len = mmc_block_size - part_start;
330 ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
331 (ulong) src, dst, end, part_start, part_end, aligned_start,
333 if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) <
337 memcpy(mmc_buf + part_start, src, part_len);
338 if ((mmc_block_write(aligned_start, mmc_buf, mmc_block_size)) <
346 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
347 src, (ulong) dst, end, part_start, part_end, aligned_start,
349 for (; dst < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
351 ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
352 src, (ulong) dst, end, part_start, part_end, aligned_start,
354 if ((mmc_block_write(dst, (uchar *) src, mmc_block_size)) < 0) {
359 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
360 src, (ulong) dst, end, part_start, part_end, aligned_start,
362 if (part_end && dst < end) {
364 ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
365 src, (ulong) dst, end, part_start, part_end, aligned_start,
367 if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
370 memcpy(mmc_buf, src, part_end);
371 if ((mmc_block_write(aligned_end, mmc_buf, mmc_block_size)) < 0) {
379 /****************************************************/
380 mmc_bread(int dev_num, ulong blknr, lbaint_t blkcnt, void *dst)
381 /****************************************************/
383 int mmc_block_size = MMC_BLOCK_SIZE;
384 ulong src = blknr * mmc_block_size + CONFIG_SYS_MMC_BASE;
386 mmc_read(src, (uchar *) dst, blkcnt * mmc_block_size);
391 #define likely(x) __builtin_expect(!!(x), 1)
392 #define unlikely(x) __builtin_expect(!!(x), 0)
394 #define likely(x) (x)
395 #define unlikely(x) (x)
398 #define UNSTUFF_BITS(resp,start,size) \
400 const int __size = size; \
401 const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1; \
402 const int32_t __off = 3 - ((start) / 32); \
403 const int32_t __shft = (start) & 31; \
406 __res = resp[__off] >> __shft; \
407 if (__size + __shft > 32) \
408 __res |= resp[__off-1] << ((32 - __shft) % 32); \
413 * Given the decoded CSD structure, decode the raw CID to our CID structure.
415 static void mmc_decode_cid(uint32_t * resp)
417 if (IF_TYPE_SD == mmc_dev.if_type) {
419 * SD doesn't currently have a version field so we will
420 * have to assume we can parse this.
422 sprintf((char *)mmc_dev.vendor,
423 "Man %02x OEM %c%c \"%c%c%c%c%c\" Date %02u/%04u",
424 UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp, 112, 8),
425 UNSTUFF_BITS(resp, 104, 8), UNSTUFF_BITS(resp, 96, 8),
426 UNSTUFF_BITS(resp, 88, 8), UNSTUFF_BITS(resp, 80, 8),
427 UNSTUFF_BITS(resp, 72, 8), UNSTUFF_BITS(resp, 64, 8),
428 UNSTUFF_BITS(resp, 8, 4), UNSTUFF_BITS(resp, 12,
430 sprintf((char *)mmc_dev.revision, "%d.%d",
431 UNSTUFF_BITS(resp, 60, 4), UNSTUFF_BITS(resp, 56, 4));
432 sprintf((char *)mmc_dev.product, "%u",
433 UNSTUFF_BITS(resp, 24, 32));
436 * The selection of the format here is based upon published
437 * specs from sandisk and from what people have reported.
440 case 0: /* MMC v1.0 - v1.2 */
441 case 1: /* MMC v1.4 */
442 sprintf((char *)mmc_dev.vendor,
443 "Man %02x%02x%02x \"%c%c%c%c%c%c%c\" Date %02u/%04u",
444 UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp,
447 UNSTUFF_BITS(resp, 104, 8), UNSTUFF_BITS(resp,
449 UNSTUFF_BITS(resp, 88, 8), UNSTUFF_BITS(resp,
451 UNSTUFF_BITS(resp, 72, 8), UNSTUFF_BITS(resp,
453 UNSTUFF_BITS(resp, 56, 8), UNSTUFF_BITS(resp,
455 UNSTUFF_BITS(resp, 12, 4), UNSTUFF_BITS(resp, 8,
458 sprintf((char *)mmc_dev.revision, "%d.%d",
459 UNSTUFF_BITS(resp, 44, 4), UNSTUFF_BITS(resp,
461 sprintf((char *)mmc_dev.product, "%u",
462 UNSTUFF_BITS(resp, 16, 24));
465 case 2: /* MMC v2.0 - v2.2 */
466 case 3: /* MMC v3.1 - v3.3 */
468 sprintf((char *)mmc_dev.vendor,
469 "Man %02x OEM %04x \"%c%c%c%c%c%c\" Date %02u/%04u",
470 UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp,
473 UNSTUFF_BITS(resp, 96, 8), UNSTUFF_BITS(resp,
475 UNSTUFF_BITS(resp, 80, 8), UNSTUFF_BITS(resp,
477 UNSTUFF_BITS(resp, 64, 8), UNSTUFF_BITS(resp,
479 UNSTUFF_BITS(resp, 12, 4), UNSTUFF_BITS(resp, 8,
482 sprintf((char *)mmc_dev.product, "%u",
483 UNSTUFF_BITS(resp, 16, 32));
484 sprintf((char *)mmc_dev.revision, "N/A");
488 printf("MMC card has unknown MMCA version %d\n",
493 printf("%s card.\nVendor: %s\nProduct: %s\nRevision: %s\n",
494 (IF_TYPE_SD == mmc_dev.if_type) ? "SD" : "MMC", mmc_dev.vendor,
495 mmc_dev.product, mmc_dev.revision);
499 * Given a 128-bit response, decode to our card CSD structure.
501 static void mmc_decode_csd(uint32_t * resp)
503 unsigned int mult, csd_struct;
505 if (IF_TYPE_SD == mmc_dev.if_type) {
506 csd_struct = UNSTUFF_BITS(resp, 126, 2);
507 if (csd_struct != 0) {
508 printf("SD: unrecognised CSD structure version %d\n",
514 * We only understand CSD structure v1.1 and v1.2.
515 * v1.2 has extra information in bits 15, 11 and 10.
517 csd_struct = UNSTUFF_BITS(resp, 126, 2);
518 if (csd_struct != 1 && csd_struct != 2) {
519 printf("MMC: unrecognised CSD structure version %d\n",
524 spec_ver = UNSTUFF_BITS(resp, 122, 4);
525 mmc_dev.if_type = IF_TYPE_MMC;
528 mult = 1 << (UNSTUFF_BITS(resp, 47, 3) + 2);
529 mmc_dev.lba = (1 + UNSTUFF_BITS(resp, 62, 12)) * mult;
530 mmc_dev.blksz = 1 << UNSTUFF_BITS(resp, 80, 4);
532 /* FIXME: The following just makes assumes that's the partition type -- should really read it */
533 mmc_dev.part_type = PART_TYPE_DOS;
536 mmc_dev.type = DEV_TYPE_HARDDISK;
537 mmc_dev.removable = 0;
538 mmc_dev.block_read = mmc_bread;
540 printf("Detected: %lu blocks of %lu bytes (%luMB) ",
543 mmc_dev.lba * mmc_dev.blksz / (1024 * 1024));
547 /****************************************************/
548 mmc_legacy_init(int verbose)
549 /****************************************************/
551 int retries, rc = -ENODEV;
552 uint32_t cid_resp[4];
556 /* Reset device interface type */
557 mmc_dev.if_type = IF_TYPE_UNKNOWN;
559 #if defined (CONFIG_LUBBOCK) || (defined (CONFIG_GUMSTIX) && !defined(CONFIG_PXA27X))
560 set_GPIO_mode(GPIO6_MMCCLK_MD);
561 set_GPIO_mode(GPIO8_MMCCS0_MD);
563 CKEN |= CKEN12_MMC; /* enable MMC unit clock */
565 MMC_CLKRT = MMC_CLKRT_0_3125MHZ;
566 MMC_RESTO = MMC_RES_TO_MAX;
567 MMC_SPI = MMC_SPI_DISABLE;
570 mmc_cmd(MMC_CMD_GO_IDLE_STATE, 0, 0, MMC_CMDAT_INIT | MMC_CMDAT_R0);
574 resp = mmc_cmd(MMC_CMD_APP_CMD, 0, 0, MMC_CMDAT_R1);
575 if (!(resp[0] & 0x00000020)) { /* Card does not support APP_CMD */
576 debug("Card does not support APP_CMD\n");
580 /* Select 3.2-3.3V and 3.3-3.4V */
581 resp = mmc_cmd(SD_CMD_APP_SEND_OP_COND, 0x0030, 0x0000,
582 MMC_CMDAT_R3 | (retries < 2 ? 0
584 if (resp[0] & 0x80000000) {
585 mmc_dev.if_type = IF_TYPE_SD;
586 debug("Detected SD card\n");
596 if (retries <= 0 || !(IF_TYPE_SD == mmc_dev.if_type)) {
597 debug("Failed to detect SD Card, trying MMC\n");
599 mmc_cmd(MMC_CMD_SEND_OP_COND, 0x00ff, 0x8000, MMC_CMDAT_R3);
602 while (retries-- && resp && !(resp[0] & 0x80000000)) {
609 mmc_cmd(MMC_CMD_SEND_OP_COND, 0x00ff, 0x8000,
614 /* try to get card id */
616 mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, 0, MMC_CMDAT_R2 | MMC_CMDAT_BUSY);
618 memcpy(cid_resp, resp, sizeof(cid_resp));
620 /* MMC exists, get CSD too */
621 resp = mmc_cmd(MMC_CMD_SET_RELATIVE_ADDR, 0, 0, MMC_CMDAT_R1);
622 if (IF_TYPE_SD == mmc_dev.if_type)
623 rca = ((resp[0] & 0xffff0000) >> 16);
624 resp = mmc_cmd(MMC_CMD_SEND_CSD, rca, 0, MMC_CMDAT_R2);
626 mmc_decode_csd(resp);
631 mmc_decode_cid(cid_resp);
634 MMC_CLKRT = 0; /* 20 MHz */
635 resp = mmc_cmd(MMC_CMD_SELECT_CARD, rca, 0, MMC_CMDAT_R1);
638 if (IF_TYPE_SD == mmc_dev.if_type) {
639 resp = mmc_cmd(MMC_CMD_APP_CMD, rca, 0, MMC_CMDAT_R1);
640 resp = mmc_cmd(SD_CMD_APP_SET_BUS_WIDTH, 0, 2, MMC_CMDAT_R1);
641 wide = MMC_CMDAT_SD_4DAT;
645 fat_register_device(&mmc_dev, 1); /* partitions start counting with 1 */
650 #endif /* CONFIG_MMC */