blob: c5b7872900104acb19fa76f503ebac1b3302465b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse7ecf7c2015-06-23 15:38:48 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glasse7ecf7c2015-06-23 15:38:48 -06005 */
6
7#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glasse7ecf7c2015-06-23 15:38:48 -06009#include <mmc.h>
10#include <dm.h>
Simon Glasseede8972016-06-12 23:30:16 -060011#include <dm/device-internal.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <dm/device_compat.h>
Simon Glasse7ecf7c2015-06-23 15:38:48 -060013#include <dm/lists.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <linux/compat.h>
Simon Glasseede8972016-06-12 23:30:16 -060015#include "mmc_private.h"
Simon Glasse7ecf7c2015-06-23 15:38:48 -060016
Marek Vasut145429a2020-04-04 12:45:05 +020017int dm_mmc_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
18{
19 struct dm_mmc_ops *ops = mmc_get_ops(dev);
20 struct mmc *mmc = mmc_get_mmc_dev(dev);
21
22 if (ops->get_b_max)
23 return ops->get_b_max(dev, dst, blkcnt);
24 else
25 return mmc->cfg->b_max;
26}
27
28int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
29{
30 return dm_mmc_get_b_max(mmc->dev, dst, blkcnt);
31}
32
Simon Glass8ca51e52016-06-12 23:30:22 -060033int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
34 struct mmc_data *data)
35{
36 struct mmc *mmc = mmc_get_mmc_dev(dev);
37 struct dm_mmc_ops *ops = mmc_get_ops(dev);
38 int ret;
39
40 mmmc_trace_before_send(mmc, cmd);
41 if (ops->send_cmd)
42 ret = ops->send_cmd(dev, cmd, data);
43 else
44 ret = -ENOSYS;
45 mmmc_trace_after_send(mmc, cmd, ret);
46
47 return ret;
48}
49
50int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
51{
52 return dm_mmc_send_cmd(mmc->dev, cmd, data);
53}
54
55int dm_mmc_set_ios(struct udevice *dev)
56{
57 struct dm_mmc_ops *ops = mmc_get_ops(dev);
58
59 if (!ops->set_ios)
60 return -ENOSYS;
61 return ops->set_ios(dev);
62}
63
64int mmc_set_ios(struct mmc *mmc)
65{
66 return dm_mmc_set_ios(mmc->dev);
67}
68
Sam Protsenko6cf8a902019-08-14 22:52:51 +030069int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020070{
71 struct dm_mmc_ops *ops = mmc_get_ops(dev);
72
73 if (!ops->wait_dat0)
74 return -ENOSYS;
Sam Protsenko6cf8a902019-08-14 22:52:51 +030075 return ops->wait_dat0(dev, state, timeout_us);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020076}
77
Sam Protsenko6cf8a902019-08-14 22:52:51 +030078int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020079{
Sam Protsenko6cf8a902019-08-14 22:52:51 +030080 return dm_mmc_wait_dat0(mmc->dev, state, timeout_us);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020081}
82
Simon Glass8ca51e52016-06-12 23:30:22 -060083int dm_mmc_get_wp(struct udevice *dev)
84{
85 struct dm_mmc_ops *ops = mmc_get_ops(dev);
86
87 if (!ops->get_wp)
88 return -ENOSYS;
89 return ops->get_wp(dev);
90}
91
92int mmc_getwp(struct mmc *mmc)
93{
94 return dm_mmc_get_wp(mmc->dev);
95}
96
97int dm_mmc_get_cd(struct udevice *dev)
98{
99 struct dm_mmc_ops *ops = mmc_get_ops(dev);
100
101 if (!ops->get_cd)
102 return -ENOSYS;
103 return ops->get_cd(dev);
104}
105
106int mmc_getcd(struct mmc *mmc)
107{
108 return dm_mmc_get_cd(mmc->dev);
109}
Simon Glass8ca51e52016-06-12 23:30:22 -0600110
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100111#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +0200112int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
113{
114 struct dm_mmc_ops *ops = mmc_get_ops(dev);
115
116 if (!ops->execute_tuning)
117 return -ENOSYS;
118 return ops->execute_tuning(dev, opcode);
119}
120
121int mmc_execute_tuning(struct mmc *mmc, uint opcode)
122{
123 return dm_mmc_execute_tuning(mmc->dev, opcode);
124}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100125#endif
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +0200126
Peng Fan44acd492019-07-10 14:43:07 +0800127#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
128int dm_mmc_set_enhanced_strobe(struct udevice *dev)
129{
130 struct dm_mmc_ops *ops = mmc_get_ops(dev);
131
132 if (ops->set_enhanced_strobe)
133 return ops->set_enhanced_strobe(dev);
134
135 return -ENOTSUPP;
136}
137
138int mmc_set_enhanced_strobe(struct mmc *mmc)
139{
140 return dm_mmc_set_enhanced_strobe(mmc->dev);
141}
142#endif
143
Yann Gautier3602a562019-09-19 17:56:12 +0200144int dm_mmc_host_power_cycle(struct udevice *dev)
145{
146 struct dm_mmc_ops *ops = mmc_get_ops(dev);
147
148 if (ops->host_power_cycle)
149 return ops->host_power_cycle(dev);
150 return 0;
151}
152
153int mmc_host_power_cycle(struct mmc *mmc)
154{
155 return dm_mmc_host_power_cycle(mmc->dev);
156}
157
Faiz Abbas32860bd2020-02-26 13:44:30 +0530158int dm_mmc_deferred_probe(struct udevice *dev)
159{
160 struct dm_mmc_ops *ops = mmc_get_ops(dev);
161
162 if (ops->deferred_probe)
163 return ops->deferred_probe(dev);
164
165 return 0;
166}
167
168int mmc_deferred_probe(struct mmc *mmc)
169{
170 return dm_mmc_deferred_probe(mmc->dev);
171}
172
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100173int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200174{
175 int val;
176
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100177 val = dev_read_u32_default(dev, "bus-width", 1);
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200178
179 switch (val) {
180 case 0x8:
181 cfg->host_caps |= MMC_MODE_8BIT;
182 /* fall through */
183 case 0x4:
184 cfg->host_caps |= MMC_MODE_4BIT;
185 /* fall through */
186 case 0x1:
187 cfg->host_caps |= MMC_MODE_1BIT;
188 break;
189 default:
Masahiro Yamada4b28f7b2017-12-30 02:00:07 +0900190 dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
191 return -EINVAL;
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200192 }
193
Masahiro Yamadac42ee362017-12-30 02:00:06 +0900194 /* f_max is obtained from the optional "max-frequency" property */
195 dev_read_u32(dev, "max-frequency", &cfg->f_max);
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200196
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100197 if (dev_read_bool(dev, "cap-sd-highspeed"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200198 cfg->host_caps |= MMC_CAP(SD_HS);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100199 if (dev_read_bool(dev, "cap-mmc-highspeed"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200200 cfg->host_caps |= MMC_CAP(MMC_HS);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100201 if (dev_read_bool(dev, "sd-uhs-sdr12"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200202 cfg->host_caps |= MMC_CAP(UHS_SDR12);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100203 if (dev_read_bool(dev, "sd-uhs-sdr25"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200204 cfg->host_caps |= MMC_CAP(UHS_SDR25);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100205 if (dev_read_bool(dev, "sd-uhs-sdr50"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200206 cfg->host_caps |= MMC_CAP(UHS_SDR50);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100207 if (dev_read_bool(dev, "sd-uhs-sdr104"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200208 cfg->host_caps |= MMC_CAP(UHS_SDR104);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100209 if (dev_read_bool(dev, "sd-uhs-ddr50"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200210 cfg->host_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100211 if (dev_read_bool(dev, "mmc-ddr-1_8v"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200212 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100213 if (dev_read_bool(dev, "mmc-ddr-1_2v"))
214 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
215 if (dev_read_bool(dev, "mmc-hs200-1_8v"))
216 cfg->host_caps |= MMC_CAP(MMC_HS_200);
217 if (dev_read_bool(dev, "mmc-hs200-1_2v"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200218 cfg->host_caps |= MMC_CAP(MMC_HS_200);
Marek Vasuteb2acba2018-06-13 06:50:16 +0200219 if (dev_read_bool(dev, "mmc-hs400-1_8v"))
220 cfg->host_caps |= MMC_CAP(MMC_HS_400);
221 if (dev_read_bool(dev, "mmc-hs400-1_2v"))
222 cfg->host_caps |= MMC_CAP(MMC_HS_400);
Peng Fanb0fc3122019-07-10 09:35:18 +0000223 if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe"))
224 cfg->host_caps |= MMC_CAP(MMC_HS_400_ES);
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200225
T Karthik Reddy86a94e72019-06-25 13:39:02 +0200226 if (dev_read_bool(dev, "non-removable")) {
227 cfg->host_caps |= MMC_CAP_NONREMOVABLE;
228 } else {
229 if (dev_read_bool(dev, "cd-inverted"))
230 cfg->host_caps |= MMC_CAP_CD_ACTIVE_HIGH;
231 if (dev_read_bool(dev, "broken-cd"))
232 cfg->host_caps |= MMC_CAP_NEEDS_POLL;
233 }
234
Peng Fan479a8dd2019-07-10 09:35:20 +0000235 if (dev_read_bool(dev, "no-1-8-v")) {
236 cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 |
237 MMC_MODE_HS400 | MMC_MODE_HS400_ES);
238 }
239
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200240 return 0;
241}
242
Simon Glass3a905cd2020-04-08 08:33:00 -0600243struct mmc *mmc_get_mmc_dev(const struct udevice *dev)
Simon Glasse7ecf7c2015-06-23 15:38:48 -0600244{
245 struct mmc_uclass_priv *upriv;
246
247 if (!device_active(dev))
248 return NULL;
249 upriv = dev_get_uclass_priv(dev);
250 return upriv->mmc;
251}
252
Simon Glassc4d660d2017-07-04 13:31:19 -0600253#if CONFIG_IS_ENABLED(BLK)
Simon Glass8ef761e2016-05-01 13:52:39 -0600254struct mmc *find_mmc_device(int dev_num)
255{
256 struct udevice *dev, *mmc_dev;
257 int ret;
258
Simon Glass97525642017-05-27 11:37:19 -0600259 ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev);
Simon Glass8ef761e2016-05-01 13:52:39 -0600260
261 if (ret) {
262#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
263 printf("MMC Device %d not found\n", dev_num);
264#endif
265 return NULL;
266 }
267
268 mmc_dev = dev_get_parent(dev);
269
Simon Glass97525642017-05-27 11:37:19 -0600270 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
271
272 return mmc;
Simon Glass8ef761e2016-05-01 13:52:39 -0600273}
274
275int get_mmc_num(void)
276{
Kever Yang46683f32016-07-22 17:22:50 +0800277 return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
Simon Glass8ef761e2016-05-01 13:52:39 -0600278}
279
280int mmc_get_next_devnum(void)
281{
Masahiro Yamadadd399cb2016-10-14 00:13:18 +0900282 return blk_find_max_devnum(IF_TYPE_MMC);
Simon Glass8ef761e2016-05-01 13:52:39 -0600283}
284
285struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
286{
287 struct blk_desc *desc;
288 struct udevice *dev;
289
290 device_find_first_child(mmc->dev, &dev);
291 if (!dev)
292 return NULL;
293 desc = dev_get_uclass_platdata(dev);
294
295 return desc;
296}
297
298void mmc_do_preinit(void)
299{
300 struct udevice *dev;
301 struct uclass *uc;
302 int ret;
303
304 ret = uclass_get(UCLASS_MMC, &uc);
305 if (ret)
306 return;
307 uclass_foreach_dev(dev, uc) {
308 struct mmc *m = mmc_get_mmc_dev(dev);
309
310 if (!m)
311 continue;
312#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
313 mmc_set_preinit(m, 1);
314#endif
315 if (m->preinit)
316 mmc_start_init(m);
317 }
318}
319
320#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
321void print_mmc_devices(char separator)
322{
323 struct udevice *dev;
324 char *mmc_type;
325 bool first = true;
326
327 for (uclass_first_device(UCLASS_MMC, &dev);
328 dev;
Xu Ziyuan1bd4f922016-07-23 11:11:22 +0800329 uclass_next_device(&dev), first = false) {
Simon Glass8ef761e2016-05-01 13:52:39 -0600330 struct mmc *m = mmc_get_mmc_dev(dev);
331
332 if (!first) {
333 printf("%c", separator);
334 if (separator != '\n')
335 puts(" ");
336 }
337 if (m->has_init)
338 mmc_type = IS_SD(m) ? "SD" : "eMMC";
339 else
340 mmc_type = NULL;
341
342 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
343 if (mmc_type)
344 printf(" (%s)", mmc_type);
345 }
346
347 printf("\n");
348}
349
350#else
351void print_mmc_devices(char separator) { }
352#endif
Simon Glasseede8972016-06-12 23:30:16 -0600353
354int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
355{
356 struct blk_desc *bdesc;
357 struct udevice *bdev;
Jaehoon Chung02ad33a2017-02-02 13:41:14 +0900358 int ret, devnum = -1;
Simon Glasseede8972016-06-12 23:30:16 -0600359
Simon Glass66656022017-04-23 20:02:09 -0600360 if (!mmc_get_ops(dev))
361 return -ENOSYS;
Jaehoon Chung02ad33a2017-02-02 13:41:14 +0900362#ifndef CONFIG_SPL_BUILD
363 /* Use the fixed index with aliase node's index */
Simon Glass66e0ed52017-05-18 20:09:36 -0600364 ret = dev_read_alias_seq(dev, &devnum);
365 debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum);
Jaehoon Chung02ad33a2017-02-02 13:41:14 +0900366#endif
367
368 ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
369 devnum, 512, 0, &bdev);
Simon Glasseede8972016-06-12 23:30:16 -0600370 if (ret) {
371 debug("Cannot create block device\n");
372 return ret;
373 }
374 bdesc = dev_get_uclass_platdata(bdev);
375 mmc->cfg = cfg;
376 mmc->priv = dev;
377
378 /* the following chunk was from mmc_register() */
379
380 /* Setup dsr related values */
381 mmc->dsr_imp = 0;
382 mmc->dsr = 0xffffffff;
383 /* Setup the universal parts of the block interface just once */
384 bdesc->removable = 1;
385
386 /* setup initial part type */
387 bdesc->part_type = cfg->part_type;
388 mmc->dev = dev;
389
390 return 0;
391}
392
393int mmc_unbind(struct udevice *dev)
394{
395 struct udevice *bdev;
396
397 device_find_first_child(dev, &bdev);
398 if (bdev) {
Stefan Roese706865a2017-03-20 12:51:48 +0100399 device_remove(bdev, DM_REMOVE_NORMAL);
Simon Glasseede8972016-06-12 23:30:16 -0600400 device_unbind(bdev);
401 }
402
403 return 0;
404}
405
406static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
407{
408 struct udevice *mmc_dev = dev_get_parent(bdev);
409 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
410 struct blk_desc *desc = dev_get_uclass_platdata(bdev);
Weijie Gao47b7fa32019-08-27 15:32:19 +0800411 int ret;
Simon Glasseede8972016-06-12 23:30:16 -0600412
413 if (desc->hwpart == hwpart)
414 return 0;
415
416 if (mmc->part_config == MMCPART_NOAVAILABLE)
417 return -EMEDIUMTYPE;
418
Weijie Gao47b7fa32019-08-27 15:32:19 +0800419 ret = mmc_switch_part(mmc, hwpart);
420 if (!ret)
421 blkcache_invalidate(desc->if_type, desc->devnum);
422
423 return ret;
Simon Glasseede8972016-06-12 23:30:16 -0600424}
425
Fiach Antawa0269bb2017-01-25 19:00:24 +1000426static int mmc_blk_probe(struct udevice *dev)
427{
Simon Glass854f9a72017-04-23 20:02:10 -0600428 struct udevice *mmc_dev = dev_get_parent(dev);
429 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
430 struct mmc *mmc = upriv->mmc;
431 int ret;
Fiach Antawa0269bb2017-01-25 19:00:24 +1000432
Simon Glass854f9a72017-04-23 20:02:10 -0600433 ret = mmc_init(mmc);
434 if (ret) {
435 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
436 return ret;
437 }
438
439 return 0;
Fiach Antawa0269bb2017-01-25 19:00:24 +1000440}
441
Marek Vasutfceea992019-01-29 04:45:51 +0100442#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
443 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
444 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
445static int mmc_blk_remove(struct udevice *dev)
446{
447 struct udevice *mmc_dev = dev_get_parent(dev);
448 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
449 struct mmc *mmc = upriv->mmc;
450
451 return mmc_deinit(mmc);
452}
453#endif
454
Simon Glasseede8972016-06-12 23:30:16 -0600455static const struct blk_ops mmc_blk_ops = {
456 .read = mmc_bread,
Jean-Jacques Hiblotd6400c32018-01-04 15:23:32 +0100457#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glasseede8972016-06-12 23:30:16 -0600458 .write = mmc_bwrite,
Simon Glass561e6242016-10-01 14:43:17 -0600459 .erase = mmc_berase,
Simon Glasseede8972016-06-12 23:30:16 -0600460#endif
461 .select_hwpart = mmc_select_hwpart,
462};
463
464U_BOOT_DRIVER(mmc_blk) = {
465 .name = "mmc_blk",
466 .id = UCLASS_BLK,
467 .ops = &mmc_blk_ops,
Fiach Antawa0269bb2017-01-25 19:00:24 +1000468 .probe = mmc_blk_probe,
Marek Vasutfceea992019-01-29 04:45:51 +0100469#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
470 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
471 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
472 .remove = mmc_blk_remove,
473 .flags = DM_FLAG_OS_PREPARE,
474#endif
Simon Glasseede8972016-06-12 23:30:16 -0600475};
Simon Glass8ef761e2016-05-01 13:52:39 -0600476#endif /* CONFIG_BLK */
477
Simon Glasse7ecf7c2015-06-23 15:38:48 -0600478
479UCLASS_DRIVER(mmc) = {
480 .id = UCLASS_MMC,
481 .name = "mmc",
482 .flags = DM_UC_FLAG_SEQ_ALIAS,
483 .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),
484};