gx-suspmod.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. * Suspend Modulation - Definitions *
  73. ************************************************************************/
  74. #include <linux/kernel.h>
  75. #include <linux/module.h>
  76. #include <linux/init.h>
  77. #include <linux/smp.h>
  78. #include <linux/cpufreq.h>
  79. #include <linux/pci.h>
  80. #include <asm/processor-cyrix.h>
  81. #include <asm/errno.h>
  82. /* PCI config registers, all at F0 */
  83. #define PCI_PMER1 0x80 /* power management enable register 1 */
  84. #define PCI_PMER2 0x81 /* power management enable register 2 */
  85. #define PCI_PMER3 0x82 /* power management enable register 3 */
  86. #define PCI_IRQTC 0x8c /* irq speedup timer counter register:typical 2 to 4ms */
  87. #define PCI_VIDTC 0x8d /* video speedup timer counter register: typical 50 to 100ms */
  88. #define PCI_MODOFF 0x94 /* suspend modulation OFF counter register, 1 = 32us */
  89. #define PCI_MODON 0x95 /* suspend modulation ON counter register */
  90. #define PCI_SUSCFG 0x96 /* suspend configuration register */
  91. /* PMER1 bits */
  92. #define GPM (1<<0) /* global power management */
  93. #define GIT (1<<1) /* globally enable PM device idle timers */
  94. #define GTR (1<<2) /* globally enable IO traps */
  95. #define IRQ_SPDUP (1<<3) /* disable clock throttle during interrupt handling */
  96. #define VID_SPDUP (1<<4) /* disable clock throttle during vga video handling */
  97. /* SUSCFG bits */
  98. #define SUSMOD (1<<0) /* enable/disable suspend modulation */
  99. /* the belows support only with cs5530 (after rev.1.2)/cs5530A */
  100. #define SMISPDUP (1<<1) /* select how SMI re-enable suspend modulation: */
  101. /* IRQTC timer or read SMI speedup disable reg.(F1BAR[08-09h]) */
  102. #define SUSCFG (1<<2) /* enable powering down a GXLV processor. "Special 3Volt Suspend" mode */
  103. /* the belows support only with cs5530A */
  104. #define PWRSVE_ISA (1<<3) /* stop ISA clock */
  105. #define PWRSVE (1<<4) /* active idle */
  106. struct gxfreq_params {
  107. u8 on_duration;
  108. u8 off_duration;
  109. u8 pci_suscfg;
  110. u8 pci_pmer1;
  111. u8 pci_pmer2;
  112. struct pci_dev *cs55x0;
  113. };
  114. static struct gxfreq_params *gx_params;
  115. static int stock_freq;
  116. /* PCI bus clock - defaults to 30.000 if cpu_khz is not available */
  117. static int pci_busclk = 0;
  118. module_param (pci_busclk, int, 0444);
  119. /* maximum duration for which the cpu may be suspended
  120. * (32us * MAX_DURATION). If no parameter is given, this defaults
  121. * to 255.
  122. * Note that this leads to a maximum of 8 ms(!) where the CPU clock
  123. * is suspended -- processing power is just 0.39% of what it used to be,
  124. * though. 781.25 kHz(!) for a 200 MHz processor -- wow. */
  125. static int max_duration = 255;
  126. module_param (max_duration, int, 0444);
  127. /* For the default policy, we want at least some processing power
  128. * - let's say 5%. (min = maxfreq / POLICY_MIN_DIV)
  129. */
  130. #define POLICY_MIN_DIV 20
  131. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "gx-suspmod", msg)
  132. /**
  133. * we can detect a core multipiler from dir0_lsb
  134. * from GX1 datasheet p.56,
  135. * MULT[3:0]:
  136. * 0000 = SYSCLK multiplied by 4 (test only)
  137. * 0001 = SYSCLK multiplied by 10
  138. * 0010 = SYSCLK multiplied by 4
  139. * 0011 = SYSCLK multiplied by 6
  140. * 0100 = SYSCLK multiplied by 9
  141. * 0101 = SYSCLK multiplied by 5
  142. * 0110 = SYSCLK multiplied by 7
  143. * 0111 = SYSCLK multiplied by 8
  144. * of 33.3MHz
  145. **/
  146. static int gx_freq_mult[16] = {
  147. 4, 10, 4, 6, 9, 5, 7, 8,
  148. 0, 0, 0, 0, 0, 0, 0, 0
  149. };
  150. /****************************************************************
  151. * Low Level chipset interface *
  152. ****************************************************************/
  153. static struct pci_device_id gx_chipset_tbl[] __initdata = {
  154. { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5530_LEGACY, PCI_ANY_ID, PCI_ANY_ID },
  155. { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5520, PCI_ANY_ID, PCI_ANY_ID },
  156. { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5510, PCI_ANY_ID, PCI_ANY_ID },
  157. { 0, },
  158. };
  159. /**
  160. * gx_detect_chipset:
  161. *
  162. **/
  163. static __init struct pci_dev *gx_detect_chipset(void)
  164. {
  165. struct pci_dev *gx_pci = NULL;
  166. /* check if CPU is a MediaGX or a Geode. */
  167. if ((current_cpu_data.x86_vendor != X86_VENDOR_NSC) &&
  168. (current_cpu_data.x86_vendor != X86_VENDOR_CYRIX)) {
  169. dprintk("error: no MediaGX/Geode processor found!\n");
  170. return NULL;
  171. }
  172. /* detect which companion chip is used */
  173. while ((gx_pci = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, gx_pci)) != NULL) {
  174. if ((pci_match_id(gx_chipset_tbl, gx_pci)) != NULL)
  175. return gx_pci;
  176. }
  177. dprintk("error: no supported chipset found!\n");
  178. return NULL;
  179. }
  180. /**
  181. * gx_get_cpuspeed:
  182. *
  183. * Finds out at which efficient frequency the Cyrix MediaGX/NatSemi Geode CPU runs.
  184. */
  185. static unsigned int gx_get_cpuspeed(unsigned int cpu)
  186. {
  187. if ((gx_params->pci_suscfg & SUSMOD) == 0)
  188. return stock_freq;
  189. return (stock_freq * gx_params->off_duration)
  190. / (gx_params->on_duration + gx_params->off_duration);
  191. }
  192. /**
  193. * gx_validate_speed:
  194. * determine current cpu speed
  195. *
  196. **/
  197. static unsigned int gx_validate_speed(unsigned int khz, u8 *on_duration, u8 *off_duration)
  198. {
  199. unsigned int i;
  200. u8 tmp_on, tmp_off;
  201. int old_tmp_freq = stock_freq;
  202. int tmp_freq;
  203. *off_duration=1;
  204. *on_duration=0;
  205. for (i=max_duration; i>0; i--) {
  206. tmp_off = ((khz * i) / stock_freq) & 0xff;
  207. tmp_on = i - tmp_off;
  208. tmp_freq = (stock_freq * tmp_off) / i;
  209. /* if this relation is closer to khz, use this. If it's equal,
  210. * prefer it, too - lower latency */
  211. if (abs(tmp_freq - khz) <= abs(old_tmp_freq - khz)) {
  212. *on_duration = tmp_on;
  213. *off_duration = tmp_off;
  214. old_tmp_freq = tmp_freq;
  215. }
  216. }
  217. return old_tmp_freq;
  218. }
  219. /**
  220. * gx_set_cpuspeed:
  221. * set cpu speed in khz.
  222. **/
  223. static void gx_set_cpuspeed(unsigned int khz)
  224. {
  225. u8 suscfg, pmer1;
  226. unsigned int new_khz;
  227. unsigned long flags;
  228. struct cpufreq_freqs freqs;
  229. freqs.cpu = 0;
  230. freqs.old = gx_get_cpuspeed(0);
  231. new_khz = gx_validate_speed(khz, &gx_params->on_duration, &gx_params->off_duration);
  232. freqs.new = new_khz;
  233. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  234. local_irq_save(flags);
  235. if (new_khz != stock_freq) { /* if new khz == 100% of CPU speed, it is special case */
  236. switch (gx_params->cs55x0->device) {
  237. case PCI_DEVICE_ID_CYRIX_5530_LEGACY:
  238. pmer1 = gx_params->pci_pmer1 | IRQ_SPDUP | VID_SPDUP;
  239. /* FIXME: need to test other values -- Zwane,Miura */
  240. pci_write_config_byte(gx_params->cs55x0, PCI_IRQTC, 4); /* typical 2 to 4ms */
  241. pci_write_config_byte(gx_params->cs55x0, PCI_VIDTC, 100);/* typical 50 to 100ms */
  242. pci_write_config_byte(gx_params->cs55x0, PCI_PMER1, pmer1);
  243. if (gx_params->cs55x0->revision < 0x10) { /* CS5530(rev 1.2, 1.3) */
  244. suscfg = gx_params->pci_suscfg | SUSMOD;
  245. } else { /* CS5530A,B.. */
  246. suscfg = gx_params->pci_suscfg | SUSMOD | PWRSVE;
  247. }
  248. break;
  249. case PCI_DEVICE_ID_CYRIX_5520:
  250. case PCI_DEVICE_ID_CYRIX_5510:
  251. suscfg = gx_params->pci_suscfg | SUSMOD;
  252. break;
  253. default:
  254. local_irq_restore(flags);
  255. dprintk("fatal: try to set unknown chipset.\n");
  256. return;
  257. }
  258. } else {
  259. suscfg = gx_params->pci_suscfg & ~(SUSMOD);
  260. gx_params->off_duration = 0;
  261. gx_params->on_duration = 0;
  262. dprintk("suspend modulation disabled: cpu runs 100 percent speed.\n");
  263. }
  264. pci_write_config_byte(gx_params->cs55x0, PCI_MODOFF, gx_params->off_duration);
  265. pci_write_config_byte(gx_params->cs55x0, PCI_MODON, gx_params->on_duration);
  266. pci_write_config_byte(gx_params->cs55x0, PCI_SUSCFG, suscfg);
  267. pci_read_config_byte(gx_params->cs55x0, PCI_SUSCFG, &suscfg);
  268. local_irq_restore(flags);
  269. gx_params->pci_suscfg = suscfg;
  270. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  271. dprintk("suspend modulation w/ duration of ON:%d us, OFF:%d us\n",
  272. gx_params->on_duration * 32, gx_params->off_duration * 32);
  273. dprintk("suspend modulation w/ clock speed: %d kHz.\n", freqs.new);
  274. }
  275. /****************************************************************
  276. * High level functions *
  277. ****************************************************************/
  278. /*
  279. * cpufreq_gx_verify: test if frequency range is valid
  280. *
  281. * This function checks if a given frequency range in kHz is valid
  282. * for the hardware supported by the driver.
  283. */
  284. static int cpufreq_gx_verify(struct cpufreq_policy *policy)
  285. {
  286. unsigned int tmp_freq = 0;
  287. u8 tmp1, tmp2;
  288. if (!stock_freq || !policy)
  289. return -EINVAL;
  290. policy->cpu = 0;
  291. cpufreq_verify_within_limits(policy, (stock_freq / max_duration), stock_freq);
  292. /* it needs to be assured that at least one supported frequency is
  293. * within policy->min and policy->max. If it is not, policy->max
  294. * needs to be increased until one freuqency is supported.
  295. * policy->min may not be decreased, though. This way we guarantee a
  296. * specific processing capacity.
  297. */
  298. tmp_freq = gx_validate_speed(policy->min, &tmp1, &tmp2);
  299. if (tmp_freq < policy->min)
  300. tmp_freq += stock_freq / max_duration;
  301. policy->min = tmp_freq;
  302. if (policy->min > policy->max)
  303. policy->max = tmp_freq;
  304. tmp_freq = gx_validate_speed(policy->max, &tmp1, &tmp2);
  305. if (tmp_freq > policy->max)
  306. tmp_freq -= stock_freq / max_duration;
  307. policy->max = tmp_freq;
  308. if (policy->max < policy->min)
  309. policy->max = policy->min;
  310. cpufreq_verify_within_limits(policy, (stock_freq / max_duration), stock_freq);
  311. return 0;
  312. }
  313. /*
  314. * cpufreq_gx_target:
  315. *
  316. */
  317. static int cpufreq_gx_target(struct cpufreq_policy *policy,
  318. unsigned int target_freq,
  319. unsigned int relation)
  320. {
  321. u8 tmp1, tmp2;
  322. unsigned int tmp_freq;
  323. if (!stock_freq || !policy)
  324. return -EINVAL;
  325. policy->cpu = 0;
  326. tmp_freq = gx_validate_speed(target_freq, &tmp1, &tmp2);
  327. while (tmp_freq < policy->min) {
  328. tmp_freq += stock_freq / max_duration;
  329. tmp_freq = gx_validate_speed(tmp_freq, &tmp1, &tmp2);
  330. }
  331. while (tmp_freq > policy->max) {
  332. tmp_freq -= stock_freq / max_duration;
  333. tmp_freq = gx_validate_speed(tmp_freq, &tmp1, &tmp2);
  334. }
  335. gx_set_cpuspeed(tmp_freq);
  336. return 0;
  337. }
  338. static int cpufreq_gx_cpu_init(struct cpufreq_policy *policy)
  339. {
  340. unsigned int maxfreq, curfreq;
  341. if (!policy || policy->cpu != 0)
  342. return -ENODEV;
  343. /* determine maximum frequency */
  344. if (pci_busclk) {
  345. maxfreq = pci_busclk * gx_freq_mult[getCx86(CX86_DIR1) & 0x0f];
  346. } else if (cpu_khz) {
  347. maxfreq = cpu_khz;
  348. } else {
  349. maxfreq = 30000 * gx_freq_mult[getCx86(CX86_DIR1) & 0x0f];
  350. }
  351. stock_freq = maxfreq;
  352. curfreq = gx_get_cpuspeed(0);
  353. dprintk("cpu max frequency is %d.\n", maxfreq);
  354. dprintk("cpu current frequency is %dkHz.\n",curfreq);
  355. /* setup basic struct for cpufreq API */
  356. policy->cpu = 0;
  357. if (max_duration < POLICY_MIN_DIV)
  358. policy->min = maxfreq / max_duration;
  359. else
  360. policy->min = maxfreq / POLICY_MIN_DIV;
  361. policy->max = maxfreq;
  362. policy->cur = curfreq;
  363. policy->cpuinfo.min_freq = maxfreq / max_duration;
  364. policy->cpuinfo.max_freq = maxfreq;
  365. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  366. return 0;
  367. }
  368. /*
  369. * cpufreq_gx_init:
  370. * MediaGX/Geode GX initialize cpufreq driver
  371. */
  372. static struct cpufreq_driver gx_suspmod_driver = {
  373. .get = gx_get_cpuspeed,
  374. .verify = cpufreq_gx_verify,
  375. .target = cpufreq_gx_target,
  376. .init = cpufreq_gx_cpu_init,
  377. .name = "gx-suspmod",
  378. .owner = THIS_MODULE,
  379. };
  380. static int __init cpufreq_gx_init(void)
  381. {
  382. int ret;
  383. struct gxfreq_params *params;
  384. struct pci_dev *gx_pci;
  385. /* Test if we have the right hardware */
  386. if ((gx_pci = gx_detect_chipset()) == NULL)
  387. return -ENODEV;
  388. /* check whether module parameters are sane */
  389. if (max_duration > 0xff)
  390. max_duration = 0xff;
  391. dprintk("geode suspend modulation available.\n");
  392. params = kzalloc(sizeof(struct gxfreq_params), GFP_KERNEL);
  393. if (params == NULL)
  394. return -ENOMEM;
  395. params->cs55x0 = gx_pci;
  396. gx_params = params;
  397. /* keep cs55x0 configurations */
  398. pci_read_config_byte(params->cs55x0, PCI_SUSCFG, &(params->pci_suscfg));
  399. pci_read_config_byte(params->cs55x0, PCI_PMER1, &(params->pci_pmer1));
  400. pci_read_config_byte(params->cs55x0, PCI_PMER2, &(params->pci_pmer2));
  401. pci_read_config_byte(params->cs55x0, PCI_MODON, &(params->on_duration));
  402. pci_read_config_byte(params->cs55x0, PCI_MODOFF, &(params->off_duration));
  403. if ((ret = cpufreq_register_driver(&gx_suspmod_driver))) {
  404. kfree(params);
  405. return ret; /* register error! */
  406. }
  407. return 0;
  408. }
  409. static void __exit cpufreq_gx_exit(void)
  410. {
  411. cpufreq_unregister_driver(&gx_suspmod_driver);
  412. pci_dev_put(gx_params->cs55x0);
  413. kfree(gx_params);
  414. }
  415. MODULE_AUTHOR ("Hiroshi Miura <miura@da-cha.org>");
  416. MODULE_DESCRIPTION ("Cpufreq driver for Cyrix MediaGX and NatSemi Geode");
  417. MODULE_LICENSE ("GPL");
  418. module_init(cpufreq_gx_init);
  419. module_exit(cpufreq_gx_exit);