ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / kernel / cpu / cpufreq / gx-suspmod.c
1 /*
2  *      Cyrix MediaGX and NatSemi Geode Suspend Modulation
3  *      (C) 2002 Zwane Mwaikambo <zwane@commfireservices.com>
4  *      (C) 2002 Hiroshi Miura   <miura@da-cha.org>
5  *      All Rights Reserved
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      version 2 as published by the Free Software Foundation 
10  *
11  *      The author(s) of this software shall not be held liable for damages
12  *      of any nature resulting due to the use of this software. This
13  *      software is provided AS-IS with no warranties.
14  *      
15  * Theoritical note:
16  *
17  *      (see Geode(tm) CS5530 manual (rev.4.1) page.56)
18  *
19  *      CPU frequency control on NatSemi Geode GX1/GXLV processor and CS55x0
20  *      are based on Suspend Moduration.
21  *
22  *      Suspend Modulation works by asserting and de-asserting the SUSP# pin
23  *      to CPU(GX1/GXLV) for configurable durations. When asserting SUSP#
24  *      the CPU enters an idle state. GX1 stops its core clock when SUSP# is 
25  *      asserted then power consumption is reduced.
26  *
27  *      Suspend Modulation's OFF/ON duration are configurable 
28  *      with 'Suspend Modulation OFF Count Register'
29  *      and 'Suspend Modulation ON Count Register'.
30  *      These registers are 8bit counters that represent the number of 
31  *      32us intervals which the SUSP# pin is asserted(ON)/de-asserted(OFF)
32  *      to the processor.
33  *
34  *      These counters define a ratio which is the effective frequency 
35  *      of operation of the system.
36  *
37  *                             OFF Count
38  *      F_eff = Fgx * ----------------------
39  *                      OFF Count + ON Count
40  *
41  *      0 <= On Count, Off Count <= 255
42  *
43  *      From these limits, we can get register values 
44  *
45  *      off_duration + on_duration <= MAX_DURATION
46  *      on_duration = off_duration * (stock_freq - freq) / freq
47  *
48  *      off_duration  =  (freq * DURATION) / stock_freq 
49  *      on_duration = DURATION - off_duration 
50  *
51  *
52  *---------------------------------------------------------------------------
53  *
54  * ChangeLog:
55  *      Dec. 12, 2003   Hiroshi Miura <miura@da-cha.org>
56  *              - fix on/off register mistake
57  *              - fix cpu_khz calc when it stops cpu modulation.
58  *
59  *      Dec. 11, 2002   Hiroshi Miura <miura@da-cha.org>
60  *              - rewrite for Cyrix MediaGX Cx5510/5520 and 
61  *                NatSemi Geode Cs5530(A).
62  *
63  *      Jul. ??, 2002  Zwane Mwaikambo <zwane@commfireservices.com>
64  *              - cs5530_mod patch for 2.4.19-rc1.
65  *
66  *---------------------------------------------------------------------------
67  *
68  * Todo
69  *      Test on machines with 5510, 5530, 5530A
70  */
71
72 /************************************************************************
73  *                      Suspend Modulation - Definitions                *
74  ************************************************************************/
75
76 #include <linux/kernel.h>
77 #include <linux/module.h> 
78 #include <linux/sched.h>
79 #include <linux/init.h>
80 #include <linux/smp.h>
81 #include <linux/cpufreq.h>
82 #include <linux/pci.h>
83 #include <asm/processor.h> 
84 #include <asm/errno.h>
85
86 /* 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 */
95
96 /* 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 */
102
103 /* SUSCFG bits */
104 #define SUSMOD                 (1<<0)  /* enable/disable suspend modulation */
105 /* the belows support 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 belows support only with cs5530A */ 
110 #define PWRSVE_ISA             (1<<3)  /* stop ISA clock  */
111 #define PWRSVE                 (1<<4)  /* active idle */
112
113 struct gxfreq_params {
114         u8 on_duration;
115         u8 off_duration;
116         u8 pci_suscfg;
117         u8 pci_pmer1;
118         u8 pci_pmer2;
119         u8 pci_rev;
120         struct pci_dev *cs55x0;
121 };
122
123 static struct gxfreq_params *gx_params;
124 static int stock_freq;
125
126 /* PCI bus clock - defaults to 30.000 if cpu_khz is not available */
127 static int pci_busclk = 0;
128 MODULE_PARM(pci_busclk, "i");
129
130 /* maximum duration for which the cpu may be suspended
131  * (32us * MAX_DURATION). If no parameter is given, this defaults
132  * to 255. 
133  * Note that this leads to a maximum of 8 ms(!) where the CPU clock
134  * is suspended -- processing power is just 0.39% of what it used to be,
135  * though. 781.25 kHz(!) for a 200 MHz processor -- wow. */
136 static int max_duration = 255;
137 MODULE_PARM(max_duration, "i");
138
139 /* For the default policy, we want at least some processing power
140  * - let's say 5%. (min = maxfreq / POLICY_MIN_DIV)
141  */
142 #define POLICY_MIN_DIV 20
143
144
145 /* DEBUG
146  *   Define it if you want verbose debug output
147  */
148
149 #define SUSPMOD_DEBUG 1
150
151 #ifdef SUSPMOD_DEBUG
152 #define dprintk(msg...) printk(KERN_DEBUG "cpufreq:" msg)
153 #else
154 #define dprintk(msg...) do { } while(0)
155 #endif
156
157 /**
158  *      we can detect a core multipiler from dir0_lsb 
159  *      from GX1 datasheet p.56, 
160  *         MULT[3:0]:
161  *         0000 = SYSCLK multiplied by 4 (test only)
162  *         0001 = SYSCLK multiplied by 10
163  *         0010 = SYSCLK multiplied by 4
164  *         0011 = SYSCLK multiplied by 6
165  *         0100 = SYSCLK multiplied by 9
166  *         0101 = SYSCLK multiplied by 5
167  *         0110 = SYSCLK multiplied by 7
168  *         0111 = SYSCLK multiplied by 8
169  *              of 33.3MHz
170  **/
171 static int gx_freq_mult[16] = {
172                 4, 10, 4, 6, 9, 5, 7, 8,
173                 0, 0, 0, 0, 0, 0, 0, 0
174 };
175
176
177 /****************************************************************
178  *      Low Level chipset interface                             *
179  ****************************************************************/
180 static struct pci_device_id gx_chipset_tbl[] __initdata = {
181         { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5530_LEGACY, PCI_ANY_ID, PCI_ANY_ID },
182         { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5520, PCI_ANY_ID, PCI_ANY_ID },
183         { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5510, PCI_ANY_ID, PCI_ANY_ID },
184         { 0, },
185 };
186
187 /**
188  *     gx_detect_chipset:
189  *
190  **/
191 static __init struct pci_dev *gx_detect_chipset(void)
192 {
193         struct pci_dev *gx_pci = NULL;
194
195         /* check if CPU is a MediaGX or a Geode. */
196         if ((current_cpu_data.x86_vendor != X86_VENDOR_NSC) && 
197             (current_cpu_data.x86_vendor != X86_VENDOR_CYRIX)) {
198                 printk(KERN_INFO "gx-suspmod: error: no MediaGX/Geode processor found!\n");
199                 return NULL;            
200         }
201
202         /* detect which companion chip is used */
203         while ((gx_pci = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, gx_pci)) != NULL) {
204                 if ((pci_match_device (gx_chipset_tbl, gx_pci)) != NULL) {
205                         return gx_pci;
206                 }
207         }
208
209         dprintk(KERN_INFO "gx-suspmod: error: no supported chipset found!\n");
210         return NULL;
211 }
212
213 /**
214  *      gx_get_cpuspeed:
215  *
216  * Finds out at which efficient frequency the Cyrix MediaGX/NatSemi Geode CPU runs.
217  */
218 static int gx_get_cpuspeed(void)
219 {
220         if ((gx_params->pci_suscfg & SUSMOD) == 0) 
221                 return stock_freq;
222
223         return (stock_freq * gx_params->on_duration) 
224                 / (gx_params->on_duration + gx_params->off_duration);
225 }
226
227 /**
228  *      gx_validate_speed:
229  *      determine current cpu speed
230  *       
231 **/
232
233 static unsigned int gx_validate_speed(unsigned int khz, u8 *on_duration, u8 *off_duration)
234 {
235         unsigned int i;
236         u8 tmp_on, tmp_off;
237         int old_tmp_freq = stock_freq;
238         int tmp_freq;
239
240         *off_duration=1;
241         *on_duration=0;
242
243         for (i=max_duration; i>0; i--) {
244                 tmp_off = ((khz * i) / stock_freq) & 0xff; 
245                 tmp_on = i - tmp_off;
246                 tmp_freq = (stock_freq * tmp_off) / i;
247                 /* if this relation is closer to khz, use this. If it's equal,
248                  * prefer it, too - lower latency */
249                 if (abs(tmp_freq - khz) <= abs(old_tmp_freq - khz)) {
250                         *on_duration = tmp_on;
251                         *off_duration = tmp_off;
252                         old_tmp_freq = tmp_freq;
253                 }
254         }
255
256         return old_tmp_freq;
257 }
258
259
260 /**
261  *      gx_set_cpuspeed:
262  *              set cpu speed in khz.
263  **/
264
265 static void gx_set_cpuspeed(unsigned int khz)
266 {
267         u8 suscfg, pmer1;
268         unsigned int new_khz;
269         unsigned long flags;
270         struct cpufreq_freqs freqs;
271
272
273         freqs.cpu = 0;
274         freqs.old = gx_get_cpuspeed();
275
276         new_khz = gx_validate_speed(khz, &gx_params->on_duration, &gx_params->off_duration);
277
278         freqs.new = new_khz;
279
280         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
281         local_irq_save(flags);
282
283         if (new_khz != stock_freq) {  /* if new khz == 100% of CPU speed, it is special case */
284                 switch (gx_params->cs55x0->device) {
285                 case PCI_DEVICE_ID_CYRIX_5530_LEGACY:
286                         pmer1 = gx_params->pci_pmer1 | IRQ_SPDUP | VID_SPDUP;
287                         /* FIXME: need to test other values -- Zwane,Miura */
288                         pci_write_config_byte(gx_params->cs55x0, PCI_IRQTC, 4); /* typical 2 to 4ms */
289                         pci_write_config_byte(gx_params->cs55x0, PCI_VIDTC, 100);/* typical 50 to 100ms */
290                         pci_write_config_byte(gx_params->cs55x0, PCI_PMER1, pmer1);
291
292                         if (gx_params->pci_rev < 0x10) {   /* CS5530(rev 1.2, 1.3) */
293                                 suscfg = gx_params->pci_suscfg | SUSMOD;
294                         } else {                           /* CS5530A,B.. */
295                                 suscfg = gx_params->pci_suscfg | SUSMOD | PWRSVE;
296                         }
297                         break;
298                 case PCI_DEVICE_ID_CYRIX_5520:
299                 case PCI_DEVICE_ID_CYRIX_5510:
300                         suscfg = gx_params->pci_suscfg | SUSMOD;
301                 default:
302                         local_irq_restore(flags);
303                         dprintk("fatal: try to set unknown chipset.\n");
304                         return;
305                 }
306         } else {
307                 suscfg = gx_params->pci_suscfg & ~(SUSMOD);
308                 gx_params->off_duration = 0;
309                 gx_params->on_duration = 0;
310                 dprintk("suspend modulation disabled: cpu runs 100 percent speed.\n");
311         }
312
313         pci_write_config_byte(gx_params->cs55x0, PCI_MODOFF, gx_params->off_duration);
314         pci_write_config_byte(gx_params->cs55x0, PCI_MODON, gx_params->on_duration);
315
316         pci_write_config_byte(gx_params->cs55x0, PCI_SUSCFG, suscfg);
317         pci_read_config_byte(gx_params->cs55x0, PCI_SUSCFG, &suscfg);
318
319         local_irq_restore(flags);
320
321         gx_params->pci_suscfg = suscfg;
322
323         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
324
325         dprintk("suspend modulation w/ duration of ON:%d us, OFF:%d us\n",
326                 gx_params->on_duration * 32, gx_params->off_duration * 32);
327         dprintk("suspend modulation w/ clock speed: %d kHz.\n", freqs.new); 
328 }
329
330 /****************************************************************
331  *             High level functions                             *
332  ****************************************************************/
333
334 /*
335  *      cpufreq_gx_verify: test if frequency range is valid 
336  *
337  *      This function checks if a given frequency range in kHz is valid 
338  *      for the hardware supported by the driver. 
339  */
340
341 static int cpufreq_gx_verify(struct cpufreq_policy *policy)
342 {
343         unsigned int tmp_freq = 0;
344         u8 tmp1, tmp2;
345
346         if (!stock_freq || !policy)
347                 return -EINVAL;
348
349         policy->cpu = 0;
350         cpufreq_verify_within_limits(policy, (stock_freq / max_duration), stock_freq);
351
352         /* it needs to be assured that at least one supported frequency is
353          * within policy->min and policy->max. If it is not, policy->max
354          * needs to be increased until one freuqency is supported.
355          * policy->min may not be decreased, though. This way we guarantee a 
356          * specific processing capacity.
357          */
358         tmp_freq = gx_validate_speed(policy->min, &tmp1, &tmp2);
359         if (tmp_freq < policy->min) 
360                 tmp_freq += stock_freq / max_duration;
361         policy->min = tmp_freq;
362         if (policy->min > policy->max) 
363                 policy->max = tmp_freq;
364         tmp_freq = gx_validate_speed(policy->max, &tmp1, &tmp2);
365         if (tmp_freq > policy->max)
366                 tmp_freq -= stock_freq / max_duration;
367         policy->max = tmp_freq;
368         if (policy->max < policy->min)
369                 policy->max = policy->min;
370         cpufreq_verify_within_limits(policy, (stock_freq / max_duration), stock_freq);
371         
372         return 0;
373 }
374
375 /*
376  *      cpufreq_gx_target:  
377  *
378  */
379 static int cpufreq_gx_target(struct cpufreq_policy *policy,
380                              unsigned int target_freq,
381                              unsigned int relation)
382 {
383         u8 tmp1, tmp2;
384         unsigned int tmp_freq;
385
386         if (!stock_freq || !policy)
387                 return -EINVAL;
388
389         policy->cpu = 0;
390
391         tmp_freq = gx_validate_speed(target_freq, &tmp1, &tmp2);
392         while (tmp_freq < policy->min) {
393                 tmp_freq += stock_freq / max_duration;
394                 tmp_freq = gx_validate_speed(tmp_freq, &tmp1, &tmp2);
395         }
396         while (tmp_freq > policy->max) {
397                 tmp_freq -= stock_freq / max_duration;
398                 tmp_freq = gx_validate_speed(tmp_freq, &tmp1, &tmp2);
399         }
400
401         gx_set_cpuspeed(tmp_freq);
402
403         return 0;
404 }
405
406 static int cpufreq_gx_cpu_init(struct cpufreq_policy *policy)
407 {
408         int maxfreq, curfreq;
409
410         if (!policy || policy->cpu != 0)
411                 return -ENODEV;
412
413         /* determine maximum frequency */
414         if (pci_busclk) {
415                 maxfreq = pci_busclk * gx_freq_mult[getCx86(CX86_DIR1) & 0x0f];
416         } else if (cpu_khz) {
417                 maxfreq = cpu_khz;
418         } else {
419                 maxfreq = 30000 * gx_freq_mult[getCx86(CX86_DIR1) & 0x0f];
420         }
421         stock_freq = maxfreq;
422         curfreq = gx_get_cpuspeed();
423
424         dprintk("cpu max frequency is %d.\n", maxfreq);
425         dprintk("cpu current frequency is %dkHz.\n",curfreq);
426
427         /* setup basic struct for cpufreq API */
428         policy->cpu = 0;
429
430         if (max_duration < POLICY_MIN_DIV)
431                 policy->min = maxfreq / max_duration;
432         else
433                 policy->min = maxfreq / POLICY_MIN_DIV;
434         policy->max = maxfreq;
435         policy->cur = curfreq;
436         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
437         policy->cpuinfo.min_freq = maxfreq / max_duration;
438         policy->cpuinfo.max_freq = maxfreq;
439         policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
440
441         return 0;
442 }
443
444 /* 
445  * cpufreq_gx_init:
446  *   MediaGX/Geode GX initialize cpufreq driver
447  */
448 static struct cpufreq_driver gx_suspmod_driver = {
449         .verify         = cpufreq_gx_verify,
450         .target         = cpufreq_gx_target,
451         .init           = cpufreq_gx_cpu_init,
452         .name           = "gx-suspmod",
453         .owner          = THIS_MODULE,
454 };
455
456 static int __init cpufreq_gx_init(void)
457 {
458         int ret;
459         struct gxfreq_params *params;
460         struct pci_dev *gx_pci;
461         u32 class_rev;
462
463         /* Test if we have the right hardware */
464         if ((gx_pci = gx_detect_chipset()) == NULL) 
465                 return -ENODEV;
466
467         /* check whether module parameters are sane */
468         if (max_duration > 0xff)
469                 max_duration = 0xff;
470
471         dprintk("geode suspend modulation available.\n");
472
473         params = kmalloc(sizeof(struct gxfreq_params), GFP_KERNEL);
474         if (params == NULL)
475                 return -ENOMEM;
476         memset(params, 0, sizeof(struct gxfreq_params));
477
478         params->cs55x0 = gx_pci;
479         gx_params = params;
480
481         /* keep cs55x0 configurations */
482         pci_read_config_byte(params->cs55x0, PCI_SUSCFG, &(params->pci_suscfg));
483         pci_read_config_byte(params->cs55x0, PCI_PMER1, &(params->pci_pmer1));
484         pci_read_config_byte(params->cs55x0, PCI_PMER2, &(params->pci_pmer2));
485         pci_read_config_byte(params->cs55x0, PCI_MODON, &(params->on_duration));
486         pci_read_config_byte(params->cs55x0, PCI_MODOFF, &(params->off_duration));
487         pci_read_config_dword(params->cs55x0, PCI_CLASS_REVISION, &class_rev);
488         params->pci_rev = class_rev && 0xff;
489
490         if ((ret = cpufreq_register_driver(&gx_suspmod_driver))) { 
491                 kfree(params);
492                 return ret;                   /* register error! */
493         }
494
495         return 0;
496 }
497
498 static void __exit cpufreq_gx_exit(void)
499 {
500         cpufreq_unregister_driver(&gx_suspmod_driver);
501         kfree(gx_params);
502 }
503
504 MODULE_AUTHOR ("Hiroshi Miura <miura@da-cha.org>");
505 MODULE_DESCRIPTION ("Cpufreq driver for Cyrix MediaGX and NatSemi Geode");
506 MODULE_LICENSE ("GPL");
507
508 module_init(cpufreq_gx_init);
509 module_exit(cpufreq_gx_exit);
510