Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
Yangbo Lu | f9049b2 | 2020-06-17 18:08:58 +0800 | [diff] [blame] | 4 | * Copyright 2020 NXP |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 5 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY UCLASS_MMC |
| 9 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | f8b4a29 | 2022-04-24 23:31:14 -0600 | [diff] [blame] | 11 | #include <bootdev.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 13 | #include <mmc.h> |
| 14 | #include <dm.h> |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 17 | #include <dm/lists.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 18 | #include <linux/compat.h> |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 19 | #include "mmc_private.h" |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 20 | |
Jaehoon Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 21 | static int dm_mmc_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt) |
Marek Vasut | 31976d9 | 2020-04-04 12:45:05 +0200 | [diff] [blame] | 22 | { |
| 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 | |
| 32 | int 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 Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 37 | static int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 38 | 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 | |
| 54 | int 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 Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 59 | static int dm_mmc_set_ios(struct udevice *dev) |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 60 | { |
| 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 | |
| 68 | int mmc_set_ios(struct mmc *mmc) |
| 69 | { |
| 70 | return dm_mmc_set_ios(mmc->dev); |
| 71 | } |
| 72 | |
Jaehoon Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 73 | static int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout_us) |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 74 | { |
| 75 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 76 | |
| 77 | if (!ops->wait_dat0) |
| 78 | return -ENOSYS; |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 79 | return ops->wait_dat0(dev, state, timeout_us); |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 82 | int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us) |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 83 | { |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 84 | return dm_mmc_wait_dat0(mmc->dev, state, timeout_us); |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Jaehoon Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 87 | static int dm_mmc_get_wp(struct udevice *dev) |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 88 | { |
| 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 | |
| 96 | int mmc_getwp(struct mmc *mmc) |
| 97 | { |
| 98 | return dm_mmc_get_wp(mmc->dev); |
| 99 | } |
| 100 | |
Jaehoon Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 101 | static int dm_mmc_get_cd(struct udevice *dev) |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 102 | { |
| 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 | |
| 110 | int mmc_getcd(struct mmc *mmc) |
| 111 | { |
| 112 | return dm_mmc_get_cd(mmc->dev); |
| 113 | } |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 114 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 115 | #ifdef MMC_SUPPORTS_TUNING |
Jaehoon Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 116 | static int dm_mmc_execute_tuning(struct udevice *dev, uint opcode) |
Kishon Vijay Abraham I | ae7174f | 2017-09-21 16:30:05 +0200 | [diff] [blame] | 117 | { |
| 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 | |
| 125 | int mmc_execute_tuning(struct mmc *mmc, uint opcode) |
| 126 | { |
Marek Vasut | b6e8629 | 2024-02-24 23:32:10 +0100 | [diff] [blame] | 127 | 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 I | ae7174f | 2017-09-21 16:30:05 +0200 | [diff] [blame] | 134 | } |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 135 | #endif |
Kishon Vijay Abraham I | ae7174f | 2017-09-21 16:30:05 +0200 | [diff] [blame] | 136 | |
Peng Fan | eede83b | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 137 | #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT) |
Jaehoon Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 138 | static int dm_mmc_set_enhanced_strobe(struct udevice *dev) |
Peng Fan | eede83b | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 139 | { |
| 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 | |
| 148 | int mmc_set_enhanced_strobe(struct mmc *mmc) |
| 149 | { |
| 150 | return dm_mmc_set_enhanced_strobe(mmc->dev); |
| 151 | } |
| 152 | #endif |
| 153 | |
Jaehoon Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 154 | static int dm_mmc_hs400_prepare_ddr(struct udevice *dev) |
Yangbo Lu | 5347aea | 2020-09-01 16:58:04 +0800 | [diff] [blame] | 155 | { |
| 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 | |
| 164 | int mmc_hs400_prepare_ddr(struct mmc *mmc) |
| 165 | { |
| 166 | return dm_mmc_hs400_prepare_ddr(mmc->dev); |
| 167 | } |
| 168 | |
Jaehoon Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 169 | static int dm_mmc_host_power_cycle(struct udevice *dev) |
Yann Gautier | 6f55833 | 2019-09-19 17:56:12 +0200 | [diff] [blame] | 170 | { |
| 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 | |
| 178 | int mmc_host_power_cycle(struct mmc *mmc) |
| 179 | { |
| 180 | return dm_mmc_host_power_cycle(mmc->dev); |
| 181 | } |
| 182 | |
Jaehoon Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 183 | static int dm_mmc_deferred_probe(struct udevice *dev) |
Faiz Abbas | f6fd4ec | 2020-02-26 13:44:30 +0530 | [diff] [blame] | 184 | { |
| 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 | |
| 193 | int mmc_deferred_probe(struct mmc *mmc) |
| 194 | { |
| 195 | return dm_mmc_deferred_probe(mmc->dev); |
| 196 | } |
| 197 | |
Jaehoon Chung | ff6bf1f | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 198 | static int dm_mmc_reinit(struct udevice *dev) |
Yangbo Lu | c46f5d7 | 2020-09-01 16:57:59 +0800 | [diff] [blame] | 199 | { |
| 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 | |
| 208 | int mmc_reinit(struct mmc *mmc) |
| 209 | { |
| 210 | return dm_mmc_reinit(mmc->dev); |
| 211 | } |
| 212 | |
Jean-Jacques Hiblot | d39be65 | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 213 | int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg) |
Kishon Vijay Abraham I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 214 | { |
| 215 | int val; |
| 216 | |
Jean-Jacques Hiblot | d39be65 | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 217 | val = dev_read_u32_default(dev, "bus-width", 1); |
Kishon Vijay Abraham I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 218 | |
| 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 Yamada | 57b12d8 | 2017-12-30 02:00:07 +0900 | [diff] [blame] | 230 | dev_err(dev, "Invalid \"bus-width\" value %u!\n", val); |
| 231 | return -EINVAL; |
Kishon Vijay Abraham I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Masahiro Yamada | 191cd13 | 2017-12-30 02:00:06 +0900 | [diff] [blame] | 234 | /* f_max is obtained from the optional "max-frequency" property */ |
| 235 | dev_read_u32(dev, "max-frequency", &cfg->f_max); |
Kishon Vijay Abraham I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 236 | |
Jean-Jacques Hiblot | d39be65 | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 237 | if (dev_read_bool(dev, "cap-sd-highspeed")) |
Kishon Vijay Abraham I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 238 | cfg->host_caps |= MMC_CAP(SD_HS); |
Jean-Jacques Hiblot | d39be65 | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 239 | if (dev_read_bool(dev, "cap-mmc-highspeed")) |
Alexandru Gagniuc | d6ff68d | 2020-09-15 14:51:46 -0500 | [diff] [blame] | 240 | cfg->host_caps |= MMC_CAP(MMC_HS) | MMC_CAP(MMC_HS_52); |
Jean-Jacques Hiblot | d39be65 | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 241 | if (dev_read_bool(dev, "sd-uhs-sdr12")) |
Kishon Vijay Abraham I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 242 | cfg->host_caps |= MMC_CAP(UHS_SDR12); |
Jean-Jacques Hiblot | d39be65 | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 243 | if (dev_read_bool(dev, "sd-uhs-sdr25")) |
Kishon Vijay Abraham I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 244 | cfg->host_caps |= MMC_CAP(UHS_SDR25); |
Jean-Jacques Hiblot | d39be65 | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 245 | if (dev_read_bool(dev, "sd-uhs-sdr50")) |
Kishon Vijay Abraham I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 246 | cfg->host_caps |= MMC_CAP(UHS_SDR50); |
Jean-Jacques Hiblot | d39be65 | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 247 | if (dev_read_bool(dev, "sd-uhs-sdr104")) |
Kishon Vijay Abraham I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 248 | cfg->host_caps |= MMC_CAP(UHS_SDR104); |
Jean-Jacques Hiblot | d39be65 | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 249 | if (dev_read_bool(dev, "sd-uhs-ddr50")) |
Kishon Vijay Abraham I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 250 | cfg->host_caps |= MMC_CAP(UHS_DDR50); |
Jean-Jacques Hiblot | d39be65 | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 251 | 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 I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 254 | cfg->host_caps |= MMC_CAP(MMC_DDR_52); |
Jean-Jacques Hiblot | d39be65 | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 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 I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 258 | cfg->host_caps |= MMC_CAP(MMC_HS_200); |
Marek Vasut | b22a11a | 2018-06-13 06:50:16 +0200 | [diff] [blame] | 259 | if (dev_read_bool(dev, "mmc-hs400-1_8v")) |
Jonas Karlman | f23ac28 | 2024-04-08 21:06:15 +0000 | [diff] [blame] | 260 | cfg->host_caps |= MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_200); |
Marek Vasut | b22a11a | 2018-06-13 06:50:16 +0200 | [diff] [blame] | 261 | if (dev_read_bool(dev, "mmc-hs400-1_2v")) |
Jonas Karlman | f23ac28 | 2024-04-08 21:06:15 +0000 | [diff] [blame] | 262 | cfg->host_caps |= MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_200); |
Peng Fan | fb96879 | 2019-07-10 09:35:18 +0000 | [diff] [blame] | 263 | if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe")) |
| 264 | cfg->host_caps |= MMC_CAP(MMC_HS_400_ES); |
Jonas Karlman | 925f650 | 2024-04-08 21:06:16 +0000 | [diff] [blame] | 265 | 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 I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 268 | |
T Karthik Reddy | d0bb516 | 2019-06-25 13:39:02 +0200 | [diff] [blame] | 269 | 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 Fan | b01c773 | 2019-07-10 09:35:20 +0000 | [diff] [blame] | 278 | 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 I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
Simon Glass | 5a18f87 | 2020-04-08 08:33:00 -0600 | [diff] [blame] | 286 | struct mmc *mmc_get_mmc_dev(const struct udevice *dev) |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 287 | { |
| 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 Glass | 5f4bd8c | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 296 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 297 | struct mmc *find_mmc_device(int dev_num) |
| 298 | { |
| 299 | struct udevice *dev, *mmc_dev; |
| 300 | int ret; |
| 301 | |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 302 | ret = blk_find_device(UCLASS_MMC, dev_num, &dev); |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 303 | |
| 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 Glass | bcad604 | 2017-05-27 11:37:19 -0600 | [diff] [blame] | 313 | struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); |
| 314 | |
| 315 | return mmc; |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | int get_mmc_num(void) |
| 319 | { |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 320 | return max((blk_find_max_devnum(UCLASS_MMC) + 1), 0); |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | int mmc_get_next_devnum(void) |
| 324 | { |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 325 | return blk_find_max_devnum(UCLASS_MMC); |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 326 | } |
| 327 | |
Simon Glass | f8b4a29 | 2022-04-24 23:31:14 -0600 | [diff] [blame] | 328 | int 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 Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 342 | struct blk_desc *mmc_get_blk_desc(struct mmc *mmc) |
| 343 | { |
| 344 | struct blk_desc *desc; |
| 345 | struct udevice *dev; |
| 346 | |
Simon Glass | 3a29664 | 2021-10-23 17:26:00 -0600 | [diff] [blame] | 347 | device_find_first_child_by_uclass(mmc->dev, UCLASS_BLK, &dev); |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 348 | if (!dev) |
| 349 | return NULL; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 350 | desc = dev_get_uclass_plat(dev); |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 351 | |
| 352 | return desc; |
| 353 | } |
| 354 | |
| 355 | void 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 Govindraju | bb5b9fe | 2021-08-13 23:04:41 +0530 | [diff] [blame] | 369 | |
| 370 | m->user_speed_mode = MMC_MODES_END; /* Initialising user set speed mode */ |
| 371 | |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 372 | if (m->preinit) |
| 373 | mmc_start_init(m); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 378 | void 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 Ziyuan | 77d747e | 2016-07-23 11:11:22 +0800 | [diff] [blame] | 386 | uclass_next_device(&dev), first = false) { |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 387 | 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 |
| 408 | void print_mmc_devices(char separator) { } |
| 409 | #endif |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 410 | |
| 411 | int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg) |
| 412 | { |
| 413 | struct blk_desc *bdesc; |
| 414 | struct udevice *bdev; |
Aswath Govindraju | b9f84af | 2021-03-25 12:48:48 +0530 | [diff] [blame] | 415 | int ret; |
Jaehoon Chung | 2910da8 | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 416 | |
Simon Glass | 9e720b0 | 2017-04-23 20:02:09 -0600 | [diff] [blame] | 417 | if (!mmc_get_ops(dev)) |
| 418 | return -ENOSYS; |
Aswath Govindraju | b9f84af | 2021-03-25 12:48:48 +0530 | [diff] [blame] | 419 | |
| 420 | /* Use the fixed index with aliases node's index */ |
| 421 | debug("%s: alias devnum=%d\n", __func__, dev_seq(dev)); |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 422 | |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 423 | ret = blk_create_devicef(dev, "mmc_blk", "blk", UCLASS_MMC, |
Bin Meng | 2294ecb | 2023-09-26 16:43:31 +0800 | [diff] [blame] | 424 | dev_seq(dev), DEFAULT_BLKSZ, 0, &bdev); |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 425 | if (ret) { |
| 426 | debug("Cannot create block device\n"); |
| 427 | return ret; |
| 428 | } |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 429 | bdesc = dev_get_uclass_plat(bdev); |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 430 | mmc->cfg = cfg; |
| 431 | mmc->priv = dev; |
| 432 | |
Simon Glass | b1d581d | 2023-07-30 11:15:14 -0600 | [diff] [blame] | 433 | ret = bootdev_setup_for_sibling_blk(bdev, "mmc_bootdev"); |
Simon Glass | f8b4a29 | 2022-04-24 23:31:14 -0600 | [diff] [blame] | 434 | if (ret) |
| 435 | return log_msg_ret("bootdev", ret); |
| 436 | |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 437 | /* 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 Govindraju | bb5b9fe | 2021-08-13 23:04:41 +0530 | [diff] [blame] | 448 | mmc->user_speed_mode = MMC_MODES_END; |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | int mmc_unbind(struct udevice *dev) |
| 453 | { |
| 454 | struct udevice *bdev; |
Simon Glass | f8b4a29 | 2022-04-24 23:31:14 -0600 | [diff] [blame] | 455 | int ret; |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 456 | |
Simon Glass | 3a29664 | 2021-10-23 17:26:00 -0600 | [diff] [blame] | 457 | device_find_first_child_by_uclass(dev, UCLASS_BLK, &bdev); |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 458 | if (bdev) { |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 459 | device_remove(bdev, DM_REMOVE_NORMAL); |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 460 | device_unbind(bdev); |
| 461 | } |
Simon Glass | f8b4a29 | 2022-04-24 23:31:14 -0600 | [diff] [blame] | 462 | ret = bootdev_unbind_dev(dev); |
| 463 | if (ret) |
| 464 | return log_msg_ret("bootdev", ret); |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static 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 Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 473 | struct blk_desc *desc = dev_get_uclass_plat(bdev); |
developer | 90fdc3e | 2019-08-27 15:32:19 +0800 | [diff] [blame] | 474 | int ret; |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 475 | |
| 476 | if (desc->hwpart == hwpart) |
| 477 | return 0; |
| 478 | |
| 479 | if (mmc->part_config == MMCPART_NOAVAILABLE) |
| 480 | return -EMEDIUMTYPE; |
| 481 | |
developer | 90fdc3e | 2019-08-27 15:32:19 +0800 | [diff] [blame] | 482 | ret = mmc_switch_part(mmc, hwpart); |
| 483 | if (!ret) |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 484 | blkcache_invalidate(desc->uclass_id, desc->devnum); |
developer | 90fdc3e | 2019-08-27 15:32:19 +0800 | [diff] [blame] | 485 | |
| 486 | return ret; |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 487 | } |
| 488 | |
Fiach Antaw | 7cdbe50 | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 489 | static int mmc_blk_probe(struct udevice *dev) |
| 490 | { |
Simon Glass | e11b239 | 2017-04-23 20:02:10 -0600 | [diff] [blame] | 491 | 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 Antaw | 7cdbe50 | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 501 | |
AKASHI Takahiro | 80022d7 | 2022-03-08 20:36:41 +0900 | [diff] [blame] | 502 | ret = device_probe(dev); |
| 503 | if (ret) { |
| 504 | debug("Probing %s failed (err=%d)\n", dev->name, ret); |
| 505 | |
Marek Vasut | 67c77f9 | 2024-03-17 04:01:22 +0100 | [diff] [blame] | 506 | mmc_deinit(mmc); |
AKASHI Takahiro | 80022d7 | 2022-03-08 20:36:41 +0900 | [diff] [blame] | 507 | |
| 508 | return ret; |
| 509 | } |
| 510 | |
Simon Glass | e11b239 | 2017-04-23 20:02:10 -0600 | [diff] [blame] | 511 | return 0; |
Fiach Antaw | 7cdbe50 | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 512 | } |
| 513 | |
Marek Vasut | a4773fc | 2019-01-29 04:45:51 +0100 | [diff] [blame] | 514 | static 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 Vasut | a4773fc | 2019-01-29 04:45:51 +0100 | [diff] [blame] | 522 | |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 523 | static const struct blk_ops mmc_blk_ops = { |
| 524 | .read = mmc_bread, |
Jean-Jacques Hiblot | d053167 | 2018-01-04 15:23:32 +0100 | [diff] [blame] | 525 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 526 | .write = mmc_bwrite, |
Simon Glass | fcc53c1 | 2016-10-01 14:43:17 -0600 | [diff] [blame] | 527 | .erase = mmc_berase, |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 528 | #endif |
| 529 | .select_hwpart = mmc_select_hwpart, |
| 530 | }; |
| 531 | |
| 532 | U_BOOT_DRIVER(mmc_blk) = { |
| 533 | .name = "mmc_blk", |
| 534 | .id = UCLASS_BLK, |
| 535 | .ops = &mmc_blk_ops, |
Fiach Antaw | 7cdbe50 | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 536 | .probe = mmc_blk_probe, |
Marek Vasut | a4773fc | 2019-01-29 04:45:51 +0100 | [diff] [blame] | 537 | .remove = mmc_blk_remove, |
| 538 | .flags = DM_FLAG_OS_PREPARE, |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 539 | }; |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 540 | #endif /* CONFIG_BLK */ |
| 541 | |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 542 | |
| 543 | UCLASS_DRIVER(mmc) = { |
| 544 | .id = UCLASS_MMC, |
| 545 | .name = "mmc", |
| 546 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 547 | .per_device_auto = sizeof(struct mmc_uclass_priv), |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 548 | }; |