2
0

etraxfs_timer.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #define D(x)
  33. #define RW_TMR0_DIV 0x00
  34. #define R_TMR0_DATA 0x04
  35. #define RW_TMR0_CTRL 0x08
  36. #define RW_TMR1_DIV 0x10
  37. #define R_TMR1_DATA 0x14
  38. #define RW_TMR1_CTRL 0x18
  39. #define R_TIME 0x38
  40. #define RW_WD_CTRL 0x40
  41. #define R_WD_STAT 0x44
  42. #define RW_INTR_MASK 0x48
  43. #define RW_ACK_INTR 0x4c
  44. #define R_INTR 0x50
  45. #define R_MASKED_INTR 0x54
  46. #define TYPE_ETRAX_FS_TIMER "etraxfs,timer"
  47. #define ETRAX_TIMER(obj) \
  48. OBJECT_CHECK(ETRAXTimerState, (obj), TYPE_ETRAX_FS_TIMER)
  49. typedef struct ETRAXTimerState {
  50. SysBusDevice parent_obj;
  51. MemoryRegion mmio;
  52. qemu_irq irq;
  53. qemu_irq nmi;
  54. ptimer_state *ptimer_t0;
  55. ptimer_state *ptimer_t1;
  56. ptimer_state *ptimer_wd;
  57. int wd_hits;
  58. /* Control registers. */
  59. uint32_t rw_tmr0_div;
  60. uint32_t r_tmr0_data;
  61. uint32_t rw_tmr0_ctrl;
  62. uint32_t rw_tmr1_div;
  63. uint32_t r_tmr1_data;
  64. uint32_t rw_tmr1_ctrl;
  65. uint32_t rw_wd_ctrl;
  66. uint32_t rw_intr_mask;
  67. uint32_t rw_ack_intr;
  68. uint32_t r_intr;
  69. uint32_t r_masked_intr;
  70. } ETRAXTimerState;
  71. static uint64_t
  72. timer_read(void *opaque, hwaddr addr, unsigned int size)
  73. {
  74. ETRAXTimerState *t = opaque;
  75. uint32_t r = 0;
  76. switch (addr) {
  77. case R_TMR0_DATA:
  78. r = ptimer_get_count(t->ptimer_t0);
  79. break;
  80. case R_TMR1_DATA:
  81. r = ptimer_get_count(t->ptimer_t1);
  82. break;
  83. case R_TIME:
  84. r = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / 10;
  85. break;
  86. case RW_INTR_MASK:
  87. r = t->rw_intr_mask;
  88. break;
  89. case R_MASKED_INTR:
  90. r = t->r_intr & t->rw_intr_mask;
  91. break;
  92. default:
  93. D(printf ("%s %x\n", __func__, addr));
  94. break;
  95. }
  96. return r;
  97. }
  98. static void update_ctrl(ETRAXTimerState *t, int tnum)
  99. {
  100. unsigned int op;
  101. unsigned int freq;
  102. unsigned int freq_hz;
  103. unsigned int div;
  104. uint32_t ctrl;
  105. ptimer_state *timer;
  106. if (tnum == 0) {
  107. ctrl = t->rw_tmr0_ctrl;
  108. div = t->rw_tmr0_div;
  109. timer = t->ptimer_t0;
  110. } else {
  111. ctrl = t->rw_tmr1_ctrl;
  112. div = t->rw_tmr1_div;
  113. timer = t->ptimer_t1;
  114. }
  115. op = ctrl & 3;
  116. freq = ctrl >> 2;
  117. freq_hz = 32000000;
  118. switch (freq)
  119. {
  120. case 0:
  121. case 1:
  122. D(printf ("extern or disabled timer clock?\n"));
  123. break;
  124. case 4: freq_hz = 29493000; break;
  125. case 5: freq_hz = 32000000; break;
  126. case 6: freq_hz = 32768000; break;
  127. case 7: freq_hz = 100000000; break;
  128. default:
  129. abort();
  130. break;
  131. }
  132. D(printf ("freq_hz=%d div=%d\n", freq_hz, div));
  133. ptimer_transaction_begin(timer);
  134. ptimer_set_freq(timer, freq_hz);
  135. ptimer_set_limit(timer, div, 0);
  136. switch (op)
  137. {
  138. case 0:
  139. /* Load. */
  140. ptimer_set_limit(timer, div, 1);
  141. break;
  142. case 1:
  143. /* Hold. */
  144. ptimer_stop(timer);
  145. break;
  146. case 2:
  147. /* Run. */
  148. ptimer_run(timer, 0);
  149. break;
  150. default:
  151. abort();
  152. break;
  153. }
  154. ptimer_transaction_commit(timer);
  155. }
  156. static void timer_update_irq(ETRAXTimerState *t)
  157. {
  158. t->r_intr &= ~(t->rw_ack_intr);
  159. t->r_masked_intr = t->r_intr & t->rw_intr_mask;
  160. D(printf("%s: masked_intr=%x\n", __func__, t->r_masked_intr));
  161. qemu_set_irq(t->irq, !!t->r_masked_intr);
  162. }
  163. static void timer0_hit(void *opaque)
  164. {
  165. ETRAXTimerState *t = opaque;
  166. t->r_intr |= 1;
  167. timer_update_irq(t);
  168. }
  169. static void timer1_hit(void *opaque)
  170. {
  171. ETRAXTimerState *t = opaque;
  172. t->r_intr |= 2;
  173. timer_update_irq(t);
  174. }
  175. static void watchdog_hit(void *opaque)
  176. {
  177. ETRAXTimerState *t = opaque;
  178. if (t->wd_hits == 0) {
  179. /* real hw gives a single tick before reseting but we are
  180. a bit friendlier to compensate for our slower execution. */
  181. ptimer_set_count(t->ptimer_wd, 10);
  182. ptimer_run(t->ptimer_wd, 1);
  183. qemu_irq_raise(t->nmi);
  184. }
  185. else
  186. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  187. t->wd_hits++;
  188. }
  189. static inline void timer_watchdog_update(ETRAXTimerState *t, uint32_t value)
  190. {
  191. unsigned int wd_en = t->rw_wd_ctrl & (1 << 8);
  192. unsigned int wd_key = t->rw_wd_ctrl >> 9;
  193. unsigned int wd_cnt = t->rw_wd_ctrl & 511;
  194. unsigned int new_key = value >> 9 & ((1 << 7) - 1);
  195. unsigned int new_cmd = (value >> 8) & 1;
  196. /* If the watchdog is enabled, they written key must match the
  197. complement of the previous. */
  198. wd_key = ~wd_key & ((1 << 7) - 1);
  199. if (wd_en && wd_key != new_key)
  200. return;
  201. D(printf("en=%d new_key=%x oldkey=%x cmd=%d cnt=%d\n",
  202. wd_en, new_key, wd_key, new_cmd, wd_cnt));
  203. if (t->wd_hits)
  204. qemu_irq_lower(t->nmi);
  205. t->wd_hits = 0;
  206. ptimer_transaction_begin(t->ptimer_wd);
  207. ptimer_set_freq(t->ptimer_wd, 760);
  208. if (wd_cnt == 0)
  209. wd_cnt = 256;
  210. ptimer_set_count(t->ptimer_wd, wd_cnt);
  211. if (new_cmd)
  212. ptimer_run(t->ptimer_wd, 1);
  213. else
  214. ptimer_stop(t->ptimer_wd);
  215. t->rw_wd_ctrl = value;
  216. ptimer_transaction_commit(t->ptimer_wd);
  217. }
  218. static void
  219. timer_write(void *opaque, hwaddr addr,
  220. uint64_t val64, unsigned int size)
  221. {
  222. ETRAXTimerState *t = opaque;
  223. uint32_t value = val64;
  224. switch (addr)
  225. {
  226. case RW_TMR0_DIV:
  227. t->rw_tmr0_div = value;
  228. break;
  229. case RW_TMR0_CTRL:
  230. D(printf ("RW_TMR0_CTRL=%x\n", value));
  231. t->rw_tmr0_ctrl = value;
  232. update_ctrl(t, 0);
  233. break;
  234. case RW_TMR1_DIV:
  235. t->rw_tmr1_div = value;
  236. break;
  237. case RW_TMR1_CTRL:
  238. D(printf ("RW_TMR1_CTRL=%x\n", value));
  239. t->rw_tmr1_ctrl = value;
  240. update_ctrl(t, 1);
  241. break;
  242. case RW_INTR_MASK:
  243. D(printf ("RW_INTR_MASK=%x\n", value));
  244. t->rw_intr_mask = value;
  245. timer_update_irq(t);
  246. break;
  247. case RW_WD_CTRL:
  248. timer_watchdog_update(t, value);
  249. break;
  250. case RW_ACK_INTR:
  251. t->rw_ack_intr = value;
  252. timer_update_irq(t);
  253. t->rw_ack_intr = 0;
  254. break;
  255. default:
  256. printf ("%s " TARGET_FMT_plx " %x\n",
  257. __func__, addr, value);
  258. break;
  259. }
  260. }
  261. static const MemoryRegionOps timer_ops = {
  262. .read = timer_read,
  263. .write = timer_write,
  264. .endianness = DEVICE_LITTLE_ENDIAN,
  265. .valid = {
  266. .min_access_size = 4,
  267. .max_access_size = 4
  268. }
  269. };
  270. static void etraxfs_timer_reset(void *opaque)
  271. {
  272. ETRAXTimerState *t = opaque;
  273. ptimer_transaction_begin(t->ptimer_t0);
  274. ptimer_stop(t->ptimer_t0);
  275. ptimer_transaction_commit(t->ptimer_t0);
  276. ptimer_transaction_begin(t->ptimer_t1);
  277. ptimer_stop(t->ptimer_t1);
  278. ptimer_transaction_commit(t->ptimer_t1);
  279. ptimer_transaction_begin(t->ptimer_wd);
  280. ptimer_stop(t->ptimer_wd);
  281. ptimer_transaction_commit(t->ptimer_wd);
  282. t->rw_wd_ctrl = 0;
  283. t->r_intr = 0;
  284. t->rw_intr_mask = 0;
  285. qemu_irq_lower(t->irq);
  286. }
  287. static void etraxfs_timer_realize(DeviceState *dev, Error **errp)
  288. {
  289. ETRAXTimerState *t = ETRAX_TIMER(dev);
  290. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  291. t->ptimer_t0 = ptimer_init(timer0_hit, t, PTIMER_POLICY_DEFAULT);
  292. t->ptimer_t1 = ptimer_init(timer1_hit, t, PTIMER_POLICY_DEFAULT);
  293. t->ptimer_wd = ptimer_init(watchdog_hit, t, PTIMER_POLICY_DEFAULT);
  294. sysbus_init_irq(sbd, &t->irq);
  295. sysbus_init_irq(sbd, &t->nmi);
  296. memory_region_init_io(&t->mmio, OBJECT(t), &timer_ops, t,
  297. "etraxfs-timer", 0x5c);
  298. sysbus_init_mmio(sbd, &t->mmio);
  299. qemu_register_reset(etraxfs_timer_reset, t);
  300. }
  301. static void etraxfs_timer_class_init(ObjectClass *klass, void *data)
  302. {
  303. DeviceClass *dc = DEVICE_CLASS(klass);
  304. dc->realize = etraxfs_timer_realize;
  305. }
  306. static const TypeInfo etraxfs_timer_info = {
  307. .name = TYPE_ETRAX_FS_TIMER,
  308. .parent = TYPE_SYS_BUS_DEVICE,
  309. .instance_size = sizeof(ETRAXTimerState),
  310. .class_init = etraxfs_timer_class_init,
  311. };
  312. static void etraxfs_timer_register_types(void)
  313. {
  314. type_register_static(&etraxfs_timer_info);
  315. }
  316. type_init(etraxfs_timer_register_types)