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