Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <mmc.h> |
| 10 | #include <dm.h> |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 11 | #include <dm/device-internal.h> |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 12 | #include <dm/lists.h> |
| 13 | #include <dm/root.h> |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 14 | #include "mmc_private.h" |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 15 | |
Jaehoon Chung | 2910da8 | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 18 | int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 19 | struct mmc_data *data) |
| 20 | { |
| 21 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 22 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 23 | int ret; |
| 24 | |
| 25 | mmmc_trace_before_send(mmc, cmd); |
| 26 | if (ops->send_cmd) |
| 27 | ret = ops->send_cmd(dev, cmd, data); |
| 28 | else |
| 29 | ret = -ENOSYS; |
| 30 | mmmc_trace_after_send(mmc, cmd, ret); |
| 31 | |
| 32 | return ret; |
| 33 | } |
| 34 | |
| 35 | int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 36 | { |
| 37 | return dm_mmc_send_cmd(mmc->dev, cmd, data); |
| 38 | } |
| 39 | |
| 40 | int dm_mmc_set_ios(struct udevice *dev) |
| 41 | { |
| 42 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 43 | |
| 44 | if (!ops->set_ios) |
| 45 | return -ENOSYS; |
| 46 | return ops->set_ios(dev); |
| 47 | } |
| 48 | |
| 49 | int mmc_set_ios(struct mmc *mmc) |
| 50 | { |
| 51 | return dm_mmc_set_ios(mmc->dev); |
| 52 | } |
| 53 | |
Jean-Jacques Hiblot | 5f23d87 | 2017-09-21 16:30:01 +0200 | [diff] [blame] | 54 | void dm_mmc_send_init_stream(struct udevice *dev) |
| 55 | { |
| 56 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 57 | |
| 58 | if (ops->send_init_stream) |
| 59 | ops->send_init_stream(dev); |
| 60 | } |
| 61 | |
| 62 | void mmc_send_init_stream(struct mmc *mmc) |
| 63 | { |
| 64 | dm_mmc_send_init_stream(mmc->dev); |
| 65 | } |
| 66 | |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 67 | int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout) |
| 68 | { |
| 69 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 70 | |
| 71 | if (!ops->wait_dat0) |
| 72 | return -ENOSYS; |
| 73 | return ops->wait_dat0(dev, state, timeout); |
| 74 | } |
| 75 | |
| 76 | int mmc_wait_dat0(struct mmc *mmc, int state, int timeout) |
| 77 | { |
| 78 | return dm_mmc_wait_dat0(mmc->dev, state, timeout); |
| 79 | } |
| 80 | |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 81 | int dm_mmc_get_wp(struct udevice *dev) |
| 82 | { |
| 83 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 84 | |
| 85 | if (!ops->get_wp) |
| 86 | return -ENOSYS; |
| 87 | return ops->get_wp(dev); |
| 88 | } |
| 89 | |
| 90 | int mmc_getwp(struct mmc *mmc) |
| 91 | { |
| 92 | return dm_mmc_get_wp(mmc->dev); |
| 93 | } |
| 94 | |
| 95 | int dm_mmc_get_cd(struct udevice *dev) |
| 96 | { |
| 97 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 98 | |
| 99 | if (!ops->get_cd) |
| 100 | return -ENOSYS; |
| 101 | return ops->get_cd(dev); |
| 102 | } |
| 103 | |
| 104 | int mmc_getcd(struct mmc *mmc) |
| 105 | { |
| 106 | return dm_mmc_get_cd(mmc->dev); |
| 107 | } |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 108 | |
Kishon Vijay Abraham I | ae7174f | 2017-09-21 16:30:05 +0200 | [diff] [blame] | 109 | int dm_mmc_execute_tuning(struct udevice *dev, uint opcode) |
| 110 | { |
| 111 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 112 | |
| 113 | if (!ops->execute_tuning) |
| 114 | return -ENOSYS; |
| 115 | return ops->execute_tuning(dev, opcode); |
| 116 | } |
| 117 | |
| 118 | int mmc_execute_tuning(struct mmc *mmc, uint opcode) |
| 119 | { |
| 120 | return dm_mmc_execute_tuning(mmc->dev, opcode); |
| 121 | } |
| 122 | |
Kishon Vijay Abraham I | 422b294 | 2017-09-21 16:30:13 +0200 | [diff] [blame^] | 123 | int mmc_of_parse(const void *fdt, int node, struct mmc_config *cfg) |
| 124 | { |
| 125 | int val; |
| 126 | |
| 127 | val = fdtdec_get_int(fdt, node, "bus-width", 1); |
| 128 | |
| 129 | switch (val) { |
| 130 | case 0x8: |
| 131 | cfg->host_caps |= MMC_MODE_8BIT; |
| 132 | /* fall through */ |
| 133 | case 0x4: |
| 134 | cfg->host_caps |= MMC_MODE_4BIT; |
| 135 | /* fall through */ |
| 136 | case 0x1: |
| 137 | cfg->host_caps |= MMC_MODE_1BIT; |
| 138 | break; |
| 139 | default: |
| 140 | printf("error: %s invalid bus-width property %d\n", |
| 141 | fdt_get_name(fdt, node, NULL), val); |
| 142 | return -ENOENT; |
| 143 | } |
| 144 | |
| 145 | cfg->f_max = fdtdec_get_int(fdt, node, "max-frequency", 52000000); |
| 146 | |
| 147 | if (fdtdec_get_bool(fdt, node, "cap-sd-highspeed")) |
| 148 | cfg->host_caps |= MMC_CAP(SD_HS); |
| 149 | if (fdtdec_get_bool(fdt, node, "cap-mmc-highspeed")) |
| 150 | cfg->host_caps |= MMC_CAP(MMC_HS); |
| 151 | if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr12")) |
| 152 | cfg->host_caps |= MMC_CAP(UHS_SDR12); |
| 153 | if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr25")) |
| 154 | cfg->host_caps |= MMC_CAP(UHS_SDR25); |
| 155 | if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr50")) |
| 156 | cfg->host_caps |= MMC_CAP(UHS_SDR50); |
| 157 | if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr104")) |
| 158 | cfg->host_caps |= MMC_CAP(UHS_SDR104); |
| 159 | if (fdtdec_get_bool(fdt, node, "sd-uhs-ddr50")) |
| 160 | cfg->host_caps |= MMC_CAP(UHS_DDR50); |
| 161 | if (fdtdec_get_bool(fdt, node, "mmc-ddr-1_8v")) |
| 162 | cfg->host_caps |= MMC_CAP(MMC_DDR_52); |
| 163 | if (fdtdec_get_bool(fdt, node, "mmc-hs200-1_8v")) |
| 164 | cfg->host_caps |= MMC_CAP(MMC_HS_200); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 169 | struct mmc *mmc_get_mmc_dev(struct udevice *dev) |
| 170 | { |
| 171 | struct mmc_uclass_priv *upriv; |
| 172 | |
| 173 | if (!device_active(dev)) |
| 174 | return NULL; |
| 175 | upriv = dev_get_uclass_priv(dev); |
| 176 | return upriv->mmc; |
| 177 | } |
| 178 | |
Simon Glass | 5f4bd8c | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 179 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 180 | struct mmc *find_mmc_device(int dev_num) |
| 181 | { |
| 182 | struct udevice *dev, *mmc_dev; |
| 183 | int ret; |
| 184 | |
Simon Glass | bcad604 | 2017-05-27 11:37:19 -0600 | [diff] [blame] | 185 | ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev); |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 186 | |
| 187 | if (ret) { |
| 188 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 189 | printf("MMC Device %d not found\n", dev_num); |
| 190 | #endif |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | mmc_dev = dev_get_parent(dev); |
| 195 | |
Simon Glass | bcad604 | 2017-05-27 11:37:19 -0600 | [diff] [blame] | 196 | struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); |
| 197 | |
| 198 | return mmc; |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | int get_mmc_num(void) |
| 202 | { |
Kever Yang | 3845660 | 2016-07-22 17:22:50 +0800 | [diff] [blame] | 203 | return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0); |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | int mmc_get_next_devnum(void) |
| 207 | { |
Masahiro Yamada | a6f07e5 | 2016-10-14 00:13:18 +0900 | [diff] [blame] | 208 | return blk_find_max_devnum(IF_TYPE_MMC); |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | struct blk_desc *mmc_get_blk_desc(struct mmc *mmc) |
| 212 | { |
| 213 | struct blk_desc *desc; |
| 214 | struct udevice *dev; |
| 215 | |
| 216 | device_find_first_child(mmc->dev, &dev); |
| 217 | if (!dev) |
| 218 | return NULL; |
| 219 | desc = dev_get_uclass_platdata(dev); |
| 220 | |
| 221 | return desc; |
| 222 | } |
| 223 | |
| 224 | void mmc_do_preinit(void) |
| 225 | { |
| 226 | struct udevice *dev; |
| 227 | struct uclass *uc; |
| 228 | int ret; |
| 229 | |
| 230 | ret = uclass_get(UCLASS_MMC, &uc); |
| 231 | if (ret) |
| 232 | return; |
| 233 | uclass_foreach_dev(dev, uc) { |
| 234 | struct mmc *m = mmc_get_mmc_dev(dev); |
| 235 | |
| 236 | if (!m) |
| 237 | continue; |
| 238 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 239 | mmc_set_preinit(m, 1); |
| 240 | #endif |
| 241 | if (m->preinit) |
| 242 | mmc_start_init(m); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 247 | void print_mmc_devices(char separator) |
| 248 | { |
| 249 | struct udevice *dev; |
| 250 | char *mmc_type; |
| 251 | bool first = true; |
| 252 | |
| 253 | for (uclass_first_device(UCLASS_MMC, &dev); |
| 254 | dev; |
Xu Ziyuan | 77d747e | 2016-07-23 11:11:22 +0800 | [diff] [blame] | 255 | uclass_next_device(&dev), first = false) { |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 256 | struct mmc *m = mmc_get_mmc_dev(dev); |
| 257 | |
| 258 | if (!first) { |
| 259 | printf("%c", separator); |
| 260 | if (separator != '\n') |
| 261 | puts(" "); |
| 262 | } |
| 263 | if (m->has_init) |
| 264 | mmc_type = IS_SD(m) ? "SD" : "eMMC"; |
| 265 | else |
| 266 | mmc_type = NULL; |
| 267 | |
| 268 | printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum); |
| 269 | if (mmc_type) |
| 270 | printf(" (%s)", mmc_type); |
| 271 | } |
| 272 | |
| 273 | printf("\n"); |
| 274 | } |
| 275 | |
| 276 | #else |
| 277 | void print_mmc_devices(char separator) { } |
| 278 | #endif |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 279 | |
| 280 | int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg) |
| 281 | { |
| 282 | struct blk_desc *bdesc; |
| 283 | struct udevice *bdev; |
Jaehoon Chung | 2910da8 | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 284 | int ret, devnum = -1; |
| 285 | |
Simon Glass | 9e720b0 | 2017-04-23 20:02:09 -0600 | [diff] [blame] | 286 | if (!mmc_get_ops(dev)) |
| 287 | return -ENOSYS; |
Jaehoon Chung | 2910da8 | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 288 | #ifndef CONFIG_SPL_BUILD |
| 289 | /* Use the fixed index with aliase node's index */ |
Simon Glass | 6f4187c | 2017-05-18 20:09:36 -0600 | [diff] [blame] | 290 | ret = dev_read_alias_seq(dev, &devnum); |
| 291 | debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum); |
Jaehoon Chung | 2910da8 | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 292 | #endif |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 293 | |
Jaehoon Chung | 2910da8 | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 294 | ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC, |
| 295 | devnum, 512, 0, &bdev); |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 296 | if (ret) { |
| 297 | debug("Cannot create block device\n"); |
| 298 | return ret; |
| 299 | } |
| 300 | bdesc = dev_get_uclass_platdata(bdev); |
| 301 | mmc->cfg = cfg; |
| 302 | mmc->priv = dev; |
| 303 | |
| 304 | /* the following chunk was from mmc_register() */ |
| 305 | |
| 306 | /* Setup dsr related values */ |
| 307 | mmc->dsr_imp = 0; |
| 308 | mmc->dsr = 0xffffffff; |
| 309 | /* Setup the universal parts of the block interface just once */ |
| 310 | bdesc->removable = 1; |
| 311 | |
| 312 | /* setup initial part type */ |
| 313 | bdesc->part_type = cfg->part_type; |
| 314 | mmc->dev = dev; |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | int mmc_unbind(struct udevice *dev) |
| 320 | { |
| 321 | struct udevice *bdev; |
| 322 | |
| 323 | device_find_first_child(dev, &bdev); |
| 324 | if (bdev) { |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 325 | device_remove(bdev, DM_REMOVE_NORMAL); |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 326 | device_unbind(bdev); |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int mmc_select_hwpart(struct udevice *bdev, int hwpart) |
| 333 | { |
| 334 | struct udevice *mmc_dev = dev_get_parent(bdev); |
| 335 | struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); |
| 336 | struct blk_desc *desc = dev_get_uclass_platdata(bdev); |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 337 | |
| 338 | if (desc->hwpart == hwpart) |
| 339 | return 0; |
| 340 | |
| 341 | if (mmc->part_config == MMCPART_NOAVAILABLE) |
| 342 | return -EMEDIUMTYPE; |
| 343 | |
Masahiro Yamada | a6f07e5 | 2016-10-14 00:13:18 +0900 | [diff] [blame] | 344 | return mmc_switch_part(mmc, hwpart); |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 345 | } |
| 346 | |
Fiach Antaw | 7cdbe50 | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 347 | static int mmc_blk_probe(struct udevice *dev) |
| 348 | { |
Simon Glass | e11b239 | 2017-04-23 20:02:10 -0600 | [diff] [blame] | 349 | struct udevice *mmc_dev = dev_get_parent(dev); |
| 350 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev); |
| 351 | struct mmc *mmc = upriv->mmc; |
| 352 | int ret; |
| 353 | |
| 354 | ret = mmc_init(mmc); |
| 355 | if (ret) { |
| 356 | debug("%s: mmc_init() failed (err=%d)\n", __func__, ret); |
| 357 | return ret; |
| 358 | } |
Fiach Antaw | 7cdbe50 | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 359 | |
Simon Glass | e11b239 | 2017-04-23 20:02:10 -0600 | [diff] [blame] | 360 | return 0; |
Fiach Antaw | 7cdbe50 | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 361 | } |
| 362 | |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 363 | static const struct blk_ops mmc_blk_ops = { |
| 364 | .read = mmc_bread, |
| 365 | #ifndef CONFIG_SPL_BUILD |
| 366 | .write = mmc_bwrite, |
Simon Glass | fcc53c1 | 2016-10-01 14:43:17 -0600 | [diff] [blame] | 367 | .erase = mmc_berase, |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 368 | #endif |
| 369 | .select_hwpart = mmc_select_hwpart, |
| 370 | }; |
| 371 | |
| 372 | U_BOOT_DRIVER(mmc_blk) = { |
| 373 | .name = "mmc_blk", |
| 374 | .id = UCLASS_BLK, |
| 375 | .ops = &mmc_blk_ops, |
Fiach Antaw | 7cdbe50 | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 376 | .probe = mmc_blk_probe, |
Simon Glass | 2f1ea90 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 377 | }; |
Simon Glass | 4bca666 | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 378 | #endif /* CONFIG_BLK */ |
| 379 | |
Simon Glass | 1e8eb1b | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 380 | U_BOOT_DRIVER(mmc) = { |
| 381 | .name = "mmc", |
| 382 | .id = UCLASS_MMC, |
| 383 | }; |
| 384 | |
| 385 | UCLASS_DRIVER(mmc) = { |
| 386 | .id = UCLASS_MMC, |
| 387 | .name = "mmc", |
| 388 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 389 | .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv), |
| 390 | }; |