summaryrefslogtreecommitdiffstats
path: root/hw/misc/imx7_ccm.c
blob: 075159e497b1a76b14a9ed041ba06d109dd8c3e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (c) 2018, Impinj, Inc.
 *
 * i.MX7 CCM, PMU and ANALOG IP blocks emulation code
 *
 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

#include "qemu/osdep.h"
#include "qemu/log.h"
#include "qemu/module.h"

#include "hw/misc/imx7_ccm.h"
#include "migration/vmstate.h"

static void imx7_analog_reset(DeviceState *dev)
{
    IMX7AnalogState *s = IMX7_ANALOG(dev);

    memset(s->pmu, 0, sizeof(s->pmu));
    memset(s->analog, 0, sizeof(s->analog));

    s->analog[ANALOG_PLL_ARM]         = 0x00002042;
    s->analog[ANALOG_PLL_DDR]         = 0x0060302c;
    s->analog[ANALOG_PLL_DDR_SS]      = 0x00000000;
    s->analog[ANALOG_PLL_DDR_NUM]     = 0x06aaac4d;
    s->analog[ANALOG_PLL_DDR_DENOM]   = 0x100003ec;
    s->analog[ANALOG_PLL_480]         = 0x00002000;
    s->analog[ANALOG_PLL_480A]        = 0x52605a56;
    s->analog[ANALOG_PLL_480B]        = 0x52525216;
    s->analog[ANALOG_PLL_ENET]        = 0x00001fc0;
    s->analog[ANALOG_PLL_AUDIO]       = 0x0001301b;
    s->analog[ANALOG_PLL_AUDIO_SS]    = 0x00000000;
    s->analog[ANALOG_PLL_AUDIO_NUM]   = 0x05f5e100;
    s->analog[ANALOG_PLL_AUDIO_DENOM] = 0x2964619c;
    s->analog[ANALOG_PLL_VIDEO]       = 0x0008201b;
    s->analog[ANALOG_PLL_VIDEO_SS]    = 0x00000000;
    s->analog[ANALOG_PLL_VIDEO_NUM]   = 0x0000f699;
    s->analog[ANALOG_PLL_VIDEO_DENOM] = 0x000f4240;
    s->analog[ANALOG_PLL_MISC0]       = 0x00000000;

    /* all PLLs need to be locked */
    s->analog[ANALOG_PLL_ARM]   |= ANALOG_PLL_LOCK;
    s->analog[ANALOG_PLL_DDR]   |= ANALOG_PLL_LOCK;
    s->analog[ANALOG_PLL_480]   |= ANALOG_PLL_LOCK;
    s->analog[ANALOG_PLL_480A]  |= ANALOG_PLL_LOCK;
    s->analog[ANALOG_PLL_480B]  |= ANALOG_PLL_LOCK;
    s->analog[ANALOG_PLL_ENET]  |= ANALOG_PLL_LOCK;
    s->analog[ANALOG_PLL_AUDIO] |= ANALOG_PLL_LOCK;
    s->analog[ANALOG_PLL_VIDEO] |= ANALOG_PLL_LOCK;
    s->analog[ANALOG_PLL_MISC0] |= ANALOG_PLL_LOCK;

    /*
     * Since I couldn't find any info about this in the reference
     * manual the value of this register is based strictly on matching
     * what Linux kernel expects it to be.
     */
    s->analog[ANALOG_DIGPROG]  = 0x720000;
    /*
     * Set revision to be 1.0 (Arbitrary choice, no particular
     * reason).
     */
    s->analog[ANALOG_DIGPROG] |= 0x000010;
}

static void imx7_ccm_reset(DeviceState *dev)
{
    IMX7CCMState *s = IMX7_CCM(dev);

    memset(s->ccm, 0, sizeof(s->ccm));
}

#define CCM_INDEX(offset)   (((offset) & ~(hwaddr)0xF) / sizeof(uint32_t))
#define CCM_BITOP(offset)   ((offset) & (hwaddr)0xF)

enum {
    CCM_BITOP_NONE = 0x00,
    CCM_BITOP_SET  = 0x04,
    CCM_BITOP_CLR  = 0x08,
    CCM_BITOP_TOG  = 0x0C,
};

static uint64_t imx7_set_clr_tog_read(void *opaque, hwaddr offset,
                                      unsigned size)
{
    const uint32_t *mmio = opaque;

    return mmio[CCM_INDEX(offset)];
}

static void imx7_set_clr_tog_write(void *opaque, hwaddr offset,
                                   uint64_t value, unsigned size)
{
    const uint8_t  bitop = CCM_BITOP(offset);
    const uint32_t index = CCM_INDEX(offset);
    uint32_t *mmio = opaque;

    switch (bitop) {
    case CCM_BITOP_NONE:
        mmio[index]  = value;
        break;
    case CCM_BITOP_SET:
        mmio[index] |= value;
        break;
    case CCM_BITOP_CLR:
        mmio[index] &= ~value;
        break;
    case CCM_BITOP_TOG:
        mmio[index] ^= value;
        break;
    };
}

static const struct MemoryRegionOps imx7_set_clr_tog_ops = {
    .read = imx7_set_clr_tog_read,
    .write = imx7_set_clr_tog_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
    .impl = {
        /*
         * Our device would not work correctly if the guest was doing
         * unaligned access. This might not be a limitation on the real
         * device but in practice there is no reason for a guest to access
         * this device unaligned.
         */
        .min_access_size = 4,
        .max_access_size = 4,
        .unaligned = false,
    },
};

static void imx7_digprog_write(void *opaque, hwaddr addr,
                                        uint64_t data, unsigned size)
{
    qemu_log_mask(LOG_GUEST_ERROR,
                  "Guest write to read-only ANALOG_DIGPROG register\n");
}

static const struct MemoryRegionOps imx7_digprog_ops = {
    .read = imx7_set_clr_tog_read,
    .write = imx7_digprog_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
    .impl = {
        .min_access_size = 4,
        .max_access_size = 4,
        .unaligned = false,
    },
};

static void imx7_ccm_init(Object *obj)
{
    SysBusDevice *sd = SYS_BUS_DEVICE(obj);
    IMX7CCMState *s = IMX7_CCM(obj);

    memory_region_init_io(&s->iomem,
                          obj,
                          &imx7_set_clr_tog_ops,
                          s->ccm,
                          TYPE_IMX7_CCM ".ccm",
                          sizeof(s->ccm));

    sysbus_init_mmio(sd, &s->iomem);
}

static void imx7_analog_init(Object *obj)
{
    SysBusDevice *sd = SYS_BUS_DEVICE(obj);
    IMX7AnalogState *s = IMX7_ANALOG(obj);

    memory_region_init(&s->mmio.container, obj, TYPE_IMX7_ANALOG,
                       0x10000);

    memory_region_init_io(&s->mmio.analog,
                          obj,
                          &imx7_set_clr_tog_ops,
                          s->analog,
                          TYPE_IMX7_ANALOG,
                          sizeof(s->analog));

    memory_region_add_subregion(&s->mmio.container,
                                0x60, &s->mmio.analog);

    memory_region_init_io(&s->mmio.pmu,
                          obj,
                          &imx7_set_clr_tog_ops,
                          s->pmu,
                          TYPE_IMX7_ANALOG ".pmu",
                          sizeof(s->pmu));

    memory_region_add_subregion(&s->mmio.container,
                                0x200, &s->mmio.pmu);

    memory_region_init_io(&s->mmio.digprog,
                          obj,
                          &imx7_digprog_ops,
                          &s->analog[ANALOG_DIGPROG],
                          TYPE_IMX7_ANALOG ".digprog",
                          sizeof(uint32_t));

    memory_region_add_subregion_overlap(&s->mmio.container,
                                        0x800, &s->mmio.digprog, 10);


    sysbus_init_mmio(sd, &s->mmio.container);
}

static const VMStateDescription vmstate_imx7_ccm = {
    .name = TYPE_IMX7_CCM,
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (VMStateField[]) {
        VMSTATE_UINT32_ARRAY(ccm, IMX7CCMState, CCM_MAX),
        VMSTATE_END_OF_LIST()
    },
};

static uint32_t imx7_ccm_get_clock_frequency(IMXCCMState *dev, IMXClk clock)
{
    /*
     * This function is "consumed" by GPT emulation code, however on
     * i.MX7 each GPT block can have their own clock root. This means
     * that this functions needs somehow to know requester's identity
     * and the way to pass it: be it via additional IMXClk constants
     * or by adding another argument to this method needs to be
     * figured out
     */
    qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Not implemented\n",
                  TYPE_IMX7_CCM, __func__);
    return 0;
}

static void imx7_ccm_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    IMXCCMClass *ccm = IMX_CCM_CLASS(klass);

    dc->reset = imx7_ccm_reset;
    dc->vmsd  = &vmstate_imx7_ccm;
    dc->desc  = "i.MX7 Clock Control Module";

    ccm->get_clock_frequency = imx7_ccm_get_clock_frequency;
}

static const TypeInfo imx7_ccm_info = {
    .name          = TYPE_IMX7_CCM,
    .parent        = TYPE_IMX_CCM,
    .instance_size = sizeof(IMX7CCMState),
    .instance_init = imx7_ccm_init,
    .class_init    = imx7_ccm_class_init,
};

static const VMStateDescription vmstate_imx7_analog = {
    .name = TYPE_IMX7_ANALOG,
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (VMStateField[]) {
        VMSTATE_UINT32_ARRAY(analog, IMX7AnalogState, ANALOG_MAX),
        VMSTATE_UINT32_ARRAY(pmu,    IMX7AnalogState, PMU_MAX),
        VMSTATE_END_OF_LIST()
    },
};

static void imx7_analog_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);

    dc->reset = imx7_analog_reset;
    dc->vmsd  = &vmstate_imx7_analog;
    dc->desc  = "i.MX7 Analog Module";
}

static const TypeInfo imx7_analog_info = {
    .name          = TYPE_IMX7_ANALOG,
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(IMX7AnalogState),
    .instance_init = imx7_analog_init,
    .class_init    = imx7_analog_class_init,
};

static void imx7_ccm_register_type(void)
{
    type_register_static(&imx7_ccm_info);
    type_register_static(&imx7_analog_info);
}
type_init(imx7_ccm_register_type)