grlib_gptimer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * QEMU GRLIB GPTimer Emulator
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * Copyright (c) 2010-2024 AdaCore
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "hw/timer/grlib_gptimer.h"
  28. #include "hw/sysbus.h"
  29. #include "qemu/timer.h"
  30. #include "hw/irq.h"
  31. #include "hw/ptimer.h"
  32. #include "hw/qdev-properties.h"
  33. #include "qemu/module.h"
  34. #include "trace.h"
  35. #include "qom/object.h"
  36. #define UNIT_REG_SIZE 16 /* Size of memory mapped regs for the unit */
  37. #define GPTIMER_REG_SIZE 16 /* Size of memory mapped regs for a GPTimer */
  38. #define GPTIMER_MAX_TIMERS 8
  39. /* GPTimer Config register fields */
  40. #define GPTIMER_ENABLE (1 << 0)
  41. #define GPTIMER_RESTART (1 << 1)
  42. #define GPTIMER_LOAD (1 << 2)
  43. #define GPTIMER_INT_ENABLE (1 << 3)
  44. #define GPTIMER_INT_PENDING (1 << 4)
  45. #define GPTIMER_CHAIN (1 << 5) /* Not supported */
  46. #define GPTIMER_DEBUG_HALT (1 << 6) /* Not supported */
  47. /* Memory mapped register offsets */
  48. #define SCALER_OFFSET 0x00
  49. #define SCALER_RELOAD_OFFSET 0x04
  50. #define CONFIG_OFFSET 0x08
  51. #define COUNTER_OFFSET 0x00
  52. #define COUNTER_RELOAD_OFFSET 0x04
  53. #define TIMER_BASE 0x10
  54. OBJECT_DECLARE_SIMPLE_TYPE(GPTimerUnit, GRLIB_GPTIMER)
  55. typedef struct GPTimer GPTimer;
  56. struct GPTimer {
  57. struct ptimer_state *ptimer;
  58. qemu_irq irq;
  59. int id;
  60. GPTimerUnit *unit;
  61. /* registers */
  62. uint32_t counter;
  63. uint32_t reload;
  64. uint32_t config;
  65. };
  66. struct GPTimerUnit {
  67. SysBusDevice parent_obj;
  68. MemoryRegion iomem;
  69. uint32_t nr_timers; /* Number of timers available */
  70. uint32_t freq_hz; /* System frequency */
  71. uint32_t irq_line; /* Base irq line */
  72. GPTimer *timers;
  73. /* registers */
  74. uint32_t scaler;
  75. uint32_t reload;
  76. uint32_t config;
  77. };
  78. static void grlib_gptimer_tx_begin(GPTimer *timer)
  79. {
  80. ptimer_transaction_begin(timer->ptimer);
  81. }
  82. static void grlib_gptimer_tx_commit(GPTimer *timer)
  83. {
  84. ptimer_transaction_commit(timer->ptimer);
  85. }
  86. /* Must be called within grlib_gptimer_tx_begin/commit block */
  87. static void grlib_gptimer_enable(GPTimer *timer)
  88. {
  89. assert(timer != NULL);
  90. ptimer_stop(timer->ptimer);
  91. if (!(timer->config & GPTIMER_ENABLE)) {
  92. /* Timer disabled */
  93. trace_grlib_gptimer_disabled(timer->id, timer->config);
  94. return;
  95. }
  96. /* ptimer is triggered when the counter reach 0 but GPTimer is triggered at
  97. underflow. Set count + 1 to simulate the GPTimer behavior. */
  98. trace_grlib_gptimer_enable(timer->id, timer->counter);
  99. ptimer_set_count(timer->ptimer, (uint64_t)timer->counter + 1);
  100. ptimer_run(timer->ptimer, 1);
  101. }
  102. /* Must be called within grlib_gptimer_tx_begin/commit block */
  103. static void grlib_gptimer_restart(GPTimer *timer)
  104. {
  105. assert(timer != NULL);
  106. trace_grlib_gptimer_restart(timer->id, timer->reload);
  107. timer->counter = timer->reload;
  108. grlib_gptimer_enable(timer);
  109. }
  110. static void grlib_gptimer_set_scaler(GPTimerUnit *unit, uint32_t scaler)
  111. {
  112. int i = 0;
  113. uint32_t value = 0;
  114. assert(unit != NULL);
  115. if (scaler > 0) {
  116. value = unit->freq_hz / (scaler + 1);
  117. } else {
  118. value = unit->freq_hz;
  119. }
  120. trace_grlib_gptimer_set_scaler(scaler, value);
  121. for (i = 0; i < unit->nr_timers; i++) {
  122. ptimer_transaction_begin(unit->timers[i].ptimer);
  123. ptimer_set_freq(unit->timers[i].ptimer, value);
  124. ptimer_transaction_commit(unit->timers[i].ptimer);
  125. }
  126. }
  127. static void grlib_gptimer_hit(void *opaque)
  128. {
  129. GPTimer *timer = opaque;
  130. assert(timer != NULL);
  131. trace_grlib_gptimer_hit(timer->id);
  132. /* Timer expired */
  133. if (timer->config & GPTIMER_INT_ENABLE) {
  134. /* Set the pending bit (only unset by write in the config register) */
  135. timer->config |= GPTIMER_INT_PENDING;
  136. qemu_irq_pulse(timer->irq);
  137. }
  138. if (timer->config & GPTIMER_RESTART) {
  139. grlib_gptimer_restart(timer);
  140. }
  141. }
  142. static uint64_t grlib_gptimer_read(void *opaque, hwaddr addr,
  143. unsigned size)
  144. {
  145. GPTimerUnit *unit = opaque;
  146. hwaddr timer_addr;
  147. int id;
  148. uint32_t value = 0;
  149. addr &= 0xff;
  150. /* Unit registers */
  151. switch (addr) {
  152. case SCALER_OFFSET:
  153. trace_grlib_gptimer_readl(-1, addr, unit->scaler);
  154. return unit->scaler;
  155. case SCALER_RELOAD_OFFSET:
  156. trace_grlib_gptimer_readl(-1, addr, unit->reload);
  157. return unit->reload;
  158. case CONFIG_OFFSET:
  159. trace_grlib_gptimer_readl(-1, addr, unit->config);
  160. return unit->config;
  161. default:
  162. break;
  163. }
  164. timer_addr = (addr % TIMER_BASE);
  165. id = (addr - TIMER_BASE) / TIMER_BASE;
  166. if (id >= 0 && id < unit->nr_timers) {
  167. /* GPTimer registers */
  168. switch (timer_addr) {
  169. case COUNTER_OFFSET:
  170. value = ptimer_get_count(unit->timers[id].ptimer);
  171. trace_grlib_gptimer_readl(id, addr, value);
  172. return value;
  173. case COUNTER_RELOAD_OFFSET:
  174. value = unit->timers[id].reload;
  175. trace_grlib_gptimer_readl(id, addr, value);
  176. return value;
  177. case CONFIG_OFFSET:
  178. trace_grlib_gptimer_readl(id, addr, unit->timers[id].config);
  179. return unit->timers[id].config;
  180. default:
  181. break;
  182. }
  183. }
  184. trace_grlib_gptimer_readl(-1, addr, 0);
  185. return 0;
  186. }
  187. static void grlib_gptimer_write(void *opaque, hwaddr addr,
  188. uint64_t value, unsigned size)
  189. {
  190. GPTimerUnit *unit = opaque;
  191. hwaddr timer_addr;
  192. int id;
  193. addr &= 0xff;
  194. /* Unit registers */
  195. switch (addr) {
  196. case SCALER_OFFSET:
  197. value &= 0xFFFF; /* clean up the value */
  198. unit->scaler = value;
  199. trace_grlib_gptimer_writel(-1, addr, unit->scaler);
  200. return;
  201. case SCALER_RELOAD_OFFSET:
  202. value &= 0xFFFF; /* clean up the value */
  203. unit->reload = value;
  204. trace_grlib_gptimer_writel(-1, addr, unit->reload);
  205. grlib_gptimer_set_scaler(unit, value);
  206. return;
  207. case CONFIG_OFFSET:
  208. /* Read Only (disable timer freeze not supported) */
  209. trace_grlib_gptimer_writel(-1, addr, 0);
  210. return;
  211. default:
  212. break;
  213. }
  214. timer_addr = (addr % TIMER_BASE);
  215. id = (addr - TIMER_BASE) / TIMER_BASE;
  216. if (id >= 0 && id < unit->nr_timers) {
  217. /* GPTimer registers */
  218. switch (timer_addr) {
  219. case COUNTER_OFFSET:
  220. trace_grlib_gptimer_writel(id, addr, value);
  221. grlib_gptimer_tx_begin(&unit->timers[id]);
  222. unit->timers[id].counter = value;
  223. grlib_gptimer_enable(&unit->timers[id]);
  224. grlib_gptimer_tx_commit(&unit->timers[id]);
  225. return;
  226. case COUNTER_RELOAD_OFFSET:
  227. trace_grlib_gptimer_writel(id, addr, value);
  228. unit->timers[id].reload = value;
  229. return;
  230. case CONFIG_OFFSET:
  231. trace_grlib_gptimer_writel(id, addr, value);
  232. if (value & GPTIMER_INT_PENDING) {
  233. /* clear pending bit */
  234. value &= ~GPTIMER_INT_PENDING;
  235. } else {
  236. /* keep pending bit */
  237. value |= unit->timers[id].config & GPTIMER_INT_PENDING;
  238. }
  239. unit->timers[id].config = value;
  240. /* gptimer_restart calls gptimer_enable, so if "enable" and "load"
  241. bits are present, we just have to call restart. */
  242. grlib_gptimer_tx_begin(&unit->timers[id]);
  243. if (value & GPTIMER_LOAD) {
  244. grlib_gptimer_restart(&unit->timers[id]);
  245. } else if (value & GPTIMER_ENABLE) {
  246. grlib_gptimer_enable(&unit->timers[id]);
  247. }
  248. /* These fields must always be read as 0 */
  249. value &= ~(GPTIMER_LOAD & GPTIMER_DEBUG_HALT);
  250. unit->timers[id].config = value;
  251. grlib_gptimer_tx_commit(&unit->timers[id]);
  252. return;
  253. default:
  254. break;
  255. }
  256. }
  257. trace_grlib_gptimer_writel(-1, addr, value);
  258. }
  259. static const MemoryRegionOps grlib_gptimer_ops = {
  260. .read = grlib_gptimer_read,
  261. .write = grlib_gptimer_write,
  262. .endianness = DEVICE_NATIVE_ENDIAN,
  263. .valid = {
  264. .min_access_size = 4,
  265. .max_access_size = 4,
  266. },
  267. };
  268. static void grlib_gptimer_reset(DeviceState *d)
  269. {
  270. GPTimerUnit *unit = GRLIB_GPTIMER(d);
  271. int i = 0;
  272. assert(unit != NULL);
  273. unit->scaler = 0;
  274. unit->reload = 0;
  275. unit->config = unit->nr_timers;
  276. unit->config |= unit->irq_line << 3;
  277. unit->config |= 1 << 8; /* separate interrupt */
  278. unit->config |= 1 << 9; /* Disable timer freeze */
  279. for (i = 0; i < unit->nr_timers; i++) {
  280. GPTimer *timer = &unit->timers[i];
  281. timer->counter = 0;
  282. timer->reload = 0;
  283. timer->config = 0;
  284. ptimer_transaction_begin(timer->ptimer);
  285. ptimer_stop(timer->ptimer);
  286. ptimer_set_count(timer->ptimer, 0);
  287. ptimer_set_freq(timer->ptimer, unit->freq_hz);
  288. ptimer_transaction_commit(timer->ptimer);
  289. }
  290. }
  291. static void grlib_gptimer_realize(DeviceState *dev, Error **errp)
  292. {
  293. GPTimerUnit *unit = GRLIB_GPTIMER(dev);
  294. unsigned int i;
  295. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  296. assert(unit->nr_timers > 0);
  297. assert(unit->nr_timers <= GPTIMER_MAX_TIMERS);
  298. unit->timers = g_malloc0(sizeof unit->timers[0] * unit->nr_timers);
  299. for (i = 0; i < unit->nr_timers; i++) {
  300. GPTimer *timer = &unit->timers[i];
  301. timer->unit = unit;
  302. timer->ptimer = ptimer_init(grlib_gptimer_hit, timer,
  303. PTIMER_POLICY_LEGACY);
  304. timer->id = i;
  305. /* One IRQ line for each timer */
  306. sysbus_init_irq(sbd, &timer->irq);
  307. ptimer_transaction_begin(timer->ptimer);
  308. ptimer_set_freq(timer->ptimer, unit->freq_hz);
  309. ptimer_transaction_commit(timer->ptimer);
  310. }
  311. memory_region_init_io(&unit->iomem, OBJECT(unit), &grlib_gptimer_ops,
  312. unit, "gptimer",
  313. UNIT_REG_SIZE + GPTIMER_REG_SIZE * unit->nr_timers);
  314. sysbus_init_mmio(sbd, &unit->iomem);
  315. }
  316. static const Property grlib_gptimer_properties[] = {
  317. DEFINE_PROP_UINT32("frequency", GPTimerUnit, freq_hz, 40000000),
  318. DEFINE_PROP_UINT32("irq-line", GPTimerUnit, irq_line, 8),
  319. DEFINE_PROP_UINT32("nr-timers", GPTimerUnit, nr_timers, 2),
  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. device_class_set_legacy_reset(dc, 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)