mmc-uclass.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (C) 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <mmc.h>
  9. #include <dm.h>
  10. #include <dm/device-internal.h>
  11. #include <dm/lists.h>
  12. #include <dm/root.h>
  13. #include "mmc_private.h"
  14. DECLARE_GLOBAL_DATA_PTR;
  15. int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  16. struct mmc_data *data)
  17. {
  18. struct mmc *mmc = mmc_get_mmc_dev(dev);
  19. struct dm_mmc_ops *ops = mmc_get_ops(dev);
  20. int ret;
  21. mmmc_trace_before_send(mmc, cmd);
  22. if (ops->send_cmd)
  23. ret = ops->send_cmd(dev, cmd, data);
  24. else
  25. ret = -ENOSYS;
  26. mmmc_trace_after_send(mmc, cmd, ret);
  27. return ret;
  28. }
  29. int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  30. {
  31. return dm_mmc_send_cmd(mmc->dev, cmd, data);
  32. }
  33. int dm_mmc_set_ios(struct udevice *dev)
  34. {
  35. struct dm_mmc_ops *ops = mmc_get_ops(dev);
  36. if (!ops->set_ios)
  37. return -ENOSYS;
  38. return ops->set_ios(dev);
  39. }
  40. int mmc_set_ios(struct mmc *mmc)
  41. {
  42. return dm_mmc_set_ios(mmc->dev);
  43. }
  44. void dm_mmc_send_init_stream(struct udevice *dev)
  45. {
  46. struct dm_mmc_ops *ops = mmc_get_ops(dev);
  47. if (ops->send_init_stream)
  48. ops->send_init_stream(dev);
  49. }
  50. void mmc_send_init_stream(struct mmc *mmc)
  51. {
  52. dm_mmc_send_init_stream(mmc->dev);
  53. }
  54. int dm_mmc_get_wp(struct udevice *dev)
  55. {
  56. struct dm_mmc_ops *ops = mmc_get_ops(dev);
  57. if (!ops->get_wp)
  58. return -ENOSYS;
  59. return ops->get_wp(dev);
  60. }
  61. int mmc_getwp(struct mmc *mmc)
  62. {
  63. return dm_mmc_get_wp(mmc->dev);
  64. }
  65. int dm_mmc_get_cd(struct udevice *dev)
  66. {
  67. struct dm_mmc_ops *ops = mmc_get_ops(dev);
  68. if (!ops->get_cd)
  69. return -ENOSYS;
  70. return ops->get_cd(dev);
  71. }
  72. int mmc_getcd(struct mmc *mmc)
  73. {
  74. return dm_mmc_get_cd(mmc->dev);
  75. }
  76. struct mmc *mmc_get_mmc_dev(struct udevice *dev)
  77. {
  78. struct mmc_uclass_priv *upriv;
  79. if (!device_active(dev))
  80. return NULL;
  81. upriv = dev_get_uclass_priv(dev);
  82. return upriv->mmc;
  83. }
  84. #if CONFIG_IS_ENABLED(BLK)
  85. struct mmc *find_mmc_device(int dev_num)
  86. {
  87. struct udevice *dev, *mmc_dev;
  88. int ret;
  89. ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev);
  90. if (ret) {
  91. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  92. printf("MMC Device %d not found\n", dev_num);
  93. #endif
  94. return NULL;
  95. }
  96. mmc_dev = dev_get_parent(dev);
  97. struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
  98. return mmc;
  99. }
  100. int get_mmc_num(void)
  101. {
  102. return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
  103. }
  104. int mmc_get_next_devnum(void)
  105. {
  106. return blk_find_max_devnum(IF_TYPE_MMC);
  107. }
  108. struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
  109. {
  110. struct blk_desc *desc;
  111. struct udevice *dev;
  112. device_find_first_child(mmc->dev, &dev);
  113. if (!dev)
  114. return NULL;
  115. desc = dev_get_uclass_platdata(dev);
  116. return desc;
  117. }
  118. void mmc_do_preinit(void)
  119. {
  120. struct udevice *dev;
  121. struct uclass *uc;
  122. int ret;
  123. ret = uclass_get(UCLASS_MMC, &uc);
  124. if (ret)
  125. return;
  126. uclass_foreach_dev(dev, uc) {
  127. struct mmc *m = mmc_get_mmc_dev(dev);
  128. if (!m)
  129. continue;
  130. #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
  131. mmc_set_preinit(m, 1);
  132. #endif
  133. if (m->preinit)
  134. mmc_start_init(m);
  135. }
  136. }
  137. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  138. void print_mmc_devices(char separator)
  139. {
  140. struct udevice *dev;
  141. char *mmc_type;
  142. bool first = true;
  143. for (uclass_first_device(UCLASS_MMC, &dev);
  144. dev;
  145. uclass_next_device(&dev), first = false) {
  146. struct mmc *m = mmc_get_mmc_dev(dev);
  147. if (!first) {
  148. printf("%c", separator);
  149. if (separator != '\n')
  150. puts(" ");
  151. }
  152. if (m->has_init)
  153. mmc_type = IS_SD(m) ? "SD" : "eMMC";
  154. else
  155. mmc_type = NULL;
  156. printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
  157. if (mmc_type)
  158. printf(" (%s)", mmc_type);
  159. }
  160. printf("\n");
  161. }
  162. #else
  163. void print_mmc_devices(char separator) { }
  164. #endif
  165. int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
  166. {
  167. struct blk_desc *bdesc;
  168. struct udevice *bdev;
  169. int ret, devnum = -1;
  170. if (!mmc_get_ops(dev))
  171. return -ENOSYS;
  172. #ifndef CONFIG_SPL_BUILD
  173. /* Use the fixed index with aliase node's index */
  174. ret = dev_read_alias_seq(dev, &devnum);
  175. debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum);
  176. #endif
  177. ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
  178. devnum, 512, 0, &bdev);
  179. if (ret) {
  180. debug("Cannot create block device\n");
  181. return ret;
  182. }
  183. bdesc = dev_get_uclass_platdata(bdev);
  184. mmc->cfg = cfg;
  185. mmc->priv = dev;
  186. /* the following chunk was from mmc_register() */
  187. /* Setup dsr related values */
  188. mmc->dsr_imp = 0;
  189. mmc->dsr = 0xffffffff;
  190. /* Setup the universal parts of the block interface just once */
  191. bdesc->removable = 1;
  192. /* setup initial part type */
  193. bdesc->part_type = cfg->part_type;
  194. mmc->dev = dev;
  195. return 0;
  196. }
  197. int mmc_unbind(struct udevice *dev)
  198. {
  199. struct udevice *bdev;
  200. device_find_first_child(dev, &bdev);
  201. if (bdev) {
  202. device_remove(bdev, DM_REMOVE_NORMAL);
  203. device_unbind(bdev);
  204. }
  205. return 0;
  206. }
  207. static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
  208. {
  209. struct udevice *mmc_dev = dev_get_parent(bdev);
  210. struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
  211. struct blk_desc *desc = dev_get_uclass_platdata(bdev);
  212. if (desc->hwpart == hwpart)
  213. return 0;
  214. if (mmc->part_config == MMCPART_NOAVAILABLE)
  215. return -EMEDIUMTYPE;
  216. return mmc_switch_part(mmc, hwpart);
  217. }
  218. static int mmc_blk_probe(struct udevice *dev)
  219. {
  220. struct udevice *mmc_dev = dev_get_parent(dev);
  221. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
  222. struct mmc *mmc = upriv->mmc;
  223. int ret;
  224. ret = mmc_init(mmc);
  225. if (ret) {
  226. debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
  227. return ret;
  228. }
  229. return 0;
  230. }
  231. static const struct blk_ops mmc_blk_ops = {
  232. .read = mmc_bread,
  233. #ifndef CONFIG_SPL_BUILD
  234. .write = mmc_bwrite,
  235. .erase = mmc_berase,
  236. #endif
  237. .select_hwpart = mmc_select_hwpart,
  238. };
  239. U_BOOT_DRIVER(mmc_blk) = {
  240. .name = "mmc_blk",
  241. .id = UCLASS_BLK,
  242. .ops = &mmc_blk_ops,
  243. .probe = mmc_blk_probe,
  244. };
  245. #endif /* CONFIG_BLK */
  246. U_BOOT_DRIVER(mmc) = {
  247. .name = "mmc",
  248. .id = UCLASS_MMC,
  249. };
  250. UCLASS_DRIVER(mmc) = {
  251. .id = UCLASS_MMC,
  252. .name = "mmc",
  253. .flags = DM_UC_FLAG_SEQ_ALIAS,
  254. .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),
  255. };