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 |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 9 | #include <mmc.h> |
| 10 | #include <dm.h> |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 11 | #include <dm/device-internal.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <dm/device_compat.h> |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 13 | #include <dm/lists.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <linux/compat.h> |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 15 | #include "mmc_private.h" |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 16 | |
Marek Vasut | 145429a | 2020-04-04 12:45:05 +0200 | [diff] [blame] | 17 | int dm_mmc_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt) |
| 18 | { |
| 19 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 20 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 21 | |
| 22 | if (ops->get_b_max) |
| 23 | return ops->get_b_max(dev, dst, blkcnt); |
| 24 | else |
| 25 | return mmc->cfg->b_max; |
| 26 | } |
| 27 | |
| 28 | int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt) |
| 29 | { |
| 30 | return dm_mmc_get_b_max(mmc->dev, dst, blkcnt); |
| 31 | } |
| 32 | |
Simon Glass | 8ca51e5 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 33 | int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 34 | struct mmc_data *data) |
| 35 | { |
| 36 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 37 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 38 | int ret; |
| 39 | |
| 40 | mmmc_trace_before_send(mmc, cmd); |
| 41 | if (ops->send_cmd) |
| 42 | ret = ops->send_cmd(dev, cmd, data); |
| 43 | else |
| 44 | ret = -ENOSYS; |
| 45 | mmmc_trace_after_send(mmc, cmd, ret); |
| 46 | |
| 47 | return ret; |
| 48 | } |
| 49 | |
| 50 | int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 51 | { |
| 52 | return dm_mmc_send_cmd(mmc->dev, cmd, data); |
| 53 | } |
| 54 | |
| 55 | int dm_mmc_set_ios(struct udevice *dev) |
| 56 | { |
| 57 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 58 | |
| 59 | if (!ops->set_ios) |
| 60 | return -ENOSYS; |
| 61 | return ops->set_ios(dev); |
| 62 | } |
| 63 | |
| 64 | int mmc_set_ios(struct mmc *mmc) |
| 65 | { |
| 66 | return dm_mmc_set_ios(mmc->dev); |
| 67 | } |
| 68 | |
Sam Protsenko | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 69 | 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] | 70 | { |
| 71 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 72 | |
| 73 | if (!ops->wait_dat0) |
| 74 | return -ENOSYS; |
Sam Protsenko | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 75 | return ops->wait_dat0(dev, state, timeout_us); |
Jean-Jacques Hiblot | c10b85d | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Sam Protsenko | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 78 | 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] | 79 | { |
Sam Protsenko | 6cf8a90 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 80 | return dm_mmc_wait_dat0(mmc->dev, state, timeout_us); |
Jean-Jacques Hiblot | c10b85d | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Simon Glass | 8ca51e5 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 83 | int dm_mmc_get_wp(struct udevice *dev) |
| 84 | { |
| 85 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 86 | |
| 87 | if (!ops->get_wp) |
| 88 | return -ENOSYS; |
| 89 | return ops->get_wp(dev); |
| 90 | } |
| 91 | |
| 92 | int mmc_getwp(struct mmc *mmc) |
| 93 | { |
| 94 | return dm_mmc_get_wp(mmc->dev); |
| 95 | } |
| 96 | |
| 97 | int dm_mmc_get_cd(struct udevice *dev) |
| 98 | { |
| 99 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 100 | |
| 101 | if (!ops->get_cd) |
| 102 | return -ENOSYS; |
| 103 | return ops->get_cd(dev); |
| 104 | } |
| 105 | |
| 106 | int mmc_getcd(struct mmc *mmc) |
| 107 | { |
| 108 | return dm_mmc_get_cd(mmc->dev); |
| 109 | } |
Simon Glass | 8ca51e5 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 110 | |
Jean-Jacques Hiblot | f99c2ef | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 111 | #ifdef MMC_SUPPORTS_TUNING |
Kishon Vijay Abraham I | ec84120 | 2017-09-21 16:30:05 +0200 | [diff] [blame] | 112 | int dm_mmc_execute_tuning(struct udevice *dev, uint opcode) |
| 113 | { |
| 114 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 115 | |
| 116 | if (!ops->execute_tuning) |
| 117 | return -ENOSYS; |
| 118 | return ops->execute_tuning(dev, opcode); |
| 119 | } |
| 120 | |
| 121 | int mmc_execute_tuning(struct mmc *mmc, uint opcode) |
| 122 | { |
| 123 | return dm_mmc_execute_tuning(mmc->dev, opcode); |
| 124 | } |
Jean-Jacques Hiblot | f99c2ef | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 125 | #endif |
Kishon Vijay Abraham I | ec84120 | 2017-09-21 16:30:05 +0200 | [diff] [blame] | 126 | |
Peng Fan | 44acd49 | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 127 | #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT) |
| 128 | int dm_mmc_set_enhanced_strobe(struct udevice *dev) |
| 129 | { |
| 130 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 131 | |
| 132 | if (ops->set_enhanced_strobe) |
| 133 | return ops->set_enhanced_strobe(dev); |
| 134 | |
| 135 | return -ENOTSUPP; |
| 136 | } |
| 137 | |
| 138 | int mmc_set_enhanced_strobe(struct mmc *mmc) |
| 139 | { |
| 140 | return dm_mmc_set_enhanced_strobe(mmc->dev); |
| 141 | } |
| 142 | #endif |
| 143 | |
Yann Gautier | 3602a56 | 2019-09-19 17:56:12 +0200 | [diff] [blame] | 144 | int dm_mmc_host_power_cycle(struct udevice *dev) |
| 145 | { |
| 146 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 147 | |
| 148 | if (ops->host_power_cycle) |
| 149 | return ops->host_power_cycle(dev); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int mmc_host_power_cycle(struct mmc *mmc) |
| 154 | { |
| 155 | return dm_mmc_host_power_cycle(mmc->dev); |
| 156 | } |
| 157 | |
Faiz Abbas | 32860bd | 2020-02-26 13:44:30 +0530 | [diff] [blame] | 158 | int dm_mmc_deferred_probe(struct udevice *dev) |
| 159 | { |
| 160 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 161 | |
| 162 | if (ops->deferred_probe) |
| 163 | return ops->deferred_probe(dev); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | int mmc_deferred_probe(struct mmc *mmc) |
| 169 | { |
| 170 | return dm_mmc_deferred_probe(mmc->dev); |
| 171 | } |
| 172 | |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 173 | 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] | 174 | { |
| 175 | int val; |
| 176 | |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 177 | val = dev_read_u32_default(dev, "bus-width", 1); |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 178 | |
| 179 | switch (val) { |
| 180 | case 0x8: |
| 181 | cfg->host_caps |= MMC_MODE_8BIT; |
| 182 | /* fall through */ |
| 183 | case 0x4: |
| 184 | cfg->host_caps |= MMC_MODE_4BIT; |
| 185 | /* fall through */ |
| 186 | case 0x1: |
| 187 | cfg->host_caps |= MMC_MODE_1BIT; |
| 188 | break; |
| 189 | default: |
Masahiro Yamada | 4b28f7b | 2017-12-30 02:00:07 +0900 | [diff] [blame] | 190 | dev_err(dev, "Invalid \"bus-width\" value %u!\n", val); |
| 191 | return -EINVAL; |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Masahiro Yamada | c42ee36 | 2017-12-30 02:00:06 +0900 | [diff] [blame] | 194 | /* f_max is obtained from the optional "max-frequency" property */ |
| 195 | dev_read_u32(dev, "max-frequency", &cfg->f_max); |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 196 | |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 197 | if (dev_read_bool(dev, "cap-sd-highspeed")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 198 | cfg->host_caps |= MMC_CAP(SD_HS); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 199 | if (dev_read_bool(dev, "cap-mmc-highspeed")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 200 | cfg->host_caps |= MMC_CAP(MMC_HS); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 201 | if (dev_read_bool(dev, "sd-uhs-sdr12")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 202 | cfg->host_caps |= MMC_CAP(UHS_SDR12); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 203 | if (dev_read_bool(dev, "sd-uhs-sdr25")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 204 | cfg->host_caps |= MMC_CAP(UHS_SDR25); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 205 | if (dev_read_bool(dev, "sd-uhs-sdr50")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 206 | cfg->host_caps |= MMC_CAP(UHS_SDR50); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 207 | if (dev_read_bool(dev, "sd-uhs-sdr104")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 208 | cfg->host_caps |= MMC_CAP(UHS_SDR104); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 209 | if (dev_read_bool(dev, "sd-uhs-ddr50")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 210 | cfg->host_caps |= MMC_CAP(UHS_DDR50); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 211 | if (dev_read_bool(dev, "mmc-ddr-1_8v")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 212 | cfg->host_caps |= MMC_CAP(MMC_DDR_52); |
Jean-Jacques Hiblot | 7abff2c | 2017-11-30 17:43:55 +0100 | [diff] [blame] | 213 | if (dev_read_bool(dev, "mmc-ddr-1_2v")) |
| 214 | cfg->host_caps |= MMC_CAP(MMC_DDR_52); |
| 215 | if (dev_read_bool(dev, "mmc-hs200-1_8v")) |
| 216 | cfg->host_caps |= MMC_CAP(MMC_HS_200); |
| 217 | if (dev_read_bool(dev, "mmc-hs200-1_2v")) |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 218 | cfg->host_caps |= MMC_CAP(MMC_HS_200); |
Marek Vasut | eb2acba | 2018-06-13 06:50:16 +0200 | [diff] [blame] | 219 | if (dev_read_bool(dev, "mmc-hs400-1_8v")) |
| 220 | cfg->host_caps |= MMC_CAP(MMC_HS_400); |
| 221 | if (dev_read_bool(dev, "mmc-hs400-1_2v")) |
| 222 | cfg->host_caps |= MMC_CAP(MMC_HS_400); |
Peng Fan | b0fc312 | 2019-07-10 09:35:18 +0000 | [diff] [blame] | 223 | if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe")) |
| 224 | cfg->host_caps |= MMC_CAP(MMC_HS_400_ES); |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 225 | |
T Karthik Reddy | 86a94e7 | 2019-06-25 13:39:02 +0200 | [diff] [blame] | 226 | if (dev_read_bool(dev, "non-removable")) { |
| 227 | cfg->host_caps |= MMC_CAP_NONREMOVABLE; |
| 228 | } else { |
| 229 | if (dev_read_bool(dev, "cd-inverted")) |
| 230 | cfg->host_caps |= MMC_CAP_CD_ACTIVE_HIGH; |
| 231 | if (dev_read_bool(dev, "broken-cd")) |
| 232 | cfg->host_caps |= MMC_CAP_NEEDS_POLL; |
| 233 | } |
| 234 | |
Peng Fan | 479a8dd | 2019-07-10 09:35:20 +0000 | [diff] [blame] | 235 | if (dev_read_bool(dev, "no-1-8-v")) { |
| 236 | cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 | |
| 237 | MMC_MODE_HS400 | MMC_MODE_HS400_ES); |
| 238 | } |
| 239 | |
Kishon Vijay Abraham I | 9215ef5 | 2017-09-21 16:30:13 +0200 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
Simon Glass | 3a905cd | 2020-04-08 08:33:00 -0600 | [diff] [blame] | 243 | struct mmc *mmc_get_mmc_dev(const struct udevice *dev) |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 244 | { |
| 245 | struct mmc_uclass_priv *upriv; |
| 246 | |
| 247 | if (!device_active(dev)) |
| 248 | return NULL; |
| 249 | upriv = dev_get_uclass_priv(dev); |
| 250 | return upriv->mmc; |
| 251 | } |
| 252 | |
Simon Glass | c4d660d | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 253 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 254 | struct mmc *find_mmc_device(int dev_num) |
| 255 | { |
| 256 | struct udevice *dev, *mmc_dev; |
| 257 | int ret; |
| 258 | |
Simon Glass | 9752564 | 2017-05-27 11:37:19 -0600 | [diff] [blame] | 259 | ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev); |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 260 | |
| 261 | if (ret) { |
| 262 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 263 | printf("MMC Device %d not found\n", dev_num); |
| 264 | #endif |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | mmc_dev = dev_get_parent(dev); |
| 269 | |
Simon Glass | 9752564 | 2017-05-27 11:37:19 -0600 | [diff] [blame] | 270 | struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); |
| 271 | |
| 272 | return mmc; |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | int get_mmc_num(void) |
| 276 | { |
Kever Yang | 46683f3 | 2016-07-22 17:22:50 +0800 | [diff] [blame] | 277 | return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0); |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | int mmc_get_next_devnum(void) |
| 281 | { |
Masahiro Yamada | dd399cb | 2016-10-14 00:13:18 +0900 | [diff] [blame] | 282 | return blk_find_max_devnum(IF_TYPE_MMC); |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | struct blk_desc *mmc_get_blk_desc(struct mmc *mmc) |
| 286 | { |
| 287 | struct blk_desc *desc; |
| 288 | struct udevice *dev; |
| 289 | |
| 290 | device_find_first_child(mmc->dev, &dev); |
| 291 | if (!dev) |
| 292 | return NULL; |
| 293 | desc = dev_get_uclass_platdata(dev); |
| 294 | |
| 295 | return desc; |
| 296 | } |
| 297 | |
| 298 | void mmc_do_preinit(void) |
| 299 | { |
| 300 | struct udevice *dev; |
| 301 | struct uclass *uc; |
| 302 | int ret; |
| 303 | |
| 304 | ret = uclass_get(UCLASS_MMC, &uc); |
| 305 | if (ret) |
| 306 | return; |
| 307 | uclass_foreach_dev(dev, uc) { |
| 308 | struct mmc *m = mmc_get_mmc_dev(dev); |
| 309 | |
| 310 | if (!m) |
| 311 | continue; |
| 312 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 313 | mmc_set_preinit(m, 1); |
| 314 | #endif |
| 315 | if (m->preinit) |
| 316 | mmc_start_init(m); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 321 | void print_mmc_devices(char separator) |
| 322 | { |
| 323 | struct udevice *dev; |
| 324 | char *mmc_type; |
| 325 | bool first = true; |
| 326 | |
| 327 | for (uclass_first_device(UCLASS_MMC, &dev); |
| 328 | dev; |
Xu Ziyuan | 1bd4f92 | 2016-07-23 11:11:22 +0800 | [diff] [blame] | 329 | uclass_next_device(&dev), first = false) { |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 330 | struct mmc *m = mmc_get_mmc_dev(dev); |
| 331 | |
| 332 | if (!first) { |
| 333 | printf("%c", separator); |
| 334 | if (separator != '\n') |
| 335 | puts(" "); |
| 336 | } |
| 337 | if (m->has_init) |
| 338 | mmc_type = IS_SD(m) ? "SD" : "eMMC"; |
| 339 | else |
| 340 | mmc_type = NULL; |
| 341 | |
| 342 | printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum); |
| 343 | if (mmc_type) |
| 344 | printf(" (%s)", mmc_type); |
| 345 | } |
| 346 | |
| 347 | printf("\n"); |
| 348 | } |
| 349 | |
| 350 | #else |
| 351 | void print_mmc_devices(char separator) { } |
| 352 | #endif |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 353 | |
| 354 | int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg) |
| 355 | { |
| 356 | struct blk_desc *bdesc; |
| 357 | struct udevice *bdev; |
Jaehoon Chung | 02ad33a | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 358 | int ret, devnum = -1; |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 359 | |
Simon Glass | 6665602 | 2017-04-23 20:02:09 -0600 | [diff] [blame] | 360 | if (!mmc_get_ops(dev)) |
| 361 | return -ENOSYS; |
Jaehoon Chung | 02ad33a | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 362 | #ifndef CONFIG_SPL_BUILD |
| 363 | /* Use the fixed index with aliase node's index */ |
Simon Glass | 66e0ed5 | 2017-05-18 20:09:36 -0600 | [diff] [blame] | 364 | ret = dev_read_alias_seq(dev, &devnum); |
| 365 | debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum); |
Jaehoon Chung | 02ad33a | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 366 | #endif |
| 367 | |
| 368 | ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC, |
| 369 | devnum, 512, 0, &bdev); |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 370 | if (ret) { |
| 371 | debug("Cannot create block device\n"); |
| 372 | return ret; |
| 373 | } |
| 374 | bdesc = dev_get_uclass_platdata(bdev); |
| 375 | mmc->cfg = cfg; |
| 376 | mmc->priv = dev; |
| 377 | |
| 378 | /* the following chunk was from mmc_register() */ |
| 379 | |
| 380 | /* Setup dsr related values */ |
| 381 | mmc->dsr_imp = 0; |
| 382 | mmc->dsr = 0xffffffff; |
| 383 | /* Setup the universal parts of the block interface just once */ |
| 384 | bdesc->removable = 1; |
| 385 | |
| 386 | /* setup initial part type */ |
| 387 | bdesc->part_type = cfg->part_type; |
| 388 | mmc->dev = dev; |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | int mmc_unbind(struct udevice *dev) |
| 394 | { |
| 395 | struct udevice *bdev; |
| 396 | |
| 397 | device_find_first_child(dev, &bdev); |
| 398 | if (bdev) { |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 399 | device_remove(bdev, DM_REMOVE_NORMAL); |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 400 | device_unbind(bdev); |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int mmc_select_hwpart(struct udevice *bdev, int hwpart) |
| 407 | { |
| 408 | struct udevice *mmc_dev = dev_get_parent(bdev); |
| 409 | struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); |
| 410 | struct blk_desc *desc = dev_get_uclass_platdata(bdev); |
Weijie Gao | 47b7fa3 | 2019-08-27 15:32:19 +0800 | [diff] [blame] | 411 | int ret; |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 412 | |
| 413 | if (desc->hwpart == hwpart) |
| 414 | return 0; |
| 415 | |
| 416 | if (mmc->part_config == MMCPART_NOAVAILABLE) |
| 417 | return -EMEDIUMTYPE; |
| 418 | |
Weijie Gao | 47b7fa3 | 2019-08-27 15:32:19 +0800 | [diff] [blame] | 419 | ret = mmc_switch_part(mmc, hwpart); |
| 420 | if (!ret) |
| 421 | blkcache_invalidate(desc->if_type, desc->devnum); |
| 422 | |
| 423 | return ret; |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 424 | } |
| 425 | |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 426 | static int mmc_blk_probe(struct udevice *dev) |
| 427 | { |
Simon Glass | 854f9a7 | 2017-04-23 20:02:10 -0600 | [diff] [blame] | 428 | struct udevice *mmc_dev = dev_get_parent(dev); |
| 429 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev); |
| 430 | struct mmc *mmc = upriv->mmc; |
| 431 | int ret; |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 432 | |
Simon Glass | 854f9a7 | 2017-04-23 20:02:10 -0600 | [diff] [blame] | 433 | ret = mmc_init(mmc); |
| 434 | if (ret) { |
| 435 | debug("%s: mmc_init() failed (err=%d)\n", __func__, ret); |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | return 0; |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 440 | } |
| 441 | |
Marek Vasut | fceea99 | 2019-01-29 04:45:51 +0100 | [diff] [blame] | 442 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 443 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 444 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 445 | static int mmc_blk_remove(struct udevice *dev) |
| 446 | { |
| 447 | struct udevice *mmc_dev = dev_get_parent(dev); |
| 448 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev); |
| 449 | struct mmc *mmc = upriv->mmc; |
| 450 | |
| 451 | return mmc_deinit(mmc); |
| 452 | } |
| 453 | #endif |
| 454 | |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 455 | static const struct blk_ops mmc_blk_ops = { |
| 456 | .read = mmc_bread, |
Jean-Jacques Hiblot | d6400c3 | 2018-01-04 15:23:32 +0100 | [diff] [blame] | 457 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 458 | .write = mmc_bwrite, |
Simon Glass | 561e624 | 2016-10-01 14:43:17 -0600 | [diff] [blame] | 459 | .erase = mmc_berase, |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 460 | #endif |
| 461 | .select_hwpart = mmc_select_hwpart, |
| 462 | }; |
| 463 | |
| 464 | U_BOOT_DRIVER(mmc_blk) = { |
| 465 | .name = "mmc_blk", |
| 466 | .id = UCLASS_BLK, |
| 467 | .ops = &mmc_blk_ops, |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 468 | .probe = mmc_blk_probe, |
Marek Vasut | fceea99 | 2019-01-29 04:45:51 +0100 | [diff] [blame] | 469 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \ |
| 470 | CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 471 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 472 | .remove = mmc_blk_remove, |
| 473 | .flags = DM_FLAG_OS_PREPARE, |
| 474 | #endif |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 475 | }; |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 476 | #endif /* CONFIG_BLK */ |
| 477 | |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 478 | |
| 479 | UCLASS_DRIVER(mmc) = { |
| 480 | .id = UCLASS_MMC, |
| 481 | .name = "mmc", |
| 482 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 483 | .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv), |
| 484 | }; |