renesas_tmr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Renesas 8bit 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_tmr.h"
  29. #include "migration/vmstate.h"
  30. REG8(TCR, 0)
  31. FIELD(TCR, CCLR, 3, 2)
  32. FIELD(TCR, OVIE, 5, 1)
  33. FIELD(TCR, CMIEA, 6, 1)
  34. FIELD(TCR, CMIEB, 7, 1)
  35. REG8(TCSR, 2)
  36. FIELD(TCSR, OSA, 0, 2)
  37. FIELD(TCSR, OSB, 2, 2)
  38. FIELD(TCSR, ADTE, 4, 2)
  39. REG8(TCORA, 4)
  40. REG8(TCORB, 6)
  41. REG8(TCNT, 8)
  42. REG8(TCCR, 10)
  43. FIELD(TCCR, CKS, 0, 3)
  44. FIELD(TCCR, CSS, 3, 2)
  45. FIELD(TCCR, TMRIS, 7, 1)
  46. #define INTERNAL 0x01
  47. #define CASCADING 0x03
  48. #define CCLR_A 0x01
  49. #define CCLR_B 0x02
  50. static const int clkdiv[] = {0, 1, 2, 8, 32, 64, 1024, 8192};
  51. static uint8_t concat_reg(uint8_t *reg)
  52. {
  53. return (reg[0] << 8) | reg[1];
  54. }
  55. static void update_events(RTMRState *tmr, int ch)
  56. {
  57. uint16_t diff[TMR_NR_EVENTS], min;
  58. int64_t next_time;
  59. int i, event;
  60. if (tmr->tccr[ch] == 0) {
  61. return ;
  62. }
  63. if (FIELD_EX8(tmr->tccr[ch], TCCR, CSS) == 0) {
  64. /* external clock mode */
  65. /* event not happened */
  66. return ;
  67. }
  68. if (FIELD_EX8(tmr->tccr[0], TCCR, CSS) == CASCADING) {
  69. /* cascading mode */
  70. if (ch == 1) {
  71. tmr->next[ch] = none;
  72. return ;
  73. }
  74. diff[cmia] = concat_reg(tmr->tcora) - concat_reg(tmr->tcnt);
  75. diff[cmib] = concat_reg(tmr->tcorb) - concat_reg(tmr->tcnt);
  76. diff[ovi] = 0x10000 - concat_reg(tmr->tcnt);
  77. } else {
  78. /* separate mode */
  79. diff[cmia] = tmr->tcora[ch] - tmr->tcnt[ch];
  80. diff[cmib] = tmr->tcorb[ch] - tmr->tcnt[ch];
  81. diff[ovi] = 0x100 - tmr->tcnt[ch];
  82. }
  83. /* Search for the most recently occurring event. */
  84. for (event = 0, min = diff[0], i = 1; i < none; i++) {
  85. if (min > diff[i]) {
  86. event = i;
  87. min = diff[i];
  88. }
  89. }
  90. tmr->next[ch] = event;
  91. next_time = diff[event];
  92. next_time *= clkdiv[FIELD_EX8(tmr->tccr[ch], TCCR, CKS)];
  93. next_time *= NANOSECONDS_PER_SECOND;
  94. next_time /= tmr->input_freq;
  95. next_time += qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  96. timer_mod(&tmr->timer[ch], next_time);
  97. }
  98. static int elapsed_time(RTMRState *tmr, int ch, int64_t delta)
  99. {
  100. int divrate = clkdiv[FIELD_EX8(tmr->tccr[ch], TCCR, CKS)];
  101. int et;
  102. tmr->div_round[ch] += delta;
  103. if (divrate > 0) {
  104. et = tmr->div_round[ch] / divrate;
  105. tmr->div_round[ch] %= divrate;
  106. } else {
  107. /* disble clock. so no update */
  108. et = 0;
  109. }
  110. return et;
  111. }
  112. static uint16_t read_tcnt(RTMRState *tmr, unsigned size, int ch)
  113. {
  114. int64_t delta, now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  115. int elapsed, ovf = 0;
  116. uint16_t tcnt[2];
  117. uint32_t ret;
  118. delta = (now - tmr->tick) * NANOSECONDS_PER_SECOND / tmr->input_freq;
  119. if (delta > 0) {
  120. tmr->tick = now;
  121. if (FIELD_EX8(tmr->tccr[1], TCCR, CSS) == INTERNAL) {
  122. /* timer1 count update */
  123. elapsed = elapsed_time(tmr, 1, delta);
  124. if (elapsed >= 0x100) {
  125. ovf = elapsed >> 8;
  126. }
  127. tcnt[1] = tmr->tcnt[1] + (elapsed & 0xff);
  128. }
  129. switch (FIELD_EX8(tmr->tccr[0], TCCR, CSS)) {
  130. case INTERNAL:
  131. elapsed = elapsed_time(tmr, 0, delta);
  132. tcnt[0] = tmr->tcnt[0] + elapsed;
  133. break;
  134. case CASCADING:
  135. if (ovf > 0) {
  136. tcnt[0] = tmr->tcnt[0] + ovf;
  137. }
  138. break;
  139. }
  140. } else {
  141. tcnt[0] = tmr->tcnt[0];
  142. tcnt[1] = tmr->tcnt[1];
  143. }
  144. if (size == 1) {
  145. return tcnt[ch];
  146. } else {
  147. ret = 0;
  148. ret = deposit32(ret, 0, 8, tcnt[1]);
  149. ret = deposit32(ret, 8, 8, tcnt[0]);
  150. return ret;
  151. }
  152. }
  153. static uint8_t read_tccr(uint8_t r)
  154. {
  155. uint8_t tccr = 0;
  156. tccr = FIELD_DP8(tccr, TCCR, TMRIS,
  157. FIELD_EX8(r, TCCR, TMRIS));
  158. tccr = FIELD_DP8(tccr, TCCR, CSS,
  159. FIELD_EX8(r, TCCR, CSS));
  160. tccr = FIELD_DP8(tccr, TCCR, CKS,
  161. FIELD_EX8(r, TCCR, CKS));
  162. return tccr;
  163. }
  164. static uint64_t tmr_read(void *opaque, hwaddr addr, unsigned size)
  165. {
  166. RTMRState *tmr = opaque;
  167. int ch = addr & 1;
  168. uint64_t ret;
  169. if (size == 2 && (ch != 0 || addr == A_TCR || addr == A_TCSR)) {
  170. qemu_log_mask(LOG_GUEST_ERROR, "renesas_tmr: Invalid read size 0x%"
  171. HWADDR_PRIX "\n",
  172. addr);
  173. return UINT64_MAX;
  174. }
  175. switch (addr & 0x0e) {
  176. case A_TCR:
  177. ret = 0;
  178. ret = FIELD_DP8(ret, TCR, CCLR,
  179. FIELD_EX8(tmr->tcr[ch], TCR, CCLR));
  180. ret = FIELD_DP8(ret, TCR, OVIE,
  181. FIELD_EX8(tmr->tcr[ch], TCR, OVIE));
  182. ret = FIELD_DP8(ret, TCR, CMIEA,
  183. FIELD_EX8(tmr->tcr[ch], TCR, CMIEA));
  184. ret = FIELD_DP8(ret, TCR, CMIEB,
  185. FIELD_EX8(tmr->tcr[ch], TCR, CMIEB));
  186. return ret;
  187. case A_TCSR:
  188. ret = 0;
  189. ret = FIELD_DP8(ret, TCSR, OSA,
  190. FIELD_EX8(tmr->tcsr[ch], TCSR, OSA));
  191. ret = FIELD_DP8(ret, TCSR, OSB,
  192. FIELD_EX8(tmr->tcsr[ch], TCSR, OSB));
  193. switch (ch) {
  194. case 0:
  195. ret = FIELD_DP8(ret, TCSR, ADTE,
  196. FIELD_EX8(tmr->tcsr[ch], TCSR, ADTE));
  197. break;
  198. case 1: /* CH1 ADTE unimplement always 1 */
  199. ret = FIELD_DP8(ret, TCSR, ADTE, 1);
  200. break;
  201. }
  202. return ret;
  203. case A_TCORA:
  204. if (size == 1) {
  205. return tmr->tcora[ch];
  206. } else if (ch == 0) {
  207. return concat_reg(tmr->tcora);
  208. }
  209. case A_TCORB:
  210. if (size == 1) {
  211. return tmr->tcorb[ch];
  212. } else {
  213. return concat_reg(tmr->tcorb);
  214. }
  215. case A_TCNT:
  216. return read_tcnt(tmr, size, ch);
  217. case A_TCCR:
  218. if (size == 1) {
  219. return read_tccr(tmr->tccr[ch]);
  220. } else {
  221. return read_tccr(tmr->tccr[0]) << 8 | read_tccr(tmr->tccr[1]);
  222. }
  223. default:
  224. qemu_log_mask(LOG_UNIMP, "renesas_tmr: Register 0x%" HWADDR_PRIX
  225. " not implemented\n",
  226. addr);
  227. break;
  228. }
  229. return UINT64_MAX;
  230. }
  231. static void tmr_write_count(RTMRState *tmr, int ch, unsigned size,
  232. uint8_t *reg, uint64_t val)
  233. {
  234. if (size == 1) {
  235. reg[ch] = val;
  236. update_events(tmr, ch);
  237. } else {
  238. reg[0] = extract32(val, 8, 8);
  239. reg[1] = extract32(val, 0, 8);
  240. update_events(tmr, 0);
  241. update_events(tmr, 1);
  242. }
  243. }
  244. static void tmr_write(void *opaque, hwaddr addr, uint64_t val, unsigned size)
  245. {
  246. RTMRState *tmr = opaque;
  247. int ch = addr & 1;
  248. if (size == 2 && (ch != 0 || addr == A_TCR || addr == A_TCSR)) {
  249. qemu_log_mask(LOG_GUEST_ERROR,
  250. "renesas_tmr: Invalid write size 0x%" HWADDR_PRIX "\n",
  251. addr);
  252. return;
  253. }
  254. switch (addr & 0x0e) {
  255. case A_TCR:
  256. tmr->tcr[ch] = val;
  257. break;
  258. case A_TCSR:
  259. tmr->tcsr[ch] = val;
  260. break;
  261. case A_TCORA:
  262. tmr_write_count(tmr, ch, size, tmr->tcora, val);
  263. break;
  264. case A_TCORB:
  265. tmr_write_count(tmr, ch, size, tmr->tcorb, val);
  266. break;
  267. case A_TCNT:
  268. tmr_write_count(tmr, ch, size, tmr->tcnt, val);
  269. break;
  270. case A_TCCR:
  271. tmr_write_count(tmr, ch, size, tmr->tccr, val);
  272. break;
  273. default:
  274. qemu_log_mask(LOG_UNIMP, "renesas_tmr: Register 0x%" HWADDR_PRIX
  275. " not implemented\n",
  276. addr);
  277. break;
  278. }
  279. }
  280. static const MemoryRegionOps tmr_ops = {
  281. .write = tmr_write,
  282. .read = tmr_read,
  283. .endianness = DEVICE_LITTLE_ENDIAN,
  284. .impl = {
  285. .min_access_size = 1,
  286. .max_access_size = 2,
  287. },
  288. .valid = {
  289. .min_access_size = 1,
  290. .max_access_size = 2,
  291. },
  292. };
  293. static void timer_events(RTMRState *tmr, int ch);
  294. static uint16_t issue_event(RTMRState *tmr, int ch, int sz,
  295. uint16_t tcnt, uint16_t tcora, uint16_t tcorb)
  296. {
  297. uint16_t ret = tcnt;
  298. switch (tmr->next[ch]) {
  299. case none:
  300. break;
  301. case cmia:
  302. if (tcnt >= tcora) {
  303. if (FIELD_EX8(tmr->tcr[ch], TCR, CCLR) == CCLR_A) {
  304. ret = tcnt - tcora;
  305. }
  306. if (FIELD_EX8(tmr->tcr[ch], TCR, CMIEA)) {
  307. qemu_irq_pulse(tmr->cmia[ch]);
  308. }
  309. if (sz == 8 && ch == 0 &&
  310. FIELD_EX8(tmr->tccr[1], TCCR, CSS) == CASCADING) {
  311. tmr->tcnt[1]++;
  312. timer_events(tmr, 1);
  313. }
  314. }
  315. break;
  316. case cmib:
  317. if (tcnt >= tcorb) {
  318. if (FIELD_EX8(tmr->tcr[ch], TCR, CCLR) == CCLR_B) {
  319. ret = tcnt - tcorb;
  320. }
  321. if (FIELD_EX8(tmr->tcr[ch], TCR, CMIEB)) {
  322. qemu_irq_pulse(tmr->cmib[ch]);
  323. }
  324. }
  325. break;
  326. case ovi:
  327. if ((tcnt >= (1 << sz)) && FIELD_EX8(tmr->tcr[ch], TCR, OVIE)) {
  328. qemu_irq_pulse(tmr->ovi[ch]);
  329. }
  330. break;
  331. default:
  332. g_assert_not_reached();
  333. }
  334. return ret;
  335. }
  336. static void timer_events(RTMRState *tmr, int ch)
  337. {
  338. uint16_t tcnt;
  339. tmr->tcnt[ch] = read_tcnt(tmr, 1, ch);
  340. if (FIELD_EX8(tmr->tccr[0], TCCR, CSS) != CASCADING) {
  341. tmr->tcnt[ch] = issue_event(tmr, ch, 8,
  342. tmr->tcnt[ch],
  343. tmr->tcora[ch],
  344. tmr->tcorb[ch]) & 0xff;
  345. } else {
  346. if (ch == 1) {
  347. return ;
  348. }
  349. tcnt = issue_event(tmr, ch, 16,
  350. concat_reg(tmr->tcnt),
  351. concat_reg(tmr->tcora),
  352. concat_reg(tmr->tcorb));
  353. tmr->tcnt[0] = (tcnt >> 8) & 0xff;
  354. tmr->tcnt[1] = tcnt & 0xff;
  355. }
  356. update_events(tmr, ch);
  357. }
  358. static void timer_event0(void *opaque)
  359. {
  360. RTMRState *tmr = opaque;
  361. timer_events(tmr, 0);
  362. }
  363. static void timer_event1(void *opaque)
  364. {
  365. RTMRState *tmr = opaque;
  366. timer_events(tmr, 1);
  367. }
  368. static void rtmr_reset(DeviceState *dev)
  369. {
  370. RTMRState *tmr = RTMR(dev);
  371. tmr->tcr[0] = tmr->tcr[1] = 0x00;
  372. tmr->tcsr[0] = 0x00;
  373. tmr->tcsr[1] = 0x10;
  374. tmr->tcnt[0] = tmr->tcnt[1] = 0x00;
  375. tmr->tcora[0] = tmr->tcora[1] = 0xff;
  376. tmr->tcorb[0] = tmr->tcorb[1] = 0xff;
  377. tmr->tccr[0] = tmr->tccr[1] = 0x00;
  378. tmr->next[0] = tmr->next[1] = none;
  379. tmr->tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  380. }
  381. static void rtmr_init(Object *obj)
  382. {
  383. SysBusDevice *d = SYS_BUS_DEVICE(obj);
  384. RTMRState *tmr = RTMR(obj);
  385. int i;
  386. memory_region_init_io(&tmr->memory, OBJECT(tmr), &tmr_ops,
  387. tmr, "renesas-tmr", 0x10);
  388. sysbus_init_mmio(d, &tmr->memory);
  389. for (i = 0; i < ARRAY_SIZE(tmr->ovi); i++) {
  390. sysbus_init_irq(d, &tmr->cmia[i]);
  391. sysbus_init_irq(d, &tmr->cmib[i]);
  392. sysbus_init_irq(d, &tmr->ovi[i]);
  393. }
  394. timer_init_ns(&tmr->timer[0], QEMU_CLOCK_VIRTUAL, timer_event0, tmr);
  395. timer_init_ns(&tmr->timer[1], QEMU_CLOCK_VIRTUAL, timer_event1, tmr);
  396. }
  397. static const VMStateDescription vmstate_rtmr = {
  398. .name = "rx-tmr",
  399. .version_id = 1,
  400. .minimum_version_id = 1,
  401. .fields = (VMStateField[]) {
  402. VMSTATE_INT64(tick, RTMRState),
  403. VMSTATE_UINT8_ARRAY(tcnt, RTMRState, TMR_CH),
  404. VMSTATE_UINT8_ARRAY(tcora, RTMRState, TMR_CH),
  405. VMSTATE_UINT8_ARRAY(tcorb, RTMRState, TMR_CH),
  406. VMSTATE_UINT8_ARRAY(tcr, RTMRState, TMR_CH),
  407. VMSTATE_UINT8_ARRAY(tccr, RTMRState, TMR_CH),
  408. VMSTATE_UINT8_ARRAY(tcor, RTMRState, TMR_CH),
  409. VMSTATE_UINT8_ARRAY(tcsr, RTMRState, TMR_CH),
  410. VMSTATE_INT64_ARRAY(div_round, RTMRState, TMR_CH),
  411. VMSTATE_UINT8_ARRAY(next, RTMRState, TMR_CH),
  412. VMSTATE_TIMER_ARRAY(timer, RTMRState, TMR_CH),
  413. VMSTATE_END_OF_LIST()
  414. }
  415. };
  416. static Property rtmr_properties[] = {
  417. DEFINE_PROP_UINT64("input-freq", RTMRState, input_freq, 0),
  418. DEFINE_PROP_END_OF_LIST(),
  419. };
  420. static void rtmr_class_init(ObjectClass *klass, void *data)
  421. {
  422. DeviceClass *dc = DEVICE_CLASS(klass);
  423. dc->vmsd = &vmstate_rtmr;
  424. dc->reset = rtmr_reset;
  425. device_class_set_props(dc, rtmr_properties);
  426. }
  427. static const TypeInfo rtmr_info = {
  428. .name = TYPE_RENESAS_TMR,
  429. .parent = TYPE_SYS_BUS_DEVICE,
  430. .instance_size = sizeof(RTMRState),
  431. .instance_init = rtmr_init,
  432. .class_init = rtmr_class_init,
  433. };
  434. static void rtmr_register_types(void)
  435. {
  436. type_register_static(&rtmr_info);
  437. }
  438. type_init(rtmr_register_types)