2
0

etraxfs_timer.c 8.4 KB

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