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