2 * QTests for Nuvoton NPCM7xx PWM Modules.
4 * Copyright 2020 Google LLC
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.
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
17 #include "qemu/osdep.h"
18 #include "qemu/bitops.h"
19 #include "libqos/libqtest.h"
20 #include "qapi/qmp/qdict.h"
21 #include "qapi/qmp/qnum.h"
23 #define REF_HZ 25000000
25 /* Register field definitions. */
30 /* Registers shared between all PWMs in a module */
37 /* CLK module related */
38 #define CLK_BA 0xf0801000
44 #define PLL_INDV(rv) extract32((rv), 0, 6)
45 #define PLL_FBDV(rv) extract32((rv), 16, 12)
46 #define PLL_OTDV1(rv) extract32((rv), 8, 3)
47 #define PLL_OTDV2(rv) extract32((rv), 13, 3)
48 #define APB4CKDIV(rv) extract32((rv), 30, 2)
49 #define APB3CKDIV(rv) extract32((rv), 28, 2)
50 #define CLK2CKDIV(rv) extract32((rv), 0, 1)
51 #define CLK4CKDIV(rv) extract32((rv), 26, 2)
52 #define CPUCKSEL(rv) extract32((rv), 0, 2)
54 #define MAX_DUTY 1000000
56 /* MFT (PWM fan) related */
57 #define MFT_BA(n) (0xf0180000 + ((n) * 0x1000))
58 #define MFT_IRQ(n) (96 + (n))
65 #define MFT_MCTRL 0x0c
66 #define MFT_ICTRL 0x0e
71 #define MFT_CPCFG 0x18
72 #define MFT_INASEL 0x1a
73 #define MFT_INBSEL 0x1c
75 #define MFT_MCTRL_ALL 0x64
76 #define MFT_ICLR_ALL 0x3f
77 #define MFT_IEN_ALL 0x3f
78 #define MFT_CPCFG_EQ_MODE 0x44
80 #define MFT_CKC_C2CSEL BIT(3)
81 #define MFT_CKC_C1CSEL BIT(0)
83 #define MFT_ICTRL_TFPND BIT(5)
84 #define MFT_ICTRL_TEPND BIT(4)
85 #define MFT_ICTRL_TDPND BIT(3)
86 #define MFT_ICTRL_TCPND BIT(2)
87 #define MFT_ICTRL_TBPND BIT(1)
88 #define MFT_ICTRL_TAPND BIT(0)
90 #define MFT_MAX_CNT 0xffff
91 #define MFT_TIMEOUT 0x5000
93 #define DEFAULT_RPM 19800
94 #define DEFAULT_PRSC 255
95 #define MFT_PULSE_PER_REVOLUTION 2
99 typedef struct PWMModule
{
108 uint32_t pwdr_offset
;
111 typedef struct TestData
{
112 const PWMModule
*module
;
116 static const PWMModule pwm_module_list
[] = {
119 .base_addr
= 0xf0103000
123 .base_addr
= 0xf0104000
127 static const PWM pwm_list
[] = {
154 static const int ppr_base
[] = { 0, 0, 8, 8 };
155 static const int csr_base
[] = { 0, 4, 8, 12 };
156 static const int pcr_base
[] = { 0, 8, 12, 16 };
158 static const uint32_t ppr_list
[] = {
163 255, /* Max possible value. */
166 static const uint32_t csr_list
[] = {
171 4, /* Max possible value. */
174 static const uint32_t cnr_list
[] = {
183 65535, /* Max possible value. */
186 static const uint32_t cmr_list
[] = {
196 65535, /* Max possible value. */
199 /* Returns the index of the PWM module. */
200 static int pwm_module_index(const PWMModule
*module
)
202 ptrdiff_t diff
= module
- pwm_module_list
;
204 g_assert(diff
>= 0 && diff
< ARRAY_SIZE(pwm_module_list
));
209 /* Returns the index of the PWM entry. */
210 static int pwm_index(const PWM
*pwm
)
212 ptrdiff_t diff
= pwm
- pwm_list
;
214 g_assert(diff
>= 0 && diff
< ARRAY_SIZE(pwm_list
));
219 static uint64_t pwm_qom_get(QTestState
*qts
, const char *path
, const char *name
)
224 g_test_message("Getting properties %s from %s", name
, path
);
225 response
= qtest_qmp(qts
, "{ 'execute': 'qom-get',"
226 " 'arguments': { 'path': %s, 'property': %s}}",
228 /* The qom set message returns successfully. */
229 g_assert_true(qdict_haskey(response
, "return"));
230 val
= qnum_get_uint(qobject_to(QNum
, qdict_get(response
, "return")));
231 qobject_unref(response
);
235 static uint64_t pwm_get_freq(QTestState
*qts
, int module_index
, int pwm_index
)
240 sprintf(path
, "/machine/soc/pwm[%d]", module_index
);
241 sprintf(name
, "freq[%d]", pwm_index
);
243 return pwm_qom_get(qts
, path
, name
);
246 static uint64_t pwm_get_duty(QTestState
*qts
, int module_index
, int pwm_index
)
251 sprintf(path
, "/machine/soc/pwm[%d]", module_index
);
252 sprintf(name
, "duty[%d]", pwm_index
);
254 return pwm_qom_get(qts
, path
, name
);
257 static void mft_qom_set(QTestState
*qts
, int index
, const char *name
,
261 char *path
= g_strdup_printf("/machine/soc/mft[%d]", index
);
263 g_test_message("Setting properties %s of mft[%d] with value %u",
265 response
= qtest_qmp(qts
, "{ 'execute': 'qom-set',"
266 " 'arguments': { 'path': %s, "
267 " 'property': %s, 'value': %u}}",
269 /* The qom set message returns successfully. */
270 g_assert_true(qdict_haskey(response
, "return"));
273 static uint32_t get_pll(uint32_t con
)
275 return REF_HZ
* PLL_FBDV(con
) / (PLL_INDV(con
) * PLL_OTDV1(con
)
279 static uint64_t read_pclk(QTestState
*qts
, bool mft
)
281 uint64_t freq
= REF_HZ
;
282 uint32_t clksel
= qtest_readl(qts
, CLK_BA
+ CLKSEL
);
284 uint32_t clkdiv1
= qtest_readl(qts
, CLK_BA
+ CLKDIV1
);
285 uint32_t clkdiv2
= qtest_readl(qts
, CLK_BA
+ CLKDIV2
);
286 uint32_t apbdiv
= mft
? APB4CKDIV(clkdiv2
) : APB3CKDIV(clkdiv2
);
288 switch (CPUCKSEL(clksel
)) {
290 pllcon
= qtest_readl(qts
, CLK_BA
+ PLLCON0
);
291 freq
= get_pll(pllcon
);
294 pllcon
= qtest_readl(qts
, CLK_BA
+ PLLCON1
);
295 freq
= get_pll(pllcon
);
302 g_assert_not_reached();
305 freq
>>= (CLK2CKDIV(clkdiv1
) + CLK4CKDIV(clkdiv1
) + apbdiv
);
310 static uint32_t pwm_selector(uint32_t csr
)
324 g_assert_not_reached();
328 static uint64_t pwm_compute_freq(QTestState
*qts
, uint32_t ppr
, uint32_t csr
,
331 return read_pclk(qts
, false) / ((ppr
+ 1) * pwm_selector(csr
) * (cnr
+ 1));
334 static uint64_t pwm_compute_duty(uint32_t cnr
, uint32_t cmr
, bool inverted
)
339 /* PWM is stopped. */
341 } else if (cmr
>= cnr
) {
344 duty
= (uint64_t)MAX_DUTY
* (cmr
+ 1) / (cnr
+ 1);
348 duty
= MAX_DUTY
- duty
;
354 static uint32_t pwm_read(QTestState
*qts
, const TestData
*td
, unsigned offset
)
356 return qtest_readl(qts
, td
->module
->base_addr
+ offset
);
359 static void pwm_write(QTestState
*qts
, const TestData
*td
, unsigned offset
,
362 qtest_writel(qts
, td
->module
->base_addr
+ offset
, value
);
365 static uint8_t mft_readb(QTestState
*qts
, int index
, unsigned offset
)
367 return qtest_readb(qts
, MFT_BA(index
) + offset
);
370 static uint16_t mft_readw(QTestState
*qts
, int index
, unsigned offset
)
372 return qtest_readw(qts
, MFT_BA(index
) + offset
);
375 static void mft_writeb(QTestState
*qts
, int index
, unsigned offset
,
378 qtest_writeb(qts
, MFT_BA(index
) + offset
, value
);
381 static void mft_writew(QTestState
*qts
, int index
, unsigned offset
,
384 return qtest_writew(qts
, MFT_BA(index
) + offset
, value
);
387 static uint32_t pwm_read_ppr(QTestState
*qts
, const TestData
*td
)
389 return extract32(pwm_read(qts
, td
, PPR
), ppr_base
[pwm_index(td
->pwm
)], 8);
392 static void pwm_write_ppr(QTestState
*qts
, const TestData
*td
, uint32_t value
)
394 pwm_write(qts
, td
, PPR
, value
<< ppr_base
[pwm_index(td
->pwm
)]);
397 static uint32_t pwm_read_csr(QTestState
*qts
, const TestData
*td
)
399 return extract32(pwm_read(qts
, td
, CSR
), csr_base
[pwm_index(td
->pwm
)], 3);
402 static void pwm_write_csr(QTestState
*qts
, const TestData
*td
, uint32_t value
)
404 pwm_write(qts
, td
, CSR
, value
<< csr_base
[pwm_index(td
->pwm
)]);
407 static uint32_t pwm_read_pcr(QTestState
*qts
, const TestData
*td
)
409 return extract32(pwm_read(qts
, td
, PCR
), pcr_base
[pwm_index(td
->pwm
)], 4);
412 static void pwm_write_pcr(QTestState
*qts
, const TestData
*td
, uint32_t value
)
414 pwm_write(qts
, td
, PCR
, value
<< pcr_base
[pwm_index(td
->pwm
)]);
417 static uint32_t pwm_read_cnr(QTestState
*qts
, const TestData
*td
)
419 return pwm_read(qts
, td
, td
->pwm
->cnr_offset
);
422 static void pwm_write_cnr(QTestState
*qts
, const TestData
*td
, uint32_t value
)
424 pwm_write(qts
, td
, td
->pwm
->cnr_offset
, value
);
427 static uint32_t pwm_read_cmr(QTestState
*qts
, const TestData
*td
)
429 return pwm_read(qts
, td
, td
->pwm
->cmr_offset
);
432 static void pwm_write_cmr(QTestState
*qts
, const TestData
*td
, uint32_t value
)
434 pwm_write(qts
, td
, td
->pwm
->cmr_offset
, value
);
437 static int mft_compute_index(const TestData
*td
)
439 int index
= pwm_module_index(td
->module
) * ARRAY_SIZE(pwm_list
) +
442 g_assert_cmpint(index
, <,
443 ARRAY_SIZE(pwm_module_list
) * ARRAY_SIZE(pwm_list
));
448 static void mft_reset_counters(QTestState
*qts
, int index
)
450 mft_writew(qts
, index
, MFT_CNT1
, MFT_MAX_CNT
);
451 mft_writew(qts
, index
, MFT_CNT2
, MFT_MAX_CNT
);
452 mft_writew(qts
, index
, MFT_CRA
, MFT_MAX_CNT
);
453 mft_writew(qts
, index
, MFT_CRB
, MFT_MAX_CNT
);
454 mft_writew(qts
, index
, MFT_CPA
, MFT_MAX_CNT
- MFT_TIMEOUT
);
455 mft_writew(qts
, index
, MFT_CPB
, MFT_MAX_CNT
- MFT_TIMEOUT
);
458 static void mft_init(QTestState
*qts
, const TestData
*td
)
460 int index
= mft_compute_index(td
);
462 /* Enable everything */
463 mft_writeb(qts
, index
, MFT_CKC
, 0);
464 mft_writeb(qts
, index
, MFT_ICLR
, MFT_ICLR_ALL
);
465 mft_writeb(qts
, index
, MFT_MCTRL
, MFT_MCTRL_ALL
);
466 mft_writeb(qts
, index
, MFT_IEN
, MFT_IEN_ALL
);
467 mft_writeb(qts
, index
, MFT_INASEL
, 0);
468 mft_writeb(qts
, index
, MFT_INBSEL
, 0);
470 /* Set cpcfg to use EQ mode, same as kernel driver */
471 mft_writeb(qts
, index
, MFT_CPCFG
, MFT_CPCFG_EQ_MODE
);
473 /* Write default counters, timeout and prescaler */
474 mft_reset_counters(qts
, index
);
475 mft_writeb(qts
, index
, MFT_PRSC
, DEFAULT_PRSC
);
477 /* Write default max rpm via QMP */
478 mft_qom_set(qts
, index
, "max_rpm[0]", DEFAULT_RPM
);
479 mft_qom_set(qts
, index
, "max_rpm[1]", DEFAULT_RPM
);
482 static int32_t mft_compute_cnt(uint32_t rpm
, uint64_t clk
)
490 cnt
= clk
* 60 / ((DEFAULT_PRSC
+ 1) * rpm
* MFT_PULSE_PER_REVOLUTION
);
491 if (cnt
>= MFT_TIMEOUT
) {
494 return MFT_MAX_CNT
- cnt
;
497 static void mft_verify_rpm(QTestState
*qts
, const TestData
*td
, uint64_t duty
)
499 int index
= mft_compute_index(td
);
501 uint32_t rpm
= DEFAULT_RPM
* duty
/ MAX_DUTY
;
502 uint64_t clk
= read_pclk(qts
, true);
503 int32_t expected_cnt
= mft_compute_cnt(rpm
, clk
);
505 qtest_irq_intercept_in(qts
, "/machine/soc/a9mpcore/gic");
507 "verifying rpm for mft[%d]: clk: %" PRIu64
", duty: %" PRIu64
", rpm: %u, cnt: %d",
508 index
, clk
, duty
, rpm
, expected_cnt
);
510 /* Verify rpm for fan A */
512 mft_writeb(qts
, index
, MFT_CKC
, 0);
513 mft_writeb(qts
, index
, MFT_ICLR
, MFT_ICLR_ALL
);
514 mft_reset_counters(qts
, index
);
515 g_assert_cmphex(mft_readw(qts
, index
, MFT_CNT1
), ==, MFT_MAX_CNT
);
516 g_assert_cmphex(mft_readw(qts
, index
, MFT_CRA
), ==, MFT_MAX_CNT
);
517 g_assert_cmphex(mft_readw(qts
, index
, MFT_CPA
), ==,
518 MFT_MAX_CNT
- MFT_TIMEOUT
);
520 mft_writeb(qts
, index
, MFT_CKC
, MFT_CKC_C1CSEL
);
521 g_assert_true(qtest_get_irq(qts
, MFT_IRQ(index
)));
522 if (expected_cnt
== -1) {
523 g_assert_cmphex(mft_readb(qts
, index
, MFT_ICTRL
), ==, MFT_ICTRL_TEPND
);
525 g_assert_cmphex(mft_readb(qts
, index
, MFT_ICTRL
), ==, MFT_ICTRL_TAPND
);
526 cnt
= mft_readw(qts
, index
, MFT_CNT1
);
528 * Due to error in clock measurement and rounding, we might have a small
529 * error in measuring RPM.
531 g_assert_cmphex(cnt
+ MAX_ERROR
, >=, expected_cnt
);
532 g_assert_cmphex(cnt
, <=, expected_cnt
+ MAX_ERROR
);
533 cr
= mft_readw(qts
, index
, MFT_CRA
);
534 g_assert_cmphex(cnt
, ==, cr
);
537 /* Verify rpm for fan B */
539 qtest_irq_intercept_out(qts
, "/machine/soc/a9mpcore/gic");
542 /* Check pwm registers can be reset to default value */
543 static void test_init(gconstpointer test_data
)
545 const TestData
*td
= test_data
;
546 QTestState
*qts
= qtest_init("-machine npcm750-evb");
547 int module
= pwm_module_index(td
->module
);
548 int pwm
= pwm_index(td
->pwm
);
550 g_assert_cmpuint(pwm_get_freq(qts
, module
, pwm
), ==, 0);
551 g_assert_cmpuint(pwm_get_duty(qts
, module
, pwm
), ==, 0);
556 /* One-shot mode should not change frequency and duty cycle. */
557 static void test_oneshot(gconstpointer test_data
)
559 const TestData
*td
= test_data
;
560 QTestState
*qts
= qtest_init("-machine npcm750-evb");
561 int module
= pwm_module_index(td
->module
);
562 int pwm
= pwm_index(td
->pwm
);
563 uint32_t ppr
, csr
, pcr
;
567 for (i
= 0; i
< ARRAY_SIZE(ppr_list
); ++i
) {
569 pwm_write_ppr(qts
, td
, ppr
);
571 for (j
= 0; j
< ARRAY_SIZE(csr_list
); ++j
) {
573 pwm_write_csr(qts
, td
, csr
);
574 pwm_write_pcr(qts
, td
, pcr
);
576 g_assert_cmpuint(pwm_read_ppr(qts
, td
), ==, ppr
);
577 g_assert_cmpuint(pwm_read_csr(qts
, td
), ==, csr
);
578 g_assert_cmpuint(pwm_read_pcr(qts
, td
), ==, pcr
);
579 g_assert_cmpuint(pwm_get_freq(qts
, module
, pwm
), ==, 0);
580 g_assert_cmpuint(pwm_get_duty(qts
, module
, pwm
), ==, 0);
587 /* In toggle mode, the PWM generates correct outputs. */
588 static void test_toggle(gconstpointer test_data
)
590 const TestData
*td
= test_data
;
591 QTestState
*qts
= qtest_init("-machine npcm750-evb");
592 int module
= pwm_module_index(td
->module
);
593 int pwm
= pwm_index(td
->pwm
);
594 uint32_t ppr
, csr
, pcr
, cnr
, cmr
;
596 uint64_t expected_freq
, expected_duty
;
600 pcr
= CH_EN
| CH_MOD
;
601 for (i
= 0; i
< ARRAY_SIZE(ppr_list
); ++i
) {
603 pwm_write_ppr(qts
, td
, ppr
);
605 for (j
= 0; j
< ARRAY_SIZE(csr_list
); ++j
) {
607 pwm_write_csr(qts
, td
, csr
);
609 for (k
= 0; k
< ARRAY_SIZE(cnr_list
); ++k
) {
611 pwm_write_cnr(qts
, td
, cnr
);
613 for (l
= 0; l
< ARRAY_SIZE(cmr_list
); ++l
) {
615 pwm_write_cmr(qts
, td
, cmr
);
616 expected_freq
= pwm_compute_freq(qts
, ppr
, csr
, cnr
);
617 expected_duty
= pwm_compute_duty(cnr
, cmr
, false);
619 pwm_write_pcr(qts
, td
, pcr
);
620 g_assert_cmpuint(pwm_read_ppr(qts
, td
), ==, ppr
);
621 g_assert_cmpuint(pwm_read_csr(qts
, td
), ==, csr
);
622 g_assert_cmpuint(pwm_read_pcr(qts
, td
), ==, pcr
);
623 g_assert_cmpuint(pwm_read_cnr(qts
, td
), ==, cnr
);
624 g_assert_cmpuint(pwm_read_cmr(qts
, td
), ==, cmr
);
625 g_assert_cmpuint(pwm_get_duty(qts
, module
, pwm
),
627 if (expected_duty
!= 0 && expected_duty
!= 100) {
628 /* Duty cycle with 0 or 100 doesn't need frequency. */
629 g_assert_cmpuint(pwm_get_freq(qts
, module
, pwm
),
633 /* Test MFT's RPM is correct. */
634 mft_verify_rpm(qts
, td
, expected_duty
);
636 /* Test inverted mode */
637 expected_duty
= pwm_compute_duty(cnr
, cmr
, true);
638 pwm_write_pcr(qts
, td
, pcr
| CH_INV
);
639 g_assert_cmpuint(pwm_read_pcr(qts
, td
), ==, pcr
| CH_INV
);
640 g_assert_cmpuint(pwm_get_duty(qts
, module
, pwm
),
642 if (expected_duty
!= 0 && expected_duty
!= 100) {
643 /* Duty cycle with 0 or 100 doesn't need frequency. */
644 g_assert_cmpuint(pwm_get_freq(qts
, module
, pwm
),
656 static void pwm_add_test(const char *name
, const TestData
* td
,
659 g_autofree
char *full_name
= g_strdup_printf(
660 "npcm7xx_pwm/module[%d]/pwm[%d]/%s", pwm_module_index(td
->module
),
661 pwm_index(td
->pwm
), name
);
662 qtest_add_data_func(full_name
, td
, fn
);
664 #define add_test(name, td) pwm_add_test(#name, td, test_##name)
666 int main(int argc
, char **argv
)
668 TestData test_data_list
[ARRAY_SIZE(pwm_module_list
) * ARRAY_SIZE(pwm_list
)];
670 g_test_init(&argc
, &argv
, NULL
);
672 for (int i
= 0; i
< ARRAY_SIZE(pwm_module_list
); ++i
) {
673 for (int j
= 0; j
< ARRAY_SIZE(pwm_list
); ++j
) {
674 TestData
*td
= &test_data_list
[i
* ARRAY_SIZE(pwm_list
) + j
];
676 td
->module
= &pwm_module_list
[i
];
677 td
->pwm
= &pwm_list
[j
];
680 add_test(oneshot
, td
);
681 add_test(toggle
, td
);