ct69000.c 39 KB

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