mach64_gx.c 20 KB

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