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 <dm/root.h> 14 #include "mmc_private.h" 15 16 DECLARE_GLOBAL_DATA_PTR; 17 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, retry_time = 3; 24 25 retry: 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 if (ret && cmd->cmdidx != SD_CMD_SEND_IF_COND && 34 cmd->cmdidx != MMC_CMD_APP_CMD && 35 cmd->cmdidx != MMC_CMD_SEND_OP_COND && 36 cmd->cmdidx != MMC_SEND_TUNING_BLOCK_HS200 && 37 cmd->cmdidx != MMC_CMD_READ_MULTIPLE_BLOCK && 38 cmd->cmdidx != MMC_CMD_WRITE_MULTIPLE_BLOCK) { 39 /* execute tuning at last retry. */ 40 if (retry_time == 1 && 41 mmc->timing == MMC_TIMING_MMC_HS200 && 42 ops->execute_tuning) { 43 u32 opcode = MMC_SEND_TUNING_BLOCK_HS200; 44 ops->execute_tuning(mmc->dev, opcode); 45 } 46 if (retry_time-- > 0) 47 goto retry; 48 printf("MMC error: The cmd index is %d, ret is %d\n", cmd->cmdidx, ret); 49 } 50 51 return ret; 52 } 53 54 #ifdef CONFIG_SPL_BLK_READ_PREPARE 55 int dm_mmc_send_cmd_prepare(struct udevice *dev, struct mmc_cmd *cmd, 56 struct mmc_data *data) 57 { 58 struct mmc *mmc = mmc_get_mmc_dev(dev); 59 struct dm_mmc_ops *ops = mmc_get_ops(dev); 60 int ret; 61 62 mmmc_trace_before_send(mmc, cmd); 63 if (ops->send_cmd_prepare) 64 ret = ops->send_cmd_prepare(dev, cmd, data); 65 else 66 ret = -ENOSYS; 67 mmmc_trace_after_send(mmc, cmd, ret); 68 if (ret && cmd->cmdidx != SD_CMD_SEND_IF_COND 69 && cmd->cmdidx != MMC_CMD_APP_CMD) 70 printf("MMC error: The cmd index is %d, ret is %d\n", cmd->cmdidx, ret); 71 72 return ret; 73 } 74 #endif 75 76 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) 77 { 78 return dm_mmc_send_cmd(mmc->dev, cmd, data); 79 } 80 81 #ifdef CONFIG_SPL_BLK_READ_PREPARE 82 int mmc_send_cmd_prepare(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) 83 { 84 return dm_mmc_send_cmd_prepare(mmc->dev, cmd, data); 85 } 86 #endif 87 88 bool mmc_card_busy(struct mmc *mmc) 89 { 90 struct dm_mmc_ops *ops = mmc_get_ops(mmc->dev); 91 92 if (!ops->card_busy) 93 return -ENOSYS; 94 return ops->card_busy(mmc->dev); 95 } 96 97 bool mmc_can_card_busy(struct mmc *mmc) 98 { 99 struct dm_mmc_ops *ops = mmc_get_ops(mmc->dev); 100 101 return !!ops->card_busy; 102 } 103 104 int dm_mmc_set_ios(struct udevice *dev) 105 { 106 struct dm_mmc_ops *ops = mmc_get_ops(dev); 107 108 if (!ops->set_ios) 109 return -ENOSYS; 110 return ops->set_ios(dev); 111 } 112 113 int mmc_set_ios(struct mmc *mmc) 114 { 115 return dm_mmc_set_ios(mmc->dev); 116 } 117 118 int dm_mmc_get_wp(struct udevice *dev) 119 { 120 struct dm_mmc_ops *ops = mmc_get_ops(dev); 121 122 if (!ops->get_wp) 123 return -ENOSYS; 124 return ops->get_wp(dev); 125 } 126 127 int mmc_getwp(struct mmc *mmc) 128 { 129 return dm_mmc_get_wp(mmc->dev); 130 } 131 132 int dm_mmc_get_cd(struct udevice *dev) 133 { 134 struct dm_mmc_ops *ops = mmc_get_ops(dev); 135 136 if (!ops->get_cd) 137 return -ENOSYS; 138 return ops->get_cd(dev); 139 } 140 141 int mmc_getcd(struct mmc *mmc) 142 { 143 return dm_mmc_get_cd(mmc->dev); 144 } 145 146 struct mmc *mmc_get_mmc_dev(struct udevice *dev) 147 { 148 struct mmc_uclass_priv *upriv; 149 150 if (!device_active(dev)) 151 return NULL; 152 upriv = dev_get_uclass_priv(dev); 153 return upriv->mmc; 154 } 155 156 #if CONFIG_IS_ENABLED(BLK) 157 struct mmc *find_mmc_device(int dev_num) 158 { 159 struct udevice *dev, *mmc_dev; 160 int ret; 161 162 ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev); 163 164 if (ret) { 165 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 166 printf("MMC Device %d not found\n", dev_num); 167 #endif 168 return NULL; 169 } 170 171 mmc_dev = dev_get_parent(dev); 172 173 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); 174 175 return mmc; 176 } 177 178 int get_mmc_num(void) 179 { 180 return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0); 181 } 182 183 int mmc_get_next_devnum(void) 184 { 185 return blk_find_max_devnum(IF_TYPE_MMC); 186 } 187 188 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc) 189 { 190 struct blk_desc *desc; 191 struct udevice *dev; 192 193 device_find_first_child(mmc->dev, &dev); 194 if (!dev) 195 return NULL; 196 desc = dev_get_uclass_platdata(dev); 197 198 return desc; 199 } 200 201 void mmc_do_preinit(void) 202 { 203 struct udevice *dev; 204 struct uclass *uc; 205 int ret; 206 207 ret = uclass_get(UCLASS_MMC, &uc); 208 if (ret) 209 return; 210 uclass_foreach_dev(dev, uc) { 211 struct mmc *m = mmc_get_mmc_dev(dev); 212 213 if (!m) 214 continue; 215 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT 216 mmc_set_preinit(m, 1); 217 #endif 218 if (m->preinit) 219 mmc_start_init(m); 220 } 221 } 222 223 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 224 void print_mmc_devices(char separator) 225 { 226 struct udevice *dev; 227 char *mmc_type; 228 bool first = true; 229 230 for (uclass_first_device(UCLASS_MMC, &dev); 231 dev; 232 uclass_next_device(&dev), first = false) { 233 struct mmc *m = mmc_get_mmc_dev(dev); 234 235 if (!first) { 236 printf("%c", separator); 237 if (separator != '\n') 238 puts(" "); 239 } 240 if (m->has_init) 241 mmc_type = IS_SD(m) ? "SD" : "eMMC"; 242 else 243 mmc_type = NULL; 244 245 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum); 246 if (mmc_type) 247 printf(" (%s)", mmc_type); 248 } 249 250 printf("\n"); 251 } 252 253 #else 254 void print_mmc_devices(char separator) { } 255 #endif 256 257 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg) 258 { 259 struct blk_desc *bdesc; 260 struct udevice *bdev; 261 int ret, devnum = -1; 262 263 if (!mmc_get_ops(dev)) 264 return -ENOSYS; 265 /* Use the fixed index with aliase node's index */ 266 ret = dev_read_alias_seq(dev, &devnum); 267 debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum); 268 269 ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC, 270 devnum, 512, 0, &bdev); 271 if (ret) { 272 debug("Cannot create block device\n"); 273 return ret; 274 } 275 bdesc = dev_get_uclass_platdata(bdev); 276 mmc->cfg = cfg; 277 mmc->priv = dev; 278 279 /* the following chunk was from mmc_register() */ 280 281 /* Setup dsr related values */ 282 mmc->dsr_imp = 0; 283 mmc->dsr = 0xffffffff; 284 /* Setup the universal parts of the block interface just once */ 285 bdesc->removable = 1; 286 287 /* setup initial part type */ 288 bdesc->part_type = cfg->part_type; 289 mmc->dev = dev; 290 291 return 0; 292 } 293 294 int mmc_unbind(struct udevice *dev) 295 { 296 struct udevice *bdev; 297 298 device_find_first_child(dev, &bdev); 299 if (bdev) { 300 device_remove(bdev, DM_REMOVE_NORMAL); 301 device_unbind(bdev); 302 } 303 304 return 0; 305 } 306 307 static int mmc_select_hwpart(struct udevice *bdev, int hwpart) 308 { 309 struct udevice *mmc_dev = dev_get_parent(bdev); 310 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev); 311 struct blk_desc *desc = dev_get_uclass_platdata(bdev); 312 313 if (desc->hwpart == hwpart) 314 return 0; 315 316 if (mmc->part_config == MMCPART_NOAVAILABLE) 317 return -EMEDIUMTYPE; 318 319 return mmc_switch_part(mmc, hwpart); 320 } 321 322 static int mmc_blk_probe(struct udevice *dev) 323 { 324 struct udevice *mmc_dev = dev_get_parent(dev); 325 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev); 326 struct mmc *mmc = upriv->mmc; 327 int ret; 328 329 ret = mmc_init(mmc); 330 if (ret) { 331 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret); 332 return ret; 333 } 334 335 return 0; 336 } 337 338 static const struct blk_ops mmc_blk_ops = { 339 .read = mmc_bread, 340 #if CONFIG_IS_ENABLED(MMC_WRITE) 341 .write = mmc_bwrite, 342 .erase = mmc_berase, 343 #endif 344 .select_hwpart = mmc_select_hwpart, 345 }; 346 347 U_BOOT_DRIVER(mmc_blk) = { 348 .name = "mmc_blk", 349 .id = UCLASS_BLK, 350 .ops = &mmc_blk_ops, 351 .probe = mmc_blk_probe, 352 }; 353 #endif /* CONFIG_BLK */ 354 355 U_BOOT_DRIVER(mmc) = { 356 .name = "mmc", 357 .id = UCLASS_MMC, 358 }; 359 360 UCLASS_DRIVER(mmc) = { 361 .id = UCLASS_MMC, 362 .name = "mmc", 363 .flags = DM_UC_FLAG_SEQ_ALIAS, 364 .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv), 365 }; 366