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