xref: /linux/drivers/bus/stm32_etzpc.c (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2023, STMicroelectronics - All Rights Reserved
4  */
5 
6 #include <linux/bitfield.h>
7 #include <linux/bits.h>
8 #include <linux/bus/stm32_firewall.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_platform.h>
17 #include <linux/platform_device.h>
18 #include <linux/types.h>
19 
20 /*
21  * ETZPC registers
22  */
23 #define ETZPC_DECPROT			0x10
24 #define ETZPC_HWCFGR			0x3F0
25 
26 /*
27  * HWCFGR register
28  */
29 #define ETZPC_HWCFGR_NUM_TZMA		GENMASK(7, 0)
30 #define ETZPC_HWCFGR_NUM_PER_SEC	GENMASK(15, 8)
31 #define ETZPC_HWCFGR_NUM_AHB_SEC	GENMASK(23, 16)
32 #define ETZPC_HWCFGR_CHUNKS1N4		GENMASK(31, 24)
33 
34 /*
35  * ETZPC miscellaneous
36  */
37 #define ETZPC_PROT_MASK			GENMASK(1, 0)
38 #define ETZPC_PROT_A7NS			0x3
39 #define ETZPC_DECPROT_SHIFT		1
40 
41 #define IDS_PER_DECPROT_REGS		16
42 
43 static int stm32_etzpc_grant_access(struct stm32_firewall_controller *ctrl, u32 firewall_id)
44 {
45 	u32 offset, reg_offset, sec_val;
46 
47 	if (firewall_id >= ctrl->max_entries) {
48 		dev_err(ctrl->dev, "Invalid sys bus ID %u", firewall_id);
49 		return -EINVAL;
50 	}
51 
52 	/* Check access configuration, 16 peripherals per register */
53 	reg_offset = ETZPC_DECPROT + 0x4 * (firewall_id / IDS_PER_DECPROT_REGS);
54 	offset = (firewall_id % IDS_PER_DECPROT_REGS) << ETZPC_DECPROT_SHIFT;
55 
56 	/* Verify peripheral is non-secure and attributed to cortex A7 */
57 	sec_val = (readl(ctrl->mmio + reg_offset) >> offset) & ETZPC_PROT_MASK;
58 	if (sec_val != ETZPC_PROT_A7NS) {
59 		dev_dbg(ctrl->dev, "Invalid bus configuration: reg_offset %#x, value %d\n",
60 			reg_offset, sec_val);
61 		return -EACCES;
62 	}
63 
64 	return 0;
65 }
66 
67 static void stm32_etzpc_release_access(struct stm32_firewall_controller *ctrl __maybe_unused,
68 				       u32 firewall_id __maybe_unused)
69 {
70 }
71 
72 static int stm32_etzpc_probe(struct platform_device *pdev)
73 {
74 	struct stm32_firewall_controller *etzpc_controller;
75 	struct device_node *np = pdev->dev.of_node;
76 	u32 nb_per, nb_master;
77 	struct resource *res;
78 	void __iomem *mmio;
79 	int rc;
80 
81 	etzpc_controller = devm_kzalloc(&pdev->dev, sizeof(*etzpc_controller), GFP_KERNEL);
82 	if (!etzpc_controller)
83 		return -ENOMEM;
84 
85 	mmio = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
86 	if (IS_ERR(mmio))
87 		return PTR_ERR(mmio);
88 
89 	etzpc_controller->dev = &pdev->dev;
90 	etzpc_controller->mmio = mmio;
91 	etzpc_controller->name = dev_driver_string(etzpc_controller->dev);
92 	etzpc_controller->type = STM32_PERIPHERAL_FIREWALL | STM32_MEMORY_FIREWALL;
93 	etzpc_controller->grant_access = stm32_etzpc_grant_access;
94 	etzpc_controller->release_access = stm32_etzpc_release_access;
95 
96 	/* Get number of etzpc entries*/
97 	nb_per = FIELD_GET(ETZPC_HWCFGR_NUM_PER_SEC,
98 			   readl(etzpc_controller->mmio + ETZPC_HWCFGR));
99 	nb_master = FIELD_GET(ETZPC_HWCFGR_NUM_AHB_SEC,
100 			      readl(etzpc_controller->mmio + ETZPC_HWCFGR));
101 	etzpc_controller->max_entries = nb_per + nb_master;
102 
103 	platform_set_drvdata(pdev, etzpc_controller);
104 
105 	rc = stm32_firewall_controller_register(etzpc_controller);
106 	if (rc) {
107 		dev_err(etzpc_controller->dev, "Couldn't register as a firewall controller: %d",
108 			rc);
109 		return rc;
110 	}
111 
112 	rc = stm32_firewall_populate_bus(etzpc_controller);
113 	if (rc) {
114 		dev_err(etzpc_controller->dev, "Couldn't populate ETZPC bus: %d",
115 			rc);
116 		return rc;
117 	}
118 
119 	/* Populate all allowed nodes */
120 	return of_platform_populate(np, NULL, NULL, &pdev->dev);
121 }
122 
123 static const struct of_device_id stm32_etzpc_of_match[] = {
124 	{ .compatible = "st,stm32-etzpc" },
125 	{}
126 };
127 MODULE_DEVICE_TABLE(of, stm32_etzpc_of_match);
128 
129 static struct platform_driver stm32_etzpc_driver = {
130 	.probe  = stm32_etzpc_probe,
131 	.driver = {
132 		.name = "stm32-etzpc",
133 		.of_match_table = stm32_etzpc_of_match,
134 	},
135 };
136 module_platform_driver(stm32_etzpc_driver);
137 
138 MODULE_AUTHOR("Gatien Chevallier <gatien.chevallier@foss.st.com>");
139 MODULE_DESCRIPTION("STMicroelectronics ETZPC driver");
140 MODULE_LICENSE("GPL");
141