mmc_legacy.c 4.5 KB

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