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 DECLARE_GLOBAL_DATA_PTR;
18 int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
19 struct mmc_data *data)
21 struct mmc *mmc = mmc_get_mmc_dev(dev);
22 struct dm_mmc_ops *ops = mmc_get_ops(dev);
25 mmmc_trace_before_send(mmc, cmd);
27 ret = ops->send_cmd(dev, cmd, data);
30 mmmc_trace_after_send(mmc, cmd, ret);
35 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
37 return dm_mmc_send_cmd(mmc->dev, cmd, data);
40 int dm_mmc_set_ios(struct udevice *dev)
42 struct dm_mmc_ops *ops = mmc_get_ops(dev);
46 return ops->set_ios(dev);
49 int mmc_set_ios(struct mmc *mmc)
51 return dm_mmc_set_ios(mmc->dev);
54 int dm_mmc_get_wp(struct udevice *dev)
56 struct dm_mmc_ops *ops = mmc_get_ops(dev);
60 return ops->get_wp(dev);
63 int mmc_getwp(struct mmc *mmc)
65 return dm_mmc_get_wp(mmc->dev);
68 int dm_mmc_get_cd(struct udevice *dev)
70 struct dm_mmc_ops *ops = mmc_get_ops(dev);
74 return ops->get_cd(dev);
77 int mmc_getcd(struct mmc *mmc)
79 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);
92 #if CONFIG_IS_ENABLED(BLK)
93 struct mmc *find_mmc_device(int dev_num)
95 struct udevice *dev, *mmc_dev;
98 ret = blk_find_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 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
114 int get_mmc_num(void)
116 return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
119 int mmc_get_next_devnum(void)
121 return blk_find_max_devnum(IF_TYPE_MMC);
124 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
126 struct blk_desc *desc;
129 device_find_first_child(mmc->dev, &dev);
132 desc = dev_get_uclass_platdata(dev);
137 void mmc_do_preinit(void)
143 ret = uclass_get(UCLASS_MMC, &uc);
146 uclass_foreach_dev(dev, uc) {
147 struct mmc *m = mmc_get_mmc_dev(dev);
151 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
152 mmc_set_preinit(m, 1);
159 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
160 void print_mmc_devices(char separator)
166 for (uclass_first_device(UCLASS_MMC, &dev);
168 uclass_next_device(&dev), first = false) {
169 struct mmc *m = mmc_get_mmc_dev(dev);
172 printf("%c", separator);
173 if (separator != '\n')
177 mmc_type = IS_SD(m) ? "SD" : "eMMC";
181 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
183 printf(" (%s)", mmc_type);
190 void print_mmc_devices(char separator) { }
193 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
195 struct blk_desc *bdesc;
196 struct udevice *bdev;
197 int ret, devnum = -1;
199 if (!mmc_get_ops(dev))
201 #ifndef CONFIG_SPL_BUILD
202 /* Use the fixed index with aliase node's index */
203 ret = dev_read_alias_seq(dev, &devnum);
204 debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum);
207 ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
208 devnum, 512, 0, &bdev);
210 debug("Cannot create block device\n");
213 bdesc = dev_get_uclass_platdata(bdev);
217 /* the following chunk was from mmc_register() */
219 /* Setup dsr related values */
221 mmc->dsr = 0xffffffff;
222 /* Setup the universal parts of the block interface just once */
223 bdesc->removable = 1;
225 /* setup initial part type */
226 bdesc->part_type = cfg->part_type;
232 int mmc_unbind(struct udevice *dev)
234 struct udevice *bdev;
236 device_find_first_child(dev, &bdev);
238 device_remove(bdev, DM_REMOVE_NORMAL);
245 static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
247 struct udevice *mmc_dev = dev_get_parent(bdev);
248 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
249 struct blk_desc *desc = dev_get_uclass_platdata(bdev);
251 if (desc->hwpart == hwpart)
254 if (mmc->part_config == MMCPART_NOAVAILABLE)
257 return mmc_switch_part(mmc, hwpart);
260 static int mmc_blk_probe(struct udevice *dev)
262 struct udevice *mmc_dev = dev_get_parent(dev);
263 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
264 struct mmc *mmc = upriv->mmc;
269 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
276 static const struct blk_ops mmc_blk_ops = {
278 #ifndef CONFIG_SPL_BUILD
282 .select_hwpart = mmc_select_hwpart,
285 U_BOOT_DRIVER(mmc_blk) = {
289 .probe = mmc_blk_probe,
291 #endif /* CONFIG_BLK */
293 U_BOOT_DRIVER(mmc) = {
298 UCLASS_DRIVER(mmc) = {
301 .flags = DM_UC_FLAG_SEQ_ALIAS,
302 .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),