blob: 304bd5eaee23f12b4a1d5dc32bd2f751020b09f8 [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
Yangbo Lu39913ac2020-06-17 18:08:58 +08004 * Copyright 2020 NXP
Simon Glasse7ecf7c2015-06-23 15:38:48 -06005 * Written by Simon Glass <sjg@chromium.org>
Simon Glasse7ecf7c2015-06-23 15:38:48 -06006 */
7
Patrick Delaunayb953ec22021-04-27 11:02:19 +02008#define LOG_CATEGORY UCLASS_MMC
9
Simon Glasse7ecf7c2015-06-23 15:38:48 -060010#include <common.h>
Simon Glassb8aa4632022-04-24 23:31:14 -060011#include <bootdev.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glasse7ecf7c2015-06-23 15:38:48 -060013#include <mmc.h>
14#include <dm.h>
Simon Glasseede8972016-06-12 23:30:16 -060015#include <dm/device-internal.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glasse7ecf7c2015-06-23 15:38:48 -060017#include <dm/lists.h>
Simon Glass336d4612020-02-03 07:36:16 -070018#include <linux/compat.h>
Simon Glasseede8972016-06-12 23:30:16 -060019#include "mmc_private.h"
Simon Glasse7ecf7c2015-06-23 15:38:48 -060020
Jaehoon Chungd77d61e2021-05-31 08:31:50 +090021static int dm_mmc_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
Marek Vasut145429a2020-04-04 12:45:05 +020022{
23 struct dm_mmc_ops *ops = mmc_get_ops(dev);
24 struct mmc *mmc = mmc_get_mmc_dev(dev);
25
26 if (ops->get_b_max)
27 return ops->get_b_max(dev, dst, blkcnt);
28 else
29 return mmc->cfg->b_max;
30}
31
32int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
33{
34 return dm_mmc_get_b_max(mmc->dev, dst, blkcnt);
35}
36
Jaehoon Chungd77d61e2021-05-31 08:31:50 +090037static int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glass8ca51e52016-06-12 23:30:22 -060038 struct mmc_data *data)
39{
40 struct mmc *mmc = mmc_get_mmc_dev(dev);
41 struct dm_mmc_ops *ops = mmc_get_ops(dev);
42 int ret;
43
44 mmmc_trace_before_send(mmc, cmd);
45 if (ops->send_cmd)
46 ret = ops->send_cmd(dev, cmd, data);
47 else
48 ret = -ENOSYS;
49 mmmc_trace_after_send(mmc, cmd, ret);
50
51 return ret;
52}
53
54int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
55{
56 return dm_mmc_send_cmd(mmc->dev, cmd, data);
57}
58
Jaehoon Chungd77d61e2021-05-31 08:31:50 +090059static int dm_mmc_set_ios(struct udevice *dev)
Simon Glass8ca51e52016-06-12 23:30:22 -060060{
61 struct dm_mmc_ops *ops = mmc_get_ops(dev);
62
63 if (!ops->set_ios)
64 return -ENOSYS;
65 return ops->set_ios(dev);
66}
67
68int mmc_set_ios(struct mmc *mmc)
69{
70 return dm_mmc_set_ios(mmc->dev);
71}
72
Jaehoon Chungd77d61e2021-05-31 08:31:50 +090073static int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020074{
75 struct dm_mmc_ops *ops = mmc_get_ops(dev);
76
77 if (!ops->wait_dat0)
78 return -ENOSYS;
Sam Protsenko6cf8a902019-08-14 22:52:51 +030079 return ops->wait_dat0(dev, state, timeout_us);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020080}
81
Sam Protsenko6cf8a902019-08-14 22:52:51 +030082int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020083{
Sam Protsenko6cf8a902019-08-14 22:52:51 +030084 return dm_mmc_wait_dat0(mmc->dev, state, timeout_us);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020085}
86
Jaehoon Chungd77d61e2021-05-31 08:31:50 +090087static int dm_mmc_get_wp(struct udevice *dev)
Simon Glass8ca51e52016-06-12 23:30:22 -060088{
89 struct dm_mmc_ops *ops = mmc_get_ops(dev);
90
91 if (!ops->get_wp)
92 return -ENOSYS;
93 return ops->get_wp(dev);
94}
95
96int mmc_getwp(struct mmc *mmc)
97{
98 return dm_mmc_get_wp(mmc->dev);
99}
100
Jaehoon Chungd77d61e2021-05-31 08:31:50 +0900101static int dm_mmc_get_cd(struct udevice *dev)
Simon Glass8ca51e52016-06-12 23:30:22 -0600102{
103 struct dm_mmc_ops *ops = mmc_get_ops(dev);
104
105 if (!ops->get_cd)
106 return -ENOSYS;
107 return ops->get_cd(dev);
108}
109
110int mmc_getcd(struct mmc *mmc)
111{
112 return dm_mmc_get_cd(mmc->dev);
113}
Simon Glass8ca51e52016-06-12 23:30:22 -0600114
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100115#ifdef MMC_SUPPORTS_TUNING
Jaehoon Chungd77d61e2021-05-31 08:31:50 +0900116static int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +0200117{
118 struct dm_mmc_ops *ops = mmc_get_ops(dev);
119
120 if (!ops->execute_tuning)
121 return -ENOSYS;
122 return ops->execute_tuning(dev, opcode);
123}
124
125int mmc_execute_tuning(struct mmc *mmc, uint opcode)
126{
Marek Vasut8c220892024-02-24 23:32:10 +0100127 int ret;
128
129 mmc->tuning = true;
130 ret = dm_mmc_execute_tuning(mmc->dev, opcode);
131 mmc->tuning = false;
132
133 return ret;
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +0200134}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100135#endif
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +0200136
Peng Fan44acd492019-07-10 14:43:07 +0800137#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Jaehoon Chungd77d61e2021-05-31 08:31:50 +0900138static int dm_mmc_set_enhanced_strobe(struct udevice *dev)
Peng Fan44acd492019-07-10 14:43:07 +0800139{
140 struct dm_mmc_ops *ops = mmc_get_ops(dev);
141
142 if (ops->set_enhanced_strobe)
143 return ops->set_enhanced_strobe(dev);
144
145 return -ENOTSUPP;
146}
147
148int mmc_set_enhanced_strobe(struct mmc *mmc)
149{
150 return dm_mmc_set_enhanced_strobe(mmc->dev);
151}
152#endif
153
Jaehoon Chungd77d61e2021-05-31 08:31:50 +0900154static int dm_mmc_hs400_prepare_ddr(struct udevice *dev)
Yangbo Lud271e102020-09-01 16:58:04 +0800155{
156 struct dm_mmc_ops *ops = mmc_get_ops(dev);
157
158 if (ops->hs400_prepare_ddr)
159 return ops->hs400_prepare_ddr(dev);
160
161 return 0;
162}
163
164int mmc_hs400_prepare_ddr(struct mmc *mmc)
165{
166 return dm_mmc_hs400_prepare_ddr(mmc->dev);
167}
168
Jaehoon Chungd77d61e2021-05-31 08:31:50 +0900169static int dm_mmc_host_power_cycle(struct udevice *dev)
Yann Gautier3602a562019-09-19 17:56:12 +0200170{
171 struct dm_mmc_ops *ops = mmc_get_ops(dev);
172
173 if (ops->host_power_cycle)
174 return ops->host_power_cycle(dev);
175 return 0;
176}
177
178int mmc_host_power_cycle(struct mmc *mmc)
179{
180 return dm_mmc_host_power_cycle(mmc->dev);
181}
182
Jaehoon Chungd77d61e2021-05-31 08:31:50 +0900183static int dm_mmc_deferred_probe(struct udevice *dev)
Faiz Abbas32860bd2020-02-26 13:44:30 +0530184{
185 struct dm_mmc_ops *ops = mmc_get_ops(dev);
186
187 if (ops->deferred_probe)
188 return ops->deferred_probe(dev);
189
190 return 0;
191}
192
193int mmc_deferred_probe(struct mmc *mmc)
194{
195 return dm_mmc_deferred_probe(mmc->dev);
196}
197
Jaehoon Chungd77d61e2021-05-31 08:31:50 +0900198static int dm_mmc_reinit(struct udevice *dev)
Yangbo Lu390f9bd2020-09-01 16:57:59 +0800199{
200 struct dm_mmc_ops *ops = mmc_get_ops(dev);
201
202 if (ops->reinit)
203 return ops->reinit(dev);
204
205 return 0;
206}
207
208int mmc_reinit(struct mmc *mmc)
209{
210 return dm_mmc_reinit(mmc->dev);
211}
212
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100213int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200214{
215 int val;
216
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100217 val = dev_read_u32_default(dev, "bus-width", 1);
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200218
219 switch (val) {
220 case 0x8:
221 cfg->host_caps |= MMC_MODE_8BIT;
222 /* fall through */
223 case 0x4:
224 cfg->host_caps |= MMC_MODE_4BIT;
225 /* fall through */
226 case 0x1:
227 cfg->host_caps |= MMC_MODE_1BIT;
228 break;
229 default:
Masahiro Yamada4b28f7b2017-12-30 02:00:07 +0900230 dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
231 return -EINVAL;
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200232 }
233
Masahiro Yamadac42ee362017-12-30 02:00:06 +0900234 /* f_max is obtained from the optional "max-frequency" property */
235 dev_read_u32(dev, "max-frequency", &cfg->f_max);
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200236
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100237 if (dev_read_bool(dev, "cap-sd-highspeed"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200238 cfg->host_caps |= MMC_CAP(SD_HS);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100239 if (dev_read_bool(dev, "cap-mmc-highspeed"))
Alexandru Gagniucf80b8ac2020-09-15 14:51:46 -0500240 cfg->host_caps |= MMC_CAP(MMC_HS) | MMC_CAP(MMC_HS_52);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100241 if (dev_read_bool(dev, "sd-uhs-sdr12"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200242 cfg->host_caps |= MMC_CAP(UHS_SDR12);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100243 if (dev_read_bool(dev, "sd-uhs-sdr25"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200244 cfg->host_caps |= MMC_CAP(UHS_SDR25);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100245 if (dev_read_bool(dev, "sd-uhs-sdr50"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200246 cfg->host_caps |= MMC_CAP(UHS_SDR50);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100247 if (dev_read_bool(dev, "sd-uhs-sdr104"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200248 cfg->host_caps |= MMC_CAP(UHS_SDR104);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100249 if (dev_read_bool(dev, "sd-uhs-ddr50"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200250 cfg->host_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100251 if (dev_read_bool(dev, "mmc-ddr-1_8v"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200252 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
Jean-Jacques Hiblot7abff2c2017-11-30 17:43:55 +0100253 if (dev_read_bool(dev, "mmc-ddr-1_2v"))
254 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
255 if (dev_read_bool(dev, "mmc-hs200-1_8v"))
256 cfg->host_caps |= MMC_CAP(MMC_HS_200);
257 if (dev_read_bool(dev, "mmc-hs200-1_2v"))
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200258 cfg->host_caps |= MMC_CAP(MMC_HS_200);
Marek Vasuteb2acba2018-06-13 06:50:16 +0200259 if (dev_read_bool(dev, "mmc-hs400-1_8v"))
260 cfg->host_caps |= MMC_CAP(MMC_HS_400);
261 if (dev_read_bool(dev, "mmc-hs400-1_2v"))
262 cfg->host_caps |= MMC_CAP(MMC_HS_400);
Peng Fanb0fc3122019-07-10 09:35:18 +0000263 if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe"))
264 cfg->host_caps |= MMC_CAP(MMC_HS_400_ES);
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200265
T Karthik Reddy86a94e72019-06-25 13:39:02 +0200266 if (dev_read_bool(dev, "non-removable")) {
267 cfg->host_caps |= MMC_CAP_NONREMOVABLE;
268 } else {
269 if (dev_read_bool(dev, "cd-inverted"))
270 cfg->host_caps |= MMC_CAP_CD_ACTIVE_HIGH;
271 if (dev_read_bool(dev, "broken-cd"))
272 cfg->host_caps |= MMC_CAP_NEEDS_POLL;
273 }
274
Peng Fan479a8dd2019-07-10 09:35:20 +0000275 if (dev_read_bool(dev, "no-1-8-v")) {
276 cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 |
277 MMC_MODE_HS400 | MMC_MODE_HS400_ES);
278 }
279
Kishon Vijay Abraham I9215ef52017-09-21 16:30:13 +0200280 return 0;
281}
282
Simon Glass3a905cd2020-04-08 08:33:00 -0600283struct mmc *mmc_get_mmc_dev(const struct udevice *dev)
Simon Glasse7ecf7c2015-06-23 15:38:48 -0600284{
285 struct mmc_uclass_priv *upriv;
286
287 if (!device_active(dev))
288 return NULL;
289 upriv = dev_get_uclass_priv(dev);
290 return upriv->mmc;
291}
292
Simon Glassc4d660d2017-07-04 13:31:19 -0600293#if CONFIG_IS_ENABLED(BLK)
Simon Glass8ef761e2016-05-01 13:52:39 -0600294struct mmc *find_mmc_device(int dev_num)
295{
296 struct udevice *dev, *mmc_dev;
297 int ret;
298
Simon Glasse33a5c62022-08-11 19:34:59 -0600299 ret = blk_find_device(UCLASS_MMC, dev_num, &dev);
Simon Glass8ef761e2016-05-01 13:52:39 -0600300
301 if (ret) {
302#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
303 printf("MMC Device %d not found\n", dev_num);
304#endif
305 return NULL;
306 }
307
308 mmc_dev = dev_get_parent(dev);
309
Simon Glass97525642017-05-27 11:37:19 -0600310 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
311
312 return mmc;
Simon Glass8ef761e2016-05-01 13:52:39 -0600313}
314
315int get_mmc_num(void)
316{
Simon Glasse33a5c62022-08-11 19:34:59 -0600317 return max((blk_find_max_devnum(UCLASS_MMC) + 1), 0);
Simon Glass8ef761e2016-05-01 13:52:39 -0600318}
319
320int mmc_get_next_devnum(void)
321{
Simon Glasse33a5c62022-08-11 19:34:59 -0600322 return blk_find_max_devnum(UCLASS_MMC);
Simon Glass8ef761e2016-05-01 13:52:39 -0600323}
324
Simon Glassb8aa4632022-04-24 23:31:14 -0600325int mmc_get_blk(struct udevice *dev, struct udevice **blkp)
326{
327 struct udevice *blk;
328 int ret;
329
330 device_find_first_child_by_uclass(dev, UCLASS_BLK, &blk);
331 ret = device_probe(blk);
332 if (ret)
333 return ret;
334 *blkp = blk;
335
336 return 0;
337}
338
Simon Glass8ef761e2016-05-01 13:52:39 -0600339struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
340{
341 struct blk_desc *desc;
342 struct udevice *dev;
343
Simon Glassce34a662021-10-23 17:26:00 -0600344 device_find_first_child_by_uclass(mmc->dev, UCLASS_BLK, &dev);
Simon Glass8ef761e2016-05-01 13:52:39 -0600345 if (!dev)
346 return NULL;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700347 desc = dev_get_uclass_plat(dev);
Simon Glass8ef761e2016-05-01 13:52:39 -0600348
349 return desc;
350}
351
352void mmc_do_preinit(void)
353{
354 struct udevice *dev;
355 struct uclass *uc;
356 int ret;
357
358 ret = uclass_get(UCLASS_MMC, &uc);
359 if (ret)
360 return;
361 uclass_foreach_dev(dev, uc) {
362 struct mmc *m = mmc_get_mmc_dev(dev);
363
364 if (!m)
365 continue;
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530366
367 m->user_speed_mode = MMC_MODES_END; /* Initialising user set speed mode */
368
Simon Glass8ef761e2016-05-01 13:52:39 -0600369 if (m->preinit)
370 mmc_start_init(m);
371 }
372}
373
374#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
375void print_mmc_devices(char separator)
376{
377 struct udevice *dev;
378 char *mmc_type;
379 bool first = true;
380
381 for (uclass_first_device(UCLASS_MMC, &dev);
382 dev;
Xu Ziyuan1bd4f922016-07-23 11:11:22 +0800383 uclass_next_device(&dev), first = false) {
Simon Glass8ef761e2016-05-01 13:52:39 -0600384 struct mmc *m = mmc_get_mmc_dev(dev);
385
386 if (!first) {
387 printf("%c", separator);
388 if (separator != '\n')
389 puts(" ");
390 }
391 if (m->has_init)
392 mmc_type = IS_SD(m) ? "SD" : "eMMC";
393 else
394 mmc_type = NULL;
395
396 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
397 if (mmc_type)
398 printf(" (%s)", mmc_type);
399 }
400
401 printf("\n");
402}
403
404#else
405void print_mmc_devices(char separator) { }
406#endif
Simon Glasseede8972016-06-12 23:30:16 -0600407
408int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
409{
410 struct blk_desc *bdesc;
411 struct udevice *bdev;
Aswath Govindraju2243d192021-03-25 12:48:48 +0530412 int ret;
Simon Glasseede8972016-06-12 23:30:16 -0600413
Simon Glass66656022017-04-23 20:02:09 -0600414 if (!mmc_get_ops(dev))
415 return -ENOSYS;
Aswath Govindraju2243d192021-03-25 12:48:48 +0530416
417 /* Use the fixed index with aliases node's index */
418 debug("%s: alias devnum=%d\n", __func__, dev_seq(dev));
Jaehoon Chung02ad33a2017-02-02 13:41:14 +0900419
Simon Glasse33a5c62022-08-11 19:34:59 -0600420 ret = blk_create_devicef(dev, "mmc_blk", "blk", UCLASS_MMC,
Bin Meng7020b2e2023-09-26 16:43:31 +0800421 dev_seq(dev), DEFAULT_BLKSZ, 0, &bdev);
Simon Glasseede8972016-06-12 23:30:16 -0600422 if (ret) {
423 debug("Cannot create block device\n");
424 return ret;
425 }
Simon Glasscaa4daa2020-12-03 16:55:18 -0700426 bdesc = dev_get_uclass_plat(bdev);
Simon Glasseede8972016-06-12 23:30:16 -0600427 mmc->cfg = cfg;
428 mmc->priv = dev;
429
Simon Glassd7d78572023-07-30 11:15:14 -0600430 ret = bootdev_setup_for_sibling_blk(bdev, "mmc_bootdev");
Simon Glassb8aa4632022-04-24 23:31:14 -0600431 if (ret)
432 return log_msg_ret("bootdev", ret);
433
Simon Glasseede8972016-06-12 23:30:16 -0600434 /* the following chunk was from mmc_register() */
435
436 /* Setup dsr related values */
437 mmc->dsr_imp = 0;
438 mmc->dsr = 0xffffffff;
439 /* Setup the universal parts of the block interface just once */
440 bdesc->removable = 1;
441
442 /* setup initial part type */
443 bdesc->part_type = cfg->part_type;
444 mmc->dev = dev;
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530445 mmc->user_speed_mode = MMC_MODES_END;
Simon Glasseede8972016-06-12 23:30:16 -0600446 return 0;
447}
448
449int mmc_unbind(struct udevice *dev)
450{
451 struct udevice *bdev;
Simon Glassb8aa4632022-04-24 23:31:14 -0600452 int ret;
Simon Glasseede8972016-06-12 23:30:16 -0600453
Simon Glassce34a662021-10-23 17:26:00 -0600454 device_find_first_child_by_uclass(dev, UCLASS_BLK, &bdev);
Simon Glasseede8972016-06-12 23:30:16 -0600455 if (bdev) {
Stefan Roese706865a2017-03-20 12:51:48 +0100456 device_remove(bdev, DM_REMOVE_NORMAL);
Simon Glasseede8972016-06-12 23:30:16 -0600457 device_unbind(bdev);
458 }
Simon Glassb8aa4632022-04-24 23:31:14 -0600459 ret = bootdev_unbind_dev(dev);
460 if (ret)
461 return log_msg_ret("bootdev", ret);
Simon Glasseede8972016-06-12 23:30:16 -0600462
463 return 0;
464}
465
466static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
467{
468 struct udevice *mmc_dev = dev_get_parent(bdev);
469 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
Simon Glasscaa4daa2020-12-03 16:55:18 -0700470 struct blk_desc *desc = dev_get_uclass_plat(bdev);
Weijie Gao47b7fa32019-08-27 15:32:19 +0800471 int ret;
Simon Glasseede8972016-06-12 23:30:16 -0600472
473 if (desc->hwpart == hwpart)
474 return 0;
475
476 if (mmc->part_config == MMCPART_NOAVAILABLE)
477 return -EMEDIUMTYPE;
478
Weijie Gao47b7fa32019-08-27 15:32:19 +0800479 ret = mmc_switch_part(mmc, hwpart);
480 if (!ret)
Simon Glass8149b152022-09-17 09:00:09 -0600481 blkcache_invalidate(desc->uclass_id, desc->devnum);
Weijie Gao47b7fa32019-08-27 15:32:19 +0800482
483 return ret;
Simon Glasseede8972016-06-12 23:30:16 -0600484}
485
Fiach Antawa0269bb2017-01-25 19:00:24 +1000486static int mmc_blk_probe(struct udevice *dev)
487{
Simon Glass854f9a72017-04-23 20:02:10 -0600488 struct udevice *mmc_dev = dev_get_parent(dev);
489 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
490 struct mmc *mmc = upriv->mmc;
491 int ret;
Fiach Antawa0269bb2017-01-25 19:00:24 +1000492
Simon Glass854f9a72017-04-23 20:02:10 -0600493 ret = mmc_init(mmc);
494 if (ret) {
495 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
496 return ret;
497 }
498
AKASHI Takahiroc822c1a2022-03-08 20:36:41 +0900499 ret = device_probe(dev);
500 if (ret) {
501 debug("Probing %s failed (err=%d)\n", dev->name, ret);
502
503 if (CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) ||
504 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) ||
505 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT))
506 mmc_deinit(mmc);
507
508 return ret;
509 }
510
Simon Glass854f9a72017-04-23 20:02:10 -0600511 return 0;
Fiach Antawa0269bb2017-01-25 19:00:24 +1000512}
513
Marek Vasutfceea992019-01-29 04:45:51 +0100514#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
515 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
516 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
517static int mmc_blk_remove(struct udevice *dev)
518{
519 struct udevice *mmc_dev = dev_get_parent(dev);
520 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
521 struct mmc *mmc = upriv->mmc;
522
523 return mmc_deinit(mmc);
524}
525#endif
526
Simon Glasseede8972016-06-12 23:30:16 -0600527static const struct blk_ops mmc_blk_ops = {
528 .read = mmc_bread,
Jean-Jacques Hiblotd6400c32018-01-04 15:23:32 +0100529#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glasseede8972016-06-12 23:30:16 -0600530 .write = mmc_bwrite,
Simon Glass561e6242016-10-01 14:43:17 -0600531 .erase = mmc_berase,
Simon Glasseede8972016-06-12 23:30:16 -0600532#endif
533 .select_hwpart = mmc_select_hwpart,
534};
535
536U_BOOT_DRIVER(mmc_blk) = {
537 .name = "mmc_blk",
538 .id = UCLASS_BLK,
539 .ops = &mmc_blk_ops,
Fiach Antawa0269bb2017-01-25 19:00:24 +1000540 .probe = mmc_blk_probe,
Marek Vasutfceea992019-01-29 04:45:51 +0100541#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
542 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
543 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
544 .remove = mmc_blk_remove,
545 .flags = DM_FLAG_OS_PREPARE,
546#endif
Simon Glasseede8972016-06-12 23:30:16 -0600547};
Simon Glass8ef761e2016-05-01 13:52:39 -0600548#endif /* CONFIG_BLK */
549
Simon Glasse7ecf7c2015-06-23 15:38:48 -0600550
551UCLASS_DRIVER(mmc) = {
552 .id = UCLASS_MMC,
553 .name = "mmc",
554 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass41575d82020-12-03 16:55:17 -0700555 .per_device_auto = sizeof(struct mmc_uclass_priv),
Simon Glasse7ecf7c2015-06-23 15:38:48 -0600556};