mmc-uclass.c 10 KB

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