fbdev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * linux/drivers/video/kyro/fbdev.c
  3. *
  4. * Copyright (C) 2002 STMicroelectronics
  5. * Copyright (C) 2003, 2004 Paul Mundt
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/delay.h>
  18. #include <linux/fb.h>
  19. #include <linux/ioctl.h>
  20. #include <linux/init.h>
  21. #include <linux/pci.h>
  22. #include <asm/io.h>
  23. #include <linux/uaccess.h>
  24. #ifdef CONFIG_MTRR
  25. #include <asm/mtrr.h>
  26. #endif
  27. #include <video/kyro.h>
  28. #include "STG4000Reg.h"
  29. #include "STG4000Interface.h"
  30. /*
  31. * PCI Definitions
  32. */
  33. #define PCI_VENDOR_ID_ST 0x104a
  34. #define PCI_DEVICE_ID_STG4000 0x0010
  35. #define KHZ2PICOS(a) (1000000000UL/(a))
  36. /****************************************************************************/
  37. static struct fb_fix_screeninfo kyro_fix __devinitdata = {
  38. .id = "ST Kyro",
  39. .type = FB_TYPE_PACKED_PIXELS,
  40. .visual = FB_VISUAL_TRUECOLOR,
  41. .accel = FB_ACCEL_NONE,
  42. };
  43. static struct fb_var_screeninfo kyro_var __devinitdata = {
  44. /* 640x480, 16bpp @ 60 Hz */
  45. .xres = 640,
  46. .yres = 480,
  47. .xres_virtual = 640,
  48. .yres_virtual = 480,
  49. .bits_per_pixel = 16,
  50. .red = { 11, 5, 0 },
  51. .green = { 5, 6, 0 },
  52. .blue = { 0, 5, 0 },
  53. .activate = FB_ACTIVATE_NOW,
  54. .height = -1,
  55. .width = -1,
  56. .pixclock = KHZ2PICOS(25175),
  57. .left_margin = 48,
  58. .right_margin = 16,
  59. .upper_margin = 33,
  60. .lower_margin = 10,
  61. .hsync_len = 96,
  62. .vsync_len = 2,
  63. .vmode = FB_VMODE_NONINTERLACED,
  64. };
  65. typedef struct {
  66. STG4000REG __iomem *pSTGReg; /* Virtual address of PCI register region */
  67. u32 ulNextFreeVidMem; /* Offset from start of vid mem to next free region */
  68. u32 ulOverlayOffset; /* Offset from start of vid mem to overlay */
  69. u32 ulOverlayStride; /* Interleaved YUV and 422 mode Y stride */
  70. u32 ulOverlayUVStride; /* 422 mode U & V stride */
  71. } device_info_t;
  72. /* global graphics card info structure (one per card) */
  73. static device_info_t deviceInfo;
  74. static char *mode_option __devinitdata = NULL;
  75. static int nopan __devinitdata = 0;
  76. static int nowrap __devinitdata = 1;
  77. #ifdef CONFIG_MTRR
  78. static int nomtrr __devinitdata = 0;
  79. #endif
  80. /* PCI driver prototypes */
  81. static int kyrofb_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
  82. static void kyrofb_remove(struct pci_dev *pdev);
  83. static struct fb_videomode kyro_modedb[] __devinitdata = {
  84. {
  85. /* 640x350 @ 85Hz */
  86. NULL, 85, 640, 350, KHZ2PICOS(31500),
  87. 96, 32, 60, 32, 64, 3,
  88. FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED
  89. }, {
  90. /* 640x400 @ 85Hz */
  91. NULL, 85, 640, 400, KHZ2PICOS(31500),
  92. 96, 32, 41, 1, 64, 3,
  93. FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  94. }, {
  95. /* 720x400 @ 85Hz */
  96. NULL, 85, 720, 400, KHZ2PICOS(35500),
  97. 108, 36, 42, 1, 72, 3,
  98. FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  99. }, {
  100. /* 640x480 @ 60Hz */
  101. NULL, 60, 640, 480, KHZ2PICOS(25175),
  102. 48, 16, 33, 10, 96, 2,
  103. 0, FB_VMODE_NONINTERLACED
  104. }, {
  105. /* 640x480 @ 72Hz */
  106. NULL, 72, 640, 480, KHZ2PICOS(31500),
  107. 128, 24, 28, 9, 40, 3,
  108. 0, FB_VMODE_NONINTERLACED
  109. }, {
  110. /* 640x480 @ 75Hz */
  111. NULL, 75, 640, 480, KHZ2PICOS(31500),
  112. 120, 16, 16, 1, 64, 3,
  113. 0, FB_VMODE_NONINTERLACED
  114. }, {
  115. /* 640x480 @ 85Hz */
  116. NULL, 85, 640, 480, KHZ2PICOS(36000),
  117. 80, 56, 25, 1, 56, 3,
  118. 0, FB_VMODE_NONINTERLACED
  119. }, {
  120. /* 800x600 @ 56Hz */
  121. NULL, 56, 800, 600, KHZ2PICOS(36000),
  122. 128, 24, 22, 1, 72, 2,
  123. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  124. }, {
  125. /* 800x600 @ 60Hz */
  126. NULL, 60, 800, 600, KHZ2PICOS(40000),
  127. 88, 40, 23, 1, 128, 4,
  128. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  129. }, {
  130. /* 800x600 @ 72Hz */
  131. NULL, 72, 800, 600, KHZ2PICOS(50000),
  132. 64, 56, 23, 37, 120, 6,
  133. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  134. }, {
  135. /* 800x600 @ 75Hz */
  136. NULL, 75, 800, 600, KHZ2PICOS(49500),
  137. 160, 16, 21, 1, 80, 3,
  138. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  139. }, {
  140. /* 800x600 @ 85Hz */
  141. NULL, 85, 800, 600, KHZ2PICOS(56250),
  142. 152, 32, 27, 1, 64, 3,
  143. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  144. }, {
  145. /* 1024x768 @ 60Hz */
  146. NULL, 60, 1024, 768, KHZ2PICOS(65000),
  147. 160, 24, 29, 3, 136, 6,
  148. 0, FB_VMODE_NONINTERLACED
  149. }, {
  150. /* 1024x768 @ 70Hz */
  151. NULL, 70, 1024, 768, KHZ2PICOS(75000),
  152. 144, 24, 29, 3, 136, 6,
  153. 0, FB_VMODE_NONINTERLACED
  154. }, {
  155. /* 1024x768 @ 75Hz */
  156. NULL, 75, 1024, 768, KHZ2PICOS(78750),
  157. 176, 16, 28, 1, 96, 3,
  158. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  159. }, {
  160. /* 1024x768 @ 85Hz */
  161. NULL, 85, 1024, 768, KHZ2PICOS(94500),
  162. 208, 48, 36, 1, 96, 3,
  163. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  164. }, {
  165. /* 1152x864 @ 75Hz */
  166. NULL, 75, 1152, 864, KHZ2PICOS(108000),
  167. 256, 64, 32, 1, 128, 3,
  168. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  169. }, {
  170. /* 1280x960 @ 60Hz */
  171. NULL, 60, 1280, 960, KHZ2PICOS(108000),
  172. 312, 96, 36, 1, 112, 3,
  173. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  174. }, {
  175. /* 1280x960 @ 85Hz */
  176. NULL, 85, 1280, 960, KHZ2PICOS(148500),
  177. 224, 64, 47, 1, 160, 3,
  178. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  179. }, {
  180. /* 1280x1024 @ 60Hz */
  181. NULL, 60, 1280, 1024, KHZ2PICOS(108000),
  182. 248, 48, 38, 1, 112, 3,
  183. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  184. }, {
  185. /* 1280x1024 @ 75Hz */
  186. NULL, 75, 1280, 1024, KHZ2PICOS(135000),
  187. 248, 16, 38, 1, 144, 3,
  188. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  189. }, {
  190. /* 1280x1024 @ 85Hz */
  191. NULL, 85, 1280, 1024, KHZ2PICOS(157500),
  192. 224, 64, 44, 1, 160, 3,
  193. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  194. }, {
  195. /* 1600x1200 @ 60Hz */
  196. NULL, 60, 1600, 1200, KHZ2PICOS(162000),
  197. 304, 64, 46, 1, 192, 3,
  198. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  199. }, {
  200. /* 1600x1200 @ 65Hz */
  201. NULL, 65, 1600, 1200, KHZ2PICOS(175500),
  202. 304, 64, 46, 1, 192, 3,
  203. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  204. }, {
  205. /* 1600x1200 @ 70Hz */
  206. NULL, 70, 1600, 1200, KHZ2PICOS(189000),
  207. 304, 64, 46, 1, 192, 3,
  208. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  209. }, {
  210. /* 1600x1200 @ 75Hz */
  211. NULL, 75, 1600, 1200, KHZ2PICOS(202500),
  212. 304, 64, 46, 1, 192, 3,
  213. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  214. }, {
  215. /* 1600x1200 @ 85Hz */
  216. NULL, 85, 1600, 1200, KHZ2PICOS(229500),
  217. 304, 64, 46, 1, 192, 3,
  218. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  219. }, {
  220. /* 1792x1344 @ 60Hz */
  221. NULL, 60, 1792, 1344, KHZ2PICOS(204750),
  222. 328, 128, 46, 1, 200, 3,
  223. FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  224. }, {
  225. /* 1792x1344 @ 75Hz */
  226. NULL, 75, 1792, 1344, KHZ2PICOS(261000),
  227. 352, 96, 69, 1, 216, 3,
  228. FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  229. }, {
  230. /* 1856x1392 @ 60Hz */
  231. NULL, 60, 1856, 1392, KHZ2PICOS(218250),
  232. 352, 96, 43, 1, 224, 3,
  233. FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  234. }, {
  235. /* 1856x1392 @ 75Hz */
  236. NULL, 75, 1856, 1392, KHZ2PICOS(288000),
  237. 352, 128, 104, 1, 224, 3,
  238. FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  239. }, {
  240. /* 1920x1440 @ 60Hz */
  241. NULL, 60, 1920, 1440, KHZ2PICOS(234000),
  242. 344, 128, 56, 1, 208, 3,
  243. FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  244. }, {
  245. /* 1920x1440 @ 75Hz */
  246. NULL, 75, 1920, 1440, KHZ2PICOS(297000),
  247. 352, 144, 56, 1, 224, 3,
  248. FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
  249. },
  250. };
  251. #define NUM_TOTAL_MODES ARRAY_SIZE(kyro_modedb)
  252. /*
  253. * This needs to be kept ordered corresponding to kyro_modedb.
  254. */
  255. enum {
  256. VMODE_640_350_85,
  257. VMODE_640_400_85,
  258. VMODE_720_400_85,
  259. VMODE_640_480_60,
  260. VMODE_640_480_72,
  261. VMODE_640_480_75,
  262. VMODE_640_480_85,
  263. VMODE_800_600_56,
  264. VMODE_800_600_60,
  265. VMODE_800_600_72,
  266. VMODE_800_600_75,
  267. VMODE_800_600_85,
  268. VMODE_1024_768_60,
  269. VMODE_1024_768_70,
  270. VMODE_1024_768_75,
  271. VMODE_1024_768_85,
  272. VMODE_1152_864_75,
  273. VMODE_1280_960_60,
  274. VMODE_1280_960_85,
  275. VMODE_1280_1024_60,
  276. VMODE_1280_1024_75,
  277. VMODE_1280_1024_85,
  278. VMODE_1600_1200_60,
  279. VMODE_1600_1200_65,
  280. VMODE_1600_1200_70,
  281. VMODE_1600_1200_75,
  282. VMODE_1600_1200_85,
  283. VMODE_1792_1344_60,
  284. VMODE_1792_1344_75,
  285. VMODE_1856_1392_60,
  286. VMODE_1856_1392_75,
  287. VMODE_1920_1440_60,
  288. VMODE_1920_1440_75,
  289. };
  290. /* Accessors */
  291. static int kyro_dev_video_mode_set(struct fb_info *info)
  292. {
  293. struct kyrofb_info *par = info->par;
  294. /* Turn off display */
  295. StopVTG(deviceInfo.pSTGReg);
  296. DisableRamdacOutput(deviceInfo.pSTGReg);
  297. /* Bring us out of VGA and into Hi-Res mode, if not already. */
  298. DisableVGA(deviceInfo.pSTGReg);
  299. if (InitialiseRamdac(deviceInfo.pSTGReg,
  300. info->var.bits_per_pixel,
  301. info->var.xres, info->var.yres,
  302. par->HSP, par->VSP, &par->PIXCLK) < 0)
  303. return -EINVAL;
  304. SetupVTG(deviceInfo.pSTGReg, par);
  305. ResetOverlayRegisters(deviceInfo.pSTGReg);
  306. /* Turn on display in new mode */
  307. EnableRamdacOutput(deviceInfo.pSTGReg);
  308. StartVTG(deviceInfo.pSTGReg);
  309. deviceInfo.ulNextFreeVidMem = info->var.xres * info->var.yres *
  310. info->var.bits_per_pixel;
  311. deviceInfo.ulOverlayOffset = 0;
  312. return 0;
  313. }
  314. static int kyro_dev_overlay_create(u32 ulWidth,
  315. u32 ulHeight, int bLinear)
  316. {
  317. u32 offset;
  318. u32 stride, uvStride;
  319. if (deviceInfo.ulOverlayOffset != 0)
  320. /*
  321. * Can only create one overlay without resetting the card or
  322. * changing display mode
  323. */
  324. return -EINVAL;
  325. ResetOverlayRegisters(deviceInfo.pSTGReg);
  326. /* Overlays are addressed in multiples of 16bytes or 32bytes, so make
  327. * sure the start offset is on an appropriate boundary.
  328. */
  329. offset = deviceInfo.ulNextFreeVidMem;
  330. if ((offset & 0x1f) != 0) {
  331. offset = (offset + 32L) & 0xffffffE0L;
  332. }
  333. if (CreateOverlaySurface(deviceInfo.pSTGReg, ulWidth, ulHeight,
  334. bLinear, offset, &stride, &uvStride) < 0)
  335. return -EINVAL;
  336. deviceInfo.ulOverlayOffset = offset;
  337. deviceInfo.ulOverlayStride = stride;
  338. deviceInfo.ulOverlayUVStride = uvStride;
  339. deviceInfo.ulNextFreeVidMem = offset + (ulHeight * stride) + (ulHeight * 2 * uvStride);
  340. SetOverlayBlendMode(deviceInfo.pSTGReg, GLOBAL_ALPHA, 0xf, 0x0);
  341. return 0;
  342. }
  343. static int kyro_dev_overlay_viewport_set(u32 x, u32 y, u32 ulWidth, u32 ulHeight)
  344. {
  345. if (deviceInfo.ulOverlayOffset == 0)
  346. /* probably haven't called CreateOverlay yet */
  347. return -EINVAL;
  348. /* Stop Ramdac Output */
  349. DisableRamdacOutput(deviceInfo.pSTGReg);
  350. SetOverlayViewPort(deviceInfo.pSTGReg,
  351. x, y, x + ulWidth - 1, y + ulHeight - 1);
  352. EnableOverlayPlane(deviceInfo.pSTGReg);
  353. /* Start Ramdac Output */
  354. EnableRamdacOutput(deviceInfo.pSTGReg);
  355. return 0;
  356. }
  357. static inline unsigned long get_line_length(int x, int bpp)
  358. {
  359. return (unsigned long)((((x*bpp)+31)&~31) >> 3);
  360. }
  361. static int kyrofb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  362. {
  363. struct kyrofb_info *par = info->par;
  364. if (var->bits_per_pixel != 16 && var->bits_per_pixel != 32) {
  365. printk(KERN_WARNING "kyrofb: depth not supported: %u\n", var->bits_per_pixel);
  366. return -EINVAL;
  367. }
  368. switch (var->bits_per_pixel) {
  369. case 16:
  370. var->red.offset = 11;
  371. var->red.length = 5;
  372. var->green.offset = 5;
  373. var->green.length = 6;
  374. var->blue.length = 5;
  375. break;
  376. case 32:
  377. var->transp.offset = 24;
  378. var->red.offset = 16;
  379. var->green.offset = 8;
  380. var->blue.offset = 0;
  381. var->red.length = 8;
  382. var->green.length = 8;
  383. var->blue.length = 8;
  384. var->transp.length = 8;
  385. break;
  386. }
  387. /* Height/Width of picture in mm */
  388. var->height = var->width = -1;
  389. /* Timing information. All values are in picoseconds */
  390. /* par->PIXCLK is in 100Hz units. Convert to picoseconds -
  391. * ensuring we do not exceed 32 bit precision
  392. */
  393. /*
  394. * XXX: Enabling this really screws over the pixclock value when we
  395. * read it back with fbset. As such, leaving this commented out appears
  396. * to do the right thing (at least for now) .. bearing in mind that we
  397. * have infact already done the KHZ2PICOS conversion in both the modedb
  398. * and kyro_var. -- PFM.
  399. */
  400. // var->pixclock = 1000000000 / (par->PIXCLK / 10);
  401. /* the header file claims we should use picoseconds
  402. * - nobody else does though, the all use pixels and lines
  403. * of h and v sizes. Both options here.
  404. */
  405. /*
  406. * If we're being called by __fb_try_mode(), then we don't want to
  407. * override any of the var settings that we've already parsed
  408. * from our modedb. -- PFM.
  409. */
  410. if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_TEST)
  411. return 0;
  412. var->left_margin = par->HBP;
  413. var->hsync_len = par->HST;
  414. var->right_margin = par->HFP;
  415. var->upper_margin = par->VBP;
  416. var->vsync_len = par->VST;
  417. var->lower_margin = par->VFP;
  418. if (par->HSP == 1)
  419. var->sync |= FB_SYNC_HOR_HIGH_ACT;
  420. if (par->VSP == 1)
  421. var->sync |= FB_SYNC_VERT_HIGH_ACT;
  422. return 0;
  423. }
  424. static int kyrofb_set_par(struct fb_info *info)
  425. {
  426. struct kyrofb_info *par = info->par;
  427. unsigned long lineclock;
  428. unsigned long frameclock;
  429. /* Actual resolution */
  430. par->XRES = info->var.xres;
  431. par->YRES = info->var.yres;
  432. /* pixel depth */
  433. par->PIXDEPTH = info->var.bits_per_pixel;
  434. /* Refresh rate */
  435. /* time for a line in ns */
  436. lineclock = (info->var.pixclock * (info->var.xres +
  437. info->var.right_margin +
  438. info->var.hsync_len +
  439. info->var.left_margin)) / 1000;
  440. /* time for a frame in ns (precision in 32bpp) */
  441. frameclock = lineclock * (info->var.yres +
  442. info->var.lower_margin +
  443. info->var.vsync_len +
  444. info->var.upper_margin);
  445. /* Calculate refresh rate and horrizontal clocks */
  446. par->VFREQ = (1000000000 + (frameclock / 2)) / frameclock;
  447. par->HCLK = (1000000000 + (lineclock / 2)) / lineclock;
  448. par->PIXCLK = ((1000000000 + (info->var.pixclock / 2))
  449. / info->var.pixclock) * 10;
  450. /* calculate horizontal timings */
  451. par->HFP = info->var.right_margin;
  452. par->HST = info->var.hsync_len;
  453. par->HBP = info->var.left_margin;
  454. par->HTot = par->XRES + par->HBP + par->HST + par->HFP;
  455. /* calculate vertical timings */
  456. par->VFP = info->var.lower_margin;
  457. par->VST = info->var.vsync_len;
  458. par->VBP = info->var.upper_margin;
  459. par->VTot = par->YRES + par->VBP + par->VST + par->VFP;
  460. par->HSP = (info->var.sync & FB_SYNC_HOR_HIGH_ACT) ? 1 : 0;
  461. par->VSP = (info->var.sync & FB_SYNC_VERT_HIGH_ACT) ? 1 : 0;
  462. kyro_dev_video_mode_set(info);
  463. /* length of a line in bytes */
  464. info->fix.line_length = get_line_length(par->XRES, par->PIXDEPTH);
  465. info->fix.visual = FB_VISUAL_TRUECOLOR;
  466. return 0;
  467. }
  468. static int kyrofb_setcolreg(u_int regno, u_int red, u_int green,
  469. u_int blue, u_int transp, struct fb_info *info)
  470. {
  471. struct kyrofb_info *par = info->par;
  472. if (regno > 255)
  473. return 1; /* Invalid register */
  474. if (regno < 16) {
  475. switch (info->var.bits_per_pixel) {
  476. case 16:
  477. par->palette[regno] =
  478. (red & 0xf800) |
  479. ((green & 0xfc00) >> 5) |
  480. ((blue & 0xf800) >> 11);
  481. break;
  482. case 32:
  483. red >>= 8; green >>= 8; blue >>= 8; transp >>= 8;
  484. par->palette[regno] =
  485. (transp << 24) | (red << 16) | (green << 8) | blue;
  486. break;
  487. }
  488. }
  489. return 0;
  490. }
  491. #ifndef MODULE
  492. static int __init kyrofb_setup(char *options)
  493. {
  494. char *this_opt;
  495. if (!options || !*options)
  496. return 0;
  497. while ((this_opt = strsep(&options, ","))) {
  498. if (!*this_opt)
  499. continue;
  500. if (strcmp(this_opt, "nopan") == 0) {
  501. nopan = 1;
  502. } else if (strcmp(this_opt, "nowrap") == 0) {
  503. nowrap = 1;
  504. #ifdef CONFIG_MTRR
  505. } else if (strcmp(this_opt, "nomtrr") == 0) {
  506. nomtrr = 1;
  507. #endif
  508. } else {
  509. mode_option = this_opt;
  510. }
  511. }
  512. return 0;
  513. }
  514. #endif
  515. static int kyrofb_ioctl(struct fb_info *info,
  516. unsigned int cmd, unsigned long arg)
  517. {
  518. overlay_create ol_create;
  519. overlay_viewport_set ol_viewport_set;
  520. void __user *argp = (void __user *)arg;
  521. switch (cmd) {
  522. case KYRO_IOCTL_OVERLAY_CREATE:
  523. if (copy_from_user(&ol_create, argp, sizeof(overlay_create)))
  524. return -EFAULT;
  525. if (kyro_dev_overlay_create(ol_create.ulWidth,
  526. ol_create.ulHeight, 0) < 0) {
  527. printk(KERN_ERR "Kyro FB: failed to create overlay surface.\n");
  528. return -EINVAL;
  529. }
  530. break;
  531. case KYRO_IOCTL_OVERLAY_VIEWPORT_SET:
  532. if (copy_from_user(&ol_viewport_set, argp,
  533. sizeof(overlay_viewport_set)))
  534. return -EFAULT;
  535. if (kyro_dev_overlay_viewport_set(ol_viewport_set.xOrgin,
  536. ol_viewport_set.yOrgin,
  537. ol_viewport_set.xSize,
  538. ol_viewport_set.ySize) != 0)
  539. {
  540. printk(KERN_ERR "Kyro FB: failed to create overlay viewport.\n");
  541. return -EINVAL;
  542. }
  543. break;
  544. case KYRO_IOCTL_SET_VIDEO_MODE:
  545. {
  546. printk(KERN_ERR "Kyro FB: KYRO_IOCTL_SET_VIDEO_MODE is"
  547. "obsolete, use the appropriate fb_ioctl()"
  548. "command instead.\n");
  549. return -EINVAL;
  550. }
  551. break;
  552. case KYRO_IOCTL_UVSTRIDE:
  553. if (copy_to_user(argp, &deviceInfo.ulOverlayUVStride, sizeof(deviceInfo.ulOverlayUVStride)))
  554. return -EFAULT;
  555. break;
  556. case KYRO_IOCTL_STRIDE:
  557. if (copy_to_user(argp, &deviceInfo.ulOverlayStride, sizeof(deviceInfo.ulOverlayStride)))
  558. return -EFAULT;
  559. break;
  560. case KYRO_IOCTL_OVERLAY_OFFSET:
  561. if (copy_to_user(argp, &deviceInfo.ulOverlayOffset, sizeof(deviceInfo.ulOverlayOffset)))
  562. return -EFAULT;
  563. break;
  564. }
  565. return 0;
  566. }
  567. static struct pci_device_id kyrofb_pci_tbl[] = {
  568. { PCI_VENDOR_ID_ST, PCI_DEVICE_ID_STG4000,
  569. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  570. { 0, }
  571. };
  572. MODULE_DEVICE_TABLE(pci, kyrofb_pci_tbl);
  573. static struct pci_driver kyrofb_pci_driver = {
  574. .name = "kyrofb",
  575. .id_table = kyrofb_pci_tbl,
  576. .probe = kyrofb_probe,
  577. .remove = __devexit_p(kyrofb_remove),
  578. };
  579. static struct fb_ops kyrofb_ops = {
  580. .owner = THIS_MODULE,
  581. .fb_check_var = kyrofb_check_var,
  582. .fb_set_par = kyrofb_set_par,
  583. .fb_setcolreg = kyrofb_setcolreg,
  584. .fb_ioctl = kyrofb_ioctl,
  585. .fb_fillrect = cfb_fillrect,
  586. .fb_copyarea = cfb_copyarea,
  587. .fb_imageblit = cfb_imageblit,
  588. };
  589. static int __devinit kyrofb_probe(struct pci_dev *pdev,
  590. const struct pci_device_id *ent)
  591. {
  592. struct fb_info *info;
  593. struct kyrofb_info *currentpar;
  594. unsigned long size;
  595. int err;
  596. if ((err = pci_enable_device(pdev))) {
  597. printk(KERN_WARNING "kyrofb: Can't enable pdev: %d\n", err);
  598. return err;
  599. }
  600. info = framebuffer_alloc(sizeof(struct kyrofb_info), &pdev->dev);
  601. if (!info)
  602. return -ENOMEM;
  603. currentpar = info->par;
  604. kyro_fix.smem_start = pci_resource_start(pdev, 0);
  605. kyro_fix.smem_len = pci_resource_len(pdev, 0);
  606. kyro_fix.mmio_start = pci_resource_start(pdev, 1);
  607. kyro_fix.mmio_len = pci_resource_len(pdev, 1);
  608. currentpar->regbase = deviceInfo.pSTGReg =
  609. ioremap_nocache(kyro_fix.mmio_start, kyro_fix.mmio_len);
  610. info->screen_base = ioremap_nocache(kyro_fix.smem_start,
  611. kyro_fix.smem_len);
  612. #ifdef CONFIG_MTRR
  613. if (!nomtrr)
  614. currentpar->mtrr_handle =
  615. mtrr_add(kyro_fix.smem_start,
  616. kyro_fix.smem_len,
  617. MTRR_TYPE_WRCOMB, 1);
  618. #endif
  619. kyro_fix.ypanstep = nopan ? 0 : 1;
  620. kyro_fix.ywrapstep = nowrap ? 0 : 1;
  621. info->fbops = &kyrofb_ops;
  622. info->fix = kyro_fix;
  623. info->pseudo_palette = currentpar->palette;
  624. info->flags = FBINFO_DEFAULT;
  625. SetCoreClockPLL(deviceInfo.pSTGReg, pdev);
  626. deviceInfo.ulNextFreeVidMem = 0;
  627. deviceInfo.ulOverlayOffset = 0;
  628. /* This should give a reasonable default video mode */
  629. if (!fb_find_mode(&info->var, info, mode_option, kyro_modedb,
  630. NUM_TOTAL_MODES, &kyro_modedb[VMODE_1024_768_75], 32))
  631. info->var = kyro_var;
  632. fb_alloc_cmap(&info->cmap, 256, 0);
  633. kyrofb_set_par(info);
  634. kyrofb_check_var(&info->var, info);
  635. size = get_line_length(info->var.xres_virtual,
  636. info->var.bits_per_pixel);
  637. size *= info->var.yres_virtual;
  638. fb_memset(info->screen_base, 0, size);
  639. if (register_framebuffer(info) < 0)
  640. goto out_unmap;
  641. printk("fb%d: %s frame buffer device, at %dx%d@%d using %ldk/%ldk of VRAM\n",
  642. info->node, info->fix.id, info->var.xres,
  643. info->var.yres, info->var.bits_per_pixel, size >> 10,
  644. (unsigned long)info->fix.smem_len >> 10);
  645. pci_set_drvdata(pdev, info);
  646. return 0;
  647. out_unmap:
  648. iounmap(currentpar->regbase);
  649. iounmap(info->screen_base);
  650. framebuffer_release(info);
  651. return -EINVAL;
  652. }
  653. static void __devexit kyrofb_remove(struct pci_dev *pdev)
  654. {
  655. struct fb_info *info = pci_get_drvdata(pdev);
  656. struct kyrofb_info *par = info->par;
  657. /* Reset the board */
  658. StopVTG(deviceInfo.pSTGReg);
  659. DisableRamdacOutput(deviceInfo.pSTGReg);
  660. /* Sync up the PLL */
  661. SetCoreClockPLL(deviceInfo.pSTGReg, pdev);
  662. deviceInfo.ulNextFreeVidMem = 0;
  663. deviceInfo.ulOverlayOffset = 0;
  664. iounmap(info->screen_base);
  665. iounmap(par->regbase);
  666. #ifdef CONFIG_MTRR
  667. if (par->mtrr_handle)
  668. mtrr_del(par->mtrr_handle,
  669. info->fix.smem_start,
  670. info->fix.smem_len);
  671. #endif
  672. unregister_framebuffer(info);
  673. pci_set_drvdata(pdev, NULL);
  674. framebuffer_release(info);
  675. }
  676. static int __init kyrofb_init(void)
  677. {
  678. #ifndef MODULE
  679. char *option = NULL;
  680. if (fb_get_options("kyrofb", &option))
  681. return -ENODEV;
  682. kyrofb_setup(option);
  683. #endif
  684. return pci_register_driver(&kyrofb_pci_driver);
  685. }
  686. static void __exit kyrofb_exit(void)
  687. {
  688. pci_unregister_driver(&kyrofb_pci_driver);
  689. }
  690. module_init(kyrofb_init);
  691. #ifdef MODULE
  692. module_exit(kyrofb_exit);
  693. #endif
  694. MODULE_AUTHOR("STMicroelectronics; Paul Mundt <lethal@linux-sh.org>");
  695. MODULE_LICENSE("GPL");