mps2-fpgaio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * ARM MPS2 AN505 FPGAIO emulation
  3. *
  4. * Copyright (c) 2018 Linaro Limited
  5. * Written by Peter Maydell
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 or
  9. * (at your option) any later version.
  10. */
  11. /* This is a model of the "FPGA system control and I/O" block found
  12. * in the AN505 FPGA image for the MPS2 devboard.
  13. * It is documented in AN505:
  14. * https://developer.arm.com/documentation/dai0505/latest/
  15. */
  16. #include "qemu/osdep.h"
  17. #include "qemu/log.h"
  18. #include "qemu/module.h"
  19. #include "qapi/error.h"
  20. #include "trace.h"
  21. #include "hw/sysbus.h"
  22. #include "migration/vmstate.h"
  23. #include "hw/registerfields.h"
  24. #include "hw/misc/mps2-fpgaio.h"
  25. #include "hw/misc/led.h"
  26. #include "hw/qdev-properties.h"
  27. #include "qemu/timer.h"
  28. REG32(LED0, 0)
  29. REG32(DBGCTRL, 4)
  30. REG32(BUTTON, 8)
  31. REG32(CLK1HZ, 0x10)
  32. REG32(CLK100HZ, 0x14)
  33. REG32(COUNTER, 0x18)
  34. REG32(PRESCALE, 0x1c)
  35. REG32(PSCNTR, 0x20)
  36. REG32(SWITCH, 0x28)
  37. REG32(MISC, 0x4c)
  38. static uint32_t counter_from_tickoff(int64_t now, int64_t tick_offset, int frq)
  39. {
  40. return muldiv64(now - tick_offset, frq, NANOSECONDS_PER_SECOND);
  41. }
  42. static int64_t tickoff_from_counter(int64_t now, uint32_t count, int frq)
  43. {
  44. return now - muldiv64(count, NANOSECONDS_PER_SECOND, frq);
  45. }
  46. static void resync_counter(MPS2FPGAIO *s)
  47. {
  48. /*
  49. * Update s->counter and s->pscntr to their true current values
  50. * by calculating how many times PSCNTR has ticked since the
  51. * last time we did a resync.
  52. */
  53. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  54. int64_t elapsed = now - s->pscntr_sync_ticks;
  55. /*
  56. * Round elapsed down to a whole number of PSCNTR ticks, so we don't
  57. * lose time if we do multiple resyncs in a single tick.
  58. */
  59. uint64_t ticks = muldiv64(elapsed, s->prescale_clk, NANOSECONDS_PER_SECOND);
  60. /*
  61. * Work out what PSCNTR and COUNTER have moved to. We assume that
  62. * PSCNTR reloads from PRESCALE one tick-period after it hits zero,
  63. * and that COUNTER increments at the same moment.
  64. */
  65. if (ticks == 0) {
  66. /* We haven't ticked since the last time we were asked */
  67. return;
  68. } else if (ticks < s->pscntr) {
  69. /* We haven't yet reached zero, just reduce the PSCNTR */
  70. s->pscntr -= ticks;
  71. } else {
  72. if (s->prescale == 0) {
  73. /*
  74. * If the reload value is zero then the PSCNTR will stick
  75. * at zero once it reaches it, and so we will increment
  76. * COUNTER every tick after that.
  77. */
  78. s->counter += ticks - s->pscntr;
  79. s->pscntr = 0;
  80. } else {
  81. /*
  82. * This is the complicated bit. This ASCII art diagram gives an
  83. * example with PRESCALE==5 PSCNTR==7:
  84. *
  85. * ticks 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  86. * PSCNTR 7 6 5 4 3 2 1 0 5 4 3 2 1 0 5
  87. * cinc 1 2
  88. * y 0 1 2 3 4 5 6 7 8 9 10 11 12
  89. * x 0 1 2 3 4 5 0 1 2 3 4 5 0
  90. *
  91. * where x = y % (s->prescale + 1)
  92. * and so PSCNTR = s->prescale - x
  93. * and COUNTER is incremented by y / (s->prescale + 1)
  94. *
  95. * The case where PSCNTR < PRESCALE works out the same,
  96. * though we must be careful to calculate y as 64-bit unsigned
  97. * for all parts of the expression.
  98. * y < 0 is not possible because that implies ticks < s->pscntr.
  99. */
  100. uint64_t y = ticks - s->pscntr + s->prescale;
  101. s->pscntr = s->prescale - (y % (s->prescale + 1));
  102. s->counter += y / (s->prescale + 1);
  103. }
  104. }
  105. /*
  106. * Only advance the sync time to the timestamp of the last PSCNTR tick,
  107. * not all the way to 'now', so we don't lose time if we do multiple
  108. * resyncs in a single tick.
  109. */
  110. s->pscntr_sync_ticks += muldiv64(ticks, NANOSECONDS_PER_SECOND,
  111. s->prescale_clk);
  112. }
  113. static uint64_t mps2_fpgaio_read(void *opaque, hwaddr offset, unsigned size)
  114. {
  115. MPS2FPGAIO *s = MPS2_FPGAIO(opaque);
  116. uint64_t r;
  117. int64_t now;
  118. switch (offset) {
  119. case A_LED0:
  120. r = s->led0;
  121. break;
  122. case A_DBGCTRL:
  123. if (!s->has_dbgctrl) {
  124. goto bad_offset;
  125. }
  126. r = s->dbgctrl;
  127. break;
  128. case A_BUTTON:
  129. /* User-pressable board buttons. We don't model that, so just return
  130. * zeroes.
  131. */
  132. r = 0;
  133. break;
  134. case A_PRESCALE:
  135. r = s->prescale;
  136. break;
  137. case A_MISC:
  138. r = s->misc;
  139. break;
  140. case A_CLK1HZ:
  141. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  142. r = counter_from_tickoff(now, s->clk1hz_tick_offset, 1);
  143. break;
  144. case A_CLK100HZ:
  145. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  146. r = counter_from_tickoff(now, s->clk100hz_tick_offset, 100);
  147. break;
  148. case A_COUNTER:
  149. resync_counter(s);
  150. r = s->counter;
  151. break;
  152. case A_PSCNTR:
  153. resync_counter(s);
  154. r = s->pscntr;
  155. break;
  156. case A_SWITCH:
  157. if (!s->has_switches) {
  158. goto bad_offset;
  159. }
  160. /* User-togglable board switches. We don't model that, so report 0. */
  161. r = 0;
  162. break;
  163. default:
  164. bad_offset:
  165. qemu_log_mask(LOG_GUEST_ERROR,
  166. "MPS2 FPGAIO read: bad offset %x\n", (int) offset);
  167. r = 0;
  168. break;
  169. }
  170. trace_mps2_fpgaio_read(offset, r, size);
  171. return r;
  172. }
  173. static void mps2_fpgaio_write(void *opaque, hwaddr offset, uint64_t value,
  174. unsigned size)
  175. {
  176. MPS2FPGAIO *s = MPS2_FPGAIO(opaque);
  177. int64_t now;
  178. trace_mps2_fpgaio_write(offset, value, size);
  179. switch (offset) {
  180. case A_LED0:
  181. if (s->num_leds != 0) {
  182. uint32_t i;
  183. s->led0 = value & MAKE_64BIT_MASK(0, s->num_leds);
  184. for (i = 0; i < s->num_leds; i++) {
  185. led_set_state(s->led[i], value & (1 << i));
  186. }
  187. }
  188. break;
  189. case A_DBGCTRL:
  190. if (!s->has_dbgctrl) {
  191. goto bad_offset;
  192. }
  193. qemu_log_mask(LOG_UNIMP,
  194. "MPS2 FPGAIO: DBGCTRL unimplemented\n");
  195. s->dbgctrl = value;
  196. break;
  197. case A_PRESCALE:
  198. resync_counter(s);
  199. s->prescale = value;
  200. break;
  201. case A_MISC:
  202. /* These are control bits for some of the other devices on the
  203. * board (SPI, CLCD, etc). We don't implement that yet, so just
  204. * make the bits read as written.
  205. */
  206. qemu_log_mask(LOG_UNIMP,
  207. "MPS2 FPGAIO: MISC control bits unimplemented\n");
  208. s->misc = value;
  209. break;
  210. case A_CLK1HZ:
  211. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  212. s->clk1hz_tick_offset = tickoff_from_counter(now, value, 1);
  213. break;
  214. case A_CLK100HZ:
  215. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  216. s->clk100hz_tick_offset = tickoff_from_counter(now, value, 100);
  217. break;
  218. case A_COUNTER:
  219. resync_counter(s);
  220. s->counter = value;
  221. break;
  222. case A_PSCNTR:
  223. resync_counter(s);
  224. s->pscntr = value;
  225. break;
  226. default:
  227. bad_offset:
  228. qemu_log_mask(LOG_GUEST_ERROR,
  229. "MPS2 FPGAIO write: bad offset 0x%x\n", (int) offset);
  230. break;
  231. }
  232. }
  233. static const MemoryRegionOps mps2_fpgaio_ops = {
  234. .read = mps2_fpgaio_read,
  235. .write = mps2_fpgaio_write,
  236. .endianness = DEVICE_LITTLE_ENDIAN,
  237. };
  238. static void mps2_fpgaio_reset(DeviceState *dev)
  239. {
  240. MPS2FPGAIO *s = MPS2_FPGAIO(dev);
  241. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  242. trace_mps2_fpgaio_reset();
  243. s->led0 = 0;
  244. s->prescale = 0;
  245. s->misc = 0;
  246. s->clk1hz_tick_offset = tickoff_from_counter(now, 0, 1);
  247. s->clk100hz_tick_offset = tickoff_from_counter(now, 0, 100);
  248. s->counter = 0;
  249. s->pscntr = 0;
  250. s->pscntr_sync_ticks = now;
  251. for (size_t i = 0; i < s->num_leds; i++) {
  252. device_cold_reset(DEVICE(s->led[i]));
  253. }
  254. }
  255. static void mps2_fpgaio_init(Object *obj)
  256. {
  257. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  258. MPS2FPGAIO *s = MPS2_FPGAIO(obj);
  259. memory_region_init_io(&s->iomem, obj, &mps2_fpgaio_ops, s,
  260. "mps2-fpgaio", 0x1000);
  261. sysbus_init_mmio(sbd, &s->iomem);
  262. }
  263. static void mps2_fpgaio_realize(DeviceState *dev, Error **errp)
  264. {
  265. MPS2FPGAIO *s = MPS2_FPGAIO(dev);
  266. uint32_t i;
  267. if (s->num_leds > MPS2FPGAIO_MAX_LEDS) {
  268. error_setg(errp, "num-leds cannot be greater than %d",
  269. MPS2FPGAIO_MAX_LEDS);
  270. return;
  271. }
  272. for (i = 0; i < s->num_leds; i++) {
  273. g_autofree char *ledname = g_strdup_printf("USERLED%d", i);
  274. s->led[i] = led_create_simple(OBJECT(dev), GPIO_POLARITY_ACTIVE_HIGH,
  275. LED_COLOR_GREEN, ledname);
  276. }
  277. }
  278. static const VMStateDescription mps2_fpgaio_vmstate = {
  279. .name = "mps2-fpgaio",
  280. .version_id = 3,
  281. .minimum_version_id = 3,
  282. .fields = (const VMStateField[]) {
  283. VMSTATE_UINT32(led0, MPS2FPGAIO),
  284. VMSTATE_UINT32(prescale, MPS2FPGAIO),
  285. VMSTATE_UINT32(misc, MPS2FPGAIO),
  286. VMSTATE_UINT32(dbgctrl, MPS2FPGAIO),
  287. VMSTATE_INT64(clk1hz_tick_offset, MPS2FPGAIO),
  288. VMSTATE_INT64(clk100hz_tick_offset, MPS2FPGAIO),
  289. VMSTATE_UINT32(counter, MPS2FPGAIO),
  290. VMSTATE_UINT32(pscntr, MPS2FPGAIO),
  291. VMSTATE_INT64(pscntr_sync_ticks, MPS2FPGAIO),
  292. VMSTATE_END_OF_LIST()
  293. },
  294. };
  295. static Property mps2_fpgaio_properties[] = {
  296. /* Frequency of the prescale counter */
  297. DEFINE_PROP_UINT32("prescale-clk", MPS2FPGAIO, prescale_clk, 20000000),
  298. /* Number of LEDs controlled by LED0 register */
  299. DEFINE_PROP_UINT32("num-leds", MPS2FPGAIO, num_leds, 2),
  300. DEFINE_PROP_BOOL("has-switches", MPS2FPGAIO, has_switches, false),
  301. DEFINE_PROP_BOOL("has-dbgctrl", MPS2FPGAIO, has_dbgctrl, false),
  302. DEFINE_PROP_END_OF_LIST(),
  303. };
  304. static void mps2_fpgaio_class_init(ObjectClass *klass, void *data)
  305. {
  306. DeviceClass *dc = DEVICE_CLASS(klass);
  307. dc->vmsd = &mps2_fpgaio_vmstate;
  308. dc->realize = mps2_fpgaio_realize;
  309. dc->reset = mps2_fpgaio_reset;
  310. device_class_set_props(dc, mps2_fpgaio_properties);
  311. }
  312. static const TypeInfo mps2_fpgaio_info = {
  313. .name = TYPE_MPS2_FPGAIO,
  314. .parent = TYPE_SYS_BUS_DEVICE,
  315. .instance_size = sizeof(MPS2FPGAIO),
  316. .instance_init = mps2_fpgaio_init,
  317. .class_init = mps2_fpgaio_class_init,
  318. };
  319. static void mps2_fpgaio_register_types(void)
  320. {
  321. type_register_static(&mps2_fpgaio_info);
  322. }
  323. type_init(mps2_fpgaio_register_types);