exynos4210_clk.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Exynos4210 Clock Controller Emulation
  3. *
  4. * Copyright (c) 2017 Krzysztof Kozlowski <krzk@kernel.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * 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, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "hw/sysbus.h"
  21. #include "migration/vmstate.h"
  22. #include "qemu/log.h"
  23. #include "qemu/module.h"
  24. #include "qom/object.h"
  25. #define TYPE_EXYNOS4210_CLK "exynos4210.clk"
  26. OBJECT_DECLARE_SIMPLE_TYPE(Exynos4210ClkState, EXYNOS4210_CLK)
  27. #define CLK_PLL_LOCKED BIT(29)
  28. #define EXYNOS4210_CLK_REGS_MEM_SIZE 0x15104
  29. typedef struct Exynos4210Reg {
  30. const char *name; /* for debug only */
  31. uint32_t offset;
  32. uint32_t reset_value;
  33. } Exynos4210Reg;
  34. /* Clock controller register base: 0x10030000 */
  35. static const Exynos4210Reg exynos4210_clk_regs[] = {
  36. {"EPLL_LOCK", 0xc010, 0x00000fff},
  37. {"VPLL_LOCK", 0xc020, 0x00000fff},
  38. {"EPLL_CON0", 0xc110, 0x00300301 | CLK_PLL_LOCKED},
  39. {"EPLL_CON1", 0xc114, 0x00000000},
  40. {"VPLL_CON0", 0xc120, 0x00240201 | CLK_PLL_LOCKED},
  41. {"VPLL_CON1", 0xc124, 0x66010464},
  42. {"APLL_LOCK", 0x14000, 0x00000fff},
  43. {"MPLL_LOCK", 0x14004, 0x00000fff},
  44. {"APLL_CON0", 0x14100, 0x00c80601 | CLK_PLL_LOCKED},
  45. {"APLL_CON1", 0x14104, 0x0000001c},
  46. {"MPLL_CON0", 0x14108, 0x00c80601 | CLK_PLL_LOCKED},
  47. {"MPLL_CON1", 0x1410c, 0x0000001c},
  48. };
  49. #define EXYNOS4210_REGS_NUM ARRAY_SIZE(exynos4210_clk_regs)
  50. struct Exynos4210ClkState {
  51. SysBusDevice parent_obj;
  52. MemoryRegion iomem;
  53. uint32_t reg[EXYNOS4210_REGS_NUM];
  54. };
  55. static uint64_t exynos4210_clk_read(void *opaque, hwaddr offset,
  56. unsigned size)
  57. {
  58. const Exynos4210ClkState *s = (Exynos4210ClkState *)opaque;
  59. const Exynos4210Reg *regs = exynos4210_clk_regs;
  60. unsigned int i;
  61. for (i = 0; i < EXYNOS4210_REGS_NUM; i++) {
  62. if (regs->offset == offset) {
  63. return s->reg[i];
  64. }
  65. regs++;
  66. }
  67. qemu_log_mask(LOG_GUEST_ERROR, "%s: bad read offset 0x%04x\n",
  68. __func__, (uint32_t)offset);
  69. return 0;
  70. }
  71. static void exynos4210_clk_write(void *opaque, hwaddr offset,
  72. uint64_t val, unsigned size)
  73. {
  74. Exynos4210ClkState *s = (Exynos4210ClkState *)opaque;
  75. const Exynos4210Reg *regs = exynos4210_clk_regs;
  76. unsigned int i;
  77. for (i = 0; i < EXYNOS4210_REGS_NUM; i++) {
  78. if (regs->offset == offset) {
  79. s->reg[i] = val;
  80. return;
  81. }
  82. regs++;
  83. }
  84. qemu_log_mask(LOG_GUEST_ERROR, "%s: bad write offset 0x%04x\n",
  85. __func__, (uint32_t)offset);
  86. }
  87. static const MemoryRegionOps exynos4210_clk_ops = {
  88. .read = exynos4210_clk_read,
  89. .write = exynos4210_clk_write,
  90. .endianness = DEVICE_NATIVE_ENDIAN,
  91. .valid = {
  92. .min_access_size = 4,
  93. .max_access_size = 4,
  94. .unaligned = false
  95. }
  96. };
  97. static void exynos4210_clk_reset(DeviceState *dev)
  98. {
  99. Exynos4210ClkState *s = EXYNOS4210_CLK(dev);
  100. unsigned int i;
  101. /* Set default values for registers */
  102. for (i = 0; i < EXYNOS4210_REGS_NUM; i++) {
  103. s->reg[i] = exynos4210_clk_regs[i].reset_value;
  104. }
  105. }
  106. static void exynos4210_clk_init(Object *obj)
  107. {
  108. Exynos4210ClkState *s = EXYNOS4210_CLK(obj);
  109. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  110. /* memory mapping */
  111. memory_region_init_io(&s->iomem, obj, &exynos4210_clk_ops, s,
  112. TYPE_EXYNOS4210_CLK, EXYNOS4210_CLK_REGS_MEM_SIZE);
  113. sysbus_init_mmio(dev, &s->iomem);
  114. }
  115. static const VMStateDescription exynos4210_clk_vmstate = {
  116. .name = TYPE_EXYNOS4210_CLK,
  117. .version_id = 1,
  118. .minimum_version_id = 1,
  119. .fields = (VMStateField[]) {
  120. VMSTATE_UINT32_ARRAY(reg, Exynos4210ClkState, EXYNOS4210_REGS_NUM),
  121. VMSTATE_END_OF_LIST()
  122. }
  123. };
  124. static void exynos4210_clk_class_init(ObjectClass *klass, void *data)
  125. {
  126. DeviceClass *dc = DEVICE_CLASS(klass);
  127. dc->reset = exynos4210_clk_reset;
  128. dc->vmsd = &exynos4210_clk_vmstate;
  129. }
  130. static const TypeInfo exynos4210_clk_info = {
  131. .name = TYPE_EXYNOS4210_CLK,
  132. .parent = TYPE_SYS_BUS_DEVICE,
  133. .instance_size = sizeof(Exynos4210ClkState),
  134. .instance_init = exynos4210_clk_init,
  135. .class_init = exynos4210_clk_class_init,
  136. };
  137. static void exynos4210_clk_register(void)
  138. {
  139. qemu_log_mask(LOG_GUEST_ERROR, "Clock init\n");
  140. type_register_static(&exynos4210_clk_info);
  141. }
  142. type_init(exynos4210_clk_register)