drm/panel: samsung-s6e88a0-ams452ef01: transition to mipi_dsi wrapped functions
[drm/drm-misc.git] / arch / arm / mach-versatile / spc.c
blob790092734cf6155daa63c44a1e5af00ecef30737
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
18 #include <linux/io.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>
26 #include "spc.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
34 #define COMMS 0x10
35 #define COMMS_REQ 0x14
36 #define PWC_STATUS 0x18
37 #define PWC_FLAG 0x1c
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
81 #define MAX_OPPS 8
82 #define CA15_DVFS 0
83 #define CA7_DVFS 1
84 #define SPC_SYS_CFG 2
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))
89 struct ve_spc_opp {
90 unsigned long freq;
91 unsigned long u_volt;
94 struct ve_spc_drvdata {
95 void __iomem *baseaddr;
97 * A15s cluster identifier
98 * It corresponds to A15 processors MPIDR[15:8] bitfield
100 u32 a15_clusid;
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)
127 u32 reg;
129 reg = readl_relaxed(info->baseaddr + WAKE_INT_MASK);
131 if (set)
132 reg |= GBL_WAKEUP_INT_MSK;
133 else
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)
152 u32 mask, reg;
154 if (cluster >= MAX_CLUSTERS)
155 return;
157 mask = BIT(cpu);
159 if (!cluster_is_a15(cluster))
160 mask <<= 4;
162 reg = readl_relaxed(info->baseaddr + WAKE_INT_MASK);
164 if (set)
165 reg |= mask;
166 else
167 reg &= ~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)
184 return;
186 if (cluster_is_a15(cluster))
187 baseaddr = info->baseaddr + A15_BX_ADDR0 + (cpu << 2);
188 else
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)
206 u32 pwdrn_reg;
208 if (cluster >= MAX_CLUSTERS)
209 return;
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
232 * parked.
234 int ve_spc_cpu_in_wfi(u32 cpu, u32 cluster)
236 int ret;
237 u32 mask = standbywfi_cpu_mask(cpu, cluster);
239 if (cluster >= MAX_CLUSTERS)
240 return 1;
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);
247 return 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;
254 u32 perf;
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])
260 return -EINVAL;
262 opps += perf;
263 *freq = opps->freq;
265 return 0;
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++) {
277 ftmp = opps->freq;
278 if (ftmp >= freq) {
279 if (ftmp <= fmax)
280 fmax = ftmp;
281 } else {
282 if (ftmp >= fmin)
283 fmin = ftmp;
286 if (fmax != ~0)
287 return fmax * 1000;
288 else
289 return fmin * 1000;
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)
299 break;
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));
307 if (ret == 0)
308 ret = -ETIMEDOUT;
309 else if (ret > 0)
310 ret = info->cur_rsp_stat & STAT_COMPLETE(req_type) ? 0 : -EIO;
311 return ret;
314 static int ve_spc_set_performance(int cluster, u32 freq)
316 u32 perf_cfg_reg;
317 int ret, perf, req_type;
319 if (cluster_is_a15(cluster)) {
320 req_type = CA15_DVFS;
321 perf_cfg_reg = PERF_LVL_A15;
322 } else {
323 req_type = CA7_DVFS;
324 perf_cfg_reg = PERF_LVL_A7;
327 perf = ve_spc_find_performance_index(cluster, freq);
329 if (perf < 0)
330 return perf;
332 if (down_timeout(&info->sem, usecs_to_jiffies(TIMEOUT_US)))
333 return -ETIME;
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;
342 up(&info->sem);
344 return ret;
347 static int ve_spc_read_sys_cfg(int func, int offset, uint32_t *data)
349 int ret;
351 if (down_timeout(&info->sem, usecs_to_jiffies(TIMEOUT_US)))
352 return -ETIME;
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);
361 if (ret == 0)
362 *data = readl(info->baseaddr + SYSCFG_RDATA);
364 info->cur_rsp_mask = 0;
365 up(&info->sem);
367 return ret;
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);
380 return IRQ_HANDLED;
384 * +--------------------------+
385 * | 31 20 | 19 0 |
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);
399 if (!opps)
400 return -ENOMEM;
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);
407 if (!ret) {
408 opps->freq = (data & FREQ_MASK) * MULT_FACTOR;
409 opps->u_volt = (data >> VOLT_SHIFT) * 1000;
410 } else {
411 break;
414 info->num_opps[cluster] = idx;
416 return ret;
419 static int ve_init_opp_table(struct device *cpu_dev)
421 int cluster;
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);
433 if (ret) {
434 dev_warn(cpu_dev, "failed to add opp %lu %lu\n",
435 opps->freq, opps->u_volt);
436 return ret;
439 return ret;
442 int __init ve_spc_init(void __iomem *baseaddr, u32 a15_clusid, int irq)
444 int ret;
445 info = kzalloc(sizeof(*info), GFP_KERNEL);
446 if (!info)
447 return -ENOMEM;
449 info->baseaddr = baseaddr;
450 info->a15_clusid = a15_clusid;
452 if (irq <= 0) {
453 pr_err(SPCLOG "Invalid IRQ %d\n", irq);
454 kfree(info);
455 return -EINVAL;
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);
464 if (ret) {
465 pr_err(SPCLOG "IRQ %d request failed\n", irq);
466 kfree(info);
467 return -ENODEV;
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
474 * memory.
476 sync_cache_w(info);
477 sync_cache_w(&info);
479 return 0;
482 struct clk_spc {
483 struct clk_hw hw;
484 int cluster;
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);
492 u32 freq;
494 if (ve_spc_get_performance(spc->cluster, &freq))
495 return -EIO;
497 return freq * 1000;
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;
525 struct clk_spc *spc;
527 spc = kzalloc(sizeof(*spc), GFP_KERNEL);
528 if (!spc)
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)
546 int cpu, cluster;
547 struct clk *clk;
548 bool init_opp_table[MAX_CLUSTERS] = { false };
550 if (!info)
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");
555 return -ENODEV;
558 for_each_possible_cpu(cpu) {
559 struct device *cpu_dev = get_cpu_device(cpu);
560 if (!cpu_dev) {
561 pr_warn("failed to get cpu%d device\n", cpu);
562 continue;
564 clk = ve_spc_clk_register(cpu_dev);
565 if (IS_ERR(clk)) {
566 pr_warn("failed to register cpu%d clock\n", cpu);
567 continue;
569 if (clk_register_clkdev(clk, NULL, dev_name(cpu_dev))) {
570 pr_warn("failed to register cpu%d clock lookup\n", cpu);
571 continue;
574 cluster = topology_physical_package_id(cpu_dev->id);
575 if (cluster < 0 || init_opp_table[cluster])
576 continue;
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);
583 else
584 init_opp_table[cluster] = true;
587 platform_device_register_simple("vexpress-spc-cpufreq", -1, NULL, 0);
588 return 0;
590 device_initcall(ve_spc_clk_init);