cpufreq-nforce2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * (C) 2004 Sebastian Witt <se.witt@gmx.net>
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2.
  5. * Based upon reverse engineered information
  6. *
  7. * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/init.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/pci.h>
  15. #include <linux/delay.h>
  16. #define NFORCE2_XTAL 25
  17. #define NFORCE2_BOOTFSB 0x48
  18. #define NFORCE2_PLLENABLE 0xa8
  19. #define NFORCE2_PLLREG 0xa4
  20. #define NFORCE2_PLLADR 0xa0
  21. #define NFORCE2_PLL(mul, div) (0x100000 | (mul << 8) | div)
  22. #define NFORCE2_MIN_FSB 50
  23. #define NFORCE2_SAFE_DISTANCE 50
  24. /* Delay in ms between FSB changes */
  25. //#define NFORCE2_DELAY 10
  26. /* nforce2_chipset:
  27. * FSB is changed using the chipset
  28. */
  29. static struct pci_dev *nforce2_chipset_dev;
  30. /* fid:
  31. * multiplier * 10
  32. */
  33. static int fid = 0;
  34. /* min_fsb, max_fsb:
  35. * minimum and maximum FSB (= FSB at boot time)
  36. */
  37. static int min_fsb = 0;
  38. static int max_fsb = 0;
  39. MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");
  40. MODULE_DESCRIPTION("nForce2 FSB changing cpufreq driver");
  41. MODULE_LICENSE("GPL");
  42. module_param(fid, int, 0444);
  43. module_param(min_fsb, int, 0444);
  44. MODULE_PARM_DESC(fid, "CPU multiplier to use (11.5 = 115)");
  45. MODULE_PARM_DESC(min_fsb,
  46. "Minimum FSB to use, if not defined: current FSB - 50");
  47. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "cpufreq-nforce2", msg)
  48. /*
  49. * nforce2_calc_fsb - calculate FSB
  50. * @pll: PLL value
  51. *
  52. * Calculates FSB from PLL value
  53. */
  54. static int nforce2_calc_fsb(int pll)
  55. {
  56. unsigned char mul, div;
  57. mul = (pll >> 8) & 0xff;
  58. div = pll & 0xff;
  59. if (div > 0)
  60. return NFORCE2_XTAL * mul / div;
  61. return 0;
  62. }
  63. /*
  64. * nforce2_calc_pll - calculate PLL value
  65. * @fsb: FSB
  66. *
  67. * Calculate PLL value for given FSB
  68. */
  69. static int nforce2_calc_pll(unsigned int fsb)
  70. {
  71. unsigned char xmul, xdiv;
  72. unsigned char mul = 0, div = 0;
  73. int tried = 0;
  74. /* Try to calculate multiplier and divider up to 4 times */
  75. while (((mul == 0) || (div == 0)) && (tried <= 3)) {
  76. for (xdiv = 1; xdiv <= 0x80; xdiv++)
  77. for (xmul = 1; xmul <= 0xfe; xmul++)
  78. if (nforce2_calc_fsb(NFORCE2_PLL(xmul, xdiv)) ==
  79. fsb + tried) {
  80. mul = xmul;
  81. div = xdiv;
  82. }
  83. tried++;
  84. }
  85. if ((mul == 0) || (div == 0))
  86. return -1;
  87. return NFORCE2_PLL(mul, div);
  88. }
  89. /*
  90. * nforce2_write_pll - write PLL value to chipset
  91. * @pll: PLL value
  92. *
  93. * Writes new FSB PLL value to chipset
  94. */
  95. static void nforce2_write_pll(int pll)
  96. {
  97. int temp;
  98. /* Set the pll addr. to 0x00 */
  99. temp = 0x00;
  100. pci_write_config_dword(nforce2_chipset_dev, NFORCE2_PLLADR, temp);
  101. /* Now write the value in all 64 registers */
  102. for (temp = 0; temp <= 0x3f; temp++) {
  103. pci_write_config_dword(nforce2_chipset_dev,
  104. NFORCE2_PLLREG, pll);
  105. }
  106. return;
  107. }
  108. /*
  109. * nforce2_fsb_read - Read FSB
  110. *
  111. * Read FSB from chipset
  112. * If bootfsb != 0, return FSB at boot-time
  113. */
  114. static unsigned int nforce2_fsb_read(int bootfsb)
  115. {
  116. struct pci_dev *nforce2_sub5;
  117. u32 fsb, temp = 0;
  118. /* Get chipset boot FSB from subdevice 5 (FSB at boot-time) */
  119. nforce2_sub5 = pci_get_subsys(PCI_VENDOR_ID_NVIDIA,
  120. 0x01EF,
  121. PCI_ANY_ID,
  122. PCI_ANY_ID,
  123. NULL);
  124. if (!nforce2_sub5)
  125. return 0;
  126. pci_read_config_dword(nforce2_sub5, NFORCE2_BOOTFSB, &fsb);
  127. fsb /= 1000000;
  128. /* Check if PLL register is already set */
  129. pci_read_config_byte(nforce2_chipset_dev,
  130. NFORCE2_PLLENABLE, (u8 *)&temp);
  131. if(bootfsb || !temp)
  132. return fsb;
  133. /* Use PLL register FSB value */
  134. pci_read_config_dword(nforce2_chipset_dev,
  135. NFORCE2_PLLREG, &temp);
  136. fsb = nforce2_calc_fsb(temp);
  137. return fsb;
  138. }
  139. /*
  140. * nforce2_set_fsb - set new FSB
  141. * @fsb: New FSB
  142. *
  143. * Sets new FSB
  144. */
  145. static int nforce2_set_fsb(unsigned int fsb)
  146. {
  147. u32 temp = 0;
  148. unsigned int tfsb;
  149. int diff;
  150. int pll = 0;
  151. if ((fsb > max_fsb) || (fsb < NFORCE2_MIN_FSB)) {
  152. printk(KERN_ERR "cpufreq: FSB %d is out of range!\n", fsb);
  153. return -EINVAL;
  154. }
  155. tfsb = nforce2_fsb_read(0);
  156. if (!tfsb) {
  157. printk(KERN_ERR "cpufreq: Error while reading the FSB\n");
  158. return -EINVAL;
  159. }
  160. /* First write? Then set actual value */
  161. pci_read_config_byte(nforce2_chipset_dev,
  162. NFORCE2_PLLENABLE, (u8 *)&temp);
  163. if (!temp) {
  164. pll = nforce2_calc_pll(tfsb);
  165. if (pll < 0)
  166. return -EINVAL;
  167. nforce2_write_pll(pll);
  168. }
  169. /* Enable write access */
  170. temp = 0x01;
  171. pci_write_config_byte(nforce2_chipset_dev, NFORCE2_PLLENABLE, (u8)temp);
  172. diff = tfsb - fsb;
  173. if (!diff)
  174. return 0;
  175. while ((tfsb != fsb) && (tfsb <= max_fsb) && (tfsb >= min_fsb)) {
  176. if (diff < 0)
  177. tfsb++;
  178. else
  179. tfsb--;
  180. /* Calculate the PLL reg. value */
  181. if ((pll = nforce2_calc_pll(tfsb)) == -1)
  182. return -EINVAL;
  183. nforce2_write_pll(pll);
  184. #ifdef NFORCE2_DELAY
  185. mdelay(NFORCE2_DELAY);
  186. #endif
  187. }
  188. temp = 0x40;
  189. pci_write_config_byte(nforce2_chipset_dev, NFORCE2_PLLADR, (u8)temp);
  190. return 0;
  191. }
  192. /**
  193. * nforce2_get - get the CPU frequency
  194. * @cpu: CPU number
  195. *
  196. * Returns the CPU frequency
  197. */
  198. static unsigned int nforce2_get(unsigned int cpu)
  199. {
  200. if (cpu)
  201. return 0;
  202. return nforce2_fsb_read(0) * fid * 100;
  203. }
  204. /**
  205. * nforce2_target - set a new CPUFreq policy
  206. * @policy: new policy
  207. * @target_freq: the target frequency
  208. * @relation: how that frequency relates to achieved frequency (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
  209. *
  210. * Sets a new CPUFreq policy.
  211. */
  212. static int nforce2_target(struct cpufreq_policy *policy,
  213. unsigned int target_freq, unsigned int relation)
  214. {
  215. // unsigned long flags;
  216. struct cpufreq_freqs freqs;
  217. unsigned int target_fsb;
  218. if ((target_freq > policy->max) || (target_freq < policy->min))
  219. return -EINVAL;
  220. target_fsb = target_freq / (fid * 100);
  221. freqs.old = nforce2_get(policy->cpu);
  222. freqs.new = target_fsb * fid * 100;
  223. freqs.cpu = 0; /* Only one CPU on nForce2 plattforms */
  224. if (freqs.old == freqs.new)
  225. return 0;
  226. dprintk(KERN_INFO "cpufreq: Old CPU frequency %d kHz, new %d kHz\n",
  227. freqs.old, freqs.new);
  228. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  229. /* Disable IRQs */
  230. //local_irq_save(flags);
  231. if (nforce2_set_fsb(target_fsb) < 0)
  232. printk(KERN_ERR "cpufreq: Changing FSB to %d failed\n",
  233. target_fsb);
  234. else
  235. dprintk(KERN_INFO "cpufreq: Changed FSB successfully to %d\n",
  236. target_fsb);
  237. /* Enable IRQs */
  238. //local_irq_restore(flags);
  239. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  240. return 0;
  241. }
  242. /**
  243. * nforce2_verify - verifies a new CPUFreq policy
  244. * @policy: new policy
  245. */
  246. static int nforce2_verify(struct cpufreq_policy *policy)
  247. {
  248. unsigned int fsb_pol_max;
  249. fsb_pol_max = policy->max / (fid * 100);
  250. if (policy->min < (fsb_pol_max * fid * 100))
  251. policy->max = (fsb_pol_max + 1) * fid * 100;
  252. cpufreq_verify_within_limits(policy,
  253. policy->cpuinfo.min_freq,
  254. policy->cpuinfo.max_freq);
  255. return 0;
  256. }
  257. static int nforce2_cpu_init(struct cpufreq_policy *policy)
  258. {
  259. unsigned int fsb;
  260. unsigned int rfid;
  261. /* capability check */
  262. if (policy->cpu != 0)
  263. return -ENODEV;
  264. /* Get current FSB */
  265. fsb = nforce2_fsb_read(0);
  266. if (!fsb)
  267. return -EIO;
  268. /* FIX: Get FID from CPU */
  269. if (!fid) {
  270. if (!cpu_khz) {
  271. printk(KERN_WARNING
  272. "cpufreq: cpu_khz not set, can't calculate multiplier!\n");
  273. return -ENODEV;
  274. }
  275. fid = cpu_khz / (fsb * 100);
  276. rfid = fid % 5;
  277. if (rfid) {
  278. if (rfid > 2)
  279. fid += 5 - rfid;
  280. else
  281. fid -= rfid;
  282. }
  283. }
  284. printk(KERN_INFO "cpufreq: FSB currently at %i MHz, FID %d.%d\n", fsb,
  285. fid / 10, fid % 10);
  286. /* Set maximum FSB to FSB at boot time */
  287. max_fsb = nforce2_fsb_read(1);
  288. if(!max_fsb)
  289. return -EIO;
  290. if (!min_fsb)
  291. min_fsb = max_fsb - NFORCE2_SAFE_DISTANCE;
  292. if (min_fsb < NFORCE2_MIN_FSB)
  293. min_fsb = NFORCE2_MIN_FSB;
  294. /* cpuinfo and default policy values */
  295. policy->cpuinfo.min_freq = min_fsb * fid * 100;
  296. policy->cpuinfo.max_freq = max_fsb * fid * 100;
  297. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  298. policy->cur = nforce2_get(policy->cpu);
  299. policy->min = policy->cpuinfo.min_freq;
  300. policy->max = policy->cpuinfo.max_freq;
  301. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  302. return 0;
  303. }
  304. static int nforce2_cpu_exit(struct cpufreq_policy *policy)
  305. {
  306. return 0;
  307. }
  308. static struct cpufreq_driver nforce2_driver = {
  309. .name = "nforce2",
  310. .verify = nforce2_verify,
  311. .target = nforce2_target,
  312. .get = nforce2_get,
  313. .init = nforce2_cpu_init,
  314. .exit = nforce2_cpu_exit,
  315. .owner = THIS_MODULE,
  316. };
  317. /**
  318. * nforce2_detect_chipset - detect the Southbridge which contains FSB PLL logic
  319. *
  320. * Detects nForce2 A2 and C1 stepping
  321. *
  322. */
  323. static unsigned int nforce2_detect_chipset(void)
  324. {
  325. u8 revision;
  326. nforce2_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_NVIDIA,
  327. PCI_DEVICE_ID_NVIDIA_NFORCE2,
  328. PCI_ANY_ID,
  329. PCI_ANY_ID,
  330. NULL);
  331. if (nforce2_chipset_dev == NULL)
  332. return -ENODEV;
  333. pci_read_config_byte(nforce2_chipset_dev, PCI_REVISION_ID, &revision);
  334. printk(KERN_INFO "cpufreq: Detected nForce2 chipset revision %X\n",
  335. revision);
  336. printk(KERN_INFO
  337. "cpufreq: FSB changing is maybe unstable and can lead to crashes and data loss.\n");
  338. return 0;
  339. }
  340. /**
  341. * nforce2_init - initializes the nForce2 CPUFreq driver
  342. *
  343. * Initializes the nForce2 FSB support. Returns -ENODEV on unsupported
  344. * devices, -EINVAL on problems during initiatization, and zero on
  345. * success.
  346. */
  347. static int __init nforce2_init(void)
  348. {
  349. /* TODO: do we need to detect the processor? */
  350. /* detect chipset */
  351. if (nforce2_detect_chipset()) {
  352. printk(KERN_ERR "cpufreq: No nForce2 chipset.\n");
  353. return -ENODEV;
  354. }
  355. return cpufreq_register_driver(&nforce2_driver);
  356. }
  357. /**
  358. * nforce2_exit - unregisters cpufreq module
  359. *
  360. * Unregisters nForce2 FSB change support.
  361. */
  362. static void __exit nforce2_exit(void)
  363. {
  364. cpufreq_unregister_driver(&nforce2_driver);
  365. }
  366. module_init(nforce2_init);
  367. module_exit(nforce2_exit);