1b82621acSYinbo Zhu // SPDX-License-Identifier: GPL-2.0+
2b82621acSYinbo Zhu /*
3b82621acSYinbo Zhu * Author: Yinbo Zhu <zhuyinbo@loongson.cn>
4b82621acSYinbo Zhu * Copyright (C) 2022-2023 Loongson Technology Corporation Limited
5b82621acSYinbo Zhu */
6b82621acSYinbo Zhu
7b82621acSYinbo Zhu #include <linux/io.h>
8b82621acSYinbo Zhu #include <linux/slab.h>
9b82621acSYinbo Zhu #include <linux/module.h>
10b82621acSYinbo Zhu #include <linux/of_fdt.h>
11b82621acSYinbo Zhu #include <linux/sys_soc.h>
12b82621acSYinbo Zhu #include <linux/of_address.h>
13b82621acSYinbo Zhu #include <linux/platform_device.h>
14b82621acSYinbo Zhu
15b82621acSYinbo Zhu static struct soc_device_attribute soc_dev_attr;
16b82621acSYinbo Zhu static struct soc_device *soc_dev;
17b82621acSYinbo Zhu
18b82621acSYinbo Zhu /*
19b82621acSYinbo Zhu * Global Utility Registers.
20b82621acSYinbo Zhu *
21b82621acSYinbo Zhu * Not all registers defined in this structure are available on all chips, so
22b82621acSYinbo Zhu * you are expected to know whether a given register actually exists on your
23b82621acSYinbo Zhu * chip before you access it.
24b82621acSYinbo Zhu *
25b82621acSYinbo Zhu * Also, some registers are similar on different chips but have slightly
26b82621acSYinbo Zhu * different names. In these cases, one name is chosen to avoid extraneous
27b82621acSYinbo Zhu * #ifdefs.
28b82621acSYinbo Zhu */
29b82621acSYinbo Zhu struct scfg_guts {
30b82621acSYinbo Zhu u32 svr; /* Version Register */
31b82621acSYinbo Zhu u8 res0[4];
32b82621acSYinbo Zhu u16 feature; /* Feature Register */
33b82621acSYinbo Zhu u32 vendor; /* Vendor Register */
34b82621acSYinbo Zhu u8 res1[6];
35b82621acSYinbo Zhu u32 id;
36b82621acSYinbo Zhu u8 res2[0x3ff8 - 0x18];
37b82621acSYinbo Zhu u32 chip;
38b82621acSYinbo Zhu };
39b82621acSYinbo Zhu
40b82621acSYinbo Zhu static struct guts {
41b82621acSYinbo Zhu struct scfg_guts __iomem *regs;
42b82621acSYinbo Zhu bool little_endian;
43b82621acSYinbo Zhu } *guts;
44b82621acSYinbo Zhu
45b82621acSYinbo Zhu struct loongson2_soc_die_attr {
46b82621acSYinbo Zhu char *die;
47b82621acSYinbo Zhu u32 svr;
48b82621acSYinbo Zhu u32 mask;
49b82621acSYinbo Zhu };
50b82621acSYinbo Zhu
51b82621acSYinbo Zhu /* SoC die attribute definition for Loongson-2 platform */
52b82621acSYinbo Zhu static const struct loongson2_soc_die_attr loongson2_soc_die[] = {
53b82621acSYinbo Zhu
54b82621acSYinbo Zhu /*
55b82621acSYinbo Zhu * LoongArch-based SoCs Loongson-2 Series
56b82621acSYinbo Zhu */
57b82621acSYinbo Zhu
58b82621acSYinbo Zhu /* Die: 2k1000, SoC: 2k1000 */
59b82621acSYinbo Zhu { .die = "2K1000",
60b82621acSYinbo Zhu .svr = 0x00000013,
61b82621acSYinbo Zhu .mask = 0x000000ff,
62b82621acSYinbo Zhu },
63b82621acSYinbo Zhu { },
64b82621acSYinbo Zhu };
65b82621acSYinbo Zhu
loongson2_soc_die_match(u32 svr,const struct loongson2_soc_die_attr * matches)66b82621acSYinbo Zhu static const struct loongson2_soc_die_attr *loongson2_soc_die_match(
67b82621acSYinbo Zhu u32 svr, const struct loongson2_soc_die_attr *matches)
68b82621acSYinbo Zhu {
69b82621acSYinbo Zhu while (matches->svr) {
70b82621acSYinbo Zhu if (matches->svr == (svr & matches->mask))
71b82621acSYinbo Zhu return matches;
72b82621acSYinbo Zhu matches++;
73a776cc49SMingtong Bao }
74b82621acSYinbo Zhu
75b82621acSYinbo Zhu return NULL;
76b82621acSYinbo Zhu }
77b82621acSYinbo Zhu
loongson2_guts_get_svr(void)78b82621acSYinbo Zhu static u32 loongson2_guts_get_svr(void)
79b82621acSYinbo Zhu {
80b82621acSYinbo Zhu u32 svr = 0;
81b82621acSYinbo Zhu
82b82621acSYinbo Zhu if (!guts || !guts->regs)
83b82621acSYinbo Zhu return svr;
84b82621acSYinbo Zhu
85b82621acSYinbo Zhu if (guts->little_endian)
86b82621acSYinbo Zhu svr = ioread32(&guts->regs->svr);
87b82621acSYinbo Zhu else
88b82621acSYinbo Zhu svr = ioread32be(&guts->regs->svr);
89b82621acSYinbo Zhu
90b82621acSYinbo Zhu return svr;
91b82621acSYinbo Zhu }
92b82621acSYinbo Zhu
loongson2_guts_probe(struct platform_device * pdev)93b82621acSYinbo Zhu static int loongson2_guts_probe(struct platform_device *pdev)
94b82621acSYinbo Zhu {
95b82621acSYinbo Zhu struct device_node *root, *np = pdev->dev.of_node;
96b82621acSYinbo Zhu struct device *dev = &pdev->dev;
97b82621acSYinbo Zhu const struct loongson2_soc_die_attr *soc_die;
98b82621acSYinbo Zhu const char *machine;
99b82621acSYinbo Zhu u32 svr;
100b82621acSYinbo Zhu
101b82621acSYinbo Zhu /* Initialize guts */
102b82621acSYinbo Zhu guts = devm_kzalloc(dev, sizeof(*guts), GFP_KERNEL);
103b82621acSYinbo Zhu if (!guts)
104b82621acSYinbo Zhu return -ENOMEM;
105b82621acSYinbo Zhu
106b82621acSYinbo Zhu guts->little_endian = of_property_read_bool(np, "little-endian");
107b82621acSYinbo Zhu
108daacef89SDongliang Mu guts->regs = devm_platform_ioremap_resource(pdev, 0);
109b82621acSYinbo Zhu if (IS_ERR(guts->regs))
110b82621acSYinbo Zhu return PTR_ERR(guts->regs);
111b82621acSYinbo Zhu
112b82621acSYinbo Zhu /* Register soc device */
113b82621acSYinbo Zhu root = of_find_node_by_path("/");
114b82621acSYinbo Zhu if (of_property_read_string(root, "model", &machine))
115b82621acSYinbo Zhu of_property_read_string_index(root, "compatible", 0, &machine);
116b82621acSYinbo Zhu of_node_put(root);
117b82621acSYinbo Zhu if (machine)
118b82621acSYinbo Zhu soc_dev_attr.machine = devm_kstrdup(dev, machine, GFP_KERNEL);
119b82621acSYinbo Zhu
120b82621acSYinbo Zhu svr = loongson2_guts_get_svr();
121b82621acSYinbo Zhu soc_die = loongson2_soc_die_match(svr, loongson2_soc_die);
122b82621acSYinbo Zhu if (soc_die) {
123b82621acSYinbo Zhu soc_dev_attr.family = devm_kasprintf(dev, GFP_KERNEL,
124b82621acSYinbo Zhu "Loongson %s", soc_die->die);
125b82621acSYinbo Zhu } else {
126b82621acSYinbo Zhu soc_dev_attr.family = devm_kasprintf(dev, GFP_KERNEL, "Loongson");
127b82621acSYinbo Zhu }
128b82621acSYinbo Zhu if (!soc_dev_attr.family)
129b82621acSYinbo Zhu return -ENOMEM;
130b82621acSYinbo Zhu soc_dev_attr.soc_id = devm_kasprintf(dev, GFP_KERNEL,
131b82621acSYinbo Zhu "svr:0x%08x", svr);
132b82621acSYinbo Zhu if (!soc_dev_attr.soc_id)
133b82621acSYinbo Zhu return -ENOMEM;
134b82621acSYinbo Zhu soc_dev_attr.revision = devm_kasprintf(dev, GFP_KERNEL, "%d.%d",
135b82621acSYinbo Zhu (svr >> 4) & 0xf, svr & 0xf);
136b82621acSYinbo Zhu if (!soc_dev_attr.revision)
137b82621acSYinbo Zhu return -ENOMEM;
138b82621acSYinbo Zhu
139b82621acSYinbo Zhu soc_dev = soc_device_register(&soc_dev_attr);
140b82621acSYinbo Zhu if (IS_ERR(soc_dev))
141b82621acSYinbo Zhu return PTR_ERR(soc_dev);
142b82621acSYinbo Zhu
143b82621acSYinbo Zhu pr_info("Machine: %s\n", soc_dev_attr.machine);
144b82621acSYinbo Zhu pr_info("SoC family: %s\n", soc_dev_attr.family);
145b82621acSYinbo Zhu pr_info("SoC ID: %s, Revision: %s\n",
146b82621acSYinbo Zhu soc_dev_attr.soc_id, soc_dev_attr.revision);
147b82621acSYinbo Zhu
148b82621acSYinbo Zhu return 0;
149b82621acSYinbo Zhu }
150b82621acSYinbo Zhu
loongson2_guts_remove(struct platform_device * dev)151*db758b73SUwe Kleine-König static void loongson2_guts_remove(struct platform_device *dev)
152b82621acSYinbo Zhu {
153b82621acSYinbo Zhu soc_device_unregister(soc_dev);
154b82621acSYinbo Zhu }
155b82621acSYinbo Zhu
156b82621acSYinbo Zhu /*
157b82621acSYinbo Zhu * Table for matching compatible strings, for device tree
158b82621acSYinbo Zhu * guts node, for Loongson-2 SoCs.
159b82621acSYinbo Zhu */
160b82621acSYinbo Zhu static const struct of_device_id loongson2_guts_of_match[] = {
161b82621acSYinbo Zhu { .compatible = "loongson,ls2k-chipid", },
162b82621acSYinbo Zhu {}
163b82621acSYinbo Zhu };
164b82621acSYinbo Zhu MODULE_DEVICE_TABLE(of, loongson2_guts_of_match);
165b82621acSYinbo Zhu
166b82621acSYinbo Zhu static struct platform_driver loongson2_guts_driver = {
167b82621acSYinbo Zhu .driver = {
168b82621acSYinbo Zhu .name = "loongson2-guts",
169b82621acSYinbo Zhu .of_match_table = loongson2_guts_of_match,
170b82621acSYinbo Zhu },
171b82621acSYinbo Zhu .probe = loongson2_guts_probe,
172*db758b73SUwe Kleine-König .remove_new = loongson2_guts_remove,
173b82621acSYinbo Zhu };
174b82621acSYinbo Zhu
loongson2_guts_init(void)175b82621acSYinbo Zhu static int __init loongson2_guts_init(void)
176b82621acSYinbo Zhu {
177b82621acSYinbo Zhu return platform_driver_register(&loongson2_guts_driver);
178b82621acSYinbo Zhu }
179b82621acSYinbo Zhu core_initcall(loongson2_guts_init);
180b82621acSYinbo Zhu
loongson2_guts_exit(void)181b82621acSYinbo Zhu static void __exit loongson2_guts_exit(void)
182b82621acSYinbo Zhu {
183b82621acSYinbo Zhu platform_driver_unregister(&loongson2_guts_driver);
184b82621acSYinbo Zhu }
185b82621acSYinbo Zhu module_exit(loongson2_guts_exit);
186b82621acSYinbo Zhu
187b82621acSYinbo Zhu MODULE_DESCRIPTION("Loongson2 GUTS driver");
188b82621acSYinbo Zhu MODULE_LICENSE("GPL");
189