18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	HP300 Topcat framebuffer support (derived from macfb of all things)
48c2ecf20Sopenharmony_ci *	Phil Blundell <philb@gnu.org> 1998
58c2ecf20Sopenharmony_ci *	DIO-II, colour map and Catseye support by
68c2ecf20Sopenharmony_ci *	Kars de Jong <jongk@linux-m68k.org>, May 2004.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/errno.h>
128c2ecf20Sopenharmony_ci#include <linux/string.h>
138c2ecf20Sopenharmony_ci#include <linux/mm.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/fb.h>
178c2ecf20Sopenharmony_ci#include <linux/dio.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <asm/io.h>
208c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic struct fb_info fb_info = {
238c2ecf20Sopenharmony_ci	.fix = {
248c2ecf20Sopenharmony_ci		.id		= "HP300 ",
258c2ecf20Sopenharmony_ci		.type		= FB_TYPE_PACKED_PIXELS,
268c2ecf20Sopenharmony_ci		.visual		= FB_VISUAL_PSEUDOCOLOR,
278c2ecf20Sopenharmony_ci		.accel		= FB_ACCEL_NONE,
288c2ecf20Sopenharmony_ci	}
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic unsigned long fb_regs;
328c2ecf20Sopenharmony_cistatic unsigned char fb_bitmask;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define TC_NBLANK	0x4080
358c2ecf20Sopenharmony_ci#define TC_WEN		0x4088
368c2ecf20Sopenharmony_ci#define TC_REN		0x408c
378c2ecf20Sopenharmony_ci#define TC_FBEN		0x4090
388c2ecf20Sopenharmony_ci#define TC_PRR		0x40ea
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* These defines match the X window system */
418c2ecf20Sopenharmony_ci#define RR_CLEAR	0x0
428c2ecf20Sopenharmony_ci#define RR_COPY		0x3
438c2ecf20Sopenharmony_ci#define RR_NOOP		0x5
448c2ecf20Sopenharmony_ci#define RR_XOR		0x6
458c2ecf20Sopenharmony_ci#define RR_INVERT	0xa
468c2ecf20Sopenharmony_ci#define RR_COPYINVERTED 0xc
478c2ecf20Sopenharmony_ci#define RR_SET		0xf
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* blitter regs */
508c2ecf20Sopenharmony_ci#define BUSY		0x4044
518c2ecf20Sopenharmony_ci#define WMRR		0x40ef
528c2ecf20Sopenharmony_ci#define SOURCE_X	0x40f2
538c2ecf20Sopenharmony_ci#define SOURCE_Y	0x40f6
548c2ecf20Sopenharmony_ci#define DEST_X		0x40fa
558c2ecf20Sopenharmony_ci#define DEST_Y		0x40fe
568c2ecf20Sopenharmony_ci#define WHEIGHT		0x4106
578c2ecf20Sopenharmony_ci#define WWIDTH		0x4102
588c2ecf20Sopenharmony_ci#define WMOVE		0x409c
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic struct fb_var_screeninfo hpfb_defined = {
618c2ecf20Sopenharmony_ci	.red		= {
628c2ecf20Sopenharmony_ci		.length = 8,
638c2ecf20Sopenharmony_ci	},
648c2ecf20Sopenharmony_ci	.green		= {
658c2ecf20Sopenharmony_ci		.length = 8,
668c2ecf20Sopenharmony_ci	},
678c2ecf20Sopenharmony_ci	.blue		= {
688c2ecf20Sopenharmony_ci		.length = 8,
698c2ecf20Sopenharmony_ci	},
708c2ecf20Sopenharmony_ci	.activate	= FB_ACTIVATE_NOW,
718c2ecf20Sopenharmony_ci	.height		= -1,
728c2ecf20Sopenharmony_ci	.width		= -1,
738c2ecf20Sopenharmony_ci	.vmode		= FB_VMODE_NONINTERLACED,
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic int hpfb_setcolreg(unsigned regno, unsigned red, unsigned green,
778c2ecf20Sopenharmony_ci			  unsigned blue, unsigned transp,
788c2ecf20Sopenharmony_ci			  struct fb_info *info)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	/* use MSBs */
818c2ecf20Sopenharmony_ci	unsigned char _red  =red>>8;
828c2ecf20Sopenharmony_ci	unsigned char _green=green>>8;
838c2ecf20Sopenharmony_ci	unsigned char _blue =blue>>8;
848c2ecf20Sopenharmony_ci	unsigned char _regno=regno;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	/*
878c2ecf20Sopenharmony_ci	 *  Set a single color register. The values supplied are
888c2ecf20Sopenharmony_ci	 *  already rounded down to the hardware's capabilities
898c2ecf20Sopenharmony_ci	 *  (according to the entries in the `var' structure). Return
908c2ecf20Sopenharmony_ci	 *  != 0 for invalid regno.
918c2ecf20Sopenharmony_ci	 */
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (regno >= info->cmap.len)
948c2ecf20Sopenharmony_ci		return 1;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	while (in_be16(fb_regs + 0x6002) & 0x4) udelay(1);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	out_be16(fb_regs + 0x60ba, 0xff);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	out_be16(fb_regs + 0x60b2, _red);
1018c2ecf20Sopenharmony_ci	out_be16(fb_regs + 0x60b4, _green);
1028c2ecf20Sopenharmony_ci	out_be16(fb_regs + 0x60b6, _blue);
1038c2ecf20Sopenharmony_ci	out_be16(fb_regs + 0x60b8, ~_regno);
1048c2ecf20Sopenharmony_ci	out_be16(fb_regs + 0x60f0, 0xff);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	udelay(100);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	while (in_be16(fb_regs + 0x6002) & 0x4) udelay(1);
1098c2ecf20Sopenharmony_ci	out_be16(fb_regs + 0x60b2, 0);
1108c2ecf20Sopenharmony_ci	out_be16(fb_regs + 0x60b4, 0);
1118c2ecf20Sopenharmony_ci	out_be16(fb_regs + 0x60b6, 0);
1128c2ecf20Sopenharmony_ci	out_be16(fb_regs + 0x60b8, 0);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return 0;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int hpfb_blank(int blank, struct fb_info *info)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_NBLANK, (blank ? 0x00 : fb_bitmask));
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	return 0;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic void topcat_blit(int x0, int y0, int x1, int y1, int w, int h, int rr)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	if (rr >= 0) {
1298c2ecf20Sopenharmony_ci		while (in_8(fb_regs + BUSY) & fb_bitmask)
1308c2ecf20Sopenharmony_ci			;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_FBEN, fb_bitmask);
1338c2ecf20Sopenharmony_ci	if (rr >= 0) {
1348c2ecf20Sopenharmony_ci		out_8(fb_regs + TC_WEN, fb_bitmask);
1358c2ecf20Sopenharmony_ci		out_8(fb_regs + WMRR, rr);
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci	out_be16(fb_regs + SOURCE_X, x0);
1388c2ecf20Sopenharmony_ci	out_be16(fb_regs + SOURCE_Y, y0);
1398c2ecf20Sopenharmony_ci	out_be16(fb_regs + DEST_X, x1);
1408c2ecf20Sopenharmony_ci	out_be16(fb_regs + DEST_Y, y1);
1418c2ecf20Sopenharmony_ci	out_be16(fb_regs + WWIDTH, w);
1428c2ecf20Sopenharmony_ci	out_be16(fb_regs + WHEIGHT, h);
1438c2ecf20Sopenharmony_ci	out_8(fb_regs + WMOVE, fb_bitmask);
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic void hpfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	topcat_blit(area->sx, area->sy, area->dx, area->dy, area->width, area->height, RR_COPY);
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic void hpfb_fillrect(struct fb_info *p, const struct fb_fillrect *region)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	u8 clr;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	clr = region->color & 0xff;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	while (in_8(fb_regs + BUSY) & fb_bitmask)
1588c2ecf20Sopenharmony_ci		;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/* Foreground */
1618c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_WEN, fb_bitmask & clr);
1628c2ecf20Sopenharmony_ci	out_8(fb_regs + WMRR, (region->rop == ROP_COPY ? RR_SET : RR_INVERT));
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* Background */
1658c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_WEN, fb_bitmask & ~clr);
1668c2ecf20Sopenharmony_ci	out_8(fb_regs + WMRR, (region->rop == ROP_COPY ? RR_CLEAR : RR_NOOP));
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	topcat_blit(region->dx, region->dy, region->dx, region->dy, region->width, region->height, -1);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic int hpfb_sync(struct fb_info *info)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	/*
1748c2ecf20Sopenharmony_ci	 * Since we also access the framebuffer directly, we have to wait
1758c2ecf20Sopenharmony_ci	 * until the block mover is finished
1768c2ecf20Sopenharmony_ci	 */
1778c2ecf20Sopenharmony_ci	while (in_8(fb_regs + BUSY) & fb_bitmask)
1788c2ecf20Sopenharmony_ci		;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_WEN, fb_bitmask);
1818c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_PRR, RR_COPY);
1828c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_FBEN, fb_bitmask);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	return 0;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic const struct fb_ops hpfb_ops = {
1888c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
1898c2ecf20Sopenharmony_ci	.fb_setcolreg	= hpfb_setcolreg,
1908c2ecf20Sopenharmony_ci	.fb_blank	= hpfb_blank,
1918c2ecf20Sopenharmony_ci	.fb_fillrect	= hpfb_fillrect,
1928c2ecf20Sopenharmony_ci	.fb_copyarea	= hpfb_copyarea,
1938c2ecf20Sopenharmony_ci	.fb_imageblit	= cfb_imageblit,
1948c2ecf20Sopenharmony_ci	.fb_sync	= hpfb_sync,
1958c2ecf20Sopenharmony_ci};
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/* Common to all HP framebuffers */
1988c2ecf20Sopenharmony_ci#define HPFB_FBWMSB	0x05	/* Frame buffer width 		*/
1998c2ecf20Sopenharmony_ci#define HPFB_FBWLSB	0x07
2008c2ecf20Sopenharmony_ci#define HPFB_FBHMSB	0x09	/* Frame buffer height		*/
2018c2ecf20Sopenharmony_ci#define HPFB_FBHLSB	0x0b
2028c2ecf20Sopenharmony_ci#define HPFB_DWMSB	0x0d	/* Display width		*/
2038c2ecf20Sopenharmony_ci#define HPFB_DWLSB	0x0f
2048c2ecf20Sopenharmony_ci#define HPFB_DHMSB	0x11	/* Display height		*/
2058c2ecf20Sopenharmony_ci#define HPFB_DHLSB	0x13
2068c2ecf20Sopenharmony_ci#define HPFB_NUMPLANES	0x5b	/* Number of colour planes	*/
2078c2ecf20Sopenharmony_ci#define HPFB_FBOMSB	0x5d	/* Frame buffer offset		*/
2088c2ecf20Sopenharmony_ci#define HPFB_FBOLSB	0x5f
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic int hpfb_init_one(unsigned long phys_base, unsigned long virt_base)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	unsigned long fboff, fb_width, fb_height, fb_start;
2138c2ecf20Sopenharmony_ci	int ret;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	fb_regs = virt_base;
2168c2ecf20Sopenharmony_ci	fboff = (in_8(fb_regs + HPFB_FBOMSB) << 8) | in_8(fb_regs + HPFB_FBOLSB);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	fb_info.fix.smem_start = (in_8(fb_regs + fboff) << 16);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	if (phys_base >= DIOII_BASE) {
2218c2ecf20Sopenharmony_ci		fb_info.fix.smem_start += phys_base;
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	if (DIO_SECID(fb_regs) != DIO_ID2_TOPCAT) {
2258c2ecf20Sopenharmony_ci		/* This is the magic incantation the HP X server uses to make Catseye boards work. */
2268c2ecf20Sopenharmony_ci		while (in_be16(fb_regs+0x4800) & 1)
2278c2ecf20Sopenharmony_ci			;
2288c2ecf20Sopenharmony_ci		out_be16(fb_regs+0x4800, 0);	/* Catseye status */
2298c2ecf20Sopenharmony_ci		out_be16(fb_regs+0x4510, 0);	/* VB */
2308c2ecf20Sopenharmony_ci		out_be16(fb_regs+0x4512, 0);	/* TCNTRL */
2318c2ecf20Sopenharmony_ci		out_be16(fb_regs+0x4514, 0);	/* ACNTRL */
2328c2ecf20Sopenharmony_ci		out_be16(fb_regs+0x4516, 0);	/* PNCNTRL */
2338c2ecf20Sopenharmony_ci		out_be16(fb_regs+0x4206, 0x90);	/* RUG Command/Status */
2348c2ecf20Sopenharmony_ci		out_be16(fb_regs+0x60a2, 0);	/* Overlay Mask */
2358c2ecf20Sopenharmony_ci		out_be16(fb_regs+0x60bc, 0);	/* Ram Select */
2368c2ecf20Sopenharmony_ci	}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	/*
2398c2ecf20Sopenharmony_ci	 *	Fill in the available video resolution
2408c2ecf20Sopenharmony_ci	 */
2418c2ecf20Sopenharmony_ci	fb_width = (in_8(fb_regs + HPFB_FBWMSB) << 8) | in_8(fb_regs + HPFB_FBWLSB);
2428c2ecf20Sopenharmony_ci	fb_info.fix.line_length = fb_width;
2438c2ecf20Sopenharmony_ci	fb_height = (in_8(fb_regs + HPFB_FBHMSB) << 8) | in_8(fb_regs + HPFB_FBHLSB);
2448c2ecf20Sopenharmony_ci	fb_info.fix.smem_len = fb_width * fb_height;
2458c2ecf20Sopenharmony_ci	fb_start = (unsigned long)ioremap_wt(fb_info.fix.smem_start,
2468c2ecf20Sopenharmony_ci					     fb_info.fix.smem_len);
2478c2ecf20Sopenharmony_ci	hpfb_defined.xres = (in_8(fb_regs + HPFB_DWMSB) << 8) | in_8(fb_regs + HPFB_DWLSB);
2488c2ecf20Sopenharmony_ci	hpfb_defined.yres = (in_8(fb_regs + HPFB_DHMSB) << 8) | in_8(fb_regs + HPFB_DHLSB);
2498c2ecf20Sopenharmony_ci	hpfb_defined.xres_virtual = hpfb_defined.xres;
2508c2ecf20Sopenharmony_ci	hpfb_defined.yres_virtual = hpfb_defined.yres;
2518c2ecf20Sopenharmony_ci	hpfb_defined.bits_per_pixel = in_8(fb_regs + HPFB_NUMPLANES);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	printk(KERN_INFO "hpfb: framebuffer at 0x%lx, mapped to 0x%lx, size %dk\n",
2548c2ecf20Sopenharmony_ci	       fb_info.fix.smem_start, fb_start, fb_info.fix.smem_len/1024);
2558c2ecf20Sopenharmony_ci	printk(KERN_INFO "hpfb: mode is %dx%dx%d, linelength=%d\n",
2568c2ecf20Sopenharmony_ci	       hpfb_defined.xres, hpfb_defined.yres, hpfb_defined.bits_per_pixel, fb_info.fix.line_length);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	/*
2598c2ecf20Sopenharmony_ci	 *	Give the hardware a bit of a prod and work out how many bits per
2608c2ecf20Sopenharmony_ci	 *	pixel are supported.
2618c2ecf20Sopenharmony_ci	 */
2628c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_WEN, 0xff);
2638c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_PRR, RR_COPY);
2648c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_FBEN, 0xff);
2658c2ecf20Sopenharmony_ci	out_8(fb_start, 0xff);
2668c2ecf20Sopenharmony_ci	fb_bitmask = in_8(fb_start);
2678c2ecf20Sopenharmony_ci	out_8(fb_start, 0);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	/*
2708c2ecf20Sopenharmony_ci	 *	Enable reading/writing of all the planes.
2718c2ecf20Sopenharmony_ci	 */
2728c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_WEN, fb_bitmask);
2738c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_PRR, RR_COPY);
2748c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_REN, fb_bitmask);
2758c2ecf20Sopenharmony_ci	out_8(fb_regs + TC_FBEN, fb_bitmask);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/*
2788c2ecf20Sopenharmony_ci	 *	Clear the screen.
2798c2ecf20Sopenharmony_ci	 */
2808c2ecf20Sopenharmony_ci	topcat_blit(0, 0, 0, 0, fb_width, fb_height, RR_CLEAR);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	/*
2838c2ecf20Sopenharmony_ci	 *	Let there be consoles..
2848c2ecf20Sopenharmony_ci	 */
2858c2ecf20Sopenharmony_ci	if (DIO_SECID(fb_regs) == DIO_ID2_TOPCAT)
2868c2ecf20Sopenharmony_ci		strcat(fb_info.fix.id, "Topcat");
2878c2ecf20Sopenharmony_ci	else
2888c2ecf20Sopenharmony_ci		strcat(fb_info.fix.id, "Catseye");
2898c2ecf20Sopenharmony_ci	fb_info.fbops = &hpfb_ops;
2908c2ecf20Sopenharmony_ci	fb_info.flags = FBINFO_DEFAULT;
2918c2ecf20Sopenharmony_ci	fb_info.var   = hpfb_defined;
2928c2ecf20Sopenharmony_ci	fb_info.screen_base = (char *)fb_start;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	ret = fb_alloc_cmap(&fb_info.cmap, 1 << hpfb_defined.bits_per_pixel, 0);
2958c2ecf20Sopenharmony_ci	if (ret < 0)
2968c2ecf20Sopenharmony_ci		goto unmap_screen_base;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	ret = register_framebuffer(&fb_info);
2998c2ecf20Sopenharmony_ci	if (ret < 0)
3008c2ecf20Sopenharmony_ci		goto dealloc_cmap;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	fb_info(&fb_info, "%s frame buffer device\n", fb_info.fix.id);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	return 0;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cidealloc_cmap:
3078c2ecf20Sopenharmony_ci	fb_dealloc_cmap(&fb_info.cmap);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ciunmap_screen_base:
3108c2ecf20Sopenharmony_ci	if (fb_info.screen_base) {
3118c2ecf20Sopenharmony_ci		iounmap(fb_info.screen_base);
3128c2ecf20Sopenharmony_ci		fb_info.screen_base = NULL;
3138c2ecf20Sopenharmony_ci	}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	return ret;
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci/*
3198c2ecf20Sopenharmony_ci * Check that the secondary ID indicates that we have some hope of working with this
3208c2ecf20Sopenharmony_ci * framebuffer.  The catseye boards are pretty much like topcats and we can muddle through.
3218c2ecf20Sopenharmony_ci */
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci#define topcat_sid_ok(x)  (((x) == DIO_ID2_LRCATSEYE) || ((x) == DIO_ID2_HRCCATSEYE)    \
3248c2ecf20Sopenharmony_ci			   || ((x) == DIO_ID2_HRMCATSEYE) || ((x) == DIO_ID2_TOPCAT))
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci/*
3278c2ecf20Sopenharmony_ci * Initialise the framebuffer
3288c2ecf20Sopenharmony_ci */
3298c2ecf20Sopenharmony_cistatic int hpfb_dio_probe(struct dio_dev *d, const struct dio_device_id *ent)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	unsigned long paddr, vaddr;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	paddr = d->resource.start;
3348c2ecf20Sopenharmony_ci	if (!request_mem_region(d->resource.start, resource_size(&d->resource), d->name))
3358c2ecf20Sopenharmony_ci                return -EBUSY;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	if (d->scode >= DIOII_SCBASE) {
3388c2ecf20Sopenharmony_ci		vaddr = (unsigned long)ioremap(paddr, resource_size(&d->resource));
3398c2ecf20Sopenharmony_ci	} else {
3408c2ecf20Sopenharmony_ci		vaddr = paddr + DIO_VIRADDRBASE;
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci	printk(KERN_INFO "Topcat found at DIO select code %d "
3438c2ecf20Sopenharmony_ci	       "(secondary id %02x)\n", d->scode, (d->id >> 8) & 0xff);
3448c2ecf20Sopenharmony_ci	if (hpfb_init_one(paddr, vaddr)) {
3458c2ecf20Sopenharmony_ci		if (d->scode >= DIOII_SCBASE)
3468c2ecf20Sopenharmony_ci			iounmap((void *)vaddr);
3478c2ecf20Sopenharmony_ci		return -ENOMEM;
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci	return 0;
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic void hpfb_remove_one(struct dio_dev *d)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	unregister_framebuffer(&fb_info);
3558c2ecf20Sopenharmony_ci	if (d->scode >= DIOII_SCBASE)
3568c2ecf20Sopenharmony_ci		iounmap((void *)fb_regs);
3578c2ecf20Sopenharmony_ci	release_mem_region(d->resource.start, resource_size(&d->resource));
3588c2ecf20Sopenharmony_ci	fb_dealloc_cmap(&fb_info.cmap);
3598c2ecf20Sopenharmony_ci	if (fb_info.screen_base)
3608c2ecf20Sopenharmony_ci		iounmap(fb_info.screen_base);
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic struct dio_device_id hpfb_dio_tbl[] = {
3648c2ecf20Sopenharmony_ci    { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_LRCATSEYE) },
3658c2ecf20Sopenharmony_ci    { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_HRCCATSEYE) },
3668c2ecf20Sopenharmony_ci    { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_HRMCATSEYE) },
3678c2ecf20Sopenharmony_ci    { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_TOPCAT) },
3688c2ecf20Sopenharmony_ci    { 0 }
3698c2ecf20Sopenharmony_ci};
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic struct dio_driver hpfb_driver = {
3728c2ecf20Sopenharmony_ci    .name      = "hpfb",
3738c2ecf20Sopenharmony_ci    .id_table  = hpfb_dio_tbl,
3748c2ecf20Sopenharmony_ci    .probe     = hpfb_dio_probe,
3758c2ecf20Sopenharmony_ci    .remove    = hpfb_remove_one,
3768c2ecf20Sopenharmony_ci};
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ciint __init hpfb_init(void)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	unsigned int sid;
3818c2ecf20Sopenharmony_ci	unsigned char i;
3828c2ecf20Sopenharmony_ci	int err;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* Topcats can be on the internal IO bus or real DIO devices.
3858c2ecf20Sopenharmony_ci	 * The internal variant sits at 0x560000; it has primary
3868c2ecf20Sopenharmony_ci	 * and secondary ID registers just like the DIO version.
3878c2ecf20Sopenharmony_ci	 * So we merge the two detection routines.
3888c2ecf20Sopenharmony_ci	 *
3898c2ecf20Sopenharmony_ci	 * Perhaps this #define should be in a global header file:
3908c2ecf20Sopenharmony_ci	 * I believe it's common to all internal fbs, not just topcat.
3918c2ecf20Sopenharmony_ci	 */
3928c2ecf20Sopenharmony_ci#define INTFBVADDR 0xf0560000
3938c2ecf20Sopenharmony_ci#define INTFBPADDR 0x560000
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	if (!MACH_IS_HP300)
3968c2ecf20Sopenharmony_ci		return -ENODEV;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	if (fb_get_options("hpfb", NULL))
3998c2ecf20Sopenharmony_ci		return -ENODEV;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	err = dio_register_driver(&hpfb_driver);
4028c2ecf20Sopenharmony_ci	if (err)
4038c2ecf20Sopenharmony_ci		return err;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	err = copy_from_kernel_nofault(&i, (unsigned char *)INTFBVADDR + DIO_IDOFF, 1);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	if (!err && (i == DIO_ID_FBUFFER) && topcat_sid_ok(sid = DIO_SECID(INTFBVADDR))) {
4088c2ecf20Sopenharmony_ci		if (!request_mem_region(INTFBPADDR, DIO_DEVSIZE, "Internal Topcat"))
4098c2ecf20Sopenharmony_ci			return -EBUSY;
4108c2ecf20Sopenharmony_ci		printk(KERN_INFO "Internal Topcat found (secondary id %02x)\n", sid);
4118c2ecf20Sopenharmony_ci		if (hpfb_init_one(INTFBPADDR, INTFBVADDR)) {
4128c2ecf20Sopenharmony_ci			return -ENOMEM;
4138c2ecf20Sopenharmony_ci		}
4148c2ecf20Sopenharmony_ci	}
4158c2ecf20Sopenharmony_ci	return 0;
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_civoid __exit hpfb_cleanup_module(void)
4198c2ecf20Sopenharmony_ci{
4208c2ecf20Sopenharmony_ci	dio_unregister_driver(&hpfb_driver);
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cimodule_init(hpfb_init);
4248c2ecf20Sopenharmony_cimodule_exit(hpfb_cleanup_module);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
427