controlfb.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. if (default_cmode == CMODE_NVRAM){
  350. cmode = nvram_read_byte(NV_CMODE);
  351. if(cmode < CMODE_8 || cmode > CMODE_32)
  352. cmode = CMODE_8;
  353. } else
  354. cmode=default_cmode;
  355. if (default_vmode == VMODE_NVRAM) {
  356. vmode = nvram_read_byte(NV_VMODE);
  357. if (vmode < 1 || vmode > VMODE_MAX ||
  358. control_mac_modes[vmode - 1].m[full] < cmode) {
  359. sense = read_control_sense(p);
  360. printk("Monitor sense value = 0x%x, ", sense);
  361. vmode = mac_map_monitor_sense(sense);
  362. if (control_mac_modes[vmode - 1].m[full] < cmode)
  363. vmode = VMODE_640_480_60;
  364. }
  365. } else {
  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 speficied 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. if (fb_get_options("controlfb", &option))
  495. return -ENODEV;
  496. control_setup(option);
  497. dp = find_devices("control");
  498. if (dp != 0 && !control_of_init(dp))
  499. return 0;
  500. return -ENXIO;
  501. }
  502. module_init(control_init);
  503. /* Work out which banks of VRAM we have installed. */
  504. /* danj: I guess the card just ignores writes to nonexistant VRAM... */
  505. static void __init find_vram_size(struct fb_info_control *p)
  506. {
  507. int bank1, bank2;
  508. /*
  509. * Set VRAM in 2MB (bank 1) mode
  510. * VRAM Bank 2 will be accessible through offset 0x600000 if present
  511. * and VRAM Bank 1 will not respond at that offset even if present
  512. */
  513. out_le32(CNTRL_REG(p,vram_attr), 0x31);
  514. out_8(&p->frame_buffer[0x600000], 0xb3);
  515. out_8(&p->frame_buffer[0x600001], 0x71);
  516. asm volatile("eieio; dcbf 0,%0" : : "r" (&p->frame_buffer[0x600000])
  517. : "memory" );
  518. mb();
  519. asm volatile("eieio; dcbi 0,%0" : : "r" (&p->frame_buffer[0x600000])
  520. : "memory" );
  521. mb();
  522. bank2 = (in_8(&p->frame_buffer[0x600000]) == 0xb3)
  523. && (in_8(&p->frame_buffer[0x600001]) == 0x71);
  524. /*
  525. * Set VRAM in 2MB (bank 2) mode
  526. * VRAM Bank 1 will be accessible through offset 0x000000 if present
  527. * and VRAM Bank 2 will not respond at that offset even if present
  528. */
  529. out_le32(CNTRL_REG(p,vram_attr), 0x39);
  530. out_8(&p->frame_buffer[0], 0x5a);
  531. out_8(&p->frame_buffer[1], 0xc7);
  532. asm volatile("eieio; dcbf 0,%0" : : "r" (&p->frame_buffer[0])
  533. : "memory" );
  534. mb();
  535. asm volatile("eieio; dcbi 0,%0" : : "r" (&p->frame_buffer[0])
  536. : "memory" );
  537. mb();
  538. bank1 = (in_8(&p->frame_buffer[0]) == 0x5a)
  539. && (in_8(&p->frame_buffer[1]) == 0xc7);
  540. if (bank2) {
  541. if (!bank1) {
  542. /*
  543. * vram bank 2 only
  544. */
  545. p->control_use_bank2 = 1;
  546. p->vram_attr = 0x39;
  547. p->frame_buffer += 0x600000;
  548. p->frame_buffer_phys += 0x600000;
  549. } else {
  550. /*
  551. * 4 MB vram
  552. */
  553. p->vram_attr = 0x51;
  554. }
  555. } else {
  556. /*
  557. * vram bank 1 only
  558. */
  559. p->vram_attr = 0x31;
  560. }
  561. p->total_vram = (bank1 + bank2) * 0x200000;
  562. printk(KERN_INFO "controlfb: VRAM Total = %dMB "
  563. "(%dMB @ bank 1, %dMB @ bank 2)\n",
  564. (bank1 + bank2) << 1, bank1 << 1, bank2 << 1);
  565. }
  566. /*
  567. * find "control" and initialize
  568. */
  569. static int __init control_of_init(struct device_node *dp)
  570. {
  571. struct fb_info_control *p;
  572. struct resource fb_res, reg_res;
  573. if (control_fb) {
  574. printk(KERN_ERR "controlfb: only one control is supported\n");
  575. return -ENXIO;
  576. }
  577. if (of_pci_address_to_resource(dp, 2, &fb_res) ||
  578. of_pci_address_to_resource(dp, 1, &reg_res)) {
  579. printk(KERN_ERR "can't get 2 addresses for control\n");
  580. return -ENXIO;
  581. }
  582. p = kmalloc(sizeof(*p), GFP_KERNEL);
  583. if (p == 0)
  584. return -ENXIO;
  585. control_fb = p; /* save it for cleanups */
  586. memset(p, 0, sizeof(*p));
  587. /* Map in frame buffer and registers */
  588. p->fb_orig_base = fb_res.start;
  589. p->fb_orig_size = fb_res.end - fb_res.start + 1;
  590. /* use the big-endian aperture (??) */
  591. p->frame_buffer_phys = fb_res.start + 0x800000;
  592. p->control_regs_phys = reg_res.start;
  593. p->control_regs_size = reg_res.end - reg_res.start + 1;
  594. if (!p->fb_orig_base ||
  595. !request_mem_region(p->fb_orig_base,p->fb_orig_size,"controlfb")) {
  596. p->fb_orig_base = 0;
  597. goto error_out;
  598. }
  599. /* map at most 8MB for the frame buffer */
  600. p->frame_buffer = __ioremap(p->frame_buffer_phys, 0x800000,
  601. _PAGE_WRITETHRU);
  602. if (!p->control_regs_phys ||
  603. !request_mem_region(p->control_regs_phys, p->control_regs_size,
  604. "controlfb regs")) {
  605. p->control_regs_phys = 0;
  606. goto error_out;
  607. }
  608. p->control_regs = ioremap(p->control_regs_phys, p->control_regs_size);
  609. p->cmap_regs_phys = 0xf301b000; /* XXX not in prom? */
  610. if (!request_mem_region(p->cmap_regs_phys, 0x1000, "controlfb cmap")) {
  611. p->cmap_regs_phys = 0;
  612. goto error_out;
  613. }
  614. p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
  615. if (!p->cmap_regs || !p->control_regs || !p->frame_buffer)
  616. goto error_out;
  617. find_vram_size(p);
  618. if (!p->total_vram)
  619. goto error_out;
  620. if (init_control(p) < 0)
  621. goto error_out;
  622. return 0;
  623. error_out:
  624. control_cleanup();
  625. return -ENXIO;
  626. }
  627. /*
  628. * Get the monitor sense value.
  629. * Note that this can be called before calibrate_delay,
  630. * so we can't use udelay.
  631. */
  632. static int read_control_sense(struct fb_info_control *p)
  633. {
  634. int sense;
  635. out_le32(CNTRL_REG(p,mon_sense), 7); /* drive all lines high */
  636. __delay(200);
  637. out_le32(CNTRL_REG(p,mon_sense), 077); /* turn off drivers */
  638. __delay(2000);
  639. sense = (in_le32(CNTRL_REG(p,mon_sense)) & 0x1c0) << 2;
  640. /* drive each sense line low in turn and collect the other 2 */
  641. out_le32(CNTRL_REG(p,mon_sense), 033); /* drive A low */
  642. __delay(2000);
  643. sense |= (in_le32(CNTRL_REG(p,mon_sense)) & 0xc0) >> 2;
  644. out_le32(CNTRL_REG(p,mon_sense), 055); /* drive B low */
  645. __delay(2000);
  646. sense |= ((in_le32(CNTRL_REG(p,mon_sense)) & 0x100) >> 5)
  647. | ((in_le32(CNTRL_REG(p,mon_sense)) & 0x40) >> 4);
  648. out_le32(CNTRL_REG(p,mon_sense), 066); /* drive C low */
  649. __delay(2000);
  650. sense |= (in_le32(CNTRL_REG(p,mon_sense)) & 0x180) >> 7;
  651. out_le32(CNTRL_REG(p,mon_sense), 077); /* turn off drivers */
  652. return sense;
  653. }
  654. /********************** Various translation functions **********************/
  655. #define CONTROL_PIXCLOCK_BASE 256016
  656. #define CONTROL_PIXCLOCK_MIN 5000 /* ~ 200 MHz dot clock */
  657. /*
  658. * calculate the clock paramaters to be sent to CUDA according to given
  659. * pixclock in pico second.
  660. */
  661. static int calc_clock_params(unsigned long clk, unsigned char *param)
  662. {
  663. unsigned long p0, p1, p2, k, l, m, n, min;
  664. if (clk > (CONTROL_PIXCLOCK_BASE << 3))
  665. return 1;
  666. p2 = ((clk << 4) < CONTROL_PIXCLOCK_BASE)? 3: 2;
  667. l = clk << p2;
  668. p0 = 0;
  669. p1 = 0;
  670. for (k = 1, min = l; k < 32; k++) {
  671. unsigned long rem;
  672. m = CONTROL_PIXCLOCK_BASE * k;
  673. n = m / l;
  674. rem = m % l;
  675. if (n && (n < 128) && rem < min) {
  676. p0 = k;
  677. p1 = n;
  678. min = rem;
  679. }
  680. }
  681. if (!p0 || !p1)
  682. return 1;
  683. param[0] = p0;
  684. param[1] = p1;
  685. param[2] = p2;
  686. return 0;
  687. }
  688. /*
  689. * This routine takes a user-supplied var, and picks the best vmode/cmode
  690. * from it.
  691. */
  692. static int control_var_to_par(struct fb_var_screeninfo *var,
  693. struct fb_par_control *par, const struct fb_info *fb_info)
  694. {
  695. int cmode, piped_diff, hstep;
  696. unsigned hperiod, hssync, hsblank, hesync, heblank, piped, heq, hlfln,
  697. hserr, vperiod, vssync, vesync, veblank, vsblank, vswin, vewin;
  698. unsigned long pixclock;
  699. struct fb_info_control *p = (struct fb_info_control *) fb_info;
  700. struct control_regvals *r = &par->regvals;
  701. switch (var->bits_per_pixel) {
  702. case 8:
  703. par->cmode = CMODE_8;
  704. if (p->total_vram > 0x200000) {
  705. r->mode = 3;
  706. r->radacal_ctrl = 0x20;
  707. piped_diff = 13;
  708. } else {
  709. r->mode = 2;
  710. r->radacal_ctrl = 0x10;
  711. piped_diff = 9;
  712. }
  713. break;
  714. case 15:
  715. case 16:
  716. par->cmode = CMODE_16;
  717. if (p->total_vram > 0x200000) {
  718. r->mode = 2;
  719. r->radacal_ctrl = 0x24;
  720. piped_diff = 5;
  721. } else {
  722. r->mode = 1;
  723. r->radacal_ctrl = 0x14;
  724. piped_diff = 3;
  725. }
  726. break;
  727. case 32:
  728. par->cmode = CMODE_32;
  729. if (p->total_vram > 0x200000) {
  730. r->mode = 1;
  731. r->radacal_ctrl = 0x28;
  732. } else {
  733. r->mode = 0;
  734. r->radacal_ctrl = 0x18;
  735. }
  736. piped_diff = 1;
  737. break;
  738. default:
  739. return -EINVAL;
  740. }
  741. /*
  742. * adjust xres and vxres so that the corresponding memory widths are
  743. * 32-byte aligned
  744. */
  745. hstep = 31 >> par->cmode;
  746. par->xres = (var->xres + hstep) & ~hstep;
  747. par->vxres = (var->xres_virtual + hstep) & ~hstep;
  748. par->xoffset = (var->xoffset + hstep) & ~hstep;
  749. if (par->vxres < par->xres)
  750. par->vxres = par->xres;
  751. par->pitch = par->vxres << par->cmode;
  752. par->yres = var->yres;
  753. par->vyres = var->yres_virtual;
  754. par->yoffset = var->yoffset;
  755. if (par->vyres < par->yres)
  756. par->vyres = par->yres;
  757. par->sync = var->sync;
  758. if (par->pitch * par->vyres + CTRLFB_OFF > p->total_vram)
  759. return -EINVAL;
  760. if (par->xoffset + par->xres > par->vxres)
  761. par->xoffset = par->vxres - par->xres;
  762. if (par->yoffset + par->yres > par->vyres)
  763. par->yoffset = par->vyres - par->yres;
  764. pixclock = (var->pixclock < CONTROL_PIXCLOCK_MIN)? CONTROL_PIXCLOCK_MIN:
  765. var->pixclock;
  766. if (calc_clock_params(pixclock, r->clock_params))
  767. return -EINVAL;
  768. hperiod = ((var->left_margin + par->xres + var->right_margin
  769. + var->hsync_len) >> 1) - 2;
  770. hssync = hperiod + 1;
  771. hsblank = hssync - (var->right_margin >> 1);
  772. hesync = (var->hsync_len >> 1) - 1;
  773. heblank = (var->left_margin >> 1) + hesync;
  774. piped = heblank - piped_diff;
  775. heq = var->hsync_len >> 2;
  776. hlfln = (hperiod+2) >> 1;
  777. hserr = hssync-hesync;
  778. vperiod = (var->vsync_len + var->lower_margin + par->yres
  779. + var->upper_margin) << 1;
  780. vssync = vperiod - 2;
  781. vesync = (var->vsync_len << 1) - vperiod + vssync;
  782. veblank = (var->upper_margin << 1) + vesync;
  783. vsblank = vssync - (var->lower_margin << 1);
  784. vswin = (vsblank+vssync) >> 1;
  785. vewin = (vesync+veblank) >> 1;
  786. r->regs[0] = vswin;
  787. r->regs[1] = vsblank;
  788. r->regs[2] = veblank;
  789. r->regs[3] = vewin;
  790. r->regs[4] = vesync;
  791. r->regs[5] = vssync;
  792. r->regs[6] = vperiod;
  793. r->regs[7] = piped;
  794. r->regs[8] = hperiod;
  795. r->regs[9] = hsblank;
  796. r->regs[10] = heblank;
  797. r->regs[11] = hesync;
  798. r->regs[12] = hssync;
  799. r->regs[13] = heq;
  800. r->regs[14] = hlfln;
  801. r->regs[15] = hserr;
  802. if (par->xres >= 1280 && par->cmode >= CMODE_16)
  803. par->ctrl = 0x7f;
  804. else
  805. par->ctrl = 0x3b;
  806. if (mac_var_to_vmode(var, &par->vmode, &cmode))
  807. par->vmode = 0;
  808. return 0;
  809. }
  810. /*
  811. * Convert hardware data in par to an fb_var_screeninfo
  812. */
  813. static void control_par_to_var(struct fb_par_control *par, struct fb_var_screeninfo *var)
  814. {
  815. struct control_regints *rv;
  816. rv = (struct control_regints *) par->regvals.regs;
  817. memset(var, 0, sizeof(*var));
  818. var->xres = par->xres;
  819. var->yres = par->yres;
  820. var->xres_virtual = par->vxres;
  821. var->yres_virtual = par->vyres;
  822. var->xoffset = par->xoffset;
  823. var->yoffset = par->yoffset;
  824. switch(par->cmode) {
  825. default:
  826. case CMODE_8:
  827. var->bits_per_pixel = 8;
  828. var->red.length = 8;
  829. var->green.length = 8;
  830. var->blue.length = 8;
  831. break;
  832. case CMODE_16: /* RGB 555 */
  833. var->bits_per_pixel = 16;
  834. var->red.offset = 10;
  835. var->red.length = 5;
  836. var->green.offset = 5;
  837. var->green.length = 5;
  838. var->blue.length = 5;
  839. break;
  840. case CMODE_32: /* RGB 888 */
  841. var->bits_per_pixel = 32;
  842. var->red.offset = 16;
  843. var->red.length = 8;
  844. var->green.offset = 8;
  845. var->green.length = 8;
  846. var->blue.length = 8;
  847. var->transp.offset = 24;
  848. var->transp.length = 8;
  849. break;
  850. }
  851. var->height = -1;
  852. var->width = -1;
  853. var->vmode = FB_VMODE_NONINTERLACED;
  854. var->left_margin = (rv->heblank - rv->hesync) << 1;
  855. var->right_margin = (rv->hssync - rv->hsblank) << 1;
  856. var->hsync_len = (rv->hperiod + 2 - rv->hssync + rv->hesync) << 1;
  857. var->upper_margin = (rv->veblank - rv->vesync) >> 1;
  858. var->lower_margin = (rv->vssync - rv->vsblank) >> 1;
  859. var->vsync_len = (rv->vperiod - rv->vssync + rv->vesync) >> 1;
  860. var->sync = par->sync;
  861. /*
  862. * 10^12 * clock_params[0] / (3906400 * clock_params[1]
  863. * * 2^clock_params[2])
  864. * (10^12 * clock_params[0] / (3906400 * clock_params[1]))
  865. * >> clock_params[2]
  866. */
  867. /* (255990.17 * clock_params[0] / clock_params[1]) >> clock_params[2] */
  868. var->pixclock = CONTROL_PIXCLOCK_BASE * par->regvals.clock_params[0];
  869. var->pixclock /= par->regvals.clock_params[1];
  870. var->pixclock >>= par->regvals.clock_params[2];
  871. }
  872. /*
  873. * Set misc info vars for this driver
  874. */
  875. static void __init control_init_info(struct fb_info *info, struct fb_info_control *p)
  876. {
  877. /* Fill fb_info */
  878. info->par = &p->par;
  879. info->fbops = &controlfb_ops;
  880. info->pseudo_palette = p->pseudo_palette;
  881. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  882. info->screen_base = p->frame_buffer + CTRLFB_OFF;
  883. fb_alloc_cmap(&info->cmap, 256, 0);
  884. /* Fill fix common fields */
  885. strcpy(info->fix.id, "control");
  886. info->fix.mmio_start = p->control_regs_phys;
  887. info->fix.mmio_len = sizeof(struct control_regs);
  888. info->fix.type = FB_TYPE_PACKED_PIXELS;
  889. info->fix.smem_start = p->frame_buffer_phys + CTRLFB_OFF;
  890. info->fix.smem_len = p->total_vram - CTRLFB_OFF;
  891. info->fix.ywrapstep = 0;
  892. info->fix.type_aux = 0;
  893. info->fix.accel = FB_ACCEL_NONE;
  894. }
  895. static void control_cleanup(void)
  896. {
  897. struct fb_info_control *p = control_fb;
  898. if (!p)
  899. return;
  900. if (p->cmap_regs)
  901. iounmap(p->cmap_regs);
  902. if (p->control_regs)
  903. iounmap(p->control_regs);
  904. if (p->frame_buffer) {
  905. if (p->control_use_bank2)
  906. p->frame_buffer -= 0x600000;
  907. iounmap(p->frame_buffer);
  908. }
  909. if (p->cmap_regs_phys)
  910. release_mem_region(p->cmap_regs_phys, 0x1000);
  911. if (p->control_regs_phys)
  912. release_mem_region(p->control_regs_phys, p->control_regs_size);
  913. if (p->fb_orig_base)
  914. release_mem_region(p->fb_orig_base, p->fb_orig_size);
  915. kfree(p);
  916. }