mmc-uclass.c 8.0 KB

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