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