allwinner-h3-ccu.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Allwinner H3 Clock Control Unit emulation
  3. *
  4. * Copyright (C) 2019 Niek Linnenbank <nieklinnenbank@gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/units.h"
  21. #include "hw/sysbus.h"
  22. #include "migration/vmstate.h"
  23. #include "qemu/log.h"
  24. #include "qemu/module.h"
  25. #include "hw/misc/allwinner-h3-ccu.h"
  26. /* CCU register offsets */
  27. enum {
  28. REG_PLL_CPUX = 0x0000, /* PLL CPUX Control */
  29. REG_PLL_AUDIO = 0x0008, /* PLL Audio Control */
  30. REG_PLL_VIDEO = 0x0010, /* PLL Video Control */
  31. REG_PLL_VE = 0x0018, /* PLL VE Control */
  32. REG_PLL_DDR = 0x0020, /* PLL DDR Control */
  33. REG_PLL_PERIPH0 = 0x0028, /* PLL Peripherals 0 Control */
  34. REG_PLL_GPU = 0x0038, /* PLL GPU Control */
  35. REG_PLL_PERIPH1 = 0x0044, /* PLL Peripherals 1 Control */
  36. REG_PLL_DE = 0x0048, /* PLL Display Engine Control */
  37. REG_CPUX_AXI = 0x0050, /* CPUX/AXI Configuration */
  38. REG_APB1 = 0x0054, /* ARM Peripheral Bus 1 Config */
  39. REG_APB2 = 0x0058, /* ARM Peripheral Bus 2 Config */
  40. REG_DRAM_CFG = 0x00F4, /* DRAM Configuration */
  41. REG_MBUS = 0x00FC, /* MBUS Reset */
  42. REG_PLL_TIME0 = 0x0200, /* PLL Stable Time 0 */
  43. REG_PLL_TIME1 = 0x0204, /* PLL Stable Time 1 */
  44. REG_PLL_CPUX_BIAS = 0x0220, /* PLL CPUX Bias */
  45. REG_PLL_AUDIO_BIAS = 0x0224, /* PLL Audio Bias */
  46. REG_PLL_VIDEO_BIAS = 0x0228, /* PLL Video Bias */
  47. REG_PLL_VE_BIAS = 0x022C, /* PLL VE Bias */
  48. REG_PLL_DDR_BIAS = 0x0230, /* PLL DDR Bias */
  49. REG_PLL_PERIPH0_BIAS = 0x0234, /* PLL Peripherals 0 Bias */
  50. REG_PLL_GPU_BIAS = 0x023C, /* PLL GPU Bias */
  51. REG_PLL_PERIPH1_BIAS = 0x0244, /* PLL Peripherals 1 Bias */
  52. REG_PLL_DE_BIAS = 0x0248, /* PLL Display Engine Bias */
  53. REG_PLL_CPUX_TUNING = 0x0250, /* PLL CPUX Tuning */
  54. REG_PLL_DDR_TUNING = 0x0260, /* PLL DDR Tuning */
  55. };
  56. #define REG_INDEX(offset) (offset / sizeof(uint32_t))
  57. /* CCU register flags */
  58. enum {
  59. REG_DRAM_CFG_UPDATE = (1 << 16),
  60. };
  61. enum {
  62. REG_PLL_ENABLE = (1 << 31),
  63. REG_PLL_LOCK = (1 << 28),
  64. };
  65. /* CCU register reset values */
  66. enum {
  67. REG_PLL_CPUX_RST = 0x00001000,
  68. REG_PLL_AUDIO_RST = 0x00035514,
  69. REG_PLL_VIDEO_RST = 0x03006207,
  70. REG_PLL_VE_RST = 0x03006207,
  71. REG_PLL_DDR_RST = 0x00001000,
  72. REG_PLL_PERIPH0_RST = 0x00041811,
  73. REG_PLL_GPU_RST = 0x03006207,
  74. REG_PLL_PERIPH1_RST = 0x00041811,
  75. REG_PLL_DE_RST = 0x03006207,
  76. REG_CPUX_AXI_RST = 0x00010000,
  77. REG_APB1_RST = 0x00001010,
  78. REG_APB2_RST = 0x01000000,
  79. REG_DRAM_CFG_RST = 0x00000000,
  80. REG_MBUS_RST = 0x80000000,
  81. REG_PLL_TIME0_RST = 0x000000FF,
  82. REG_PLL_TIME1_RST = 0x000000FF,
  83. REG_PLL_CPUX_BIAS_RST = 0x08100200,
  84. REG_PLL_AUDIO_BIAS_RST = 0x10100000,
  85. REG_PLL_VIDEO_BIAS_RST = 0x10100000,
  86. REG_PLL_VE_BIAS_RST = 0x10100000,
  87. REG_PLL_DDR_BIAS_RST = 0x81104000,
  88. REG_PLL_PERIPH0_BIAS_RST = 0x10100010,
  89. REG_PLL_GPU_BIAS_RST = 0x10100000,
  90. REG_PLL_PERIPH1_BIAS_RST = 0x10100010,
  91. REG_PLL_DE_BIAS_RST = 0x10100000,
  92. REG_PLL_CPUX_TUNING_RST = 0x0A101000,
  93. REG_PLL_DDR_TUNING_RST = 0x14880000,
  94. };
  95. static uint64_t allwinner_h3_ccu_read(void *opaque, hwaddr offset,
  96. unsigned size)
  97. {
  98. const AwH3ClockCtlState *s = AW_H3_CCU(opaque);
  99. const uint32_t idx = REG_INDEX(offset);
  100. switch (offset) {
  101. case 0x308 ... AW_H3_CCU_IOSIZE:
  102. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  103. __func__, (uint32_t)offset);
  104. return 0;
  105. }
  106. return s->regs[idx];
  107. }
  108. static void allwinner_h3_ccu_write(void *opaque, hwaddr offset,
  109. uint64_t val, unsigned size)
  110. {
  111. AwH3ClockCtlState *s = AW_H3_CCU(opaque);
  112. const uint32_t idx = REG_INDEX(offset);
  113. switch (offset) {
  114. case REG_DRAM_CFG: /* DRAM Configuration */
  115. val &= ~REG_DRAM_CFG_UPDATE;
  116. break;
  117. case REG_PLL_CPUX: /* PLL CPUX Control */
  118. case REG_PLL_AUDIO: /* PLL Audio Control */
  119. case REG_PLL_VIDEO: /* PLL Video Control */
  120. case REG_PLL_VE: /* PLL VE Control */
  121. case REG_PLL_DDR: /* PLL DDR Control */
  122. case REG_PLL_PERIPH0: /* PLL Peripherals 0 Control */
  123. case REG_PLL_GPU: /* PLL GPU Control */
  124. case REG_PLL_PERIPH1: /* PLL Peripherals 1 Control */
  125. case REG_PLL_DE: /* PLL Display Engine Control */
  126. if (val & REG_PLL_ENABLE) {
  127. val |= REG_PLL_LOCK;
  128. }
  129. break;
  130. case 0x308 ... AW_H3_CCU_IOSIZE:
  131. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  132. __func__, (uint32_t)offset);
  133. break;
  134. default:
  135. qemu_log_mask(LOG_UNIMP, "%s: unimplemented write offset 0x%04x\n",
  136. __func__, (uint32_t)offset);
  137. break;
  138. }
  139. s->regs[idx] = (uint32_t) val;
  140. }
  141. static const MemoryRegionOps allwinner_h3_ccu_ops = {
  142. .read = allwinner_h3_ccu_read,
  143. .write = allwinner_h3_ccu_write,
  144. .endianness = DEVICE_LITTLE_ENDIAN,
  145. .valid = {
  146. .min_access_size = 4,
  147. .max_access_size = 4,
  148. },
  149. .impl.min_access_size = 4,
  150. };
  151. static void allwinner_h3_ccu_reset(DeviceState *dev)
  152. {
  153. AwH3ClockCtlState *s = AW_H3_CCU(dev);
  154. /* Set default values for registers */
  155. s->regs[REG_INDEX(REG_PLL_CPUX)] = REG_PLL_CPUX_RST;
  156. s->regs[REG_INDEX(REG_PLL_AUDIO)] = REG_PLL_AUDIO_RST;
  157. s->regs[REG_INDEX(REG_PLL_VIDEO)] = REG_PLL_VIDEO_RST;
  158. s->regs[REG_INDEX(REG_PLL_VE)] = REG_PLL_VE_RST;
  159. s->regs[REG_INDEX(REG_PLL_DDR)] = REG_PLL_DDR_RST;
  160. s->regs[REG_INDEX(REG_PLL_PERIPH0)] = REG_PLL_PERIPH0_RST;
  161. s->regs[REG_INDEX(REG_PLL_GPU)] = REG_PLL_GPU_RST;
  162. s->regs[REG_INDEX(REG_PLL_PERIPH1)] = REG_PLL_PERIPH1_RST;
  163. s->regs[REG_INDEX(REG_PLL_DE)] = REG_PLL_DE_RST;
  164. s->regs[REG_INDEX(REG_CPUX_AXI)] = REG_CPUX_AXI_RST;
  165. s->regs[REG_INDEX(REG_APB1)] = REG_APB1_RST;
  166. s->regs[REG_INDEX(REG_APB2)] = REG_APB2_RST;
  167. s->regs[REG_INDEX(REG_DRAM_CFG)] = REG_DRAM_CFG_RST;
  168. s->regs[REG_INDEX(REG_MBUS)] = REG_MBUS_RST;
  169. s->regs[REG_INDEX(REG_PLL_TIME0)] = REG_PLL_TIME0_RST;
  170. s->regs[REG_INDEX(REG_PLL_TIME1)] = REG_PLL_TIME1_RST;
  171. s->regs[REG_INDEX(REG_PLL_CPUX_BIAS)] = REG_PLL_CPUX_BIAS_RST;
  172. s->regs[REG_INDEX(REG_PLL_AUDIO_BIAS)] = REG_PLL_AUDIO_BIAS_RST;
  173. s->regs[REG_INDEX(REG_PLL_VIDEO_BIAS)] = REG_PLL_VIDEO_BIAS_RST;
  174. s->regs[REG_INDEX(REG_PLL_VE_BIAS)] = REG_PLL_VE_BIAS_RST;
  175. s->regs[REG_INDEX(REG_PLL_DDR_BIAS)] = REG_PLL_DDR_BIAS_RST;
  176. s->regs[REG_INDEX(REG_PLL_PERIPH0_BIAS)] = REG_PLL_PERIPH0_BIAS_RST;
  177. s->regs[REG_INDEX(REG_PLL_GPU_BIAS)] = REG_PLL_GPU_BIAS_RST;
  178. s->regs[REG_INDEX(REG_PLL_PERIPH1_BIAS)] = REG_PLL_PERIPH1_BIAS_RST;
  179. s->regs[REG_INDEX(REG_PLL_DE_BIAS)] = REG_PLL_DE_BIAS_RST;
  180. s->regs[REG_INDEX(REG_PLL_CPUX_TUNING)] = REG_PLL_CPUX_TUNING_RST;
  181. s->regs[REG_INDEX(REG_PLL_DDR_TUNING)] = REG_PLL_DDR_TUNING_RST;
  182. }
  183. static void allwinner_h3_ccu_init(Object *obj)
  184. {
  185. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  186. AwH3ClockCtlState *s = AW_H3_CCU(obj);
  187. /* Memory mapping */
  188. memory_region_init_io(&s->iomem, OBJECT(s), &allwinner_h3_ccu_ops, s,
  189. TYPE_AW_H3_CCU, AW_H3_CCU_IOSIZE);
  190. sysbus_init_mmio(sbd, &s->iomem);
  191. }
  192. static const VMStateDescription allwinner_h3_ccu_vmstate = {
  193. .name = "allwinner-h3-ccu",
  194. .version_id = 1,
  195. .minimum_version_id = 1,
  196. .fields = (const VMStateField[]) {
  197. VMSTATE_UINT32_ARRAY(regs, AwH3ClockCtlState, AW_H3_CCU_REGS_NUM),
  198. VMSTATE_END_OF_LIST()
  199. }
  200. };
  201. static void allwinner_h3_ccu_class_init(ObjectClass *klass, void *data)
  202. {
  203. DeviceClass *dc = DEVICE_CLASS(klass);
  204. device_class_set_legacy_reset(dc, allwinner_h3_ccu_reset);
  205. dc->vmsd = &allwinner_h3_ccu_vmstate;
  206. }
  207. static const TypeInfo allwinner_h3_ccu_info = {
  208. .name = TYPE_AW_H3_CCU,
  209. .parent = TYPE_SYS_BUS_DEVICE,
  210. .instance_init = allwinner_h3_ccu_init,
  211. .instance_size = sizeof(AwH3ClockCtlState),
  212. .class_init = allwinner_h3_ccu_class_init,
  213. };
  214. static void allwinner_h3_ccu_register(void)
  215. {
  216. type_register_static(&allwinner_h3_ccu_info);
  217. }
  218. type_init(allwinner_h3_ccu_register)