2
0

rx62n.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * RX62N Microcontroller
  3. *
  4. * Datasheet: RX62N Group, RX621 Group User's Manual: Hardware
  5. * (Rev.1.40 R01UH0033EJ0140)
  6. *
  7. * Copyright (c) 2019 Yoshinori Sato
  8. * Copyright (c) 2020 Philippe Mathieu-Daudé
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2 or later, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "qapi/error.h"
  24. #include "qemu/error-report.h"
  25. #include "qemu/units.h"
  26. #include "hw/rx/rx62n.h"
  27. #include "hw/loader.h"
  28. #include "hw/sysbus.h"
  29. #include "hw/qdev-properties.h"
  30. #include "system/system.h"
  31. #include "qobject/qlist.h"
  32. #include "qom/object.h"
  33. /*
  34. * RX62N Internal Memory
  35. */
  36. #define RX62N_IRAM_BASE 0x00000000
  37. #define RX62N_DFLASH_BASE 0x00100000
  38. #define RX62N_CFLASH_BASE 0xfff80000
  39. /*
  40. * RX62N Peripheral Address
  41. * See users manual section 5
  42. */
  43. #define RX62N_ICU_BASE 0x00087000
  44. #define RX62N_TMR_BASE 0x00088200
  45. #define RX62N_CMT_BASE 0x00088000
  46. #define RX62N_SCI_BASE 0x00088240
  47. /*
  48. * RX62N Peripheral IRQ
  49. * See users manual section 11
  50. */
  51. #define RX62N_TMR_IRQ 174
  52. #define RX62N_CMT_IRQ 28
  53. #define RX62N_SCI_IRQ 214
  54. #define RX62N_XTAL_MIN_HZ (8 * 1000 * 1000)
  55. #define RX62N_XTAL_MAX_HZ (14 * 1000 * 1000)
  56. #define RX62N_PCLK_MAX_HZ (50 * 1000 * 1000)
  57. struct RX62NClass {
  58. /*< private >*/
  59. DeviceClass parent_class;
  60. /*< public >*/
  61. const char *name;
  62. uint64_t ram_size;
  63. uint64_t rom_flash_size;
  64. uint64_t data_flash_size;
  65. };
  66. typedef struct RX62NClass RX62NClass;
  67. DECLARE_CLASS_CHECKERS(RX62NClass, RX62N_MCU,
  68. TYPE_RX62N_MCU)
  69. /*
  70. * IRQ -> IPR mapping table
  71. * 0x00 - 0x91: IPR no (IPR00 to IPR91)
  72. * 0xff: IPR not assigned
  73. * See "11.3.1 Interrupt Vector Table" in hardware manual.
  74. */
  75. static const uint8_t ipr_table[NR_IRQS] = {
  76. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  77. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 15 */
  78. 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0x02,
  79. 0xff, 0xff, 0xff, 0x03, 0x04, 0x05, 0x06, 0x07, /* 31 */
  80. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  81. 0x10, 0x11, 0x12, 0x13, 0x14, 0x14, 0x14, 0x14, /* 47 */
  82. 0x15, 0x15, 0x15, 0x15, 0xff, 0xff, 0xff, 0xff,
  83. 0x18, 0x18, 0x18, 0x18, 0x18, 0x1d, 0x1e, 0x1f, /* 63 */
  84. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  85. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 79 */
  86. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  87. 0xff, 0xff, 0x3a, 0x3b, 0x3c, 0xff, 0xff, 0xff, /* 95 */
  88. 0x40, 0xff, 0x44, 0x45, 0xff, 0xff, 0x48, 0xff,
  89. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 111 */
  90. 0xff, 0xff, 0x51, 0x51, 0x51, 0x51, 0x52, 0x52,
  91. 0x52, 0x53, 0x53, 0x54, 0x54, 0x55, 0x55, 0x56, /* 127 */
  92. 0x56, 0x57, 0x57, 0x57, 0x57, 0x58, 0x59, 0x59,
  93. 0x59, 0x59, 0x5a, 0x5b, 0x5b, 0x5b, 0x5c, 0x5c, /* 143 */
  94. 0x5c, 0x5c, 0x5d, 0x5d, 0x5d, 0x5e, 0x5e, 0x5f,
  95. 0x5f, 0x60, 0x60, 0x61, 0x61, 0x62, 0x62, 0x62, /* 159 */
  96. 0x62, 0x63, 0x64, 0x64, 0x64, 0x64, 0x65, 0x66,
  97. 0x66, 0x66, 0x67, 0x67, 0x67, 0x67, 0x68, 0x68, /* 175 */
  98. 0x68, 0x69, 0x69, 0x69, 0x6a, 0x6a, 0x6a, 0x6b,
  99. 0x6b, 0x6b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 191 */
  100. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x70, 0x71,
  101. 0x72, 0x73, 0x74, 0x75, 0xff, 0xff, 0xff, 0xff, /* 207 */
  102. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x80,
  103. 0x80, 0x80, 0x81, 0x81, 0x81, 0x81, 0x82, 0x82, /* 223 */
  104. 0x82, 0x82, 0x83, 0x83, 0x83, 0x83, 0xff, 0xff,
  105. 0xff, 0xff, 0x85, 0x85, 0x85, 0x85, 0x86, 0x86, /* 239 */
  106. 0x86, 0x86, 0xff, 0xff, 0xff, 0xff, 0x88, 0x89,
  107. 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, /* 255 */
  108. };
  109. /*
  110. * Level triggered IRQ list
  111. * Not listed IRQ is Edge trigger.
  112. * See "11.3.1 Interrupt Vector Table" in hardware manual.
  113. */
  114. static const uint8_t levelirq[] = {
  115. 16, 21, 32, 44, 47, 48, 51, 64, 65, 66,
  116. 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
  117. 77, 78, 79, 90, 91, 170, 171, 172, 173, 214,
  118. 217, 218, 221, 222, 225, 226, 229, 234, 237, 238,
  119. 241, 246, 249, 250, 253,
  120. };
  121. static void register_icu(RX62NState *s)
  122. {
  123. int i;
  124. SysBusDevice *icu;
  125. QList *ipr_map, *trigger_level;
  126. object_initialize_child(OBJECT(s), "icu", &s->icu, TYPE_RX_ICU);
  127. icu = SYS_BUS_DEVICE(&s->icu);
  128. ipr_map = qlist_new();
  129. for (i = 0; i < NR_IRQS; i++) {
  130. qlist_append_int(ipr_map, ipr_table[i]);
  131. }
  132. qdev_prop_set_array(DEVICE(icu), "ipr-map", ipr_map);
  133. trigger_level = qlist_new();
  134. for (i = 0; i < ARRAY_SIZE(levelirq); i++) {
  135. qlist_append_int(trigger_level, levelirq[i]);
  136. }
  137. qdev_prop_set_array(DEVICE(icu), "trigger-level", trigger_level);
  138. sysbus_realize(icu, &error_abort);
  139. sysbus_connect_irq(icu, 0, qdev_get_gpio_in(DEVICE(&s->cpu), RX_CPU_IRQ));
  140. sysbus_connect_irq(icu, 1, qdev_get_gpio_in(DEVICE(&s->cpu), RX_CPU_FIR));
  141. sysbus_connect_irq(icu, 2, qdev_get_gpio_in(DEVICE(&s->icu), SWI));
  142. sysbus_mmio_map(icu, 0, RX62N_ICU_BASE);
  143. }
  144. static void register_tmr(RX62NState *s, int unit)
  145. {
  146. SysBusDevice *tmr;
  147. int i, irqbase;
  148. object_initialize_child(OBJECT(s), "tmr[*]",
  149. &s->tmr[unit], TYPE_RENESAS_TMR);
  150. tmr = SYS_BUS_DEVICE(&s->tmr[unit]);
  151. qdev_prop_set_uint64(DEVICE(tmr), "input-freq", s->pclk_freq_hz);
  152. sysbus_realize(tmr, &error_abort);
  153. irqbase = RX62N_TMR_IRQ + TMR_NR_IRQ * unit;
  154. for (i = 0; i < TMR_NR_IRQ; i++) {
  155. sysbus_connect_irq(tmr, i,
  156. qdev_get_gpio_in(DEVICE(&s->icu), irqbase + i));
  157. }
  158. sysbus_mmio_map(tmr, 0, RX62N_TMR_BASE + unit * 0x10);
  159. }
  160. static void register_cmt(RX62NState *s, int unit)
  161. {
  162. SysBusDevice *cmt;
  163. int i, irqbase;
  164. object_initialize_child(OBJECT(s), "cmt[*]",
  165. &s->cmt[unit], TYPE_RENESAS_CMT);
  166. cmt = SYS_BUS_DEVICE(&s->cmt[unit]);
  167. qdev_prop_set_uint64(DEVICE(cmt), "input-freq", s->pclk_freq_hz);
  168. sysbus_realize(cmt, &error_abort);
  169. irqbase = RX62N_CMT_IRQ + CMT_NR_IRQ * unit;
  170. for (i = 0; i < CMT_NR_IRQ; i++) {
  171. sysbus_connect_irq(cmt, i,
  172. qdev_get_gpio_in(DEVICE(&s->icu), irqbase + i));
  173. }
  174. sysbus_mmio_map(cmt, 0, RX62N_CMT_BASE + unit * 0x10);
  175. }
  176. static void register_sci(RX62NState *s, int unit)
  177. {
  178. SysBusDevice *sci;
  179. int i, irqbase;
  180. object_initialize_child(OBJECT(s), "sci[*]",
  181. &s->sci[unit], TYPE_RENESAS_SCI);
  182. sci = SYS_BUS_DEVICE(&s->sci[unit]);
  183. qdev_prop_set_chr(DEVICE(sci), "chardev", serial_hd(unit));
  184. qdev_prop_set_uint64(DEVICE(sci), "input-freq", s->pclk_freq_hz);
  185. sysbus_realize(sci, &error_abort);
  186. irqbase = RX62N_SCI_IRQ + SCI_NR_IRQ * unit;
  187. for (i = 0; i < SCI_NR_IRQ; i++) {
  188. sysbus_connect_irq(sci, i,
  189. qdev_get_gpio_in(DEVICE(&s->icu), irqbase + i));
  190. }
  191. sysbus_mmio_map(sci, 0, RX62N_SCI_BASE + unit * 0x08);
  192. }
  193. static void rx62n_realize(DeviceState *dev, Error **errp)
  194. {
  195. RX62NState *s = RX62N_MCU(dev);
  196. RX62NClass *rxc = RX62N_MCU_GET_CLASS(dev);
  197. if (s->xtal_freq_hz == 0) {
  198. error_setg(errp, "\"xtal-frequency-hz\" property must be provided.");
  199. return;
  200. }
  201. /* XTAL range: 8-14 MHz */
  202. if (s->xtal_freq_hz < RX62N_XTAL_MIN_HZ
  203. || s->xtal_freq_hz > RX62N_XTAL_MAX_HZ) {
  204. error_setg(errp, "\"xtal-frequency-hz\" property in incorrect range.");
  205. return;
  206. }
  207. /* Use a 4x fixed multiplier */
  208. s->pclk_freq_hz = 4 * s->xtal_freq_hz;
  209. /* PCLK range: 8-50 MHz */
  210. assert(s->pclk_freq_hz <= RX62N_PCLK_MAX_HZ);
  211. memory_region_init_ram(&s->iram, OBJECT(dev), "iram",
  212. rxc->ram_size, &error_abort);
  213. memory_region_add_subregion(s->sysmem, RX62N_IRAM_BASE, &s->iram);
  214. memory_region_init_rom(&s->d_flash, OBJECT(dev), "flash-data",
  215. rxc->data_flash_size, &error_abort);
  216. memory_region_add_subregion(s->sysmem, RX62N_DFLASH_BASE, &s->d_flash);
  217. memory_region_init_rom(&s->c_flash, OBJECT(dev), "flash-code",
  218. rxc->rom_flash_size, &error_abort);
  219. memory_region_add_subregion(s->sysmem, RX62N_CFLASH_BASE, &s->c_flash);
  220. /* Initialize CPU */
  221. object_initialize_child(OBJECT(s), "cpu", &s->cpu, TYPE_RX62N_CPU);
  222. qdev_realize(DEVICE(&s->cpu), NULL, &error_abort);
  223. register_icu(s);
  224. s->cpu.env.ack = qdev_get_gpio_in_named(DEVICE(&s->icu), "ack", 0);
  225. register_tmr(s, 0);
  226. register_tmr(s, 1);
  227. register_cmt(s, 0);
  228. register_cmt(s, 1);
  229. register_sci(s, 0);
  230. }
  231. static const Property rx62n_properties[] = {
  232. DEFINE_PROP_LINK("main-bus", RX62NState, sysmem, TYPE_MEMORY_REGION,
  233. MemoryRegion *),
  234. DEFINE_PROP_BOOL("load-kernel", RX62NState, kernel, false),
  235. DEFINE_PROP_UINT32("xtal-frequency-hz", RX62NState, xtal_freq_hz, 0),
  236. };
  237. static void rx62n_class_init(ObjectClass *klass, void *data)
  238. {
  239. DeviceClass *dc = DEVICE_CLASS(klass);
  240. dc->realize = rx62n_realize;
  241. device_class_set_props(dc, rx62n_properties);
  242. }
  243. static void r5f562n7_class_init(ObjectClass *oc, void *data)
  244. {
  245. RX62NClass *rxc = RX62N_MCU_CLASS(oc);
  246. rxc->ram_size = 64 * KiB;
  247. rxc->rom_flash_size = 384 * KiB;
  248. rxc->data_flash_size = 32 * KiB;
  249. };
  250. static void r5f562n8_class_init(ObjectClass *oc, void *data)
  251. {
  252. RX62NClass *rxc = RX62N_MCU_CLASS(oc);
  253. rxc->ram_size = 96 * KiB;
  254. rxc->rom_flash_size = 512 * KiB;
  255. rxc->data_flash_size = 32 * KiB;
  256. };
  257. static const TypeInfo rx62n_types[] = {
  258. {
  259. .name = TYPE_R5F562N7_MCU,
  260. .parent = TYPE_RX62N_MCU,
  261. .class_init = r5f562n7_class_init,
  262. }, {
  263. .name = TYPE_R5F562N8_MCU,
  264. .parent = TYPE_RX62N_MCU,
  265. .class_init = r5f562n8_class_init,
  266. }, {
  267. .name = TYPE_RX62N_MCU,
  268. .parent = TYPE_DEVICE,
  269. .instance_size = sizeof(RX62NState),
  270. .class_size = sizeof(RX62NClass),
  271. .class_init = rx62n_class_init,
  272. .abstract = true,
  273. }
  274. };
  275. DEFINE_TYPES(rx62n_types)