mmc-uclass.c 12 KB

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