ct69000.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /* ported from ctfb.c (linux kernel):
  2. * Created in Jan - July 2000 by Thomas Höhenleitner <th@visuelle-maschinen.de>
  3. *
  4. * Ported to U-Boot:
  5. * (C) Copyright 2002 Denis Peter, MPL AG Switzerland
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #ifdef CONFIG_VIDEO
  11. #include <pci.h>
  12. #include <video_fb.h>
  13. #include "videomodes.h"
  14. /* debug */
  15. #undef VGA_DEBUG
  16. #undef VGA_DUMP_REG
  17. #ifdef VGA_DEBUG
  18. #undef _DEBUG
  19. #define _DEBUG 1
  20. #else
  21. #undef _DEBUG
  22. #define _DEBUG 0
  23. #endif
  24. /* Macros */
  25. #ifndef min
  26. #define min( a, b ) ( ( a ) < ( b ) ) ? ( a ) : ( b )
  27. #endif
  28. #ifndef max
  29. #define max( a, b ) ( ( a ) > ( b ) ) ? ( a ) : ( b )
  30. #endif
  31. #ifdef minmax
  32. #error "term minmax already used."
  33. #endif
  34. #define minmax( a, x, b ) max( ( a ), min( ( x ), ( b ) ) )
  35. #define N_ELTS( x ) ( sizeof( x ) / sizeof( x[ 0 ] ) )
  36. /* CT Register Offsets */
  37. #define CT_AR_O 0x3c0 /* Index and Data write port of the attribute Registers */
  38. #define CT_GR_O 0x3ce /* Index port of the Graphic Controller Registers */
  39. #define CT_SR_O 0x3c4 /* Index port of the Sequencer Controller */
  40. #define CT_CR_O 0x3d4 /* Index port of the CRT Controller */
  41. #define CT_XR_O 0x3d6 /* Extended Register index */
  42. #define CT_MSR_W_O 0x3c2 /* Misc. Output Register (write only) */
  43. #define CT_LUT_MASK_O 0x3c6 /* Color Palette Mask */
  44. #define CT_LUT_START_O 0x3c8 /* Color Palette Write Mode Index */
  45. #define CT_LUT_RGB_O 0x3c9 /* Color Palette Data Port */
  46. #define CT_STATUS_REG0_O 0x3c2 /* Status Register 0 (read only) */
  47. #define CT_STATUS_REG1_O 0x3da /* Input Status Register 1 (read only) */
  48. #define CT_FP_O 0x3d0 /* Index port of the Flat panel Registers */
  49. #define CT_MR_O 0x3d2 /* Index Port of the Multimedia Extension */
  50. /* defines for the memory mapped registers */
  51. #define BR00_o 0x400000 /* Source and Destination Span Register */
  52. #define BR01_o 0x400004 /* Pattern/Source Expansion Background Color & Transparency Key Register */
  53. #define BR02_o 0x400008 /* Pattern/Source Expansion Foreground Color Register */
  54. #define BR03_o 0x40000C /* Monochrome Source Control Register */
  55. #define BR04_o 0x400010 /* BitBLT Control Register */
  56. #define BR05_o 0x400014 /* Pattern Address Registe */
  57. #define BR06_o 0x400018 /* Source Address Register */
  58. #define BR07_o 0x40001C /* Destination Address Register */
  59. #define BR08_o 0x400020 /* Destination Width & Height Register */
  60. #define BR09_o 0x400024 /* Source Expansion Background Color & Transparency Key Register */
  61. #define BR0A_o 0x400028 /* Source Expansion Foreground Color Register */
  62. #define CURSOR_SIZE 0x1000 /* in KByte for HW Cursor */
  63. #define PATTERN_ADR (pGD->dprBase + CURSOR_SIZE) /* pattern Memory after Cursor Memory */
  64. #define PATTERN_SIZE 8*8*4 /* 4 Bytes per Pixel 8 x 8 Pixel */
  65. #define ACCELMEMORY (CURSOR_SIZE + PATTERN_SIZE) /* reserved Memory for BITBlt and hw cursor */
  66. /* Some Mode definitions */
  67. #define FB_SYNC_HOR_HIGH_ACT 1 /* horizontal sync high active */
  68. #define FB_SYNC_VERT_HIGH_ACT 2 /* vertical sync high active */
  69. #define FB_SYNC_EXT 4 /* external sync */
  70. #define FB_SYNC_COMP_HIGH_ACT 8 /* composite sync high active */
  71. #define FB_SYNC_BROADCAST 16 /* broadcast video timings */
  72. /* vtotal = 144d/288n/576i => PAL */
  73. /* vtotal = 121d/242n/484i => NTSC */
  74. #define FB_SYNC_ON_GREEN 32 /* sync on green */
  75. #define FB_VMODE_NONINTERLACED 0 /* non interlaced */
  76. #define FB_VMODE_INTERLACED 1 /* interlaced */
  77. #define FB_VMODE_DOUBLE 2 /* double scan */
  78. #define FB_VMODE_MASK 255
  79. #define FB_VMODE_YWRAP 256 /* ywrap instead of panning */
  80. #define FB_VMODE_SMOOTH_XPAN 512 /* smooth xpan possible (internally used) */
  81. #define FB_VMODE_CONUPDATE 512 /* don't update x/yoffset */
  82. #define text 0
  83. #define fntwidth 8
  84. /* table for VGA Initialization */
  85. typedef struct {
  86. const unsigned char reg;
  87. const unsigned char val;
  88. } CT_CFG_TABLE;
  89. /* this table provides some basic initialisations such as Memory Clock etc */
  90. static CT_CFG_TABLE xreg[] = {
  91. {0x09, 0x01}, /* CRT Controller Extensions Enable */
  92. {0x0A, 0x02}, /* Frame Buffer Mapping */
  93. {0x0B, 0x01}, /* PCI Write Burst support */
  94. {0x20, 0x00}, /* BitBLT Configuration */
  95. {0x40, 0x03}, /* Memory Access Control */
  96. {0x60, 0x00}, /* Video Pin Control */
  97. {0x61, 0x00}, /* DPMS Synch control */
  98. {0x62, 0x00}, /* GPIO Pin Control */
  99. {0x63, 0xBD}, /* GPIO Pin Data */
  100. {0x67, 0x00}, /* Pin Tri-State */
  101. {0x80, 0x80}, /* Pixel Pipeline Config 0 register */
  102. {0xA0, 0x00}, /* Cursor 1 Control Reg */
  103. {0xA1, 0x00}, /* Cursor 1 Vertical Extension Reg */
  104. {0xA2, 0x00}, /* Cursor 1 Base Address Low */
  105. {0xA3, 0x00}, /* Cursor 1 Base Address High */
  106. {0xA4, 0x00}, /* Cursor 1 X-Position Low */
  107. {0xA5, 0x00}, /* Cursor 1 X-Position High */
  108. {0xA6, 0x00}, /* Cursor 1 Y-Position Low */
  109. {0xA7, 0x00}, /* Cursor 1 Y-Position High */
  110. {0xA8, 0x00}, /* Cursor 2 Control Reg */
  111. {0xA9, 0x00}, /* Cursor 2 Vertical Extension Reg */
  112. {0xAA, 0x00}, /* Cursor 2 Base Address Low */
  113. {0xAB, 0x00}, /* Cursor 2 Base Address High */
  114. {0xAC, 0x00}, /* Cursor 2 X-Position Low */
  115. {0xAD, 0x00}, /* Cursor 2 X-Position High */
  116. {0xAE, 0x00}, /* Cursor 2 Y-Position Low */
  117. {0xAF, 0x00}, /* Cursor 2 Y-Position High */
  118. {0xC0, 0x7D}, /* Dot Clock 0 VCO M-Divisor */
  119. {0xC1, 0x07}, /* Dot Clock 0 VCO N-Divisor */
  120. {0xC3, 0x34}, /* Dot Clock 0 Divisor select */
  121. {0xC4, 0x55}, /* Dot Clock 1 VCO M-Divisor */
  122. {0xC5, 0x09}, /* Dot Clock 1 VCO N-Divisor */
  123. {0xC7, 0x24}, /* Dot Clock 1 Divisor select */
  124. {0xC8, 0x7D}, /* Dot Clock 2 VCO M-Divisor */
  125. {0xC9, 0x07}, /* Dot Clock 2 VCO N-Divisor */
  126. {0xCB, 0x34}, /* Dot Clock 2 Divisor select */
  127. {0xCC, 0x38}, /* Memory Clock 0 VCO M-Divisor */
  128. {0xCD, 0x03}, /* Memory Clock 0 VCO N-Divisor */
  129. {0xCE, 0x90}, /* Memory Clock 0 Divisor select */
  130. {0xCF, 0x06}, /* Clock Config */
  131. {0xD0, 0x0F}, /* Power Down */
  132. {0xD1, 0x01}, /* Power Down BitBLT */
  133. {0xFF, 0xFF} /* end of table */
  134. };
  135. /* Clock Config:
  136. * =============
  137. *
  138. * PD Registers:
  139. * -------------
  140. * Bit2 and Bit4..6 are used for the Loop Divisor and Post Divisor.
  141. * They are encoded as follows:
  142. *
  143. * +---+--------------+
  144. * | 2 | Loop Divisor |
  145. * +---+--------------+
  146. * | 1 | 1 |
  147. * +---+--------------+
  148. * | 0 | 4 |
  149. * +---+--------------+
  150. * Note: The Memory Clock does not have a Loop Divisor.
  151. * +---+---+---+--------------+
  152. * | 6 | 5 | 4 | Post Divisor |
  153. * +---+---+---+--------------+
  154. * | 0 | 0 | 0 | 1 |
  155. * +---+---+---+--------------+
  156. * | 0 | 0 | 1 | 2 |
  157. * +---+---+---+--------------+
  158. * | 0 | 1 | 0 | 4 |
  159. * +---+---+---+--------------+
  160. * | 0 | 1 | 1 | 8 |
  161. * +---+---+---+--------------+
  162. * | 1 | 0 | 0 | 16 |
  163. * +---+---+---+--------------+
  164. * | 1 | 0 | 1 | 32 |
  165. * +---+---+---+--------------+
  166. * | 1 | 1 | X | reserved |
  167. * +---+---+---+--------------+
  168. *
  169. * All other bits are reserved in these registers.
  170. *
  171. * Clock VCO M Registers:
  172. * ----------------------
  173. * These Registers contain the M Value -2.
  174. *
  175. * Clock VCO N Registers:
  176. * ----------------------
  177. * These Registers contain the N Value -2.
  178. *
  179. * Formulas:
  180. * ---------
  181. * Fvco = (Fref * Loop Divisor * M/N), whereas 100MHz < Fvco < 220MHz
  182. * Fout = Fvco / Post Divisor
  183. *
  184. * Dot Clk0 (default 25MHz):
  185. * -------------------------
  186. * Fvco = 14.318 * 127 / 9 = 202.045MHz
  187. * Fout = 202.045MHz / 8 = 25.25MHz
  188. * Post Divisor = 8
  189. * Loop Divisor = 1
  190. * XRC0 = (M - 2) = 125 = 0x7D
  191. * XRC1 = (N - 2) = 7 = 0x07
  192. * XRC3 = 0x34
  193. *
  194. * Dot Clk1 (default 28MHz):
  195. * -------------------------
  196. * Fvco = 14.318 * 87 / 11 = 113.24MHz
  197. * Fout = 113.24MHz / 4 = 28.31MHz
  198. * Post Divisor = 4
  199. * Loop Divisor = 1
  200. * XRC4 = (M - 2) = 85 = 0x55
  201. * XRC5 = (N - 2) = 9 = 0x09
  202. * XRC7 = 0x24
  203. *
  204. * Dot Clk2 (variable for extended modes set to 25MHz):
  205. * ----------------------------------------------------
  206. * Fvco = 14.318 * 127 / 9 = 202.045MHz
  207. * Fout = 202.045MHz / 8 = 25.25MHz
  208. * Post Divisor = 8
  209. * Loop Divisor = 1
  210. * XRC8 = (M - 2) = 125 = 0x7D
  211. * XRC9 = (N - 2) = 7 = 0x07
  212. * XRCB = 0x34
  213. *
  214. * Memory Clk for most modes >50MHz:
  215. * ----------------------------------
  216. * Fvco = 14.318 * 58 / 5 = 166MHz
  217. * Fout = 166MHz / 2 = 83MHz
  218. * Post Divisor = 2
  219. * XRCC = (M - 2) = 57 = 0x38
  220. * XRCD = (N - 2) = 3 = 0x03
  221. * XRCE = 0x90
  222. *
  223. * Note Bit7 enables the clock source from the VCO
  224. *
  225. */
  226. /*******************************************************************
  227. * Chips struct
  228. *******************************************************************/
  229. struct ctfb_chips_properties {
  230. int device_id; /* PCI Device ID */
  231. unsigned long max_mem; /* memory for frame buffer */
  232. int vld_set; /* value of VLD if bit2 in clock control is set */
  233. int vld_not_set; /* value of VLD if bit2 in clock control is set */
  234. int mn_diff; /* difference between M/N Value + mn_diff = M/N Register */
  235. int mn_min; /* min value of M/N Value */
  236. int mn_max; /* max value of M/N Value */
  237. int vco_min; /* VCO Min in MHz */
  238. int vco_max; /* VCO Max in MHz */
  239. };
  240. static const struct ctfb_chips_properties chips[] = {
  241. {PCI_DEVICE_ID_CT_69000, 0x200000, 1, 4, -2, 3, 257, 100, 220},
  242. #ifdef CONFIG_USE_CPCIDVI
  243. {PCI_DEVICE_ID_CT_69030, 0x400000, 1, 4, -2, 3, 257, 100, 220},
  244. #endif
  245. {PCI_DEVICE_ID_CT_65555, 0x100000, 16, 4, 0, 1, 255, 48, 220}, /* NOT TESTED */
  246. {0, 0, 0, 0, 0, 0, 0, 0, 0} /* Terminator */
  247. };
  248. /*
  249. * The Graphic Device
  250. */
  251. GraphicDevice ctfb;
  252. /*******************************************************************************
  253. *
  254. * Low Level Routines
  255. */
  256. /*******************************************************************************
  257. *
  258. * Read CT ISA register
  259. */
  260. #ifdef VGA_DEBUG
  261. static unsigned char
  262. ctRead (unsigned short index)
  263. {
  264. GraphicDevice *pGD = (GraphicDevice *) & ctfb;
  265. if (index == CT_AR_O)
  266. /* synch the Flip Flop */
  267. in8 (pGD->isaBase + CT_STATUS_REG1_O);
  268. return (in8 (pGD->isaBase + index));
  269. }
  270. #endif
  271. /*******************************************************************************
  272. *
  273. * Write CT ISA register
  274. */
  275. static void
  276. ctWrite (unsigned short index, unsigned char val)
  277. {
  278. GraphicDevice *pGD = (GraphicDevice *) & ctfb;
  279. out8 ((pGD->isaBase + index), val);
  280. }
  281. /*******************************************************************************
  282. *
  283. * Read CT ISA register indexed
  284. */
  285. static unsigned char
  286. ctRead_i (unsigned short index, char reg)
  287. {
  288. GraphicDevice *pGD = (GraphicDevice *) & ctfb;
  289. if (index == CT_AR_O)
  290. /* synch the Flip Flop */
  291. in8 (pGD->isaBase + CT_STATUS_REG1_O);
  292. out8 ((pGD->isaBase + index), reg);
  293. return (in8 (pGD->isaBase + index + 1));
  294. }
  295. /*******************************************************************************
  296. *
  297. * Write CT ISA register indexed
  298. */
  299. static void
  300. ctWrite_i (unsigned short index, char reg, char val)
  301. {
  302. GraphicDevice *pGD = (GraphicDevice *) & ctfb;
  303. if (index == CT_AR_O) {
  304. /* synch the Flip Flop */
  305. in8 (pGD->isaBase + CT_STATUS_REG1_O);
  306. out8 ((pGD->isaBase + index), reg);
  307. out8 ((pGD->isaBase + index), val);
  308. } else {
  309. out8 ((pGD->isaBase + index), reg);
  310. out8 ((pGD->isaBase + index + 1), val);
  311. }
  312. }
  313. /*******************************************************************************
  314. *
  315. * Write a table of CT ISA register
  316. */
  317. static void
  318. ctLoadRegs (unsigned short index, CT_CFG_TABLE * regTab)
  319. {
  320. while (regTab->reg != 0xFF) {
  321. ctWrite_i (index, regTab->reg, regTab->val);
  322. regTab++;
  323. }
  324. }
  325. /*****************************************************************************/
  326. static void
  327. SetArRegs (void)
  328. {
  329. int i, tmp;
  330. for (i = 0; i < 0x10; i++)
  331. ctWrite_i (CT_AR_O, i, i);
  332. if (text)
  333. tmp = 0x04;
  334. else
  335. tmp = 0x41;
  336. ctWrite_i (CT_AR_O, 0x10, tmp); /* Mode Control Register */
  337. ctWrite_i (CT_AR_O, 0x11, 0x00); /* Overscan Color Register */
  338. ctWrite_i (CT_AR_O, 0x12, 0x0f); /* Memory Plane Enable Register */
  339. if (fntwidth == 9)
  340. tmp = 0x08;
  341. else
  342. tmp = 0x00;
  343. ctWrite_i (CT_AR_O, 0x13, tmp); /* Horizontal Pixel Panning */
  344. ctWrite_i (CT_AR_O, 0x14, 0x00); /* Color Select Register */
  345. ctWrite (CT_AR_O, 0x20); /* enable video */
  346. }
  347. /*****************************************************************************/
  348. static void
  349. SetGrRegs (void)
  350. { /* Set Graphics Mode */
  351. int i;
  352. for (i = 0; i < 0x05; i++)
  353. ctWrite_i (CT_GR_O, i, 0);
  354. if (text) {
  355. ctWrite_i (CT_GR_O, 0x05, 0x10);
  356. ctWrite_i (CT_GR_O, 0x06, 0x02);
  357. } else {
  358. ctWrite_i (CT_GR_O, 0x05, 0x40);
  359. ctWrite_i (CT_GR_O, 0x06, 0x05);
  360. }
  361. ctWrite_i (CT_GR_O, 0x07, 0x0f);
  362. ctWrite_i (CT_GR_O, 0x08, 0xff);
  363. }
  364. /*****************************************************************************/
  365. static void
  366. SetSrRegs (void)
  367. {
  368. int tmp = 0;
  369. ctWrite_i (CT_SR_O, 0x00, 0x00); /* reset */
  370. /*rr( sr, 0x01, tmp );
  371. if( fntwidth == 8 ) tmp |= 0x01; else tmp &= ~0x01;
  372. wr( sr, 0x01, tmp ); */
  373. if (fntwidth == 8)
  374. ctWrite_i (CT_SR_O, 0x01, 0x01); /* Clocking Mode Register */
  375. else
  376. ctWrite_i (CT_SR_O, 0x01, 0x00); /* Clocking Mode Register */
  377. ctWrite_i (CT_SR_O, 0x02, 0x0f); /* Enable CPU wr access to given memory plane */
  378. ctWrite_i (CT_SR_O, 0x03, 0x00); /* Character Map Select Register */
  379. if (text)
  380. tmp = 0x02;
  381. else
  382. tmp = 0x0e;
  383. ctWrite_i (CT_SR_O, 0x04, tmp); /* Enable CPU accesses to the rest of the 256KB
  384. total VGA memory beyond the first 64KB and set
  385. fb mapping mode. */
  386. ctWrite_i (CT_SR_O, 0x00, 0x03); /* enable */
  387. }
  388. /*****************************************************************************/
  389. static void
  390. SetBitsPerPixelIntoXrRegs (int bpp)
  391. {
  392. unsigned int n = (bpp >> 3), tmp; /* only for 15, 8, 16, 24 bpp */
  393. static char md[4] = { 0x04, 0x02, 0x05, 0x06 }; /* DisplayColorMode */
  394. static char off[4] = { ~0x20, ~0x30, ~0x20, ~0x10 }; /* mask */
  395. static char on[4] = { 0x10, 0x00, 0x10, 0x20 }; /* mask */
  396. if (bpp == 15)
  397. n = 0;
  398. tmp = ctRead_i (CT_XR_O, 0x20);
  399. tmp &= off[n];
  400. tmp |= on[n];
  401. ctWrite_i (CT_XR_O, 0x20, tmp); /* BitBLT Configuration */
  402. ctWrite_i (CT_XR_O, 0x81, md[n]);
  403. }
  404. /*****************************************************************************/
  405. static void
  406. SetCrRegs (struct ctfb_res_modes *var, int bits_per_pixel)
  407. { /* he -le- ht|0 hd -ri- hs -h- he */
  408. unsigned char cr[0x7a];
  409. int i, tmp;
  410. unsigned int hd, hs, he, ht, hbe; /* Horizontal. */
  411. unsigned int vd, vs, ve, vt; /* vertical */
  412. unsigned int bpp, wd, dblscan, interlaced, bcast, CrtHalfLine;
  413. unsigned int CompSyncCharClkDelay, CompSyncPixelClkDelay;
  414. unsigned int NTSC_PAL_HorizontalPulseWidth, BlDelayCtrl;
  415. unsigned int HorizontalEqualizationPulses;
  416. unsigned int HorizontalSerration1Start, HorizontalSerration2Start;
  417. const int LineCompare = 0x3ff;
  418. unsigned int TextScanLines = 1; /* this is in fact a vertical zoom factor */
  419. unsigned int RAMDAC_BlankPedestalEnable = 0; /* 1=en-, 0=disable, see XR82 */
  420. hd = (var->xres) / 8; /* HDisp. */
  421. hs = (var->xres + var->right_margin) / 8; /* HsStrt */
  422. he = (var->xres + var->right_margin + var->hsync_len) / 8; /* HsEnd */
  423. ht = (var->left_margin + var->xres + var->right_margin + var->hsync_len) / 8; /* HTotal */
  424. hbe = ht - 1; /* HBlankEnable todo docu wants ht here, but it does not work */
  425. /* ve -up- vt|0 vd -lo- vs -v- ve */
  426. vd = var->yres; /* VDisplay */
  427. vs = var->yres + var->lower_margin; /* VSyncStart */
  428. ve = var->yres + var->lower_margin + var->vsync_len; /* VSyncEnd */
  429. vt = var->upper_margin + var->yres + var->lower_margin + var->vsync_len; /* VTotal */
  430. bpp = bits_per_pixel;
  431. dblscan = (var->vmode & FB_VMODE_DOUBLE) ? 1 : 0;
  432. interlaced = var->vmode & FB_VMODE_INTERLACED;
  433. bcast = var->sync & FB_SYNC_BROADCAST;
  434. CrtHalfLine = bcast ? (hd >> 1) : 0;
  435. BlDelayCtrl = bcast ? 1 : 0;
  436. CompSyncCharClkDelay = 0; /* 2 bit */
  437. CompSyncPixelClkDelay = 0; /* 3 bit */
  438. if (bcast) {
  439. NTSC_PAL_HorizontalPulseWidth = 7; /*( var->hsync_len >> 1 ) + 1 */
  440. HorizontalEqualizationPulses = 0; /* inverse value */
  441. HorizontalSerration1Start = 31; /* ( ht >> 1 ) */
  442. HorizontalSerration2Start = 89; /* ( ht >> 1 ) */
  443. } else {
  444. NTSC_PAL_HorizontalPulseWidth = 0;
  445. /* 4 bit: hsync pulse width = ( ( CR74[4:0] - CR74[5] )
  446. * / 2 ) + 1 --> CR74[4:0] = 2*(hs-1) + CR74[5] */
  447. HorizontalEqualizationPulses = 1; /* inverse value */
  448. HorizontalSerration1Start = 0; /* ( ht >> 1 ) */
  449. HorizontalSerration2Start = 0; /* ( ht >> 1 ) */
  450. }
  451. if (bpp == 15)
  452. bpp = 16;
  453. wd = var->xres * bpp / 64; /* double words per line */
  454. if (interlaced) { /* we divide all vertical timings, exept vd */
  455. vs >>= 1;
  456. ve >>= 1;
  457. vt >>= 1;
  458. }
  459. memset (cr, 0, sizeof (cr));
  460. cr[0x00] = 0xff & (ht - 5);
  461. cr[0x01] = hd - 1; /* soll:4f ist 59 */
  462. cr[0x02] = hd;
  463. cr[0x03] = (hbe & 0x1F) | 0x80; /* hd + ht - hd */
  464. cr[0x04] = hs;
  465. cr[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
  466. cr[0x06] = (vt - 2) & 0xFF;
  467. cr[0x30] = (vt - 2) >> 8;
  468. cr[0x07] = ((vt & 0x100) >> 8)
  469. | ((vd & 0x100) >> 7)
  470. | ((vs & 0x100) >> 6)
  471. | ((vs & 0x100) >> 5)
  472. | ((LineCompare & 0x100) >> 4)
  473. | ((vt & 0x200) >> 4)
  474. | ((vd & 0x200) >> 3)
  475. | ((vs & 0x200) >> 2);
  476. cr[0x08] = 0x00;
  477. cr[0x09] = (dblscan << 7)
  478. | ((LineCompare & 0x200) >> 3)
  479. | ((vs & 0x200) >> 4)
  480. | (TextScanLines - 1);
  481. cr[0x10] = vs & 0xff; /* VSyncPulseStart */
  482. cr[0x32] = (vs & 0xf00) >> 8; /* VSyncPulseStart */
  483. cr[0x11] = (ve & 0x0f); /* | 0x20; */
  484. cr[0x12] = (vd - 1) & 0xff; /* LineCount */
  485. cr[0x31] = ((vd - 1) & 0xf00) >> 8; /* LineCount */
  486. cr[0x13] = wd & 0xff;
  487. cr[0x41] = (wd & 0xf00) >> 8;
  488. cr[0x15] = vs & 0xff;
  489. cr[0x33] = (vs & 0xf00) >> 8;
  490. cr[0x38] = (0x100 & (ht - 5)) >> 8;
  491. cr[0x3C] = 0xc0 & hbe;
  492. cr[0x16] = (vt - 1) & 0xff; /* vbe - docu wants vt here, */
  493. cr[0x17] = 0xe3; /* but it does not work */
  494. cr[0x18] = 0xff & LineCompare;
  495. cr[0x22] = 0xff; /* todo? */
  496. cr[0x70] = interlaced ? (0x80 | CrtHalfLine) : 0x00; /* check:0xa6 */
  497. cr[0x71] = 0x80 | (RAMDAC_BlankPedestalEnable << 6)
  498. | (BlDelayCtrl << 5)
  499. | ((0x03 & CompSyncCharClkDelay) << 3)
  500. | (0x07 & CompSyncPixelClkDelay); /* todo: see XR82 */
  501. cr[0x72] = HorizontalSerration1Start;
  502. cr[0x73] = HorizontalSerration2Start;
  503. cr[0x74] = (HorizontalEqualizationPulses << 5)
  504. | NTSC_PAL_HorizontalPulseWidth;
  505. /* todo: ct69000 has also 0x75-79 */
  506. /* now set the registers */
  507. for (i = 0; i <= 0x0d; i++) { /*CR00 .. CR0D */
  508. ctWrite_i (CT_CR_O, i, cr[i]);
  509. }
  510. for (i = 0x10; i <= 0x18; i++) { /*CR10 .. CR18 */
  511. ctWrite_i (CT_CR_O, i, cr[i]);
  512. }
  513. i = 0x22; /*CR22 */
  514. ctWrite_i (CT_CR_O, i, cr[i]);
  515. for (i = 0x30; i <= 0x33; i++) { /*CR30 .. CR33 */
  516. ctWrite_i (CT_CR_O, i, cr[i]);
  517. }
  518. i = 0x38; /*CR38 */
  519. ctWrite_i (CT_CR_O, i, cr[i]);
  520. i = 0x3C; /*CR3C */
  521. ctWrite_i (CT_CR_O, i, cr[i]);
  522. for (i = 0x40; i <= 0x41; i++) { /*CR40 .. CR41 */
  523. ctWrite_i (CT_CR_O, i, cr[i]);
  524. }
  525. for (i = 0x70; i <= 0x74; i++) { /*CR70 .. CR74 */
  526. ctWrite_i (CT_CR_O, i, cr[i]);
  527. }
  528. tmp = ctRead_i (CT_CR_O, 0x40);
  529. tmp &= 0x0f;
  530. tmp |= 0x80;
  531. ctWrite_i (CT_CR_O, 0x40, tmp); /* StartAddressEnable */
  532. }
  533. /* pixelclock control */
  534. /*****************************************************************************
  535. We have a rational number p/q and need an m/n which is very close to p/q
  536. but has m and n within mnmin and mnmax. We have no floating point in the
  537. kernel. We can use long long without divide. And we have time to compute...
  538. ******************************************************************************/
  539. static unsigned int
  540. FindBestPQFittingMN (unsigned int p, unsigned int q, unsigned int mnmin,
  541. unsigned int mnmax, unsigned int *pm, unsigned int *pn)
  542. {
  543. /* this code is not for general purpose usable but good for our number ranges */
  544. unsigned int n = mnmin, m = 0;
  545. long long int L = 0, P = p, Q = q, H = P >> 1;
  546. long long int D = 0x7ffffffffffffffLL;
  547. for (n = mnmin; n <= mnmax; n++) {
  548. m = mnmin; /* p/q ~ m/n -> p*n ~ m*q -> p*n-x*q ~ 0 */
  549. L = P * n - m * Q; /* n * vco - m * fref should be near 0 */
  550. while (L > 0 && m < mnmax) {
  551. L -= q; /* difference is greater as 0 subtract fref */
  552. m++; /* and increment m */
  553. }
  554. /* difference is less or equal than 0 or m > maximum */
  555. if (m > mnmax)
  556. break; /* no solution: if we increase n we get the same situation */
  557. /* L is <= 0 now */
  558. if (-L > H && m > mnmin) { /* if difference > the half fref */
  559. L += q; /* we take the situation before */
  560. m--; /* because its closer to 0 */
  561. }
  562. L = (L < 0) ? -L : +L; /* absolute value */
  563. if (D < L) /* if last difference was better take next n */
  564. continue;
  565. D = L;
  566. *pm = m;
  567. *pn = n; /* keep improved data */
  568. if (D == 0)
  569. break; /* best result we can get */
  570. }
  571. return (unsigned int) (0xffffffff & D);
  572. }
  573. /* that is the hardware < 69000 we have to manage
  574. +---------+ +-------------------+ +----------------------+ +--+
  575. | REFCLK |__|NTSC Divisor Select|__|FVCO Reference Divisor|__|÷N|__
  576. | 14.3MHz | |(NTSCDS) (÷1, ÷5) | |Select (RDS) (÷1, ÷4) | | | |
  577. +---------+ +-------------------+ +----------------------+ +--+ |
  578. ___________________________________________________________________|
  579. |
  580. | fvco fout
  581. | +--------+ +------------+ +-----+ +-------------------+ +----+
  582. +-| Phase |__|Charge Pump |__| VCO |_____|Post Divisor (PD) |___|CLK |--->
  583. +-| Detect | |& Filter VCO| | | | |÷1, 2, 4, 8, 16, 32| | |
  584. | +--------+ +------------+ +-----+ | +-------------------+ +----+
  585. | |
  586. | +--+ +---------------+ |
  587. |____|÷M|___|VCO Loop Divide|__________|
  588. | | |(VLD)(÷4, ÷16) |
  589. +--+ +---------------+
  590. ****************************************************************************
  591. that is the hardware >= 69000 we have to manage
  592. +---------+ +--+
  593. | REFCLK |__|÷N|__
  594. | 14.3MHz | | | |
  595. +---------+ +--+ |
  596. __________________|
  597. |
  598. | fvco fout
  599. | +--------+ +------------+ +-----+ +-------------------+ +----+
  600. +-| Phase |__|Charge Pump |__| VCO |_____|Post Divisor (PD) |___|CLK |--->
  601. +-| Detect | |& Filter VCO| | | | |÷1, 2, 4, 8, 16, 32| | |
  602. | +--------+ +------------+ +-----+ | +-------------------+ +----+
  603. | |
  604. | +--+ +---------------+ |
  605. |____|÷M|___|VCO Loop Divide|__________|
  606. | | |(VLD)(÷1, ÷4) |
  607. +--+ +---------------+
  608. */
  609. #define VIDEO_FREF 14318180; /* Hz */
  610. /*****************************************************************************/
  611. static int
  612. ReadPixClckFromXrRegsBack (struct ctfb_chips_properties *param)
  613. {
  614. unsigned int m, n, vld, pd, PD, fref, xr_cb, i, pixclock;
  615. i = 0;
  616. pixclock = -1;
  617. fref = VIDEO_FREF;
  618. m = ctRead_i (CT_XR_O, 0xc8);
  619. n = ctRead_i (CT_XR_O, 0xc9);
  620. m -= param->mn_diff;
  621. n -= param->mn_diff;
  622. xr_cb = ctRead_i (CT_XR_O, 0xcb);
  623. PD = (0x70 & xr_cb) >> 4;
  624. pd = 1;
  625. for (i = 0; i < PD; i++) {
  626. pd *= 2;
  627. }
  628. vld = (0x04 & xr_cb) ? param->vld_set : param->vld_not_set;
  629. if (n * vld * m) {
  630. unsigned long long p = 1000000000000LL * pd * n;
  631. unsigned long long q = (long long) fref * vld * m;
  632. while ((p > 0xffffffffLL) || (q > 0xffffffffLL)) {
  633. p >>= 1; /* can't divide with long long so we scale down */
  634. q >>= 1;
  635. }
  636. pixclock = (unsigned) p / (unsigned) q;
  637. } else
  638. printf ("Invalid data in xr regs.\n");
  639. return pixclock;
  640. }
  641. /*****************************************************************************/
  642. static void
  643. FindAndSetPllParamIntoXrRegs (unsigned int pixelclock,
  644. struct ctfb_chips_properties *param)
  645. {
  646. unsigned int m, n, vld, pd, PD, fref, xr_cb;
  647. unsigned int fvcomin, fvcomax, pclckmin, pclckmax, pclk;
  648. unsigned int pfreq, fvco, new_pixclock;
  649. unsigned int D,nback,mback;
  650. fref = VIDEO_FREF;
  651. pd = 1;
  652. PD = 0;
  653. fvcomin = param->vco_min;
  654. fvcomax = param->vco_max; /* MHz */
  655. pclckmin = 1000000 / fvcomax + 1; /* 4546 */
  656. pclckmax = 32000000 / fvcomin - 1; /* 666665 */
  657. pclk = minmax (pclckmin, pixelclock, pclckmax); /* ps pp */
  658. pfreq = 250 * (4000000000U / pclk);
  659. fvco = pfreq; /* Hz */
  660. new_pixclock = 0;
  661. while (fvco < fvcomin * 1000000) {
  662. /* double VCO starting with the pixelclock frequency
  663. * as long as it is lower than the minimal VCO frequency */
  664. fvco *= 2;
  665. pd *= 2;
  666. PD++;
  667. }
  668. /* fvco is exactly pd * pixelclock and higher than the ninmal VCO frequency */
  669. /* first try */
  670. vld = param->vld_set;
  671. D=FindBestPQFittingMN (fvco / vld, fref, param->mn_min, param->mn_max, &m, &n); /* rds = 1 */
  672. mback=m;
  673. nback=n;
  674. /* second try */
  675. vld = param->vld_not_set;
  676. if(D<FindBestPQFittingMN (fvco / vld, fref, param->mn_min, param->mn_max, &m, &n)) { /* rds = 1 */
  677. /* first try was better */
  678. m=mback;
  679. n=nback;
  680. vld = param->vld_set;
  681. }
  682. m += param->mn_diff;
  683. n += param->mn_diff;
  684. debug("VCO %d, pd %d, m %d n %d vld %d\n", fvco, pd, m, n, vld);
  685. xr_cb = ((0x7 & PD) << 4) | (vld == param->vld_set ? 0x04 : 0);
  686. /* All four of the registers used for dot clock 2 (XRC8 - XRCB) must be
  687. * written, and in order from XRC8 to XRCB, before the hardware will
  688. * update the synthesizer s settings.
  689. */
  690. ctWrite_i (CT_XR_O, 0xc8, m);
  691. ctWrite_i (CT_XR_O, 0xc9, n); /* xrca does not exist in CT69000 and CT69030 */
  692. ctWrite_i (CT_XR_O, 0xca, 0); /* because of a hw bug I guess, but we write */
  693. ctWrite_i (CT_XR_O, 0xcb, xr_cb); /* 0 to it for savety */
  694. new_pixclock = ReadPixClckFromXrRegsBack (param);
  695. debug("pixelclock.set = %d, pixelclock.real = %d\n",
  696. pixelclock, new_pixclock);
  697. }
  698. /*****************************************************************************/
  699. static void
  700. SetMsrRegs (struct ctfb_res_modes *mode)
  701. {
  702. unsigned char h_synch_high, v_synch_high;
  703. h_synch_high = (mode->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 0x40; /* horizontal Synch High active */
  704. v_synch_high = (mode->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 0x80; /* vertical Synch High active */
  705. ctWrite (CT_MSR_W_O, (h_synch_high | v_synch_high | 0x29));
  706. /* upper64K==0x20, CLC2select==0x08, RAMenable==0x02!(todo), CGA==0x01
  707. * Selects the upper 64KB page.Bit5=1
  708. * CLK2 (left reserved in standard VGA) Bit3|2=1|0
  709. * Disables CPU access to frame buffer. Bit1=0
  710. * Sets the I/O address decode for ST01, FCR, and all CR registers
  711. * to the 3Dx I/O address range (CGA emulation). Bit0=1
  712. */
  713. }
  714. /************************************************************************************/
  715. #ifdef VGA_DUMP_REG
  716. static void
  717. ctDispRegs (unsigned short index, int from, int to)
  718. {
  719. unsigned char status;
  720. int i;
  721. for (i = from; i < to; i++) {
  722. status = ctRead_i (index, i);
  723. printf ("%02X: is %02X\n", i, status);
  724. }
  725. }
  726. void
  727. video_dump_reg (void)
  728. {
  729. int i;
  730. printf ("Extended Regs:\n");
  731. ctDispRegs (CT_XR_O, 0, 0xC);
  732. ctDispRegs (CT_XR_O, 0xe, 0xf);
  733. ctDispRegs (CT_XR_O, 0x20, 0x21);
  734. ctDispRegs (CT_XR_O, 0x40, 0x50);
  735. ctDispRegs (CT_XR_O, 0x60, 0x64);
  736. ctDispRegs (CT_XR_O, 0x67, 0x68);
  737. ctDispRegs (CT_XR_O, 0x70, 0x72);
  738. ctDispRegs (CT_XR_O, 0x80, 0x83);
  739. ctDispRegs (CT_XR_O, 0xA0, 0xB0);
  740. ctDispRegs (CT_XR_O, 0xC0, 0xD3);
  741. printf ("Sequencer Regs:\n");
  742. ctDispRegs (CT_SR_O, 0, 0x8);
  743. printf ("Graphic Regs:\n");
  744. ctDispRegs (CT_GR_O, 0, 0x9);
  745. printf ("CRT Regs:\n");
  746. ctDispRegs (CT_CR_O, 0, 0x19);
  747. ctDispRegs (CT_CR_O, 0x22, 0x23);
  748. ctDispRegs (CT_CR_O, 0x30, 0x34);
  749. ctDispRegs (CT_CR_O, 0x38, 0x39);
  750. ctDispRegs (CT_CR_O, 0x3C, 0x3D);
  751. ctDispRegs (CT_CR_O, 0x40, 0x42);
  752. ctDispRegs (CT_CR_O, 0x70, 0x80);
  753. /* don't display the attributes */
  754. }
  755. #endif
  756. #ifdef CONFIG_VIDEO_HW_CURSOR
  757. /***************************************************************
  758. * Set Hardware Cursor in Pixel
  759. */
  760. void
  761. video_set_hw_cursor (int x, int y)
  762. {
  763. int sig_x = 0, sig_y = 0;
  764. if (x < 0) {
  765. x *= -1;
  766. sig_x = 1;
  767. }
  768. if (y < 0) {
  769. y *= -1;
  770. sig_y = 1;
  771. }
  772. ctWrite_i (CT_XR_O, 0xa4, x & 0xff);
  773. ctWrite_i (CT_XR_O, 0xa5, (x >> 8) & 0x7);
  774. ctWrite_i (CT_XR_O, 0xa6, y & 0xff);
  775. ctWrite_i (CT_XR_O, 0xa7, (y >> 8) & 0x7);
  776. }
  777. /***************************************************************
  778. * Init Hardware Cursor. To know the size of the Cursor,
  779. * we have to know the Font size.
  780. */
  781. void
  782. video_init_hw_cursor (int font_width, int font_height)
  783. {
  784. unsigned char xr_80;
  785. unsigned long *curs, pattern;
  786. int i;
  787. int cursor_start;
  788. GraphicDevice *pGD = (GraphicDevice *) & ctfb;
  789. cursor_start = pGD->dprBase;
  790. xr_80 = ctRead_i (CT_XR_O, 0x80);
  791. /* set start address */
  792. ctWrite_i (CT_XR_O, 0xa2, (cursor_start >> 8) & 0xf0);
  793. ctWrite_i (CT_XR_O, 0xa3, (cursor_start >> 16) & 0x3f);
  794. /* set cursor shape */
  795. curs = (unsigned long *) cursor_start;
  796. i = 0;
  797. while (i < 0x400) {
  798. curs[i++] = 0xffffffff; /* AND mask */
  799. curs[i++] = 0xffffffff; /* AND mask */
  800. curs[i++] = 0; /* XOR mask */
  801. curs[i++] = 0; /* XOR mask */
  802. /* Transparent */
  803. }
  804. pattern = 0xffffffff >> font_width;
  805. i = 0;
  806. while (i < (font_height * 2)) {
  807. curs[i++] = pattern; /* AND mask */
  808. curs[i++] = pattern; /* AND mask */
  809. curs[i++] = 0; /* XOR mask */
  810. curs[i++] = 0; /* XOR mask */
  811. /* Cursor Color 0 */
  812. }
  813. /* set blink rate */
  814. ctWrite_i (CT_FP_O, 0x19, 0xf);
  815. /* set cursors colors */
  816. xr_80 = ctRead_i (CT_XR_O, 0x80);
  817. xr_80 |= 0x1; /* alternate palette select */
  818. ctWrite_i (CT_XR_O, 0x80, xr_80);
  819. video_set_lut (4, CONSOLE_FG_COL, CONSOLE_FG_COL, CONSOLE_FG_COL);
  820. /* position 4 is color 0 cursor 0 */
  821. xr_80 &= 0xfe; /* normal palette select */
  822. ctWrite_i (CT_XR_O, 0x80, xr_80);
  823. /* cursor enable */
  824. ctWrite_i (CT_XR_O, 0xa0, 0x91);
  825. xr_80 |= 0x10; /* enable hwcursor */
  826. ctWrite_i (CT_XR_O, 0x80, xr_80);
  827. video_set_hw_cursor (0, 0);
  828. }
  829. #endif /* CONFIG_VIDEO_HW_CURSOR */
  830. /***************************************************************
  831. * Wait for BitBlt ready
  832. */
  833. static int
  834. video_wait_bitblt (unsigned long addr)
  835. {
  836. unsigned long br04;
  837. int i = 0;
  838. br04 = in32r (addr);
  839. while (br04 & 0x80000000) {
  840. udelay (1);
  841. br04 = in32r (addr);
  842. if (i++ > 1000000) {
  843. printf ("ERROR Timeout %lx\n", br04);
  844. return 1;
  845. }
  846. }
  847. return 0;
  848. }
  849. /***************************************************************
  850. * Set up BitBlt Registrs
  851. */
  852. static void
  853. SetDrawingEngine (int bits_per_pixel)
  854. {
  855. unsigned long br04, br00;
  856. unsigned char tmp;
  857. GraphicDevice *pGD = (GraphicDevice *) & ctfb;
  858. tmp = ctRead_i (CT_XR_O, 0x20); /* BitBLT Configuration */
  859. tmp |= 0x02; /* reset BitBLT */
  860. ctWrite_i (CT_XR_O, 0x20, tmp); /* BitBLT Configuration */
  861. udelay (10);
  862. tmp &= 0xfd; /* release reset BitBLT */
  863. ctWrite_i (CT_XR_O, 0x20, tmp); /* BitBLT Configuration */
  864. video_wait_bitblt (pGD->pciBase + BR04_o);
  865. /* set pattern Address */
  866. out32r (pGD->pciBase + BR05_o, PATTERN_ADR & 0x003ffff8);
  867. br04 = 0;
  868. if (bits_per_pixel == 1) {
  869. br04 |= 0x00040000; /* monochome Pattern */
  870. br04 |= 0x00001000; /* monochome source */
  871. }
  872. br00 = ((pGD->winSizeX * pGD->gdfBytesPP) << 16) + (pGD->winSizeX * pGD->gdfBytesPP); /* bytes per scanline */
  873. out32r (pGD->pciBase + BR00_o, br00); /* */
  874. out32r (pGD->pciBase + BR08_o, (10 << 16) + 10); /* dummy */
  875. out32r (pGD->pciBase + BR04_o, br04); /* write all 0 */
  876. out32r (pGD->pciBase + BR07_o, 0); /* destination */
  877. video_wait_bitblt (pGD->pciBase + BR04_o);
  878. }
  879. /****************************************************************************
  880. * supported Video Chips
  881. */
  882. static struct pci_device_id supported[] = {
  883. {PCI_VENDOR_ID_CT, PCI_DEVICE_ID_CT_69000},
  884. #ifdef CONFIG_USE_CPCIDVI
  885. {PCI_VENDOR_ID_CT, PCI_DEVICE_ID_CT_69030},
  886. #endif
  887. {}
  888. };
  889. /*******************************************************************************
  890. *
  891. * Init video chip
  892. */
  893. void *
  894. video_hw_init (void)
  895. {
  896. GraphicDevice *pGD = (GraphicDevice *) & ctfb;
  897. unsigned short device_id;
  898. pci_dev_t devbusfn;
  899. int videomode;
  900. unsigned long t1, hsynch, vsynch;
  901. unsigned int pci_mem_base, *vm;
  902. int tmp, i, bits_per_pixel;
  903. char *penv;
  904. struct ctfb_res_modes *res_mode;
  905. struct ctfb_res_modes var_mode;
  906. struct ctfb_chips_properties *chips_param;
  907. /* Search for video chip */
  908. if ((devbusfn = pci_find_devices (supported, 0)) < 0) {
  909. #ifdef CONFIG_VIDEO_ONBOARD
  910. printf ("Video: Controller not found !\n");
  911. #endif
  912. return (NULL);
  913. }
  914. /* PCI setup */
  915. pci_write_config_dword (devbusfn, PCI_COMMAND,
  916. (PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
  917. pci_read_config_word (devbusfn, PCI_DEVICE_ID, &device_id);
  918. pci_read_config_dword (devbusfn, PCI_BASE_ADDRESS_0, &pci_mem_base);
  919. pci_mem_base = pci_mem_to_phys (devbusfn, pci_mem_base);
  920. /* get chips params */
  921. for (chips_param = (struct ctfb_chips_properties *) &chips[0];
  922. chips_param->device_id != 0; chips_param++) {
  923. if (chips_param->device_id == device_id)
  924. break;
  925. }
  926. if (chips_param->device_id == 0) {
  927. #ifdef CONFIG_VIDEO_ONBOARD
  928. printf ("Video: controller 0x%X not supported\n", device_id);
  929. #endif
  930. return NULL;
  931. }
  932. /* supported Video controller found */
  933. printf ("Video: ");
  934. tmp = 0;
  935. videomode = 0x301;
  936. /* get video mode via environment */
  937. if ((penv = getenv ("videomode")) != NULL) {
  938. /* deceide if it is a string */
  939. if (penv[0] <= '9') {
  940. videomode = (int) simple_strtoul (penv, NULL, 16);
  941. tmp = 1;
  942. }
  943. } else {
  944. tmp = 1;
  945. }
  946. if (tmp) {
  947. /* parameter are vesa modes */
  948. /* search params */
  949. for (i = 0; i < VESA_MODES_COUNT; i++) {
  950. if (vesa_modes[i].vesanr == videomode)
  951. break;
  952. }
  953. if (i == VESA_MODES_COUNT) {
  954. printf ("no VESA Mode found, switching to mode 0x301 ");
  955. i = 0;
  956. }
  957. res_mode =
  958. (struct ctfb_res_modes *) &res_mode_init[vesa_modes[i].
  959. resindex];
  960. bits_per_pixel = vesa_modes[i].bits_per_pixel;
  961. } else {
  962. res_mode = (struct ctfb_res_modes *) &var_mode;
  963. bits_per_pixel = video_get_params (res_mode, penv);
  964. }
  965. /* calculate available color depth for controller memory */
  966. if (bits_per_pixel == 15)
  967. tmp = 2;
  968. else
  969. tmp = bits_per_pixel >> 3; /* /8 */
  970. if (((chips_param->max_mem -
  971. ACCELMEMORY) / (res_mode->xres * res_mode->yres)) < tmp) {
  972. tmp =
  973. ((chips_param->max_mem -
  974. ACCELMEMORY) / (res_mode->xres * res_mode->yres));
  975. if (tmp == 0) {
  976. printf
  977. ("No matching videomode found .-> reduce resolution\n");
  978. return NULL;
  979. } else {
  980. printf ("Switching back to %d Bits per Pixel ",
  981. tmp << 3);
  982. bits_per_pixel = tmp << 3;
  983. }
  984. }
  985. /* calculate hsynch and vsynch freq (info only) */
  986. t1 = (res_mode->left_margin + res_mode->xres +
  987. res_mode->right_margin + res_mode->hsync_len) / 8;
  988. t1 *= 8;
  989. t1 *= res_mode->pixclock;
  990. t1 /= 1000;
  991. hsynch = 1000000000L / t1;
  992. t1 *=
  993. (res_mode->upper_margin + res_mode->yres +
  994. res_mode->lower_margin + res_mode->vsync_len);
  995. t1 /= 1000;
  996. vsynch = 1000000000L / t1;
  997. /* fill in Graphic device struct */
  998. sprintf (pGD->modeIdent, "%dx%dx%d %ldkHz %ldHz", res_mode->xres,
  999. res_mode->yres, bits_per_pixel, (hsynch / 1000),
  1000. (vsynch / 1000));
  1001. printf ("%s\n", pGD->modeIdent);
  1002. pGD->winSizeX = res_mode->xres;
  1003. pGD->winSizeY = res_mode->yres;
  1004. pGD->plnSizeX = res_mode->xres;
  1005. pGD->plnSizeY = res_mode->yres;
  1006. switch (bits_per_pixel) {
  1007. case 8:
  1008. pGD->gdfBytesPP = 1;
  1009. pGD->gdfIndex = GDF__8BIT_INDEX;
  1010. break;
  1011. case 15:
  1012. pGD->gdfBytesPP = 2;
  1013. pGD->gdfIndex = GDF_15BIT_555RGB;
  1014. break;
  1015. case 16:
  1016. pGD->gdfBytesPP = 2;
  1017. pGD->gdfIndex = GDF_16BIT_565RGB;
  1018. break;
  1019. case 24:
  1020. pGD->gdfBytesPP = 3;
  1021. pGD->gdfIndex = GDF_24BIT_888RGB;
  1022. break;
  1023. }
  1024. pGD->isaBase = CONFIG_SYS_ISA_IO_BASE_ADDRESS;
  1025. pGD->pciBase = pci_mem_base;
  1026. pGD->frameAdrs = pci_mem_base;
  1027. pGD->memSize = chips_param->max_mem;
  1028. /* Cursor Start Address */
  1029. pGD->dprBase =
  1030. (pGD->winSizeX * pGD->winSizeY * pGD->gdfBytesPP) + pci_mem_base;
  1031. if ((pGD->dprBase & 0x0fff) != 0) {
  1032. /* allign it */
  1033. pGD->dprBase &= 0xfffff000;
  1034. pGD->dprBase += 0x00001000;
  1035. }
  1036. debug("Cursor Start %x Pattern Start %x\n", pGD->dprBase,
  1037. PATTERN_ADR);
  1038. pGD->vprBase = pci_mem_base; /* Dummy */
  1039. pGD->cprBase = pci_mem_base; /* Dummy */
  1040. /* set up Hardware */
  1041. #ifdef CONFIG_USE_CPCIDVI
  1042. if (device_id == PCI_DEVICE_ID_CT_69030) {
  1043. ctWrite (CT_MSR_W_O, 0x0b);
  1044. ctWrite (0x3cd, 0x13);
  1045. ctWrite_i (CT_FP_O, 0x02, 0x00);
  1046. ctWrite_i (CT_FP_O, 0x05, 0x00);
  1047. ctWrite_i (CT_FP_O, 0x06, 0x00);
  1048. ctWrite (0x3c2, 0x0b);
  1049. ctWrite_i (CT_FP_O, 0x02, 0x10);
  1050. ctWrite_i (CT_FP_O, 0x01, 0x09);
  1051. } else {
  1052. ctWrite (CT_MSR_W_O, 0x01);
  1053. }
  1054. #else
  1055. ctWrite (CT_MSR_W_O, 0x01);
  1056. #endif
  1057. /* set the extended Registers */
  1058. ctLoadRegs (CT_XR_O, xreg);
  1059. /* set atribute registers */
  1060. SetArRegs ();
  1061. /* set Graphics register */
  1062. SetGrRegs ();
  1063. /* set sequencer */
  1064. SetSrRegs ();
  1065. /* set msr */
  1066. SetMsrRegs (res_mode);
  1067. /* set CRT Registers */
  1068. SetCrRegs (res_mode, bits_per_pixel);
  1069. /* set color mode */
  1070. SetBitsPerPixelIntoXrRegs (bits_per_pixel);
  1071. /* set PLL */
  1072. FindAndSetPllParamIntoXrRegs (res_mode->pixclock, chips_param);
  1073. ctWrite_i (CT_SR_O, 0, 0x03); /* clear synchronous reset */
  1074. /* Clear video memory */
  1075. i = pGD->memSize / 4;
  1076. vm = (unsigned int *) pGD->pciBase;
  1077. while (i--)
  1078. *vm++ = 0;
  1079. SetDrawingEngine (bits_per_pixel);
  1080. #ifdef VGA_DUMP_REG
  1081. video_dump_reg ();
  1082. #endif
  1083. return ((void *) &ctfb);
  1084. }
  1085. /*******************************************************************************
  1086. *
  1087. * Set a RGB color in the LUT (8 bit index)
  1088. */
  1089. void
  1090. video_set_lut (unsigned int index, /* color number */
  1091. unsigned char r, /* red */
  1092. unsigned char g, /* green */
  1093. unsigned char b /* blue */
  1094. )
  1095. {
  1096. ctWrite (CT_LUT_MASK_O, 0xff);
  1097. ctWrite (CT_LUT_START_O, (char) index);
  1098. ctWrite (CT_LUT_RGB_O, r); /* red */
  1099. ctWrite (CT_LUT_RGB_O, g); /* green */
  1100. ctWrite (CT_LUT_RGB_O, b); /* blue */
  1101. udelay (1);
  1102. ctWrite (CT_LUT_MASK_O, 0xff);
  1103. }
  1104. /*******************************************************************************
  1105. *
  1106. * Drawing engine fill on screen region
  1107. */
  1108. void
  1109. video_hw_rectfill (unsigned int bpp, /* bytes per pixel */
  1110. unsigned int dst_x, /* dest pos x */
  1111. unsigned int dst_y, /* dest pos y */
  1112. unsigned int dim_x, /* frame width */
  1113. unsigned int dim_y, /* frame height */
  1114. unsigned int color /* fill color */
  1115. )
  1116. {
  1117. GraphicDevice *pGD = (GraphicDevice *) & ctfb;
  1118. unsigned long *p, br04;
  1119. video_wait_bitblt (pGD->pciBase + BR04_o);
  1120. p = (unsigned long *) PATTERN_ADR;
  1121. dim_x *= bpp;
  1122. if (bpp == 3)
  1123. bpp++; /* 24Bit needs a 32bit pattern */
  1124. memset (p, color, (bpp * sizeof (unsigned char) * 8 * 8)); /* 8 x 8 pattern data */
  1125. out32r (pGD->pciBase + BR07_o, ((pGD->winSizeX * dst_y) + dst_x) * pGD->gdfBytesPP); /* destination */
  1126. br04 = in32r (pGD->pciBase + BR04_o) & 0xffffff00;
  1127. br04 |= 0xF0; /* write Pattern P -> D */
  1128. out32r (pGD->pciBase + BR04_o, br04); /* */
  1129. out32r (pGD->pciBase + BR08_o, (dim_y << 16) + dim_x); /* starts the BITBlt */
  1130. video_wait_bitblt (pGD->pciBase + BR04_o);
  1131. }
  1132. /*******************************************************************************
  1133. *
  1134. * Drawing engine bitblt with screen region
  1135. */
  1136. void
  1137. video_hw_bitblt (unsigned int bpp, /* bytes per pixel */
  1138. unsigned int src_x, /* source pos x */
  1139. unsigned int src_y, /* source pos y */
  1140. unsigned int dst_x, /* dest pos x */
  1141. unsigned int dst_y, /* dest pos y */
  1142. unsigned int dim_x, /* frame width */
  1143. unsigned int dim_y /* frame height */
  1144. )
  1145. {
  1146. GraphicDevice *pGD = (GraphicDevice *) & ctfb;
  1147. unsigned long br04;
  1148. br04 = in32r (pGD->pciBase + BR04_o);
  1149. /* to prevent data corruption due to overlap, we have to
  1150. * find out if, and how the frames overlaps */
  1151. if (src_x < dst_x) {
  1152. /* src is more left than dest
  1153. * the frame may overlap -> start from right to left */
  1154. br04 |= 0x00000100; /* set bit 8 */
  1155. src_x += dim_x;
  1156. dst_x += dim_x;
  1157. } else {
  1158. br04 &= 0xfffffeff; /* clear bit 8 left to right */
  1159. }
  1160. if (src_y < dst_y) {
  1161. /* src is higher than dst
  1162. * the frame may overlap => start from bottom */
  1163. br04 |= 0x00000200; /* set bit 9 */
  1164. src_y += dim_y;
  1165. dst_y += dim_y;
  1166. } else {
  1167. br04 &= 0xfffffdff; /* clear bit 9 top to bottom */
  1168. }
  1169. dim_x *= bpp;
  1170. out32r (pGD->pciBase + BR06_o, ((pGD->winSizeX * src_y) + src_x) * pGD->gdfBytesPP); /* source */
  1171. out32r (pGD->pciBase + BR07_o, ((pGD->winSizeX * dst_y) + dst_x) * pGD->gdfBytesPP); /* destination */
  1172. br04 &= 0xffffff00;
  1173. br04 |= 0x000000CC; /* S -> D */
  1174. out32r (pGD->pciBase + BR04_o, br04); /* */
  1175. out32r (pGD->pciBase + BR08_o, (dim_y << 16) + dim_x); /* start the BITBlt */
  1176. video_wait_bitblt (pGD->pciBase + BR04_o);
  1177. }
  1178. #endif /* CONFIG_VIDEO */