1*86c2dff9SBernhard Beschow /*
2*86c2dff9SBernhard Beschow * Copyright (c) 2025 Bernhard Beschow <shentey@gmail.com>
3*86c2dff9SBernhard Beschow *
4*86c2dff9SBernhard Beschow * i.MX 8M Plus ANALOG IP block emulation code
5*86c2dff9SBernhard Beschow *
6*86c2dff9SBernhard Beschow * Based on hw/misc/imx7_ccm.c
7*86c2dff9SBernhard Beschow *
8*86c2dff9SBernhard Beschow * SPDX-License-Identifier: GPL-2.0-or-later
9*86c2dff9SBernhard Beschow */
10*86c2dff9SBernhard Beschow
11*86c2dff9SBernhard Beschow #include "qemu/osdep.h"
12*86c2dff9SBernhard Beschow #include "qemu/log.h"
13*86c2dff9SBernhard Beschow
14*86c2dff9SBernhard Beschow #include "hw/misc/imx8mp_analog.h"
15*86c2dff9SBernhard Beschow #include "migration/vmstate.h"
16*86c2dff9SBernhard Beschow
17*86c2dff9SBernhard Beschow #define ANALOG_PLL_LOCK BIT(31)
18*86c2dff9SBernhard Beschow
imx8mp_analog_reset(DeviceState * dev)19*86c2dff9SBernhard Beschow static void imx8mp_analog_reset(DeviceState *dev)
20*86c2dff9SBernhard Beschow {
21*86c2dff9SBernhard Beschow IMX8MPAnalogState *s = IMX8MP_ANALOG(dev);
22*86c2dff9SBernhard Beschow
23*86c2dff9SBernhard Beschow memset(s->analog, 0, sizeof(s->analog));
24*86c2dff9SBernhard Beschow
25*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL1_GEN_CTRL] = 0x00002010;
26*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL1_FDIV_CTL0] = 0x00145032;
27*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL1_FDIV_CTL1] = 0x00000000;
28*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL1_SSCG_CTRL] = 0x00000000;
29*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL1_MNIT_CTRL] = 0x00100103;
30*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL2_GEN_CTRL] = 0x00002010;
31*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL2_FDIV_CTL0] = 0x00145032;
32*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL2_FDIV_CTL1] = 0x00000000;
33*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL2_SSCG_CTRL] = 0x00000000;
34*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL2_MNIT_CTRL] = 0x00100103;
35*86c2dff9SBernhard Beschow s->analog[ANALOG_VIDEO_PLL1_GEN_CTRL] = 0x00002010;
36*86c2dff9SBernhard Beschow s->analog[ANALOG_VIDEO_PLL1_FDIV_CTL0] = 0x00145032;
37*86c2dff9SBernhard Beschow s->analog[ANALOG_VIDEO_PLL1_FDIV_CTL1] = 0x00000000;
38*86c2dff9SBernhard Beschow s->analog[ANALOG_VIDEO_PLL1_SSCG_CTRL] = 0x00000000;
39*86c2dff9SBernhard Beschow s->analog[ANALOG_VIDEO_PLL1_MNIT_CTRL] = 0x00100103;
40*86c2dff9SBernhard Beschow s->analog[ANALOG_DRAM_PLL_GEN_CTRL] = 0x00002010;
41*86c2dff9SBernhard Beschow s->analog[ANALOG_DRAM_PLL_FDIV_CTL0] = 0x0012c032;
42*86c2dff9SBernhard Beschow s->analog[ANALOG_DRAM_PLL_FDIV_CTL1] = 0x00000000;
43*86c2dff9SBernhard Beschow s->analog[ANALOG_DRAM_PLL_SSCG_CTRL] = 0x00000000;
44*86c2dff9SBernhard Beschow s->analog[ANALOG_DRAM_PLL_MNIT_CTRL] = 0x00100103;
45*86c2dff9SBernhard Beschow s->analog[ANALOG_GPU_PLL_GEN_CTRL] = 0x00000810;
46*86c2dff9SBernhard Beschow s->analog[ANALOG_GPU_PLL_FDIV_CTL0] = 0x000c8031;
47*86c2dff9SBernhard Beschow s->analog[ANALOG_GPU_PLL_LOCKD_CTRL] = 0x0010003f;
48*86c2dff9SBernhard Beschow s->analog[ANALOG_GPU_PLL_MNIT_CTRL] = 0x00280081;
49*86c2dff9SBernhard Beschow s->analog[ANALOG_VPU_PLL_GEN_CTRL] = 0x00000810;
50*86c2dff9SBernhard Beschow s->analog[ANALOG_VPU_PLL_FDIV_CTL0] = 0x0012c032;
51*86c2dff9SBernhard Beschow s->analog[ANALOG_VPU_PLL_LOCKD_CTRL] = 0x0010003f;
52*86c2dff9SBernhard Beschow s->analog[ANALOG_VPU_PLL_MNIT_CTRL] = 0x00280081;
53*86c2dff9SBernhard Beschow s->analog[ANALOG_ARM_PLL_GEN_CTRL] = 0x00000810;
54*86c2dff9SBernhard Beschow s->analog[ANALOG_ARM_PLL_FDIV_CTL0] = 0x000fa031;
55*86c2dff9SBernhard Beschow s->analog[ANALOG_ARM_PLL_LOCKD_CTRL] = 0x0010003f;
56*86c2dff9SBernhard Beschow s->analog[ANALOG_ARM_PLL_MNIT_CTRL] = 0x00280081;
57*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL1_GEN_CTRL] = 0x0aaaa810;
58*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL1_FDIV_CTL0] = 0x00190032;
59*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL1_LOCKD_CTRL] = 0x0010003f;
60*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL1_MNIT_CTRL] = 0x00280081;
61*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL2_GEN_CTRL] = 0x0aaaa810;
62*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL2_FDIV_CTL0] = 0x000fa031;
63*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL2_LOCKD_CTRL] = 0x0010003f;
64*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL2_MNIT_CTRL] = 0x00280081;
65*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL3_GEN_CTRL] = 0x00000810;
66*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL3_FDIV_CTL0] = 0x000fa031;
67*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL3_LOCKD_CTRL] = 0x0010003f;
68*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL3_MNIT_CTRL] = 0x00280081;
69*86c2dff9SBernhard Beschow s->analog[ANALOG_OSC_MISC_CFG] = 0x00000000;
70*86c2dff9SBernhard Beschow s->analog[ANALOG_ANAMIX_PLL_MNIT_CTL] = 0x00000000;
71*86c2dff9SBernhard Beschow s->analog[ANALOG_DIGPROG] = 0x00824010;
72*86c2dff9SBernhard Beschow
73*86c2dff9SBernhard Beschow /* all PLLs need to be locked */
74*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL1_GEN_CTRL] |= ANALOG_PLL_LOCK;
75*86c2dff9SBernhard Beschow s->analog[ANALOG_AUDIO_PLL2_GEN_CTRL] |= ANALOG_PLL_LOCK;
76*86c2dff9SBernhard Beschow s->analog[ANALOG_VIDEO_PLL1_GEN_CTRL] |= ANALOG_PLL_LOCK;
77*86c2dff9SBernhard Beschow s->analog[ANALOG_DRAM_PLL_GEN_CTRL] |= ANALOG_PLL_LOCK;
78*86c2dff9SBernhard Beschow s->analog[ANALOG_GPU_PLL_GEN_CTRL] |= ANALOG_PLL_LOCK;
79*86c2dff9SBernhard Beschow s->analog[ANALOG_VPU_PLL_GEN_CTRL] |= ANALOG_PLL_LOCK;
80*86c2dff9SBernhard Beschow s->analog[ANALOG_ARM_PLL_GEN_CTRL] |= ANALOG_PLL_LOCK;
81*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL1_GEN_CTRL] |= ANALOG_PLL_LOCK;
82*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL2_GEN_CTRL] |= ANALOG_PLL_LOCK;
83*86c2dff9SBernhard Beschow s->analog[ANALOG_SYS_PLL3_GEN_CTRL] |= ANALOG_PLL_LOCK;
84*86c2dff9SBernhard Beschow }
85*86c2dff9SBernhard Beschow
imx8mp_analog_read(void * opaque,hwaddr offset,unsigned size)86*86c2dff9SBernhard Beschow static uint64_t imx8mp_analog_read(void *opaque, hwaddr offset, unsigned size)
87*86c2dff9SBernhard Beschow {
88*86c2dff9SBernhard Beschow IMX8MPAnalogState *s = opaque;
89*86c2dff9SBernhard Beschow
90*86c2dff9SBernhard Beschow return s->analog[offset >> 2];
91*86c2dff9SBernhard Beschow }
92*86c2dff9SBernhard Beschow
imx8mp_analog_write(void * opaque,hwaddr offset,uint64_t value,unsigned size)93*86c2dff9SBernhard Beschow static void imx8mp_analog_write(void *opaque, hwaddr offset,
94*86c2dff9SBernhard Beschow uint64_t value, unsigned size)
95*86c2dff9SBernhard Beschow {
96*86c2dff9SBernhard Beschow IMX8MPAnalogState *s = opaque;
97*86c2dff9SBernhard Beschow
98*86c2dff9SBernhard Beschow if (offset >> 2 == ANALOG_DIGPROG) {
99*86c2dff9SBernhard Beschow qemu_log_mask(LOG_GUEST_ERROR,
100*86c2dff9SBernhard Beschow "Guest write to read-only ANALOG_DIGPROG register\n");
101*86c2dff9SBernhard Beschow } else {
102*86c2dff9SBernhard Beschow s->analog[offset >> 2] = value;
103*86c2dff9SBernhard Beschow }
104*86c2dff9SBernhard Beschow }
105*86c2dff9SBernhard Beschow
106*86c2dff9SBernhard Beschow static const struct MemoryRegionOps imx8mp_analog_ops = {
107*86c2dff9SBernhard Beschow .read = imx8mp_analog_read,
108*86c2dff9SBernhard Beschow .write = imx8mp_analog_write,
109*86c2dff9SBernhard Beschow .endianness = DEVICE_NATIVE_ENDIAN,
110*86c2dff9SBernhard Beschow .impl = {
111*86c2dff9SBernhard Beschow .min_access_size = 4,
112*86c2dff9SBernhard Beschow .max_access_size = 4,
113*86c2dff9SBernhard Beschow .unaligned = false,
114*86c2dff9SBernhard Beschow },
115*86c2dff9SBernhard Beschow };
116*86c2dff9SBernhard Beschow
imx8mp_analog_init(Object * obj)117*86c2dff9SBernhard Beschow static void imx8mp_analog_init(Object *obj)
118*86c2dff9SBernhard Beschow {
119*86c2dff9SBernhard Beschow IMX8MPAnalogState *s = IMX8MP_ANALOG(obj);
120*86c2dff9SBernhard Beschow SysBusDevice *sd = SYS_BUS_DEVICE(obj);
121*86c2dff9SBernhard Beschow
122*86c2dff9SBernhard Beschow memory_region_init(&s->mmio.container, obj, TYPE_IMX8MP_ANALOG, 0x10000);
123*86c2dff9SBernhard Beschow
124*86c2dff9SBernhard Beschow memory_region_init_io(&s->mmio.analog, obj, &imx8mp_analog_ops, s,
125*86c2dff9SBernhard Beschow TYPE_IMX8MP_ANALOG, sizeof(s->analog));
126*86c2dff9SBernhard Beschow memory_region_add_subregion(&s->mmio.container, 0, &s->mmio.analog);
127*86c2dff9SBernhard Beschow
128*86c2dff9SBernhard Beschow sysbus_init_mmio(sd, &s->mmio.container);
129*86c2dff9SBernhard Beschow }
130*86c2dff9SBernhard Beschow
131*86c2dff9SBernhard Beschow static const VMStateDescription imx8mp_analog_vmstate = {
132*86c2dff9SBernhard Beschow .name = TYPE_IMX8MP_ANALOG,
133*86c2dff9SBernhard Beschow .version_id = 1,
134*86c2dff9SBernhard Beschow .minimum_version_id = 1,
135*86c2dff9SBernhard Beschow .fields = (const VMStateField[]) {
136*86c2dff9SBernhard Beschow VMSTATE_UINT32_ARRAY(analog, IMX8MPAnalogState, ANALOG_MAX),
137*86c2dff9SBernhard Beschow VMSTATE_END_OF_LIST()
138*86c2dff9SBernhard Beschow },
139*86c2dff9SBernhard Beschow };
140*86c2dff9SBernhard Beschow
imx8mp_analog_class_init(ObjectClass * klass,void * data)141*86c2dff9SBernhard Beschow static void imx8mp_analog_class_init(ObjectClass *klass, void *data)
142*86c2dff9SBernhard Beschow {
143*86c2dff9SBernhard Beschow DeviceClass *dc = DEVICE_CLASS(klass);
144*86c2dff9SBernhard Beschow
145*86c2dff9SBernhard Beschow device_class_set_legacy_reset(dc, imx8mp_analog_reset);
146*86c2dff9SBernhard Beschow dc->vmsd = &imx8mp_analog_vmstate;
147*86c2dff9SBernhard Beschow dc->desc = "i.MX 8M Plus Analog Module";
148*86c2dff9SBernhard Beschow }
149*86c2dff9SBernhard Beschow
150*86c2dff9SBernhard Beschow static const TypeInfo imx8mp_analog_types[] = {
151*86c2dff9SBernhard Beschow {
152*86c2dff9SBernhard Beschow .name = TYPE_IMX8MP_ANALOG,
153*86c2dff9SBernhard Beschow .parent = TYPE_SYS_BUS_DEVICE,
154*86c2dff9SBernhard Beschow .instance_size = sizeof(IMX8MPAnalogState),
155*86c2dff9SBernhard Beschow .instance_init = imx8mp_analog_init,
156*86c2dff9SBernhard Beschow .class_init = imx8mp_analog_class_init,
157*86c2dff9SBernhard Beschow }
158*86c2dff9SBernhard Beschow };
159*86c2dff9SBernhard Beschow
160*86c2dff9SBernhard Beschow DEFINE_TYPES(imx8mp_analog_types);
161