fbdev.c 21 KB

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