sifive_fu740.c 5.0 KB

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