mmc-uclass.c 5.5 KB

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