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