1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2015 Google, Inc
4 * Copyright 2020 NXP
5 * Written by Simon Glass <sjg@chromium.org>
6 */
7
8 #define LOG_CATEGORY UCLASS_MMC
9
10 #include <bootdev.h>
11 #include <log.h>
12 #include <mmc.h>
13 #include <dm.h>
14 #include <dm/device-internal.h>
15 #include <dm/device_compat.h>
16 #include <dm/lists.h>
17 #include <linux/compat.h>
18 #include "mmc_private.h"
19
dm_mmc_get_b_max(struct udevice * dev,void * dst,lbaint_t blkcnt)20 static int dm_mmc_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
21 {
22 struct dm_mmc_ops *ops = mmc_get_ops(dev);
23 struct mmc *mmc = mmc_get_mmc_dev(dev);
24
25 if (ops->get_b_max)
26 return ops->get_b_max(dev, dst, blkcnt);
27 else
28 return mmc->cfg->b_max;
29 }
30
mmc_get_b_max(struct mmc * mmc,void * dst,lbaint_t blkcnt)31 int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
32 {
33 return dm_mmc_get_b_max(mmc->dev, dst, blkcnt);
34 }
35
dm_mmc_send_cmd(struct udevice * dev,struct mmc_cmd * cmd,struct mmc_data * data)36 static int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
37 struct mmc_data *data)
38 {
39 struct mmc *mmc = mmc_get_mmc_dev(dev);
40 struct dm_mmc_ops *ops = mmc_get_ops(dev);
41 int ret;
42
43 mmmc_trace_before_send(mmc, cmd);
44 if (ops->send_cmd)
45 ret = ops->send_cmd(dev, cmd, data);
46 else
47 ret = -ENOSYS;
48 mmmc_trace_after_send(mmc, cmd, ret);
49
50 return ret;
51 }
52
mmc_send_cmd(struct mmc * mmc,struct mmc_cmd * cmd,struct mmc_data * data)53 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
54 {
55 return dm_mmc_send_cmd(mmc->dev, cmd, data);
56 }
57
dm_mmc_set_ios(struct udevice * dev)58 static int dm_mmc_set_ios(struct udevice *dev)
59 {
60 struct dm_mmc_ops *ops = mmc_get_ops(dev);
61
62 if (!ops->set_ios)
63 return -ENOSYS;
64 return ops->set_ios(dev);
65 }
66
mmc_set_ios(struct mmc * mmc)67 int mmc_set_ios(struct mmc *mmc)
68 {
69 return dm_mmc_set_ios(mmc->dev);
70 }
71
dm_mmc_wait_dat0(struct udevice * dev,int state,int timeout_us)72 static int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
73 {
74 struct dm_mmc_ops *ops = mmc_get_ops(dev);
75
76 if (!ops->wait_dat0)
77 return -ENOSYS;
78 return ops->wait_dat0(dev, state, timeout_us);
79 }
80
mmc_wait_dat0(struct mmc * mmc,int state,int timeout_us)81 int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
82 {
83 return dm_mmc_wait_dat0(mmc->dev, state, timeout_us);
84 }
85
dm_mmc_send_init_stream(struct udevice * dev)86 void dm_mmc_send_init_stream(struct udevice *dev)
87 {
88 struct dm_mmc_ops *ops = mmc_get_ops(dev);
89
90 if (ops->send_init_stream)
91 ops->send_init_stream(dev);
92 }
93
mmc_send_init_stream(struct mmc * mmc)94 void mmc_send_init_stream(struct mmc *mmc)
95 {
96 dm_mmc_send_init_stream(mmc->dev);
97 }
98
dm_mmc_get_wp(struct udevice * dev)99 static int dm_mmc_get_wp(struct udevice *dev)
100 {
101 struct dm_mmc_ops *ops = mmc_get_ops(dev);
102
103 if (!ops->get_wp)
104 return -ENOSYS;
105 return ops->get_wp(dev);
106 }
107
mmc_getwp(struct mmc * mmc)108 int mmc_getwp(struct mmc *mmc)
109 {
110 return dm_mmc_get_wp(mmc->dev);
111 }
112
dm_mmc_get_cd(struct udevice * dev)113 static int dm_mmc_get_cd(struct udevice *dev)
114 {
115 struct dm_mmc_ops *ops = mmc_get_ops(dev);
116
117 if (!ops->get_cd)
118 return -ENOSYS;
119 return ops->get_cd(dev);
120 }
121
mmc_getcd(struct mmc * mmc)122 int mmc_getcd(struct mmc *mmc)
123 {
124 return dm_mmc_get_cd(mmc->dev);
125 }
126
127 #if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
dm_mmc_execute_tuning(struct udevice * dev,uint opcode)128 static int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
129 {
130 struct dm_mmc_ops *ops = mmc_get_ops(dev);
131
132 if (!ops->execute_tuning)
133 return -ENOSYS;
134 return ops->execute_tuning(dev, opcode);
135 }
136
mmc_execute_tuning(struct mmc * mmc,uint opcode)137 int mmc_execute_tuning(struct mmc *mmc, uint opcode)
138 {
139 int ret;
140
141 mmc->tuning = true;
142 ret = dm_mmc_execute_tuning(mmc->dev, opcode);
143 mmc->tuning = false;
144
145 return ret;
146 }
147 #endif
148
149 #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
dm_mmc_set_enhanced_strobe(struct udevice * dev)150 static int dm_mmc_set_enhanced_strobe(struct udevice *dev)
151 {
152 struct dm_mmc_ops *ops = mmc_get_ops(dev);
153
154 if (ops->set_enhanced_strobe)
155 return ops->set_enhanced_strobe(dev);
156
157 return -ENOTSUPP;
158 }
159
mmc_set_enhanced_strobe(struct mmc * mmc)160 int mmc_set_enhanced_strobe(struct mmc *mmc)
161 {
162 return dm_mmc_set_enhanced_strobe(mmc->dev);
163 }
164 #endif
165
dm_mmc_hs400_prepare_ddr(struct udevice * dev)166 static int dm_mmc_hs400_prepare_ddr(struct udevice *dev)
167 {
168 struct dm_mmc_ops *ops = mmc_get_ops(dev);
169
170 if (ops->hs400_prepare_ddr)
171 return ops->hs400_prepare_ddr(dev);
172
173 return 0;
174 }
175
mmc_hs400_prepare_ddr(struct mmc * mmc)176 int mmc_hs400_prepare_ddr(struct mmc *mmc)
177 {
178 return dm_mmc_hs400_prepare_ddr(mmc->dev);
179 }
180
dm_mmc_host_power_cycle(struct udevice * dev)181 static int dm_mmc_host_power_cycle(struct udevice *dev)
182 {
183 struct dm_mmc_ops *ops = mmc_get_ops(dev);
184
185 if (ops->host_power_cycle)
186 return ops->host_power_cycle(dev);
187 return 0;
188 }
189
mmc_host_power_cycle(struct mmc * mmc)190 int mmc_host_power_cycle(struct mmc *mmc)
191 {
192 return dm_mmc_host_power_cycle(mmc->dev);
193 }
194
dm_mmc_deferred_probe(struct udevice * dev)195 static int dm_mmc_deferred_probe(struct udevice *dev)
196 {
197 struct dm_mmc_ops *ops = mmc_get_ops(dev);
198
199 if (ops->deferred_probe)
200 return ops->deferred_probe(dev);
201
202 return 0;
203 }
204
mmc_deferred_probe(struct mmc * mmc)205 int mmc_deferred_probe(struct mmc *mmc)
206 {
207 return dm_mmc_deferred_probe(mmc->dev);
208 }
209
dm_mmc_reinit(struct udevice * dev)210 static int dm_mmc_reinit(struct udevice *dev)
211 {
212 struct dm_mmc_ops *ops = mmc_get_ops(dev);
213
214 if (ops->reinit)
215 return ops->reinit(dev);
216
217 return 0;
218 }
219
mmc_reinit(struct mmc * mmc)220 int mmc_reinit(struct mmc *mmc)
221 {
222 return dm_mmc_reinit(mmc->dev);
223 }
224
mmc_of_parse(struct udevice * dev,struct mmc_config * cfg)225 int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
226 {
227 int val;
228
229 val = dev_read_u32_default(dev, "bus-width", 1);
230
231 switch (val) {
232 case 0x8:
233 cfg->host_caps |= MMC_MODE_8BIT;
234 /* fall through */
235 case 0x4:
236 cfg->host_caps |= MMC_MODE_4BIT;
237 /* fall through */
238 case 0x1:
239 cfg->host_caps |= MMC_MODE_1BIT;
240 break;
241 default:
242 dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
243 return -EINVAL;
244 }
245
246 /* f_max is obtained from the optional "max-frequency" property */
247 dev_read_u32(dev, "max-frequency", &cfg->f_max);
248
249 if (dev_read_bool(dev, "cap-sd-highspeed"))
250 cfg->host_caps |= MMC_CAP(SD_HS);
251 if (dev_read_bool(dev, "cap-mmc-highspeed"))
252 cfg->host_caps |= MMC_CAP(MMC_HS) | MMC_CAP(MMC_HS_52);
253 if (dev_read_bool(dev, "sd-uhs-sdr12"))
254 cfg->host_caps |= MMC_CAP(UHS_SDR12);
255 if (dev_read_bool(dev, "sd-uhs-sdr25"))
256 cfg->host_caps |= MMC_CAP(UHS_SDR25);
257 if (dev_read_bool(dev, "sd-uhs-sdr50"))
258 cfg->host_caps |= MMC_CAP(UHS_SDR50);
259 if (dev_read_bool(dev, "sd-uhs-sdr104"))
260 cfg->host_caps |= MMC_CAP(UHS_SDR104);
261 if (dev_read_bool(dev, "sd-uhs-ddr50"))
262 cfg->host_caps |= MMC_CAP(UHS_DDR50);
263 if (dev_read_bool(dev, "mmc-ddr-1_8v"))
264 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
265 if (dev_read_bool(dev, "mmc-ddr-1_2v"))
266 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
267 if (dev_read_bool(dev, "mmc-hs200-1_8v"))
268 cfg->host_caps |= MMC_CAP(MMC_HS_200);
269 if (dev_read_bool(dev, "mmc-hs200-1_2v"))
270 cfg->host_caps |= MMC_CAP(MMC_HS_200);
271 if (dev_read_bool(dev, "mmc-hs400-1_8v"))
272 cfg->host_caps |= MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_200);
273 if (dev_read_bool(dev, "mmc-hs400-1_2v"))
274 cfg->host_caps |= MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_200);
275 if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe"))
276 cfg->host_caps |= MMC_CAP(MMC_HS_400_ES);
277 if (dev_read_bool(dev, "no-mmc-hs400"))
278 cfg->host_caps &= ~(MMC_CAP(MMC_HS_400) |
279 MMC_CAP(MMC_HS_400_ES));
280
281 if (dev_read_bool(dev, "non-removable")) {
282 cfg->host_caps |= MMC_CAP_NONREMOVABLE;
283 } else {
284 if (dev_read_bool(dev, "cd-inverted"))
285 cfg->host_caps |= MMC_CAP_CD_ACTIVE_HIGH;
286 if (dev_read_bool(dev, "broken-cd"))
287 cfg->host_caps |= MMC_CAP_NEEDS_POLL;
288 }
289
290 if (dev_read_bool(dev, "no-1-8-v")) {
291 cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 |
292 MMC_MODE_HS400 | MMC_MODE_HS400_ES);
293 }
294
295 return 0;
296 }
297
mmc_get_mmc_dev(const struct udevice * dev)298 struct mmc *mmc_get_mmc_dev(const struct udevice *dev)
299 {
300 struct mmc_uclass_priv *upriv;
301
302 if (!device_active(dev))
303 return NULL;
304 upriv = dev_get_uclass_priv(dev);
305 return upriv->mmc;
306 }
307
308 #if CONFIG_IS_ENABLED(BLK)
find_mmc_device(int dev_num)309 struct mmc *find_mmc_device(int dev_num)
310 {
311 struct udevice *dev, *mmc_dev;
312 int ret;
313
314 ret = blk_find_device(UCLASS_MMC, dev_num, &dev);
315
316 if (ret) {
317 #if !defined(CONFIG_XPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
318 printf("MMC Device %d not found\n", dev_num);
319 #endif
320 return NULL;
321 }
322
323 mmc_dev = dev_get_parent(dev);
324
325 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
326
327 return mmc;
328 }
329
get_mmc_num(void)330 int get_mmc_num(void)
331 {
332 return max((blk_find_max_devnum(UCLASS_MMC) + 1), 0);
333 }
334
mmc_get_next_devnum(void)335 int mmc_get_next_devnum(void)
336 {
337 return blk_find_max_devnum(UCLASS_MMC);
338 }
339
mmc_get_blk(struct udevice * dev,struct udevice ** blkp)340 int mmc_get_blk(struct udevice *dev, struct udevice **blkp)
341 {
342 struct udevice *blk;
343 int ret;
344
345 device_find_first_child_by_uclass(dev, UCLASS_BLK, &blk);
346 ret = device_probe(blk);
347 if (ret)
348 return ret;
349 *blkp = blk;
350
351 return 0;
352 }
353
mmc_get_blk_desc(struct mmc * mmc)354 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
355 {
356 struct blk_desc *desc;
357 struct udevice *dev;
358
359 device_find_first_child_by_uclass(mmc->dev, UCLASS_BLK, &dev);
360 if (!dev)
361 return NULL;
362 desc = dev_get_uclass_plat(dev);
363
364 return desc;
365 }
366
mmc_do_preinit(void)367 void mmc_do_preinit(void)
368 {
369 struct udevice *dev;
370 struct uclass *uc;
371 int ret;
372
373 ret = uclass_get(UCLASS_MMC, &uc);
374 if (ret)
375 return;
376 uclass_foreach_dev(dev, uc) {
377 struct mmc *m = mmc_get_mmc_dev(dev);
378
379 if (!m)
380 continue;
381
382 m->user_speed_mode = MMC_MODES_END; /* Initialising user set speed mode */
383
384 if (m->preinit)
385 mmc_start_init(m);
386 }
387 }
388
389 #if !defined(CONFIG_XPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
print_mmc_devices(char separator)390 void print_mmc_devices(char separator)
391 {
392 struct udevice *dev;
393 char *mmc_type;
394 bool first = true;
395
396 for (uclass_first_device(UCLASS_MMC, &dev);
397 dev;
398 uclass_next_device(&dev), first = false) {
399 struct mmc *m = mmc_get_mmc_dev(dev);
400
401 if (!first) {
402 printf("%c", separator);
403 if (separator != '\n')
404 puts(" ");
405 }
406 if (m->has_init)
407 mmc_type = IS_SD(m) ? "SD" : "eMMC";
408 else
409 mmc_type = NULL;
410
411 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
412 if (mmc_type)
413 printf(" (%s)", mmc_type);
414 }
415
416 printf("\n");
417 }
418
419 #else
print_mmc_devices(char separator)420 void print_mmc_devices(char separator) { }
421 #endif
422
mmc_bind(struct udevice * dev,struct mmc * mmc,const struct mmc_config * cfg)423 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
424 {
425 struct blk_desc *bdesc;
426 struct udevice *bdev;
427 int ret;
428
429 if (!mmc_get_ops(dev))
430 return -ENOSYS;
431
432 /* Use the fixed index with aliases node's index */
433 debug("%s: alias devnum=%d\n", __func__, dev_seq(dev));
434
435 ret = blk_create_devicef(dev, "mmc_blk", "blk", UCLASS_MMC,
436 dev_seq(dev), DEFAULT_BLKSZ, 0, &bdev);
437 if (ret) {
438 debug("Cannot create block device\n");
439 return ret;
440 }
441 bdesc = dev_get_uclass_plat(bdev);
442 mmc->cfg = cfg;
443 mmc->priv = dev;
444
445 ret = bootdev_setup_for_sibling_blk(bdev, "mmc_bootdev");
446 if (ret)
447 return log_msg_ret("bootdev", ret);
448
449 /* the following chunk was from mmc_register() */
450
451 /* Setup dsr related values */
452 mmc->dsr_imp = 0;
453 mmc->dsr = 0xffffffff;
454 /* Setup the universal parts of the block interface just once */
455 bdesc->removable = 1;
456
457 /* setup initial part type */
458 bdesc->part_type = cfg->part_type;
459 mmc->dev = dev;
460 mmc->user_speed_mode = MMC_MODES_END;
461 return 0;
462 }
463
mmc_unbind(struct udevice * dev)464 int mmc_unbind(struct udevice *dev)
465 {
466 struct udevice *bdev;
467 int ret;
468
469 device_find_first_child_by_uclass(dev, UCLASS_BLK, &bdev);
470 if (bdev) {
471 device_remove(bdev, DM_REMOVE_NORMAL);
472 device_unbind(bdev);
473 }
474 ret = bootdev_unbind_dev(dev);
475 if (ret)
476 return log_msg_ret("bootdev", ret);
477
478 return 0;
479 }
480
mmc_select_hwpart(struct udevice * bdev,int hwpart)481 static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
482 {
483 struct udevice *mmc_dev = dev_get_parent(bdev);
484 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
485 struct blk_desc *desc = dev_get_uclass_plat(bdev);
486 int ret;
487
488 if (desc->hwpart == hwpart)
489 return 0;
490
491 if (mmc->part_config == MMCPART_NOAVAILABLE)
492 return -EMEDIUMTYPE;
493
494 ret = mmc_switch_part(mmc, hwpart);
495 if (!ret)
496 blkcache_invalidate(desc->uclass_id, desc->devnum);
497
498 return ret;
499 }
500
mmc_blk_probe(struct udevice * dev)501 static int mmc_blk_probe(struct udevice *dev)
502 {
503 struct udevice *mmc_dev = dev_get_parent(dev);
504 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
505 struct mmc *mmc = upriv->mmc;
506 int ret;
507
508 ret = mmc_init(mmc);
509 if (ret) {
510 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
511 return ret;
512 }
513
514 return 0;
515 }
516
mmc_remove(struct udevice * dev)517 static int mmc_remove(struct udevice *dev)
518 {
519 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
520 struct mmc *mmc = upriv->mmc;
521
522 return mmc_deinit(mmc);
523 }
524
525 static const struct blk_ops mmc_blk_ops = {
526 .read = mmc_bread,
527 #if CONFIG_IS_ENABLED(MMC_WRITE)
528 .write = mmc_bwrite,
529 .erase = mmc_berase,
530 #endif
531 .select_hwpart = mmc_select_hwpart,
532 };
533
534 U_BOOT_DRIVER(mmc_blk) = {
535 .name = "mmc_blk",
536 .id = UCLASS_BLK,
537 .ops = &mmc_blk_ops,
538 .probe = mmc_blk_probe,
539 .flags = DM_FLAG_OS_PREPARE,
540 };
541 #endif /* CONFIG_BLK */
542
543 UCLASS_DRIVER(mmc) = {
544 .id = UCLASS_MMC,
545 .name = "mmc",
546 .flags = DM_UC_FLAG_SEQ_ALIAS,
547 .per_device_auto = sizeof(struct mmc_uclass_priv),
548 .pre_remove = mmc_remove,
549 };
550