etraxfs_timer.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * QEMU ETRAX Timers
  3. *
  4. * Copyright (c) 2007 Edgar E. Iglesias, Axis Communications AB.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "hw/sysbus.h"
  26. #include "sysemu/reset.h"
  27. #include "sysemu/runstate.h"
  28. #include "qemu/module.h"
  29. #include "qemu/timer.h"
  30. #include "hw/irq.h"
  31. #include "hw/ptimer.h"
  32. #include "qom/object.h"
  33. #define D(x)
  34. #define RW_TMR0_DIV 0x00
  35. #define R_TMR0_DATA 0x04
  36. #define RW_TMR0_CTRL 0x08
  37. #define RW_TMR1_DIV 0x10
  38. #define R_TMR1_DATA 0x14
  39. #define RW_TMR1_CTRL 0x18
  40. #define R_TIME 0x38
  41. #define RW_WD_CTRL 0x40
  42. #define R_WD_STAT 0x44
  43. #define RW_INTR_MASK 0x48
  44. #define RW_ACK_INTR 0x4c
  45. #define R_INTR 0x50
  46. #define R_MASKED_INTR 0x54
  47. #define TYPE_ETRAX_FS_TIMER "etraxfs,timer"
  48. typedef struct ETRAXTimerState ETRAXTimerState;
  49. DECLARE_INSTANCE_CHECKER(ETRAXTimerState, ETRAX_TIMER,
  50. TYPE_ETRAX_FS_TIMER)
  51. struct ETRAXTimerState {
  52. SysBusDevice parent_obj;
  53. MemoryRegion mmio;
  54. qemu_irq irq;
  55. qemu_irq nmi;
  56. ptimer_state *ptimer_t0;
  57. ptimer_state *ptimer_t1;
  58. ptimer_state *ptimer_wd;
  59. int wd_hits;
  60. /* Control registers. */
  61. uint32_t rw_tmr0_div;
  62. uint32_t r_tmr0_data;
  63. uint32_t rw_tmr0_ctrl;
  64. uint32_t rw_tmr1_div;
  65. uint32_t r_tmr1_data;
  66. uint32_t rw_tmr1_ctrl;
  67. uint32_t rw_wd_ctrl;
  68. uint32_t rw_intr_mask;
  69. uint32_t rw_ack_intr;
  70. uint32_t r_intr;
  71. uint32_t r_masked_intr;
  72. };
  73. static uint64_t
  74. timer_read(void *opaque, hwaddr addr, unsigned int size)
  75. {
  76. ETRAXTimerState *t = opaque;
  77. uint32_t r = 0;
  78. switch (addr) {
  79. case R_TMR0_DATA:
  80. r = ptimer_get_count(t->ptimer_t0);
  81. break;
  82. case R_TMR1_DATA:
  83. r = ptimer_get_count(t->ptimer_t1);
  84. break;
  85. case R_TIME:
  86. r = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / 10;
  87. break;
  88. case RW_INTR_MASK:
  89. r = t->rw_intr_mask;
  90. break;
  91. case R_MASKED_INTR:
  92. r = t->r_intr & t->rw_intr_mask;
  93. break;
  94. default:
  95. D(printf ("%s %x\n", __func__, addr));
  96. break;
  97. }
  98. return r;
  99. }
  100. static void update_ctrl(ETRAXTimerState *t, int tnum)
  101. {
  102. unsigned int op;
  103. unsigned int freq;
  104. unsigned int freq_hz;
  105. unsigned int div;
  106. uint32_t ctrl;
  107. ptimer_state *timer;
  108. if (tnum == 0) {
  109. ctrl = t->rw_tmr0_ctrl;
  110. div = t->rw_tmr0_div;
  111. timer = t->ptimer_t0;
  112. } else {
  113. ctrl = t->rw_tmr1_ctrl;
  114. div = t->rw_tmr1_div;
  115. timer = t->ptimer_t1;
  116. }
  117. op = ctrl & 3;
  118. freq = ctrl >> 2;
  119. freq_hz = 32000000;
  120. switch (freq)
  121. {
  122. case 0:
  123. case 1:
  124. D(printf ("extern or disabled timer clock?\n"));
  125. break;
  126. case 4: freq_hz = 29493000; break;
  127. case 5: freq_hz = 32000000; break;
  128. case 6: freq_hz = 32768000; break;
  129. case 7: freq_hz = 100000000; break;
  130. default:
  131. abort();
  132. break;
  133. }
  134. D(printf ("freq_hz=%d div=%d\n", freq_hz, div));
  135. ptimer_transaction_begin(timer);
  136. ptimer_set_freq(timer, freq_hz);
  137. ptimer_set_limit(timer, div, 0);
  138. switch (op)
  139. {
  140. case 0:
  141. /* Load. */
  142. ptimer_set_limit(timer, div, 1);
  143. break;
  144. case 1:
  145. /* Hold. */
  146. ptimer_stop(timer);
  147. break;
  148. case 2:
  149. /* Run. */
  150. ptimer_run(timer, 0);
  151. break;
  152. default:
  153. abort();
  154. break;
  155. }
  156. ptimer_transaction_commit(timer);
  157. }
  158. static void timer_update_irq(ETRAXTimerState *t)
  159. {
  160. t->r_intr &= ~(t->rw_ack_intr);
  161. t->r_masked_intr = t->r_intr & t->rw_intr_mask;
  162. D(printf("%s: masked_intr=%x\n", __func__, t->r_masked_intr));
  163. qemu_set_irq(t->irq, !!t->r_masked_intr);
  164. }
  165. static void timer0_hit(void *opaque)
  166. {
  167. ETRAXTimerState *t = opaque;
  168. t->r_intr |= 1;
  169. timer_update_irq(t);
  170. }
  171. static void timer1_hit(void *opaque)
  172. {
  173. ETRAXTimerState *t = opaque;
  174. t->r_intr |= 2;
  175. timer_update_irq(t);
  176. }
  177. static void watchdog_hit(void *opaque)
  178. {
  179. ETRAXTimerState *t = opaque;
  180. if (t->wd_hits == 0) {
  181. /* real hw gives a single tick before reseting but we are
  182. a bit friendlier to compensate for our slower execution. */
  183. ptimer_set_count(t->ptimer_wd, 10);
  184. ptimer_run(t->ptimer_wd, 1);
  185. qemu_irq_raise(t->nmi);
  186. }
  187. else
  188. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  189. t->wd_hits++;
  190. }
  191. static inline void timer_watchdog_update(ETRAXTimerState *t, uint32_t value)
  192. {
  193. unsigned int wd_en = t->rw_wd_ctrl & (1 << 8);
  194. unsigned int wd_key = t->rw_wd_ctrl >> 9;
  195. unsigned int wd_cnt = t->rw_wd_ctrl & 511;
  196. unsigned int new_key = value >> 9 & ((1 << 7) - 1);
  197. unsigned int new_cmd = (value >> 8) & 1;
  198. /* If the watchdog is enabled, they written key must match the
  199. complement of the previous. */
  200. wd_key = ~wd_key & ((1 << 7) - 1);
  201. if (wd_en && wd_key != new_key)
  202. return;
  203. D(printf("en=%d new_key=%x oldkey=%x cmd=%d cnt=%d\n",
  204. wd_en, new_key, wd_key, new_cmd, wd_cnt));
  205. if (t->wd_hits)
  206. qemu_irq_lower(t->nmi);
  207. t->wd_hits = 0;
  208. ptimer_transaction_begin(t->ptimer_wd);
  209. ptimer_set_freq(t->ptimer_wd, 760);
  210. if (wd_cnt == 0)
  211. wd_cnt = 256;
  212. ptimer_set_count(t->ptimer_wd, wd_cnt);
  213. if (new_cmd)
  214. ptimer_run(t->ptimer_wd, 1);
  215. else
  216. ptimer_stop(t->ptimer_wd);
  217. t->rw_wd_ctrl = value;
  218. ptimer_transaction_commit(t->ptimer_wd);
  219. }
  220. static void
  221. timer_write(void *opaque, hwaddr addr,
  222. uint64_t val64, unsigned int size)
  223. {
  224. ETRAXTimerState *t = opaque;
  225. uint32_t value = val64;
  226. switch (addr)
  227. {
  228. case RW_TMR0_DIV:
  229. t->rw_tmr0_div = value;
  230. break;
  231. case RW_TMR0_CTRL:
  232. D(printf ("RW_TMR0_CTRL=%x\n", value));
  233. t->rw_tmr0_ctrl = value;
  234. update_ctrl(t, 0);
  235. break;
  236. case RW_TMR1_DIV:
  237. t->rw_tmr1_div = value;
  238. break;
  239. case RW_TMR1_CTRL:
  240. D(printf ("RW_TMR1_CTRL=%x\n", value));
  241. t->rw_tmr1_ctrl = value;
  242. update_ctrl(t, 1);
  243. break;
  244. case RW_INTR_MASK:
  245. D(printf ("RW_INTR_MASK=%x\n", value));
  246. t->rw_intr_mask = value;
  247. timer_update_irq(t);
  248. break;
  249. case RW_WD_CTRL:
  250. timer_watchdog_update(t, value);
  251. break;
  252. case RW_ACK_INTR:
  253. t->rw_ack_intr = value;
  254. timer_update_irq(t);
  255. t->rw_ack_intr = 0;
  256. break;
  257. default:
  258. printf ("%s " TARGET_FMT_plx " %x\n",
  259. __func__, addr, value);
  260. break;
  261. }
  262. }
  263. static const MemoryRegionOps timer_ops = {
  264. .read = timer_read,
  265. .write = timer_write,
  266. .endianness = DEVICE_LITTLE_ENDIAN,
  267. .valid = {
  268. .min_access_size = 4,
  269. .max_access_size = 4
  270. }
  271. };
  272. static void etraxfs_timer_reset(void *opaque)
  273. {
  274. ETRAXTimerState *t = opaque;
  275. ptimer_transaction_begin(t->ptimer_t0);
  276. ptimer_stop(t->ptimer_t0);
  277. ptimer_transaction_commit(t->ptimer_t0);
  278. ptimer_transaction_begin(t->ptimer_t1);
  279. ptimer_stop(t->ptimer_t1);
  280. ptimer_transaction_commit(t->ptimer_t1);
  281. ptimer_transaction_begin(t->ptimer_wd);
  282. ptimer_stop(t->ptimer_wd);
  283. ptimer_transaction_commit(t->ptimer_wd);
  284. t->rw_wd_ctrl = 0;
  285. t->r_intr = 0;
  286. t->rw_intr_mask = 0;
  287. qemu_irq_lower(t->irq);
  288. }
  289. static void etraxfs_timer_realize(DeviceState *dev, Error **errp)
  290. {
  291. ETRAXTimerState *t = ETRAX_TIMER(dev);
  292. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  293. t->ptimer_t0 = ptimer_init(timer0_hit, t, PTIMER_POLICY_DEFAULT);
  294. t->ptimer_t1 = ptimer_init(timer1_hit, t, PTIMER_POLICY_DEFAULT);
  295. t->ptimer_wd = ptimer_init(watchdog_hit, t, PTIMER_POLICY_DEFAULT);
  296. sysbus_init_irq(sbd, &t->irq);
  297. sysbus_init_irq(sbd, &t->nmi);
  298. memory_region_init_io(&t->mmio, OBJECT(t), &timer_ops, t,
  299. "etraxfs-timer", 0x5c);
  300. sysbus_init_mmio(sbd, &t->mmio);
  301. qemu_register_reset(etraxfs_timer_reset, t);
  302. }
  303. static void etraxfs_timer_class_init(ObjectClass *klass, void *data)
  304. {
  305. DeviceClass *dc = DEVICE_CLASS(klass);
  306. dc->realize = etraxfs_timer_realize;
  307. }
  308. static const TypeInfo etraxfs_timer_info = {
  309. .name = TYPE_ETRAX_FS_TIMER,
  310. .parent = TYPE_SYS_BUS_DEVICE,
  311. .instance_size = sizeof(ETRAXTimerState),
  312. .class_init = etraxfs_timer_class_init,
  313. };
  314. static void etraxfs_timer_register_types(void)
  315. {
  316. type_register_static(&etraxfs_timer_info);
  317. }
  318. type_init(etraxfs_timer_register_types)