pvr2fb.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /* drivers/video/pvr2fb.c
  2. *
  3. * Frame buffer and fbcon support for the NEC PowerVR2 found within the Sega
  4. * Dreamcast.
  5. *
  6. * Copyright (c) 2001 M. R. Brown <mrbrown@0xd6.org>
  7. * Copyright (c) 2001, 2002, 2003, 2004, 2005 Paul Mundt <lethal@linux-sh.org>
  8. *
  9. * This file is part of the LinuxDC project (linuxdc.sourceforge.net).
  10. *
  11. */
  12. /*
  13. * This driver is mostly based on the excellent amifb and vfb sources. It uses
  14. * an odd scheme for converting hardware values to/from framebuffer values,
  15. * here are some hacked-up formulas:
  16. *
  17. * The Dreamcast has screen offsets from each side of its four borders and
  18. * the start offsets of the display window. I used these values to calculate
  19. * 'pseudo' values (think of them as placeholders) for the fb video mode, so
  20. * that when it came time to convert these values back into their hardware
  21. * values, I could just add mode- specific offsets to get the correct mode
  22. * settings:
  23. *
  24. * left_margin = diwstart_h - borderstart_h;
  25. * right_margin = borderstop_h - (diwstart_h + xres);
  26. * upper_margin = diwstart_v - borderstart_v;
  27. * lower_margin = borderstop_v - (diwstart_h + yres);
  28. *
  29. * hsync_len = borderstart_h + (hsync_total - borderstop_h);
  30. * vsync_len = borderstart_v + (vsync_total - borderstop_v);
  31. *
  32. * Then, when it's time to convert back to hardware settings, the only
  33. * constants are the borderstart_* offsets, all other values are derived from
  34. * the fb video mode:
  35. *
  36. * // PAL
  37. * borderstart_h = 116;
  38. * borderstart_v = 44;
  39. * ...
  40. * borderstop_h = borderstart_h + hsync_total - hsync_len;
  41. * ...
  42. * diwstart_v = borderstart_v - upper_margin;
  43. *
  44. * However, in the current implementation, the borderstart values haven't had
  45. * the benefit of being fully researched, so some modes may be broken.
  46. */
  47. #undef DEBUG
  48. #include <linux/module.h>
  49. #include <linux/kernel.h>
  50. #include <linux/errno.h>
  51. #include <linux/string.h>
  52. #include <linux/mm.h>
  53. #include <linux/slab.h>
  54. #include <linux/delay.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/fb.h>
  57. #include <linux/init.h>
  58. #include <linux/pci.h>
  59. #ifdef CONFIG_SH_DREAMCAST
  60. #include <asm/machvec.h>
  61. #include <asm/mach/sysasic.h>
  62. #endif
  63. #ifdef CONFIG_SH_DMA
  64. #include <linux/pagemap.h>
  65. #include <asm/mach/dma.h>
  66. #include <asm/dma.h>
  67. #endif
  68. #ifdef CONFIG_SH_STORE_QUEUES
  69. #include <linux/uaccess.h>
  70. #include <asm/cpu/sq.h>
  71. #endif
  72. #ifndef PCI_DEVICE_ID_NEC_NEON250
  73. # define PCI_DEVICE_ID_NEC_NEON250 0x0067
  74. #endif
  75. /* 2D video registers */
  76. #define DISP_BASE par->mmio_base
  77. #define DISP_BRDRCOLR (DISP_BASE + 0x40)
  78. #define DISP_DIWMODE (DISP_BASE + 0x44)
  79. #define DISP_DIWADDRL (DISP_BASE + 0x50)
  80. #define DISP_DIWADDRS (DISP_BASE + 0x54)
  81. #define DISP_DIWSIZE (DISP_BASE + 0x5c)
  82. #define DISP_SYNCCONF (DISP_BASE + 0xd0)
  83. #define DISP_BRDRHORZ (DISP_BASE + 0xd4)
  84. #define DISP_SYNCSIZE (DISP_BASE + 0xd8)
  85. #define DISP_BRDRVERT (DISP_BASE + 0xdc)
  86. #define DISP_DIWCONF (DISP_BASE + 0xe8)
  87. #define DISP_DIWHSTRT (DISP_BASE + 0xec)
  88. #define DISP_DIWVSTRT (DISP_BASE + 0xf0)
  89. #define DISP_PIXDEPTH (DISP_BASE + 0x108)
  90. /* Pixel clocks, one for TV output, doubled for VGA output */
  91. #define TV_CLK 74239
  92. #define VGA_CLK 37119
  93. /* This is for 60Hz - the VTOTAL is doubled for interlaced modes */
  94. #define PAL_HTOTAL 863
  95. #define PAL_VTOTAL 312
  96. #define NTSC_HTOTAL 857
  97. #define NTSC_VTOTAL 262
  98. /* Supported cable types */
  99. enum { CT_VGA, CT_NONE, CT_RGB, CT_COMPOSITE };
  100. /* Supported video output types */
  101. enum { VO_PAL, VO_NTSC, VO_VGA };
  102. /* Supported palette types */
  103. enum { PAL_ARGB1555, PAL_RGB565, PAL_ARGB4444, PAL_ARGB8888 };
  104. struct pvr2_params { unsigned int val; char *name; };
  105. static struct pvr2_params cables[] __devinitdata = {
  106. { CT_VGA, "VGA" }, { CT_RGB, "RGB" }, { CT_COMPOSITE, "COMPOSITE" },
  107. };
  108. static struct pvr2_params outputs[] __devinitdata = {
  109. { VO_PAL, "PAL" }, { VO_NTSC, "NTSC" }, { VO_VGA, "VGA" },
  110. };
  111. /*
  112. * This describes the current video mode
  113. */
  114. static struct pvr2fb_par {
  115. unsigned int hsync_total; /* Clocks/line */
  116. unsigned int vsync_total; /* Lines/field */
  117. unsigned int borderstart_h;
  118. unsigned int borderstop_h;
  119. unsigned int borderstart_v;
  120. unsigned int borderstop_v;
  121. unsigned int diwstart_h; /* Horizontal offset of the display field */
  122. unsigned int diwstart_v; /* Vertical offset of the display field, for
  123. interlaced modes, this is the long field */
  124. unsigned long disp_start; /* Address of image within VRAM */
  125. unsigned char is_interlaced; /* Is the display interlaced? */
  126. unsigned char is_doublescan; /* Are scanlines output twice? (doublescan) */
  127. unsigned char is_lowres; /* Is horizontal pixel-doubling enabled? */
  128. unsigned long mmio_base; /* MMIO base */
  129. u32 palette[16];
  130. } *currentpar;
  131. static struct fb_info *fb_info;
  132. static struct fb_fix_screeninfo pvr2_fix __devinitdata = {
  133. .id = "NEC PowerVR2",
  134. .type = FB_TYPE_PACKED_PIXELS,
  135. .visual = FB_VISUAL_TRUECOLOR,
  136. .ypanstep = 1,
  137. .ywrapstep = 1,
  138. .accel = FB_ACCEL_NONE,
  139. };
  140. static struct fb_var_screeninfo pvr2_var __devinitdata = {
  141. .xres = 640,
  142. .yres = 480,
  143. .xres_virtual = 640,
  144. .yres_virtual = 480,
  145. .bits_per_pixel =16,
  146. .red = { 11, 5, 0 },
  147. .green = { 5, 6, 0 },
  148. .blue = { 0, 5, 0 },
  149. .activate = FB_ACTIVATE_NOW,
  150. .height = -1,
  151. .width = -1,
  152. .vmode = FB_VMODE_NONINTERLACED,
  153. };
  154. static int cable_type = CT_VGA;
  155. static int video_output = VO_VGA;
  156. static int nopan = 0;
  157. static int nowrap = 1;
  158. /*
  159. * We do all updating, blanking, etc. during the vertical retrace period
  160. */
  161. static unsigned int do_vmode_full = 0; /* Change the video mode */
  162. static unsigned int do_vmode_pan = 0; /* Update the video mode */
  163. static short do_blank = 0; /* (Un)Blank the screen */
  164. static unsigned int is_blanked = 0; /* Is the screen blanked? */
  165. #ifdef CONFIG_SH_STORE_QUEUES
  166. static unsigned long pvr2fb_map;
  167. #endif
  168. #ifdef CONFIG_SH_DMA
  169. static unsigned int shdma = PVR2_CASCADE_CHAN;
  170. static unsigned int pvr2dma = ONCHIP_NR_DMA_CHANNELS;
  171. #endif
  172. static int pvr2fb_setcolreg(unsigned int regno, unsigned int red, unsigned int green, unsigned int blue,
  173. unsigned int transp, struct fb_info *info);
  174. static int pvr2fb_blank(int blank, struct fb_info *info);
  175. static unsigned long get_line_length(int xres_virtual, int bpp);
  176. static void set_color_bitfields(struct fb_var_screeninfo *var);
  177. static int pvr2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info);
  178. static int pvr2fb_set_par(struct fb_info *info);
  179. static void pvr2_update_display(struct fb_info *info);
  180. static void pvr2_init_display(struct fb_info *info);
  181. static void pvr2_do_blank(void);
  182. static irqreturn_t pvr2fb_interrupt(int irq, void *dev_id);
  183. static int pvr2_init_cable(void);
  184. static int pvr2_get_param(const struct pvr2_params *p, const char *s,
  185. int val, int size);
  186. #ifdef CONFIG_SH_DMA
  187. static ssize_t pvr2fb_write(struct fb_info *info, const char *buf,
  188. size_t count, loff_t *ppos);
  189. #endif
  190. static struct fb_ops pvr2fb_ops = {
  191. .owner = THIS_MODULE,
  192. .fb_setcolreg = pvr2fb_setcolreg,
  193. .fb_blank = pvr2fb_blank,
  194. .fb_check_var = pvr2fb_check_var,
  195. .fb_set_par = pvr2fb_set_par,
  196. #ifdef CONFIG_SH_DMA
  197. .fb_write = pvr2fb_write,
  198. #endif
  199. .fb_fillrect = cfb_fillrect,
  200. .fb_copyarea = cfb_copyarea,
  201. .fb_imageblit = cfb_imageblit,
  202. };
  203. static struct fb_videomode pvr2_modedb[] __devinitdata = {
  204. /*
  205. * Broadcast video modes (PAL and NTSC). I'm unfamiliar with
  206. * PAL-M and PAL-N, but from what I've read both modes parallel PAL and
  207. * NTSC, so it shouldn't be a problem (I hope).
  208. */
  209. {
  210. /* 640x480 @ 60Hz interlaced (NTSC) */
  211. "ntsc_640x480i", 60, 640, 480, TV_CLK, 38, 33, 0, 18, 146, 26,
  212. FB_SYNC_BROADCAST, FB_VMODE_INTERLACED | FB_VMODE_YWRAP
  213. }, {
  214. /* 640x240 @ 60Hz (NTSC) */
  215. /* XXX: Broken! Don't use... */
  216. "ntsc_640x240", 60, 640, 240, TV_CLK, 38, 33, 0, 0, 146, 22,
  217. FB_SYNC_BROADCAST, FB_VMODE_YWRAP
  218. }, {
  219. /* 640x480 @ 60hz (VGA) */
  220. "vga_640x480", 60, 640, 480, VGA_CLK, 38, 33, 0, 18, 146, 26,
  221. 0, FB_VMODE_YWRAP
  222. },
  223. };
  224. #define NUM_TOTAL_MODES ARRAY_SIZE(pvr2_modedb)
  225. #define DEFMODE_NTSC 0
  226. #define DEFMODE_PAL 0
  227. #define DEFMODE_VGA 2
  228. static int defmode = DEFMODE_NTSC;
  229. static char *mode_option __devinitdata = NULL;
  230. static inline void pvr2fb_set_pal_type(unsigned int type)
  231. {
  232. struct pvr2fb_par *par = (struct pvr2fb_par *)fb_info->par;
  233. fb_writel(type, par->mmio_base + 0x108);
  234. }
  235. static inline void pvr2fb_set_pal_entry(struct pvr2fb_par *par,
  236. unsigned int regno,
  237. unsigned int val)
  238. {
  239. fb_writel(val, par->mmio_base + 0x1000 + (4 * regno));
  240. }
  241. static int pvr2fb_blank(int blank, struct fb_info *info)
  242. {
  243. do_blank = blank ? blank : -1;
  244. return 0;
  245. }
  246. static inline unsigned long get_line_length(int xres_virtual, int bpp)
  247. {
  248. return (unsigned long)((((xres_virtual*bpp)+31)&~31) >> 3);
  249. }
  250. static void set_color_bitfields(struct fb_var_screeninfo *var)
  251. {
  252. switch (var->bits_per_pixel) {
  253. case 16: /* RGB 565 */
  254. pvr2fb_set_pal_type(PAL_RGB565);
  255. var->red.offset = 11; var->red.length = 5;
  256. var->green.offset = 5; var->green.length = 6;
  257. var->blue.offset = 0; var->blue.length = 5;
  258. var->transp.offset = 0; var->transp.length = 0;
  259. break;
  260. case 24: /* RGB 888 */
  261. var->red.offset = 16; var->red.length = 8;
  262. var->green.offset = 8; var->green.length = 8;
  263. var->blue.offset = 0; var->blue.length = 8;
  264. var->transp.offset = 0; var->transp.length = 0;
  265. break;
  266. case 32: /* ARGB 8888 */
  267. pvr2fb_set_pal_type(PAL_ARGB8888);
  268. var->red.offset = 16; var->red.length = 8;
  269. var->green.offset = 8; var->green.length = 8;
  270. var->blue.offset = 0; var->blue.length = 8;
  271. var->transp.offset = 24; var->transp.length = 8;
  272. break;
  273. }
  274. }
  275. static int pvr2fb_setcolreg(unsigned int regno, unsigned int red,
  276. unsigned int green, unsigned int blue,
  277. unsigned int transp, struct fb_info *info)
  278. {
  279. struct pvr2fb_par *par = (struct pvr2fb_par *)info->par;
  280. unsigned int tmp;
  281. if (regno > info->cmap.len)
  282. return 1;
  283. /*
  284. * We only support the hardware palette for 16 and 32bpp. It's also
  285. * expected that the palette format has been set by the time we get
  286. * here, so we don't waste time setting it again.
  287. */
  288. switch (info->var.bits_per_pixel) {
  289. case 16: /* RGB 565 */
  290. tmp = (red & 0xf800) |
  291. ((green & 0xfc00) >> 5) |
  292. ((blue & 0xf800) >> 11);
  293. pvr2fb_set_pal_entry(par, regno, tmp);
  294. break;
  295. case 24: /* RGB 888 */
  296. red >>= 8; green >>= 8; blue >>= 8;
  297. tmp = (red << 16) | (green << 8) | blue;
  298. break;
  299. case 32: /* ARGB 8888 */
  300. red >>= 8; green >>= 8; blue >>= 8;
  301. tmp = (transp << 24) | (red << 16) | (green << 8) | blue;
  302. pvr2fb_set_pal_entry(par, regno, tmp);
  303. break;
  304. default:
  305. pr_debug("Invalid bit depth %d?!?\n", info->var.bits_per_pixel);
  306. return 1;
  307. }
  308. if (regno < 16)
  309. ((u32*)(info->pseudo_palette))[regno] = tmp;
  310. return 0;
  311. }
  312. static int pvr2fb_set_par(struct fb_info *info)
  313. {
  314. struct pvr2fb_par *par = (struct pvr2fb_par *)info->par;
  315. struct fb_var_screeninfo *var = &info->var;
  316. unsigned long line_length;
  317. unsigned int vtotal;
  318. /*
  319. * XXX: It's possible that a user could use a VGA box, change the cable
  320. * type in hardware (i.e. switch from VGA<->composite), then change
  321. * modes (i.e. switching to another VT). If that happens we should
  322. * automagically change the output format to cope, but currently I
  323. * don't have a VGA box to make sure this works properly.
  324. */
  325. cable_type = pvr2_init_cable();
  326. if (cable_type == CT_VGA && video_output != VO_VGA)
  327. video_output = VO_VGA;
  328. var->vmode &= FB_VMODE_MASK;
  329. if (var->vmode & FB_VMODE_INTERLACED && video_output != VO_VGA)
  330. par->is_interlaced = 1;
  331. /*
  332. * XXX: Need to be more creative with this (i.e. allow doublecan for
  333. * PAL/NTSC output).
  334. */
  335. if (var->vmode & FB_VMODE_DOUBLE && video_output == VO_VGA)
  336. par->is_doublescan = 1;
  337. par->hsync_total = var->left_margin + var->xres + var->right_margin +
  338. var->hsync_len;
  339. par->vsync_total = var->upper_margin + var->yres + var->lower_margin +
  340. var->vsync_len;
  341. if (var->sync & FB_SYNC_BROADCAST) {
  342. vtotal = par->vsync_total;
  343. if (par->is_interlaced)
  344. vtotal /= 2;
  345. if (vtotal > (PAL_VTOTAL + NTSC_VTOTAL)/2) {
  346. /* XXX: Check for start values here... */
  347. /* XXX: Check hardware for PAL-compatibility */
  348. par->borderstart_h = 116;
  349. par->borderstart_v = 44;
  350. } else {
  351. /* NTSC video output */
  352. par->borderstart_h = 126;
  353. par->borderstart_v = 18;
  354. }
  355. } else {
  356. /* VGA mode */
  357. /* XXX: What else needs to be checked? */
  358. /*
  359. * XXX: We have a little freedom in VGA modes, what ranges
  360. * should be here (i.e. hsync/vsync totals, etc.)?
  361. */
  362. par->borderstart_h = 126;
  363. par->borderstart_v = 40;
  364. }
  365. /* Calculate the remainding offsets */
  366. par->diwstart_h = par->borderstart_h + var->left_margin;
  367. par->diwstart_v = par->borderstart_v + var->upper_margin;
  368. par->borderstop_h = par->diwstart_h + var->xres +
  369. var->right_margin;
  370. par->borderstop_v = par->diwstart_v + var->yres +
  371. var->lower_margin;
  372. if (!par->is_interlaced)
  373. par->borderstop_v /= 2;
  374. if (info->var.xres < 640)
  375. par->is_lowres = 1;
  376. line_length = get_line_length(var->xres_virtual, var->bits_per_pixel);
  377. par->disp_start = info->fix.smem_start + (line_length * var->yoffset) * line_length;
  378. info->fix.line_length = line_length;
  379. return 0;
  380. }
  381. static int pvr2fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  382. {
  383. struct pvr2fb_par *par = (struct pvr2fb_par *)info->par;
  384. unsigned int vtotal, hsync_total;
  385. unsigned long line_length;
  386. if (var->pixclock != TV_CLK && var->pixclock != VGA_CLK) {
  387. pr_debug("Invalid pixclock value %d\n", var->pixclock);
  388. return -EINVAL;
  389. }
  390. if (var->xres < 320)
  391. var->xres = 320;
  392. if (var->yres < 240)
  393. var->yres = 240;
  394. if (var->xres_virtual < var->xres)
  395. var->xres_virtual = var->xres;
  396. if (var->yres_virtual < var->yres)
  397. var->yres_virtual = var->yres;
  398. if (var->bits_per_pixel <= 16)
  399. var->bits_per_pixel = 16;
  400. else if (var->bits_per_pixel <= 24)
  401. var->bits_per_pixel = 24;
  402. else if (var->bits_per_pixel <= 32)
  403. var->bits_per_pixel = 32;
  404. set_color_bitfields(var);
  405. if (var->vmode & FB_VMODE_YWRAP) {
  406. if (var->xoffset || var->yoffset < 0 ||
  407. var->yoffset >= var->yres_virtual) {
  408. var->xoffset = var->yoffset = 0;
  409. } else {
  410. if (var->xoffset > var->xres_virtual - var->xres ||
  411. var->yoffset > var->yres_virtual - var->yres ||
  412. var->xoffset < 0 || var->yoffset < 0)
  413. var->xoffset = var->yoffset = 0;
  414. }
  415. } else {
  416. var->xoffset = var->yoffset = 0;
  417. }
  418. /*
  419. * XXX: Need to be more creative with this (i.e. allow doublecan for
  420. * PAL/NTSC output).
  421. */
  422. if (var->yres < 480 && video_output == VO_VGA)
  423. var->vmode |= FB_VMODE_DOUBLE;
  424. if (video_output != VO_VGA) {
  425. var->sync |= FB_SYNC_BROADCAST;
  426. var->vmode |= FB_VMODE_INTERLACED;
  427. } else {
  428. var->sync &= ~FB_SYNC_BROADCAST;
  429. var->vmode &= ~FB_VMODE_INTERLACED;
  430. var->vmode |= pvr2_var.vmode;
  431. }
  432. if ((var->activate & FB_ACTIVATE_MASK) != FB_ACTIVATE_TEST) {
  433. var->right_margin = par->borderstop_h -
  434. (par->diwstart_h + var->xres);
  435. var->left_margin = par->diwstart_h - par->borderstart_h;
  436. var->hsync_len = par->borderstart_h +
  437. (par->hsync_total - par->borderstop_h);
  438. var->upper_margin = par->diwstart_v - par->borderstart_v;
  439. var->lower_margin = par->borderstop_v -
  440. (par->diwstart_v + var->yres);
  441. var->vsync_len = par->borderstop_v +
  442. (par->vsync_total - par->borderstop_v);
  443. }
  444. hsync_total = var->left_margin + var->xres + var->right_margin +
  445. var->hsync_len;
  446. vtotal = var->upper_margin + var->yres + var->lower_margin +
  447. var->vsync_len;
  448. if (var->sync & FB_SYNC_BROADCAST) {
  449. if (var->vmode & FB_VMODE_INTERLACED)
  450. vtotal /= 2;
  451. if (vtotal > (PAL_VTOTAL + NTSC_VTOTAL)/2) {
  452. /* PAL video output */
  453. /* XXX: Should be using a range here ... ? */
  454. if (hsync_total != PAL_HTOTAL) {
  455. pr_debug("invalid hsync total for PAL\n");
  456. return -EINVAL;
  457. }
  458. } else {
  459. /* NTSC video output */
  460. if (hsync_total != NTSC_HTOTAL) {
  461. pr_debug("invalid hsync total for NTSC\n");
  462. return -EINVAL;
  463. }
  464. }
  465. }
  466. /* Check memory sizes */
  467. line_length = get_line_length(var->xres_virtual, var->bits_per_pixel);
  468. if (line_length * var->yres_virtual > info->fix.smem_len)
  469. return -ENOMEM;
  470. return 0;
  471. }
  472. static void pvr2_update_display(struct fb_info *info)
  473. {
  474. struct pvr2fb_par *par = (struct pvr2fb_par *) info->par;
  475. struct fb_var_screeninfo *var = &info->var;
  476. /* Update the start address of the display image */
  477. fb_writel(par->disp_start, DISP_DIWADDRL);
  478. fb_writel(par->disp_start +
  479. get_line_length(var->xoffset+var->xres, var->bits_per_pixel),
  480. DISP_DIWADDRS);
  481. }
  482. /*
  483. * Initialize the video mode. Currently, the 16bpp and 24bpp modes aren't
  484. * very stable. It's probably due to the fact that a lot of the 2D video
  485. * registers are still undocumented.
  486. */
  487. static void pvr2_init_display(struct fb_info *info)
  488. {
  489. struct pvr2fb_par *par = (struct pvr2fb_par *) info->par;
  490. struct fb_var_screeninfo *var = &info->var;
  491. unsigned int diw_height, diw_width, diw_modulo = 1;
  492. unsigned int bytesperpixel = var->bits_per_pixel >> 3;
  493. /* hsync and vsync totals */
  494. fb_writel((par->vsync_total << 16) | par->hsync_total, DISP_SYNCSIZE);
  495. /* column height, modulo, row width */
  496. /* since we're "panning" within vram, we need to offset things based
  497. * on the offset from the virtual x start to our real gfx. */
  498. if (video_output != VO_VGA && par->is_interlaced)
  499. diw_modulo += info->fix.line_length / 4;
  500. diw_height = (par->is_interlaced ? var->yres / 2 : var->yres);
  501. diw_width = get_line_length(var->xres, var->bits_per_pixel) / 4;
  502. fb_writel((diw_modulo << 20) | (--diw_height << 10) | --diw_width,
  503. DISP_DIWSIZE);
  504. /* display address, long and short fields */
  505. fb_writel(par->disp_start, DISP_DIWADDRL);
  506. fb_writel(par->disp_start +
  507. get_line_length(var->xoffset+var->xres, var->bits_per_pixel),
  508. DISP_DIWADDRS);
  509. /* border horizontal, border vertical, border color */
  510. fb_writel((par->borderstart_h << 16) | par->borderstop_h, DISP_BRDRHORZ);
  511. fb_writel((par->borderstart_v << 16) | par->borderstop_v, DISP_BRDRVERT);
  512. fb_writel(0, DISP_BRDRCOLR);
  513. /* display window start position */
  514. fb_writel(par->diwstart_h, DISP_DIWHSTRT);
  515. fb_writel((par->diwstart_v << 16) | par->diwstart_v, DISP_DIWVSTRT);
  516. /* misc. settings */
  517. fb_writel((0x16 << 16) | par->is_lowres, DISP_DIWCONF);
  518. /* clock doubler (for VGA), scan doubler, display enable */
  519. fb_writel(((video_output == VO_VGA) << 23) |
  520. (par->is_doublescan << 1) | 1, DISP_DIWMODE);
  521. /* bits per pixel */
  522. fb_writel(fb_readl(DISP_DIWMODE) | (--bytesperpixel << 2), DISP_DIWMODE);
  523. fb_writel(bytesperpixel << 2, DISP_PIXDEPTH);
  524. /* video enable, color sync, interlace,
  525. * hsync and vsync polarity (currently unused) */
  526. fb_writel(0x100 | ((par->is_interlaced /*|4*/) << 4), DISP_SYNCCONF);
  527. }
  528. /* Simulate blanking by making the border cover the entire screen */
  529. #define BLANK_BIT (1<<3)
  530. static void pvr2_do_blank(void)
  531. {
  532. struct pvr2fb_par *par = currentpar;
  533. unsigned long diwconf;
  534. diwconf = fb_readl(DISP_DIWCONF);
  535. if (do_blank > 0)
  536. fb_writel(diwconf | BLANK_BIT, DISP_DIWCONF);
  537. else
  538. fb_writel(diwconf & ~BLANK_BIT, DISP_DIWCONF);
  539. is_blanked = do_blank > 0 ? do_blank : 0;
  540. }
  541. static irqreturn_t pvr2fb_interrupt(int irq, void *dev_id)
  542. {
  543. struct fb_info *info = dev_id;
  544. if (do_vmode_pan || do_vmode_full)
  545. pvr2_update_display(info);
  546. if (do_vmode_full)
  547. pvr2_init_display(info);
  548. if (do_vmode_pan)
  549. do_vmode_pan = 0;
  550. if (do_vmode_full)
  551. do_vmode_full = 0;
  552. if (do_blank) {
  553. pvr2_do_blank();
  554. do_blank = 0;
  555. }
  556. return IRQ_HANDLED;
  557. }
  558. /*
  559. * Determine the cable type and initialize the cable output format. Don't do
  560. * anything if the cable type has been overidden (via "cable:XX").
  561. */
  562. #define PCTRA 0xff80002c
  563. #define PDTRA 0xff800030
  564. #define VOUTC 0xa0702c00
  565. static int pvr2_init_cable(void)
  566. {
  567. if (cable_type < 0) {
  568. fb_writel((fb_readl(PCTRA) & 0xfff0ffff) | 0x000a0000,
  569. PCTRA);
  570. cable_type = (fb_readw(PDTRA) >> 8) & 3;
  571. }
  572. /* Now select the output format (either composite or other) */
  573. /* XXX: Save the previous val first, as this reg is also AICA
  574. related */
  575. if (cable_type == CT_COMPOSITE)
  576. fb_writel(3 << 8, VOUTC);
  577. else if (cable_type == CT_RGB)
  578. fb_writel(1 << 9, VOUTC);
  579. else
  580. fb_writel(0, VOUTC);
  581. return cable_type;
  582. }
  583. #ifdef CONFIG_SH_DMA
  584. static ssize_t pvr2fb_write(struct fb_info *info, const char *buf,
  585. size_t count, loff_t *ppos)
  586. {
  587. unsigned long dst, start, end, len;
  588. unsigned int nr_pages;
  589. struct page **pages;
  590. int ret, i;
  591. nr_pages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
  592. pages = kmalloc(nr_pages * sizeof(struct page *), GFP_KERNEL);
  593. if (!pages)
  594. return -ENOMEM;
  595. down_read(&current->mm->mmap_sem);
  596. ret = get_user_pages(current, current->mm, (unsigned long)buf,
  597. nr_pages, WRITE, 0, pages, NULL);
  598. up_read(&current->mm->mmap_sem);
  599. if (ret < nr_pages) {
  600. nr_pages = ret;
  601. ret = -EINVAL;
  602. goto out_unmap;
  603. }
  604. dma_configure_channel(shdma, 0x12c1);
  605. dst = (unsigned long)fb_info->screen_base + *ppos;
  606. start = (unsigned long)page_address(pages[0]);
  607. end = (unsigned long)page_address(pages[nr_pages]);
  608. len = nr_pages << PAGE_SHIFT;
  609. /* Half-assed contig check */
  610. if (start + len == end) {
  611. /* As we do this in one shot, it's either all or nothing.. */
  612. if ((*ppos + len) > fb_info->fix.smem_len) {
  613. ret = -ENOSPC;
  614. goto out_unmap;
  615. }
  616. dma_write(shdma, start, 0, len);
  617. dma_write(pvr2dma, 0, dst, len);
  618. dma_wait_for_completion(pvr2dma);
  619. goto out;
  620. }
  621. /* Not contiguous, writeout per-page instead.. */
  622. for (i = 0; i < nr_pages; i++, dst += PAGE_SIZE) {
  623. if ((*ppos + (i << PAGE_SHIFT)) > fb_info->fix.smem_len) {
  624. ret = -ENOSPC;
  625. goto out_unmap;
  626. }
  627. dma_write_page(shdma, (unsigned long)page_address(pages[i]), 0);
  628. dma_write_page(pvr2dma, 0, dst);
  629. dma_wait_for_completion(pvr2dma);
  630. }
  631. out:
  632. *ppos += count;
  633. ret = count;
  634. out_unmap:
  635. for (i = 0; i < nr_pages; i++)
  636. page_cache_release(pages[i]);
  637. kfree(pages);
  638. return ret;
  639. }
  640. #endif /* CONFIG_SH_DMA */
  641. /**
  642. * pvr2fb_common_init
  643. *
  644. * Common init code for the PVR2 chips.
  645. *
  646. * This mostly takes care of the common aspects of the fb setup and
  647. * registration. It's expected that the board-specific init code has
  648. * already setup pvr2_fix with something meaningful at this point.
  649. *
  650. * Device info reporting is also done here, as well as picking a sane
  651. * default from the modedb. For board-specific modelines, simply define
  652. * a per-board modedb.
  653. *
  654. * Also worth noting is that the cable and video output types are likely
  655. * always going to be VGA for the PCI-based PVR2 boards, but we leave this
  656. * in for flexibility anyways. Who knows, maybe someone has tv-out on a
  657. * PCI-based version of these things ;-)
  658. */
  659. static int __devinit pvr2fb_common_init(void)
  660. {
  661. struct pvr2fb_par *par = currentpar;
  662. unsigned long modememused, rev;
  663. fb_info->screen_base = ioremap_nocache(pvr2_fix.smem_start,
  664. pvr2_fix.smem_len);
  665. if (!fb_info->screen_base) {
  666. printk(KERN_ERR "pvr2fb: Failed to remap smem space\n");
  667. goto out_err;
  668. }
  669. par->mmio_base = (unsigned long)ioremap_nocache(pvr2_fix.mmio_start,
  670. pvr2_fix.mmio_len);
  671. if (!par->mmio_base) {
  672. printk(KERN_ERR "pvr2fb: Failed to remap mmio space\n");
  673. goto out_err;
  674. }
  675. fb_memset(fb_info->screen_base, 0, pvr2_fix.smem_len);
  676. pvr2_fix.ypanstep = nopan ? 0 : 1;
  677. pvr2_fix.ywrapstep = nowrap ? 0 : 1;
  678. fb_info->fbops = &pvr2fb_ops;
  679. fb_info->fix = pvr2_fix;
  680. fb_info->par = currentpar;
  681. fb_info->pseudo_palette = currentpar->palette;
  682. fb_info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  683. if (video_output == VO_VGA)
  684. defmode = DEFMODE_VGA;
  685. if (!mode_option)
  686. mode_option = "640x480@60";
  687. if (!fb_find_mode(&fb_info->var, fb_info, mode_option, pvr2_modedb,
  688. NUM_TOTAL_MODES, &pvr2_modedb[defmode], 16))
  689. fb_info->var = pvr2_var;
  690. fb_alloc_cmap(&fb_info->cmap, 256, 0);
  691. if (register_framebuffer(fb_info) < 0)
  692. goto out_err;
  693. /*Must write PIXDEPTH to register before anything is displayed - so force init */
  694. pvr2_init_display(fb_info);
  695. modememused = get_line_length(fb_info->var.xres_virtual,
  696. fb_info->var.bits_per_pixel);
  697. modememused *= fb_info->var.yres_virtual;
  698. rev = fb_readl(par->mmio_base + 0x04);
  699. printk("fb%d: %s (rev %ld.%ld) frame buffer device, using %ldk/%ldk of video memory\n",
  700. fb_info->node, fb_info->fix.id, (rev >> 4) & 0x0f, rev & 0x0f,
  701. modememused >> 10, (unsigned long)(fb_info->fix.smem_len >> 10));
  702. printk("fb%d: Mode %dx%d-%d pitch = %ld cable: %s video output: %s\n",
  703. fb_info->node, fb_info->var.xres, fb_info->var.yres,
  704. fb_info->var.bits_per_pixel,
  705. get_line_length(fb_info->var.xres, fb_info->var.bits_per_pixel),
  706. (char *)pvr2_get_param(cables, NULL, cable_type, 3),
  707. (char *)pvr2_get_param(outputs, NULL, video_output, 3));
  708. #ifdef CONFIG_SH_STORE_QUEUES
  709. printk(KERN_NOTICE "fb%d: registering with SQ API\n", fb_info->node);
  710. pvr2fb_map = sq_remap(fb_info->fix.smem_start, fb_info->fix.smem_len,
  711. fb_info->fix.id, pgprot_val(PAGE_SHARED));
  712. printk(KERN_NOTICE "fb%d: Mapped video memory to SQ addr 0x%lx\n",
  713. fb_info->node, pvr2fb_map);
  714. #endif
  715. return 0;
  716. out_err:
  717. if (fb_info->screen_base)
  718. iounmap(fb_info->screen_base);
  719. if (par->mmio_base)
  720. iounmap((void *)par->mmio_base);
  721. return -ENXIO;
  722. }
  723. #ifdef CONFIG_SH_DREAMCAST
  724. static int __init pvr2fb_dc_init(void)
  725. {
  726. if (!mach_is_dreamcast())
  727. return -ENXIO;
  728. /* Make a guess at the monitor based on the attached cable */
  729. if (pvr2_init_cable() == CT_VGA) {
  730. fb_info->monspecs.hfmin = 30000;
  731. fb_info->monspecs.hfmax = 70000;
  732. fb_info->monspecs.vfmin = 60;
  733. fb_info->monspecs.vfmax = 60;
  734. } else {
  735. /* Not VGA, using a TV (taken from acornfb) */
  736. fb_info->monspecs.hfmin = 15469;
  737. fb_info->monspecs.hfmax = 15781;
  738. fb_info->monspecs.vfmin = 49;
  739. fb_info->monspecs.vfmax = 51;
  740. }
  741. /*
  742. * XXX: This needs to pull default video output via BIOS or other means
  743. */
  744. if (video_output < 0) {
  745. if (cable_type == CT_VGA) {
  746. video_output = VO_VGA;
  747. } else {
  748. video_output = VO_NTSC;
  749. }
  750. }
  751. /*
  752. * Nothing exciting about the DC PVR2 .. only a measly 8MiB.
  753. */
  754. pvr2_fix.smem_start = 0xa5000000; /* RAM starts here */
  755. pvr2_fix.smem_len = 8 << 20;
  756. pvr2_fix.mmio_start = 0xa05f8000; /* registers start here */
  757. pvr2_fix.mmio_len = 0x2000;
  758. if (request_irq(HW_EVENT_VSYNC, pvr2fb_interrupt, IRQF_SHARED,
  759. "pvr2 VBL handler", fb_info)) {
  760. return -EBUSY;
  761. }
  762. #ifdef CONFIG_SH_DMA
  763. if (request_dma(pvr2dma, "pvr2") != 0) {
  764. free_irq(HW_EVENT_VSYNC, 0);
  765. return -EBUSY;
  766. }
  767. #endif
  768. return pvr2fb_common_init();
  769. }
  770. static void __exit pvr2fb_dc_exit(void)
  771. {
  772. if (fb_info->screen_base) {
  773. iounmap(fb_info->screen_base);
  774. fb_info->screen_base = NULL;
  775. }
  776. if (currentpar->mmio_base) {
  777. iounmap((void *)currentpar->mmio_base);
  778. currentpar->mmio_base = 0;
  779. }
  780. free_irq(HW_EVENT_VSYNC, 0);
  781. #ifdef CONFIG_SH_DMA
  782. free_dma(pvr2dma);
  783. #endif
  784. }
  785. #endif /* CONFIG_SH_DREAMCAST */
  786. #ifdef CONFIG_PCI
  787. static int __devinit pvr2fb_pci_probe(struct pci_dev *pdev,
  788. const struct pci_device_id *ent)
  789. {
  790. int ret;
  791. ret = pci_enable_device(pdev);
  792. if (ret) {
  793. printk(KERN_ERR "pvr2fb: PCI enable failed\n");
  794. return ret;
  795. }
  796. ret = pci_request_regions(pdev, "pvr2fb");
  797. if (ret) {
  798. printk(KERN_ERR "pvr2fb: PCI request regions failed\n");
  799. return ret;
  800. }
  801. /*
  802. * Slightly more exciting than the DC PVR2 .. 16MiB!
  803. */
  804. pvr2_fix.smem_start = pci_resource_start(pdev, 0);
  805. pvr2_fix.smem_len = pci_resource_len(pdev, 0);
  806. pvr2_fix.mmio_start = pci_resource_start(pdev, 1);
  807. pvr2_fix.mmio_len = pci_resource_len(pdev, 1);
  808. fb_info->device = &pdev->dev;
  809. return pvr2fb_common_init();
  810. }
  811. static void __devexit pvr2fb_pci_remove(struct pci_dev *pdev)
  812. {
  813. if (fb_info->screen_base) {
  814. iounmap(fb_info->screen_base);
  815. fb_info->screen_base = NULL;
  816. }
  817. if (currentpar->mmio_base) {
  818. iounmap((void *)currentpar->mmio_base);
  819. currentpar->mmio_base = 0;
  820. }
  821. pci_release_regions(pdev);
  822. }
  823. static struct pci_device_id pvr2fb_pci_tbl[] __devinitdata = {
  824. { PCI_VENDOR_ID_NEC, PCI_DEVICE_ID_NEC_NEON250,
  825. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  826. { 0, },
  827. };
  828. MODULE_DEVICE_TABLE(pci, pvr2fb_pci_tbl);
  829. static struct pci_driver pvr2fb_pci_driver = {
  830. .name = "pvr2fb",
  831. .id_table = pvr2fb_pci_tbl,
  832. .probe = pvr2fb_pci_probe,
  833. .remove = __devexit_p(pvr2fb_pci_remove),
  834. };
  835. static int __init pvr2fb_pci_init(void)
  836. {
  837. return pci_register_driver(&pvr2fb_pci_driver);
  838. }
  839. static void __exit pvr2fb_pci_exit(void)
  840. {
  841. pci_unregister_driver(&pvr2fb_pci_driver);
  842. }
  843. #endif /* CONFIG_PCI */
  844. static int __devinit pvr2_get_param(const struct pvr2_params *p, const char *s,
  845. int val, int size)
  846. {
  847. int i;
  848. for (i = 0 ; i < size ; i++ ) {
  849. if (s != NULL) {
  850. if (!strnicmp(p[i].name, s, strlen(s)))
  851. return p[i].val;
  852. } else {
  853. if (p[i].val == val)
  854. return (int)p[i].name;
  855. }
  856. }
  857. return -1;
  858. }
  859. /*
  860. * Parse command arguments. Supported arguments are:
  861. * inverse Use inverse color maps
  862. * cable:composite|rgb|vga Override the video cable type
  863. * output:NTSC|PAL|VGA Override the video output format
  864. *
  865. * <xres>x<yres>[-<bpp>][@<refresh>] or,
  866. * <name>[-<bpp>][@<refresh>] Startup using this video mode
  867. */
  868. #ifndef MODULE
  869. static int __init pvr2fb_setup(char *options)
  870. {
  871. char *this_opt;
  872. char cable_arg[80];
  873. char output_arg[80];
  874. if (!options || !*options)
  875. return 0;
  876. while ((this_opt = strsep(&options, ","))) {
  877. if (!*this_opt)
  878. continue;
  879. if (!strcmp(this_opt, "inverse")) {
  880. fb_invert_cmaps();
  881. } else if (!strncmp(this_opt, "cable:", 6)) {
  882. strcpy(cable_arg, this_opt + 6);
  883. } else if (!strncmp(this_opt, "output:", 7)) {
  884. strcpy(output_arg, this_opt + 7);
  885. } else if (!strncmp(this_opt, "nopan", 5)) {
  886. nopan = 1;
  887. } else if (!strncmp(this_opt, "nowrap", 6)) {
  888. nowrap = 1;
  889. } else {
  890. mode_option = this_opt;
  891. }
  892. }
  893. if (*cable_arg)
  894. cable_type = pvr2_get_param(cables, cable_arg, 0, 3);
  895. if (*output_arg)
  896. video_output = pvr2_get_param(outputs, output_arg, 0, 3);
  897. return 0;
  898. }
  899. #endif
  900. static struct pvr2_board {
  901. int (*init)(void);
  902. void (*exit)(void);
  903. char name[16];
  904. } board_driver[] = {
  905. #ifdef CONFIG_SH_DREAMCAST
  906. { pvr2fb_dc_init, pvr2fb_dc_exit, "Sega DC PVR2" },
  907. #endif
  908. #ifdef CONFIG_PCI
  909. { pvr2fb_pci_init, pvr2fb_pci_exit, "PCI PVR2" },
  910. #endif
  911. { 0, },
  912. };
  913. static int __init pvr2fb_init(void)
  914. {
  915. int i, ret = -ENODEV;
  916. int size;
  917. #ifndef MODULE
  918. char *option = NULL;
  919. if (fb_get_options("pvr2fb", &option))
  920. return -ENODEV;
  921. pvr2fb_setup(option);
  922. #endif
  923. size = sizeof(struct fb_info) + sizeof(struct pvr2fb_par) + 16 * sizeof(u32);
  924. fb_info = framebuffer_alloc(sizeof(struct pvr2fb_par), NULL);
  925. if (!fb_info) {
  926. printk(KERN_ERR "Failed to allocate memory for fb_info\n");
  927. return -ENOMEM;
  928. }
  929. currentpar = fb_info->par;
  930. for (i = 0; i < ARRAY_SIZE(board_driver); i++) {
  931. struct pvr2_board *pvr_board = board_driver + i;
  932. if (!pvr_board->init)
  933. continue;
  934. ret = pvr_board->init();
  935. if (ret != 0) {
  936. printk(KERN_ERR "pvr2fb: Failed init of %s device\n",
  937. pvr_board->name);
  938. framebuffer_release(fb_info);
  939. break;
  940. }
  941. }
  942. return ret;
  943. }
  944. static void __exit pvr2fb_exit(void)
  945. {
  946. int i;
  947. for (i = 0; i < ARRAY_SIZE(board_driver); i++) {
  948. struct pvr2_board *pvr_board = board_driver + i;
  949. if (pvr_board->exit)
  950. pvr_board->exit();
  951. }
  952. #ifdef CONFIG_SH_STORE_QUEUES
  953. sq_unmap(pvr2fb_map);
  954. #endif
  955. unregister_framebuffer(fb_info);
  956. framebuffer_release(fb_info);
  957. }
  958. module_init(pvr2fb_init);
  959. module_exit(pvr2fb_exit);
  960. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
  961. MODULE_DESCRIPTION("Framebuffer driver for NEC PowerVR 2 based graphics boards");
  962. MODULE_LICENSE("GPL");