1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Arm Ltd
4 * Author: Liviu Dudau <liviu.dudau@foss.arm.com>
11 #include <linux/bitops.h>
12 #include <linux/delay.h>
15 #define SYS_CFGDATA 0xa0
17 #define SYS_CFGCTRL 0xa4
18 #define SYS_CFGCTRL_START BIT(31)
19 #define SYS_CFGCTRL_WRITE BIT(30)
21 #define SYS_CFGSTAT 0xa8
22 #define SYS_CFGSTAT_ERR BIT(1)
23 #define SYS_CFGSTAT_COMPLETE BIT(0)
25 struct vexpress_config_sysreg {
30 static int vexpress_config_exec(struct vexpress_config_sysreg *syscfg,
31 bool write, void *buf, int size)
33 u32 cmd, status, tries = 100;
35 cmd = (*(u32 *)buf) | SYS_CFGCTRL_START | (syscfg->site << 16);
38 /* write a canary in the data register for reads */
39 writel(0xdeadbeef, syscfg->addr + SYS_CFGDATA);
41 cmd |= SYS_CFGCTRL_WRITE;
42 writel(((u32 *)buf)[1], syscfg->addr + SYS_CFGDATA);
44 writel(0, syscfg->addr + SYS_CFGSTAT);
45 writel(cmd, syscfg->addr + SYS_CFGCTRL);
47 /* completion of command takes ages, go to sleep (150us) */
50 status = readl(syscfg->addr + SYS_CFGSTAT);
51 if (status & SYS_CFGSTAT_ERR)
53 } while (--tries && !(status & SYS_CFGSTAT_COMPLETE));
59 (*(u32 *)buf) = readl(syscfg->addr + SYS_CFGDATA);
64 static int vexpress_config_read(struct udevice *dev,
65 int offset, void *buf, int size)
67 struct vexpress_config_sysreg *priv = dev_get_uclass_priv(dev);
69 if (size != sizeof(u32))
72 return vexpress_config_exec(priv, false, buf, size);
75 static int vexpress_config_write(struct udevice *dev,
76 int offset, const void *buf, int size)
78 struct vexpress_config_sysreg *priv = dev_get_uclass_priv(dev);
80 if (size != sizeof(u32) * 2)
83 return vexpress_config_exec(priv, true, (void *)buf, size);
86 static struct misc_ops vexpress_config_ops = {
87 .read = vexpress_config_read,
88 .write = vexpress_config_write,
91 static int vexpress_config_probe(struct udevice *dev)
93 struct ofnode_phandle_args args;
94 struct vexpress_config_sysreg *priv = dev_get_priv(dev);
98 err = dev_read_phandle_with_args(dev, "arm,vexpress,config-bridge",
103 prop = ofnode_get_property(args.node, "compatible", &prop_size);
104 if (!prop || (strncmp(prop, "arm,vexpress-sysreg", 19) != 0))
110 priv->addr = ofnode_get_addr(args.node);
112 return dev_read_u32(dev, "arm,vexpress,site", &priv->site);
115 static const struct udevice_id vexpress_config_ids[] = {
116 { .compatible = "arm,vexpress,config-bus" },
120 U_BOOT_DRIVER(vexpress_config_drv) = {
121 .name = "vexpress_config_bus",
123 .of_match = vexpress_config_ids,
124 .bind = dm_scan_fdt_dev,
125 .probe = vexpress_config_probe,
126 .ops = &vexpress_config_ops,
127 .priv_auto = sizeof(struct vexpress_config_sysreg),