mmc_legacy.c 4.1 KB

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