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