dev_sdio_dm.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-12-06 GuEe-GUI first version
  9. */
  10. #include <rtatomic.h>
  11. #include "dev_sdio_dm.h"
  12. #define DBG_TAG "SDIO"
  13. #ifdef RT_SDIO_DEBUG
  14. #define DBG_LVL DBG_LOG
  15. #else
  16. #define DBG_LVL DBG_INFO
  17. #endif /* RT_SDIO_DEBUG */
  18. #include <rtdbg.h>
  19. int sdio_host_set_name(struct rt_mmcsd_host *host, char *out_devname)
  20. {
  21. int id = -1, res;
  22. static int uid_min = -1;
  23. static volatile rt_atomic_t uid = 0;
  24. RT_ASSERT(host != RT_NULL);
  25. #ifdef RT_USING_OFW
  26. if (host->ofw_node)
  27. {
  28. id = rt_ofw_get_alias_id(host->ofw_node, "mmc");
  29. if (uid_min < 0)
  30. {
  31. uid_min = rt_ofw_get_alias_last_id("mmc");
  32. uid_min = uid_min < 0 ? 0 : (uid_min + 1);
  33. rt_atomic_store(&uid, uid_min);
  34. }
  35. }
  36. #endif /* RT_USING_OFW */
  37. if (id < 0)
  38. {
  39. id = (int)rt_atomic_add(&uid, 1);
  40. }
  41. res = rt_snprintf(host->name, RT_NAME_MAX, "sd%u", id);
  42. if (out_devname)
  43. {
  44. rt_strncpy(out_devname, host->name, RT_NAME_MAX);
  45. }
  46. return res;
  47. }
  48. #ifdef RT_USING_OFW
  49. rt_err_t sdio_ofw_parse(struct rt_ofw_node *dev_np, struct rt_mmcsd_host *host)
  50. {
  51. rt_uint32_t bus_width = 1;
  52. if (!dev_np)
  53. {
  54. return -RT_EINVAL;
  55. }
  56. host->ofw_node = host->ofw_node ? : dev_np;
  57. host->flags = MMCSD_MUTBLKWRITE;
  58. rt_ofw_prop_read_u32(dev_np, "bus-width", &bus_width);
  59. switch (bus_width)
  60. {
  61. case 0x8:
  62. host->flags |= MMCSD_BUSWIDTH_8;
  63. break;
  64. case 0x4:
  65. host->flags |= MMCSD_BUSWIDTH_4;
  66. break;
  67. case 0x1:
  68. break;
  69. default:
  70. LOG_E("Invalid \"bus-width\" value %d", bus_width);
  71. return -RT_EIO;
  72. }
  73. rt_ofw_prop_read_u32(dev_np, "max-frequency", &host->freq_max);
  74. if (rt_ofw_prop_read_bool(dev_np, "non-removable"))
  75. {
  76. host->flags |= MMCSD_SUP_NONREMOVABLE;
  77. }
  78. if (rt_ofw_prop_read_bool(dev_np, "cap-sdio-irq"))
  79. {
  80. host->flags |= MMCSD_SUP_SDIO_IRQ;
  81. }
  82. if (rt_ofw_prop_read_bool(dev_np, "cap-sd-highspeed") ||
  83. rt_ofw_prop_read_bool(dev_np, "cap-mmc-highspeed"))
  84. {
  85. host->flags |= MMCSD_SUP_HIGHSPEED;
  86. }
  87. if (rt_ofw_prop_read_bool(dev_np, "mmc-ddr-3_3v"))
  88. {
  89. host->flags |= MMCSD_SUP_DDR_3V3;
  90. }
  91. if (rt_ofw_prop_read_bool(dev_np, "mmc-ddr-1_8v"))
  92. {
  93. host->flags |= MMCSD_SUP_DDR_1V8;
  94. }
  95. if (rt_ofw_prop_read_bool(dev_np, "mmc-ddr-1_2v"))
  96. {
  97. host->flags |= MMCSD_SUP_DDR_1V2;
  98. }
  99. if (rt_ofw_prop_read_bool(dev_np, "mmc-hs200-1_2v"))
  100. {
  101. host->flags |= MMCSD_SUP_HS200_1V2;
  102. }
  103. if (rt_ofw_prop_read_bool(dev_np, "mmc-hs200-1_8v"))
  104. {
  105. host->flags |= MMCSD_SUP_HS200_1V8;
  106. }
  107. if (rt_ofw_prop_read_bool(dev_np, "mmc-hs400-1_8v"))
  108. {
  109. host->flags |= MMCSD_SUP_HS400_1V8;
  110. }
  111. if (rt_ofw_prop_read_bool(dev_np, "mmc-hs400-1_2v"))
  112. {
  113. host->flags |= MMCSD_SUP_HS400_1V2;
  114. }
  115. if (rt_ofw_prop_read_bool(dev_np, "sd-uhs-sdr50"))
  116. {
  117. host->flags |= MMCSD_SUP_SDR50;
  118. }
  119. if (rt_ofw_prop_read_bool(dev_np, "sd-uhs-sdr104"))
  120. {
  121. host->flags |= MMCSD_SUP_SDR104;
  122. }
  123. if (rt_ofw_prop_read_bool(dev_np, "sd-uhs-ddr50"))
  124. {
  125. host->flags |= MMCSD_SUP_DDR50;
  126. }
  127. return RT_EOK;
  128. }
  129. #endif /* RT_USING_OFW */