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