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