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