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