controlfb.c 27 KB

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