/*1* Cyrix MediaGX and NatSemi Geode Suspend Modulation2* (C) 2002 Zwane Mwaikambo <[email protected]>3* (C) 2002 Hiroshi Miura <[email protected]>4* All Rights Reserved5*6* This program is free software; you can redistribute it and/or7* modify it under the terms of the GNU General Public License8* version 2 as published by the Free Software Foundation9*10* The author(s) of this software shall not be held liable for damages11* of any nature resulting due to the use of this software. This12* software is provided AS-IS with no warranties.13*14* Theoretical note:15*16* (see Geode(tm) CS5530 manual (rev.4.1) page.56)17*18* CPU frequency control on NatSemi Geode GX1/GXLV processor and CS55x019* are based on Suspend Modulation.20*21* Suspend Modulation works by asserting and de-asserting the SUSP# pin22* to CPU(GX1/GXLV) for configurable durations. When asserting SUSP#23* the CPU enters an idle state. GX1 stops its core clock when SUSP# is24* asserted then power consumption is reduced.25*26* Suspend Modulation's OFF/ON duration are configurable27* with 'Suspend Modulation OFF Count Register'28* and 'Suspend Modulation ON Count Register'.29* These registers are 8bit counters that represent the number of30* 32us intervals which the SUSP# pin is asserted(ON)/de-asserted(OFF)31* to the processor.32*33* These counters define a ratio which is the effective frequency34* of operation of the system.35*36* OFF Count37* F_eff = Fgx * ----------------------38* OFF Count + ON Count39*40* 0 <= On Count, Off Count <= 25541*42* From these limits, we can get register values43*44* off_duration + on_duration <= MAX_DURATION45* on_duration = off_duration * (stock_freq - freq) / freq46*47* off_duration = (freq * DURATION) / stock_freq48* on_duration = DURATION - off_duration49*50*51*---------------------------------------------------------------------------52*53* ChangeLog:54* Dec. 12, 2003 Hiroshi Miura <[email protected]>55* - fix on/off register mistake56* - fix cpu_khz calc when it stops cpu modulation.57*58* Dec. 11, 2002 Hiroshi Miura <[email protected]>59* - rewrite for Cyrix MediaGX Cx5510/5520 and60* NatSemi Geode Cs5530(A).61*62* Jul. ??, 2002 Zwane Mwaikambo <[email protected]>63* - cs5530_mod patch for 2.4.19-rc1.64*65*---------------------------------------------------------------------------66*67* Todo68* Test on machines with 5510, 5530, 5530A69*/7071/************************************************************************72* Suspend Modulation - Definitions *73************************************************************************/7475#include <linux/kernel.h>76#include <linux/module.h>77#include <linux/init.h>78#include <linux/smp.h>79#include <linux/cpufreq.h>80#include <linux/pci.h>81#include <linux/errno.h>82#include <linux/slab.h>8384#include <asm/processor-cyrix.h>8586/* PCI config registers, all at F0 */87#define PCI_PMER1 0x80 /* power management enable register 1 */88#define PCI_PMER2 0x81 /* power management enable register 2 */89#define PCI_PMER3 0x82 /* power management enable register 3 */90#define PCI_IRQTC 0x8c /* irq speedup timer counter register:typical 2 to 4ms */91#define PCI_VIDTC 0x8d /* video speedup timer counter register: typical 50 to 100ms */92#define PCI_MODOFF 0x94 /* suspend modulation OFF counter register, 1 = 32us */93#define PCI_MODON 0x95 /* suspend modulation ON counter register */94#define PCI_SUSCFG 0x96 /* suspend configuration register */9596/* PMER1 bits */97#define GPM (1<<0) /* global power management */98#define GIT (1<<1) /* globally enable PM device idle timers */99#define GTR (1<<2) /* globally enable IO traps */100#define IRQ_SPDUP (1<<3) /* disable clock throttle during interrupt handling */101#define VID_SPDUP (1<<4) /* disable clock throttle during vga video handling */102103/* SUSCFG bits */104#define SUSMOD (1<<0) /* enable/disable suspend modulation */105/* the below is supported only with cs5530 (after rev.1.2)/cs5530A */106#define SMISPDUP (1<<1) /* select how SMI re-enable suspend modulation: */107/* IRQTC timer or read SMI speedup disable reg.(F1BAR[08-09h]) */108#define SUSCFG (1<<2) /* enable powering down a GXLV processor. "Special 3Volt Suspend" mode */109/* the below is supported only with cs5530A */110#define PWRSVE_ISA (1<<3) /* stop ISA clock */111#define PWRSVE (1<<4) /* active idle */112113struct gxfreq_params {114u8 on_duration;115u8 off_duration;116u8 pci_suscfg;117u8 pci_pmer1;118u8 pci_pmer2;119struct pci_dev *cs55x0;120};121122static struct gxfreq_params *gx_params;123static int stock_freq;124125/* PCI bus clock - defaults to 30.000 if cpu_khz is not available */126static int pci_busclk;127module_param(pci_busclk, int, 0444);128129/* maximum duration for which the cpu may be suspended130* (32us * MAX_DURATION). If no parameter is given, this defaults131* to 255.132* Note that this leads to a maximum of 8 ms(!) where the CPU clock133* is suspended -- processing power is just 0.39% of what it used to be,134* though. 781.25 kHz(!) for a 200 MHz processor -- wow. */135static int max_duration = 255;136module_param(max_duration, int, 0444);137138/* For the default policy, we want at least some processing power139* - let's say 5%. (min = maxfreq / POLICY_MIN_DIV)140*/141#define POLICY_MIN_DIV 20142143144/**145* we can detect a core multipiler from dir0_lsb146* from GX1 datasheet p.56,147* MULT[3:0]:148* 0000 = SYSCLK multiplied by 4 (test only)149* 0001 = SYSCLK multiplied by 10150* 0010 = SYSCLK multiplied by 4151* 0011 = SYSCLK multiplied by 6152* 0100 = SYSCLK multiplied by 9153* 0101 = SYSCLK multiplied by 5154* 0110 = SYSCLK multiplied by 7155* 0111 = SYSCLK multiplied by 8156* of 33.3MHz157**/158static int gx_freq_mult[16] = {1594, 10, 4, 6, 9, 5, 7, 8,1600, 0, 0, 0, 0, 0, 0, 0161};162163164/****************************************************************165* Low Level chipset interface *166****************************************************************/167static struct pci_device_id gx_chipset_tbl[] __initdata = {168{ PCI_VDEVICE(CYRIX, PCI_DEVICE_ID_CYRIX_5530_LEGACY), },169{ PCI_VDEVICE(CYRIX, PCI_DEVICE_ID_CYRIX_5520), },170{ PCI_VDEVICE(CYRIX, PCI_DEVICE_ID_CYRIX_5510), },171{ 0, },172};173174static void gx_write_byte(int reg, int value)175{176pci_write_config_byte(gx_params->cs55x0, reg, value);177}178179/**180* gx_detect_chipset:181*182**/183static __init struct pci_dev *gx_detect_chipset(void)184{185struct pci_dev *gx_pci = NULL;186187/* check if CPU is a MediaGX or a Geode. */188if ((boot_cpu_data.x86_vendor != X86_VENDOR_NSC) &&189(boot_cpu_data.x86_vendor != X86_VENDOR_CYRIX)) {190pr_debug("error: no MediaGX/Geode processor found!\n");191return NULL;192}193194/* detect which companion chip is used */195for_each_pci_dev(gx_pci) {196if ((pci_match_id(gx_chipset_tbl, gx_pci)) != NULL)197return gx_pci;198}199200pr_debug("error: no supported chipset found!\n");201return NULL;202}203204/**205* gx_get_cpuspeed:206*207* Finds out at which efficient frequency the Cyrix MediaGX/NatSemi208* Geode CPU runs.209*/210static unsigned int gx_get_cpuspeed(unsigned int cpu)211{212if ((gx_params->pci_suscfg & SUSMOD) == 0)213return stock_freq;214215return (stock_freq * gx_params->off_duration)216/ (gx_params->on_duration + gx_params->off_duration);217}218219/**220* gx_validate_speed:221* determine current cpu speed222*223**/224225static unsigned int gx_validate_speed(unsigned int khz, u8 *on_duration,226u8 *off_duration)227{228unsigned int i;229u8 tmp_on, tmp_off;230int old_tmp_freq = stock_freq;231int tmp_freq;232233*off_duration = 1;234*on_duration = 0;235236for (i = max_duration; i > 0; i--) {237tmp_off = ((khz * i) / stock_freq) & 0xff;238tmp_on = i - tmp_off;239tmp_freq = (stock_freq * tmp_off) / i;240/* if this relation is closer to khz, use this. If it's equal,241* prefer it, too - lower latency */242if (abs(tmp_freq - khz) <= abs(old_tmp_freq - khz)) {243*on_duration = tmp_on;244*off_duration = tmp_off;245old_tmp_freq = tmp_freq;246}247}248249return old_tmp_freq;250}251252253/**254* gx_set_cpuspeed:255* set cpu speed in khz.256**/257258static void gx_set_cpuspeed(unsigned int khz)259{260u8 suscfg, pmer1;261unsigned int new_khz;262unsigned long flags;263struct cpufreq_freqs freqs;264265freqs.cpu = 0;266freqs.old = gx_get_cpuspeed(0);267268new_khz = gx_validate_speed(khz, &gx_params->on_duration,269&gx_params->off_duration);270271freqs.new = new_khz;272273cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);274local_irq_save(flags);275276277278if (new_khz != stock_freq) {279/* if new khz == 100% of CPU speed, it is special case */280switch (gx_params->cs55x0->device) {281case PCI_DEVICE_ID_CYRIX_5530_LEGACY:282pmer1 = gx_params->pci_pmer1 | IRQ_SPDUP | VID_SPDUP;283/* FIXME: need to test other values -- Zwane,Miura */284/* typical 2 to 4ms */285gx_write_byte(PCI_IRQTC, 4);286/* typical 50 to 100ms */287gx_write_byte(PCI_VIDTC, 100);288gx_write_byte(PCI_PMER1, pmer1);289290if (gx_params->cs55x0->revision < 0x10) {291/* CS5530(rev 1.2, 1.3) */292suscfg = gx_params->pci_suscfg|SUSMOD;293} else {294/* CS5530A,B.. */295suscfg = gx_params->pci_suscfg|SUSMOD|PWRSVE;296}297break;298case PCI_DEVICE_ID_CYRIX_5520:299case PCI_DEVICE_ID_CYRIX_5510:300suscfg = gx_params->pci_suscfg | SUSMOD;301break;302default:303local_irq_restore(flags);304pr_debug("fatal: try to set unknown chipset.\n");305return;306}307} else {308suscfg = gx_params->pci_suscfg & ~(SUSMOD);309gx_params->off_duration = 0;310gx_params->on_duration = 0;311pr_debug("suspend modulation disabled: cpu runs 100%% speed.\n");312}313314gx_write_byte(PCI_MODOFF, gx_params->off_duration);315gx_write_byte(PCI_MODON, gx_params->on_duration);316317gx_write_byte(PCI_SUSCFG, suscfg);318pci_read_config_byte(gx_params->cs55x0, PCI_SUSCFG, &suscfg);319320local_irq_restore(flags);321322gx_params->pci_suscfg = suscfg;323324cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);325326pr_debug("suspend modulation w/ duration of ON:%d us, OFF:%d us\n",327gx_params->on_duration * 32, gx_params->off_duration * 32);328pr_debug("suspend modulation w/ clock speed: %d kHz.\n", freqs.new);329}330331/****************************************************************332* High level functions *333****************************************************************/334335/*336* cpufreq_gx_verify: test if frequency range is valid337*338* This function checks if a given frequency range in kHz is valid339* for the hardware supported by the driver.340*/341342static int cpufreq_gx_verify(struct cpufreq_policy *policy)343{344unsigned int tmp_freq = 0;345u8 tmp1, tmp2;346347if (!stock_freq || !policy)348return -EINVAL;349350policy->cpu = 0;351cpufreq_verify_within_limits(policy, (stock_freq / max_duration),352stock_freq);353354/* it needs to be assured that at least one supported frequency is355* within policy->min and policy->max. If it is not, policy->max356* needs to be increased until one freuqency is supported.357* policy->min may not be decreased, though. This way we guarantee a358* specific processing capacity.359*/360tmp_freq = gx_validate_speed(policy->min, &tmp1, &tmp2);361if (tmp_freq < policy->min)362tmp_freq += stock_freq / max_duration;363policy->min = tmp_freq;364if (policy->min > policy->max)365policy->max = tmp_freq;366tmp_freq = gx_validate_speed(policy->max, &tmp1, &tmp2);367if (tmp_freq > policy->max)368tmp_freq -= stock_freq / max_duration;369policy->max = tmp_freq;370if (policy->max < policy->min)371policy->max = policy->min;372cpufreq_verify_within_limits(policy, (stock_freq / max_duration),373stock_freq);374375return 0;376}377378/*379* cpufreq_gx_target:380*381*/382static int cpufreq_gx_target(struct cpufreq_policy *policy,383unsigned int target_freq,384unsigned int relation)385{386u8 tmp1, tmp2;387unsigned int tmp_freq;388389if (!stock_freq || !policy)390return -EINVAL;391392policy->cpu = 0;393394tmp_freq = gx_validate_speed(target_freq, &tmp1, &tmp2);395while (tmp_freq < policy->min) {396tmp_freq += stock_freq / max_duration;397tmp_freq = gx_validate_speed(tmp_freq, &tmp1, &tmp2);398}399while (tmp_freq > policy->max) {400tmp_freq -= stock_freq / max_duration;401tmp_freq = gx_validate_speed(tmp_freq, &tmp1, &tmp2);402}403404gx_set_cpuspeed(tmp_freq);405406return 0;407}408409static int cpufreq_gx_cpu_init(struct cpufreq_policy *policy)410{411unsigned int maxfreq, curfreq;412413if (!policy || policy->cpu != 0)414return -ENODEV;415416/* determine maximum frequency */417if (pci_busclk)418maxfreq = pci_busclk * gx_freq_mult[getCx86(CX86_DIR1) & 0x0f];419else if (cpu_khz)420maxfreq = cpu_khz;421else422maxfreq = 30000 * gx_freq_mult[getCx86(CX86_DIR1) & 0x0f];423424stock_freq = maxfreq;425curfreq = gx_get_cpuspeed(0);426427pr_debug("cpu max frequency is %d.\n", maxfreq);428pr_debug("cpu current frequency is %dkHz.\n", curfreq);429430/* setup basic struct for cpufreq API */431policy->cpu = 0;432433if (max_duration < POLICY_MIN_DIV)434policy->min = maxfreq / max_duration;435else436policy->min = maxfreq / POLICY_MIN_DIV;437policy->max = maxfreq;438policy->cur = curfreq;439policy->cpuinfo.min_freq = maxfreq / max_duration;440policy->cpuinfo.max_freq = maxfreq;441policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;442443return 0;444}445446/*447* cpufreq_gx_init:448* MediaGX/Geode GX initialize cpufreq driver449*/450static struct cpufreq_driver gx_suspmod_driver = {451.get = gx_get_cpuspeed,452.verify = cpufreq_gx_verify,453.target = cpufreq_gx_target,454.init = cpufreq_gx_cpu_init,455.name = "gx-suspmod",456.owner = THIS_MODULE,457};458459static int __init cpufreq_gx_init(void)460{461int ret;462struct gxfreq_params *params;463struct pci_dev *gx_pci;464465/* Test if we have the right hardware */466gx_pci = gx_detect_chipset();467if (gx_pci == NULL)468return -ENODEV;469470/* check whether module parameters are sane */471if (max_duration > 0xff)472max_duration = 0xff;473474pr_debug("geode suspend modulation available.\n");475476params = kzalloc(sizeof(struct gxfreq_params), GFP_KERNEL);477if (params == NULL)478return -ENOMEM;479480params->cs55x0 = gx_pci;481gx_params = params;482483/* keep cs55x0 configurations */484pci_read_config_byte(params->cs55x0, PCI_SUSCFG, &(params->pci_suscfg));485pci_read_config_byte(params->cs55x0, PCI_PMER1, &(params->pci_pmer1));486pci_read_config_byte(params->cs55x0, PCI_PMER2, &(params->pci_pmer2));487pci_read_config_byte(params->cs55x0, PCI_MODON, &(params->on_duration));488pci_read_config_byte(params->cs55x0, PCI_MODOFF,489&(params->off_duration));490491ret = cpufreq_register_driver(&gx_suspmod_driver);492if (ret) {493kfree(params);494return ret; /* register error! */495}496497return 0;498}499500static void __exit cpufreq_gx_exit(void)501{502cpufreq_unregister_driver(&gx_suspmod_driver);503pci_dev_put(gx_params->cs55x0);504kfree(gx_params);505}506507MODULE_AUTHOR("Hiroshi Miura <[email protected]>");508MODULE_DESCRIPTION("Cpufreq driver for Cyrix MediaGX and NatSemi Geode");509MODULE_LICENSE("GPL");510511module_init(cpufreq_gx_init);512module_exit(cpufreq_gx_exit);513514515516