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 168ca51e51SSimon Glass #ifdef CONFIG_DM_MMC_OPS 178ca51e51SSimon Glass int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, 188ca51e51SSimon Glass struct mmc_data *data) 198ca51e51SSimon Glass { 208ca51e51SSimon Glass struct mmc *mmc = mmc_get_mmc_dev(dev); 218ca51e51SSimon Glass struct dm_mmc_ops *ops = mmc_get_ops(dev); 228ca51e51SSimon Glass int ret; 238ca51e51SSimon Glass 248ca51e51SSimon Glass mmmc_trace_before_send(mmc, cmd); 258ca51e51SSimon Glass if (ops->send_cmd) 268ca51e51SSimon Glass ret = ops->send_cmd(dev, cmd, data); 278ca51e51SSimon Glass else 288ca51e51SSimon Glass ret = -ENOSYS; 298ca51e51SSimon Glass mmmc_trace_after_send(mmc, cmd, ret); 308ca51e51SSimon Glass 318ca51e51SSimon Glass return ret; 328ca51e51SSimon Glass } 338ca51e51SSimon Glass 348ca51e51SSimon Glass int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) 358ca51e51SSimon Glass { 368ca51e51SSimon Glass return dm_mmc_send_cmd(mmc->dev, cmd, data); 378ca51e51SSimon Glass } 388ca51e51SSimon Glass 398ca51e51SSimon Glass int dm_mmc_set_ios(struct udevice *dev) 408ca51e51SSimon Glass { 418ca51e51SSimon Glass struct dm_mmc_ops *ops = mmc_get_ops(dev); 428ca51e51SSimon Glass 438ca51e51SSimon Glass if (!ops->set_ios) 448ca51e51SSimon Glass return -ENOSYS; 458ca51e51SSimon Glass return ops->set_ios(dev); 468ca51e51SSimon Glass } 478ca51e51SSimon Glass 488ca51e51SSimon Glass int mmc_set_ios(struct mmc *mmc) 498ca51e51SSimon Glass { 508ca51e51SSimon Glass return dm_mmc_set_ios(mmc->dev); 518ca51e51SSimon Glass } 528ca51e51SSimon Glass 538ca51e51SSimon Glass int dm_mmc_get_wp(struct udevice *dev) 548ca51e51SSimon Glass { 558ca51e51SSimon Glass struct dm_mmc_ops *ops = mmc_get_ops(dev); 568ca51e51SSimon Glass 578ca51e51SSimon Glass if (!ops->get_wp) 588ca51e51SSimon Glass return -ENOSYS; 598ca51e51SSimon Glass return ops->get_wp(dev); 608ca51e51SSimon Glass } 618ca51e51SSimon Glass 628ca51e51SSimon Glass int mmc_getwp(struct mmc *mmc) 638ca51e51SSimon Glass { 648ca51e51SSimon Glass return dm_mmc_get_wp(mmc->dev); 658ca51e51SSimon Glass } 668ca51e51SSimon Glass 678ca51e51SSimon Glass int dm_mmc_get_cd(struct udevice *dev) 688ca51e51SSimon Glass { 698ca51e51SSimon Glass struct dm_mmc_ops *ops = mmc_get_ops(dev); 708ca51e51SSimon Glass 718ca51e51SSimon Glass if (!ops->get_cd) 728ca51e51SSimon Glass return -ENOSYS; 738ca51e51SSimon Glass return ops->get_cd(dev); 748ca51e51SSimon Glass } 758ca51e51SSimon Glass 768ca51e51SSimon Glass int mmc_getcd(struct mmc *mmc) 778ca51e51SSimon Glass { 788ca51e51SSimon Glass return dm_mmc_get_cd(mmc->dev); 798ca51e51SSimon Glass } 808ca51e51SSimon Glass #endif 818ca51e51SSimon Glass 82e7ecf7cbSSimon Glass struct mmc *mmc_get_mmc_dev(struct udevice *dev) 83e7ecf7cbSSimon Glass { 84e7ecf7cbSSimon Glass struct mmc_uclass_priv *upriv; 85e7ecf7cbSSimon Glass 86e7ecf7cbSSimon Glass if (!device_active(dev)) 87e7ecf7cbSSimon Glass return NULL; 88e7ecf7cbSSimon Glass upriv = dev_get_uclass_priv(dev); 89e7ecf7cbSSimon Glass return upriv->mmc; 90e7ecf7cbSSimon Glass } 91e7ecf7cbSSimon Glass 928ef761edSSimon Glass #ifdef CONFIG_BLK 938ef761edSSimon Glass struct mmc *find_mmc_device(int dev_num) 948ef761edSSimon Glass { 958ef761edSSimon Glass struct udevice *dev, *mmc_dev; 968ef761edSSimon Glass int ret; 978ef761edSSimon Glass 988ef761edSSimon Glass ret = blk_get_device(IF_TYPE_MMC, dev_num, &dev); 998ef761edSSimon Glass 1008ef761edSSimon Glass if (ret) { 1018ef761edSSimon Glass #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1028ef761edSSimon Glass printf("MMC Device %d not found\n", dev_num); 1038ef761edSSimon Glass #endif 1048ef761edSSimon Glass return NULL; 1058ef761edSSimon Glass } 1068ef761edSSimon Glass 1078ef761edSSimon Glass mmc_dev = dev_get_parent(dev); 1088ef761edSSimon Glass 1098ef761edSSimon Glass return mmc_get_mmc_dev(mmc_dev); 1108ef761edSSimon Glass } 1118ef761edSSimon Glass 1128ef761edSSimon Glass int get_mmc_num(void) 1138ef761edSSimon Glass { 11446683f3dSKever Yang return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0); 1158ef761edSSimon Glass } 1168ef761edSSimon Glass 1178ef761edSSimon Glass int mmc_get_next_devnum(void) 1188ef761edSSimon Glass { 1198ef761edSSimon Glass int ret; 1208ef761edSSimon Glass 12146683f3dSKever Yang ret = blk_find_max_devnum(IF_TYPE_MMC); 1228ef761edSSimon Glass if (ret < 0) 1238ef761edSSimon Glass return ret; 1248ef761edSSimon Glass 12546683f3dSKever Yang return ret; 1268ef761edSSimon Glass } 1278ef761edSSimon Glass 1288ef761edSSimon Glass struct blk_desc *mmc_get_blk_desc(struct mmc *mmc) 1298ef761edSSimon Glass { 1308ef761edSSimon Glass struct blk_desc *desc; 1318ef761edSSimon Glass struct udevice *dev; 1328ef761edSSimon Glass 1338ef761edSSimon Glass device_find_first_child(mmc->dev, &dev); 1348ef761edSSimon Glass if (!dev) 1358ef761edSSimon Glass return NULL; 1368ef761edSSimon Glass desc = dev_get_uclass_platdata(dev); 1378ef761edSSimon Glass 1388ef761edSSimon Glass return desc; 1398ef761edSSimon Glass } 1408ef761edSSimon Glass 1418ef761edSSimon Glass void mmc_do_preinit(void) 1428ef761edSSimon Glass { 1438ef761edSSimon Glass struct udevice *dev; 1448ef761edSSimon Glass struct uclass *uc; 1458ef761edSSimon Glass int ret; 1468ef761edSSimon Glass 1478ef761edSSimon Glass ret = uclass_get(UCLASS_MMC, &uc); 1488ef761edSSimon Glass if (ret) 1498ef761edSSimon Glass return; 1508ef761edSSimon Glass uclass_foreach_dev(dev, uc) { 1518ef761edSSimon Glass struct mmc *m = mmc_get_mmc_dev(dev); 1528ef761edSSimon Glass 1538ef761edSSimon Glass if (!m) 1548ef761edSSimon Glass continue; 1558ef761edSSimon Glass #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT 1568ef761edSSimon Glass mmc_set_preinit(m, 1); 1578ef761edSSimon Glass #endif 1588ef761edSSimon Glass if (m->preinit) 1598ef761edSSimon Glass mmc_start_init(m); 1608ef761edSSimon Glass } 1618ef761edSSimon Glass } 1628ef761edSSimon Glass 1638ef761edSSimon Glass #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1648ef761edSSimon Glass void print_mmc_devices(char separator) 1658ef761edSSimon Glass { 1668ef761edSSimon Glass struct udevice *dev; 1678ef761edSSimon Glass char *mmc_type; 1688ef761edSSimon Glass bool first = true; 1698ef761edSSimon Glass 1708ef761edSSimon Glass for (uclass_first_device(UCLASS_MMC, &dev); 1718ef761edSSimon Glass dev; 1721bd4f92cSXu Ziyuan uclass_next_device(&dev), first = false) { 1738ef761edSSimon Glass struct mmc *m = mmc_get_mmc_dev(dev); 1748ef761edSSimon Glass 1758ef761edSSimon Glass if (!first) { 1768ef761edSSimon Glass printf("%c", separator); 1778ef761edSSimon Glass if (separator != '\n') 1788ef761edSSimon Glass puts(" "); 1798ef761edSSimon Glass } 1808ef761edSSimon Glass if (m->has_init) 1818ef761edSSimon Glass mmc_type = IS_SD(m) ? "SD" : "eMMC"; 1828ef761edSSimon Glass else 1838ef761edSSimon Glass mmc_type = NULL; 1848ef761edSSimon Glass 1858ef761edSSimon Glass printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum); 1868ef761edSSimon Glass if (mmc_type) 1878ef761edSSimon Glass printf(" (%s)", mmc_type); 1888ef761edSSimon Glass } 1898ef761edSSimon Glass 1908ef761edSSimon Glass printf("\n"); 1918ef761edSSimon Glass } 1928ef761edSSimon Glass 1938ef761edSSimon Glass #else 1948ef761edSSimon Glass void print_mmc_devices(char separator) { } 1958ef761edSSimon Glass #endif 196eede897eSSimon Glass 197eede897eSSimon Glass int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg) 198eede897eSSimon Glass { 199eede897eSSimon Glass struct blk_desc *bdesc; 200eede897eSSimon Glass struct udevice *bdev; 201eede897eSSimon Glass int ret; 202eede897eSSimon Glass 203eede897eSSimon Glass ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC, -1, 512, 204eede897eSSimon Glass 0, &bdev); 205eede897eSSimon Glass if (ret) { 206eede897eSSimon Glass debug("Cannot create block device\n"); 207eede897eSSimon Glass return ret; 208eede897eSSimon Glass } 209eede897eSSimon Glass bdesc = dev_get_uclass_platdata(bdev); 210eede897eSSimon Glass mmc->cfg = cfg; 211eede897eSSimon Glass mmc->priv = dev; 212eede897eSSimon Glass 213eede897eSSimon Glass /* the following chunk was from mmc_register() */ 214eede897eSSimon Glass 215eede897eSSimon Glass /* Setup dsr related values */ 216eede897eSSimon Glass mmc->dsr_imp = 0; 217eede897eSSimon Glass mmc->dsr = 0xffffffff; 218eede897eSSimon Glass /* Setup the universal parts of the block interface just once */ 219eede897eSSimon Glass bdesc->removable = 1; 220eede897eSSimon Glass 221eede897eSSimon Glass /* setup initial part type */ 222eede897eSSimon Glass bdesc->part_type = cfg->part_type; 223eede897eSSimon Glass mmc->dev = dev; 224eede897eSSimon Glass 225eede897eSSimon Glass return 0; 226eede897eSSimon Glass } 227eede897eSSimon Glass 228eede897eSSimon Glass int mmc_unbind(struct udevice *dev) 229eede897eSSimon Glass { 230eede897eSSimon Glass struct udevice *bdev; 231eede897eSSimon Glass 232eede897eSSimon Glass device_find_first_child(dev, &bdev); 233eede897eSSimon Glass if (bdev) { 234eede897eSSimon Glass device_remove(bdev); 235eede897eSSimon Glass device_unbind(bdev); 236eede897eSSimon Glass } 237eede897eSSimon Glass 238eede897eSSimon Glass return 0; 239eede897eSSimon Glass } 240eede897eSSimon Glass 241eede897eSSimon Glass static int mmc_select_hwpart(struct udevice *bdev, int hwpart) 242eede897eSSimon Glass { 243eede897eSSimon Glass struct udevice *mmc_dev = dev_get_parent(bdev); 244eede897eSSimon Glass struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); 245eede897eSSimon Glass struct blk_desc *desc = dev_get_uclass_platdata(bdev); 246eede897eSSimon Glass int ret; 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 254eede897eSSimon Glass ret = mmc_switch_part(mmc, hwpart); 255eede897eSSimon Glass if (ret) 256eede897eSSimon Glass return ret; 257eede897eSSimon Glass 258eede897eSSimon Glass return 0; 259eede897eSSimon Glass } 260eede897eSSimon Glass 261eede897eSSimon Glass static const struct blk_ops mmc_blk_ops = { 262eede897eSSimon Glass .read = mmc_bread, 263eede897eSSimon Glass #ifndef CONFIG_SPL_BUILD 264eede897eSSimon Glass .write = mmc_bwrite, 265*561e624cSSimon Glass .erase = mmc_berase, 266eede897eSSimon Glass #endif 267eede897eSSimon Glass .select_hwpart = mmc_select_hwpart, 268eede897eSSimon Glass }; 269eede897eSSimon Glass 270eede897eSSimon Glass U_BOOT_DRIVER(mmc_blk) = { 271eede897eSSimon Glass .name = "mmc_blk", 272eede897eSSimon Glass .id = UCLASS_BLK, 273eede897eSSimon Glass .ops = &mmc_blk_ops, 274eede897eSSimon Glass }; 2758ef761edSSimon Glass #endif /* CONFIG_BLK */ 2768ef761edSSimon Glass 277e7ecf7cbSSimon Glass U_BOOT_DRIVER(mmc) = { 278e7ecf7cbSSimon Glass .name = "mmc", 279e7ecf7cbSSimon Glass .id = UCLASS_MMC, 280e7ecf7cbSSimon Glass }; 281e7ecf7cbSSimon Glass 282e7ecf7cbSSimon Glass UCLASS_DRIVER(mmc) = { 283e7ecf7cbSSimon Glass .id = UCLASS_MMC, 284e7ecf7cbSSimon Glass .name = "mmc", 285e7ecf7cbSSimon Glass .flags = DM_UC_FLAG_SEQ_ALIAS, 286e7ecf7cbSSimon Glass .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv), 287e7ecf7cbSSimon Glass }; 288