aboutsummaryrefslogtreecommitdiff
path: root/tests/qtest/npcm7xx_adc-test.c
blob: 8048044d28108d3ba95fd865e100077fdf73959b (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * QTests for Nuvoton NPCM7xx ADCModules.
 *
 * Copyright 2020 Google LLC
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 */

#include "qemu/osdep.h"
#include "qemu/bitops.h"
#include "qemu/timer.h"
#include "libqtest.h"
#include "qapi/qmp/qdict.h"

#define REF_HZ          (25000000)

#define CON_OFFSET      0x0
#define DATA_OFFSET     0x4

#define NUM_INPUTS      8
#define DEFAULT_IREF    2000000
#define CONV_CYCLES     20
#define RESET_CYCLES    10
#define R0_INPUT        500000
#define R1_INPUT        1500000
#define MAX_RESULT      1023

#define DEFAULT_CLKDIV  5

#define FUSE_ARRAY_BA   0xf018a000
#define FCTL_OFFSET     0x14
#define FST_OFFSET      0x0
#define FADDR_OFFSET    0x4
#define FDATA_OFFSET    0x8
#define ADC_CALIB_ADDR  24
#define FUSE_READ       0x2

/* Register field definitions. */
#define CON_MUX(rv) ((rv) << 24)
#define CON_INT_EN  BIT(21)
#define CON_REFSEL  BIT(19)
#define CON_INT     BIT(18)
#define CON_EN      BIT(17)
#define CON_RST     BIT(16)
#define CON_CONV    BIT(13)
#define CON_DIV(rv) extract32(rv, 1, 8)

#define FST_RDST    BIT(1)
#define FDATA_MASK  0xff

#define MAX_ERROR   10000
#define MIN_CALIB_INPUT 100000
#define MAX_CALIB_INPUT 1800000

static const uint32_t input_list[] = {
    100000,
    500000,
    1000000,
    1500000,
    1800000,
    2000000,
};

static const uint32_t vref_list[] = {
    2000000,
    2200000,
    2500000,
};

static const uint32_t iref_list[] = {
    1800000,
    1900000,
    2000000,
    2100000,
    2200000,
};

static const uint32_t div_list[] = {0, 1, 3, 7, 15};

typedef struct ADC {
    int irq;
    uint64_t base_addr;
} ADC;

ADC adc = {
    .irq        = 0,
    .base_addr  = 0xf000c000
};

static uint32_t adc_read_con(QTestState *qts, const ADC *adc)
{
    return qtest_readl(qts, adc->base_addr + CON_OFFSET);
}

static void adc_write_con(QTestState *qts, const ADC *adc, uint32_t value)
{
    qtest_writel(qts, adc->base_addr + CON_OFFSET, value);
}

static uint32_t adc_read_data(QTestState *qts, const ADC *adc)
{
    return qtest_readl(qts, adc->base_addr + DATA_OFFSET);
}

static uint32_t adc_calibrate(uint32_t measured, uint32_t *rv)
{
    return R0_INPUT + (R1_INPUT - R0_INPUT) * (int32_t)(measured - rv[0])
        / (int32_t)(rv[1] - rv[0]);
}

static void adc_qom_set(QTestState *qts, const ADC *adc,
        const char *name, uint32_t value)
{
    QDict *response;
    const char *path = "/machine/soc/adc";

    g_test_message("Setting properties %s of %s with value %u",
            name, path, value);
    response = qtest_qmp(qts, "{ 'execute': 'qom-set',"
            " 'arguments': { 'path': %s, 'property': %s, 'value': %u}}",
            path, name, value);
    /* The qom set message returns successfully. */
    g_assert_true(qdict_haskey(response, "return"));
    qobject_unref(response);
}

static void adc_write_input(QTestState *qts, const ADC *adc,
        uint32_t index, uint32_t value)
{
    char name[100];

    sprintf(name, "adci[%u]", index);
    adc_qom_set(qts, adc, name, value);
}

static void adc_write_vref(QTestState *qts, const ADC *adc, uint32_t value)
{
    adc_qom_set(qts, adc, "vref", value);
}

static uint32_t adc_calculate_output(uint32_t input, uint32_t ref)
{
    uint32_t output;

    g_assert_cmpuint(input, <=, ref);
    output = (input * (MAX_RESULT + 1)) / ref;
    if (output > MAX_RESULT) {
        output = MAX_RESULT;
    }

    return output;
}

static uint32_t adc_prescaler(QTestState *qts, const ADC *adc)
{
    uint32_t div = extract32(adc_read_con(qts, adc), 1, 8);

    return 2 * (div + 1);
}

static int64_t adc_calculate_steps(uint32_t cycles, uint32_t prescale,
        uint32_t clkdiv)
{
    return (NANOSECONDS_PER_SECOND / (REF_HZ >> clkdiv)) * cycles * prescale;
}

static void adc_wait_conv_finished(QTestState *qts, const ADC *adc,
        uint32_t clkdiv)
{
    uint32_t prescaler = adc_prescaler(qts, adc);

    /*
     * ADC should takes roughly 20 cycles to convert one sample. So we assert it
     * should take 10~30 cycles here.
     */
    qtest_clock_step(qts, adc_calculate_steps(CONV_CYCLES / 2, prescaler,
                clkdiv));
    /* ADC is still converting. */
    g_assert_true(adc_read_con(qts, adc) & CON_CONV);
    qtest_clock_step(qts, adc_calculate_steps(CONV_CYCLES, prescaler, clkdiv));
    /* ADC has finished conversion. */
    g_assert_false(adc_read_con(qts, adc) & CON_CONV);
}

/* Check ADC can be reset to default value. */
static void test_init(gconstpointer adc_p)
{
    const ADC *adc = adc_p;

    QTestState *qts = qtest_init("-machine quanta-gsj");
    adc_write_con(qts, adc, CON_REFSEL | CON_INT);
    g_assert_cmphex(adc_read_con(qts, adc), ==, CON_REFSEL);
    qtest_quit(qts);
}

/* Check ADC can convert from an internal reference. */
static void test_convert_internal(gconstpointer adc_p)
{
    const ADC *adc = adc_p;
    uint32_t index, input, output, expected_output;
    QTestState *qts = qtest_init("-machine quanta-gsj");
    qtest_irq_intercept_in(qts, "/machine/soc/a9mpcore/gic");

    for (index = 0; index < NUM_INPUTS; ++index) {
        for (size_t i = 0; i < ARRAY_SIZE(input_list); ++i) {
            input = input_list[i];
            expected_output = adc_calculate_output(input, DEFAULT_IREF);

            adc_write_input(qts, adc, index, input);
            adc_write_con(qts, adc, CON_MUX(index) | CON_REFSEL | CON_INT |
                    CON_EN | CON_CONV);
            adc_wait_conv_finished(qts, adc, DEFAULT_CLKDIV);
            g_assert_cmphex(adc_read_con(qts, adc), ==, CON_MUX(index) |
                    CON_REFSEL | CON_EN);
            g_assert_false(qtest_get_irq(qts, adc->irq));
            output = adc_read_data(qts, adc);
            g_assert_cmpuint(output, ==, expected_output);
        }
    }

    qtest_quit(qts);
}

/* Check ADC can convert from an external reference. */
static void test_convert_external(gconstpointer adc_p)
{
    const ADC *adc = adc_p;
    uint32_t index, input, vref, output, expected_output;
    QTestState *qts = qtest_init("-machine quanta-gsj");
    qtest_irq_intercept_in(qts, "/machine/soc/a9mpcore/gic");

    for (index = 0; index < NUM_INPUTS; ++index) {
        for (size_t i = 0; i < ARRAY_SIZE(input_list); ++i) {
            for (size_t j = 0; j < ARRAY_SIZE(vref_list); ++j) {
                input = input_list[i];
                vref = vref_list[j];
                expected_output = adc_calculate_output(input, vref);

                adc_write_input(qts, adc, index, input);
                adc_write_vref(qts, adc, vref);
                adc_write_con(qts, adc, CON_MUX(index) | CON_INT | CON_EN |
                        CON_CONV);
                adc_wait_conv_finished(qts, adc, DEFAULT_CLKDIV);
                g_assert_cmphex(adc_read_con(qts, adc), ==,
                        CON_MUX(index) | CON_EN);
                g_assert_false(qtest_get_irq(qts, adc->irq));
                output = adc_read_data(qts, adc);
                g_assert_cmpuint(output, ==, expected_output);
            }
        }
    }

    qtest_quit(qts);
}

/* Check ADC interrupt files if and only if CON_INT_EN is set. */
static void test_interrupt(gconstpointer adc_p)
{
    const ADC *adc = adc_p;
    uint32_t index, input, output, expected_output;
    QTestState *qts = qtest_init("-machine quanta-gsj");

    index = 1;
    input = input_list[1];
    expected_output = adc_calculate_output(input, DEFAULT_IREF);

    qtest_irq_intercept_in(qts, "/machine/soc/a9mpcore/gic");
    adc_write_input(qts, adc, index, input);
    g_assert_false(qtest_get_irq(qts, adc->irq));
    adc_write_con(qts, adc, CON_MUX(index) | CON_INT_EN | CON_REFSEL | CON_INT
            | CON_EN | CON_CONV);
    adc_wait_conv_finished(qts, adc, DEFAULT_CLKDIV);
    g_assert_cmphex(adc_read_con(qts, adc), ==, CON_MUX(index) | CON_INT_EN
            | CON_REFSEL | CON_INT | CON_EN);
    g_assert_true(qtest_get_irq(qts, adc->irq));
    output = adc_read_data(qts, adc);
    g_assert_cmpuint(output, ==, expected_output);

    qtest_quit(qts);
}

/* Check ADC is reset after setting ADC_RST for 10 ADC cycles. */
static void test_reset(gconstpointer adc_p)
{
    const ADC *adc = adc_p;
    QTestState *qts = qtest_init("-machine quanta-gsj");

    for (size_t i = 0; i < ARRAY_SIZE(div_list); ++i) {
        uint32_t div = div_list[i];

        adc_write_con(qts, adc, CON_INT | CON_EN | CON_RST | CON_DIV(div));
        qtest_clock_step(qts, adc_calculate_steps(RESET_CYCLES,
                    adc_prescaler(qts, adc), DEFAULT_CLKDIV));
        g_assert_false(adc_read_con(qts, adc) & CON_EN);
    }
    qtest_quit(qts);
}

/* Check ADC Calibration works as desired. */
static void test_calibrate(gconstpointer adc_p)
{
    int i, j;
    const ADC *adc = adc_p;

    for (j = 0; j < ARRAY_SIZE(iref_list); ++j) {
        uint32_t iref = iref_list[j];
        uint32_t expected_rv[] = {
            adc_calculate_output(R0_INPUT, iref),
            adc_calculate_output(R1_INPUT, iref),
        };
        char buf[100];
        QTestState *qts;

        sprintf(buf, "-machine quanta-gsj -global npcm7xx-adc.iref=%u", iref);
        qts = qtest_init(buf);

        /* Check the converted value is correct using the calibration value. */
        for (i = 0; i < ARRAY_SIZE(input_list); ++i) {
            uint32_t input;
            uint32_t output;
            uint32_t expected_output;
            uint32_t calibrated_voltage;
            uint32_t index = 0;

            input = input_list[i];
            /* Calibration only works for input range 0.1V ~ 1.8V. */
            if (input < MIN_CALIB_INPUT || input > MAX_CALIB_INPUT) {
                continue;
            }
            expected_output = adc_calculate_output(input, iref);

            adc_write_input(qts, adc, index, input);
            adc_write_con(qts, adc, CON_MUX(index) | CON_REFSEL | CON_INT |
                    CON_EN | CON_CONV);
            adc_wait_conv_finished(qts, adc, DEFAULT_CLKDIV);
            g_assert_cmphex(adc_read_con(qts, adc), ==,
                    CON_REFSEL | CON_MUX(index) | CON_EN);
            output = adc_read_data(qts, adc);
            g_assert_cmpuint(output, ==, expected_output);

            calibrated_voltage = adc_calibrate(output, expected_rv);
            g_assert_cmpuint(calibrated_voltage, >, input - MAX_ERROR);
            g_assert_cmpuint(calibrated_voltage, <, input + MAX_ERROR);
        }

        qtest_quit(qts);
    }
}

static void adc_add_test(const char *name, const ADC* wd,
        GTestDataFunc fn)
{
    g_autofree char *full_name = g_strdup_printf("npcm7xx_adc/%s",  name);
    qtest_add_data_func(full_name, wd, fn);
}
#define add_test(name, td) adc_add_test(#name, td, test_##name)

int main(int argc, char **argv)
{
    g_test_init(&argc, &argv, NULL);

    add_test(init, &adc);
    add_test(convert_internal, &adc);
    add_test(convert_external, &adc);
    add_test(interrupt, &adc);
    add_test(reset, &adc);
    add_test(calibrate, &adc);

    return g_test_run();
}