Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
Yangbo Lu | 39913ac | 2020-06-17 18:08:58 +0800 | [diff] [blame] | 4 | * Copyright 2020 NXP |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 5 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
Patrick Delaunay | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY UCLASS_MMC |
| 9 | |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | b8aa463 | 2022-04-24 23:31:14 -0600 | [diff] [blame] | 11 | #include <bootdev.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 13 | #include <mmc.h> |
| 14 | #include <dm.h> |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 17 | #include <dm/lists.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 18 | #include <linux/compat.h> |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 19 | #include "mmc_private.h" |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 20 | |
Jaehoon Chung | d77d61e | 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 | 145429a | 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 | d77d61e | 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 | 8ca51e5 | 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 | d77d61e | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 59 | static int dm_mmc_set_ios(struct udevice *dev) |
Simon Glass | 8ca51e5 | 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 | d77d61e | 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 | c10b85d | 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 | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 79 | return ops->wait_dat0(dev, state, timeout_us); |
Jean-Jacques Hiblot | c10b85d | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Sam Protsenko | 6cf8a90 | 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 | c10b85d | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 83 | { |
Sam Protsenko | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 84 | return dm_mmc_wait_dat0(mmc->dev, state, timeout_us); |
Jean-Jacques Hiblot | c10b85d | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Jaehoon Chung | d77d61e | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 87 | static int dm_mmc_get_wp(struct udevice *dev) |
Simon Glass | 8ca51e5 | 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 | d77d61e | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 101 | static int dm_mmc_get_cd(struct udevice *dev) |
Simon Glass | 8ca51e5 | 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 | 8ca51e5 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 114 | |
Jean-Jacques Hiblot | f99c2ef | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 115 | #ifdef MMC_SUPPORTS_TUNING |
Jaehoon Chung | d77d61e | 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 | ec84120 | 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 | { |
| 127 | return dm_mmc_execute_tuning(mmc->dev, opcode); |
| 128 | } |
Jean-Jacques Hiblot | f99c2ef | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 129 | #endif |
Kishon Vijay Abraham I | ec84120 | 2017-09-21 16:30:05 +0200 | [diff] [blame] | 130 | |
Peng Fan | 44acd49 | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 131 | #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT) |
Jaehoon Chung | d77d61e | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 132 | static int dm_mmc_set_enhanced_strobe(struct udevice *dev) |
Peng Fan | 44acd49 | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 133 | { |
| 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 | |
| 142 | int mmc_set_enhanced_strobe(struct mmc *mmc) |
| 143 | { |
| 144 | return dm_mmc_set_enhanced_strobe(mmc->dev); |
| 145 | } |
| 146 | #endif |
| 147 | |
Jaehoon Chung | d77d61e | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 148 | static int dm_mmc_hs400_prepare_ddr(struct udevice *dev) |
Yangbo Lu | d271e10 | 2020-09-01 16:58:04 +0800 | [diff] [blame] | 149 | { |
| 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 | |
| 158 | int mmc_hs400_prepare_ddr(struct mmc *mmc) |
| 159 | { |
| 160 | return dm_mmc_hs400_prepare_ddr(mmc->dev); |
| 161 | } |
| 162 | |
Jaehoon Chung | d77d61e | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 163 | static int dm_mmc_host_power_cycle(struct udevice *dev) |
Yann Gautier | 3602a56 | 2019-09-19 17:56:12 +0200 | [diff] [blame] | 164 | { |
| 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 | |
| 172 | int mmc_host_power_cycle(struct mmc *mmc) |
| 173 | { |
| 174 | return dm_mmc_host_power_cycle(mmc->dev); |
| 175 | } |
| 176 | |
Jaehoon Chung | d77d61e | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 177 | static int dm_mmc_deferred_probe(struct udevice *dev) |
Faiz Abbas | 32860bd | 2020-02-26 13:44:30 +0530 | [diff] [blame] | 178 | { |
| 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 | |
| 187 | int mmc_deferred_probe(struct mmc *mmc) |
| 188 | { |
| 189 | return dm_mmc_deferred_probe(mmc->dev); |
| 190 | } |
| 191 | |
Jaehoon Chung | d77d61e | 2021-05-31 08:31:50 +0900 | [diff] [blame] | 192 | static int dm_mmc_reinit(struct udevice *dev) |
Yangbo Lu | 390f9bd | 2020-09-01 16:57:59 +0800 | [diff] [blame] | 193 | { |
| 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 | |
| 202 | int mmc_reinit(struct mmc *mmc) |
| 203 | { |
| 204 | return dm_mmc_reinit(mmc->dev); |
| 205 | } |
| 206 | |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 207 | int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 208 | { |
| 209 | int val; |
| 210 | |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 211 | val = dev_read_u32_default(dev, "bus-width", 1); |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 212 | |
| 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 Yamada | 4b28f7b | 2017-12-30 02:00:07 +0900 | [diff] [blame] | 224 | dev_err(dev, "Invalid \"bus-width\" value %u!\n", val); |
| 225 | return -EINVAL; |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Masahiro Yamada | c42ee36 | 2017-12-30 02:00:06 +0900 | [diff] [blame] | 228 | /* f_max is obtained from the optional "max-frequency" property */ |
| 229 | dev_read_u32(dev, "max-frequency", &cfg->f_max); |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 230 | |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 231 | if (dev_read_bool(dev, "cap-sd-highspeed")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 232 | cfg->host_caps |= MMC_CAP(SD_HS); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 233 | if (dev_read_bool(dev, "cap-mmc-highspeed")) |
Alexandru Gagniuc | f80b8ac | 2020-09-15 14:51:46 -0500 | [diff] [blame] | 234 | cfg->host_caps |= MMC_CAP(MMC_HS) | MMC_CAP(MMC_HS_52); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 235 | if (dev_read_bool(dev, "sd-uhs-sdr12")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 236 | cfg->host_caps |= MMC_CAP(UHS_SDR12); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 237 | if (dev_read_bool(dev, "sd-uhs-sdr25")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 238 | cfg->host_caps |= MMC_CAP(UHS_SDR25); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 239 | if (dev_read_bool(dev, "sd-uhs-sdr50")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 240 | cfg->host_caps |= MMC_CAP(UHS_SDR50); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 241 | if (dev_read_bool(dev, "sd-uhs-sdr104")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 242 | cfg->host_caps |= MMC_CAP(UHS_SDR104); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 243 | if (dev_read_bool(dev, "sd-uhs-ddr50")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 244 | cfg->host_caps |= MMC_CAP(UHS_DDR50); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 245 | if (dev_read_bool(dev, "mmc-ddr-1_8v")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 246 | cfg->host_caps |= MMC_CAP(MMC_DDR_52); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 247 | if (dev_read_bool(dev, "mmc-ddr-1_2v")) |
| 248 | cfg->host_caps |= MMC_CAP(MMC_DDR_52); |
| 249 | 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 I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 252 | cfg->host_caps |= MMC_CAP(MMC_HS_200); |
Marek Vasut | eb2acba | 2018-06-13 06:50:16 +0200 | [diff] [blame] | 253 | 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 Fan | b0fc312 | 2019-07-10 09:35:18 +0000 | [diff] [blame] | 257 | if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe")) |
| 258 | cfg->host_caps |= MMC_CAP(MMC_HS_400_ES); |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 259 | |
T Karthik Reddy | 86a94e7 | 2019-06-25 13:39:02 +0200 | [diff] [blame] | 260 | 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 Fan | 479a8dd | 2019-07-10 09:35:20 +0000 | [diff] [blame] | 269 | 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 I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
Simon Glass | 3a905cd | 2020-04-08 08:33:00 -0600 | [diff] [blame] | 277 | struct mmc *mmc_get_mmc_dev(const struct udevice *dev) |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 278 | { |
| 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 Glass | c4d660d | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 287 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 288 | struct mmc *find_mmc_device(int dev_num) |
| 289 | { |
| 290 | struct udevice *dev, *mmc_dev; |
| 291 | int ret; |
| 292 | |
Simon Glass | e33a5c6 | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 293 | ret = blk_find_device(UCLASS_MMC, dev_num, &dev); |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 294 | |
| 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 Glass | 9752564 | 2017-05-27 11:37:19 -0600 | [diff] [blame] | 304 | struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); |
| 305 | |
| 306 | return mmc; |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | int get_mmc_num(void) |
| 310 | { |
Simon Glass | e33a5c6 | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 311 | return max((blk_find_max_devnum(UCLASS_MMC) + 1), 0); |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | int mmc_get_next_devnum(void) |
| 315 | { |
Simon Glass | e33a5c6 | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 316 | return blk_find_max_devnum(UCLASS_MMC); |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 317 | } |
| 318 | |
Simon Glass | b8aa463 | 2022-04-24 23:31:14 -0600 | [diff] [blame] | 319 | int 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 Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 333 | struct blk_desc *mmc_get_blk_desc(struct mmc *mmc) |
| 334 | { |
| 335 | struct blk_desc *desc; |
| 336 | struct udevice *dev; |
| 337 | |
Simon Glass | ce34a66 | 2021-10-23 17:26:00 -0600 | [diff] [blame] | 338 | device_find_first_child_by_uclass(mmc->dev, UCLASS_BLK, &dev); |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 339 | if (!dev) |
| 340 | return NULL; |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 341 | desc = dev_get_uclass_plat(dev); |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 342 | |
| 343 | return desc; |
| 344 | } |
| 345 | |
| 346 | void 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 Govindraju | 19f7a34 | 2021-08-13 23:04:41 +0530 | [diff] [blame] | 360 | |
| 361 | m->user_speed_mode = MMC_MODES_END; /* Initialising user set speed mode */ |
| 362 | |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 363 | if (m->preinit) |
| 364 | mmc_start_init(m); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 369 | void 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 Ziyuan | 1bd4f92 | 2016-07-23 11:11:22 +0800 | [diff] [blame] | 377 | uclass_next_device(&dev), first = false) { |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 378 | 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 |
| 399 | void print_mmc_devices(char separator) { } |
| 400 | #endif |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 401 | |
| 402 | int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg) |
| 403 | { |
| 404 | struct blk_desc *bdesc; |
| 405 | struct udevice *bdev; |
Aswath Govindraju | 2243d19 | 2021-03-25 12:48:48 +0530 | [diff] [blame] | 406 | int ret; |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 407 | |
Simon Glass | 6665602 | 2017-04-23 20:02:09 -0600 | [diff] [blame] | 408 | if (!mmc_get_ops(dev)) |
| 409 | return -ENOSYS; |
Aswath Govindraju | 2243d19 | 2021-03-25 12:48:48 +0530 | [diff] [blame] | 410 | |
| 411 | /* Use the fixed index with aliases node's index */ |
| 412 | debug("%s: alias devnum=%d\n", __func__, dev_seq(dev)); |
Jaehoon Chung | 02ad33a | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 413 | |
Simon Glass | e33a5c6 | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 414 | ret = blk_create_devicef(dev, "mmc_blk", "blk", UCLASS_MMC, |
Bin Meng | 7020b2e | 2023-09-26 16:43:31 +0800 | [diff] [blame] | 415 | dev_seq(dev), DEFAULT_BLKSZ, 0, &bdev); |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 416 | if (ret) { |
| 417 | debug("Cannot create block device\n"); |
| 418 | return ret; |
| 419 | } |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 420 | bdesc = dev_get_uclass_plat(bdev); |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 421 | mmc->cfg = cfg; |
| 422 | mmc->priv = dev; |
| 423 | |
Simon Glass | d7d7857 | 2023-07-30 11:15:14 -0600 | [diff] [blame] | 424 | ret = bootdev_setup_for_sibling_blk(bdev, "mmc_bootdev"); |
Simon Glass | b8aa463 | 2022-04-24 23:31:14 -0600 | [diff] [blame] | 425 | if (ret) |
| 426 | return log_msg_ret("bootdev", ret); |
| 427 | |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 428 | /* 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 Govindraju | 19f7a34 | 2021-08-13 23:04:41 +0530 | [diff] [blame] | 439 | mmc->user_speed_mode = MMC_MODES_END; |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | int mmc_unbind(struct udevice *dev) |
| 444 | { |
| 445 | struct udevice *bdev; |
Simon Glass | b8aa463 | 2022-04-24 23:31:14 -0600 | [diff] [blame] | 446 | int ret; |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 447 | |
Simon Glass | ce34a66 | 2021-10-23 17:26:00 -0600 | [diff] [blame] | 448 | device_find_first_child_by_uclass(dev, UCLASS_BLK, &bdev); |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 449 | if (bdev) { |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 450 | device_remove(bdev, DM_REMOVE_NORMAL); |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 451 | device_unbind(bdev); |
| 452 | } |
Simon Glass | b8aa463 | 2022-04-24 23:31:14 -0600 | [diff] [blame] | 453 | ret = bootdev_unbind_dev(dev); |
| 454 | if (ret) |
| 455 | return log_msg_ret("bootdev", ret); |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static 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 Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 464 | struct blk_desc *desc = dev_get_uclass_plat(bdev); |
Weijie Gao | 47b7fa3 | 2019-08-27 15:32:19 +0800 | [diff] [blame] | 465 | int ret; |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 466 | |
| 467 | if (desc->hwpart == hwpart) |
| 468 | return 0; |
| 469 | |
| 470 | if (mmc->part_config == MMCPART_NOAVAILABLE) |
| 471 | return -EMEDIUMTYPE; |
| 472 | |
Weijie Gao | 47b7fa3 | 2019-08-27 15:32:19 +0800 | [diff] [blame] | 473 | ret = mmc_switch_part(mmc, hwpart); |
| 474 | if (!ret) |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 475 | blkcache_invalidate(desc->uclass_id, desc->devnum); |
Weijie Gao | 47b7fa3 | 2019-08-27 15:32:19 +0800 | [diff] [blame] | 476 | |
| 477 | return ret; |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 478 | } |
| 479 | |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 480 | static int mmc_blk_probe(struct udevice *dev) |
| 481 | { |
Simon Glass | 854f9a7 | 2017-04-23 20:02:10 -0600 | [diff] [blame] | 482 | 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; |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 486 | |
Simon Glass | 854f9a7 | 2017-04-23 20:02:10 -0600 | [diff] [blame] | 487 | ret = mmc_init(mmc); |
| 488 | if (ret) { |
| 489 | debug("%s: mmc_init() failed (err=%d)\n", __func__, ret); |
| 490 | return ret; |
| 491 | } |
| 492 | |
AKASHI Takahiro | c822c1a | 2022-03-08 20:36:41 +0900 | [diff] [blame] | 493 | 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 Glass | 854f9a7 | 2017-04-23 20:02:10 -0600 | [diff] [blame] | 505 | return 0; |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 506 | } |
| 507 | |
Marek Vasut | fceea99 | 2019-01-29 04:45:51 +0100 | [diff] [blame] | 508 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 509 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 510 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 511 | static 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 Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 521 | static const struct blk_ops mmc_blk_ops = { |
| 522 | .read = mmc_bread, |
Jean-Jacques Hiblot | d6400c3 | 2018-01-04 15:23:32 +0100 | [diff] [blame] | 523 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 524 | .write = mmc_bwrite, |
Simon Glass | 561e624 | 2016-10-01 14:43:17 -0600 | [diff] [blame] | 525 | .erase = mmc_berase, |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 526 | #endif |
| 527 | .select_hwpart = mmc_select_hwpart, |
| 528 | }; |
| 529 | |
| 530 | U_BOOT_DRIVER(mmc_blk) = { |
| 531 | .name = "mmc_blk", |
| 532 | .id = UCLASS_BLK, |
| 533 | .ops = &mmc_blk_ops, |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 534 | .probe = mmc_blk_probe, |
Marek Vasut | fceea99 | 2019-01-29 04:45:51 +0100 | [diff] [blame] | 535 | #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 Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 541 | }; |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 542 | #endif /* CONFIG_BLK */ |
| 543 | |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 544 | |
| 545 | UCLASS_DRIVER(mmc) = { |
| 546 | .id = UCLASS_MMC, |
| 547 | .name = "mmc", |
| 548 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 549 | .per_device_auto = sizeof(struct mmc_uclass_priv), |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 550 | }; |