// SPDX-License-Identifier: GPL-2.0-only1/*2* Cyrix MediaGX and NatSemi Geode Suspend Modulation3* (C) 2002 Zwane Mwaikambo <[email protected]>4* (C) 2002 Hiroshi Miura <[email protected]>5* All Rights Reserved6*7* The author(s) of this software shall not be held liable for damages8* of any nature resulting due to the use of this software. This9* software is provided AS-IS with no warranties.10*11* Theoretical note:12*13* (see Geode(tm) CS5530 manual (rev.4.1) page.56)14*15* CPU frequency control on NatSemi Geode GX1/GXLV processor and CS55x016* are based on Suspend Modulation.17*18* Suspend Modulation works by asserting and de-asserting the SUSP# pin19* to CPU(GX1/GXLV) for configurable durations. When asserting SUSP#20* the CPU enters an idle state. GX1 stops its core clock when SUSP# is21* asserted then power consumption is reduced.22*23* Suspend Modulation's OFF/ON duration are configurable24* with 'Suspend Modulation OFF Count Register'25* and 'Suspend Modulation ON Count Register'.26* These registers are 8bit counters that represent the number of27* 32us intervals which the SUSP# pin is asserted(ON)/de-asserted(OFF)28* to the processor.29*30* These counters define a ratio which is the effective frequency31* of operation of the system.32*33* OFF Count34* F_eff = Fgx * ----------------------35* OFF Count + ON Count36*37* 0 <= On Count, Off Count <= 25538*39* From these limits, we can get register values40*41* off_duration + on_duration <= MAX_DURATION42* on_duration = off_duration * (stock_freq - freq) / freq43*44* off_duration = (freq * DURATION) / stock_freq45* on_duration = DURATION - off_duration46*47*---------------------------------------------------------------------------48*49* ChangeLog:50* Dec. 12, 2003 Hiroshi Miura <[email protected]>51* - fix on/off register mistake52* - fix cpu_khz calc when it stops cpu modulation.53*54* Dec. 11, 2002 Hiroshi Miura <[email protected]>55* - rewrite for Cyrix MediaGX Cx5510/5520 and56* NatSemi Geode Cs5530(A).57*58* Jul. ??, 2002 Zwane Mwaikambo <[email protected]>59* - cs5530_mod patch for 2.4.19-rc1.60*61*---------------------------------------------------------------------------62*63* Todo64* Test on machines with 5510, 5530, 5530A65*/6667/************************************************************************68* Suspend Modulation - Definitions *69************************************************************************/7071#include <linux/kernel.h>72#include <linux/module.h>73#include <linux/init.h>74#include <linux/smp.h>75#include <linux/cpufreq.h>76#include <linux/pci.h>77#include <linux/errno.h>78#include <linux/slab.h>7980#include <asm/cpu_device_id.h>81#include <asm/processor-cyrix.h>8283/* PCI config registers, all at F0 */84#define PCI_PMER1 0x80 /* power management enable register 1 */85#define PCI_PMER2 0x81 /* power management enable register 2 */86#define PCI_PMER3 0x82 /* power management enable register 3 */87#define PCI_IRQTC 0x8c /* irq speedup timer counter register:typical 2 to 4ms */88#define PCI_VIDTC 0x8d /* video speedup timer counter register: typical 50 to 100ms */89#define PCI_MODOFF 0x94 /* suspend modulation OFF counter register, 1 = 32us */90#define PCI_MODON 0x95 /* suspend modulation ON counter register */91#define PCI_SUSCFG 0x96 /* suspend configuration register */9293/* PMER1 bits */94#define GPM (1<<0) /* global power management */95#define GIT (1<<1) /* globally enable PM device idle timers */96#define GTR (1<<2) /* globally enable IO traps */97#define IRQ_SPDUP (1<<3) /* disable clock throttle during interrupt handling */98#define VID_SPDUP (1<<4) /* disable clock throttle during vga video handling */99100/* SUSCFG bits */101#define SUSMOD (1<<0) /* enable/disable suspend modulation */102/* the below is supported only with cs5530 (after rev.1.2)/cs5530A */103#define SMISPDUP (1<<1) /* select how SMI re-enable suspend modulation: */104/* IRQTC timer or read SMI speedup disable reg.(F1BAR[08-09h]) */105#define SUSCFG (1<<2) /* enable powering down a GXLV processor. "Special 3Volt Suspend" mode */106/* the below is supported only with cs5530A */107#define PWRSVE_ISA (1<<3) /* stop ISA clock */108#define PWRSVE (1<<4) /* active idle */109110struct gxfreq_params {111u8 on_duration;112u8 off_duration;113u8 pci_suscfg;114u8 pci_pmer1;115u8 pci_pmer2;116struct pci_dev *cs55x0;117};118119static struct gxfreq_params *gx_params;120static int stock_freq;121122/* PCI bus clock - defaults to 30.000 if cpu_khz is not available */123static int pci_busclk;124module_param(pci_busclk, int, 0444);125126/* maximum duration for which the cpu may be suspended127* (32us * MAX_DURATION). If no parameter is given, this defaults128* to 255.129* Note that this leads to a maximum of 8 ms(!) where the CPU clock130* is suspended -- processing power is just 0.39% of what it used to be,131* though. 781.25 kHz(!) for a 200 MHz processor -- wow. */132static int max_duration = 255;133module_param(max_duration, int, 0444);134135/* For the default policy, we want at least some processing power136* - let's say 5%. (min = maxfreq / POLICY_MIN_DIV)137*/138#define POLICY_MIN_DIV 20139140141/**142* we can detect a core multiplier from dir0_lsb143* from GX1 datasheet p.56,144* MULT[3:0]:145* 0000 = SYSCLK multiplied by 4 (test only)146* 0001 = SYSCLK multiplied by 10147* 0010 = SYSCLK multiplied by 4148* 0011 = SYSCLK multiplied by 6149* 0100 = SYSCLK multiplied by 9150* 0101 = SYSCLK multiplied by 5151* 0110 = SYSCLK multiplied by 7152* 0111 = SYSCLK multiplied by 8153* of 33.3MHz154**/155static int gx_freq_mult[16] = {1564, 10, 4, 6, 9, 5, 7, 8,1570, 0, 0, 0, 0, 0, 0, 0158};159160161/****************************************************************162* Low Level chipset interface *163****************************************************************/164static struct pci_device_id gx_chipset_tbl[] __initdata = {165{ PCI_VDEVICE(CYRIX, PCI_DEVICE_ID_CYRIX_5530_LEGACY), },166{ PCI_VDEVICE(CYRIX, PCI_DEVICE_ID_CYRIX_5520), },167{ PCI_VDEVICE(CYRIX, PCI_DEVICE_ID_CYRIX_5510), },168{ 0, },169};170MODULE_DEVICE_TABLE(pci, gx_chipset_tbl);171172static void gx_write_byte(int reg, int value)173{174pci_write_config_byte(gx_params->cs55x0, reg, value);175}176177/**178* gx_detect_chipset:179*180**/181static struct pci_dev * __init gx_detect_chipset(void)182{183struct pci_dev *gx_pci = NULL;184185/* detect which companion chip is used */186for_each_pci_dev(gx_pci) {187if ((pci_match_id(gx_chipset_tbl, gx_pci)) != NULL)188return gx_pci;189}190191pr_debug("error: no supported chipset found!\n");192return NULL;193}194195/**196* gx_get_cpuspeed:197*198* Finds out at which efficient frequency the Cyrix MediaGX/NatSemi199* Geode CPU runs.200*/201static unsigned int gx_get_cpuspeed(unsigned int cpu)202{203if ((gx_params->pci_suscfg & SUSMOD) == 0)204return stock_freq;205206return (stock_freq * gx_params->off_duration)207/ (gx_params->on_duration + gx_params->off_duration);208}209210/**211* gx_validate_speed:212* determine current cpu speed213*214**/215216static unsigned int gx_validate_speed(unsigned int khz, u8 *on_duration,217u8 *off_duration)218{219unsigned int i;220u8 tmp_on, tmp_off;221int old_tmp_freq = stock_freq;222int tmp_freq;223224*off_duration = 1;225*on_duration = 0;226227for (i = max_duration; i > 0; i--) {228tmp_off = ((khz * i) / stock_freq) & 0xff;229tmp_on = i - tmp_off;230tmp_freq = (stock_freq * tmp_off) / i;231/* if this relation is closer to khz, use this. If it's equal,232* prefer it, too - lower latency */233if (abs(tmp_freq - khz) <= abs(old_tmp_freq - khz)) {234*on_duration = tmp_on;235*off_duration = tmp_off;236old_tmp_freq = tmp_freq;237}238}239240return old_tmp_freq;241}242243244/**245* gx_set_cpuspeed:246* set cpu speed in khz.247**/248249static void gx_set_cpuspeed(struct cpufreq_policy *policy, unsigned int khz)250{251u8 suscfg, pmer1;252unsigned int new_khz;253unsigned long flags;254struct cpufreq_freqs freqs;255256freqs.old = gx_get_cpuspeed(0);257258new_khz = gx_validate_speed(khz, &gx_params->on_duration,259&gx_params->off_duration);260261freqs.new = new_khz;262263cpufreq_freq_transition_begin(policy, &freqs);264local_irq_save(flags);265266if (new_khz != stock_freq) {267/* if new khz == 100% of CPU speed, it is special case */268switch (gx_params->cs55x0->device) {269case PCI_DEVICE_ID_CYRIX_5530_LEGACY:270pmer1 = gx_params->pci_pmer1 | IRQ_SPDUP | VID_SPDUP;271/* FIXME: need to test other values -- Zwane,Miura */272/* typical 2 to 4ms */273gx_write_byte(PCI_IRQTC, 4);274/* typical 50 to 100ms */275gx_write_byte(PCI_VIDTC, 100);276gx_write_byte(PCI_PMER1, pmer1);277278if (gx_params->cs55x0->revision < 0x10) {279/* CS5530(rev 1.2, 1.3) */280suscfg = gx_params->pci_suscfg|SUSMOD;281} else {282/* CS5530A,B.. */283suscfg = gx_params->pci_suscfg|SUSMOD|PWRSVE;284}285break;286case PCI_DEVICE_ID_CYRIX_5520:287case PCI_DEVICE_ID_CYRIX_5510:288suscfg = gx_params->pci_suscfg | SUSMOD;289break;290default:291local_irq_restore(flags);292pr_debug("fatal: try to set unknown chipset.\n");293return;294}295} else {296suscfg = gx_params->pci_suscfg & ~(SUSMOD);297gx_params->off_duration = 0;298gx_params->on_duration = 0;299pr_debug("suspend modulation disabled: cpu runs 100%% speed.\n");300}301302gx_write_byte(PCI_MODOFF, gx_params->off_duration);303gx_write_byte(PCI_MODON, gx_params->on_duration);304305gx_write_byte(PCI_SUSCFG, suscfg);306pci_read_config_byte(gx_params->cs55x0, PCI_SUSCFG, &suscfg);307308local_irq_restore(flags);309310gx_params->pci_suscfg = suscfg;311312cpufreq_freq_transition_end(policy, &freqs, 0);313314pr_debug("suspend modulation w/ duration of ON:%d us, OFF:%d us\n",315gx_params->on_duration * 32, gx_params->off_duration * 32);316pr_debug("suspend modulation w/ clock speed: %d kHz.\n", freqs.new);317}318319/****************************************************************320* High level functions *321****************************************************************/322323/*324* cpufreq_gx_verify: test if frequency range is valid325*326* This function checks if a given frequency range in kHz is valid327* for the hardware supported by the driver.328*/329330static int cpufreq_gx_verify(struct cpufreq_policy_data *policy)331{332unsigned int tmp_freq = 0;333u8 tmp1, tmp2;334335if (!stock_freq || !policy)336return -EINVAL;337338policy->cpu = 0;339cpufreq_verify_within_limits(policy, (stock_freq / max_duration),340stock_freq);341342/* it needs to be assured that at least one supported frequency is343* within policy->min and policy->max. If it is not, policy->max344* needs to be increased until one frequency is supported.345* policy->min may not be decreased, though. This way we guarantee a346* specific processing capacity.347*/348tmp_freq = gx_validate_speed(policy->min, &tmp1, &tmp2);349if (tmp_freq < policy->min)350tmp_freq += stock_freq / max_duration;351policy->min = tmp_freq;352if (policy->min > policy->max)353policy->max = tmp_freq;354tmp_freq = gx_validate_speed(policy->max, &tmp1, &tmp2);355if (tmp_freq > policy->max)356tmp_freq -= stock_freq / max_duration;357policy->max = tmp_freq;358if (policy->max < policy->min)359policy->max = policy->min;360cpufreq_verify_within_limits(policy, (stock_freq / max_duration),361stock_freq);362363return 0;364}365366/*367* cpufreq_gx_target:368*369*/370static int cpufreq_gx_target(struct cpufreq_policy *policy,371unsigned int target_freq,372unsigned int relation)373{374u8 tmp1, tmp2;375unsigned int tmp_freq;376377if (!stock_freq || !policy)378return -EINVAL;379380policy->cpu = 0;381382tmp_freq = gx_validate_speed(target_freq, &tmp1, &tmp2);383while (tmp_freq < policy->min) {384tmp_freq += stock_freq / max_duration;385tmp_freq = gx_validate_speed(tmp_freq, &tmp1, &tmp2);386}387while (tmp_freq > policy->max) {388tmp_freq -= stock_freq / max_duration;389tmp_freq = gx_validate_speed(tmp_freq, &tmp1, &tmp2);390}391392gx_set_cpuspeed(policy, tmp_freq);393394return 0;395}396397static int cpufreq_gx_cpu_init(struct cpufreq_policy *policy)398{399unsigned int maxfreq;400401if (!policy || policy->cpu != 0)402return -ENODEV;403404/* determine maximum frequency */405if (pci_busclk)406maxfreq = pci_busclk * gx_freq_mult[getCx86(CX86_DIR1) & 0x0f];407else if (cpu_khz)408maxfreq = cpu_khz;409else410maxfreq = 30000 * gx_freq_mult[getCx86(CX86_DIR1) & 0x0f];411412stock_freq = maxfreq;413414pr_debug("cpu max frequency is %d.\n", maxfreq);415416/* setup basic struct for cpufreq API */417policy->cpu = 0;418419if (max_duration < POLICY_MIN_DIV)420policy->min = maxfreq / max_duration;421else422policy->min = maxfreq / POLICY_MIN_DIV;423policy->max = maxfreq;424policy->cpuinfo.min_freq = maxfreq / max_duration;425policy->cpuinfo.max_freq = maxfreq;426427return 0;428}429430/*431* cpufreq_gx_init:432* MediaGX/Geode GX initialize cpufreq driver433*/434static struct cpufreq_driver gx_suspmod_driver = {435.flags = CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING,436.get = gx_get_cpuspeed,437.verify = cpufreq_gx_verify,438.target = cpufreq_gx_target,439.init = cpufreq_gx_cpu_init,440.name = "gx-suspmod",441};442443static int __init cpufreq_gx_init(void)444{445int ret;446struct gxfreq_params *params;447struct pci_dev *gx_pci;448449/* Test if we have the right hardware */450gx_pci = gx_detect_chipset();451if (gx_pci == NULL)452return -ENODEV;453454/* check whether module parameters are sane */455if (max_duration > 0xff)456max_duration = 0xff;457458pr_debug("geode suspend modulation available.\n");459460params = kzalloc(sizeof(*params), GFP_KERNEL);461if (params == NULL)462return -ENOMEM;463464params->cs55x0 = gx_pci;465gx_params = params;466467/* keep cs55x0 configurations */468pci_read_config_byte(params->cs55x0, PCI_SUSCFG, &(params->pci_suscfg));469pci_read_config_byte(params->cs55x0, PCI_PMER1, &(params->pci_pmer1));470pci_read_config_byte(params->cs55x0, PCI_PMER2, &(params->pci_pmer2));471pci_read_config_byte(params->cs55x0, PCI_MODON, &(params->on_duration));472pci_read_config_byte(params->cs55x0, PCI_MODOFF,473&(params->off_duration));474475ret = cpufreq_register_driver(&gx_suspmod_driver);476if (ret) {477kfree(params);478return ret; /* register error! */479}480481return 0;482}483484static void __exit cpufreq_gx_exit(void)485{486cpufreq_unregister_driver(&gx_suspmod_driver);487pci_dev_put(gx_params->cs55x0);488kfree(gx_params);489}490491MODULE_AUTHOR("Hiroshi Miura <[email protected]>");492MODULE_DESCRIPTION("Cpufreq driver for Cyrix MediaGX and NatSemi Geode");493MODULE_LICENSE("GPL");494495module_init(cpufreq_gx_init);496module_exit(cpufreq_gx_exit);497498499500