pm2fb.c 39 KB

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