valkyriefb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * valkyriefb.c -- frame buffer device for the PowerMac 'valkyrie' display
  3. *
  4. * Created 8 August 1998 by
  5. * Martin Costabel <[email protected]> and Kevin Schoedel
  6. *
  7. * Vmode-switching changes and vmode 15/17 modifications created 29 August
  8. * 1998 by Barry K. Nathan <[email protected]>.
  9. *
  10. * Ported to m68k Macintosh by David Huggins-Daines <[email protected]>
  11. *
  12. * Derived directly from:
  13. *
  14. * controlfb.c -- frame buffer device for the PowerMac 'control' display
  15. * Copyright (C) 1998 Dan Jacobowitz <[email protected]>
  16. *
  17. * pmc-valkyrie.c -- Console support for PowerMac "valkyrie" display adaptor.
  18. * Copyright (C) 1997 Paul Mackerras.
  19. *
  20. * and indirectly:
  21. *
  22. * Frame buffer structure from:
  23. * drivers/video/chipsfb.c -- frame buffer device for
  24. * Chips & Technologies 65550 chip.
  25. *
  26. * Copyright (C) 1998 Paul Mackerras
  27. *
  28. * This file is derived from the Powermac "chips" driver:
  29. * Copyright (C) 1997 Fabio Riccardi.
  30. * And from the frame buffer device for Open Firmware-initialized devices:
  31. * Copyright (C) 1997 Geert Uytterhoeven.
  32. *
  33. * Hardware information from:
  34. * control.c: Console support for PowerMac "control" display adaptor.
  35. * Copyright (C) 1996 Paul Mackerras
  36. *
  37. * This file is subject to the terms and conditions of the GNU General Public
  38. * License. See the file COPYING in the main directory of this archive for
  39. * more details.
  40. */
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/errno.h>
  44. #include <linux/string.h>
  45. #include <linux/mm.h>
  46. #include <linux/slab.h>
  47. #include <linux/vmalloc.h>
  48. #include <linux/delay.h>
  49. #include <linux/interrupt.h>
  50. #include <linux/fb.h>
  51. #include <linux/selection.h>
  52. #include <linux/init.h>
  53. #include <linux/nvram.h>
  54. #include <linux/adb.h>
  55. #include <linux/cuda.h>
  56. #include <linux/of_address.h>
  57. #ifdef CONFIG_MAC
  58. #include <asm/macintosh.h>
  59. #endif
  60. #include "macmodes.h"
  61. #include "valkyriefb.h"
  62. static int default_vmode = VMODE_NVRAM;
  63. static int default_cmode = CMODE_NVRAM;
  64. struct fb_par_valkyrie {
  65. int vmode, cmode;
  66. int xres, yres;
  67. int vxres, vyres;
  68. struct valkyrie_regvals *init;
  69. };
  70. struct fb_info_valkyrie {
  71. struct fb_info info;
  72. struct fb_par_valkyrie par;
  73. struct cmap_regs __iomem *cmap_regs;
  74. unsigned long cmap_regs_phys;
  75. struct valkyrie_regs __iomem *valkyrie_regs;
  76. unsigned long valkyrie_regs_phys;
  77. __u8 __iomem *frame_buffer;
  78. unsigned long frame_buffer_phys;
  79. int sense;
  80. unsigned long total_vram;
  81. u32 pseudo_palette[16];
  82. };
  83. static int valkyriefb_setup(char*);
  84. static int valkyriefb_check_var(struct fb_var_screeninfo *var,
  85. struct fb_info *info);
  86. static int valkyriefb_set_par(struct fb_info *info);
  87. static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  88. u_int transp, struct fb_info *info);
  89. static int valkyriefb_blank(int blank_mode, struct fb_info *info);
  90. static int read_valkyrie_sense(struct fb_info_valkyrie *p);
  91. static void set_valkyrie_clock(unsigned char *params);
  92. static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
  93. struct fb_par_valkyrie *par, const struct fb_info *fb_info);
  94. static int valkyrie_init_info(struct fb_info *info, struct fb_info_valkyrie *p);
  95. static void valkyrie_par_to_fix(struct fb_par_valkyrie *par, struct fb_fix_screeninfo *fix);
  96. static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p);
  97. static const struct fb_ops valkyriefb_ops = {
  98. .owner = THIS_MODULE,
  99. .fb_check_var = valkyriefb_check_var,
  100. .fb_set_par = valkyriefb_set_par,
  101. .fb_setcolreg = valkyriefb_setcolreg,
  102. .fb_blank = valkyriefb_blank,
  103. .fb_fillrect = cfb_fillrect,
  104. .fb_copyarea = cfb_copyarea,
  105. .fb_imageblit = cfb_imageblit,
  106. };
  107. /* Sets the video mode according to info->var */
  108. static int valkyriefb_set_par(struct fb_info *info)
  109. {
  110. struct fb_info_valkyrie *p =
  111. container_of(info, struct fb_info_valkyrie, info);
  112. volatile struct valkyrie_regs __iomem *valkyrie_regs = p->valkyrie_regs;
  113. struct fb_par_valkyrie *par = info->par;
  114. struct valkyrie_regvals *init;
  115. int err;
  116. if ((err = valkyrie_var_to_par(&info->var, par, info)))
  117. return err;
  118. valkyrie_par_to_fix(par, &info->fix);
  119. /* Reset the valkyrie */
  120. out_8(&valkyrie_regs->status.r, 0);
  121. udelay(100);
  122. /* Initialize display timing registers */
  123. init = par->init;
  124. out_8(&valkyrie_regs->mode.r, init->mode | 0x80);
  125. out_8(&valkyrie_regs->depth.r, par->cmode + 3);
  126. set_valkyrie_clock(init->clock_params);
  127. udelay(100);
  128. /* Turn on display */
  129. out_8(&valkyrie_regs->mode.r, init->mode);
  130. return 0;
  131. }
  132. static inline int valkyrie_par_to_var(struct fb_par_valkyrie *par,
  133. struct fb_var_screeninfo *var)
  134. {
  135. return mac_vmode_to_var(par->vmode, par->cmode, var);
  136. }
  137. static int
  138. valkyriefb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  139. {
  140. int err;
  141. struct fb_par_valkyrie par;
  142. if ((err = valkyrie_var_to_par(var, &par, info)))
  143. return err;
  144. valkyrie_par_to_var(&par, var);
  145. return 0;
  146. }
  147. /*
  148. * Blank the screen if blank_mode != 0, else unblank. If blank_mode == NULL
  149. * then the caller blanks by setting the CLUT (Color Look Up Table) to all
  150. * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
  151. * to e.g. a video mode which doesn't support it. Implements VESA suspend
  152. * and powerdown modes on hardware that supports disabling hsync/vsync:
  153. * blank_mode == 2: suspend vsync
  154. * blank_mode == 3: suspend hsync
  155. * blank_mode == 4: powerdown
  156. */
  157. static int valkyriefb_blank(int blank_mode, struct fb_info *info)
  158. {
  159. struct fb_info_valkyrie *p =
  160. container_of(info, struct fb_info_valkyrie, info);
  161. struct fb_par_valkyrie *par = info->par;
  162. struct valkyrie_regvals *init = par->init;
  163. if (init == NULL)
  164. return 1;
  165. switch (blank_mode) {
  166. case FB_BLANK_UNBLANK: /* unblank */
  167. out_8(&p->valkyrie_regs->mode.r, init->mode);
  168. break;
  169. case FB_BLANK_NORMAL:
  170. return 1; /* get caller to set CLUT to all black */
  171. case FB_BLANK_VSYNC_SUSPEND:
  172. case FB_BLANK_HSYNC_SUSPEND:
  173. /*
  174. * [kps] Value extracted from MacOS. I don't know
  175. * whether this bit disables hsync or vsync, or
  176. * whether the hardware can do the other as well.
  177. */
  178. out_8(&p->valkyrie_regs->mode.r, init->mode | 0x40);
  179. break;
  180. case FB_BLANK_POWERDOWN:
  181. out_8(&p->valkyrie_regs->mode.r, 0x66);
  182. break;
  183. }
  184. return 0;
  185. }
  186. static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  187. u_int transp, struct fb_info *info)
  188. {
  189. struct fb_info_valkyrie *p =
  190. container_of(info, struct fb_info_valkyrie, info);
  191. volatile struct cmap_regs __iomem *cmap_regs = p->cmap_regs;
  192. struct fb_par_valkyrie *par = info->par;
  193. if (regno > 255)
  194. return 1;
  195. red >>= 8;
  196. green >>= 8;
  197. blue >>= 8;
  198. /* tell clut which address to fill */
  199. out_8(&p->cmap_regs->addr, regno);
  200. udelay(1);
  201. /* send one color channel at a time */
  202. out_8(&cmap_regs->lut, red);
  203. out_8(&cmap_regs->lut, green);
  204. out_8(&cmap_regs->lut, blue);
  205. if (regno < 16 && par->cmode == CMODE_16)
  206. ((u32 *)info->pseudo_palette)[regno] =
  207. (regno << 10) | (regno << 5) | regno;
  208. return 0;
  209. }
  210. static inline int valkyrie_vram_reqd(int video_mode, int color_mode)
  211. {
  212. int pitch;
  213. struct valkyrie_regvals *init = valkyrie_reg_init[video_mode-1];
  214. if ((pitch = init->pitch[color_mode]) == 0)
  215. pitch = 2 * init->pitch[0];
  216. return init->vres * pitch;
  217. }
  218. static void set_valkyrie_clock(unsigned char *params)
  219. {
  220. #ifdef CONFIG_ADB_CUDA
  221. struct adb_request req;
  222. int i;
  223. for (i = 0; i < 3; ++i) {
  224. cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
  225. 0x50, i + 1, params[i]);
  226. while (!req.complete)
  227. cuda_poll();
  228. }
  229. #endif
  230. }
  231. static void __init valkyrie_choose_mode(struct fb_info_valkyrie *p)
  232. {
  233. p->sense = read_valkyrie_sense(p);
  234. printk(KERN_INFO "Monitor sense value = 0x%x\n", p->sense);
  235. /* Try to pick a video mode out of NVRAM if we have one. */
  236. #ifdef CONFIG_PPC_PMAC
  237. if (IS_REACHABLE(CONFIG_NVRAM) && default_vmode == VMODE_NVRAM)
  238. default_vmode = nvram_read_byte(NV_VMODE);
  239. #endif
  240. if (default_vmode <= 0 || default_vmode > VMODE_MAX ||
  241. !valkyrie_reg_init[default_vmode - 1]) {
  242. default_vmode = mac_map_monitor_sense(p->sense);
  243. if (!valkyrie_reg_init[default_vmode - 1])
  244. default_vmode = VMODE_640_480_67;
  245. }
  246. #ifdef CONFIG_PPC_PMAC
  247. if (IS_REACHABLE(CONFIG_NVRAM) && default_cmode == CMODE_NVRAM)
  248. default_cmode = nvram_read_byte(NV_CMODE);
  249. #endif
  250. /*
  251. * Reduce the pixel size if we don't have enough VRAM or bandwidth.
  252. */
  253. if (default_cmode < CMODE_8 || default_cmode > CMODE_16
  254. || valkyrie_reg_init[default_vmode-1]->pitch[default_cmode] == 0
  255. || valkyrie_vram_reqd(default_vmode, default_cmode) > p->total_vram)
  256. default_cmode = CMODE_8;
  257. printk(KERN_INFO "using video mode %d and color mode %d.\n",
  258. default_vmode, default_cmode);
  259. }
  260. static int __init valkyriefb_init(void)
  261. {
  262. struct fb_info_valkyrie *p;
  263. unsigned long frame_buffer_phys, cmap_regs_phys;
  264. int err;
  265. char *option = NULL;
  266. if (fb_get_options("valkyriefb", &option))
  267. return -ENODEV;
  268. valkyriefb_setup(option);
  269. #ifdef CONFIG_MAC
  270. if (!MACH_IS_MAC)
  271. return -ENODEV;
  272. if (!(mac_bi_data.id == MAC_MODEL_Q630
  273. /* I'm not sure about this one */
  274. || mac_bi_data.id == MAC_MODEL_P588))
  275. return -ENODEV;
  276. /* Hardcoded addresses... welcome to 68k Macintosh country :-) */
  277. frame_buffer_phys = 0xf9000000;
  278. cmap_regs_phys = 0x50f24000;
  279. #else /* ppc (!CONFIG_MAC) */
  280. {
  281. struct device_node *dp;
  282. struct resource r;
  283. dp = of_find_node_by_name(NULL, "valkyrie");
  284. if (!dp)
  285. return 0;
  286. if (of_address_to_resource(dp, 0, &r)) {
  287. printk(KERN_ERR "can't find address for valkyrie\n");
  288. return 0;
  289. }
  290. frame_buffer_phys = r.start;
  291. cmap_regs_phys = r.start + 0x304000;
  292. }
  293. #endif /* ppc (!CONFIG_MAC) */
  294. p = kzalloc(sizeof(*p), GFP_ATOMIC);
  295. if (!p)
  296. return -ENOMEM;
  297. /* Map in frame buffer and registers */
  298. if (!request_mem_region(frame_buffer_phys, 0x100000, "valkyriefb")) {
  299. kfree(p);
  300. return 0;
  301. }
  302. p->total_vram = 0x100000;
  303. p->frame_buffer_phys = frame_buffer_phys;
  304. #ifdef CONFIG_MAC
  305. p->frame_buffer = ioremap(frame_buffer_phys, p->total_vram);
  306. #else
  307. p->frame_buffer = ioremap_wt(frame_buffer_phys, p->total_vram);
  308. #endif
  309. p->cmap_regs_phys = cmap_regs_phys;
  310. p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
  311. p->valkyrie_regs_phys = cmap_regs_phys+0x6000;
  312. p->valkyrie_regs = ioremap(p->valkyrie_regs_phys, 0x1000);
  313. err = -ENOMEM;
  314. if (p->frame_buffer == NULL || p->cmap_regs == NULL
  315. || p->valkyrie_regs == NULL) {
  316. printk(KERN_ERR "valkyriefb: couldn't map resources\n");
  317. goto out_free;
  318. }
  319. valkyrie_choose_mode(p);
  320. mac_vmode_to_var(default_vmode, default_cmode, &p->info.var);
  321. err = valkyrie_init_info(&p->info, p);
  322. if (err < 0)
  323. goto out_free;
  324. valkyrie_init_fix(&p->info.fix, p);
  325. if (valkyriefb_set_par(&p->info))
  326. /* "can't happen" */
  327. printk(KERN_ERR "valkyriefb: can't set default video mode\n");
  328. if ((err = register_framebuffer(&p->info)) != 0)
  329. goto out_cmap_free;
  330. fb_info(&p->info, "valkyrie frame buffer device\n");
  331. return 0;
  332. out_cmap_free:
  333. fb_dealloc_cmap(&p->info.cmap);
  334. out_free:
  335. if (p->frame_buffer)
  336. iounmap(p->frame_buffer);
  337. if (p->cmap_regs)
  338. iounmap(p->cmap_regs);
  339. if (p->valkyrie_regs)
  340. iounmap(p->valkyrie_regs);
  341. kfree(p);
  342. return err;
  343. }
  344. /*
  345. * Get the monitor sense value.
  346. */
  347. static int read_valkyrie_sense(struct fb_info_valkyrie *p)
  348. {
  349. int sense, in;
  350. out_8(&p->valkyrie_regs->msense.r, 0); /* release all lines */
  351. __delay(20000);
  352. sense = ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x70) << 4;
  353. /* drive each sense line low in turn and collect the other 2 */
  354. out_8(&p->valkyrie_regs->msense.r, 4); /* drive A low */
  355. __delay(20000);
  356. sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x30);
  357. out_8(&p->valkyrie_regs->msense.r, 2); /* drive B low */
  358. __delay(20000);
  359. sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x40) >> 3;
  360. sense |= (in & 0x10) >> 2;
  361. out_8(&p->valkyrie_regs->msense.r, 1); /* drive C low */
  362. __delay(20000);
  363. sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x60) >> 5;
  364. out_8(&p->valkyrie_regs->msense.r, 7);
  365. return sense;
  366. }
  367. /*
  368. * This routine takes a user-supplied var,
  369. * and picks the best vmode/cmode from it.
  370. */
  371. /* [bkn] I did a major overhaul of this function.
  372. *
  373. * Much of the old code was "swiped by jonh from atyfb.c". Because
  374. * macmodes has mac_var_to_vmode, I felt that it would be better to
  375. * rework this function to use that, instead of reinventing the wheel to
  376. * add support for vmode 17. This was reinforced by the fact that
  377. * the previously swiped atyfb.c code is no longer there.
  378. *
  379. * So, I swiped and adapted platinum_var_to_par (from platinumfb.c), replacing
  380. * most, but not all, of the old code in the process. One side benefit of
  381. * swiping the platinumfb code is that we now have more comprehensible error
  382. * messages when a vmode/cmode switch fails. (Most of the error messages are
  383. * platinumfb.c, but I added two of my own, and I also changed some commas
  384. * into colons to make the messages more consistent with other Linux error
  385. * messages.) In addition, I think the new code *might* fix some vmode-
  386. * switching oddities, but I'm not sure.
  387. *
  388. * There may be some more opportunities for cleanup in here, but this is a
  389. * good start...
  390. */
  391. static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
  392. struct fb_par_valkyrie *par, const struct fb_info *fb_info)
  393. {
  394. int vmode, cmode;
  395. struct valkyrie_regvals *init;
  396. struct fb_info_valkyrie *p =
  397. container_of(fb_info, struct fb_info_valkyrie, info);
  398. if (mac_var_to_vmode(var, &vmode, &cmode) != 0) {
  399. printk(KERN_ERR "valkyriefb: can't do %dx%dx%d.\n",
  400. var->xres, var->yres, var->bits_per_pixel);
  401. return -EINVAL;
  402. }
  403. /* Check if we know about the wanted video mode */
  404. if (vmode < 1 || vmode > VMODE_MAX || !valkyrie_reg_init[vmode-1]) {
  405. printk(KERN_ERR "valkyriefb: vmode %d not valid.\n", vmode);
  406. return -EINVAL;
  407. }
  408. if (cmode != CMODE_8 && cmode != CMODE_16) {
  409. printk(KERN_ERR "valkyriefb: cmode %d not valid.\n", cmode);
  410. return -EINVAL;
  411. }
  412. if (var->xres_virtual > var->xres || var->yres_virtual > var->yres
  413. || var->xoffset != 0 || var->yoffset != 0) {
  414. return -EINVAL;
  415. }
  416. init = valkyrie_reg_init[vmode-1];
  417. if (init->pitch[cmode] == 0) {
  418. printk(KERN_ERR "valkyriefb: vmode %d does not support "
  419. "cmode %d.\n", vmode, cmode);
  420. return -EINVAL;
  421. }
  422. if (valkyrie_vram_reqd(vmode, cmode) > p->total_vram) {
  423. printk(KERN_ERR "valkyriefb: not enough ram for vmode %d, "
  424. "cmode %d.\n", vmode, cmode);
  425. return -EINVAL;
  426. }
  427. par->vmode = vmode;
  428. par->cmode = cmode;
  429. par->init = init;
  430. par->xres = var->xres;
  431. par->yres = var->yres;
  432. par->vxres = par->xres;
  433. par->vyres = par->yres;
  434. return 0;
  435. }
  436. static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p)
  437. {
  438. memset(fix, 0, sizeof(*fix));
  439. strcpy(fix->id, "valkyrie");
  440. fix->mmio_start = p->valkyrie_regs_phys;
  441. fix->mmio_len = sizeof(struct valkyrie_regs);
  442. fix->type = FB_TYPE_PACKED_PIXELS;
  443. fix->smem_start = p->frame_buffer_phys + 0x1000;
  444. fix->smem_len = p->total_vram;
  445. fix->type_aux = 0;
  446. fix->ywrapstep = 0;
  447. fix->ypanstep = 0;
  448. fix->xpanstep = 0;
  449. }
  450. /* Fix must already be inited above */
  451. static void valkyrie_par_to_fix(struct fb_par_valkyrie *par,
  452. struct fb_fix_screeninfo *fix)
  453. {
  454. fix->smem_len = valkyrie_vram_reqd(par->vmode, par->cmode);
  455. fix->visual = (par->cmode == CMODE_8) ?
  456. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
  457. fix->line_length = par->vxres << par->cmode;
  458. /* ywrapstep, xpanstep, ypanstep */
  459. }
  460. static int __init valkyrie_init_info(struct fb_info *info,
  461. struct fb_info_valkyrie *p)
  462. {
  463. info->fbops = &valkyriefb_ops;
  464. info->screen_base = p->frame_buffer + 0x1000;
  465. info->flags = FBINFO_DEFAULT;
  466. info->pseudo_palette = p->pseudo_palette;
  467. info->par = &p->par;
  468. return fb_alloc_cmap(&info->cmap, 256, 0);
  469. }
  470. /*
  471. * Parse user specified options (`video=valkyriefb:')
  472. */
  473. static int __init valkyriefb_setup(char *options)
  474. {
  475. char *this_opt;
  476. if (!options || !*options)
  477. return 0;
  478. while ((this_opt = strsep(&options, ",")) != NULL) {
  479. if (!strncmp(this_opt, "vmode:", 6)) {
  480. int vmode = simple_strtoul(this_opt+6, NULL, 0);
  481. if (vmode > 0 && vmode <= VMODE_MAX)
  482. default_vmode = vmode;
  483. }
  484. else if (!strncmp(this_opt, "cmode:", 6)) {
  485. int depth = simple_strtoul(this_opt+6, NULL, 0);
  486. switch (depth) {
  487. case 8:
  488. default_cmode = CMODE_8;
  489. break;
  490. case 15:
  491. case 16:
  492. default_cmode = CMODE_16;
  493. break;
  494. }
  495. }
  496. }
  497. return 0;
  498. }
  499. module_init(valkyriefb_init);
  500. MODULE_LICENSE("GPL");