acornfb.c 28 KB

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