cpufreq-nforce2.c 9.4 KB

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