mmc-uclass.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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_host_power_cycle(struct udevice *dev)
  118. {
  119. struct dm_mmc_ops *ops = mmc_get_ops(dev);
  120. if (ops->host_power_cycle)
  121. return ops->host_power_cycle(dev);
  122. return 0;
  123. }
  124. int mmc_host_power_cycle(struct mmc *mmc)
  125. {
  126. return dm_mmc_host_power_cycle(mmc->dev);
  127. }
  128. int dm_mmc_deferred_probe(struct udevice *dev)
  129. {
  130. struct dm_mmc_ops *ops = mmc_get_ops(dev);
  131. if (ops->deferred_probe)
  132. return ops->deferred_probe(dev);
  133. return 0;
  134. }
  135. int mmc_deferred_probe(struct mmc *mmc)
  136. {
  137. return dm_mmc_deferred_probe(mmc->dev);
  138. }
  139. int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
  140. {
  141. int val;
  142. val = dev_read_u32_default(dev, "bus-width", 1);
  143. switch (val) {
  144. case 0x8:
  145. cfg->host_caps |= MMC_MODE_8BIT;
  146. /* fall through */
  147. case 0x4:
  148. cfg->host_caps |= MMC_MODE_4BIT;
  149. /* fall through */
  150. case 0x1:
  151. cfg->host_caps |= MMC_MODE_1BIT;
  152. break;
  153. default:
  154. dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
  155. return -EINVAL;
  156. }
  157. /* f_max is obtained from the optional "max-frequency" property */
  158. dev_read_u32(dev, "max-frequency", &cfg->f_max);
  159. if (dev_read_bool(dev, "cap-sd-highspeed"))
  160. cfg->host_caps |= MMC_CAP(SD_HS);
  161. if (dev_read_bool(dev, "cap-mmc-highspeed"))
  162. cfg->host_caps |= MMC_CAP(MMC_HS);
  163. if (dev_read_bool(dev, "sd-uhs-sdr12"))
  164. cfg->host_caps |= MMC_CAP(UHS_SDR12);
  165. if (dev_read_bool(dev, "sd-uhs-sdr25"))
  166. cfg->host_caps |= MMC_CAP(UHS_SDR25);
  167. if (dev_read_bool(dev, "sd-uhs-sdr50"))
  168. cfg->host_caps |= MMC_CAP(UHS_SDR50);
  169. if (dev_read_bool(dev, "sd-uhs-sdr104"))
  170. cfg->host_caps |= MMC_CAP(UHS_SDR104);
  171. if (dev_read_bool(dev, "sd-uhs-ddr50"))
  172. cfg->host_caps |= MMC_CAP(UHS_DDR50);
  173. if (dev_read_bool(dev, "mmc-ddr-1_8v"))
  174. cfg->host_caps |= MMC_CAP(MMC_DDR_52);
  175. if (dev_read_bool(dev, "mmc-ddr-1_2v"))
  176. cfg->host_caps |= MMC_CAP(MMC_DDR_52);
  177. if (dev_read_bool(dev, "mmc-hs200-1_8v"))
  178. cfg->host_caps |= MMC_CAP(MMC_HS_200);
  179. if (dev_read_bool(dev, "mmc-hs200-1_2v"))
  180. cfg->host_caps |= MMC_CAP(MMC_HS_200);
  181. if (dev_read_bool(dev, "mmc-hs400-1_8v"))
  182. cfg->host_caps |= MMC_CAP(MMC_HS_400);
  183. if (dev_read_bool(dev, "mmc-hs400-1_2v"))
  184. cfg->host_caps |= MMC_CAP(MMC_HS_400);
  185. if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe"))
  186. cfg->host_caps |= MMC_CAP(MMC_HS_400_ES);
  187. if (dev_read_bool(dev, "non-removable")) {
  188. cfg->host_caps |= MMC_CAP_NONREMOVABLE;
  189. } else {
  190. if (dev_read_bool(dev, "cd-inverted"))
  191. cfg->host_caps |= MMC_CAP_CD_ACTIVE_HIGH;
  192. if (dev_read_bool(dev, "broken-cd"))
  193. cfg->host_caps |= MMC_CAP_NEEDS_POLL;
  194. }
  195. if (dev_read_bool(dev, "no-1-8-v")) {
  196. cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 |
  197. MMC_MODE_HS400 | MMC_MODE_HS400_ES);
  198. }
  199. return 0;
  200. }
  201. struct mmc *mmc_get_mmc_dev(const struct udevice *dev)
  202. {
  203. struct mmc_uclass_priv *upriv;
  204. if (!device_active(dev))
  205. return NULL;
  206. upriv = dev_get_uclass_priv(dev);
  207. return upriv->mmc;
  208. }
  209. #if CONFIG_IS_ENABLED(BLK)
  210. struct mmc *find_mmc_device(int dev_num)
  211. {
  212. struct udevice *dev, *mmc_dev;
  213. int ret;
  214. ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev);
  215. if (ret) {
  216. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  217. printf("MMC Device %d not found\n", dev_num);
  218. #endif
  219. return NULL;
  220. }
  221. mmc_dev = dev_get_parent(dev);
  222. struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
  223. return mmc;
  224. }
  225. int get_mmc_num(void)
  226. {
  227. return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
  228. }
  229. int mmc_get_next_devnum(void)
  230. {
  231. return blk_find_max_devnum(IF_TYPE_MMC);
  232. }
  233. struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
  234. {
  235. struct blk_desc *desc;
  236. struct udevice *dev;
  237. device_find_first_child(mmc->dev, &dev);
  238. if (!dev)
  239. return NULL;
  240. desc = dev_get_uclass_platdata(dev);
  241. return desc;
  242. }
  243. void mmc_do_preinit(void)
  244. {
  245. struct udevice *dev;
  246. struct uclass *uc;
  247. int ret;
  248. ret = uclass_get(UCLASS_MMC, &uc);
  249. if (ret)
  250. return;
  251. uclass_foreach_dev(dev, uc) {
  252. struct mmc *m = mmc_get_mmc_dev(dev);
  253. if (!m)
  254. continue;
  255. if (m->preinit)
  256. mmc_start_init(m);
  257. }
  258. }
  259. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  260. void print_mmc_devices(char separator)
  261. {
  262. struct udevice *dev;
  263. char *mmc_type;
  264. bool first = true;
  265. for (uclass_first_device(UCLASS_MMC, &dev);
  266. dev;
  267. uclass_next_device(&dev), first = false) {
  268. struct mmc *m = mmc_get_mmc_dev(dev);
  269. if (!first) {
  270. printf("%c", separator);
  271. if (separator != '\n')
  272. puts(" ");
  273. }
  274. if (m->has_init)
  275. mmc_type = IS_SD(m) ? "SD" : "eMMC";
  276. else
  277. mmc_type = NULL;
  278. printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
  279. if (mmc_type)
  280. printf(" (%s)", mmc_type);
  281. }
  282. printf("\n");
  283. }
  284. #else
  285. void print_mmc_devices(char separator) { }
  286. #endif
  287. int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
  288. {
  289. struct blk_desc *bdesc;
  290. struct udevice *bdev;
  291. int ret, devnum = -1;
  292. if (!mmc_get_ops(dev))
  293. return -ENOSYS;
  294. #ifndef CONFIG_SPL_BUILD
  295. /* Use the fixed index with aliase node's index */
  296. ret = dev_read_alias_seq(dev, &devnum);
  297. debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum);
  298. #endif
  299. ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
  300. devnum, 512, 0, &bdev);
  301. if (ret) {
  302. debug("Cannot create block device\n");
  303. return ret;
  304. }
  305. bdesc = dev_get_uclass_platdata(bdev);
  306. mmc->cfg = cfg;
  307. mmc->priv = dev;
  308. /* the following chunk was from mmc_register() */
  309. /* Setup dsr related values */
  310. mmc->dsr_imp = 0;
  311. mmc->dsr = 0xffffffff;
  312. /* Setup the universal parts of the block interface just once */
  313. bdesc->removable = 1;
  314. /* setup initial part type */
  315. bdesc->part_type = cfg->part_type;
  316. mmc->dev = dev;
  317. return 0;
  318. }
  319. int mmc_unbind(struct udevice *dev)
  320. {
  321. struct udevice *bdev;
  322. device_find_first_child(dev, &bdev);
  323. if (bdev) {
  324. device_remove(bdev, DM_REMOVE_NORMAL);
  325. device_unbind(bdev);
  326. }
  327. return 0;
  328. }
  329. static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
  330. {
  331. struct udevice *mmc_dev = dev_get_parent(bdev);
  332. struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
  333. struct blk_desc *desc = dev_get_uclass_platdata(bdev);
  334. int ret;
  335. if (desc->hwpart == hwpart)
  336. return 0;
  337. if (mmc->part_config == MMCPART_NOAVAILABLE)
  338. return -EMEDIUMTYPE;
  339. ret = mmc_switch_part(mmc, hwpart);
  340. if (!ret)
  341. blkcache_invalidate(desc->if_type, desc->devnum);
  342. return ret;
  343. }
  344. static int mmc_blk_probe(struct udevice *dev)
  345. {
  346. struct udevice *mmc_dev = dev_get_parent(dev);
  347. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
  348. struct mmc *mmc = upriv->mmc;
  349. int ret;
  350. ret = mmc_init(mmc);
  351. if (ret) {
  352. debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
  353. return ret;
  354. }
  355. return 0;
  356. }
  357. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
  358. CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
  359. CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
  360. static int mmc_blk_remove(struct udevice *dev)
  361. {
  362. struct udevice *mmc_dev = dev_get_parent(dev);
  363. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
  364. struct mmc *mmc = upriv->mmc;
  365. return mmc_deinit(mmc);
  366. }
  367. #endif
  368. static const struct blk_ops mmc_blk_ops = {
  369. .read = mmc_bread,
  370. #if CONFIG_IS_ENABLED(MMC_WRITE)
  371. .write = mmc_bwrite,
  372. .erase = mmc_berase,
  373. #endif
  374. .select_hwpart = mmc_select_hwpart,
  375. };
  376. U_BOOT_DRIVER(mmc_blk) = {
  377. .name = "mmc_blk",
  378. .id = UCLASS_BLK,
  379. .ops = &mmc_blk_ops,
  380. .probe = mmc_blk_probe,
  381. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
  382. CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
  383. CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
  384. .remove = mmc_blk_remove,
  385. .flags = DM_FLAG_OS_PREPARE,
  386. #endif
  387. };
  388. #endif /* CONFIG_BLK */
  389. UCLASS_DRIVER(mmc) = {
  390. .id = UCLASS_MMC,
  391. .name = "mmc",
  392. .flags = DM_UC_FLAG_SEQ_ALIAS,
  393. .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),
  394. };