1 // SPDX-License-Identifier: GPL-2.0-only
3 * Versatile Express Serial Power Controller (SPC) support
5 * Copyright (C) 2013 ARM Ltd.
7 * Authors: Sudeep KarkadaNagesha <sudeep.karkadanagesha@arm.com>
8 * Achin Gupta <achin.gupta@arm.com>
9 * Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
12 #include <linux/clk-provider.h>
13 #include <linux/clkdev.h>
14 #include <linux/cpu.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_opp.h>
21 #include <linux/slab.h>
22 #include <linux/semaphore.h>
24 #include <asm/cacheflush.h>
28 #define SPCLOG "vexpress-spc: "
30 #define PERF_LVL_A15 0x00
31 #define PERF_REQ_A15 0x04
32 #define PERF_LVL_A7 0x08
33 #define PERF_REQ_A7 0x0c
35 #define COMMS_REQ 0x14
36 #define PWC_STATUS 0x18
39 /* SPC wake-up IRQs status and mask */
40 #define WAKE_INT_MASK 0x24
41 #define WAKE_INT_RAW 0x28
42 #define WAKE_INT_STAT 0x2c
43 /* SPC power down registers */
44 #define A15_PWRDN_EN 0x30
45 #define A7_PWRDN_EN 0x34
46 /* SPC per-CPU mailboxes */
47 #define A15_BX_ADDR0 0x68
48 #define A7_BX_ADDR0 0x78
50 /* SPC CPU/cluster reset statue */
51 #define STANDBYWFI_STAT 0x3c
52 #define STANDBYWFI_STAT_A15_CPU_MASK(cpu) (1 << (cpu))
53 #define STANDBYWFI_STAT_A7_CPU_MASK(cpu) (1 << (3 + (cpu)))
55 /* SPC system config interface registers */
56 #define SYSCFG_WDATA 0x70
57 #define SYSCFG_RDATA 0x74
59 /* A15/A7 OPP virtual register base */
60 #define A15_PERFVAL_BASE 0xC10
61 #define A7_PERFVAL_BASE 0xC30
63 /* Config interface control bits */
64 #define SYSCFG_START BIT(31)
65 #define SYSCFG_SCC (6 << 20)
66 #define SYSCFG_STAT (14 << 20)
68 /* wake-up interrupt masks */
69 #define GBL_WAKEUP_INT_MSK (0x3 << 10)
71 /* TC2 static dual-cluster configuration */
72 #define MAX_CLUSTERS 2
75 * Even though the SPC takes max 3-5 ms to complete any OPP/COMMS
76 * operation, the operation could start just before jiffy is about
77 * to be incremented. So setting timeout value of 20ms = 2jiffies@100Hz
79 #define TIMEOUT_US 20000
85 #define STAT_COMPLETE(type) ((1 << 0) << (type << 2))
86 #define STAT_ERR(type) ((1 << 1) << (type << 2))
87 #define RESPONSE_MASK(type) (STAT_COMPLETE(type) | STAT_ERR(type))
94 struct ve_spc_drvdata
{
95 void __iomem
*baseaddr
;
97 * A15s cluster identifier
98 * It corresponds to A15 processors MPIDR[15:8] bitfield
101 uint32_t cur_rsp_mask
;
102 uint32_t cur_rsp_stat
;
103 struct semaphore sem
;
104 struct completion done
;
105 struct ve_spc_opp
*opps
[MAX_CLUSTERS
];
106 int num_opps
[MAX_CLUSTERS
];
109 static struct ve_spc_drvdata
*info
;
111 static inline bool cluster_is_a15(u32 cluster
)
113 return cluster
== info
->a15_clusid
;
117 * ve_spc_global_wakeup_irq() - sets/clears global wakeup IRQs
119 * @set: if true, global wake-up IRQs are set, if false they are cleared
121 * Function to set/clear global wakeup IRQs. Not protected by locking since
122 * it might be used in code paths where normal cacheable locks are not
123 * working. Locking must be provided by the caller to ensure atomicity.
125 void ve_spc_global_wakeup_irq(bool set
)
129 reg
= readl_relaxed(info
->baseaddr
+ WAKE_INT_MASK
);
132 reg
|= GBL_WAKEUP_INT_MSK
;
134 reg
&= ~GBL_WAKEUP_INT_MSK
;
136 writel_relaxed(reg
, info
->baseaddr
+ WAKE_INT_MASK
);
140 * ve_spc_cpu_wakeup_irq() - sets/clears per-CPU wake-up IRQs
142 * @cluster: mpidr[15:8] bitfield describing cluster affinity level
143 * @cpu: mpidr[7:0] bitfield describing cpu affinity level
144 * @set: if true, wake-up IRQs are set, if false they are cleared
146 * Function to set/clear per-CPU wake-up IRQs. Not protected by locking since
147 * it might be used in code paths where normal cacheable locks are not
148 * working. Locking must be provided by the caller to ensure atomicity.
150 void ve_spc_cpu_wakeup_irq(u32 cluster
, u32 cpu
, bool set
)
154 if (cluster
>= MAX_CLUSTERS
)
159 if (!cluster_is_a15(cluster
))
162 reg
= readl_relaxed(info
->baseaddr
+ WAKE_INT_MASK
);
169 writel_relaxed(reg
, info
->baseaddr
+ WAKE_INT_MASK
);
173 * ve_spc_set_resume_addr() - set the jump address used for warm boot
175 * @cluster: mpidr[15:8] bitfield describing cluster affinity level
176 * @cpu: mpidr[7:0] bitfield describing cpu affinity level
177 * @addr: physical resume address
179 void ve_spc_set_resume_addr(u32 cluster
, u32 cpu
, u32 addr
)
181 void __iomem
*baseaddr
;
183 if (cluster
>= MAX_CLUSTERS
)
186 if (cluster_is_a15(cluster
))
187 baseaddr
= info
->baseaddr
+ A15_BX_ADDR0
+ (cpu
<< 2);
189 baseaddr
= info
->baseaddr
+ A7_BX_ADDR0
+ (cpu
<< 2);
191 writel_relaxed(addr
, baseaddr
);
195 * ve_spc_powerdown() - enables/disables cluster powerdown
197 * @cluster: mpidr[15:8] bitfield describing cluster affinity level
198 * @enable: if true enables powerdown, if false disables it
200 * Function to enable/disable cluster powerdown. Not protected by locking
201 * since it might be used in code paths where normal cacheable locks are not
202 * working. Locking must be provided by the caller to ensure atomicity.
204 void ve_spc_powerdown(u32 cluster
, bool enable
)
208 if (cluster
>= MAX_CLUSTERS
)
211 pwdrn_reg
= cluster_is_a15(cluster
) ? A15_PWRDN_EN
: A7_PWRDN_EN
;
212 writel_relaxed(enable
, info
->baseaddr
+ pwdrn_reg
);
215 static u32
standbywfi_cpu_mask(u32 cpu
, u32 cluster
)
217 return cluster_is_a15(cluster
) ?
218 STANDBYWFI_STAT_A15_CPU_MASK(cpu
)
219 : STANDBYWFI_STAT_A7_CPU_MASK(cpu
);
223 * ve_spc_cpu_in_wfi() - Checks if the specified CPU is in WFI or not
225 * @cpu: mpidr[7:0] bitfield describing CPU affinity level within cluster
226 * @cluster: mpidr[15:8] bitfield describing cluster affinity level
228 * @return: non-zero if and only if the specified CPU is in WFI
230 * Take care when interpreting the result of this function: a CPU might
231 * be in WFI temporarily due to idle, and is not necessarily safely
234 int ve_spc_cpu_in_wfi(u32 cpu
, u32 cluster
)
237 u32 mask
= standbywfi_cpu_mask(cpu
, cluster
);
239 if (cluster
>= MAX_CLUSTERS
)
242 ret
= readl_relaxed(info
->baseaddr
+ STANDBYWFI_STAT
);
244 pr_debug("%s: PCFGREG[0x%X] = 0x%08X, mask = 0x%X\n",
245 __func__
, STANDBYWFI_STAT
, ret
, mask
);
250 static int ve_spc_get_performance(int cluster
, u32
*freq
)
252 struct ve_spc_opp
*opps
= info
->opps
[cluster
];
253 u32 perf_cfg_reg
= 0;
256 perf_cfg_reg
= cluster_is_a15(cluster
) ? PERF_LVL_A15
: PERF_LVL_A7
;
258 perf
= readl_relaxed(info
->baseaddr
+ perf_cfg_reg
);
259 if (perf
>= info
->num_opps
[cluster
])
268 /* find closest match to given frequency in OPP table */
269 static int ve_spc_round_performance(int cluster
, u32 freq
)
271 int idx
, max_opp
= info
->num_opps
[cluster
];
272 struct ve_spc_opp
*opps
= info
->opps
[cluster
];
273 u32 fmin
= 0, fmax
= ~0, ftmp
;
275 freq
/= 1000; /* OPP entries in kHz */
276 for (idx
= 0; idx
< max_opp
; idx
++, opps
++) {
292 static int ve_spc_find_performance_index(int cluster
, u32 freq
)
294 int idx
, max_opp
= info
->num_opps
[cluster
];
295 struct ve_spc_opp
*opps
= info
->opps
[cluster
];
297 for (idx
= 0; idx
< max_opp
; idx
++, opps
++)
298 if (opps
->freq
== freq
)
300 return (idx
== max_opp
) ? -EINVAL
: idx
;
303 static int ve_spc_waitforcompletion(int req_type
)
305 int ret
= wait_for_completion_interruptible_timeout(
306 &info
->done
, usecs_to_jiffies(TIMEOUT_US
));
310 ret
= info
->cur_rsp_stat
& STAT_COMPLETE(req_type
) ? 0 : -EIO
;
314 static int ve_spc_set_performance(int cluster
, u32 freq
)
317 int ret
, perf
, req_type
;
319 if (cluster_is_a15(cluster
)) {
320 req_type
= CA15_DVFS
;
321 perf_cfg_reg
= PERF_LVL_A15
;
324 perf_cfg_reg
= PERF_LVL_A7
;
327 perf
= ve_spc_find_performance_index(cluster
, freq
);
332 if (down_timeout(&info
->sem
, usecs_to_jiffies(TIMEOUT_US
)))
335 init_completion(&info
->done
);
336 info
->cur_rsp_mask
= RESPONSE_MASK(req_type
);
338 writel(perf
, info
->baseaddr
+ perf_cfg_reg
);
339 ret
= ve_spc_waitforcompletion(req_type
);
341 info
->cur_rsp_mask
= 0;
347 static int ve_spc_read_sys_cfg(int func
, int offset
, uint32_t *data
)
351 if (down_timeout(&info
->sem
, usecs_to_jiffies(TIMEOUT_US
)))
354 init_completion(&info
->done
);
355 info
->cur_rsp_mask
= RESPONSE_MASK(SPC_SYS_CFG
);
357 /* Set the control value */
358 writel(SYSCFG_START
| func
| offset
>> 2, info
->baseaddr
+ COMMS
);
359 ret
= ve_spc_waitforcompletion(SPC_SYS_CFG
);
362 *data
= readl(info
->baseaddr
+ SYSCFG_RDATA
);
364 info
->cur_rsp_mask
= 0;
370 static irqreturn_t
ve_spc_irq_handler(int irq
, void *data
)
372 struct ve_spc_drvdata
*drv_data
= data
;
373 uint32_t status
= readl_relaxed(drv_data
->baseaddr
+ PWC_STATUS
);
375 if (info
->cur_rsp_mask
& status
) {
376 info
->cur_rsp_stat
= status
;
377 complete(&drv_data
->done
);
384 * +--------------------------+
386 * +--------------------------+
387 * | m_volt | freq(kHz) |
388 * +--------------------------+
390 #define MULT_FACTOR 20
391 #define VOLT_SHIFT 20
392 #define FREQ_MASK (0xFFFFF)
393 static int ve_spc_populate_opps(uint32_t cluster
)
395 uint32_t data
= 0, off
, ret
, idx
;
396 struct ve_spc_opp
*opps
;
398 opps
= kcalloc(MAX_OPPS
, sizeof(*opps
), GFP_KERNEL
);
402 info
->opps
[cluster
] = opps
;
404 off
= cluster_is_a15(cluster
) ? A15_PERFVAL_BASE
: A7_PERFVAL_BASE
;
405 for (idx
= 0; idx
< MAX_OPPS
; idx
++, off
+= 4, opps
++) {
406 ret
= ve_spc_read_sys_cfg(SYSCFG_SCC
, off
, &data
);
408 opps
->freq
= (data
& FREQ_MASK
) * MULT_FACTOR
;
409 opps
->u_volt
= (data
>> VOLT_SHIFT
) * 1000;
414 info
->num_opps
[cluster
] = idx
;
419 static int ve_init_opp_table(struct device
*cpu_dev
)
422 int idx
, ret
= 0, max_opp
;
423 struct ve_spc_opp
*opps
;
425 cluster
= topology_physical_package_id(cpu_dev
->id
);
426 cluster
= cluster
< 0 ? 0 : cluster
;
428 max_opp
= info
->num_opps
[cluster
];
429 opps
= info
->opps
[cluster
];
431 for (idx
= 0; idx
< max_opp
; idx
++, opps
++) {
432 ret
= dev_pm_opp_add(cpu_dev
, opps
->freq
* 1000, opps
->u_volt
);
434 dev_warn(cpu_dev
, "failed to add opp %lu %lu\n",
435 opps
->freq
, opps
->u_volt
);
442 int __init
ve_spc_init(void __iomem
*baseaddr
, u32 a15_clusid
, int irq
)
445 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
449 info
->baseaddr
= baseaddr
;
450 info
->a15_clusid
= a15_clusid
;
453 pr_err(SPCLOG
"Invalid IRQ %d\n", irq
);
458 init_completion(&info
->done
);
460 readl_relaxed(info
->baseaddr
+ PWC_STATUS
);
462 ret
= request_irq(irq
, ve_spc_irq_handler
, IRQF_TRIGGER_HIGH
463 | IRQF_ONESHOT
, "vexpress-spc", info
);
465 pr_err(SPCLOG
"IRQ %d request failed\n", irq
);
470 sema_init(&info
->sem
, 1);
472 * Multi-cluster systems may need this data when non-coherent, during
473 * cluster power-up/power-down. Make sure driver info reaches main
487 #define to_clk_spc(spc) container_of(spc, struct clk_spc, hw)
488 static unsigned long spc_recalc_rate(struct clk_hw
*hw
,
489 unsigned long parent_rate
)
491 struct clk_spc
*spc
= to_clk_spc(hw
);
494 if (ve_spc_get_performance(spc
->cluster
, &freq
))
500 static long spc_round_rate(struct clk_hw
*hw
, unsigned long drate
,
501 unsigned long *parent_rate
)
503 struct clk_spc
*spc
= to_clk_spc(hw
);
505 return ve_spc_round_performance(spc
->cluster
, drate
);
508 static int spc_set_rate(struct clk_hw
*hw
, unsigned long rate
,
509 unsigned long parent_rate
)
511 struct clk_spc
*spc
= to_clk_spc(hw
);
513 return ve_spc_set_performance(spc
->cluster
, rate
/ 1000);
516 static struct clk_ops clk_spc_ops
= {
517 .recalc_rate
= spc_recalc_rate
,
518 .round_rate
= spc_round_rate
,
519 .set_rate
= spc_set_rate
,
522 static struct clk
*ve_spc_clk_register(struct device
*cpu_dev
)
524 struct clk_init_data init
;
527 spc
= kzalloc(sizeof(*spc
), GFP_KERNEL
);
529 return ERR_PTR(-ENOMEM
);
531 spc
->hw
.init
= &init
;
532 spc
->cluster
= topology_physical_package_id(cpu_dev
->id
);
534 spc
->cluster
= spc
->cluster
< 0 ? 0 : spc
->cluster
;
536 init
.name
= dev_name(cpu_dev
);
537 init
.ops
= &clk_spc_ops
;
538 init
.flags
= CLK_GET_RATE_NOCACHE
;
539 init
.num_parents
= 0;
541 return devm_clk_register(cpu_dev
, &spc
->hw
);
544 static int __init
ve_spc_clk_init(void)
548 bool init_opp_table
[MAX_CLUSTERS
] = { false };
551 return 0; /* Continue only if SPC is initialised */
553 if (ve_spc_populate_opps(0) || ve_spc_populate_opps(1)) {
554 pr_err("failed to build OPP table\n");
558 for_each_possible_cpu(cpu
) {
559 struct device
*cpu_dev
= get_cpu_device(cpu
);
561 pr_warn("failed to get cpu%d device\n", cpu
);
564 clk
= ve_spc_clk_register(cpu_dev
);
566 pr_warn("failed to register cpu%d clock\n", cpu
);
569 if (clk_register_clkdev(clk
, NULL
, dev_name(cpu_dev
))) {
570 pr_warn("failed to register cpu%d clock lookup\n", cpu
);
574 cluster
= topology_physical_package_id(cpu_dev
->id
);
575 if (cluster
< 0 || init_opp_table
[cluster
])
578 if (ve_init_opp_table(cpu_dev
))
579 pr_warn("failed to initialise cpu%d opp table\n", cpu
);
580 else if (dev_pm_opp_set_sharing_cpus(cpu_dev
,
581 topology_core_cpumask(cpu_dev
->id
)))
582 pr_warn("failed to mark OPPs shared for cpu%d\n", cpu
);
584 init_opp_table
[cluster
] = true;
587 platform_device_register_simple("vexpress-spc-cpufreq", -1, NULL
, 0);
590 device_initcall(ve_spc_clk_init
);