cyrix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bitops.h>
  3. #include <linux/delay.h>
  4. #include <linux/isa-dma.h>
  5. #include <linux/pci.h>
  6. #include <asm/dma.h>
  7. #include <linux/io.h>
  8. #include <asm/processor-cyrix.h>
  9. #include <asm/processor-flags.h>
  10. #include <linux/timer.h>
  11. #include <asm/pci-direct.h>
  12. #include <asm/tsc.h>
  13. #include <asm/cpufeature.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/clock.h>
  16. #include "cpu.h"
  17. /*
  18. * Read NSC/Cyrix DEVID registers (DIR) to get more detailed info. about the CPU
  19. */
  20. static void __do_cyrix_devid(unsigned char *dir0, unsigned char *dir1)
  21. {
  22. unsigned char ccr2, ccr3;
  23. /* we test for DEVID by checking whether CCR3 is writable */
  24. ccr3 = getCx86(CX86_CCR3);
  25. setCx86(CX86_CCR3, ccr3 ^ 0x80);
  26. getCx86(0xc0); /* dummy to change bus */
  27. if (getCx86(CX86_CCR3) == ccr3) { /* no DEVID regs. */
  28. ccr2 = getCx86(CX86_CCR2);
  29. setCx86(CX86_CCR2, ccr2 ^ 0x04);
  30. getCx86(0xc0); /* dummy */
  31. if (getCx86(CX86_CCR2) == ccr2) /* old Cx486SLC/DLC */
  32. *dir0 = 0xfd;
  33. else { /* Cx486S A step */
  34. setCx86(CX86_CCR2, ccr2);
  35. *dir0 = 0xfe;
  36. }
  37. } else {
  38. setCx86(CX86_CCR3, ccr3); /* restore CCR3 */
  39. /* read DIR0 and DIR1 CPU registers */
  40. *dir0 = getCx86(CX86_DIR0);
  41. *dir1 = getCx86(CX86_DIR1);
  42. }
  43. }
  44. static void do_cyrix_devid(unsigned char *dir0, unsigned char *dir1)
  45. {
  46. unsigned long flags;
  47. local_irq_save(flags);
  48. __do_cyrix_devid(dir0, dir1);
  49. local_irq_restore(flags);
  50. }
  51. /*
  52. * Cx86_dir0_msb is a HACK needed by check_cx686_cpuid/slop in bugs.h in
  53. * order to identify the Cyrix CPU model after we're out of setup.c
  54. *
  55. * Actually since bugs.h doesn't even reference this perhaps someone should
  56. * fix the documentation ???
  57. */
  58. static unsigned char Cx86_dir0_msb = 0;
  59. static const char Cx86_model[][9] = {
  60. "Cx486", "Cx486", "5x86 ", "6x86", "MediaGX ", "6x86MX ",
  61. "M II ", "Unknown"
  62. };
  63. static const char Cx486_name[][5] = {
  64. "SLC", "DLC", "SLC2", "DLC2", "SRx", "DRx",
  65. "SRx2", "DRx2"
  66. };
  67. static const char Cx486S_name[][4] = {
  68. "S", "S2", "Se", "S2e"
  69. };
  70. static const char Cx486D_name[][4] = {
  71. "DX", "DX2", "?", "?", "?", "DX4"
  72. };
  73. static char Cx86_cb[] = "?.5x Core/Bus Clock";
  74. static const char cyrix_model_mult1[] = "12??43";
  75. static const char cyrix_model_mult2[] = "12233445";
  76. /*
  77. * Reset the slow-loop (SLOP) bit on the 686(L) which is set by some old
  78. * BIOSes for compatibility with DOS games. This makes the udelay loop
  79. * work correctly, and improves performance.
  80. *
  81. * FIXME: our newer udelay uses the tsc. We don't need to frob with SLOP
  82. */
  83. static void check_cx686_slop(struct cpuinfo_x86 *c)
  84. {
  85. unsigned long flags;
  86. if (Cx86_dir0_msb == 3) {
  87. unsigned char ccr3, ccr5;
  88. local_irq_save(flags);
  89. ccr3 = getCx86(CX86_CCR3);
  90. setCx86(CX86_CCR3, (ccr3 & 0x0f) | 0x10); /* enable MAPEN */
  91. ccr5 = getCx86(CX86_CCR5);
  92. if (ccr5 & 2)
  93. setCx86(CX86_CCR5, ccr5 & 0xfd); /* reset SLOP */
  94. setCx86(CX86_CCR3, ccr3); /* disable MAPEN */
  95. local_irq_restore(flags);
  96. if (ccr5 & 2) { /* possible wrong calibration done */
  97. pr_info("Recalibrating delay loop with SLOP bit reset\n");
  98. calibrate_delay();
  99. c->loops_per_jiffy = loops_per_jiffy;
  100. }
  101. }
  102. }
  103. static void set_cx86_reorder(void)
  104. {
  105. u8 ccr3;
  106. pr_info("Enable Memory access reorder on Cyrix/NSC processor.\n");
  107. ccr3 = getCx86(CX86_CCR3);
  108. setCx86(CX86_CCR3, (ccr3 & 0x0f) | 0x10); /* enable MAPEN */
  109. /* Load/Store Serialize to mem access disable (=reorder it) */
  110. setCx86(CX86_PCR0, getCx86(CX86_PCR0) & ~0x80);
  111. /* set load/store serialize from 1GB to 4GB */
  112. ccr3 |= 0xe0;
  113. setCx86(CX86_CCR3, ccr3);
  114. }
  115. static void set_cx86_memwb(void)
  116. {
  117. pr_info("Enable Memory-Write-back mode on Cyrix/NSC processor.\n");
  118. /* CCR2 bit 2: unlock NW bit */
  119. setCx86(CX86_CCR2, getCx86(CX86_CCR2) & ~0x04);
  120. /* set 'Not Write-through' */
  121. write_cr0(read_cr0() | X86_CR0_NW);
  122. /* CCR2 bit 2: lock NW bit and set WT1 */
  123. setCx86(CX86_CCR2, getCx86(CX86_CCR2) | 0x14);
  124. }
  125. /*
  126. * Configure later MediaGX and/or Geode processor.
  127. */
  128. static void geode_configure(void)
  129. {
  130. unsigned long flags;
  131. u8 ccr3;
  132. local_irq_save(flags);
  133. /* Suspend on halt power saving and enable #SUSP pin */
  134. setCx86(CX86_CCR2, getCx86(CX86_CCR2) | 0x88);
  135. ccr3 = getCx86(CX86_CCR3);
  136. setCx86(CX86_CCR3, (ccr3 & 0x0f) | 0x10); /* enable MAPEN */
  137. /* FPU fast, DTE cache, Mem bypass */
  138. setCx86(CX86_CCR4, getCx86(CX86_CCR4) | 0x38);
  139. setCx86(CX86_CCR3, ccr3); /* disable MAPEN */
  140. set_cx86_memwb();
  141. set_cx86_reorder();
  142. local_irq_restore(flags);
  143. }
  144. static void early_init_cyrix(struct cpuinfo_x86 *c)
  145. {
  146. unsigned char dir0, dir0_msn, dir1 = 0;
  147. __do_cyrix_devid(&dir0, &dir1);
  148. dir0_msn = dir0 >> 4; /* identifies CPU "family" */
  149. switch (dir0_msn) {
  150. case 3: /* 6x86/6x86L */
  151. /* Emulate MTRRs using Cyrix's ARRs. */
  152. set_cpu_cap(c, X86_FEATURE_CYRIX_ARR);
  153. break;
  154. case 5: /* 6x86MX/M II */
  155. /* Emulate MTRRs using Cyrix's ARRs. */
  156. set_cpu_cap(c, X86_FEATURE_CYRIX_ARR);
  157. break;
  158. }
  159. }
  160. static void init_cyrix(struct cpuinfo_x86 *c)
  161. {
  162. unsigned char dir0, dir0_msn, dir0_lsn, dir1 = 0;
  163. char *buf = c->x86_model_id;
  164. const char *p = NULL;
  165. /*
  166. * Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  167. * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway
  168. */
  169. clear_cpu_cap(c, 0*32+31);
  170. /* Cyrix used bit 24 in extended (AMD) CPUID for Cyrix MMX extensions */
  171. if (test_cpu_cap(c, 1*32+24)) {
  172. clear_cpu_cap(c, 1*32+24);
  173. set_cpu_cap(c, X86_FEATURE_CXMMX);
  174. }
  175. do_cyrix_devid(&dir0, &dir1);
  176. check_cx686_slop(c);
  177. Cx86_dir0_msb = dir0_msn = dir0 >> 4; /* identifies CPU "family" */
  178. dir0_lsn = dir0 & 0xf; /* model or clock multiplier */
  179. /* common case step number/rev -- exceptions handled below */
  180. c->x86_model = (dir1 >> 4) + 1;
  181. c->x86_stepping = dir1 & 0xf;
  182. /* Now cook; the original recipe is by Channing Corn, from Cyrix.
  183. * We do the same thing for each generation: we work out
  184. * the model, multiplier and stepping. Black magic included,
  185. * to make the silicon step/rev numbers match the printed ones.
  186. */
  187. switch (dir0_msn) {
  188. unsigned char tmp;
  189. case 0: /* Cx486SLC/DLC/SRx/DRx */
  190. p = Cx486_name[dir0_lsn & 7];
  191. break;
  192. case 1: /* Cx486S/DX/DX2/DX4 */
  193. p = (dir0_lsn & 8) ? Cx486D_name[dir0_lsn & 5]
  194. : Cx486S_name[dir0_lsn & 3];
  195. break;
  196. case 2: /* 5x86 */
  197. Cx86_cb[2] = cyrix_model_mult1[dir0_lsn & 5];
  198. p = Cx86_cb+2;
  199. break;
  200. case 3: /* 6x86/6x86L */
  201. Cx86_cb[1] = ' ';
  202. Cx86_cb[2] = cyrix_model_mult1[dir0_lsn & 5];
  203. if (dir1 > 0x21) { /* 686L */
  204. Cx86_cb[0] = 'L';
  205. p = Cx86_cb;
  206. (c->x86_model)++;
  207. } else /* 686 */
  208. p = Cx86_cb+1;
  209. /* Emulate MTRRs using Cyrix's ARRs. */
  210. set_cpu_cap(c, X86_FEATURE_CYRIX_ARR);
  211. /* 6x86's contain this bug */
  212. set_cpu_bug(c, X86_BUG_COMA);
  213. break;
  214. case 4: /* MediaGX/GXm or Geode GXM/GXLV/GX1 */
  215. case 11: /* GX1 with inverted Device ID */
  216. #ifdef CONFIG_PCI
  217. {
  218. u32 vendor, device;
  219. /*
  220. * It isn't really a PCI quirk directly, but the cure is the
  221. * same. The MediaGX has deep magic SMM stuff that handles the
  222. * SB emulation. It throws away the fifo on disable_dma() which
  223. * is wrong and ruins the audio.
  224. *
  225. * Bug2: VSA1 has a wrap bug so that using maximum sized DMA
  226. * causes bad things. According to NatSemi VSA2 has another
  227. * bug to do with 'hlt'. I've not seen any boards using VSA2
  228. * and X doesn't seem to support it either so who cares 8).
  229. * VSA1 we work around however.
  230. */
  231. pr_info("Working around Cyrix MediaGX virtual DMA bugs.\n");
  232. isa_dma_bridge_buggy = 2;
  233. /* We do this before the PCI layer is running. However we
  234. are safe here as we know the bridge must be a Cyrix
  235. companion and must be present */
  236. vendor = read_pci_config_16(0, 0, 0x12, PCI_VENDOR_ID);
  237. device = read_pci_config_16(0, 0, 0x12, PCI_DEVICE_ID);
  238. /*
  239. * The 5510/5520 companion chips have a funky PIT.
  240. */
  241. if (vendor == PCI_VENDOR_ID_CYRIX &&
  242. (device == PCI_DEVICE_ID_CYRIX_5510 ||
  243. device == PCI_DEVICE_ID_CYRIX_5520))
  244. mark_tsc_unstable("cyrix 5510/5520 detected");
  245. }
  246. #endif
  247. c->x86_cache_size = 16; /* Yep 16K integrated cache that's it */
  248. /* GXm supports extended cpuid levels 'ala' AMD */
  249. if (c->cpuid_level == 2) {
  250. /* Enable cxMMX extensions (GX1 Datasheet 54) */
  251. setCx86(CX86_CCR7, getCx86(CX86_CCR7) | 1);
  252. /*
  253. * GXm : 0x30 ... 0x5f GXm datasheet 51
  254. * GXlv: 0x6x GXlv datasheet 54
  255. * ? : 0x7x
  256. * GX1 : 0x8x GX1 datasheet 56
  257. */
  258. if ((0x30 <= dir1 && dir1 <= 0x6f) ||
  259. (0x80 <= dir1 && dir1 <= 0x8f))
  260. geode_configure();
  261. return;
  262. } else { /* MediaGX */
  263. Cx86_cb[2] = (dir0_lsn & 1) ? '3' : '4';
  264. p = Cx86_cb+2;
  265. c->x86_model = (dir1 & 0x20) ? 1 : 2;
  266. }
  267. break;
  268. case 5: /* 6x86MX/M II */
  269. if (dir1 > 7) {
  270. dir0_msn++; /* M II */
  271. /* Enable MMX extensions (App note 108) */
  272. setCx86(CX86_CCR7, getCx86(CX86_CCR7)|1);
  273. } else {
  274. /* A 6x86MX - it has the bug. */
  275. set_cpu_bug(c, X86_BUG_COMA);
  276. }
  277. tmp = (!(dir0_lsn & 7) || dir0_lsn & 1) ? 2 : 0;
  278. Cx86_cb[tmp] = cyrix_model_mult2[dir0_lsn & 7];
  279. p = Cx86_cb+tmp;
  280. if (((dir1 & 0x0f) > 4) || ((dir1 & 0xf0) == 0x20))
  281. (c->x86_model)++;
  282. /* Emulate MTRRs using Cyrix's ARRs. */
  283. set_cpu_cap(c, X86_FEATURE_CYRIX_ARR);
  284. break;
  285. case 0xf: /* Cyrix 486 without DEVID registers */
  286. switch (dir0_lsn) {
  287. case 0xd: /* either a 486SLC or DLC w/o DEVID */
  288. dir0_msn = 0;
  289. p = Cx486_name[!!boot_cpu_has(X86_FEATURE_FPU)];
  290. break;
  291. case 0xe: /* a 486S A step */
  292. dir0_msn = 0;
  293. p = Cx486S_name[0];
  294. break;
  295. }
  296. break;
  297. default: /* unknown (shouldn't happen, we know everyone ;-) */
  298. dir0_msn = 7;
  299. break;
  300. }
  301. strcpy(buf, Cx86_model[dir0_msn & 7]);
  302. if (p)
  303. strcat(buf, p);
  304. return;
  305. }
  306. /*
  307. * Handle National Semiconductor branded processors
  308. */
  309. static void init_nsc(struct cpuinfo_x86 *c)
  310. {
  311. /*
  312. * There may be GX1 processors in the wild that are branded
  313. * NSC and not Cyrix.
  314. *
  315. * This function only handles the GX processor, and kicks every
  316. * thing else to the Cyrix init function above - that should
  317. * cover any processors that might have been branded differently
  318. * after NSC acquired Cyrix.
  319. *
  320. * If this breaks your GX1 horribly, please e-mail
  321. * [email protected] to tell us.
  322. */
  323. /* Handle the GX (Formally known as the GX2) */
  324. if (c->x86 == 5 && c->x86_model == 5)
  325. cpu_detect_cache_sizes(c);
  326. else
  327. init_cyrix(c);
  328. }
  329. /*
  330. * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
  331. * by the fact that they preserve the flags across the division of 5/2.
  332. * PII and PPro exhibit this behavior too, but they have cpuid available.
  333. */
  334. /*
  335. * Perform the Cyrix 5/2 test. A Cyrix won't change
  336. * the flags, while other 486 chips will.
  337. */
  338. static inline int test_cyrix_52div(void)
  339. {
  340. unsigned int test;
  341. __asm__ __volatile__(
  342. "sahf\n\t" /* clear flags (%eax = 0x0005) */
  343. "div %b2\n\t" /* divide 5 by 2 */
  344. "lahf" /* store flags into %ah */
  345. : "=a" (test)
  346. : "0" (5), "q" (2)
  347. : "cc");
  348. /* AH is 0x02 on Cyrix after the divide.. */
  349. return (unsigned char) (test >> 8) == 0x02;
  350. }
  351. static void cyrix_identify(struct cpuinfo_x86 *c)
  352. {
  353. /* Detect Cyrix with disabled CPUID */
  354. if (c->x86 == 4 && test_cyrix_52div()) {
  355. unsigned char dir0, dir1;
  356. strcpy(c->x86_vendor_id, "CyrixInstead");
  357. c->x86_vendor = X86_VENDOR_CYRIX;
  358. /* Actually enable cpuid on the older cyrix */
  359. /* Retrieve CPU revisions */
  360. do_cyrix_devid(&dir0, &dir1);
  361. dir0 >>= 4;
  362. /* Check it is an affected model */
  363. if (dir0 == 5 || dir0 == 3) {
  364. unsigned char ccr3;
  365. unsigned long flags;
  366. pr_info("Enabling CPUID on Cyrix processor.\n");
  367. local_irq_save(flags);
  368. ccr3 = getCx86(CX86_CCR3);
  369. /* enable MAPEN */
  370. setCx86(CX86_CCR3, (ccr3 & 0x0f) | 0x10);
  371. /* enable cpuid */
  372. setCx86(CX86_CCR4, getCx86(CX86_CCR4) | 0x80);
  373. /* disable MAPEN */
  374. setCx86(CX86_CCR3, ccr3);
  375. local_irq_restore(flags);
  376. }
  377. }
  378. }
  379. static const struct cpu_dev cyrix_cpu_dev = {
  380. .c_vendor = "Cyrix",
  381. .c_ident = { "CyrixInstead" },
  382. .c_early_init = early_init_cyrix,
  383. .c_init = init_cyrix,
  384. .c_identify = cyrix_identify,
  385. .c_x86_vendor = X86_VENDOR_CYRIX,
  386. };
  387. cpu_dev_register(cyrix_cpu_dev);
  388. static const struct cpu_dev nsc_cpu_dev = {
  389. .c_vendor = "NSC",
  390. .c_ident = { "Geode by NSC" },
  391. .c_init = init_nsc,
  392. .c_x86_vendor = X86_VENDOR_NSC,
  393. };
  394. cpu_dev_register(nsc_cpu_dev);