ct69000.c 40 KB

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