cpufreq-nforce2.c 9.3 KB

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