controlfb.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. * controlfb.c -- frame buffer device for the PowerMac 'control' display
  3. *
  4. * Created 12 July 1998 by Dan Jacobowitz <dan@debian.org>
  5. * Copyright (C) 1998 Dan Jacobowitz
  6. * Copyright (C) 2001 Takashi Oe
  7. *
  8. * Mmap code by Michel Lanners <mlan@cpu.lu>
  9. *
  10. * Frame buffer structure from:
  11. * drivers/video/chipsfb.c -- frame buffer device for
  12. * Chips & Technologies 65550 chip.
  13. *
  14. * Copyright (C) 1998 Paul Mackerras
  15. *
  16. * This file is derived from the Powermac "chips" driver:
  17. * Copyright (C) 1997 Fabio Riccardi.
  18. * And from the frame buffer device for Open Firmware-initialized devices:
  19. * Copyright (C) 1997 Geert Uytterhoeven.
  20. *
  21. * Hardware information from:
  22. * control.c: Console support for PowerMac "control" display adaptor.
  23. * Copyright (C) 1996 Paul Mackerras
  24. *
  25. * Updated to 2.5 framebuffer API by Ben Herrenschmidt
  26. * <benh@kernel.crashing.org>, Paul Mackerras <paulus@samba.org>,
  27. * and James Simmons <jsimmons@infradead.org>.
  28. *
  29. * This file is subject to the terms and conditions of the GNU General Public
  30. * License. See the file COPYING in the main directory of this archive for
  31. * more details.
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/errno.h>
  35. #include <linux/string.h>
  36. #include <linux/mm.h>
  37. #include <linux/slab.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/delay.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/of.h>
  42. #include <linux/of_address.h>
  43. #include <linux/fb.h>
  44. #include <linux/init.h>
  45. #include <linux/pci.h>
  46. #include <linux/nvram.h>
  47. #include <linux/adb.h>
  48. #include <linux/cuda.h>
  49. #ifdef CONFIG_BOOTX_TEXT
  50. #include <asm/btext.h>
  51. #endif
  52. #include "macmodes.h"
  53. #include "controlfb.h"
  54. #if !defined(CONFIG_PPC_PMAC) || !defined(CONFIG_PPC32)
  55. #define invalid_vram_cache(addr)
  56. #undef in_8
  57. #undef out_8
  58. #undef in_le32
  59. #undef out_le32
  60. #define in_8(addr) 0
  61. #define out_8(addr, val) (void)(val)
  62. #define in_le32(addr) 0
  63. #define out_le32(addr, val) (void)(val)
  64. #ifndef pgprot_cached_wthru
  65. #define pgprot_cached_wthru(prot) (prot)
  66. #endif
  67. #else
  68. static void invalid_vram_cache(void __force *addr)
  69. {
  70. eieio();
  71. dcbf(addr);
  72. mb();
  73. eieio();
  74. dcbf(addr);
  75. mb();
  76. }
  77. #endif
  78. struct fb_par_control {
  79. int vmode, cmode;
  80. int xres, yres;
  81. int vxres, vyres;
  82. int xoffset, yoffset;
  83. int pitch;
  84. struct control_regvals regvals;
  85. unsigned long sync;
  86. unsigned char ctrl;
  87. };
  88. #define DIRTY(z) ((x)->z != (y)->z)
  89. #define DIRTY_CMAP(z) (memcmp(&((x)->z), &((y)->z), sizeof((y)->z)))
  90. static inline int PAR_EQUAL(struct fb_par_control *x, struct fb_par_control *y)
  91. {
  92. int i, results;
  93. results = 1;
  94. for (i = 0; i < 3; i++)
  95. results &= !DIRTY(regvals.clock_params[i]);
  96. if (!results)
  97. return 0;
  98. for (i = 0; i < 16; i++)
  99. results &= !DIRTY(regvals.regs[i]);
  100. if (!results)
  101. return 0;
  102. return (!DIRTY(cmode) && !DIRTY(xres) && !DIRTY(yres)
  103. && !DIRTY(vxres) && !DIRTY(vyres));
  104. }
  105. struct fb_info_control {
  106. struct fb_info info;
  107. struct fb_par_control par;
  108. u32 pseudo_palette[16];
  109. struct cmap_regs __iomem *cmap_regs;
  110. unsigned long cmap_regs_phys;
  111. struct control_regs __iomem *control_regs;
  112. unsigned long control_regs_phys;
  113. unsigned long control_regs_size;
  114. __u8 __iomem *frame_buffer;
  115. unsigned long frame_buffer_phys;
  116. unsigned long fb_orig_base;
  117. unsigned long fb_orig_size;
  118. int control_use_bank2;
  119. unsigned long total_vram;
  120. unsigned char vram_attr;
  121. };
  122. /* control register access macro */
  123. #define CNTRL_REG(INFO,REG) (&(((INFO)->control_regs->REG).r))
  124. /************************** Internal variables *******************************/
  125. static struct fb_info_control *control_fb;
  126. static int default_vmode __initdata = VMODE_NVRAM;
  127. static int default_cmode __initdata = CMODE_NVRAM;
  128. static int controlfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  129. u_int transp, struct fb_info *info)
  130. {
  131. struct fb_info_control *p =
  132. container_of(info, struct fb_info_control, info);
  133. __u8 r, g, b;
  134. if (regno > 255)
  135. return 1;
  136. r = red >> 8;
  137. g = green >> 8;
  138. b = blue >> 8;
  139. out_8(&p->cmap_regs->addr, regno); /* tell clut what addr to fill */
  140. out_8(&p->cmap_regs->lut, r); /* send one color channel at */
  141. out_8(&p->cmap_regs->lut, g); /* a time... */
  142. out_8(&p->cmap_regs->lut, b);
  143. if (regno < 16) {
  144. int i;
  145. switch (p->par.cmode) {
  146. case CMODE_16:
  147. p->pseudo_palette[regno] =
  148. (regno << 10) | (regno << 5) | regno;
  149. break;
  150. case CMODE_32:
  151. i = (regno << 8) | regno;
  152. p->pseudo_palette[regno] = (i << 16) | i;
  153. break;
  154. }
  155. }
  156. return 0;
  157. }
  158. /******************** End of controlfb_ops implementation ******************/
  159. static void set_control_clock(unsigned char *params)
  160. {
  161. #ifdef CONFIG_ADB_CUDA
  162. struct adb_request req;
  163. int i;
  164. for (i = 0; i < 3; ++i) {
  165. cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
  166. 0x50, i + 1, params[i]);
  167. while (!req.complete)
  168. cuda_poll();
  169. }
  170. #endif
  171. }
  172. /*
  173. * Set screen start address according to var offset values
  174. */
  175. static inline void set_screen_start(int xoffset, int yoffset,
  176. struct fb_info_control *p)
  177. {
  178. struct fb_par_control *par = &p->par;
  179. par->xoffset = xoffset;
  180. par->yoffset = yoffset;
  181. out_le32(CNTRL_REG(p,start_addr),
  182. par->yoffset * par->pitch + (par->xoffset << par->cmode));
  183. }
  184. #define RADACAL_WRITE(a,d) \
  185. out_8(&p->cmap_regs->addr, (a)); \
  186. out_8(&p->cmap_regs->dat, (d))
  187. /* Now how about actually saying, Make it so! */
  188. /* Some things in here probably don't need to be done each time. */
  189. static void control_set_hardware(struct fb_info_control *p, struct fb_par_control *par)
  190. {
  191. struct control_regvals *r;
  192. volatile struct preg __iomem *rp;
  193. int i, cmode;
  194. if (PAR_EQUAL(&p->par, par)) {
  195. /*
  196. * check if only xoffset or yoffset differs.
  197. * this prevents flickers in typical VT switch case.
  198. */
  199. if (p->par.xoffset != par->xoffset ||
  200. p->par.yoffset != par->yoffset)
  201. set_screen_start(par->xoffset, par->yoffset, p);
  202. return;
  203. }
  204. p->par = *par;
  205. cmode = p->par.cmode;
  206. r = &par->regvals;
  207. /* Turn off display */
  208. out_le32(CNTRL_REG(p,ctrl), 0x400 | par->ctrl);
  209. set_control_clock(r->clock_params);
  210. RADACAL_WRITE(0x20, r->radacal_ctrl);
  211. RADACAL_WRITE(0x21, p->control_use_bank2 ? 0 : 1);
  212. RADACAL_WRITE(0x10, 0);
  213. RADACAL_WRITE(0x11, 0);
  214. rp = &p->control_regs->vswin;
  215. for (i = 0; i < 16; ++i, ++rp)
  216. out_le32(&rp->r, r->regs[i]);
  217. out_le32(CNTRL_REG(p,pitch), par->pitch);
  218. out_le32(CNTRL_REG(p,mode), r->mode);
  219. out_le32(CNTRL_REG(p,vram_attr), p->vram_attr);
  220. out_le32(CNTRL_REG(p,start_addr), par->yoffset * par->pitch
  221. + (par->xoffset << cmode));
  222. out_le32(CNTRL_REG(p,rfrcnt), 0x1e5);
  223. out_le32(CNTRL_REG(p,intr_ena), 0);
  224. /* Turn on display */
  225. out_le32(CNTRL_REG(p,ctrl), par->ctrl);
  226. #ifdef CONFIG_BOOTX_TEXT
  227. btext_update_display(p->frame_buffer_phys + CTRLFB_OFF,
  228. p->par.xres, p->par.yres,
  229. (cmode == CMODE_32? 32: cmode == CMODE_16? 16: 8),
  230. p->par.pitch);
  231. #endif /* CONFIG_BOOTX_TEXT */
  232. }
  233. /* Work out which banks of VRAM we have installed. */
  234. /* danj: I guess the card just ignores writes to nonexistant VRAM... */
  235. static void __init find_vram_size(struct fb_info_control *p)
  236. {
  237. int bank1, bank2;
  238. /*
  239. * Set VRAM in 2MB (bank 1) mode
  240. * VRAM Bank 2 will be accessible through offset 0x600000 if present
  241. * and VRAM Bank 1 will not respond at that offset even if present
  242. */
  243. out_le32(CNTRL_REG(p,vram_attr), 0x31);
  244. out_8(&p->frame_buffer[0x600000], 0xb3);
  245. out_8(&p->frame_buffer[0x600001], 0x71);
  246. invalid_vram_cache(&p->frame_buffer[0x600000]);
  247. bank2 = (in_8(&p->frame_buffer[0x600000]) == 0xb3)
  248. && (in_8(&p->frame_buffer[0x600001]) == 0x71);
  249. /*
  250. * Set VRAM in 2MB (bank 2) mode
  251. * VRAM Bank 1 will be accessible through offset 0x000000 if present
  252. * and VRAM Bank 2 will not respond at that offset even if present
  253. */
  254. out_le32(CNTRL_REG(p,vram_attr), 0x39);
  255. out_8(&p->frame_buffer[0], 0x5a);
  256. out_8(&p->frame_buffer[1], 0xc7);
  257. invalid_vram_cache(&p->frame_buffer[0]);
  258. bank1 = (in_8(&p->frame_buffer[0]) == 0x5a)
  259. && (in_8(&p->frame_buffer[1]) == 0xc7);
  260. if (bank2) {
  261. if (!bank1) {
  262. /*
  263. * vram bank 2 only
  264. */
  265. p->control_use_bank2 = 1;
  266. p->vram_attr = 0x39;
  267. p->frame_buffer += 0x600000;
  268. p->frame_buffer_phys += 0x600000;
  269. } else {
  270. /*
  271. * 4 MB vram
  272. */
  273. p->vram_attr = 0x51;
  274. }
  275. } else {
  276. /*
  277. * vram bank 1 only
  278. */
  279. p->vram_attr = 0x31;
  280. }
  281. p->total_vram = (bank1 + bank2) * 0x200000;
  282. printk(KERN_INFO "controlfb: VRAM Total = %dMB "
  283. "(%dMB @ bank 1, %dMB @ bank 2)\n",
  284. (bank1 + bank2) << 1, bank1 << 1, bank2 << 1);
  285. }
  286. /*
  287. * Get the monitor sense value.
  288. * Note that this can be called before calibrate_delay,
  289. * so we can't use udelay.
  290. */
  291. static int read_control_sense(struct fb_info_control *p)
  292. {
  293. int sense;
  294. out_le32(CNTRL_REG(p,mon_sense), 7); /* drive all lines high */
  295. __delay(200);
  296. out_le32(CNTRL_REG(p,mon_sense), 077); /* turn off drivers */
  297. __delay(2000);
  298. sense = (in_le32(CNTRL_REG(p,mon_sense)) & 0x1c0) << 2;
  299. /* drive each sense line low in turn and collect the other 2 */
  300. out_le32(CNTRL_REG(p,mon_sense), 033); /* drive A low */
  301. __delay(2000);
  302. sense |= (in_le32(CNTRL_REG(p,mon_sense)) & 0xc0) >> 2;
  303. out_le32(CNTRL_REG(p,mon_sense), 055); /* drive B low */
  304. __delay(2000);
  305. sense |= ((in_le32(CNTRL_REG(p,mon_sense)) & 0x100) >> 5)
  306. | ((in_le32(CNTRL_REG(p,mon_sense)) & 0x40) >> 4);
  307. out_le32(CNTRL_REG(p,mon_sense), 066); /* drive C low */
  308. __delay(2000);
  309. sense |= (in_le32(CNTRL_REG(p,mon_sense)) & 0x180) >> 7;
  310. out_le32(CNTRL_REG(p,mon_sense), 077); /* turn off drivers */
  311. return sense;
  312. }
  313. /********************** Various translation functions **********************/
  314. #define CONTROL_PIXCLOCK_BASE 256016
  315. #define CONTROL_PIXCLOCK_MIN 5000 /* ~ 200 MHz dot clock */
  316. /*
  317. * calculate the clock parameters to be sent to CUDA according to given
  318. * pixclock in pico second.
  319. */
  320. static int calc_clock_params(unsigned long clk, unsigned char *param)
  321. {
  322. unsigned long p0, p1, p2, k, l, m, n, min;
  323. if (clk > (CONTROL_PIXCLOCK_BASE << 3))
  324. return 1;
  325. p2 = ((clk << 4) < CONTROL_PIXCLOCK_BASE)? 3: 2;
  326. l = clk << p2;
  327. p0 = 0;
  328. p1 = 0;
  329. for (k = 1, min = l; k < 32; k++) {
  330. unsigned long rem;
  331. m = CONTROL_PIXCLOCK_BASE * k;
  332. n = m / l;
  333. rem = m % l;
  334. if (n && (n < 128) && rem < min) {
  335. p0 = k;
  336. p1 = n;
  337. min = rem;
  338. }
  339. }
  340. if (!p0 || !p1)
  341. return 1;
  342. param[0] = p0;
  343. param[1] = p1;
  344. param[2] = p2;
  345. return 0;
  346. }
  347. /*
  348. * This routine takes a user-supplied var, and picks the best vmode/cmode
  349. * from it.
  350. */
  351. static int control_var_to_par(struct fb_var_screeninfo *var,
  352. struct fb_par_control *par, const struct fb_info *fb_info)
  353. {
  354. int cmode, piped_diff, hstep;
  355. unsigned hperiod, hssync, hsblank, hesync, heblank, piped, heq, hlfln,
  356. hserr, vperiod, vssync, vesync, veblank, vsblank, vswin, vewin;
  357. unsigned long pixclock;
  358. struct fb_info_control *p =
  359. container_of(fb_info, struct fb_info_control, info);
  360. struct control_regvals *r = &par->regvals;
  361. switch (var->bits_per_pixel) {
  362. case 8:
  363. par->cmode = CMODE_8;
  364. if (p->total_vram > 0x200000) {
  365. r->mode = 3;
  366. r->radacal_ctrl = 0x20;
  367. piped_diff = 13;
  368. } else {
  369. r->mode = 2;
  370. r->radacal_ctrl = 0x10;
  371. piped_diff = 9;
  372. }
  373. break;
  374. case 15:
  375. case 16:
  376. par->cmode = CMODE_16;
  377. if (p->total_vram > 0x200000) {
  378. r->mode = 2;
  379. r->radacal_ctrl = 0x24;
  380. piped_diff = 5;
  381. } else {
  382. r->mode = 1;
  383. r->radacal_ctrl = 0x14;
  384. piped_diff = 3;
  385. }
  386. break;
  387. case 32:
  388. par->cmode = CMODE_32;
  389. if (p->total_vram > 0x200000) {
  390. r->mode = 1;
  391. r->radacal_ctrl = 0x28;
  392. } else {
  393. r->mode = 0;
  394. r->radacal_ctrl = 0x18;
  395. }
  396. piped_diff = 1;
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. /*
  402. * adjust xres and vxres so that the corresponding memory widths are
  403. * 32-byte aligned
  404. */
  405. hstep = 31 >> par->cmode;
  406. par->xres = (var->xres + hstep) & ~hstep;
  407. par->vxres = (var->xres_virtual + hstep) & ~hstep;
  408. par->xoffset = (var->xoffset + hstep) & ~hstep;
  409. if (par->vxres < par->xres)
  410. par->vxres = par->xres;
  411. par->pitch = par->vxres << par->cmode;
  412. par->yres = var->yres;
  413. par->vyres = var->yres_virtual;
  414. par->yoffset = var->yoffset;
  415. if (par->vyres < par->yres)
  416. par->vyres = par->yres;
  417. par->sync = var->sync;
  418. if (par->pitch * par->vyres + CTRLFB_OFF > p->total_vram)
  419. return -EINVAL;
  420. if (par->xoffset + par->xres > par->vxres)
  421. par->xoffset = par->vxres - par->xres;
  422. if (par->yoffset + par->yres > par->vyres)
  423. par->yoffset = par->vyres - par->yres;
  424. pixclock = (var->pixclock < CONTROL_PIXCLOCK_MIN)? CONTROL_PIXCLOCK_MIN:
  425. var->pixclock;
  426. if (calc_clock_params(pixclock, r->clock_params))
  427. return -EINVAL;
  428. hperiod = ((var->left_margin + par->xres + var->right_margin
  429. + var->hsync_len) >> 1) - 2;
  430. hssync = hperiod + 1;
  431. hsblank = hssync - (var->right_margin >> 1);
  432. hesync = (var->hsync_len >> 1) - 1;
  433. heblank = (var->left_margin >> 1) + hesync;
  434. piped = heblank - piped_diff;
  435. heq = var->hsync_len >> 2;
  436. hlfln = (hperiod+2) >> 1;
  437. hserr = hssync-hesync;
  438. vperiod = (var->vsync_len + var->lower_margin + par->yres
  439. + var->upper_margin) << 1;
  440. vssync = vperiod - 2;
  441. vesync = (var->vsync_len << 1) - vperiod + vssync;
  442. veblank = (var->upper_margin << 1) + vesync;
  443. vsblank = vssync - (var->lower_margin << 1);
  444. vswin = (vsblank+vssync) >> 1;
  445. vewin = (vesync+veblank) >> 1;
  446. r->regs[0] = vswin;
  447. r->regs[1] = vsblank;
  448. r->regs[2] = veblank;
  449. r->regs[3] = vewin;
  450. r->regs[4] = vesync;
  451. r->regs[5] = vssync;
  452. r->regs[6] = vperiod;
  453. r->regs[7] = piped;
  454. r->regs[8] = hperiod;
  455. r->regs[9] = hsblank;
  456. r->regs[10] = heblank;
  457. r->regs[11] = hesync;
  458. r->regs[12] = hssync;
  459. r->regs[13] = heq;
  460. r->regs[14] = hlfln;
  461. r->regs[15] = hserr;
  462. if (par->xres >= 1280 && par->cmode >= CMODE_16)
  463. par->ctrl = 0x7f;
  464. else
  465. par->ctrl = 0x3b;
  466. if (mac_var_to_vmode(var, &par->vmode, &cmode))
  467. par->vmode = 0;
  468. return 0;
  469. }
  470. /*
  471. * Convert hardware data in par to an fb_var_screeninfo
  472. */
  473. static void control_par_to_var(struct fb_par_control *par, struct fb_var_screeninfo *var)
  474. {
  475. struct control_regints *rv;
  476. rv = (struct control_regints *) par->regvals.regs;
  477. memset(var, 0, sizeof(*var));
  478. var->xres = par->xres;
  479. var->yres = par->yres;
  480. var->xres_virtual = par->vxres;
  481. var->yres_virtual = par->vyres;
  482. var->xoffset = par->xoffset;
  483. var->yoffset = par->yoffset;
  484. switch(par->cmode) {
  485. default:
  486. case CMODE_8:
  487. var->bits_per_pixel = 8;
  488. var->red.length = 8;
  489. var->green.length = 8;
  490. var->blue.length = 8;
  491. break;
  492. case CMODE_16: /* RGB 555 */
  493. var->bits_per_pixel = 16;
  494. var->red.offset = 10;
  495. var->red.length = 5;
  496. var->green.offset = 5;
  497. var->green.length = 5;
  498. var->blue.length = 5;
  499. break;
  500. case CMODE_32: /* RGB 888 */
  501. var->bits_per_pixel = 32;
  502. var->red.offset = 16;
  503. var->red.length = 8;
  504. var->green.offset = 8;
  505. var->green.length = 8;
  506. var->blue.length = 8;
  507. var->transp.offset = 24;
  508. var->transp.length = 8;
  509. break;
  510. }
  511. var->height = -1;
  512. var->width = -1;
  513. var->vmode = FB_VMODE_NONINTERLACED;
  514. var->left_margin = (rv->heblank - rv->hesync) << 1;
  515. var->right_margin = (rv->hssync - rv->hsblank) << 1;
  516. var->hsync_len = (rv->hperiod + 2 - rv->hssync + rv->hesync) << 1;
  517. var->upper_margin = (rv->veblank - rv->vesync) >> 1;
  518. var->lower_margin = (rv->vssync - rv->vsblank) >> 1;
  519. var->vsync_len = (rv->vperiod - rv->vssync + rv->vesync) >> 1;
  520. var->sync = par->sync;
  521. /*
  522. * 10^12 * clock_params[0] / (3906400 * clock_params[1]
  523. * * 2^clock_params[2])
  524. * (10^12 * clock_params[0] / (3906400 * clock_params[1]))
  525. * >> clock_params[2]
  526. */
  527. /* (255990.17 * clock_params[0] / clock_params[1]) >> clock_params[2] */
  528. var->pixclock = CONTROL_PIXCLOCK_BASE * par->regvals.clock_params[0];
  529. var->pixclock /= par->regvals.clock_params[1];
  530. var->pixclock >>= par->regvals.clock_params[2];
  531. }
  532. /******************** The functions for controlfb_ops ********************/
  533. /*
  534. * Checks a var structure
  535. */
  536. static int controlfb_check_var (struct fb_var_screeninfo *var, struct fb_info *info)
  537. {
  538. struct fb_par_control par;
  539. int err;
  540. err = control_var_to_par(var, &par, info);
  541. if (err)
  542. return err;
  543. control_par_to_var(&par, var);
  544. return 0;
  545. }
  546. /*
  547. * Applies current var to display
  548. */
  549. static int controlfb_set_par (struct fb_info *info)
  550. {
  551. struct fb_info_control *p =
  552. container_of(info, struct fb_info_control, info);
  553. struct fb_par_control par;
  554. int err;
  555. if((err = control_var_to_par(&info->var, &par, info))) {
  556. printk (KERN_ERR "controlfb_set_par: error calling"
  557. " control_var_to_par: %d.\n", err);
  558. return err;
  559. }
  560. control_set_hardware(p, &par);
  561. info->fix.visual = (p->par.cmode == CMODE_8) ?
  562. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
  563. info->fix.line_length = p->par.pitch;
  564. info->fix.xpanstep = 32 >> p->par.cmode;
  565. info->fix.ypanstep = 1;
  566. return 0;
  567. }
  568. static int controlfb_pan_display(struct fb_var_screeninfo *var,
  569. struct fb_info *info)
  570. {
  571. unsigned int xoffset, hstep;
  572. struct fb_info_control *p =
  573. container_of(info, struct fb_info_control, info);
  574. struct fb_par_control *par = &p->par;
  575. /*
  576. * make sure start addr will be 32-byte aligned
  577. */
  578. hstep = 0x1f >> par->cmode;
  579. xoffset = (var->xoffset + hstep) & ~hstep;
  580. if (xoffset+par->xres > par->vxres ||
  581. var->yoffset+par->yres > par->vyres)
  582. return -EINVAL;
  583. set_screen_start(xoffset, var->yoffset, p);
  584. return 0;
  585. }
  586. static int controlfb_blank(int blank_mode, struct fb_info *info)
  587. {
  588. struct fb_info_control __maybe_unused *p =
  589. container_of(info, struct fb_info_control, info);
  590. unsigned ctrl;
  591. ctrl = in_le32(CNTRL_REG(p, ctrl));
  592. if (blank_mode > 0)
  593. switch (blank_mode) {
  594. case FB_BLANK_VSYNC_SUSPEND:
  595. ctrl &= ~3;
  596. break;
  597. case FB_BLANK_HSYNC_SUSPEND:
  598. ctrl &= ~0x30;
  599. break;
  600. case FB_BLANK_POWERDOWN:
  601. ctrl &= ~0x33;
  602. fallthrough;
  603. case FB_BLANK_NORMAL:
  604. ctrl |= 0x400;
  605. break;
  606. default:
  607. break;
  608. }
  609. else {
  610. ctrl &= ~0x400;
  611. ctrl |= 0x33;
  612. }
  613. out_le32(CNTRL_REG(p,ctrl), ctrl);
  614. return 0;
  615. }
  616. /*
  617. * Private mmap since we want to have a different caching on the framebuffer
  618. * for controlfb.
  619. * Note there's no locking in here; it's done in fb_mmap() in fbmem.c.
  620. */
  621. static int controlfb_mmap(struct fb_info *info,
  622. struct vm_area_struct *vma)
  623. {
  624. unsigned long mmio_pgoff;
  625. unsigned long start;
  626. u32 len;
  627. start = info->fix.smem_start;
  628. len = info->fix.smem_len;
  629. mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT;
  630. if (vma->vm_pgoff >= mmio_pgoff) {
  631. if (info->var.accel_flags)
  632. return -EINVAL;
  633. vma->vm_pgoff -= mmio_pgoff;
  634. start = info->fix.mmio_start;
  635. len = info->fix.mmio_len;
  636. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  637. } else {
  638. /* framebuffer */
  639. vma->vm_page_prot = pgprot_cached_wthru(vma->vm_page_prot);
  640. }
  641. return vm_iomap_memory(vma, start, len);
  642. }
  643. static const struct fb_ops controlfb_ops = {
  644. .owner = THIS_MODULE,
  645. __FB_DEFAULT_IOMEM_OPS_RDWR,
  646. .fb_check_var = controlfb_check_var,
  647. .fb_set_par = controlfb_set_par,
  648. .fb_setcolreg = controlfb_setcolreg,
  649. .fb_pan_display = controlfb_pan_display,
  650. .fb_blank = controlfb_blank,
  651. __FB_DEFAULT_IOMEM_OPS_DRAW,
  652. .fb_mmap = controlfb_mmap,
  653. };
  654. /*
  655. * Set misc info vars for this driver
  656. */
  657. static void __init control_init_info(struct fb_info *info, struct fb_info_control *p)
  658. {
  659. /* Fill fb_info */
  660. info->par = &p->par;
  661. info->fbops = &controlfb_ops;
  662. info->pseudo_palette = p->pseudo_palette;
  663. info->flags = FBINFO_HWACCEL_YPAN;
  664. info->screen_base = p->frame_buffer + CTRLFB_OFF;
  665. fb_alloc_cmap(&info->cmap, 256, 0);
  666. /* Fill fix common fields */
  667. strcpy(info->fix.id, "control");
  668. info->fix.mmio_start = p->control_regs_phys;
  669. info->fix.mmio_len = sizeof(struct control_regs);
  670. info->fix.type = FB_TYPE_PACKED_PIXELS;
  671. info->fix.smem_start = p->frame_buffer_phys + CTRLFB_OFF;
  672. info->fix.smem_len = p->total_vram - CTRLFB_OFF;
  673. info->fix.ywrapstep = 0;
  674. info->fix.type_aux = 0;
  675. info->fix.accel = FB_ACCEL_NONE;
  676. }
  677. /*
  678. * Parse user specified options (`video=controlfb:')
  679. */
  680. static void __init control_setup(char *options)
  681. {
  682. char *this_opt;
  683. if (!options || !*options)
  684. return;
  685. while ((this_opt = strsep(&options, ",")) != NULL) {
  686. if (!strncmp(this_opt, "vmode:", 6)) {
  687. int vmode = simple_strtoul(this_opt+6, NULL, 0);
  688. if (vmode > 0 && vmode <= VMODE_MAX &&
  689. control_mac_modes[vmode - 1].m[1] >= 0)
  690. default_vmode = vmode;
  691. } else if (!strncmp(this_opt, "cmode:", 6)) {
  692. int depth = simple_strtoul(this_opt+6, NULL, 0);
  693. switch (depth) {
  694. case CMODE_8:
  695. case CMODE_16:
  696. case CMODE_32:
  697. default_cmode = depth;
  698. break;
  699. case 8:
  700. default_cmode = CMODE_8;
  701. break;
  702. case 15:
  703. case 16:
  704. default_cmode = CMODE_16;
  705. break;
  706. case 24:
  707. case 32:
  708. default_cmode = CMODE_32;
  709. break;
  710. }
  711. }
  712. }
  713. }
  714. /*
  715. * finish off the driver initialization and register
  716. */
  717. static int __init init_control(struct fb_info_control *p)
  718. {
  719. int full, sense, vmode, cmode, vyres;
  720. struct fb_var_screeninfo var;
  721. int rc;
  722. printk(KERN_INFO "controlfb: ");
  723. full = p->total_vram == 0x400000;
  724. /* Try to pick a video mode out of NVRAM if we have one. */
  725. cmode = default_cmode;
  726. if (IS_REACHABLE(CONFIG_NVRAM) && cmode == CMODE_NVRAM)
  727. cmode = nvram_read_byte(NV_CMODE);
  728. if (cmode < CMODE_8 || cmode > CMODE_32)
  729. cmode = CMODE_8;
  730. vmode = default_vmode;
  731. if (IS_REACHABLE(CONFIG_NVRAM) && vmode == VMODE_NVRAM)
  732. vmode = nvram_read_byte(NV_VMODE);
  733. if (vmode < 1 || vmode > VMODE_MAX ||
  734. control_mac_modes[vmode - 1].m[full] < cmode) {
  735. sense = read_control_sense(p);
  736. printk(KERN_CONT "Monitor sense value = 0x%x, ", sense);
  737. vmode = mac_map_monitor_sense(sense);
  738. if (control_mac_modes[vmode - 1].m[full] < 0)
  739. vmode = VMODE_640_480_60;
  740. cmode = min(cmode, control_mac_modes[vmode - 1].m[full]);
  741. }
  742. /* Initialize info structure */
  743. control_init_info(&p->info, p);
  744. /* Setup default var */
  745. if (mac_vmode_to_var(vmode, cmode, &var) < 0) {
  746. /* This shouldn't happen! */
  747. printk("mac_vmode_to_var(%d, %d,) failed\n", vmode, cmode);
  748. try_again:
  749. vmode = VMODE_640_480_60;
  750. cmode = CMODE_8;
  751. if (mac_vmode_to_var(vmode, cmode, &var) < 0) {
  752. printk(KERN_ERR "controlfb: mac_vmode_to_var() failed\n");
  753. return -ENXIO;
  754. }
  755. printk(KERN_INFO "controlfb: ");
  756. }
  757. printk("using video mode %d and color mode %d.\n", vmode, cmode);
  758. vyres = (p->total_vram - CTRLFB_OFF) / (var.xres << cmode);
  759. if (vyres > var.yres)
  760. var.yres_virtual = vyres;
  761. /* Apply default var */
  762. var.activate = FB_ACTIVATE_NOW;
  763. rc = fb_set_var(&p->info, &var);
  764. if (rc && (vmode != VMODE_640_480_60 || cmode != CMODE_8))
  765. goto try_again;
  766. /* Register with fbdev layer */
  767. if (register_framebuffer(&p->info) < 0)
  768. return -ENXIO;
  769. fb_info(&p->info, "control display adapter\n");
  770. return 0;
  771. }
  772. static void control_cleanup(void)
  773. {
  774. struct fb_info_control *p = control_fb;
  775. if (!p)
  776. return;
  777. if (p->cmap_regs)
  778. iounmap(p->cmap_regs);
  779. if (p->control_regs)
  780. iounmap(p->control_regs);
  781. if (p->frame_buffer) {
  782. if (p->control_use_bank2)
  783. p->frame_buffer -= 0x600000;
  784. iounmap(p->frame_buffer);
  785. }
  786. if (p->cmap_regs_phys)
  787. release_mem_region(p->cmap_regs_phys, 0x1000);
  788. if (p->control_regs_phys)
  789. release_mem_region(p->control_regs_phys, p->control_regs_size);
  790. if (p->fb_orig_base)
  791. release_mem_region(p->fb_orig_base, p->fb_orig_size);
  792. kfree(p);
  793. }
  794. /*
  795. * find "control" and initialize
  796. */
  797. static int __init control_of_init(struct device_node *dp)
  798. {
  799. struct fb_info_control *p;
  800. struct resource fb_res, reg_res;
  801. if (control_fb) {
  802. printk(KERN_ERR "controlfb: only one control is supported\n");
  803. return -ENXIO;
  804. }
  805. if (of_pci_address_to_resource(dp, 2, &fb_res) ||
  806. of_pci_address_to_resource(dp, 1, &reg_res)) {
  807. printk(KERN_ERR "can't get 2 addresses for control\n");
  808. return -ENXIO;
  809. }
  810. p = kzalloc_obj(*p);
  811. if (!p)
  812. return -ENOMEM;
  813. control_fb = p; /* save it for cleanups */
  814. /* Map in frame buffer and registers */
  815. p->fb_orig_base = fb_res.start;
  816. p->fb_orig_size = resource_size(&fb_res);
  817. /* use the big-endian aperture (??) */
  818. p->frame_buffer_phys = fb_res.start + 0x800000;
  819. p->control_regs_phys = reg_res.start;
  820. p->control_regs_size = resource_size(&reg_res);
  821. if (!p->fb_orig_base ||
  822. !request_mem_region(p->fb_orig_base,p->fb_orig_size,"controlfb")) {
  823. p->fb_orig_base = 0;
  824. goto error_out;
  825. }
  826. /* map at most 8MB for the frame buffer */
  827. p->frame_buffer = ioremap_wt(p->frame_buffer_phys, 0x800000);
  828. if (!p->control_regs_phys ||
  829. !request_mem_region(p->control_regs_phys, p->control_regs_size,
  830. "controlfb regs")) {
  831. p->control_regs_phys = 0;
  832. goto error_out;
  833. }
  834. p->control_regs = ioremap(p->control_regs_phys, p->control_regs_size);
  835. p->cmap_regs_phys = 0xf301b000; /* XXX not in prom? */
  836. if (!request_mem_region(p->cmap_regs_phys, 0x1000, "controlfb cmap")) {
  837. p->cmap_regs_phys = 0;
  838. goto error_out;
  839. }
  840. p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
  841. if (!p->cmap_regs || !p->control_regs || !p->frame_buffer)
  842. goto error_out;
  843. find_vram_size(p);
  844. if (!p->total_vram)
  845. goto error_out;
  846. if (init_control(p) < 0)
  847. goto error_out;
  848. return 0;
  849. error_out:
  850. control_cleanup();
  851. return -ENXIO;
  852. }
  853. static int __init control_init(void)
  854. {
  855. struct device_node *dp;
  856. char *option = NULL;
  857. int ret = -ENXIO;
  858. if (fb_get_options("controlfb", &option))
  859. return -ENODEV;
  860. control_setup(option);
  861. dp = of_find_node_by_name(NULL, "control");
  862. if (dp && !control_of_init(dp))
  863. ret = 0;
  864. of_node_put(dp);
  865. return ret;
  866. }
  867. device_initcall(control_init);