xref: /openbmc/linux/drivers/soc/fsl/guts.c (revision b7019ac5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Freescale QorIQ Platforms GUTS Driver
4  *
5  * Copyright (C) 2016 Freescale Semiconductor, Inc.
6  */
7 
8 #include <linux/io.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/of_fdt.h>
12 #include <linux/sys_soc.h>
13 #include <linux/of_address.h>
14 #include <linux/platform_device.h>
15 #include <linux/fsl/guts.h>
16 
17 struct guts {
18 	struct ccsr_guts __iomem *regs;
19 	bool little_endian;
20 };
21 
22 struct fsl_soc_die_attr {
23 	char	*die;
24 	u32	svr;
25 	u32	mask;
26 };
27 
28 static struct guts *guts;
29 static struct soc_device_attribute soc_dev_attr;
30 static struct soc_device *soc_dev;
31 static struct device_node *root;
32 
33 
34 /* SoC die attribute definition for QorIQ platform */
35 static const struct fsl_soc_die_attr fsl_soc_die[] = {
36 	/*
37 	 * Power Architecture-based SoCs T Series
38 	 */
39 
40 	/* Die: T4240, SoC: T4240/T4160/T4080 */
41 	{ .die		= "T4240",
42 	  .svr		= 0x82400000,
43 	  .mask		= 0xfff00000,
44 	},
45 	/* Die: T1040, SoC: T1040/T1020/T1042/T1022 */
46 	{ .die		= "T1040",
47 	  .svr		= 0x85200000,
48 	  .mask		= 0xfff00000,
49 	},
50 	/* Die: T2080, SoC: T2080/T2081 */
51 	{ .die		= "T2080",
52 	  .svr		= 0x85300000,
53 	  .mask		= 0xfff00000,
54 	},
55 	/* Die: T1024, SoC: T1024/T1014/T1023/T1013 */
56 	{ .die		= "T1024",
57 	  .svr		= 0x85400000,
58 	  .mask		= 0xfff00000,
59 	},
60 
61 	/*
62 	 * ARM-based SoCs LS Series
63 	 */
64 
65 	/* Die: LS1043A, SoC: LS1043A/LS1023A */
66 	{ .die		= "LS1043A",
67 	  .svr		= 0x87920000,
68 	  .mask		= 0xffff0000,
69 	},
70 	/* Die: LS2080A, SoC: LS2080A/LS2040A/LS2085A */
71 	{ .die		= "LS2080A",
72 	  .svr		= 0x87010000,
73 	  .mask		= 0xff3f0000,
74 	},
75 	/* Die: LS1088A, SoC: LS1088A/LS1048A/LS1084A/LS1044A */
76 	{ .die		= "LS1088A",
77 	  .svr		= 0x87030000,
78 	  .mask		= 0xff3f0000,
79 	},
80 	/* Die: LS1012A, SoC: LS1012A */
81 	{ .die		= "LS1012A",
82 	  .svr		= 0x87040000,
83 	  .mask		= 0xffff0000,
84 	},
85 	/* Die: LS1046A, SoC: LS1046A/LS1026A */
86 	{ .die		= "LS1046A",
87 	  .svr		= 0x87070000,
88 	  .mask		= 0xffff0000,
89 	},
90 	/* Die: LS2088A, SoC: LS2088A/LS2048A/LS2084A/LS2044A */
91 	{ .die		= "LS2088A",
92 	  .svr		= 0x87090000,
93 	  .mask		= 0xff3f0000,
94 	},
95 	/* Die: LS1021A, SoC: LS1021A/LS1020A/LS1022A */
96 	{ .die		= "LS1021A",
97 	  .svr		= 0x87000000,
98 	  .mask		= 0xfff70000,
99 	},
100 	{ },
101 };
102 
103 static const struct fsl_soc_die_attr *fsl_soc_die_match(
104 	u32 svr, const struct fsl_soc_die_attr *matches)
105 {
106 	while (matches->svr) {
107 		if (matches->svr == (svr & matches->mask))
108 			return matches;
109 		matches++;
110 	};
111 	return NULL;
112 }
113 
114 static u32 fsl_guts_get_svr(void)
115 {
116 	u32 svr = 0;
117 
118 	if (!guts || !guts->regs)
119 		return svr;
120 
121 	if (guts->little_endian)
122 		svr = ioread32(&guts->regs->svr);
123 	else
124 		svr = ioread32be(&guts->regs->svr);
125 
126 	return svr;
127 }
128 
129 static int fsl_guts_probe(struct platform_device *pdev)
130 {
131 	struct device_node *np = pdev->dev.of_node;
132 	struct device *dev = &pdev->dev;
133 	struct resource *res;
134 	const struct fsl_soc_die_attr *soc_die;
135 	const char *machine;
136 	u32 svr;
137 
138 	/* Initialize guts */
139 	guts = devm_kzalloc(dev, sizeof(*guts), GFP_KERNEL);
140 	if (!guts)
141 		return -ENOMEM;
142 
143 	guts->little_endian = of_property_read_bool(np, "little-endian");
144 
145 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
146 	guts->regs = devm_ioremap_resource(dev, res);
147 	if (IS_ERR(guts->regs))
148 		return PTR_ERR(guts->regs);
149 
150 	/* Register soc device */
151 	root = of_find_node_by_path("/");
152 	if (of_property_read_string(root, "model", &machine))
153 		of_property_read_string_index(root, "compatible", 0, &machine);
154 	if (machine)
155 		soc_dev_attr.machine = machine;
156 
157 	svr = fsl_guts_get_svr();
158 	soc_die = fsl_soc_die_match(svr, fsl_soc_die);
159 	if (soc_die) {
160 		soc_dev_attr.family = devm_kasprintf(dev, GFP_KERNEL,
161 						     "QorIQ %s", soc_die->die);
162 	} else {
163 		soc_dev_attr.family = devm_kasprintf(dev, GFP_KERNEL, "QorIQ");
164 	}
165 	if (!soc_dev_attr.family)
166 		return -ENOMEM;
167 	soc_dev_attr.soc_id = devm_kasprintf(dev, GFP_KERNEL,
168 					     "svr:0x%08x", svr);
169 	if (!soc_dev_attr.soc_id)
170 		return -ENOMEM;
171 	soc_dev_attr.revision = devm_kasprintf(dev, GFP_KERNEL, "%d.%d",
172 					       (svr >>  4) & 0xf, svr & 0xf);
173 	if (!soc_dev_attr.revision)
174 		return -ENOMEM;
175 
176 	soc_dev = soc_device_register(&soc_dev_attr);
177 	if (IS_ERR(soc_dev))
178 		return PTR_ERR(soc_dev);
179 
180 	pr_info("Machine: %s\n", soc_dev_attr.machine);
181 	pr_info("SoC family: %s\n", soc_dev_attr.family);
182 	pr_info("SoC ID: %s, Revision: %s\n",
183 		soc_dev_attr.soc_id, soc_dev_attr.revision);
184 	return 0;
185 }
186 
187 static int fsl_guts_remove(struct platform_device *dev)
188 {
189 	soc_device_unregister(soc_dev);
190 	of_node_put(root);
191 	return 0;
192 }
193 
194 /*
195  * Table for matching compatible strings, for device tree
196  * guts node, for Freescale QorIQ SOCs.
197  */
198 static const struct of_device_id fsl_guts_of_match[] = {
199 	{ .compatible = "fsl,qoriq-device-config-1.0", },
200 	{ .compatible = "fsl,qoriq-device-config-2.0", },
201 	{ .compatible = "fsl,p1010-guts", },
202 	{ .compatible = "fsl,p1020-guts", },
203 	{ .compatible = "fsl,p1021-guts", },
204 	{ .compatible = "fsl,p1022-guts", },
205 	{ .compatible = "fsl,p1023-guts", },
206 	{ .compatible = "fsl,p2020-guts", },
207 	{ .compatible = "fsl,bsc9131-guts", },
208 	{ .compatible = "fsl,bsc9132-guts", },
209 	{ .compatible = "fsl,mpc8536-guts", },
210 	{ .compatible = "fsl,mpc8544-guts", },
211 	{ .compatible = "fsl,mpc8548-guts", },
212 	{ .compatible = "fsl,mpc8568-guts", },
213 	{ .compatible = "fsl,mpc8569-guts", },
214 	{ .compatible = "fsl,mpc8572-guts", },
215 	{ .compatible = "fsl,ls1021a-dcfg", },
216 	{ .compatible = "fsl,ls1043a-dcfg", },
217 	{ .compatible = "fsl,ls2080a-dcfg", },
218 	{ .compatible = "fsl,ls1088a-dcfg", },
219 	{ .compatible = "fsl,ls1012a-dcfg", },
220 	{ .compatible = "fsl,ls1046a-dcfg", },
221 	{}
222 };
223 MODULE_DEVICE_TABLE(of, fsl_guts_of_match);
224 
225 static struct platform_driver fsl_guts_driver = {
226 	.driver = {
227 		.name = "fsl-guts",
228 		.of_match_table = fsl_guts_of_match,
229 	},
230 	.probe = fsl_guts_probe,
231 	.remove = fsl_guts_remove,
232 };
233 
234 static int __init fsl_guts_init(void)
235 {
236 	return platform_driver_register(&fsl_guts_driver);
237 }
238 core_initcall(fsl_guts_init);
239 
240 static void __exit fsl_guts_exit(void)
241 {
242 	platform_driver_unregister(&fsl_guts_driver);
243 }
244 module_exit(fsl_guts_exit);
245