mmc-uclass.c 8.0 KB

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