2 * Copyright (C) 2015 Google, Inc
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <dm/device-internal.h>
14 #include "mmc_private.h"
16 #ifdef CONFIG_DM_MMC_OPS
17 int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
18 struct mmc_data *data)
20 struct mmc *mmc = mmc_get_mmc_dev(dev);
21 struct dm_mmc_ops *ops = mmc_get_ops(dev);
24 mmmc_trace_before_send(mmc, cmd);
26 ret = ops->send_cmd(dev, cmd, data);
29 mmmc_trace_after_send(mmc, cmd, ret);
34 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
36 return dm_mmc_send_cmd(mmc->dev, cmd, data);
39 int dm_mmc_set_ios(struct udevice *dev)
41 struct dm_mmc_ops *ops = mmc_get_ops(dev);
45 return ops->set_ios(dev);
48 int mmc_set_ios(struct mmc *mmc)
50 return dm_mmc_set_ios(mmc->dev);
53 int dm_mmc_get_wp(struct udevice *dev)
55 struct dm_mmc_ops *ops = mmc_get_ops(dev);
59 return ops->get_wp(dev);
62 int mmc_getwp(struct mmc *mmc)
64 return dm_mmc_get_wp(mmc->dev);
67 int dm_mmc_get_cd(struct udevice *dev)
69 struct dm_mmc_ops *ops = mmc_get_ops(dev);
73 return ops->get_cd(dev);
76 int mmc_getcd(struct mmc *mmc)
78 return dm_mmc_get_cd(mmc->dev);
82 struct mmc *mmc_get_mmc_dev(struct udevice *dev)
84 struct mmc_uclass_priv *upriv;
86 if (!device_active(dev))
88 upriv = dev_get_uclass_priv(dev);
93 struct mmc *find_mmc_device(int dev_num)
95 struct udevice *dev, *mmc_dev;
98 ret = blk_get_device(IF_TYPE_MMC, dev_num, &dev);
101 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
102 printf("MMC Device %d not found\n", dev_num);
107 mmc_dev = dev_get_parent(dev);
109 return mmc_get_mmc_dev(mmc_dev);
112 int get_mmc_num(void)
114 return max(blk_find_max_devnum(IF_TYPE_MMC), 0);
117 int mmc_get_next_devnum(void)
128 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
130 struct blk_desc *desc;
133 device_find_first_child(mmc->dev, &dev);
136 desc = dev_get_uclass_platdata(dev);
141 void mmc_do_preinit(void)
147 ret = uclass_get(UCLASS_MMC, &uc);
150 uclass_foreach_dev(dev, uc) {
151 struct mmc *m = mmc_get_mmc_dev(dev);
155 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
156 mmc_set_preinit(m, 1);
163 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
164 void print_mmc_devices(char separator)
170 for (uclass_first_device(UCLASS_MMC, &dev);
172 uclass_next_device(&dev)) {
173 struct mmc *m = mmc_get_mmc_dev(dev);
176 printf("%c", separator);
177 if (separator != '\n')
181 mmc_type = IS_SD(m) ? "SD" : "eMMC";
185 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
187 printf(" (%s)", mmc_type);
194 void print_mmc_devices(char separator) { }
197 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
199 struct blk_desc *bdesc;
200 struct udevice *bdev;
203 ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC, -1, 512,
206 debug("Cannot create block device\n");
209 bdesc = dev_get_uclass_platdata(bdev);
213 /* the following chunk was from mmc_register() */
215 /* Setup dsr related values */
217 mmc->dsr = 0xffffffff;
218 /* Setup the universal parts of the block interface just once */
219 bdesc->removable = 1;
221 /* setup initial part type */
222 bdesc->part_type = cfg->part_type;
228 int mmc_unbind(struct udevice *dev)
230 struct udevice *bdev;
232 device_find_first_child(dev, &bdev);
241 static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
243 struct udevice *mmc_dev = dev_get_parent(bdev);
244 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
245 struct blk_desc *desc = dev_get_uclass_platdata(bdev);
248 if (desc->hwpart == hwpart)
251 if (mmc->part_config == MMCPART_NOAVAILABLE)
254 ret = mmc_switch_part(mmc, hwpart);
261 static const struct blk_ops mmc_blk_ops = {
263 #ifndef CONFIG_SPL_BUILD
266 .select_hwpart = mmc_select_hwpart,
269 U_BOOT_DRIVER(mmc_blk) = {
274 #endif /* CONFIG_BLK */
276 U_BOOT_DRIVER(mmc) = {
281 UCLASS_DRIVER(mmc) = {
284 .flags = DM_UC_FLAG_SEQ_ALIAS,
285 .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),