pm2fb.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. /*
  2. * Permedia2 framebuffer driver.
  3. *
  4. * 2.5/2.6 driver:
  5. * Copyright (c) 2003 Jim Hague (jim.hague@acm.org)
  6. *
  7. * based on 2.4 driver:
  8. * Copyright (c) 1998-2000 Ilario Nardinocchi (nardinoc@CS.UniBO.IT)
  9. * Copyright (c) 1999 Jakub Jelinek (jakub@redhat.com)
  10. *
  11. * and additional input from James Simmon's port of Hannu Mallat's tdfx
  12. * driver.
  13. *
  14. * I have a Creative Graphics Blaster Exxtreme card - pm2fb on x86. I
  15. * have no access to other pm2fb implementations. Sparc (and thus
  16. * hopefully other big-endian) devices now work, thanks to a lot of
  17. * testing work by Ron Murray. I have no access to CVision hardware,
  18. * and therefore for now I am omitting the CVision code.
  19. *
  20. * Multiple boards support has been on the TODO list for ages.
  21. * Don't expect this to change.
  22. *
  23. * This file is subject to the terms and conditions of the GNU General Public
  24. * License. See the file COPYING in the main directory of this archive for
  25. * more details.
  26. *
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/kernel.h>
  32. #include <linux/errno.h>
  33. #include <linux/string.h>
  34. #include <linux/mm.h>
  35. #include <linux/slab.h>
  36. #include <linux/delay.h>
  37. #include <linux/fb.h>
  38. #include <linux/init.h>
  39. #include <linux/pci.h>
  40. #include <video/permedia2.h>
  41. #include <video/cvisionppc.h>
  42. #if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
  43. #error "The endianness of the target host has not been defined."
  44. #endif
  45. #if !defined(CONFIG_PCI)
  46. #error "Only generic PCI cards supported."
  47. #endif
  48. #undef PM2FB_MASTER_DEBUG
  49. #ifdef PM2FB_MASTER_DEBUG
  50. #define DPRINTK(a,b...) printk(KERN_DEBUG "pm2fb: %s: " a, __FUNCTION__ , ## b)
  51. #else
  52. #define DPRINTK(a,b...)
  53. #endif
  54. /*
  55. * Driver data
  56. */
  57. static char *mode __devinitdata = NULL;
  58. /*
  59. * The XFree GLINT driver will (I think to implement hardware cursor
  60. * support on TVP4010 and similar where there is no RAMDAC - see
  61. * comment in set_video) always request +ve sync regardless of what
  62. * the mode requires. This screws me because I have a Sun
  63. * fixed-frequency monitor which absolutely has to have -ve sync. So
  64. * these flags allow the user to specify that requests for +ve sync
  65. * should be silently turned in -ve sync.
  66. */
  67. static int lowhsync;
  68. static int lowvsync;
  69. /*
  70. * The hardware state of the graphics card that isn't part of the
  71. * screeninfo.
  72. */
  73. struct pm2fb_par
  74. {
  75. pm2type_t type; /* Board type */
  76. unsigned char __iomem *v_regs;/* virtual address of p_regs */
  77. u32 memclock; /* memclock */
  78. u32 video; /* video flags before blanking */
  79. u32 mem_config; /* MemConfig reg at probe */
  80. u32 mem_control; /* MemControl reg at probe */
  81. u32 boot_address; /* BootAddress reg at probe */
  82. u32 palette[16];
  83. };
  84. /*
  85. * Here we define the default structs fb_fix_screeninfo and fb_var_screeninfo
  86. * if we don't use modedb.
  87. */
  88. static struct fb_fix_screeninfo pm2fb_fix __devinitdata = {
  89. .id = "",
  90. .type = FB_TYPE_PACKED_PIXELS,
  91. .visual = FB_VISUAL_PSEUDOCOLOR,
  92. .xpanstep = 1,
  93. .ypanstep = 1,
  94. .ywrapstep = 0,
  95. .accel = FB_ACCEL_3DLABS_PERMEDIA2,
  96. };
  97. /*
  98. * Default video mode. In case the modedb doesn't work.
  99. */
  100. static struct fb_var_screeninfo pm2fb_var __devinitdata = {
  101. /* "640x480, 8 bpp @ 60 Hz */
  102. .xres = 640,
  103. .yres = 480,
  104. .xres_virtual = 640,
  105. .yres_virtual = 480,
  106. .bits_per_pixel =8,
  107. .red = {0, 8, 0},
  108. .blue = {0, 8, 0},
  109. .green = {0, 8, 0},
  110. .activate = FB_ACTIVATE_NOW,
  111. .height = -1,
  112. .width = -1,
  113. .accel_flags = 0,
  114. .pixclock = 39721,
  115. .left_margin = 40,
  116. .right_margin = 24,
  117. .upper_margin = 32,
  118. .lower_margin = 11,
  119. .hsync_len = 96,
  120. .vsync_len = 2,
  121. .vmode = FB_VMODE_NONINTERLACED
  122. };
  123. /*
  124. * Utility functions
  125. */
  126. static inline u32 RD32(unsigned char __iomem *base, s32 off)
  127. {
  128. return fb_readl(base + off);
  129. }
  130. static inline void WR32(unsigned char __iomem *base, s32 off, u32 v)
  131. {
  132. fb_writel(v, base + off);
  133. }
  134. static inline u32 pm2_RD(struct pm2fb_par* p, s32 off)
  135. {
  136. return RD32(p->v_regs, off);
  137. }
  138. static inline void pm2_WR(struct pm2fb_par* p, s32 off, u32 v)
  139. {
  140. WR32(p->v_regs, off, v);
  141. }
  142. static inline u32 pm2_RDAC_RD(struct pm2fb_par* p, s32 idx)
  143. {
  144. int index = PM2R_RD_INDEXED_DATA;
  145. switch (p->type) {
  146. case PM2_TYPE_PERMEDIA2:
  147. pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
  148. break;
  149. case PM2_TYPE_PERMEDIA2V:
  150. pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
  151. index = PM2VR_RD_INDEXED_DATA;
  152. break;
  153. }
  154. mb();
  155. return pm2_RD(p, index);
  156. }
  157. static inline void pm2_RDAC_WR(struct pm2fb_par* p, s32 idx, u32 v)
  158. {
  159. int index = PM2R_RD_INDEXED_DATA;
  160. switch (p->type) {
  161. case PM2_TYPE_PERMEDIA2:
  162. pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, idx);
  163. break;
  164. case PM2_TYPE_PERMEDIA2V:
  165. pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
  166. index = PM2VR_RD_INDEXED_DATA;
  167. break;
  168. }
  169. mb();
  170. pm2_WR(p, index, v);
  171. }
  172. static inline void pm2v_RDAC_WR(struct pm2fb_par* p, s32 idx, u32 v)
  173. {
  174. pm2_WR(p, PM2VR_RD_INDEX_LOW, idx & 0xff);
  175. mb();
  176. pm2_WR(p, PM2VR_RD_INDEXED_DATA, v);
  177. }
  178. #ifdef CONFIG_FB_PM2_FIFO_DISCONNECT
  179. #define WAIT_FIFO(p,a)
  180. #else
  181. static inline void WAIT_FIFO(struct pm2fb_par* p, u32 a)
  182. {
  183. while( pm2_RD(p, PM2R_IN_FIFO_SPACE) < a );
  184. mb();
  185. }
  186. #endif
  187. /*
  188. * partial products for the supported horizontal resolutions.
  189. */
  190. #define PACKPP(p0,p1,p2) (((p2) << 6) | ((p1) << 3) | (p0))
  191. static const struct {
  192. u16 width;
  193. u16 pp;
  194. } pp_table[] = {
  195. { 32, PACKPP(1, 0, 0) }, { 64, PACKPP(1, 1, 0) },
  196. { 96, PACKPP(1, 1, 1) }, { 128, PACKPP(2, 1, 1) },
  197. { 160, PACKPP(2, 2, 1) }, { 192, PACKPP(2, 2, 2) },
  198. { 224, PACKPP(3, 2, 1) }, { 256, PACKPP(3, 2, 2) },
  199. { 288, PACKPP(3, 3, 1) }, { 320, PACKPP(3, 3, 2) },
  200. { 384, PACKPP(3, 3, 3) }, { 416, PACKPP(4, 3, 1) },
  201. { 448, PACKPP(4, 3, 2) }, { 512, PACKPP(4, 3, 3) },
  202. { 544, PACKPP(4, 4, 1) }, { 576, PACKPP(4, 4, 2) },
  203. { 640, PACKPP(4, 4, 3) }, { 768, PACKPP(4, 4, 4) },
  204. { 800, PACKPP(5, 4, 1) }, { 832, PACKPP(5, 4, 2) },
  205. { 896, PACKPP(5, 4, 3) }, { 1024, PACKPP(5, 4, 4) },
  206. { 1056, PACKPP(5, 5, 1) }, { 1088, PACKPP(5, 5, 2) },
  207. { 1152, PACKPP(5, 5, 3) }, { 1280, PACKPP(5, 5, 4) },
  208. { 1536, PACKPP(5, 5, 5) }, { 1568, PACKPP(6, 5, 1) },
  209. { 1600, PACKPP(6, 5, 2) }, { 1664, PACKPP(6, 5, 3) },
  210. { 1792, PACKPP(6, 5, 4) }, { 2048, PACKPP(6, 5, 5) },
  211. { 0, 0 } };
  212. static u32 partprod(u32 xres)
  213. {
  214. int i;
  215. for (i = 0; pp_table[i].width && pp_table[i].width != xres; i++)
  216. ;
  217. if ( pp_table[i].width == 0 )
  218. DPRINTK("invalid width %u\n", xres);
  219. return pp_table[i].pp;
  220. }
  221. static u32 to3264(u32 timing, int bpp, int is64)
  222. {
  223. switch (bpp) {
  224. case 8:
  225. timing >>= 2 + is64;
  226. break;
  227. case 16:
  228. timing >>= 1 + is64;
  229. break;
  230. case 24:
  231. timing = (timing * 3) >> (2 + is64);
  232. break;
  233. case 32:
  234. if (is64)
  235. timing >>= 1;
  236. break;
  237. }
  238. return timing;
  239. }
  240. static void pm2_mnp(u32 clk, unsigned char* mm, unsigned char* nn,
  241. unsigned char* pp)
  242. {
  243. unsigned char m;
  244. unsigned char n;
  245. unsigned char p;
  246. u32 f;
  247. s32 curr;
  248. s32 delta = 100000;
  249. *mm = *nn = *pp = 0;
  250. for (n = 2; n < 15; n++) {
  251. for (m = 2; m; m++) {
  252. f = PM2_REFERENCE_CLOCK * m / n;
  253. if (f >= 150000 && f <= 300000) {
  254. for ( p = 0; p < 5; p++, f >>= 1) {
  255. curr = ( clk > f ) ? clk - f : f - clk;
  256. if ( curr < delta ) {
  257. delta=curr;
  258. *mm=m;
  259. *nn=n;
  260. *pp=p;
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }
  267. static void pm2v_mnp(u32 clk, unsigned char* mm, unsigned char* nn,
  268. unsigned char* pp)
  269. {
  270. unsigned char m;
  271. unsigned char n;
  272. unsigned char p;
  273. u32 f;
  274. s32 delta = 1000;
  275. *mm = *nn = *pp = 0;
  276. for ( m = 1; m < 128; m++) {
  277. for (n = 2 * m + 1; n; n++) {
  278. for ( p = 0; p < 2; p++) {
  279. f = ( PM2_REFERENCE_CLOCK >> ( p + 1 )) * n / m;
  280. if ( clk > f - delta && clk < f + delta ) {
  281. delta = ( clk > f ) ? clk - f : f - clk;
  282. *mm=m;
  283. *nn=n;
  284. *pp=p;
  285. }
  286. }
  287. }
  288. }
  289. }
  290. static void clear_palette(struct pm2fb_par* p) {
  291. int i=256;
  292. WAIT_FIFO(p, 1);
  293. pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, 0);
  294. wmb();
  295. while (i--) {
  296. WAIT_FIFO(p, 3);
  297. pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
  298. pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
  299. pm2_WR(p, PM2R_RD_PALETTE_DATA, 0);
  300. }
  301. }
  302. static void reset_card(struct pm2fb_par* p)
  303. {
  304. if (p->type == PM2_TYPE_PERMEDIA2V)
  305. pm2_WR(p, PM2VR_RD_INDEX_HIGH, 0);
  306. pm2_WR(p, PM2R_RESET_STATUS, 0);
  307. mb();
  308. while (pm2_RD(p, PM2R_RESET_STATUS) & PM2F_BEING_RESET)
  309. ;
  310. mb();
  311. #ifdef CONFIG_FB_PM2_FIFO_DISCONNECT
  312. DPRINTK("FIFO disconnect enabled\n");
  313. pm2_WR(p, PM2R_FIFO_DISCON, 1);
  314. mb();
  315. #endif
  316. /* Restore stashed memory config information from probe */
  317. WAIT_FIFO(p, 3);
  318. pm2_WR(p, PM2R_MEM_CONTROL, p->mem_control);
  319. pm2_WR(p, PM2R_BOOT_ADDRESS, p->boot_address);
  320. wmb();
  321. pm2_WR(p, PM2R_MEM_CONFIG, p->mem_config);
  322. }
  323. static void reset_config(struct pm2fb_par* p)
  324. {
  325. WAIT_FIFO(p, 52);
  326. pm2_WR(p, PM2R_CHIP_CONFIG, pm2_RD(p, PM2R_CHIP_CONFIG)&
  327. ~(PM2F_VGA_ENABLE|PM2F_VGA_FIXED));
  328. pm2_WR(p, PM2R_BYPASS_WRITE_MASK, ~(0L));
  329. pm2_WR(p, PM2R_FRAMEBUFFER_WRITE_MASK, ~(0L));
  330. pm2_WR(p, PM2R_FIFO_CONTROL, 0);
  331. pm2_WR(p, PM2R_APERTURE_ONE, 0);
  332. pm2_WR(p, PM2R_APERTURE_TWO, 0);
  333. pm2_WR(p, PM2R_RASTERIZER_MODE, 0);
  334. pm2_WR(p, PM2R_DELTA_MODE, PM2F_DELTA_ORDER_RGB);
  335. pm2_WR(p, PM2R_LB_READ_FORMAT, 0);
  336. pm2_WR(p, PM2R_LB_WRITE_FORMAT, 0);
  337. pm2_WR(p, PM2R_LB_READ_MODE, 0);
  338. pm2_WR(p, PM2R_LB_SOURCE_OFFSET, 0);
  339. pm2_WR(p, PM2R_FB_SOURCE_OFFSET, 0);
  340. pm2_WR(p, PM2R_FB_PIXEL_OFFSET, 0);
  341. pm2_WR(p, PM2R_FB_WINDOW_BASE, 0);
  342. pm2_WR(p, PM2R_LB_WINDOW_BASE, 0);
  343. pm2_WR(p, PM2R_FB_SOFT_WRITE_MASK, ~(0L));
  344. pm2_WR(p, PM2R_FB_HARD_WRITE_MASK, ~(0L));
  345. pm2_WR(p, PM2R_FB_READ_PIXEL, 0);
  346. pm2_WR(p, PM2R_DITHER_MODE, 0);
  347. pm2_WR(p, PM2R_AREA_STIPPLE_MODE, 0);
  348. pm2_WR(p, PM2R_DEPTH_MODE, 0);
  349. pm2_WR(p, PM2R_STENCIL_MODE, 0);
  350. pm2_WR(p, PM2R_TEXTURE_ADDRESS_MODE, 0);
  351. pm2_WR(p, PM2R_TEXTURE_READ_MODE, 0);
  352. pm2_WR(p, PM2R_TEXEL_LUT_MODE, 0);
  353. pm2_WR(p, PM2R_YUV_MODE, 0);
  354. pm2_WR(p, PM2R_COLOR_DDA_MODE, 0);
  355. pm2_WR(p, PM2R_TEXTURE_COLOR_MODE, 0);
  356. pm2_WR(p, PM2R_FOG_MODE, 0);
  357. pm2_WR(p, PM2R_ALPHA_BLEND_MODE, 0);
  358. pm2_WR(p, PM2R_LOGICAL_OP_MODE, 0);
  359. pm2_WR(p, PM2R_STATISTICS_MODE, 0);
  360. pm2_WR(p, PM2R_SCISSOR_MODE, 0);
  361. pm2_WR(p, PM2R_FILTER_MODE, PM2F_SYNCHRONIZATION);
  362. switch (p->type) {
  363. case PM2_TYPE_PERMEDIA2:
  364. pm2_RDAC_WR(p, PM2I_RD_MODE_CONTROL, 0); /* no overlay */
  365. pm2_RDAC_WR(p, PM2I_RD_CURSOR_CONTROL, 0);
  366. pm2_RDAC_WR(p, PM2I_RD_MISC_CONTROL, PM2F_RD_PALETTE_WIDTH_8);
  367. break;
  368. case PM2_TYPE_PERMEDIA2V:
  369. pm2v_RDAC_WR(p, PM2VI_RD_MISC_CONTROL, 1); /* 8bit */
  370. break;
  371. }
  372. pm2_RDAC_WR(p, PM2I_RD_COLOR_KEY_CONTROL, 0);
  373. pm2_RDAC_WR(p, PM2I_RD_OVERLAY_KEY, 0);
  374. pm2_RDAC_WR(p, PM2I_RD_RED_KEY, 0);
  375. pm2_RDAC_WR(p, PM2I_RD_GREEN_KEY, 0);
  376. pm2_RDAC_WR(p, PM2I_RD_BLUE_KEY, 0);
  377. }
  378. static void set_aperture(struct pm2fb_par* p, u32 depth)
  379. {
  380. /*
  381. * The hardware is little-endian. When used in big-endian
  382. * hosts, the on-chip aperture settings are used where
  383. * possible to translate from host to card byte order.
  384. */
  385. WAIT_FIFO(p, 4);
  386. #ifdef __LITTLE_ENDIAN
  387. pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_STANDARD);
  388. #else
  389. switch (depth) {
  390. case 24: /* RGB->BGR */
  391. /*
  392. * We can't use the aperture to translate host to
  393. * card byte order here, so we switch to BGR mode
  394. * in pm2fb_set_par().
  395. */
  396. case 8: /* B->B */
  397. pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_STANDARD);
  398. break;
  399. case 16: /* HL->LH */
  400. pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_HALFWORDSWAP);
  401. break;
  402. case 32: /* RGBA->ABGR */
  403. pm2_WR(p, PM2R_APERTURE_ONE, PM2F_APERTURE_BYTESWAP);
  404. break;
  405. }
  406. #endif
  407. // We don't use aperture two, so this may be superflous
  408. pm2_WR(p, PM2R_APERTURE_TWO, PM2F_APERTURE_STANDARD);
  409. }
  410. static void set_color(struct pm2fb_par* p, unsigned char regno,
  411. unsigned char r, unsigned char g, unsigned char b)
  412. {
  413. WAIT_FIFO(p, 4);
  414. pm2_WR(p, PM2R_RD_PALETTE_WRITE_ADDRESS, regno);
  415. wmb();
  416. pm2_WR(p, PM2R_RD_PALETTE_DATA, r);
  417. wmb();
  418. pm2_WR(p, PM2R_RD_PALETTE_DATA, g);
  419. wmb();
  420. pm2_WR(p, PM2R_RD_PALETTE_DATA, b);
  421. }
  422. static void set_memclock(struct pm2fb_par* par, u32 clk)
  423. {
  424. int i;
  425. unsigned char m, n, p;
  426. switch (par->type) {
  427. case PM2_TYPE_PERMEDIA2V:
  428. pm2v_mnp(clk/2, &m, &n, &p);
  429. WAIT_FIFO(par, 8);
  430. pm2_WR(par, PM2VR_RD_INDEX_HIGH, PM2VI_RD_MCLK_CONTROL >> 8);
  431. pm2v_RDAC_WR(par, PM2VI_RD_MCLK_CONTROL, 0);
  432. wmb();
  433. pm2v_RDAC_WR(par, PM2VI_RD_MCLK_PRESCALE, m);
  434. pm2v_RDAC_WR(par, PM2VI_RD_MCLK_FEEDBACK, n);
  435. pm2v_RDAC_WR(par, PM2VI_RD_MCLK_POSTSCALE, p);
  436. wmb();
  437. pm2v_RDAC_WR(par, PM2VI_RD_MCLK_CONTROL, 1);
  438. rmb();
  439. for (i = 256;
  440. i && !(pm2_RDAC_RD(par, PM2VI_RD_MCLK_CONTROL) & 2);
  441. i--)
  442. ;
  443. pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
  444. break;
  445. case PM2_TYPE_PERMEDIA2:
  446. pm2_mnp(clk, &m, &n, &p);
  447. WAIT_FIFO(par, 10);
  448. pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_3, 6);
  449. wmb();
  450. pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_1, m);
  451. pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_2, n);
  452. wmb();
  453. pm2_RDAC_WR(par, PM2I_RD_MEMORY_CLOCK_3, 8|p);
  454. wmb();
  455. pm2_RDAC_RD(par, PM2I_RD_MEMORY_CLOCK_STATUS);
  456. rmb();
  457. for (i = 256;
  458. i && !(pm2_RD(par, PM2R_RD_INDEXED_DATA) & PM2F_PLL_LOCKED);
  459. i--)
  460. ;
  461. break;
  462. }
  463. }
  464. static void set_pixclock(struct pm2fb_par* par, u32 clk)
  465. {
  466. int i;
  467. unsigned char m, n, p;
  468. switch (par->type) {
  469. case PM2_TYPE_PERMEDIA2:
  470. pm2_mnp(clk, &m, &n, &p);
  471. WAIT_FIFO(par, 8);
  472. pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A3, 0);
  473. wmb();
  474. pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A1, m);
  475. pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A2, n);
  476. wmb();
  477. pm2_RDAC_WR(par, PM2I_RD_PIXEL_CLOCK_A3, 8|p);
  478. wmb();
  479. pm2_RDAC_RD(par, PM2I_RD_PIXEL_CLOCK_STATUS);
  480. rmb();
  481. for (i = 256;
  482. i && !(pm2_RD(par, PM2R_RD_INDEXED_DATA) & PM2F_PLL_LOCKED);
  483. i--)
  484. ;
  485. break;
  486. case PM2_TYPE_PERMEDIA2V:
  487. pm2v_mnp(clk/2, &m, &n, &p);
  488. WAIT_FIFO(par, 8);
  489. pm2_WR(par, PM2VR_RD_INDEX_HIGH, PM2VI_RD_CLK0_PRESCALE >> 8);
  490. pm2v_RDAC_WR(par, PM2VI_RD_CLK0_PRESCALE, m);
  491. pm2v_RDAC_WR(par, PM2VI_RD_CLK0_FEEDBACK, n);
  492. pm2v_RDAC_WR(par, PM2VI_RD_CLK0_POSTSCALE, p);
  493. pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
  494. break;
  495. }
  496. }
  497. static void set_video(struct pm2fb_par* p, u32 video) {
  498. u32 tmp;
  499. u32 vsync;
  500. vsync = video;
  501. DPRINTK("video = 0x%x\n", video);
  502. /*
  503. * The hardware cursor needs +vsync to recognise vert retrace.
  504. * We may not be using the hardware cursor, but the X Glint
  505. * driver may well. So always set +hsync/+vsync and then set
  506. * the RAMDAC to invert the sync if necessary.
  507. */
  508. vsync &= ~(PM2F_HSYNC_MASK|PM2F_VSYNC_MASK);
  509. vsync |= PM2F_HSYNC_ACT_HIGH|PM2F_VSYNC_ACT_HIGH;
  510. WAIT_FIFO(p, 5);
  511. pm2_WR(p, PM2R_VIDEO_CONTROL, vsync);
  512. switch (p->type) {
  513. case PM2_TYPE_PERMEDIA2:
  514. tmp = PM2F_RD_PALETTE_WIDTH_8;
  515. if ((video & PM2F_HSYNC_MASK) == PM2F_HSYNC_ACT_LOW)
  516. tmp |= 4; /* invert hsync */
  517. if ((video & PM2F_VSYNC_MASK) == PM2F_VSYNC_ACT_LOW)
  518. tmp |= 8; /* invert vsync */
  519. pm2_RDAC_WR(p, PM2I_RD_MISC_CONTROL, tmp);
  520. break;
  521. case PM2_TYPE_PERMEDIA2V:
  522. tmp = 0;
  523. if ((video & PM2F_HSYNC_MASK) == PM2F_HSYNC_ACT_LOW)
  524. tmp |= 1; /* invert hsync */
  525. if ((video & PM2F_VSYNC_MASK) == PM2F_VSYNC_ACT_LOW)
  526. tmp |= 4; /* invert vsync */
  527. pm2v_RDAC_WR(p, PM2VI_RD_SYNC_CONTROL, tmp);
  528. pm2v_RDAC_WR(p, PM2VI_RD_MISC_CONTROL, 1);
  529. break;
  530. }
  531. }
  532. /*
  533. *
  534. */
  535. /**
  536. * pm2fb_check_var - Optional function. Validates a var passed in.
  537. * @var: frame buffer variable screen structure
  538. * @info: frame buffer structure that represents a single frame buffer
  539. *
  540. * Checks to see if the hardware supports the state requested by
  541. * var passed in.
  542. *
  543. * Returns negative errno on error, or zero on success.
  544. */
  545. static int pm2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  546. {
  547. u32 lpitch;
  548. if (var->bits_per_pixel != 8 && var->bits_per_pixel != 16 &&
  549. var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
  550. DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
  551. return -EINVAL;
  552. }
  553. if (var->xres != var->xres_virtual) {
  554. DPRINTK("virtual x resolution != physical x resolution not supported\n");
  555. return -EINVAL;
  556. }
  557. if (var->yres > var->yres_virtual) {
  558. DPRINTK("virtual y resolution < physical y resolution not possible\n");
  559. return -EINVAL;
  560. }
  561. if (var->xoffset) {
  562. DPRINTK("xoffset not supported\n");
  563. return -EINVAL;
  564. }
  565. if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
  566. DPRINTK("interlace not supported\n");
  567. return -EINVAL;
  568. }
  569. var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
  570. lpitch = var->xres * ((var->bits_per_pixel + 7)>>3);
  571. if (var->xres < 320 || var->xres > 1600) {
  572. DPRINTK("width not supported: %u\n", var->xres);
  573. return -EINVAL;
  574. }
  575. if (var->yres < 200 || var->yres > 1200) {
  576. DPRINTK("height not supported: %u\n", var->yres);
  577. return -EINVAL;
  578. }
  579. if (lpitch * var->yres_virtual > info->fix.smem_len) {
  580. DPRINTK("no memory for screen (%ux%ux%u)\n",
  581. var->xres, var->yres_virtual, var->bits_per_pixel);
  582. return -EINVAL;
  583. }
  584. if (PICOS2KHZ(var->pixclock) > PM2_MAX_PIXCLOCK) {
  585. DPRINTK("pixclock too high (%ldKHz)\n", PICOS2KHZ(var->pixclock));
  586. return -EINVAL;
  587. }
  588. var->transp.offset = 0;
  589. var->transp.length = 0;
  590. switch(var->bits_per_pixel) {
  591. case 8:
  592. var->red.length = var->green.length = var->blue.length = 8;
  593. break;
  594. case 16:
  595. var->red.offset = 11;
  596. var->red.length = 5;
  597. var->green.offset = 5;
  598. var->green.length = 6;
  599. var->blue.offset = 0;
  600. var->blue.length = 5;
  601. break;
  602. case 32:
  603. var->transp.offset = 24;
  604. var->transp.length = 8;
  605. var->red.offset = 16;
  606. var->green.offset = 8;
  607. var->blue.offset = 0;
  608. var->red.length = var->green.length = var->blue.length = 8;
  609. break;
  610. case 24:
  611. #ifdef __BIG_ENDIAN
  612. var->red.offset = 0;
  613. var->blue.offset = 16;
  614. #else
  615. var->red.offset = 16;
  616. var->blue.offset = 0;
  617. #endif
  618. var->green.offset = 8;
  619. var->red.length = var->green.length = var->blue.length = 8;
  620. break;
  621. }
  622. var->height = var->width = -1;
  623. var->accel_flags = 0; /* Can't mmap if this is on */
  624. DPRINTK("Checking graphics mode at %dx%d depth %d\n",
  625. var->xres, var->yres, var->bits_per_pixel);
  626. return 0;
  627. }
  628. /**
  629. * pm2fb_set_par - Alters the hardware state.
  630. * @info: frame buffer structure that represents a single frame buffer
  631. *
  632. * Using the fb_var_screeninfo in fb_info we set the resolution of the
  633. * this particular framebuffer.
  634. */
  635. static int pm2fb_set_par(struct fb_info *info)
  636. {
  637. struct pm2fb_par *par = info->par;
  638. u32 pixclock;
  639. u32 width, height, depth;
  640. u32 hsstart, hsend, hbend, htotal;
  641. u32 vsstart, vsend, vbend, vtotal;
  642. u32 stride;
  643. u32 base;
  644. u32 video = 0;
  645. u32 clrmode = PM2F_RD_COLOR_MODE_RGB | PM2F_RD_GUI_ACTIVE;
  646. u32 txtmap = 0;
  647. u32 pixsize = 0;
  648. u32 clrformat = 0;
  649. u32 xres;
  650. int data64;
  651. reset_card(par);
  652. reset_config(par);
  653. clear_palette(par);
  654. if ( par->memclock )
  655. set_memclock(par, par->memclock);
  656. width = (info->var.xres_virtual + 7) & ~7;
  657. height = info->var.yres_virtual;
  658. depth = (info->var.bits_per_pixel + 7) & ~7;
  659. depth = (depth > 32) ? 32 : depth;
  660. data64 = depth > 8 || par->type == PM2_TYPE_PERMEDIA2V;
  661. xres = (info->var.xres + 31) & ~31;
  662. pixclock = PICOS2KHZ(info->var.pixclock);
  663. if (pixclock > PM2_MAX_PIXCLOCK) {
  664. DPRINTK("pixclock too high (%uKHz)\n", pixclock);
  665. return -EINVAL;
  666. }
  667. hsstart = to3264(info->var.right_margin, depth, data64);
  668. hsend = hsstart + to3264(info->var.hsync_len, depth, data64);
  669. hbend = hsend + to3264(info->var.left_margin, depth, data64);
  670. htotal = to3264(xres, depth, data64) + hbend - 1;
  671. vsstart = (info->var.lower_margin)
  672. ? info->var.lower_margin - 1
  673. : 0; /* FIXME! */
  674. vsend = info->var.lower_margin + info->var.vsync_len - 1;
  675. vbend = info->var.lower_margin + info->var.vsync_len + info->var.upper_margin;
  676. vtotal = info->var.yres + vbend - 1;
  677. stride = to3264(width, depth, 1);
  678. base = to3264(info->var.yoffset * xres + info->var.xoffset, depth, 1);
  679. if (data64)
  680. video |= PM2F_DATA_64_ENABLE;
  681. if (info->var.sync & FB_SYNC_HOR_HIGH_ACT) {
  682. if (lowhsync) {
  683. DPRINTK("ignoring +hsync, using -hsync.\n");
  684. video |= PM2F_HSYNC_ACT_LOW;
  685. } else
  686. video |= PM2F_HSYNC_ACT_HIGH;
  687. }
  688. else
  689. video |= PM2F_HSYNC_ACT_LOW;
  690. if (info->var.sync & FB_SYNC_VERT_HIGH_ACT) {
  691. if (lowvsync) {
  692. DPRINTK("ignoring +vsync, using -vsync.\n");
  693. video |= PM2F_VSYNC_ACT_LOW;
  694. } else
  695. video |= PM2F_VSYNC_ACT_HIGH;
  696. }
  697. else
  698. video |= PM2F_VSYNC_ACT_LOW;
  699. if ((info->var.vmode & FB_VMODE_MASK)==FB_VMODE_INTERLACED) {
  700. DPRINTK("interlaced not supported\n");
  701. return -EINVAL;
  702. }
  703. if ((info->var.vmode & FB_VMODE_MASK)==FB_VMODE_DOUBLE)
  704. video |= PM2F_LINE_DOUBLE;
  705. if ((info->var.activate & FB_ACTIVATE_MASK)==FB_ACTIVATE_NOW)
  706. video |= PM2F_VIDEO_ENABLE;
  707. par->video = video;
  708. info->fix.visual =
  709. (depth == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
  710. info->fix.line_length = info->var.xres * depth / 8;
  711. info->cmap.len = 256;
  712. /*
  713. * Settings calculated. Now write them out.
  714. */
  715. if (par->type == PM2_TYPE_PERMEDIA2V) {
  716. WAIT_FIFO(par, 1);
  717. pm2_WR(par, PM2VR_RD_INDEX_HIGH, 0);
  718. }
  719. set_aperture(par, depth);
  720. mb();
  721. WAIT_FIFO(par, 19);
  722. pm2_RDAC_WR(par, PM2I_RD_COLOR_KEY_CONTROL,
  723. ( depth == 8 ) ? 0 : PM2F_COLOR_KEY_TEST_OFF);
  724. switch (depth) {
  725. case 8:
  726. pm2_WR(par, PM2R_FB_READ_PIXEL, 0);
  727. clrformat = 0x0e;
  728. break;
  729. case 16:
  730. pm2_WR(par, PM2R_FB_READ_PIXEL, 1);
  731. clrmode |= PM2F_RD_TRUECOLOR | PM2F_RD_PIXELFORMAT_RGB565;
  732. txtmap = PM2F_TEXTEL_SIZE_16;
  733. pixsize = 1;
  734. clrformat = 0x70;
  735. break;
  736. case 32:
  737. pm2_WR(par, PM2R_FB_READ_PIXEL, 2);
  738. clrmode |= PM2F_RD_TRUECOLOR | PM2F_RD_PIXELFORMAT_RGBA8888;
  739. txtmap = PM2F_TEXTEL_SIZE_32;
  740. pixsize = 2;
  741. clrformat = 0x20;
  742. break;
  743. case 24:
  744. pm2_WR(par, PM2R_FB_READ_PIXEL, 4);
  745. clrmode |= PM2F_RD_TRUECOLOR | PM2F_RD_PIXELFORMAT_RGB888;
  746. txtmap = PM2F_TEXTEL_SIZE_24;
  747. pixsize = 4;
  748. clrformat = 0x20;
  749. break;
  750. }
  751. pm2_WR(par, PM2R_FB_WRITE_MODE, PM2F_FB_WRITE_ENABLE);
  752. pm2_WR(par, PM2R_FB_READ_MODE, partprod(xres));
  753. pm2_WR(par, PM2R_LB_READ_MODE, partprod(xres));
  754. pm2_WR(par, PM2R_TEXTURE_MAP_FORMAT, txtmap | partprod(xres));
  755. pm2_WR(par, PM2R_H_TOTAL, htotal);
  756. pm2_WR(par, PM2R_HS_START, hsstart);
  757. pm2_WR(par, PM2R_HS_END, hsend);
  758. pm2_WR(par, PM2R_HG_END, hbend);
  759. pm2_WR(par, PM2R_HB_END, hbend);
  760. pm2_WR(par, PM2R_V_TOTAL, vtotal);
  761. pm2_WR(par, PM2R_VS_START, vsstart);
  762. pm2_WR(par, PM2R_VS_END, vsend);
  763. pm2_WR(par, PM2R_VB_END, vbend);
  764. pm2_WR(par, PM2R_SCREEN_STRIDE, stride);
  765. wmb();
  766. pm2_WR(par, PM2R_WINDOW_ORIGIN, 0);
  767. pm2_WR(par, PM2R_SCREEN_SIZE, (height << 16) | width);
  768. pm2_WR(par, PM2R_SCISSOR_MODE, PM2F_SCREEN_SCISSOR_ENABLE);
  769. wmb();
  770. pm2_WR(par, PM2R_SCREEN_BASE, base);
  771. wmb();
  772. set_video(par, video);
  773. WAIT_FIFO(par, 4);
  774. switch (par->type) {
  775. case PM2_TYPE_PERMEDIA2:
  776. pm2_RDAC_WR(par, PM2I_RD_COLOR_MODE, clrmode);
  777. break;
  778. case PM2_TYPE_PERMEDIA2V:
  779. pm2v_RDAC_WR(par, PM2VI_RD_PIXEL_SIZE, pixsize);
  780. pm2v_RDAC_WR(par, PM2VI_RD_COLOR_FORMAT, clrformat);
  781. break;
  782. }
  783. set_pixclock(par, pixclock);
  784. DPRINTK("Setting graphics mode at %dx%d depth %d\n",
  785. info->var.xres, info->var.yres, info->var.bits_per_pixel);
  786. return 0;
  787. }
  788. /**
  789. * pm2fb_setcolreg - Sets a color register.
  790. * @regno: boolean, 0 copy local, 1 get_user() function
  791. * @red: frame buffer colormap structure
  792. * @green: The green value which can be up to 16 bits wide
  793. * @blue: The blue value which can be up to 16 bits wide.
  794. * @transp: If supported the alpha value which can be up to 16 bits wide.
  795. * @info: frame buffer info structure
  796. *
  797. * Set a single color register. The values supplied have a 16 bit
  798. * magnitude which needs to be scaled in this function for the hardware.
  799. * Pretty much a direct lift from tdfxfb.c.
  800. *
  801. * Returns negative errno on error, or zero on success.
  802. */
  803. static int pm2fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  804. unsigned blue, unsigned transp,
  805. struct fb_info *info)
  806. {
  807. struct pm2fb_par *par = info->par;
  808. if (regno >= info->cmap.len) /* no. of hw registers */
  809. return 1;
  810. /*
  811. * Program hardware... do anything you want with transp
  812. */
  813. /* grayscale works only partially under directcolor */
  814. if (info->var.grayscale) {
  815. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  816. red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
  817. }
  818. /* Directcolor:
  819. * var->{color}.offset contains start of bitfield
  820. * var->{color}.length contains length of bitfield
  821. * {hardwarespecific} contains width of DAC
  822. * cmap[X] is programmed to
  823. * (X << red.offset) | (X << green.offset) | (X << blue.offset)
  824. * RAMDAC[X] is programmed to (red, green, blue)
  825. *
  826. * Pseudocolor:
  827. * uses offset = 0 && length = DAC register width.
  828. * var->{color}.offset is 0
  829. * var->{color}.length contains widht of DAC
  830. * cmap is not used
  831. * DAC[X] is programmed to (red, green, blue)
  832. * Truecolor:
  833. * does not use RAMDAC (usually has 3 of them).
  834. * var->{color}.offset contains start of bitfield
  835. * var->{color}.length contains length of bitfield
  836. * cmap is programmed to
  837. * (red << red.offset) | (green << green.offset) |
  838. * (blue << blue.offset) | (transp << transp.offset)
  839. * RAMDAC does not exist
  840. */
  841. #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
  842. switch (info->fix.visual) {
  843. case FB_VISUAL_TRUECOLOR:
  844. case FB_VISUAL_PSEUDOCOLOR:
  845. red = CNVT_TOHW(red, info->var.red.length);
  846. green = CNVT_TOHW(green, info->var.green.length);
  847. blue = CNVT_TOHW(blue, info->var.blue.length);
  848. transp = CNVT_TOHW(transp, info->var.transp.length);
  849. break;
  850. case FB_VISUAL_DIRECTCOLOR:
  851. /* example here assumes 8 bit DAC. Might be different
  852. * for your hardware */
  853. red = CNVT_TOHW(red, 8);
  854. green = CNVT_TOHW(green, 8);
  855. blue = CNVT_TOHW(blue, 8);
  856. /* hey, there is bug in transp handling... */
  857. transp = CNVT_TOHW(transp, 8);
  858. break;
  859. }
  860. #undef CNVT_TOHW
  861. /* Truecolor has hardware independent palette */
  862. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  863. u32 v;
  864. if (regno >= 16)
  865. return 1;
  866. v = (red << info->var.red.offset) |
  867. (green << info->var.green.offset) |
  868. (blue << info->var.blue.offset) |
  869. (transp << info->var.transp.offset);
  870. switch (info->var.bits_per_pixel) {
  871. case 8:
  872. break;
  873. case 16:
  874. case 24:
  875. case 32:
  876. par->palette[regno] = v;
  877. break;
  878. }
  879. return 0;
  880. }
  881. else if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR)
  882. set_color(par, regno, red, green, blue);
  883. return 0;
  884. }
  885. /**
  886. * pm2fb_pan_display - Pans the display.
  887. * @var: frame buffer variable screen structure
  888. * @info: frame buffer structure that represents a single frame buffer
  889. *
  890. * Pan (or wrap, depending on the `vmode' field) the display using the
  891. * `xoffset' and `yoffset' fields of the `var' structure.
  892. * If the values don't fit, return -EINVAL.
  893. *
  894. * Returns negative errno on error, or zero on success.
  895. *
  896. */
  897. static int pm2fb_pan_display(struct fb_var_screeninfo *var,
  898. struct fb_info *info)
  899. {
  900. struct pm2fb_par *p = info->par;
  901. u32 base;
  902. u32 depth;
  903. u32 xres;
  904. xres = (var->xres + 31) & ~31;
  905. depth = (var->bits_per_pixel + 7) & ~7;
  906. depth = (depth > 32) ? 32 : depth;
  907. base = to3264(var->yoffset * xres + var->xoffset, depth, 1);
  908. WAIT_FIFO(p, 1);
  909. pm2_WR(p, PM2R_SCREEN_BASE, base);
  910. return 0;
  911. }
  912. /**
  913. * pm2fb_blank - Blanks the display.
  914. * @blank_mode: the blank mode we want.
  915. * @info: frame buffer structure that represents a single frame buffer
  916. *
  917. * Blank the screen if blank_mode != 0, else unblank. Return 0 if
  918. * blanking succeeded, != 0 if un-/blanking failed due to e.g. a
  919. * video mode which doesn't support it. Implements VESA suspend
  920. * and powerdown modes on hardware that supports disabling hsync/vsync:
  921. * blank_mode == 2: suspend vsync
  922. * blank_mode == 3: suspend hsync
  923. * blank_mode == 4: powerdown
  924. *
  925. * Returns negative errno on error, or zero on success.
  926. *
  927. */
  928. static int pm2fb_blank(int blank_mode, struct fb_info *info)
  929. {
  930. struct pm2fb_par *par = info->par;
  931. u32 video = par->video;
  932. DPRINTK("blank_mode %d\n", blank_mode);
  933. switch (blank_mode) {
  934. case FB_BLANK_UNBLANK:
  935. /* Screen: On */
  936. video |= PM2F_VIDEO_ENABLE;
  937. break;
  938. case FB_BLANK_NORMAL:
  939. /* Screen: Off */
  940. video &= ~PM2F_VIDEO_ENABLE;
  941. break;
  942. case FB_BLANK_VSYNC_SUSPEND:
  943. /* VSync: Off */
  944. video &= ~(PM2F_VSYNC_MASK | PM2F_BLANK_LOW );
  945. break;
  946. case FB_BLANK_HSYNC_SUSPEND:
  947. /* HSync: Off */
  948. video &= ~(PM2F_HSYNC_MASK | PM2F_BLANK_LOW );
  949. break;
  950. case FB_BLANK_POWERDOWN:
  951. /* HSync: Off, VSync: Off */
  952. video &= ~(PM2F_VSYNC_MASK | PM2F_HSYNC_MASK| PM2F_BLANK_LOW);
  953. break;
  954. }
  955. set_video(par, video);
  956. return 0;
  957. }
  958. static int pm2fb_sync(struct fb_info *info)
  959. {
  960. struct pm2fb_par *par = info->par;
  961. WAIT_FIFO(par, 1);
  962. pm2_WR(par, PM2R_SYNC, 0);
  963. mb();
  964. do {
  965. while (pm2_RD(par, PM2R_OUT_FIFO_WORDS) == 0)
  966. udelay(10);
  967. rmb();
  968. } while (pm2_RD(par, PM2R_OUT_FIFO) != PM2TAG(PM2R_SYNC));
  969. return 0;
  970. }
  971. /*
  972. * block operation. copy=0: rectangle fill, copy=1: rectangle copy.
  973. */
  974. static void pm2fb_block_op(struct fb_info* info, int copy,
  975. s32 xsrc, s32 ysrc,
  976. s32 x, s32 y, s32 w, s32 h,
  977. u32 color) {
  978. struct pm2fb_par *par = info->par;
  979. if (!w || !h)
  980. return;
  981. WAIT_FIFO(par, 6);
  982. pm2_WR(par, PM2R_CONFIG, PM2F_CONFIG_FB_WRITE_ENABLE |
  983. PM2F_CONFIG_FB_READ_SOURCE_ENABLE);
  984. pm2_WR(par, PM2R_FB_PIXEL_OFFSET, 0);
  985. if (copy)
  986. pm2_WR(par, PM2R_FB_SOURCE_DELTA,
  987. ((ysrc-y) & 0xfff) << 16 | ((xsrc-x) & 0xfff));
  988. else
  989. pm2_WR(par, PM2R_FB_BLOCK_COLOR, color);
  990. pm2_WR(par, PM2R_RECTANGLE_ORIGIN, (y << 16) | x);
  991. pm2_WR(par, PM2R_RECTANGLE_SIZE, (h << 16) | w);
  992. wmb();
  993. pm2_WR(par, PM2R_RENDER,PM2F_RENDER_RECTANGLE |
  994. (x<xsrc ? PM2F_INCREASE_X : 0) |
  995. (y<ysrc ? PM2F_INCREASE_Y : 0) |
  996. (copy ? 0 : PM2F_RENDER_FASTFILL));
  997. }
  998. static void pm2fb_fillrect (struct fb_info *info,
  999. const struct fb_fillrect *region)
  1000. {
  1001. struct fb_fillrect modded;
  1002. int vxres, vyres;
  1003. u32 color = (info->fix.visual == FB_VISUAL_TRUECOLOR) ?
  1004. ((u32*)info->pseudo_palette)[region->color] : region->color;
  1005. if (info->state != FBINFO_STATE_RUNNING)
  1006. return;
  1007. if ((info->flags & FBINFO_HWACCEL_DISABLED) ||
  1008. region->rop != ROP_COPY ) {
  1009. cfb_fillrect(info, region);
  1010. return;
  1011. }
  1012. vxres = info->var.xres_virtual;
  1013. vyres = info->var.yres_virtual;
  1014. memcpy(&modded, region, sizeof(struct fb_fillrect));
  1015. if(!modded.width || !modded.height ||
  1016. modded.dx >= vxres || modded.dy >= vyres)
  1017. return;
  1018. if(modded.dx + modded.width > vxres)
  1019. modded.width = vxres - modded.dx;
  1020. if(modded.dy + modded.height > vyres)
  1021. modded.height = vyres - modded.dy;
  1022. if(info->var.bits_per_pixel == 8)
  1023. color |= color << 8;
  1024. if(info->var.bits_per_pixel <= 16)
  1025. color |= color << 16;
  1026. if(info->var.bits_per_pixel != 24)
  1027. pm2fb_block_op(info, 0, 0, 0,
  1028. modded.dx, modded.dy,
  1029. modded.width, modded.height, color);
  1030. else
  1031. cfb_fillrect(info, region);
  1032. }
  1033. static void pm2fb_copyarea(struct fb_info *info,
  1034. const struct fb_copyarea *area)
  1035. {
  1036. struct fb_copyarea modded;
  1037. u32 vxres, vyres;
  1038. if (info->state != FBINFO_STATE_RUNNING)
  1039. return;
  1040. if (info->flags & FBINFO_HWACCEL_DISABLED) {
  1041. cfb_copyarea(info, area);
  1042. return;
  1043. }
  1044. memcpy(&modded, area, sizeof(struct fb_copyarea));
  1045. vxres = info->var.xres_virtual;
  1046. vyres = info->var.yres_virtual;
  1047. if(!modded.width || !modded.height ||
  1048. modded.sx >= vxres || modded.sy >= vyres ||
  1049. modded.dx >= vxres || modded.dy >= vyres)
  1050. return;
  1051. if(modded.sx + modded.width > vxres)
  1052. modded.width = vxres - modded.sx;
  1053. if(modded.dx + modded.width > vxres)
  1054. modded.width = vxres - modded.dx;
  1055. if(modded.sy + modded.height > vyres)
  1056. modded.height = vyres - modded.sy;
  1057. if(modded.dy + modded.height > vyres)
  1058. modded.height = vyres - modded.dy;
  1059. pm2fb_block_op(info, 1, modded.sx, modded.sy,
  1060. modded.dx, modded.dy,
  1061. modded.width, modded.height, 0);
  1062. }
  1063. /* ------------ Hardware Independent Functions ------------ */
  1064. /*
  1065. * Frame buffer operations
  1066. */
  1067. static struct fb_ops pm2fb_ops = {
  1068. .owner = THIS_MODULE,
  1069. .fb_check_var = pm2fb_check_var,
  1070. .fb_set_par = pm2fb_set_par,
  1071. .fb_setcolreg = pm2fb_setcolreg,
  1072. .fb_blank = pm2fb_blank,
  1073. .fb_pan_display = pm2fb_pan_display,
  1074. .fb_fillrect = pm2fb_fillrect,
  1075. .fb_copyarea = pm2fb_copyarea,
  1076. .fb_imageblit = cfb_imageblit,
  1077. .fb_sync = pm2fb_sync,
  1078. };
  1079. /*
  1080. * PCI stuff
  1081. */
  1082. /**
  1083. * Device initialisation
  1084. *
  1085. * Initialise and allocate resource for PCI device.
  1086. *
  1087. * @param pdev PCI device.
  1088. * @param id PCI device ID.
  1089. */
  1090. static int __devinit pm2fb_probe(struct pci_dev *pdev,
  1091. const struct pci_device_id *id)
  1092. {
  1093. struct pm2fb_par *default_par;
  1094. struct fb_info *info;
  1095. int err, err_retval = -ENXIO;
  1096. err = pci_enable_device(pdev);
  1097. if ( err ) {
  1098. printk(KERN_WARNING "pm2fb: Can't enable pdev: %d\n", err);
  1099. return err;
  1100. }
  1101. info = framebuffer_alloc(sizeof(struct pm2fb_par), &pdev->dev);
  1102. if ( !info )
  1103. return -ENOMEM;
  1104. default_par = info->par;
  1105. switch (pdev->device) {
  1106. case PCI_DEVICE_ID_TI_TVP4020:
  1107. strcpy(pm2fb_fix.id, "TVP4020");
  1108. default_par->type = PM2_TYPE_PERMEDIA2;
  1109. break;
  1110. case PCI_DEVICE_ID_3DLABS_PERMEDIA2:
  1111. strcpy(pm2fb_fix.id, "Permedia2");
  1112. default_par->type = PM2_TYPE_PERMEDIA2;
  1113. break;
  1114. case PCI_DEVICE_ID_3DLABS_PERMEDIA2V:
  1115. strcpy(pm2fb_fix.id, "Permedia2v");
  1116. default_par->type = PM2_TYPE_PERMEDIA2V;
  1117. break;
  1118. }
  1119. pm2fb_fix.mmio_start = pci_resource_start(pdev, 0);
  1120. pm2fb_fix.mmio_len = PM2_REGS_SIZE;
  1121. #if defined(__BIG_ENDIAN)
  1122. /*
  1123. * PM2 has a 64k register file, mapped twice in 128k. Lower
  1124. * map is little-endian, upper map is big-endian.
  1125. */
  1126. pm2fb_fix.mmio_start += PM2_REGS_SIZE;
  1127. DPRINTK("Adjusting register base for big-endian.\n");
  1128. #endif
  1129. DPRINTK("Register base at 0x%lx\n", pm2fb_fix.mmio_start);
  1130. /* Registers - request region and map it. */
  1131. if ( !request_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len,
  1132. "pm2fb regbase") ) {
  1133. printk(KERN_WARNING "pm2fb: Can't reserve regbase.\n");
  1134. goto err_exit_neither;
  1135. }
  1136. default_par->v_regs =
  1137. ioremap_nocache(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
  1138. if ( !default_par->v_regs ) {
  1139. printk(KERN_WARNING "pm2fb: Can't remap %s register area.\n",
  1140. pm2fb_fix.id);
  1141. release_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
  1142. goto err_exit_neither;
  1143. }
  1144. /* Stash away memory register info for use when we reset the board */
  1145. default_par->mem_control = pm2_RD(default_par, PM2R_MEM_CONTROL);
  1146. default_par->boot_address = pm2_RD(default_par, PM2R_BOOT_ADDRESS);
  1147. default_par->mem_config = pm2_RD(default_par, PM2R_MEM_CONFIG);
  1148. DPRINTK("MemControl 0x%x BootAddress 0x%x MemConfig 0x%x\n",
  1149. default_par->mem_control, default_par->boot_address,
  1150. default_par->mem_config);
  1151. if(default_par->mem_control == 0 &&
  1152. default_par->boot_address == 0x31 &&
  1153. default_par->mem_config == 0x259fffff) {
  1154. default_par->memclock = CVPPC_MEMCLOCK;
  1155. default_par->mem_control=0;
  1156. default_par->boot_address=0x20;
  1157. default_par->mem_config=0xe6002021;
  1158. if (pdev->subsystem_vendor == 0x1048 &&
  1159. pdev->subsystem_device == 0x0a31) {
  1160. DPRINTK("subsystem_vendor: %04x, subsystem_device: %04x\n",
  1161. pdev->subsystem_vendor, pdev->subsystem_device);
  1162. DPRINTK("We have not been initialized by VGA BIOS "
  1163. "and are running on an Elsa Winner 2000 Office\n");
  1164. DPRINTK("Initializing card timings manually...\n");
  1165. default_par->memclock=70000;
  1166. }
  1167. if (pdev->subsystem_vendor == 0x3d3d &&
  1168. pdev->subsystem_device == 0x0100) {
  1169. DPRINTK("subsystem_vendor: %04x, subsystem_device: %04x\n",
  1170. pdev->subsystem_vendor, pdev->subsystem_device);
  1171. DPRINTK("We have not been initialized by VGA BIOS "
  1172. "and are running on an 3dlabs reference board\n");
  1173. DPRINTK("Initializing card timings manually...\n");
  1174. default_par->memclock=74894;
  1175. }
  1176. }
  1177. /* Now work out how big lfb is going to be. */
  1178. switch(default_par->mem_config & PM2F_MEM_CONFIG_RAM_MASK) {
  1179. case PM2F_MEM_BANKS_1:
  1180. pm2fb_fix.smem_len=0x200000;
  1181. break;
  1182. case PM2F_MEM_BANKS_2:
  1183. pm2fb_fix.smem_len=0x400000;
  1184. break;
  1185. case PM2F_MEM_BANKS_3:
  1186. pm2fb_fix.smem_len=0x600000;
  1187. break;
  1188. case PM2F_MEM_BANKS_4:
  1189. pm2fb_fix.smem_len=0x800000;
  1190. break;
  1191. }
  1192. pm2fb_fix.smem_start = pci_resource_start(pdev, 1);
  1193. /* Linear frame buffer - request region and map it. */
  1194. if ( !request_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len,
  1195. "pm2fb smem") ) {
  1196. printk(KERN_WARNING "pm2fb: Can't reserve smem.\n");
  1197. goto err_exit_mmio;
  1198. }
  1199. info->screen_base =
  1200. ioremap_nocache(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
  1201. if ( !info->screen_base ) {
  1202. printk(KERN_WARNING "pm2fb: Can't ioremap smem area.\n");
  1203. release_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
  1204. goto err_exit_mmio;
  1205. }
  1206. info->fbops = &pm2fb_ops;
  1207. info->fix = pm2fb_fix;
  1208. info->pseudo_palette = default_par->palette;
  1209. info->flags = FBINFO_DEFAULT |
  1210. FBINFO_HWACCEL_YPAN |
  1211. FBINFO_HWACCEL_COPYAREA |
  1212. FBINFO_HWACCEL_FILLRECT;
  1213. if (!mode)
  1214. mode = "640x480@60";
  1215. err = fb_find_mode(&info->var, info, mode, NULL, 0, NULL, 8);
  1216. if (!err || err == 4)
  1217. info->var = pm2fb_var;
  1218. if (fb_alloc_cmap(&info->cmap, 256, 0) < 0)
  1219. goto err_exit_both;
  1220. if (register_framebuffer(info) < 0)
  1221. goto err_exit_all;
  1222. printk(KERN_INFO "fb%d: %s frame buffer device, memory = %dK.\n",
  1223. info->node, info->fix.id, pm2fb_fix.smem_len / 1024);
  1224. /*
  1225. * Our driver data
  1226. */
  1227. pci_set_drvdata(pdev, info);
  1228. return 0;
  1229. err_exit_all:
  1230. fb_dealloc_cmap(&info->cmap);
  1231. err_exit_both:
  1232. iounmap(info->screen_base);
  1233. release_mem_region(pm2fb_fix.smem_start, pm2fb_fix.smem_len);
  1234. err_exit_mmio:
  1235. iounmap(default_par->v_regs);
  1236. release_mem_region(pm2fb_fix.mmio_start, pm2fb_fix.mmio_len);
  1237. err_exit_neither:
  1238. framebuffer_release(info);
  1239. return err_retval;
  1240. }
  1241. /**
  1242. * Device removal.
  1243. *
  1244. * Release all device resources.
  1245. *
  1246. * @param pdev PCI device to clean up.
  1247. */
  1248. static void __devexit pm2fb_remove(struct pci_dev *pdev)
  1249. {
  1250. struct fb_info* info = pci_get_drvdata(pdev);
  1251. struct fb_fix_screeninfo* fix = &info->fix;
  1252. struct pm2fb_par *par = info->par;
  1253. unregister_framebuffer(info);
  1254. iounmap(info->screen_base);
  1255. release_mem_region(fix->smem_start, fix->smem_len);
  1256. iounmap(par->v_regs);
  1257. release_mem_region(fix->mmio_start, fix->mmio_len);
  1258. pci_set_drvdata(pdev, NULL);
  1259. kfree(info);
  1260. }
  1261. static struct pci_device_id pm2fb_id_table[] = {
  1262. { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TVP4020,
  1263. PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
  1264. 0xff0000, 0 },
  1265. { PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2,
  1266. PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
  1267. 0xff0000, 0 },
  1268. { PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2V,
  1269. PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
  1270. 0xff0000, 0 },
  1271. { PCI_VENDOR_ID_3DLABS, PCI_DEVICE_ID_3DLABS_PERMEDIA2V,
  1272. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NOT_DEFINED_VGA << 8,
  1273. 0xff00, 0 },
  1274. { 0, }
  1275. };
  1276. static struct pci_driver pm2fb_driver = {
  1277. .name = "pm2fb",
  1278. .id_table = pm2fb_id_table,
  1279. .probe = pm2fb_probe,
  1280. .remove = __devexit_p(pm2fb_remove),
  1281. };
  1282. MODULE_DEVICE_TABLE(pci, pm2fb_id_table);
  1283. #ifndef MODULE
  1284. /**
  1285. * Parse user speficied options.
  1286. *
  1287. * This is, comma-separated options following `video=pm2fb:'.
  1288. */
  1289. static int __init pm2fb_setup(char *options)
  1290. {
  1291. char* this_opt;
  1292. if (!options || !*options)
  1293. return 0;
  1294. while ((this_opt = strsep(&options, ",")) != NULL) {
  1295. if (!*this_opt)
  1296. continue;
  1297. if(!strcmp(this_opt, "lowhsync")) {
  1298. lowhsync = 1;
  1299. } else if(!strcmp(this_opt, "lowvsync")) {
  1300. lowvsync = 1;
  1301. } else {
  1302. mode = this_opt;
  1303. }
  1304. }
  1305. return 0;
  1306. }
  1307. #endif
  1308. static int __init pm2fb_init(void)
  1309. {
  1310. #ifndef MODULE
  1311. char *option = NULL;
  1312. if (fb_get_options("pm2fb", &option))
  1313. return -ENODEV;
  1314. pm2fb_setup(option);
  1315. #endif
  1316. return pci_register_driver(&pm2fb_driver);
  1317. }
  1318. module_init(pm2fb_init);
  1319. #ifdef MODULE
  1320. /*
  1321. * Cleanup
  1322. */
  1323. static void __exit pm2fb_exit(void)
  1324. {
  1325. pci_unregister_driver(&pm2fb_driver);
  1326. }
  1327. #endif
  1328. #ifdef MODULE
  1329. module_exit(pm2fb_exit);
  1330. module_param(mode, charp, 0);
  1331. MODULE_PARM_DESC(mode, "Preferred video mode e.g. '648x480-8@60'");
  1332. module_param(lowhsync, bool, 0);
  1333. MODULE_PARM_DESC(lowhsync, "Force horizontal sync low regardless of mode");
  1334. module_param(lowvsync, bool, 0);
  1335. MODULE_PARM_DESC(lowvsync, "Force vertical sync low regardless of mode");
  1336. MODULE_AUTHOR("Jim Hague <jim.hague@acm.org>");
  1337. MODULE_DESCRIPTION("Permedia2 framebuffer device driver");
  1338. MODULE_LICENSE("GPL");
  1339. #endif