watchdog-i6300esb.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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-11-26 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #define DBG_TAG "wdt.i6300esb"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #define I6300ESB_REG_BAR 0
  16. /* PCI configuration registers */
  17. #define ESB_CONFIG_PCI_REG 0x60 /* Config register */
  18. #define ESB_LOCK_PCI_REG 0x68 /* WDT lock register */
  19. /* Memory mapped registers */
  20. #define ESB_TIMER1_REG 0x00 /* Timer1 value after each reset */
  21. #define ESB_TIMER2_REG 0x04 /* Timer2 value after each reset */
  22. #define ESB_GINTSR_REG 0x08 /* General Interrupt Status Reg */
  23. #define ESB_RELOAD_REG 0x0c /* Reload register */
  24. /* Lock register bits */
  25. #define ESB_WDT_FUNC (0x01 << 2) /* Watchdog functionality */
  26. #define ESB_WDT_ENABLE (0x01 << 1) /* Enable WDT */
  27. #define ESB_WDT_LOCK (0x01 << 0) /* Lock (nowayout) */
  28. /* Config register bits */
  29. #define ESB_WDT_REBOOT (0x01 << 5) /* Enable reboot on timeout */
  30. #define ESB_WDT_FREQ (0x01 << 2) /* Decrement frequency */
  31. #define ESB_WDT_INTTYPE (0x03 << 0) /* Interrupt type on timer1 timeout */
  32. /* Reload register bits */
  33. #define ESB_WDT_TIMEOUT (0x01 << 9) /* Watchdog timed out */
  34. #define ESB_WDT_RELOAD (0x01 << 8) /* prevent timeout */
  35. /* Magic constants */
  36. #define ESB_UNLOCK1 0x80 /* Step 1 to unlock reset registers */
  37. #define ESB_UNLOCK2 0x86 /* Step 2 to unlock reset registers */
  38. /* 30 sec default heartbeat (1 < heartbeat < 2*1023) */
  39. #define ESB_HEARTBEAT_MIN 1
  40. #define ESB_HEARTBEAT_MAX 2046
  41. #define ESB_HEARTBEAT_DEFAULT 30
  42. struct i6300esb_wdt
  43. {
  44. rt_watchdog_t parent;
  45. void *regs;
  46. rt_uint32_t timeout;
  47. struct rt_pci_device *pdev;
  48. };
  49. #define raw_to_i6300esb_wdt(raw) rt_container_of(raw, struct i6300esb_wdt, parent)
  50. /*
  51. * Prepare for reloading the timer by unlocking the proper registers.
  52. * This is performed by first writing 0x80 followed by 0x86 to the
  53. * reload register. After this the appropriate registers can be written
  54. * to once before they need to be unlocked again.
  55. */
  56. rt_inline void i6300esb_wdt_unlock_registers(struct i6300esb_wdt *esb)
  57. {
  58. HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_UNLOCK1;
  59. HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_UNLOCK2;
  60. }
  61. static rt_uint32_t i6300esb_timer_start(struct i6300esb_wdt *esb)
  62. {
  63. i6300esb_wdt_unlock_registers(esb);
  64. HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_WDT_RELOAD;
  65. rt_pci_write_config_u8(esb->pdev, ESB_LOCK_PCI_REG, ESB_WDT_ENABLE);
  66. return RT_EOK;
  67. }
  68. static rt_uint32_t i6300esb_timer_stop(struct i6300esb_wdt *esb)
  69. {
  70. rt_uint8_t val;
  71. /* First, reset timers as suggested by the docs */
  72. i6300esb_wdt_unlock_registers(esb);
  73. HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_WDT_RELOAD;
  74. /* Then disable the WDT */
  75. rt_pci_write_config_u8(esb->pdev, ESB_LOCK_PCI_REG, 0x0);
  76. rt_pci_read_config_u8(esb->pdev, ESB_LOCK_PCI_REG, &val);
  77. /* Returns 0 if the timer was disabled, non-zero otherwise */
  78. return val & ESB_WDT_ENABLE;
  79. }
  80. static rt_err_t esb_timer_keepalive(struct i6300esb_wdt *esb)
  81. {
  82. i6300esb_wdt_unlock_registers(esb);
  83. HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_WDT_RELOAD;
  84. return RT_EOK;
  85. }
  86. static rt_err_t i6300esb_timer_set_heartbeat(struct i6300esb_wdt *esb, rt_uint32_t time)
  87. {
  88. rt_uint32_t val;
  89. /*
  90. * We shift by 9, so if we are passed a value of 1 sec,
  91. * val will be 1 << 9 = 512, then write that to two
  92. * timers => 2 * 512 = 1024 (which is decremented at 1KHz)
  93. */
  94. val = time << 9;
  95. /* Write timer 1 */
  96. i6300esb_wdt_unlock_registers(esb);
  97. HWREG32(esb->regs + ESB_TIMER1_REG) = val;
  98. /* Write timer 2 */
  99. i6300esb_wdt_unlock_registers(esb);
  100. HWREG32(esb->regs + ESB_TIMER2_REG) = val;
  101. /* Reload */
  102. i6300esb_wdt_unlock_registers(esb);
  103. HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_WDT_RELOAD;
  104. esb->timeout = time;
  105. return RT_EOK;
  106. }
  107. static rt_err_t i6300esb_wdt_init(rt_watchdog_t *wdt)
  108. {
  109. return RT_EOK;
  110. }
  111. static rt_err_t i6300esb_wdt_control(rt_watchdog_t *wdt, int cmd, void *args)
  112. {
  113. rt_err_t err = RT_EOK;
  114. struct i6300esb_wdt *esb = raw_to_i6300esb_wdt(wdt);
  115. switch (cmd)
  116. {
  117. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  118. *(rt_uint32_t *)args = esb->timeout;
  119. break;
  120. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  121. err = i6300esb_timer_set_heartbeat(esb, *(rt_uint32_t *)args);
  122. break;
  123. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  124. err = esb_timer_keepalive(esb);
  125. break;
  126. case RT_DEVICE_CTRL_WDT_START:
  127. err = i6300esb_timer_start(esb);
  128. break;
  129. case RT_DEVICE_CTRL_WDT_STOP:
  130. err = i6300esb_timer_stop(esb);
  131. break;
  132. default:
  133. err = -RT_EINVAL;
  134. }
  135. return err;
  136. }
  137. static const struct rt_watchdog_ops i6300esb_wdt_ops =
  138. {
  139. .init = i6300esb_wdt_init,
  140. .control = i6300esb_wdt_control,
  141. };
  142. static rt_err_t i6300esb_wdt_probe(struct rt_pci_device *pdev)
  143. {
  144. rt_err_t err;
  145. rt_uint8_t val1;
  146. rt_uint16_t val2;
  147. const char *dev_name;
  148. struct i6300esb_wdt *esb = rt_calloc(1, sizeof(*esb));
  149. if (!esb)
  150. {
  151. return -RT_ENOMEM;
  152. }
  153. esb->regs = rt_pci_iomap(pdev, I6300ESB_REG_BAR);
  154. if (!esb->regs)
  155. {
  156. err = -RT_EIO;
  157. goto _fail;
  158. }
  159. /*
  160. * Config register:
  161. * Bit 5 : 0 = Enable WDT_OUTPUT
  162. * Bit 2 : 0 = set the timer frequency to the PCI clock
  163. * divided by 2^15 (approx 1KHz).
  164. * Bits 1:0 : 11 = WDT_INT_TYPE Disabled.
  165. * The watchdog has two timers, it can be setup so that the expiry of timer1
  166. * results in an interrupt and the expiry of timer2 results in a reboot.
  167. * We set it to not generate any interrupts as there is not much
  168. * we can do with it right now.
  169. */
  170. rt_pci_write_config_u16(pdev, ESB_CONFIG_PCI_REG, 0x0003);
  171. /* Check that the WDT isn't already locked */
  172. rt_pci_read_config_u8(pdev, ESB_LOCK_PCI_REG, &val1);
  173. if (val1 & ESB_WDT_LOCK)
  174. {
  175. LOG_W("Nowayout already set");
  176. }
  177. /* Set the timer to watchdog mode and disable it for now */
  178. rt_pci_write_config_u8(pdev, ESB_LOCK_PCI_REG, 0x00);
  179. /* Check if the watchdog was previously triggered */
  180. i6300esb_wdt_unlock_registers(esb);
  181. val2 = HWREG16(esb->regs + ESB_RELOAD_REG);
  182. if (val2 & ESB_WDT_TIMEOUT)
  183. {
  184. LOG_D("Card previously reset the CPU");
  185. }
  186. /* Reset WDT_TIMEOUT flag and timers */
  187. i6300esb_wdt_unlock_registers(esb);
  188. HWREG16(esb->regs + ESB_RELOAD_REG) = ESB_WDT_TIMEOUT | ESB_WDT_RELOAD;
  189. /* And set the correct timeout value */
  190. i6300esb_timer_set_heartbeat(esb, ESB_HEARTBEAT_DEFAULT);
  191. pdev->parent.user_data = esb;
  192. esb->pdev = pdev;
  193. esb->parent.ops = &i6300esb_wdt_ops;
  194. rt_dm_dev_set_name_auto(&esb->parent.parent, "wdt");
  195. dev_name = rt_dm_dev_get_name(&esb->parent.parent);
  196. rt_hw_watchdog_register(&esb->parent, dev_name, 0, esb);
  197. return RT_EOK;
  198. _fail:
  199. if (esb->regs)
  200. {
  201. rt_iounmap(esb->regs);
  202. }
  203. rt_free(esb);
  204. return err;
  205. }
  206. static rt_err_t i6300esb_wdt_remove(struct rt_pci_device *pdev)
  207. {
  208. struct i6300esb_wdt *esb = pdev->parent.user_data;
  209. i6300esb_timer_stop(esb);
  210. rt_device_unregister(&esb->parent.parent);
  211. rt_iounmap(esb->regs);
  212. rt_free(esb);
  213. return RT_EOK;
  214. }
  215. static const struct rt_pci_device_id i6300esb_wdt_pci_ids[] =
  216. {
  217. { RT_PCI_DEVICE_ID(PCI_VENDOR_ID_INTEL, 0x25ab), },
  218. { /* sentinel */ }
  219. };
  220. static struct rt_pci_driver i6300esb_wdt_driver =
  221. {
  222. .name = "i6300esb-wdt",
  223. .ids = i6300esb_wdt_pci_ids,
  224. .probe = i6300esb_wdt_probe,
  225. .remove = i6300esb_wdt_remove,
  226. };
  227. RT_PCI_DRIVER_EXPORT(i6300esb_wdt_driver);