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 void dm_mmc_send_init_stream(struct udevice *dev)
56 struct dm_mmc_ops *ops = mmc_get_ops(dev);
58 if (ops->send_init_stream)
59 ops->send_init_stream(dev);
62 void mmc_send_init_stream(struct mmc *mmc)
64 dm_mmc_send_init_stream(mmc->dev);
67 int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout)
69 struct dm_mmc_ops *ops = mmc_get_ops(dev);
73 return ops->wait_dat0(dev, state, timeout);
76 int mmc_wait_dat0(struct mmc *mmc, int state, int timeout)
78 return dm_mmc_wait_dat0(mmc->dev, state, timeout);
81 int dm_mmc_get_wp(struct udevice *dev)
83 struct dm_mmc_ops *ops = mmc_get_ops(dev);
87 return ops->get_wp(dev);
90 int mmc_getwp(struct mmc *mmc)
92 return dm_mmc_get_wp(mmc->dev);
95 int dm_mmc_get_cd(struct udevice *dev)
97 struct dm_mmc_ops *ops = mmc_get_ops(dev);
101 return ops->get_cd(dev);
104 int mmc_getcd(struct mmc *mmc)
106 return dm_mmc_get_cd(mmc->dev);
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);
123 int mmc_of_parse(const void *fdt, int node, struct mmc_config *cfg)
127 val = fdtdec_get_int(fdt, node, "bus-width", 1);
131 cfg->host_caps |= MMC_MODE_8BIT;
134 cfg->host_caps |= MMC_MODE_4BIT;
137 cfg->host_caps |= MMC_MODE_1BIT;
140 printf("error: %s invalid bus-width property %d\n",
141 fdt_get_name(fdt, node, NULL), val);
145 cfg->f_max = fdtdec_get_int(fdt, node, "max-frequency", 52000000);
147 if (fdtdec_get_bool(fdt, node, "cap-sd-highspeed"))
148 cfg->host_caps |= MMC_CAP(SD_HS);
149 if (fdtdec_get_bool(fdt, node, "cap-mmc-highspeed"))
150 cfg->host_caps |= MMC_CAP(MMC_HS);
151 if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr12"))
152 cfg->host_caps |= MMC_CAP(UHS_SDR12);
153 if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr25"))
154 cfg->host_caps |= MMC_CAP(UHS_SDR25);
155 if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr50"))
156 cfg->host_caps |= MMC_CAP(UHS_SDR50);
157 if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr104"))
158 cfg->host_caps |= MMC_CAP(UHS_SDR104);
159 if (fdtdec_get_bool(fdt, node, "sd-uhs-ddr50"))
160 cfg->host_caps |= MMC_CAP(UHS_DDR50);
161 if (fdtdec_get_bool(fdt, node, "mmc-ddr-1_8v"))
162 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
163 if (fdtdec_get_bool(fdt, node, "mmc-hs200-1_8v"))
164 cfg->host_caps |= MMC_CAP(MMC_HS_200);
169 struct mmc *mmc_get_mmc_dev(struct udevice *dev)
171 struct mmc_uclass_priv *upriv;
173 if (!device_active(dev))
175 upriv = dev_get_uclass_priv(dev);
179 #if CONFIG_IS_ENABLED(BLK)
180 struct mmc *find_mmc_device(int dev_num)
182 struct udevice *dev, *mmc_dev;
185 ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev);
188 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
189 printf("MMC Device %d not found\n", dev_num);
194 mmc_dev = dev_get_parent(dev);
196 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
201 int get_mmc_num(void)
203 return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
206 int mmc_get_next_devnum(void)
208 return blk_find_max_devnum(IF_TYPE_MMC);
211 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
213 struct blk_desc *desc;
216 device_find_first_child(mmc->dev, &dev);
219 desc = dev_get_uclass_platdata(dev);
224 void mmc_do_preinit(void)
230 ret = uclass_get(UCLASS_MMC, &uc);
233 uclass_foreach_dev(dev, uc) {
234 struct mmc *m = mmc_get_mmc_dev(dev);
238 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
239 mmc_set_preinit(m, 1);
246 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
247 void print_mmc_devices(char separator)
253 for (uclass_first_device(UCLASS_MMC, &dev);
255 uclass_next_device(&dev), first = false) {
256 struct mmc *m = mmc_get_mmc_dev(dev);
259 printf("%c", separator);
260 if (separator != '\n')
264 mmc_type = IS_SD(m) ? "SD" : "eMMC";
268 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
270 printf(" (%s)", mmc_type);
277 void print_mmc_devices(char separator) { }
280 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
282 struct blk_desc *bdesc;
283 struct udevice *bdev;
284 int ret, devnum = -1;
286 if (!mmc_get_ops(dev))
288 #ifndef CONFIG_SPL_BUILD
289 /* Use the fixed index with aliase node's index */
290 ret = dev_read_alias_seq(dev, &devnum);
291 debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum);
294 ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
295 devnum, 512, 0, &bdev);
297 debug("Cannot create block device\n");
300 bdesc = dev_get_uclass_platdata(bdev);
304 /* the following chunk was from mmc_register() */
306 /* Setup dsr related values */
308 mmc->dsr = 0xffffffff;
309 /* Setup the universal parts of the block interface just once */
310 bdesc->removable = 1;
312 /* setup initial part type */
313 bdesc->part_type = cfg->part_type;
319 int mmc_unbind(struct udevice *dev)
321 struct udevice *bdev;
323 device_find_first_child(dev, &bdev);
325 device_remove(bdev, DM_REMOVE_NORMAL);
332 static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
334 struct udevice *mmc_dev = dev_get_parent(bdev);
335 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
336 struct blk_desc *desc = dev_get_uclass_platdata(bdev);
338 if (desc->hwpart == hwpart)
341 if (mmc->part_config == MMCPART_NOAVAILABLE)
344 return mmc_switch_part(mmc, hwpart);
347 static int mmc_blk_probe(struct udevice *dev)
349 struct udevice *mmc_dev = dev_get_parent(dev);
350 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
351 struct mmc *mmc = upriv->mmc;
356 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
363 static const struct blk_ops mmc_blk_ops = {
365 #ifndef CONFIG_SPL_BUILD
369 .select_hwpart = mmc_select_hwpart,
372 U_BOOT_DRIVER(mmc_blk) = {
376 .probe = mmc_blk_probe,
378 #endif /* CONFIG_BLK */
380 U_BOOT_DRIVER(mmc) = {
385 UCLASS_DRIVER(mmc) = {
388 .flags = DM_UC_FLAG_SEQ_ALIAS,
389 .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),