mmc-uclass.c 5.7 KB

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