controlfb.c 27 KB

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