sifive_fu740.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2021 SiFive
  5. * Copyright (c) 2021 YADRO
  6. *
  7. * Authors:
  8. * David Abdurachmanov <david.abdurachmanov@sifive.com>
  9. * Nikita Shubin <n.shubin@yadro.com>
  10. */
  11. #include <platform_override.h>
  12. #include <libfdt.h>
  13. #include <sbi/sbi_error.h>
  14. #include <sbi/sbi_hart.h>
  15. #include <sbi/sbi_system.h>
  16. #include <sbi/sbi_console.h>
  17. #include <sbi_utils/fdt/fdt_helper.h>
  18. #include <sbi_utils/fdt/fdt_fixup.h>
  19. #include <sbi_utils/reset/fdt_reset.h>
  20. #include <sbi_utils/i2c/fdt_i2c.h>
  21. #define DA9063_REG_PAGE_CON 0x00
  22. #define DA9063_REG_CONTROL_A 0x0e
  23. #define DA9063_REG_CONTROL_F 0x13
  24. #define DA9063_REG_DEVICE_ID 0x81
  25. #define DA9063_CONTROL_A_M_POWER1_EN (1 << 6)
  26. #define DA9063_CONTROL_A_M_POWER_EN (1 << 5)
  27. #define DA9063_CONTROL_A_STANDBY (1 << 3)
  28. #define DA9063_CONTROL_F_WAKEUP (1 << 2)
  29. #define DA9063_CONTROL_F_SHUTDOWN (1 << 1)
  30. #define PMIC_CHIP_ID_DA9063 0x61
  31. static struct {
  32. struct i2c_adapter *adapter;
  33. uint32_t reg;
  34. } da9063;
  35. static int da9063_system_reset_check(u32 type, u32 reason)
  36. {
  37. switch (type) {
  38. case SBI_SRST_RESET_TYPE_SHUTDOWN:
  39. return 1;
  40. case SBI_SRST_RESET_TYPE_COLD_REBOOT:
  41. case SBI_SRST_RESET_TYPE_WARM_REBOOT:
  42. return 255;
  43. }
  44. return 0;
  45. }
  46. static inline int da9063_sanity_check(struct i2c_adapter *adap, uint32_t reg)
  47. {
  48. uint8_t val;
  49. int rc = i2c_adapter_reg_write(adap, reg, DA9063_REG_PAGE_CON, 0x02);
  50. if (rc)
  51. return rc;
  52. /* check set page*/
  53. rc = i2c_adapter_reg_read(adap, reg, 0x0, &val);
  54. if (rc)
  55. return rc;
  56. if (val != 0x02)
  57. return SBI_ENODEV;
  58. /* read and check device id */
  59. rc = i2c_adapter_reg_read(adap, reg, DA9063_REG_DEVICE_ID, &val);
  60. if (rc)
  61. return rc;
  62. if (val != PMIC_CHIP_ID_DA9063)
  63. return SBI_ENODEV;
  64. return 0;
  65. }
  66. static inline int da9063_shutdown(struct i2c_adapter *adap, uint32_t reg)
  67. {
  68. int rc = i2c_adapter_reg_write(adap, da9063.reg,
  69. DA9063_REG_PAGE_CON, 0x00);
  70. if (rc)
  71. return rc;
  72. return i2c_adapter_reg_write(adap, da9063.reg,
  73. DA9063_REG_CONTROL_F,
  74. DA9063_CONTROL_F_SHUTDOWN);
  75. }
  76. static inline int da9063_reset(struct i2c_adapter *adap, uint32_t reg)
  77. {
  78. int rc = i2c_adapter_reg_write(adap, da9063.reg,
  79. DA9063_REG_PAGE_CON, 0x00);
  80. if (rc)
  81. return rc;
  82. rc = i2c_adapter_reg_write(adap, da9063.reg,
  83. DA9063_REG_CONTROL_F,
  84. DA9063_CONTROL_F_WAKEUP);
  85. if (rc)
  86. return rc;
  87. return i2c_adapter_reg_write(adap, da9063.reg,
  88. DA9063_REG_CONTROL_A,
  89. DA9063_CONTROL_A_M_POWER1_EN |
  90. DA9063_CONTROL_A_M_POWER_EN |
  91. DA9063_CONTROL_A_STANDBY);
  92. }
  93. static void da9063_system_reset(u32 type, u32 reason)
  94. {
  95. struct i2c_adapter *adap = da9063.adapter;
  96. uint32_t reg = da9063.reg;
  97. int rc;
  98. if (adap) {
  99. /* sanity check */
  100. rc = da9063_sanity_check(adap, reg);
  101. if (rc) {
  102. sbi_printf("%s: chip is not da9063 PMIC\n", __func__);
  103. goto skip_reset;
  104. }
  105. switch (type) {
  106. case SBI_SRST_RESET_TYPE_SHUTDOWN:
  107. da9063_shutdown(adap, reg);
  108. break;
  109. case SBI_SRST_RESET_TYPE_COLD_REBOOT:
  110. case SBI_SRST_RESET_TYPE_WARM_REBOOT:
  111. da9063_reset(adap, reg);
  112. break;
  113. }
  114. }
  115. skip_reset:
  116. sbi_hart_hang();
  117. }
  118. static struct sbi_system_reset_device da9063_reset_i2c = {
  119. .name = "da9063-reset",
  120. .system_reset_check = da9063_system_reset_check,
  121. .system_reset = da9063_system_reset
  122. };
  123. static int da9063_reset_init(void *fdt, int nodeoff,
  124. const struct fdt_match *match)
  125. {
  126. int rc, i2c_bus;
  127. struct i2c_adapter *adapter;
  128. uint64_t addr;
  129. /* we are dlg,da9063 node */
  130. rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
  131. if (rc)
  132. return rc;
  133. da9063.reg = addr;
  134. /* find i2c bus parent node */
  135. i2c_bus = fdt_parent_offset(fdt, nodeoff);
  136. if (i2c_bus < 0)
  137. return i2c_bus;
  138. /* i2c adapter get */
  139. rc = fdt_i2c_adapter_get(fdt, i2c_bus, &adapter);
  140. if (rc)
  141. return rc;
  142. da9063.adapter = adapter;
  143. sbi_system_reset_add_device(&da9063_reset_i2c);
  144. return 0;
  145. }
  146. static const struct fdt_match da9063_reset_match[] = {
  147. { .compatible = "dlg,da9063", .data = (void *)TRUE },
  148. { },
  149. };
  150. struct fdt_reset fdt_reset_da9063 = {
  151. .match_table = da9063_reset_match,
  152. .init = da9063_reset_init,
  153. };
  154. static u64 sifive_fu740_tlbr_flush_limit(const struct fdt_match *match)
  155. {
  156. /*
  157. * Needed to address CIP-1200 errata on SiFive FU740
  158. * Title: Instruction TLB can fail to respect a non-global SFENCE
  159. * Workaround: Flush the TLB using SFENCE.VMA x0, x0
  160. * See Errata_FU740-C000_20210205 from
  161. * https://www.sifive.com/boards/hifive-unmatched
  162. */
  163. return 0;
  164. }
  165. static int sifive_fu740_final_init(bool cold_boot,
  166. const struct fdt_match *match)
  167. {
  168. int rc;
  169. void *fdt = fdt_get_address();
  170. if (cold_boot) {
  171. rc = fdt_reset_driver_init(fdt, &fdt_reset_da9063);
  172. if (rc)
  173. sbi_printf("%s: failed to find da9063 for reset\n",
  174. __func__);
  175. }
  176. return 0;
  177. }
  178. static const struct fdt_match sifive_fu740_match[] = {
  179. { .compatible = "sifive,fu740" },
  180. { .compatible = "sifive,fu740-c000" },
  181. { .compatible = "sifive,hifive-unmatched-a00" },
  182. { },
  183. };
  184. const struct platform_override sifive_fu740 = {
  185. .match_table = sifive_fu740_match,
  186. .tlbr_flush_limit = sifive_fu740_tlbr_flush_limit,
  187. .final_init = sifive_fu740_final_init,
  188. };