1 /*
2  * Copyright (c) 2020 Intel Corporation
3  * Copyright (c) 2022 Carlo Caione <ccaione@baylibre.com>
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/ztest.h>
9 #include <zephyr/device.h>
10 
11 #define DT_DRV_COMPAT	fakedriver_multireg
12 
13 /*
14  * Driver with multiple MMIO regions to manage defined into DT
15  */
16 
17 struct foo_multireg_dev_data {
18 	int baz;
19 
20 	DEVICE_MMIO_NAMED_RAM(chip);
21 	DEVICE_MMIO_NAMED_RAM(dale);
22 };
23 
24 struct foo_multireg_dev_data foo_multireg_data;
25 
26 struct foo_multireg_config_info {
27 	DEVICE_MMIO_NAMED_ROM(chip);
28 	DEVICE_MMIO_NAMED_ROM(dale);
29 };
30 
31 const struct foo_multireg_config_info foo_multireg_config = {
32 	DEVICE_MMIO_NAMED_ROM_INIT_BY_NAME(chip, DT_DRV_INST(0)),
33 	DEVICE_MMIO_NAMED_ROM_INIT_BY_NAME(dale, DT_DRV_INST(0))
34 };
35 
36 #define DEV_DATA(dev)	((struct foo_multireg_dev_data *)((dev)->data))
37 #define DEV_CFG(dev)	((struct foo_multireg_config_info *)((dev)->config))
38 
foo_multireg_init(const struct device * dev)39 int foo_multireg_init(const struct device *dev)
40 {
41 	DEVICE_MMIO_NAMED_MAP(dev, chip, K_MEM_CACHE_NONE);
42 	DEVICE_MMIO_NAMED_MAP(dev, dale, K_MEM_CACHE_NONE);
43 
44 	return 0;
45 }
46 
47 DEVICE_DEFINE(foo_multireg, "foo_multireg", foo_multireg_init, NULL,
48 		&foo_multireg_data, &foo_multireg_config,
49 		POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
50 		(void *)0xDEADBEEF);
51 /**
52  * @brief Test DEVICE_MMIO_NAMED_* macros
53  *
54  * This is the same as the test_mmio_multiple test but in this test the
55  * memory regions are created by the named DT property 'reg'.
56  *
57  * @see test_mmio_multiple
58  *
59  * @ingroup kernel_device_tests
60  */
ZTEST(device,test_mmio_multireg)61 ZTEST(device, test_mmio_multireg)
62 {
63 	const struct device *dev = device_get_binding("foo_multireg");
64 	mm_reg_t regs_chip, regs_dale;
65 	const struct z_device_mmio_rom *rom_chip, *rom_dale;
66 
67 	zassert_not_null(dev, "null foo_multireg");
68 
69 	regs_chip = DEVICE_MMIO_NAMED_GET(dev, chip);
70 	regs_dale = DEVICE_MMIO_NAMED_GET(dev, dale);
71 	rom_chip = DEVICE_MMIO_NAMED_ROM_PTR(dev, chip);
72 	rom_dale = DEVICE_MMIO_NAMED_ROM_PTR(dev, dale);
73 
74 	zassert_not_equal(regs_chip, 0, "bad regs_chip");
75 	zassert_not_equal(regs_dale, 0, "bad regs_dale");
76 
77 #ifdef DEVICE_MMIO_IS_IN_RAM
78 	zassert_equal(rom_chip->phys_addr, DT_INST_REG_ADDR_BY_NAME(0, chip),
79 		      "bad phys_addr (chip)");
80 	zassert_equal(rom_chip->size, DT_INST_REG_SIZE_BY_NAME(0, chip),
81 		      "bad size (chip)");
82 	zassert_equal(rom_dale->phys_addr, DT_INST_REG_ADDR_BY_NAME(0, dale),
83 		      "bad phys_addr (dale)");
84 	zassert_equal(rom_dale->size, DT_INST_REG_SIZE_BY_NAME(0, dale),
85 		      "bad size (dale)");
86 #else
87 	zassert_equal(rom_chip->addr, DT_INST_REG_ADDR_BY_NAME(0, chip),
88 		      "bad addr (chip)");
89 	zassert_equal(regs_chip, rom_chip->addr, "bad regs (chip)");
90 	zassert_equal(rom_dale->addr, DT_INST_REG_ADDR_BY_NAME(0, dale),
91 		      "bad addr (dale)");
92 	zassert_equal(regs_dale, rom_dale->addr, "bad regs (dale)");
93 	zassert_equal(sizeof(struct foo_multireg_dev_data), sizeof(int),
94 		      "too big foo_multireg_dev_data");
95 #endif
96 }
97