pvr2fb.c 31 KB

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