soc: samsung: chipid: Add ABB register region read helper
[platform/kernel/linux-exynos.git] / drivers / soc / samsung / exynos-chipid.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4  *            http://www.samsung.com/
5  *
6  * EXYNOS - CHIP ID support
7  * Author: Pankaj Dubey <pankaj.dubey@samsung.com>
8  * Author: Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>
9  */
10
11 #include <linux/io.h>
12 #include <linux/of.h>
13 #include <linux/of_address.h>
14 #include <linux/of_platform.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/sys_soc.h>
18
19 #include "exynos-chipid.h"
20
21 static const struct exynos_soc_id {
22         const char *name;
23         unsigned int id;
24 } soc_ids[] = {
25         { "EXYNOS3250", 0xE3472000 },
26         { "EXYNOS4210", 0x43200000 },   /* EVT0 revision */
27         { "EXYNOS4210", 0x43210000 },
28         { "EXYNOS4212", 0x43220000 },
29         { "EXYNOS4412", 0xE4412000 },
30         { "EXYNOS5250", 0x43520000 },
31         { "EXYNOS5260", 0xE5260000 },
32         { "EXYNOS5410", 0xE5410000 },
33         { "EXYNOS5420", 0xE5420000 },
34         { "EXYNOS5440", 0xE5440000 },
35         { "EXYNOS5800", 0xE5422000 },
36         { "EXYNOS7420", 0xE7420000 },
37         { "EXYNOS5433", 0xE5433000 },
38 };
39
40 static void __iomem *exynos_abb_base;
41 static void __iomem *exynos_chipid_base;
42
43 unsigned int exynos_chipid_read(unsigned int offset)
44 {
45         return readl_relaxed(exynos_chipid_base + offset);
46 }
47
48 unsigned int exynos_chipid_read_bits(unsigned int offset, unsigned int shift,
49                                      unsigned int mask)
50 {
51         return (readl_relaxed(exynos_chipid_base + offset) >> shift) & mask;
52 }
53
54 unsigned int exynos_chipid_abb_read(unsigned int offset)
55 {
56         if (WARN_ON(exynos_abb_base == NULL))
57                 return 0;
58
59         return readl_relaxed(exynos_abb_base + offset);
60 }
61
62 static const char * __init product_id_to_soc_id(unsigned int product_id)
63 {
64         int i;
65
66         for (i = 0; i < ARRAY_SIZE(soc_ids); i++)
67                 if ((product_id & EXYNOS_MASK) == soc_ids[i].id)
68                         return soc_ids[i].name;
69         return NULL;
70 }
71
72 int __init exynos_chipid_early_init(void)
73 {
74         struct soc_device_attribute *soc_dev_attr;
75         struct soc_device *soc_dev;
76         struct device_node *root;
77         struct device_node *np;
78         u32 product_id;
79         u32 revision;
80
81         /* look up for chipid node */
82         np = of_find_compatible_node(NULL, NULL, "samsung,exynos4210-chipid");
83         if (!np)
84                 return -ENODEV;
85
86         exynos_chipid_base = of_iomap(np, 0);
87         if (!exynos_chipid_base) {
88                 of_node_put(np);
89                 pr_err("Failed to map SoC CHIPID\n");
90                 return -ENXIO;
91         }
92
93         if (of_device_is_compatible(np, "samsung,exynos5433-chipid")) {
94                 exynos_abb_base = of_iomap(np, 1);
95                 if (!exynos_abb_base) {
96                         of_node_put(np);
97                         pr_err("Failed to map SoC CHIPID/ABB\n");
98                         return -ENXIO;
99                 }
100         }
101         of_node_put(np);
102
103         product_id = exynos_chipid_read(EXYNOS_CHIPID_REG_PRO_ID);
104         revision = product_id & EXYNOS_REV_MASK;
105
106         soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
107         if (!soc_dev_attr)
108                 return -ENOMEM;
109
110         soc_dev_attr->family = "Samsung Exynos";
111
112         root = of_find_node_by_path("/");
113         of_property_read_string(root, "model", &soc_dev_attr->machine);
114         of_node_put(root);
115
116         soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%x", revision);
117         soc_dev_attr->soc_id = product_id_to_soc_id(product_id);
118         if (!soc_dev_attr->soc_id) {
119                 pr_err("Unknown SoC\n");
120                 return -ENODEV;
121         }
122
123         /* please note that the actual registration will be deferred */
124         soc_dev = soc_device_register(soc_dev_attr);
125         if (IS_ERR(soc_dev)) {
126                 kfree(soc_dev_attr->revision);
127                 kfree(soc_dev_attr);
128                 return PTR_ERR(soc_dev);
129         }
130
131         /* it is too early to use dev_info() here (soc_dev is NULL) */
132         pr_info("soc soc0: Exynos: CPU[%s] PRO_ID[0x%x] REV[0x%x] Detected\n",
133                 soc_dev_attr->soc_id, product_id, revision);
134
135         return 0;
136 }
137 early_initcall(exynos_chipid_early_init);