18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Frame buffer device for IBM GXT4500P/6500P and GXT4000P/6000P
48c2ecf20Sopenharmony_ci * display adaptors
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2006 Paul Mackerras, IBM Corp. <paulus@samba.org>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/fb.h>
128c2ecf20Sopenharmony_ci#include <linux/console.h>
138c2ecf20Sopenharmony_ci#include <linux/pci.h>
148c2ecf20Sopenharmony_ci#include <linux/pci_ids.h>
158c2ecf20Sopenharmony_ci#include <linux/delay.h>
168c2ecf20Sopenharmony_ci#include <linux/string.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_IBM_GXT4500P	0x21c
198c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_IBM_GXT6500P	0x21b
208c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_IBM_GXT4000P	0x16e
218c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_IBM_GXT6000P	0x170
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* GXT4500P registers */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* Registers in PCI config space */
268c2ecf20Sopenharmony_ci#define CFG_ENDIAN0		0x40
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* Misc control/status registers */
298c2ecf20Sopenharmony_ci#define STATUS			0x1000
308c2ecf20Sopenharmony_ci#define CTRL_REG0		0x1004
318c2ecf20Sopenharmony_ci#define   CR0_HALT_DMA			0x4
328c2ecf20Sopenharmony_ci#define   CR0_RASTER_RESET		0x8
338c2ecf20Sopenharmony_ci#define   CR0_GEOM_RESET		0x10
348c2ecf20Sopenharmony_ci#define   CR0_MEM_CTRLER_RESET		0x20
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* Framebuffer control registers */
378c2ecf20Sopenharmony_ci#define FB_AB_CTRL		0x1100
388c2ecf20Sopenharmony_ci#define FB_CD_CTRL		0x1104
398c2ecf20Sopenharmony_ci#define FB_WID_CTRL		0x1108
408c2ecf20Sopenharmony_ci#define FB_Z_CTRL		0x110c
418c2ecf20Sopenharmony_ci#define FB_VGA_CTRL		0x1110
428c2ecf20Sopenharmony_ci#define REFRESH_AB_CTRL		0x1114
438c2ecf20Sopenharmony_ci#define REFRESH_CD_CTRL		0x1118
448c2ecf20Sopenharmony_ci#define FB_OVL_CTRL		0x111c
458c2ecf20Sopenharmony_ci#define   FB_CTRL_TYPE			0x80000000
468c2ecf20Sopenharmony_ci#define   FB_CTRL_WIDTH_MASK		0x007f0000
478c2ecf20Sopenharmony_ci#define   FB_CTRL_WIDTH_SHIFT		16
488c2ecf20Sopenharmony_ci#define   FB_CTRL_START_SEG_MASK	0x00003fff
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define REFRESH_START		0x1098
518c2ecf20Sopenharmony_ci#define REFRESH_SIZE		0x109c
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* "Direct" framebuffer access registers */
548c2ecf20Sopenharmony_ci#define DFA_FB_A		0x11e0
558c2ecf20Sopenharmony_ci#define DFA_FB_B		0x11e4
568c2ecf20Sopenharmony_ci#define DFA_FB_C		0x11e8
578c2ecf20Sopenharmony_ci#define DFA_FB_D		0x11ec
588c2ecf20Sopenharmony_ci#define   DFA_FB_ENABLE			0x80000000
598c2ecf20Sopenharmony_ci#define   DFA_FB_BASE_MASK		0x03f00000
608c2ecf20Sopenharmony_ci#define   DFA_FB_STRIDE_1k		0x00000000
618c2ecf20Sopenharmony_ci#define   DFA_FB_STRIDE_2k		0x00000010
628c2ecf20Sopenharmony_ci#define   DFA_FB_STRIDE_4k		0x00000020
638c2ecf20Sopenharmony_ci#define   DFA_PIX_8BIT			0x00000000
648c2ecf20Sopenharmony_ci#define   DFA_PIX_16BIT_565		0x00000001
658c2ecf20Sopenharmony_ci#define   DFA_PIX_16BIT_1555		0x00000002
668c2ecf20Sopenharmony_ci#define   DFA_PIX_24BIT			0x00000004
678c2ecf20Sopenharmony_ci#define   DFA_PIX_32BIT			0x00000005
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* maps DFA_PIX_* to pixel size in bytes */
708c2ecf20Sopenharmony_cistatic const unsigned char pixsize[] = {
718c2ecf20Sopenharmony_ci	1, 2, 2, 2, 4, 4
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/* Display timing generator registers */
758c2ecf20Sopenharmony_ci#define DTG_CONTROL		0x1900
768c2ecf20Sopenharmony_ci#define   DTG_CTL_SCREEN_REFRESH	2
778c2ecf20Sopenharmony_ci#define   DTG_CTL_ENABLE		1
788c2ecf20Sopenharmony_ci#define DTG_HORIZ_EXTENT	0x1904
798c2ecf20Sopenharmony_ci#define DTG_HORIZ_DISPLAY	0x1908
808c2ecf20Sopenharmony_ci#define DTG_HSYNC_START		0x190c
818c2ecf20Sopenharmony_ci#define DTG_HSYNC_END		0x1910
828c2ecf20Sopenharmony_ci#define DTG_HSYNC_END_COMP	0x1914
838c2ecf20Sopenharmony_ci#define DTG_VERT_EXTENT		0x1918
848c2ecf20Sopenharmony_ci#define DTG_VERT_DISPLAY	0x191c
858c2ecf20Sopenharmony_ci#define DTG_VSYNC_START		0x1920
868c2ecf20Sopenharmony_ci#define DTG_VSYNC_END		0x1924
878c2ecf20Sopenharmony_ci#define DTG_VERT_SHORT		0x1928
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/* PLL/RAMDAC registers */
908c2ecf20Sopenharmony_ci#define DISP_CTL		0x402c
918c2ecf20Sopenharmony_ci#define   DISP_CTL_OFF			2
928c2ecf20Sopenharmony_ci#define SYNC_CTL		0x4034
938c2ecf20Sopenharmony_ci#define   SYNC_CTL_SYNC_ON_RGB		1
948c2ecf20Sopenharmony_ci#define   SYNC_CTL_SYNC_OFF		2
958c2ecf20Sopenharmony_ci#define   SYNC_CTL_HSYNC_INV		8
968c2ecf20Sopenharmony_ci#define   SYNC_CTL_VSYNC_INV		0x10
978c2ecf20Sopenharmony_ci#define   SYNC_CTL_HSYNC_OFF		0x20
988c2ecf20Sopenharmony_ci#define   SYNC_CTL_VSYNC_OFF		0x40
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci#define PLL_M			0x4040
1018c2ecf20Sopenharmony_ci#define PLL_N			0x4044
1028c2ecf20Sopenharmony_ci#define PLL_POSTDIV		0x4048
1038c2ecf20Sopenharmony_ci#define PLL_C			0x404c
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/* Hardware cursor */
1068c2ecf20Sopenharmony_ci#define CURSOR_X		0x4078
1078c2ecf20Sopenharmony_ci#define CURSOR_Y		0x407c
1088c2ecf20Sopenharmony_ci#define CURSOR_HOTSPOT		0x4080
1098c2ecf20Sopenharmony_ci#define CURSOR_MODE		0x4084
1108c2ecf20Sopenharmony_ci#define   CURSOR_MODE_OFF		0
1118c2ecf20Sopenharmony_ci#define   CURSOR_MODE_4BPP		1
1128c2ecf20Sopenharmony_ci#define CURSOR_PIXMAP		0x5000
1138c2ecf20Sopenharmony_ci#define CURSOR_CMAP		0x7400
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/* Window attribute table */
1168c2ecf20Sopenharmony_ci#define WAT_FMT			0x4100
1178c2ecf20Sopenharmony_ci#define   WAT_FMT_24BIT			0
1188c2ecf20Sopenharmony_ci#define   WAT_FMT_16BIT_565		1
1198c2ecf20Sopenharmony_ci#define   WAT_FMT_16BIT_1555		2
1208c2ecf20Sopenharmony_ci#define   WAT_FMT_32BIT			3	/* 0 vs. 3 is a guess */
1218c2ecf20Sopenharmony_ci#define   WAT_FMT_8BIT_332		9
1228c2ecf20Sopenharmony_ci#define   WAT_FMT_8BIT			0xa
1238c2ecf20Sopenharmony_ci#define   WAT_FMT_NO_CMAP		4	/* ORd in to other values */
1248c2ecf20Sopenharmony_ci#define WAT_CMAP_OFFSET		0x4104		/* 4-bit value gets << 6 */
1258c2ecf20Sopenharmony_ci#define WAT_CTRL		0x4108
1268c2ecf20Sopenharmony_ci#define   WAT_CTRL_SEL_B		1	/* select B buffer if 1 */
1278c2ecf20Sopenharmony_ci#define   WAT_CTRL_NO_INC		2
1288c2ecf20Sopenharmony_ci#define WAT_GAMMA_CTRL		0x410c
1298c2ecf20Sopenharmony_ci#define   WAT_GAMMA_DISABLE		1	/* disables gamma cmap */
1308c2ecf20Sopenharmony_ci#define WAT_OVL_CTRL		0x430c		/* controls overlay */
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* Indexed by DFA_PIX_* values */
1338c2ecf20Sopenharmony_cistatic const unsigned char watfmt[] = {
1348c2ecf20Sopenharmony_ci	WAT_FMT_8BIT, WAT_FMT_16BIT_565, WAT_FMT_16BIT_1555, 0,
1358c2ecf20Sopenharmony_ci	WAT_FMT_24BIT, WAT_FMT_32BIT
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci/* Colormap array; 1k entries of 4 bytes each */
1398c2ecf20Sopenharmony_ci#define CMAP			0x6000
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#define readreg(par, reg)	readl((par)->regs + (reg))
1428c2ecf20Sopenharmony_ci#define writereg(par, reg, val)	writel((val), (par)->regs + (reg))
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistruct gxt4500_par {
1458c2ecf20Sopenharmony_ci	void __iomem *regs;
1468c2ecf20Sopenharmony_ci	int wc_cookie;
1478c2ecf20Sopenharmony_ci	int pixfmt;		/* pixel format, see DFA_PIX_* values */
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/* PLL parameters */
1508c2ecf20Sopenharmony_ci	int refclk_ps;		/* ref clock period in picoseconds */
1518c2ecf20Sopenharmony_ci	int pll_m;		/* ref clock divisor */
1528c2ecf20Sopenharmony_ci	int pll_n;		/* VCO divisor */
1538c2ecf20Sopenharmony_ci	int pll_pd1;		/* first post-divisor */
1548c2ecf20Sopenharmony_ci	int pll_pd2;		/* second post-divisor */
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	u32 pseudo_palette[16];	/* used in color blits */
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/* mode requested by user */
1608c2ecf20Sopenharmony_cistatic char *mode_option;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci/* default mode: 1280x1024 @ 60 Hz, 8 bpp */
1638c2ecf20Sopenharmony_cistatic const struct fb_videomode defaultmode = {
1648c2ecf20Sopenharmony_ci	.refresh = 60,
1658c2ecf20Sopenharmony_ci	.xres = 1280,
1668c2ecf20Sopenharmony_ci	.yres = 1024,
1678c2ecf20Sopenharmony_ci	.pixclock = 9295,
1688c2ecf20Sopenharmony_ci	.left_margin = 248,
1698c2ecf20Sopenharmony_ci	.right_margin = 48,
1708c2ecf20Sopenharmony_ci	.upper_margin = 38,
1718c2ecf20Sopenharmony_ci	.lower_margin = 1,
1728c2ecf20Sopenharmony_ci	.hsync_len = 112,
1738c2ecf20Sopenharmony_ci	.vsync_len = 3,
1748c2ecf20Sopenharmony_ci	.vmode = FB_VMODE_NONINTERLACED
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci/* List of supported cards */
1788c2ecf20Sopenharmony_cienum gxt_cards {
1798c2ecf20Sopenharmony_ci	GXT4500P,
1808c2ecf20Sopenharmony_ci	GXT6500P,
1818c2ecf20Sopenharmony_ci	GXT4000P,
1828c2ecf20Sopenharmony_ci	GXT6000P
1838c2ecf20Sopenharmony_ci};
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci/* Card-specific information */
1868c2ecf20Sopenharmony_cistatic const struct cardinfo {
1878c2ecf20Sopenharmony_ci	int	refclk_ps;	/* period of PLL reference clock in ps */
1888c2ecf20Sopenharmony_ci	const char *cardname;
1898c2ecf20Sopenharmony_ci} cardinfo[] = {
1908c2ecf20Sopenharmony_ci	[GXT4500P] = { .refclk_ps = 9259, .cardname = "IBM GXT4500P" },
1918c2ecf20Sopenharmony_ci	[GXT6500P] = { .refclk_ps = 9259, .cardname = "IBM GXT6500P" },
1928c2ecf20Sopenharmony_ci	[GXT4000P] = { .refclk_ps = 40000, .cardname = "IBM GXT4000P" },
1938c2ecf20Sopenharmony_ci	[GXT6000P] = { .refclk_ps = 40000, .cardname = "IBM GXT6000P" },
1948c2ecf20Sopenharmony_ci};
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci/*
1978c2ecf20Sopenharmony_ci * The refclk and VCO dividers appear to use a linear feedback shift
1988c2ecf20Sopenharmony_ci * register, which gets reloaded when it reaches a terminal value, at
1998c2ecf20Sopenharmony_ci * which point the divider output is toggled.  Thus one can obtain
2008c2ecf20Sopenharmony_ci * whatever divisor is required by putting the appropriate value into
2018c2ecf20Sopenharmony_ci * the reload register.  For a divisor of N, one puts the value from
2028c2ecf20Sopenharmony_ci * the LFSR sequence that comes N-1 places before the terminal value
2038c2ecf20Sopenharmony_ci * into the reload register.
2048c2ecf20Sopenharmony_ci */
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic const unsigned char mdivtab[] = {
2078c2ecf20Sopenharmony_ci/* 1 */		      0x3f, 0x00, 0x20, 0x10, 0x28, 0x14, 0x2a, 0x15, 0x0a,
2088c2ecf20Sopenharmony_ci/* 10 */	0x25, 0x32, 0x19, 0x0c, 0x26, 0x13, 0x09, 0x04, 0x22, 0x11,
2098c2ecf20Sopenharmony_ci/* 20 */	0x08, 0x24, 0x12, 0x29, 0x34, 0x1a, 0x2d, 0x36, 0x1b, 0x0d,
2108c2ecf20Sopenharmony_ci/* 30 */	0x06, 0x23, 0x31, 0x38, 0x1c, 0x2e, 0x17, 0x0b, 0x05, 0x02,
2118c2ecf20Sopenharmony_ci/* 40 */	0x21, 0x30, 0x18, 0x2c, 0x16, 0x2b, 0x35, 0x3a, 0x1d, 0x0e,
2128c2ecf20Sopenharmony_ci/* 50 */	0x27, 0x33, 0x39, 0x3c, 0x1e, 0x2f, 0x37, 0x3b, 0x3d, 0x3e,
2138c2ecf20Sopenharmony_ci/* 60 */	0x1f, 0x0f, 0x07, 0x03, 0x01,
2148c2ecf20Sopenharmony_ci};
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic const unsigned char ndivtab[] = {
2178c2ecf20Sopenharmony_ci/* 2 */		            0x00, 0x80, 0xc0, 0xe0, 0xf0, 0x78, 0xbc, 0x5e,
2188c2ecf20Sopenharmony_ci/* 10 */	0x2f, 0x17, 0x0b, 0x85, 0xc2, 0xe1, 0x70, 0x38, 0x9c, 0x4e,
2198c2ecf20Sopenharmony_ci/* 20 */	0xa7, 0xd3, 0xe9, 0xf4, 0xfa, 0xfd, 0xfe, 0x7f, 0xbf, 0xdf,
2208c2ecf20Sopenharmony_ci/* 30 */	0xef, 0x77, 0x3b, 0x1d, 0x8e, 0xc7, 0xe3, 0x71, 0xb8, 0xdc,
2218c2ecf20Sopenharmony_ci/* 40 */	0x6e, 0xb7, 0x5b, 0x2d, 0x16, 0x8b, 0xc5, 0xe2, 0xf1, 0xf8,
2228c2ecf20Sopenharmony_ci/* 50 */	0xfc, 0x7e, 0x3f, 0x9f, 0xcf, 0x67, 0xb3, 0xd9, 0x6c, 0xb6,
2238c2ecf20Sopenharmony_ci/* 60 */	0xdb, 0x6d, 0x36, 0x9b, 0x4d, 0x26, 0x13, 0x89, 0xc4, 0x62,
2248c2ecf20Sopenharmony_ci/* 70 */	0xb1, 0xd8, 0xec, 0xf6, 0xfb, 0x7d, 0xbe, 0x5f, 0xaf, 0x57,
2258c2ecf20Sopenharmony_ci/* 80 */	0x2b, 0x95, 0x4a, 0x25, 0x92, 0x49, 0xa4, 0x52, 0x29, 0x94,
2268c2ecf20Sopenharmony_ci/* 90 */	0xca, 0x65, 0xb2, 0x59, 0x2c, 0x96, 0xcb, 0xe5, 0xf2, 0x79,
2278c2ecf20Sopenharmony_ci/* 100 */	0x3c, 0x1e, 0x0f, 0x07, 0x83, 0x41, 0x20, 0x90, 0x48, 0x24,
2288c2ecf20Sopenharmony_ci/* 110 */	0x12, 0x09, 0x84, 0x42, 0xa1, 0x50, 0x28, 0x14, 0x8a, 0x45,
2298c2ecf20Sopenharmony_ci/* 120 */	0xa2, 0xd1, 0xe8, 0x74, 0xba, 0xdd, 0xee, 0xf7, 0x7b, 0x3d,
2308c2ecf20Sopenharmony_ci/* 130 */	0x9e, 0x4f, 0x27, 0x93, 0xc9, 0xe4, 0x72, 0x39, 0x1c, 0x0e,
2318c2ecf20Sopenharmony_ci/* 140 */	0x87, 0xc3, 0x61, 0x30, 0x18, 0x8c, 0xc6, 0x63, 0x31, 0x98,
2328c2ecf20Sopenharmony_ci/* 150 */	0xcc, 0xe6, 0x73, 0xb9, 0x5c, 0x2e, 0x97, 0x4b, 0xa5, 0xd2,
2338c2ecf20Sopenharmony_ci/* 160 */	0x69,
2348c2ecf20Sopenharmony_ci};
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic int calc_pll(int period_ps, struct gxt4500_par *par)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	int m, n, pdiv1, pdiv2, postdiv;
2398c2ecf20Sopenharmony_ci	int pll_period, best_error, t, intf;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/* only deal with range 5MHz - 300MHz */
2428c2ecf20Sopenharmony_ci	if (period_ps < 3333 || period_ps > 200000)
2438c2ecf20Sopenharmony_ci		return -1;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	best_error = 1000000;
2468c2ecf20Sopenharmony_ci	for (pdiv1 = 1; pdiv1 <= 8; ++pdiv1) {
2478c2ecf20Sopenharmony_ci		for (pdiv2 = 1; pdiv2 <= pdiv1; ++pdiv2) {
2488c2ecf20Sopenharmony_ci			postdiv = pdiv1 * pdiv2;
2498c2ecf20Sopenharmony_ci			pll_period = DIV_ROUND_UP(period_ps, postdiv);
2508c2ecf20Sopenharmony_ci			/* keep pll in range 350..600 MHz */
2518c2ecf20Sopenharmony_ci			if (pll_period < 1666 || pll_period > 2857)
2528c2ecf20Sopenharmony_ci				continue;
2538c2ecf20Sopenharmony_ci			for (m = 1; m <= 64; ++m) {
2548c2ecf20Sopenharmony_ci				intf = m * par->refclk_ps;
2558c2ecf20Sopenharmony_ci				if (intf > 500000)
2568c2ecf20Sopenharmony_ci					break;
2578c2ecf20Sopenharmony_ci				n = intf * postdiv / period_ps;
2588c2ecf20Sopenharmony_ci				if (n < 3 || n > 160)
2598c2ecf20Sopenharmony_ci					continue;
2608c2ecf20Sopenharmony_ci				t = par->refclk_ps * m * postdiv / n;
2618c2ecf20Sopenharmony_ci				t -= period_ps;
2628c2ecf20Sopenharmony_ci				if (t >= 0 && t < best_error) {
2638c2ecf20Sopenharmony_ci					par->pll_m = m;
2648c2ecf20Sopenharmony_ci					par->pll_n = n;
2658c2ecf20Sopenharmony_ci					par->pll_pd1 = pdiv1;
2668c2ecf20Sopenharmony_ci					par->pll_pd2 = pdiv2;
2678c2ecf20Sopenharmony_ci					best_error = t;
2688c2ecf20Sopenharmony_ci				}
2698c2ecf20Sopenharmony_ci			}
2708c2ecf20Sopenharmony_ci		}
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci	if (best_error == 1000000)
2738c2ecf20Sopenharmony_ci		return -1;
2748c2ecf20Sopenharmony_ci	return 0;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic int calc_pixclock(struct gxt4500_par *par)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	return par->refclk_ps * par->pll_m * par->pll_pd1 * par->pll_pd2
2808c2ecf20Sopenharmony_ci		/ par->pll_n;
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic int gxt4500_var_to_par(struct fb_var_screeninfo *var,
2848c2ecf20Sopenharmony_ci			      struct gxt4500_par *par)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	if (var->xres + var->xoffset > var->xres_virtual ||
2878c2ecf20Sopenharmony_ci	    var->yres + var->yoffset > var->yres_virtual ||
2888c2ecf20Sopenharmony_ci	    var->xres_virtual > 4096)
2898c2ecf20Sopenharmony_ci		return -EINVAL;
2908c2ecf20Sopenharmony_ci	if ((var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
2918c2ecf20Sopenharmony_ci		return -EINVAL;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	if (calc_pll(var->pixclock, par) < 0)
2948c2ecf20Sopenharmony_ci		return -EINVAL;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	switch (var->bits_per_pixel) {
2978c2ecf20Sopenharmony_ci	case 32:
2988c2ecf20Sopenharmony_ci		if (var->transp.length)
2998c2ecf20Sopenharmony_ci			par->pixfmt = DFA_PIX_32BIT;
3008c2ecf20Sopenharmony_ci		else
3018c2ecf20Sopenharmony_ci			par->pixfmt = DFA_PIX_24BIT;
3028c2ecf20Sopenharmony_ci		break;
3038c2ecf20Sopenharmony_ci	case 24:
3048c2ecf20Sopenharmony_ci		par->pixfmt = DFA_PIX_24BIT;
3058c2ecf20Sopenharmony_ci		break;
3068c2ecf20Sopenharmony_ci	case 16:
3078c2ecf20Sopenharmony_ci		if (var->green.length == 5)
3088c2ecf20Sopenharmony_ci			par->pixfmt = DFA_PIX_16BIT_1555;
3098c2ecf20Sopenharmony_ci		else
3108c2ecf20Sopenharmony_ci			par->pixfmt = DFA_PIX_16BIT_565;
3118c2ecf20Sopenharmony_ci		break;
3128c2ecf20Sopenharmony_ci	case 8:
3138c2ecf20Sopenharmony_ci		par->pixfmt = DFA_PIX_8BIT;
3148c2ecf20Sopenharmony_ci		break;
3158c2ecf20Sopenharmony_ci	default:
3168c2ecf20Sopenharmony_ci		return -EINVAL;
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	return 0;
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic const struct fb_bitfield eightbits = {0, 8};
3238c2ecf20Sopenharmony_cistatic const struct fb_bitfield nobits = {0, 0};
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_cistatic void gxt4500_unpack_pixfmt(struct fb_var_screeninfo *var,
3268c2ecf20Sopenharmony_ci				  int pixfmt)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	var->bits_per_pixel = pixsize[pixfmt] * 8;
3298c2ecf20Sopenharmony_ci	var->red = eightbits;
3308c2ecf20Sopenharmony_ci	var->green = eightbits;
3318c2ecf20Sopenharmony_ci	var->blue = eightbits;
3328c2ecf20Sopenharmony_ci	var->transp = nobits;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	switch (pixfmt) {
3358c2ecf20Sopenharmony_ci	case DFA_PIX_16BIT_565:
3368c2ecf20Sopenharmony_ci		var->red.length = 5;
3378c2ecf20Sopenharmony_ci		var->green.length = 6;
3388c2ecf20Sopenharmony_ci		var->blue.length = 5;
3398c2ecf20Sopenharmony_ci		break;
3408c2ecf20Sopenharmony_ci	case DFA_PIX_16BIT_1555:
3418c2ecf20Sopenharmony_ci		var->red.length = 5;
3428c2ecf20Sopenharmony_ci		var->green.length = 5;
3438c2ecf20Sopenharmony_ci		var->blue.length = 5;
3448c2ecf20Sopenharmony_ci		var->transp.length = 1;
3458c2ecf20Sopenharmony_ci		break;
3468c2ecf20Sopenharmony_ci	case DFA_PIX_32BIT:
3478c2ecf20Sopenharmony_ci		var->transp.length = 8;
3488c2ecf20Sopenharmony_ci		break;
3498c2ecf20Sopenharmony_ci	}
3508c2ecf20Sopenharmony_ci	if (pixfmt != DFA_PIX_8BIT) {
3518c2ecf20Sopenharmony_ci		var->blue.offset = 0;
3528c2ecf20Sopenharmony_ci		var->green.offset = var->blue.length;
3538c2ecf20Sopenharmony_ci		var->red.offset = var->green.offset + var->green.length;
3548c2ecf20Sopenharmony_ci		if (var->transp.length)
3558c2ecf20Sopenharmony_ci			var->transp.offset =
3568c2ecf20Sopenharmony_ci				var->red.offset + var->red.length;
3578c2ecf20Sopenharmony_ci	}
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic int gxt4500_check_var(struct fb_var_screeninfo *var,
3618c2ecf20Sopenharmony_ci			     struct fb_info *info)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	struct gxt4500_par par;
3648c2ecf20Sopenharmony_ci	int err;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	par = *(struct gxt4500_par *)info->par;
3678c2ecf20Sopenharmony_ci	err = gxt4500_var_to_par(var, &par);
3688c2ecf20Sopenharmony_ci	if (!err) {
3698c2ecf20Sopenharmony_ci		var->pixclock = calc_pixclock(&par);
3708c2ecf20Sopenharmony_ci		gxt4500_unpack_pixfmt(var, par.pixfmt);
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci	return err;
3738c2ecf20Sopenharmony_ci}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic int gxt4500_set_par(struct fb_info *info)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	struct gxt4500_par *par = info->par;
3788c2ecf20Sopenharmony_ci	struct fb_var_screeninfo *var = &info->var;
3798c2ecf20Sopenharmony_ci	int err;
3808c2ecf20Sopenharmony_ci	u32 ctrlreg, tmp;
3818c2ecf20Sopenharmony_ci	unsigned int dfa_ctl, pixfmt, stride;
3828c2ecf20Sopenharmony_ci	unsigned int wid_tiles, i;
3838c2ecf20Sopenharmony_ci	unsigned int prefetch_pix, htot;
3848c2ecf20Sopenharmony_ci	struct gxt4500_par save_par;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	save_par = *par;
3878c2ecf20Sopenharmony_ci	err = gxt4500_var_to_par(var, par);
3888c2ecf20Sopenharmony_ci	if (err) {
3898c2ecf20Sopenharmony_ci		*par = save_par;
3908c2ecf20Sopenharmony_ci		return err;
3918c2ecf20Sopenharmony_ci	}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	/* turn off DTG for now */
3948c2ecf20Sopenharmony_ci	ctrlreg = readreg(par, DTG_CONTROL);
3958c2ecf20Sopenharmony_ci	ctrlreg &= ~(DTG_CTL_ENABLE | DTG_CTL_SCREEN_REFRESH);
3968c2ecf20Sopenharmony_ci	writereg(par, DTG_CONTROL, ctrlreg);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	/* set PLL registers */
3998c2ecf20Sopenharmony_ci	tmp = readreg(par, PLL_C) & ~0x7f;
4008c2ecf20Sopenharmony_ci	if (par->pll_n < 38)
4018c2ecf20Sopenharmony_ci		tmp |= 0x29;
4028c2ecf20Sopenharmony_ci	if (par->pll_n < 69)
4038c2ecf20Sopenharmony_ci		tmp |= 0x35;
4048c2ecf20Sopenharmony_ci	else if (par->pll_n < 100)
4058c2ecf20Sopenharmony_ci		tmp |= 0x76;
4068c2ecf20Sopenharmony_ci	else
4078c2ecf20Sopenharmony_ci		tmp |= 0x7e;
4088c2ecf20Sopenharmony_ci	writereg(par, PLL_C, tmp);
4098c2ecf20Sopenharmony_ci	writereg(par, PLL_M, mdivtab[par->pll_m - 1]);
4108c2ecf20Sopenharmony_ci	writereg(par, PLL_N, ndivtab[par->pll_n - 2]);
4118c2ecf20Sopenharmony_ci	tmp = ((8 - par->pll_pd2) << 3) | (8 - par->pll_pd1);
4128c2ecf20Sopenharmony_ci	if (par->pll_pd1 == 8 || par->pll_pd2 == 8) {
4138c2ecf20Sopenharmony_ci		/* work around erratum */
4148c2ecf20Sopenharmony_ci		writereg(par, PLL_POSTDIV, tmp | 0x9);
4158c2ecf20Sopenharmony_ci		udelay(1);
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci	writereg(par, PLL_POSTDIV, tmp);
4188c2ecf20Sopenharmony_ci	msleep(20);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	/* turn off hardware cursor */
4218c2ecf20Sopenharmony_ci	writereg(par, CURSOR_MODE, CURSOR_MODE_OFF);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	/* reset raster engine */
4248c2ecf20Sopenharmony_ci	writereg(par, CTRL_REG0, CR0_RASTER_RESET | (CR0_RASTER_RESET << 16));
4258c2ecf20Sopenharmony_ci	udelay(10);
4268c2ecf20Sopenharmony_ci	writereg(par, CTRL_REG0, CR0_RASTER_RESET << 16);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	/* set display timing generator registers */
4298c2ecf20Sopenharmony_ci	htot = var->xres + var->left_margin + var->right_margin +
4308c2ecf20Sopenharmony_ci		var->hsync_len;
4318c2ecf20Sopenharmony_ci	writereg(par, DTG_HORIZ_EXTENT, htot - 1);
4328c2ecf20Sopenharmony_ci	writereg(par, DTG_HORIZ_DISPLAY, var->xres - 1);
4338c2ecf20Sopenharmony_ci	writereg(par, DTG_HSYNC_START, var->xres + var->right_margin - 1);
4348c2ecf20Sopenharmony_ci	writereg(par, DTG_HSYNC_END,
4358c2ecf20Sopenharmony_ci		 var->xres + var->right_margin + var->hsync_len - 1);
4368c2ecf20Sopenharmony_ci	writereg(par, DTG_HSYNC_END_COMP,
4378c2ecf20Sopenharmony_ci		 var->xres + var->right_margin + var->hsync_len - 1);
4388c2ecf20Sopenharmony_ci	writereg(par, DTG_VERT_EXTENT,
4398c2ecf20Sopenharmony_ci		 var->yres + var->upper_margin + var->lower_margin +
4408c2ecf20Sopenharmony_ci		 var->vsync_len - 1);
4418c2ecf20Sopenharmony_ci	writereg(par, DTG_VERT_DISPLAY, var->yres - 1);
4428c2ecf20Sopenharmony_ci	writereg(par, DTG_VSYNC_START, var->yres + var->lower_margin - 1);
4438c2ecf20Sopenharmony_ci	writereg(par, DTG_VSYNC_END,
4448c2ecf20Sopenharmony_ci		 var->yres + var->lower_margin + var->vsync_len - 1);
4458c2ecf20Sopenharmony_ci	prefetch_pix = 3300000 / var->pixclock;
4468c2ecf20Sopenharmony_ci	if (prefetch_pix >= htot)
4478c2ecf20Sopenharmony_ci		prefetch_pix = htot - 1;
4488c2ecf20Sopenharmony_ci	writereg(par, DTG_VERT_SHORT, htot - prefetch_pix - 1);
4498c2ecf20Sopenharmony_ci	ctrlreg |= DTG_CTL_ENABLE | DTG_CTL_SCREEN_REFRESH;
4508c2ecf20Sopenharmony_ci	writereg(par, DTG_CONTROL, ctrlreg);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	/* calculate stride in DFA aperture */
4538c2ecf20Sopenharmony_ci	if (var->xres_virtual > 2048) {
4548c2ecf20Sopenharmony_ci		stride = 4096;
4558c2ecf20Sopenharmony_ci		dfa_ctl = DFA_FB_STRIDE_4k;
4568c2ecf20Sopenharmony_ci	} else if (var->xres_virtual > 1024) {
4578c2ecf20Sopenharmony_ci		stride = 2048;
4588c2ecf20Sopenharmony_ci		dfa_ctl = DFA_FB_STRIDE_2k;
4598c2ecf20Sopenharmony_ci	} else {
4608c2ecf20Sopenharmony_ci		stride = 1024;
4618c2ecf20Sopenharmony_ci		dfa_ctl = DFA_FB_STRIDE_1k;
4628c2ecf20Sopenharmony_ci	}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	/* Set up framebuffer definition */
4658c2ecf20Sopenharmony_ci	wid_tiles = (var->xres_virtual + 63) >> 6;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	/* XXX add proper FB allocation here someday */
4688c2ecf20Sopenharmony_ci	writereg(par, FB_AB_CTRL, FB_CTRL_TYPE | (wid_tiles << 16) | 0);
4698c2ecf20Sopenharmony_ci	writereg(par, REFRESH_AB_CTRL, FB_CTRL_TYPE | (wid_tiles << 16) | 0);
4708c2ecf20Sopenharmony_ci	writereg(par, FB_CD_CTRL, FB_CTRL_TYPE | (wid_tiles << 16) | 0);
4718c2ecf20Sopenharmony_ci	writereg(par, REFRESH_CD_CTRL, FB_CTRL_TYPE | (wid_tiles << 16) | 0);
4728c2ecf20Sopenharmony_ci	writereg(par, REFRESH_START, (var->xoffset << 16) | var->yoffset);
4738c2ecf20Sopenharmony_ci	writereg(par, REFRESH_SIZE, (var->xres << 16) | var->yres);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	/* Set up framebuffer access by CPU */
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	pixfmt = par->pixfmt;
4788c2ecf20Sopenharmony_ci	dfa_ctl |= DFA_FB_ENABLE | pixfmt;
4798c2ecf20Sopenharmony_ci	writereg(par, DFA_FB_A, dfa_ctl);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	/*
4828c2ecf20Sopenharmony_ci	 * Set up window attribute table.
4838c2ecf20Sopenharmony_ci	 * We set all WAT entries the same so it doesn't matter what the
4848c2ecf20Sopenharmony_ci	 * window ID (WID) plane contains.
4858c2ecf20Sopenharmony_ci	 */
4868c2ecf20Sopenharmony_ci	for (i = 0; i < 32; ++i) {
4878c2ecf20Sopenharmony_ci		writereg(par, WAT_FMT + (i << 4), watfmt[pixfmt]);
4888c2ecf20Sopenharmony_ci		writereg(par, WAT_CMAP_OFFSET + (i << 4), 0);
4898c2ecf20Sopenharmony_ci		writereg(par, WAT_CTRL + (i << 4), 0);
4908c2ecf20Sopenharmony_ci		writereg(par, WAT_GAMMA_CTRL + (i << 4), WAT_GAMMA_DISABLE);
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/* Set sync polarity etc. */
4948c2ecf20Sopenharmony_ci	ctrlreg = readreg(par, SYNC_CTL) &
4958c2ecf20Sopenharmony_ci		~(SYNC_CTL_SYNC_ON_RGB | SYNC_CTL_HSYNC_INV |
4968c2ecf20Sopenharmony_ci		  SYNC_CTL_VSYNC_INV);
4978c2ecf20Sopenharmony_ci	if (var->sync & FB_SYNC_ON_GREEN)
4988c2ecf20Sopenharmony_ci		ctrlreg |= SYNC_CTL_SYNC_ON_RGB;
4998c2ecf20Sopenharmony_ci	if (!(var->sync & FB_SYNC_HOR_HIGH_ACT))
5008c2ecf20Sopenharmony_ci		ctrlreg |= SYNC_CTL_HSYNC_INV;
5018c2ecf20Sopenharmony_ci	if (!(var->sync & FB_SYNC_VERT_HIGH_ACT))
5028c2ecf20Sopenharmony_ci		ctrlreg |= SYNC_CTL_VSYNC_INV;
5038c2ecf20Sopenharmony_ci	writereg(par, SYNC_CTL, ctrlreg);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	info->fix.line_length = stride * pixsize[pixfmt];
5068c2ecf20Sopenharmony_ci	info->fix.visual = (pixfmt == DFA_PIX_8BIT)? FB_VISUAL_PSEUDOCOLOR:
5078c2ecf20Sopenharmony_ci		FB_VISUAL_DIRECTCOLOR;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	return 0;
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_cistatic int gxt4500_setcolreg(unsigned int reg, unsigned int red,
5138c2ecf20Sopenharmony_ci			     unsigned int green, unsigned int blue,
5148c2ecf20Sopenharmony_ci			     unsigned int transp, struct fb_info *info)
5158c2ecf20Sopenharmony_ci{
5168c2ecf20Sopenharmony_ci	u32 cmap_entry;
5178c2ecf20Sopenharmony_ci	struct gxt4500_par *par = info->par;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	if (reg > 1023)
5208c2ecf20Sopenharmony_ci		return 1;
5218c2ecf20Sopenharmony_ci	cmap_entry = ((transp & 0xff00) << 16) | ((red & 0xff00) << 8) |
5228c2ecf20Sopenharmony_ci		(green & 0xff00) | (blue >> 8);
5238c2ecf20Sopenharmony_ci	writereg(par, CMAP + reg * 4, cmap_entry);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	if (reg < 16 && par->pixfmt != DFA_PIX_8BIT) {
5268c2ecf20Sopenharmony_ci		u32 *pal = info->pseudo_palette;
5278c2ecf20Sopenharmony_ci		u32 val = reg;
5288c2ecf20Sopenharmony_ci		switch (par->pixfmt) {
5298c2ecf20Sopenharmony_ci		case DFA_PIX_16BIT_565:
5308c2ecf20Sopenharmony_ci			val |= (reg << 11) | (reg << 5);
5318c2ecf20Sopenharmony_ci			break;
5328c2ecf20Sopenharmony_ci		case DFA_PIX_16BIT_1555:
5338c2ecf20Sopenharmony_ci			val |= (reg << 10) | (reg << 5);
5348c2ecf20Sopenharmony_ci			break;
5358c2ecf20Sopenharmony_ci		case DFA_PIX_32BIT:
5368c2ecf20Sopenharmony_ci			val |= (reg << 24);
5378c2ecf20Sopenharmony_ci			fallthrough;
5388c2ecf20Sopenharmony_ci		case DFA_PIX_24BIT:
5398c2ecf20Sopenharmony_ci			val |= (reg << 16) | (reg << 8);
5408c2ecf20Sopenharmony_ci			break;
5418c2ecf20Sopenharmony_ci		}
5428c2ecf20Sopenharmony_ci		pal[reg] = val;
5438c2ecf20Sopenharmony_ci	}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	return 0;
5468c2ecf20Sopenharmony_ci}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_cistatic int gxt4500_pan_display(struct fb_var_screeninfo *var,
5498c2ecf20Sopenharmony_ci			       struct fb_info *info)
5508c2ecf20Sopenharmony_ci{
5518c2ecf20Sopenharmony_ci	struct gxt4500_par *par = info->par;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	if (var->xoffset & 7)
5548c2ecf20Sopenharmony_ci		return -EINVAL;
5558c2ecf20Sopenharmony_ci	if (var->xoffset + info->var.xres > info->var.xres_virtual ||
5568c2ecf20Sopenharmony_ci	    var->yoffset + info->var.yres > info->var.yres_virtual)
5578c2ecf20Sopenharmony_ci		return -EINVAL;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	writereg(par, REFRESH_START, (var->xoffset << 16) | var->yoffset);
5608c2ecf20Sopenharmony_ci	return 0;
5618c2ecf20Sopenharmony_ci}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_cistatic int gxt4500_blank(int blank, struct fb_info *info)
5648c2ecf20Sopenharmony_ci{
5658c2ecf20Sopenharmony_ci	struct gxt4500_par *par = info->par;
5668c2ecf20Sopenharmony_ci	int ctrl, dctl;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	ctrl = readreg(par, SYNC_CTL);
5698c2ecf20Sopenharmony_ci	ctrl &= ~(SYNC_CTL_SYNC_OFF | SYNC_CTL_HSYNC_OFF | SYNC_CTL_VSYNC_OFF);
5708c2ecf20Sopenharmony_ci	dctl = readreg(par, DISP_CTL);
5718c2ecf20Sopenharmony_ci	dctl |= DISP_CTL_OFF;
5728c2ecf20Sopenharmony_ci	switch (blank) {
5738c2ecf20Sopenharmony_ci	case FB_BLANK_UNBLANK:
5748c2ecf20Sopenharmony_ci		dctl &= ~DISP_CTL_OFF;
5758c2ecf20Sopenharmony_ci		break;
5768c2ecf20Sopenharmony_ci	case FB_BLANK_POWERDOWN:
5778c2ecf20Sopenharmony_ci		ctrl |= SYNC_CTL_SYNC_OFF;
5788c2ecf20Sopenharmony_ci		break;
5798c2ecf20Sopenharmony_ci	case FB_BLANK_HSYNC_SUSPEND:
5808c2ecf20Sopenharmony_ci		ctrl |= SYNC_CTL_HSYNC_OFF;
5818c2ecf20Sopenharmony_ci		break;
5828c2ecf20Sopenharmony_ci	case FB_BLANK_VSYNC_SUSPEND:
5838c2ecf20Sopenharmony_ci		ctrl |= SYNC_CTL_VSYNC_OFF;
5848c2ecf20Sopenharmony_ci		break;
5858c2ecf20Sopenharmony_ci	default: ;
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci	writereg(par, SYNC_CTL, ctrl);
5888c2ecf20Sopenharmony_ci	writereg(par, DISP_CTL, dctl);
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	return 0;
5918c2ecf20Sopenharmony_ci}
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_cistatic const struct fb_fix_screeninfo gxt4500_fix = {
5948c2ecf20Sopenharmony_ci	.id = "IBM GXT4500P",
5958c2ecf20Sopenharmony_ci	.type = FB_TYPE_PACKED_PIXELS,
5968c2ecf20Sopenharmony_ci	.visual = FB_VISUAL_PSEUDOCOLOR,
5978c2ecf20Sopenharmony_ci	.xpanstep = 8,
5988c2ecf20Sopenharmony_ci	.ypanstep = 1,
5998c2ecf20Sopenharmony_ci	.mmio_len = 0x20000,
6008c2ecf20Sopenharmony_ci};
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_cistatic const struct fb_ops gxt4500_ops = {
6038c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
6048c2ecf20Sopenharmony_ci	.fb_check_var = gxt4500_check_var,
6058c2ecf20Sopenharmony_ci	.fb_set_par = gxt4500_set_par,
6068c2ecf20Sopenharmony_ci	.fb_setcolreg = gxt4500_setcolreg,
6078c2ecf20Sopenharmony_ci	.fb_pan_display = gxt4500_pan_display,
6088c2ecf20Sopenharmony_ci	.fb_blank = gxt4500_blank,
6098c2ecf20Sopenharmony_ci	.fb_fillrect = cfb_fillrect,
6108c2ecf20Sopenharmony_ci	.fb_copyarea = cfb_copyarea,
6118c2ecf20Sopenharmony_ci	.fb_imageblit = cfb_imageblit,
6128c2ecf20Sopenharmony_ci};
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci/* PCI functions */
6158c2ecf20Sopenharmony_cistatic int gxt4500_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
6168c2ecf20Sopenharmony_ci{
6178c2ecf20Sopenharmony_ci	int err;
6188c2ecf20Sopenharmony_ci	unsigned long reg_phys, fb_phys;
6198c2ecf20Sopenharmony_ci	struct gxt4500_par *par;
6208c2ecf20Sopenharmony_ci	struct fb_info *info;
6218c2ecf20Sopenharmony_ci	struct fb_var_screeninfo var;
6228c2ecf20Sopenharmony_ci	enum gxt_cards cardtype;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	err = pci_enable_device(pdev);
6258c2ecf20Sopenharmony_ci	if (err) {
6268c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "gxt4500: cannot enable PCI device: %d\n",
6278c2ecf20Sopenharmony_ci			err);
6288c2ecf20Sopenharmony_ci		return err;
6298c2ecf20Sopenharmony_ci	}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	reg_phys = pci_resource_start(pdev, 0);
6328c2ecf20Sopenharmony_ci	if (!request_mem_region(reg_phys, pci_resource_len(pdev, 0),
6338c2ecf20Sopenharmony_ci				"gxt4500 regs")) {
6348c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "gxt4500: cannot get registers\n");
6358c2ecf20Sopenharmony_ci		goto err_nodev;
6368c2ecf20Sopenharmony_ci	}
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	fb_phys = pci_resource_start(pdev, 1);
6398c2ecf20Sopenharmony_ci	if (!request_mem_region(fb_phys, pci_resource_len(pdev, 1),
6408c2ecf20Sopenharmony_ci				"gxt4500 FB")) {
6418c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "gxt4500: cannot get framebuffer\n");
6428c2ecf20Sopenharmony_ci		goto err_free_regs;
6438c2ecf20Sopenharmony_ci	}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	info = framebuffer_alloc(sizeof(struct gxt4500_par), &pdev->dev);
6468c2ecf20Sopenharmony_ci	if (!info)
6478c2ecf20Sopenharmony_ci		goto err_free_fb;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	par = info->par;
6508c2ecf20Sopenharmony_ci	cardtype = ent->driver_data;
6518c2ecf20Sopenharmony_ci	par->refclk_ps = cardinfo[cardtype].refclk_ps;
6528c2ecf20Sopenharmony_ci	info->fix = gxt4500_fix;
6538c2ecf20Sopenharmony_ci	strlcpy(info->fix.id, cardinfo[cardtype].cardname,
6548c2ecf20Sopenharmony_ci		sizeof(info->fix.id));
6558c2ecf20Sopenharmony_ci	info->pseudo_palette = par->pseudo_palette;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	info->fix.mmio_start = reg_phys;
6588c2ecf20Sopenharmony_ci	par->regs = pci_ioremap_bar(pdev, 0);
6598c2ecf20Sopenharmony_ci	if (!par->regs) {
6608c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "gxt4500: cannot map registers\n");
6618c2ecf20Sopenharmony_ci		goto err_free_all;
6628c2ecf20Sopenharmony_ci	}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	info->fix.smem_start = fb_phys;
6658c2ecf20Sopenharmony_ci	info->fix.smem_len = pci_resource_len(pdev, 1);
6668c2ecf20Sopenharmony_ci	info->screen_base = pci_ioremap_wc_bar(pdev, 1);
6678c2ecf20Sopenharmony_ci	if (!info->screen_base) {
6688c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "gxt4500: cannot map framebuffer\n");
6698c2ecf20Sopenharmony_ci		goto err_unmap_regs;
6708c2ecf20Sopenharmony_ci	}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	pci_set_drvdata(pdev, info);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	par->wc_cookie = arch_phys_wc_add(info->fix.smem_start,
6758c2ecf20Sopenharmony_ci					  info->fix.smem_len);
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN
6788c2ecf20Sopenharmony_ci	/* Set byte-swapping for DFA aperture for all pixel sizes */
6798c2ecf20Sopenharmony_ci	pci_write_config_dword(pdev, CFG_ENDIAN0, 0x333300);
6808c2ecf20Sopenharmony_ci#else /* __LITTLE_ENDIAN */
6818c2ecf20Sopenharmony_ci	/* not sure what this means but fgl23 driver does that */
6828c2ecf20Sopenharmony_ci	pci_write_config_dword(pdev, CFG_ENDIAN0, 0x2300);
6838c2ecf20Sopenharmony_ci/*	pci_write_config_dword(pdev, CFG_ENDIAN0 + 4, 0x400000);*/
6848c2ecf20Sopenharmony_ci	pci_write_config_dword(pdev, CFG_ENDIAN0 + 8, 0x98530000);
6858c2ecf20Sopenharmony_ci#endif
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	info->fbops = &gxt4500_ops;
6888c2ecf20Sopenharmony_ci	info->flags = FBINFO_FLAG_DEFAULT | FBINFO_HWACCEL_XPAN |
6898c2ecf20Sopenharmony_ci					    FBINFO_HWACCEL_YPAN;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	err = fb_alloc_cmap(&info->cmap, 256, 0);
6928c2ecf20Sopenharmony_ci	if (err) {
6938c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "gxt4500: cannot allocate cmap\n");
6948c2ecf20Sopenharmony_ci		goto err_unmap_all;
6958c2ecf20Sopenharmony_ci	}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	gxt4500_blank(FB_BLANK_UNBLANK, info);
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	if (!fb_find_mode(&var, info, mode_option, NULL, 0, &defaultmode, 8)) {
7008c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "gxt4500: cannot find valid video mode\n");
7018c2ecf20Sopenharmony_ci		goto err_free_cmap;
7028c2ecf20Sopenharmony_ci	}
7038c2ecf20Sopenharmony_ci	info->var = var;
7048c2ecf20Sopenharmony_ci	if (gxt4500_set_par(info)) {
7058c2ecf20Sopenharmony_ci		printk(KERN_ERR "gxt4500: cannot set video mode\n");
7068c2ecf20Sopenharmony_ci		goto err_free_cmap;
7078c2ecf20Sopenharmony_ci	}
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	if (register_framebuffer(info) < 0) {
7108c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "gxt4500: cannot register framebuffer\n");
7118c2ecf20Sopenharmony_ci		goto err_free_cmap;
7128c2ecf20Sopenharmony_ci	}
7138c2ecf20Sopenharmony_ci	fb_info(info, "%s frame buffer device\n", info->fix.id);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	return 0;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci err_free_cmap:
7188c2ecf20Sopenharmony_ci	fb_dealloc_cmap(&info->cmap);
7198c2ecf20Sopenharmony_ci err_unmap_all:
7208c2ecf20Sopenharmony_ci	iounmap(info->screen_base);
7218c2ecf20Sopenharmony_ci err_unmap_regs:
7228c2ecf20Sopenharmony_ci	iounmap(par->regs);
7238c2ecf20Sopenharmony_ci err_free_all:
7248c2ecf20Sopenharmony_ci	framebuffer_release(info);
7258c2ecf20Sopenharmony_ci err_free_fb:
7268c2ecf20Sopenharmony_ci	release_mem_region(fb_phys, pci_resource_len(pdev, 1));
7278c2ecf20Sopenharmony_ci err_free_regs:
7288c2ecf20Sopenharmony_ci	release_mem_region(reg_phys, pci_resource_len(pdev, 0));
7298c2ecf20Sopenharmony_ci err_nodev:
7308c2ecf20Sopenharmony_ci	return -ENODEV;
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_cistatic void gxt4500_remove(struct pci_dev *pdev)
7348c2ecf20Sopenharmony_ci{
7358c2ecf20Sopenharmony_ci	struct fb_info *info = pci_get_drvdata(pdev);
7368c2ecf20Sopenharmony_ci	struct gxt4500_par *par;
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	if (!info)
7398c2ecf20Sopenharmony_ci		return;
7408c2ecf20Sopenharmony_ci	par = info->par;
7418c2ecf20Sopenharmony_ci	unregister_framebuffer(info);
7428c2ecf20Sopenharmony_ci	arch_phys_wc_del(par->wc_cookie);
7438c2ecf20Sopenharmony_ci	fb_dealloc_cmap(&info->cmap);
7448c2ecf20Sopenharmony_ci	iounmap(par->regs);
7458c2ecf20Sopenharmony_ci	iounmap(info->screen_base);
7468c2ecf20Sopenharmony_ci	release_mem_region(pci_resource_start(pdev, 0),
7478c2ecf20Sopenharmony_ci			   pci_resource_len(pdev, 0));
7488c2ecf20Sopenharmony_ci	release_mem_region(pci_resource_start(pdev, 1),
7498c2ecf20Sopenharmony_ci			   pci_resource_len(pdev, 1));
7508c2ecf20Sopenharmony_ci	framebuffer_release(info);
7518c2ecf20Sopenharmony_ci}
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci/* supported chipsets */
7548c2ecf20Sopenharmony_cistatic const struct pci_device_id gxt4500_pci_tbl[] = {
7558c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_GXT4500P),
7568c2ecf20Sopenharmony_ci	  .driver_data = GXT4500P },
7578c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_GXT6500P),
7588c2ecf20Sopenharmony_ci	  .driver_data = GXT6500P },
7598c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_GXT4000P),
7608c2ecf20Sopenharmony_ci	  .driver_data = GXT4000P },
7618c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_GXT6000P),
7628c2ecf20Sopenharmony_ci	  .driver_data = GXT6000P },
7638c2ecf20Sopenharmony_ci	{ 0 }
7648c2ecf20Sopenharmony_ci};
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, gxt4500_pci_tbl);
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_cistatic struct pci_driver gxt4500_driver = {
7698c2ecf20Sopenharmony_ci	.name = "gxt4500",
7708c2ecf20Sopenharmony_ci	.id_table = gxt4500_pci_tbl,
7718c2ecf20Sopenharmony_ci	.probe = gxt4500_probe,
7728c2ecf20Sopenharmony_ci	.remove = gxt4500_remove,
7738c2ecf20Sopenharmony_ci};
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_cistatic int gxt4500_init(void)
7768c2ecf20Sopenharmony_ci{
7778c2ecf20Sopenharmony_ci#ifndef MODULE
7788c2ecf20Sopenharmony_ci	if (fb_get_options("gxt4500", &mode_option))
7798c2ecf20Sopenharmony_ci		return -ENODEV;
7808c2ecf20Sopenharmony_ci#endif
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	return pci_register_driver(&gxt4500_driver);
7838c2ecf20Sopenharmony_ci}
7848c2ecf20Sopenharmony_cimodule_init(gxt4500_init);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_cistatic void __exit gxt4500_exit(void)
7878c2ecf20Sopenharmony_ci{
7888c2ecf20Sopenharmony_ci	pci_unregister_driver(&gxt4500_driver);
7898c2ecf20Sopenharmony_ci}
7908c2ecf20Sopenharmony_cimodule_exit(gxt4500_exit);
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ciMODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
7938c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("FBDev driver for IBM GXT4500P/6500P and GXT4000P/6000P");
7948c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
7958c2ecf20Sopenharmony_cimodule_param(mode_option, charp, 0);
7968c2ecf20Sopenharmony_ciMODULE_PARM_DESC(mode_option, "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\"");
797