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>
34 extern int fat_register_device(block_dev_desc_t
* dev_desc
, int part_no
);
36 static block_dev_desc_t mmc_dev
;
38 block_dev_desc_t
*mmc_get_dev(int dev
)
40 return ((block_dev_desc_t
*) & mmc_dev
);
44 * FIXME needs to read cid and csd info to determine block size
45 * and other parameters
47 static uchar mmc_buf
[MMC_BLOCK_SIZE
];
48 static uchar spec_ver
;
49 static int mmc_ready
= 0;
53 /****************************************************/
54 mmc_cmd(ushort cmd
, ushort argh
, ushort argl
, ushort cmdat
)
55 /****************************************************/
57 static uint32_t resp
[4], a
, b
, c
;
61 debug("mmc_cmd %u 0x%04x 0x%04x 0x%04x\n", cmd
, argh
, argl
,
63 writel(MMC_STRPCL_STOP_CLK
, MMC_STRPCL
);
64 writel(~MMC_I_MASK_CLK_IS_OFF
, MMC_I_MASK
);
65 while (!(readl(MMC_I_REG
) & MMC_I_REG_CLK_IS_OFF
))
68 writel(argh
, MMC_ARGH
);
69 writel(argl
, MMC_ARGL
);
70 writel(cmdat
| wide
, MMC_CMDAT
);
71 writel(~MMC_I_MASK_END_CMD_RES
, MMC_I_MASK
);
72 writel(MMC_STRPCL_START_CLK
, MMC_STRPCL
);
73 while (!(readl(MMC_I_REG
) & MMC_I_REG_END_CMD_RES
))
76 status
= readl(MMC_STAT
);
77 debug("MMC status 0x%08x\n", status
);
78 if (status
& MMC_STAT_TIME_OUT_RESPONSE
) {
83 * Did I mention this is Sick. We always need to
84 * discard the upper 8 bits of the first 16-bit word.
86 a
= (readl(MMC_RES
) & 0xffff);
87 for (i
= 0; i
< 4; i
++) {
88 b
= (readl(MMC_RES
) & 0xffff);
89 c
= (readl(MMC_RES
) & 0xffff);
90 resp
[i
] = (a
<< 24) | (b
<< 8) | (c
>> 8);
92 debug("MMC resp[%d] = %#08x\n", i
, resp
[i
]);
99 /****************************************************/
100 mmc_block_read(uchar
* dst
, ulong src
, ulong len
)
101 /****************************************************/
110 debug("mmc_block_rd dst %lx src %lx len %d\n", (ulong
) dst
, src
, len
);
116 mmc_cmd(MMC_CMD_SET_BLOCKLEN
, argh
, argl
, MMC_CMDAT_R1
);
118 /* send read command */
121 writel(MMC_STRPCL_STOP_CLK
, MMC_STRPCL
);
122 writel(0xffff, MMC_RDTO
);
124 writel(len
, MMC_BLKLEN
);
125 mmc_cmd(MMC_CMD_READ_SINGLE_BLOCK
, argh
, argl
,
126 MMC_CMDAT_R1
| MMC_CMDAT_READ
| MMC_CMDAT_BLOCK
|
129 writel(~MMC_I_MASK_RXFIFO_RD_REQ
, MMC_I_MASK
);
131 if (readl(MMC_I_REG
) & MMC_I_REG_RXFIFO_RD_REQ
) {
132 #if defined(CONFIG_PXA27X) || defined(CONFIG_CPU_MONAHANS)
134 for (i
= min(len
, 32); i
; i
--) {
135 *dst
++ = readb(MMC_RXFIFO
);
139 *dst
++ = readb(MMC_RXFIFO
);
143 status
= readl(MMC_STAT
);
144 if (status
& MMC_STAT_ERRORS
) {
145 printf("MMC_STAT error %lx\n", status
);
149 writel(~MMC_I_MASK_DATA_TRAN_DONE
, MMC_I_MASK
);
150 while (!(readl(MMC_I_REG
) & MMC_I_REG_DATA_TRAN_DONE
))
152 status
= readl(MMC_STAT
);
153 if (status
& MMC_STAT_ERRORS
) {
154 printf("MMC_STAT error %lx\n", status
);
161 /****************************************************/
162 mmc_block_write(ulong dst
, uchar
* src
, int len
)
163 /****************************************************/
172 debug("mmc_block_wr dst %lx src %lx len %d\n", dst
, (ulong
) src
, len
);
178 mmc_cmd(MMC_CMD_SET_BLOCKLEN
, argh
, argl
, MMC_CMDAT_R1
);
180 /* send write command */
183 writel(MMC_STRPCL_STOP_CLK
, MMC_STRPCL
);
185 writel(len
, MMC_BLKLEN
);
186 mmc_cmd(MMC_CMD_WRITE_SINGLE_BLOCK
, argh
, argl
,
187 MMC_CMDAT_R1
| MMC_CMDAT_WRITE
| MMC_CMDAT_BLOCK
|
190 writel(~MMC_I_MASK_TXFIFO_WR_REQ
, MMC_I_MASK
);
192 if (readl(MMC_I_REG
) & MMC_I_REG_TXFIFO_WR_REQ
) {
193 int i
, bytes
= min(32, len
);
195 for (i
= 0; i
< bytes
; i
++) {
196 writel(*src
++, MMC_TXFIFO
);
199 writel(MMC_PRTBUF_BUF_PART_FULL
, MMC_PRTBUF
);
203 status
= readl(MMC_STAT
);
204 if (status
& MMC_STAT_ERRORS
) {
205 printf("MMC_STAT error %lx\n", status
);
209 writel(~MMC_I_MASK_DATA_TRAN_DONE
, MMC_I_MASK
);
210 while (!(readl(MMC_I_REG
) & MMC_I_REG_DATA_TRAN_DONE
))
212 writel(~MMC_I_MASK_PRG_DONE
, MMC_I_MASK
);
213 while (!(readl(MMC_I_REG
) & MMC_I_REG_PRG_DONE
))
215 status
= readl(MMC_STAT
);
216 if (status
& MMC_STAT_ERRORS
) {
217 printf("MMC_STAT error %lx\n", status
);
224 /****************************************************/
225 pxa_mmc_read(long src
, uchar
* dst
, int size
)
226 /****************************************************/
228 ulong end
, part_start
, part_end
, part_len
, aligned_start
, aligned_end
;
229 ulong mmc_block_size
, mmc_block_address
;
236 printf("Please initial the MMC first\n");
240 mmc_block_size
= MMC_BLOCK_SIZE
;
241 mmc_block_address
= ~(mmc_block_size
- 1);
243 src
-= CONFIG_SYS_MMC_BASE
;
245 part_start
= ~mmc_block_address
& src
;
246 part_end
= ~mmc_block_address
& end
;
247 aligned_start
= mmc_block_address
& src
;
248 aligned_end
= mmc_block_address
& end
;
250 /* all block aligned accesses */
252 ("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
,
256 part_len
= mmc_block_size
- part_start
;
258 ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
259 src
, (ulong
) dst
, end
, part_start
, part_end
, aligned_start
,
261 if ((mmc_block_read(mmc_buf
, aligned_start
, mmc_block_size
)) <
265 memcpy(dst
, mmc_buf
+ part_start
, part_len
);
270 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
271 src
, (ulong
) dst
, end
, part_start
, part_end
, aligned_start
,
273 for (; src
< aligned_end
; src
+= mmc_block_size
, dst
+= mmc_block_size
) {
275 ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
276 src
, (ulong
) dst
, end
, part_start
, part_end
, aligned_start
,
278 if ((mmc_block_read((uchar
*) (dst
), src
, mmc_block_size
)) < 0) {
283 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
284 src
, (ulong
) dst
, end
, part_start
, part_end
, aligned_start
,
286 if (part_end
&& src
< end
) {
288 ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
289 src
, (ulong
) dst
, end
, part_start
, part_end
, aligned_start
,
291 if ((mmc_block_read(mmc_buf
, aligned_end
, mmc_block_size
)) < 0) {
294 memcpy(dst
, mmc_buf
, part_end
);
300 /****************************************************/
301 pxa_mmc_write(uchar
* src
, ulong dst
, int size
)
302 /****************************************************/
304 ulong end
, part_start
, part_end
, part_len
, aligned_start
, aligned_end
;
305 ulong mmc_block_size
, mmc_block_address
;
312 printf("Please initial the MMC first\n");
316 mmc_block_size
= MMC_BLOCK_SIZE
;
317 mmc_block_address
= ~(mmc_block_size
- 1);
319 dst
-= CONFIG_SYS_MMC_BASE
;
321 part_start
= ~mmc_block_address
& dst
;
322 part_end
= ~mmc_block_address
& end
;
323 aligned_start
= mmc_block_address
& dst
;
324 aligned_end
= mmc_block_address
& end
;
326 /* all block aligned accesses */
328 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
329 src
, (ulong
) dst
, end
, part_start
, part_end
, aligned_start
,
332 part_len
= mmc_block_size
- part_start
;
334 ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
335 (ulong
) src
, dst
, end
, part_start
, part_end
, aligned_start
,
337 if ((mmc_block_read(mmc_buf
, aligned_start
, mmc_block_size
)) <
341 memcpy(mmc_buf
+ part_start
, src
, part_len
);
342 if ((mmc_block_write(aligned_start
, mmc_buf
, mmc_block_size
)) <
350 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
351 src
, (ulong
) dst
, end
, part_start
, part_end
, aligned_start
,
353 for (; dst
< aligned_end
; src
+= mmc_block_size
, dst
+= mmc_block_size
) {
355 ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
356 src
, (ulong
) dst
, end
, part_start
, part_end
, aligned_start
,
358 if ((mmc_block_write(dst
, (uchar
*) src
, mmc_block_size
)) < 0) {
363 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
364 src
, (ulong
) dst
, end
, part_start
, part_end
, aligned_start
,
366 if (part_end
&& dst
< end
) {
368 ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
369 src
, (ulong
) dst
, end
, part_start
, part_end
, aligned_start
,
371 if ((mmc_block_read(mmc_buf
, aligned_end
, mmc_block_size
)) < 0) {
374 memcpy(mmc_buf
, src
, part_end
);
375 if ((mmc_block_write(aligned_end
, mmc_buf
, mmc_block_size
)) < 0) {
383 /****************************************************/
384 mmc_bread(int dev_num
, ulong blknr
, lbaint_t blkcnt
, void *dst
)
385 /****************************************************/
387 int mmc_block_size
= MMC_BLOCK_SIZE
;
388 ulong src
= blknr
* mmc_block_size
+ CONFIG_SYS_MMC_BASE
;
390 pxa_mmc_read(src
, (uchar
*) dst
, blkcnt
* mmc_block_size
);
395 #define likely(x) __builtin_expect(!!(x), 1)
396 #define unlikely(x) __builtin_expect(!!(x), 0)
398 #define likely(x) (x)
399 #define unlikely(x) (x)
402 #define UNSTUFF_BITS(resp,start,size) \
404 const int __size = size; \
405 const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1; \
406 const int32_t __off = 3 - ((start) / 32); \
407 const int32_t __shft = (start) & 31; \
410 __res = resp[__off] >> __shft; \
411 if (__size + __shft > 32) \
412 __res |= resp[__off-1] << ((32 - __shft) % 32); \
417 * Given the decoded CSD structure, decode the raw CID to our CID structure.
419 static void mmc_decode_cid(uint32_t * resp
)
421 if (IF_TYPE_SD
== mmc_dev
.if_type
) {
423 * SD doesn't currently have a version field so we will
424 * have to assume we can parse this.
426 sprintf((char *)mmc_dev
.vendor
,
427 "Man %02x OEM %c%c \"%c%c%c%c%c\" Date %02u/%04u",
428 UNSTUFF_BITS(resp
, 120, 8), UNSTUFF_BITS(resp
, 112, 8),
429 UNSTUFF_BITS(resp
, 104, 8), UNSTUFF_BITS(resp
, 96, 8),
430 UNSTUFF_BITS(resp
, 88, 8), UNSTUFF_BITS(resp
, 80, 8),
431 UNSTUFF_BITS(resp
, 72, 8), UNSTUFF_BITS(resp
, 64, 8),
432 UNSTUFF_BITS(resp
, 8, 4), UNSTUFF_BITS(resp
, 12,
434 sprintf((char *)mmc_dev
.revision
, "%d.%d",
435 UNSTUFF_BITS(resp
, 60, 4), UNSTUFF_BITS(resp
, 56, 4));
436 sprintf((char *)mmc_dev
.product
, "%u",
437 UNSTUFF_BITS(resp
, 24, 32));
440 * The selection of the format here is based upon published
441 * specs from sandisk and from what people have reported.
444 case 0: /* MMC v1.0 - v1.2 */
445 case 1: /* MMC v1.4 */
446 sprintf((char *)mmc_dev
.vendor
,
447 "Man %02x%02x%02x \"%c%c%c%c%c%c%c\" Date %02u/%04u",
448 UNSTUFF_BITS(resp
, 120, 8), UNSTUFF_BITS(resp
,
451 UNSTUFF_BITS(resp
, 104, 8), UNSTUFF_BITS(resp
,
453 UNSTUFF_BITS(resp
, 88, 8), UNSTUFF_BITS(resp
,
455 UNSTUFF_BITS(resp
, 72, 8), UNSTUFF_BITS(resp
,
457 UNSTUFF_BITS(resp
, 56, 8), UNSTUFF_BITS(resp
,
459 UNSTUFF_BITS(resp
, 12, 4), UNSTUFF_BITS(resp
, 8,
462 sprintf((char *)mmc_dev
.revision
, "%d.%d",
463 UNSTUFF_BITS(resp
, 44, 4), UNSTUFF_BITS(resp
,
465 sprintf((char *)mmc_dev
.product
, "%u",
466 UNSTUFF_BITS(resp
, 16, 24));
469 case 2: /* MMC v2.0 - v2.2 */
470 case 3: /* MMC v3.1 - v3.3 */
472 sprintf((char *)mmc_dev
.vendor
,
473 "Man %02x OEM %04x \"%c%c%c%c%c%c\" Date %02u/%04u",
474 UNSTUFF_BITS(resp
, 120, 8), UNSTUFF_BITS(resp
,
477 UNSTUFF_BITS(resp
, 96, 8), UNSTUFF_BITS(resp
,
479 UNSTUFF_BITS(resp
, 80, 8), UNSTUFF_BITS(resp
,
481 UNSTUFF_BITS(resp
, 64, 8), UNSTUFF_BITS(resp
,
483 UNSTUFF_BITS(resp
, 12, 4), UNSTUFF_BITS(resp
, 8,
486 sprintf((char *)mmc_dev
.product
, "%u",
487 UNSTUFF_BITS(resp
, 16, 32));
488 sprintf((char *)mmc_dev
.revision
, "N/A");
492 printf("MMC card has unknown MMCA version %d\n",
497 printf("%s card.\nVendor: %s\nProduct: %s\nRevision: %s\n",
498 (IF_TYPE_SD
== mmc_dev
.if_type
) ? "SD" : "MMC", mmc_dev
.vendor
,
499 mmc_dev
.product
, mmc_dev
.revision
);
503 * Given a 128-bit response, decode to our card CSD structure.
505 static void mmc_decode_csd(uint32_t * resp
)
507 unsigned int mult
, csd_struct
;
509 if (IF_TYPE_SD
== mmc_dev
.if_type
) {
510 csd_struct
= UNSTUFF_BITS(resp
, 126, 2);
511 if (csd_struct
!= 0) {
512 printf("SD: unrecognised CSD structure version %d\n",
518 * We only understand CSD structure v1.1 and v1.2.
519 * v1.2 has extra information in bits 15, 11 and 10.
521 csd_struct
= UNSTUFF_BITS(resp
, 126, 2);
522 if (csd_struct
!= 1 && csd_struct
!= 2) {
523 printf("MMC: unrecognised CSD structure version %d\n",
528 spec_ver
= UNSTUFF_BITS(resp
, 122, 4);
529 mmc_dev
.if_type
= IF_TYPE_MMC
;
532 mult
= 1 << (UNSTUFF_BITS(resp
, 47, 3) + 2);
533 mmc_dev
.lba
= (1 + UNSTUFF_BITS(resp
, 62, 12)) * mult
;
534 mmc_dev
.blksz
= 1 << UNSTUFF_BITS(resp
, 80, 4);
536 /* FIXME: The following just makes assumes that's the partition type -- should really read it */
537 mmc_dev
.part_type
= PART_TYPE_DOS
;
540 mmc_dev
.type
= DEV_TYPE_HARDDISK
;
541 mmc_dev
.removable
= 0;
542 mmc_dev
.block_read
= mmc_bread
;
544 printf("Detected: %lu blocks of %lu bytes (%luMB) ",
547 mmc_dev
.lba
* mmc_dev
.blksz
/ (1024 * 1024));
551 /****************************************************/
552 mmc_legacy_init(int verbose
)
553 /****************************************************/
555 int retries
, rc
= -ENODEV
;
556 uint32_t cid_resp
[4];
560 /* Reset device interface type */
561 mmc_dev
.if_type
= IF_TYPE_UNKNOWN
;
563 #if defined (CONFIG_LUBBOCK) || (defined (CONFIG_GUMSTIX) && !defined(CONFIG_PXA27X))
564 set_GPIO_mode(GPIO6_MMCCLK_MD
);
565 set_GPIO_mode(GPIO8_MMCCS0_MD
);
567 #ifdef CONFIG_CPU_MONAHANS /* pxa3xx */
568 writel(readl(CKENA
) | CKENA_12_MMC0
| CKENA_13_MMC1
, CKENA
);
570 writel(readl(CKEN
) | CKEN12_MMC
, CKEN
); /* enable MMC unit clock */
572 writel(MMC_CLKRT_0_3125MHZ
, MMC_CLKRT
);
573 writel(MMC_RES_TO_MAX
, MMC_RESTO
);
574 writel(MMC_SPI_DISABLE
, MMC_SPI
);
577 mmc_cmd(MMC_CMD_GO_IDLE_STATE
, 0, 0, MMC_CMDAT_INIT
| MMC_CMDAT_R0
);
581 resp
= mmc_cmd(MMC_CMD_APP_CMD
, 0, 0, MMC_CMDAT_R1
);
582 if (!(resp
[0] & 0x00000020)) { /* Card does not support APP_CMD */
583 debug("Card does not support APP_CMD\n");
587 /* Select 3.2-3.3V and 3.3-3.4V */
588 resp
= mmc_cmd(SD_CMD_APP_SEND_OP_COND
, 0x0030, 0x0000,
589 MMC_CMDAT_R3
| (retries
< 2 ? 0
591 if (resp
[0] & 0x80000000) {
592 mmc_dev
.if_type
= IF_TYPE_SD
;
593 debug("Detected SD card\n");
599 if (retries
<= 0 || !(IF_TYPE_SD
== mmc_dev
.if_type
)) {
600 debug("Failed to detect SD Card, trying MMC\n");
602 mmc_cmd(MMC_CMD_SEND_OP_COND
, 0x00ff, 0x8000, MMC_CMDAT_R3
);
605 while (retries
-- && resp
&& !(resp
[0] & 0x80000000)) {
608 mmc_cmd(MMC_CMD_SEND_OP_COND
, 0x00ff, 0x8000,
613 /* try to get card id */
615 mmc_cmd(MMC_CMD_ALL_SEND_CID
, 0, 0, MMC_CMDAT_R2
| MMC_CMDAT_BUSY
);
617 memcpy(cid_resp
, resp
, sizeof(cid_resp
));
619 /* MMC exists, get CSD too */
620 resp
= mmc_cmd(MMC_CMD_SET_RELATIVE_ADDR
, 0, 0, MMC_CMDAT_R1
);
621 if (IF_TYPE_SD
== mmc_dev
.if_type
)
622 rca
= ((resp
[0] & 0xffff0000) >> 16);
623 resp
= mmc_cmd(MMC_CMD_SEND_CSD
, rca
, 0, MMC_CMDAT_R2
);
625 mmc_decode_csd(resp
);
630 mmc_decode_cid(cid_resp
);
633 writel(0, MMC_CLKRT
); /* 20 MHz */
634 resp
= mmc_cmd(MMC_CMD_SELECT_CARD
, rca
, 0, MMC_CMDAT_R1
);
636 #if defined(CONFIG_PXA27X) || defined(CONFIG_CPU_MONAHANS)
637 if (IF_TYPE_SD
== mmc_dev
.if_type
) {
638 resp
= mmc_cmd(MMC_CMD_APP_CMD
, rca
, 0, MMC_CMDAT_R1
);
639 resp
= mmc_cmd(SD_CMD_APP_SET_BUS_WIDTH
, 0, 2, MMC_CMDAT_R1
);
640 wide
= MMC_CMDAT_SD_4DAT
;
644 fat_register_device(&mmc_dev
, 1); /* partitions start counting with 1 */