2
0

etraxfs_timer.c 8.7 KB

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