controlfb.c 27 KB

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