allwinner-wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Allwinner Watchdog emulation
  3. *
  4. * Copyright (C) 2023 Strahinja Jankovic <strahinja.p.jankovic@gmail.com>
  5. *
  6. * This file is derived from Allwinner RTC,
  7. * by Niek Linnenbank.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "qemu/log.h"
  24. #include "qemu/units.h"
  25. #include "qemu/module.h"
  26. #include "trace.h"
  27. #include "hw/sysbus.h"
  28. #include "hw/registerfields.h"
  29. #include "hw/watchdog/allwinner-wdt.h"
  30. #include "system/watchdog.h"
  31. #include "migration/vmstate.h"
  32. /* WDT registers */
  33. enum {
  34. REG_IRQ_EN = 0, /* Watchdog interrupt enable */
  35. REG_IRQ_STA, /* Watchdog interrupt status */
  36. REG_CTRL, /* Watchdog control register */
  37. REG_CFG, /* Watchdog configuration register */
  38. REG_MODE, /* Watchdog mode register */
  39. };
  40. /* Universal WDT register flags */
  41. #define WDT_RESTART_MASK (1 << 0)
  42. #define WDT_EN_MASK (1 << 0)
  43. /* sun4i specific WDT register flags */
  44. #define RST_EN_SUN4I_MASK (1 << 1)
  45. #define INTV_VALUE_SUN4I_SHIFT (3)
  46. #define INTV_VALUE_SUN4I_MASK (0xfu << INTV_VALUE_SUN4I_SHIFT)
  47. /* sun6i specific WDT register flags */
  48. #define RST_EN_SUN6I_MASK (1 << 0)
  49. #define KEY_FIELD_SUN6I_SHIFT (1)
  50. #define KEY_FIELD_SUN6I_MASK (0xfffu << KEY_FIELD_SUN6I_SHIFT)
  51. #define KEY_FIELD_SUN6I (0xA57u)
  52. #define INTV_VALUE_SUN6I_SHIFT (4)
  53. #define INTV_VALUE_SUN6I_MASK (0xfu << INTV_VALUE_SUN6I_SHIFT)
  54. /* Map of INTV_VALUE to 0.5s units. */
  55. static const uint8_t allwinner_wdt_count_map[] = {
  56. 1,
  57. 2,
  58. 4,
  59. 6,
  60. 8,
  61. 10,
  62. 12,
  63. 16,
  64. 20,
  65. 24,
  66. 28,
  67. 32
  68. };
  69. /* WDT sun4i register map (offset to name) */
  70. const uint8_t allwinner_wdt_sun4i_regmap[] = {
  71. [0x0000] = REG_CTRL,
  72. [0x0004] = REG_MODE,
  73. };
  74. /* WDT sun6i register map (offset to name) */
  75. const uint8_t allwinner_wdt_sun6i_regmap[] = {
  76. [0x0000] = REG_IRQ_EN,
  77. [0x0004] = REG_IRQ_STA,
  78. [0x0010] = REG_CTRL,
  79. [0x0014] = REG_CFG,
  80. [0x0018] = REG_MODE,
  81. };
  82. static bool allwinner_wdt_sun4i_read(AwWdtState *s, uint32_t offset)
  83. {
  84. /* no sun4i specific registers currently implemented */
  85. return false;
  86. }
  87. static bool allwinner_wdt_sun4i_write(AwWdtState *s, uint32_t offset,
  88. uint32_t data)
  89. {
  90. /* no sun4i specific registers currently implemented */
  91. return false;
  92. }
  93. static bool allwinner_wdt_sun4i_can_reset_system(AwWdtState *s)
  94. {
  95. if (s->regs[REG_MODE] & RST_EN_SUN4I_MASK) {
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. }
  101. static bool allwinner_wdt_sun4i_is_key_valid(AwWdtState *s, uint32_t val)
  102. {
  103. /* sun4i has no key */
  104. return true;
  105. }
  106. static uint8_t allwinner_wdt_sun4i_get_intv_value(AwWdtState *s)
  107. {
  108. return ((s->regs[REG_MODE] & INTV_VALUE_SUN4I_MASK) >>
  109. INTV_VALUE_SUN4I_SHIFT);
  110. }
  111. static bool allwinner_wdt_sun6i_read(AwWdtState *s, uint32_t offset)
  112. {
  113. const AwWdtClass *c = AW_WDT_GET_CLASS(s);
  114. switch (c->regmap[offset]) {
  115. case REG_IRQ_EN:
  116. case REG_IRQ_STA:
  117. case REG_CFG:
  118. return true;
  119. default:
  120. break;
  121. }
  122. return false;
  123. }
  124. static bool allwinner_wdt_sun6i_write(AwWdtState *s, uint32_t offset,
  125. uint32_t data)
  126. {
  127. const AwWdtClass *c = AW_WDT_GET_CLASS(s);
  128. switch (c->regmap[offset]) {
  129. case REG_IRQ_EN:
  130. case REG_IRQ_STA:
  131. case REG_CFG:
  132. return true;
  133. default:
  134. break;
  135. }
  136. return false;
  137. }
  138. static bool allwinner_wdt_sun6i_can_reset_system(AwWdtState *s)
  139. {
  140. if (s->regs[REG_CFG] & RST_EN_SUN6I_MASK) {
  141. return true;
  142. } else {
  143. return false;
  144. }
  145. }
  146. static bool allwinner_wdt_sun6i_is_key_valid(AwWdtState *s, uint32_t val)
  147. {
  148. uint16_t key = (val & KEY_FIELD_SUN6I_MASK) >> KEY_FIELD_SUN6I_SHIFT;
  149. return (key == KEY_FIELD_SUN6I);
  150. }
  151. static uint8_t allwinner_wdt_sun6i_get_intv_value(AwWdtState *s)
  152. {
  153. return ((s->regs[REG_MODE] & INTV_VALUE_SUN6I_MASK) >>
  154. INTV_VALUE_SUN6I_SHIFT);
  155. }
  156. static void allwinner_wdt_update_timer(AwWdtState *s)
  157. {
  158. const AwWdtClass *c = AW_WDT_GET_CLASS(s);
  159. uint8_t count = c->get_intv_value(s);
  160. ptimer_transaction_begin(s->timer);
  161. ptimer_stop(s->timer);
  162. /* Use map to convert. */
  163. if (count < sizeof(allwinner_wdt_count_map)) {
  164. ptimer_set_count(s->timer, allwinner_wdt_count_map[count]);
  165. } else {
  166. qemu_log_mask(LOG_GUEST_ERROR, "%s: incorrect INTV_VALUE 0x%02x\n",
  167. __func__, count);
  168. }
  169. ptimer_run(s->timer, 1);
  170. ptimer_transaction_commit(s->timer);
  171. trace_allwinner_wdt_update_timer(count);
  172. }
  173. static uint64_t allwinner_wdt_read(void *opaque, hwaddr offset,
  174. unsigned size)
  175. {
  176. AwWdtState *s = AW_WDT(opaque);
  177. const AwWdtClass *c = AW_WDT_GET_CLASS(s);
  178. uint64_t r;
  179. if (offset >= c->regmap_size) {
  180. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  181. __func__, (uint32_t)offset);
  182. return 0;
  183. }
  184. switch (c->regmap[offset]) {
  185. case REG_CTRL:
  186. case REG_MODE:
  187. r = s->regs[c->regmap[offset]];
  188. break;
  189. default:
  190. if (!c->read(s, offset)) {
  191. qemu_log_mask(LOG_UNIMP, "%s: unimplemented register 0x%04x\n",
  192. __func__, (uint32_t)offset);
  193. return 0;
  194. }
  195. r = s->regs[c->regmap[offset]];
  196. break;
  197. }
  198. trace_allwinner_wdt_read(offset, r, size);
  199. return r;
  200. }
  201. static void allwinner_wdt_write(void *opaque, hwaddr offset,
  202. uint64_t val, unsigned size)
  203. {
  204. AwWdtState *s = AW_WDT(opaque);
  205. const AwWdtClass *c = AW_WDT_GET_CLASS(s);
  206. uint32_t old_val;
  207. if (offset >= c->regmap_size) {
  208. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  209. __func__, (uint32_t)offset);
  210. return;
  211. }
  212. trace_allwinner_wdt_write(offset, val, size);
  213. switch (c->regmap[offset]) {
  214. case REG_CTRL:
  215. if (c->is_key_valid(s, val)) {
  216. if (val & WDT_RESTART_MASK) {
  217. /* Kick timer */
  218. allwinner_wdt_update_timer(s);
  219. }
  220. }
  221. break;
  222. case REG_MODE:
  223. old_val = s->regs[REG_MODE];
  224. s->regs[REG_MODE] = (uint32_t)val;
  225. /* Check for rising edge on WDOG_MODE_EN */
  226. if ((s->regs[REG_MODE] & ~old_val) & WDT_EN_MASK) {
  227. allwinner_wdt_update_timer(s);
  228. }
  229. break;
  230. default:
  231. if (!c->write(s, offset, val)) {
  232. qemu_log_mask(LOG_UNIMP, "%s: unimplemented register 0x%04x\n",
  233. __func__, (uint32_t)offset);
  234. }
  235. s->regs[c->regmap[offset]] = (uint32_t)val;
  236. break;
  237. }
  238. }
  239. static const MemoryRegionOps allwinner_wdt_ops = {
  240. .read = allwinner_wdt_read,
  241. .write = allwinner_wdt_write,
  242. .endianness = DEVICE_LITTLE_ENDIAN,
  243. .valid = {
  244. .min_access_size = 4,
  245. .max_access_size = 4,
  246. },
  247. .impl.min_access_size = 4,
  248. };
  249. static void allwinner_wdt_expired(void *opaque)
  250. {
  251. AwWdtState *s = AW_WDT(opaque);
  252. const AwWdtClass *c = AW_WDT_GET_CLASS(s);
  253. bool enabled = s->regs[REG_MODE] & WDT_EN_MASK;
  254. bool reset_enabled = c->can_reset_system(s);
  255. trace_allwinner_wdt_expired(enabled, reset_enabled);
  256. /* Perform watchdog action if watchdog is enabled and can trigger reset */
  257. if (enabled && reset_enabled) {
  258. watchdog_perform_action();
  259. }
  260. }
  261. static void allwinner_wdt_reset_enter(Object *obj, ResetType type)
  262. {
  263. AwWdtState *s = AW_WDT(obj);
  264. trace_allwinner_wdt_reset_enter();
  265. /* Clear registers */
  266. memset(s->regs, 0, sizeof(s->regs));
  267. }
  268. static const VMStateDescription allwinner_wdt_vmstate = {
  269. .name = "allwinner-wdt",
  270. .version_id = 1,
  271. .minimum_version_id = 1,
  272. .fields = (const VMStateField[]) {
  273. VMSTATE_PTIMER(timer, AwWdtState),
  274. VMSTATE_UINT32_ARRAY(regs, AwWdtState, AW_WDT_REGS_NUM),
  275. VMSTATE_END_OF_LIST()
  276. }
  277. };
  278. static void allwinner_wdt_init(Object *obj)
  279. {
  280. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  281. AwWdtState *s = AW_WDT(obj);
  282. const AwWdtClass *c = AW_WDT_GET_CLASS(s);
  283. /* Memory mapping */
  284. memory_region_init_io(&s->iomem, OBJECT(s), &allwinner_wdt_ops, s,
  285. TYPE_AW_WDT, c->regmap_size * 4);
  286. sysbus_init_mmio(sbd, &s->iomem);
  287. }
  288. static void allwinner_wdt_realize(DeviceState *dev, Error **errp)
  289. {
  290. AwWdtState *s = AW_WDT(dev);
  291. s->timer = ptimer_init(allwinner_wdt_expired, s,
  292. PTIMER_POLICY_NO_IMMEDIATE_TRIGGER |
  293. PTIMER_POLICY_NO_IMMEDIATE_RELOAD |
  294. PTIMER_POLICY_NO_COUNTER_ROUND_DOWN);
  295. ptimer_transaction_begin(s->timer);
  296. /* Set to 2Hz (0.5s period); other periods are multiples of 0.5s. */
  297. ptimer_set_freq(s->timer, 2);
  298. ptimer_set_limit(s->timer, 0xff, 1);
  299. ptimer_transaction_commit(s->timer);
  300. }
  301. static void allwinner_wdt_class_init(ObjectClass *klass, void *data)
  302. {
  303. DeviceClass *dc = DEVICE_CLASS(klass);
  304. ResettableClass *rc = RESETTABLE_CLASS(klass);
  305. rc->phases.enter = allwinner_wdt_reset_enter;
  306. dc->realize = allwinner_wdt_realize;
  307. dc->vmsd = &allwinner_wdt_vmstate;
  308. }
  309. static void allwinner_wdt_sun4i_class_init(ObjectClass *klass, void *data)
  310. {
  311. AwWdtClass *awc = AW_WDT_CLASS(klass);
  312. awc->regmap = allwinner_wdt_sun4i_regmap;
  313. awc->regmap_size = sizeof(allwinner_wdt_sun4i_regmap);
  314. awc->read = allwinner_wdt_sun4i_read;
  315. awc->write = allwinner_wdt_sun4i_write;
  316. awc->can_reset_system = allwinner_wdt_sun4i_can_reset_system;
  317. awc->is_key_valid = allwinner_wdt_sun4i_is_key_valid;
  318. awc->get_intv_value = allwinner_wdt_sun4i_get_intv_value;
  319. }
  320. static void allwinner_wdt_sun6i_class_init(ObjectClass *klass, void *data)
  321. {
  322. AwWdtClass *awc = AW_WDT_CLASS(klass);
  323. awc->regmap = allwinner_wdt_sun6i_regmap;
  324. awc->regmap_size = sizeof(allwinner_wdt_sun6i_regmap);
  325. awc->read = allwinner_wdt_sun6i_read;
  326. awc->write = allwinner_wdt_sun6i_write;
  327. awc->can_reset_system = allwinner_wdt_sun6i_can_reset_system;
  328. awc->is_key_valid = allwinner_wdt_sun6i_is_key_valid;
  329. awc->get_intv_value = allwinner_wdt_sun6i_get_intv_value;
  330. }
  331. static const TypeInfo allwinner_wdt_info = {
  332. .name = TYPE_AW_WDT,
  333. .parent = TYPE_SYS_BUS_DEVICE,
  334. .instance_init = allwinner_wdt_init,
  335. .instance_size = sizeof(AwWdtState),
  336. .class_init = allwinner_wdt_class_init,
  337. .class_size = sizeof(AwWdtClass),
  338. .abstract = true,
  339. };
  340. static const TypeInfo allwinner_wdt_sun4i_info = {
  341. .name = TYPE_AW_WDT_SUN4I,
  342. .parent = TYPE_AW_WDT,
  343. .class_init = allwinner_wdt_sun4i_class_init,
  344. };
  345. static const TypeInfo allwinner_wdt_sun6i_info = {
  346. .name = TYPE_AW_WDT_SUN6I,
  347. .parent = TYPE_AW_WDT,
  348. .class_init = allwinner_wdt_sun6i_class_init,
  349. };
  350. static void allwinner_wdt_register(void)
  351. {
  352. type_register_static(&allwinner_wdt_info);
  353. type_register_static(&allwinner_wdt_sun4i_info);
  354. type_register_static(&allwinner_wdt_sun6i_info);
  355. }
  356. type_init(allwinner_wdt_register)