1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright (C) 2022-2023 Loongson Technology Corporation Limited
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/of_fdt.h>
11 #include <linux/sys_soc.h>
12 #include <linux/of_address.h>
13 #include <linux/platform_device.h>
15 static struct soc_device_attribute soc_dev_attr;
16 static struct soc_device *soc_dev;
19 * Global Utility Registers.
21 * Not all registers defined in this structure are available on all chips, so
22 * you are expected to know whether a given register actually exists on your
23 * chip before you access it.
25 * Also, some registers are similar on different chips but have slightly
26 * different names. In these cases, one name is chosen to avoid extraneous
30 u32 svr; /* Version Register */
32 u16 feature; /* Feature Register */
33 u32 vendor; /* Vendor Register */
36 u8 res2[0x3ff8 - 0x18];
41 struct scfg_guts __iomem *regs;
45 struct loongson2_soc_die_attr {
51 /* SoC die attribute definition for Loongson-2 platform */
52 static const struct loongson2_soc_die_attr loongson2_soc_die[] = {
55 * LoongArch-based SoCs Loongson-2 Series
58 /* Die: 2k1000, SoC: 2k1000 */
66 static const struct loongson2_soc_die_attr *loongson2_soc_die_match(
67 u32 svr, const struct loongson2_soc_die_attr *matches)
69 while (matches->svr) {
70 if (matches->svr == (svr & matches->mask))
78 static u32 loongson2_guts_get_svr(void)
82 if (!guts || !guts->regs)
85 if (guts->little_endian)
86 svr = ioread32(&guts->regs->svr);
88 svr = ioread32be(&guts->regs->svr);
93 static int loongson2_guts_probe(struct platform_device *pdev)
95 struct device_node *root, *np = pdev->dev.of_node;
96 struct device *dev = &pdev->dev;
97 const struct loongson2_soc_die_attr *soc_die;
101 /* Initialize guts */
102 guts = devm_kzalloc(dev, sizeof(*guts), GFP_KERNEL);
106 guts->little_endian = of_property_read_bool(np, "little-endian");
108 guts->regs = devm_platform_ioremap_resource(pdev, 0);
109 if (IS_ERR(guts->regs))
110 return PTR_ERR(guts->regs);
112 /* Register soc device */
113 root = of_find_node_by_path("/");
114 if (of_property_read_string(root, "model", &machine))
115 of_property_read_string_index(root, "compatible", 0, &machine);
118 soc_dev_attr.machine = devm_kstrdup(dev, machine, GFP_KERNEL);
120 svr = loongson2_guts_get_svr();
121 soc_die = loongson2_soc_die_match(svr, loongson2_soc_die);
123 soc_dev_attr.family = devm_kasprintf(dev, GFP_KERNEL,
124 "Loongson %s", soc_die->die);
126 soc_dev_attr.family = devm_kasprintf(dev, GFP_KERNEL, "Loongson");
128 if (!soc_dev_attr.family)
130 soc_dev_attr.soc_id = devm_kasprintf(dev, GFP_KERNEL,
132 if (!soc_dev_attr.soc_id)
134 soc_dev_attr.revision = devm_kasprintf(dev, GFP_KERNEL, "%d.%d",
135 (svr >> 4) & 0xf, svr & 0xf);
136 if (!soc_dev_attr.revision)
139 soc_dev = soc_device_register(&soc_dev_attr);
141 return PTR_ERR(soc_dev);
143 pr_info("Machine: %s\n", soc_dev_attr.machine);
144 pr_info("SoC family: %s\n", soc_dev_attr.family);
145 pr_info("SoC ID: %s, Revision: %s\n",
146 soc_dev_attr.soc_id, soc_dev_attr.revision);
151 static void loongson2_guts_remove(struct platform_device *dev)
153 soc_device_unregister(soc_dev);
157 * Table for matching compatible strings, for device tree
158 * guts node, for Loongson-2 SoCs.
160 static const struct of_device_id loongson2_guts_of_match[] = {
161 { .compatible = "loongson,ls2k-chipid", },
164 MODULE_DEVICE_TABLE(of, loongson2_guts_of_match);
166 static struct platform_driver loongson2_guts_driver = {
168 .name = "loongson2-guts",
169 .of_match_table = loongson2_guts_of_match,
171 .probe = loongson2_guts_probe,
172 .remove = loongson2_guts_remove,
175 static int __init loongson2_guts_init(void)
177 return platform_driver_register(&loongson2_guts_driver);
179 core_initcall(loongson2_guts_init);
181 static void __exit loongson2_guts_exit(void)
183 platform_driver_unregister(&loongson2_guts_driver);
185 module_exit(loongson2_guts_exit);
187 MODULE_DESCRIPTION("Loongson2 GUTS driver");
188 MODULE_LICENSE("GPL");