etraxfs_timer.c 8.9 KB

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