2
0

grlib_gptimer.c 12 KB

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