grlib_gptimer.c 11 KB

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