fu740.c 5.5 KB

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