mach64_gx.c 20 KB

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