xref: /linux/drivers/soc/renesas/rz-sysc.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * RZ System controller driver
4  *
5  * Copyright (C) 2024 Renesas Electronics Corp.
6  */
7 
8 #include <linux/cleanup.h>
9 #include <linux/io.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
15 #include <linux/sys_soc.h>
16 
17 #include "rz-sysc.h"
18 
19 #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
20 
21 /**
22  * struct rz_sysc - RZ SYSC private data structure
23  * @base: SYSC base address
24  * @dev: SYSC device pointer
25  */
26 struct rz_sysc {
27 	void __iomem *base;
28 	struct device *dev;
29 };
30 
31 static int rz_sysc_soc_init(struct rz_sysc *sysc, const struct of_device_id *match)
32 {
33 	const struct rz_sysc_init_data *sysc_data = match->data;
34 	const struct rz_sysc_soc_id_init_data *soc_data = sysc_data->soc_id_init_data;
35 	struct soc_device_attribute *soc_dev_attr;
36 	const char *soc_id_start, *soc_id_end;
37 	u32 val, revision, specific_id;
38 	struct soc_device *soc_dev;
39 	char soc_id[32] = {0};
40 	size_t size;
41 
42 	soc_id_start = strchr(match->compatible, ',') + 1;
43 	soc_id_end = strchr(match->compatible, '-');
44 	size = soc_id_end - soc_id_start + 1;
45 	if (size > 32)
46 		size = sizeof(soc_id);
47 	strscpy(soc_id, soc_id_start, size);
48 
49 	soc_dev_attr = devm_kzalloc(sysc->dev, sizeof(*soc_dev_attr), GFP_KERNEL);
50 	if (!soc_dev_attr)
51 		return -ENOMEM;
52 
53 	soc_dev_attr->family = devm_kstrdup(sysc->dev, soc_data->family, GFP_KERNEL);
54 	if (!soc_dev_attr->family)
55 		return -ENOMEM;
56 
57 	soc_dev_attr->soc_id = devm_kstrdup(sysc->dev, soc_id, GFP_KERNEL);
58 	if (!soc_dev_attr->soc_id)
59 		return -ENOMEM;
60 
61 	val = readl(sysc->base + soc_data->devid_offset);
62 	revision = field_get(soc_data->revision_mask, val);
63 	specific_id = field_get(soc_data->specific_id_mask, val);
64 	soc_dev_attr->revision = devm_kasprintf(sysc->dev, GFP_KERNEL, "%u", revision);
65 	if (!soc_dev_attr->revision)
66 		return -ENOMEM;
67 
68 	if (soc_data->id && specific_id != soc_data->id) {
69 		dev_warn(sysc->dev, "SoC mismatch (product = 0x%x)\n", specific_id);
70 		return -ENODEV;
71 	}
72 
73 	/* Try to call SoC-specific device identification */
74 	if (soc_data->print_id) {
75 		soc_data->print_id(sysc->dev, sysc->base, soc_dev_attr);
76 	} else {
77 		dev_info(sysc->dev, "Detected Renesas %s %s Rev %s\n",
78 			 soc_dev_attr->family, soc_dev_attr->soc_id, soc_dev_attr->revision);
79 	}
80 
81 	soc_dev = soc_device_register(soc_dev_attr);
82 	if (IS_ERR(soc_dev))
83 		return PTR_ERR(soc_dev);
84 
85 	return 0;
86 }
87 
88 static const struct of_device_id rz_sysc_match[] = {
89 #ifdef CONFIG_SYSC_R9A08G045
90 	{ .compatible = "renesas,r9a08g045-sysc", .data = &rzg3s_sysc_init_data },
91 #endif
92 #ifdef CONFIG_SYS_R9A09G047
93 	{ .compatible = "renesas,r9a09g047-sys", .data = &rzg3e_sys_init_data },
94 #endif
95 #ifdef CONFIG_SYS_R9A09G056
96 	{ .compatible = "renesas,r9a09g056-sys", .data = &rzv2n_sys_init_data },
97 #endif
98 #ifdef CONFIG_SYS_R9A09G057
99 	{ .compatible = "renesas,r9a09g057-sys", .data = &rzv2h_sys_init_data },
100 #endif
101 	{ }
102 };
103 MODULE_DEVICE_TABLE(of, rz_sysc_match);
104 
105 static int rz_sysc_probe(struct platform_device *pdev)
106 {
107 	const struct rz_sysc_init_data *data;
108 	const struct of_device_id *match;
109 	struct device *dev = &pdev->dev;
110 	struct regmap *regmap;
111 	struct rz_sysc *sysc;
112 	int ret;
113 
114 	struct regmap_config *regmap_cfg __free(kfree) = kzalloc(sizeof(*regmap_cfg), GFP_KERNEL);
115 	if (!regmap_cfg)
116 		return -ENOMEM;
117 
118 	match = of_match_node(rz_sysc_match, dev->of_node);
119 	if (!match)
120 		return -ENODEV;
121 
122 	data = match->data;
123 
124 	sysc = devm_kzalloc(dev, sizeof(*sysc), GFP_KERNEL);
125 	if (!sysc)
126 		return -ENOMEM;
127 
128 	sysc->base = devm_platform_ioremap_resource(pdev, 0);
129 	if (IS_ERR(sysc->base))
130 		return PTR_ERR(sysc->base);
131 
132 	sysc->dev = dev;
133 	ret = rz_sysc_soc_init(sysc, match);
134 	if (ret)
135 		return ret;
136 
137 	regmap_cfg->name = "rz_sysc_regs";
138 	regmap_cfg->reg_bits = 32;
139 	regmap_cfg->reg_stride = 4;
140 	regmap_cfg->val_bits = 32;
141 	regmap_cfg->fast_io = true;
142 	regmap_cfg->max_register = data->max_register;
143 
144 	regmap = devm_regmap_init_mmio(dev, sysc->base, regmap_cfg);
145 	if (IS_ERR(regmap))
146 		return PTR_ERR(regmap);
147 
148 	return of_syscon_register_regmap(dev->of_node, regmap);
149 }
150 
151 static struct platform_driver rz_sysc_driver = {
152 	.driver = {
153 		.name = "renesas-rz-sysc",
154 		.suppress_bind_attrs = true,
155 		.of_match_table = rz_sysc_match
156 	},
157 	.probe = rz_sysc_probe
158 };
159 
160 static int __init rz_sysc_init(void)
161 {
162 	return platform_driver_register(&rz_sysc_driver);
163 }
164 subsys_initcall(rz_sysc_init);
165 
166 MODULE_DESCRIPTION("Renesas RZ System Controller Driver");
167 MODULE_AUTHOR("Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com>");
168 MODULE_LICENSE("GPL");
169