Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2006 Atmel Corporation |
| 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | #include <common.h> |
| 23 | |
| 24 | #ifdef CONFIG_MMC |
| 25 | |
| 26 | #include <part.h> |
| 27 | #include <mmc.h> |
| 28 | |
| 29 | #include <asm/io.h> |
| 30 | #include <asm/errno.h> |
| 31 | #include <asm/byteorder.h> |
| 32 | #include <asm/arch/clk.h> |
| 33 | #include <asm/arch/memory-map.h> |
| 34 | |
| 35 | #include "atmel_mci.h" |
| 36 | |
| 37 | #ifdef DEBUG |
| 38 | #define pr_debug(fmt, args...) printf(fmt, ##args) |
| 39 | #else |
| 40 | #define pr_debug(...) do { } while(0) |
| 41 | #endif |
| 42 | |
| 43 | #ifndef CFG_MMC_CLK_OD |
| 44 | #define CFG_MMC_CLK_OD 150000 |
| 45 | #endif |
| 46 | |
| 47 | #ifndef CFG_MMC_CLK_PP |
| 48 | #define CFG_MMC_CLK_PP 5000000 |
| 49 | #endif |
| 50 | |
| 51 | #ifndef CFG_MMC_OP_COND |
| 52 | #define CFG_MMC_OP_COND 0x00100000 |
| 53 | #endif |
| 54 | |
| 55 | #define MMC_DEFAULT_BLKLEN 512 |
| 56 | #define MMC_DEFAULT_RCA 1 |
| 57 | |
| 58 | static unsigned int mmc_rca; |
Haavard Skinnemoen | a084583 | 2007-06-29 18:38:51 +0200 | [diff] [blame] | 59 | static int mmc_card_is_sd; |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 60 | static block_dev_desc_t mmc_blkdev; |
| 61 | |
| 62 | block_dev_desc_t *mmc_get_dev(int dev) |
| 63 | { |
| 64 | return &mmc_blkdev; |
| 65 | } |
| 66 | |
| 67 | static void mci_set_mode(unsigned long hz, unsigned long blklen) |
| 68 | { |
| 69 | unsigned long bus_hz; |
| 70 | unsigned long clkdiv; |
| 71 | |
| 72 | bus_hz = get_mci_clk_rate(); |
| 73 | clkdiv = (bus_hz / hz) / 2 - 1; |
| 74 | |
| 75 | pr_debug("mmc: setting clock %lu Hz, block size %lu\n", |
| 76 | hz, blklen); |
| 77 | |
| 78 | if (clkdiv & ~255UL) { |
| 79 | clkdiv = 255; |
| 80 | printf("mmc: clock %lu too low; setting CLKDIV to 255\n", |
| 81 | hz); |
| 82 | } |
| 83 | |
| 84 | blklen &= 0xfffc; |
| 85 | mmci_writel(MR, (MMCI_BF(CLKDIV, clkdiv) |
Haavard Skinnemoen | f0d1246 | 2007-06-27 13:34:26 +0200 | [diff] [blame] | 86 | | MMCI_BF(BLKLEN, blklen) |
| 87 | | MMCI_BIT(RDPROOF) |
| 88 | | MMCI_BIT(WRPROOF))); |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | #define RESP_NO_CRC 1 |
| 92 | #define R1 MMCI_BF(RSPTYP, 1) |
| 93 | #define R2 MMCI_BF(RSPTYP, 2) |
| 94 | #define R3 (R1 | RESP_NO_CRC) |
| 95 | #define R6 R1 |
| 96 | #define NID MMCI_BF(MAXLAT, 0) |
| 97 | #define NCR MMCI_BF(MAXLAT, 1) |
| 98 | #define TRCMD_START MMCI_BF(TRCMD, 1) |
| 99 | #define TRDIR_READ MMCI_BF(TRDIR, 1) |
| 100 | #define TRTYP_BLOCK MMCI_BF(TRTYP, 0) |
| 101 | #define INIT_CMD MMCI_BF(SPCMD, 1) |
| 102 | #define OPEN_DRAIN MMCI_BF(OPDCMD, 1) |
| 103 | |
| 104 | #define ERROR_FLAGS (MMCI_BIT(DTOE) \ |
| 105 | | MMCI_BIT(RDIRE) \ |
| 106 | | MMCI_BIT(RENDE) \ |
| 107 | | MMCI_BIT(RINDE) \ |
| 108 | | MMCI_BIT(RTOE)) |
| 109 | |
| 110 | static int |
| 111 | mmc_cmd(unsigned long cmd, unsigned long arg, |
| 112 | void *resp, unsigned long flags) |
| 113 | { |
| 114 | unsigned long *response = resp; |
| 115 | int i, response_words = 0; |
| 116 | unsigned long error_flags; |
| 117 | u32 status; |
| 118 | |
| 119 | pr_debug("mmc: CMD%lu 0x%lx (flags 0x%lx)\n", |
| 120 | cmd, arg, flags); |
| 121 | |
| 122 | error_flags = ERROR_FLAGS; |
| 123 | if (!(flags & RESP_NO_CRC)) |
| 124 | error_flags |= MMCI_BIT(RCRCE); |
| 125 | |
| 126 | flags &= ~MMCI_BF(CMDNB, ~0UL); |
| 127 | |
| 128 | if (MMCI_BFEXT(RSPTYP, flags) == MMCI_RSPTYP_48_BIT_RESP) |
| 129 | response_words = 1; |
| 130 | else if (MMCI_BFEXT(RSPTYP, flags) == MMCI_RSPTYP_136_BIT_RESP) |
| 131 | response_words = 4; |
| 132 | |
| 133 | mmci_writel(ARGR, arg); |
| 134 | mmci_writel(CMDR, cmd | flags); |
| 135 | do { |
| 136 | udelay(40); |
| 137 | status = mmci_readl(SR); |
| 138 | } while (!(status & MMCI_BIT(CMDRDY))); |
| 139 | |
| 140 | pr_debug("mmc: status 0x%08lx\n", status); |
| 141 | |
Haavard Skinnemoen | e92a5bf | 2008-05-22 12:28:25 +0200 | [diff] [blame^] | 142 | if (status & error_flags) { |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 143 | printf("mmc: command %lu failed (status: 0x%08lx)\n", |
| 144 | cmd, status); |
| 145 | return -EIO; |
| 146 | } |
| 147 | |
| 148 | if (response_words) |
| 149 | pr_debug("mmc: response:"); |
| 150 | |
| 151 | for (i = 0; i < response_words; i++) { |
| 152 | response[i] = mmci_readl(RSPR); |
| 153 | pr_debug(" %08lx", response[i]); |
| 154 | } |
| 155 | pr_debug("\n"); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int mmc_acmd(unsigned long cmd, unsigned long arg, |
| 161 | void *resp, unsigned long flags) |
| 162 | { |
| 163 | unsigned long aresp[4]; |
| 164 | int ret; |
| 165 | |
| 166 | /* |
| 167 | * Seems like the APP_CMD part of an ACMD has 64 cycles max |
| 168 | * latency even though the ACMD part doesn't. This isn't |
| 169 | * entirely clear in the SD Card spec, but some cards refuse |
| 170 | * to work if we attempt to use 5 cycles max latency here... |
| 171 | */ |
| 172 | ret = mmc_cmd(MMC_CMD_APP_CMD, 0, aresp, |
| 173 | R1 | NCR | (flags & OPEN_DRAIN)); |
| 174 | if (ret) |
| 175 | return ret; |
| 176 | if ((aresp[0] & (R1_ILLEGAL_COMMAND | R1_APP_CMD)) != R1_APP_CMD) |
| 177 | return -ENODEV; |
| 178 | |
| 179 | ret = mmc_cmd(cmd, arg, resp, flags); |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static unsigned long |
| 184 | mmc_bread(int dev, unsigned long start, lbaint_t blkcnt, |
Haavard Skinnemoen | 7a96dda | 2008-05-21 11:10:59 +0200 | [diff] [blame] | 185 | void *buffer) |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 186 | { |
| 187 | int ret, i = 0; |
| 188 | unsigned long resp[4]; |
| 189 | unsigned long card_status, data; |
| 190 | unsigned long wordcount; |
Haavard Skinnemoen | 7a96dda | 2008-05-21 11:10:59 +0200 | [diff] [blame] | 191 | u32 *p = buffer; |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 192 | u32 status; |
| 193 | |
| 194 | if (blkcnt == 0) |
| 195 | return 0; |
| 196 | |
| 197 | pr_debug("mmc_bread: dev %d, start %lx, blkcnt %lx\n", |
| 198 | dev, start, blkcnt); |
| 199 | |
| 200 | /* Put the device into Transfer state */ |
| 201 | ret = mmc_cmd(MMC_CMD_SELECT_CARD, mmc_rca << 16, resp, R1 | NCR); |
Haavard Skinnemoen | 4d5fa99 | 2007-06-29 18:22:34 +0200 | [diff] [blame] | 202 | if (ret) goto out; |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 203 | |
| 204 | /* Set block length */ |
| 205 | ret = mmc_cmd(MMC_CMD_SET_BLOCKLEN, mmc_blkdev.blksz, resp, R1 | NCR); |
Haavard Skinnemoen | 4d5fa99 | 2007-06-29 18:22:34 +0200 | [diff] [blame] | 206 | if (ret) goto out; |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 207 | |
| 208 | pr_debug("MCI_DTOR = %08lx\n", mmci_readl(DTOR)); |
| 209 | |
| 210 | for (i = 0; i < blkcnt; i++, start++) { |
| 211 | ret = mmc_cmd(MMC_CMD_READ_SINGLE_BLOCK, |
| 212 | start * mmc_blkdev.blksz, resp, |
| 213 | (R1 | NCR | TRCMD_START | TRDIR_READ |
| 214 | | TRTYP_BLOCK)); |
Haavard Skinnemoen | 4d5fa99 | 2007-06-29 18:22:34 +0200 | [diff] [blame] | 215 | if (ret) goto out; |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 216 | |
| 217 | ret = -EIO; |
| 218 | wordcount = 0; |
| 219 | do { |
| 220 | do { |
| 221 | status = mmci_readl(SR); |
| 222 | if (status & (ERROR_FLAGS | MMCI_BIT(OVRE))) |
Haavard Skinnemoen | 4d5fa99 | 2007-06-29 18:22:34 +0200 | [diff] [blame] | 223 | goto read_error; |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 224 | } while (!(status & MMCI_BIT(RXRDY))); |
| 225 | |
| 226 | if (status & MMCI_BIT(RXRDY)) { |
| 227 | data = mmci_readl(RDR); |
Wolfgang Denk | b99c1e6 | 2007-04-18 16:53:52 +0200 | [diff] [blame] | 228 | /* pr_debug("%x\n", data); */ |
Haavard Skinnemoen | 7a96dda | 2008-05-21 11:10:59 +0200 | [diff] [blame] | 229 | *p++ = data; |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 230 | wordcount++; |
| 231 | } |
Haavard Skinnemoen | f0d1246 | 2007-06-27 13:34:26 +0200 | [diff] [blame] | 232 | } while(wordcount < (mmc_blkdev.blksz / 4)); |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 233 | |
| 234 | pr_debug("mmc: read %u words, waiting for BLKE\n", wordcount); |
| 235 | |
| 236 | do { |
| 237 | status = mmci_readl(SR); |
| 238 | } while (!(status & MMCI_BIT(BLKE))); |
| 239 | |
| 240 | putc('.'); |
| 241 | } |
| 242 | |
| 243 | out: |
| 244 | /* Put the device back into Standby state */ |
| 245 | mmc_cmd(MMC_CMD_SELECT_CARD, 0, resp, NCR); |
| 246 | return i; |
| 247 | |
Haavard Skinnemoen | 4d5fa99 | 2007-06-29 18:22:34 +0200 | [diff] [blame] | 248 | read_error: |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 249 | mmc_cmd(MMC_CMD_SEND_STATUS, mmc_rca << 16, &card_status, R1 | NCR); |
Haavard Skinnemoen | 4d5fa99 | 2007-06-29 18:22:34 +0200 | [diff] [blame] | 250 | printf("mmc: bread failed, status = %08x, card status = %08x\n", |
| 251 | status, card_status); |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 252 | goto out; |
| 253 | } |
| 254 | |
| 255 | static void mmc_parse_cid(struct mmc_cid *cid, unsigned long *resp) |
| 256 | { |
| 257 | cid->mid = resp[0] >> 24; |
| 258 | cid->oid = (resp[0] >> 8) & 0xffff; |
| 259 | cid->pnm[0] = resp[0]; |
| 260 | cid->pnm[1] = resp[1] >> 24; |
| 261 | cid->pnm[2] = resp[1] >> 16; |
| 262 | cid->pnm[3] = resp[1] >> 8; |
| 263 | cid->pnm[4] = resp[1]; |
| 264 | cid->pnm[5] = resp[2] >> 24; |
| 265 | cid->pnm[6] = 0; |
| 266 | cid->prv = resp[2] >> 16; |
| 267 | cid->psn = (resp[2] << 16) | (resp[3] >> 16); |
| 268 | cid->mdt = resp[3] >> 8; |
| 269 | } |
| 270 | |
| 271 | static void sd_parse_cid(struct mmc_cid *cid, unsigned long *resp) |
| 272 | { |
| 273 | cid->mid = resp[0] >> 24; |
| 274 | cid->oid = (resp[0] >> 8) & 0xffff; |
| 275 | cid->pnm[0] = resp[0]; |
| 276 | cid->pnm[1] = resp[1] >> 24; |
| 277 | cid->pnm[2] = resp[1] >> 16; |
| 278 | cid->pnm[3] = resp[1] >> 8; |
| 279 | cid->pnm[4] = resp[1]; |
| 280 | cid->pnm[5] = 0; |
| 281 | cid->pnm[6] = 0; |
| 282 | cid->prv = resp[2] >> 24; |
| 283 | cid->psn = (resp[2] << 8) | (resp[3] >> 24); |
| 284 | cid->mdt = (resp[3] >> 8) & 0x0fff; |
| 285 | } |
| 286 | |
| 287 | static void mmc_dump_cid(const struct mmc_cid *cid) |
| 288 | { |
| 289 | printf("Manufacturer ID: %02lX\n", cid->mid); |
| 290 | printf("OEM/Application ID: %04lX\n", cid->oid); |
| 291 | printf("Product name: %s\n", cid->pnm); |
| 292 | printf("Product Revision: %lu.%lu\n", |
| 293 | cid->prv >> 4, cid->prv & 0x0f); |
| 294 | printf("Product Serial Number: %lu\n", cid->psn); |
| 295 | printf("Manufacturing Date: %02lu/%02lu\n", |
| 296 | cid->mdt >> 4, cid->mdt & 0x0f); |
| 297 | } |
| 298 | |
| 299 | static void mmc_dump_csd(const struct mmc_csd *csd) |
| 300 | { |
| 301 | unsigned long *csd_raw = (unsigned long *)csd; |
| 302 | printf("CSD data: %08lx %08lx %08lx %08lx\n", |
| 303 | csd_raw[0], csd_raw[1], csd_raw[2], csd_raw[3]); |
| 304 | printf("CSD structure version: 1.%u\n", csd->csd_structure); |
| 305 | printf("MMC System Spec version: %u\n", csd->spec_vers); |
| 306 | printf("Card command classes: %03x\n", csd->ccc); |
| 307 | printf("Read block length: %u\n", 1 << csd->read_bl_len); |
| 308 | if (csd->read_bl_partial) |
| 309 | puts("Supports partial reads\n"); |
| 310 | else |
| 311 | puts("Does not support partial reads\n"); |
| 312 | printf("Write block length: %u\n", 1 << csd->write_bl_len); |
| 313 | if (csd->write_bl_partial) |
| 314 | puts("Supports partial writes\n"); |
| 315 | else |
| 316 | puts("Does not support partial writes\n"); |
| 317 | if (csd->wp_grp_enable) |
| 318 | printf("Supports group WP: %u\n", csd->wp_grp_size + 1); |
| 319 | else |
| 320 | puts("Does not support group WP\n"); |
| 321 | printf("Card capacity: %u bytes\n", |
| 322 | (csd->c_size + 1) * (1 << (csd->c_size_mult + 2)) * |
| 323 | (1 << csd->read_bl_len)); |
| 324 | printf("File format: %u/%u\n", |
| 325 | csd->file_format_grp, csd->file_format); |
| 326 | puts("Write protection: "); |
| 327 | if (csd->perm_write_protect) |
| 328 | puts(" permanent"); |
| 329 | if (csd->tmp_write_protect) |
| 330 | puts(" temporary"); |
| 331 | putc('\n'); |
| 332 | } |
| 333 | |
| 334 | static int mmc_idle_cards(void) |
| 335 | { |
| 336 | int ret; |
| 337 | |
| 338 | /* Reset and initialize all cards */ |
| 339 | ret = mmc_cmd(MMC_CMD_GO_IDLE_STATE, 0, NULL, 0); |
| 340 | if (ret) |
| 341 | return ret; |
| 342 | |
| 343 | /* Keep the bus idle for 74 clock cycles */ |
| 344 | return mmc_cmd(0, 0, NULL, INIT_CMD); |
| 345 | } |
| 346 | |
| 347 | static int sd_init_card(struct mmc_cid *cid, int verbose) |
| 348 | { |
| 349 | unsigned long resp[4]; |
| 350 | int i, ret = 0; |
| 351 | |
| 352 | mmc_idle_cards(); |
| 353 | for (i = 0; i < 1000; i++) { |
| 354 | ret = mmc_acmd(MMC_ACMD_SD_SEND_OP_COND, CFG_MMC_OP_COND, |
| 355 | resp, R3 | NID); |
| 356 | if (ret || (resp[0] & 0x80000000)) |
| 357 | break; |
| 358 | ret = -ETIMEDOUT; |
| 359 | } |
| 360 | |
| 361 | if (ret) |
| 362 | return ret; |
| 363 | |
| 364 | ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID); |
| 365 | if (ret) |
| 366 | return ret; |
| 367 | sd_parse_cid(cid, resp); |
| 368 | if (verbose) |
| 369 | mmc_dump_cid(cid); |
| 370 | |
| 371 | /* Get RCA of the card that responded */ |
| 372 | ret = mmc_cmd(MMC_CMD_SD_SEND_RELATIVE_ADDR, 0, resp, R6 | NCR); |
| 373 | if (ret) |
| 374 | return ret; |
| 375 | |
| 376 | mmc_rca = resp[0] >> 16; |
| 377 | if (verbose) |
| 378 | printf("SD Card detected (RCA %u)\n", mmc_rca); |
Haavard Skinnemoen | a084583 | 2007-06-29 18:38:51 +0200 | [diff] [blame] | 379 | mmc_card_is_sd = 1; |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static int mmc_init_card(struct mmc_cid *cid, int verbose) |
| 384 | { |
| 385 | unsigned long resp[4]; |
| 386 | int i, ret = 0; |
| 387 | |
| 388 | mmc_idle_cards(); |
| 389 | for (i = 0; i < 1000; i++) { |
| 390 | ret = mmc_cmd(MMC_CMD_SEND_OP_COND, CFG_MMC_OP_COND, resp, |
| 391 | R3 | NID | OPEN_DRAIN); |
| 392 | if (ret || (resp[0] & 0x80000000)) |
| 393 | break; |
| 394 | ret = -ETIMEDOUT; |
| 395 | } |
| 396 | |
| 397 | if (ret) |
| 398 | return ret; |
| 399 | |
| 400 | /* Get CID of all cards. FIXME: Support more than one card */ |
| 401 | ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID | OPEN_DRAIN); |
| 402 | if (ret) |
| 403 | return ret; |
| 404 | mmc_parse_cid(cid, resp); |
| 405 | if (verbose) |
| 406 | mmc_dump_cid(cid); |
| 407 | |
| 408 | /* Set Relative Address of the card that responded */ |
| 409 | ret = mmc_cmd(MMC_CMD_SET_RELATIVE_ADDR, mmc_rca << 16, resp, |
| 410 | R1 | NCR | OPEN_DRAIN); |
| 411 | return ret; |
| 412 | } |
| 413 | |
Haavard Skinnemoen | a084583 | 2007-06-29 18:38:51 +0200 | [diff] [blame] | 414 | static void mci_set_data_timeout(struct mmc_csd *csd) |
| 415 | { |
| 416 | static const unsigned int dtomul_to_shift[] = { |
| 417 | 0, 4, 7, 8, 10, 12, 16, 20, |
| 418 | }; |
| 419 | static const unsigned int taac_exp[] = { |
| 420 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, |
| 421 | }; |
| 422 | static const unsigned int taac_mant[] = { |
| 423 | 0, 10, 12, 13, 15, 60, 25, 30, |
| 424 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 425 | }; |
| 426 | unsigned int timeout_ns, timeout_clks; |
| 427 | unsigned int e, m; |
| 428 | unsigned int dtocyc, dtomul; |
| 429 | unsigned int shift; |
| 430 | u32 dtor; |
| 431 | |
| 432 | e = csd->taac & 0x07; |
| 433 | m = (csd->taac >> 3) & 0x0f; |
| 434 | |
| 435 | timeout_ns = (taac_exp[e] * taac_mant[m] + 9) / 10; |
| 436 | timeout_clks = csd->nsac * 100; |
| 437 | |
| 438 | timeout_clks += (((timeout_ns + 9) / 10) |
| 439 | * ((CFG_MMC_CLK_PP + 99999) / 100000) + 9999) / 10000; |
| 440 | if (!mmc_card_is_sd) |
| 441 | timeout_clks *= 10; |
| 442 | else |
| 443 | timeout_clks *= 100; |
| 444 | |
| 445 | dtocyc = timeout_clks; |
| 446 | dtomul = 0; |
Haavard Skinnemoen | 7a96dda | 2008-05-21 11:10:59 +0200 | [diff] [blame] | 447 | shift = 0; |
Haavard Skinnemoen | a084583 | 2007-06-29 18:38:51 +0200 | [diff] [blame] | 448 | while (dtocyc > 15 && dtomul < 8) { |
| 449 | dtomul++; |
| 450 | shift = dtomul_to_shift[dtomul]; |
| 451 | dtocyc = (timeout_clks + (1 << shift) - 1) >> shift; |
| 452 | } |
| 453 | |
| 454 | if (dtomul >= 8) { |
| 455 | dtomul = 7; |
| 456 | dtocyc = 15; |
| 457 | puts("Warning: Using maximum data timeout\n"); |
| 458 | } |
| 459 | |
| 460 | dtor = (MMCI_BF(DTOMUL, dtomul) |
| 461 | | MMCI_BF(DTOCYC, dtocyc)); |
| 462 | mmci_writel(DTOR, dtor); |
| 463 | |
| 464 | printf("mmc: Using %u cycles data timeout (DTOR=0x%x)\n", |
| 465 | dtocyc << shift, dtor); |
| 466 | } |
| 467 | |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 468 | int mmc_init(int verbose) |
| 469 | { |
| 470 | struct mmc_cid cid; |
| 471 | struct mmc_csd csd; |
Haavard Skinnemoen | f0d1246 | 2007-06-27 13:34:26 +0200 | [diff] [blame] | 472 | unsigned int max_blksz; |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 473 | int ret; |
| 474 | |
| 475 | /* Initialize controller */ |
| 476 | mmci_writel(CR, MMCI_BIT(SWRST)); |
| 477 | mmci_writel(CR, MMCI_BIT(MCIEN)); |
| 478 | mmci_writel(DTOR, 0x5f); |
| 479 | mmci_writel(IDR, ~0UL); |
| 480 | mci_set_mode(CFG_MMC_CLK_OD, MMC_DEFAULT_BLKLEN); |
| 481 | |
Haavard Skinnemoen | a084583 | 2007-06-29 18:38:51 +0200 | [diff] [blame] | 482 | mmc_card_is_sd = 0; |
| 483 | |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 484 | ret = sd_init_card(&cid, verbose); |
| 485 | if (ret) { |
| 486 | mmc_rca = MMC_DEFAULT_RCA; |
| 487 | ret = mmc_init_card(&cid, verbose); |
| 488 | } |
| 489 | if (ret) |
| 490 | return ret; |
| 491 | |
| 492 | /* Get CSD from the card */ |
| 493 | ret = mmc_cmd(MMC_CMD_SEND_CSD, mmc_rca << 16, &csd, R2 | NCR); |
| 494 | if (ret) |
| 495 | return ret; |
| 496 | if (verbose) |
| 497 | mmc_dump_csd(&csd); |
| 498 | |
Haavard Skinnemoen | a084583 | 2007-06-29 18:38:51 +0200 | [diff] [blame] | 499 | mci_set_data_timeout(&csd); |
| 500 | |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 501 | /* Initialize the blockdev structure */ |
| 502 | mmc_blkdev.if_type = IF_TYPE_MMC; |
| 503 | mmc_blkdev.part_type = PART_TYPE_DOS; |
| 504 | mmc_blkdev.block_read = mmc_bread; |
| 505 | sprintf((char *)mmc_blkdev.vendor, |
| 506 | "Man %02x%04x Snr %08x", |
| 507 | cid.mid, cid.oid, cid.psn); |
| 508 | strncpy((char *)mmc_blkdev.product, cid.pnm, |
| 509 | sizeof(mmc_blkdev.product)); |
| 510 | sprintf((char *)mmc_blkdev.revision, "%x %x", |
| 511 | cid.prv >> 4, cid.prv & 0x0f); |
Haavard Skinnemoen | f0d1246 | 2007-06-27 13:34:26 +0200 | [diff] [blame] | 512 | |
| 513 | /* |
| 514 | * If we can't use 512 byte blocks, refuse to deal with the |
| 515 | * card. Tons of code elsewhere seems to depend on this. |
| 516 | */ |
| 517 | max_blksz = 1 << csd.read_bl_len; |
| 518 | if (max_blksz < 512 || (max_blksz > 512 && !csd.read_bl_partial)) { |
| 519 | printf("Card does not support 512 byte reads, aborting.\n"); |
| 520 | return -ENODEV; |
| 521 | } |
| 522 | mmc_blkdev.blksz = 512; |
Haavard Skinnemoen | fc26c97 | 2006-01-20 10:03:53 +0100 | [diff] [blame] | 523 | mmc_blkdev.lba = (csd.c_size + 1) * (1 << (csd.c_size_mult + 2)); |
| 524 | |
| 525 | mci_set_mode(CFG_MMC_CLK_PP, mmc_blkdev.blksz); |
| 526 | |
| 527 | #if 0 |
| 528 | if (fat_register_device(&mmc_blkdev, 1)) |
| 529 | printf("Could not register MMC fat device\n"); |
| 530 | #else |
| 531 | init_part(&mmc_blkdev); |
| 532 | #endif |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | int mmc_read(ulong src, uchar *dst, int size) |
| 538 | { |
| 539 | return -ENOSYS; |
| 540 | } |
| 541 | |
| 542 | int mmc_write(uchar *src, ulong dst, int size) |
| 543 | { |
| 544 | return -ENOSYS; |
| 545 | } |
| 546 | |
| 547 | int mmc2info(ulong addr) |
| 548 | { |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | #endif /* CONFIG_MMC */ |