etraxfs_timer.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "sysbus.h"
  25. #include "sysemu.h"
  26. #include "qemu-timer.h"
  27. #define D(x)
  28. #define RW_TMR0_DIV 0x00
  29. #define R_TMR0_DATA 0x04
  30. #define RW_TMR0_CTRL 0x08
  31. #define RW_TMR1_DIV 0x10
  32. #define R_TMR1_DATA 0x14
  33. #define RW_TMR1_CTRL 0x18
  34. #define R_TIME 0x38
  35. #define RW_WD_CTRL 0x40
  36. #define R_WD_STAT 0x44
  37. #define RW_INTR_MASK 0x48
  38. #define RW_ACK_INTR 0x4c
  39. #define R_INTR 0x50
  40. #define R_MASKED_INTR 0x54
  41. struct etrax_timer {
  42. SysBusDevice busdev;
  43. qemu_irq irq;
  44. qemu_irq nmi;
  45. QEMUBH *bh_t0;
  46. QEMUBH *bh_t1;
  47. QEMUBH *bh_wd;
  48. ptimer_state *ptimer_t0;
  49. ptimer_state *ptimer_t1;
  50. ptimer_state *ptimer_wd;
  51. int wd_hits;
  52. /* Control registers. */
  53. uint32_t rw_tmr0_div;
  54. uint32_t r_tmr0_data;
  55. uint32_t rw_tmr0_ctrl;
  56. uint32_t rw_tmr1_div;
  57. uint32_t r_tmr1_data;
  58. uint32_t rw_tmr1_ctrl;
  59. uint32_t rw_wd_ctrl;
  60. uint32_t rw_intr_mask;
  61. uint32_t rw_ack_intr;
  62. uint32_t r_intr;
  63. uint32_t r_masked_intr;
  64. };
  65. static uint32_t timer_readl (void *opaque, target_phys_addr_t addr)
  66. {
  67. struct etrax_timer *t = opaque;
  68. uint32_t r = 0;
  69. switch (addr) {
  70. case R_TMR0_DATA:
  71. r = ptimer_get_count(t->ptimer_t0);
  72. break;
  73. case R_TMR1_DATA:
  74. r = ptimer_get_count(t->ptimer_t1);
  75. break;
  76. case R_TIME:
  77. r = qemu_get_clock_ns(vm_clock) / 10;
  78. break;
  79. case RW_INTR_MASK:
  80. r = t->rw_intr_mask;
  81. break;
  82. case R_MASKED_INTR:
  83. r = t->r_intr & t->rw_intr_mask;
  84. break;
  85. default:
  86. D(printf ("%s %x\n", __func__, addr));
  87. break;
  88. }
  89. return r;
  90. }
  91. static void update_ctrl(struct etrax_timer *t, int tnum)
  92. {
  93. unsigned int op;
  94. unsigned int freq;
  95. unsigned int freq_hz;
  96. unsigned int div;
  97. uint32_t ctrl;
  98. ptimer_state *timer;
  99. if (tnum == 0) {
  100. ctrl = t->rw_tmr0_ctrl;
  101. div = t->rw_tmr0_div;
  102. timer = t->ptimer_t0;
  103. } else {
  104. ctrl = t->rw_tmr1_ctrl;
  105. div = t->rw_tmr1_div;
  106. timer = t->ptimer_t1;
  107. }
  108. op = ctrl & 3;
  109. freq = ctrl >> 2;
  110. freq_hz = 32000000;
  111. switch (freq)
  112. {
  113. case 0:
  114. case 1:
  115. D(printf ("extern or disabled timer clock?\n"));
  116. break;
  117. case 4: freq_hz = 29493000; break;
  118. case 5: freq_hz = 32000000; break;
  119. case 6: freq_hz = 32768000; break;
  120. case 7: freq_hz = 100000000; break;
  121. default:
  122. abort();
  123. break;
  124. }
  125. D(printf ("freq_hz=%d div=%d\n", freq_hz, div));
  126. ptimer_set_freq(timer, freq_hz);
  127. ptimer_set_limit(timer, div, 0);
  128. switch (op)
  129. {
  130. case 0:
  131. /* Load. */
  132. ptimer_set_limit(timer, div, 1);
  133. break;
  134. case 1:
  135. /* Hold. */
  136. ptimer_stop(timer);
  137. break;
  138. case 2:
  139. /* Run. */
  140. ptimer_run(timer, 0);
  141. break;
  142. default:
  143. abort();
  144. break;
  145. }
  146. }
  147. static void timer_update_irq(struct etrax_timer *t)
  148. {
  149. t->r_intr &= ~(t->rw_ack_intr);
  150. t->r_masked_intr = t->r_intr & t->rw_intr_mask;
  151. D(printf("%s: masked_intr=%x\n", __func__, t->r_masked_intr));
  152. qemu_set_irq(t->irq, !!t->r_masked_intr);
  153. }
  154. static void timer0_hit(void *opaque)
  155. {
  156. struct etrax_timer *t = opaque;
  157. t->r_intr |= 1;
  158. timer_update_irq(t);
  159. }
  160. static void timer1_hit(void *opaque)
  161. {
  162. struct etrax_timer *t = opaque;
  163. t->r_intr |= 2;
  164. timer_update_irq(t);
  165. }
  166. static void watchdog_hit(void *opaque)
  167. {
  168. struct etrax_timer *t = opaque;
  169. if (t->wd_hits == 0) {
  170. /* real hw gives a single tick before reseting but we are
  171. a bit friendlier to compensate for our slower execution. */
  172. ptimer_set_count(t->ptimer_wd, 10);
  173. ptimer_run(t->ptimer_wd, 1);
  174. qemu_irq_raise(t->nmi);
  175. }
  176. else
  177. qemu_system_reset_request();
  178. t->wd_hits++;
  179. }
  180. static inline void timer_watchdog_update(struct etrax_timer *t, uint32_t value)
  181. {
  182. unsigned int wd_en = t->rw_wd_ctrl & (1 << 8);
  183. unsigned int wd_key = t->rw_wd_ctrl >> 9;
  184. unsigned int wd_cnt = t->rw_wd_ctrl & 511;
  185. unsigned int new_key = value >> 9 & ((1 << 7) - 1);
  186. unsigned int new_cmd = (value >> 8) & 1;
  187. /* If the watchdog is enabled, they written key must match the
  188. complement of the previous. */
  189. wd_key = ~wd_key & ((1 << 7) - 1);
  190. if (wd_en && wd_key != new_key)
  191. return;
  192. D(printf("en=%d new_key=%x oldkey=%x cmd=%d cnt=%d\n",
  193. wd_en, new_key, wd_key, new_cmd, wd_cnt));
  194. if (t->wd_hits)
  195. qemu_irq_lower(t->nmi);
  196. t->wd_hits = 0;
  197. ptimer_set_freq(t->ptimer_wd, 760);
  198. if (wd_cnt == 0)
  199. wd_cnt = 256;
  200. ptimer_set_count(t->ptimer_wd, wd_cnt);
  201. if (new_cmd)
  202. ptimer_run(t->ptimer_wd, 1);
  203. else
  204. ptimer_stop(t->ptimer_wd);
  205. t->rw_wd_ctrl = value;
  206. }
  207. static void
  208. timer_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
  209. {
  210. struct etrax_timer *t = opaque;
  211. switch (addr)
  212. {
  213. case RW_TMR0_DIV:
  214. t->rw_tmr0_div = value;
  215. break;
  216. case RW_TMR0_CTRL:
  217. D(printf ("RW_TMR0_CTRL=%x\n", value));
  218. t->rw_tmr0_ctrl = value;
  219. update_ctrl(t, 0);
  220. break;
  221. case RW_TMR1_DIV:
  222. t->rw_tmr1_div = value;
  223. break;
  224. case RW_TMR1_CTRL:
  225. D(printf ("RW_TMR1_CTRL=%x\n", value));
  226. t->rw_tmr1_ctrl = value;
  227. update_ctrl(t, 1);
  228. break;
  229. case RW_INTR_MASK:
  230. D(printf ("RW_INTR_MASK=%x\n", value));
  231. t->rw_intr_mask = value;
  232. timer_update_irq(t);
  233. break;
  234. case RW_WD_CTRL:
  235. timer_watchdog_update(t, value);
  236. break;
  237. case RW_ACK_INTR:
  238. t->rw_ack_intr = value;
  239. timer_update_irq(t);
  240. t->rw_ack_intr = 0;
  241. break;
  242. default:
  243. printf ("%s " TARGET_FMT_plx " %x\n",
  244. __func__, addr, value);
  245. break;
  246. }
  247. }
  248. static CPUReadMemoryFunc * const timer_read[] = {
  249. NULL, NULL,
  250. &timer_readl,
  251. };
  252. static CPUWriteMemoryFunc * const timer_write[] = {
  253. NULL, NULL,
  254. &timer_writel,
  255. };
  256. static void etraxfs_timer_reset(void *opaque)
  257. {
  258. struct etrax_timer *t = opaque;
  259. ptimer_stop(t->ptimer_t0);
  260. ptimer_stop(t->ptimer_t1);
  261. ptimer_stop(t->ptimer_wd);
  262. t->rw_wd_ctrl = 0;
  263. t->r_intr = 0;
  264. t->rw_intr_mask = 0;
  265. qemu_irq_lower(t->irq);
  266. }
  267. static int etraxfs_timer_init(SysBusDevice *dev)
  268. {
  269. struct etrax_timer *t = FROM_SYSBUS(typeof (*t), dev);
  270. int timer_regs;
  271. t->bh_t0 = qemu_bh_new(timer0_hit, t);
  272. t->bh_t1 = qemu_bh_new(timer1_hit, t);
  273. t->bh_wd = qemu_bh_new(watchdog_hit, t);
  274. t->ptimer_t0 = ptimer_init(t->bh_t0);
  275. t->ptimer_t1 = ptimer_init(t->bh_t1);
  276. t->ptimer_wd = ptimer_init(t->bh_wd);
  277. sysbus_init_irq(dev, &t->irq);
  278. sysbus_init_irq(dev, &t->nmi);
  279. timer_regs = cpu_register_io_memory(timer_read, timer_write, t,
  280. DEVICE_NATIVE_ENDIAN);
  281. sysbus_init_mmio(dev, 0x5c, timer_regs);
  282. qemu_register_reset(etraxfs_timer_reset, t);
  283. return 0;
  284. }
  285. static void etraxfs_timer_register(void)
  286. {
  287. sysbus_register_dev("etraxfs,timer", sizeof (struct etrax_timer),
  288. etraxfs_timer_init);
  289. }
  290. device_init(etraxfs_timer_register)