blob: 0e157672eae0b0707ea333145761435b4916ef74 [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
Simon Glass1e8eb1b2015-06-23 15:38:48 -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
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100115#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{
127 return dm_mmc_execute_tuning(mmc->dev, opcode);
128}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100129#endif
Kishon Vijay Abraham Iae7174f2017-09-21 16:30:05 +0200130
Peng Faneede83b2019-07-10 14:43:07 +0800131#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Jaehoon Chungff6bf1f2021-05-31 08:31:50 +0900132static int dm_mmc_set_enhanced_strobe(struct udevice *dev)
Peng Faneede83b2019-07-10 14:43:07 +0800133{
134 struct dm_mmc_ops *ops = mmc_get_ops(dev);
135
136 if (ops->set_enhanced_strobe)
137 return ops->set_enhanced_strobe(dev);
138
139 return -ENOTSUPP;
140}
141
142int mmc_set_enhanced_strobe(struct mmc *mmc)
143{
144 return dm_mmc_set_enhanced_strobe(mmc->dev);
145}
146#endif
147
Jaehoon Chungff6bf1f2021-05-31 08:31:50 +0900148static int dm_mmc_hs400_prepare_ddr(struct udevice *dev)
Yangbo Lu5347aea2020-09-01 16:58:04 +0800149{
150 struct dm_mmc_ops *ops = mmc_get_ops(dev);
151
152 if (ops->hs400_prepare_ddr)
153 return ops->hs400_prepare_ddr(dev);
154
155 return 0;
156}
157
158int mmc_hs400_prepare_ddr(struct mmc *mmc)
159{
160 return dm_mmc_hs400_prepare_ddr(mmc->dev);
161}
162
Jaehoon Chungff6bf1f2021-05-31 08:31:50 +0900163static int dm_mmc_host_power_cycle(struct udevice *dev)
Yann Gautier6f558332019-09-19 17:56:12 +0200164{
165 struct dm_mmc_ops *ops = mmc_get_ops(dev);
166
167 if (ops->host_power_cycle)
168 return ops->host_power_cycle(dev);
169 return 0;
170}
171
172int mmc_host_power_cycle(struct mmc *mmc)
173{
174 return dm_mmc_host_power_cycle(mmc->dev);
175}
176
Jaehoon Chungff6bf1f2021-05-31 08:31:50 +0900177static int dm_mmc_deferred_probe(struct udevice *dev)
Faiz Abbasf6fd4ec2020-02-26 13:44:30 +0530178{
179 struct dm_mmc_ops *ops = mmc_get_ops(dev);
180
181 if (ops->deferred_probe)
182 return ops->deferred_probe(dev);
183
184 return 0;
185}
186
187int mmc_deferred_probe(struct mmc *mmc)
188{
189 return dm_mmc_deferred_probe(mmc->dev);
190}
191
Jaehoon Chungff6bf1f2021-05-31 08:31:50 +0900192static int dm_mmc_reinit(struct udevice *dev)
Yangbo Luc46f5d72020-09-01 16:57:59 +0800193{
194 struct dm_mmc_ops *ops = mmc_get_ops(dev);
195
196 if (ops->reinit)
197 return ops->reinit(dev);
198
199 return 0;
200}
201
202int mmc_reinit(struct mmc *mmc)
203{
204 return dm_mmc_reinit(mmc->dev);
205}
206
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100207int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200208{
209 int val;
210
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100211 val = dev_read_u32_default(dev, "bus-width", 1);
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200212
213 switch (val) {
214 case 0x8:
215 cfg->host_caps |= MMC_MODE_8BIT;
216 /* fall through */
217 case 0x4:
218 cfg->host_caps |= MMC_MODE_4BIT;
219 /* fall through */
220 case 0x1:
221 cfg->host_caps |= MMC_MODE_1BIT;
222 break;
223 default:
Masahiro Yamada57b12d82017-12-30 02:00:07 +0900224 dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
225 return -EINVAL;
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200226 }
227
Masahiro Yamada191cd132017-12-30 02:00:06 +0900228 /* f_max is obtained from the optional "max-frequency" property */
229 dev_read_u32(dev, "max-frequency", &cfg->f_max);
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200230
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100231 if (dev_read_bool(dev, "cap-sd-highspeed"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200232 cfg->host_caps |= MMC_CAP(SD_HS);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100233 if (dev_read_bool(dev, "cap-mmc-highspeed"))
Alexandru Gagniucd6ff68d2020-09-15 14:51:46 -0500234 cfg->host_caps |= MMC_CAP(MMC_HS) | MMC_CAP(MMC_HS_52);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100235 if (dev_read_bool(dev, "sd-uhs-sdr12"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200236 cfg->host_caps |= MMC_CAP(UHS_SDR12);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100237 if (dev_read_bool(dev, "sd-uhs-sdr25"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200238 cfg->host_caps |= MMC_CAP(UHS_SDR25);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100239 if (dev_read_bool(dev, "sd-uhs-sdr50"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200240 cfg->host_caps |= MMC_CAP(UHS_SDR50);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100241 if (dev_read_bool(dev, "sd-uhs-sdr104"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200242 cfg->host_caps |= MMC_CAP(UHS_SDR104);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100243 if (dev_read_bool(dev, "sd-uhs-ddr50"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200244 cfg->host_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100245 if (dev_read_bool(dev, "mmc-ddr-1_8v"))
246 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
247 if (dev_read_bool(dev, "mmc-ddr-1_2v"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200248 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
Jean-Jacques Hiblotd39be652017-11-30 17:43:55 +0100249 if (dev_read_bool(dev, "mmc-hs200-1_8v"))
250 cfg->host_caps |= MMC_CAP(MMC_HS_200);
251 if (dev_read_bool(dev, "mmc-hs200-1_2v"))
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200252 cfg->host_caps |= MMC_CAP(MMC_HS_200);
Marek Vasutb22a11a2018-06-13 06:50:16 +0200253 if (dev_read_bool(dev, "mmc-hs400-1_8v"))
254 cfg->host_caps |= MMC_CAP(MMC_HS_400);
255 if (dev_read_bool(dev, "mmc-hs400-1_2v"))
256 cfg->host_caps |= MMC_CAP(MMC_HS_400);
Peng Fanfb968792019-07-10 09:35:18 +0000257 if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe"))
258 cfg->host_caps |= MMC_CAP(MMC_HS_400_ES);
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200259
T Karthik Reddyd0bb5162019-06-25 13:39:02 +0200260 if (dev_read_bool(dev, "non-removable")) {
261 cfg->host_caps |= MMC_CAP_NONREMOVABLE;
262 } else {
263 if (dev_read_bool(dev, "cd-inverted"))
264 cfg->host_caps |= MMC_CAP_CD_ACTIVE_HIGH;
265 if (dev_read_bool(dev, "broken-cd"))
266 cfg->host_caps |= MMC_CAP_NEEDS_POLL;
267 }
268
Peng Fanb01c7732019-07-10 09:35:20 +0000269 if (dev_read_bool(dev, "no-1-8-v")) {
270 cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 |
271 MMC_MODE_HS400 | MMC_MODE_HS400_ES);
272 }
273
Kishon Vijay Abraham I422b2942017-09-21 16:30:13 +0200274 return 0;
275}
276
Simon Glass5a18f872020-04-08 08:33:00 -0600277struct mmc *mmc_get_mmc_dev(const struct udevice *dev)
Simon Glass1e8eb1b2015-06-23 15:38:48 -0600278{
279 struct mmc_uclass_priv *upriv;
280
281 if (!device_active(dev))
282 return NULL;
283 upriv = dev_get_uclass_priv(dev);
284 return upriv->mmc;
285}
286
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600287#if CONFIG_IS_ENABLED(BLK)
Simon Glass4bca6662016-05-01 13:52:39 -0600288struct mmc *find_mmc_device(int dev_num)
289{
290 struct udevice *dev, *mmc_dev;
291 int ret;
292
Simon Glassdbfa32c2022-08-11 19:34:59 -0600293 ret = blk_find_device(UCLASS_MMC, dev_num, &dev);
Simon Glass4bca6662016-05-01 13:52:39 -0600294
295 if (ret) {
296#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
297 printf("MMC Device %d not found\n", dev_num);
298#endif
299 return NULL;
300 }
301
302 mmc_dev = dev_get_parent(dev);
303
Simon Glassbcad6042017-05-27 11:37:19 -0600304 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
305
306 return mmc;
Simon Glass4bca6662016-05-01 13:52:39 -0600307}
308
309int get_mmc_num(void)
310{
Simon Glassdbfa32c2022-08-11 19:34:59 -0600311 return max((blk_find_max_devnum(UCLASS_MMC) + 1), 0);
Simon Glass4bca6662016-05-01 13:52:39 -0600312}
313
314int mmc_get_next_devnum(void)
315{
Simon Glassdbfa32c2022-08-11 19:34:59 -0600316 return blk_find_max_devnum(UCLASS_MMC);
Simon Glass4bca6662016-05-01 13:52:39 -0600317}
318
Simon Glassf8b4a292022-04-24 23:31:14 -0600319int mmc_get_blk(struct udevice *dev, struct udevice **blkp)
320{
321 struct udevice *blk;
322 int ret;
323
324 device_find_first_child_by_uclass(dev, UCLASS_BLK, &blk);
325 ret = device_probe(blk);
326 if (ret)
327 return ret;
328 *blkp = blk;
329
330 return 0;
331}
332
Simon Glass4bca6662016-05-01 13:52:39 -0600333struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
334{
335 struct blk_desc *desc;
336 struct udevice *dev;
337
Simon Glass3a296642021-10-23 17:26:00 -0600338 device_find_first_child_by_uclass(mmc->dev, UCLASS_BLK, &dev);
Simon Glass4bca6662016-05-01 13:52:39 -0600339 if (!dev)
340 return NULL;
Simon Glass71fa5b42020-12-03 16:55:18 -0700341 desc = dev_get_uclass_plat(dev);
Simon Glass4bca6662016-05-01 13:52:39 -0600342
343 return desc;
344}
345
346void mmc_do_preinit(void)
347{
348 struct udevice *dev;
349 struct uclass *uc;
350 int ret;
351
352 ret = uclass_get(UCLASS_MMC, &uc);
353 if (ret)
354 return;
355 uclass_foreach_dev(dev, uc) {
356 struct mmc *m = mmc_get_mmc_dev(dev);
357
358 if (!m)
359 continue;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530360
361 m->user_speed_mode = MMC_MODES_END; /* Initialising user set speed mode */
362
Simon Glass4bca6662016-05-01 13:52:39 -0600363 if (m->preinit)
364 mmc_start_init(m);
365 }
366}
367
368#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
369void print_mmc_devices(char separator)
370{
371 struct udevice *dev;
372 char *mmc_type;
373 bool first = true;
374
375 for (uclass_first_device(UCLASS_MMC, &dev);
376 dev;
Xu Ziyuan77d747e2016-07-23 11:11:22 +0800377 uclass_next_device(&dev), first = false) {
Simon Glass4bca6662016-05-01 13:52:39 -0600378 struct mmc *m = mmc_get_mmc_dev(dev);
379
380 if (!first) {
381 printf("%c", separator);
382 if (separator != '\n')
383 puts(" ");
384 }
385 if (m->has_init)
386 mmc_type = IS_SD(m) ? "SD" : "eMMC";
387 else
388 mmc_type = NULL;
389
390 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
391 if (mmc_type)
392 printf(" (%s)", mmc_type);
393 }
394
395 printf("\n");
396}
397
398#else
399void print_mmc_devices(char separator) { }
400#endif
Simon Glass2f1ea902016-06-12 23:30:16 -0600401
402int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
403{
404 struct blk_desc *bdesc;
405 struct udevice *bdev;
Aswath Govindrajub9f84af2021-03-25 12:48:48 +0530406 int ret;
Jaehoon Chung2910da82017-02-02 13:41:14 +0900407
Simon Glass9e720b02017-04-23 20:02:09 -0600408 if (!mmc_get_ops(dev))
409 return -ENOSYS;
Aswath Govindrajub9f84af2021-03-25 12:48:48 +0530410
411 /* Use the fixed index with aliases node's index */
412 debug("%s: alias devnum=%d\n", __func__, dev_seq(dev));
Simon Glass2f1ea902016-06-12 23:30:16 -0600413
Simon Glassdbfa32c2022-08-11 19:34:59 -0600414 ret = blk_create_devicef(dev, "mmc_blk", "blk", UCLASS_MMC,
415 dev_seq(dev), 512, 0, &bdev);
Simon Glass2f1ea902016-06-12 23:30:16 -0600416 if (ret) {
417 debug("Cannot create block device\n");
418 return ret;
419 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700420 bdesc = dev_get_uclass_plat(bdev);
Simon Glass2f1ea902016-06-12 23:30:16 -0600421 mmc->cfg = cfg;
422 mmc->priv = dev;
423
Simon Glassb1d581d2023-07-30 11:15:14 -0600424 ret = bootdev_setup_for_sibling_blk(bdev, "mmc_bootdev");
Simon Glassf8b4a292022-04-24 23:31:14 -0600425 if (ret)
426 return log_msg_ret("bootdev", ret);
427
Simon Glass2f1ea902016-06-12 23:30:16 -0600428 /* the following chunk was from mmc_register() */
429
430 /* Setup dsr related values */
431 mmc->dsr_imp = 0;
432 mmc->dsr = 0xffffffff;
433 /* Setup the universal parts of the block interface just once */
434 bdesc->removable = 1;
435
436 /* setup initial part type */
437 bdesc->part_type = cfg->part_type;
438 mmc->dev = dev;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530439 mmc->user_speed_mode = MMC_MODES_END;
Simon Glass2f1ea902016-06-12 23:30:16 -0600440 return 0;
441}
442
443int mmc_unbind(struct udevice *dev)
444{
445 struct udevice *bdev;
Simon Glassf8b4a292022-04-24 23:31:14 -0600446 int ret;
Simon Glass2f1ea902016-06-12 23:30:16 -0600447
Simon Glass3a296642021-10-23 17:26:00 -0600448 device_find_first_child_by_uclass(dev, UCLASS_BLK, &bdev);
Simon Glass2f1ea902016-06-12 23:30:16 -0600449 if (bdev) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100450 device_remove(bdev, DM_REMOVE_NORMAL);
Simon Glass2f1ea902016-06-12 23:30:16 -0600451 device_unbind(bdev);
452 }
Simon Glassf8b4a292022-04-24 23:31:14 -0600453 ret = bootdev_unbind_dev(dev);
454 if (ret)
455 return log_msg_ret("bootdev", ret);
Simon Glass2f1ea902016-06-12 23:30:16 -0600456
457 return 0;
458}
459
460static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
461{
462 struct udevice *mmc_dev = dev_get_parent(bdev);
463 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700464 struct blk_desc *desc = dev_get_uclass_plat(bdev);
developer90fdc3e2019-08-27 15:32:19 +0800465 int ret;
Simon Glass2f1ea902016-06-12 23:30:16 -0600466
467 if (desc->hwpart == hwpart)
468 return 0;
469
470 if (mmc->part_config == MMCPART_NOAVAILABLE)
471 return -EMEDIUMTYPE;
472
developer90fdc3e2019-08-27 15:32:19 +0800473 ret = mmc_switch_part(mmc, hwpart);
474 if (!ret)
Simon Glassfada3f92022-09-17 09:00:09 -0600475 blkcache_invalidate(desc->uclass_id, desc->devnum);
developer90fdc3e2019-08-27 15:32:19 +0800476
477 return ret;
Simon Glass2f1ea902016-06-12 23:30:16 -0600478}
479
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000480static int mmc_blk_probe(struct udevice *dev)
481{
Simon Glasse11b2392017-04-23 20:02:10 -0600482 struct udevice *mmc_dev = dev_get_parent(dev);
483 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
484 struct mmc *mmc = upriv->mmc;
485 int ret;
486
487 ret = mmc_init(mmc);
488 if (ret) {
489 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
490 return ret;
491 }
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000492
AKASHI Takahiro80022d72022-03-08 20:36:41 +0900493 ret = device_probe(dev);
494 if (ret) {
495 debug("Probing %s failed (err=%d)\n", dev->name, ret);
496
497 if (CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) ||
498 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) ||
499 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT))
500 mmc_deinit(mmc);
501
502 return ret;
503 }
504
Simon Glasse11b2392017-04-23 20:02:10 -0600505 return 0;
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000506}
507
Marek Vasuta4773fc2019-01-29 04:45:51 +0100508#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
509 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
510 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
511static int mmc_blk_remove(struct udevice *dev)
512{
513 struct udevice *mmc_dev = dev_get_parent(dev);
514 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
515 struct mmc *mmc = upriv->mmc;
516
517 return mmc_deinit(mmc);
518}
519#endif
520
Simon Glass2f1ea902016-06-12 23:30:16 -0600521static const struct blk_ops mmc_blk_ops = {
522 .read = mmc_bread,
Jean-Jacques Hiblotd0531672018-01-04 15:23:32 +0100523#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glass2f1ea902016-06-12 23:30:16 -0600524 .write = mmc_bwrite,
Simon Glassfcc53c12016-10-01 14:43:17 -0600525 .erase = mmc_berase,
Simon Glass2f1ea902016-06-12 23:30:16 -0600526#endif
527 .select_hwpart = mmc_select_hwpart,
528};
529
530U_BOOT_DRIVER(mmc_blk) = {
531 .name = "mmc_blk",
532 .id = UCLASS_BLK,
533 .ops = &mmc_blk_ops,
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000534 .probe = mmc_blk_probe,
Marek Vasuta4773fc2019-01-29 04:45:51 +0100535#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
536 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
537 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
538 .remove = mmc_blk_remove,
539 .flags = DM_FLAG_OS_PREPARE,
540#endif
Simon Glass2f1ea902016-06-12 23:30:16 -0600541};
Simon Glass4bca6662016-05-01 13:52:39 -0600542#endif /* CONFIG_BLK */
543
Simon Glass1e8eb1b2015-06-23 15:38:48 -0600544
545UCLASS_DRIVER(mmc) = {
546 .id = UCLASS_MMC,
547 .name = "mmc",
548 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700549 .per_device_auto = sizeof(struct mmc_uclass_priv),
Simon Glass1e8eb1b2015-06-23 15:38:48 -0600550};