1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
8 #define LOG_CATEGORY UCLASS_MMC
15 #include <dm/device-internal.h>
16 #include <dm/device_compat.h>
18 #include <linux/compat.h>
19 #include "mmc_private.h"
21 static int dm_mmc_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
23 struct dm_mmc_ops *ops = mmc_get_ops(dev);
24 struct mmc *mmc = mmc_get_mmc_dev(dev);
27 return ops->get_b_max(dev, dst, blkcnt);
29 return mmc->cfg->b_max;
32 int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
34 return dm_mmc_get_b_max(mmc->dev, dst, blkcnt);
37 static int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
38 struct mmc_data *data)
40 struct mmc *mmc = mmc_get_mmc_dev(dev);
41 struct dm_mmc_ops *ops = mmc_get_ops(dev);
44 mmmc_trace_before_send(mmc, cmd);
46 ret = ops->send_cmd(dev, cmd, data);
49 mmmc_trace_after_send(mmc, cmd, ret);
54 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
56 return dm_mmc_send_cmd(mmc->dev, cmd, data);
59 static int dm_mmc_set_ios(struct udevice *dev)
61 struct dm_mmc_ops *ops = mmc_get_ops(dev);
65 return ops->set_ios(dev);
68 int mmc_set_ios(struct mmc *mmc)
70 return dm_mmc_set_ios(mmc->dev);
73 static int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
75 struct dm_mmc_ops *ops = mmc_get_ops(dev);
79 return ops->wait_dat0(dev, state, timeout_us);
82 int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
84 return dm_mmc_wait_dat0(mmc->dev, state, timeout_us);
87 static int dm_mmc_get_wp(struct udevice *dev)
89 struct dm_mmc_ops *ops = mmc_get_ops(dev);
93 return ops->get_wp(dev);
96 int mmc_getwp(struct mmc *mmc)
98 return dm_mmc_get_wp(mmc->dev);
101 static int dm_mmc_get_cd(struct udevice *dev)
103 struct dm_mmc_ops *ops = mmc_get_ops(dev);
107 return ops->get_cd(dev);
110 int mmc_getcd(struct mmc *mmc)
112 return dm_mmc_get_cd(mmc->dev);
115 #ifdef MMC_SUPPORTS_TUNING
116 static int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
118 struct dm_mmc_ops *ops = mmc_get_ops(dev);
120 if (!ops->execute_tuning)
122 return ops->execute_tuning(dev, opcode);
125 int mmc_execute_tuning(struct mmc *mmc, uint opcode)
127 return dm_mmc_execute_tuning(mmc->dev, opcode);
131 #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
132 static int dm_mmc_set_enhanced_strobe(struct udevice *dev)
134 struct dm_mmc_ops *ops = mmc_get_ops(dev);
136 if (ops->set_enhanced_strobe)
137 return ops->set_enhanced_strobe(dev);
142 int mmc_set_enhanced_strobe(struct mmc *mmc)
144 return dm_mmc_set_enhanced_strobe(mmc->dev);
148 static int dm_mmc_hs400_prepare_ddr(struct udevice *dev)
150 struct dm_mmc_ops *ops = mmc_get_ops(dev);
152 if (ops->hs400_prepare_ddr)
153 return ops->hs400_prepare_ddr(dev);
158 int mmc_hs400_prepare_ddr(struct mmc *mmc)
160 return dm_mmc_hs400_prepare_ddr(mmc->dev);
163 static int dm_mmc_host_power_cycle(struct udevice *dev)
165 struct dm_mmc_ops *ops = mmc_get_ops(dev);
167 if (ops->host_power_cycle)
168 return ops->host_power_cycle(dev);
172 int mmc_host_power_cycle(struct mmc *mmc)
174 return dm_mmc_host_power_cycle(mmc->dev);
177 static int dm_mmc_deferred_probe(struct udevice *dev)
179 struct dm_mmc_ops *ops = mmc_get_ops(dev);
181 if (ops->deferred_probe)
182 return ops->deferred_probe(dev);
187 int mmc_deferred_probe(struct mmc *mmc)
189 return dm_mmc_deferred_probe(mmc->dev);
192 static int dm_mmc_reinit(struct udevice *dev)
194 struct dm_mmc_ops *ops = mmc_get_ops(dev);
197 return ops->reinit(dev);
202 int mmc_reinit(struct mmc *mmc)
204 return dm_mmc_reinit(mmc->dev);
207 int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
211 val = dev_read_u32_default(dev, "bus-width", 1);
215 cfg->host_caps |= MMC_MODE_8BIT;
218 cfg->host_caps |= MMC_MODE_4BIT;
221 cfg->host_caps |= MMC_MODE_1BIT;
224 dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
228 /* f_max is obtained from the optional "max-frequency" property */
229 dev_read_u32(dev, "max-frequency", &cfg->f_max);
231 if (dev_read_bool(dev, "cap-sd-highspeed"))
232 cfg->host_caps |= MMC_CAP(SD_HS);
233 if (dev_read_bool(dev, "cap-mmc-highspeed"))
234 cfg->host_caps |= MMC_CAP(MMC_HS) | MMC_CAP(MMC_HS_52);
235 if (dev_read_bool(dev, "sd-uhs-sdr12"))
236 cfg->host_caps |= MMC_CAP(UHS_SDR12);
237 if (dev_read_bool(dev, "sd-uhs-sdr25"))
238 cfg->host_caps |= MMC_CAP(UHS_SDR25);
239 if (dev_read_bool(dev, "sd-uhs-sdr50"))
240 cfg->host_caps |= MMC_CAP(UHS_SDR50);
241 if (dev_read_bool(dev, "sd-uhs-sdr104"))
242 cfg->host_caps |= MMC_CAP(UHS_SDR104);
243 if (dev_read_bool(dev, "sd-uhs-ddr50"))
244 cfg->host_caps |= MMC_CAP(UHS_DDR50);
245 if (dev_read_bool(dev, "mmc-ddr-1_8v"))
246 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
247 if (dev_read_bool(dev, "mmc-ddr-1_2v"))
248 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
249 if (dev_read_bool(dev, "mmc-hs200-1_8v"))
250 cfg->host_caps |= MMC_CAP(MMC_HS_200);
251 if (dev_read_bool(dev, "mmc-hs200-1_2v"))
252 cfg->host_caps |= MMC_CAP(MMC_HS_200);
253 if (dev_read_bool(dev, "mmc-hs400-1_8v"))
254 cfg->host_caps |= MMC_CAP(MMC_HS_400);
255 if (dev_read_bool(dev, "mmc-hs400-1_2v"))
256 cfg->host_caps |= MMC_CAP(MMC_HS_400);
257 if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe"))
258 cfg->host_caps |= MMC_CAP(MMC_HS_400_ES);
260 if (dev_read_bool(dev, "non-removable")) {
261 cfg->host_caps |= MMC_CAP_NONREMOVABLE;
263 if (dev_read_bool(dev, "cd-inverted"))
264 cfg->host_caps |= MMC_CAP_CD_ACTIVE_HIGH;
265 if (dev_read_bool(dev, "broken-cd"))
266 cfg->host_caps |= MMC_CAP_NEEDS_POLL;
269 if (dev_read_bool(dev, "no-1-8-v")) {
270 cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 |
271 MMC_MODE_HS400 | MMC_MODE_HS400_ES);
277 struct mmc *mmc_get_mmc_dev(const struct udevice *dev)
279 struct mmc_uclass_priv *upriv;
281 if (!device_active(dev))
283 upriv = dev_get_uclass_priv(dev);
287 #if CONFIG_IS_ENABLED(BLK)
288 struct mmc *find_mmc_device(int dev_num)
290 struct udevice *dev, *mmc_dev;
293 ret = blk_find_device(UCLASS_MMC, dev_num, &dev);
296 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
297 printf("MMC Device %d not found\n", dev_num);
302 mmc_dev = dev_get_parent(dev);
304 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
309 int get_mmc_num(void)
311 return max((blk_find_max_devnum(UCLASS_MMC) + 1), 0);
314 int mmc_get_next_devnum(void)
316 return blk_find_max_devnum(UCLASS_MMC);
319 int mmc_get_blk(struct udevice *dev, struct udevice **blkp)
324 device_find_first_child_by_uclass(dev, UCLASS_BLK, &blk);
325 ret = device_probe(blk);
333 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
335 struct blk_desc *desc;
338 device_find_first_child_by_uclass(mmc->dev, UCLASS_BLK, &dev);
341 desc = dev_get_uclass_plat(dev);
346 void mmc_do_preinit(void)
352 ret = uclass_get(UCLASS_MMC, &uc);
355 uclass_foreach_dev(dev, uc) {
356 struct mmc *m = mmc_get_mmc_dev(dev);
361 m->user_speed_mode = MMC_MODES_END; /* Initialising user set speed mode */
368 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
369 void print_mmc_devices(char separator)
375 for (uclass_first_device(UCLASS_MMC, &dev);
377 uclass_next_device(&dev), first = false) {
378 struct mmc *m = mmc_get_mmc_dev(dev);
381 printf("%c", separator);
382 if (separator != '\n')
386 mmc_type = IS_SD(m) ? "SD" : "eMMC";
390 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
392 printf(" (%s)", mmc_type);
399 void print_mmc_devices(char separator) { }
402 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
404 struct blk_desc *bdesc;
405 struct udevice *bdev;
408 if (!mmc_get_ops(dev))
411 /* Use the fixed index with aliases node's index */
412 debug("%s: alias devnum=%d\n", __func__, dev_seq(dev));
414 ret = blk_create_devicef(dev, "mmc_blk", "blk", UCLASS_MMC,
415 dev_seq(dev), 512, 0, &bdev);
417 debug("Cannot create block device\n");
420 bdesc = dev_get_uclass_plat(bdev);
424 ret = bootdev_setup_for_sibling_blk(bdev, "mmc_bootdev");
426 return log_msg_ret("bootdev", ret);
428 /* the following chunk was from mmc_register() */
430 /* Setup dsr related values */
432 mmc->dsr = 0xffffffff;
433 /* Setup the universal parts of the block interface just once */
434 bdesc->removable = 1;
436 /* setup initial part type */
437 bdesc->part_type = cfg->part_type;
439 mmc->user_speed_mode = MMC_MODES_END;
443 int mmc_unbind(struct udevice *dev)
445 struct udevice *bdev;
448 device_find_first_child_by_uclass(dev, UCLASS_BLK, &bdev);
450 device_remove(bdev, DM_REMOVE_NORMAL);
453 ret = bootdev_unbind_dev(dev);
455 return log_msg_ret("bootdev", ret);
460 static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
462 struct udevice *mmc_dev = dev_get_parent(bdev);
463 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
464 struct blk_desc *desc = dev_get_uclass_plat(bdev);
467 if (desc->hwpart == hwpart)
470 if (mmc->part_config == MMCPART_NOAVAILABLE)
473 ret = mmc_switch_part(mmc, hwpart);
475 blkcache_invalidate(desc->uclass_id, desc->devnum);
480 static int mmc_blk_probe(struct udevice *dev)
482 struct udevice *mmc_dev = dev_get_parent(dev);
483 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
484 struct mmc *mmc = upriv->mmc;
489 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
493 ret = device_probe(dev);
495 debug("Probing %s failed (err=%d)\n", dev->name, ret);
497 if (CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) ||
498 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) ||
499 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT))
508 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
509 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
510 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
511 static int mmc_blk_remove(struct udevice *dev)
513 struct udevice *mmc_dev = dev_get_parent(dev);
514 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
515 struct mmc *mmc = upriv->mmc;
517 return mmc_deinit(mmc);
521 static const struct blk_ops mmc_blk_ops = {
523 #if CONFIG_IS_ENABLED(MMC_WRITE)
527 .select_hwpart = mmc_select_hwpart,
530 U_BOOT_DRIVER(mmc_blk) = {
534 .probe = mmc_blk_probe,
535 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
536 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
537 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
538 .remove = mmc_blk_remove,
539 .flags = DM_FLAG_OS_PREPARE,
542 #endif /* CONFIG_BLK */
545 UCLASS_DRIVER(mmc) = {
548 .flags = DM_UC_FLAG_SEQ_ALIAS,
549 .per_device_auto = sizeof(struct mmc_uclass_priv),