acornfb.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/video/acornfb.c
  4. *
  5. * Copyright (C) 1998-2001 Russell King
  6. *
  7. * Frame buffer code for Acorn platforms
  8. *
  9. * NOTE: Most of the modes with X!=640 will disappear shortly.
  10. * NOTE: Startup setting of HS & VS polarity not supported.
  11. * (do we need to support it if we're coming up in 640x480?)
  12. *
  13. * FIXME: (things broken by the "new improved" FBCON API)
  14. * - Blanking 8bpp displays with VIDC
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/string.h>
  20. #include <linux/ctype.h>
  21. #include <linux/mm.h>
  22. #include <linux/init.h>
  23. #include <linux/fb.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/io.h>
  27. #include <linux/gfp.h>
  28. #include <mach/hardware.h>
  29. #include <asm/irq.h>
  30. #include <asm/mach-types.h>
  31. #include "acornfb.h"
  32. /*
  33. * Default resolution.
  34. * NOTE that it has to be supported in the table towards
  35. * the end of this file.
  36. */
  37. #define DEFAULT_XRES 640
  38. #define DEFAULT_YRES 480
  39. #define DEFAULT_BPP 4
  40. /*
  41. * define this to debug the video mode selection
  42. */
  43. #undef DEBUG_MODE_SELECTION
  44. /*
  45. * Translation from RISC OS monitor types to actual
  46. * HSYNC and VSYNC frequency ranges. These are
  47. * probably not right, but they're the best info I
  48. * have. Allow 1% either way on the nominal for TVs.
  49. */
  50. #define NR_MONTYPES 6
  51. static struct fb_monspecs monspecs[NR_MONTYPES] = {
  52. { /* TV */
  53. .hfmin = 15469,
  54. .hfmax = 15781,
  55. .vfmin = 49,
  56. .vfmax = 51,
  57. }, { /* Multi Freq */
  58. .hfmin = 0,
  59. .hfmax = 99999,
  60. .vfmin = 0,
  61. .vfmax = 199,
  62. }, { /* Hi-res mono */
  63. .hfmin = 58608,
  64. .hfmax = 58608,
  65. .vfmin = 64,
  66. .vfmax = 64,
  67. }, { /* VGA */
  68. .hfmin = 30000,
  69. .hfmax = 70000,
  70. .vfmin = 60,
  71. .vfmax = 60,
  72. }, { /* SVGA */
  73. .hfmin = 30000,
  74. .hfmax = 70000,
  75. .vfmin = 56,
  76. .vfmax = 75,
  77. }, {
  78. .hfmin = 30000,
  79. .hfmax = 70000,
  80. .vfmin = 60,
  81. .vfmax = 60,
  82. }
  83. };
  84. static struct fb_info fb_info;
  85. static struct acornfb_par current_par;
  86. static struct vidc_timing current_vidc;
  87. extern unsigned int vram_size; /* set by setup.c */
  88. #ifdef HAS_VIDC20
  89. #include <mach/acornfb.h>
  90. #define MAX_SIZE (2*1024*1024)
  91. /* VIDC20 has a different set of rules from the VIDC:
  92. * hcr : must be multiple of 4
  93. * hswr : must be even
  94. * hdsr : must be even
  95. * hder : must be even
  96. * vcr : >= 2, (interlace, must be odd)
  97. * vswr : >= 1
  98. * vdsr : >= 1
  99. * vder : >= vdsr
  100. */
  101. static void acornfb_set_timing(struct fb_info *info)
  102. {
  103. struct fb_var_screeninfo *var = &info->var;
  104. struct vidc_timing vidc;
  105. u_int vcr, fsize;
  106. u_int ext_ctl, dat_ctl;
  107. u_int words_per_line;
  108. memset(&vidc, 0, sizeof(vidc));
  109. vidc.h_sync_width = var->hsync_len - 8;
  110. vidc.h_border_start = vidc.h_sync_width + var->left_margin + 8 - 12;
  111. vidc.h_display_start = vidc.h_border_start + 12 - 18;
  112. vidc.h_display_end = vidc.h_display_start + var->xres;
  113. vidc.h_border_end = vidc.h_display_end + 18 - 12;
  114. vidc.h_cycle = vidc.h_border_end + var->right_margin + 12 - 8;
  115. vidc.h_interlace = vidc.h_cycle / 2;
  116. vidc.v_sync_width = var->vsync_len - 1;
  117. vidc.v_border_start = vidc.v_sync_width + var->upper_margin;
  118. vidc.v_display_start = vidc.v_border_start;
  119. vidc.v_display_end = vidc.v_display_start + var->yres;
  120. vidc.v_border_end = vidc.v_display_end;
  121. vidc.control = acornfb_default_control();
  122. vcr = var->vsync_len + var->upper_margin + var->yres +
  123. var->lower_margin;
  124. if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
  125. vidc.v_cycle = (vcr - 3) / 2;
  126. vidc.control |= VIDC20_CTRL_INT;
  127. } else
  128. vidc.v_cycle = vcr - 2;
  129. switch (var->bits_per_pixel) {
  130. case 1: vidc.control |= VIDC20_CTRL_1BPP; break;
  131. case 2: vidc.control |= VIDC20_CTRL_2BPP; break;
  132. case 4: vidc.control |= VIDC20_CTRL_4BPP; break;
  133. default:
  134. case 8: vidc.control |= VIDC20_CTRL_8BPP; break;
  135. case 16: vidc.control |= VIDC20_CTRL_16BPP; break;
  136. case 32: vidc.control |= VIDC20_CTRL_32BPP; break;
  137. }
  138. acornfb_vidc20_find_rates(&vidc, var);
  139. fsize = var->vsync_len + var->upper_margin + var->lower_margin - 1;
  140. if (memcmp(&current_vidc, &vidc, sizeof(vidc))) {
  141. current_vidc = vidc;
  142. vidc_writel(VIDC20_CTRL | vidc.control);
  143. vidc_writel(0xd0000000 | vidc.pll_ctl);
  144. vidc_writel(0x80000000 | vidc.h_cycle);
  145. vidc_writel(0x81000000 | vidc.h_sync_width);
  146. vidc_writel(0x82000000 | vidc.h_border_start);
  147. vidc_writel(0x83000000 | vidc.h_display_start);
  148. vidc_writel(0x84000000 | vidc.h_display_end);
  149. vidc_writel(0x85000000 | vidc.h_border_end);
  150. vidc_writel(0x86000000);
  151. vidc_writel(0x87000000 | vidc.h_interlace);
  152. vidc_writel(0x90000000 | vidc.v_cycle);
  153. vidc_writel(0x91000000 | vidc.v_sync_width);
  154. vidc_writel(0x92000000 | vidc.v_border_start);
  155. vidc_writel(0x93000000 | vidc.v_display_start);
  156. vidc_writel(0x94000000 | vidc.v_display_end);
  157. vidc_writel(0x95000000 | vidc.v_border_end);
  158. vidc_writel(0x96000000);
  159. vidc_writel(0x97000000);
  160. }
  161. iomd_writel(fsize, IOMD_FSIZE);
  162. ext_ctl = acornfb_default_econtrol();
  163. if (var->sync & FB_SYNC_COMP_HIGH_ACT) /* should be FB_SYNC_COMP */
  164. ext_ctl |= VIDC20_ECTL_HS_NCSYNC | VIDC20_ECTL_VS_NCSYNC;
  165. else {
  166. if (var->sync & FB_SYNC_HOR_HIGH_ACT)
  167. ext_ctl |= VIDC20_ECTL_HS_HSYNC;
  168. else
  169. ext_ctl |= VIDC20_ECTL_HS_NHSYNC;
  170. if (var->sync & FB_SYNC_VERT_HIGH_ACT)
  171. ext_ctl |= VIDC20_ECTL_VS_VSYNC;
  172. else
  173. ext_ctl |= VIDC20_ECTL_VS_NVSYNC;
  174. }
  175. vidc_writel(VIDC20_ECTL | ext_ctl);
  176. words_per_line = var->xres * var->bits_per_pixel / 32;
  177. if (current_par.using_vram && info->fix.smem_len == 2048*1024)
  178. words_per_line /= 2;
  179. /* RiscPC doesn't use the VIDC's VRAM control. */
  180. dat_ctl = VIDC20_DCTL_VRAM_DIS | VIDC20_DCTL_SNA | words_per_line;
  181. /* The data bus width is dependent on both the type
  182. * and amount of video memory.
  183. * DRAM 32bit low
  184. * 1MB VRAM 32bit
  185. * 2MB VRAM 64bit
  186. */
  187. if (current_par.using_vram && current_par.vram_half_sam == 2048)
  188. dat_ctl |= VIDC20_DCTL_BUS_D63_0;
  189. else
  190. dat_ctl |= VIDC20_DCTL_BUS_D31_0;
  191. vidc_writel(VIDC20_DCTL | dat_ctl);
  192. #ifdef DEBUG_MODE_SELECTION
  193. printk(KERN_DEBUG "VIDC registers for %dx%dx%d:\n", var->xres,
  194. var->yres, var->bits_per_pixel);
  195. printk(KERN_DEBUG " H-cycle : %d\n", vidc.h_cycle);
  196. printk(KERN_DEBUG " H-sync-width : %d\n", vidc.h_sync_width);
  197. printk(KERN_DEBUG " H-border-start : %d\n", vidc.h_border_start);
  198. printk(KERN_DEBUG " H-display-start : %d\n", vidc.h_display_start);
  199. printk(KERN_DEBUG " H-display-end : %d\n", vidc.h_display_end);
  200. printk(KERN_DEBUG " H-border-end : %d\n", vidc.h_border_end);
  201. printk(KERN_DEBUG " H-interlace : %d\n", vidc.h_interlace);
  202. printk(KERN_DEBUG " V-cycle : %d\n", vidc.v_cycle);
  203. printk(KERN_DEBUG " V-sync-width : %d\n", vidc.v_sync_width);
  204. printk(KERN_DEBUG " V-border-start : %d\n", vidc.v_border_start);
  205. printk(KERN_DEBUG " V-display-start : %d\n", vidc.v_display_start);
  206. printk(KERN_DEBUG " V-display-end : %d\n", vidc.v_display_end);
  207. printk(KERN_DEBUG " V-border-end : %d\n", vidc.v_border_end);
  208. printk(KERN_DEBUG " Ext Ctrl (C) : 0x%08X\n", ext_ctl);
  209. printk(KERN_DEBUG " PLL Ctrl (D) : 0x%08X\n", vidc.pll_ctl);
  210. printk(KERN_DEBUG " Ctrl (E) : 0x%08X\n", vidc.control);
  211. printk(KERN_DEBUG " Data Ctrl (F) : 0x%08X\n", dat_ctl);
  212. printk(KERN_DEBUG " Fsize : 0x%08X\n", fsize);
  213. #endif
  214. }
  215. /*
  216. * We have to take note of the VIDC20's 16-bit palette here.
  217. * The VIDC20 looks up a 16 bit pixel as follows:
  218. *
  219. * bits 111111
  220. * 5432109876543210
  221. * red ++++++++ (8 bits, 7 to 0)
  222. * green ++++++++ (8 bits, 11 to 4)
  223. * blue ++++++++ (8 bits, 15 to 8)
  224. *
  225. * We use a pixel which looks like:
  226. *
  227. * bits 111111
  228. * 5432109876543210
  229. * red +++++ (5 bits, 4 to 0)
  230. * green +++++ (5 bits, 9 to 5)
  231. * blue +++++ (5 bits, 14 to 10)
  232. */
  233. static int
  234. acornfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  235. u_int trans, struct fb_info *info)
  236. {
  237. union palette pal;
  238. if (regno >= current_par.palette_size)
  239. return 1;
  240. if (regno < 16 && info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  241. u32 pseudo_val;
  242. pseudo_val = regno << info->var.red.offset;
  243. pseudo_val |= regno << info->var.green.offset;
  244. pseudo_val |= regno << info->var.blue.offset;
  245. ((u32 *)info->pseudo_palette)[regno] = pseudo_val;
  246. }
  247. pal.p = 0;
  248. pal.vidc20.red = red >> 8;
  249. pal.vidc20.green = green >> 8;
  250. pal.vidc20.blue = blue >> 8;
  251. current_par.palette[regno] = pal;
  252. if (info->var.bits_per_pixel == 16) {
  253. int i;
  254. pal.p = 0;
  255. vidc_writel(0x10000000);
  256. for (i = 0; i < 256; i += 1) {
  257. pal.vidc20.red = current_par.palette[i & 31].vidc20.red;
  258. pal.vidc20.green = current_par.palette[(i >> 1) & 31].vidc20.green;
  259. pal.vidc20.blue = current_par.palette[(i >> 2) & 31].vidc20.blue;
  260. vidc_writel(pal.p);
  261. /* Palette register pointer auto-increments */
  262. }
  263. } else {
  264. vidc_writel(0x10000000 | regno);
  265. vidc_writel(pal.p);
  266. }
  267. return 0;
  268. }
  269. #endif
  270. /*
  271. * Before selecting the timing parameters, adjust
  272. * the resolution to fit the rules.
  273. */
  274. static int
  275. acornfb_adjust_timing(struct fb_info *info, struct fb_var_screeninfo *var, u_int fontht)
  276. {
  277. u_int font_line_len, sam_size, min_size, size, nr_y;
  278. /* xres must be even */
  279. var->xres = (var->xres + 1) & ~1;
  280. /*
  281. * We don't allow xres_virtual to differ from xres
  282. */
  283. var->xres_virtual = var->xres;
  284. var->xoffset = 0;
  285. if (current_par.using_vram)
  286. sam_size = current_par.vram_half_sam * 2;
  287. else
  288. sam_size = 16;
  289. /*
  290. * Now, find a value for yres_virtual which allows
  291. * us to do ywrap scrolling. The value of
  292. * yres_virtual must be such that the end of the
  293. * displayable frame buffer must be aligned with
  294. * the start of a font line.
  295. */
  296. font_line_len = var->xres * var->bits_per_pixel * fontht / 8;
  297. min_size = var->xres * var->yres * var->bits_per_pixel / 8;
  298. /*
  299. * If minimum screen size is greater than that we have
  300. * available, reject it.
  301. */
  302. if (min_size > info->fix.smem_len)
  303. return -EINVAL;
  304. /* Find int 'y', such that y * fll == s * sam < maxsize
  305. * y = s * sam / fll; s = maxsize / sam
  306. */
  307. for (size = info->fix.smem_len;
  308. nr_y = size / font_line_len, min_size <= size;
  309. size -= sam_size) {
  310. if (nr_y * font_line_len == size)
  311. break;
  312. }
  313. nr_y *= fontht;
  314. if (var->accel_flags & FB_ACCELF_TEXT) {
  315. if (min_size > size) {
  316. /*
  317. * failed, use ypan
  318. */
  319. size = info->fix.smem_len;
  320. var->yres_virtual = size / (font_line_len / fontht);
  321. } else
  322. var->yres_virtual = nr_y;
  323. } else if (var->yres_virtual > nr_y)
  324. var->yres_virtual = nr_y;
  325. current_par.screen_end = info->fix.smem_start + size;
  326. /*
  327. * Fix yres & yoffset if needed.
  328. */
  329. if (var->yres > var->yres_virtual)
  330. var->yres = var->yres_virtual;
  331. if (var->vmode & FB_VMODE_YWRAP) {
  332. if (var->yoffset > var->yres_virtual)
  333. var->yoffset = var->yres_virtual;
  334. } else {
  335. if (var->yoffset + var->yres > var->yres_virtual)
  336. var->yoffset = var->yres_virtual - var->yres;
  337. }
  338. /* hsync_len must be even */
  339. var->hsync_len = (var->hsync_len + 1) & ~1;
  340. #if defined(HAS_VIDC20)
  341. /* left_margin must be even */
  342. if (var->left_margin & 1) {
  343. var->left_margin += 1;
  344. var->right_margin -= 1;
  345. }
  346. /* right_margin must be even */
  347. if (var->right_margin & 1)
  348. var->right_margin += 1;
  349. #endif
  350. if (var->vsync_len < 1)
  351. var->vsync_len = 1;
  352. return 0;
  353. }
  354. static int
  355. acornfb_validate_timing(struct fb_var_screeninfo *var,
  356. struct fb_monspecs *monspecs)
  357. {
  358. unsigned long hs, vs;
  359. /*
  360. * hs(Hz) = 10^12 / (pixclock * xtotal)
  361. * vs(Hz) = hs(Hz) / ytotal
  362. *
  363. * No need to do long long divisions or anything
  364. * like that if you factor it correctly
  365. */
  366. hs = 1953125000 / var->pixclock;
  367. hs = hs * 512 /
  368. (var->xres + var->left_margin + var->right_margin + var->hsync_len);
  369. vs = hs /
  370. (var->yres + var->upper_margin + var->lower_margin + var->vsync_len);
  371. return (vs >= monspecs->vfmin && vs <= monspecs->vfmax &&
  372. hs >= monspecs->hfmin && hs <= monspecs->hfmax) ? 0 : -EINVAL;
  373. }
  374. static inline void
  375. acornfb_update_dma(struct fb_info *info, struct fb_var_screeninfo *var)
  376. {
  377. u_int off = var->yoffset * info->fix.line_length;
  378. #if defined(HAS_MEMC)
  379. memc_write(VDMA_INIT, off >> 2);
  380. #elif defined(HAS_IOMD)
  381. iomd_writel(info->fix.smem_start + off, IOMD_VIDINIT);
  382. #endif
  383. }
  384. static int
  385. acornfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  386. {
  387. u_int fontht;
  388. int err;
  389. /*
  390. * FIXME: Find the font height
  391. */
  392. fontht = 8;
  393. var->red.msb_right = 0;
  394. var->green.msb_right = 0;
  395. var->blue.msb_right = 0;
  396. var->transp.msb_right = 0;
  397. switch (var->bits_per_pixel) {
  398. case 1: case 2: case 4: case 8:
  399. var->red.offset = 0;
  400. var->red.length = var->bits_per_pixel;
  401. var->green = var->red;
  402. var->blue = var->red;
  403. var->transp.offset = 0;
  404. var->transp.length = 0;
  405. break;
  406. #ifdef HAS_VIDC20
  407. case 16:
  408. var->red.offset = 0;
  409. var->red.length = 5;
  410. var->green.offset = 5;
  411. var->green.length = 5;
  412. var->blue.offset = 10;
  413. var->blue.length = 5;
  414. var->transp.offset = 15;
  415. var->transp.length = 1;
  416. break;
  417. case 32:
  418. var->red.offset = 0;
  419. var->red.length = 8;
  420. var->green.offset = 8;
  421. var->green.length = 8;
  422. var->blue.offset = 16;
  423. var->blue.length = 8;
  424. var->transp.offset = 24;
  425. var->transp.length = 4;
  426. break;
  427. #endif
  428. default:
  429. return -EINVAL;
  430. }
  431. /*
  432. * Check to see if the pixel rate is valid.
  433. */
  434. if (!acornfb_valid_pixrate(var))
  435. return -EINVAL;
  436. /*
  437. * Validate and adjust the resolution to
  438. * match the video generator hardware.
  439. */
  440. err = acornfb_adjust_timing(info, var, fontht);
  441. if (err)
  442. return err;
  443. /*
  444. * Validate the timing against the
  445. * monitor hardware.
  446. */
  447. return acornfb_validate_timing(var, &info->monspecs);
  448. }
  449. static int acornfb_set_par(struct fb_info *info)
  450. {
  451. switch (info->var.bits_per_pixel) {
  452. case 1:
  453. current_par.palette_size = 2;
  454. info->fix.visual = FB_VISUAL_MONO10;
  455. break;
  456. case 2:
  457. current_par.palette_size = 4;
  458. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  459. break;
  460. case 4:
  461. current_par.palette_size = 16;
  462. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  463. break;
  464. case 8:
  465. current_par.palette_size = VIDC_PALETTE_SIZE;
  466. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  467. break;
  468. #ifdef HAS_VIDC20
  469. case 16:
  470. current_par.palette_size = 32;
  471. info->fix.visual = FB_VISUAL_DIRECTCOLOR;
  472. break;
  473. case 32:
  474. current_par.palette_size = VIDC_PALETTE_SIZE;
  475. info->fix.visual = FB_VISUAL_DIRECTCOLOR;
  476. break;
  477. #endif
  478. default:
  479. BUG();
  480. }
  481. info->fix.line_length = (info->var.xres * info->var.bits_per_pixel) / 8;
  482. #if defined(HAS_MEMC)
  483. {
  484. unsigned long size = info->fix.smem_len - VDMA_XFERSIZE;
  485. memc_write(VDMA_START, 0);
  486. memc_write(VDMA_END, size >> 2);
  487. }
  488. #elif defined(HAS_IOMD)
  489. {
  490. unsigned long start, size;
  491. u_int control;
  492. start = info->fix.smem_start;
  493. size = current_par.screen_end;
  494. if (current_par.using_vram) {
  495. size -= current_par.vram_half_sam;
  496. control = DMA_CR_E | (current_par.vram_half_sam / 256);
  497. } else {
  498. size -= 16;
  499. control = DMA_CR_E | DMA_CR_D | 16;
  500. }
  501. iomd_writel(start, IOMD_VIDSTART);
  502. iomd_writel(size, IOMD_VIDEND);
  503. iomd_writel(control, IOMD_VIDCR);
  504. }
  505. #endif
  506. acornfb_update_dma(info, &info->var);
  507. acornfb_set_timing(info);
  508. return 0;
  509. }
  510. static int
  511. acornfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
  512. {
  513. u_int y_bottom = var->yoffset;
  514. if (!(var->vmode & FB_VMODE_YWRAP))
  515. y_bottom += info->var.yres;
  516. if (y_bottom > info->var.yres_virtual)
  517. return -EINVAL;
  518. acornfb_update_dma(info, var);
  519. return 0;
  520. }
  521. static const struct fb_ops acornfb_ops = {
  522. .owner = THIS_MODULE,
  523. .fb_check_var = acornfb_check_var,
  524. .fb_set_par = acornfb_set_par,
  525. .fb_setcolreg = acornfb_setcolreg,
  526. .fb_pan_display = acornfb_pan_display,
  527. .fb_fillrect = cfb_fillrect,
  528. .fb_copyarea = cfb_copyarea,
  529. .fb_imageblit = cfb_imageblit,
  530. };
  531. /*
  532. * Everything after here is initialisation!!!
  533. */
  534. static struct fb_videomode modedb[] = {
  535. { /* 320x256 @ 50Hz */
  536. NULL, 50, 320, 256, 125000, 92, 62, 35, 19, 38, 2,
  537. FB_SYNC_COMP_HIGH_ACT,
  538. FB_VMODE_NONINTERLACED
  539. }, { /* 640x250 @ 50Hz, 15.6 kHz hsync */
  540. NULL, 50, 640, 250, 62500, 185, 123, 38, 21, 76, 3,
  541. 0,
  542. FB_VMODE_NONINTERLACED
  543. }, { /* 640x256 @ 50Hz, 15.6 kHz hsync */
  544. NULL, 50, 640, 256, 62500, 185, 123, 35, 18, 76, 3,
  545. 0,
  546. FB_VMODE_NONINTERLACED
  547. }, { /* 640x512 @ 50Hz, 26.8 kHz hsync */
  548. NULL, 50, 640, 512, 41667, 113, 87, 18, 1, 56, 3,
  549. 0,
  550. FB_VMODE_NONINTERLACED
  551. }, { /* 640x250 @ 70Hz, 31.5 kHz hsync */
  552. NULL, 70, 640, 250, 39722, 48, 16, 109, 88, 96, 2,
  553. 0,
  554. FB_VMODE_NONINTERLACED
  555. }, { /* 640x256 @ 70Hz, 31.5 kHz hsync */
  556. NULL, 70, 640, 256, 39722, 48, 16, 106, 85, 96, 2,
  557. 0,
  558. FB_VMODE_NONINTERLACED
  559. }, { /* 640x352 @ 70Hz, 31.5 kHz hsync */
  560. NULL, 70, 640, 352, 39722, 48, 16, 58, 37, 96, 2,
  561. 0,
  562. FB_VMODE_NONINTERLACED
  563. }, { /* 640x480 @ 60Hz, 31.5 kHz hsync */
  564. NULL, 60, 640, 480, 39722, 48, 16, 32, 11, 96, 2,
  565. 0,
  566. FB_VMODE_NONINTERLACED
  567. }, { /* 800x600 @ 56Hz, 35.2 kHz hsync */
  568. NULL, 56, 800, 600, 27778, 101, 23, 22, 1, 100, 2,
  569. 0,
  570. FB_VMODE_NONINTERLACED
  571. }, { /* 896x352 @ 60Hz, 21.8 kHz hsync */
  572. NULL, 60, 896, 352, 41667, 59, 27, 9, 0, 118, 3,
  573. 0,
  574. FB_VMODE_NONINTERLACED
  575. }, { /* 1024x 768 @ 60Hz, 48.4 kHz hsync */
  576. NULL, 60, 1024, 768, 15385, 160, 24, 29, 3, 136, 6,
  577. 0,
  578. FB_VMODE_NONINTERLACED
  579. }, { /* 1280x1024 @ 60Hz, 63.8 kHz hsync */
  580. NULL, 60, 1280, 1024, 9090, 186, 96, 38, 1, 160, 3,
  581. 0,
  582. FB_VMODE_NONINTERLACED
  583. }
  584. };
  585. static struct fb_videomode acornfb_default_mode = {
  586. .name = NULL,
  587. .refresh = 60,
  588. .xres = 640,
  589. .yres = 480,
  590. .pixclock = 39722,
  591. .left_margin = 56,
  592. .right_margin = 16,
  593. .upper_margin = 34,
  594. .lower_margin = 9,
  595. .hsync_len = 88,
  596. .vsync_len = 2,
  597. .sync = 0,
  598. .vmode = FB_VMODE_NONINTERLACED
  599. };
  600. static void acornfb_init_fbinfo(void)
  601. {
  602. static int first = 1;
  603. if (!first)
  604. return;
  605. first = 0;
  606. fb_info.fbops = &acornfb_ops;
  607. fb_info.flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  608. fb_info.pseudo_palette = current_par.pseudo_palette;
  609. strcpy(fb_info.fix.id, "Acorn");
  610. fb_info.fix.type = FB_TYPE_PACKED_PIXELS;
  611. fb_info.fix.type_aux = 0;
  612. fb_info.fix.xpanstep = 0;
  613. fb_info.fix.ypanstep = 1;
  614. fb_info.fix.ywrapstep = 1;
  615. fb_info.fix.line_length = 0;
  616. fb_info.fix.accel = FB_ACCEL_NONE;
  617. /*
  618. * setup initial parameters
  619. */
  620. memset(&fb_info.var, 0, sizeof(fb_info.var));
  621. #if defined(HAS_VIDC20)
  622. fb_info.var.red.length = 8;
  623. fb_info.var.transp.length = 4;
  624. #endif
  625. fb_info.var.green = fb_info.var.red;
  626. fb_info.var.blue = fb_info.var.red;
  627. fb_info.var.nonstd = 0;
  628. fb_info.var.activate = FB_ACTIVATE_NOW;
  629. fb_info.var.height = -1;
  630. fb_info.var.width = -1;
  631. fb_info.var.vmode = FB_VMODE_NONINTERLACED;
  632. fb_info.var.accel_flags = FB_ACCELF_TEXT;
  633. current_par.dram_size = 0;
  634. current_par.montype = -1;
  635. current_par.dpms = 0;
  636. }
  637. /*
  638. * setup acornfb options:
  639. *
  640. * mon:hmin-hmax:vmin-vmax:dpms:width:height
  641. * Set monitor parameters:
  642. * hmin = horizontal minimum frequency (Hz)
  643. * hmax = horizontal maximum frequency (Hz) (optional)
  644. * vmin = vertical minimum frequency (Hz)
  645. * vmax = vertical maximum frequency (Hz) (optional)
  646. * dpms = DPMS supported? (optional)
  647. * width = width of picture in mm. (optional)
  648. * height = height of picture in mm. (optional)
  649. *
  650. * montype:type
  651. * Set RISC-OS style monitor type:
  652. * 0 (or tv) - TV frequency
  653. * 1 (or multi) - Multi frequency
  654. * 2 (or hires) - Hi-res monochrome
  655. * 3 (or vga) - VGA
  656. * 4 (or svga) - SVGA
  657. * auto, or option missing
  658. * - try hardware detect
  659. *
  660. * dram:size
  661. * Set the amount of DRAM to use for the frame buffer
  662. * (even if you have VRAM).
  663. * size can optionally be followed by 'M' or 'K' for
  664. * MB or KB respectively.
  665. */
  666. static void acornfb_parse_mon(char *opt)
  667. {
  668. char *p = opt;
  669. current_par.montype = -2;
  670. fb_info.monspecs.hfmin = simple_strtoul(p, &p, 0);
  671. if (*p == '-')
  672. fb_info.monspecs.hfmax = simple_strtoul(p + 1, &p, 0);
  673. else
  674. fb_info.monspecs.hfmax = fb_info.monspecs.hfmin;
  675. if (*p != ':')
  676. goto bad;
  677. fb_info.monspecs.vfmin = simple_strtoul(p + 1, &p, 0);
  678. if (*p == '-')
  679. fb_info.monspecs.vfmax = simple_strtoul(p + 1, &p, 0);
  680. else
  681. fb_info.monspecs.vfmax = fb_info.monspecs.vfmin;
  682. if (*p != ':')
  683. goto check_values;
  684. fb_info.monspecs.dpms = simple_strtoul(p + 1, &p, 0);
  685. if (*p != ':')
  686. goto check_values;
  687. fb_info.var.width = simple_strtoul(p + 1, &p, 0);
  688. if (*p != ':')
  689. goto check_values;
  690. fb_info.var.height = simple_strtoul(p + 1, NULL, 0);
  691. check_values:
  692. if (fb_info.monspecs.hfmax < fb_info.monspecs.hfmin ||
  693. fb_info.monspecs.vfmax < fb_info.monspecs.vfmin)
  694. goto bad;
  695. return;
  696. bad:
  697. printk(KERN_ERR "Acornfb: bad monitor settings: %s\n", opt);
  698. current_par.montype = -1;
  699. }
  700. static void acornfb_parse_montype(char *opt)
  701. {
  702. current_par.montype = -2;
  703. if (strncmp(opt, "tv", 2) == 0) {
  704. opt += 2;
  705. current_par.montype = 0;
  706. } else if (strncmp(opt, "multi", 5) == 0) {
  707. opt += 5;
  708. current_par.montype = 1;
  709. } else if (strncmp(opt, "hires", 5) == 0) {
  710. opt += 5;
  711. current_par.montype = 2;
  712. } else if (strncmp(opt, "vga", 3) == 0) {
  713. opt += 3;
  714. current_par.montype = 3;
  715. } else if (strncmp(opt, "svga", 4) == 0) {
  716. opt += 4;
  717. current_par.montype = 4;
  718. } else if (strncmp(opt, "auto", 4) == 0) {
  719. opt += 4;
  720. current_par.montype = -1;
  721. } else if (isdigit(*opt))
  722. current_par.montype = simple_strtoul(opt, &opt, 0);
  723. if (current_par.montype == -2 ||
  724. current_par.montype > NR_MONTYPES) {
  725. printk(KERN_ERR "acornfb: unknown monitor type: %s\n",
  726. opt);
  727. current_par.montype = -1;
  728. } else
  729. if (opt && *opt) {
  730. if (strcmp(opt, ",dpms") == 0)
  731. current_par.dpms = 1;
  732. else
  733. printk(KERN_ERR
  734. "acornfb: unknown monitor option: %s\n",
  735. opt);
  736. }
  737. }
  738. static void acornfb_parse_dram(char *opt)
  739. {
  740. unsigned int size;
  741. size = simple_strtoul(opt, &opt, 0);
  742. if (opt) {
  743. switch (*opt) {
  744. case 'M':
  745. case 'm':
  746. size *= 1024;
  747. fallthrough;
  748. case 'K':
  749. case 'k':
  750. size *= 1024;
  751. default:
  752. break;
  753. }
  754. }
  755. current_par.dram_size = size;
  756. }
  757. static struct options {
  758. char *name;
  759. void (*parse)(char *opt);
  760. } opt_table[] = {
  761. { "mon", acornfb_parse_mon },
  762. { "montype", acornfb_parse_montype },
  763. { "dram", acornfb_parse_dram },
  764. { NULL, NULL }
  765. };
  766. static int acornfb_setup(char *options)
  767. {
  768. struct options *optp;
  769. char *opt;
  770. if (!options || !*options)
  771. return 0;
  772. acornfb_init_fbinfo();
  773. while ((opt = strsep(&options, ",")) != NULL) {
  774. if (!*opt)
  775. continue;
  776. for (optp = opt_table; optp->name; optp++) {
  777. int optlen;
  778. optlen = strlen(optp->name);
  779. if (strncmp(opt, optp->name, optlen) == 0 &&
  780. opt[optlen] == ':') {
  781. optp->parse(opt + optlen + 1);
  782. break;
  783. }
  784. }
  785. if (!optp->name)
  786. printk(KERN_ERR "acornfb: unknown parameter: %s\n",
  787. opt);
  788. }
  789. return 0;
  790. }
  791. /*
  792. * Detect type of monitor connected
  793. * For now, we just assume SVGA
  794. */
  795. static int acornfb_detect_monitortype(void)
  796. {
  797. return 4;
  798. }
  799. /*
  800. * This enables the unused memory to be freed on older Acorn machines.
  801. * We are freeing memory on behalf of the architecture initialisation
  802. * code here.
  803. */
  804. static inline void
  805. free_unused_pages(unsigned int virtual_start, unsigned int virtual_end)
  806. {
  807. int mb_freed = 0;
  808. /*
  809. * Align addresses
  810. */
  811. virtual_start = PAGE_ALIGN(virtual_start);
  812. virtual_end = PAGE_ALIGN(virtual_end);
  813. while (virtual_start < virtual_end) {
  814. struct page *page;
  815. /*
  816. * Clear page reserved bit,
  817. * set count to 1, and free
  818. * the page.
  819. */
  820. page = virt_to_page(virtual_start);
  821. __free_reserved_page(page);
  822. virtual_start += PAGE_SIZE;
  823. mb_freed += PAGE_SIZE / 1024;
  824. }
  825. printk("acornfb: freed %dK memory\n", mb_freed);
  826. }
  827. static int acornfb_probe(struct platform_device *dev)
  828. {
  829. unsigned long size;
  830. u_int h_sync, v_sync;
  831. int rc, i;
  832. char *option = NULL;
  833. if (fb_get_options("acornfb", &option))
  834. return -ENODEV;
  835. acornfb_setup(option);
  836. acornfb_init_fbinfo();
  837. current_par.dev = &dev->dev;
  838. if (current_par.montype == -1)
  839. current_par.montype = acornfb_detect_monitortype();
  840. if (current_par.montype == -1 || current_par.montype > NR_MONTYPES)
  841. current_par.montype = 4;
  842. if (current_par.montype >= 0) {
  843. fb_info.monspecs = monspecs[current_par.montype];
  844. fb_info.monspecs.dpms = current_par.dpms;
  845. }
  846. /*
  847. * Try to select a suitable default mode
  848. */
  849. for (i = 0; i < ARRAY_SIZE(modedb); i++) {
  850. unsigned long hs;
  851. hs = modedb[i].refresh *
  852. (modedb[i].yres + modedb[i].upper_margin +
  853. modedb[i].lower_margin + modedb[i].vsync_len);
  854. if (modedb[i].xres == DEFAULT_XRES &&
  855. modedb[i].yres == DEFAULT_YRES &&
  856. modedb[i].refresh >= fb_info.monspecs.vfmin &&
  857. modedb[i].refresh <= fb_info.monspecs.vfmax &&
  858. hs >= fb_info.monspecs.hfmin &&
  859. hs <= fb_info.monspecs.hfmax) {
  860. acornfb_default_mode = modedb[i];
  861. break;
  862. }
  863. }
  864. fb_info.screen_base = (char *)SCREEN_BASE;
  865. fb_info.fix.smem_start = SCREEN_START;
  866. current_par.using_vram = 0;
  867. /*
  868. * If vram_size is set, we are using VRAM in
  869. * a Risc PC. However, if the user has specified
  870. * an amount of DRAM then use that instead.
  871. */
  872. if (vram_size && !current_par.dram_size) {
  873. size = vram_size;
  874. current_par.vram_half_sam = vram_size / 1024;
  875. current_par.using_vram = 1;
  876. } else if (current_par.dram_size)
  877. size = current_par.dram_size;
  878. else
  879. size = MAX_SIZE;
  880. /*
  881. * Limit maximum screen size.
  882. */
  883. if (size > MAX_SIZE)
  884. size = MAX_SIZE;
  885. size = PAGE_ALIGN(size);
  886. #if defined(HAS_VIDC20)
  887. if (!current_par.using_vram) {
  888. dma_addr_t handle;
  889. void *base;
  890. /*
  891. * RiscPC needs to allocate the DRAM memory
  892. * for the framebuffer if we are not using
  893. * VRAM.
  894. */
  895. base = dma_alloc_wc(current_par.dev, size, &handle,
  896. GFP_KERNEL);
  897. if (base == NULL) {
  898. printk(KERN_ERR "acornfb: unable to allocate screen memory\n");
  899. return -ENOMEM;
  900. }
  901. fb_info.screen_base = base;
  902. fb_info.fix.smem_start = handle;
  903. }
  904. #endif
  905. fb_info.fix.smem_len = size;
  906. current_par.palette_size = VIDC_PALETTE_SIZE;
  907. /*
  908. * Lookup the timing for this resolution. If we can't
  909. * find it, then we can't restore it if we change
  910. * the resolution, so we disable this feature.
  911. */
  912. do {
  913. rc = fb_find_mode(&fb_info.var, &fb_info, NULL, modedb,
  914. ARRAY_SIZE(modedb),
  915. &acornfb_default_mode, DEFAULT_BPP);
  916. /*
  917. * If we found an exact match, all ok.
  918. */
  919. if (rc == 1)
  920. break;
  921. rc = fb_find_mode(&fb_info.var, &fb_info, NULL, NULL, 0,
  922. &acornfb_default_mode, DEFAULT_BPP);
  923. /*
  924. * If we found an exact match, all ok.
  925. */
  926. if (rc == 1)
  927. break;
  928. rc = fb_find_mode(&fb_info.var, &fb_info, NULL, modedb,
  929. ARRAY_SIZE(modedb),
  930. &acornfb_default_mode, DEFAULT_BPP);
  931. if (rc)
  932. break;
  933. rc = fb_find_mode(&fb_info.var, &fb_info, NULL, NULL, 0,
  934. &acornfb_default_mode, DEFAULT_BPP);
  935. } while (0);
  936. /*
  937. * If we didn't find an exact match, try the
  938. * generic database.
  939. */
  940. if (rc == 0) {
  941. printk("Acornfb: no valid mode found\n");
  942. return -EINVAL;
  943. }
  944. h_sync = 1953125000 / fb_info.var.pixclock;
  945. h_sync = h_sync * 512 / (fb_info.var.xres + fb_info.var.left_margin +
  946. fb_info.var.right_margin + fb_info.var.hsync_len);
  947. v_sync = h_sync / (fb_info.var.yres + fb_info.var.upper_margin +
  948. fb_info.var.lower_margin + fb_info.var.vsync_len);
  949. printk(KERN_INFO "Acornfb: %dkB %cRAM, %s, using %dx%d, %d.%03dkHz, %dHz\n",
  950. fb_info.fix.smem_len / 1024,
  951. current_par.using_vram ? 'V' : 'D',
  952. VIDC_NAME, fb_info.var.xres, fb_info.var.yres,
  953. h_sync / 1000, h_sync % 1000, v_sync);
  954. printk(KERN_INFO "Acornfb: Monitor: %d.%03d-%d.%03dkHz, %d-%dHz%s\n",
  955. fb_info.monspecs.hfmin / 1000, fb_info.monspecs.hfmin % 1000,
  956. fb_info.monspecs.hfmax / 1000, fb_info.monspecs.hfmax % 1000,
  957. fb_info.monspecs.vfmin, fb_info.monspecs.vfmax,
  958. fb_info.monspecs.dpms ? ", DPMS" : "");
  959. if (fb_set_var(&fb_info, &fb_info.var))
  960. printk(KERN_ERR "Acornfb: unable to set display parameters\n");
  961. if (register_framebuffer(&fb_info) < 0)
  962. return -EINVAL;
  963. return 0;
  964. }
  965. static struct platform_driver acornfb_driver = {
  966. .probe = acornfb_probe,
  967. .driver = {
  968. .name = "acornfb",
  969. },
  970. };
  971. static int __init acornfb_init(void)
  972. {
  973. return platform_driver_register(&acornfb_driver);
  974. }
  975. module_init(acornfb_init);
  976. MODULE_AUTHOR("Russell King");
  977. MODULE_DESCRIPTION("VIDC 1/1a/20 framebuffer driver");
  978. MODULE_LICENSE("GPL");