mach64_gx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ATI Mach64 GX Support
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/fb.h>
  7. #include <asm/io.h>
  8. #include <video/mach64.h>
  9. #include "atyfb.h"
  10. /* Definitions for the ICS 2595 == ATI 18818_1 Clockchip */
  11. #define REF_FREQ_2595 1432 /* 14.33 MHz (exact 14.31818) */
  12. #define REF_DIV_2595 46 /* really 43 on ICS 2595 !!! */
  13. /* ohne Prescaler */
  14. #define MAX_FREQ_2595 15938 /* 159.38 MHz (really 170.486) */
  15. #define MIN_FREQ_2595 8000 /* 80.00 MHz ( 85.565) */
  16. /* mit Prescaler 2, 4, 8 */
  17. #define ABS_MIN_FREQ_2595 1000 /* 10.00 MHz (really 10.697) */
  18. #define N_ADJ_2595 257
  19. #define STOP_BITS_2595 0x1800
  20. #define MIN_N_408 2
  21. #define MIN_N_1703 6
  22. #define MIN_M 2
  23. #define MAX_M 30
  24. #define MIN_N 35
  25. #define MAX_N 255-8
  26. /*
  27. * Support Functions
  28. */
  29. static void aty_dac_waste4(const struct atyfb_par *par)
  30. {
  31. (void) aty_ld_8(DAC_REGS, par);
  32. (void) aty_ld_8(DAC_REGS + 2, par);
  33. (void) aty_ld_8(DAC_REGS + 2, par);
  34. (void) aty_ld_8(DAC_REGS + 2, par);
  35. (void) aty_ld_8(DAC_REGS + 2, par);
  36. }
  37. static void aty_StrobeClock(const struct atyfb_par *par)
  38. {
  39. u8 tmp;
  40. udelay(26);
  41. tmp = aty_ld_8(CLOCK_CNTL, par);
  42. aty_st_8(CLOCK_CNTL + par->clk_wr_offset, tmp | CLOCK_STROBE, par);
  43. return;
  44. }
  45. /*
  46. * IBM RGB514 DAC and Clock Chip
  47. */
  48. static void aty_st_514(int offset, u8 val, const struct atyfb_par *par)
  49. {
  50. aty_st_8(DAC_CNTL, 1, par);
  51. /* right addr byte */
  52. aty_st_8(DAC_W_INDEX, offset & 0xff, par);
  53. /* left addr byte */
  54. aty_st_8(DAC_DATA, (offset >> 8) & 0xff, par);
  55. aty_st_8(DAC_MASK, val, par);
  56. aty_st_8(DAC_CNTL, 0, par);
  57. }
  58. static int aty_set_dac_514(const struct fb_info *info,
  59. const union aty_pll *pll, u32 bpp, u32 accel)
  60. {
  61. struct atyfb_par *par = (struct atyfb_par *) info->par;
  62. static struct {
  63. u8 pixel_dly;
  64. u8 misc2_cntl;
  65. u8 pixel_rep;
  66. u8 pixel_cntl_index;
  67. u8 pixel_cntl_v1;
  68. } tab[3] = {
  69. {
  70. 0, 0x41, 0x03, 0x71, 0x45}, /* 8 bpp */
  71. {
  72. 0, 0x45, 0x04, 0x0c, 0x01}, /* 555 */
  73. {
  74. 0, 0x45, 0x06, 0x0e, 0x00}, /* XRGB */
  75. };
  76. int i;
  77. switch (bpp) {
  78. case 8:
  79. default:
  80. i = 0;
  81. break;
  82. case 16:
  83. i = 1;
  84. break;
  85. case 32:
  86. i = 2;
  87. break;
  88. }
  89. aty_st_514(0x90, 0x00, par); /* VRAM Mask Low */
  90. aty_st_514(0x04, tab[i].pixel_dly, par); /* Horizontal Sync Control */
  91. aty_st_514(0x05, 0x00, par); /* Power Management */
  92. aty_st_514(0x02, 0x01, par); /* Misc Clock Control */
  93. aty_st_514(0x71, tab[i].misc2_cntl, par); /* Misc Control 2 */
  94. aty_st_514(0x0a, tab[i].pixel_rep, par); /* Pixel Format */
  95. aty_st_514(tab[i].pixel_cntl_index, tab[i].pixel_cntl_v1, par);
  96. /* Misc Control 2 / 16 BPP Control / 32 BPP Control */
  97. return 0;
  98. }
  99. static int aty_var_to_pll_514(const struct fb_info *info, u32 vclk_per,
  100. u32 bpp, union aty_pll *pll)
  101. {
  102. /*
  103. * FIXME: use real calculations instead of using fixed values from the old
  104. * driver
  105. */
  106. static struct {
  107. u32 limit; /* pixlock rounding limit (arbitrary) */
  108. u8 m; /* (df<<6) | vco_div_count */
  109. u8 n; /* ref_div_count */
  110. } RGB514_clocks[7] = {
  111. {
  112. 8000, (3 << 6) | 20, 9}, /* 7395 ps / 135.2273 MHz */
  113. {
  114. 10000, (1 << 6) | 19, 3}, /* 9977 ps / 100.2273 MHz */
  115. {
  116. 13000, (1 << 6) | 2, 3}, /* 12509 ps / 79.9432 MHz */
  117. {
  118. 14000, (2 << 6) | 8, 7}, /* 13394 ps / 74.6591 MHz */
  119. {
  120. 16000, (1 << 6) | 44, 6}, /* 15378 ps / 65.0284 MHz */
  121. {
  122. 25000, (1 << 6) | 15, 5}, /* 17460 ps / 57.2727 MHz */
  123. {
  124. 50000, (0 << 6) | 53, 7}, /* 33145 ps / 30.1705 MHz */
  125. };
  126. int i;
  127. for (i = 0; i < ARRAY_SIZE(RGB514_clocks); i++)
  128. if (vclk_per <= RGB514_clocks[i].limit) {
  129. pll->ibm514.m = RGB514_clocks[i].m;
  130. pll->ibm514.n = RGB514_clocks[i].n;
  131. return 0;
  132. }
  133. return -EINVAL;
  134. }
  135. static u32 aty_pll_514_to_var(const struct fb_info *info,
  136. const union aty_pll *pll)
  137. {
  138. struct atyfb_par *par = (struct atyfb_par *) info->par;
  139. u8 df, vco_div_count, ref_div_count;
  140. df = pll->ibm514.m >> 6;
  141. vco_div_count = pll->ibm514.m & 0x3f;
  142. ref_div_count = pll->ibm514.n;
  143. return ((par->ref_clk_per * ref_div_count) << (3 - df))/
  144. (vco_div_count + 65);
  145. }
  146. static void aty_set_pll_514(const struct fb_info *info,
  147. const union aty_pll *pll)
  148. {
  149. struct atyfb_par *par = (struct atyfb_par *) info->par;
  150. aty_st_514(0x06, 0x02, par); /* DAC Operation */
  151. aty_st_514(0x10, 0x01, par); /* PLL Control 1 */
  152. aty_st_514(0x70, 0x01, par); /* Misc Control 1 */
  153. aty_st_514(0x8f, 0x1f, par); /* PLL Ref. Divider Input */
  154. aty_st_514(0x03, 0x00, par); /* Sync Control */
  155. aty_st_514(0x05, 0x00, par); /* Power Management */
  156. aty_st_514(0x20, pll->ibm514.m, par); /* F0 / M0 */
  157. aty_st_514(0x21, pll->ibm514.n, par); /* F1 / N0 */
  158. }
  159. const struct aty_dac_ops aty_dac_ibm514 = {
  160. .set_dac = aty_set_dac_514,
  161. };
  162. const struct aty_pll_ops aty_pll_ibm514 = {
  163. .var_to_pll = aty_var_to_pll_514,
  164. .pll_to_var = aty_pll_514_to_var,
  165. .set_pll = aty_set_pll_514,
  166. };
  167. /*
  168. * ATI 68860-B DAC
  169. */
  170. static int aty_set_dac_ATI68860_B(const struct fb_info *info,
  171. const union aty_pll *pll, u32 bpp,
  172. u32 accel)
  173. {
  174. struct atyfb_par *par = (struct atyfb_par *) info->par;
  175. u32 gModeReg, devSetupRegA, temp, mask;
  176. gModeReg = 0;
  177. devSetupRegA = 0;
  178. switch (bpp) {
  179. case 8:
  180. gModeReg = 0x83;
  181. devSetupRegA =
  182. 0x60 | 0x00 /*(info->mach64DAC8Bit ? 0x00 : 0x01) */ ;
  183. break;
  184. case 15:
  185. gModeReg = 0xA0;
  186. devSetupRegA = 0x60;
  187. break;
  188. case 16:
  189. gModeReg = 0xA1;
  190. devSetupRegA = 0x60;
  191. break;
  192. case 24:
  193. gModeReg = 0xC0;
  194. devSetupRegA = 0x60;
  195. break;
  196. case 32:
  197. gModeReg = 0xE3;
  198. devSetupRegA = 0x60;
  199. break;
  200. }
  201. if (!accel) {
  202. gModeReg = 0x80;
  203. devSetupRegA = 0x61;
  204. }
  205. temp = aty_ld_8(DAC_CNTL, par);
  206. aty_st_8(DAC_CNTL, (temp & ~DAC_EXT_SEL_RS2) | DAC_EXT_SEL_RS3,
  207. par);
  208. aty_st_8(DAC_REGS + 2, 0x1D, par);
  209. aty_st_8(DAC_REGS + 3, gModeReg, par);
  210. aty_st_8(DAC_REGS, 0x02, par);
  211. temp = aty_ld_8(DAC_CNTL, par);
  212. aty_st_8(DAC_CNTL, temp | DAC_EXT_SEL_RS2 | DAC_EXT_SEL_RS3, par);
  213. if (info->fix.smem_len < ONE_MB)
  214. mask = 0x04;
  215. else if (info->fix.smem_len == ONE_MB)
  216. mask = 0x08;
  217. else
  218. mask = 0x0C;
  219. /* The following assumes that the BIOS has correctly set R7 of the
  220. * Device Setup Register A at boot time.
  221. */
  222. #define A860_DELAY_L 0x80
  223. temp = aty_ld_8(DAC_REGS, par);
  224. aty_st_8(DAC_REGS, (devSetupRegA | mask) | (temp & A860_DELAY_L),
  225. par);
  226. temp = aty_ld_8(DAC_CNTL, par);
  227. aty_st_8(DAC_CNTL, (temp & ~(DAC_EXT_SEL_RS2 | DAC_EXT_SEL_RS3)),
  228. par);
  229. aty_st_le32(BUS_CNTL, 0x890e20f1, par);
  230. aty_st_le32(DAC_CNTL, 0x47052100, par);
  231. return 0;
  232. }
  233. const struct aty_dac_ops aty_dac_ati68860b = {
  234. .set_dac = aty_set_dac_ATI68860_B,
  235. };
  236. /*
  237. * AT&T 21C498 DAC
  238. */
  239. static int aty_set_dac_ATT21C498(const struct fb_info *info,
  240. const union aty_pll *pll, u32 bpp,
  241. u32 accel)
  242. {
  243. struct atyfb_par *par = (struct atyfb_par *) info->par;
  244. u32 dotClock;
  245. int muxmode = 0;
  246. int DACMask = 0;
  247. dotClock = 100000000 / pll->ics2595.period_in_ps;
  248. switch (bpp) {
  249. case 8:
  250. if (dotClock > 8000) {
  251. DACMask = 0x24;
  252. muxmode = 1;
  253. } else
  254. DACMask = 0x04;
  255. break;
  256. case 15:
  257. DACMask = 0x16;
  258. break;
  259. case 16:
  260. DACMask = 0x36;
  261. break;
  262. case 24:
  263. DACMask = 0xE6;
  264. break;
  265. case 32:
  266. DACMask = 0xE6;
  267. break;
  268. }
  269. if (1 /* info->mach64DAC8Bit */ )
  270. DACMask |= 0x02;
  271. aty_dac_waste4(par);
  272. aty_st_8(DAC_REGS + 2, DACMask, par);
  273. aty_st_le32(BUS_CNTL, 0x890e20f1, par);
  274. aty_st_le32(DAC_CNTL, 0x00072000, par);
  275. return muxmode;
  276. }
  277. const struct aty_dac_ops aty_dac_att21c498 = {
  278. .set_dac = aty_set_dac_ATT21C498,
  279. };
  280. /*
  281. * ATI 18818 / ICS 2595 Clock Chip
  282. */
  283. static int aty_var_to_pll_18818(const struct fb_info *info, u32 vclk_per,
  284. u32 bpp, union aty_pll *pll)
  285. {
  286. u32 MHz100; /* in 0.01 MHz */
  287. u32 program_bits;
  288. u32 post_divider;
  289. /* Calculate the programming word */
  290. MHz100 = 100000000 / vclk_per;
  291. program_bits = -1;
  292. post_divider = 1;
  293. if (MHz100 > MAX_FREQ_2595) {
  294. return -EINVAL;
  295. } else if (MHz100 < ABS_MIN_FREQ_2595) {
  296. return -EINVAL;
  297. } else {
  298. while (MHz100 < MIN_FREQ_2595) {
  299. MHz100 *= 2;
  300. post_divider *= 2;
  301. }
  302. }
  303. MHz100 *= 1000;
  304. MHz100 = (REF_DIV_2595 * MHz100) / REF_FREQ_2595;
  305. MHz100 += 500; /* + 0.5 round */
  306. MHz100 /= 1000;
  307. if (program_bits == -1) {
  308. program_bits = MHz100 - N_ADJ_2595;
  309. switch (post_divider) {
  310. case 1:
  311. program_bits |= 0x0600;
  312. break;
  313. case 2:
  314. program_bits |= 0x0400;
  315. break;
  316. case 4:
  317. program_bits |= 0x0200;
  318. break;
  319. case 8:
  320. default:
  321. break;
  322. }
  323. }
  324. program_bits |= STOP_BITS_2595;
  325. pll->ics2595.program_bits = program_bits;
  326. pll->ics2595.locationAddr = 0;
  327. pll->ics2595.post_divider = post_divider;
  328. pll->ics2595.period_in_ps = vclk_per;
  329. return 0;
  330. }
  331. static u32 aty_pll_18818_to_var(const struct fb_info *info,
  332. const union aty_pll *pll)
  333. {
  334. return (pll->ics2595.period_in_ps); /* default for now */
  335. }
  336. static void aty_ICS2595_put1bit(u8 data, const struct atyfb_par *par)
  337. {
  338. u8 tmp;
  339. data &= 0x01;
  340. tmp = aty_ld_8(CLOCK_CNTL, par);
  341. aty_st_8(CLOCK_CNTL + par->clk_wr_offset,
  342. (tmp & ~0x04) | (data << 2), par);
  343. tmp = aty_ld_8(CLOCK_CNTL, par);
  344. aty_st_8(CLOCK_CNTL + par->clk_wr_offset, (tmp & ~0x08) | (0 << 3),
  345. par);
  346. aty_StrobeClock(par);
  347. tmp = aty_ld_8(CLOCK_CNTL, par);
  348. aty_st_8(CLOCK_CNTL + par->clk_wr_offset, (tmp & ~0x08) | (1 << 3),
  349. par);
  350. aty_StrobeClock(par);
  351. return;
  352. }
  353. static void aty_set_pll18818(const struct fb_info *info,
  354. const union aty_pll *pll)
  355. {
  356. struct atyfb_par *par = (struct atyfb_par *) info->par;
  357. u32 program_bits;
  358. u32 locationAddr;
  359. u32 i;
  360. u8 old_clock_cntl;
  361. u8 old_crtc_ext_disp;
  362. old_clock_cntl = aty_ld_8(CLOCK_CNTL, par);
  363. aty_st_8(CLOCK_CNTL + par->clk_wr_offset, 0, par);
  364. old_crtc_ext_disp = aty_ld_8(CRTC_GEN_CNTL + 3, par);
  365. aty_st_8(CRTC_GEN_CNTL + 3,
  366. old_crtc_ext_disp | (CRTC_EXT_DISP_EN >> 24), par);
  367. mdelay(15); /* delay for 50 (15) ms */
  368. program_bits = pll->ics2595.program_bits;
  369. locationAddr = pll->ics2595.locationAddr;
  370. /* Program the clock chip */
  371. aty_st_8(CLOCK_CNTL + par->clk_wr_offset, 0, par); /* Strobe = 0 */
  372. aty_StrobeClock(par);
  373. aty_st_8(CLOCK_CNTL + par->clk_wr_offset, 1, par); /* Strobe = 0 */
  374. aty_StrobeClock(par);
  375. aty_ICS2595_put1bit(1, par); /* Send start bits */
  376. aty_ICS2595_put1bit(0, par); /* Start bit */
  377. aty_ICS2595_put1bit(0, par); /* Read / ~Write */
  378. for (i = 0; i < 5; i++) { /* Location 0..4 */
  379. aty_ICS2595_put1bit(locationAddr & 1, par);
  380. locationAddr >>= 1;
  381. }
  382. for (i = 0; i < 8 + 1 + 2 + 2; i++) {
  383. aty_ICS2595_put1bit(program_bits & 1, par);
  384. program_bits >>= 1;
  385. }
  386. mdelay(1); /* delay for 1 ms */
  387. (void) aty_ld_8(DAC_REGS, par); /* Clear DAC Counter */
  388. aty_st_8(CRTC_GEN_CNTL + 3, old_crtc_ext_disp, par);
  389. aty_st_8(CLOCK_CNTL + par->clk_wr_offset,
  390. old_clock_cntl | CLOCK_STROBE, par);
  391. mdelay(50); /* delay for 50 (15) ms */
  392. aty_st_8(CLOCK_CNTL + par->clk_wr_offset,
  393. ((pll->ics2595.locationAddr & 0x0F) | CLOCK_STROBE), par);
  394. return;
  395. }
  396. const struct aty_pll_ops aty_pll_ati18818_1 = {
  397. .var_to_pll = aty_var_to_pll_18818,
  398. .pll_to_var = aty_pll_18818_to_var,
  399. .set_pll = aty_set_pll18818,
  400. };
  401. /*
  402. * STG 1703 Clock Chip
  403. */
  404. static int aty_var_to_pll_1703(const struct fb_info *info, u32 vclk_per,
  405. u32 bpp, union aty_pll *pll)
  406. {
  407. u32 mhz100; /* in 0.01 MHz */
  408. u32 program_bits;
  409. /* u32 post_divider; */
  410. u32 mach64MinFreq, mach64MaxFreq, mach64RefFreq;
  411. u32 temp, tempB;
  412. u16 remainder, preRemainder;
  413. short divider = 0, tempA;
  414. /* Calculate the programming word */
  415. mhz100 = 100000000 / vclk_per;
  416. mach64MinFreq = MIN_FREQ_2595;
  417. mach64MaxFreq = MAX_FREQ_2595;
  418. mach64RefFreq = REF_FREQ_2595; /* 14.32 MHz */
  419. /* Calculate program word */
  420. if (mhz100 == 0)
  421. program_bits = 0xE0;
  422. else {
  423. if (mhz100 < mach64MinFreq)
  424. mhz100 = mach64MinFreq;
  425. if (mhz100 > mach64MaxFreq)
  426. mhz100 = mach64MaxFreq;
  427. divider = 0;
  428. while (mhz100 < (mach64MinFreq << 3)) {
  429. mhz100 <<= 1;
  430. divider += 0x20;
  431. }
  432. temp = (unsigned int) (mhz100);
  433. temp = (unsigned int) (temp * (MIN_N_1703 + 2));
  434. temp -= (short) (mach64RefFreq << 1);
  435. tempA = MIN_N_1703;
  436. preRemainder = 0xffff;
  437. do {
  438. tempB = temp;
  439. remainder = tempB % mach64RefFreq;
  440. tempB = tempB / mach64RefFreq;
  441. if ((tempB & 0xffff) <= 127
  442. && (remainder <= preRemainder)) {
  443. preRemainder = remainder;
  444. divider &= ~0x1f;
  445. divider |= tempA;
  446. divider =
  447. (divider & 0x00ff) +
  448. ((tempB & 0xff) << 8);
  449. }
  450. temp += mhz100;
  451. tempA++;
  452. } while (tempA <= (MIN_N_1703 << 1));
  453. program_bits = divider;
  454. }
  455. pll->ics2595.program_bits = program_bits;
  456. pll->ics2595.locationAddr = 0;
  457. pll->ics2595.post_divider = divider; /* fuer nix */
  458. pll->ics2595.period_in_ps = vclk_per;
  459. return 0;
  460. }
  461. static u32 aty_pll_1703_to_var(const struct fb_info *info,
  462. const union aty_pll *pll)
  463. {
  464. return (pll->ics2595.period_in_ps); /* default for now */
  465. }
  466. static void aty_set_pll_1703(const struct fb_info *info,
  467. const union aty_pll *pll)
  468. {
  469. struct atyfb_par *par = (struct atyfb_par *) info->par;
  470. u32 program_bits;
  471. u32 locationAddr;
  472. char old_crtc_ext_disp;
  473. old_crtc_ext_disp = aty_ld_8(CRTC_GEN_CNTL + 3, par);
  474. aty_st_8(CRTC_GEN_CNTL + 3,
  475. old_crtc_ext_disp | (CRTC_EXT_DISP_EN >> 24), par);
  476. program_bits = pll->ics2595.program_bits;
  477. locationAddr = pll->ics2595.locationAddr;
  478. /* Program clock */
  479. aty_dac_waste4(par);
  480. (void) aty_ld_8(DAC_REGS + 2, par);
  481. aty_st_8(DAC_REGS + 2, (locationAddr << 1) + 0x20, par);
  482. aty_st_8(DAC_REGS + 2, 0, par);
  483. aty_st_8(DAC_REGS + 2, (program_bits & 0xFF00) >> 8, par);
  484. aty_st_8(DAC_REGS + 2, (program_bits & 0xFF), par);
  485. (void) aty_ld_8(DAC_REGS, par); /* Clear DAC Counter */
  486. aty_st_8(CRTC_GEN_CNTL + 3, old_crtc_ext_disp, par);
  487. return;
  488. }
  489. const struct aty_pll_ops aty_pll_stg1703 = {
  490. .var_to_pll = aty_var_to_pll_1703,
  491. .pll_to_var = aty_pll_1703_to_var,
  492. .set_pll = aty_set_pll_1703,
  493. };
  494. /*
  495. * Chrontel 8398 Clock Chip
  496. */
  497. static int aty_var_to_pll_8398(const struct fb_info *info, u32 vclk_per,
  498. u32 bpp, union aty_pll *pll)
  499. {
  500. u32 tempA, tempB, fOut, longMHz100, diff, preDiff;
  501. u32 mhz100; /* in 0.01 MHz */
  502. u32 program_bits;
  503. /* u32 post_divider; */
  504. u32 mach64MinFreq, mach64MaxFreq;
  505. u16 m, n, k = 0, save_m, save_n, twoToKth;
  506. /* Calculate the programming word */
  507. mhz100 = 100000000 / vclk_per;
  508. mach64MinFreq = MIN_FREQ_2595;
  509. mach64MaxFreq = MAX_FREQ_2595;
  510. save_m = 0;
  511. save_n = 0;
  512. /* Calculate program word */
  513. if (mhz100 == 0)
  514. program_bits = 0xE0;
  515. else {
  516. if (mhz100 < mach64MinFreq)
  517. mhz100 = mach64MinFreq;
  518. if (mhz100 > mach64MaxFreq)
  519. mhz100 = mach64MaxFreq;
  520. longMHz100 = mhz100 * 256 / 100; /* 8 bit scale this */
  521. while (mhz100 < (mach64MinFreq << 3)) {
  522. mhz100 <<= 1;
  523. k++;
  524. }
  525. twoToKth = 1 << k;
  526. diff = 0;
  527. preDiff = 0xFFFFFFFF;
  528. for (m = MIN_M; m <= MAX_M; m++) {
  529. for (n = MIN_N; n <= MAX_N; n++) {
  530. tempA = 938356; /* 14.31818 * 65536 */
  531. tempA *= (n + 8); /* 43..256 */
  532. tempB = twoToKth * 256;
  533. tempB *= (m + 2); /* 4..32 */
  534. fOut = tempA / tempB; /* 8 bit scale */
  535. if (longMHz100 > fOut)
  536. diff = longMHz100 - fOut;
  537. else
  538. diff = fOut - longMHz100;
  539. if (diff < preDiff) {
  540. save_m = m;
  541. save_n = n;
  542. preDiff = diff;
  543. }
  544. }
  545. }
  546. program_bits = (k << 6) + (save_m) + (save_n << 8);
  547. }
  548. pll->ics2595.program_bits = program_bits;
  549. pll->ics2595.locationAddr = 0;
  550. pll->ics2595.post_divider = 0;
  551. pll->ics2595.period_in_ps = vclk_per;
  552. return 0;
  553. }
  554. static u32 aty_pll_8398_to_var(const struct fb_info *info,
  555. const union aty_pll *pll)
  556. {
  557. return (pll->ics2595.period_in_ps); /* default for now */
  558. }
  559. static void aty_set_pll_8398(const struct fb_info *info,
  560. const union aty_pll *pll)
  561. {
  562. struct atyfb_par *par = (struct atyfb_par *) info->par;
  563. u32 program_bits;
  564. u32 locationAddr;
  565. char old_crtc_ext_disp;
  566. char tmp;
  567. old_crtc_ext_disp = aty_ld_8(CRTC_GEN_CNTL + 3, par);
  568. aty_st_8(CRTC_GEN_CNTL + 3,
  569. old_crtc_ext_disp | (CRTC_EXT_DISP_EN >> 24), par);
  570. program_bits = pll->ics2595.program_bits;
  571. locationAddr = pll->ics2595.locationAddr;
  572. /* Program clock */
  573. tmp = aty_ld_8(DAC_CNTL, par);
  574. aty_st_8(DAC_CNTL, tmp | DAC_EXT_SEL_RS2 | DAC_EXT_SEL_RS3, par);
  575. aty_st_8(DAC_REGS, locationAddr, par);
  576. aty_st_8(DAC_REGS + 1, (program_bits & 0xff00) >> 8, par);
  577. aty_st_8(DAC_REGS + 1, (program_bits & 0xff), par);
  578. tmp = aty_ld_8(DAC_CNTL, par);
  579. aty_st_8(DAC_CNTL, (tmp & ~DAC_EXT_SEL_RS2) | DAC_EXT_SEL_RS3,
  580. par);
  581. (void) aty_ld_8(DAC_REGS, par); /* Clear DAC Counter */
  582. aty_st_8(CRTC_GEN_CNTL + 3, old_crtc_ext_disp, par);
  583. return;
  584. }
  585. const struct aty_pll_ops aty_pll_ch8398 = {
  586. .var_to_pll = aty_var_to_pll_8398,
  587. .pll_to_var = aty_pll_8398_to_var,
  588. .set_pll = aty_set_pll_8398,
  589. };
  590. /*
  591. * AT&T 20C408 Clock Chip
  592. */
  593. static int aty_var_to_pll_408(const struct fb_info *info, u32 vclk_per,
  594. u32 bpp, union aty_pll *pll)
  595. {
  596. u32 mhz100; /* in 0.01 MHz */
  597. u32 program_bits;
  598. /* u32 post_divider; */
  599. u32 mach64MinFreq, mach64MaxFreq, mach64RefFreq;
  600. u32 temp, tempB;
  601. u16 remainder, preRemainder;
  602. short divider = 0, tempA;
  603. /* Calculate the programming word */
  604. mhz100 = 100000000 / vclk_per;
  605. mach64MinFreq = MIN_FREQ_2595;
  606. mach64MaxFreq = MAX_FREQ_2595;
  607. mach64RefFreq = REF_FREQ_2595; /* 14.32 MHz */
  608. /* Calculate program word */
  609. if (mhz100 == 0)
  610. program_bits = 0xFF;
  611. else {
  612. if (mhz100 < mach64MinFreq)
  613. mhz100 = mach64MinFreq;
  614. if (mhz100 > mach64MaxFreq)
  615. mhz100 = mach64MaxFreq;
  616. while (mhz100 < (mach64MinFreq << 3)) {
  617. mhz100 <<= 1;
  618. divider += 0x40;
  619. }
  620. temp = (unsigned int) mhz100;
  621. temp = (unsigned int) (temp * (MIN_N_408 + 2));
  622. temp -= ((short) (mach64RefFreq << 1));
  623. tempA = MIN_N_408;
  624. preRemainder = 0xFFFF;
  625. do {
  626. tempB = temp;
  627. remainder = tempB % mach64RefFreq;
  628. tempB = tempB / mach64RefFreq;
  629. if (((tempB & 0xFFFF) <= 255)
  630. && (remainder <= preRemainder)) {
  631. preRemainder = remainder;
  632. divider &= ~0x3f;
  633. divider |= tempA;
  634. divider =
  635. (divider & 0x00FF) +
  636. ((tempB & 0xFF) << 8);
  637. }
  638. temp += mhz100;
  639. tempA++;
  640. } while (tempA <= 32);
  641. program_bits = divider;
  642. }
  643. pll->ics2595.program_bits = program_bits;
  644. pll->ics2595.locationAddr = 0;
  645. pll->ics2595.post_divider = divider; /* fuer nix */
  646. pll->ics2595.period_in_ps = vclk_per;
  647. return 0;
  648. }
  649. static u32 aty_pll_408_to_var(const struct fb_info *info,
  650. const union aty_pll *pll)
  651. {
  652. return (pll->ics2595.period_in_ps); /* default for now */
  653. }
  654. static void aty_set_pll_408(const struct fb_info *info,
  655. const union aty_pll *pll)
  656. {
  657. struct atyfb_par *par = (struct atyfb_par *) info->par;
  658. u32 program_bits;
  659. u32 locationAddr;
  660. u8 tmpA, tmpB, tmpC;
  661. char old_crtc_ext_disp;
  662. old_crtc_ext_disp = aty_ld_8(CRTC_GEN_CNTL + 3, par);
  663. aty_st_8(CRTC_GEN_CNTL + 3,
  664. old_crtc_ext_disp | (CRTC_EXT_DISP_EN >> 24), par);
  665. program_bits = pll->ics2595.program_bits;
  666. locationAddr = pll->ics2595.locationAddr;
  667. /* Program clock */
  668. aty_dac_waste4(par);
  669. tmpB = aty_ld_8(DAC_REGS + 2, par) | 1;
  670. aty_dac_waste4(par);
  671. aty_st_8(DAC_REGS + 2, tmpB, par);
  672. tmpA = tmpB;
  673. tmpC = tmpA;
  674. tmpA |= 8;
  675. tmpB = 1;
  676. aty_st_8(DAC_REGS, tmpB, par);
  677. aty_st_8(DAC_REGS + 2, tmpA, par);
  678. udelay(400); /* delay for 400 us */
  679. locationAddr = (locationAddr << 2) + 0x40;
  680. tmpB = locationAddr;
  681. tmpA = program_bits >> 8;
  682. aty_st_8(DAC_REGS, tmpB, par);
  683. aty_st_8(DAC_REGS + 2, tmpA, par);
  684. tmpB = locationAddr + 1;
  685. tmpA = (u8) program_bits;
  686. aty_st_8(DAC_REGS, tmpB, par);
  687. aty_st_8(DAC_REGS + 2, tmpA, par);
  688. tmpB = locationAddr + 2;
  689. tmpA = 0x77;
  690. aty_st_8(DAC_REGS, tmpB, par);
  691. aty_st_8(DAC_REGS + 2, tmpA, par);
  692. udelay(400); /* delay for 400 us */
  693. tmpA = tmpC & (~(1 | 8));
  694. tmpB = 1;
  695. aty_st_8(DAC_REGS, tmpB, par);
  696. aty_st_8(DAC_REGS + 2, tmpA, par);
  697. (void) aty_ld_8(DAC_REGS, par); /* Clear DAC Counter */
  698. aty_st_8(CRTC_GEN_CNTL + 3, old_crtc_ext_disp, par);
  699. return;
  700. }
  701. const struct aty_pll_ops aty_pll_att20c408 = {
  702. .var_to_pll = aty_var_to_pll_408,
  703. .pll_to_var = aty_pll_408_to_var,
  704. .set_pll = aty_set_pll_408,
  705. };
  706. /*
  707. * Unsupported DAC and Clock Chip
  708. */
  709. static int aty_set_dac_unsupported(const struct fb_info *info,
  710. const union aty_pll *pll, u32 bpp,
  711. u32 accel)
  712. {
  713. struct atyfb_par *par = (struct atyfb_par *) info->par;
  714. aty_st_le32(BUS_CNTL, 0x890e20f1, par);
  715. aty_st_le32(DAC_CNTL, 0x47052100, par);
  716. /* new in 2.2.3p1 from Geert. ???????? */
  717. aty_st_le32(BUS_CNTL, 0x590e10ff, par);
  718. aty_st_le32(DAC_CNTL, 0x47012100, par);
  719. return 0;
  720. }
  721. static int dummy(void)
  722. {
  723. return 0;
  724. }
  725. const struct aty_dac_ops aty_dac_unsupported = {
  726. .set_dac = aty_set_dac_unsupported,
  727. };
  728. const struct aty_pll_ops aty_pll_unsupported = {
  729. .var_to_pll = (void *) dummy,
  730. .pll_to_var = (void *) dummy,
  731. .set_pll = (void *) dummy,
  732. };