xref: /rk3399_rockchip-uboot/drivers/mmc/mmc-uclass.c (revision 473221da5a5a767b650f97c0a6e63b0854c2221a)
1 /*
2  * Copyright (C) 2015 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <mmc.h>
10 #include <dm.h>
11 #include <dm/device-internal.h>
12 #include <dm/lists.h>
13 #include <dm/root.h>
14 #include "mmc_private.h"
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
19 		    struct mmc_data *data)
20 {
21 	struct mmc *mmc = mmc_get_mmc_dev(dev);
22 	struct dm_mmc_ops *ops = mmc_get_ops(dev);
23 	int ret;
24 
25 	mmmc_trace_before_send(mmc, cmd);
26 	if (ops->send_cmd)
27 		ret = ops->send_cmd(dev, cmd, data);
28 	else
29 		ret = -ENOSYS;
30 	mmmc_trace_after_send(mmc, cmd, ret);
31 
32 	return ret;
33 }
34 
35 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
36 {
37 	return dm_mmc_send_cmd(mmc->dev, cmd, data);
38 }
39 
40 bool mmc_card_busy(struct mmc *mmc)
41 {
42 	struct dm_mmc_ops *ops = mmc_get_ops(mmc->dev);
43 
44 	if (!ops->card_busy)
45 		return -ENOSYS;
46 	return ops->card_busy(mmc->dev);
47 }
48 
49 bool mmc_can_card_busy(struct mmc *mmc)
50 {
51 	struct dm_mmc_ops *ops = mmc_get_ops(mmc->dev);
52 
53 	return !!ops->card_busy;
54 }
55 
56 int dm_mmc_set_ios(struct udevice *dev)
57 {
58 	struct dm_mmc_ops *ops = mmc_get_ops(dev);
59 
60 	if (!ops->set_ios)
61 		return -ENOSYS;
62 	return ops->set_ios(dev);
63 }
64 
65 int mmc_set_ios(struct mmc *mmc)
66 {
67 	return dm_mmc_set_ios(mmc->dev);
68 }
69 
70 int dm_mmc_get_wp(struct udevice *dev)
71 {
72 	struct dm_mmc_ops *ops = mmc_get_ops(dev);
73 
74 	if (!ops->get_wp)
75 		return -ENOSYS;
76 	return ops->get_wp(dev);
77 }
78 
79 int mmc_getwp(struct mmc *mmc)
80 {
81 	return dm_mmc_get_wp(mmc->dev);
82 }
83 
84 int dm_mmc_get_cd(struct udevice *dev)
85 {
86 	struct dm_mmc_ops *ops = mmc_get_ops(dev);
87 
88 	if (!ops->get_cd)
89 		return -ENOSYS;
90 	return ops->get_cd(dev);
91 }
92 
93 int mmc_getcd(struct mmc *mmc)
94 {
95 	return dm_mmc_get_cd(mmc->dev);
96 }
97 
98 struct mmc *mmc_get_mmc_dev(struct udevice *dev)
99 {
100 	struct mmc_uclass_priv *upriv;
101 
102 	if (!device_active(dev))
103 		return NULL;
104 	upriv = dev_get_uclass_priv(dev);
105 	return upriv->mmc;
106 }
107 
108 #if CONFIG_IS_ENABLED(BLK)
109 struct mmc *find_mmc_device(int dev_num)
110 {
111 	struct udevice *dev, *mmc_dev;
112 	int ret;
113 
114 	ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev);
115 
116 	if (ret) {
117 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
118 		printf("MMC Device %d not found\n", dev_num);
119 #endif
120 		return NULL;
121 	}
122 
123 	mmc_dev = dev_get_parent(dev);
124 
125 	struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
126 
127 	return mmc;
128 }
129 
130 int get_mmc_num(void)
131 {
132 	return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
133 }
134 
135 int mmc_get_next_devnum(void)
136 {
137 	return blk_find_max_devnum(IF_TYPE_MMC);
138 }
139 
140 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
141 {
142 	struct blk_desc *desc;
143 	struct udevice *dev;
144 
145 	device_find_first_child(mmc->dev, &dev);
146 	if (!dev)
147 		return NULL;
148 	desc = dev_get_uclass_platdata(dev);
149 
150 	return desc;
151 }
152 
153 void mmc_do_preinit(void)
154 {
155 	struct udevice *dev;
156 	struct uclass *uc;
157 	int ret;
158 
159 	ret = uclass_get(UCLASS_MMC, &uc);
160 	if (ret)
161 		return;
162 	uclass_foreach_dev(dev, uc) {
163 		struct mmc *m = mmc_get_mmc_dev(dev);
164 
165 		if (!m)
166 			continue;
167 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
168 		mmc_set_preinit(m, 1);
169 #endif
170 		if (m->preinit)
171 			mmc_start_init(m);
172 	}
173 }
174 
175 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
176 void print_mmc_devices(char separator)
177 {
178 	struct udevice *dev;
179 	char *mmc_type;
180 	bool first = true;
181 
182 	for (uclass_first_device(UCLASS_MMC, &dev);
183 	     dev;
184 	     uclass_next_device(&dev), first = false) {
185 		struct mmc *m = mmc_get_mmc_dev(dev);
186 
187 		if (!first) {
188 			printf("%c", separator);
189 			if (separator != '\n')
190 				puts(" ");
191 		}
192 		if (m->has_init)
193 			mmc_type = IS_SD(m) ? "SD" : "eMMC";
194 		else
195 			mmc_type = NULL;
196 
197 		printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
198 		if (mmc_type)
199 			printf(" (%s)", mmc_type);
200 	}
201 
202 	printf("\n");
203 }
204 
205 #else
206 void print_mmc_devices(char separator) { }
207 #endif
208 
209 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
210 {
211 	struct blk_desc *bdesc;
212 	struct udevice *bdev;
213 	int ret, devnum = -1;
214 
215 	if (!mmc_get_ops(dev))
216 		return -ENOSYS;
217 	/* Use the fixed index with aliase node's index */
218 	ret = dev_read_alias_seq(dev, &devnum);
219 	debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum);
220 
221 	ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
222 			devnum, 512, 0, &bdev);
223 	if (ret) {
224 		debug("Cannot create block device\n");
225 		return ret;
226 	}
227 	bdesc = dev_get_uclass_platdata(bdev);
228 	mmc->cfg = cfg;
229 	mmc->priv = dev;
230 
231 	/* the following chunk was from mmc_register() */
232 
233 	/* Setup dsr related values */
234 	mmc->dsr_imp = 0;
235 	mmc->dsr = 0xffffffff;
236 	/* Setup the universal parts of the block interface just once */
237 	bdesc->removable = 1;
238 
239 	/* setup initial part type */
240 	bdesc->part_type = cfg->part_type;
241 	mmc->dev = dev;
242 
243 	return 0;
244 }
245 
246 int mmc_unbind(struct udevice *dev)
247 {
248 	struct udevice *bdev;
249 
250 	device_find_first_child(dev, &bdev);
251 	if (bdev) {
252 		device_remove(bdev, DM_REMOVE_NORMAL);
253 		device_unbind(bdev);
254 	}
255 
256 	return 0;
257 }
258 
259 static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
260 {
261 	struct udevice *mmc_dev = dev_get_parent(bdev);
262 	struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
263 	struct blk_desc *desc = dev_get_uclass_platdata(bdev);
264 
265 	if (desc->hwpart == hwpart)
266 		return 0;
267 
268 	if (mmc->part_config == MMCPART_NOAVAILABLE)
269 		return -EMEDIUMTYPE;
270 
271 	return mmc_switch_part(mmc, hwpart);
272 }
273 
274 static int mmc_blk_probe(struct udevice *dev)
275 {
276 	struct udevice *mmc_dev = dev_get_parent(dev);
277 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
278 	struct mmc *mmc = upriv->mmc;
279 	int ret;
280 
281 	ret = mmc_init(mmc);
282 	if (ret) {
283 		debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
284 		return ret;
285 	}
286 
287 	return 0;
288 }
289 
290 static const struct blk_ops mmc_blk_ops = {
291 	.read	= mmc_bread,
292 #ifndef CONFIG_SPL_BUILD
293 	.write	= mmc_bwrite,
294 	.erase	= mmc_berase,
295 #endif
296 	.select_hwpart	= mmc_select_hwpart,
297 };
298 
299 U_BOOT_DRIVER(mmc_blk) = {
300 	.name		= "mmc_blk",
301 	.id		= UCLASS_BLK,
302 	.ops		= &mmc_blk_ops,
303 	.probe		= mmc_blk_probe,
304 };
305 #endif /* CONFIG_BLK */
306 
307 U_BOOT_DRIVER(mmc) = {
308 	.name	= "mmc",
309 	.id	= UCLASS_MMC,
310 };
311 
312 UCLASS_DRIVER(mmc) = {
313 	.id		= UCLASS_MMC,
314 	.name		= "mmc",
315 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
316 	.per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),
317 };
318