mmc-uclass.c 10 KB

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