grlib_gptimer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * QEMU GRLIB GPTimer Emulator
  3. *
  4. * Copyright (c) 2010-2011 AdaCore
  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 "qemu/timer.h"
  26. #include "ptimer.h"
  27. #include "trace.h"
  28. #define UNIT_REG_SIZE 16 /* Size of memory mapped regs for the unit */
  29. #define GPTIMER_REG_SIZE 16 /* Size of memory mapped regs for a GPTimer */
  30. #define GPTIMER_MAX_TIMERS 8
  31. /* GPTimer Config register fields */
  32. #define GPTIMER_ENABLE (1 << 0)
  33. #define GPTIMER_RESTART (1 << 1)
  34. #define GPTIMER_LOAD (1 << 2)
  35. #define GPTIMER_INT_ENABLE (1 << 3)
  36. #define GPTIMER_INT_PENDING (1 << 4)
  37. #define GPTIMER_CHAIN (1 << 5) /* Not supported */
  38. #define GPTIMER_DEBUG_HALT (1 << 6) /* Not supported */
  39. /* Memory mapped register offsets */
  40. #define SCALER_OFFSET 0x00
  41. #define SCALER_RELOAD_OFFSET 0x04
  42. #define CONFIG_OFFSET 0x08
  43. #define COUNTER_OFFSET 0x00
  44. #define COUNTER_RELOAD_OFFSET 0x04
  45. #define TIMER_BASE 0x10
  46. typedef struct GPTimer GPTimer;
  47. typedef struct GPTimerUnit GPTimerUnit;
  48. struct GPTimer {
  49. QEMUBH *bh;
  50. struct ptimer_state *ptimer;
  51. qemu_irq irq;
  52. int id;
  53. GPTimerUnit *unit;
  54. /* registers */
  55. uint32_t counter;
  56. uint32_t reload;
  57. uint32_t config;
  58. };
  59. struct GPTimerUnit {
  60. SysBusDevice busdev;
  61. MemoryRegion iomem;
  62. uint32_t nr_timers; /* Number of timers available */
  63. uint32_t freq_hz; /* System frequency */
  64. uint32_t irq_line; /* Base irq line */
  65. GPTimer *timers;
  66. /* registers */
  67. uint32_t scaler;
  68. uint32_t reload;
  69. uint32_t config;
  70. };
  71. static void grlib_gptimer_enable(GPTimer *timer)
  72. {
  73. assert(timer != NULL);
  74. ptimer_stop(timer->ptimer);
  75. if (!(timer->config & GPTIMER_ENABLE)) {
  76. /* Timer disabled */
  77. trace_grlib_gptimer_disabled(timer->id, timer->config);
  78. return;
  79. }
  80. /* ptimer is triggered when the counter reach 0 but GPTimer is triggered at
  81. underflow. Set count + 1 to simulate the GPTimer behavior. */
  82. trace_grlib_gptimer_enable(timer->id, timer->counter + 1);
  83. ptimer_set_count(timer->ptimer, timer->counter + 1);
  84. ptimer_run(timer->ptimer, 1);
  85. }
  86. static void grlib_gptimer_restart(GPTimer *timer)
  87. {
  88. assert(timer != NULL);
  89. trace_grlib_gptimer_restart(timer->id, timer->reload);
  90. timer->counter = timer->reload;
  91. grlib_gptimer_enable(timer);
  92. }
  93. static void grlib_gptimer_set_scaler(GPTimerUnit *unit, uint32_t scaler)
  94. {
  95. int i = 0;
  96. uint32_t value = 0;
  97. assert(unit != NULL);
  98. if (scaler > 0) {
  99. value = unit->freq_hz / (scaler + 1);
  100. } else {
  101. value = unit->freq_hz;
  102. }
  103. trace_grlib_gptimer_set_scaler(scaler, value);
  104. for (i = 0; i < unit->nr_timers; i++) {
  105. ptimer_set_freq(unit->timers[i].ptimer, value);
  106. }
  107. }
  108. static void grlib_gptimer_hit(void *opaque)
  109. {
  110. GPTimer *timer = opaque;
  111. assert(timer != NULL);
  112. trace_grlib_gptimer_hit(timer->id);
  113. /* Timer expired */
  114. if (timer->config & GPTIMER_INT_ENABLE) {
  115. /* Set the pending bit (only unset by write in the config register) */
  116. timer->config |= GPTIMER_INT_PENDING;
  117. qemu_irq_pulse(timer->irq);
  118. }
  119. if (timer->config & GPTIMER_RESTART) {
  120. grlib_gptimer_restart(timer);
  121. }
  122. }
  123. static uint64_t grlib_gptimer_read(void *opaque, hwaddr addr,
  124. unsigned size)
  125. {
  126. GPTimerUnit *unit = opaque;
  127. hwaddr timer_addr;
  128. int id;
  129. uint32_t value = 0;
  130. addr &= 0xff;
  131. /* Unit registers */
  132. switch (addr) {
  133. case SCALER_OFFSET:
  134. trace_grlib_gptimer_readl(-1, addr, unit->scaler);
  135. return unit->scaler;
  136. case SCALER_RELOAD_OFFSET:
  137. trace_grlib_gptimer_readl(-1, addr, unit->reload);
  138. return unit->reload;
  139. case CONFIG_OFFSET:
  140. trace_grlib_gptimer_readl(-1, addr, unit->config);
  141. return unit->config;
  142. default:
  143. break;
  144. }
  145. timer_addr = (addr % TIMER_BASE);
  146. id = (addr - TIMER_BASE) / TIMER_BASE;
  147. if (id >= 0 && id < unit->nr_timers) {
  148. /* GPTimer registers */
  149. switch (timer_addr) {
  150. case COUNTER_OFFSET:
  151. value = ptimer_get_count(unit->timers[id].ptimer);
  152. trace_grlib_gptimer_readl(id, addr, value);
  153. return value;
  154. case COUNTER_RELOAD_OFFSET:
  155. value = unit->timers[id].reload;
  156. trace_grlib_gptimer_readl(id, addr, value);
  157. return value;
  158. case CONFIG_OFFSET:
  159. trace_grlib_gptimer_readl(id, addr, unit->timers[id].config);
  160. return unit->timers[id].config;
  161. default:
  162. break;
  163. }
  164. }
  165. trace_grlib_gptimer_readl(-1, addr, 0);
  166. return 0;
  167. }
  168. static void grlib_gptimer_write(void *opaque, hwaddr addr,
  169. uint64_t value, unsigned size)
  170. {
  171. GPTimerUnit *unit = opaque;
  172. hwaddr timer_addr;
  173. int id;
  174. addr &= 0xff;
  175. /* Unit registers */
  176. switch (addr) {
  177. case SCALER_OFFSET:
  178. value &= 0xFFFF; /* clean up the value */
  179. unit->scaler = value;
  180. trace_grlib_gptimer_writel(-1, addr, unit->scaler);
  181. return;
  182. case SCALER_RELOAD_OFFSET:
  183. value &= 0xFFFF; /* clean up the value */
  184. unit->reload = value;
  185. trace_grlib_gptimer_writel(-1, addr, unit->reload);
  186. grlib_gptimer_set_scaler(unit, value);
  187. return;
  188. case CONFIG_OFFSET:
  189. /* Read Only (disable timer freeze not supported) */
  190. trace_grlib_gptimer_writel(-1, addr, 0);
  191. return;
  192. default:
  193. break;
  194. }
  195. timer_addr = (addr % TIMER_BASE);
  196. id = (addr - TIMER_BASE) / TIMER_BASE;
  197. if (id >= 0 && id < unit->nr_timers) {
  198. /* GPTimer registers */
  199. switch (timer_addr) {
  200. case COUNTER_OFFSET:
  201. trace_grlib_gptimer_writel(id, addr, value);
  202. unit->timers[id].counter = value;
  203. grlib_gptimer_enable(&unit->timers[id]);
  204. return;
  205. case COUNTER_RELOAD_OFFSET:
  206. trace_grlib_gptimer_writel(id, addr, value);
  207. unit->timers[id].reload = value;
  208. return;
  209. case CONFIG_OFFSET:
  210. trace_grlib_gptimer_writel(id, addr, value);
  211. if (value & GPTIMER_INT_PENDING) {
  212. /* clear pending bit */
  213. value &= ~GPTIMER_INT_PENDING;
  214. } else {
  215. /* keep pending bit */
  216. value |= unit->timers[id].config & GPTIMER_INT_PENDING;
  217. }
  218. unit->timers[id].config = value;
  219. /* gptimer_restart calls gptimer_enable, so if "enable" and "load"
  220. bits are present, we just have to call restart. */
  221. if (value & GPTIMER_LOAD) {
  222. grlib_gptimer_restart(&unit->timers[id]);
  223. } else if (value & GPTIMER_ENABLE) {
  224. grlib_gptimer_enable(&unit->timers[id]);
  225. }
  226. /* These fields must always be read as 0 */
  227. value &= ~(GPTIMER_LOAD & GPTIMER_DEBUG_HALT);
  228. unit->timers[id].config = value;
  229. return;
  230. default:
  231. break;
  232. }
  233. }
  234. trace_grlib_gptimer_writel(-1, addr, value);
  235. }
  236. static const MemoryRegionOps grlib_gptimer_ops = {
  237. .read = grlib_gptimer_read,
  238. .write = grlib_gptimer_write,
  239. .endianness = DEVICE_NATIVE_ENDIAN,
  240. .valid = {
  241. .min_access_size = 4,
  242. .max_access_size = 4,
  243. },
  244. };
  245. static void grlib_gptimer_reset(DeviceState *d)
  246. {
  247. GPTimerUnit *unit = container_of(d, GPTimerUnit, busdev.qdev);
  248. int i = 0;
  249. assert(unit != NULL);
  250. unit->scaler = 0;
  251. unit->reload = 0;
  252. unit->config = 0;
  253. unit->config = unit->nr_timers;
  254. unit->config |= unit->irq_line << 3;
  255. unit->config |= 1 << 8; /* separate interrupt */
  256. unit->config |= 1 << 9; /* Disable timer freeze */
  257. for (i = 0; i < unit->nr_timers; i++) {
  258. GPTimer *timer = &unit->timers[i];
  259. timer->counter = 0;
  260. timer->reload = 0;
  261. timer->config = 0;
  262. ptimer_stop(timer->ptimer);
  263. ptimer_set_count(timer->ptimer, 0);
  264. ptimer_set_freq(timer->ptimer, unit->freq_hz);
  265. }
  266. }
  267. static int grlib_gptimer_init(SysBusDevice *dev)
  268. {
  269. GPTimerUnit *unit = FROM_SYSBUS(typeof(*unit), dev);
  270. unsigned int i;
  271. assert(unit->nr_timers > 0);
  272. assert(unit->nr_timers <= GPTIMER_MAX_TIMERS);
  273. unit->timers = g_malloc0(sizeof unit->timers[0] * unit->nr_timers);
  274. for (i = 0; i < unit->nr_timers; i++) {
  275. GPTimer *timer = &unit->timers[i];
  276. timer->unit = unit;
  277. timer->bh = qemu_bh_new(grlib_gptimer_hit, timer);
  278. timer->ptimer = ptimer_init(timer->bh);
  279. timer->id = i;
  280. /* One IRQ line for each timer */
  281. sysbus_init_irq(dev, &timer->irq);
  282. ptimer_set_freq(timer->ptimer, unit->freq_hz);
  283. }
  284. memory_region_init_io(&unit->iomem, &grlib_gptimer_ops, unit, "gptimer",
  285. UNIT_REG_SIZE + GPTIMER_REG_SIZE * unit->nr_timers);
  286. sysbus_init_mmio(dev, &unit->iomem);
  287. return 0;
  288. }
  289. static Property grlib_gptimer_properties[] = {
  290. DEFINE_PROP_UINT32("frequency", GPTimerUnit, freq_hz, 40000000),
  291. DEFINE_PROP_UINT32("irq-line", GPTimerUnit, irq_line, 8),
  292. DEFINE_PROP_UINT32("nr-timers", GPTimerUnit, nr_timers, 2),
  293. DEFINE_PROP_END_OF_LIST(),
  294. };
  295. static void grlib_gptimer_class_init(ObjectClass *klass, void *data)
  296. {
  297. DeviceClass *dc = DEVICE_CLASS(klass);
  298. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  299. k->init = grlib_gptimer_init;
  300. dc->reset = grlib_gptimer_reset;
  301. dc->props = grlib_gptimer_properties;
  302. }
  303. static const TypeInfo grlib_gptimer_info = {
  304. .name = "grlib,gptimer",
  305. .parent = TYPE_SYS_BUS_DEVICE,
  306. .instance_size = sizeof(GPTimerUnit),
  307. .class_init = grlib_gptimer_class_init,
  308. };
  309. static void grlib_gptimer_register_types(void)
  310. {
  311. type_register_static(&grlib_gptimer_info);
  312. }
  313. type_init(grlib_gptimer_register_types)