1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Test for irq uclass
4  *
5  * Copyright 2019 Google LLC
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <irq.h>
11 #include <acpi/acpi_device.h>
12 #include <asm/test.h>
13 #include <dm/test.h>
14 #include <test/ut.h>
15 
16 /* Base test of the irq uclass */
dm_test_irq_base(struct unit_test_state * uts)17 static int dm_test_irq_base(struct unit_test_state *uts)
18 {
19 	struct udevice *dev;
20 
21 	ut_assertok(uclass_first_device_err(UCLASS_IRQ, &dev));
22 
23 	ut_asserteq(5, irq_route_pmc_gpio_gpe(dev, 4));
24 	ut_asserteq(-ENOENT, irq_route_pmc_gpio_gpe(dev, 14));
25 
26 	ut_assertok(irq_set_polarity(dev, 4, true));
27 	ut_asserteq(-EINVAL, irq_set_polarity(dev, 14, true));
28 
29 	ut_assertok(irq_snapshot_polarities(dev));
30 	ut_assertok(irq_restore_polarities(dev));
31 
32 	return 0;
33 }
34 DM_TEST(dm_test_irq_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
35 
36 /* Test of irq_first_device_type() */
dm_test_irq_type(struct unit_test_state * uts)37 static int dm_test_irq_type(struct unit_test_state *uts)
38 {
39 	struct udevice *dev;
40 
41 	ut_assertok(irq_first_device_type(SANDBOX_IRQT_BASE, &dev));
42 	ut_asserteq(-ENODEV, irq_first_device_type(X86_IRQT_BASE, &dev));
43 
44 	return 0;
45 }
46 DM_TEST(dm_test_irq_type, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
47 
48 /* Test of irq_read_and_clear() */
dm_test_read_and_clear(struct unit_test_state * uts)49 static int dm_test_read_and_clear(struct unit_test_state *uts)
50 {
51 	struct irq irq;
52 
53 	ut_assertok(irq_first_device_type(SANDBOX_IRQT_BASE, &irq.dev));
54 	irq.id = SANDBOX_IRQN_PEND;
55 	ut_asserteq(0, irq_read_and_clear(&irq));
56 	ut_asserteq(0, irq_read_and_clear(&irq));
57 	ut_asserteq(0, irq_read_and_clear(&irq));
58 	ut_asserteq(1, irq_read_and_clear(&irq));
59 	ut_asserteq(0, irq_read_and_clear(&irq));
60 
61 	return 0;
62 }
63 DM_TEST(dm_test_read_and_clear, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
64 
65 /* Test of irq_request() */
dm_test_request(struct unit_test_state * uts)66 static int dm_test_request(struct unit_test_state *uts)
67 {
68 	struct udevice *dev;
69 	struct irq irq;
70 
71 	ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
72 	ut_asserteq_str("a-test", dev->name);
73 	ut_assertok(irq_get_by_index(dev, 0, &irq));
74 	ut_asserteq(3, irq.id);
75 
76 	return 0;
77 }
78 DM_TEST(dm_test_request, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
79 
80 /* Test of irq_get_acpi() */
dm_test_irq_get_acpi(struct unit_test_state * uts)81 static int dm_test_irq_get_acpi(struct unit_test_state *uts)
82 {
83 	struct acpi_irq airq;
84 	struct udevice *dev;
85 	struct irq irq;
86 
87 	ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
88 	ut_assertok(irq_get_by_index(dev, 0, &irq));
89 
90 	/* see sandbox_get_acpi() */
91 	ut_assertok(irq_get_acpi(&irq, &airq));
92 	ut_asserteq(3, airq.pin);
93 	ut_asserteq(ACPI_IRQ_LEVEL_TRIGGERED, airq.mode);
94 	ut_asserteq(ACPI_IRQ_ACTIVE_HIGH, airq.polarity);
95 	ut_asserteq(ACPI_IRQ_SHARED, airq.shared);
96 	ut_asserteq(ACPI_IRQ_WAKE, airq.wake);
97 
98 	return 0;
99 }
100 DM_TEST(dm_test_irq_get_acpi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
101