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