mmc.c (69f45cd53b8ad8bc3afef2cf2410baf58fe75a6f) | mmc.c (c40fdca6b7db469d3982cc44fd68a269adb41b25) |
---|---|
1/* 2 * Copyright 2008, Freescale Semiconductor, Inc 3 * Andy Fleming 4 * 5 * Based vaguely on the Linux code 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ --- 7 unchanged lines hidden (view full) --- 16#include <mmc.h> 17#include <part.h> 18#include <malloc.h> 19#include <memalign.h> 20#include <linux/list.h> 21#include <div64.h> 22#include "mmc_private.h" 23 | 1/* 2 * Copyright 2008, Freescale Semiconductor, Inc 3 * Andy Fleming 4 * 5 * Based vaguely on the Linux code 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ --- 7 unchanged lines hidden (view full) --- 16#include <mmc.h> 17#include <part.h> 18#include <malloc.h> 19#include <memalign.h> 20#include <linux/list.h> 21#include <div64.h> 22#include "mmc_private.h" 23 |
24static struct list_head mmc_devices; 25static int cur_dev_num = -1; 26 27struct blk_desc *mmc_get_blk_desc(struct mmc *mmc) 28{ 29 return &mmc->block_dev; 30} 31 | |
32__weak int board_mmc_getwp(struct mmc *mmc) 33{ 34 return -1; 35} 36 37int mmc_getwp(struct mmc *mmc) 38{ 39 int wp; --- 138 unchanged lines hidden (view full) --- 178 179 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; 180 cmd.resp_type = MMC_RSP_R1; 181 cmd.cmdarg = len; 182 183 return mmc_send_cmd(mmc, &cmd, NULL); 184} 185 | 24__weak int board_mmc_getwp(struct mmc *mmc) 25{ 26 return -1; 27} 28 29int mmc_getwp(struct mmc *mmc) 30{ 31 int wp; --- 138 unchanged lines hidden (view full) --- 170 171 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; 172 cmd.resp_type = MMC_RSP_R1; 173 cmd.cmdarg = len; 174 175 return mmc_send_cmd(mmc, &cmd, NULL); 176} 177 |
186struct mmc *find_mmc_device(int dev_num) 187{ 188 struct mmc *m; 189 struct list_head *entry; 190 191 list_for_each(entry, &mmc_devices) { 192 m = list_entry(entry, struct mmc, link); 193 194 if (m->block_dev.devnum == dev_num) 195 return m; 196 } 197 198#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 199 printf("MMC Device %d not found\n", dev_num); 200#endif 201 202 return NULL; 203} 204 | |
205static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start, 206 lbaint_t blkcnt) 207{ 208 struct mmc_cmd cmd; 209 struct mmc_data data; 210 211 if (blkcnt > 1) 212 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; --- 43 unchanged lines hidden (view full) --- 256 struct mmc *mmc = find_mmc_device(dev_num); 257 if (!mmc) 258 return 0; 259 260 err = blk_dselect_hwpart(block_dev, block_dev->hwpart); 261 if (err < 0) 262 return 0; 263 | 178static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start, 179 lbaint_t blkcnt) 180{ 181 struct mmc_cmd cmd; 182 struct mmc_data data; 183 184 if (blkcnt > 1) 185 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; --- 43 unchanged lines hidden (view full) --- 229 struct mmc *mmc = find_mmc_device(dev_num); 230 if (!mmc) 231 return 0; 232 233 err = blk_dselect_hwpart(block_dev, block_dev->hwpart); 234 if (err < 0) 235 return 0; 236 |
264 if ((start + blkcnt) > mmc->block_dev.lba) { | 237 if ((start + blkcnt) > block_dev->lba) { |
265#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 266 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", | 238#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 239 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", |
267 start + blkcnt, mmc->block_dev.lba); | 240 start + blkcnt, block_dev->lba); |
268#endif 269 return 0; 270 } 271 272 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) { 273 debug("%s: Failed to set blocklen\n", __func__); 274 return 0; 275 } --- 301 unchanged lines hidden (view full) --- 577 case 6: 578 case 7: 579 mmc->capacity = mmc->capacity_gp[part_num - 4]; 580 break; 581 default: 582 return -1; 583 } 584 | 241#endif 242 return 0; 243 } 244 245 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) { 246 debug("%s: Failed to set blocklen\n", __func__); 247 return 0; 248 } --- 301 unchanged lines hidden (view full) --- 550 case 6: 551 case 7: 552 mmc->capacity = mmc->capacity_gp[part_num - 4]; 553 break; 554 default: 555 return -1; 556 } 557 |
585 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); | 558 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len); |
586 587 return 0; 588} 589 590int mmc_switch_part(int dev_num, unsigned int part_num) 591{ 592 struct mmc *mmc = find_mmc_device(dev_num); 593 int ret; --- 463 unchanged lines hidden (view full) --- 1057 uint mult, freq; 1058 u64 cmult, csize, capacity; 1059 struct mmc_cmd cmd; 1060 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); 1061 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN); 1062 int timeout = 1000; 1063 bool has_parts = false; 1064 bool part_completed; | 559 560 return 0; 561} 562 563int mmc_switch_part(int dev_num, unsigned int part_num) 564{ 565 struct mmc *mmc = find_mmc_device(dev_num); 566 int ret; --- 463 unchanged lines hidden (view full) --- 1030 uint mult, freq; 1031 u64 cmult, csize, capacity; 1032 struct mmc_cmd cmd; 1033 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); 1034 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN); 1035 int timeout = 1000; 1036 bool has_parts = false; 1037 bool part_completed; |
1038 struct blk_desc *bdesc; |
|
1065 1066#ifdef CONFIG_MMC_SPI_CRC_ON 1067 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ 1068 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF; 1069 cmd.resp_type = MMC_RSP_R1; 1070 cmd.cmdarg = 1; 1071 err = mmc_send_cmd(mmc, &cmd, NULL); 1072 --- 280 unchanged lines hidden (view full) --- 1353 1354 mmc->hc_wp_grp_size = 1024 1355 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] 1356 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 1357 1358 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET]; 1359 } 1360 | 1039 1040#ifdef CONFIG_MMC_SPI_CRC_ON 1041 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ 1042 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF; 1043 cmd.resp_type = MMC_RSP_R1; 1044 cmd.cmdarg = 1; 1045 err = mmc_send_cmd(mmc, &cmd, NULL); 1046 --- 280 unchanged lines hidden (view full) --- 1327 1328 mmc->hc_wp_grp_size = 1024 1329 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] 1330 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 1331 1332 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET]; 1333 } 1334 |
1361 err = mmc_set_capacity(mmc, mmc->block_dev.hwpart); | 1335 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart); |
1362 if (err) 1363 return err; 1364 1365 if (IS_SD(mmc)) 1366 err = sd_change_freq(mmc); 1367 else 1368 err = mmc_change_freq(mmc); 1369 --- 123 unchanged lines hidden (view full) --- 1493 1494 /* Fix the block length for DDR mode */ 1495 if (mmc->ddr_mode) { 1496 mmc->read_bl_len = MMC_MAX_BLOCK_LEN; 1497 mmc->write_bl_len = MMC_MAX_BLOCK_LEN; 1498 } 1499 1500 /* fill in device description */ | 1336 if (err) 1337 return err; 1338 1339 if (IS_SD(mmc)) 1340 err = sd_change_freq(mmc); 1341 else 1342 err = mmc_change_freq(mmc); 1343 --- 123 unchanged lines hidden (view full) --- 1467 1468 /* Fix the block length for DDR mode */ 1469 if (mmc->ddr_mode) { 1470 mmc->read_bl_len = MMC_MAX_BLOCK_LEN; 1471 mmc->write_bl_len = MMC_MAX_BLOCK_LEN; 1472 } 1473 1474 /* fill in device description */ |
1501 mmc->block_dev.lun = 0; 1502 mmc->block_dev.hwpart = 0; 1503 mmc->block_dev.type = 0; 1504 mmc->block_dev.blksz = mmc->read_bl_len; 1505 mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz); 1506 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); | 1475 bdesc = mmc_get_blk_desc(mmc); 1476 bdesc->lun = 0; 1477 bdesc->hwpart = 0; 1478 bdesc->type = 0; 1479 bdesc->blksz = mmc->read_bl_len; 1480 bdesc->log2blksz = LOG2(bdesc->blksz); 1481 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len); |
1507#if !defined(CONFIG_SPL_BUILD) || \ 1508 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \ 1509 !defined(CONFIG_USE_TINY_PRINTF)) | 1482#if !defined(CONFIG_SPL_BUILD) || \ 1483 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \ 1484 !defined(CONFIG_USE_TINY_PRINTF)) |
1510 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x", | 1485 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x", |
1511 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff), 1512 (mmc->cid[3] >> 16) & 0xffff); | 1486 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff), 1487 (mmc->cid[3] >> 16) & 0xffff); |
1513 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff, | 1488 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff, |
1514 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, 1515 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff, 1516 (mmc->cid[2] >> 24) & 0xff); | 1489 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, 1490 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff, 1491 (mmc->cid[2] >> 24) & 0xff); |
1517 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf, | 1492 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf, |
1518 (mmc->cid[2] >> 16) & 0xf); 1519#else | 1493 (mmc->cid[2] >> 16) & 0xf); 1494#else |
1520 mmc->block_dev.vendor[0] = 0; 1521 mmc->block_dev.product[0] = 0; 1522 mmc->block_dev.revision[0] = 0; | 1495 bdesc->vendor[0] = 0; 1496 bdesc->product[0] = 0; 1497 bdesc->revision[0] = 0; |
1523#endif 1524#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT) | 1498#endif 1499#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT) |
1525 part_init(&mmc->block_dev); | 1500 part_init(bdesc); |
1526#endif 1527 1528 return 0; 1529} 1530 1531static int mmc_send_if_cond(struct mmc *mmc) 1532{ 1533 struct mmc_cmd cmd; --- 23 unchanged lines hidden (view full) --- 1557#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1558 printf("%s is deprecated! use mmc_create() instead.\n", __func__); 1559#endif 1560 return -1; 1561} 1562 1563struct mmc *mmc_create(const struct mmc_config *cfg, void *priv) 1564{ | 1501#endif 1502 1503 return 0; 1504} 1505 1506static int mmc_send_if_cond(struct mmc *mmc) 1507{ 1508 struct mmc_cmd cmd; --- 23 unchanged lines hidden (view full) --- 1532#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1533 printf("%s is deprecated! use mmc_create() instead.\n", __func__); 1534#endif 1535 return -1; 1536} 1537 1538struct mmc *mmc_create(const struct mmc_config *cfg, void *priv) 1539{ |
1540 struct blk_desc *bdesc; |
|
1565 struct mmc *mmc; 1566 1567 /* quick validation */ 1568 if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL || 1569 cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0) 1570 return NULL; 1571 1572 mmc = calloc(1, sizeof(*mmc)); --- 4 unchanged lines hidden (view full) --- 1577 mmc->priv = priv; 1578 1579 /* the following chunk was mmc_register() */ 1580 1581 /* Setup dsr related values */ 1582 mmc->dsr_imp = 0; 1583 mmc->dsr = 0xffffffff; 1584 /* Setup the universal parts of the block interface just once */ | 1541 struct mmc *mmc; 1542 1543 /* quick validation */ 1544 if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL || 1545 cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0) 1546 return NULL; 1547 1548 mmc = calloc(1, sizeof(*mmc)); --- 4 unchanged lines hidden (view full) --- 1553 mmc->priv = priv; 1554 1555 /* the following chunk was mmc_register() */ 1556 1557 /* Setup dsr related values */ 1558 mmc->dsr_imp = 0; 1559 mmc->dsr = 0xffffffff; 1560 /* Setup the universal parts of the block interface just once */ |
1585 mmc->block_dev.if_type = IF_TYPE_MMC; 1586 mmc->block_dev.devnum = cur_dev_num++; 1587 mmc->block_dev.removable = 1; 1588 mmc->block_dev.block_read = mmc_bread; 1589 mmc->block_dev.block_write = mmc_bwrite; 1590 mmc->block_dev.block_erase = mmc_berase; | 1561 bdesc = mmc_get_blk_desc(mmc); 1562 bdesc->if_type = IF_TYPE_MMC; 1563 bdesc->removable = 1; 1564 bdesc->devnum = mmc_get_next_devnum(); 1565 bdesc->block_read = mmc_bread; 1566 bdesc->block_write = mmc_bwrite; 1567 bdesc->block_erase = mmc_berase; |
1591 1592 /* setup initial part type */ | 1568 1569 /* setup initial part type */ |
1593 mmc->block_dev.part_type = mmc->cfg->part_type; | 1570 bdesc->part_type = mmc->cfg->part_type; 1571 mmc_list_add(mmc); |
1594 | 1572 |
1595 INIT_LIST_HEAD(&mmc->link); 1596 1597 list_add_tail(&mmc->link, &mmc_devices); 1598 | |
1599 return mmc; 1600} 1601 1602void mmc_destroy(struct mmc *mmc) 1603{ 1604 /* only freeing memory for now */ 1605 free(mmc); 1606} --- 52 unchanged lines hidden (view full) --- 1659 1660 /* Reset the Card */ 1661 err = mmc_go_idle(mmc); 1662 1663 if (err) 1664 return err; 1665 1666 /* The internal partition reset to user partition(0) at every CMD0*/ | 1573 return mmc; 1574} 1575 1576void mmc_destroy(struct mmc *mmc) 1577{ 1578 /* only freeing memory for now */ 1579 free(mmc); 1580} --- 52 unchanged lines hidden (view full) --- 1633 1634 /* Reset the Card */ 1635 err = mmc_go_idle(mmc); 1636 1637 if (err) 1638 return err; 1639 1640 /* The internal partition reset to user partition(0) at every CMD0*/ |
1667 mmc->block_dev.hwpart = 0; | 1641 mmc_get_blk_desc(mmc)->hwpart = 0; |
1668 1669 /* Test for SD version 2 */ 1670 err = mmc_send_if_cond(mmc); 1671 1672 /* Now try to get the SD card's operating condition */ 1673 err = sd_send_op_cond(mmc); 1674 1675 /* If the command timed out, we check for an MMC card */ --- 63 unchanged lines hidden (view full) --- 1739} 1740 1741/* board-specific MMC initializations. */ 1742__weak int board_mmc_init(bd_t *bis) 1743{ 1744 return -1; 1745} 1746 | 1642 1643 /* Test for SD version 2 */ 1644 err = mmc_send_if_cond(mmc); 1645 1646 /* Now try to get the SD card's operating condition */ 1647 err = sd_send_op_cond(mmc); 1648 1649 /* If the command timed out, we check for an MMC card */ --- 63 unchanged lines hidden (view full) --- 1713} 1714 1715/* board-specific MMC initializations. */ 1716__weak int board_mmc_init(bd_t *bis) 1717{ 1718 return -1; 1719} 1720 |
1747#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1748 1749void print_mmc_devices(char separator) 1750{ 1751 struct mmc *m; 1752 struct list_head *entry; 1753 char *mmc_type; 1754 1755 list_for_each(entry, &mmc_devices) { 1756 m = list_entry(entry, struct mmc, link); 1757 1758 if (m->has_init) 1759 mmc_type = IS_SD(m) ? "SD" : "eMMC"; 1760 else 1761 mmc_type = NULL; 1762 1763 printf("%s: %d", m->cfg->name, m->block_dev.devnum); 1764 if (mmc_type) 1765 printf(" (%s)", mmc_type); 1766 1767 if (entry->next != &mmc_devices) { 1768 printf("%c", separator); 1769 if (separator != '\n') 1770 puts (" "); 1771 } 1772 } 1773 1774 printf("\n"); 1775} 1776 1777#else 1778void print_mmc_devices(char separator) { } 1779#endif 1780 1781int get_mmc_num(void) 1782{ 1783 return cur_dev_num; 1784} 1785 | |
1786void mmc_set_preinit(struct mmc *mmc, int preinit) 1787{ 1788 mmc->preinit = preinit; 1789} 1790 | 1721void mmc_set_preinit(struct mmc *mmc, int preinit) 1722{ 1723 mmc->preinit = preinit; 1724} 1725 |
1791static void do_preinit(void) 1792{ 1793 struct mmc *m; 1794 struct list_head *entry; 1795 1796 list_for_each(entry, &mmc_devices) { 1797 m = list_entry(entry, struct mmc, link); 1798 1799#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT 1800 mmc_set_preinit(m, 1); 1801#endif 1802 if (m->preinit) 1803 mmc_start_init(m); 1804 } 1805} 1806 | |
1807#if defined(CONFIG_DM_MMC) && defined(CONFIG_SPL_BUILD) 1808static int mmc_probe(bd_t *bis) 1809{ 1810 return 0; 1811} 1812#elif defined(CONFIG_DM_MMC) 1813static int mmc_probe(bd_t *bis) 1814{ --- 36 unchanged lines hidden (view full) --- 1851int mmc_initialize(bd_t *bis) 1852{ 1853 static int initialized = 0; 1854 int ret; 1855 if (initialized) /* Avoid initializing mmc multiple times */ 1856 return 0; 1857 initialized = 1; 1858 | 1726#if defined(CONFIG_DM_MMC) && defined(CONFIG_SPL_BUILD) 1727static int mmc_probe(bd_t *bis) 1728{ 1729 return 0; 1730} 1731#elif defined(CONFIG_DM_MMC) 1732static int mmc_probe(bd_t *bis) 1733{ --- 36 unchanged lines hidden (view full) --- 1770int mmc_initialize(bd_t *bis) 1771{ 1772 static int initialized = 0; 1773 int ret; 1774 if (initialized) /* Avoid initializing mmc multiple times */ 1775 return 0; 1776 initialized = 1; 1777 |
1859 INIT_LIST_HEAD (&mmc_devices); 1860 cur_dev_num = 0; 1861 | 1778#ifndef CONFIG_BLK 1779 mmc_list_init(); 1780#endif |
1862 ret = mmc_probe(bis); 1863 if (ret) 1864 return ret; 1865 1866#ifndef CONFIG_SPL_BUILD 1867 print_mmc_devices(','); 1868#endif 1869 | 1781 ret = mmc_probe(bis); 1782 if (ret) 1783 return ret; 1784 1785#ifndef CONFIG_SPL_BUILD 1786 print_mmc_devices(','); 1787#endif 1788 |
1870 do_preinit(); | 1789 mmc_do_preinit(); |
1871 return 0; 1872} 1873 1874#ifdef CONFIG_SUPPORT_EMMC_BOOT 1875/* 1876 * This function changes the size of boot partition and the size of rpmb 1877 * partition present on EMMC devices. 1878 * --- 125 unchanged lines hidden --- | 1790 return 0; 1791} 1792 1793#ifdef CONFIG_SUPPORT_EMMC_BOOT 1794/* 1795 * This function changes the size of boot partition and the size of rpmb 1796 * partition present on EMMC devices. 1797 * --- 125 unchanged lines hidden --- |