1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
11 #include <dm/device-internal.h>
12 #include <dm/device_compat.h>
14 #include <linux/compat.h>
15 #include "mmc_private.h"
17 int dm_mmc_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
19 struct dm_mmc_ops *ops = mmc_get_ops(dev);
20 struct mmc *mmc = mmc_get_mmc_dev(dev);
23 return ops->get_b_max(dev, dst, blkcnt);
25 return mmc->cfg->b_max;
28 int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
30 return dm_mmc_get_b_max(mmc->dev, dst, blkcnt);
33 int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
34 struct mmc_data *data)
36 struct mmc *mmc = mmc_get_mmc_dev(dev);
37 struct dm_mmc_ops *ops = mmc_get_ops(dev);
40 mmmc_trace_before_send(mmc, cmd);
42 ret = ops->send_cmd(dev, cmd, data);
45 mmmc_trace_after_send(mmc, cmd, ret);
50 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
52 return dm_mmc_send_cmd(mmc->dev, cmd, data);
55 int dm_mmc_set_ios(struct udevice *dev)
57 struct dm_mmc_ops *ops = mmc_get_ops(dev);
61 return ops->set_ios(dev);
64 int mmc_set_ios(struct mmc *mmc)
66 return dm_mmc_set_ios(mmc->dev);
69 int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
71 struct dm_mmc_ops *ops = mmc_get_ops(dev);
75 return ops->wait_dat0(dev, state, timeout_us);
78 int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
80 return dm_mmc_wait_dat0(mmc->dev, state, timeout_us);
83 int dm_mmc_get_wp(struct udevice *dev)
85 struct dm_mmc_ops *ops = mmc_get_ops(dev);
89 return ops->get_wp(dev);
92 int mmc_getwp(struct mmc *mmc)
94 return dm_mmc_get_wp(mmc->dev);
97 int dm_mmc_get_cd(struct udevice *dev)
99 struct dm_mmc_ops *ops = mmc_get_ops(dev);
103 return ops->get_cd(dev);
106 int mmc_getcd(struct mmc *mmc)
108 return dm_mmc_get_cd(mmc->dev);
111 #ifdef MMC_SUPPORTS_TUNING
112 int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
114 struct dm_mmc_ops *ops = mmc_get_ops(dev);
116 if (!ops->execute_tuning)
118 return ops->execute_tuning(dev, opcode);
121 int mmc_execute_tuning(struct mmc *mmc, uint opcode)
123 return dm_mmc_execute_tuning(mmc->dev, opcode);
127 #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
128 int dm_mmc_set_enhanced_strobe(struct udevice *dev)
130 struct dm_mmc_ops *ops = mmc_get_ops(dev);
132 if (ops->set_enhanced_strobe)
133 return ops->set_enhanced_strobe(dev);
138 int mmc_set_enhanced_strobe(struct mmc *mmc)
140 return dm_mmc_set_enhanced_strobe(mmc->dev);
144 int dm_mmc_host_power_cycle(struct udevice *dev)
146 struct dm_mmc_ops *ops = mmc_get_ops(dev);
148 if (ops->host_power_cycle)
149 return ops->host_power_cycle(dev);
153 int mmc_host_power_cycle(struct mmc *mmc)
155 return dm_mmc_host_power_cycle(mmc->dev);
158 int dm_mmc_deferred_probe(struct udevice *dev)
160 struct dm_mmc_ops *ops = mmc_get_ops(dev);
162 if (ops->deferred_probe)
163 return ops->deferred_probe(dev);
168 int mmc_deferred_probe(struct mmc *mmc)
170 return dm_mmc_deferred_probe(mmc->dev);
173 int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
177 val = dev_read_u32_default(dev, "bus-width", 1);
181 cfg->host_caps |= MMC_MODE_8BIT;
184 cfg->host_caps |= MMC_MODE_4BIT;
187 cfg->host_caps |= MMC_MODE_1BIT;
190 dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
194 /* f_max is obtained from the optional "max-frequency" property */
195 dev_read_u32(dev, "max-frequency", &cfg->f_max);
197 if (dev_read_bool(dev, "cap-sd-highspeed"))
198 cfg->host_caps |= MMC_CAP(SD_HS);
199 if (dev_read_bool(dev, "cap-mmc-highspeed"))
200 cfg->host_caps |= MMC_CAP(MMC_HS);
201 if (dev_read_bool(dev, "sd-uhs-sdr12"))
202 cfg->host_caps |= MMC_CAP(UHS_SDR12);
203 if (dev_read_bool(dev, "sd-uhs-sdr25"))
204 cfg->host_caps |= MMC_CAP(UHS_SDR25);
205 if (dev_read_bool(dev, "sd-uhs-sdr50"))
206 cfg->host_caps |= MMC_CAP(UHS_SDR50);
207 if (dev_read_bool(dev, "sd-uhs-sdr104"))
208 cfg->host_caps |= MMC_CAP(UHS_SDR104);
209 if (dev_read_bool(dev, "sd-uhs-ddr50"))
210 cfg->host_caps |= MMC_CAP(UHS_DDR50);
211 if (dev_read_bool(dev, "mmc-ddr-1_8v"))
212 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
213 if (dev_read_bool(dev, "mmc-ddr-1_2v"))
214 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
215 if (dev_read_bool(dev, "mmc-hs200-1_8v"))
216 cfg->host_caps |= MMC_CAP(MMC_HS_200);
217 if (dev_read_bool(dev, "mmc-hs200-1_2v"))
218 cfg->host_caps |= MMC_CAP(MMC_HS_200);
219 if (dev_read_bool(dev, "mmc-hs400-1_8v"))
220 cfg->host_caps |= MMC_CAP(MMC_HS_400);
221 if (dev_read_bool(dev, "mmc-hs400-1_2v"))
222 cfg->host_caps |= MMC_CAP(MMC_HS_400);
223 if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe"))
224 cfg->host_caps |= MMC_CAP(MMC_HS_400_ES);
226 if (dev_read_bool(dev, "non-removable")) {
227 cfg->host_caps |= MMC_CAP_NONREMOVABLE;
229 if (dev_read_bool(dev, "cd-inverted"))
230 cfg->host_caps |= MMC_CAP_CD_ACTIVE_HIGH;
231 if (dev_read_bool(dev, "broken-cd"))
232 cfg->host_caps |= MMC_CAP_NEEDS_POLL;
235 if (dev_read_bool(dev, "no-1-8-v")) {
236 cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 |
237 MMC_MODE_HS400 | MMC_MODE_HS400_ES);
243 struct mmc *mmc_get_mmc_dev(const struct udevice *dev)
245 struct mmc_uclass_priv *upriv;
247 if (!device_active(dev))
249 upriv = dev_get_uclass_priv(dev);
253 #if CONFIG_IS_ENABLED(BLK)
254 struct mmc *find_mmc_device(int dev_num)
256 struct udevice *dev, *mmc_dev;
259 ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev);
262 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
263 printf("MMC Device %d not found\n", dev_num);
268 mmc_dev = dev_get_parent(dev);
270 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
275 int get_mmc_num(void)
277 return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
280 int mmc_get_next_devnum(void)
282 return blk_find_max_devnum(IF_TYPE_MMC);
285 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
287 struct blk_desc *desc;
290 device_find_first_child(mmc->dev, &dev);
293 desc = dev_get_uclass_platdata(dev);
298 void mmc_do_preinit(void)
304 ret = uclass_get(UCLASS_MMC, &uc);
307 uclass_foreach_dev(dev, uc) {
308 struct mmc *m = mmc_get_mmc_dev(dev);
312 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
313 mmc_set_preinit(m, 1);
320 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
321 void print_mmc_devices(char separator)
327 for (uclass_first_device(UCLASS_MMC, &dev);
329 uclass_next_device(&dev), first = false) {
330 struct mmc *m = mmc_get_mmc_dev(dev);
333 printf("%c", separator);
334 if (separator != '\n')
338 mmc_type = IS_SD(m) ? "SD" : "eMMC";
342 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
344 printf(" (%s)", mmc_type);
351 void print_mmc_devices(char separator) { }
354 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
356 struct blk_desc *bdesc;
357 struct udevice *bdev;
358 int ret, devnum = -1;
360 if (!mmc_get_ops(dev))
362 #ifndef CONFIG_SPL_BUILD
363 /* Use the fixed index with aliase node's index */
364 ret = dev_read_alias_seq(dev, &devnum);
365 debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum);
368 ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
369 devnum, 512, 0, &bdev);
371 debug("Cannot create block device\n");
374 bdesc = dev_get_uclass_platdata(bdev);
378 /* the following chunk was from mmc_register() */
380 /* Setup dsr related values */
382 mmc->dsr = 0xffffffff;
383 /* Setup the universal parts of the block interface just once */
384 bdesc->removable = 1;
386 /* setup initial part type */
387 bdesc->part_type = cfg->part_type;
393 int mmc_unbind(struct udevice *dev)
395 struct udevice *bdev;
397 device_find_first_child(dev, &bdev);
399 device_remove(bdev, DM_REMOVE_NORMAL);
406 static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
408 struct udevice *mmc_dev = dev_get_parent(bdev);
409 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
410 struct blk_desc *desc = dev_get_uclass_platdata(bdev);
413 if (desc->hwpart == hwpart)
416 if (mmc->part_config == MMCPART_NOAVAILABLE)
419 ret = mmc_switch_part(mmc, hwpart);
421 blkcache_invalidate(desc->if_type, desc->devnum);
426 static int mmc_blk_probe(struct udevice *dev)
428 struct udevice *mmc_dev = dev_get_parent(dev);
429 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
430 struct mmc *mmc = upriv->mmc;
435 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
442 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
443 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
444 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
445 static int mmc_blk_remove(struct udevice *dev)
447 struct udevice *mmc_dev = dev_get_parent(dev);
448 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
449 struct mmc *mmc = upriv->mmc;
451 return mmc_deinit(mmc);
455 static const struct blk_ops mmc_blk_ops = {
457 #if CONFIG_IS_ENABLED(MMC_WRITE)
461 .select_hwpart = mmc_select_hwpart,
464 U_BOOT_DRIVER(mmc_blk) = {
468 .probe = mmc_blk_probe,
469 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
470 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
471 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
472 .remove = mmc_blk_remove,
473 .flags = DM_FLAG_OS_PREPARE,
476 #endif /* CONFIG_BLK */
479 UCLASS_DRIVER(mmc) = {
482 .flags = DM_UC_FLAG_SEQ_ALIAS,
483 .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),