2 * Copyright (C) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <dm/device-internal.h>
14 #include "mmc_private.h"
16 struct mmc *mmc_get_mmc_dev(struct udevice *dev)
18 struct mmc_uclass_priv *upriv;
20 if (!device_active(dev))
22 upriv = dev_get_uclass_priv(dev);
27 struct mmc *find_mmc_device(int dev_num)
29 struct udevice *dev, *mmc_dev;
32 ret = blk_get_device(IF_TYPE_MMC, dev_num, &dev);
35 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
36 printf("MMC Device %d not found\n", dev_num);
41 mmc_dev = dev_get_parent(dev);
43 return mmc_get_mmc_dev(mmc_dev);
48 return max(blk_find_max_devnum(IF_TYPE_MMC), 0);
51 int mmc_get_next_devnum(void)
62 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
64 struct blk_desc *desc;
67 device_find_first_child(mmc->dev, &dev);
70 desc = dev_get_uclass_platdata(dev);
75 void mmc_do_preinit(void)
81 ret = uclass_get(UCLASS_MMC, &uc);
84 uclass_foreach_dev(dev, uc) {
85 struct mmc *m = mmc_get_mmc_dev(dev);
89 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
90 mmc_set_preinit(m, 1);
97 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
98 void print_mmc_devices(char separator)
104 for (uclass_first_device(UCLASS_MMC, &dev);
106 uclass_next_device(&dev)) {
107 struct mmc *m = mmc_get_mmc_dev(dev);
110 printf("%c", separator);
111 if (separator != '\n')
115 mmc_type = IS_SD(m) ? "SD" : "eMMC";
119 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
121 printf(" (%s)", mmc_type);
128 void print_mmc_devices(char separator) { }
131 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
133 struct blk_desc *bdesc;
134 struct udevice *bdev;
137 ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC, -1, 512,
140 debug("Cannot create block device\n");
143 bdesc = dev_get_uclass_platdata(bdev);
147 /* the following chunk was from mmc_register() */
149 /* Setup dsr related values */
151 mmc->dsr = 0xffffffff;
152 /* Setup the universal parts of the block interface just once */
153 bdesc->removable = 1;
155 /* setup initial part type */
156 bdesc->part_type = cfg->part_type;
162 int mmc_unbind(struct udevice *dev)
164 struct udevice *bdev;
166 device_find_first_child(dev, &bdev);
175 static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
177 struct udevice *mmc_dev = dev_get_parent(bdev);
178 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
179 struct blk_desc *desc = dev_get_uclass_platdata(bdev);
182 if (desc->hwpart == hwpart)
185 if (mmc->part_config == MMCPART_NOAVAILABLE)
188 ret = mmc_switch_part(mmc, hwpart);
195 static const struct blk_ops mmc_blk_ops = {
197 #ifndef CONFIG_SPL_BUILD
200 .select_hwpart = mmc_select_hwpart,
203 U_BOOT_DRIVER(mmc_blk) = {
208 #endif /* CONFIG_BLK */
210 U_BOOT_DRIVER(mmc) = {
215 UCLASS_DRIVER(mmc) = {
218 .flags = DM_UC_FLAG_SEQ_ALIAS,
219 .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),