2 * CPU frequency scaling for Broadcom SoCs with AVS firmware that
5 * Copyright (c) 2016 Broadcom
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 * "AVS" is the name of a firmware developed at Broadcom. It derives
19 * its name from the technique called "Adaptive Voltage Scaling".
20 * Adaptive voltage scaling was the original purpose of this firmware.
21 * The AVS firmware still supports "AVS mode", where all it does is
22 * adaptive voltage scaling. However, on some newer Broadcom SoCs, the
23 * AVS Firmware, despite its unchanged name, also supports DFS mode and
26 * In the context of this document and the related driver, "AVS" by
27 * itself always means the Broadcom firmware and never refers to the
28 * technique called "Adaptive Voltage Scaling".
30 * The Broadcom STB AVS CPUfreq driver provides voltage and frequency
31 * scaling on Broadcom SoCs using AVS firmware with support for DFS and
32 * DVFS. The AVS firmware is running on its own co-processor. The
33 * driver supports both uniprocessor (UP) and symmetric multiprocessor
34 * (SMP) systems which share clock and voltage across all CPUs.
36 * Actual voltage and frequency scaling is done solely by the AVS
37 * firmware. This driver does not change frequency or voltage itself.
38 * It provides a standard CPUfreq interface to the rest of the kernel
39 * and to userland. It interfaces with the AVS firmware to effect the
40 * requested changes and to report back the current system status in a
41 * way that is expected by existing tools.
44 #include <linux/cpufreq.h>
45 #include <linux/delay.h>
46 #include <linux/interrupt.h>
48 #include <linux/module.h>
49 #include <linux/of_address.h>
50 #include <linux/platform_device.h>
51 #include <linux/semaphore.h>
53 /* Max number of arguments AVS calls take */
54 #define AVS_MAX_CMD_ARGS 4
56 * This macro is used to generate AVS parameter register offsets. For
57 * x >= AVS_MAX_CMD_ARGS, it returns 0 to protect against accidental memory
58 * access outside of the parameter range. (Offset 0 is the first parameter.)
60 #define AVS_PARAM_MULT(x) ((x) < AVS_MAX_CMD_ARGS ? (x) : 0)
62 /* AVS Mailbox Register offsets */
63 #define AVS_MBOX_COMMAND 0x00
64 #define AVS_MBOX_STATUS 0x04
65 #define AVS_MBOX_VOLTAGE0 0x08
66 #define AVS_MBOX_TEMP0 0x0c
67 #define AVS_MBOX_PV0 0x10
68 #define AVS_MBOX_MV0 0x14
69 #define AVS_MBOX_PARAM(x) (0x18 + AVS_PARAM_MULT(x) * sizeof(u32))
70 #define AVS_MBOX_REVISION 0x28
71 #define AVS_MBOX_PSTATE 0x2c
72 #define AVS_MBOX_HEARTBEAT 0x30
73 #define AVS_MBOX_MAGIC 0x34
74 #define AVS_MBOX_SIGMA_HVT 0x38
75 #define AVS_MBOX_SIGMA_SVT 0x3c
76 #define AVS_MBOX_VOLTAGE1 0x40
77 #define AVS_MBOX_TEMP1 0x44
78 #define AVS_MBOX_PV1 0x48
79 #define AVS_MBOX_MV1 0x4c
80 #define AVS_MBOX_FREQUENCY 0x50
83 #define AVS_CMD_AVAILABLE 0x00
84 #define AVS_CMD_DISABLE 0x10
85 #define AVS_CMD_ENABLE 0x11
86 #define AVS_CMD_S2_ENTER 0x12
87 #define AVS_CMD_S2_EXIT 0x13
88 #define AVS_CMD_BBM_ENTER 0x14
89 #define AVS_CMD_BBM_EXIT 0x15
90 #define AVS_CMD_S3_ENTER 0x16
91 #define AVS_CMD_S3_EXIT 0x17
92 #define AVS_CMD_BALANCE 0x18
93 /* PMAP and P-STATE commands */
94 #define AVS_CMD_GET_PMAP 0x30
95 #define AVS_CMD_SET_PMAP 0x31
96 #define AVS_CMD_GET_PSTATE 0x40
97 #define AVS_CMD_SET_PSTATE 0x41
99 /* Different modes AVS supports (for GET_PMAP/SET_PMAP) */
100 #define AVS_MODE_AVS 0x0
101 #define AVS_MODE_DFS 0x1
102 #define AVS_MODE_DVS 0x2
103 #define AVS_MODE_DVFS 0x3
107 * unused:31-24, mdiv_p0:23-16, unused:15-14, pdiv:13-10 , ndiv_int:9-0
109 #define NDIV_INT_SHIFT 0
110 #define NDIV_INT_MASK 0x3ff
111 #define PDIV_SHIFT 10
112 #define PDIV_MASK 0xf
113 #define MDIV_P0_SHIFT 16
114 #define MDIV_P0_MASK 0xff
117 * mdiv_p4:31-24, mdiv_p3:23-16, mdiv_p2:15:8, mdiv_p1:7:0
119 #define MDIV_P1_SHIFT 0
120 #define MDIV_P1_MASK 0xff
121 #define MDIV_P2_SHIFT 8
122 #define MDIV_P2_MASK 0xff
123 #define MDIV_P3_SHIFT 16
124 #define MDIV_P3_MASK 0xff
125 #define MDIV_P4_SHIFT 24
126 #define MDIV_P4_MASK 0xff
128 /* Different P-STATES AVS supports (for GET_PSTATE/SET_PSTATE) */
129 #define AVS_PSTATE_P0 0x0
130 #define AVS_PSTATE_P1 0x1
131 #define AVS_PSTATE_P2 0x2
132 #define AVS_PSTATE_P3 0x3
133 #define AVS_PSTATE_P4 0x4
134 #define AVS_PSTATE_MAX AVS_PSTATE_P4
136 /* CPU L2 Interrupt Controller Registers */
137 #define AVS_CPU_L2_SET0 0x04
138 #define AVS_CPU_L2_INT_MASK BIT(31)
140 /* AVS Command Status Values */
141 #define AVS_STATUS_CLEAR 0x00
142 /* Command/notification accepted */
143 #define AVS_STATUS_SUCCESS 0xf0
144 /* Command/notification rejected */
145 #define AVS_STATUS_FAILURE 0xff
146 /* Invalid command/notification (unknown) */
147 #define AVS_STATUS_INVALID 0xf1
148 /* Non-AVS modes are not supported */
149 #define AVS_STATUS_NO_SUPP 0xf2
150 /* Cannot set P-State until P-Map supplied */
151 #define AVS_STATUS_NO_MAP 0xf3
152 /* Cannot change P-Map after initial P-Map set */
153 #define AVS_STATUS_MAP_SET 0xf4
154 /* Max AVS status; higher numbers are used for debugging */
155 #define AVS_STATUS_MAX 0xff
157 /* Other AVS related constants */
158 #define AVS_LOOP_LIMIT 10000
159 #define AVS_TIMEOUT 300 /* in ms; expected completion is < 10ms */
160 #define AVS_FIRMWARE_MAGIC 0xa11600d1
162 #define BRCM_AVS_CPUFREQ_PREFIX "brcmstb-avs"
163 #define BRCM_AVS_CPUFREQ_NAME BRCM_AVS_CPUFREQ_PREFIX "-cpufreq"
164 #define BRCM_AVS_CPU_DATA "brcm,avs-cpu-data-mem"
165 #define BRCM_AVS_CPU_INTR "brcm,avs-cpu-l2-intr"
166 #define BRCM_AVS_HOST_INTR "sw_intr"
175 struct private_data {
177 void __iomem *avs_intr_base;
179 struct completion done;
180 struct semaphore sem;
185 static void __iomem *__map_region(const char *name)
187 struct device_node *np;
190 np = of_find_compatible_node(NULL, NULL, name);
194 ptr = of_iomap(np, 0);
200 static unsigned long wait_for_avs_command(struct private_data *priv,
201 unsigned long timeout)
203 unsigned long time_left = 0;
206 /* Event driven, wait for the command interrupt */
207 if (priv->host_irq >= 0)
208 return wait_for_completion_timeout(&priv->done,
209 msecs_to_jiffies(timeout));
211 /* Polling for command completion */
214 val = readl(priv->base + AVS_MBOX_STATUS);
218 usleep_range(1000, 2000);
224 static int __issue_avs_command(struct private_data *priv, unsigned int cmd,
225 unsigned int num_in, unsigned int num_out,
228 void __iomem *base = priv->base;
229 unsigned long time_left;
234 ret = down_interruptible(&priv->sem);
239 * Make sure no other command is currently running: cmd is 0 if AVS
240 * co-processor is idle. Due to the guard above, we should almost never
243 for (i = 0, val = 1; val != 0 && i < AVS_LOOP_LIMIT; i++)
244 val = readl(base + AVS_MBOX_COMMAND);
246 /* Give the caller a chance to retry if AVS is busy. */
247 if (i == AVS_LOOP_LIMIT) {
252 /* Clear status before we begin. */
253 writel(AVS_STATUS_CLEAR, base + AVS_MBOX_STATUS);
255 /* Provide input parameters */
256 for (i = 0; i < num_in; i++)
257 writel(args[i], base + AVS_MBOX_PARAM(i));
259 /* Protect from spurious interrupts. */
260 reinit_completion(&priv->done);
262 /* Now issue the command & tell firmware to wake up to process it. */
263 writel(cmd, base + AVS_MBOX_COMMAND);
264 writel(AVS_CPU_L2_INT_MASK, priv->avs_intr_base + AVS_CPU_L2_SET0);
266 /* Wait for AVS co-processor to finish processing the command. */
267 time_left = wait_for_avs_command(priv, AVS_TIMEOUT);
270 * If the AVS status is not in the expected range, it means AVS didn't
271 * complete our command in time, and we return an error. Also, if there
272 * is no "time left", we timed out waiting for the interrupt.
274 val = readl(base + AVS_MBOX_STATUS);
275 if (time_left == 0 || val == 0 || val > AVS_STATUS_MAX) {
276 dev_err(priv->dev, "AVS command %#x didn't complete in time\n",
278 dev_err(priv->dev, " Time left: %u ms, AVS status: %#x\n",
279 jiffies_to_msecs(time_left), val);
284 /* Process returned values */
285 for (i = 0; i < num_out; i++)
286 args[i] = readl(base + AVS_MBOX_PARAM(i));
288 /* Clear status to tell AVS co-processor we are done. */
289 writel(AVS_STATUS_CLEAR, base + AVS_MBOX_STATUS);
291 /* Convert firmware errors to errno's as much as possible. */
293 case AVS_STATUS_INVALID:
296 case AVS_STATUS_NO_SUPP:
299 case AVS_STATUS_NO_MAP:
302 case AVS_STATUS_MAP_SET:
305 case AVS_STATUS_FAILURE:
316 static irqreturn_t irq_handler(int irq, void *data)
318 struct private_data *priv = data;
320 /* AVS command completed execution. Wake up __issue_avs_command(). */
321 complete(&priv->done);
326 static char *brcm_avs_mode_to_string(unsigned int mode)
341 static void brcm_avs_parse_p1(u32 p1, unsigned int *mdiv_p0, unsigned int *pdiv,
344 *mdiv_p0 = (p1 >> MDIV_P0_SHIFT) & MDIV_P0_MASK;
345 *pdiv = (p1 >> PDIV_SHIFT) & PDIV_MASK;
346 *ndiv = (p1 >> NDIV_INT_SHIFT) & NDIV_INT_MASK;
349 static void brcm_avs_parse_p2(u32 p2, unsigned int *mdiv_p1,
350 unsigned int *mdiv_p2, unsigned int *mdiv_p3,
351 unsigned int *mdiv_p4)
353 *mdiv_p4 = (p2 >> MDIV_P4_SHIFT) & MDIV_P4_MASK;
354 *mdiv_p3 = (p2 >> MDIV_P3_SHIFT) & MDIV_P3_MASK;
355 *mdiv_p2 = (p2 >> MDIV_P2_SHIFT) & MDIV_P2_MASK;
356 *mdiv_p1 = (p2 >> MDIV_P1_SHIFT) & MDIV_P1_MASK;
359 static int brcm_avs_get_pmap(struct private_data *priv, struct pmap *pmap)
361 u32 args[AVS_MAX_CMD_ARGS];
364 ret = __issue_avs_command(priv, AVS_CMD_GET_PMAP, 0, 4, args);
368 pmap->mode = args[0];
371 pmap->state = args[3];
376 static int brcm_avs_set_pmap(struct private_data *priv, struct pmap *pmap)
378 u32 args[AVS_MAX_CMD_ARGS];
380 args[0] = pmap->mode;
383 args[3] = pmap->state;
385 return __issue_avs_command(priv, AVS_CMD_SET_PMAP, 4, 0, args);
388 static int brcm_avs_get_pstate(struct private_data *priv, unsigned int *pstate)
390 u32 args[AVS_MAX_CMD_ARGS];
393 ret = __issue_avs_command(priv, AVS_CMD_GET_PSTATE, 0, 1, args);
401 static int brcm_avs_set_pstate(struct private_data *priv, unsigned int pstate)
403 u32 args[AVS_MAX_CMD_ARGS];
407 return __issue_avs_command(priv, AVS_CMD_SET_PSTATE, 1, 0, args);
411 static u32 brcm_avs_get_voltage(void __iomem *base)
413 return readl(base + AVS_MBOX_VOLTAGE1);
416 static u32 brcm_avs_get_frequency(void __iomem *base)
418 return readl(base + AVS_MBOX_FREQUENCY) * 1000; /* in kHz */
422 * We determine which frequencies are supported by cycling through all P-states
423 * and reading back what frequency we are running at for each P-state.
425 static struct cpufreq_frequency_table *
426 brcm_avs_get_freq_table(struct device *dev, struct private_data *priv)
428 struct cpufreq_frequency_table *table;
432 /* Remember P-state for later */
433 ret = brcm_avs_get_pstate(priv, &pstate);
437 table = devm_kcalloc(dev, AVS_PSTATE_MAX + 1, sizeof(*table),
440 return ERR_PTR(-ENOMEM);
442 for (i = AVS_PSTATE_P0; i <= AVS_PSTATE_MAX; i++) {
443 ret = brcm_avs_set_pstate(priv, i);
446 table[i].frequency = brcm_avs_get_frequency(priv->base);
447 table[i].driver_data = i;
449 table[i].frequency = CPUFREQ_TABLE_END;
451 /* Restore P-state */
452 ret = brcm_avs_set_pstate(priv, pstate);
460 * To ensure the right firmware is running we need to
461 * - check the MAGIC matches what we expect
462 * - brcm_avs_get_pmap() doesn't return -ENOTSUPP or -EINVAL
463 * We need to set up our interrupt handling before calling brcm_avs_get_pmap()!
465 static bool brcm_avs_is_firmware_loaded(struct private_data *priv)
470 rc = brcm_avs_get_pmap(priv, NULL);
471 magic = readl(priv->base + AVS_MBOX_MAGIC);
473 return (magic == AVS_FIRMWARE_MAGIC) && ((rc != -ENOTSUPP) ||
477 static unsigned int brcm_avs_cpufreq_get(unsigned int cpu)
479 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
480 struct private_data *priv = policy->driver_data;
482 cpufreq_cpu_put(policy);
484 return brcm_avs_get_frequency(priv->base);
487 static int brcm_avs_target_index(struct cpufreq_policy *policy,
490 return brcm_avs_set_pstate(policy->driver_data,
491 policy->freq_table[index].driver_data);
494 static int brcm_avs_suspend(struct cpufreq_policy *policy)
496 struct private_data *priv = policy->driver_data;
499 ret = brcm_avs_get_pmap(priv, &priv->pmap);
504 * We can't use the P-state returned by brcm_avs_get_pmap(), since
505 * that's the initial P-state from when the P-map was downloaded to the
506 * AVS co-processor, not necessarily the P-state we are running at now.
507 * So, we get the current P-state explicitly.
509 ret = brcm_avs_get_pstate(priv, &priv->pmap.state);
513 /* This is best effort. Nothing to do if it fails. */
514 (void)__issue_avs_command(priv, AVS_CMD_S2_ENTER, 0, 0, NULL);
519 static int brcm_avs_resume(struct cpufreq_policy *policy)
521 struct private_data *priv = policy->driver_data;
524 /* This is best effort. Nothing to do if it fails. */
525 (void)__issue_avs_command(priv, AVS_CMD_S2_EXIT, 0, 0, NULL);
527 ret = brcm_avs_set_pmap(priv, &priv->pmap);
528 if (ret == -EEXIST) {
529 struct platform_device *pdev = cpufreq_get_driver_data();
530 struct device *dev = &pdev->dev;
532 dev_warn(dev, "PMAP was already set\n");
540 * All initialization code that we only want to execute once goes here. Setup
541 * code that can be re-tried on every core (if it failed before) can go into
542 * brcm_avs_cpufreq_init().
544 static int brcm_avs_prepare_init(struct platform_device *pdev)
546 struct private_data *priv;
551 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
556 sema_init(&priv->sem, 1);
557 init_completion(&priv->done);
558 platform_set_drvdata(pdev, priv);
560 priv->base = __map_region(BRCM_AVS_CPU_DATA);
562 dev_err(dev, "Couldn't find property %s in device tree.\n",
567 priv->avs_intr_base = __map_region(BRCM_AVS_CPU_INTR);
568 if (!priv->avs_intr_base) {
569 dev_err(dev, "Couldn't find property %s in device tree.\n",
575 priv->host_irq = platform_get_irq_byname(pdev, BRCM_AVS_HOST_INTR);
577 ret = devm_request_irq(dev, priv->host_irq, irq_handler,
579 BRCM_AVS_HOST_INTR, priv);
580 if (ret && priv->host_irq >= 0) {
581 dev_err(dev, "IRQ request failed: %s (%d) -- %d\n",
582 BRCM_AVS_HOST_INTR, priv->host_irq, ret);
583 goto unmap_intr_base;
586 if (brcm_avs_is_firmware_loaded(priv))
589 dev_err(dev, "AVS firmware is not loaded or doesn't support DVFS\n");
593 iounmap(priv->avs_intr_base);
600 static void brcm_avs_prepare_uninit(struct platform_device *pdev)
602 struct private_data *priv;
604 priv = platform_get_drvdata(pdev);
606 iounmap(priv->avs_intr_base);
610 static int brcm_avs_cpufreq_init(struct cpufreq_policy *policy)
612 struct cpufreq_frequency_table *freq_table;
613 struct platform_device *pdev;
614 struct private_data *priv;
618 pdev = cpufreq_get_driver_data();
619 priv = platform_get_drvdata(pdev);
620 policy->driver_data = priv;
623 freq_table = brcm_avs_get_freq_table(dev, priv);
624 if (IS_ERR(freq_table)) {
625 ret = PTR_ERR(freq_table);
626 dev_err(dev, "Couldn't determine frequency table (%d).\n", ret);
630 policy->freq_table = freq_table;
632 /* All cores share the same clock and thus the same policy. */
633 cpumask_setall(policy->cpus);
635 ret = __issue_avs_command(priv, AVS_CMD_ENABLE, 0, 0, NULL);
639 ret = brcm_avs_get_pstate(priv, &pstate);
641 policy->cur = freq_table[pstate].frequency;
642 dev_info(dev, "registered\n");
647 dev_err(dev, "couldn't initialize driver (%d)\n", ret);
652 static ssize_t show_brcm_avs_pstate(struct cpufreq_policy *policy, char *buf)
654 struct private_data *priv = policy->driver_data;
657 if (brcm_avs_get_pstate(priv, &pstate))
658 return sprintf(buf, "<unknown>\n");
660 return sprintf(buf, "%u\n", pstate);
663 static ssize_t show_brcm_avs_mode(struct cpufreq_policy *policy, char *buf)
665 struct private_data *priv = policy->driver_data;
668 if (brcm_avs_get_pmap(priv, &pmap))
669 return sprintf(buf, "<unknown>\n");
671 return sprintf(buf, "%s %u\n", brcm_avs_mode_to_string(pmap.mode),
675 static ssize_t show_brcm_avs_pmap(struct cpufreq_policy *policy, char *buf)
677 unsigned int mdiv_p0, mdiv_p1, mdiv_p2, mdiv_p3, mdiv_p4;
678 struct private_data *priv = policy->driver_data;
679 unsigned int ndiv, pdiv;
682 if (brcm_avs_get_pmap(priv, &pmap))
683 return sprintf(buf, "<unknown>\n");
685 brcm_avs_parse_p1(pmap.p1, &mdiv_p0, &pdiv, &ndiv);
686 brcm_avs_parse_p2(pmap.p2, &mdiv_p1, &mdiv_p2, &mdiv_p3, &mdiv_p4);
688 return sprintf(buf, "0x%08x 0x%08x %u %u %u %u %u %u %u %u %u\n",
689 pmap.p1, pmap.p2, ndiv, pdiv, mdiv_p0, mdiv_p1, mdiv_p2,
690 mdiv_p3, mdiv_p4, pmap.mode, pmap.state);
693 static ssize_t show_brcm_avs_voltage(struct cpufreq_policy *policy, char *buf)
695 struct private_data *priv = policy->driver_data;
697 return sprintf(buf, "0x%08x\n", brcm_avs_get_voltage(priv->base));
700 static ssize_t show_brcm_avs_frequency(struct cpufreq_policy *policy, char *buf)
702 struct private_data *priv = policy->driver_data;
704 return sprintf(buf, "0x%08x\n", brcm_avs_get_frequency(priv->base));
707 cpufreq_freq_attr_ro(brcm_avs_pstate);
708 cpufreq_freq_attr_ro(brcm_avs_mode);
709 cpufreq_freq_attr_ro(brcm_avs_pmap);
710 cpufreq_freq_attr_ro(brcm_avs_voltage);
711 cpufreq_freq_attr_ro(brcm_avs_frequency);
713 static struct freq_attr *brcm_avs_cpufreq_attr[] = {
714 &cpufreq_freq_attr_scaling_available_freqs,
723 static struct cpufreq_driver brcm_avs_driver = {
724 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
725 .verify = cpufreq_generic_frequency_table_verify,
726 .target_index = brcm_avs_target_index,
727 .get = brcm_avs_cpufreq_get,
728 .suspend = brcm_avs_suspend,
729 .resume = brcm_avs_resume,
730 .init = brcm_avs_cpufreq_init,
731 .attr = brcm_avs_cpufreq_attr,
732 .name = BRCM_AVS_CPUFREQ_PREFIX,
735 static int brcm_avs_cpufreq_probe(struct platform_device *pdev)
739 ret = brcm_avs_prepare_init(pdev);
743 brcm_avs_driver.driver_data = pdev;
745 ret = cpufreq_register_driver(&brcm_avs_driver);
747 brcm_avs_prepare_uninit(pdev);
752 static int brcm_avs_cpufreq_remove(struct platform_device *pdev)
756 ret = cpufreq_unregister_driver(&brcm_avs_driver);
759 brcm_avs_prepare_uninit(pdev);
764 static const struct of_device_id brcm_avs_cpufreq_match[] = {
765 { .compatible = BRCM_AVS_CPU_DATA },
768 MODULE_DEVICE_TABLE(of, brcm_avs_cpufreq_match);
770 static struct platform_driver brcm_avs_cpufreq_platdrv = {
772 .name = BRCM_AVS_CPUFREQ_NAME,
773 .of_match_table = brcm_avs_cpufreq_match,
775 .probe = brcm_avs_cpufreq_probe,
776 .remove = brcm_avs_cpufreq_remove,
778 module_platform_driver(brcm_avs_cpufreq_platdrv);
780 MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
781 MODULE_DESCRIPTION("CPUfreq driver for Broadcom STB AVS");
782 MODULE_LICENSE("GPL");