mmc_legacy.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Google, Inc
  4. * Copyright 2020 NXP
  5. * Written by Simon Glass <sjg@chromium.org>
  6. */
  7. #include <common.h>
  8. #include <log.h>
  9. #include <malloc.h>
  10. #include <mmc.h>
  11. #include "mmc_private.h"
  12. static struct list_head mmc_devices;
  13. static int cur_dev_num = -1;
  14. #if CONFIG_IS_ENABLED(MMC_TINY)
  15. static struct mmc mmc_static;
  16. struct mmc *find_mmc_device(int dev_num)
  17. {
  18. return &mmc_static;
  19. }
  20. void mmc_do_preinit(void)
  21. {
  22. struct mmc *m = &mmc_static;
  23. if (m->preinit)
  24. mmc_start_init(m);
  25. }
  26. struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
  27. {
  28. return &mmc->block_dev;
  29. }
  30. #else
  31. struct mmc *find_mmc_device(int dev_num)
  32. {
  33. struct mmc *m;
  34. struct list_head *entry;
  35. list_for_each(entry, &mmc_devices) {
  36. m = list_entry(entry, struct mmc, link);
  37. if (m->block_dev.devnum == dev_num)
  38. return m;
  39. }
  40. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  41. printf("MMC Device %d not found\n", dev_num);
  42. #endif
  43. return NULL;
  44. }
  45. int mmc_get_next_devnum(void)
  46. {
  47. return cur_dev_num++;
  48. }
  49. struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
  50. {
  51. return &mmc->block_dev;
  52. }
  53. int get_mmc_num(void)
  54. {
  55. return cur_dev_num;
  56. }
  57. void mmc_do_preinit(void)
  58. {
  59. struct mmc *m;
  60. struct list_head *entry;
  61. list_for_each(entry, &mmc_devices) {
  62. m = list_entry(entry, struct mmc, link);
  63. if (m->preinit)
  64. mmc_start_init(m);
  65. }
  66. }
  67. #endif
  68. void mmc_list_init(void)
  69. {
  70. INIT_LIST_HEAD(&mmc_devices);
  71. cur_dev_num = 0;
  72. }
  73. void mmc_list_add(struct mmc *mmc)
  74. {
  75. INIT_LIST_HEAD(&mmc->link);
  76. list_add_tail(&mmc->link, &mmc_devices);
  77. }
  78. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  79. void print_mmc_devices(char separator)
  80. {
  81. struct mmc *m;
  82. struct list_head *entry;
  83. char *mmc_type;
  84. list_for_each(entry, &mmc_devices) {
  85. m = list_entry(entry, struct mmc, link);
  86. if (m->has_init)
  87. mmc_type = IS_SD(m) ? "SD" : "eMMC";
  88. else
  89. mmc_type = NULL;
  90. printf("%s: %d", m->cfg->name, m->block_dev.devnum);
  91. if (mmc_type)
  92. printf(" (%s)", mmc_type);
  93. if (entry->next != &mmc_devices) {
  94. printf("%c", separator);
  95. if (separator != '\n')
  96. puts(" ");
  97. }
  98. }
  99. printf("\n");
  100. }
  101. #else
  102. void print_mmc_devices(char separator) { }
  103. #endif
  104. #if CONFIG_IS_ENABLED(MMC_TINY)
  105. static struct mmc mmc_static = {
  106. .dsr_imp = 0,
  107. .dsr = 0xffffffff,
  108. .block_dev = {
  109. .if_type = IF_TYPE_MMC,
  110. .removable = 1,
  111. .devnum = 0,
  112. .block_read = mmc_bread,
  113. .block_write = mmc_bwrite,
  114. .block_erase = mmc_berase,
  115. .part_type = 0,
  116. },
  117. };
  118. struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
  119. {
  120. struct mmc *mmc = &mmc_static;
  121. /* First MMC device registered, fail to register a new one.
  122. * Given users are not expecting this to fail, instead
  123. * of failing let's just return the only MMC device
  124. */
  125. if (mmc->cfg) {
  126. debug("Warning: MMC_TINY doesn't support multiple MMC devices\n");
  127. return mmc;
  128. }
  129. mmc->cfg = cfg;
  130. mmc->priv = priv;
  131. return mmc;
  132. }
  133. void mmc_destroy(struct mmc *mmc)
  134. {
  135. }
  136. #else
  137. struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
  138. {
  139. struct blk_desc *bdesc;
  140. struct mmc *mmc;
  141. /* quick validation */
  142. if (cfg == NULL || cfg->f_min == 0 ||
  143. cfg->f_max == 0 || cfg->b_max == 0)
  144. return NULL;
  145. #if !CONFIG_IS_ENABLED(DM_MMC)
  146. if (cfg->ops == NULL || cfg->ops->send_cmd == NULL)
  147. return NULL;
  148. #endif
  149. mmc = calloc(1, sizeof(*mmc));
  150. if (mmc == NULL)
  151. return NULL;
  152. mmc->cfg = cfg;
  153. mmc->priv = priv;
  154. /* the following chunk was mmc_register() */
  155. /* Setup dsr related values */
  156. mmc->dsr_imp = 0;
  157. mmc->dsr = 0xffffffff;
  158. /* Setup the universal parts of the block interface just once */
  159. bdesc = mmc_get_blk_desc(mmc);
  160. bdesc->if_type = IF_TYPE_MMC;
  161. bdesc->removable = 1;
  162. bdesc->devnum = mmc_get_next_devnum();
  163. bdesc->block_read = mmc_bread;
  164. bdesc->block_write = mmc_bwrite;
  165. bdesc->block_erase = mmc_berase;
  166. /* setup initial part type */
  167. bdesc->part_type = mmc->cfg->part_type;
  168. mmc_list_add(mmc);
  169. return mmc;
  170. }
  171. void mmc_destroy(struct mmc *mmc)
  172. {
  173. /* only freeing memory for now */
  174. free(mmc);
  175. }
  176. #endif
  177. static int mmc_select_hwpartp(struct blk_desc *desc, int hwpart)
  178. {
  179. struct mmc *mmc = find_mmc_device(desc->devnum);
  180. int ret;
  181. if (!mmc)
  182. return -ENODEV;
  183. if (mmc->block_dev.hwpart == hwpart)
  184. return 0;
  185. if (mmc->part_config == MMCPART_NOAVAILABLE)
  186. return -EMEDIUMTYPE;
  187. ret = mmc_switch_part(mmc, hwpart);
  188. if (ret)
  189. return ret;
  190. return 0;
  191. }
  192. static int mmc_get_dev(int dev, struct blk_desc **descp)
  193. {
  194. struct mmc *mmc = find_mmc_device(dev);
  195. int ret;
  196. if (!mmc)
  197. return -ENODEV;
  198. ret = mmc_init(mmc);
  199. if (ret)
  200. return ret;
  201. *descp = &mmc->block_dev;
  202. return 0;
  203. }
  204. U_BOOT_LEGACY_BLK(mmc) = {
  205. .if_typename = "mmc",
  206. .if_type = IF_TYPE_MMC,
  207. .max_devs = -1,
  208. .get_dev = mmc_get_dev,
  209. .select_hwpart = mmc_select_hwpartp,
  210. };