cmsdk-apb-watchdog.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * ARM CMSDK APB watchdog emulation
  3. *
  4. * Copyright (c) 2018 Linaro Limited
  5. * Written by Peter Maydell
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 or
  9. * (at your option) any later version.
  10. */
  11. /*
  12. * This is a model of the "APB watchdog" which is part of the Cortex-M
  13. * System Design Kit (CMSDK) and documented in the Cortex-M System
  14. * Design Kit Technical Reference Manual (ARM DDI0479C):
  15. * https://developer.arm.com/products/system-design/system-design-kits/cortex-m-system-design-kit
  16. *
  17. * We also support the variant of this device found in the TI
  18. * Stellaris/Luminary boards and documented in:
  19. * http://www.ti.com/lit/ds/symlink/lm3s6965.pdf
  20. */
  21. #include "qemu/osdep.h"
  22. #include "qemu/log.h"
  23. #include "trace.h"
  24. #include "qapi/error.h"
  25. #include "qemu/module.h"
  26. #include "sysemu/watchdog.h"
  27. #include "hw/sysbus.h"
  28. #include "hw/irq.h"
  29. #include "hw/qdev-properties.h"
  30. #include "hw/registerfields.h"
  31. #include "hw/watchdog/cmsdk-apb-watchdog.h"
  32. #include "migration/vmstate.h"
  33. REG32(WDOGLOAD, 0x0)
  34. REG32(WDOGVALUE, 0x4)
  35. REG32(WDOGCONTROL, 0x8)
  36. FIELD(WDOGCONTROL, INTEN, 0, 1)
  37. FIELD(WDOGCONTROL, RESEN, 1, 1)
  38. #define R_WDOGCONTROL_VALID_MASK (R_WDOGCONTROL_INTEN_MASK | \
  39. R_WDOGCONTROL_RESEN_MASK)
  40. REG32(WDOGINTCLR, 0xc)
  41. REG32(WDOGRIS, 0x10)
  42. FIELD(WDOGRIS, INT, 0, 1)
  43. REG32(WDOGMIS, 0x14)
  44. REG32(WDOGTEST, 0x418) /* only in Stellaris/Luminary version of the device */
  45. REG32(WDOGLOCK, 0xc00)
  46. #define WDOG_UNLOCK_VALUE 0x1ACCE551
  47. REG32(WDOGITCR, 0xf00)
  48. FIELD(WDOGITCR, ENABLE, 0, 1)
  49. #define R_WDOGITCR_VALID_MASK R_WDOGITCR_ENABLE_MASK
  50. REG32(WDOGITOP, 0xf04)
  51. FIELD(WDOGITOP, WDOGRES, 0, 1)
  52. FIELD(WDOGITOP, WDOGINT, 1, 1)
  53. #define R_WDOGITOP_VALID_MASK (R_WDOGITOP_WDOGRES_MASK | \
  54. R_WDOGITOP_WDOGINT_MASK)
  55. REG32(PID4, 0xfd0)
  56. REG32(PID5, 0xfd4)
  57. REG32(PID6, 0xfd8)
  58. REG32(PID7, 0xfdc)
  59. REG32(PID0, 0xfe0)
  60. REG32(PID1, 0xfe4)
  61. REG32(PID2, 0xfe8)
  62. REG32(PID3, 0xfec)
  63. REG32(CID0, 0xff0)
  64. REG32(CID1, 0xff4)
  65. REG32(CID2, 0xff8)
  66. REG32(CID3, 0xffc)
  67. /* PID/CID values */
  68. static const uint32_t cmsdk_apb_watchdog_id[] = {
  69. 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
  70. 0x24, 0xb8, 0x1b, 0x00, /* PID0..PID3 */
  71. 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
  72. };
  73. static const uint32_t luminary_watchdog_id[] = {
  74. 0x00, 0x00, 0x00, 0x00, /* PID4..PID7 */
  75. 0x05, 0x18, 0x18, 0x01, /* PID0..PID3 */
  76. 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
  77. };
  78. static bool cmsdk_apb_watchdog_intstatus(CMSDKAPBWatchdog *s)
  79. {
  80. /* Return masked interrupt status */
  81. return s->intstatus && (s->control & R_WDOGCONTROL_INTEN_MASK);
  82. }
  83. static bool cmsdk_apb_watchdog_resetstatus(CMSDKAPBWatchdog *s)
  84. {
  85. /* Return masked reset status */
  86. return s->resetstatus && (s->control & R_WDOGCONTROL_RESEN_MASK);
  87. }
  88. static void cmsdk_apb_watchdog_update(CMSDKAPBWatchdog *s)
  89. {
  90. bool wdogint;
  91. bool wdogres;
  92. if (s->itcr) {
  93. /*
  94. * Not checking that !s->is_luminary since s->itcr can't be written
  95. * when s->is_luminary in the first place.
  96. */
  97. wdogint = s->itop & R_WDOGITOP_WDOGINT_MASK;
  98. wdogres = s->itop & R_WDOGITOP_WDOGRES_MASK;
  99. } else {
  100. wdogint = cmsdk_apb_watchdog_intstatus(s);
  101. wdogres = cmsdk_apb_watchdog_resetstatus(s);
  102. }
  103. qemu_set_irq(s->wdogint, wdogint);
  104. if (wdogres) {
  105. watchdog_perform_action();
  106. }
  107. }
  108. static uint64_t cmsdk_apb_watchdog_read(void *opaque, hwaddr offset,
  109. unsigned size)
  110. {
  111. CMSDKAPBWatchdog *s = CMSDK_APB_WATCHDOG(opaque);
  112. uint64_t r;
  113. switch (offset) {
  114. case A_WDOGLOAD:
  115. r = ptimer_get_limit(s->timer);
  116. break;
  117. case A_WDOGVALUE:
  118. r = ptimer_get_count(s->timer);
  119. break;
  120. case A_WDOGCONTROL:
  121. r = s->control;
  122. break;
  123. case A_WDOGRIS:
  124. r = s->intstatus;
  125. break;
  126. case A_WDOGMIS:
  127. r = cmsdk_apb_watchdog_intstatus(s);
  128. break;
  129. case A_WDOGLOCK:
  130. r = s->lock;
  131. break;
  132. case A_WDOGITCR:
  133. if (s->is_luminary) {
  134. goto bad_offset;
  135. }
  136. r = s->itcr;
  137. break;
  138. case A_PID4 ... A_CID3:
  139. r = s->id[(offset - A_PID4) / 4];
  140. break;
  141. case A_WDOGINTCLR:
  142. case A_WDOGITOP:
  143. if (s->is_luminary) {
  144. goto bad_offset;
  145. }
  146. qemu_log_mask(LOG_GUEST_ERROR,
  147. "CMSDK APB watchdog read: read of WO offset %x\n",
  148. (int)offset);
  149. r = 0;
  150. break;
  151. case A_WDOGTEST:
  152. if (!s->is_luminary) {
  153. goto bad_offset;
  154. }
  155. qemu_log_mask(LOG_UNIMP,
  156. "Luminary watchdog read: stall not implemented\n");
  157. r = 0;
  158. break;
  159. default:
  160. bad_offset:
  161. qemu_log_mask(LOG_GUEST_ERROR,
  162. "CMSDK APB watchdog read: bad offset %x\n", (int)offset);
  163. r = 0;
  164. break;
  165. }
  166. trace_cmsdk_apb_watchdog_read(offset, r, size);
  167. return r;
  168. }
  169. static void cmsdk_apb_watchdog_write(void *opaque, hwaddr offset,
  170. uint64_t value, unsigned size)
  171. {
  172. CMSDKAPBWatchdog *s = CMSDK_APB_WATCHDOG(opaque);
  173. trace_cmsdk_apb_watchdog_write(offset, value, size);
  174. if (s->lock && offset != A_WDOGLOCK) {
  175. /* Write access is disabled via WDOGLOCK */
  176. qemu_log_mask(LOG_GUEST_ERROR,
  177. "CMSDK APB watchdog write: write to locked watchdog\n");
  178. return;
  179. }
  180. switch (offset) {
  181. case A_WDOGLOAD:
  182. /*
  183. * Reset the load value and the current count, and make sure
  184. * we're counting.
  185. */
  186. ptimer_transaction_begin(s->timer);
  187. ptimer_set_limit(s->timer, value, 1);
  188. ptimer_run(s->timer, 0);
  189. ptimer_transaction_commit(s->timer);
  190. break;
  191. case A_WDOGCONTROL:
  192. if (s->is_luminary && 0 != (R_WDOGCONTROL_INTEN_MASK & s->control)) {
  193. /*
  194. * The Luminary version of this device ignores writes to
  195. * this register after the guest has enabled interrupts
  196. * (so they can only be disabled again via reset).
  197. */
  198. break;
  199. }
  200. s->control = value & R_WDOGCONTROL_VALID_MASK;
  201. cmsdk_apb_watchdog_update(s);
  202. break;
  203. case A_WDOGINTCLR:
  204. s->intstatus = 0;
  205. ptimer_transaction_begin(s->timer);
  206. ptimer_set_count(s->timer, ptimer_get_limit(s->timer));
  207. ptimer_transaction_commit(s->timer);
  208. cmsdk_apb_watchdog_update(s);
  209. break;
  210. case A_WDOGLOCK:
  211. s->lock = (value != WDOG_UNLOCK_VALUE);
  212. break;
  213. case A_WDOGITCR:
  214. if (s->is_luminary) {
  215. goto bad_offset;
  216. }
  217. s->itcr = value & R_WDOGITCR_VALID_MASK;
  218. cmsdk_apb_watchdog_update(s);
  219. break;
  220. case A_WDOGITOP:
  221. if (s->is_luminary) {
  222. goto bad_offset;
  223. }
  224. s->itop = value & R_WDOGITOP_VALID_MASK;
  225. cmsdk_apb_watchdog_update(s);
  226. break;
  227. case A_WDOGVALUE:
  228. case A_WDOGRIS:
  229. case A_WDOGMIS:
  230. case A_PID4 ... A_CID3:
  231. qemu_log_mask(LOG_GUEST_ERROR,
  232. "CMSDK APB watchdog write: write to RO offset 0x%x\n",
  233. (int)offset);
  234. break;
  235. case A_WDOGTEST:
  236. if (!s->is_luminary) {
  237. goto bad_offset;
  238. }
  239. qemu_log_mask(LOG_UNIMP,
  240. "Luminary watchdog write: stall not implemented\n");
  241. break;
  242. default:
  243. bad_offset:
  244. qemu_log_mask(LOG_GUEST_ERROR,
  245. "CMSDK APB watchdog write: bad offset 0x%x\n",
  246. (int)offset);
  247. break;
  248. }
  249. }
  250. static const MemoryRegionOps cmsdk_apb_watchdog_ops = {
  251. .read = cmsdk_apb_watchdog_read,
  252. .write = cmsdk_apb_watchdog_write,
  253. .endianness = DEVICE_LITTLE_ENDIAN,
  254. /* byte/halfword accesses are just zero-padded on reads and writes */
  255. .impl.min_access_size = 4,
  256. .impl.max_access_size = 4,
  257. .valid.min_access_size = 1,
  258. .valid.max_access_size = 4,
  259. };
  260. static void cmsdk_apb_watchdog_tick(void *opaque)
  261. {
  262. CMSDKAPBWatchdog *s = CMSDK_APB_WATCHDOG(opaque);
  263. if (!s->intstatus) {
  264. /* Count expired for the first time: raise interrupt */
  265. s->intstatus = R_WDOGRIS_INT_MASK;
  266. } else {
  267. /* Count expired for the second time: raise reset and stop clock */
  268. s->resetstatus = 1;
  269. ptimer_stop(s->timer);
  270. }
  271. cmsdk_apb_watchdog_update(s);
  272. }
  273. static void cmsdk_apb_watchdog_reset(DeviceState *dev)
  274. {
  275. CMSDKAPBWatchdog *s = CMSDK_APB_WATCHDOG(dev);
  276. trace_cmsdk_apb_watchdog_reset();
  277. s->control = 0;
  278. s->intstatus = 0;
  279. s->lock = 0;
  280. s->itcr = 0;
  281. s->itop = 0;
  282. s->resetstatus = 0;
  283. /* Set the limit and the count */
  284. ptimer_transaction_begin(s->timer);
  285. ptimer_set_limit(s->timer, 0xffffffff, 1);
  286. ptimer_run(s->timer, 0);
  287. ptimer_transaction_commit(s->timer);
  288. }
  289. static void cmsdk_apb_watchdog_init(Object *obj)
  290. {
  291. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  292. CMSDKAPBWatchdog *s = CMSDK_APB_WATCHDOG(obj);
  293. memory_region_init_io(&s->iomem, obj, &cmsdk_apb_watchdog_ops,
  294. s, "cmsdk-apb-watchdog", 0x1000);
  295. sysbus_init_mmio(sbd, &s->iomem);
  296. sysbus_init_irq(sbd, &s->wdogint);
  297. s->is_luminary = false;
  298. s->id = cmsdk_apb_watchdog_id;
  299. }
  300. static void cmsdk_apb_watchdog_realize(DeviceState *dev, Error **errp)
  301. {
  302. CMSDKAPBWatchdog *s = CMSDK_APB_WATCHDOG(dev);
  303. if (s->wdogclk_frq == 0) {
  304. error_setg(errp,
  305. "CMSDK APB watchdog: wdogclk-frq property must be set");
  306. return;
  307. }
  308. s->timer = ptimer_init(cmsdk_apb_watchdog_tick, s,
  309. PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD |
  310. PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT |
  311. PTIMER_POLICY_NO_IMMEDIATE_RELOAD |
  312. PTIMER_POLICY_NO_COUNTER_ROUND_DOWN);
  313. ptimer_transaction_begin(s->timer);
  314. ptimer_set_freq(s->timer, s->wdogclk_frq);
  315. ptimer_transaction_commit(s->timer);
  316. }
  317. static const VMStateDescription cmsdk_apb_watchdog_vmstate = {
  318. .name = "cmsdk-apb-watchdog",
  319. .version_id = 1,
  320. .minimum_version_id = 1,
  321. .fields = (VMStateField[]) {
  322. VMSTATE_PTIMER(timer, CMSDKAPBWatchdog),
  323. VMSTATE_UINT32(control, CMSDKAPBWatchdog),
  324. VMSTATE_UINT32(intstatus, CMSDKAPBWatchdog),
  325. VMSTATE_UINT32(lock, CMSDKAPBWatchdog),
  326. VMSTATE_UINT32(itcr, CMSDKAPBWatchdog),
  327. VMSTATE_UINT32(itop, CMSDKAPBWatchdog),
  328. VMSTATE_UINT32(resetstatus, CMSDKAPBWatchdog),
  329. VMSTATE_END_OF_LIST()
  330. }
  331. };
  332. static Property cmsdk_apb_watchdog_properties[] = {
  333. DEFINE_PROP_UINT32("wdogclk-frq", CMSDKAPBWatchdog, wdogclk_frq, 0),
  334. DEFINE_PROP_END_OF_LIST(),
  335. };
  336. static void cmsdk_apb_watchdog_class_init(ObjectClass *klass, void *data)
  337. {
  338. DeviceClass *dc = DEVICE_CLASS(klass);
  339. dc->realize = cmsdk_apb_watchdog_realize;
  340. dc->vmsd = &cmsdk_apb_watchdog_vmstate;
  341. dc->reset = cmsdk_apb_watchdog_reset;
  342. dc->props = cmsdk_apb_watchdog_properties;
  343. }
  344. static const TypeInfo cmsdk_apb_watchdog_info = {
  345. .name = TYPE_CMSDK_APB_WATCHDOG,
  346. .parent = TYPE_SYS_BUS_DEVICE,
  347. .instance_size = sizeof(CMSDKAPBWatchdog),
  348. .instance_init = cmsdk_apb_watchdog_init,
  349. .class_init = cmsdk_apb_watchdog_class_init,
  350. };
  351. static void luminary_watchdog_init(Object *obj)
  352. {
  353. CMSDKAPBWatchdog *s = CMSDK_APB_WATCHDOG(obj);
  354. s->is_luminary = true;
  355. s->id = luminary_watchdog_id;
  356. }
  357. static const TypeInfo luminary_watchdog_info = {
  358. .name = TYPE_LUMINARY_WATCHDOG,
  359. .parent = TYPE_CMSDK_APB_WATCHDOG,
  360. .instance_init = luminary_watchdog_init
  361. };
  362. static void cmsdk_apb_watchdog_register_types(void)
  363. {
  364. type_register_static(&cmsdk_apb_watchdog_info);
  365. type_register_static(&luminary_watchdog_info);
  366. }
  367. type_init(cmsdk_apb_watchdog_register_types);