pvr2fb.c 31 KB

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