2
0

grlib_gptimer.c 11 KB

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