pm2fb.c 39 KB

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