blob: 99aad1412ae733c24dbdf796fc37108c0313563b [file] [log] [blame]
Liviu Dudau688db7f2018-09-28 13:43:31 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Arm Ltd
4 * Author: Liviu Dudau <liviu.dudau@foss.arm.com>
5 *
6 */
7#include <common.h>
8#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Liviu Dudau688db7f2018-09-28 13:43:31 +010010#include <dm/read.h>
11#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060012#include <linux/bitops.h>
Liviu Dudau688db7f2018-09-28 13:43:31 +010013#include <linux/delay.h>
14#include <misc.h>
15
16#define SYS_CFGDATA 0xa0
17
18#define SYS_CFGCTRL 0xa4
19#define SYS_CFGCTRL_START BIT(31)
20#define SYS_CFGCTRL_WRITE BIT(30)
21
22#define SYS_CFGSTAT 0xa8
23#define SYS_CFGSTAT_ERR BIT(1)
24#define SYS_CFGSTAT_COMPLETE BIT(0)
25
26struct vexpress_config_sysreg {
27 phys_addr_t addr;
28 u32 site;
29};
30
31static int vexpress_config_exec(struct vexpress_config_sysreg *syscfg,
32 bool write, void *buf, int size)
33{
34 u32 cmd, status, tries = 100;
35
36 cmd = (*(u32 *)buf) | SYS_CFGCTRL_START | (syscfg->site << 16);
37
38 if (!write) {
39 /* write a canary in the data register for reads */
40 writel(0xdeadbeef, syscfg->addr + SYS_CFGDATA);
41 } else {
42 cmd |= SYS_CFGCTRL_WRITE;
43 writel(((u32 *)buf)[1], syscfg->addr + SYS_CFGDATA);
44 }
45 writel(0, syscfg->addr + SYS_CFGSTAT);
46 writel(cmd, syscfg->addr + SYS_CFGCTRL);
47
48 /* completion of command takes ages, go to sleep (150us) */
49 do {
50 udelay(150);
51 status = readl(syscfg->addr + SYS_CFGSTAT);
52 if (status & SYS_CFGSTAT_ERR)
53 return -EFAULT;
54 } while (--tries && !(status & SYS_CFGSTAT_COMPLETE));
55
56 if (!tries)
57 return -ETIMEDOUT;
58
59 if (!write)
60 (*(u32 *)buf) = readl(syscfg->addr + SYS_CFGDATA);
61
62 return 0;
63}
64
65static int vexpress_config_read(struct udevice *dev,
66 int offset, void *buf, int size)
67{
68 struct vexpress_config_sysreg *priv = dev_get_uclass_priv(dev);
69
70 if (size != sizeof(u32))
71 return -EINVAL;
72
73 return vexpress_config_exec(priv, false, buf, size);
74}
75
76static int vexpress_config_write(struct udevice *dev,
77 int offset, const void *buf, int size)
78{
79 struct vexpress_config_sysreg *priv = dev_get_uclass_priv(dev);
80
81 if (size != sizeof(u32) * 2)
82 return -EINVAL;
83
84 return vexpress_config_exec(priv, true, (void *)buf, size);
85}
86
87static struct misc_ops vexpress_config_ops = {
88 .read = vexpress_config_read,
89 .write = vexpress_config_write,
90};
91
92static int vexpress_config_probe(struct udevice *dev)
93{
94 struct ofnode_phandle_args args;
Wei Chenc5952bd2023-10-18 14:27:44 +080095 struct vexpress_config_sysreg *priv = dev_get_priv(dev);
Liviu Dudau688db7f2018-09-28 13:43:31 +010096 const char *prop;
97 int err, prop_size;
98
99 err = dev_read_phandle_with_args(dev, "arm,vexpress,config-bridge",
100 NULL, 0, 0, &args);
101 if (err)
102 return err;
103
104 prop = ofnode_get_property(args.node, "compatible", &prop_size);
105 if (!prop || (strncmp(prop, "arm,vexpress-sysreg", 19) != 0))
106 return -ENOENT;
107
Liviu Dudau688db7f2018-09-28 13:43:31 +0100108 if (!priv)
109 return -ENOMEM;
110
Liviu Dudau688db7f2018-09-28 13:43:31 +0100111 priv->addr = ofnode_get_addr(args.node);
112
113 return dev_read_u32(dev, "arm,vexpress,site", &priv->site);
114}
115
116static const struct udevice_id vexpress_config_ids[] = {
117 { .compatible = "arm,vexpress,config-bus" },
118 { }
119};
120
121U_BOOT_DRIVER(vexpress_config_drv) = {
122 .name = "vexpress_config_bus",
123 .id = UCLASS_MISC,
124 .of_match = vexpress_config_ids,
125 .bind = dm_scan_fdt_dev,
126 .probe = vexpress_config_probe,
127 .ops = &vexpress_config_ops,
Wei Chenc5952bd2023-10-18 14:27:44 +0800128 .priv_auto = sizeof(struct vexpress_config_sysreg),
Liviu Dudau688db7f2018-09-28 13:43:31 +0100129};