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 <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 < 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. MHz100 = MAX_FREQ_2595;
  295. return -EINVAL;
  296. } else if (MHz100 < ABS_MIN_FREQ_2595) {
  297. program_bits = 0; /* MHz100 = 257 */
  298. return -EINVAL;
  299. } else {
  300. while (MHz100 < MIN_FREQ_2595) {
  301. MHz100 *= 2;
  302. post_divider *= 2;
  303. }
  304. }
  305. MHz100 *= 1000;
  306. MHz100 = (REF_DIV_2595 * MHz100) / REF_FREQ_2595;
  307. MHz100 += 500; /* + 0.5 round */
  308. MHz100 /= 1000;
  309. if (program_bits == -1) {
  310. program_bits = MHz100 - N_ADJ_2595;
  311. switch (post_divider) {
  312. case 1:
  313. program_bits |= 0x0600;
  314. break;
  315. case 2:
  316. program_bits |= 0x0400;
  317. break;
  318. case 4:
  319. program_bits |= 0x0200;
  320. break;
  321. case 8:
  322. default:
  323. break;
  324. }
  325. }
  326. program_bits |= STOP_BITS_2595;
  327. pll->ics2595.program_bits = program_bits;
  328. pll->ics2595.locationAddr = 0;
  329. pll->ics2595.post_divider = post_divider;
  330. pll->ics2595.period_in_ps = vclk_per;
  331. return 0;
  332. }
  333. static u32 aty_pll_18818_to_var(const struct fb_info *info,
  334. const union aty_pll *pll)
  335. {
  336. return (pll->ics2595.period_in_ps); /* default for now */
  337. }
  338. static void aty_ICS2595_put1bit(u8 data, const struct atyfb_par *par)
  339. {
  340. u8 tmp;
  341. data &= 0x01;
  342. tmp = aty_ld_8(CLOCK_CNTL, par);
  343. aty_st_8(CLOCK_CNTL + par->clk_wr_offset,
  344. (tmp & ~0x04) | (data << 2), par);
  345. tmp = aty_ld_8(CLOCK_CNTL, par);
  346. aty_st_8(CLOCK_CNTL + par->clk_wr_offset, (tmp & ~0x08) | (0 << 3),
  347. par);
  348. aty_StrobeClock(par);
  349. tmp = aty_ld_8(CLOCK_CNTL, par);
  350. aty_st_8(CLOCK_CNTL + par->clk_wr_offset, (tmp & ~0x08) | (1 << 3),
  351. par);
  352. aty_StrobeClock(par);
  353. return;
  354. }
  355. static void aty_set_pll18818(const struct fb_info *info,
  356. const union aty_pll *pll)
  357. {
  358. struct atyfb_par *par = (struct atyfb_par *) info->par;
  359. u32 program_bits;
  360. u32 locationAddr;
  361. u32 i;
  362. u8 old_clock_cntl;
  363. u8 old_crtc_ext_disp;
  364. old_clock_cntl = aty_ld_8(CLOCK_CNTL, par);
  365. aty_st_8(CLOCK_CNTL + par->clk_wr_offset, 0, par);
  366. old_crtc_ext_disp = aty_ld_8(CRTC_GEN_CNTL + 3, par);
  367. aty_st_8(CRTC_GEN_CNTL + 3,
  368. old_crtc_ext_disp | (CRTC_EXT_DISP_EN >> 24), par);
  369. mdelay(15); /* delay for 50 (15) ms */
  370. program_bits = pll->ics2595.program_bits;
  371. locationAddr = pll->ics2595.locationAddr;
  372. /* Program the clock chip */
  373. aty_st_8(CLOCK_CNTL + par->clk_wr_offset, 0, par); /* Strobe = 0 */
  374. aty_StrobeClock(par);
  375. aty_st_8(CLOCK_CNTL + par->clk_wr_offset, 1, par); /* Strobe = 0 */
  376. aty_StrobeClock(par);
  377. aty_ICS2595_put1bit(1, par); /* Send start bits */
  378. aty_ICS2595_put1bit(0, par); /* Start bit */
  379. aty_ICS2595_put1bit(0, par); /* Read / ~Write */
  380. for (i = 0; i < 5; i++) { /* Location 0..4 */
  381. aty_ICS2595_put1bit(locationAddr & 1, par);
  382. locationAddr >>= 1;
  383. }
  384. for (i = 0; i < 8 + 1 + 2 + 2; i++) {
  385. aty_ICS2595_put1bit(program_bits & 1, par);
  386. program_bits >>= 1;
  387. }
  388. mdelay(1); /* delay for 1 ms */
  389. (void) aty_ld_8(DAC_REGS, par); /* Clear DAC Counter */
  390. aty_st_8(CRTC_GEN_CNTL + 3, old_crtc_ext_disp, par);
  391. aty_st_8(CLOCK_CNTL + par->clk_wr_offset,
  392. old_clock_cntl | CLOCK_STROBE, par);
  393. mdelay(50); /* delay for 50 (15) ms */
  394. aty_st_8(CLOCK_CNTL + par->clk_wr_offset,
  395. ((pll->ics2595.locationAddr & 0x0F) | CLOCK_STROBE), par);
  396. return;
  397. }
  398. const struct aty_pll_ops aty_pll_ati18818_1 = {
  399. .var_to_pll = aty_var_to_pll_18818,
  400. .pll_to_var = aty_pll_18818_to_var,
  401. .set_pll = aty_set_pll18818,
  402. };
  403. /*
  404. * STG 1703 Clock Chip
  405. */
  406. static int aty_var_to_pll_1703(const struct fb_info *info, u32 vclk_per,
  407. u32 bpp, union aty_pll *pll)
  408. {
  409. u32 mhz100; /* in 0.01 MHz */
  410. u32 program_bits;
  411. /* u32 post_divider; */
  412. u32 mach64MinFreq, mach64MaxFreq, mach64RefFreq;
  413. u32 temp, tempB;
  414. u16 remainder, preRemainder;
  415. short divider = 0, tempA;
  416. /* Calculate the programming word */
  417. mhz100 = 100000000 / vclk_per;
  418. mach64MinFreq = MIN_FREQ_2595;
  419. mach64MaxFreq = MAX_FREQ_2595;
  420. mach64RefFreq = REF_FREQ_2595; /* 14.32 MHz */
  421. /* Calculate program word */
  422. if (mhz100 == 0)
  423. program_bits = 0xE0;
  424. else {
  425. if (mhz100 < mach64MinFreq)
  426. mhz100 = mach64MinFreq;
  427. if (mhz100 > mach64MaxFreq)
  428. mhz100 = mach64MaxFreq;
  429. divider = 0;
  430. while (mhz100 < (mach64MinFreq << 3)) {
  431. mhz100 <<= 1;
  432. divider += 0x20;
  433. }
  434. temp = (unsigned int) (mhz100);
  435. temp = (unsigned int) (temp * (MIN_N_1703 + 2));
  436. temp -= (short) (mach64RefFreq << 1);
  437. tempA = MIN_N_1703;
  438. preRemainder = 0xffff;
  439. do {
  440. tempB = temp;
  441. remainder = tempB % mach64RefFreq;
  442. tempB = tempB / mach64RefFreq;
  443. if ((tempB & 0xffff) <= 127
  444. && (remainder <= preRemainder)) {
  445. preRemainder = remainder;
  446. divider &= ~0x1f;
  447. divider |= tempA;
  448. divider =
  449. (divider & 0x00ff) +
  450. ((tempB & 0xff) << 8);
  451. }
  452. temp += mhz100;
  453. tempA++;
  454. } while (tempA <= (MIN_N_1703 << 1));
  455. program_bits = divider;
  456. }
  457. pll->ics2595.program_bits = program_bits;
  458. pll->ics2595.locationAddr = 0;
  459. pll->ics2595.post_divider = divider; /* fuer nix */
  460. pll->ics2595.period_in_ps = vclk_per;
  461. return 0;
  462. }
  463. static u32 aty_pll_1703_to_var(const struct fb_info *info,
  464. const union aty_pll *pll)
  465. {
  466. return (pll->ics2595.period_in_ps); /* default for now */
  467. }
  468. static void aty_set_pll_1703(const struct fb_info *info,
  469. const union aty_pll *pll)
  470. {
  471. struct atyfb_par *par = (struct atyfb_par *) info->par;
  472. u32 program_bits;
  473. u32 locationAddr;
  474. char old_crtc_ext_disp;
  475. old_crtc_ext_disp = aty_ld_8(CRTC_GEN_CNTL + 3, par);
  476. aty_st_8(CRTC_GEN_CNTL + 3,
  477. old_crtc_ext_disp | (CRTC_EXT_DISP_EN >> 24), par);
  478. program_bits = pll->ics2595.program_bits;
  479. locationAddr = pll->ics2595.locationAddr;
  480. /* Program clock */
  481. aty_dac_waste4(par);
  482. (void) aty_ld_8(DAC_REGS + 2, par);
  483. aty_st_8(DAC_REGS + 2, (locationAddr << 1) + 0x20, par);
  484. aty_st_8(DAC_REGS + 2, 0, par);
  485. aty_st_8(DAC_REGS + 2, (program_bits & 0xFF00) >> 8, par);
  486. aty_st_8(DAC_REGS + 2, (program_bits & 0xFF), par);
  487. (void) aty_ld_8(DAC_REGS, par); /* Clear DAC Counter */
  488. aty_st_8(CRTC_GEN_CNTL + 3, old_crtc_ext_disp, par);
  489. return;
  490. }
  491. const struct aty_pll_ops aty_pll_stg1703 = {
  492. .var_to_pll = aty_var_to_pll_1703,
  493. .pll_to_var = aty_pll_1703_to_var,
  494. .set_pll = aty_set_pll_1703,
  495. };
  496. /*
  497. * Chrontel 8398 Clock Chip
  498. */
  499. static int aty_var_to_pll_8398(const struct fb_info *info, u32 vclk_per,
  500. u32 bpp, union aty_pll *pll)
  501. {
  502. u32 tempA, tempB, fOut, longMHz100, diff, preDiff;
  503. u32 mhz100; /* in 0.01 MHz */
  504. u32 program_bits;
  505. /* u32 post_divider; */
  506. u32 mach64MinFreq, mach64MaxFreq, mach64RefFreq;
  507. u16 m, n, k = 0, save_m, save_n, twoToKth;
  508. /* Calculate the programming word */
  509. mhz100 = 100000000 / vclk_per;
  510. mach64MinFreq = MIN_FREQ_2595;
  511. mach64MaxFreq = MAX_FREQ_2595;
  512. mach64RefFreq = REF_FREQ_2595; /* 14.32 MHz */
  513. save_m = 0;
  514. save_n = 0;
  515. /* Calculate program word */
  516. if (mhz100 == 0)
  517. program_bits = 0xE0;
  518. else {
  519. if (mhz100 < mach64MinFreq)
  520. mhz100 = mach64MinFreq;
  521. if (mhz100 > mach64MaxFreq)
  522. mhz100 = mach64MaxFreq;
  523. longMHz100 = mhz100 * 256 / 100; /* 8 bit scale this */
  524. while (mhz100 < (mach64MinFreq << 3)) {
  525. mhz100 <<= 1;
  526. k++;
  527. }
  528. twoToKth = 1 << k;
  529. diff = 0;
  530. preDiff = 0xFFFFFFFF;
  531. for (m = MIN_M; m <= MAX_M; m++) {
  532. for (n = MIN_N; n <= MAX_N; n++) {
  533. tempA = 938356; /* 14.31818 * 65536 */
  534. tempA *= (n + 8); /* 43..256 */
  535. tempB = twoToKth * 256;
  536. tempB *= (m + 2); /* 4..32 */
  537. fOut = tempA / tempB; /* 8 bit scale */
  538. if (longMHz100 > fOut)
  539. diff = longMHz100 - fOut;
  540. else
  541. diff = fOut - longMHz100;
  542. if (diff < preDiff) {
  543. save_m = m;
  544. save_n = n;
  545. preDiff = diff;
  546. }
  547. }
  548. }
  549. program_bits = (k << 6) + (save_m) + (save_n << 8);
  550. }
  551. pll->ics2595.program_bits = program_bits;
  552. pll->ics2595.locationAddr = 0;
  553. pll->ics2595.post_divider = 0;
  554. pll->ics2595.period_in_ps = vclk_per;
  555. return 0;
  556. }
  557. static u32 aty_pll_8398_to_var(const struct fb_info *info,
  558. const union aty_pll *pll)
  559. {
  560. return (pll->ics2595.period_in_ps); /* default for now */
  561. }
  562. static void aty_set_pll_8398(const struct fb_info *info,
  563. const union aty_pll *pll)
  564. {
  565. struct atyfb_par *par = (struct atyfb_par *) info->par;
  566. u32 program_bits;
  567. u32 locationAddr;
  568. char old_crtc_ext_disp;
  569. char tmp;
  570. old_crtc_ext_disp = aty_ld_8(CRTC_GEN_CNTL + 3, par);
  571. aty_st_8(CRTC_GEN_CNTL + 3,
  572. old_crtc_ext_disp | (CRTC_EXT_DISP_EN >> 24), par);
  573. program_bits = pll->ics2595.program_bits;
  574. locationAddr = pll->ics2595.locationAddr;
  575. /* Program clock */
  576. tmp = aty_ld_8(DAC_CNTL, par);
  577. aty_st_8(DAC_CNTL, tmp | DAC_EXT_SEL_RS2 | DAC_EXT_SEL_RS3, par);
  578. aty_st_8(DAC_REGS, locationAddr, par);
  579. aty_st_8(DAC_REGS + 1, (program_bits & 0xff00) >> 8, par);
  580. aty_st_8(DAC_REGS + 1, (program_bits & 0xff), par);
  581. tmp = aty_ld_8(DAC_CNTL, par);
  582. aty_st_8(DAC_CNTL, (tmp & ~DAC_EXT_SEL_RS2) | DAC_EXT_SEL_RS3,
  583. par);
  584. (void) aty_ld_8(DAC_REGS, par); /* Clear DAC Counter */
  585. aty_st_8(CRTC_GEN_CNTL + 3, old_crtc_ext_disp, par);
  586. return;
  587. }
  588. const struct aty_pll_ops aty_pll_ch8398 = {
  589. .var_to_pll = aty_var_to_pll_8398,
  590. .pll_to_var = aty_pll_8398_to_var,
  591. .set_pll = aty_set_pll_8398,
  592. };
  593. /*
  594. * AT&T 20C408 Clock Chip
  595. */
  596. static int aty_var_to_pll_408(const struct fb_info *info, u32 vclk_per,
  597. u32 bpp, union aty_pll *pll)
  598. {
  599. u32 mhz100; /* in 0.01 MHz */
  600. u32 program_bits;
  601. /* u32 post_divider; */
  602. u32 mach64MinFreq, mach64MaxFreq, mach64RefFreq;
  603. u32 temp, tempB;
  604. u16 remainder, preRemainder;
  605. short divider = 0, tempA;
  606. /* Calculate the programming word */
  607. mhz100 = 100000000 / vclk_per;
  608. mach64MinFreq = MIN_FREQ_2595;
  609. mach64MaxFreq = MAX_FREQ_2595;
  610. mach64RefFreq = REF_FREQ_2595; /* 14.32 MHz */
  611. /* Calculate program word */
  612. if (mhz100 == 0)
  613. program_bits = 0xFF;
  614. else {
  615. if (mhz100 < mach64MinFreq)
  616. mhz100 = mach64MinFreq;
  617. if (mhz100 > mach64MaxFreq)
  618. mhz100 = mach64MaxFreq;
  619. while (mhz100 < (mach64MinFreq << 3)) {
  620. mhz100 <<= 1;
  621. divider += 0x40;
  622. }
  623. temp = (unsigned int) mhz100;
  624. temp = (unsigned int) (temp * (MIN_N_408 + 2));
  625. temp -= ((short) (mach64RefFreq << 1));
  626. tempA = MIN_N_408;
  627. preRemainder = 0xFFFF;
  628. do {
  629. tempB = temp;
  630. remainder = tempB % mach64RefFreq;
  631. tempB = tempB / mach64RefFreq;
  632. if (((tempB & 0xFFFF) <= 255)
  633. && (remainder <= preRemainder)) {
  634. preRemainder = remainder;
  635. divider &= ~0x3f;
  636. divider |= tempA;
  637. divider =
  638. (divider & 0x00FF) +
  639. ((tempB & 0xFF) << 8);
  640. }
  641. temp += mhz100;
  642. tempA++;
  643. } while (tempA <= 32);
  644. program_bits = divider;
  645. }
  646. pll->ics2595.program_bits = program_bits;
  647. pll->ics2595.locationAddr = 0;
  648. pll->ics2595.post_divider = divider; /* fuer nix */
  649. pll->ics2595.period_in_ps = vclk_per;
  650. return 0;
  651. }
  652. static u32 aty_pll_408_to_var(const struct fb_info *info,
  653. const union aty_pll *pll)
  654. {
  655. return (pll->ics2595.period_in_ps); /* default for now */
  656. }
  657. static void aty_set_pll_408(const struct fb_info *info,
  658. const union aty_pll *pll)
  659. {
  660. struct atyfb_par *par = (struct atyfb_par *) info->par;
  661. u32 program_bits;
  662. u32 locationAddr;
  663. u8 tmpA, tmpB, tmpC;
  664. char old_crtc_ext_disp;
  665. old_crtc_ext_disp = aty_ld_8(CRTC_GEN_CNTL + 3, par);
  666. aty_st_8(CRTC_GEN_CNTL + 3,
  667. old_crtc_ext_disp | (CRTC_EXT_DISP_EN >> 24), par);
  668. program_bits = pll->ics2595.program_bits;
  669. locationAddr = pll->ics2595.locationAddr;
  670. /* Program clock */
  671. aty_dac_waste4(par);
  672. tmpB = aty_ld_8(DAC_REGS + 2, par) | 1;
  673. aty_dac_waste4(par);
  674. aty_st_8(DAC_REGS + 2, tmpB, par);
  675. tmpA = tmpB;
  676. tmpC = tmpA;
  677. tmpA |= 8;
  678. tmpB = 1;
  679. aty_st_8(DAC_REGS, tmpB, par);
  680. aty_st_8(DAC_REGS + 2, tmpA, par);
  681. udelay(400); /* delay for 400 us */
  682. locationAddr = (locationAddr << 2) + 0x40;
  683. tmpB = locationAddr;
  684. tmpA = program_bits >> 8;
  685. aty_st_8(DAC_REGS, tmpB, par);
  686. aty_st_8(DAC_REGS + 2, tmpA, par);
  687. tmpB = locationAddr + 1;
  688. tmpA = (u8) program_bits;
  689. aty_st_8(DAC_REGS, tmpB, par);
  690. aty_st_8(DAC_REGS + 2, tmpA, par);
  691. tmpB = locationAddr + 2;
  692. tmpA = 0x77;
  693. aty_st_8(DAC_REGS, tmpB, par);
  694. aty_st_8(DAC_REGS + 2, tmpA, par);
  695. udelay(400); /* delay for 400 us */
  696. tmpA = tmpC & (~(1 | 8));
  697. tmpB = 1;
  698. aty_st_8(DAC_REGS, tmpB, par);
  699. aty_st_8(DAC_REGS + 2, tmpA, par);
  700. (void) aty_ld_8(DAC_REGS, par); /* Clear DAC Counter */
  701. aty_st_8(CRTC_GEN_CNTL + 3, old_crtc_ext_disp, par);
  702. return;
  703. }
  704. const struct aty_pll_ops aty_pll_att20c408 = {
  705. .var_to_pll = aty_var_to_pll_408,
  706. .pll_to_var = aty_pll_408_to_var,
  707. .set_pll = aty_set_pll_408,
  708. };
  709. /*
  710. * Unsupported DAC and Clock Chip
  711. */
  712. static int aty_set_dac_unsupported(const struct fb_info *info,
  713. const union aty_pll *pll, u32 bpp,
  714. u32 accel)
  715. {
  716. struct atyfb_par *par = (struct atyfb_par *) info->par;
  717. aty_st_le32(BUS_CNTL, 0x890e20f1, par);
  718. aty_st_le32(DAC_CNTL, 0x47052100, par);
  719. /* new in 2.2.3p1 from Geert. ???????? */
  720. aty_st_le32(BUS_CNTL, 0x590e10ff, par);
  721. aty_st_le32(DAC_CNTL, 0x47012100, par);
  722. return 0;
  723. }
  724. static int dummy(void)
  725. {
  726. return 0;
  727. }
  728. const struct aty_dac_ops aty_dac_unsupported = {
  729. .set_dac = aty_set_dac_unsupported,
  730. };
  731. const struct aty_pll_ops aty_pll_unsupported = {
  732. .var_to_pll = (void *) dummy,
  733. .pll_to_var = (void *) dummy,
  734. .set_pll = (void *) dummy,
  735. };