1e7ecf7cbSSimon Glass /* 2e7ecf7cbSSimon Glass * Copyright (C) 2015 Google, Inc 3e7ecf7cbSSimon Glass * Written by Simon Glass <sjg@chromium.org> 4e7ecf7cbSSimon Glass * 5e7ecf7cbSSimon Glass * SPDX-License-Identifier: GPL-2.0+ 6e7ecf7cbSSimon Glass */ 7e7ecf7cbSSimon Glass 8e7ecf7cbSSimon Glass #include <common.h> 9e7ecf7cbSSimon Glass #include <mmc.h> 10e7ecf7cbSSimon Glass #include <dm.h> 11eede897eSSimon Glass #include <dm/device-internal.h> 12e7ecf7cbSSimon Glass #include <dm/lists.h> 13e7ecf7cbSSimon Glass #include <dm/root.h> 14eede897eSSimon Glass #include "mmc_private.h" 15e7ecf7cbSSimon Glass 1602ad33aaSJaehoon Chung DECLARE_GLOBAL_DATA_PTR; 1702ad33aaSJaehoon Chung 188ca51e51SSimon Glass #ifdef CONFIG_DM_MMC_OPS 198ca51e51SSimon Glass int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, 208ca51e51SSimon Glass struct mmc_data *data) 218ca51e51SSimon Glass { 228ca51e51SSimon Glass struct mmc *mmc = mmc_get_mmc_dev(dev); 238ca51e51SSimon Glass struct dm_mmc_ops *ops = mmc_get_ops(dev); 248ca51e51SSimon Glass int ret; 258ca51e51SSimon Glass 268ca51e51SSimon Glass mmmc_trace_before_send(mmc, cmd); 278ca51e51SSimon Glass if (ops->send_cmd) 288ca51e51SSimon Glass ret = ops->send_cmd(dev, cmd, data); 298ca51e51SSimon Glass else 308ca51e51SSimon Glass ret = -ENOSYS; 318ca51e51SSimon Glass mmmc_trace_after_send(mmc, cmd, ret); 328ca51e51SSimon Glass 338ca51e51SSimon Glass return ret; 348ca51e51SSimon Glass } 358ca51e51SSimon Glass 368ca51e51SSimon Glass int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) 378ca51e51SSimon Glass { 388ca51e51SSimon Glass return dm_mmc_send_cmd(mmc->dev, cmd, data); 398ca51e51SSimon Glass } 408ca51e51SSimon Glass 418ca51e51SSimon Glass int dm_mmc_set_ios(struct udevice *dev) 428ca51e51SSimon Glass { 438ca51e51SSimon Glass struct dm_mmc_ops *ops = mmc_get_ops(dev); 448ca51e51SSimon Glass 458ca51e51SSimon Glass if (!ops->set_ios) 468ca51e51SSimon Glass return -ENOSYS; 478ca51e51SSimon Glass return ops->set_ios(dev); 488ca51e51SSimon Glass } 498ca51e51SSimon Glass 508ca51e51SSimon Glass int mmc_set_ios(struct mmc *mmc) 518ca51e51SSimon Glass { 528ca51e51SSimon Glass return dm_mmc_set_ios(mmc->dev); 538ca51e51SSimon Glass } 548ca51e51SSimon Glass 558ca51e51SSimon Glass int dm_mmc_get_wp(struct udevice *dev) 568ca51e51SSimon Glass { 578ca51e51SSimon Glass struct dm_mmc_ops *ops = mmc_get_ops(dev); 588ca51e51SSimon Glass 598ca51e51SSimon Glass if (!ops->get_wp) 608ca51e51SSimon Glass return -ENOSYS; 618ca51e51SSimon Glass return ops->get_wp(dev); 628ca51e51SSimon Glass } 638ca51e51SSimon Glass 648ca51e51SSimon Glass int mmc_getwp(struct mmc *mmc) 658ca51e51SSimon Glass { 668ca51e51SSimon Glass return dm_mmc_get_wp(mmc->dev); 678ca51e51SSimon Glass } 688ca51e51SSimon Glass 698ca51e51SSimon Glass int dm_mmc_get_cd(struct udevice *dev) 708ca51e51SSimon Glass { 718ca51e51SSimon Glass struct dm_mmc_ops *ops = mmc_get_ops(dev); 728ca51e51SSimon Glass 738ca51e51SSimon Glass if (!ops->get_cd) 748ca51e51SSimon Glass return -ENOSYS; 758ca51e51SSimon Glass return ops->get_cd(dev); 768ca51e51SSimon Glass } 778ca51e51SSimon Glass 788ca51e51SSimon Glass int mmc_getcd(struct mmc *mmc) 798ca51e51SSimon Glass { 808ca51e51SSimon Glass return dm_mmc_get_cd(mmc->dev); 818ca51e51SSimon Glass } 828ca51e51SSimon Glass #endif 838ca51e51SSimon Glass 84e7ecf7cbSSimon Glass struct mmc *mmc_get_mmc_dev(struct udevice *dev) 85e7ecf7cbSSimon Glass { 86e7ecf7cbSSimon Glass struct mmc_uclass_priv *upriv; 87e7ecf7cbSSimon Glass 88e7ecf7cbSSimon Glass if (!device_active(dev)) 89e7ecf7cbSSimon Glass return NULL; 90e7ecf7cbSSimon Glass upriv = dev_get_uclass_priv(dev); 91e7ecf7cbSSimon Glass return upriv->mmc; 92e7ecf7cbSSimon Glass } 93e7ecf7cbSSimon Glass 948ef761edSSimon Glass #ifdef CONFIG_BLK 958ef761edSSimon Glass struct mmc *find_mmc_device(int dev_num) 968ef761edSSimon Glass { 978ef761edSSimon Glass struct udevice *dev, *mmc_dev; 988ef761edSSimon Glass int ret; 998ef761edSSimon Glass 1008ef761edSSimon Glass ret = blk_get_device(IF_TYPE_MMC, dev_num, &dev); 1018ef761edSSimon Glass 1028ef761edSSimon Glass if (ret) { 1038ef761edSSimon Glass #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1048ef761edSSimon Glass printf("MMC Device %d not found\n", dev_num); 1058ef761edSSimon Glass #endif 1068ef761edSSimon Glass return NULL; 1078ef761edSSimon Glass } 1088ef761edSSimon Glass 1098ef761edSSimon Glass mmc_dev = dev_get_parent(dev); 1108ef761edSSimon Glass 1118ef761edSSimon Glass return mmc_get_mmc_dev(mmc_dev); 1128ef761edSSimon Glass } 1138ef761edSSimon Glass 1148ef761edSSimon Glass int get_mmc_num(void) 1158ef761edSSimon Glass { 11646683f3dSKever Yang return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0); 1178ef761edSSimon Glass } 1188ef761edSSimon Glass 1198ef761edSSimon Glass int mmc_get_next_devnum(void) 1208ef761edSSimon Glass { 121dd399cb7SMasahiro Yamada return blk_find_max_devnum(IF_TYPE_MMC); 1228ef761edSSimon Glass } 1238ef761edSSimon Glass 1248ef761edSSimon Glass struct blk_desc *mmc_get_blk_desc(struct mmc *mmc) 1258ef761edSSimon Glass { 1268ef761edSSimon Glass struct blk_desc *desc; 1278ef761edSSimon Glass struct udevice *dev; 1288ef761edSSimon Glass 1298ef761edSSimon Glass device_find_first_child(mmc->dev, &dev); 1308ef761edSSimon Glass if (!dev) 1318ef761edSSimon Glass return NULL; 1328ef761edSSimon Glass desc = dev_get_uclass_platdata(dev); 1338ef761edSSimon Glass 1348ef761edSSimon Glass return desc; 1358ef761edSSimon Glass } 1368ef761edSSimon Glass 1378ef761edSSimon Glass void mmc_do_preinit(void) 1388ef761edSSimon Glass { 1398ef761edSSimon Glass struct udevice *dev; 1408ef761edSSimon Glass struct uclass *uc; 1418ef761edSSimon Glass int ret; 1428ef761edSSimon Glass 1438ef761edSSimon Glass ret = uclass_get(UCLASS_MMC, &uc); 1448ef761edSSimon Glass if (ret) 1458ef761edSSimon Glass return; 1468ef761edSSimon Glass uclass_foreach_dev(dev, uc) { 1478ef761edSSimon Glass struct mmc *m = mmc_get_mmc_dev(dev); 1488ef761edSSimon Glass 1498ef761edSSimon Glass if (!m) 1508ef761edSSimon Glass continue; 1518ef761edSSimon Glass #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT 1528ef761edSSimon Glass mmc_set_preinit(m, 1); 1538ef761edSSimon Glass #endif 1548ef761edSSimon Glass if (m->preinit) 1558ef761edSSimon Glass mmc_start_init(m); 1568ef761edSSimon Glass } 1578ef761edSSimon Glass } 1588ef761edSSimon Glass 1598ef761edSSimon Glass #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1608ef761edSSimon Glass void print_mmc_devices(char separator) 1618ef761edSSimon Glass { 1628ef761edSSimon Glass struct udevice *dev; 1638ef761edSSimon Glass char *mmc_type; 1648ef761edSSimon Glass bool first = true; 1658ef761edSSimon Glass 1668ef761edSSimon Glass for (uclass_first_device(UCLASS_MMC, &dev); 1678ef761edSSimon Glass dev; 1681bd4f92cSXu Ziyuan uclass_next_device(&dev), first = false) { 1698ef761edSSimon Glass struct mmc *m = mmc_get_mmc_dev(dev); 1708ef761edSSimon Glass 1718ef761edSSimon Glass if (!first) { 1728ef761edSSimon Glass printf("%c", separator); 1738ef761edSSimon Glass if (separator != '\n') 1748ef761edSSimon Glass puts(" "); 1758ef761edSSimon Glass } 1768ef761edSSimon Glass if (m->has_init) 1778ef761edSSimon Glass mmc_type = IS_SD(m) ? "SD" : "eMMC"; 1788ef761edSSimon Glass else 1798ef761edSSimon Glass mmc_type = NULL; 1808ef761edSSimon Glass 1818ef761edSSimon Glass printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum); 1828ef761edSSimon Glass if (mmc_type) 1838ef761edSSimon Glass printf(" (%s)", mmc_type); 1848ef761edSSimon Glass } 1858ef761edSSimon Glass 1868ef761edSSimon Glass printf("\n"); 1878ef761edSSimon Glass } 1888ef761edSSimon Glass 1898ef761edSSimon Glass #else 1908ef761edSSimon Glass void print_mmc_devices(char separator) { } 1918ef761edSSimon Glass #endif 192eede897eSSimon Glass 193eede897eSSimon Glass int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg) 194eede897eSSimon Glass { 195eede897eSSimon Glass struct blk_desc *bdesc; 196eede897eSSimon Glass struct udevice *bdev; 19702ad33aaSJaehoon Chung int ret, devnum = -1; 198eede897eSSimon Glass 19966656020SSimon Glass #ifdef CONFIG_DM_MMC_OPS 20066656020SSimon Glass if (!mmc_get_ops(dev)) 20166656020SSimon Glass return -ENOSYS; 20266656020SSimon Glass #endif 20302ad33aaSJaehoon Chung #ifndef CONFIG_SPL_BUILD 20402ad33aaSJaehoon Chung /* Use the fixed index with aliase node's index */ 205*66e0ed5cSSimon Glass ret = dev_read_alias_seq(dev, &devnum); 206*66e0ed5cSSimon Glass debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum); 20702ad33aaSJaehoon Chung #endif 20802ad33aaSJaehoon Chung 20902ad33aaSJaehoon Chung ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC, 21002ad33aaSJaehoon Chung devnum, 512, 0, &bdev); 211eede897eSSimon Glass if (ret) { 212eede897eSSimon Glass debug("Cannot create block device\n"); 213eede897eSSimon Glass return ret; 214eede897eSSimon Glass } 215eede897eSSimon Glass bdesc = dev_get_uclass_platdata(bdev); 216eede897eSSimon Glass mmc->cfg = cfg; 217eede897eSSimon Glass mmc->priv = dev; 218eede897eSSimon Glass 219eede897eSSimon Glass /* the following chunk was from mmc_register() */ 220eede897eSSimon Glass 221eede897eSSimon Glass /* Setup dsr related values */ 222eede897eSSimon Glass mmc->dsr_imp = 0; 223eede897eSSimon Glass mmc->dsr = 0xffffffff; 224eede897eSSimon Glass /* Setup the universal parts of the block interface just once */ 225eede897eSSimon Glass bdesc->removable = 1; 226eede897eSSimon Glass 227eede897eSSimon Glass /* setup initial part type */ 228eede897eSSimon Glass bdesc->part_type = cfg->part_type; 229eede897eSSimon Glass mmc->dev = dev; 230eede897eSSimon Glass 231eede897eSSimon Glass return 0; 232eede897eSSimon Glass } 233eede897eSSimon Glass 234eede897eSSimon Glass int mmc_unbind(struct udevice *dev) 235eede897eSSimon Glass { 236eede897eSSimon Glass struct udevice *bdev; 237eede897eSSimon Glass 238eede897eSSimon Glass device_find_first_child(dev, &bdev); 239eede897eSSimon Glass if (bdev) { 240706865afSStefan Roese device_remove(bdev, DM_REMOVE_NORMAL); 241eede897eSSimon Glass device_unbind(bdev); 242eede897eSSimon Glass } 243eede897eSSimon Glass 244eede897eSSimon Glass return 0; 245eede897eSSimon Glass } 246eede897eSSimon Glass 247eede897eSSimon Glass static int mmc_select_hwpart(struct udevice *bdev, int hwpart) 248eede897eSSimon Glass { 249eede897eSSimon Glass struct udevice *mmc_dev = dev_get_parent(bdev); 250eede897eSSimon Glass struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); 251eede897eSSimon Glass struct blk_desc *desc = dev_get_uclass_platdata(bdev); 252eede897eSSimon Glass 253eede897eSSimon Glass if (desc->hwpart == hwpart) 254eede897eSSimon Glass return 0; 255eede897eSSimon Glass 256eede897eSSimon Glass if (mmc->part_config == MMCPART_NOAVAILABLE) 257eede897eSSimon Glass return -EMEDIUMTYPE; 258eede897eSSimon Glass 259dd399cb7SMasahiro Yamada return mmc_switch_part(mmc, hwpart); 260eede897eSSimon Glass } 261eede897eSSimon Glass 262a0269bb6SFiach Antaw static int mmc_blk_probe(struct udevice *dev) 263a0269bb6SFiach Antaw { 264854f9a71SSimon Glass struct udevice *mmc_dev = dev_get_parent(dev); 265854f9a71SSimon Glass struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev); 266854f9a71SSimon Glass struct mmc *mmc = upriv->mmc; 267854f9a71SSimon Glass int ret; 268a0269bb6SFiach Antaw 269854f9a71SSimon Glass ret = mmc_init(mmc); 270854f9a71SSimon Glass if (ret) { 271854f9a71SSimon Glass debug("%s: mmc_init() failed (err=%d)\n", __func__, ret); 272854f9a71SSimon Glass return ret; 273854f9a71SSimon Glass } 274854f9a71SSimon Glass 275854f9a71SSimon Glass return 0; 276a0269bb6SFiach Antaw } 277a0269bb6SFiach Antaw 278eede897eSSimon Glass static const struct blk_ops mmc_blk_ops = { 279eede897eSSimon Glass .read = mmc_bread, 280eede897eSSimon Glass #ifndef CONFIG_SPL_BUILD 281eede897eSSimon Glass .write = mmc_bwrite, 282561e624cSSimon Glass .erase = mmc_berase, 283eede897eSSimon Glass #endif 284eede897eSSimon Glass .select_hwpart = mmc_select_hwpart, 285eede897eSSimon Glass }; 286eede897eSSimon Glass 287eede897eSSimon Glass U_BOOT_DRIVER(mmc_blk) = { 288eede897eSSimon Glass .name = "mmc_blk", 289eede897eSSimon Glass .id = UCLASS_BLK, 290eede897eSSimon Glass .ops = &mmc_blk_ops, 291a0269bb6SFiach Antaw .probe = mmc_blk_probe, 292eede897eSSimon Glass }; 2938ef761edSSimon Glass #endif /* CONFIG_BLK */ 2948ef761edSSimon Glass 295e7ecf7cbSSimon Glass U_BOOT_DRIVER(mmc) = { 296e7ecf7cbSSimon Glass .name = "mmc", 297e7ecf7cbSSimon Glass .id = UCLASS_MMC, 298e7ecf7cbSSimon Glass }; 299e7ecf7cbSSimon Glass 300e7ecf7cbSSimon Glass UCLASS_DRIVER(mmc) = { 301e7ecf7cbSSimon Glass .id = UCLASS_MMC, 302e7ecf7cbSSimon Glass .name = "mmc", 303e7ecf7cbSSimon Glass .flags = DM_UC_FLAG_SEQ_ALIAS, 304e7ecf7cbSSimon Glass .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv), 305e7ecf7cbSSimon Glass }; 306