xref: /openbmc/u-boot/drivers/mmc/mmc-uclass.c (revision c4d660d4)
1e7ecf7cbSSimon Glass /*
2e7ecf7cbSSimon Glass  * Copyright (C) 2015 Google, Inc
3e7ecf7cbSSimon Glass  * Written by Simon Glass <sjg@chromium.org>
4e7ecf7cbSSimon Glass  *
5e7ecf7cbSSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
6e7ecf7cbSSimon Glass  */
7e7ecf7cbSSimon Glass 
8e7ecf7cbSSimon Glass #include <common.h>
9e7ecf7cbSSimon Glass #include <mmc.h>
10e7ecf7cbSSimon Glass #include <dm.h>
11eede897eSSimon Glass #include <dm/device-internal.h>
12e7ecf7cbSSimon Glass #include <dm/lists.h>
13e7ecf7cbSSimon Glass #include <dm/root.h>
14eede897eSSimon Glass #include "mmc_private.h"
15e7ecf7cbSSimon Glass 
1602ad33aaSJaehoon Chung DECLARE_GLOBAL_DATA_PTR;
1702ad33aaSJaehoon Chung 
18*c4d660d4SSimon Glass #if CONFIG_IS_ENABLED(DM_MMC_OPS)
198ca51e51SSimon Glass int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
208ca51e51SSimon Glass 		    struct mmc_data *data)
218ca51e51SSimon Glass {
228ca51e51SSimon Glass 	struct mmc *mmc = mmc_get_mmc_dev(dev);
238ca51e51SSimon Glass 	struct dm_mmc_ops *ops = mmc_get_ops(dev);
248ca51e51SSimon Glass 	int ret;
258ca51e51SSimon Glass 
268ca51e51SSimon Glass 	mmmc_trace_before_send(mmc, cmd);
278ca51e51SSimon Glass 	if (ops->send_cmd)
288ca51e51SSimon Glass 		ret = ops->send_cmd(dev, cmd, data);
298ca51e51SSimon Glass 	else
308ca51e51SSimon Glass 		ret = -ENOSYS;
318ca51e51SSimon Glass 	mmmc_trace_after_send(mmc, cmd, ret);
328ca51e51SSimon Glass 
338ca51e51SSimon Glass 	return ret;
348ca51e51SSimon Glass }
358ca51e51SSimon Glass 
368ca51e51SSimon Glass int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
378ca51e51SSimon Glass {
388ca51e51SSimon Glass 	return dm_mmc_send_cmd(mmc->dev, cmd, data);
398ca51e51SSimon Glass }
408ca51e51SSimon Glass 
418ca51e51SSimon Glass int dm_mmc_set_ios(struct udevice *dev)
428ca51e51SSimon Glass {
438ca51e51SSimon Glass 	struct dm_mmc_ops *ops = mmc_get_ops(dev);
448ca51e51SSimon Glass 
458ca51e51SSimon Glass 	if (!ops->set_ios)
468ca51e51SSimon Glass 		return -ENOSYS;
478ca51e51SSimon Glass 	return ops->set_ios(dev);
488ca51e51SSimon Glass }
498ca51e51SSimon Glass 
508ca51e51SSimon Glass int mmc_set_ios(struct mmc *mmc)
518ca51e51SSimon Glass {
528ca51e51SSimon Glass 	return dm_mmc_set_ios(mmc->dev);
538ca51e51SSimon Glass }
548ca51e51SSimon Glass 
558ca51e51SSimon Glass int dm_mmc_get_wp(struct udevice *dev)
568ca51e51SSimon Glass {
578ca51e51SSimon Glass 	struct dm_mmc_ops *ops = mmc_get_ops(dev);
588ca51e51SSimon Glass 
598ca51e51SSimon Glass 	if (!ops->get_wp)
608ca51e51SSimon Glass 		return -ENOSYS;
618ca51e51SSimon Glass 	return ops->get_wp(dev);
628ca51e51SSimon Glass }
638ca51e51SSimon Glass 
648ca51e51SSimon Glass int mmc_getwp(struct mmc *mmc)
658ca51e51SSimon Glass {
668ca51e51SSimon Glass 	return dm_mmc_get_wp(mmc->dev);
678ca51e51SSimon Glass }
688ca51e51SSimon Glass 
698ca51e51SSimon Glass int dm_mmc_get_cd(struct udevice *dev)
708ca51e51SSimon Glass {
718ca51e51SSimon Glass 	struct dm_mmc_ops *ops = mmc_get_ops(dev);
728ca51e51SSimon Glass 
738ca51e51SSimon Glass 	if (!ops->get_cd)
748ca51e51SSimon Glass 		return -ENOSYS;
758ca51e51SSimon Glass 	return ops->get_cd(dev);
768ca51e51SSimon Glass }
778ca51e51SSimon Glass 
788ca51e51SSimon Glass int mmc_getcd(struct mmc *mmc)
798ca51e51SSimon Glass {
808ca51e51SSimon Glass 	return dm_mmc_get_cd(mmc->dev);
818ca51e51SSimon Glass }
828ca51e51SSimon Glass #endif
838ca51e51SSimon Glass 
84e7ecf7cbSSimon Glass struct mmc *mmc_get_mmc_dev(struct udevice *dev)
85e7ecf7cbSSimon Glass {
86e7ecf7cbSSimon Glass 	struct mmc_uclass_priv *upriv;
87e7ecf7cbSSimon Glass 
88e7ecf7cbSSimon Glass 	if (!device_active(dev))
89e7ecf7cbSSimon Glass 		return NULL;
90e7ecf7cbSSimon Glass 	upriv = dev_get_uclass_priv(dev);
91e7ecf7cbSSimon Glass 	return upriv->mmc;
92e7ecf7cbSSimon Glass }
93e7ecf7cbSSimon Glass 
94*c4d660d4SSimon Glass #if CONFIG_IS_ENABLED(BLK)
958ef761edSSimon Glass struct mmc *find_mmc_device(int dev_num)
968ef761edSSimon Glass {
978ef761edSSimon Glass 	struct udevice *dev, *mmc_dev;
988ef761edSSimon Glass 	int ret;
998ef761edSSimon Glass 
10097525647SSimon Glass 	ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev);
1018ef761edSSimon Glass 
1028ef761edSSimon Glass 	if (ret) {
1038ef761edSSimon Glass #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1048ef761edSSimon Glass 		printf("MMC Device %d not found\n", dev_num);
1058ef761edSSimon Glass #endif
1068ef761edSSimon Glass 		return NULL;
1078ef761edSSimon Glass 	}
1088ef761edSSimon Glass 
1098ef761edSSimon Glass 	mmc_dev = dev_get_parent(dev);
1108ef761edSSimon Glass 
11197525647SSimon Glass 	struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
11297525647SSimon Glass 
11397525647SSimon Glass 	return mmc;
1148ef761edSSimon Glass }
1158ef761edSSimon Glass 
1168ef761edSSimon Glass int get_mmc_num(void)
1178ef761edSSimon Glass {
11846683f3dSKever Yang 	return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
1198ef761edSSimon Glass }
1208ef761edSSimon Glass 
1218ef761edSSimon Glass int mmc_get_next_devnum(void)
1228ef761edSSimon Glass {
123dd399cb7SMasahiro Yamada 	return blk_find_max_devnum(IF_TYPE_MMC);
1248ef761edSSimon Glass }
1258ef761edSSimon Glass 
1268ef761edSSimon Glass struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
1278ef761edSSimon Glass {
1288ef761edSSimon Glass 	struct blk_desc *desc;
1298ef761edSSimon Glass 	struct udevice *dev;
1308ef761edSSimon Glass 
1318ef761edSSimon Glass 	device_find_first_child(mmc->dev, &dev);
1328ef761edSSimon Glass 	if (!dev)
1338ef761edSSimon Glass 		return NULL;
1348ef761edSSimon Glass 	desc = dev_get_uclass_platdata(dev);
1358ef761edSSimon Glass 
1368ef761edSSimon Glass 	return desc;
1378ef761edSSimon Glass }
1388ef761edSSimon Glass 
1398ef761edSSimon Glass void mmc_do_preinit(void)
1408ef761edSSimon Glass {
1418ef761edSSimon Glass 	struct udevice *dev;
1428ef761edSSimon Glass 	struct uclass *uc;
1438ef761edSSimon Glass 	int ret;
1448ef761edSSimon Glass 
1458ef761edSSimon Glass 	ret = uclass_get(UCLASS_MMC, &uc);
1468ef761edSSimon Glass 	if (ret)
1478ef761edSSimon Glass 		return;
1488ef761edSSimon Glass 	uclass_foreach_dev(dev, uc) {
1498ef761edSSimon Glass 		struct mmc *m = mmc_get_mmc_dev(dev);
1508ef761edSSimon Glass 
1518ef761edSSimon Glass 		if (!m)
1528ef761edSSimon Glass 			continue;
1538ef761edSSimon Glass #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1548ef761edSSimon Glass 		mmc_set_preinit(m, 1);
1558ef761edSSimon Glass #endif
1568ef761edSSimon Glass 		if (m->preinit)
1578ef761edSSimon Glass 			mmc_start_init(m);
1588ef761edSSimon Glass 	}
1598ef761edSSimon Glass }
1608ef761edSSimon Glass 
1618ef761edSSimon Glass #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1628ef761edSSimon Glass void print_mmc_devices(char separator)
1638ef761edSSimon Glass {
1648ef761edSSimon Glass 	struct udevice *dev;
1658ef761edSSimon Glass 	char *mmc_type;
1668ef761edSSimon Glass 	bool first = true;
1678ef761edSSimon Glass 
1688ef761edSSimon Glass 	for (uclass_first_device(UCLASS_MMC, &dev);
1698ef761edSSimon Glass 	     dev;
1701bd4f92cSXu Ziyuan 	     uclass_next_device(&dev), first = false) {
1718ef761edSSimon Glass 		struct mmc *m = mmc_get_mmc_dev(dev);
1728ef761edSSimon Glass 
1738ef761edSSimon Glass 		if (!first) {
1748ef761edSSimon Glass 			printf("%c", separator);
1758ef761edSSimon Glass 			if (separator != '\n')
1768ef761edSSimon Glass 				puts(" ");
1778ef761edSSimon Glass 		}
1788ef761edSSimon Glass 		if (m->has_init)
1798ef761edSSimon Glass 			mmc_type = IS_SD(m) ? "SD" : "eMMC";
1808ef761edSSimon Glass 		else
1818ef761edSSimon Glass 			mmc_type = NULL;
1828ef761edSSimon Glass 
1838ef761edSSimon Glass 		printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
1848ef761edSSimon Glass 		if (mmc_type)
1858ef761edSSimon Glass 			printf(" (%s)", mmc_type);
1868ef761edSSimon Glass 	}
1878ef761edSSimon Glass 
1888ef761edSSimon Glass 	printf("\n");
1898ef761edSSimon Glass }
1908ef761edSSimon Glass 
1918ef761edSSimon Glass #else
1928ef761edSSimon Glass void print_mmc_devices(char separator) { }
1938ef761edSSimon Glass #endif
194eede897eSSimon Glass 
195eede897eSSimon Glass int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
196eede897eSSimon Glass {
197eede897eSSimon Glass 	struct blk_desc *bdesc;
198eede897eSSimon Glass 	struct udevice *bdev;
19902ad33aaSJaehoon Chung 	int ret, devnum = -1;
200eede897eSSimon Glass 
201*c4d660d4SSimon Glass #if CONFIG_IS_ENABLED(DM_MMC_OPS)
20266656020SSimon Glass 	if (!mmc_get_ops(dev))
20366656020SSimon Glass 		return -ENOSYS;
20466656020SSimon Glass #endif
20502ad33aaSJaehoon Chung #ifndef CONFIG_SPL_BUILD
20602ad33aaSJaehoon Chung 	/* Use the fixed index with aliase node's index */
20766e0ed5cSSimon Glass 	ret = dev_read_alias_seq(dev, &devnum);
20866e0ed5cSSimon Glass 	debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum);
20902ad33aaSJaehoon Chung #endif
21002ad33aaSJaehoon Chung 
21102ad33aaSJaehoon Chung 	ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
21202ad33aaSJaehoon Chung 			devnum, 512, 0, &bdev);
213eede897eSSimon Glass 	if (ret) {
214eede897eSSimon Glass 		debug("Cannot create block device\n");
215eede897eSSimon Glass 		return ret;
216eede897eSSimon Glass 	}
217eede897eSSimon Glass 	bdesc = dev_get_uclass_platdata(bdev);
218eede897eSSimon Glass 	mmc->cfg = cfg;
219eede897eSSimon Glass 	mmc->priv = dev;
220eede897eSSimon Glass 
221eede897eSSimon Glass 	/* the following chunk was from mmc_register() */
222eede897eSSimon Glass 
223eede897eSSimon Glass 	/* Setup dsr related values */
224eede897eSSimon Glass 	mmc->dsr_imp = 0;
225eede897eSSimon Glass 	mmc->dsr = 0xffffffff;
226eede897eSSimon Glass 	/* Setup the universal parts of the block interface just once */
227eede897eSSimon Glass 	bdesc->removable = 1;
228eede897eSSimon Glass 
229eede897eSSimon Glass 	/* setup initial part type */
230eede897eSSimon Glass 	bdesc->part_type = cfg->part_type;
231eede897eSSimon Glass 	mmc->dev = dev;
232eede897eSSimon Glass 
233eede897eSSimon Glass 	return 0;
234eede897eSSimon Glass }
235eede897eSSimon Glass 
236eede897eSSimon Glass int mmc_unbind(struct udevice *dev)
237eede897eSSimon Glass {
238eede897eSSimon Glass 	struct udevice *bdev;
239eede897eSSimon Glass 
240eede897eSSimon Glass 	device_find_first_child(dev, &bdev);
241eede897eSSimon Glass 	if (bdev) {
242706865afSStefan Roese 		device_remove(bdev, DM_REMOVE_NORMAL);
243eede897eSSimon Glass 		device_unbind(bdev);
244eede897eSSimon Glass 	}
245eede897eSSimon Glass 
246eede897eSSimon Glass 	return 0;
247eede897eSSimon Glass }
248eede897eSSimon Glass 
249eede897eSSimon Glass static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
250eede897eSSimon Glass {
251eede897eSSimon Glass 	struct udevice *mmc_dev = dev_get_parent(bdev);
252eede897eSSimon Glass 	struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
253eede897eSSimon Glass 	struct blk_desc *desc = dev_get_uclass_platdata(bdev);
254eede897eSSimon Glass 
255eede897eSSimon Glass 	if (desc->hwpart == hwpart)
256eede897eSSimon Glass 		return 0;
257eede897eSSimon Glass 
258eede897eSSimon Glass 	if (mmc->part_config == MMCPART_NOAVAILABLE)
259eede897eSSimon Glass 		return -EMEDIUMTYPE;
260eede897eSSimon Glass 
261dd399cb7SMasahiro Yamada 	return mmc_switch_part(mmc, hwpart);
262eede897eSSimon Glass }
263eede897eSSimon Glass 
264a0269bb6SFiach Antaw static int mmc_blk_probe(struct udevice *dev)
265a0269bb6SFiach Antaw {
266854f9a71SSimon Glass 	struct udevice *mmc_dev = dev_get_parent(dev);
267854f9a71SSimon Glass 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
268854f9a71SSimon Glass 	struct mmc *mmc = upriv->mmc;
269854f9a71SSimon Glass 	int ret;
270a0269bb6SFiach Antaw 
271854f9a71SSimon Glass 	ret = mmc_init(mmc);
272854f9a71SSimon Glass 	if (ret) {
273854f9a71SSimon Glass 		debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
274854f9a71SSimon Glass 		return ret;
275854f9a71SSimon Glass 	}
276854f9a71SSimon Glass 
277854f9a71SSimon Glass 	return 0;
278a0269bb6SFiach Antaw }
279a0269bb6SFiach Antaw 
280eede897eSSimon Glass static const struct blk_ops mmc_blk_ops = {
281eede897eSSimon Glass 	.read	= mmc_bread,
282eede897eSSimon Glass #ifndef CONFIG_SPL_BUILD
283eede897eSSimon Glass 	.write	= mmc_bwrite,
284561e624cSSimon Glass 	.erase	= mmc_berase,
285eede897eSSimon Glass #endif
286eede897eSSimon Glass 	.select_hwpart	= mmc_select_hwpart,
287eede897eSSimon Glass };
288eede897eSSimon Glass 
289eede897eSSimon Glass U_BOOT_DRIVER(mmc_blk) = {
290eede897eSSimon Glass 	.name		= "mmc_blk",
291eede897eSSimon Glass 	.id		= UCLASS_BLK,
292eede897eSSimon Glass 	.ops		= &mmc_blk_ops,
293a0269bb6SFiach Antaw 	.probe		= mmc_blk_probe,
294eede897eSSimon Glass };
2958ef761edSSimon Glass #endif /* CONFIG_BLK */
2968ef761edSSimon Glass 
297e7ecf7cbSSimon Glass U_BOOT_DRIVER(mmc) = {
298e7ecf7cbSSimon Glass 	.name	= "mmc",
299e7ecf7cbSSimon Glass 	.id	= UCLASS_MMC,
300e7ecf7cbSSimon Glass };
301e7ecf7cbSSimon Glass 
302e7ecf7cbSSimon Glass UCLASS_DRIVER(mmc) = {
303e7ecf7cbSSimon Glass 	.id		= UCLASS_MMC,
304e7ecf7cbSSimon Glass 	.name		= "mmc",
305e7ecf7cbSSimon Glass 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
306e7ecf7cbSSimon Glass 	.per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),
307e7ecf7cbSSimon Glass };
308