controlfb.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * controlfb.c -- frame buffer device for the PowerMac 'control' display
  3. *
  4. * Created 12 July 1998 by Dan Jacobowitz <[email protected]>
  5. * Copyright (C) 1998 Dan Jacobowitz
  6. * Copyright (C) 2001 Takashi Oe
  7. *
  8. * Mmap code by Michel Lanners <[email protected]>
  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. * <[email protected]>, Paul Mackerras <[email protected]>,
  27. * and James Simmons <[email protected]>.
  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 paramaters 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_check_var = controlfb_check_var,
  646. .fb_set_par = controlfb_set_par,
  647. .fb_setcolreg = controlfb_setcolreg,
  648. .fb_pan_display = controlfb_pan_display,
  649. .fb_blank = controlfb_blank,
  650. .fb_mmap = controlfb_mmap,
  651. .fb_fillrect = cfb_fillrect,
  652. .fb_copyarea = cfb_copyarea,
  653. .fb_imageblit = cfb_imageblit,
  654. };
  655. /*
  656. * Set misc info vars for this driver
  657. */
  658. static void __init control_init_info(struct fb_info *info, struct fb_info_control *p)
  659. {
  660. /* Fill fb_info */
  661. info->par = &p->par;
  662. info->fbops = &controlfb_ops;
  663. info->pseudo_palette = p->pseudo_palette;
  664. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  665. info->screen_base = p->frame_buffer + CTRLFB_OFF;
  666. fb_alloc_cmap(&info->cmap, 256, 0);
  667. /* Fill fix common fields */
  668. strcpy(info->fix.id, "control");
  669. info->fix.mmio_start = p->control_regs_phys;
  670. info->fix.mmio_len = sizeof(struct control_regs);
  671. info->fix.type = FB_TYPE_PACKED_PIXELS;
  672. info->fix.smem_start = p->frame_buffer_phys + CTRLFB_OFF;
  673. info->fix.smem_len = p->total_vram - CTRLFB_OFF;
  674. info->fix.ywrapstep = 0;
  675. info->fix.type_aux = 0;
  676. info->fix.accel = FB_ACCEL_NONE;
  677. }
  678. /*
  679. * Parse user specified options (`video=controlfb:')
  680. */
  681. static void __init control_setup(char *options)
  682. {
  683. char *this_opt;
  684. if (!options || !*options)
  685. return;
  686. while ((this_opt = strsep(&options, ",")) != NULL) {
  687. if (!strncmp(this_opt, "vmode:", 6)) {
  688. int vmode = simple_strtoul(this_opt+6, NULL, 0);
  689. if (vmode > 0 && vmode <= VMODE_MAX &&
  690. control_mac_modes[vmode - 1].m[1] >= 0)
  691. default_vmode = vmode;
  692. } else if (!strncmp(this_opt, "cmode:", 6)) {
  693. int depth = simple_strtoul(this_opt+6, NULL, 0);
  694. switch (depth) {
  695. case CMODE_8:
  696. case CMODE_16:
  697. case CMODE_32:
  698. default_cmode = depth;
  699. break;
  700. case 8:
  701. default_cmode = CMODE_8;
  702. break;
  703. case 15:
  704. case 16:
  705. default_cmode = CMODE_16;
  706. break;
  707. case 24:
  708. case 32:
  709. default_cmode = CMODE_32;
  710. break;
  711. }
  712. }
  713. }
  714. }
  715. /*
  716. * finish off the driver initialization and register
  717. */
  718. static int __init init_control(struct fb_info_control *p)
  719. {
  720. int full, sense, vmode, cmode, vyres;
  721. struct fb_var_screeninfo var;
  722. int rc;
  723. printk(KERN_INFO "controlfb: ");
  724. full = p->total_vram == 0x400000;
  725. /* Try to pick a video mode out of NVRAM if we have one. */
  726. cmode = default_cmode;
  727. if (IS_REACHABLE(CONFIG_NVRAM) && cmode == CMODE_NVRAM)
  728. cmode = nvram_read_byte(NV_CMODE);
  729. if (cmode < CMODE_8 || cmode > CMODE_32)
  730. cmode = CMODE_8;
  731. vmode = default_vmode;
  732. if (IS_REACHABLE(CONFIG_NVRAM) && vmode == VMODE_NVRAM)
  733. vmode = nvram_read_byte(NV_VMODE);
  734. if (vmode < 1 || vmode > VMODE_MAX ||
  735. control_mac_modes[vmode - 1].m[full] < cmode) {
  736. sense = read_control_sense(p);
  737. printk(KERN_CONT "Monitor sense value = 0x%x, ", sense);
  738. vmode = mac_map_monitor_sense(sense);
  739. if (control_mac_modes[vmode - 1].m[full] < 0)
  740. vmode = VMODE_640_480_60;
  741. cmode = min(cmode, control_mac_modes[vmode - 1].m[full]);
  742. }
  743. /* Initialize info structure */
  744. control_init_info(&p->info, p);
  745. /* Setup default var */
  746. if (mac_vmode_to_var(vmode, cmode, &var) < 0) {
  747. /* This shouldn't happen! */
  748. printk("mac_vmode_to_var(%d, %d,) failed\n", vmode, cmode);
  749. try_again:
  750. vmode = VMODE_640_480_60;
  751. cmode = CMODE_8;
  752. if (mac_vmode_to_var(vmode, cmode, &var) < 0) {
  753. printk(KERN_ERR "controlfb: mac_vmode_to_var() failed\n");
  754. return -ENXIO;
  755. }
  756. printk(KERN_INFO "controlfb: ");
  757. }
  758. printk("using video mode %d and color mode %d.\n", vmode, cmode);
  759. vyres = (p->total_vram - CTRLFB_OFF) / (var.xres << cmode);
  760. if (vyres > var.yres)
  761. var.yres_virtual = vyres;
  762. /* Apply default var */
  763. var.activate = FB_ACTIVATE_NOW;
  764. rc = fb_set_var(&p->info, &var);
  765. if (rc && (vmode != VMODE_640_480_60 || cmode != CMODE_8))
  766. goto try_again;
  767. /* Register with fbdev layer */
  768. if (register_framebuffer(&p->info) < 0)
  769. return -ENXIO;
  770. fb_info(&p->info, "control display adapter\n");
  771. return 0;
  772. }
  773. static void control_cleanup(void)
  774. {
  775. struct fb_info_control *p = control_fb;
  776. if (!p)
  777. return;
  778. if (p->cmap_regs)
  779. iounmap(p->cmap_regs);
  780. if (p->control_regs)
  781. iounmap(p->control_regs);
  782. if (p->frame_buffer) {
  783. if (p->control_use_bank2)
  784. p->frame_buffer -= 0x600000;
  785. iounmap(p->frame_buffer);
  786. }
  787. if (p->cmap_regs_phys)
  788. release_mem_region(p->cmap_regs_phys, 0x1000);
  789. if (p->control_regs_phys)
  790. release_mem_region(p->control_regs_phys, p->control_regs_size);
  791. if (p->fb_orig_base)
  792. release_mem_region(p->fb_orig_base, p->fb_orig_size);
  793. kfree(p);
  794. }
  795. /*
  796. * find "control" and initialize
  797. */
  798. static int __init control_of_init(struct device_node *dp)
  799. {
  800. struct fb_info_control *p;
  801. struct resource fb_res, reg_res;
  802. if (control_fb) {
  803. printk(KERN_ERR "controlfb: only one control is supported\n");
  804. return -ENXIO;
  805. }
  806. if (of_pci_address_to_resource(dp, 2, &fb_res) ||
  807. of_pci_address_to_resource(dp, 1, &reg_res)) {
  808. printk(KERN_ERR "can't get 2 addresses for control\n");
  809. return -ENXIO;
  810. }
  811. p = kzalloc(sizeof(*p), GFP_KERNEL);
  812. if (!p)
  813. return -ENOMEM;
  814. control_fb = p; /* save it for cleanups */
  815. /* Map in frame buffer and registers */
  816. p->fb_orig_base = fb_res.start;
  817. p->fb_orig_size = resource_size(&fb_res);
  818. /* use the big-endian aperture (??) */
  819. p->frame_buffer_phys = fb_res.start + 0x800000;
  820. p->control_regs_phys = reg_res.start;
  821. p->control_regs_size = resource_size(&reg_res);
  822. if (!p->fb_orig_base ||
  823. !request_mem_region(p->fb_orig_base,p->fb_orig_size,"controlfb")) {
  824. p->fb_orig_base = 0;
  825. goto error_out;
  826. }
  827. /* map at most 8MB for the frame buffer */
  828. p->frame_buffer = ioremap_wt(p->frame_buffer_phys, 0x800000);
  829. if (!p->control_regs_phys ||
  830. !request_mem_region(p->control_regs_phys, p->control_regs_size,
  831. "controlfb regs")) {
  832. p->control_regs_phys = 0;
  833. goto error_out;
  834. }
  835. p->control_regs = ioremap(p->control_regs_phys, p->control_regs_size);
  836. p->cmap_regs_phys = 0xf301b000; /* XXX not in prom? */
  837. if (!request_mem_region(p->cmap_regs_phys, 0x1000, "controlfb cmap")) {
  838. p->cmap_regs_phys = 0;
  839. goto error_out;
  840. }
  841. p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
  842. if (!p->cmap_regs || !p->control_regs || !p->frame_buffer)
  843. goto error_out;
  844. find_vram_size(p);
  845. if (!p->total_vram)
  846. goto error_out;
  847. if (init_control(p) < 0)
  848. goto error_out;
  849. return 0;
  850. error_out:
  851. control_cleanup();
  852. return -ENXIO;
  853. }
  854. static int __init control_init(void)
  855. {
  856. struct device_node *dp;
  857. char *option = NULL;
  858. int ret = -ENXIO;
  859. if (fb_get_options("controlfb", &option))
  860. return -ENODEV;
  861. control_setup(option);
  862. dp = of_find_node_by_name(NULL, "control");
  863. if (dp && !control_of_init(dp))
  864. ret = 0;
  865. of_node_put(dp);
  866. return ret;
  867. }
  868. device_initcall(control_init);