1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * RZ System controller driver 4 * 5 * Copyright (C) 2024 Renesas Electronics Corp. 6 */ 7 8 #include <linux/bitfield.h> 9 #include <linux/cleanup.h> 10 #include <linux/io.h> 11 #include <linux/mfd/syscon.h> 12 #include <linux/of.h> 13 #include <linux/platform_device.h> 14 #include <linux/regmap.h> 15 #include <linux/slab.h> 16 #include <linux/sys_soc.h> 17 18 #include "rz-sysc.h" 19 20 /** 21 * struct rz_sysc - RZ SYSC private data structure 22 * @base: SYSC base address 23 * @dev: SYSC device pointer 24 */ 25 struct rz_sysc { 26 void __iomem *base; 27 struct device *dev; 28 }; 29 30 static int rz_sysc_soc_init(struct rz_sysc *sysc, const struct of_device_id *match) 31 { 32 const struct rz_sysc_init_data *sysc_data = match->data; 33 const struct rz_sysc_soc_id_init_data *soc_data = sysc_data->soc_id_init_data; 34 struct soc_device_attribute *soc_dev_attr; 35 const char *soc_id_start, *soc_id_end; 36 u32 val, revision, specific_id; 37 struct soc_device *soc_dev; 38 char soc_id[32] = {0}; 39 size_t size; 40 41 soc_id_start = strchr(match->compatible, ',') + 1; 42 soc_id_end = strchr(match->compatible, '-'); 43 size = soc_id_end - soc_id_start + 1; 44 if (size > 32) 45 size = sizeof(soc_id); 46 strscpy(soc_id, soc_id_start, size); 47 48 soc_dev_attr = devm_kzalloc(sysc->dev, sizeof(*soc_dev_attr), GFP_KERNEL); 49 if (!soc_dev_attr) 50 return -ENOMEM; 51 52 soc_dev_attr->family = devm_kstrdup(sysc->dev, soc_data->family, GFP_KERNEL); 53 if (!soc_dev_attr->family) 54 return -ENOMEM; 55 56 soc_dev_attr->soc_id = devm_kstrdup(sysc->dev, soc_id, GFP_KERNEL); 57 if (!soc_dev_attr->soc_id) 58 return -ENOMEM; 59 60 val = readl(sysc->base + soc_data->devid_offset); 61 revision = field_get(soc_data->revision_mask, val); 62 specific_id = field_get(soc_data->specific_id_mask, val); 63 soc_dev_attr->revision = devm_kasprintf(sysc->dev, GFP_KERNEL, "%u", revision); 64 if (!soc_dev_attr->revision) 65 return -ENOMEM; 66 67 if (soc_data->id && specific_id != soc_data->id) { 68 dev_warn(sysc->dev, "SoC mismatch (product = 0x%x)\n", specific_id); 69 return -ENODEV; 70 } 71 72 /* Try to call SoC-specific device identification */ 73 if (soc_data->print_id) { 74 soc_data->print_id(sysc->dev, sysc->base, soc_dev_attr); 75 } else { 76 dev_info(sysc->dev, "Detected Renesas %s %s Rev %s\n", 77 soc_dev_attr->family, soc_dev_attr->soc_id, soc_dev_attr->revision); 78 } 79 80 soc_dev = soc_device_register(soc_dev_attr); 81 if (IS_ERR(soc_dev)) 82 return PTR_ERR(soc_dev); 83 84 return 0; 85 } 86 87 static const struct of_device_id rz_sysc_match[] = { 88 #ifdef CONFIG_SYSC_R9A08G045 89 { .compatible = "renesas,r9a08g045-sysc", .data = &rzg3s_sysc_init_data }, 90 #endif 91 #ifdef CONFIG_SYS_R9A09G047 92 { .compatible = "renesas,r9a09g047-sys", .data = &rzg3e_sys_init_data }, 93 #endif 94 #ifdef CONFIG_SYS_R9A09G056 95 { .compatible = "renesas,r9a09g056-sys", .data = &rzv2n_sys_init_data }, 96 #endif 97 #ifdef CONFIG_SYS_R9A09G057 98 { .compatible = "renesas,r9a09g057-sys", .data = &rzv2h_sys_init_data }, 99 #endif 100 { } 101 }; 102 MODULE_DEVICE_TABLE(of, rz_sysc_match); 103 104 static int rz_sysc_probe(struct platform_device *pdev) 105 { 106 const struct rz_sysc_init_data *data; 107 const struct of_device_id *match; 108 struct device *dev = &pdev->dev; 109 struct regmap *regmap; 110 struct rz_sysc *sysc; 111 int ret; 112 113 struct regmap_config *regmap_cfg __free(kfree) = kzalloc(sizeof(*regmap_cfg), GFP_KERNEL); 114 if (!regmap_cfg) 115 return -ENOMEM; 116 117 match = of_match_node(rz_sysc_match, dev->of_node); 118 if (!match) 119 return -ENODEV; 120 121 data = match->data; 122 123 sysc = devm_kzalloc(dev, sizeof(*sysc), GFP_KERNEL); 124 if (!sysc) 125 return -ENOMEM; 126 127 sysc->base = devm_platform_ioremap_resource(pdev, 0); 128 if (IS_ERR(sysc->base)) 129 return PTR_ERR(sysc->base); 130 131 sysc->dev = dev; 132 ret = rz_sysc_soc_init(sysc, match); 133 if (ret) 134 return ret; 135 136 regmap_cfg->name = "rz_sysc_regs"; 137 regmap_cfg->reg_bits = 32; 138 regmap_cfg->reg_stride = 4; 139 regmap_cfg->val_bits = 32; 140 regmap_cfg->fast_io = true; 141 regmap_cfg->max_register = data->max_register; 142 regmap_cfg->readable_reg = data->readable_reg; 143 regmap_cfg->writeable_reg = data->writeable_reg; 144 145 regmap = devm_regmap_init_mmio(dev, sysc->base, regmap_cfg); 146 if (IS_ERR(regmap)) 147 return PTR_ERR(regmap); 148 149 return of_syscon_register_regmap(dev->of_node, regmap); 150 } 151 152 static struct platform_driver rz_sysc_driver = { 153 .driver = { 154 .name = "renesas-rz-sysc", 155 .suppress_bind_attrs = true, 156 .of_match_table = rz_sysc_match 157 }, 158 .probe = rz_sysc_probe 159 }; 160 161 static int __init rz_sysc_init(void) 162 { 163 return platform_driver_register(&rz_sysc_driver); 164 } 165 subsys_initcall(rz_sysc_init); 166 167 MODULE_DESCRIPTION("Renesas RZ System Controller Driver"); 168 MODULE_AUTHOR("Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com>"); 169 MODULE_LICENSE("GPL"); 170