blob: 24170c59ecc1b3aeb3e92d022ad051f6f2dbaf18 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass1e8eb1b2015-06-23 15:38:48 -06002/*
3 * Copyright (C) 2015 Google, Inc
Yangbo Luf9049b22020-06-17 18:08:58 +08004 * Copyright 2020 NXP
Simon Glass1e8eb1b2015-06-23 15:38:48 -06005 * Written by Simon Glass <sjg@chromium.org>
Simon Glass1e8eb1b2015-06-23 15:38:48 -06006 */
7
Patrick Delaunay81313352021-04-27 11:02:19 +02008#define LOG_CATEGORY UCLASS_MMC
9
Tom Riniabb9a042024-05-18 20:20:43 -060010#include <common.h>
Simon Glassf8b4a292022-04-24 23:31:14 -060011#include <bootdev.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass1e8eb1b2015-06-23 15:38:48 -060013#include <mmc.h>
14#include <dm.h>
Simon Glass2f1ea902016-06-12 23:30:16 -060015#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glass1e8eb1b2015-06-23 15:38:48 -060017#include <dm/lists.h>
Simon Glass9bc15642020-02-03 07:36:16 -070018#include <linux/compat.h>
Simon Glass2f1ea902016-06-12 23:30:16 -060019#include "mmc_private.h"
Simon Glass1e8eb1b2015-06-23 15:38:48 -060020
Jaehoon Chungff6bf1f2021-05-31 08:31:50 +090021static int dm_mmc_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
Marek Vasut31976d92020-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 Chungff6bf1f2021-05-31 08:31:50 +090037static int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glass394dfc02016-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 Chungff6bf1f2021-05-31 08:31:50 +090059static int dm_mmc_set_ios(struct udevice *dev)
Simon Glass394dfc02016-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 Chungff6bf1f2021-05-31 08:31:50 +090073static int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
Jean-Jacques Hiblotf4d5b3e2017-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 Protsenkodb174c62019-08-14 22:52:51 +030079 return ops->wait_dat0(dev, state, timeout_us);
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +020080}
81
Sam Protsenkodb174c62019-08-14 22:52:51 +030082int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +020083{
Sam Protsenkodb174c62019-08-14 22:52:51 +030084 return dm_mmc_wait_dat0(mmc->dev, state, timeout_us);
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +020085}
86
Jaehoon Chungff6bf1f2021-05-31 08:31:50 +090087static int dm_mmc_get_wp(struct udevice *dev)
Simon Glass394dfc02016-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 Chungff6bf1f2021-05-31 08:31:50 +0900101static int dm_mmc_get_cd(struct udevice *dev)
Simon Glass394dfc02016-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 Glass394dfc02016-06-12 23:30:22 -0600114
Tom Riniabb9a042024-05-18 20:20:43 -0600115#ifdef MMC_SUPPORTS_TUNING
Jaehoon Chungff6bf1f2021-05-31 08:31:50 +0900116static int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
Kishon Vijay Abraham Iae7174f2017-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 Vasutb6e86292024-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 Iae7174f2017-09-21 16:30:05 +0200134}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100135#endif
Kishon Vijay Abraham Iae7174f2017-09-21 16:30:05 +0200136
Peng Faneede83b2019-07-10 14:43:07 +0800137#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Jaehoon Chungff6bf1f2021-05-31 08:31:50 +0900138static int dm_mmc_set_enhanced_strobe(struct udevice *dev)
Peng Faneede83b2019-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 Chungff6bf1f2021-05-31 08:31:50 +0900154static int dm_mmc_hs400_prepare_ddr(struct udevice *dev)
Yangbo Lu5347aea2020-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 Chungff6bf1f2021-05-31 08:31:50 +0900169static int dm_mmc_host_power_cycle(struct udevice *dev)
Yann Gautier6f558332019-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 Chungff6bf1f2021-05-31 08:31:50 +0900183static int dm_mmc_deferred_probe(struct udevice *dev)
Faiz Abbasf6fd4ec2020-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 Chungff6bf1f2021-05-31 08:31:50 +0900198static int dm_mmc_reinit(struct udevice *dev)
Yangbo Luc46f5d72020-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 Hiblotd39be652017-11-30 17:43:55 +0100213int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200214{
215 int val;
216
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100217 val = dev_read_u32_default(dev, "bus-width", 1);
Kishon Vijay Abraham I422b2942017-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 Yamada57b12d82017-12-30 02:00:07 +0900230 dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
231 return -EINVAL;
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200232 }
233
Masahiro Yamada191cd132017-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 I422b2942017-09-21 16:30:13 +0200236
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100237 if (dev_read_bool(dev, "cap-sd-highspeed"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200238 cfg->host_caps |= MMC_CAP(SD_HS);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100239 if (dev_read_bool(dev, "cap-mmc-highspeed"))
Alexandru Gagniucd6ff68d2020-09-15 14:51:46 -0500240 cfg->host_caps |= MMC_CAP(MMC_HS) | MMC_CAP(MMC_HS_52);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100241 if (dev_read_bool(dev, "sd-uhs-sdr12"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200242 cfg->host_caps |= MMC_CAP(UHS_SDR12);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100243 if (dev_read_bool(dev, "sd-uhs-sdr25"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200244 cfg->host_caps |= MMC_CAP(UHS_SDR25);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100245 if (dev_read_bool(dev, "sd-uhs-sdr50"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200246 cfg->host_caps |= MMC_CAP(UHS_SDR50);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100247 if (dev_read_bool(dev, "sd-uhs-sdr104"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200248 cfg->host_caps |= MMC_CAP(UHS_SDR104);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100249 if (dev_read_bool(dev, "sd-uhs-ddr50"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200250 cfg->host_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100251 if (dev_read_bool(dev, "mmc-ddr-1_8v"))
252 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
253 if (dev_read_bool(dev, "mmc-ddr-1_2v"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200254 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100255 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 I422b2942017-09-21 16:30:13 +0200258 cfg->host_caps |= MMC_CAP(MMC_HS_200);
Marek Vasutb22a11a2018-06-13 06:50:16 +0200259 if (dev_read_bool(dev, "mmc-hs400-1_8v"))
Jonas Karlmanf23ac282024-04-08 21:06:15 +0000260 cfg->host_caps |= MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_200);
Marek Vasutb22a11a2018-06-13 06:50:16 +0200261 if (dev_read_bool(dev, "mmc-hs400-1_2v"))
Jonas Karlmanf23ac282024-04-08 21:06:15 +0000262 cfg->host_caps |= MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_200);
Peng Fanfb968792019-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);
Jonas Karlman925f6502024-04-08 21:06:16 +0000265 if (dev_read_bool(dev, "no-mmc-hs400"))
266 cfg->host_caps &= ~(MMC_CAP(MMC_HS_400) |
267 MMC_CAP(MMC_HS_400_ES));
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200268
T Karthik Reddyd0bb5162019-06-25 13:39:02 +0200269 if (dev_read_bool(dev, "non-removable")) {
270 cfg->host_caps |= MMC_CAP_NONREMOVABLE;
271 } else {
272 if (dev_read_bool(dev, "cd-inverted"))
273 cfg->host_caps |= MMC_CAP_CD_ACTIVE_HIGH;
274 if (dev_read_bool(dev, "broken-cd"))
275 cfg->host_caps |= MMC_CAP_NEEDS_POLL;
276 }
277
Peng Fanb01c7732019-07-10 09:35:20 +0000278 if (dev_read_bool(dev, "no-1-8-v")) {
279 cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 |
280 MMC_MODE_HS400 | MMC_MODE_HS400_ES);
281 }
282
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200283 return 0;
284}
285
Simon Glass5a18f872020-04-08 08:33:00 -0600286struct mmc *mmc_get_mmc_dev(const struct udevice *dev)
Simon Glass1e8eb1b2015-06-23 15:38:48 -0600287{
288 struct mmc_uclass_priv *upriv;
289
290 if (!device_active(dev))
291 return NULL;
292 upriv = dev_get_uclass_priv(dev);
293 return upriv->mmc;
294}
295
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600296#if CONFIG_IS_ENABLED(BLK)
Simon Glass4bca6662016-05-01 13:52:39 -0600297struct mmc *find_mmc_device(int dev_num)
298{
299 struct udevice *dev, *mmc_dev;
300 int ret;
301
Simon Glassdbfa32c2022-08-11 19:34:59 -0600302 ret = blk_find_device(UCLASS_MMC, dev_num, &dev);
Simon Glass4bca6662016-05-01 13:52:39 -0600303
304 if (ret) {
305#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
306 printf("MMC Device %d not found\n", dev_num);
307#endif
308 return NULL;
309 }
310
311 mmc_dev = dev_get_parent(dev);
312
Simon Glassbcad6042017-05-27 11:37:19 -0600313 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
314
315 return mmc;
Simon Glass4bca6662016-05-01 13:52:39 -0600316}
317
318int get_mmc_num(void)
319{
Simon Glassdbfa32c2022-08-11 19:34:59 -0600320 return max((blk_find_max_devnum(UCLASS_MMC) + 1), 0);
Simon Glass4bca6662016-05-01 13:52:39 -0600321}
322
323int mmc_get_next_devnum(void)
324{
Simon Glassdbfa32c2022-08-11 19:34:59 -0600325 return blk_find_max_devnum(UCLASS_MMC);
Simon Glass4bca6662016-05-01 13:52:39 -0600326}
327
Simon Glassf8b4a292022-04-24 23:31:14 -0600328int mmc_get_blk(struct udevice *dev, struct udevice **blkp)
329{
330 struct udevice *blk;
331 int ret;
332
333 device_find_first_child_by_uclass(dev, UCLASS_BLK, &blk);
334 ret = device_probe(blk);
335 if (ret)
336 return ret;
337 *blkp = blk;
338
339 return 0;
340}
341
Simon Glass4bca6662016-05-01 13:52:39 -0600342struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
343{
344 struct blk_desc *desc;
345 struct udevice *dev;
346
Simon Glass3a296642021-10-23 17:26:00 -0600347 device_find_first_child_by_uclass(mmc->dev, UCLASS_BLK, &dev);
Simon Glass4bca6662016-05-01 13:52:39 -0600348 if (!dev)
349 return NULL;
Simon Glass71fa5b42020-12-03 16:55:18 -0700350 desc = dev_get_uclass_plat(dev);
Simon Glass4bca6662016-05-01 13:52:39 -0600351
352 return desc;
353}
354
355void mmc_do_preinit(void)
356{
357 struct udevice *dev;
358 struct uclass *uc;
359 int ret;
360
361 ret = uclass_get(UCLASS_MMC, &uc);
362 if (ret)
363 return;
364 uclass_foreach_dev(dev, uc) {
365 struct mmc *m = mmc_get_mmc_dev(dev);
366
367 if (!m)
368 continue;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530369
370 m->user_speed_mode = MMC_MODES_END; /* Initialising user set speed mode */
371
Simon Glass4bca6662016-05-01 13:52:39 -0600372 if (m->preinit)
373 mmc_start_init(m);
374 }
375}
376
377#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
378void print_mmc_devices(char separator)
379{
380 struct udevice *dev;
381 char *mmc_type;
382 bool first = true;
383
384 for (uclass_first_device(UCLASS_MMC, &dev);
385 dev;
Xu Ziyuan77d747e2016-07-23 11:11:22 +0800386 uclass_next_device(&dev), first = false) {
Simon Glass4bca6662016-05-01 13:52:39 -0600387 struct mmc *m = mmc_get_mmc_dev(dev);
388
389 if (!first) {
390 printf("%c", separator);
391 if (separator != '\n')
392 puts(" ");
393 }
394 if (m->has_init)
395 mmc_type = IS_SD(m) ? "SD" : "eMMC";
396 else
397 mmc_type = NULL;
398
399 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
400 if (mmc_type)
401 printf(" (%s)", mmc_type);
402 }
403
404 printf("\n");
405}
406
407#else
408void print_mmc_devices(char separator) { }
409#endif
Simon Glass2f1ea902016-06-12 23:30:16 -0600410
411int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
412{
413 struct blk_desc *bdesc;
414 struct udevice *bdev;
Aswath Govindrajub9f84af2021-03-25 12:48:48 +0530415 int ret;
Jaehoon Chung2910da82017-02-02 13:41:14 +0900416
Simon Glass9e720b02017-04-23 20:02:09 -0600417 if (!mmc_get_ops(dev))
418 return -ENOSYS;
Aswath Govindrajub9f84af2021-03-25 12:48:48 +0530419
420 /* Use the fixed index with aliases node's index */
421 debug("%s: alias devnum=%d\n", __func__, dev_seq(dev));
Simon Glass2f1ea902016-06-12 23:30:16 -0600422
Simon Glassdbfa32c2022-08-11 19:34:59 -0600423 ret = blk_create_devicef(dev, "mmc_blk", "blk", UCLASS_MMC,
Bin Meng2294ecb2023-09-26 16:43:31 +0800424 dev_seq(dev), DEFAULT_BLKSZ, 0, &bdev);
Simon Glass2f1ea902016-06-12 23:30:16 -0600425 if (ret) {
426 debug("Cannot create block device\n");
427 return ret;
428 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700429 bdesc = dev_get_uclass_plat(bdev);
Simon Glass2f1ea902016-06-12 23:30:16 -0600430 mmc->cfg = cfg;
431 mmc->priv = dev;
432
Simon Glassb1d581d2023-07-30 11:15:14 -0600433 ret = bootdev_setup_for_sibling_blk(bdev, "mmc_bootdev");
Simon Glassf8b4a292022-04-24 23:31:14 -0600434 if (ret)
435 return log_msg_ret("bootdev", ret);
436
Simon Glass2f1ea902016-06-12 23:30:16 -0600437 /* the following chunk was from mmc_register() */
438
439 /* Setup dsr related values */
440 mmc->dsr_imp = 0;
441 mmc->dsr = 0xffffffff;
442 /* Setup the universal parts of the block interface just once */
443 bdesc->removable = 1;
444
445 /* setup initial part type */
446 bdesc->part_type = cfg->part_type;
447 mmc->dev = dev;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530448 mmc->user_speed_mode = MMC_MODES_END;
Simon Glass2f1ea902016-06-12 23:30:16 -0600449 return 0;
450}
451
452int mmc_unbind(struct udevice *dev)
453{
454 struct udevice *bdev;
Simon Glassf8b4a292022-04-24 23:31:14 -0600455 int ret;
Simon Glass2f1ea902016-06-12 23:30:16 -0600456
Simon Glass3a296642021-10-23 17:26:00 -0600457 device_find_first_child_by_uclass(dev, UCLASS_BLK, &bdev);
Simon Glass2f1ea902016-06-12 23:30:16 -0600458 if (bdev) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100459 device_remove(bdev, DM_REMOVE_NORMAL);
Simon Glass2f1ea902016-06-12 23:30:16 -0600460 device_unbind(bdev);
461 }
Simon Glassf8b4a292022-04-24 23:31:14 -0600462 ret = bootdev_unbind_dev(dev);
463 if (ret)
464 return log_msg_ret("bootdev", ret);
Simon Glass2f1ea902016-06-12 23:30:16 -0600465
466 return 0;
467}
468
469static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
470{
471 struct udevice *mmc_dev = dev_get_parent(bdev);
472 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700473 struct blk_desc *desc = dev_get_uclass_plat(bdev);
developer90fdc3e2019-08-27 15:32:19 +0800474 int ret;
Simon Glass2f1ea902016-06-12 23:30:16 -0600475
476 if (desc->hwpart == hwpart)
477 return 0;
478
479 if (mmc->part_config == MMCPART_NOAVAILABLE)
480 return -EMEDIUMTYPE;
481
developer90fdc3e2019-08-27 15:32:19 +0800482 ret = mmc_switch_part(mmc, hwpart);
483 if (!ret)
Simon Glassfada3f92022-09-17 09:00:09 -0600484 blkcache_invalidate(desc->uclass_id, desc->devnum);
developer90fdc3e2019-08-27 15:32:19 +0800485
486 return ret;
Simon Glass2f1ea902016-06-12 23:30:16 -0600487}
488
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000489static int mmc_blk_probe(struct udevice *dev)
490{
Simon Glasse11b2392017-04-23 20:02:10 -0600491 struct udevice *mmc_dev = dev_get_parent(dev);
492 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
493 struct mmc *mmc = upriv->mmc;
494 int ret;
495
496 ret = mmc_init(mmc);
497 if (ret) {
498 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
499 return ret;
500 }
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000501
AKASHI Takahiro80022d72022-03-08 20:36:41 +0900502 ret = device_probe(dev);
503 if (ret) {
504 debug("Probing %s failed (err=%d)\n", dev->name, ret);
505
Marek Vasut67c77f92024-03-17 04:01:22 +0100506 mmc_deinit(mmc);
AKASHI Takahiro80022d72022-03-08 20:36:41 +0900507
508 return ret;
509 }
510
Simon Glasse11b2392017-04-23 20:02:10 -0600511 return 0;
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000512}
513
Marek Vasuta4773fc2019-01-29 04:45:51 +0100514static int mmc_blk_remove(struct udevice *dev)
515{
516 struct udevice *mmc_dev = dev_get_parent(dev);
517 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
518 struct mmc *mmc = upriv->mmc;
519
520 return mmc_deinit(mmc);
521}
Marek Vasuta4773fc2019-01-29 04:45:51 +0100522
Simon Glass2f1ea902016-06-12 23:30:16 -0600523static const struct blk_ops mmc_blk_ops = {
524 .read = mmc_bread,
Jean-Jacques Hiblotd0531672018-01-04 15:23:32 +0100525#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glass2f1ea902016-06-12 23:30:16 -0600526 .write = mmc_bwrite,
Simon Glassfcc53c12016-10-01 14:43:17 -0600527 .erase = mmc_berase,
Simon Glass2f1ea902016-06-12 23:30:16 -0600528#endif
529 .select_hwpart = mmc_select_hwpart,
530};
531
532U_BOOT_DRIVER(mmc_blk) = {
533 .name = "mmc_blk",
534 .id = UCLASS_BLK,
535 .ops = &mmc_blk_ops,
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000536 .probe = mmc_blk_probe,
Marek Vasuta4773fc2019-01-29 04:45:51 +0100537 .remove = mmc_blk_remove,
538 .flags = DM_FLAG_OS_PREPARE,
Simon Glass2f1ea902016-06-12 23:30:16 -0600539};
Simon Glass4bca6662016-05-01 13:52:39 -0600540#endif /* CONFIG_BLK */
541
Simon Glass1e8eb1b2015-06-23 15:38:48 -0600542
543UCLASS_DRIVER(mmc) = {
544 .id = UCLASS_MMC,
545 .name = "mmc",
546 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700547 .per_device_auto = sizeof(struct mmc_uclass_priv),
Simon Glass1e8eb1b2015-06-23 15:38:48 -0600548};