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>
13 #include "mmc_private.h"
15 int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
16 struct mmc_data *data)
18 struct mmc *mmc = mmc_get_mmc_dev(dev);
19 struct dm_mmc_ops *ops = mmc_get_ops(dev);
22 mmmc_trace_before_send(mmc, cmd);
24 ret = ops->send_cmd(dev, cmd, data);
27 mmmc_trace_after_send(mmc, cmd, ret);
32 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
34 return dm_mmc_send_cmd(mmc->dev, cmd, data);
37 int dm_mmc_set_ios(struct udevice *dev)
39 struct dm_mmc_ops *ops = mmc_get_ops(dev);
43 return ops->set_ios(dev);
46 int mmc_set_ios(struct mmc *mmc)
48 return dm_mmc_set_ios(mmc->dev);
51 void dm_mmc_send_init_stream(struct udevice *dev)
53 struct dm_mmc_ops *ops = mmc_get_ops(dev);
55 if (ops->send_init_stream)
56 ops->send_init_stream(dev);
59 void mmc_send_init_stream(struct mmc *mmc)
61 dm_mmc_send_init_stream(mmc->dev);
64 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
65 int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout)
67 struct dm_mmc_ops *ops = mmc_get_ops(dev);
71 return ops->wait_dat0(dev, state, timeout);
74 int mmc_wait_dat0(struct mmc *mmc, int state, int timeout)
76 return dm_mmc_wait_dat0(mmc->dev, state, timeout);
80 int dm_mmc_get_wp(struct udevice *dev)
82 struct dm_mmc_ops *ops = mmc_get_ops(dev);
86 return ops->get_wp(dev);
89 int mmc_getwp(struct mmc *mmc)
91 return dm_mmc_get_wp(mmc->dev);
94 int dm_mmc_get_cd(struct udevice *dev)
96 struct dm_mmc_ops *ops = mmc_get_ops(dev);
100 return ops->get_cd(dev);
103 int mmc_getcd(struct mmc *mmc)
105 return dm_mmc_get_cd(mmc->dev);
108 #ifdef MMC_SUPPORTS_TUNING
109 int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
111 struct dm_mmc_ops *ops = mmc_get_ops(dev);
113 if (!ops->execute_tuning)
115 return ops->execute_tuning(dev, opcode);
118 int mmc_execute_tuning(struct mmc *mmc, uint opcode)
120 return dm_mmc_execute_tuning(mmc->dev, opcode);
124 int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
128 val = dev_read_u32_default(dev, "bus-width", 1);
132 cfg->host_caps |= MMC_MODE_8BIT;
135 cfg->host_caps |= MMC_MODE_4BIT;
138 cfg->host_caps |= MMC_MODE_1BIT;
141 dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
145 /* f_max is obtained from the optional "max-frequency" property */
146 dev_read_u32(dev, "max-frequency", &cfg->f_max);
148 if (dev_read_bool(dev, "cap-sd-highspeed"))
149 cfg->host_caps |= MMC_CAP(SD_HS);
150 if (dev_read_bool(dev, "cap-mmc-highspeed"))
151 cfg->host_caps |= MMC_CAP(MMC_HS);
152 if (dev_read_bool(dev, "sd-uhs-sdr12"))
153 cfg->host_caps |= MMC_CAP(UHS_SDR12);
154 if (dev_read_bool(dev, "sd-uhs-sdr25"))
155 cfg->host_caps |= MMC_CAP(UHS_SDR25);
156 if (dev_read_bool(dev, "sd-uhs-sdr50"))
157 cfg->host_caps |= MMC_CAP(UHS_SDR50);
158 if (dev_read_bool(dev, "sd-uhs-sdr104"))
159 cfg->host_caps |= MMC_CAP(UHS_SDR104);
160 if (dev_read_bool(dev, "sd-uhs-ddr50"))
161 cfg->host_caps |= MMC_CAP(UHS_DDR50);
162 if (dev_read_bool(dev, "mmc-ddr-1_8v"))
163 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
164 if (dev_read_bool(dev, "mmc-ddr-1_2v"))
165 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
166 if (dev_read_bool(dev, "mmc-hs200-1_8v"))
167 cfg->host_caps |= MMC_CAP(MMC_HS_200);
168 if (dev_read_bool(dev, "mmc-hs200-1_2v"))
169 cfg->host_caps |= MMC_CAP(MMC_HS_200);
174 struct mmc *mmc_get_mmc_dev(struct udevice *dev)
176 struct mmc_uclass_priv *upriv;
178 if (!device_active(dev))
180 upriv = dev_get_uclass_priv(dev);
184 #if CONFIG_IS_ENABLED(BLK)
185 struct mmc *find_mmc_device(int dev_num)
187 struct udevice *dev, *mmc_dev;
190 ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev);
193 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
194 printf("MMC Device %d not found\n", dev_num);
199 mmc_dev = dev_get_parent(dev);
201 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
206 int get_mmc_num(void)
208 return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
211 int mmc_get_next_devnum(void)
213 return blk_find_max_devnum(IF_TYPE_MMC);
216 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
218 struct blk_desc *desc;
221 device_find_first_child(mmc->dev, &dev);
224 desc = dev_get_uclass_platdata(dev);
229 void mmc_do_preinit(void)
235 ret = uclass_get(UCLASS_MMC, &uc);
238 uclass_foreach_dev(dev, uc) {
239 struct mmc *m = mmc_get_mmc_dev(dev);
243 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
244 mmc_set_preinit(m, 1);
251 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
252 void print_mmc_devices(char separator)
258 for (uclass_first_device(UCLASS_MMC, &dev);
260 uclass_next_device(&dev), first = false) {
261 struct mmc *m = mmc_get_mmc_dev(dev);
264 printf("%c", separator);
265 if (separator != '\n')
269 mmc_type = IS_SD(m) ? "SD" : "eMMC";
273 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
275 printf(" (%s)", mmc_type);
282 void print_mmc_devices(char separator) { }
285 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
287 struct blk_desc *bdesc;
288 struct udevice *bdev;
289 int ret, devnum = -1;
291 if (!mmc_get_ops(dev))
293 #ifndef CONFIG_SPL_BUILD
294 /* Use the fixed index with aliase node's index */
295 ret = dev_read_alias_seq(dev, &devnum);
296 debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum);
299 ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
300 devnum, 512, 0, &bdev);
302 debug("Cannot create block device\n");
305 bdesc = dev_get_uclass_platdata(bdev);
309 /* the following chunk was from mmc_register() */
311 /* Setup dsr related values */
313 mmc->dsr = 0xffffffff;
314 /* Setup the universal parts of the block interface just once */
315 bdesc->removable = 1;
317 /* setup initial part type */
318 bdesc->part_type = cfg->part_type;
324 int mmc_unbind(struct udevice *dev)
326 struct udevice *bdev;
328 device_find_first_child(dev, &bdev);
330 device_remove(bdev, DM_REMOVE_NORMAL);
337 static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
339 struct udevice *mmc_dev = dev_get_parent(bdev);
340 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
341 struct blk_desc *desc = dev_get_uclass_platdata(bdev);
343 if (desc->hwpart == hwpart)
346 if (mmc->part_config == MMCPART_NOAVAILABLE)
349 return mmc_switch_part(mmc, hwpart);
352 static int mmc_blk_probe(struct udevice *dev)
354 struct udevice *mmc_dev = dev_get_parent(dev);
355 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
356 struct mmc *mmc = upriv->mmc;
361 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
368 static const struct blk_ops mmc_blk_ops = {
370 #if CONFIG_IS_ENABLED(MMC_WRITE)
374 .select_hwpart = mmc_select_hwpart,
377 U_BOOT_DRIVER(mmc_blk) = {
381 .probe = mmc_blk_probe,
383 #endif /* CONFIG_BLK */
385 U_BOOT_DRIVER(mmc) = {
390 UCLASS_DRIVER(mmc) = {
393 .flags = DM_UC_FLAG_SEQ_ALIAS,
394 .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),