Geert Uytterhoeven | 8d6799a | 2016-11-14 19:37:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Renesas SoC Identification |
| 3 | * |
| 4 | * Copyright (C) 2014-2016 Glider bvba |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/of_address.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/sys_soc.h> |
| 22 | |
| 23 | |
| 24 | struct renesas_family { |
| 25 | const char name[16]; |
| 26 | u32 reg; /* CCCR or PRR, if not in DT */ |
| 27 | }; |
| 28 | |
| 29 | static const struct renesas_family fam_rcar_gen1 __initconst __maybe_unused = { |
| 30 | .name = "R-Car Gen1", |
| 31 | .reg = 0xff000044, /* PRR (Product Register) */ |
| 32 | }; |
| 33 | |
| 34 | static const struct renesas_family fam_rcar_gen2 __initconst __maybe_unused = { |
| 35 | .name = "R-Car Gen2", |
| 36 | .reg = 0xff000044, /* PRR (Product Register) */ |
| 37 | }; |
| 38 | |
| 39 | static const struct renesas_family fam_rcar_gen3 __initconst __maybe_unused = { |
| 40 | .name = "R-Car Gen3", |
| 41 | .reg = 0xfff00044, /* PRR (Product Register) */ |
| 42 | }; |
| 43 | |
| 44 | static const struct renesas_family fam_rmobile __initconst __maybe_unused = { |
| 45 | .name = "R-Mobile", |
| 46 | .reg = 0xe600101c, /* CCCR (Common Chip Code Register) */ |
| 47 | }; |
| 48 | |
| 49 | static const struct renesas_family fam_rza __initconst __maybe_unused = { |
| 50 | .name = "RZ/A", |
| 51 | }; |
| 52 | |
| 53 | static const struct renesas_family fam_rzg __initconst __maybe_unused = { |
| 54 | .name = "RZ/G", |
| 55 | .reg = 0xff000044, /* PRR (Product Register) */ |
| 56 | }; |
| 57 | |
| 58 | static const struct renesas_family fam_shmobile __initconst __maybe_unused = { |
| 59 | .name = "SH-Mobile", |
| 60 | .reg = 0xe600101c, /* CCCR (Common Chip Code Register) */ |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | struct renesas_soc { |
| 65 | const struct renesas_family *family; |
| 66 | u8 id; |
| 67 | }; |
| 68 | |
| 69 | static const struct renesas_soc soc_rz_a1h __initconst __maybe_unused = { |
| 70 | .family = &fam_rza, |
| 71 | }; |
| 72 | |
| 73 | static const struct renesas_soc soc_rmobile_ape6 __initconst __maybe_unused = { |
| 74 | .family = &fam_rmobile, |
| 75 | .id = 0x3f, |
| 76 | }; |
| 77 | |
| 78 | static const struct renesas_soc soc_rmobile_a1 __initconst __maybe_unused = { |
| 79 | .family = &fam_rmobile, |
| 80 | .id = 0x40, |
| 81 | }; |
| 82 | |
Geert Uytterhoeven | 8848e1b | 2017-03-10 14:48:29 +0100 | [diff] [blame^] | 83 | static const struct renesas_soc soc_rz_g1h __initconst __maybe_unused = { |
| 84 | .family = &fam_rzg, |
| 85 | .id = 0x45, |
| 86 | }; |
| 87 | |
Geert Uytterhoeven | 8d6799a | 2016-11-14 19:37:08 +0100 | [diff] [blame] | 88 | static const struct renesas_soc soc_rz_g1m __initconst __maybe_unused = { |
| 89 | .family = &fam_rzg, |
| 90 | .id = 0x47, |
| 91 | }; |
| 92 | |
| 93 | static const struct renesas_soc soc_rz_g1e __initconst __maybe_unused = { |
| 94 | .family = &fam_rzg, |
| 95 | .id = 0x4c, |
| 96 | }; |
| 97 | |
| 98 | static const struct renesas_soc soc_rcar_m1a __initconst __maybe_unused = { |
| 99 | .family = &fam_rcar_gen1, |
| 100 | }; |
| 101 | |
| 102 | static const struct renesas_soc soc_rcar_h1 __initconst __maybe_unused = { |
| 103 | .family = &fam_rcar_gen1, |
| 104 | .id = 0x3b, |
| 105 | }; |
| 106 | |
| 107 | static const struct renesas_soc soc_rcar_h2 __initconst __maybe_unused = { |
| 108 | .family = &fam_rcar_gen2, |
| 109 | .id = 0x45, |
| 110 | }; |
| 111 | |
| 112 | static const struct renesas_soc soc_rcar_m2_w __initconst __maybe_unused = { |
| 113 | .family = &fam_rcar_gen2, |
| 114 | .id = 0x47, |
| 115 | }; |
| 116 | |
| 117 | static const struct renesas_soc soc_rcar_v2h __initconst __maybe_unused = { |
| 118 | .family = &fam_rcar_gen2, |
| 119 | .id = 0x4a, |
| 120 | }; |
| 121 | |
| 122 | static const struct renesas_soc soc_rcar_m2_n __initconst __maybe_unused = { |
| 123 | .family = &fam_rcar_gen2, |
| 124 | .id = 0x4b, |
| 125 | }; |
| 126 | |
| 127 | static const struct renesas_soc soc_rcar_e2 __initconst __maybe_unused = { |
| 128 | .family = &fam_rcar_gen2, |
| 129 | .id = 0x4c, |
| 130 | }; |
| 131 | |
| 132 | static const struct renesas_soc soc_rcar_h3 __initconst __maybe_unused = { |
| 133 | .family = &fam_rcar_gen3, |
| 134 | .id = 0x4f, |
| 135 | }; |
| 136 | |
| 137 | static const struct renesas_soc soc_rcar_m3_w __initconst __maybe_unused = { |
| 138 | .family = &fam_rcar_gen3, |
| 139 | .id = 0x52, |
| 140 | }; |
| 141 | |
| 142 | static const struct renesas_soc soc_shmobile_ag5 __initconst __maybe_unused = { |
| 143 | .family = &fam_shmobile, |
| 144 | .id = 0x37, |
| 145 | }; |
| 146 | |
| 147 | |
| 148 | static const struct of_device_id renesas_socs[] __initconst = { |
| 149 | #ifdef CONFIG_ARCH_R7S72100 |
| 150 | { .compatible = "renesas,r7s72100", .data = &soc_rz_a1h }, |
| 151 | #endif |
| 152 | #ifdef CONFIG_ARCH_R8A73A4 |
| 153 | { .compatible = "renesas,r8a73a4", .data = &soc_rmobile_ape6 }, |
| 154 | #endif |
| 155 | #ifdef CONFIG_ARCH_R8A7740 |
| 156 | { .compatible = "renesas,r8a7740", .data = &soc_rmobile_a1 }, |
| 157 | #endif |
Geert Uytterhoeven | 8848e1b | 2017-03-10 14:48:29 +0100 | [diff] [blame^] | 158 | #ifdef CONFIG_ARCH_R8A7742 |
| 159 | { .compatible = "renesas,r8a7742", .data = &soc_rz_g1h }, |
| 160 | #endif |
Geert Uytterhoeven | 8d6799a | 2016-11-14 19:37:08 +0100 | [diff] [blame] | 161 | #ifdef CONFIG_ARCH_R8A7743 |
| 162 | { .compatible = "renesas,r8a7743", .data = &soc_rz_g1m }, |
| 163 | #endif |
| 164 | #ifdef CONFIG_ARCH_R8A7745 |
| 165 | { .compatible = "renesas,r8a7745", .data = &soc_rz_g1e }, |
| 166 | #endif |
| 167 | #ifdef CONFIG_ARCH_R8A7778 |
| 168 | { .compatible = "renesas,r8a7778", .data = &soc_rcar_m1a }, |
| 169 | #endif |
| 170 | #ifdef CONFIG_ARCH_R8A7779 |
| 171 | { .compatible = "renesas,r8a7779", .data = &soc_rcar_h1 }, |
| 172 | #endif |
| 173 | #ifdef CONFIG_ARCH_R8A7790 |
| 174 | { .compatible = "renesas,r8a7790", .data = &soc_rcar_h2 }, |
| 175 | #endif |
| 176 | #ifdef CONFIG_ARCH_R8A7791 |
| 177 | { .compatible = "renesas,r8a7791", .data = &soc_rcar_m2_w }, |
| 178 | #endif |
| 179 | #ifdef CONFIG_ARCH_R8A7792 |
| 180 | { .compatible = "renesas,r8a7792", .data = &soc_rcar_v2h }, |
| 181 | #endif |
| 182 | #ifdef CONFIG_ARCH_R8A7793 |
| 183 | { .compatible = "renesas,r8a7793", .data = &soc_rcar_m2_n }, |
| 184 | #endif |
| 185 | #ifdef CONFIG_ARCH_R8A7794 |
| 186 | { .compatible = "renesas,r8a7794", .data = &soc_rcar_e2 }, |
| 187 | #endif |
| 188 | #ifdef CONFIG_ARCH_R8A7795 |
| 189 | { .compatible = "renesas,r8a7795", .data = &soc_rcar_h3 }, |
| 190 | #endif |
| 191 | #ifdef CONFIG_ARCH_R8A7796 |
| 192 | { .compatible = "renesas,r8a7796", .data = &soc_rcar_m3_w }, |
| 193 | #endif |
| 194 | #ifdef CONFIG_ARCH_SH73A0 |
| 195 | { .compatible = "renesas,sh73a0", .data = &soc_shmobile_ag5 }, |
| 196 | #endif |
| 197 | { /* sentinel */ } |
| 198 | }; |
| 199 | |
| 200 | static int __init renesas_soc_init(void) |
| 201 | { |
| 202 | struct soc_device_attribute *soc_dev_attr; |
| 203 | const struct renesas_family *family; |
| 204 | const struct of_device_id *match; |
| 205 | const struct renesas_soc *soc; |
| 206 | void __iomem *chipid = NULL; |
| 207 | struct soc_device *soc_dev; |
| 208 | struct device_node *np; |
| 209 | unsigned int product; |
| 210 | |
| 211 | match = of_match_node(renesas_socs, of_root); |
| 212 | if (!match) |
| 213 | return -ENODEV; |
| 214 | |
| 215 | soc = match->data; |
| 216 | family = soc->family; |
| 217 | |
| 218 | /* Try PRR first, then hardcoded fallback */ |
| 219 | np = of_find_compatible_node(NULL, NULL, "renesas,prr"); |
| 220 | if (np) { |
| 221 | chipid = of_iomap(np, 0); |
| 222 | of_node_put(np); |
| 223 | } else if (soc->id) { |
| 224 | chipid = ioremap(family->reg, 4); |
| 225 | } |
| 226 | if (chipid) { |
| 227 | product = readl(chipid); |
| 228 | iounmap(chipid); |
| 229 | if (soc->id && ((product >> 8) & 0xff) != soc->id) { |
| 230 | pr_warn("SoC mismatch (product = 0x%x)\n", product); |
| 231 | return -ENODEV; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL); |
| 236 | if (!soc_dev_attr) |
| 237 | return -ENOMEM; |
| 238 | |
| 239 | np = of_find_node_by_path("/"); |
| 240 | of_property_read_string(np, "model", &soc_dev_attr->machine); |
| 241 | of_node_put(np); |
| 242 | |
| 243 | soc_dev_attr->family = kstrdup_const(family->name, GFP_KERNEL); |
| 244 | soc_dev_attr->soc_id = kstrdup_const(strchr(match->compatible, ',') + 1, |
| 245 | GFP_KERNEL); |
| 246 | if (chipid) |
| 247 | soc_dev_attr->revision = kasprintf(GFP_KERNEL, "ES%u.%u", |
| 248 | ((product >> 4) & 0x0f) + 1, |
| 249 | product & 0xf); |
| 250 | |
| 251 | pr_info("Detected Renesas %s %s %s\n", soc_dev_attr->family, |
| 252 | soc_dev_attr->soc_id, soc_dev_attr->revision ?: ""); |
| 253 | |
| 254 | soc_dev = soc_device_register(soc_dev_attr); |
| 255 | if (IS_ERR(soc_dev)) { |
| 256 | kfree(soc_dev_attr->revision); |
| 257 | kfree_const(soc_dev_attr->soc_id); |
| 258 | kfree_const(soc_dev_attr->family); |
| 259 | kfree(soc_dev_attr); |
| 260 | return PTR_ERR(soc_dev); |
| 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | core_initcall(renesas_soc_init); |