1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25d8c6658SDmitry Eremin-Solenikov /*
35d8c6658SDmitry Eremin-Solenikov * Copyright (C) 2011 Dmitry Eremin-Solenikov
45d8c6658SDmitry Eremin-Solenikov * Copyright (C) 2002 - 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
55d8c6658SDmitry Eremin-Solenikov * and Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
65d8c6658SDmitry Eremin-Solenikov *
75d8c6658SDmitry Eremin-Solenikov * This driver adds basic cpufreq support for SMU & 970FX based G5 Macs,
85d8c6658SDmitry Eremin-Solenikov * that is iMac G5 and latest single CPU desktop.
95d8c6658SDmitry Eremin-Solenikov */
105d8c6658SDmitry Eremin-Solenikov
115d8c6658SDmitry Eremin-Solenikov #undef DEBUG
125d8c6658SDmitry Eremin-Solenikov
131c5864e2SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
141c5864e2SJoe Perches
155d8c6658SDmitry Eremin-Solenikov #include <linux/module.h>
165d8c6658SDmitry Eremin-Solenikov #include <linux/types.h>
175d8c6658SDmitry Eremin-Solenikov #include <linux/errno.h>
185d8c6658SDmitry Eremin-Solenikov #include <linux/kernel.h>
195d8c6658SDmitry Eremin-Solenikov #include <linux/delay.h>
205d8c6658SDmitry Eremin-Solenikov #include <linux/sched.h>
215d8c6658SDmitry Eremin-Solenikov #include <linux/cpufreq.h>
225d8c6658SDmitry Eremin-Solenikov #include <linux/init.h>
235d8c6658SDmitry Eremin-Solenikov #include <linux/completion.h>
245d8c6658SDmitry Eremin-Solenikov #include <linux/mutex.h>
255d8c6658SDmitry Eremin-Solenikov #include <linux/time.h>
26*21bb32b1SRob Herring #include <linux/of.h>
275d8c6658SDmitry Eremin-Solenikov
285d8c6658SDmitry Eremin-Solenikov #define DBG(fmt...) pr_debug(fmt)
295d8c6658SDmitry Eremin-Solenikov
305d8c6658SDmitry Eremin-Solenikov /* see 970FX user manual */
315d8c6658SDmitry Eremin-Solenikov
325d8c6658SDmitry Eremin-Solenikov #define SCOM_PCR 0x0aa001 /* PCR scom addr */
335d8c6658SDmitry Eremin-Solenikov
345d8c6658SDmitry Eremin-Solenikov #define PCR_HILO_SELECT 0x80000000U /* 1 = PCR, 0 = PCRH */
355d8c6658SDmitry Eremin-Solenikov #define PCR_SPEED_FULL 0x00000000U /* 1:1 speed value */
365d8c6658SDmitry Eremin-Solenikov #define PCR_SPEED_HALF 0x00020000U /* 1:2 speed value */
375d8c6658SDmitry Eremin-Solenikov #define PCR_SPEED_QUARTER 0x00040000U /* 1:4 speed value */
385d8c6658SDmitry Eremin-Solenikov #define PCR_SPEED_MASK 0x000e0000U /* speed mask */
395d8c6658SDmitry Eremin-Solenikov #define PCR_SPEED_SHIFT 17
405d8c6658SDmitry Eremin-Solenikov #define PCR_FREQ_REQ_VALID 0x00010000U /* freq request valid */
415d8c6658SDmitry Eremin-Solenikov #define PCR_VOLT_REQ_VALID 0x00008000U /* volt request valid */
425d8c6658SDmitry Eremin-Solenikov #define PCR_TARGET_TIME_MASK 0x00006000U /* target time */
435d8c6658SDmitry Eremin-Solenikov #define PCR_STATLAT_MASK 0x00001f00U /* STATLAT value */
445d8c6658SDmitry Eremin-Solenikov #define PCR_SNOOPLAT_MASK 0x000000f0U /* SNOOPLAT value */
455d8c6658SDmitry Eremin-Solenikov #define PCR_SNOOPACC_MASK 0x0000000fU /* SNOOPACC value */
465d8c6658SDmitry Eremin-Solenikov
475d8c6658SDmitry Eremin-Solenikov #define SCOM_PSR 0x408001 /* PSR scom addr */
485d8c6658SDmitry Eremin-Solenikov /* warning: PSR is a 64 bits register */
495d8c6658SDmitry Eremin-Solenikov #define PSR_CMD_RECEIVED 0x2000000000000000U /* command received */
505d8c6658SDmitry Eremin-Solenikov #define PSR_CMD_COMPLETED 0x1000000000000000U /* command completed */
515d8c6658SDmitry Eremin-Solenikov #define PSR_CUR_SPEED_MASK 0x0300000000000000U /* current speed */
525d8c6658SDmitry Eremin-Solenikov #define PSR_CUR_SPEED_SHIFT (56)
535d8c6658SDmitry Eremin-Solenikov
545d8c6658SDmitry Eremin-Solenikov /*
555d8c6658SDmitry Eremin-Solenikov * The G5 only supports two frequencies (Quarter speed is not supported)
565d8c6658SDmitry Eremin-Solenikov */
575d8c6658SDmitry Eremin-Solenikov #define CPUFREQ_HIGH 0
585d8c6658SDmitry Eremin-Solenikov #define CPUFREQ_LOW 1
595d8c6658SDmitry Eremin-Solenikov
605d8c6658SDmitry Eremin-Solenikov static struct cpufreq_frequency_table maple_cpu_freqs[] = {
617f4b0461SViresh Kumar {0, CPUFREQ_HIGH, 0},
627f4b0461SViresh Kumar {0, CPUFREQ_LOW, 0},
637f4b0461SViresh Kumar {0, 0, CPUFREQ_TABLE_END},
645d8c6658SDmitry Eremin-Solenikov };
655d8c6658SDmitry Eremin-Solenikov
665d8c6658SDmitry Eremin-Solenikov /* Power mode data is an array of the 32 bits PCR values to use for
675d8c6658SDmitry Eremin-Solenikov * the various frequencies, retrieved from the device-tree
685d8c6658SDmitry Eremin-Solenikov */
695d8c6658SDmitry Eremin-Solenikov static int maple_pmode_cur;
705d8c6658SDmitry Eremin-Solenikov
715d8c6658SDmitry Eremin-Solenikov static const u32 *maple_pmode_data;
725d8c6658SDmitry Eremin-Solenikov static int maple_pmode_max;
735d8c6658SDmitry Eremin-Solenikov
745d8c6658SDmitry Eremin-Solenikov /*
755d8c6658SDmitry Eremin-Solenikov * SCOM based frequency switching for 970FX rev3
765d8c6658SDmitry Eremin-Solenikov */
maple_scom_switch_freq(int speed_mode)775d8c6658SDmitry Eremin-Solenikov static int maple_scom_switch_freq(int speed_mode)
785d8c6658SDmitry Eremin-Solenikov {
795d8c6658SDmitry Eremin-Solenikov unsigned long flags;
805d8c6658SDmitry Eremin-Solenikov int to;
815d8c6658SDmitry Eremin-Solenikov
825d8c6658SDmitry Eremin-Solenikov local_irq_save(flags);
835d8c6658SDmitry Eremin-Solenikov
845d8c6658SDmitry Eremin-Solenikov /* Clear PCR high */
855d8c6658SDmitry Eremin-Solenikov scom970_write(SCOM_PCR, 0);
865d8c6658SDmitry Eremin-Solenikov /* Clear PCR low */
875d8c6658SDmitry Eremin-Solenikov scom970_write(SCOM_PCR, PCR_HILO_SELECT | 0);
885d8c6658SDmitry Eremin-Solenikov /* Set PCR low */
895d8c6658SDmitry Eremin-Solenikov scom970_write(SCOM_PCR, PCR_HILO_SELECT |
905d8c6658SDmitry Eremin-Solenikov maple_pmode_data[speed_mode]);
915d8c6658SDmitry Eremin-Solenikov
925d8c6658SDmitry Eremin-Solenikov /* Wait for completion */
935d8c6658SDmitry Eremin-Solenikov for (to = 0; to < 10; to++) {
945d8c6658SDmitry Eremin-Solenikov unsigned long psr = scom970_read(SCOM_PSR);
955d8c6658SDmitry Eremin-Solenikov
965d8c6658SDmitry Eremin-Solenikov if ((psr & PSR_CMD_RECEIVED) == 0 &&
975d8c6658SDmitry Eremin-Solenikov (((psr >> PSR_CUR_SPEED_SHIFT) ^
985d8c6658SDmitry Eremin-Solenikov (maple_pmode_data[speed_mode] >> PCR_SPEED_SHIFT)) & 0x3)
995d8c6658SDmitry Eremin-Solenikov == 0)
1005d8c6658SDmitry Eremin-Solenikov break;
1015d8c6658SDmitry Eremin-Solenikov if (psr & PSR_CMD_COMPLETED)
1025d8c6658SDmitry Eremin-Solenikov break;
1035d8c6658SDmitry Eremin-Solenikov udelay(100);
1045d8c6658SDmitry Eremin-Solenikov }
1055d8c6658SDmitry Eremin-Solenikov
1065d8c6658SDmitry Eremin-Solenikov local_irq_restore(flags);
1075d8c6658SDmitry Eremin-Solenikov
1085d8c6658SDmitry Eremin-Solenikov maple_pmode_cur = speed_mode;
1095d8c6658SDmitry Eremin-Solenikov ppc_proc_freq = maple_cpu_freqs[speed_mode].frequency * 1000ul;
1105d8c6658SDmitry Eremin-Solenikov
1115d8c6658SDmitry Eremin-Solenikov return 0;
1125d8c6658SDmitry Eremin-Solenikov }
1135d8c6658SDmitry Eremin-Solenikov
maple_scom_query_freq(void)1145d8c6658SDmitry Eremin-Solenikov static int maple_scom_query_freq(void)
1155d8c6658SDmitry Eremin-Solenikov {
1165d8c6658SDmitry Eremin-Solenikov unsigned long psr = scom970_read(SCOM_PSR);
1175d8c6658SDmitry Eremin-Solenikov int i;
1185d8c6658SDmitry Eremin-Solenikov
1195d8c6658SDmitry Eremin-Solenikov for (i = 0; i <= maple_pmode_max; i++)
1205d8c6658SDmitry Eremin-Solenikov if ((((psr >> PSR_CUR_SPEED_SHIFT) ^
1215d8c6658SDmitry Eremin-Solenikov (maple_pmode_data[i] >> PCR_SPEED_SHIFT)) & 0x3) == 0)
1225d8c6658SDmitry Eremin-Solenikov break;
1235d8c6658SDmitry Eremin-Solenikov return i;
1245d8c6658SDmitry Eremin-Solenikov }
1255d8c6658SDmitry Eremin-Solenikov
1265d8c6658SDmitry Eremin-Solenikov /*
1275d8c6658SDmitry Eremin-Solenikov * Common interface to the cpufreq core
1285d8c6658SDmitry Eremin-Solenikov */
1295d8c6658SDmitry Eremin-Solenikov
maple_cpufreq_target(struct cpufreq_policy * policy,unsigned int index)1305d8c6658SDmitry Eremin-Solenikov static int maple_cpufreq_target(struct cpufreq_policy *policy,
1319c0ebcf7SViresh Kumar unsigned int index)
1325d8c6658SDmitry Eremin-Solenikov {
133d4019f0aSViresh Kumar return maple_scom_switch_freq(index);
1345d8c6658SDmitry Eremin-Solenikov }
1355d8c6658SDmitry Eremin-Solenikov
maple_cpufreq_get_speed(unsigned int cpu)1365d8c6658SDmitry Eremin-Solenikov static unsigned int maple_cpufreq_get_speed(unsigned int cpu)
1375d8c6658SDmitry Eremin-Solenikov {
1385d8c6658SDmitry Eremin-Solenikov return maple_cpu_freqs[maple_pmode_cur].frequency;
1395d8c6658SDmitry Eremin-Solenikov }
1405d8c6658SDmitry Eremin-Solenikov
maple_cpufreq_cpu_init(struct cpufreq_policy * policy)1415d8c6658SDmitry Eremin-Solenikov static int maple_cpufreq_cpu_init(struct cpufreq_policy *policy)
1425d8c6658SDmitry Eremin-Solenikov {
143c4dcc8a1SViresh Kumar cpufreq_generic_init(policy, maple_cpu_freqs, 12000);
144c4dcc8a1SViresh Kumar return 0;
1455d8c6658SDmitry Eremin-Solenikov }
1465d8c6658SDmitry Eremin-Solenikov
1475d8c6658SDmitry Eremin-Solenikov static struct cpufreq_driver maple_cpufreq_driver = {
1485d8c6658SDmitry Eremin-Solenikov .name = "maple",
1495d8c6658SDmitry Eremin-Solenikov .flags = CPUFREQ_CONST_LOOPS,
1505d8c6658SDmitry Eremin-Solenikov .init = maple_cpufreq_cpu_init,
151b766b908SViresh Kumar .verify = cpufreq_generic_frequency_table_verify,
1529c0ebcf7SViresh Kumar .target_index = maple_cpufreq_target,
1535d8c6658SDmitry Eremin-Solenikov .get = maple_cpufreq_get_speed,
154b766b908SViresh Kumar .attr = cpufreq_generic_attr,
1555d8c6658SDmitry Eremin-Solenikov };
1565d8c6658SDmitry Eremin-Solenikov
maple_cpufreq_init(void)1575d8c6658SDmitry Eremin-Solenikov static int __init maple_cpufreq_init(void)
1585d8c6658SDmitry Eremin-Solenikov {
1595d8c6658SDmitry Eremin-Solenikov struct device_node *cpunode;
1605d8c6658SDmitry Eremin-Solenikov unsigned int psize;
1615d8c6658SDmitry Eremin-Solenikov unsigned long max_freq;
1625d8c6658SDmitry Eremin-Solenikov const u32 *valp;
1635d8c6658SDmitry Eremin-Solenikov u32 pvr_hi;
1645d8c6658SDmitry Eremin-Solenikov int rc = -ENODEV;
1655d8c6658SDmitry Eremin-Solenikov
1665d8c6658SDmitry Eremin-Solenikov /*
1675d8c6658SDmitry Eremin-Solenikov * Behave here like powermac driver which checks machine compatibility
1685d8c6658SDmitry Eremin-Solenikov * to ease merging of two drivers in future.
1695d8c6658SDmitry Eremin-Solenikov */
1705d8c6658SDmitry Eremin-Solenikov if (!of_machine_is_compatible("Momentum,Maple") &&
1715d8c6658SDmitry Eremin-Solenikov !of_machine_is_compatible("Momentum,Apache"))
1725d8c6658SDmitry Eremin-Solenikov return 0;
1735d8c6658SDmitry Eremin-Solenikov
1745d8c6658SDmitry Eremin-Solenikov /* Get first CPU node */
1752421d4c3SSudeep KarkadaNagesha cpunode = of_cpu_device_node_get(0);
1765d8c6658SDmitry Eremin-Solenikov if (cpunode == NULL) {
1771c5864e2SJoe Perches pr_err("Can't find any CPU 0 node\n");
1782421d4c3SSudeep KarkadaNagesha goto bail_noprops;
1795d8c6658SDmitry Eremin-Solenikov }
1805d8c6658SDmitry Eremin-Solenikov
1815d8c6658SDmitry Eremin-Solenikov /* Check 970FX for now */
1825d8c6658SDmitry Eremin-Solenikov /* we actually don't care on which CPU to access PVR */
1835d8c6658SDmitry Eremin-Solenikov pvr_hi = PVR_VER(mfspr(SPRN_PVR));
1845d8c6658SDmitry Eremin-Solenikov if (pvr_hi != 0x3c && pvr_hi != 0x44) {
1851c5864e2SJoe Perches pr_err("Unsupported CPU version (%x)\n", pvr_hi);
1865d8c6658SDmitry Eremin-Solenikov goto bail_noprops;
1875d8c6658SDmitry Eremin-Solenikov }
1885d8c6658SDmitry Eremin-Solenikov
1895d8c6658SDmitry Eremin-Solenikov /* Look for the powertune data in the device-tree */
1905d8c6658SDmitry Eremin-Solenikov /*
1915d8c6658SDmitry Eremin-Solenikov * On Maple this property is provided by PIBS in dual-processor config,
1925d8c6658SDmitry Eremin-Solenikov * not provided by PIBS in CPU0 config and also not provided by SLOF,
1935d8c6658SDmitry Eremin-Solenikov * so YMMV
1945d8c6658SDmitry Eremin-Solenikov */
1955d8c6658SDmitry Eremin-Solenikov maple_pmode_data = of_get_property(cpunode, "power-mode-data", &psize);
1965d8c6658SDmitry Eremin-Solenikov if (!maple_pmode_data) {
1975d8c6658SDmitry Eremin-Solenikov DBG("No power-mode-data !\n");
1985d8c6658SDmitry Eremin-Solenikov goto bail_noprops;
1995d8c6658SDmitry Eremin-Solenikov }
2005d8c6658SDmitry Eremin-Solenikov maple_pmode_max = psize / sizeof(u32) - 1;
2015d8c6658SDmitry Eremin-Solenikov
2025d8c6658SDmitry Eremin-Solenikov /*
2035d8c6658SDmitry Eremin-Solenikov * From what I see, clock-frequency is always the maximal frequency.
2045d8c6658SDmitry Eremin-Solenikov * The current driver can not slew sysclk yet, so we really only deal
2055d8c6658SDmitry Eremin-Solenikov * with powertune steps for now. We also only implement full freq and
2065d8c6658SDmitry Eremin-Solenikov * half freq in this version. So far, I haven't yet seen a machine
2075d8c6658SDmitry Eremin-Solenikov * supporting anything else.
2085d8c6658SDmitry Eremin-Solenikov */
2095d8c6658SDmitry Eremin-Solenikov valp = of_get_property(cpunode, "clock-frequency", NULL);
2105d8c6658SDmitry Eremin-Solenikov if (!valp)
211ddb07fbaSWen Yang goto bail_noprops;
2125d8c6658SDmitry Eremin-Solenikov max_freq = (*valp)/1000;
2135d8c6658SDmitry Eremin-Solenikov maple_cpu_freqs[0].frequency = max_freq;
2145d8c6658SDmitry Eremin-Solenikov maple_cpu_freqs[1].frequency = max_freq/2;
2155d8c6658SDmitry Eremin-Solenikov
2165d8c6658SDmitry Eremin-Solenikov /* Force apply current frequency to make sure everything is in
2175d8c6658SDmitry Eremin-Solenikov * sync (voltage is right for example). Firmware may leave us with
2185d8c6658SDmitry Eremin-Solenikov * a strange setting ...
2195d8c6658SDmitry Eremin-Solenikov */
2205d8c6658SDmitry Eremin-Solenikov msleep(10);
2215d8c6658SDmitry Eremin-Solenikov maple_pmode_cur = -1;
2225d8c6658SDmitry Eremin-Solenikov maple_scom_switch_freq(maple_scom_query_freq());
2235d8c6658SDmitry Eremin-Solenikov
224b49c22a6SJoe Perches pr_info("Registering Maple CPU frequency driver\n");
225b49c22a6SJoe Perches pr_info("Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
2265d8c6658SDmitry Eremin-Solenikov maple_cpu_freqs[1].frequency/1000,
2275d8c6658SDmitry Eremin-Solenikov maple_cpu_freqs[0].frequency/1000,
2285d8c6658SDmitry Eremin-Solenikov maple_cpu_freqs[maple_pmode_cur].frequency/1000);
2295d8c6658SDmitry Eremin-Solenikov
2305d8c6658SDmitry Eremin-Solenikov rc = cpufreq_register_driver(&maple_cpufreq_driver);
2315d8c6658SDmitry Eremin-Solenikov
2325d8c6658SDmitry Eremin-Solenikov bail_noprops:
2335d8c6658SDmitry Eremin-Solenikov of_node_put(cpunode);
2345d8c6658SDmitry Eremin-Solenikov
2355d8c6658SDmitry Eremin-Solenikov return rc;
2365d8c6658SDmitry Eremin-Solenikov }
2375d8c6658SDmitry Eremin-Solenikov
2385d8c6658SDmitry Eremin-Solenikov module_init(maple_cpufreq_init);
2395d8c6658SDmitry Eremin-Solenikov
2405d8c6658SDmitry Eremin-Solenikov
2415d8c6658SDmitry Eremin-Solenikov MODULE_LICENSE("GPL");
242