blob: 871f98342d50e67e9962ae863436bf839c8cb4e7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Thierry Reding1ddd35b2013-09-30 14:04:51 +02002#include <linux/err.h>
Anson Huang8267ff82019-10-28 17:16:01 +08003#include <linux/mfd/syscon.h>
Sascha Hauer198016e2009-02-06 15:38:22 +01004#include <linux/module.h>
Fabio Estevambb07d752012-02-29 10:28:08 -03005#include <linux/io.h>
Shawn Guoa2887542013-08-13 16:59:28 +08006#include <linux/of.h>
Steffen Trumtrare57e4ab2014-07-07 11:41:26 +02007#include <linux/of_address.h>
Anson Huang8267ff82019-10-28 17:16:01 +08008#include <linux/regmap.h>
Shawn Guoa2887542013-08-13 16:59:28 +08009#include <linux/slab.h>
10#include <linux/sys_soc.h>
Shawn Guo50f2de62012-09-14 14:14:45 +080011
12#include "hardware.h"
Fabio Estevame7feaaa2013-03-25 09:20:28 -030013#include "common.h"
Sascha Hauer198016e2009-02-06 15:38:22 +010014
Anson Huang8267ff82019-10-28 17:16:01 +080015#define OCOTP_UID_H 0x420
16#define OCOTP_UID_L 0x410
17
Sascha Hauer198016e2009-02-06 15:38:22 +010018unsigned int __mxc_cpu_type;
Shawn Guobfefdff2013-08-13 13:54:02 +080019static unsigned int imx_soc_revision;
20
Sascha Hauer198016e2009-02-06 15:38:22 +010021void mxc_set_cpu_type(unsigned int type)
22{
23 __mxc_cpu_type = type;
24}
25
Shawn Guobfefdff2013-08-13 13:54:02 +080026void imx_set_soc_revision(unsigned int rev)
27{
28 imx_soc_revision = rev;
29}
30
31unsigned int imx_get_soc_revision(void)
32{
33 return imx_soc_revision;
34}
35
Fabio Estevam059e58f2011-08-26 13:35:18 +080036void imx_print_silicon_rev(const char *cpu, int srev)
37{
38 if (srev == IMX_CHIP_REVISION_UNKNOWN)
39 pr_info("CPU identified as %s, unknown revision\n", cpu);
40 else
41 pr_info("CPU identified as %s, silicon rev %d.%d\n",
42 cpu, (srev >> 4) & 0xf, srev & 0xf);
43}
Fabio Estevambb07d752012-02-29 10:28:08 -030044
45void __init imx_set_aips(void __iomem *base)
46{
47 unsigned int reg;
48/*
49 * Set all MPROTx to be non-bufferable, trusted for R/W,
50 * not forced to user-mode.
51 */
Johannes Bergc5531382016-01-27 17:59:35 +010052 imx_writel(0x77777777, base + 0x0);
53 imx_writel(0x77777777, base + 0x4);
Fabio Estevambb07d752012-02-29 10:28:08 -030054
55/*
56 * Set all OPACRx to be non-bufferable, to not require
57 * supervisor privilege level for access, allow for
58 * write access and untrusted master access.
59 */
Johannes Bergc5531382016-01-27 17:59:35 +010060 imx_writel(0x0, base + 0x40);
61 imx_writel(0x0, base + 0x44);
62 imx_writel(0x0, base + 0x48);
63 imx_writel(0x0, base + 0x4C);
64 reg = imx_readl(base + 0x50) & 0x00FFFFFF;
65 imx_writel(reg, base + 0x50);
Fabio Estevambb07d752012-02-29 10:28:08 -030066}
Shawn Guoa2887542013-08-13 16:59:28 +080067
Steffen Trumtrare57e4ab2014-07-07 11:41:26 +020068void __init imx_aips_allow_unprivileged_access(
69 const char *compat)
70{
71 void __iomem *aips_base_addr;
72 struct device_node *np;
73
74 for_each_compatible_node(np, NULL, compat) {
75 aips_base_addr = of_iomap(np, 0);
Nicholas Mc Guirecff70652018-07-08 10:32:54 +020076 WARN_ON(!aips_base_addr);
Steffen Trumtrare57e4ab2014-07-07 11:41:26 +020077 imx_set_aips(aips_base_addr);
78 }
79}
80
Shawn Guoa2887542013-08-13 16:59:28 +080081struct device * __init imx_soc_device_init(void)
82{
83 struct soc_device_attribute *soc_dev_attr;
Anson Huang8267ff82019-10-28 17:16:01 +080084 const char *ocotp_compat = NULL;
Shawn Guoa2887542013-08-13 16:59:28 +080085 struct soc_device *soc_dev;
86 struct device_node *root;
Leonard Crestez7947e322019-12-10 23:49:28 +020087 struct regmap *ocotp = NULL;
Shawn Guoa2887542013-08-13 16:59:28 +080088 const char *soc_id;
Anson Huang8267ff82019-10-28 17:16:01 +080089 u64 soc_uid = 0;
90 u32 val;
Shawn Guoa2887542013-08-13 16:59:28 +080091 int ret;
92
93 soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
94 if (!soc_dev_attr)
95 return NULL;
96
97 soc_dev_attr->family = "Freescale i.MX";
98
99 root = of_find_node_by_path("/");
100 ret = of_property_read_string(root, "model", &soc_dev_attr->machine);
101 of_node_put(root);
102 if (ret)
103 goto free_soc;
104
105 switch (__mxc_cpu_type) {
106 case MXC_CPU_MX1:
107 soc_id = "i.MX1";
108 break;
109 case MXC_CPU_MX21:
110 soc_id = "i.MX21";
111 break;
112 case MXC_CPU_MX25:
113 soc_id = "i.MX25";
114 break;
115 case MXC_CPU_MX27:
116 soc_id = "i.MX27";
117 break;
118 case MXC_CPU_MX31:
119 soc_id = "i.MX31";
120 break;
121 case MXC_CPU_MX35:
122 soc_id = "i.MX35";
123 break;
124 case MXC_CPU_MX51:
125 soc_id = "i.MX51";
126 break;
127 case MXC_CPU_MX53:
128 soc_id = "i.MX53";
129 break;
130 case MXC_CPU_IMX6SL:
Anson Huang8267ff82019-10-28 17:16:01 +0800131 ocotp_compat = "fsl,imx6sl-ocotp";
Shawn Guoa2887542013-08-13 16:59:28 +0800132 soc_id = "i.MX6SL";
133 break;
134 case MXC_CPU_IMX6DL:
Anson Huang8267ff82019-10-28 17:16:01 +0800135 ocotp_compat = "fsl,imx6q-ocotp";
Shawn Guoa2887542013-08-13 16:59:28 +0800136 soc_id = "i.MX6DL";
137 break;
Shawn Guod9654dc2014-05-13 21:46:16 +0800138 case MXC_CPU_IMX6SX:
Anson Huang8267ff82019-10-28 17:16:01 +0800139 ocotp_compat = "fsl,imx6sx-ocotp";
Shawn Guod9654dc2014-05-13 21:46:16 +0800140 soc_id = "i.MX6SX";
141 break;
Shawn Guoa2887542013-08-13 16:59:28 +0800142 case MXC_CPU_IMX6Q:
Anson Huang8267ff82019-10-28 17:16:01 +0800143 ocotp_compat = "fsl,imx6q-ocotp";
Shawn Guoa2887542013-08-13 16:59:28 +0800144 soc_id = "i.MX6Q";
145 break;
Frank Li022d0712015-07-10 02:09:41 +0800146 case MXC_CPU_IMX6UL:
Anson Huang8267ff82019-10-28 17:16:01 +0800147 ocotp_compat = "fsl,imx6ul-ocotp";
Frank Li022d0712015-07-10 02:09:41 +0800148 soc_id = "i.MX6UL";
149 break;
Leonard Crestezb3ea5752017-06-06 20:50:42 +0300150 case MXC_CPU_IMX6ULL:
Christoph Niedermaier125ad462019-12-05 09:38:51 +0000151 ocotp_compat = "fsl,imx6ull-ocotp";
Leonard Crestezb3ea5752017-06-06 20:50:42 +0300152 soc_id = "i.MX6ULL";
153 break;
Anson Huangc90dec02018-09-30 11:32:26 +0800154 case MXC_CPU_IMX6ULZ:
Christoph Niedermaier125ad462019-12-05 09:38:51 +0000155 ocotp_compat = "fsl,imx6ull-ocotp";
Anson Huangc90dec02018-09-30 11:32:26 +0800156 soc_id = "i.MX6ULZ";
157 break;
Bai Pingdee5dee2018-03-08 17:34:55 +0800158 case MXC_CPU_IMX6SLL:
Anson Huang8267ff82019-10-28 17:16:01 +0800159 ocotp_compat = "fsl,imx6sll-ocotp";
Bai Pingdee5dee2018-03-08 17:34:55 +0800160 soc_id = "i.MX6SLL";
161 break;
Anson Huang5739b912015-05-08 01:35:55 +0800162 case MXC_CPU_IMX7D:
Anson Huang8267ff82019-10-28 17:16:01 +0800163 ocotp_compat = "fsl,imx7d-ocotp";
Anson Huang5739b912015-05-08 01:35:55 +0800164 soc_id = "i.MX7D";
165 break;
A.s. Dongde70d0e2018-11-10 15:13:04 +0000166 case MXC_CPU_IMX7ULP:
167 soc_id = "i.MX7ULP";
168 break;
Shawn Guoa2887542013-08-13 16:59:28 +0800169 default:
170 soc_id = "Unknown";
171 }
172 soc_dev_attr->soc_id = soc_id;
173
Anson Huang8267ff82019-10-28 17:16:01 +0800174 if (ocotp_compat) {
175 ocotp = syscon_regmap_lookup_by_compatible(ocotp_compat);
176 if (IS_ERR(ocotp))
177 pr_err("%s: failed to find %s regmap!\n", __func__, ocotp_compat);
Leonard Crestez7947e322019-12-10 23:49:28 +0200178 }
Anson Huang8267ff82019-10-28 17:16:01 +0800179
Leonard Crestez7947e322019-12-10 23:49:28 +0200180 if (!IS_ERR_OR_NULL(ocotp)) {
Anson Huang8267ff82019-10-28 17:16:01 +0800181 regmap_read(ocotp, OCOTP_UID_H, &val);
182 soc_uid = val;
183 regmap_read(ocotp, OCOTP_UID_L, &val);
184 soc_uid <<= 32;
185 soc_uid |= val;
186 }
187
Shawn Guoa2887542013-08-13 16:59:28 +0800188 soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%d.%d",
189 (imx_soc_revision >> 4) & 0xf,
190 imx_soc_revision & 0xf);
191 if (!soc_dev_attr->revision)
192 goto free_soc;
193
Anson Huang8267ff82019-10-28 17:16:01 +0800194 soc_dev_attr->serial_number = kasprintf(GFP_KERNEL, "%016llX", soc_uid);
195 if (!soc_dev_attr->serial_number)
196 goto free_rev;
197
Shawn Guoa2887542013-08-13 16:59:28 +0800198 soc_dev = soc_device_register(soc_dev_attr);
199 if (IS_ERR(soc_dev))
Anson Huang8267ff82019-10-28 17:16:01 +0800200 goto free_serial_number;
Shawn Guoa2887542013-08-13 16:59:28 +0800201
202 return soc_device_to_device(soc_dev);
203
Anson Huang8267ff82019-10-28 17:16:01 +0800204free_serial_number:
205 kfree(soc_dev_attr->serial_number);
Shawn Guoa2887542013-08-13 16:59:28 +0800206free_rev:
207 kfree(soc_dev_attr->revision);
208free_soc:
209 kfree(soc_dev_attr);
210 return NULL;
211}