Simon Glass | e7ecf7c | 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 | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 11 | #include <dm/device-internal.h> |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 12 | #include <dm/lists.h> |
| 13 | #include <dm/root.h> |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 14 | #include "mmc_private.h" |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 15 | |
Jaehoon Chung | 02ad33a | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Simon Glass | 8ca51e5 | 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 | 318a7a5 | 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 | |
Simon Glass | 8ca51e5 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 67 | int dm_mmc_get_wp(struct udevice *dev) |
| 68 | { |
| 69 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 70 | |
| 71 | if (!ops->get_wp) |
| 72 | return -ENOSYS; |
| 73 | return ops->get_wp(dev); |
| 74 | } |
| 75 | |
| 76 | int mmc_getwp(struct mmc *mmc) |
| 77 | { |
| 78 | return dm_mmc_get_wp(mmc->dev); |
| 79 | } |
| 80 | |
| 81 | int dm_mmc_get_cd(struct udevice *dev) |
| 82 | { |
| 83 | struct dm_mmc_ops *ops = mmc_get_ops(dev); |
| 84 | |
| 85 | if (!ops->get_cd) |
| 86 | return -ENOSYS; |
| 87 | return ops->get_cd(dev); |
| 88 | } |
| 89 | |
| 90 | int mmc_getcd(struct mmc *mmc) |
| 91 | { |
| 92 | return dm_mmc_get_cd(mmc->dev); |
| 93 | } |
Simon Glass | 8ca51e5 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 94 | |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 95 | struct mmc *mmc_get_mmc_dev(struct udevice *dev) |
| 96 | { |
| 97 | struct mmc_uclass_priv *upriv; |
| 98 | |
| 99 | if (!device_active(dev)) |
| 100 | return NULL; |
| 101 | upriv = dev_get_uclass_priv(dev); |
| 102 | return upriv->mmc; |
| 103 | } |
| 104 | |
Simon Glass | c4d660d | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 105 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 106 | struct mmc *find_mmc_device(int dev_num) |
| 107 | { |
| 108 | struct udevice *dev, *mmc_dev; |
| 109 | int ret; |
| 110 | |
Simon Glass | 9752564 | 2017-05-27 11:37:19 -0600 | [diff] [blame] | 111 | ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev); |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 112 | |
| 113 | if (ret) { |
| 114 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 115 | printf("MMC Device %d not found\n", dev_num); |
| 116 | #endif |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | mmc_dev = dev_get_parent(dev); |
| 121 | |
Simon Glass | 9752564 | 2017-05-27 11:37:19 -0600 | [diff] [blame] | 122 | struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); |
| 123 | |
| 124 | return mmc; |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | int get_mmc_num(void) |
| 128 | { |
Kever Yang | 46683f3 | 2016-07-22 17:22:50 +0800 | [diff] [blame] | 129 | return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0); |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int mmc_get_next_devnum(void) |
| 133 | { |
Masahiro Yamada | dd399cb | 2016-10-14 00:13:18 +0900 | [diff] [blame] | 134 | return blk_find_max_devnum(IF_TYPE_MMC); |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | struct blk_desc *mmc_get_blk_desc(struct mmc *mmc) |
| 138 | { |
| 139 | struct blk_desc *desc; |
| 140 | struct udevice *dev; |
| 141 | |
| 142 | device_find_first_child(mmc->dev, &dev); |
| 143 | if (!dev) |
| 144 | return NULL; |
| 145 | desc = dev_get_uclass_platdata(dev); |
| 146 | |
| 147 | return desc; |
| 148 | } |
| 149 | |
| 150 | void mmc_do_preinit(void) |
| 151 | { |
| 152 | struct udevice *dev; |
| 153 | struct uclass *uc; |
| 154 | int ret; |
| 155 | |
| 156 | ret = uclass_get(UCLASS_MMC, &uc); |
| 157 | if (ret) |
| 158 | return; |
| 159 | uclass_foreach_dev(dev, uc) { |
| 160 | struct mmc *m = mmc_get_mmc_dev(dev); |
| 161 | |
| 162 | if (!m) |
| 163 | continue; |
| 164 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 165 | mmc_set_preinit(m, 1); |
| 166 | #endif |
| 167 | if (m->preinit) |
| 168 | mmc_start_init(m); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 173 | void print_mmc_devices(char separator) |
| 174 | { |
| 175 | struct udevice *dev; |
| 176 | char *mmc_type; |
| 177 | bool first = true; |
| 178 | |
| 179 | for (uclass_first_device(UCLASS_MMC, &dev); |
| 180 | dev; |
Xu Ziyuan | 1bd4f92 | 2016-07-23 11:11:22 +0800 | [diff] [blame] | 181 | uclass_next_device(&dev), first = false) { |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 182 | struct mmc *m = mmc_get_mmc_dev(dev); |
| 183 | |
| 184 | if (!first) { |
| 185 | printf("%c", separator); |
| 186 | if (separator != '\n') |
| 187 | puts(" "); |
| 188 | } |
| 189 | if (m->has_init) |
| 190 | mmc_type = IS_SD(m) ? "SD" : "eMMC"; |
| 191 | else |
| 192 | mmc_type = NULL; |
| 193 | |
| 194 | printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum); |
| 195 | if (mmc_type) |
| 196 | printf(" (%s)", mmc_type); |
| 197 | } |
| 198 | |
| 199 | printf("\n"); |
| 200 | } |
| 201 | |
| 202 | #else |
| 203 | void print_mmc_devices(char separator) { } |
| 204 | #endif |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 205 | |
| 206 | int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg) |
| 207 | { |
| 208 | struct blk_desc *bdesc; |
| 209 | struct udevice *bdev; |
Jaehoon Chung | 02ad33a | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 210 | int ret, devnum = -1; |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 211 | |
Simon Glass | 6665602 | 2017-04-23 20:02:09 -0600 | [diff] [blame] | 212 | if (!mmc_get_ops(dev)) |
| 213 | return -ENOSYS; |
Jaehoon Chung | 02ad33a | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 214 | #ifndef CONFIG_SPL_BUILD |
| 215 | /* Use the fixed index with aliase node's index */ |
Simon Glass | 66e0ed5 | 2017-05-18 20:09:36 -0600 | [diff] [blame] | 216 | ret = dev_read_alias_seq(dev, &devnum); |
| 217 | debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum); |
Jaehoon Chung | 02ad33a | 2017-02-02 13:41:14 +0900 | [diff] [blame] | 218 | #endif |
| 219 | |
| 220 | ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC, |
| 221 | devnum, 512, 0, &bdev); |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 222 | if (ret) { |
| 223 | debug("Cannot create block device\n"); |
| 224 | return ret; |
| 225 | } |
| 226 | bdesc = dev_get_uclass_platdata(bdev); |
| 227 | mmc->cfg = cfg; |
| 228 | mmc->priv = dev; |
| 229 | |
| 230 | /* the following chunk was from mmc_register() */ |
| 231 | |
| 232 | /* Setup dsr related values */ |
| 233 | mmc->dsr_imp = 0; |
| 234 | mmc->dsr = 0xffffffff; |
| 235 | /* Setup the universal parts of the block interface just once */ |
| 236 | bdesc->removable = 1; |
| 237 | |
| 238 | /* setup initial part type */ |
| 239 | bdesc->part_type = cfg->part_type; |
| 240 | mmc->dev = dev; |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int mmc_unbind(struct udevice *dev) |
| 246 | { |
| 247 | struct udevice *bdev; |
| 248 | |
| 249 | device_find_first_child(dev, &bdev); |
| 250 | if (bdev) { |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 251 | device_remove(bdev, DM_REMOVE_NORMAL); |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 252 | device_unbind(bdev); |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int mmc_select_hwpart(struct udevice *bdev, int hwpart) |
| 259 | { |
| 260 | struct udevice *mmc_dev = dev_get_parent(bdev); |
| 261 | struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); |
| 262 | struct blk_desc *desc = dev_get_uclass_platdata(bdev); |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 263 | |
| 264 | if (desc->hwpart == hwpart) |
| 265 | return 0; |
| 266 | |
| 267 | if (mmc->part_config == MMCPART_NOAVAILABLE) |
| 268 | return -EMEDIUMTYPE; |
| 269 | |
Masahiro Yamada | dd399cb | 2016-10-14 00:13:18 +0900 | [diff] [blame] | 270 | return mmc_switch_part(mmc, hwpart); |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 271 | } |
| 272 | |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 273 | static int mmc_blk_probe(struct udevice *dev) |
| 274 | { |
Simon Glass | 854f9a7 | 2017-04-23 20:02:10 -0600 | [diff] [blame] | 275 | struct udevice *mmc_dev = dev_get_parent(dev); |
| 276 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev); |
| 277 | struct mmc *mmc = upriv->mmc; |
| 278 | int ret; |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 279 | |
Simon Glass | 854f9a7 | 2017-04-23 20:02:10 -0600 | [diff] [blame] | 280 | ret = mmc_init(mmc); |
| 281 | if (ret) { |
| 282 | debug("%s: mmc_init() failed (err=%d)\n", __func__, ret); |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | return 0; |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 287 | } |
| 288 | |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 289 | static const struct blk_ops mmc_blk_ops = { |
| 290 | .read = mmc_bread, |
| 291 | #ifndef CONFIG_SPL_BUILD |
| 292 | .write = mmc_bwrite, |
Simon Glass | 561e624 | 2016-10-01 14:43:17 -0600 | [diff] [blame] | 293 | .erase = mmc_berase, |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 294 | #endif |
| 295 | .select_hwpart = mmc_select_hwpart, |
| 296 | }; |
| 297 | |
| 298 | U_BOOT_DRIVER(mmc_blk) = { |
| 299 | .name = "mmc_blk", |
| 300 | .id = UCLASS_BLK, |
| 301 | .ops = &mmc_blk_ops, |
Fiach Antaw | a0269bb | 2017-01-25 19:00:24 +1000 | [diff] [blame] | 302 | .probe = mmc_blk_probe, |
Simon Glass | eede897 | 2016-06-12 23:30:16 -0600 | [diff] [blame] | 303 | }; |
Simon Glass | 8ef761e | 2016-05-01 13:52:39 -0600 | [diff] [blame] | 304 | #endif /* CONFIG_BLK */ |
| 305 | |
Simon Glass | e7ecf7c | 2015-06-23 15:38:48 -0600 | [diff] [blame] | 306 | U_BOOT_DRIVER(mmc) = { |
| 307 | .name = "mmc", |
| 308 | .id = UCLASS_MMC, |
| 309 | }; |
| 310 | |
| 311 | UCLASS_DRIVER(mmc) = { |
| 312 | .id = UCLASS_MMC, |
| 313 | .name = "mmc", |
| 314 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 315 | .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv), |
| 316 | }; |