mmc-uclass.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #ifdef CONFIG_DM_MMC_OPS
  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_set_vdd(struct udevice *dev, bool enable)
  45. {
  46. struct mmc *mmc = mmc_get_mmc_dev(dev);
  47. struct dm_mmc_ops *ops = mmc_get_ops(dev);
  48. int ret;
  49. if (!ops->set_vdd)
  50. return -ENOSYS;
  51. return ops->set_vdd(dev, enable);
  52. }
  53. int mmc_set_vdd(struct mmc *mmc, bool enable)
  54. {
  55. return dm_mmc_set_vdd(mmc->dev, enable);
  56. }
  57. int dm_mmc_get_wp(struct udevice *dev)
  58. {
  59. struct dm_mmc_ops *ops = mmc_get_ops(dev);
  60. if (!ops->get_wp)
  61. return -ENOSYS;
  62. return ops->get_wp(dev);
  63. }
  64. int mmc_getwp(struct mmc *mmc)
  65. {
  66. return dm_mmc_get_wp(mmc->dev);
  67. }
  68. int dm_mmc_get_cd(struct udevice *dev)
  69. {
  70. struct dm_mmc_ops *ops = mmc_get_ops(dev);
  71. if (!ops->get_cd)
  72. return -ENOSYS;
  73. return ops->get_cd(dev);
  74. }
  75. int mmc_getcd(struct mmc *mmc)
  76. {
  77. return dm_mmc_get_cd(mmc->dev);
  78. }
  79. int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
  80. {
  81. struct mmc *mmc = mmc_get_mmc_dev(dev);
  82. struct dm_mmc_ops *ops = mmc_get_ops(dev);
  83. int ret;
  84. if (!ops->execute_tuning)
  85. return -ENOSYS;
  86. return ops->execute_tuning(dev, opcode);
  87. }
  88. int mmc_execute_tuning(struct mmc *mmc, uint opcode)
  89. {
  90. return dm_mmc_execute_tuning(mmc->dev, opcode);
  91. }
  92. #endif
  93. int mmc_of_parse(const void *fdt, int node, struct mmc_config *cfg)
  94. {
  95. int val;
  96. val = fdtdec_get_int(fdt, node, "bus-width", -1);
  97. if (val < 0) {
  98. printf("bus-width property missing; assuming 1 bit\n");
  99. val = 1;
  100. }
  101. switch (val) {
  102. case 0x8:
  103. cfg->host_caps |= MMC_MODE_8BIT;
  104. case 0x4:
  105. cfg->host_caps |= MMC_MODE_4BIT;
  106. break;
  107. case 0x1:
  108. break;
  109. default:
  110. printf("error: invalid bus-width property\n");
  111. return -ENOENT;
  112. }
  113. cfg->f_max = fdtdec_get_int(fdt, node, "max-frequency", 52000000);
  114. if (fdtdec_get_bool(fdt, node, "cap-sd-highspeed"))
  115. cfg->host_caps |= MMC_MODE_HS;
  116. if (fdtdec_get_bool(fdt, node, "cap-mmc-highspeed"))
  117. cfg->host_caps |= MMC_MODE_HS;
  118. if (fdtdec_get_bool(fdt, node, "mmc-ddr-1_8v"))
  119. cfg->host_caps |= MMC_MODE_DDR_52MHz;
  120. if (fdtdec_get_bool(fdt, node, "mmc-hs200-1_8v"))
  121. cfg->host_caps |= MMC_MODE_HS200;
  122. if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr104"))
  123. cfg->host_caps |= MMC_MODE_UHS_SDR104;
  124. if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr50"))
  125. cfg->host_caps |= MMC_MODE_UHS_SDR50;
  126. if (fdtdec_get_bool(fdt, node, "sd-uhs-ddr50"))
  127. cfg->host_caps |= MMC_MODE_UHS_DDR50;
  128. if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr25"))
  129. cfg->host_caps |= MMC_MODE_UHS_SDR25;
  130. if (fdtdec_get_bool(fdt, node, "sd-uhs-sdr12"))
  131. cfg->host_caps |= MMC_MODE_UHS_SDR12;
  132. return 0;
  133. }
  134. struct mmc *mmc_get_mmc_dev(struct udevice *dev)
  135. {
  136. struct mmc_uclass_priv *upriv;
  137. if (!device_active(dev))
  138. return NULL;
  139. upriv = dev_get_uclass_priv(dev);
  140. return upriv->mmc;
  141. }
  142. #ifdef CONFIG_BLK
  143. struct mmc *find_mmc_device(int dev_num)
  144. {
  145. struct udevice *dev, *mmc_dev;
  146. int ret;
  147. ret = blk_get_device(IF_TYPE_MMC, dev_num, &dev);
  148. if (ret) {
  149. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  150. printf("MMC Device %d not found\n", dev_num);
  151. #endif
  152. return NULL;
  153. }
  154. mmc_dev = dev_get_parent(dev);
  155. return mmc_get_mmc_dev(mmc_dev);
  156. }
  157. int get_mmc_num(void)
  158. {
  159. return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
  160. }
  161. int mmc_get_next_devnum(void)
  162. {
  163. return blk_find_max_devnum(IF_TYPE_MMC);
  164. }
  165. struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
  166. {
  167. struct blk_desc *desc;
  168. struct udevice *dev;
  169. device_find_first_child(mmc->dev, &dev);
  170. if (!dev)
  171. return NULL;
  172. desc = dev_get_uclass_platdata(dev);
  173. return desc;
  174. }
  175. void mmc_do_preinit(void)
  176. {
  177. struct udevice *dev;
  178. struct uclass *uc;
  179. int ret;
  180. ret = uclass_get(UCLASS_MMC, &uc);
  181. if (ret)
  182. return;
  183. uclass_foreach_dev(dev, uc) {
  184. struct mmc *m = mmc_get_mmc_dev(dev);
  185. if (!m)
  186. continue;
  187. #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
  188. mmc_set_preinit(m, 1);
  189. #endif
  190. if (m->preinit)
  191. mmc_start_init(m);
  192. }
  193. }
  194. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  195. void print_mmc_devices(char separator)
  196. {
  197. struct udevice *dev;
  198. char *mmc_type;
  199. bool first = true;
  200. for (uclass_first_device(UCLASS_MMC, &dev);
  201. dev;
  202. uclass_next_device(&dev), first = false) {
  203. struct mmc *m = mmc_get_mmc_dev(dev);
  204. if (!first) {
  205. printf("%c", separator);
  206. if (separator != '\n')
  207. puts(" ");
  208. }
  209. if (m->has_init)
  210. mmc_type = IS_SD(m) ? "SD" : "eMMC";
  211. else
  212. mmc_type = NULL;
  213. printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
  214. if (mmc_type)
  215. printf(" (%s)", mmc_type);
  216. }
  217. printf("\n");
  218. }
  219. #else
  220. void print_mmc_devices(char separator) { }
  221. #endif
  222. int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
  223. {
  224. struct blk_desc *bdesc;
  225. struct udevice *bdev;
  226. int ret;
  227. ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC, -1, 512,
  228. 0, &bdev);
  229. if (ret) {
  230. debug("Cannot create block device\n");
  231. return ret;
  232. }
  233. bdesc = dev_get_uclass_platdata(bdev);
  234. mmc->cfg = cfg;
  235. mmc->priv = dev;
  236. /* the following chunk was from mmc_register() */
  237. /* Setup dsr related values */
  238. mmc->dsr_imp = 0;
  239. mmc->dsr = 0xffffffff;
  240. /* Setup the universal parts of the block interface just once */
  241. bdesc->removable = 1;
  242. /* setup initial part type */
  243. bdesc->part_type = cfg->part_type;
  244. mmc->dev = dev;
  245. return 0;
  246. }
  247. int mmc_unbind(struct udevice *dev)
  248. {
  249. struct udevice *bdev;
  250. device_find_first_child(dev, &bdev);
  251. if (bdev) {
  252. device_remove(bdev);
  253. device_unbind(bdev);
  254. }
  255. return 0;
  256. }
  257. static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
  258. {
  259. struct udevice *mmc_dev = dev_get_parent(bdev);
  260. struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
  261. struct blk_desc *desc = dev_get_uclass_platdata(bdev);
  262. if (desc->hwpart == hwpart)
  263. return 0;
  264. if (mmc->part_config == MMCPART_NOAVAILABLE)
  265. return -EMEDIUMTYPE;
  266. return mmc_switch_part(mmc, hwpart);
  267. }
  268. static const struct blk_ops mmc_blk_ops = {
  269. .read = mmc_bread,
  270. #ifndef CONFIG_SPL_BUILD
  271. .write = mmc_bwrite,
  272. .erase = mmc_berase,
  273. #endif
  274. .select_hwpart = mmc_select_hwpart,
  275. };
  276. U_BOOT_DRIVER(mmc_blk) = {
  277. .name = "mmc_blk",
  278. .id = UCLASS_BLK,
  279. .ops = &mmc_blk_ops,
  280. };
  281. #endif /* CONFIG_BLK */
  282. U_BOOT_DRIVER(mmc) = {
  283. .name = "mmc",
  284. .id = UCLASS_MMC,
  285. };
  286. UCLASS_DRIVER(mmc) = {
  287. .id = UCLASS_MMC,
  288. .name = "mmc",
  289. .flags = DM_UC_FLAG_SEQ_ALIAS,
  290. .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),
  291. };