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