18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2011 Dmitry Eremin-Solenikov 48c2ecf20Sopenharmony_ci * Copyright (C) 2002 - 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org> 58c2ecf20Sopenharmony_ci * and Markus Demleitner <msdemlei@cl.uni-heidelberg.de> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * This driver adds basic cpufreq support for SMU & 970FX based G5 Macs, 88c2ecf20Sopenharmony_ci * that is iMac G5 and latest single CPU desktop. 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#undef DEBUG 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <linux/module.h> 168c2ecf20Sopenharmony_ci#include <linux/types.h> 178c2ecf20Sopenharmony_ci#include <linux/errno.h> 188c2ecf20Sopenharmony_ci#include <linux/kernel.h> 198c2ecf20Sopenharmony_ci#include <linux/delay.h> 208c2ecf20Sopenharmony_ci#include <linux/sched.h> 218c2ecf20Sopenharmony_ci#include <linux/cpufreq.h> 228c2ecf20Sopenharmony_ci#include <linux/init.h> 238c2ecf20Sopenharmony_ci#include <linux/completion.h> 248c2ecf20Sopenharmony_ci#include <linux/mutex.h> 258c2ecf20Sopenharmony_ci#include <linux/time.h> 268c2ecf20Sopenharmony_ci#include <linux/of_device.h> 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define DBG(fmt...) pr_debug(fmt) 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/* see 970FX user manual */ 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define SCOM_PCR 0x0aa001 /* PCR scom addr */ 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#define PCR_HILO_SELECT 0x80000000U /* 1 = PCR, 0 = PCRH */ 358c2ecf20Sopenharmony_ci#define PCR_SPEED_FULL 0x00000000U /* 1:1 speed value */ 368c2ecf20Sopenharmony_ci#define PCR_SPEED_HALF 0x00020000U /* 1:2 speed value */ 378c2ecf20Sopenharmony_ci#define PCR_SPEED_QUARTER 0x00040000U /* 1:4 speed value */ 388c2ecf20Sopenharmony_ci#define PCR_SPEED_MASK 0x000e0000U /* speed mask */ 398c2ecf20Sopenharmony_ci#define PCR_SPEED_SHIFT 17 408c2ecf20Sopenharmony_ci#define PCR_FREQ_REQ_VALID 0x00010000U /* freq request valid */ 418c2ecf20Sopenharmony_ci#define PCR_VOLT_REQ_VALID 0x00008000U /* volt request valid */ 428c2ecf20Sopenharmony_ci#define PCR_TARGET_TIME_MASK 0x00006000U /* target time */ 438c2ecf20Sopenharmony_ci#define PCR_STATLAT_MASK 0x00001f00U /* STATLAT value */ 448c2ecf20Sopenharmony_ci#define PCR_SNOOPLAT_MASK 0x000000f0U /* SNOOPLAT value */ 458c2ecf20Sopenharmony_ci#define PCR_SNOOPACC_MASK 0x0000000fU /* SNOOPACC value */ 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#define SCOM_PSR 0x408001 /* PSR scom addr */ 488c2ecf20Sopenharmony_ci/* warning: PSR is a 64 bits register */ 498c2ecf20Sopenharmony_ci#define PSR_CMD_RECEIVED 0x2000000000000000U /* command received */ 508c2ecf20Sopenharmony_ci#define PSR_CMD_COMPLETED 0x1000000000000000U /* command completed */ 518c2ecf20Sopenharmony_ci#define PSR_CUR_SPEED_MASK 0x0300000000000000U /* current speed */ 528c2ecf20Sopenharmony_ci#define PSR_CUR_SPEED_SHIFT (56) 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/* 558c2ecf20Sopenharmony_ci * The G5 only supports two frequencies (Quarter speed is not supported) 568c2ecf20Sopenharmony_ci */ 578c2ecf20Sopenharmony_ci#define CPUFREQ_HIGH 0 588c2ecf20Sopenharmony_ci#define CPUFREQ_LOW 1 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistatic struct cpufreq_frequency_table maple_cpu_freqs[] = { 618c2ecf20Sopenharmony_ci {0, CPUFREQ_HIGH, 0}, 628c2ecf20Sopenharmony_ci {0, CPUFREQ_LOW, 0}, 638c2ecf20Sopenharmony_ci {0, 0, CPUFREQ_TABLE_END}, 648c2ecf20Sopenharmony_ci}; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/* Power mode data is an array of the 32 bits PCR values to use for 678c2ecf20Sopenharmony_ci * the various frequencies, retrieved from the device-tree 688c2ecf20Sopenharmony_ci */ 698c2ecf20Sopenharmony_cistatic int maple_pmode_cur; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic const u32 *maple_pmode_data; 728c2ecf20Sopenharmony_cistatic int maple_pmode_max; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci/* 758c2ecf20Sopenharmony_ci * SCOM based frequency switching for 970FX rev3 768c2ecf20Sopenharmony_ci */ 778c2ecf20Sopenharmony_cistatic int maple_scom_switch_freq(int speed_mode) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci unsigned long flags; 808c2ecf20Sopenharmony_ci int to; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci local_irq_save(flags); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci /* Clear PCR high */ 858c2ecf20Sopenharmony_ci scom970_write(SCOM_PCR, 0); 868c2ecf20Sopenharmony_ci /* Clear PCR low */ 878c2ecf20Sopenharmony_ci scom970_write(SCOM_PCR, PCR_HILO_SELECT | 0); 888c2ecf20Sopenharmony_ci /* Set PCR low */ 898c2ecf20Sopenharmony_ci scom970_write(SCOM_PCR, PCR_HILO_SELECT | 908c2ecf20Sopenharmony_ci maple_pmode_data[speed_mode]); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci /* Wait for completion */ 938c2ecf20Sopenharmony_ci for (to = 0; to < 10; to++) { 948c2ecf20Sopenharmony_ci unsigned long psr = scom970_read(SCOM_PSR); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci if ((psr & PSR_CMD_RECEIVED) == 0 && 978c2ecf20Sopenharmony_ci (((psr >> PSR_CUR_SPEED_SHIFT) ^ 988c2ecf20Sopenharmony_ci (maple_pmode_data[speed_mode] >> PCR_SPEED_SHIFT)) & 0x3) 998c2ecf20Sopenharmony_ci == 0) 1008c2ecf20Sopenharmony_ci break; 1018c2ecf20Sopenharmony_ci if (psr & PSR_CMD_COMPLETED) 1028c2ecf20Sopenharmony_ci break; 1038c2ecf20Sopenharmony_ci udelay(100); 1048c2ecf20Sopenharmony_ci } 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci local_irq_restore(flags); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci maple_pmode_cur = speed_mode; 1098c2ecf20Sopenharmony_ci ppc_proc_freq = maple_cpu_freqs[speed_mode].frequency * 1000ul; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci return 0; 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic int maple_scom_query_freq(void) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci unsigned long psr = scom970_read(SCOM_PSR); 1178c2ecf20Sopenharmony_ci int i; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci for (i = 0; i <= maple_pmode_max; i++) 1208c2ecf20Sopenharmony_ci if ((((psr >> PSR_CUR_SPEED_SHIFT) ^ 1218c2ecf20Sopenharmony_ci (maple_pmode_data[i] >> PCR_SPEED_SHIFT)) & 0x3) == 0) 1228c2ecf20Sopenharmony_ci break; 1238c2ecf20Sopenharmony_ci return i; 1248c2ecf20Sopenharmony_ci} 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci/* 1278c2ecf20Sopenharmony_ci * Common interface to the cpufreq core 1288c2ecf20Sopenharmony_ci */ 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic int maple_cpufreq_target(struct cpufreq_policy *policy, 1318c2ecf20Sopenharmony_ci unsigned int index) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci return maple_scom_switch_freq(index); 1348c2ecf20Sopenharmony_ci} 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic unsigned int maple_cpufreq_get_speed(unsigned int cpu) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci return maple_cpu_freqs[maple_pmode_cur].frequency; 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistatic int maple_cpufreq_cpu_init(struct cpufreq_policy *policy) 1428c2ecf20Sopenharmony_ci{ 1438c2ecf20Sopenharmony_ci cpufreq_generic_init(policy, maple_cpu_freqs, 12000); 1448c2ecf20Sopenharmony_ci return 0; 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cistatic struct cpufreq_driver maple_cpufreq_driver = { 1488c2ecf20Sopenharmony_ci .name = "maple", 1498c2ecf20Sopenharmony_ci .flags = CPUFREQ_CONST_LOOPS, 1508c2ecf20Sopenharmony_ci .init = maple_cpufreq_cpu_init, 1518c2ecf20Sopenharmony_ci .verify = cpufreq_generic_frequency_table_verify, 1528c2ecf20Sopenharmony_ci .target_index = maple_cpufreq_target, 1538c2ecf20Sopenharmony_ci .get = maple_cpufreq_get_speed, 1548c2ecf20Sopenharmony_ci .attr = cpufreq_generic_attr, 1558c2ecf20Sopenharmony_ci}; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic int __init maple_cpufreq_init(void) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci struct device_node *cpunode; 1608c2ecf20Sopenharmony_ci unsigned int psize; 1618c2ecf20Sopenharmony_ci unsigned long max_freq; 1628c2ecf20Sopenharmony_ci const u32 *valp; 1638c2ecf20Sopenharmony_ci u32 pvr_hi; 1648c2ecf20Sopenharmony_ci int rc = -ENODEV; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci /* 1678c2ecf20Sopenharmony_ci * Behave here like powermac driver which checks machine compatibility 1688c2ecf20Sopenharmony_ci * to ease merging of two drivers in future. 1698c2ecf20Sopenharmony_ci */ 1708c2ecf20Sopenharmony_ci if (!of_machine_is_compatible("Momentum,Maple") && 1718c2ecf20Sopenharmony_ci !of_machine_is_compatible("Momentum,Apache")) 1728c2ecf20Sopenharmony_ci return 0; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci /* Get first CPU node */ 1758c2ecf20Sopenharmony_ci cpunode = of_cpu_device_node_get(0); 1768c2ecf20Sopenharmony_ci if (cpunode == NULL) { 1778c2ecf20Sopenharmony_ci pr_err("Can't find any CPU 0 node\n"); 1788c2ecf20Sopenharmony_ci goto bail_noprops; 1798c2ecf20Sopenharmony_ci } 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci /* Check 970FX for now */ 1828c2ecf20Sopenharmony_ci /* we actually don't care on which CPU to access PVR */ 1838c2ecf20Sopenharmony_ci pvr_hi = PVR_VER(mfspr(SPRN_PVR)); 1848c2ecf20Sopenharmony_ci if (pvr_hi != 0x3c && pvr_hi != 0x44) { 1858c2ecf20Sopenharmony_ci pr_err("Unsupported CPU version (%x)\n", pvr_hi); 1868c2ecf20Sopenharmony_ci goto bail_noprops; 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci /* Look for the powertune data in the device-tree */ 1908c2ecf20Sopenharmony_ci /* 1918c2ecf20Sopenharmony_ci * On Maple this property is provided by PIBS in dual-processor config, 1928c2ecf20Sopenharmony_ci * not provided by PIBS in CPU0 config and also not provided by SLOF, 1938c2ecf20Sopenharmony_ci * so YMMV 1948c2ecf20Sopenharmony_ci */ 1958c2ecf20Sopenharmony_ci maple_pmode_data = of_get_property(cpunode, "power-mode-data", &psize); 1968c2ecf20Sopenharmony_ci if (!maple_pmode_data) { 1978c2ecf20Sopenharmony_ci DBG("No power-mode-data !\n"); 1988c2ecf20Sopenharmony_ci goto bail_noprops; 1998c2ecf20Sopenharmony_ci } 2008c2ecf20Sopenharmony_ci maple_pmode_max = psize / sizeof(u32) - 1; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci /* 2038c2ecf20Sopenharmony_ci * From what I see, clock-frequency is always the maximal frequency. 2048c2ecf20Sopenharmony_ci * The current driver can not slew sysclk yet, so we really only deal 2058c2ecf20Sopenharmony_ci * with powertune steps for now. We also only implement full freq and 2068c2ecf20Sopenharmony_ci * half freq in this version. So far, I haven't yet seen a machine 2078c2ecf20Sopenharmony_ci * supporting anything else. 2088c2ecf20Sopenharmony_ci */ 2098c2ecf20Sopenharmony_ci valp = of_get_property(cpunode, "clock-frequency", NULL); 2108c2ecf20Sopenharmony_ci if (!valp) 2118c2ecf20Sopenharmony_ci goto bail_noprops; 2128c2ecf20Sopenharmony_ci max_freq = (*valp)/1000; 2138c2ecf20Sopenharmony_ci maple_cpu_freqs[0].frequency = max_freq; 2148c2ecf20Sopenharmony_ci maple_cpu_freqs[1].frequency = max_freq/2; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci /* Force apply current frequency to make sure everything is in 2178c2ecf20Sopenharmony_ci * sync (voltage is right for example). Firmware may leave us with 2188c2ecf20Sopenharmony_ci * a strange setting ... 2198c2ecf20Sopenharmony_ci */ 2208c2ecf20Sopenharmony_ci msleep(10); 2218c2ecf20Sopenharmony_ci maple_pmode_cur = -1; 2228c2ecf20Sopenharmony_ci maple_scom_switch_freq(maple_scom_query_freq()); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci pr_info("Registering Maple CPU frequency driver\n"); 2258c2ecf20Sopenharmony_ci pr_info("Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n", 2268c2ecf20Sopenharmony_ci maple_cpu_freqs[1].frequency/1000, 2278c2ecf20Sopenharmony_ci maple_cpu_freqs[0].frequency/1000, 2288c2ecf20Sopenharmony_ci maple_cpu_freqs[maple_pmode_cur].frequency/1000); 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci rc = cpufreq_register_driver(&maple_cpufreq_driver); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_cibail_noprops: 2338c2ecf20Sopenharmony_ci of_node_put(cpunode); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci return rc; 2368c2ecf20Sopenharmony_ci} 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_cimodule_init(maple_cpufreq_init); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 242