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