renesas_cmt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Renesas 16bit Compare-match timer
  3. *
  4. * Datasheet: RX62N Group, RX621 Group User's Manual: Hardware
  5. * (Rev.1.40 R01UH0033EJ0140)
  6. *
  7. * Copyright (c) 2019 Yoshinori Sato
  8. *
  9. * SPDX-License-Identifier: GPL-2.0-or-later
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms and conditions of the GNU General Public License,
  13. * version 2 or later, as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with
  21. * this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include "qemu/osdep.h"
  24. #include "qemu/log.h"
  25. #include "hw/irq.h"
  26. #include "hw/registerfields.h"
  27. #include "hw/qdev-properties.h"
  28. #include "hw/timer/renesas_cmt.h"
  29. #include "migration/vmstate.h"
  30. /*
  31. * +0 CMSTR - common control
  32. * +2 CMCR - ch0
  33. * +4 CMCNT - ch0
  34. * +6 CMCOR - ch0
  35. * +8 CMCR - ch1
  36. * +10 CMCNT - ch1
  37. * +12 CMCOR - ch1
  38. * If we think that the address of CH 0 has an offset of +2,
  39. * we can treat it with the same address as CH 1, so define it like that.
  40. */
  41. REG16(CMSTR, 0)
  42. FIELD(CMSTR, STR0, 0, 1)
  43. FIELD(CMSTR, STR1, 1, 1)
  44. FIELD(CMSTR, STR, 0, 2)
  45. /* This addeess is channel offset */
  46. REG16(CMCR, 0)
  47. FIELD(CMCR, CKS, 0, 2)
  48. FIELD(CMCR, CMIE, 6, 1)
  49. REG16(CMCNT, 2)
  50. REG16(CMCOR, 4)
  51. static void update_events(RCMTState *cmt, int ch)
  52. {
  53. int64_t next_time;
  54. if ((cmt->cmstr & (1 << ch)) == 0) {
  55. /* count disable, so not happened next event. */
  56. return;
  57. }
  58. next_time = cmt->cmcor[ch] - cmt->cmcnt[ch];
  59. next_time *= NANOSECONDS_PER_SECOND;
  60. next_time /= cmt->input_freq;
  61. /*
  62. * CKS -> div rate
  63. * 0 -> 8 (1 << 3)
  64. * 1 -> 32 (1 << 5)
  65. * 2 -> 128 (1 << 7)
  66. * 3 -> 512 (1 << 9)
  67. */
  68. next_time *= 1 << (3 + FIELD_EX16(cmt->cmcr[ch], CMCR, CKS) * 2);
  69. next_time += qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  70. timer_mod(&cmt->timer[ch], next_time);
  71. }
  72. static int64_t read_cmcnt(RCMTState *cmt, int ch)
  73. {
  74. int64_t delta, now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  75. if (cmt->cmstr & (1 << ch)) {
  76. delta = (now - cmt->tick[ch]);
  77. delta /= NANOSECONDS_PER_SECOND;
  78. delta /= cmt->input_freq;
  79. delta /= 1 << (3 + FIELD_EX16(cmt->cmcr[ch], CMCR, CKS) * 2);
  80. cmt->tick[ch] = now;
  81. return cmt->cmcnt[ch] + delta;
  82. } else {
  83. return cmt->cmcnt[ch];
  84. }
  85. }
  86. static uint64_t cmt_read(void *opaque, hwaddr offset, unsigned size)
  87. {
  88. RCMTState *cmt = opaque;
  89. int ch = offset / 0x08;
  90. uint64_t ret;
  91. if (offset == A_CMSTR) {
  92. ret = 0;
  93. ret = FIELD_DP16(ret, CMSTR, STR,
  94. FIELD_EX16(cmt->cmstr, CMSTR, STR));
  95. return ret;
  96. } else {
  97. offset &= 0x07;
  98. if (ch == 0) {
  99. offset -= 0x02;
  100. }
  101. switch (offset) {
  102. case A_CMCR:
  103. ret = 0;
  104. ret = FIELD_DP16(ret, CMCR, CKS,
  105. FIELD_EX16(cmt->cmstr, CMCR, CKS));
  106. ret = FIELD_DP16(ret, CMCR, CMIE,
  107. FIELD_EX16(cmt->cmstr, CMCR, CMIE));
  108. return ret;
  109. case A_CMCNT:
  110. return read_cmcnt(cmt, ch);
  111. case A_CMCOR:
  112. return cmt->cmcor[ch];
  113. }
  114. }
  115. qemu_log_mask(LOG_UNIMP, "renesas_cmt: Register 0x%" HWADDR_PRIX " "
  116. "not implemented\n",
  117. offset);
  118. return UINT64_MAX;
  119. }
  120. static void start_stop(RCMTState *cmt, int ch, int st)
  121. {
  122. if (st) {
  123. update_events(cmt, ch);
  124. } else {
  125. timer_del(&cmt->timer[ch]);
  126. }
  127. }
  128. static void cmt_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
  129. {
  130. RCMTState *cmt = opaque;
  131. int ch = offset / 0x08;
  132. if (offset == A_CMSTR) {
  133. cmt->cmstr = FIELD_EX16(val, CMSTR, STR);
  134. start_stop(cmt, 0, FIELD_EX16(cmt->cmstr, CMSTR, STR0));
  135. start_stop(cmt, 1, FIELD_EX16(cmt->cmstr, CMSTR, STR1));
  136. } else {
  137. offset &= 0x07;
  138. if (ch == 0) {
  139. offset -= 0x02;
  140. }
  141. switch (offset) {
  142. case A_CMCR:
  143. cmt->cmcr[ch] = FIELD_DP16(cmt->cmcr[ch], CMCR, CKS,
  144. FIELD_EX16(val, CMCR, CKS));
  145. cmt->cmcr[ch] = FIELD_DP16(cmt->cmcr[ch], CMCR, CMIE,
  146. FIELD_EX16(val, CMCR, CMIE));
  147. break;
  148. case 2:
  149. cmt->cmcnt[ch] = val;
  150. break;
  151. case 4:
  152. cmt->cmcor[ch] = val;
  153. break;
  154. default:
  155. qemu_log_mask(LOG_UNIMP, "renesas_cmt: Register 0x%" HWADDR_PRIX " "
  156. "not implemented\n",
  157. offset);
  158. return;
  159. }
  160. if (FIELD_EX16(cmt->cmstr, CMSTR, STR) & (1 << ch)) {
  161. update_events(cmt, ch);
  162. }
  163. }
  164. }
  165. static const MemoryRegionOps cmt_ops = {
  166. .write = cmt_write,
  167. .read = cmt_read,
  168. .endianness = DEVICE_NATIVE_ENDIAN,
  169. .impl = {
  170. .min_access_size = 2,
  171. .max_access_size = 2,
  172. },
  173. .valid = {
  174. .min_access_size = 2,
  175. .max_access_size = 2,
  176. },
  177. };
  178. static void timer_events(RCMTState *cmt, int ch)
  179. {
  180. cmt->cmcnt[ch] = 0;
  181. cmt->tick[ch] = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  182. update_events(cmt, ch);
  183. if (FIELD_EX16(cmt->cmcr[ch], CMCR, CMIE)) {
  184. qemu_irq_pulse(cmt->cmi[ch]);
  185. }
  186. }
  187. static void timer_event0(void *opaque)
  188. {
  189. RCMTState *cmt = opaque;
  190. timer_events(cmt, 0);
  191. }
  192. static void timer_event1(void *opaque)
  193. {
  194. RCMTState *cmt = opaque;
  195. timer_events(cmt, 1);
  196. }
  197. static void rcmt_reset(DeviceState *dev)
  198. {
  199. RCMTState *cmt = RCMT(dev);
  200. cmt->cmstr = 0;
  201. cmt->cmcr[0] = cmt->cmcr[1] = 0;
  202. cmt->cmcnt[0] = cmt->cmcnt[1] = 0;
  203. cmt->cmcor[0] = cmt->cmcor[1] = 0xffff;
  204. }
  205. static void rcmt_init(Object *obj)
  206. {
  207. SysBusDevice *d = SYS_BUS_DEVICE(obj);
  208. RCMTState *cmt = RCMT(obj);
  209. int i;
  210. memory_region_init_io(&cmt->memory, OBJECT(cmt), &cmt_ops,
  211. cmt, "renesas-cmt", 0x10);
  212. sysbus_init_mmio(d, &cmt->memory);
  213. for (i = 0; i < ARRAY_SIZE(cmt->cmi); i++) {
  214. sysbus_init_irq(d, &cmt->cmi[i]);
  215. }
  216. timer_init_ns(&cmt->timer[0], QEMU_CLOCK_VIRTUAL, timer_event0, cmt);
  217. timer_init_ns(&cmt->timer[1], QEMU_CLOCK_VIRTUAL, timer_event1, cmt);
  218. }
  219. static const VMStateDescription vmstate_rcmt = {
  220. .name = "rx-cmt",
  221. .version_id = 1,
  222. .minimum_version_id = 1,
  223. .fields = (const VMStateField[]) {
  224. VMSTATE_UINT16(cmstr, RCMTState),
  225. VMSTATE_UINT16_ARRAY(cmcr, RCMTState, CMT_CH),
  226. VMSTATE_UINT16_ARRAY(cmcnt, RCMTState, CMT_CH),
  227. VMSTATE_UINT16_ARRAY(cmcor, RCMTState, CMT_CH),
  228. VMSTATE_INT64_ARRAY(tick, RCMTState, CMT_CH),
  229. VMSTATE_TIMER_ARRAY(timer, RCMTState, CMT_CH),
  230. VMSTATE_END_OF_LIST()
  231. }
  232. };
  233. static const Property rcmt_properties[] = {
  234. DEFINE_PROP_UINT64("input-freq", RCMTState, input_freq, 0),
  235. };
  236. static void rcmt_class_init(ObjectClass *klass, void *data)
  237. {
  238. DeviceClass *dc = DEVICE_CLASS(klass);
  239. dc->vmsd = &vmstate_rcmt;
  240. device_class_set_legacy_reset(dc, rcmt_reset);
  241. device_class_set_props(dc, rcmt_properties);
  242. }
  243. static const TypeInfo rcmt_info = {
  244. .name = TYPE_RENESAS_CMT,
  245. .parent = TYPE_SYS_BUS_DEVICE,
  246. .instance_size = sizeof(RCMTState),
  247. .instance_init = rcmt_init,
  248. .class_init = rcmt_class_init,
  249. };
  250. static void rcmt_register_types(void)
  251. {
  252. type_register_static(&rcmt_info);
  253. }
  254. type_init(rcmt_register_types)