pvr2fb.c 31 KB

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