gx-suspmod.c 14 KB

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