etraxfs_timer.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <stdio.h>
  25. #include <sys/time.h>
  26. #include "hw.h"
  27. #include "sysemu.h"
  28. #include "qemu-timer.h"
  29. #include "etraxfs.h"
  30. #define D(x)
  31. #define RW_TMR0_DIV 0x00
  32. #define R_TMR0_DATA 0x04
  33. #define RW_TMR0_CTRL 0x08
  34. #define RW_TMR1_DIV 0x10
  35. #define R_TMR1_DATA 0x14
  36. #define RW_TMR1_CTRL 0x18
  37. #define R_TIME 0x38
  38. #define RW_WD_CTRL 0x40
  39. #define R_WD_STAT 0x44
  40. #define RW_INTR_MASK 0x48
  41. #define RW_ACK_INTR 0x4c
  42. #define R_INTR 0x50
  43. #define R_MASKED_INTR 0x54
  44. struct fs_timer_t {
  45. CPUState *env;
  46. qemu_irq *irq;
  47. qemu_irq *nmi;
  48. QEMUBH *bh_t0;
  49. QEMUBH *bh_t1;
  50. QEMUBH *bh_wd;
  51. ptimer_state *ptimer_t0;
  52. ptimer_state *ptimer_t1;
  53. ptimer_state *ptimer_wd;
  54. struct timeval last;
  55. int wd_hits;
  56. /* Control registers. */
  57. uint32_t rw_tmr0_div;
  58. uint32_t r_tmr0_data;
  59. uint32_t rw_tmr0_ctrl;
  60. uint32_t rw_tmr1_div;
  61. uint32_t r_tmr1_data;
  62. uint32_t rw_tmr1_ctrl;
  63. uint32_t rw_wd_ctrl;
  64. uint32_t rw_intr_mask;
  65. uint32_t rw_ack_intr;
  66. uint32_t r_intr;
  67. uint32_t r_masked_intr;
  68. };
  69. static uint32_t timer_readl (void *opaque, target_phys_addr_t addr)
  70. {
  71. struct fs_timer_t *t = opaque;
  72. uint32_t r = 0;
  73. switch (addr) {
  74. case R_TMR0_DATA:
  75. r = ptimer_get_count(t->ptimer_t0);
  76. break;
  77. case R_TMR1_DATA:
  78. r = ptimer_get_count(t->ptimer_t1);
  79. break;
  80. case R_TIME:
  81. r = qemu_get_clock(vm_clock) / 10;
  82. break;
  83. case RW_INTR_MASK:
  84. r = t->rw_intr_mask;
  85. break;
  86. case R_MASKED_INTR:
  87. r = t->r_intr & t->rw_intr_mask;
  88. break;
  89. default:
  90. D(printf ("%s %x\n", __func__, addr));
  91. break;
  92. }
  93. return r;
  94. }
  95. #define TIMER_SLOWDOWN 1
  96. static void update_ctrl(struct fs_timer_t *t, int tnum)
  97. {
  98. unsigned int op;
  99. unsigned int freq;
  100. unsigned int freq_hz;
  101. unsigned int div;
  102. uint32_t ctrl;
  103. ptimer_state *timer;
  104. if (tnum == 0) {
  105. ctrl = t->rw_tmr0_ctrl;
  106. div = t->rw_tmr0_div;
  107. timer = t->ptimer_t0;
  108. } else {
  109. ctrl = t->rw_tmr1_ctrl;
  110. div = t->rw_tmr1_div;
  111. timer = t->ptimer_t1;
  112. }
  113. op = ctrl & 3;
  114. freq = ctrl >> 2;
  115. freq_hz = 32000000;
  116. switch (freq)
  117. {
  118. case 0:
  119. case 1:
  120. D(printf ("extern or disabled timer clock?\n"));
  121. break;
  122. case 4: freq_hz = 29493000; break;
  123. case 5: freq_hz = 32000000; break;
  124. case 6: freq_hz = 32768000; break;
  125. case 7: freq_hz = 100000000; break;
  126. default:
  127. abort();
  128. break;
  129. }
  130. D(printf ("freq_hz=%d div=%d\n", freq_hz, div));
  131. div = div * TIMER_SLOWDOWN;
  132. div /= 1000;
  133. freq_hz /= 1000;
  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. }
  155. static void timer_update_irq(struct fs_timer_t *t)
  156. {
  157. t->r_intr &= ~(t->rw_ack_intr);
  158. t->r_masked_intr = t->r_intr & t->rw_intr_mask;
  159. D(printf("%s: masked_intr=%x\n", __func__, t->r_masked_intr));
  160. if (t->r_masked_intr)
  161. qemu_irq_raise(t->irq[0]);
  162. else
  163. qemu_irq_lower(t->irq[0]);
  164. }
  165. static void timer0_hit(void *opaque)
  166. {
  167. struct fs_timer_t *t = opaque;
  168. t->r_intr |= 1;
  169. timer_update_irq(t);
  170. }
  171. static void timer1_hit(void *opaque)
  172. {
  173. struct fs_timer_t *t = opaque;
  174. t->r_intr |= 2;
  175. timer_update_irq(t);
  176. }
  177. static void watchdog_hit(void *opaque)
  178. {
  179. struct fs_timer_t *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[0]);
  186. }
  187. else
  188. qemu_system_reset_request();
  189. t->wd_hits++;
  190. }
  191. static inline void timer_watchdog_update(struct fs_timer_t *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[0]);
  207. t->wd_hits = 0;
  208. ptimer_set_freq(t->ptimer_wd, 760);
  209. if (wd_cnt == 0)
  210. wd_cnt = 256;
  211. ptimer_set_count(t->ptimer_wd, wd_cnt);
  212. if (new_cmd)
  213. ptimer_run(t->ptimer_wd, 1);
  214. else
  215. ptimer_stop(t->ptimer_wd);
  216. t->rw_wd_ctrl = value;
  217. }
  218. static void
  219. timer_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
  220. {
  221. struct fs_timer_t *t = opaque;
  222. switch (addr)
  223. {
  224. case RW_TMR0_DIV:
  225. t->rw_tmr0_div = value;
  226. break;
  227. case RW_TMR0_CTRL:
  228. D(printf ("RW_TMR0_CTRL=%x\n", value));
  229. t->rw_tmr0_ctrl = value;
  230. update_ctrl(t, 0);
  231. break;
  232. case RW_TMR1_DIV:
  233. t->rw_tmr1_div = value;
  234. break;
  235. case RW_TMR1_CTRL:
  236. D(printf ("RW_TMR1_CTRL=%x\n", value));
  237. t->rw_tmr1_ctrl = value;
  238. update_ctrl(t, 1);
  239. break;
  240. case RW_INTR_MASK:
  241. D(printf ("RW_INTR_MASK=%x\n", value));
  242. t->rw_intr_mask = value;
  243. timer_update_irq(t);
  244. break;
  245. case RW_WD_CTRL:
  246. timer_watchdog_update(t, value);
  247. break;
  248. case RW_ACK_INTR:
  249. t->rw_ack_intr = value;
  250. timer_update_irq(t);
  251. t->rw_ack_intr = 0;
  252. break;
  253. default:
  254. printf ("%s " TARGET_FMT_plx " %x\n",
  255. __func__, addr, value);
  256. break;
  257. }
  258. }
  259. static CPUReadMemoryFunc *timer_read[] = {
  260. NULL, NULL,
  261. &timer_readl,
  262. };
  263. static CPUWriteMemoryFunc *timer_write[] = {
  264. NULL, NULL,
  265. &timer_writel,
  266. };
  267. static void etraxfs_timer_reset(void *opaque)
  268. {
  269. struct fs_timer_t *t = opaque;
  270. ptimer_stop(t->ptimer_t0);
  271. ptimer_stop(t->ptimer_t1);
  272. ptimer_stop(t->ptimer_wd);
  273. t->rw_wd_ctrl = 0;
  274. t->r_intr = 0;
  275. t->rw_intr_mask = 0;
  276. qemu_irq_lower(t->irq[0]);
  277. }
  278. void etraxfs_timer_init(CPUState *env, qemu_irq *irqs, qemu_irq *nmi,
  279. target_phys_addr_t base)
  280. {
  281. static struct fs_timer_t *t;
  282. int timer_regs;
  283. t = qemu_mallocz(sizeof *t);
  284. t->bh_t0 = qemu_bh_new(timer0_hit, t);
  285. t->bh_t1 = qemu_bh_new(timer1_hit, t);
  286. t->bh_wd = qemu_bh_new(watchdog_hit, t);
  287. t->ptimer_t0 = ptimer_init(t->bh_t0);
  288. t->ptimer_t1 = ptimer_init(t->bh_t1);
  289. t->ptimer_wd = ptimer_init(t->bh_wd);
  290. t->irq = irqs;
  291. t->nmi = nmi;
  292. t->env = env;
  293. timer_regs = cpu_register_io_memory(0, timer_read, timer_write, t);
  294. cpu_register_physical_memory (base, 0x5c, timer_regs);
  295. qemu_register_reset(etraxfs_timer_reset, t);
  296. }