longhaul.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * (C) 2001-2004 Dave Jones. <davej@codemonkey.org.uk>
  3. * (C) 2002 Padraig Brady. <padraig@antefacto.com>
  4. *
  5. * Licensed under the terms of the GNU GPL License version 2.
  6. * Based upon datasheets & sample CPUs kindly provided by VIA.
  7. *
  8. * VIA have currently 3 different versions of Longhaul.
  9. * Version 1 (Longhaul) uses the BCR2 MSR at 0x1147.
  10. * It is present only in Samuel 1 (C5A), Samuel 2 (C5B) stepping 0.
  11. * Version 2 of longhaul is the same as v1, but adds voltage scaling.
  12. * Present in Samuel 2 (steppings 1-7 only) (C5B), and Ezra (C5C)
  13. * voltage scaling support has currently been disabled in this driver
  14. * until we have code that gets it right.
  15. * Version 3 of longhaul got renamed to Powersaver and redesigned
  16. * to use the POWERSAVER MSR at 0x110a.
  17. * It is present in Ezra-T (C5M), Nehemiah (C5X) and above.
  18. * It's pretty much the same feature wise to longhaul v2, though
  19. * there is provision for scaling FSB too, but this doesn't work
  20. * too well in practice so we don't even try to use this.
  21. *
  22. * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/init.h>
  28. #include <linux/cpufreq.h>
  29. #include <linux/pci.h>
  30. #include <linux/slab.h>
  31. #include <linux/string.h>
  32. #include <asm/msr.h>
  33. #include <asm/timex.h>
  34. #include <asm/io.h>
  35. #include <asm/acpi.h>
  36. #include <linux/acpi.h>
  37. #include <acpi/processor.h>
  38. #include "longhaul.h"
  39. #define PFX "longhaul: "
  40. #define TYPE_LONGHAUL_V1 1
  41. #define TYPE_LONGHAUL_V2 2
  42. #define TYPE_POWERSAVER 3
  43. #define CPU_SAMUEL 1
  44. #define CPU_SAMUEL2 2
  45. #define CPU_EZRA 3
  46. #define CPU_EZRA_T 4
  47. #define CPU_NEHEMIAH 5
  48. #define CPU_NEHEMIAH_C 6
  49. /* Flags */
  50. #define USE_ACPI_C3 (1 << 1)
  51. #define USE_NORTHBRIDGE (1 << 2)
  52. #define USE_VT8235 (1 << 3)
  53. static int cpu_model;
  54. static unsigned int numscales=16;
  55. static unsigned int fsb;
  56. static struct mV_pos *vrm_mV_table;
  57. static unsigned char *mV_vrm_table;
  58. struct f_msr {
  59. u8 vrm;
  60. u8 pos;
  61. };
  62. static struct f_msr f_msr_table[32];
  63. static unsigned int highest_speed, lowest_speed; /* kHz */
  64. static unsigned int minmult, maxmult;
  65. static int can_scale_voltage;
  66. static struct acpi_processor *pr = NULL;
  67. static struct acpi_processor_cx *cx = NULL;
  68. static u8 longhaul_flags;
  69. static u8 longhaul_pos;
  70. /* Module parameters */
  71. static int scale_voltage;
  72. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
  73. /* Clock ratios multiplied by 10 */
  74. static int clock_ratio[32];
  75. static int eblcr_table[32];
  76. static int longhaul_version;
  77. static struct cpufreq_frequency_table *longhaul_table;
  78. #ifdef CONFIG_CPU_FREQ_DEBUG
  79. static char speedbuffer[8];
  80. static char *print_speed(int speed)
  81. {
  82. if (speed < 1000) {
  83. snprintf(speedbuffer, sizeof(speedbuffer),"%dMHz", speed);
  84. return speedbuffer;
  85. }
  86. if (speed%1000 == 0)
  87. snprintf(speedbuffer, sizeof(speedbuffer),
  88. "%dGHz", speed/1000);
  89. else
  90. snprintf(speedbuffer, sizeof(speedbuffer),
  91. "%d.%dGHz", speed/1000, (speed%1000)/100);
  92. return speedbuffer;
  93. }
  94. #endif
  95. static unsigned int calc_speed(int mult)
  96. {
  97. int khz;
  98. khz = (mult/10)*fsb;
  99. if (mult%10)
  100. khz += fsb/2;
  101. khz *= 1000;
  102. return khz;
  103. }
  104. static int longhaul_get_cpu_mult(void)
  105. {
  106. unsigned long invalue=0,lo, hi;
  107. rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
  108. invalue = (lo & (1<<22|1<<23|1<<24|1<<25)) >>22;
  109. if (longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) {
  110. if (lo & (1<<27))
  111. invalue+=16;
  112. }
  113. return eblcr_table[invalue];
  114. }
  115. /* For processor with BCR2 MSR */
  116. static void do_longhaul1(unsigned int clock_ratio_index)
  117. {
  118. union msr_bcr2 bcr2;
  119. rdmsrl(MSR_VIA_BCR2, bcr2.val);
  120. /* Enable software clock multiplier */
  121. bcr2.bits.ESOFTBF = 1;
  122. bcr2.bits.CLOCKMUL = clock_ratio_index;
  123. /* Sync to timer tick */
  124. safe_halt();
  125. /* Change frequency on next halt or sleep */
  126. wrmsrl(MSR_VIA_BCR2, bcr2.val);
  127. /* Invoke transition */
  128. ACPI_FLUSH_CPU_CACHE();
  129. halt();
  130. /* Disable software clock multiplier */
  131. local_irq_disable();
  132. rdmsrl(MSR_VIA_BCR2, bcr2.val);
  133. bcr2.bits.ESOFTBF = 0;
  134. wrmsrl(MSR_VIA_BCR2, bcr2.val);
  135. }
  136. /* For processor with Longhaul MSR */
  137. static void do_powersaver(int cx_address, unsigned int clock_ratio_index)
  138. {
  139. union msr_longhaul longhaul;
  140. u8 dest_pos;
  141. u32 t;
  142. dest_pos = f_msr_table[clock_ratio_index].pos;
  143. rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  144. /* Setup new frequency */
  145. longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
  146. longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf;
  147. longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
  148. /* Setup new voltage */
  149. if (can_scale_voltage)
  150. longhaul.bits.SoftVID = f_msr_table[clock_ratio_index].vrm;
  151. /* Sync to timer tick */
  152. safe_halt();
  153. /* Raise voltage if necessary */
  154. if (can_scale_voltage && longhaul_pos < dest_pos) {
  155. longhaul.bits.EnableSoftVID = 1;
  156. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  157. /* Change voltage */
  158. if (!cx_address) {
  159. ACPI_FLUSH_CPU_CACHE();
  160. halt();
  161. } else {
  162. ACPI_FLUSH_CPU_CACHE();
  163. /* Invoke C3 */
  164. inb(cx_address);
  165. /* Dummy op - must do something useless after P_LVL3
  166. * read */
  167. t = inl(acpi_fadt.xpm_tmr_blk.address);
  168. }
  169. longhaul.bits.EnableSoftVID = 0;
  170. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  171. longhaul_pos = dest_pos;
  172. }
  173. /* Change frequency on next halt or sleep */
  174. longhaul.bits.EnableSoftBusRatio = 1;
  175. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  176. if (!cx_address) {
  177. ACPI_FLUSH_CPU_CACHE();
  178. halt();
  179. } else {
  180. ACPI_FLUSH_CPU_CACHE();
  181. /* Invoke C3 */
  182. inb(cx_address);
  183. /* Dummy op - must do something useless after P_LVL3 read */
  184. t = inl(acpi_fadt.xpm_tmr_blk.address);
  185. }
  186. /* Disable bus ratio bit */
  187. longhaul.bits.EnableSoftBusRatio = 0;
  188. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  189. /* Reduce voltage if necessary */
  190. if (can_scale_voltage && longhaul_pos > dest_pos) {
  191. longhaul.bits.EnableSoftVID = 1;
  192. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  193. /* Change voltage */
  194. if (!cx_address) {
  195. ACPI_FLUSH_CPU_CACHE();
  196. halt();
  197. } else {
  198. ACPI_FLUSH_CPU_CACHE();
  199. /* Invoke C3 */
  200. inb(cx_address);
  201. /* Dummy op - must do something useless after P_LVL3
  202. * read */
  203. t = inl(acpi_fadt.xpm_tmr_blk.address);
  204. }
  205. longhaul.bits.EnableSoftVID = 0;
  206. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  207. longhaul_pos = dest_pos;
  208. }
  209. }
  210. /**
  211. * longhaul_set_cpu_frequency()
  212. * @clock_ratio_index : bitpattern of the new multiplier.
  213. *
  214. * Sets a new clock ratio.
  215. */
  216. static void longhaul_setstate(unsigned int clock_ratio_index)
  217. {
  218. int speed, mult;
  219. struct cpufreq_freqs freqs;
  220. static unsigned int old_ratio=-1;
  221. unsigned long flags;
  222. unsigned int pic1_mask, pic2_mask;
  223. if (old_ratio == clock_ratio_index)
  224. return;
  225. old_ratio = clock_ratio_index;
  226. mult = clock_ratio[clock_ratio_index];
  227. if (mult == -1)
  228. return;
  229. speed = calc_speed(mult);
  230. if ((speed > highest_speed) || (speed < lowest_speed))
  231. return;
  232. freqs.old = calc_speed(longhaul_get_cpu_mult());
  233. freqs.new = speed;
  234. freqs.cpu = 0; /* longhaul.c is UP only driver */
  235. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  236. dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
  237. fsb, mult/10, mult%10, print_speed(speed/1000));
  238. preempt_disable();
  239. local_irq_save(flags);
  240. pic2_mask = inb(0xA1);
  241. pic1_mask = inb(0x21); /* works on C3. save mask. */
  242. outb(0xFF,0xA1); /* Overkill */
  243. outb(0xFE,0x21); /* TMR0 only */
  244. if (longhaul_flags & USE_NORTHBRIDGE) {
  245. /* Disable AGP and PCI arbiters */
  246. outb(3, 0x22);
  247. } else if ((pr != NULL) && pr->flags.bm_control) {
  248. /* Disable bus master arbitration */
  249. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1,
  250. ACPI_MTX_DO_NOT_LOCK);
  251. }
  252. switch (longhaul_version) {
  253. /*
  254. * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
  255. * Software controlled multipliers only.
  256. *
  257. * *NB* Until we get voltage scaling working v1 & v2 are the same code.
  258. * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5b] and Ezra [C5C]
  259. */
  260. case TYPE_LONGHAUL_V1:
  261. case TYPE_LONGHAUL_V2:
  262. do_longhaul1(clock_ratio_index);
  263. break;
  264. /*
  265. * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
  266. * We can scale voltage with this too, but that's currently
  267. * disabled until we come up with a decent 'match freq to voltage'
  268. * algorithm.
  269. * When we add voltage scaling, we will also need to do the
  270. * voltage/freq setting in order depending on the direction
  271. * of scaling (like we do in powernow-k7.c)
  272. * Nehemiah can do FSB scaling too, but this has never been proven
  273. * to work in practice.
  274. */
  275. case TYPE_POWERSAVER:
  276. if (longhaul_flags & USE_ACPI_C3) {
  277. /* Don't allow wakeup */
  278. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0,
  279. ACPI_MTX_DO_NOT_LOCK);
  280. do_powersaver(cx->address, clock_ratio_index);
  281. } else {
  282. do_powersaver(0, clock_ratio_index);
  283. }
  284. break;
  285. }
  286. if (longhaul_flags & USE_NORTHBRIDGE) {
  287. /* Enable arbiters */
  288. outb(0, 0x22);
  289. } else if ((pr != NULL) && pr->flags.bm_control) {
  290. /* Enable bus master arbitration */
  291. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0,
  292. ACPI_MTX_DO_NOT_LOCK);
  293. }
  294. outb(pic2_mask,0xA1); /* restore mask */
  295. outb(pic1_mask,0x21);
  296. local_irq_restore(flags);
  297. preempt_enable();
  298. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  299. }
  300. /*
  301. * Centaur decided to make life a little more tricky.
  302. * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
  303. * Samuel2 and above have to try and guess what the FSB is.
  304. * We do this by assuming we booted at maximum multiplier, and interpolate
  305. * between that value multiplied by possible FSBs and cpu_mhz which
  306. * was calculated at boot time. Really ugly, but no other way to do this.
  307. */
  308. #define ROUNDING 0xf
  309. static int guess_fsb(int mult)
  310. {
  311. int speed = cpu_khz / 1000;
  312. int i;
  313. int speeds[] = { 666, 1000, 1333, 2000 };
  314. int f_max, f_min;
  315. for (i = 0; i < 4; i++) {
  316. f_max = ((speeds[i] * mult) + 50) / 100;
  317. f_max += (ROUNDING / 2);
  318. f_min = f_max - ROUNDING;
  319. if ((speed <= f_max) && (speed >= f_min))
  320. return speeds[i] / 10;
  321. }
  322. return 0;
  323. }
  324. static int __init longhaul_get_ranges(void)
  325. {
  326. unsigned int j, k = 0;
  327. int mult;
  328. /* Get current frequency */
  329. mult = longhaul_get_cpu_mult();
  330. if (mult == -1) {
  331. printk(KERN_INFO PFX "Invalid (reserved) multiplier!\n");
  332. return -EINVAL;
  333. }
  334. fsb = guess_fsb(mult);
  335. if (fsb == 0) {
  336. printk(KERN_INFO PFX "Invalid (reserved) FSB!\n");
  337. return -EINVAL;
  338. }
  339. /* Get max multiplier - as we always did.
  340. * Longhaul MSR is usefull only when voltage scaling is enabled.
  341. * C3 is booting at max anyway. */
  342. maxmult = mult;
  343. /* Get min multiplier */
  344. switch (cpu_model) {
  345. case CPU_NEHEMIAH:
  346. minmult = 50;
  347. break;
  348. case CPU_NEHEMIAH_C:
  349. minmult = 40;
  350. break;
  351. default:
  352. minmult = 30;
  353. break;
  354. }
  355. dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
  356. minmult/10, minmult%10, maxmult/10, maxmult%10);
  357. highest_speed = calc_speed(maxmult);
  358. lowest_speed = calc_speed(minmult);
  359. dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
  360. print_speed(lowest_speed/1000),
  361. print_speed(highest_speed/1000));
  362. if (lowest_speed == highest_speed) {
  363. printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n");
  364. return -EINVAL;
  365. }
  366. if (lowest_speed > highest_speed) {
  367. printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
  368. lowest_speed, highest_speed);
  369. return -EINVAL;
  370. }
  371. longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL);
  372. if(!longhaul_table)
  373. return -ENOMEM;
  374. for (j=0; j < numscales; j++) {
  375. unsigned int ratio;
  376. ratio = clock_ratio[j];
  377. if (ratio == -1)
  378. continue;
  379. if (ratio > maxmult || ratio < minmult)
  380. continue;
  381. longhaul_table[k].frequency = calc_speed(ratio);
  382. longhaul_table[k].index = j;
  383. k++;
  384. }
  385. longhaul_table[k].frequency = CPUFREQ_TABLE_END;
  386. if (!k) {
  387. kfree (longhaul_table);
  388. return -EINVAL;
  389. }
  390. return 0;
  391. }
  392. static void __init longhaul_setup_voltagescaling(void)
  393. {
  394. union msr_longhaul longhaul;
  395. struct mV_pos minvid, maxvid;
  396. unsigned int j, speed, pos, kHz_step, numvscales;
  397. int min_vid_speed;
  398. rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  399. if (!(longhaul.bits.RevisionID & 1)) {
  400. printk(KERN_INFO PFX "Voltage scaling not supported by CPU.\n");
  401. return;
  402. }
  403. if (!longhaul.bits.VRMRev) {
  404. printk (KERN_INFO PFX "VRM 8.5\n");
  405. vrm_mV_table = &vrm85_mV[0];
  406. mV_vrm_table = &mV_vrm85[0];
  407. } else {
  408. printk (KERN_INFO PFX "Mobile VRM\n");
  409. vrm_mV_table = &mobilevrm_mV[0];
  410. mV_vrm_table = &mV_mobilevrm[0];
  411. }
  412. minvid = vrm_mV_table[longhaul.bits.MinimumVID];
  413. maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
  414. if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
  415. printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
  416. "Voltage scaling disabled.\n",
  417. minvid.mV/1000, minvid.mV%1000, maxvid.mV/1000, maxvid.mV%1000);
  418. return;
  419. }
  420. if (minvid.mV == maxvid.mV) {
  421. printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
  422. "both %d.%03d. Voltage scaling disabled\n",
  423. maxvid.mV/1000, maxvid.mV%1000);
  424. return;
  425. }
  426. /* How many voltage steps */
  427. numvscales = maxvid.pos - minvid.pos + 1;
  428. printk(KERN_INFO PFX
  429. "Max VID=%d.%03d "
  430. "Min VID=%d.%03d, "
  431. "%d possible voltage scales\n",
  432. maxvid.mV/1000, maxvid.mV%1000,
  433. minvid.mV/1000, minvid.mV%1000,
  434. numvscales);
  435. /* Calculate max frequency at min voltage */
  436. j = longhaul.bits.MinMHzBR;
  437. if (longhaul.bits.MinMHzBR4)
  438. j += 16;
  439. min_vid_speed = eblcr_table[j];
  440. if (min_vid_speed == -1)
  441. return;
  442. switch (longhaul.bits.MinMHzFSB) {
  443. case 0:
  444. min_vid_speed *= 13333;
  445. break;
  446. case 1:
  447. min_vid_speed *= 10000;
  448. break;
  449. case 3:
  450. min_vid_speed *= 6666;
  451. break;
  452. default:
  453. return;
  454. break;
  455. }
  456. if (min_vid_speed >= highest_speed)
  457. return;
  458. /* Calculate kHz for one voltage step */
  459. kHz_step = (highest_speed - min_vid_speed) / numvscales;
  460. j = 0;
  461. while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
  462. speed = longhaul_table[j].frequency;
  463. if (speed > min_vid_speed)
  464. pos = (speed - min_vid_speed) / kHz_step + minvid.pos;
  465. else
  466. pos = minvid.pos;
  467. f_msr_table[longhaul_table[j].index].vrm = mV_vrm_table[pos];
  468. f_msr_table[longhaul_table[j].index].pos = pos;
  469. j++;
  470. }
  471. longhaul_pos = maxvid.pos;
  472. can_scale_voltage = 1;
  473. printk(KERN_INFO PFX "Voltage scaling enabled. "
  474. "Use of \"conservative\" governor is highly recommended.\n");
  475. }
  476. static int longhaul_verify(struct cpufreq_policy *policy)
  477. {
  478. return cpufreq_frequency_table_verify(policy, longhaul_table);
  479. }
  480. static int longhaul_target(struct cpufreq_policy *policy,
  481. unsigned int target_freq, unsigned int relation)
  482. {
  483. unsigned int table_index = 0;
  484. unsigned int new_clock_ratio = 0;
  485. if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
  486. return -EINVAL;
  487. new_clock_ratio = longhaul_table[table_index].index & 0xFF;
  488. longhaul_setstate(new_clock_ratio);
  489. return 0;
  490. }
  491. static unsigned int longhaul_get(unsigned int cpu)
  492. {
  493. if (cpu)
  494. return 0;
  495. return calc_speed(longhaul_get_cpu_mult());
  496. }
  497. static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
  498. u32 nesting_level,
  499. void *context, void **return_value)
  500. {
  501. struct acpi_device *d;
  502. if ( acpi_bus_get_device(obj_handle, &d) ) {
  503. return 0;
  504. }
  505. *return_value = (void *)acpi_driver_data(d);
  506. return 1;
  507. }
  508. /* VIA don't support PM2 reg, but have something similar */
  509. static int enable_arbiter_disable(void)
  510. {
  511. struct pci_dev *dev;
  512. int reg;
  513. u8 pci_cmd;
  514. /* Find PLE133 host bridge */
  515. reg = 0x78;
  516. dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0, NULL);
  517. /* Find CLE266 host bridge */
  518. if (dev == NULL) {
  519. reg = 0x76;
  520. dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_862X_0, NULL);
  521. /* Find CN400 V-Link host bridge */
  522. if (dev == NULL)
  523. dev = pci_find_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
  524. }
  525. if (dev != NULL) {
  526. /* Enable access to port 0x22 */
  527. pci_read_config_byte(dev, reg, &pci_cmd);
  528. if (!(pci_cmd & 1<<7)) {
  529. pci_cmd |= 1<<7;
  530. pci_write_config_byte(dev, reg, pci_cmd);
  531. pci_read_config_byte(dev, reg, &pci_cmd);
  532. if (!(pci_cmd & 1<<7)) {
  533. printk(KERN_ERR PFX
  534. "Can't enable access to port 0x22.\n");
  535. return 0;
  536. }
  537. }
  538. return 1;
  539. }
  540. return 0;
  541. }
  542. static int longhaul_setup_vt8235(void)
  543. {
  544. struct pci_dev *dev;
  545. u8 pci_cmd;
  546. /* Find VT8235 southbridge */
  547. dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235, NULL);
  548. if (dev != NULL) {
  549. /* Set transition time to max */
  550. pci_read_config_byte(dev, 0xec, &pci_cmd);
  551. pci_cmd &= ~(1 << 2);
  552. pci_write_config_byte(dev, 0xec, pci_cmd);
  553. pci_read_config_byte(dev, 0xe4, &pci_cmd);
  554. pci_cmd &= ~(1 << 7);
  555. pci_write_config_byte(dev, 0xe4, pci_cmd);
  556. pci_read_config_byte(dev, 0xe5, &pci_cmd);
  557. pci_cmd |= 1 << 7;
  558. pci_write_config_byte(dev, 0xe5, pci_cmd);
  559. return 1;
  560. }
  561. return 0;
  562. }
  563. static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
  564. {
  565. struct cpuinfo_x86 *c = cpu_data;
  566. char *cpuname=NULL;
  567. int ret;
  568. int vt8235_present;
  569. /* Check what we have on this motherboard */
  570. switch (c->x86_model) {
  571. case 6:
  572. cpu_model = CPU_SAMUEL;
  573. cpuname = "C3 'Samuel' [C5A]";
  574. longhaul_version = TYPE_LONGHAUL_V1;
  575. memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
  576. memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr));
  577. break;
  578. case 7:
  579. longhaul_version = TYPE_LONGHAUL_V1;
  580. switch (c->x86_mask) {
  581. case 0:
  582. cpu_model = CPU_SAMUEL2;
  583. cpuname = "C3 'Samuel 2' [C5B]";
  584. /* Note, this is not a typo, early Samuel2's had Samuel1 ratios. */
  585. memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
  586. memcpy (eblcr_table, samuel2_eblcr, sizeof(samuel2_eblcr));
  587. break;
  588. case 1 ... 15:
  589. if (c->x86_mask < 8) {
  590. cpu_model = CPU_SAMUEL2;
  591. cpuname = "C3 'Samuel 2' [C5B]";
  592. } else {
  593. cpu_model = CPU_EZRA;
  594. cpuname = "C3 'Ezra' [C5C]";
  595. }
  596. memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio));
  597. memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr));
  598. break;
  599. }
  600. break;
  601. case 8:
  602. cpu_model = CPU_EZRA_T;
  603. cpuname = "C3 'Ezra-T' [C5M]";
  604. longhaul_version = TYPE_POWERSAVER;
  605. numscales=32;
  606. memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
  607. memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
  608. break;
  609. case 9:
  610. longhaul_version = TYPE_POWERSAVER;
  611. numscales = 32;
  612. memcpy(clock_ratio,
  613. nehemiah_clock_ratio,
  614. sizeof(nehemiah_clock_ratio));
  615. memcpy(eblcr_table, nehemiah_eblcr, sizeof(nehemiah_eblcr));
  616. switch (c->x86_mask) {
  617. case 0 ... 1:
  618. cpu_model = CPU_NEHEMIAH;
  619. cpuname = "C3 'Nehemiah A' [C5XLOE]";
  620. break;
  621. case 2 ... 4:
  622. cpu_model = CPU_NEHEMIAH;
  623. cpuname = "C3 'Nehemiah B' [C5XLOH]";
  624. break;
  625. case 5 ... 15:
  626. cpu_model = CPU_NEHEMIAH_C;
  627. cpuname = "C3 'Nehemiah C' [C5P]";
  628. break;
  629. }
  630. break;
  631. default:
  632. cpuname = "Unknown";
  633. break;
  634. }
  635. printk (KERN_INFO PFX "VIA %s CPU detected. ", cpuname);
  636. switch (longhaul_version) {
  637. case TYPE_LONGHAUL_V1:
  638. case TYPE_LONGHAUL_V2:
  639. printk ("Longhaul v%d supported.\n", longhaul_version);
  640. break;
  641. case TYPE_POWERSAVER:
  642. printk ("Powersaver supported.\n");
  643. break;
  644. };
  645. /* Doesn't hurt */
  646. vt8235_present = longhaul_setup_vt8235();
  647. /* Find ACPI data for processor */
  648. acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
  649. ACPI_UINT32_MAX, &longhaul_walk_callback,
  650. NULL, (void *)&pr);
  651. /* Check ACPI support for C3 state */
  652. if (pr != NULL && longhaul_version == TYPE_POWERSAVER) {
  653. cx = &pr->power.states[ACPI_STATE_C3];
  654. if (cx->address > 0 && cx->latency <= 1000) {
  655. longhaul_flags |= USE_ACPI_C3;
  656. goto print_support_type;
  657. }
  658. }
  659. /* Check if northbridge is friendly */
  660. if (enable_arbiter_disable()) {
  661. longhaul_flags |= USE_NORTHBRIDGE;
  662. goto print_support_type;
  663. }
  664. /* Use VT8235 southbridge if present */
  665. if (longhaul_version == TYPE_POWERSAVER && vt8235_present) {
  666. longhaul_flags |= USE_VT8235;
  667. goto print_support_type;
  668. }
  669. /* Check ACPI support for bus master arbiter disable */
  670. if ((pr == NULL) || !(pr->flags.bm_control)) {
  671. printk(KERN_ERR PFX
  672. "No ACPI support. Unsupported northbridge.\n");
  673. return -ENODEV;
  674. }
  675. print_support_type:
  676. if (longhaul_flags & USE_NORTHBRIDGE)
  677. printk (KERN_INFO PFX "Using northbridge support.\n");
  678. else if (longhaul_flags & USE_VT8235)
  679. printk (KERN_INFO PFX "Using VT8235 support.\n");
  680. else
  681. printk (KERN_INFO PFX "Using ACPI support.\n");
  682. ret = longhaul_get_ranges();
  683. if (ret != 0)
  684. return ret;
  685. if ((longhaul_version != TYPE_LONGHAUL_V1) && (scale_voltage != 0))
  686. longhaul_setup_voltagescaling();
  687. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  688. policy->cpuinfo.transition_latency = 200000; /* nsec */
  689. policy->cur = calc_speed(longhaul_get_cpu_mult());
  690. ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
  691. if (ret)
  692. return ret;
  693. cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
  694. return 0;
  695. }
  696. static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
  697. {
  698. cpufreq_frequency_table_put_attr(policy->cpu);
  699. return 0;
  700. }
  701. static struct freq_attr* longhaul_attr[] = {
  702. &cpufreq_freq_attr_scaling_available_freqs,
  703. NULL,
  704. };
  705. static struct cpufreq_driver longhaul_driver = {
  706. .verify = longhaul_verify,
  707. .target = longhaul_target,
  708. .get = longhaul_get,
  709. .init = longhaul_cpu_init,
  710. .exit = __devexit_p(longhaul_cpu_exit),
  711. .name = "longhaul",
  712. .owner = THIS_MODULE,
  713. .attr = longhaul_attr,
  714. };
  715. static int __init longhaul_init(void)
  716. {
  717. struct cpuinfo_x86 *c = cpu_data;
  718. if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
  719. return -ENODEV;
  720. #ifdef CONFIG_SMP
  721. if (num_online_cpus() > 1) {
  722. printk(KERN_ERR PFX "More than 1 CPU detected, longhaul disabled.\n");
  723. return -ENODEV;
  724. }
  725. #endif
  726. #ifdef CONFIG_X86_IO_APIC
  727. if (cpu_has_apic) {
  728. printk(KERN_ERR PFX "APIC detected. Longhaul is currently broken in this configuration.\n");
  729. return -ENODEV;
  730. }
  731. #endif
  732. switch (c->x86_model) {
  733. case 6 ... 9:
  734. return cpufreq_register_driver(&longhaul_driver);
  735. case 10:
  736. printk(KERN_ERR PFX "Use acpi-cpufreq driver for VIA C7\n");
  737. default:
  738. ;;
  739. }
  740. return -ENODEV;
  741. }
  742. static void __exit longhaul_exit(void)
  743. {
  744. int i;
  745. for (i=0; i < numscales; i++) {
  746. if (clock_ratio[i] == maxmult) {
  747. longhaul_setstate(i);
  748. break;
  749. }
  750. }
  751. cpufreq_unregister_driver(&longhaul_driver);
  752. kfree(longhaul_table);
  753. }
  754. module_param (scale_voltage, int, 0644);
  755. MODULE_PARM_DESC(scale_voltage, "Scale voltage of processor");
  756. MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
  757. MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors.");
  758. MODULE_LICENSE ("GPL");
  759. late_initcall(longhaul_init);
  760. module_exit(longhaul_exit);