controlfb.c 27 KB

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