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