mmc-uclass.c 8.1 KB

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