longhaul.c 22 KB

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