Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Thierry Reding | 1ddd35b | 2013-09-30 14:04:51 +0200 | [diff] [blame] | 2 | #include <linux/err.h> |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 3 | #include <linux/mfd/syscon.h> |
Sascha Hauer | 198016e | 2009-02-06 15:38:22 +0100 | [diff] [blame] | 4 | #include <linux/module.h> |
Fabio Estevam | bb07d75 | 2012-02-29 10:28:08 -0300 | [diff] [blame] | 5 | #include <linux/io.h> |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 6 | #include <linux/of.h> |
Steffen Trumtrar | e57e4ab | 2014-07-07 11:41:26 +0200 | [diff] [blame] | 7 | #include <linux/of_address.h> |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 8 | #include <linux/regmap.h> |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 9 | #include <linux/slab.h> |
| 10 | #include <linux/sys_soc.h> |
Shawn Guo | 50f2de6 | 2012-09-14 14:14:45 +0800 | [diff] [blame] | 11 | |
| 12 | #include "hardware.h" |
Fabio Estevam | e7feaaa | 2013-03-25 09:20:28 -0300 | [diff] [blame] | 13 | #include "common.h" |
Sascha Hauer | 198016e | 2009-02-06 15:38:22 +0100 | [diff] [blame] | 14 | |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 15 | #define OCOTP_UID_H 0x420 |
| 16 | #define OCOTP_UID_L 0x410 |
| 17 | |
Sascha Hauer | 198016e | 2009-02-06 15:38:22 +0100 | [diff] [blame] | 18 | unsigned int __mxc_cpu_type; |
Shawn Guo | bfefdff | 2013-08-13 13:54:02 +0800 | [diff] [blame] | 19 | static unsigned int imx_soc_revision; |
| 20 | |
Sascha Hauer | 198016e | 2009-02-06 15:38:22 +0100 | [diff] [blame] | 21 | void mxc_set_cpu_type(unsigned int type) |
| 22 | { |
| 23 | __mxc_cpu_type = type; |
| 24 | } |
| 25 | |
Shawn Guo | bfefdff | 2013-08-13 13:54:02 +0800 | [diff] [blame] | 26 | void imx_set_soc_revision(unsigned int rev) |
| 27 | { |
| 28 | imx_soc_revision = rev; |
| 29 | } |
| 30 | |
| 31 | unsigned int imx_get_soc_revision(void) |
| 32 | { |
| 33 | return imx_soc_revision; |
| 34 | } |
| 35 | |
Fabio Estevam | 059e58f | 2011-08-26 13:35:18 +0800 | [diff] [blame] | 36 | void 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 Estevam | bb07d75 | 2012-02-29 10:28:08 -0300 | [diff] [blame] | 44 | |
| 45 | void __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 Berg | c553138 | 2016-01-27 17:59:35 +0100 | [diff] [blame] | 52 | imx_writel(0x77777777, base + 0x0); |
| 53 | imx_writel(0x77777777, base + 0x4); |
Fabio Estevam | bb07d75 | 2012-02-29 10:28:08 -0300 | [diff] [blame] | 54 | |
| 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 Berg | c553138 | 2016-01-27 17:59:35 +0100 | [diff] [blame] | 60 | 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 Estevam | bb07d75 | 2012-02-29 10:28:08 -0300 | [diff] [blame] | 66 | } |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 67 | |
Steffen Trumtrar | e57e4ab | 2014-07-07 11:41:26 +0200 | [diff] [blame] | 68 | void __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 Guire | cff7065 | 2018-07-08 10:32:54 +0200 | [diff] [blame] | 76 | WARN_ON(!aips_base_addr); |
Steffen Trumtrar | e57e4ab | 2014-07-07 11:41:26 +0200 | [diff] [blame] | 77 | imx_set_aips(aips_base_addr); |
| 78 | } |
| 79 | } |
| 80 | |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 81 | struct device * __init imx_soc_device_init(void) |
| 82 | { |
| 83 | struct soc_device_attribute *soc_dev_attr; |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 84 | const char *ocotp_compat = NULL; |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 85 | struct soc_device *soc_dev; |
| 86 | struct device_node *root; |
Leonard Crestez | 7947e32 | 2019-12-10 23:49:28 +0200 | [diff] [blame] | 87 | struct regmap *ocotp = NULL; |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 88 | const char *soc_id; |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 89 | u64 soc_uid = 0; |
| 90 | u32 val; |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 91 | 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 Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 131 | ocotp_compat = "fsl,imx6sl-ocotp"; |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 132 | soc_id = "i.MX6SL"; |
| 133 | break; |
| 134 | case MXC_CPU_IMX6DL: |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 135 | ocotp_compat = "fsl,imx6q-ocotp"; |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 136 | soc_id = "i.MX6DL"; |
| 137 | break; |
Shawn Guo | d9654dc | 2014-05-13 21:46:16 +0800 | [diff] [blame] | 138 | case MXC_CPU_IMX6SX: |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 139 | ocotp_compat = "fsl,imx6sx-ocotp"; |
Shawn Guo | d9654dc | 2014-05-13 21:46:16 +0800 | [diff] [blame] | 140 | soc_id = "i.MX6SX"; |
| 141 | break; |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 142 | case MXC_CPU_IMX6Q: |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 143 | ocotp_compat = "fsl,imx6q-ocotp"; |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 144 | soc_id = "i.MX6Q"; |
| 145 | break; |
Frank Li | 022d071 | 2015-07-10 02:09:41 +0800 | [diff] [blame] | 146 | case MXC_CPU_IMX6UL: |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 147 | ocotp_compat = "fsl,imx6ul-ocotp"; |
Frank Li | 022d071 | 2015-07-10 02:09:41 +0800 | [diff] [blame] | 148 | soc_id = "i.MX6UL"; |
| 149 | break; |
Leonard Crestez | b3ea575 | 2017-06-06 20:50:42 +0300 | [diff] [blame] | 150 | case MXC_CPU_IMX6ULL: |
Christoph Niedermaier | 125ad46 | 2019-12-05 09:38:51 +0000 | [diff] [blame] | 151 | ocotp_compat = "fsl,imx6ull-ocotp"; |
Leonard Crestez | b3ea575 | 2017-06-06 20:50:42 +0300 | [diff] [blame] | 152 | soc_id = "i.MX6ULL"; |
| 153 | break; |
Anson Huang | c90dec0 | 2018-09-30 11:32:26 +0800 | [diff] [blame] | 154 | case MXC_CPU_IMX6ULZ: |
Christoph Niedermaier | 125ad46 | 2019-12-05 09:38:51 +0000 | [diff] [blame] | 155 | ocotp_compat = "fsl,imx6ull-ocotp"; |
Anson Huang | c90dec0 | 2018-09-30 11:32:26 +0800 | [diff] [blame] | 156 | soc_id = "i.MX6ULZ"; |
| 157 | break; |
Bai Ping | dee5dee | 2018-03-08 17:34:55 +0800 | [diff] [blame] | 158 | case MXC_CPU_IMX6SLL: |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 159 | ocotp_compat = "fsl,imx6sll-ocotp"; |
Bai Ping | dee5dee | 2018-03-08 17:34:55 +0800 | [diff] [blame] | 160 | soc_id = "i.MX6SLL"; |
| 161 | break; |
Anson Huang | 5739b91 | 2015-05-08 01:35:55 +0800 | [diff] [blame] | 162 | case MXC_CPU_IMX7D: |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 163 | ocotp_compat = "fsl,imx7d-ocotp"; |
Anson Huang | 5739b91 | 2015-05-08 01:35:55 +0800 | [diff] [blame] | 164 | soc_id = "i.MX7D"; |
| 165 | break; |
A.s. Dong | de70d0e | 2018-11-10 15:13:04 +0000 | [diff] [blame] | 166 | case MXC_CPU_IMX7ULP: |
| 167 | soc_id = "i.MX7ULP"; |
| 168 | break; |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 169 | default: |
| 170 | soc_id = "Unknown"; |
| 171 | } |
| 172 | soc_dev_attr->soc_id = soc_id; |
| 173 | |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 174 | 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 Crestez | 7947e32 | 2019-12-10 23:49:28 +0200 | [diff] [blame] | 178 | } |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 179 | |
Leonard Crestez | 7947e32 | 2019-12-10 23:49:28 +0200 | [diff] [blame] | 180 | if (!IS_ERR_OR_NULL(ocotp)) { |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 181 | 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 Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 188 | 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 Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 194 | soc_dev_attr->serial_number = kasprintf(GFP_KERNEL, "%016llX", soc_uid); |
| 195 | if (!soc_dev_attr->serial_number) |
| 196 | goto free_rev; |
| 197 | |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 198 | soc_dev = soc_device_register(soc_dev_attr); |
| 199 | if (IS_ERR(soc_dev)) |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 200 | goto free_serial_number; |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 201 | |
| 202 | return soc_device_to_device(soc_dev); |
| 203 | |
Anson Huang | 8267ff8 | 2019-10-28 17:16:01 +0800 | [diff] [blame] | 204 | free_serial_number: |
| 205 | kfree(soc_dev_attr->serial_number); |
Shawn Guo | a288754 | 2013-08-13 16:59:28 +0800 | [diff] [blame] | 206 | free_rev: |
| 207 | kfree(soc_dev_attr->revision); |
| 208 | free_soc: |
| 209 | kfree(soc_dev_attr); |
| 210 | return NULL; |
| 211 | } |