ct69000.c 47 KB

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