2
0

allwinner-a10-ccm.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Allwinner A10 Clock Control Module emulation
  3. *
  4. * Copyright (C) 2022 Strahinja Jankovic <strahinja.p.jankovic@gmail.com>
  5. *
  6. * This file is derived from Allwinner H3 CCU,
  7. * by Niek Linnenbank.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "qemu/units.h"
  24. #include "hw/sysbus.h"
  25. #include "migration/vmstate.h"
  26. #include "qemu/log.h"
  27. #include "qemu/module.h"
  28. #include "hw/misc/allwinner-a10-ccm.h"
  29. /* CCM register offsets */
  30. enum {
  31. REG_PLL1_CFG = 0x0000, /* PLL1 Control */
  32. REG_PLL1_TUN = 0x0004, /* PLL1 Tuning */
  33. REG_PLL2_CFG = 0x0008, /* PLL2 Control */
  34. REG_PLL2_TUN = 0x000C, /* PLL2 Tuning */
  35. REG_PLL3_CFG = 0x0010, /* PLL3 Control */
  36. REG_PLL4_CFG = 0x0018, /* PLL4 Control */
  37. REG_PLL5_CFG = 0x0020, /* PLL5 Control */
  38. REG_PLL5_TUN = 0x0024, /* PLL5 Tuning */
  39. REG_PLL6_CFG = 0x0028, /* PLL6 Control */
  40. REG_PLL6_TUN = 0x002C, /* PLL6 Tuning */
  41. REG_PLL7_CFG = 0x0030, /* PLL7 Control */
  42. REG_PLL1_TUN2 = 0x0038, /* PLL1 Tuning2 */
  43. REG_PLL5_TUN2 = 0x003C, /* PLL5 Tuning2 */
  44. REG_PLL8_CFG = 0x0040, /* PLL8 Control */
  45. REG_OSC24M_CFG = 0x0050, /* OSC24M Control */
  46. REG_CPU_AHB_APB0_CFG = 0x0054, /* CPU, AHB and APB0 Divide Ratio */
  47. };
  48. #define REG_INDEX(offset) (offset / sizeof(uint32_t))
  49. /* CCM register reset values */
  50. enum {
  51. REG_PLL1_CFG_RST = 0x21005000,
  52. REG_PLL1_TUN_RST = 0x0A101000,
  53. REG_PLL2_CFG_RST = 0x08100010,
  54. REG_PLL2_TUN_RST = 0x00000000,
  55. REG_PLL3_CFG_RST = 0x0010D063,
  56. REG_PLL4_CFG_RST = 0x21009911,
  57. REG_PLL5_CFG_RST = 0x11049280,
  58. REG_PLL5_TUN_RST = 0x14888000,
  59. REG_PLL6_CFG_RST = 0x21009911,
  60. REG_PLL6_TUN_RST = 0x00000000,
  61. REG_PLL7_CFG_RST = 0x0010D063,
  62. REG_PLL1_TUN2_RST = 0x00000000,
  63. REG_PLL5_TUN2_RST = 0x00000000,
  64. REG_PLL8_CFG_RST = 0x21009911,
  65. REG_OSC24M_CFG_RST = 0x00138013,
  66. REG_CPU_AHB_APB0_CFG_RST = 0x00010010,
  67. };
  68. static uint64_t allwinner_a10_ccm_read(void *opaque, hwaddr offset,
  69. unsigned size)
  70. {
  71. const AwA10ClockCtlState *s = AW_A10_CCM(opaque);
  72. const uint32_t idx = REG_INDEX(offset);
  73. switch (offset) {
  74. case REG_PLL1_CFG:
  75. case REG_PLL1_TUN:
  76. case REG_PLL2_CFG:
  77. case REG_PLL2_TUN:
  78. case REG_PLL3_CFG:
  79. case REG_PLL4_CFG:
  80. case REG_PLL5_CFG:
  81. case REG_PLL5_TUN:
  82. case REG_PLL6_CFG:
  83. case REG_PLL6_TUN:
  84. case REG_PLL7_CFG:
  85. case REG_PLL1_TUN2:
  86. case REG_PLL5_TUN2:
  87. case REG_PLL8_CFG:
  88. case REG_OSC24M_CFG:
  89. case REG_CPU_AHB_APB0_CFG:
  90. break;
  91. case 0x158 ... AW_A10_CCM_IOSIZE:
  92. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  93. __func__, (uint32_t)offset);
  94. return 0;
  95. default:
  96. qemu_log_mask(LOG_UNIMP, "%s: unimplemented read offset 0x%04x\n",
  97. __func__, (uint32_t)offset);
  98. return 0;
  99. }
  100. return s->regs[idx];
  101. }
  102. static void allwinner_a10_ccm_write(void *opaque, hwaddr offset,
  103. uint64_t val, unsigned size)
  104. {
  105. AwA10ClockCtlState *s = AW_A10_CCM(opaque);
  106. const uint32_t idx = REG_INDEX(offset);
  107. switch (offset) {
  108. case REG_PLL1_CFG:
  109. case REG_PLL1_TUN:
  110. case REG_PLL2_CFG:
  111. case REG_PLL2_TUN:
  112. case REG_PLL3_CFG:
  113. case REG_PLL4_CFG:
  114. case REG_PLL5_CFG:
  115. case REG_PLL5_TUN:
  116. case REG_PLL6_CFG:
  117. case REG_PLL6_TUN:
  118. case REG_PLL7_CFG:
  119. case REG_PLL1_TUN2:
  120. case REG_PLL5_TUN2:
  121. case REG_PLL8_CFG:
  122. case REG_OSC24M_CFG:
  123. case REG_CPU_AHB_APB0_CFG:
  124. break;
  125. case 0x158 ... AW_A10_CCM_IOSIZE:
  126. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  127. __func__, (uint32_t)offset);
  128. break;
  129. default:
  130. qemu_log_mask(LOG_UNIMP, "%s: unimplemented write offset 0x%04x\n",
  131. __func__, (uint32_t)offset);
  132. break;
  133. }
  134. s->regs[idx] = (uint32_t) val;
  135. }
  136. static const MemoryRegionOps allwinner_a10_ccm_ops = {
  137. .read = allwinner_a10_ccm_read,
  138. .write = allwinner_a10_ccm_write,
  139. .endianness = DEVICE_LITTLE_ENDIAN,
  140. .valid = {
  141. .min_access_size = 4,
  142. .max_access_size = 4,
  143. },
  144. .impl.min_access_size = 4,
  145. };
  146. static void allwinner_a10_ccm_reset_enter(Object *obj, ResetType type)
  147. {
  148. AwA10ClockCtlState *s = AW_A10_CCM(obj);
  149. /* Set default values for registers */
  150. s->regs[REG_INDEX(REG_PLL1_CFG)] = REG_PLL1_CFG_RST;
  151. s->regs[REG_INDEX(REG_PLL1_TUN)] = REG_PLL1_TUN_RST;
  152. s->regs[REG_INDEX(REG_PLL2_CFG)] = REG_PLL2_CFG_RST;
  153. s->regs[REG_INDEX(REG_PLL2_TUN)] = REG_PLL2_TUN_RST;
  154. s->regs[REG_INDEX(REG_PLL3_CFG)] = REG_PLL3_CFG_RST;
  155. s->regs[REG_INDEX(REG_PLL4_CFG)] = REG_PLL4_CFG_RST;
  156. s->regs[REG_INDEX(REG_PLL5_CFG)] = REG_PLL5_CFG_RST;
  157. s->regs[REG_INDEX(REG_PLL5_TUN)] = REG_PLL5_TUN_RST;
  158. s->regs[REG_INDEX(REG_PLL6_CFG)] = REG_PLL6_CFG_RST;
  159. s->regs[REG_INDEX(REG_PLL6_TUN)] = REG_PLL6_TUN_RST;
  160. s->regs[REG_INDEX(REG_PLL7_CFG)] = REG_PLL7_CFG_RST;
  161. s->regs[REG_INDEX(REG_PLL1_TUN2)] = REG_PLL1_TUN2_RST;
  162. s->regs[REG_INDEX(REG_PLL5_TUN2)] = REG_PLL5_TUN2_RST;
  163. s->regs[REG_INDEX(REG_PLL8_CFG)] = REG_PLL8_CFG_RST;
  164. s->regs[REG_INDEX(REG_OSC24M_CFG)] = REG_OSC24M_CFG_RST;
  165. s->regs[REG_INDEX(REG_CPU_AHB_APB0_CFG)] = REG_CPU_AHB_APB0_CFG_RST;
  166. }
  167. static void allwinner_a10_ccm_init(Object *obj)
  168. {
  169. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  170. AwA10ClockCtlState *s = AW_A10_CCM(obj);
  171. /* Memory mapping */
  172. memory_region_init_io(&s->iomem, OBJECT(s), &allwinner_a10_ccm_ops, s,
  173. TYPE_AW_A10_CCM, AW_A10_CCM_IOSIZE);
  174. sysbus_init_mmio(sbd, &s->iomem);
  175. }
  176. static const VMStateDescription allwinner_a10_ccm_vmstate = {
  177. .name = "allwinner-a10-ccm",
  178. .version_id = 1,
  179. .minimum_version_id = 1,
  180. .fields = (const VMStateField[]) {
  181. VMSTATE_UINT32_ARRAY(regs, AwA10ClockCtlState, AW_A10_CCM_REGS_NUM),
  182. VMSTATE_END_OF_LIST()
  183. }
  184. };
  185. static void allwinner_a10_ccm_class_init(ObjectClass *klass, void *data)
  186. {
  187. DeviceClass *dc = DEVICE_CLASS(klass);
  188. ResettableClass *rc = RESETTABLE_CLASS(klass);
  189. rc->phases.enter = allwinner_a10_ccm_reset_enter;
  190. dc->vmsd = &allwinner_a10_ccm_vmstate;
  191. }
  192. static const TypeInfo allwinner_a10_ccm_info = {
  193. .name = TYPE_AW_A10_CCM,
  194. .parent = TYPE_SYS_BUS_DEVICE,
  195. .instance_init = allwinner_a10_ccm_init,
  196. .instance_size = sizeof(AwA10ClockCtlState),
  197. .class_init = allwinner_a10_ccm_class_init,
  198. };
  199. static void allwinner_a10_ccm_register(void)
  200. {
  201. type_register_static(&allwinner_a10_ccm_info);
  202. }
  203. type_init(allwinner_a10_ccm_register)