mmc-uclass.c 11 KB

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