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 19902ad33aaSJaehoon Chung #ifndef CONFIG_SPL_BUILD 20002ad33aaSJaehoon Chung /* Use the fixed index with aliase node's index */ 20102ad33aaSJaehoon Chung fdtdec_get_alias_seq(gd->fdt_blob, "mmc", dev->of_offset, &devnum); 20202ad33aaSJaehoon Chung #endif 20302ad33aaSJaehoon Chung 20402ad33aaSJaehoon Chung ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC, 20502ad33aaSJaehoon Chung devnum, 512, 0, &bdev); 206eede897eSSimon Glass if (ret) { 207eede897eSSimon Glass debug("Cannot create block device\n"); 208eede897eSSimon Glass return ret; 209eede897eSSimon Glass } 210eede897eSSimon Glass bdesc = dev_get_uclass_platdata(bdev); 211eede897eSSimon Glass mmc->cfg = cfg; 212eede897eSSimon Glass mmc->priv = dev; 213eede897eSSimon Glass 214eede897eSSimon Glass /* the following chunk was from mmc_register() */ 215eede897eSSimon Glass 216eede897eSSimon Glass /* Setup dsr related values */ 217eede897eSSimon Glass mmc->dsr_imp = 0; 218eede897eSSimon Glass mmc->dsr = 0xffffffff; 219eede897eSSimon Glass /* Setup the universal parts of the block interface just once */ 220eede897eSSimon Glass bdesc->removable = 1; 221eede897eSSimon Glass 222eede897eSSimon Glass /* setup initial part type */ 223eede897eSSimon Glass bdesc->part_type = cfg->part_type; 224eede897eSSimon Glass mmc->dev = dev; 225eede897eSSimon Glass 226eede897eSSimon Glass return 0; 227eede897eSSimon Glass } 228eede897eSSimon Glass 229eede897eSSimon Glass int mmc_unbind(struct udevice *dev) 230eede897eSSimon Glass { 231eede897eSSimon Glass struct udevice *bdev; 232eede897eSSimon Glass 233eede897eSSimon Glass device_find_first_child(dev, &bdev); 234eede897eSSimon Glass if (bdev) { 235*706865afSStefan Roese device_remove(bdev, DM_REMOVE_NORMAL); 236eede897eSSimon Glass device_unbind(bdev); 237eede897eSSimon Glass } 238eede897eSSimon Glass 239eede897eSSimon Glass return 0; 240eede897eSSimon Glass } 241eede897eSSimon Glass 242eede897eSSimon Glass static int mmc_select_hwpart(struct udevice *bdev, int hwpart) 243eede897eSSimon Glass { 244eede897eSSimon Glass struct udevice *mmc_dev = dev_get_parent(bdev); 245eede897eSSimon Glass struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); 246eede897eSSimon Glass struct blk_desc *desc = dev_get_uclass_platdata(bdev); 247eede897eSSimon Glass 248eede897eSSimon Glass if (desc->hwpart == hwpart) 249eede897eSSimon Glass return 0; 250eede897eSSimon Glass 251eede897eSSimon Glass if (mmc->part_config == MMCPART_NOAVAILABLE) 252eede897eSSimon Glass return -EMEDIUMTYPE; 253eede897eSSimon Glass 254dd399cb7SMasahiro Yamada return mmc_switch_part(mmc, hwpart); 255eede897eSSimon Glass } 256eede897eSSimon Glass 257a0269bb6SFiach Antaw static int mmc_blk_probe(struct udevice *dev) 258a0269bb6SFiach Antaw { 259a0269bb6SFiach Antaw struct blk_desc *block_dev = dev_get_uclass_platdata(dev); 260a0269bb6SFiach Antaw int dev_num = block_dev->devnum; 261a0269bb6SFiach Antaw struct mmc *mmc = find_mmc_device(dev_num); 262a0269bb6SFiach Antaw 263a0269bb6SFiach Antaw if (!mmc) 264a0269bb6SFiach Antaw return -ENODEV; 265a0269bb6SFiach Antaw return mmc_init(mmc); 266a0269bb6SFiach Antaw } 267a0269bb6SFiach Antaw 268eede897eSSimon Glass static const struct blk_ops mmc_blk_ops = { 269eede897eSSimon Glass .read = mmc_bread, 270eede897eSSimon Glass #ifndef CONFIG_SPL_BUILD 271eede897eSSimon Glass .write = mmc_bwrite, 272561e624cSSimon Glass .erase = mmc_berase, 273eede897eSSimon Glass #endif 274eede897eSSimon Glass .select_hwpart = mmc_select_hwpart, 275eede897eSSimon Glass }; 276eede897eSSimon Glass 277eede897eSSimon Glass U_BOOT_DRIVER(mmc_blk) = { 278eede897eSSimon Glass .name = "mmc_blk", 279eede897eSSimon Glass .id = UCLASS_BLK, 280eede897eSSimon Glass .ops = &mmc_blk_ops, 281a0269bb6SFiach Antaw .probe = mmc_blk_probe, 282eede897eSSimon Glass }; 2838ef761edSSimon Glass #endif /* CONFIG_BLK */ 2848ef761edSSimon Glass 285e7ecf7cbSSimon Glass U_BOOT_DRIVER(mmc) = { 286e7ecf7cbSSimon Glass .name = "mmc", 287e7ecf7cbSSimon Glass .id = UCLASS_MMC, 288e7ecf7cbSSimon Glass }; 289e7ecf7cbSSimon Glass 290e7ecf7cbSSimon Glass UCLASS_DRIVER(mmc) = { 291e7ecf7cbSSimon Glass .id = UCLASS_MMC, 292e7ecf7cbSSimon Glass .name = "mmc", 293e7ecf7cbSSimon Glass .flags = DM_UC_FLAG_SEQ_ALIAS, 294e7ecf7cbSSimon Glass .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv), 295e7ecf7cbSSimon Glass }; 296