0001
0002
0003
0004
0005
0006
0007 #include <linux/err.h>
0008 #include <linux/init.h>
0009 #include <linux/io.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/of_device.h>
0014 #include <linux/sched/signal.h>
0015 #include <linux/slab.h>
0016 #include <linux/vexpress.h>
0017
0018 #define SYS_MISC 0x0
0019 #define SYS_MISC_MASTERSITE (1 << 14)
0020
0021 #define SYS_PROCID0 0x24
0022 #define SYS_PROCID1 0x28
0023 #define SYS_HBI_MASK 0xfff
0024 #define SYS_PROCIDx_HBI_SHIFT 0
0025
0026 #define SYS_CFGDATA 0x40
0027
0028 #define SYS_CFGCTRL 0x44
0029 #define SYS_CFGCTRL_START (1 << 31)
0030 #define SYS_CFGCTRL_WRITE (1 << 30)
0031 #define SYS_CFGCTRL_DCC(n) (((n) & 0xf) << 26)
0032 #define SYS_CFGCTRL_FUNC(n) (((n) & 0x3f) << 20)
0033 #define SYS_CFGCTRL_SITE(n) (((n) & 0x3) << 16)
0034 #define SYS_CFGCTRL_POSITION(n) (((n) & 0xf) << 12)
0035 #define SYS_CFGCTRL_DEVICE(n) (((n) & 0xfff) << 0)
0036
0037 #define SYS_CFGSTAT 0x48
0038 #define SYS_CFGSTAT_ERR (1 << 1)
0039 #define SYS_CFGSTAT_COMPLETE (1 << 0)
0040
0041 #define VEXPRESS_SITE_MB 0
0042 #define VEXPRESS_SITE_DB1 1
0043 #define VEXPRESS_SITE_DB2 2
0044 #define VEXPRESS_SITE_MASTER 0xf
0045
0046 struct vexpress_syscfg {
0047 struct device *dev;
0048 void __iomem *base;
0049 struct list_head funcs;
0050 };
0051
0052 struct vexpress_syscfg_func {
0053 struct list_head list;
0054 struct vexpress_syscfg *syscfg;
0055 struct regmap *regmap;
0056 int num_templates;
0057 u32 template[];
0058 };
0059
0060 struct vexpress_config_bridge_ops {
0061 struct regmap * (*regmap_init)(struct device *dev, void *context);
0062 void (*regmap_exit)(struct regmap *regmap, void *context);
0063 };
0064
0065 struct vexpress_config_bridge {
0066 struct vexpress_config_bridge_ops *ops;
0067 void *context;
0068 };
0069
0070
0071 static DEFINE_MUTEX(vexpress_config_mutex);
0072 static u32 vexpress_config_site_master = VEXPRESS_SITE_MASTER;
0073
0074
0075 static void vexpress_config_set_master(u32 site)
0076 {
0077 vexpress_config_site_master = site;
0078 }
0079
0080 static void vexpress_config_lock(void *arg)
0081 {
0082 mutex_lock(&vexpress_config_mutex);
0083 }
0084
0085 static void vexpress_config_unlock(void *arg)
0086 {
0087 mutex_unlock(&vexpress_config_mutex);
0088 }
0089
0090
0091 static void vexpress_config_find_prop(struct device_node *node,
0092 const char *name, u32 *val)
0093 {
0094
0095 *val = 0;
0096
0097 of_node_get(node);
0098 while (node) {
0099 if (of_property_read_u32(node, name, val) == 0) {
0100 of_node_put(node);
0101 return;
0102 }
0103 node = of_get_next_parent(node);
0104 }
0105 }
0106
0107 static int vexpress_config_get_topo(struct device_node *node, u32 *site,
0108 u32 *position, u32 *dcc)
0109 {
0110 vexpress_config_find_prop(node, "arm,vexpress,site", site);
0111 if (*site == VEXPRESS_SITE_MASTER)
0112 *site = vexpress_config_site_master;
0113 if (WARN_ON(vexpress_config_site_master == VEXPRESS_SITE_MASTER))
0114 return -EINVAL;
0115 vexpress_config_find_prop(node, "arm,vexpress,position", position);
0116 vexpress_config_find_prop(node, "arm,vexpress,dcc", dcc);
0117
0118 return 0;
0119 }
0120
0121
0122 static void vexpress_config_devres_release(struct device *dev, void *res)
0123 {
0124 struct vexpress_config_bridge *bridge = dev_get_drvdata(dev->parent);
0125 struct regmap *regmap = res;
0126
0127 bridge->ops->regmap_exit(regmap, bridge->context);
0128 }
0129
0130 struct regmap *devm_regmap_init_vexpress_config(struct device *dev)
0131 {
0132 struct vexpress_config_bridge *bridge;
0133 struct regmap *regmap;
0134 struct regmap **res;
0135
0136 bridge = dev_get_drvdata(dev->parent);
0137 if (WARN_ON(!bridge))
0138 return ERR_PTR(-EINVAL);
0139
0140 res = devres_alloc(vexpress_config_devres_release, sizeof(*res),
0141 GFP_KERNEL);
0142 if (!res)
0143 return ERR_PTR(-ENOMEM);
0144
0145 regmap = (bridge->ops->regmap_init)(dev, bridge->context);
0146 if (IS_ERR(regmap)) {
0147 devres_free(res);
0148 return regmap;
0149 }
0150
0151 *res = regmap;
0152 devres_add(dev, res);
0153
0154 return regmap;
0155 }
0156 EXPORT_SYMBOL_GPL(devm_regmap_init_vexpress_config);
0157
0158 static int vexpress_syscfg_exec(struct vexpress_syscfg_func *func,
0159 int index, bool write, u32 *data)
0160 {
0161 struct vexpress_syscfg *syscfg = func->syscfg;
0162 u32 command, status;
0163 int tries;
0164 long timeout;
0165
0166 if (WARN_ON(index >= func->num_templates))
0167 return -EINVAL;
0168
0169 command = readl(syscfg->base + SYS_CFGCTRL);
0170 if (WARN_ON(command & SYS_CFGCTRL_START))
0171 return -EBUSY;
0172
0173 command = func->template[index];
0174 command |= SYS_CFGCTRL_START;
0175 command |= write ? SYS_CFGCTRL_WRITE : 0;
0176
0177
0178 if (!write)
0179 *data = 0xdeadbeef;
0180
0181 dev_dbg(syscfg->dev, "func %p, command %x, data %x\n",
0182 func, command, *data);
0183 writel(*data, syscfg->base + SYS_CFGDATA);
0184 writel(0, syscfg->base + SYS_CFGSTAT);
0185 writel(command, syscfg->base + SYS_CFGCTRL);
0186 mb();
0187
0188
0189 tries = 100;
0190 timeout = 100;
0191 do {
0192 if (!irqs_disabled()) {
0193 set_current_state(TASK_INTERRUPTIBLE);
0194 schedule_timeout(usecs_to_jiffies(timeout));
0195 if (signal_pending(current))
0196 return -EINTR;
0197 } else {
0198 udelay(timeout);
0199 }
0200
0201 status = readl(syscfg->base + SYS_CFGSTAT);
0202 if (status & SYS_CFGSTAT_ERR)
0203 return -EFAULT;
0204
0205 if (timeout > 20)
0206 timeout -= 20;
0207 } while (--tries && !(status & SYS_CFGSTAT_COMPLETE));
0208 if (WARN_ON_ONCE(!tries))
0209 return -ETIMEDOUT;
0210
0211 if (!write) {
0212 *data = readl(syscfg->base + SYS_CFGDATA);
0213 dev_dbg(syscfg->dev, "func %p, read data %x\n", func, *data);
0214 }
0215
0216 return 0;
0217 }
0218
0219 static int vexpress_syscfg_read(void *context, unsigned int index,
0220 unsigned int *val)
0221 {
0222 struct vexpress_syscfg_func *func = context;
0223
0224 return vexpress_syscfg_exec(func, index, false, val);
0225 }
0226
0227 static int vexpress_syscfg_write(void *context, unsigned int index,
0228 unsigned int val)
0229 {
0230 struct vexpress_syscfg_func *func = context;
0231
0232 return vexpress_syscfg_exec(func, index, true, &val);
0233 }
0234
0235 static struct regmap_config vexpress_syscfg_regmap_config = {
0236 .lock = vexpress_config_lock,
0237 .unlock = vexpress_config_unlock,
0238 .reg_bits = 32,
0239 .val_bits = 32,
0240 .reg_read = vexpress_syscfg_read,
0241 .reg_write = vexpress_syscfg_write,
0242 .reg_format_endian = REGMAP_ENDIAN_LITTLE,
0243 .val_format_endian = REGMAP_ENDIAN_LITTLE,
0244 };
0245
0246
0247 static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
0248 void *context)
0249 {
0250 int err;
0251 struct vexpress_syscfg *syscfg = context;
0252 struct vexpress_syscfg_func *func;
0253 struct property *prop;
0254 const __be32 *val = NULL;
0255 __be32 energy_quirk[4];
0256 int num;
0257 u32 site, position, dcc;
0258 int i;
0259
0260 err = vexpress_config_get_topo(dev->of_node, &site,
0261 &position, &dcc);
0262 if (err)
0263 return ERR_PTR(err);
0264
0265 prop = of_find_property(dev->of_node,
0266 "arm,vexpress-sysreg,func", NULL);
0267 if (!prop)
0268 return ERR_PTR(-EINVAL);
0269
0270 num = prop->length / sizeof(u32) / 2;
0271 val = prop->value;
0272
0273
0274
0275
0276
0277 if (num == 1 && of_device_is_compatible(dev->of_node,
0278 "arm,vexpress-energy")) {
0279 num = 2;
0280 energy_quirk[0] = *val;
0281 energy_quirk[2] = *val++;
0282 energy_quirk[1] = *val;
0283 energy_quirk[3] = cpu_to_be32(be32_to_cpup(val) + 1);
0284 val = energy_quirk;
0285 }
0286
0287 func = kzalloc(struct_size(func, template, num), GFP_KERNEL);
0288 if (!func)
0289 return ERR_PTR(-ENOMEM);
0290
0291 func->syscfg = syscfg;
0292 func->num_templates = num;
0293
0294 for (i = 0; i < num; i++) {
0295 u32 function, device;
0296
0297 function = be32_to_cpup(val++);
0298 device = be32_to_cpup(val++);
0299
0300 dev_dbg(dev, "func %p: %u/%u/%u/%u/%u\n",
0301 func, site, position, dcc,
0302 function, device);
0303
0304 func->template[i] = SYS_CFGCTRL_DCC(dcc);
0305 func->template[i] |= SYS_CFGCTRL_SITE(site);
0306 func->template[i] |= SYS_CFGCTRL_POSITION(position);
0307 func->template[i] |= SYS_CFGCTRL_FUNC(function);
0308 func->template[i] |= SYS_CFGCTRL_DEVICE(device);
0309 }
0310
0311 vexpress_syscfg_regmap_config.max_register = num - 1;
0312
0313 func->regmap = regmap_init(dev, NULL, func,
0314 &vexpress_syscfg_regmap_config);
0315
0316 if (IS_ERR(func->regmap)) {
0317 void *err = func->regmap;
0318
0319 kfree(func);
0320 return err;
0321 }
0322
0323 list_add(&func->list, &syscfg->funcs);
0324
0325 return func->regmap;
0326 }
0327
0328 static void vexpress_syscfg_regmap_exit(struct regmap *regmap, void *context)
0329 {
0330 struct vexpress_syscfg *syscfg = context;
0331 struct vexpress_syscfg_func *func, *tmp;
0332
0333 regmap_exit(regmap);
0334
0335 list_for_each_entry_safe(func, tmp, &syscfg->funcs, list) {
0336 if (func->regmap == regmap) {
0337 list_del(&syscfg->funcs);
0338 kfree(func);
0339 break;
0340 }
0341 }
0342 }
0343
0344 static struct vexpress_config_bridge_ops vexpress_syscfg_bridge_ops = {
0345 .regmap_init = vexpress_syscfg_regmap_init,
0346 .regmap_exit = vexpress_syscfg_regmap_exit,
0347 };
0348
0349
0350 static int vexpress_syscfg_probe(struct platform_device *pdev)
0351 {
0352 struct vexpress_syscfg *syscfg;
0353 struct resource *res;
0354 struct vexpress_config_bridge *bridge;
0355 struct device_node *node;
0356 int master;
0357 u32 dt_hbi;
0358
0359 syscfg = devm_kzalloc(&pdev->dev, sizeof(*syscfg), GFP_KERNEL);
0360 if (!syscfg)
0361 return -ENOMEM;
0362 syscfg->dev = &pdev->dev;
0363 INIT_LIST_HEAD(&syscfg->funcs);
0364
0365 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0366 syscfg->base = devm_ioremap_resource(&pdev->dev, res);
0367 if (IS_ERR(syscfg->base))
0368 return PTR_ERR(syscfg->base);
0369
0370 bridge = devm_kmalloc(&pdev->dev, sizeof(*bridge), GFP_KERNEL);
0371 if (!bridge)
0372 return -ENOMEM;
0373
0374 bridge->ops = &vexpress_syscfg_bridge_ops;
0375 bridge->context = syscfg;
0376
0377 dev_set_drvdata(&pdev->dev, bridge);
0378
0379 master = readl(syscfg->base + SYS_MISC) & SYS_MISC_MASTERSITE ?
0380 VEXPRESS_SITE_DB2 : VEXPRESS_SITE_DB1;
0381 vexpress_config_set_master(master);
0382
0383
0384 if (of_property_read_u32(of_root, "arm,hbi", &dt_hbi) == 0) {
0385 u32 id = readl(syscfg->base + (master == VEXPRESS_SITE_DB1 ?
0386 SYS_PROCID0 : SYS_PROCID1));
0387 u32 hbi = (id >> SYS_PROCIDx_HBI_SHIFT) & SYS_HBI_MASK;
0388
0389 if (WARN_ON(dt_hbi != hbi))
0390 dev_warn(&pdev->dev, "DT HBI (%x) is not matching hardware (%x)!\n",
0391 dt_hbi, hbi);
0392 }
0393
0394 for_each_compatible_node(node, NULL, "arm,vexpress,config-bus") {
0395 struct device_node *bridge_np;
0396
0397 bridge_np = of_parse_phandle(node, "arm,vexpress,config-bridge", 0);
0398 if (bridge_np != pdev->dev.parent->of_node)
0399 continue;
0400
0401 of_platform_populate(node, NULL, NULL, &pdev->dev);
0402 }
0403
0404 return 0;
0405 }
0406
0407 static const struct platform_device_id vexpress_syscfg_id_table[] = {
0408 { "vexpress-syscfg", },
0409 {},
0410 };
0411 MODULE_DEVICE_TABLE(platform, vexpress_syscfg_id_table);
0412
0413 static struct platform_driver vexpress_syscfg_driver = {
0414 .driver.name = "vexpress-syscfg",
0415 .id_table = vexpress_syscfg_id_table,
0416 .probe = vexpress_syscfg_probe,
0417 };
0418 module_platform_driver(vexpress_syscfg_driver);
0419 MODULE_LICENSE("GPL v2");