mmc_legacy.c 3.6 KB

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