1d7445676SArnd Bergmann // SPDX-License-Identifier: GPL-2.0-only
2d7445676SArnd Bergmann /*
3d7445676SArnd Bergmann * dcscb.c - Dual Cluster System Configuration Block
4d7445676SArnd Bergmann *
5d7445676SArnd Bergmann * Created by: Nicolas Pitre, May 2012
6d7445676SArnd Bergmann * Copyright: (C) 2012-2013 Linaro Limited
7d7445676SArnd Bergmann */
8d7445676SArnd Bergmann
9d7445676SArnd Bergmann #include <linux/init.h>
10d7445676SArnd Bergmann #include <linux/kernel.h>
11d7445676SArnd Bergmann #include <linux/io.h>
12d7445676SArnd Bergmann #include <linux/errno.h>
13d7445676SArnd Bergmann #include <linux/of_address.h>
14d7445676SArnd Bergmann #include <linux/vexpress.h>
15d7445676SArnd Bergmann #include <linux/arm-cci.h>
16d7445676SArnd Bergmann
17d7445676SArnd Bergmann #include <asm/mcpm.h>
18d7445676SArnd Bergmann #include <asm/proc-fns.h>
19d7445676SArnd Bergmann #include <asm/cacheflush.h>
20d7445676SArnd Bergmann #include <asm/cputype.h>
21d7445676SArnd Bergmann #include <asm/cp15.h>
22d7445676SArnd Bergmann
23d7445676SArnd Bergmann #include "vexpress.h"
24d7445676SArnd Bergmann
25d7445676SArnd Bergmann #define RST_HOLD0 0x0
26d7445676SArnd Bergmann #define RST_HOLD1 0x4
27d7445676SArnd Bergmann #define SYS_SWRESET 0x8
28d7445676SArnd Bergmann #define RST_STAT0 0xc
29d7445676SArnd Bergmann #define RST_STAT1 0x10
30d7445676SArnd Bergmann #define EAG_CFG_R 0x20
31d7445676SArnd Bergmann #define EAG_CFG_W 0x24
32d7445676SArnd Bergmann #define KFC_CFG_R 0x28
33d7445676SArnd Bergmann #define KFC_CFG_W 0x2c
34d7445676SArnd Bergmann #define DCS_CFG_R 0x30
35d7445676SArnd Bergmann
36d7445676SArnd Bergmann static void __iomem *dcscb_base;
37d7445676SArnd Bergmann static int dcscb_allcpus_mask[2];
38d7445676SArnd Bergmann
dcscb_cpu_powerup(unsigned int cpu,unsigned int cluster)39d7445676SArnd Bergmann static int dcscb_cpu_powerup(unsigned int cpu, unsigned int cluster)
40d7445676SArnd Bergmann {
41d7445676SArnd Bergmann unsigned int rst_hold, cpumask = (1 << cpu);
42d7445676SArnd Bergmann
43d7445676SArnd Bergmann pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
44d7445676SArnd Bergmann if (cluster >= 2 || !(cpumask & dcscb_allcpus_mask[cluster]))
45d7445676SArnd Bergmann return -EINVAL;
46d7445676SArnd Bergmann
47d7445676SArnd Bergmann rst_hold = readl_relaxed(dcscb_base + RST_HOLD0 + cluster * 4);
48d7445676SArnd Bergmann rst_hold &= ~(cpumask | (cpumask << 4));
49d7445676SArnd Bergmann writel_relaxed(rst_hold, dcscb_base + RST_HOLD0 + cluster * 4);
50d7445676SArnd Bergmann return 0;
51d7445676SArnd Bergmann }
52d7445676SArnd Bergmann
dcscb_cluster_powerup(unsigned int cluster)53d7445676SArnd Bergmann static int dcscb_cluster_powerup(unsigned int cluster)
54d7445676SArnd Bergmann {
55d7445676SArnd Bergmann unsigned int rst_hold;
56d7445676SArnd Bergmann
57d7445676SArnd Bergmann pr_debug("%s: cluster %u\n", __func__, cluster);
58d7445676SArnd Bergmann if (cluster >= 2)
59d7445676SArnd Bergmann return -EINVAL;
60d7445676SArnd Bergmann
61d7445676SArnd Bergmann /* remove cluster reset and add individual CPU's reset */
62d7445676SArnd Bergmann rst_hold = readl_relaxed(dcscb_base + RST_HOLD0 + cluster * 4);
63d7445676SArnd Bergmann rst_hold &= ~(1 << 8);
64d7445676SArnd Bergmann rst_hold |= dcscb_allcpus_mask[cluster];
65d7445676SArnd Bergmann writel_relaxed(rst_hold, dcscb_base + RST_HOLD0 + cluster * 4);
66d7445676SArnd Bergmann return 0;
67d7445676SArnd Bergmann }
68d7445676SArnd Bergmann
dcscb_cpu_powerdown_prepare(unsigned int cpu,unsigned int cluster)69d7445676SArnd Bergmann static void dcscb_cpu_powerdown_prepare(unsigned int cpu, unsigned int cluster)
70d7445676SArnd Bergmann {
71d7445676SArnd Bergmann unsigned int rst_hold;
72d7445676SArnd Bergmann
73d7445676SArnd Bergmann pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
74d7445676SArnd Bergmann BUG_ON(cluster >= 2 || !((1 << cpu) & dcscb_allcpus_mask[cluster]));
75d7445676SArnd Bergmann
76d7445676SArnd Bergmann rst_hold = readl_relaxed(dcscb_base + RST_HOLD0 + cluster * 4);
77d7445676SArnd Bergmann rst_hold |= (1 << cpu);
78d7445676SArnd Bergmann writel_relaxed(rst_hold, dcscb_base + RST_HOLD0 + cluster * 4);
79d7445676SArnd Bergmann }
80d7445676SArnd Bergmann
dcscb_cluster_powerdown_prepare(unsigned int cluster)81d7445676SArnd Bergmann static void dcscb_cluster_powerdown_prepare(unsigned int cluster)
82d7445676SArnd Bergmann {
83d7445676SArnd Bergmann unsigned int rst_hold;
84d7445676SArnd Bergmann
85d7445676SArnd Bergmann pr_debug("%s: cluster %u\n", __func__, cluster);
86d7445676SArnd Bergmann BUG_ON(cluster >= 2);
87d7445676SArnd Bergmann
88d7445676SArnd Bergmann rst_hold = readl_relaxed(dcscb_base + RST_HOLD0 + cluster * 4);
89d7445676SArnd Bergmann rst_hold |= (1 << 8);
90d7445676SArnd Bergmann writel_relaxed(rst_hold, dcscb_base + RST_HOLD0 + cluster * 4);
91d7445676SArnd Bergmann }
92d7445676SArnd Bergmann
dcscb_cpu_cache_disable(void)93d7445676SArnd Bergmann static void dcscb_cpu_cache_disable(void)
94d7445676SArnd Bergmann {
95d7445676SArnd Bergmann /* Disable and flush the local CPU cache. */
96d7445676SArnd Bergmann v7_exit_coherency_flush(louis);
97d7445676SArnd Bergmann }
98d7445676SArnd Bergmann
dcscb_cluster_cache_disable(void)99d7445676SArnd Bergmann static void dcscb_cluster_cache_disable(void)
100d7445676SArnd Bergmann {
101d7445676SArnd Bergmann /* Flush all cache levels for this cluster. */
102d7445676SArnd Bergmann v7_exit_coherency_flush(all);
103d7445676SArnd Bergmann
104d7445676SArnd Bergmann /*
105d7445676SArnd Bergmann * A full outer cache flush could be needed at this point
106d7445676SArnd Bergmann * on platforms with such a cache, depending on where the
107d7445676SArnd Bergmann * outer cache sits. In some cases the notion of a "last
108d7445676SArnd Bergmann * cluster standing" would need to be implemented if the
109d7445676SArnd Bergmann * outer cache is shared across clusters. In any case, when
110d7445676SArnd Bergmann * the outer cache needs flushing, there is no concurrent
111d7445676SArnd Bergmann * access to the cache controller to worry about and no
112d7445676SArnd Bergmann * special locking besides what is already provided by the
113d7445676SArnd Bergmann * MCPM state machinery is needed.
114d7445676SArnd Bergmann */
115d7445676SArnd Bergmann
116d7445676SArnd Bergmann /*
117d7445676SArnd Bergmann * Disable cluster-level coherency by masking
118d7445676SArnd Bergmann * incoming snoops and DVM messages:
119d7445676SArnd Bergmann */
120d7445676SArnd Bergmann cci_disable_port_by_cpu(read_cpuid_mpidr());
121d7445676SArnd Bergmann }
122d7445676SArnd Bergmann
123d7445676SArnd Bergmann static const struct mcpm_platform_ops dcscb_power_ops = {
124d7445676SArnd Bergmann .cpu_powerup = dcscb_cpu_powerup,
125d7445676SArnd Bergmann .cluster_powerup = dcscb_cluster_powerup,
126d7445676SArnd Bergmann .cpu_powerdown_prepare = dcscb_cpu_powerdown_prepare,
127d7445676SArnd Bergmann .cluster_powerdown_prepare = dcscb_cluster_powerdown_prepare,
128d7445676SArnd Bergmann .cpu_cache_disable = dcscb_cpu_cache_disable,
129d7445676SArnd Bergmann .cluster_cache_disable = dcscb_cluster_cache_disable,
130d7445676SArnd Bergmann };
131d7445676SArnd Bergmann
132d7445676SArnd Bergmann extern void dcscb_power_up_setup(unsigned int affinity_level);
133d7445676SArnd Bergmann
dcscb_init(void)134d7445676SArnd Bergmann static int __init dcscb_init(void)
135d7445676SArnd Bergmann {
136d7445676SArnd Bergmann struct device_node *node;
137d7445676SArnd Bergmann unsigned int cfg;
138d7445676SArnd Bergmann int ret;
139d7445676SArnd Bergmann
140d7445676SArnd Bergmann if (!cci_probed())
141d7445676SArnd Bergmann return -ENODEV;
142d7445676SArnd Bergmann
143d7445676SArnd Bergmann node = of_find_compatible_node(NULL, NULL, "arm,rtsm,dcscb");
144d7445676SArnd Bergmann if (!node)
145d7445676SArnd Bergmann return -ENODEV;
146d7445676SArnd Bergmann dcscb_base = of_iomap(node, 0);
147*23b44f9cSPeng Wu of_node_put(node);
148d7445676SArnd Bergmann if (!dcscb_base)
149d7445676SArnd Bergmann return -EADDRNOTAVAIL;
150d7445676SArnd Bergmann cfg = readl_relaxed(dcscb_base + DCS_CFG_R);
151d7445676SArnd Bergmann dcscb_allcpus_mask[0] = (1 << (((cfg >> 16) >> (0 << 2)) & 0xf)) - 1;
152d7445676SArnd Bergmann dcscb_allcpus_mask[1] = (1 << (((cfg >> 16) >> (1 << 2)) & 0xf)) - 1;
153d7445676SArnd Bergmann
154d7445676SArnd Bergmann ret = mcpm_platform_register(&dcscb_power_ops);
155d7445676SArnd Bergmann if (!ret)
156d7445676SArnd Bergmann ret = mcpm_sync_init(dcscb_power_up_setup);
157d7445676SArnd Bergmann if (ret) {
158d7445676SArnd Bergmann iounmap(dcscb_base);
159d7445676SArnd Bergmann return ret;
160d7445676SArnd Bergmann }
161d7445676SArnd Bergmann
162d7445676SArnd Bergmann pr_info("VExpress DCSCB support installed\n");
163d7445676SArnd Bergmann
164d7445676SArnd Bergmann /*
165d7445676SArnd Bergmann * Future entries into the kernel can now go
166d7445676SArnd Bergmann * through the cluster entry vectors.
167d7445676SArnd Bergmann */
168d7445676SArnd Bergmann vexpress_flags_set(__pa_symbol(mcpm_entry_point));
169d7445676SArnd Bergmann
170d7445676SArnd Bergmann return 0;
171d7445676SArnd Bergmann }
172d7445676SArnd Bergmann
173d7445676SArnd Bergmann early_initcall(dcscb_init);
174