Neil Armstrong | a9daaba | 2017-06-23 10:28:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 BayLibre, SAS |
| 3 | * Author: Neil Armstrong <narmstrong@baylibre.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <linux/io.h> |
| 9 | #include <linux/of.h> |
| 10 | #include <linux/of_address.h> |
| 11 | #include <linux/of_platform.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/sys_soc.h> |
| 15 | #include <linux/bitfield.h> |
| 16 | #include <linux/regmap.h> |
| 17 | #include <linux/mfd/syscon.h> |
| 18 | |
| 19 | #define AO_SEC_SD_CFG8 0xe0 |
| 20 | #define AO_SEC_SOCINFO_OFFSET AO_SEC_SD_CFG8 |
| 21 | |
| 22 | #define SOCINFO_MAJOR GENMASK(31, 24) |
Arnaud Patard | 044d71b | 2017-11-29 16:09:46 +0100 | [diff] [blame] | 23 | #define SOCINFO_PACK GENMASK(23, 16) |
| 24 | #define SOCINFO_MINOR GENMASK(15, 8) |
Neil Armstrong | a9daaba | 2017-06-23 10:28:18 +0200 | [diff] [blame] | 25 | #define SOCINFO_MISC GENMASK(7, 0) |
| 26 | |
| 27 | static const struct meson_gx_soc_id { |
| 28 | const char *name; |
| 29 | unsigned int id; |
| 30 | } soc_ids[] = { |
| 31 | { "GXBB", 0x1f }, |
| 32 | { "GXTVBB", 0x20 }, |
| 33 | { "GXL", 0x21 }, |
| 34 | { "GXM", 0x22 }, |
| 35 | { "TXL", 0x23 }, |
Neil Armstrong | f842c41 | 2018-03-12 12:17:40 +0100 | [diff] [blame] | 36 | { "TXLX", 0x24 }, |
| 37 | { "AXG", 0x25 }, |
| 38 | { "GXLX", 0x26 }, |
| 39 | { "TXHD", 0x27 }, |
Neil Armstrong | a9daaba | 2017-06-23 10:28:18 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static const struct meson_gx_package_id { |
| 43 | const char *name; |
| 44 | unsigned int major_id; |
| 45 | unsigned int pack_id; |
| 46 | } soc_packages[] = { |
| 47 | { "S905", 0x1f, 0 }, |
Neil Armstrong | f842c41 | 2018-03-12 12:17:40 +0100 | [diff] [blame] | 48 | { "S905H", 0x1f, 0x13 }, |
Neil Armstrong | a9daaba | 2017-06-23 10:28:18 +0200 | [diff] [blame] | 49 | { "S905M", 0x1f, 0x20 }, |
| 50 | { "S905D", 0x21, 0 }, |
| 51 | { "S905X", 0x21, 0x80 }, |
Neil Armstrong | f842c41 | 2018-03-12 12:17:40 +0100 | [diff] [blame] | 52 | { "S905W", 0x21, 0xa0 }, |
Neil Armstrong | a9daaba | 2017-06-23 10:28:18 +0200 | [diff] [blame] | 53 | { "S905L", 0x21, 0xc0 }, |
| 54 | { "S905M2", 0x21, 0xe0 }, |
| 55 | { "S912", 0x22, 0 }, |
Neil Armstrong | f842c41 | 2018-03-12 12:17:40 +0100 | [diff] [blame] | 56 | { "962X", 0x24, 0x10 }, |
| 57 | { "962E", 0x24, 0x20 }, |
| 58 | { "A113X", 0x25, 0x37 }, |
| 59 | { "A113D", 0x25, 0x22 }, |
Neil Armstrong | a9daaba | 2017-06-23 10:28:18 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static inline unsigned int socinfo_to_major(u32 socinfo) |
| 63 | { |
| 64 | return FIELD_GET(SOCINFO_MAJOR, socinfo); |
| 65 | } |
| 66 | |
| 67 | static inline unsigned int socinfo_to_minor(u32 socinfo) |
| 68 | { |
| 69 | return FIELD_GET(SOCINFO_MINOR, socinfo); |
| 70 | } |
| 71 | |
| 72 | static inline unsigned int socinfo_to_pack(u32 socinfo) |
| 73 | { |
| 74 | return FIELD_GET(SOCINFO_PACK, socinfo); |
| 75 | } |
| 76 | |
| 77 | static inline unsigned int socinfo_to_misc(u32 socinfo) |
| 78 | { |
| 79 | return FIELD_GET(SOCINFO_MISC, socinfo); |
| 80 | } |
| 81 | |
| 82 | static const char *socinfo_to_package_id(u32 socinfo) |
| 83 | { |
| 84 | unsigned int pack = socinfo_to_pack(socinfo) & 0xf0; |
| 85 | unsigned int major = socinfo_to_major(socinfo); |
| 86 | int i; |
| 87 | |
| 88 | for (i = 0 ; i < ARRAY_SIZE(soc_packages) ; ++i) { |
| 89 | if (soc_packages[i].major_id == major && |
| 90 | soc_packages[i].pack_id == pack) |
| 91 | return soc_packages[i].name; |
| 92 | } |
| 93 | |
| 94 | return "Unknown"; |
| 95 | } |
| 96 | |
| 97 | static const char *socinfo_to_soc_id(u32 socinfo) |
| 98 | { |
| 99 | unsigned int id = socinfo_to_major(socinfo); |
| 100 | int i; |
| 101 | |
| 102 | for (i = 0 ; i < ARRAY_SIZE(soc_ids) ; ++i) { |
| 103 | if (soc_ids[i].id == id) |
| 104 | return soc_ids[i].name; |
| 105 | } |
| 106 | |
| 107 | return "Unknown"; |
| 108 | } |
| 109 | |
weiyongjun (A) | 01517df | 2018-01-10 14:19:48 +0000 | [diff] [blame] | 110 | static int __init meson_gx_socinfo_init(void) |
Neil Armstrong | a9daaba | 2017-06-23 10:28:18 +0200 | [diff] [blame] | 111 | { |
| 112 | struct soc_device_attribute *soc_dev_attr; |
| 113 | struct soc_device *soc_dev; |
| 114 | struct device_node *np; |
| 115 | struct regmap *regmap; |
| 116 | unsigned int socinfo; |
| 117 | struct device *dev; |
| 118 | int ret; |
| 119 | |
| 120 | /* look up for chipid node */ |
| 121 | np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gx-ao-secure"); |
| 122 | if (!np) |
| 123 | return -ENODEV; |
| 124 | |
| 125 | /* check if interface is enabled */ |
| 126 | if (!of_device_is_available(np)) |
| 127 | return -ENODEV; |
| 128 | |
| 129 | /* check if chip-id is available */ |
| 130 | if (!of_property_read_bool(np, "amlogic,has-chip-id")) |
| 131 | return -ENODEV; |
| 132 | |
| 133 | /* node should be a syscon */ |
| 134 | regmap = syscon_node_to_regmap(np); |
| 135 | of_node_put(np); |
| 136 | if (IS_ERR(regmap)) { |
| 137 | pr_err("%s: failed to get regmap\n", __func__); |
| 138 | return -ENODEV; |
| 139 | } |
| 140 | |
| 141 | ret = regmap_read(regmap, AO_SEC_SOCINFO_OFFSET, &socinfo); |
| 142 | if (ret < 0) |
| 143 | return ret; |
| 144 | |
| 145 | if (!socinfo) { |
| 146 | pr_err("%s: invalid chipid value\n", __func__); |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | |
| 150 | soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL); |
| 151 | if (!soc_dev_attr) |
| 152 | return -ENODEV; |
| 153 | |
| 154 | soc_dev_attr->family = "Amlogic Meson"; |
| 155 | |
| 156 | np = of_find_node_by_path("/"); |
| 157 | of_property_read_string(np, "model", &soc_dev_attr->machine); |
| 158 | of_node_put(np); |
| 159 | |
| 160 | soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%x:%x - %x:%x", |
| 161 | socinfo_to_major(socinfo), |
| 162 | socinfo_to_minor(socinfo), |
| 163 | socinfo_to_pack(socinfo), |
| 164 | socinfo_to_misc(socinfo)); |
| 165 | soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%s (%s)", |
| 166 | socinfo_to_soc_id(socinfo), |
| 167 | socinfo_to_package_id(socinfo)); |
| 168 | |
| 169 | soc_dev = soc_device_register(soc_dev_attr); |
| 170 | if (IS_ERR(soc_dev)) { |
| 171 | kfree(soc_dev_attr->revision); |
| 172 | kfree_const(soc_dev_attr->soc_id); |
| 173 | kfree(soc_dev_attr); |
| 174 | return PTR_ERR(soc_dev); |
| 175 | } |
| 176 | dev = soc_device_to_device(soc_dev); |
| 177 | |
| 178 | dev_info(dev, "Amlogic Meson %s Revision %x:%x (%x:%x) Detected\n", |
| 179 | soc_dev_attr->soc_id, |
| 180 | socinfo_to_major(socinfo), |
| 181 | socinfo_to_minor(socinfo), |
| 182 | socinfo_to_pack(socinfo), |
| 183 | socinfo_to_misc(socinfo)); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | device_initcall(meson_gx_socinfo_init); |