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"
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"));
272 qobject_unref(response
);
276 static uint32_t get_pll(uint32_t con
)
278 return REF_HZ
* PLL_FBDV(con
) / (PLL_INDV(con
) * PLL_OTDV1(con
)
282 static uint64_t read_pclk(QTestState
*qts
, bool mft
)
284 uint64_t freq
= REF_HZ
;
285 uint32_t clksel
= qtest_readl(qts
, CLK_BA
+ CLKSEL
);
287 uint32_t clkdiv1
= qtest_readl(qts
, CLK_BA
+ CLKDIV1
);
288 uint32_t clkdiv2
= qtest_readl(qts
, CLK_BA
+ CLKDIV2
);
289 uint32_t apbdiv
= mft
? APB4CKDIV(clkdiv2
) : APB3CKDIV(clkdiv2
);
291 switch (CPUCKSEL(clksel
)) {
293 pllcon
= qtest_readl(qts
, CLK_BA
+ PLLCON0
);
294 freq
= get_pll(pllcon
);
297 pllcon
= qtest_readl(qts
, CLK_BA
+ PLLCON1
);
298 freq
= get_pll(pllcon
);
305 g_assert_not_reached();
308 freq
>>= (CLK2CKDIV(clkdiv1
) + CLK4CKDIV(clkdiv1
) + apbdiv
);
313 static uint32_t pwm_selector(uint32_t csr
)
327 g_assert_not_reached();
331 static uint64_t pwm_compute_freq(QTestState
*qts
, uint32_t ppr
, uint32_t csr
,
334 return read_pclk(qts
, false) / ((ppr
+ 1) * pwm_selector(csr
) * (cnr
+ 1));
337 static uint64_t pwm_compute_duty(uint32_t cnr
, uint32_t cmr
, bool inverted
)
342 /* PWM is stopped. */
344 } else if (cmr
>= cnr
) {
347 duty
= (uint64_t)MAX_DUTY
* (cmr
+ 1) / (cnr
+ 1);
351 duty
= MAX_DUTY
- duty
;
357 static uint32_t pwm_read(QTestState
*qts
, const TestData
*td
, unsigned offset
)
359 return qtest_readl(qts
, td
->module
->base_addr
+ offset
);
362 static void pwm_write(QTestState
*qts
, const TestData
*td
, unsigned offset
,
365 qtest_writel(qts
, td
->module
->base_addr
+ offset
, value
);
368 static uint8_t mft_readb(QTestState
*qts
, int index
, unsigned offset
)
370 return qtest_readb(qts
, MFT_BA(index
) + offset
);
373 static uint16_t mft_readw(QTestState
*qts
, int index
, unsigned offset
)
375 return qtest_readw(qts
, MFT_BA(index
) + offset
);
378 static void mft_writeb(QTestState
*qts
, int index
, unsigned offset
,
381 qtest_writeb(qts
, MFT_BA(index
) + offset
, value
);
384 static void mft_writew(QTestState
*qts
, int index
, unsigned offset
,
387 return qtest_writew(qts
, MFT_BA(index
) + offset
, value
);
390 static uint32_t pwm_read_ppr(QTestState
*qts
, const TestData
*td
)
392 return extract32(pwm_read(qts
, td
, PPR
), ppr_base
[pwm_index(td
->pwm
)], 8);
395 static void pwm_write_ppr(QTestState
*qts
, const TestData
*td
, uint32_t value
)
397 pwm_write(qts
, td
, PPR
, value
<< ppr_base
[pwm_index(td
->pwm
)]);
400 static uint32_t pwm_read_csr(QTestState
*qts
, const TestData
*td
)
402 return extract32(pwm_read(qts
, td
, CSR
), csr_base
[pwm_index(td
->pwm
)], 3);
405 static void pwm_write_csr(QTestState
*qts
, const TestData
*td
, uint32_t value
)
407 pwm_write(qts
, td
, CSR
, value
<< csr_base
[pwm_index(td
->pwm
)]);
410 static uint32_t pwm_read_pcr(QTestState
*qts
, const TestData
*td
)
412 return extract32(pwm_read(qts
, td
, PCR
), pcr_base
[pwm_index(td
->pwm
)], 4);
415 static void pwm_write_pcr(QTestState
*qts
, const TestData
*td
, uint32_t value
)
417 pwm_write(qts
, td
, PCR
, value
<< pcr_base
[pwm_index(td
->pwm
)]);
420 static uint32_t pwm_read_cnr(QTestState
*qts
, const TestData
*td
)
422 return pwm_read(qts
, td
, td
->pwm
->cnr_offset
);
425 static void pwm_write_cnr(QTestState
*qts
, const TestData
*td
, uint32_t value
)
427 pwm_write(qts
, td
, td
->pwm
->cnr_offset
, value
);
430 static uint32_t pwm_read_cmr(QTestState
*qts
, const TestData
*td
)
432 return pwm_read(qts
, td
, td
->pwm
->cmr_offset
);
435 static void pwm_write_cmr(QTestState
*qts
, const TestData
*td
, uint32_t value
)
437 pwm_write(qts
, td
, td
->pwm
->cmr_offset
, value
);
440 static int mft_compute_index(const TestData
*td
)
442 int index
= pwm_module_index(td
->module
) * ARRAY_SIZE(pwm_list
) +
445 g_assert_cmpint(index
, <,
446 ARRAY_SIZE(pwm_module_list
) * ARRAY_SIZE(pwm_list
));
451 static void mft_reset_counters(QTestState
*qts
, int index
)
453 mft_writew(qts
, index
, MFT_CNT1
, MFT_MAX_CNT
);
454 mft_writew(qts
, index
, MFT_CNT2
, MFT_MAX_CNT
);
455 mft_writew(qts
, index
, MFT_CRA
, MFT_MAX_CNT
);
456 mft_writew(qts
, index
, MFT_CRB
, MFT_MAX_CNT
);
457 mft_writew(qts
, index
, MFT_CPA
, MFT_MAX_CNT
- MFT_TIMEOUT
);
458 mft_writew(qts
, index
, MFT_CPB
, MFT_MAX_CNT
- MFT_TIMEOUT
);
461 static void mft_init(QTestState
*qts
, const TestData
*td
)
463 int index
= mft_compute_index(td
);
465 /* Enable everything */
466 mft_writeb(qts
, index
, MFT_CKC
, 0);
467 mft_writeb(qts
, index
, MFT_ICLR
, MFT_ICLR_ALL
);
468 mft_writeb(qts
, index
, MFT_MCTRL
, MFT_MCTRL_ALL
);
469 mft_writeb(qts
, index
, MFT_IEN
, MFT_IEN_ALL
);
470 mft_writeb(qts
, index
, MFT_INASEL
, 0);
471 mft_writeb(qts
, index
, MFT_INBSEL
, 0);
473 /* Set cpcfg to use EQ mode, same as kernel driver */
474 mft_writeb(qts
, index
, MFT_CPCFG
, MFT_CPCFG_EQ_MODE
);
476 /* Write default counters, timeout and prescaler */
477 mft_reset_counters(qts
, index
);
478 mft_writeb(qts
, index
, MFT_PRSC
, DEFAULT_PRSC
);
480 /* Write default max rpm via QMP */
481 mft_qom_set(qts
, index
, "max_rpm[0]", DEFAULT_RPM
);
482 mft_qom_set(qts
, index
, "max_rpm[1]", DEFAULT_RPM
);
485 static int32_t mft_compute_cnt(uint32_t rpm
, uint64_t clk
)
493 cnt
= clk
* 60 / ((DEFAULT_PRSC
+ 1) * rpm
* MFT_PULSE_PER_REVOLUTION
);
494 if (cnt
>= MFT_TIMEOUT
) {
497 return MFT_MAX_CNT
- cnt
;
500 static void mft_verify_rpm(QTestState
*qts
, const TestData
*td
, uint64_t duty
)
502 int index
= mft_compute_index(td
);
504 uint32_t rpm
= DEFAULT_RPM
* duty
/ MAX_DUTY
;
505 uint64_t clk
= read_pclk(qts
, true);
506 int32_t expected_cnt
= mft_compute_cnt(rpm
, clk
);
508 qtest_irq_intercept_in(qts
, "/machine/soc/a9mpcore/gic");
510 "verifying rpm for mft[%d]: clk: %" PRIu64
", duty: %" PRIu64
", rpm: %u, cnt: %d",
511 index
, clk
, duty
, rpm
, expected_cnt
);
513 /* Verify rpm for fan A */
515 mft_writeb(qts
, index
, MFT_CKC
, 0);
516 mft_writeb(qts
, index
, MFT_ICLR
, MFT_ICLR_ALL
);
517 mft_reset_counters(qts
, index
);
518 g_assert_cmphex(mft_readw(qts
, index
, MFT_CNT1
), ==, MFT_MAX_CNT
);
519 g_assert_cmphex(mft_readw(qts
, index
, MFT_CRA
), ==, MFT_MAX_CNT
);
520 g_assert_cmphex(mft_readw(qts
, index
, MFT_CPA
), ==,
521 MFT_MAX_CNT
- MFT_TIMEOUT
);
523 mft_writeb(qts
, index
, MFT_CKC
, MFT_CKC_C1CSEL
);
524 g_assert_true(qtest_get_irq(qts
, MFT_IRQ(index
)));
525 if (expected_cnt
== -1) {
526 g_assert_cmphex(mft_readb(qts
, index
, MFT_ICTRL
), ==, MFT_ICTRL_TEPND
);
528 g_assert_cmphex(mft_readb(qts
, index
, MFT_ICTRL
), ==, MFT_ICTRL_TAPND
);
529 cnt
= mft_readw(qts
, index
, MFT_CNT1
);
531 * Due to error in clock measurement and rounding, we might have a small
532 * error in measuring RPM.
534 g_assert_cmphex(cnt
+ MAX_ERROR
, >=, expected_cnt
);
535 g_assert_cmphex(cnt
, <=, expected_cnt
+ MAX_ERROR
);
536 cr
= mft_readw(qts
, index
, MFT_CRA
);
537 g_assert_cmphex(cnt
, ==, cr
);
540 /* Verify rpm for fan B */
542 qtest_irq_intercept_out(qts
, "/machine/soc/a9mpcore/gic");
545 /* Check pwm registers can be reset to default value */
546 static void test_init(gconstpointer test_data
)
548 const TestData
*td
= test_data
;
549 QTestState
*qts
= qtest_init("-machine npcm750-evb");
550 int module
= pwm_module_index(td
->module
);
551 int pwm
= pwm_index(td
->pwm
);
553 g_assert_cmpuint(pwm_get_freq(qts
, module
, pwm
), ==, 0);
554 g_assert_cmpuint(pwm_get_duty(qts
, module
, pwm
), ==, 0);
559 /* One-shot mode should not change frequency and duty cycle. */
560 static void test_oneshot(gconstpointer test_data
)
562 const TestData
*td
= test_data
;
563 QTestState
*qts
= qtest_init("-machine npcm750-evb");
564 int module
= pwm_module_index(td
->module
);
565 int pwm
= pwm_index(td
->pwm
);
566 uint32_t ppr
, csr
, pcr
;
570 for (i
= 0; i
< ARRAY_SIZE(ppr_list
); ++i
) {
572 pwm_write_ppr(qts
, td
, ppr
);
574 for (j
= 0; j
< ARRAY_SIZE(csr_list
); ++j
) {
576 pwm_write_csr(qts
, td
, csr
);
577 pwm_write_pcr(qts
, td
, pcr
);
579 g_assert_cmpuint(pwm_read_ppr(qts
, td
), ==, ppr
);
580 g_assert_cmpuint(pwm_read_csr(qts
, td
), ==, csr
);
581 g_assert_cmpuint(pwm_read_pcr(qts
, td
), ==, pcr
);
582 g_assert_cmpuint(pwm_get_freq(qts
, module
, pwm
), ==, 0);
583 g_assert_cmpuint(pwm_get_duty(qts
, module
, pwm
), ==, 0);
590 /* In toggle mode, the PWM generates correct outputs. */
591 static void test_toggle(gconstpointer test_data
)
593 const TestData
*td
= test_data
;
594 QTestState
*qts
= qtest_init("-machine npcm750-evb");
595 int module
= pwm_module_index(td
->module
);
596 int pwm
= pwm_index(td
->pwm
);
597 uint32_t ppr
, csr
, pcr
, cnr
, cmr
;
599 uint64_t expected_freq
, expected_duty
;
603 pcr
= CH_EN
| CH_MOD
;
604 for (i
= 0; i
< ARRAY_SIZE(ppr_list
); ++i
) {
606 pwm_write_ppr(qts
, td
, ppr
);
608 for (j
= 0; j
< ARRAY_SIZE(csr_list
); ++j
) {
610 pwm_write_csr(qts
, td
, csr
);
612 for (k
= 0; k
< ARRAY_SIZE(cnr_list
); ++k
) {
614 pwm_write_cnr(qts
, td
, cnr
);
616 for (l
= 0; l
< ARRAY_SIZE(cmr_list
); ++l
) {
618 pwm_write_cmr(qts
, td
, cmr
);
619 expected_freq
= pwm_compute_freq(qts
, ppr
, csr
, cnr
);
620 expected_duty
= pwm_compute_duty(cnr
, cmr
, false);
622 pwm_write_pcr(qts
, td
, pcr
);
623 g_assert_cmpuint(pwm_read_ppr(qts
, td
), ==, ppr
);
624 g_assert_cmpuint(pwm_read_csr(qts
, td
), ==, csr
);
625 g_assert_cmpuint(pwm_read_pcr(qts
, td
), ==, pcr
);
626 g_assert_cmpuint(pwm_read_cnr(qts
, td
), ==, cnr
);
627 g_assert_cmpuint(pwm_read_cmr(qts
, td
), ==, cmr
);
628 g_assert_cmpuint(pwm_get_duty(qts
, module
, pwm
),
630 if (expected_duty
!= 0 && expected_duty
!= 100) {
631 /* Duty cycle with 0 or 100 doesn't need frequency. */
632 g_assert_cmpuint(pwm_get_freq(qts
, module
, pwm
),
636 /* Test MFT's RPM is correct. */
637 mft_verify_rpm(qts
, td
, expected_duty
);
639 /* Test inverted mode */
640 expected_duty
= pwm_compute_duty(cnr
, cmr
, true);
641 pwm_write_pcr(qts
, td
, pcr
| CH_INV
);
642 g_assert_cmpuint(pwm_read_pcr(qts
, td
), ==, pcr
| CH_INV
);
643 g_assert_cmpuint(pwm_get_duty(qts
, module
, pwm
),
645 if (expected_duty
!= 0 && expected_duty
!= 100) {
646 /* Duty cycle with 0 or 100 doesn't need frequency. */
647 g_assert_cmpuint(pwm_get_freq(qts
, module
, pwm
),
659 static void pwm_add_test(const char *name
, const TestData
* td
,
662 g_autofree
char *full_name
= g_strdup_printf(
663 "npcm7xx_pwm/module[%d]/pwm[%d]/%s", pwm_module_index(td
->module
),
664 pwm_index(td
->pwm
), name
);
665 qtest_add_data_func(full_name
, td
, fn
);
667 #define add_test(name, td) pwm_add_test(#name, td, test_##name)
669 int main(int argc
, char **argv
)
671 TestData test_data_list
[ARRAY_SIZE(pwm_module_list
) * ARRAY_SIZE(pwm_list
)];
673 g_test_init(&argc
, &argv
, NULL
);
675 for (int i
= 0; i
< ARRAY_SIZE(pwm_module_list
); ++i
) {
676 for (int j
= 0; j
< ARRAY_SIZE(pwm_list
); ++j
) {
677 TestData
*td
= &test_data_list
[i
* ARRAY_SIZE(pwm_list
) + j
];
679 td
->module
= &pwm_module_list
[i
];
680 td
->pwm
= &pwm_list
[j
];
683 add_test(oneshot
, td
);
684 add_test(toggle
, td
);