grlib_gptimer.c 12 KB

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