18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* Geode LX framebuffer driver
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2006-2007, Advanced Micro Devices,Inc.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/errno.h>
98c2ecf20Sopenharmony_ci#include <linux/fb.h>
108c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
118c2ecf20Sopenharmony_ci#include <linux/delay.h>
128c2ecf20Sopenharmony_ci#include <linux/cs5535.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "lxfb.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/* TODO
178c2ecf20Sopenharmony_ci * Support panel scaling
188c2ecf20Sopenharmony_ci * Add acceleration
198c2ecf20Sopenharmony_ci * Add support for interlacing (TV out)
208c2ecf20Sopenharmony_ci * Support compression
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* This is the complete list of PLL frequencies that we can set -
248c2ecf20Sopenharmony_ci * we will choose the closest match to the incoming clock.
258c2ecf20Sopenharmony_ci * freq is the frequency of the dotclock * 1000 (for example,
268c2ecf20Sopenharmony_ci * 24823 = 24.983 Mhz).
278c2ecf20Sopenharmony_ci * pllval is the corresponding PLL value
288c2ecf20Sopenharmony_ci*/
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic const struct {
318c2ecf20Sopenharmony_ci  unsigned int pllval;
328c2ecf20Sopenharmony_ci  unsigned int freq;
338c2ecf20Sopenharmony_ci} pll_table[] = {
348c2ecf20Sopenharmony_ci  { 0x000131AC,   6231 },
358c2ecf20Sopenharmony_ci  { 0x0001215D,   6294 },
368c2ecf20Sopenharmony_ci  { 0x00011087,   6750 },
378c2ecf20Sopenharmony_ci  { 0x0001216C,   7081 },
388c2ecf20Sopenharmony_ci  { 0x0001218D,   7140 },
398c2ecf20Sopenharmony_ci  { 0x000110C9,   7800 },
408c2ecf20Sopenharmony_ci  { 0x00013147,   7875 },
418c2ecf20Sopenharmony_ci  { 0x000110A7,   8258 },
428c2ecf20Sopenharmony_ci  { 0x00012159,   8778 },
438c2ecf20Sopenharmony_ci  { 0x00014249,   8875 },
448c2ecf20Sopenharmony_ci  { 0x00010057,   9000 },
458c2ecf20Sopenharmony_ci  { 0x0001219A,   9472 },
468c2ecf20Sopenharmony_ci  { 0x00012158,   9792 },
478c2ecf20Sopenharmony_ci  { 0x00010045,  10000 },
488c2ecf20Sopenharmony_ci  { 0x00010089,  10791 },
498c2ecf20Sopenharmony_ci  { 0x000110E7,  11225 },
508c2ecf20Sopenharmony_ci  { 0x00012136,  11430 },
518c2ecf20Sopenharmony_ci  { 0x00013207,  12375 },
528c2ecf20Sopenharmony_ci  { 0x00012187,  12500 },
538c2ecf20Sopenharmony_ci  { 0x00014286,  14063 },
548c2ecf20Sopenharmony_ci  { 0x000110E5,  15016 },
558c2ecf20Sopenharmony_ci  { 0x00014214,  16250 },
568c2ecf20Sopenharmony_ci  { 0x00011105,  17045 },
578c2ecf20Sopenharmony_ci  { 0x000131E4,  18563 },
588c2ecf20Sopenharmony_ci  { 0x00013183,  18750 },
598c2ecf20Sopenharmony_ci  { 0x00014284,  19688 },
608c2ecf20Sopenharmony_ci  { 0x00011104,  20400 },
618c2ecf20Sopenharmony_ci  { 0x00016363,  23625 },
628c2ecf20Sopenharmony_ci  { 0x000031AC,  24923 },
638c2ecf20Sopenharmony_ci  { 0x0000215D,  25175 },
648c2ecf20Sopenharmony_ci  { 0x00001087,  27000 },
658c2ecf20Sopenharmony_ci  { 0x0000216C,  28322 },
668c2ecf20Sopenharmony_ci  { 0x0000218D,  28560 },
678c2ecf20Sopenharmony_ci  { 0x000010C9,  31200 },
688c2ecf20Sopenharmony_ci  { 0x00003147,  31500 },
698c2ecf20Sopenharmony_ci  { 0x000010A7,  33032 },
708c2ecf20Sopenharmony_ci  { 0x00002159,  35112 },
718c2ecf20Sopenharmony_ci  { 0x00004249,  35500 },
728c2ecf20Sopenharmony_ci  { 0x00000057,  36000 },
738c2ecf20Sopenharmony_ci  { 0x0000219A,  37889 },
748c2ecf20Sopenharmony_ci  { 0x00002158,  39168 },
758c2ecf20Sopenharmony_ci  { 0x00000045,  40000 },
768c2ecf20Sopenharmony_ci  { 0x00000089,  43163 },
778c2ecf20Sopenharmony_ci  { 0x000010E7,  44900 },
788c2ecf20Sopenharmony_ci  { 0x00002136,  45720 },
798c2ecf20Sopenharmony_ci  { 0x00003207,  49500 },
808c2ecf20Sopenharmony_ci  { 0x00002187,  50000 },
818c2ecf20Sopenharmony_ci  { 0x00004286,  56250 },
828c2ecf20Sopenharmony_ci  { 0x000010E5,  60065 },
838c2ecf20Sopenharmony_ci  { 0x00004214,  65000 },
848c2ecf20Sopenharmony_ci  { 0x00001105,  68179 },
858c2ecf20Sopenharmony_ci  { 0x000031E4,  74250 },
868c2ecf20Sopenharmony_ci  { 0x00003183,  75000 },
878c2ecf20Sopenharmony_ci  { 0x00004284,  78750 },
888c2ecf20Sopenharmony_ci  { 0x00001104,  81600 },
898c2ecf20Sopenharmony_ci  { 0x00006363,  94500 },
908c2ecf20Sopenharmony_ci  { 0x00005303,  97520 },
918c2ecf20Sopenharmony_ci  { 0x00002183, 100187 },
928c2ecf20Sopenharmony_ci  { 0x00002122, 101420 },
938c2ecf20Sopenharmony_ci  { 0x00001081, 108000 },
948c2ecf20Sopenharmony_ci  { 0x00006201, 113310 },
958c2ecf20Sopenharmony_ci  { 0x00000041, 119650 },
968c2ecf20Sopenharmony_ci  { 0x000041A1, 129600 },
978c2ecf20Sopenharmony_ci  { 0x00002182, 133500 },
988c2ecf20Sopenharmony_ci  { 0x000041B1, 135000 },
998c2ecf20Sopenharmony_ci  { 0x00000051, 144000 },
1008c2ecf20Sopenharmony_ci  { 0x000041E1, 148500 },
1018c2ecf20Sopenharmony_ci  { 0x000062D1, 157500 },
1028c2ecf20Sopenharmony_ci  { 0x000031A1, 162000 },
1038c2ecf20Sopenharmony_ci  { 0x00000061, 169203 },
1048c2ecf20Sopenharmony_ci  { 0x00004231, 172800 },
1058c2ecf20Sopenharmony_ci  { 0x00002151, 175500 },
1068c2ecf20Sopenharmony_ci  { 0x000052E1, 189000 },
1078c2ecf20Sopenharmony_ci  { 0x00000071, 192000 },
1088c2ecf20Sopenharmony_ci  { 0x00003201, 198000 },
1098c2ecf20Sopenharmony_ci  { 0x00004291, 202500 },
1108c2ecf20Sopenharmony_ci  { 0x00001101, 204750 },
1118c2ecf20Sopenharmony_ci  { 0x00007481, 218250 },
1128c2ecf20Sopenharmony_ci  { 0x00004170, 229500 },
1138c2ecf20Sopenharmony_ci  { 0x00006210, 234000 },
1148c2ecf20Sopenharmony_ci  { 0x00003140, 251182 },
1158c2ecf20Sopenharmony_ci  { 0x00006250, 261000 },
1168c2ecf20Sopenharmony_ci  { 0x000041C0, 278400 },
1178c2ecf20Sopenharmony_ci  { 0x00005220, 280640 },
1188c2ecf20Sopenharmony_ci  { 0x00000050, 288000 },
1198c2ecf20Sopenharmony_ci  { 0x000041E0, 297000 },
1208c2ecf20Sopenharmony_ci  { 0x00002130, 320207 }
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic void lx_set_dotpll(u32 pllval)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	u32 dotpll_lo, dotpll_hi;
1278c2ecf20Sopenharmony_ci	int i;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	rdmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	if ((dotpll_lo & MSR_GLCP_DOTPLL_LOCK) && (dotpll_hi == pllval))
1328c2ecf20Sopenharmony_ci		return;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	dotpll_hi = pllval;
1358c2ecf20Sopenharmony_ci	dotpll_lo &= ~(MSR_GLCP_DOTPLL_BYPASS | MSR_GLCP_DOTPLL_HALFPIX);
1368c2ecf20Sopenharmony_ci	dotpll_lo |= MSR_GLCP_DOTPLL_DOTRESET;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	wrmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	/* Wait 100us for the PLL to lock */
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	udelay(100);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* Now, loop for the lock bit */
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	for (i = 0; i < 1000; i++) {
1478c2ecf20Sopenharmony_ci		rdmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);
1488c2ecf20Sopenharmony_ci		if (dotpll_lo & MSR_GLCP_DOTPLL_LOCK)
1498c2ecf20Sopenharmony_ci			break;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	/* Clear the reset bit */
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	dotpll_lo &= ~MSR_GLCP_DOTPLL_DOTRESET;
1558c2ecf20Sopenharmony_ci	wrmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/* Set the clock based on the frequency specified by the current mode */
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic void lx_set_clock(struct fb_info *info)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	unsigned int diff, min, best = 0;
1638c2ecf20Sopenharmony_ci	unsigned int freq, i;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	freq = (unsigned int) (1000000000 / info->var.pixclock);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	min = abs(pll_table[0].freq - freq);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(pll_table); i++) {
1708c2ecf20Sopenharmony_ci		diff = abs(pll_table[i].freq - freq);
1718c2ecf20Sopenharmony_ci		if (diff < min) {
1728c2ecf20Sopenharmony_ci			min = diff;
1738c2ecf20Sopenharmony_ci			best = i;
1748c2ecf20Sopenharmony_ci		}
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	lx_set_dotpll(pll_table[best].pllval & 0x00017FFF);
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic void lx_graphics_disable(struct fb_info *info)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	struct lxfb_par *par = info->par;
1838c2ecf20Sopenharmony_ci	unsigned int val, gcfg;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* Note:  This assumes that the video is in a quitet state */
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	write_vp(par, VP_A1T, 0);
1888c2ecf20Sopenharmony_ci	write_vp(par, VP_A2T, 0);
1898c2ecf20Sopenharmony_ci	write_vp(par, VP_A3T, 0);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	/* Turn off the VGA and video enable */
1928c2ecf20Sopenharmony_ci	val = read_dc(par, DC_GENERAL_CFG) & ~(DC_GENERAL_CFG_VGAE |
1938c2ecf20Sopenharmony_ci			DC_GENERAL_CFG_VIDE);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	write_dc(par, DC_GENERAL_CFG, val);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	val = read_vp(par, VP_VCFG) & ~VP_VCFG_VID_EN;
1988c2ecf20Sopenharmony_ci	write_vp(par, VP_VCFG, val);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	write_dc(par, DC_IRQ, DC_IRQ_MASK | DC_IRQ_VIP_VSYNC_LOSS_IRQ_MASK |
2018c2ecf20Sopenharmony_ci			DC_IRQ_STATUS | DC_IRQ_VIP_VSYNC_IRQ_STATUS);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	val = read_dc(par, DC_GENLK_CTL) & ~DC_GENLK_CTL_GENLK_EN;
2048c2ecf20Sopenharmony_ci	write_dc(par, DC_GENLK_CTL, val);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	val = read_dc(par, DC_CLR_KEY);
2078c2ecf20Sopenharmony_ci	write_dc(par, DC_CLR_KEY, val & ~DC_CLR_KEY_CLR_KEY_EN);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	/* turn off the panel */
2108c2ecf20Sopenharmony_ci	write_fp(par, FP_PM, read_fp(par, FP_PM) & ~FP_PM_P);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	val = read_vp(par, VP_MISC) | VP_MISC_DACPWRDN;
2138c2ecf20Sopenharmony_ci	write_vp(par, VP_MISC, val);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	/* Turn off the display */
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	val = read_vp(par, VP_DCFG);
2188c2ecf20Sopenharmony_ci	write_vp(par, VP_DCFG, val & ~(VP_DCFG_CRT_EN | VP_DCFG_HSYNC_EN |
2198c2ecf20Sopenharmony_ci			VP_DCFG_VSYNC_EN | VP_DCFG_DAC_BL_EN));
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	gcfg = read_dc(par, DC_GENERAL_CFG);
2228c2ecf20Sopenharmony_ci	gcfg &= ~(DC_GENERAL_CFG_CMPE | DC_GENERAL_CFG_DECE);
2238c2ecf20Sopenharmony_ci	write_dc(par, DC_GENERAL_CFG, gcfg);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* Turn off the TGEN */
2268c2ecf20Sopenharmony_ci	val = read_dc(par, DC_DISPLAY_CFG);
2278c2ecf20Sopenharmony_ci	val &= ~DC_DISPLAY_CFG_TGEN;
2288c2ecf20Sopenharmony_ci	write_dc(par, DC_DISPLAY_CFG, val);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* Wait 1000 usecs to ensure that the TGEN is clear */
2318c2ecf20Sopenharmony_ci	udelay(1000);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	/* Turn off the FIFO loader */
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	gcfg &= ~DC_GENERAL_CFG_DFLE;
2368c2ecf20Sopenharmony_ci	write_dc(par, DC_GENERAL_CFG, gcfg);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	/* Lastly, wait for the GP to go idle */
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	do {
2418c2ecf20Sopenharmony_ci		val = read_gp(par, GP_BLT_STATUS);
2428c2ecf20Sopenharmony_ci	} while ((val & GP_BLT_STATUS_PB) || !(val & GP_BLT_STATUS_CE));
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic void lx_graphics_enable(struct fb_info *info)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	struct lxfb_par *par = info->par;
2488c2ecf20Sopenharmony_ci	u32 temp, config;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/* Set the video request register */
2518c2ecf20Sopenharmony_ci	write_vp(par, VP_VRR, 0);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	/* Set up the polarities */
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	config = read_vp(par, VP_DCFG);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	config &= ~(VP_DCFG_CRT_SYNC_SKW | VP_DCFG_PWR_SEQ_DELAY |
2588c2ecf20Sopenharmony_ci			VP_DCFG_CRT_HSYNC_POL | VP_DCFG_CRT_VSYNC_POL);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	config |= (VP_DCFG_CRT_SYNC_SKW_DEFAULT | VP_DCFG_PWR_SEQ_DELAY_DEFAULT
2618c2ecf20Sopenharmony_ci			| VP_DCFG_GV_GAM);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
2648c2ecf20Sopenharmony_ci		config |= VP_DCFG_CRT_HSYNC_POL;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
2678c2ecf20Sopenharmony_ci		config |= VP_DCFG_CRT_VSYNC_POL;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	if (par->output & OUTPUT_PANEL) {
2708c2ecf20Sopenharmony_ci		u32 msrlo, msrhi;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		write_fp(par, FP_PT1, 0);
2738c2ecf20Sopenharmony_ci		temp = FP_PT2_SCRC;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci		if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
2768c2ecf20Sopenharmony_ci			temp |= FP_PT2_HSP;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci		if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
2798c2ecf20Sopenharmony_ci			temp |= FP_PT2_VSP;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci		write_fp(par, FP_PT2, temp);
2828c2ecf20Sopenharmony_ci		write_fp(par, FP_DFC, FP_DFC_BC);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci		msrlo = MSR_LX_MSR_PADSEL_TFT_SEL_LOW;
2858c2ecf20Sopenharmony_ci		msrhi = MSR_LX_MSR_PADSEL_TFT_SEL_HIGH;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci		wrmsr(MSR_LX_MSR_PADSEL, msrlo, msrhi);
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (par->output & OUTPUT_CRT) {
2918c2ecf20Sopenharmony_ci		config |= VP_DCFG_CRT_EN | VP_DCFG_HSYNC_EN |
2928c2ecf20Sopenharmony_ci				VP_DCFG_VSYNC_EN | VP_DCFG_DAC_BL_EN;
2938c2ecf20Sopenharmony_ci	}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	write_vp(par, VP_DCFG, config);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	/* Turn the CRT dacs back on */
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	if (par->output & OUTPUT_CRT) {
3008c2ecf20Sopenharmony_ci		temp = read_vp(par, VP_MISC);
3018c2ecf20Sopenharmony_ci		temp &= ~(VP_MISC_DACPWRDN | VP_MISC_APWRDN);
3028c2ecf20Sopenharmony_ci		write_vp(par, VP_MISC, temp);
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/* Turn the panel on (if it isn't already) */
3068c2ecf20Sopenharmony_ci	if (par->output & OUTPUT_PANEL)
3078c2ecf20Sopenharmony_ci		write_fp(par, FP_PM, read_fp(par, FP_PM) | FP_PM_P);
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ciunsigned int lx_framebuffer_size(void)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	unsigned int val;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	if (!cs5535_has_vsa2()) {
3158c2ecf20Sopenharmony_ci		uint32_t hi, lo;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci		/* The number of pages is (PMAX - PMIN)+1 */
3188c2ecf20Sopenharmony_ci		rdmsr(MSR_GLIU_P2D_RO0, lo, hi);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci		/* PMAX */
3218c2ecf20Sopenharmony_ci		val = ((hi & 0xff) << 12) | ((lo & 0xfff00000) >> 20);
3228c2ecf20Sopenharmony_ci		/* PMIN */
3238c2ecf20Sopenharmony_ci		val -= (lo & 0x000fffff);
3248c2ecf20Sopenharmony_ci		val += 1;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci		/* The page size is 4k */
3278c2ecf20Sopenharmony_ci		return (val << 12);
3288c2ecf20Sopenharmony_ci	}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	/* The frame buffer size is reported by a VSM in VSA II */
3318c2ecf20Sopenharmony_ci	/* Virtual Register Class    = 0x02                     */
3328c2ecf20Sopenharmony_ci	/* VG_MEM_SIZE (1MB units)   = 0x00                     */
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	outw(VSA_VR_UNLOCK, VSA_VRC_INDEX);
3358c2ecf20Sopenharmony_ci	outw(VSA_VR_MEM_SIZE, VSA_VRC_INDEX);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	val = (unsigned int)(inw(VSA_VRC_DATA)) & 0xFE;
3388c2ecf20Sopenharmony_ci	return (val << 20);
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_civoid lx_set_mode(struct fb_info *info)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	struct lxfb_par *par = info->par;
3448c2ecf20Sopenharmony_ci	u64 msrval;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	unsigned int max, dv, val, size;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	unsigned int gcfg, dcfg;
3498c2ecf20Sopenharmony_ci	int hactive, hblankstart, hsyncstart, hsyncend, hblankend, htotal;
3508c2ecf20Sopenharmony_ci	int vactive, vblankstart, vsyncstart, vsyncend, vblankend, vtotal;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	/* Unlock the DC registers */
3538c2ecf20Sopenharmony_ci	write_dc(par, DC_UNLOCK, DC_UNLOCK_UNLOCK);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	lx_graphics_disable(info);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	lx_set_clock(info);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	/* Set output mode */
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	rdmsrl(MSR_LX_GLD_MSR_CONFIG, msrval);
3628c2ecf20Sopenharmony_ci	msrval &= ~MSR_LX_GLD_MSR_CONFIG_FMT;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	if (par->output & OUTPUT_PANEL) {
3658c2ecf20Sopenharmony_ci		msrval |= MSR_LX_GLD_MSR_CONFIG_FMT_FP;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci		if (par->output & OUTPUT_CRT)
3688c2ecf20Sopenharmony_ci			msrval |= MSR_LX_GLD_MSR_CONFIG_FPC;
3698c2ecf20Sopenharmony_ci		else
3708c2ecf20Sopenharmony_ci			msrval &= ~MSR_LX_GLD_MSR_CONFIG_FPC;
3718c2ecf20Sopenharmony_ci	} else
3728c2ecf20Sopenharmony_ci		msrval |= MSR_LX_GLD_MSR_CONFIG_FMT_CRT;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	wrmsrl(MSR_LX_GLD_MSR_CONFIG, msrval);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	/* Clear the various buffers */
3778c2ecf20Sopenharmony_ci	/* FIXME:  Adjust for panning here */
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	write_dc(par, DC_FB_ST_OFFSET, 0);
3808c2ecf20Sopenharmony_ci	write_dc(par, DC_CB_ST_OFFSET, 0);
3818c2ecf20Sopenharmony_ci	write_dc(par, DC_CURS_ST_OFFSET, 0);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	/* FIXME: Add support for interlacing */
3848c2ecf20Sopenharmony_ci	/* FIXME: Add support for scaling */
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	val = read_dc(par, DC_GENLK_CTL);
3878c2ecf20Sopenharmony_ci	val &= ~(DC_GENLK_CTL_ALPHA_FLICK_EN | DC_GENLK_CTL_FLICK_EN |
3888c2ecf20Sopenharmony_ci			DC_GENLK_CTL_FLICK_SEL_MASK);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	/* Default scaling params */
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	write_dc(par, DC_GFX_SCALE, (0x4000 << 16) | 0x4000);
3938c2ecf20Sopenharmony_ci	write_dc(par, DC_IRQ_FILT_CTL, 0);
3948c2ecf20Sopenharmony_ci	write_dc(par, DC_GENLK_CTL, val);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/* FIXME:  Support compression */
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	if (info->fix.line_length > 4096)
3998c2ecf20Sopenharmony_ci		dv = DC_DV_CTL_DV_LINE_SIZE_8K;
4008c2ecf20Sopenharmony_ci	else if (info->fix.line_length > 2048)
4018c2ecf20Sopenharmony_ci		dv = DC_DV_CTL_DV_LINE_SIZE_4K;
4028c2ecf20Sopenharmony_ci	else if (info->fix.line_length > 1024)
4038c2ecf20Sopenharmony_ci		dv = DC_DV_CTL_DV_LINE_SIZE_2K;
4048c2ecf20Sopenharmony_ci	else
4058c2ecf20Sopenharmony_ci		dv = DC_DV_CTL_DV_LINE_SIZE_1K;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	max = info->fix.line_length * info->var.yres;
4088c2ecf20Sopenharmony_ci	max = (max + 0x3FF) & 0xFFFFFC00;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	write_dc(par, DC_DV_TOP, max | DC_DV_TOP_DV_TOP_EN);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	val = read_dc(par, DC_DV_CTL) & ~DC_DV_CTL_DV_LINE_SIZE;
4138c2ecf20Sopenharmony_ci	write_dc(par, DC_DV_CTL, val | dv);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	size = info->var.xres * (info->var.bits_per_pixel >> 3);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	write_dc(par, DC_GFX_PITCH, info->fix.line_length >> 3);
4188c2ecf20Sopenharmony_ci	write_dc(par, DC_LINE_SIZE, (size + 7) >> 3);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	/* Set default watermark values */
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	rdmsrl(MSR_LX_SPARE_MSR, msrval);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	msrval &= ~(MSR_LX_SPARE_MSR_DIS_CFIFO_HGO
4258c2ecf20Sopenharmony_ci			| MSR_LX_SPARE_MSR_VFIFO_ARB_SEL
4268c2ecf20Sopenharmony_ci			| MSR_LX_SPARE_MSR_LOAD_WM_LPEN_M
4278c2ecf20Sopenharmony_ci			| MSR_LX_SPARE_MSR_WM_LPEN_OVRD);
4288c2ecf20Sopenharmony_ci	msrval |= MSR_LX_SPARE_MSR_DIS_VIFO_WM |
4298c2ecf20Sopenharmony_ci			MSR_LX_SPARE_MSR_DIS_INIT_V_PRI;
4308c2ecf20Sopenharmony_ci	wrmsrl(MSR_LX_SPARE_MSR, msrval);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	gcfg = DC_GENERAL_CFG_DFLE;   /* Display fifo enable */
4338c2ecf20Sopenharmony_ci	gcfg |= (0x6 << DC_GENERAL_CFG_DFHPSL_SHIFT) | /* default priority */
4348c2ecf20Sopenharmony_ci			(0xb << DC_GENERAL_CFG_DFHPEL_SHIFT);
4358c2ecf20Sopenharmony_ci	gcfg |= DC_GENERAL_CFG_FDTY;  /* Set the frame dirty mode */
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	dcfg  = DC_DISPLAY_CFG_VDEN;  /* Enable video data */
4388c2ecf20Sopenharmony_ci	dcfg |= DC_DISPLAY_CFG_GDEN;  /* Enable graphics */
4398c2ecf20Sopenharmony_ci	dcfg |= DC_DISPLAY_CFG_TGEN;  /* Turn on the timing generator */
4408c2ecf20Sopenharmony_ci	dcfg |= DC_DISPLAY_CFG_TRUP;  /* Update timings immediately */
4418c2ecf20Sopenharmony_ci	dcfg |= DC_DISPLAY_CFG_PALB;  /* Palette bypass in > 8 bpp modes */
4428c2ecf20Sopenharmony_ci	dcfg |= DC_DISPLAY_CFG_VISL;
4438c2ecf20Sopenharmony_ci	dcfg |= DC_DISPLAY_CFG_DCEN;  /* Always center the display */
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	/* Set the current BPP mode */
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	switch (info->var.bits_per_pixel) {
4488c2ecf20Sopenharmony_ci	case 8:
4498c2ecf20Sopenharmony_ci		dcfg |= DC_DISPLAY_CFG_DISP_MODE_8BPP;
4508c2ecf20Sopenharmony_ci		break;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	case 16:
4538c2ecf20Sopenharmony_ci		dcfg |= DC_DISPLAY_CFG_DISP_MODE_16BPP;
4548c2ecf20Sopenharmony_ci		break;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	case 32:
4578c2ecf20Sopenharmony_ci	case 24:
4588c2ecf20Sopenharmony_ci		dcfg |= DC_DISPLAY_CFG_DISP_MODE_24BPP;
4598c2ecf20Sopenharmony_ci		break;
4608c2ecf20Sopenharmony_ci	}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	/* Now - set up the timings */
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	hactive = info->var.xres;
4658c2ecf20Sopenharmony_ci	hblankstart = hactive;
4668c2ecf20Sopenharmony_ci	hsyncstart = hblankstart + info->var.right_margin;
4678c2ecf20Sopenharmony_ci	hsyncend =  hsyncstart + info->var.hsync_len;
4688c2ecf20Sopenharmony_ci	hblankend = hsyncend + info->var.left_margin;
4698c2ecf20Sopenharmony_ci	htotal = hblankend;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	vactive = info->var.yres;
4728c2ecf20Sopenharmony_ci	vblankstart = vactive;
4738c2ecf20Sopenharmony_ci	vsyncstart = vblankstart + info->var.lower_margin;
4748c2ecf20Sopenharmony_ci	vsyncend =  vsyncstart + info->var.vsync_len;
4758c2ecf20Sopenharmony_ci	vblankend = vsyncend + info->var.upper_margin;
4768c2ecf20Sopenharmony_ci	vtotal = vblankend;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	write_dc(par, DC_H_ACTIVE_TIMING, (hactive - 1) | ((htotal - 1) << 16));
4798c2ecf20Sopenharmony_ci	write_dc(par, DC_H_BLANK_TIMING,
4808c2ecf20Sopenharmony_ci			(hblankstart - 1) | ((hblankend - 1) << 16));
4818c2ecf20Sopenharmony_ci	write_dc(par, DC_H_SYNC_TIMING,
4828c2ecf20Sopenharmony_ci			(hsyncstart - 1) | ((hsyncend - 1) << 16));
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	write_dc(par, DC_V_ACTIVE_TIMING, (vactive - 1) | ((vtotal - 1) << 16));
4858c2ecf20Sopenharmony_ci	write_dc(par, DC_V_BLANK_TIMING,
4868c2ecf20Sopenharmony_ci			(vblankstart - 1) | ((vblankend - 1) << 16));
4878c2ecf20Sopenharmony_ci	write_dc(par, DC_V_SYNC_TIMING,
4888c2ecf20Sopenharmony_ci			(vsyncstart - 1) | ((vsyncend - 1) << 16));
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	write_dc(par, DC_FB_ACTIVE,
4918c2ecf20Sopenharmony_ci			(info->var.xres - 1) << 16 | (info->var.yres - 1));
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/* And re-enable the graphics output */
4948c2ecf20Sopenharmony_ci	lx_graphics_enable(info);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	/* Write the two main configuration registers */
4978c2ecf20Sopenharmony_ci	write_dc(par, DC_DISPLAY_CFG, dcfg);
4988c2ecf20Sopenharmony_ci	write_dc(par, DC_ARB_CFG, 0);
4998c2ecf20Sopenharmony_ci	write_dc(par, DC_GENERAL_CFG, gcfg);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	/* Lock the DC registers */
5028c2ecf20Sopenharmony_ci	write_dc(par, DC_UNLOCK, DC_UNLOCK_LOCK);
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_civoid lx_set_palette_reg(struct fb_info *info, unsigned regno,
5068c2ecf20Sopenharmony_ci			unsigned red, unsigned green, unsigned blue)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	struct lxfb_par *par = info->par;
5098c2ecf20Sopenharmony_ci	int val;
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	/* Hardware palette is in RGB 8-8-8 format. */
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	val  = (red   << 8) & 0xff0000;
5148c2ecf20Sopenharmony_ci	val |= (green)      & 0x00ff00;
5158c2ecf20Sopenharmony_ci	val |= (blue  >> 8) & 0x0000ff;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	write_dc(par, DC_PAL_ADDRESS, regno);
5188c2ecf20Sopenharmony_ci	write_dc(par, DC_PAL_DATA, val);
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ciint lx_blank_display(struct fb_info *info, int blank_mode)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	struct lxfb_par *par = info->par;
5248c2ecf20Sopenharmony_ci	u32 dcfg, misc, fp_pm;
5258c2ecf20Sopenharmony_ci	int blank, hsync, vsync;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	/* CRT power saving modes. */
5288c2ecf20Sopenharmony_ci	switch (blank_mode) {
5298c2ecf20Sopenharmony_ci	case FB_BLANK_UNBLANK:
5308c2ecf20Sopenharmony_ci		blank = 0; hsync = 1; vsync = 1;
5318c2ecf20Sopenharmony_ci		break;
5328c2ecf20Sopenharmony_ci	case FB_BLANK_NORMAL:
5338c2ecf20Sopenharmony_ci		blank = 1; hsync = 1; vsync = 1;
5348c2ecf20Sopenharmony_ci		break;
5358c2ecf20Sopenharmony_ci	case FB_BLANK_VSYNC_SUSPEND:
5368c2ecf20Sopenharmony_ci		blank = 1; hsync = 1; vsync = 0;
5378c2ecf20Sopenharmony_ci		break;
5388c2ecf20Sopenharmony_ci	case FB_BLANK_HSYNC_SUSPEND:
5398c2ecf20Sopenharmony_ci		blank = 1; hsync = 0; vsync = 1;
5408c2ecf20Sopenharmony_ci		break;
5418c2ecf20Sopenharmony_ci	case FB_BLANK_POWERDOWN:
5428c2ecf20Sopenharmony_ci		blank = 1; hsync = 0; vsync = 0;
5438c2ecf20Sopenharmony_ci		break;
5448c2ecf20Sopenharmony_ci	default:
5458c2ecf20Sopenharmony_ci		return -EINVAL;
5468c2ecf20Sopenharmony_ci	}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	dcfg = read_vp(par, VP_DCFG);
5498c2ecf20Sopenharmony_ci	dcfg &= ~(VP_DCFG_DAC_BL_EN | VP_DCFG_HSYNC_EN | VP_DCFG_VSYNC_EN |
5508c2ecf20Sopenharmony_ci			VP_DCFG_CRT_EN);
5518c2ecf20Sopenharmony_ci	if (!blank)
5528c2ecf20Sopenharmony_ci		dcfg |= VP_DCFG_DAC_BL_EN | VP_DCFG_CRT_EN;
5538c2ecf20Sopenharmony_ci	if (hsync)
5548c2ecf20Sopenharmony_ci		dcfg |= VP_DCFG_HSYNC_EN;
5558c2ecf20Sopenharmony_ci	if (vsync)
5568c2ecf20Sopenharmony_ci		dcfg |= VP_DCFG_VSYNC_EN;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	write_vp(par, VP_DCFG, dcfg);
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	misc = read_vp(par, VP_MISC);
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	if (vsync && hsync)
5638c2ecf20Sopenharmony_ci		misc &= ~VP_MISC_DACPWRDN;
5648c2ecf20Sopenharmony_ci	else
5658c2ecf20Sopenharmony_ci		misc |= VP_MISC_DACPWRDN;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	write_vp(par, VP_MISC, misc);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	/* Power on/off flat panel */
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	if (par->output & OUTPUT_PANEL) {
5728c2ecf20Sopenharmony_ci		fp_pm = read_fp(par, FP_PM);
5738c2ecf20Sopenharmony_ci		if (blank_mode == FB_BLANK_POWERDOWN)
5748c2ecf20Sopenharmony_ci			fp_pm &= ~FP_PM_P;
5758c2ecf20Sopenharmony_ci		else
5768c2ecf20Sopenharmony_ci			fp_pm |= FP_PM_P;
5778c2ecf20Sopenharmony_ci		write_fp(par, FP_PM, fp_pm);
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	return 0;
5818c2ecf20Sopenharmony_ci}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_cistatic void lx_save_regs(struct lxfb_par *par)
5848c2ecf20Sopenharmony_ci{
5858c2ecf20Sopenharmony_ci	uint32_t filt;
5868c2ecf20Sopenharmony_ci	int i;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	/* wait for the BLT engine to stop being busy */
5898c2ecf20Sopenharmony_ci	do {
5908c2ecf20Sopenharmony_ci		i = read_gp(par, GP_BLT_STATUS);
5918c2ecf20Sopenharmony_ci	} while ((i & GP_BLT_STATUS_PB) || !(i & GP_BLT_STATUS_CE));
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	/* save MSRs */
5948c2ecf20Sopenharmony_ci	rdmsrl(MSR_LX_MSR_PADSEL, par->msr.padsel);
5958c2ecf20Sopenharmony_ci	rdmsrl(MSR_GLCP_DOTPLL, par->msr.dotpll);
5968c2ecf20Sopenharmony_ci	rdmsrl(MSR_LX_GLD_MSR_CONFIG, par->msr.dfglcfg);
5978c2ecf20Sopenharmony_ci	rdmsrl(MSR_LX_SPARE_MSR, par->msr.dcspare);
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	write_dc(par, DC_UNLOCK, DC_UNLOCK_UNLOCK);
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	/* save registers */
6028c2ecf20Sopenharmony_ci	memcpy(par->gp, par->gp_regs, sizeof(par->gp));
6038c2ecf20Sopenharmony_ci	memcpy(par->dc, par->dc_regs, sizeof(par->dc));
6048c2ecf20Sopenharmony_ci	memcpy(par->vp, par->vp_regs, sizeof(par->vp));
6058c2ecf20Sopenharmony_ci	memcpy(par->fp, par->vp_regs + VP_FP_START, sizeof(par->fp));
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	/* save the display controller palette */
6088c2ecf20Sopenharmony_ci	write_dc(par, DC_PAL_ADDRESS, 0);
6098c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->dc_pal); i++)
6108c2ecf20Sopenharmony_ci		par->dc_pal[i] = read_dc(par, DC_PAL_DATA);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	/* save the video processor palette */
6138c2ecf20Sopenharmony_ci	write_vp(par, VP_PAR, 0);
6148c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->vp_pal); i++)
6158c2ecf20Sopenharmony_ci		par->vp_pal[i] = read_vp(par, VP_PDR);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	/* save the horizontal filter coefficients */
6188c2ecf20Sopenharmony_ci	filt = par->dc[DC_IRQ_FILT_CTL] | DC_IRQ_FILT_CTL_H_FILT_SEL;
6198c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->hcoeff); i += 2) {
6208c2ecf20Sopenharmony_ci		write_dc(par, DC_IRQ_FILT_CTL, (filt & 0xffffff00) | i);
6218c2ecf20Sopenharmony_ci		par->hcoeff[i] = read_dc(par, DC_FILT_COEFF1);
6228c2ecf20Sopenharmony_ci		par->hcoeff[i + 1] = read_dc(par, DC_FILT_COEFF2);
6238c2ecf20Sopenharmony_ci	}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	/* save the vertical filter coefficients */
6268c2ecf20Sopenharmony_ci	filt &= ~DC_IRQ_FILT_CTL_H_FILT_SEL;
6278c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->vcoeff); i++) {
6288c2ecf20Sopenharmony_ci		write_dc(par, DC_IRQ_FILT_CTL, (filt & 0xffffff00) | i);
6298c2ecf20Sopenharmony_ci		par->vcoeff[i] = read_dc(par, DC_FILT_COEFF1);
6308c2ecf20Sopenharmony_ci	}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	/* save video coeff ram */
6338c2ecf20Sopenharmony_ci	memcpy(par->vp_coeff, par->vp_regs + VP_VCR, sizeof(par->vp_coeff));
6348c2ecf20Sopenharmony_ci}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_cistatic void lx_restore_gfx_proc(struct lxfb_par *par)
6378c2ecf20Sopenharmony_ci{
6388c2ecf20Sopenharmony_ci	int i;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	/* a bunch of registers require GP_RASTER_MODE to be set first */
6418c2ecf20Sopenharmony_ci	write_gp(par, GP_RASTER_MODE, par->gp[GP_RASTER_MODE]);
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->gp); i++) {
6448c2ecf20Sopenharmony_ci		switch (i) {
6458c2ecf20Sopenharmony_ci		case GP_RASTER_MODE:
6468c2ecf20Sopenharmony_ci		case GP_VECTOR_MODE:
6478c2ecf20Sopenharmony_ci		case GP_BLT_MODE:
6488c2ecf20Sopenharmony_ci		case GP_BLT_STATUS:
6498c2ecf20Sopenharmony_ci		case GP_HST_SRC:
6508c2ecf20Sopenharmony_ci			/* FIXME: restore LUT data */
6518c2ecf20Sopenharmony_ci		case GP_LUT_INDEX:
6528c2ecf20Sopenharmony_ci		case GP_LUT_DATA:
6538c2ecf20Sopenharmony_ci			/* don't restore these registers */
6548c2ecf20Sopenharmony_ci			break;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci		default:
6578c2ecf20Sopenharmony_ci			write_gp(par, i, par->gp[i]);
6588c2ecf20Sopenharmony_ci		}
6598c2ecf20Sopenharmony_ci	}
6608c2ecf20Sopenharmony_ci}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_cistatic void lx_restore_display_ctlr(struct lxfb_par *par)
6638c2ecf20Sopenharmony_ci{
6648c2ecf20Sopenharmony_ci	uint32_t filt;
6658c2ecf20Sopenharmony_ci	int i;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	wrmsrl(MSR_LX_SPARE_MSR, par->msr.dcspare);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->dc); i++) {
6708c2ecf20Sopenharmony_ci		switch (i) {
6718c2ecf20Sopenharmony_ci		case DC_UNLOCK:
6728c2ecf20Sopenharmony_ci			/* unlock the DC; runs first */
6738c2ecf20Sopenharmony_ci			write_dc(par, DC_UNLOCK, DC_UNLOCK_UNLOCK);
6748c2ecf20Sopenharmony_ci			break;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci		case DC_GENERAL_CFG:
6778c2ecf20Sopenharmony_ci		case DC_DISPLAY_CFG:
6788c2ecf20Sopenharmony_ci			/* disable all while restoring */
6798c2ecf20Sopenharmony_ci			write_dc(par, i, 0);
6808c2ecf20Sopenharmony_ci			break;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci		case DC_DV_CTL:
6838c2ecf20Sopenharmony_ci			/* set all ram to dirty */
6848c2ecf20Sopenharmony_ci			write_dc(par, i, par->dc[i] | DC_DV_CTL_CLEAR_DV_RAM);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci		case DC_RSVD_1:
6878c2ecf20Sopenharmony_ci		case DC_RSVD_2:
6888c2ecf20Sopenharmony_ci		case DC_RSVD_3:
6898c2ecf20Sopenharmony_ci		case DC_LINE_CNT:
6908c2ecf20Sopenharmony_ci		case DC_PAL_ADDRESS:
6918c2ecf20Sopenharmony_ci		case DC_PAL_DATA:
6928c2ecf20Sopenharmony_ci		case DC_DFIFO_DIAG:
6938c2ecf20Sopenharmony_ci		case DC_CFIFO_DIAG:
6948c2ecf20Sopenharmony_ci		case DC_FILT_COEFF1:
6958c2ecf20Sopenharmony_ci		case DC_FILT_COEFF2:
6968c2ecf20Sopenharmony_ci		case DC_RSVD_4:
6978c2ecf20Sopenharmony_ci		case DC_RSVD_5:
6988c2ecf20Sopenharmony_ci			/* don't restore these registers */
6998c2ecf20Sopenharmony_ci			break;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci		default:
7028c2ecf20Sopenharmony_ci			write_dc(par, i, par->dc[i]);
7038c2ecf20Sopenharmony_ci		}
7048c2ecf20Sopenharmony_ci	}
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	/* restore the palette */
7078c2ecf20Sopenharmony_ci	write_dc(par, DC_PAL_ADDRESS, 0);
7088c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->dc_pal); i++)
7098c2ecf20Sopenharmony_ci		write_dc(par, DC_PAL_DATA, par->dc_pal[i]);
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	/* restore the horizontal filter coefficients */
7128c2ecf20Sopenharmony_ci	filt = par->dc[DC_IRQ_FILT_CTL] | DC_IRQ_FILT_CTL_H_FILT_SEL;
7138c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->hcoeff); i += 2) {
7148c2ecf20Sopenharmony_ci		write_dc(par, DC_IRQ_FILT_CTL, (filt & 0xffffff00) | i);
7158c2ecf20Sopenharmony_ci		write_dc(par, DC_FILT_COEFF1, par->hcoeff[i]);
7168c2ecf20Sopenharmony_ci		write_dc(par, DC_FILT_COEFF2, par->hcoeff[i + 1]);
7178c2ecf20Sopenharmony_ci	}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	/* restore the vertical filter coefficients */
7208c2ecf20Sopenharmony_ci	filt &= ~DC_IRQ_FILT_CTL_H_FILT_SEL;
7218c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->vcoeff); i++) {
7228c2ecf20Sopenharmony_ci		write_dc(par, DC_IRQ_FILT_CTL, (filt & 0xffffff00) | i);
7238c2ecf20Sopenharmony_ci		write_dc(par, DC_FILT_COEFF1, par->vcoeff[i]);
7248c2ecf20Sopenharmony_ci	}
7258c2ecf20Sopenharmony_ci}
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_cistatic void lx_restore_video_proc(struct lxfb_par *par)
7288c2ecf20Sopenharmony_ci{
7298c2ecf20Sopenharmony_ci	int i;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	wrmsrl(MSR_LX_GLD_MSR_CONFIG, par->msr.dfglcfg);
7328c2ecf20Sopenharmony_ci	wrmsrl(MSR_LX_MSR_PADSEL, par->msr.padsel);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->vp); i++) {
7358c2ecf20Sopenharmony_ci		switch (i) {
7368c2ecf20Sopenharmony_ci		case VP_VCFG:
7378c2ecf20Sopenharmony_ci		case VP_DCFG:
7388c2ecf20Sopenharmony_ci		case VP_PAR:
7398c2ecf20Sopenharmony_ci		case VP_PDR:
7408c2ecf20Sopenharmony_ci		case VP_CCS:
7418c2ecf20Sopenharmony_ci		case VP_RSVD_0:
7428c2ecf20Sopenharmony_ci		/* case VP_VDC: */ /* why should this not be restored? */
7438c2ecf20Sopenharmony_ci		case VP_RSVD_1:
7448c2ecf20Sopenharmony_ci		case VP_CRC32:
7458c2ecf20Sopenharmony_ci			/* don't restore these registers */
7468c2ecf20Sopenharmony_ci			break;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci		default:
7498c2ecf20Sopenharmony_ci			write_vp(par, i, par->vp[i]);
7508c2ecf20Sopenharmony_ci		}
7518c2ecf20Sopenharmony_ci	}
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	/* restore video processor palette */
7548c2ecf20Sopenharmony_ci	write_vp(par, VP_PAR, 0);
7558c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->vp_pal); i++)
7568c2ecf20Sopenharmony_ci		write_vp(par, VP_PDR, par->vp_pal[i]);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	/* restore video coeff ram */
7598c2ecf20Sopenharmony_ci	memcpy(par->vp_regs + VP_VCR, par->vp_coeff, sizeof(par->vp_coeff));
7608c2ecf20Sopenharmony_ci}
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_cistatic void lx_restore_regs(struct lxfb_par *par)
7638c2ecf20Sopenharmony_ci{
7648c2ecf20Sopenharmony_ci	int i;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	lx_set_dotpll((u32) (par->msr.dotpll >> 32));
7678c2ecf20Sopenharmony_ci	lx_restore_gfx_proc(par);
7688c2ecf20Sopenharmony_ci	lx_restore_display_ctlr(par);
7698c2ecf20Sopenharmony_ci	lx_restore_video_proc(par);
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	/* Flat Panel */
7728c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(par->fp); i++) {
7738c2ecf20Sopenharmony_ci		switch (i) {
7748c2ecf20Sopenharmony_ci		case FP_PM:
7758c2ecf20Sopenharmony_ci		case FP_RSVD_0:
7768c2ecf20Sopenharmony_ci		case FP_RSVD_1:
7778c2ecf20Sopenharmony_ci		case FP_RSVD_2:
7788c2ecf20Sopenharmony_ci		case FP_RSVD_3:
7798c2ecf20Sopenharmony_ci		case FP_RSVD_4:
7808c2ecf20Sopenharmony_ci			/* don't restore these registers */
7818c2ecf20Sopenharmony_ci			break;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci		default:
7848c2ecf20Sopenharmony_ci			write_fp(par, i, par->fp[i]);
7858c2ecf20Sopenharmony_ci		}
7868c2ecf20Sopenharmony_ci	}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	/* control the panel */
7898c2ecf20Sopenharmony_ci	if (par->fp[FP_PM] & FP_PM_P) {
7908c2ecf20Sopenharmony_ci		/* power on the panel if not already power{ed,ing} on */
7918c2ecf20Sopenharmony_ci		if (!(read_fp(par, FP_PM) &
7928c2ecf20Sopenharmony_ci				(FP_PM_PANEL_ON|FP_PM_PANEL_PWR_UP)))
7938c2ecf20Sopenharmony_ci			write_fp(par, FP_PM, par->fp[FP_PM]);
7948c2ecf20Sopenharmony_ci	} else {
7958c2ecf20Sopenharmony_ci		/* power down the panel if not already power{ed,ing} down */
7968c2ecf20Sopenharmony_ci		if (!(read_fp(par, FP_PM) &
7978c2ecf20Sopenharmony_ci				(FP_PM_PANEL_OFF|FP_PM_PANEL_PWR_DOWN)))
7988c2ecf20Sopenharmony_ci			write_fp(par, FP_PM, par->fp[FP_PM]);
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	/* turn everything on */
8028c2ecf20Sopenharmony_ci	write_vp(par, VP_VCFG, par->vp[VP_VCFG]);
8038c2ecf20Sopenharmony_ci	write_vp(par, VP_DCFG, par->vp[VP_DCFG]);
8048c2ecf20Sopenharmony_ci	write_dc(par, DC_DISPLAY_CFG, par->dc[DC_DISPLAY_CFG]);
8058c2ecf20Sopenharmony_ci	/* do this last; it will enable the FIFO load */
8068c2ecf20Sopenharmony_ci	write_dc(par, DC_GENERAL_CFG, par->dc[DC_GENERAL_CFG]);
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	/* lock the door behind us */
8098c2ecf20Sopenharmony_ci	write_dc(par, DC_UNLOCK, DC_UNLOCK_LOCK);
8108c2ecf20Sopenharmony_ci}
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ciint lx_powerdown(struct fb_info *info)
8138c2ecf20Sopenharmony_ci{
8148c2ecf20Sopenharmony_ci	struct lxfb_par *par = info->par;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	if (par->powered_down)
8178c2ecf20Sopenharmony_ci		return 0;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	lx_save_regs(par);
8208c2ecf20Sopenharmony_ci	lx_graphics_disable(info);
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	par->powered_down = 1;
8238c2ecf20Sopenharmony_ci	return 0;
8248c2ecf20Sopenharmony_ci}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ciint lx_powerup(struct fb_info *info)
8278c2ecf20Sopenharmony_ci{
8288c2ecf20Sopenharmony_ci	struct lxfb_par *par = info->par;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	if (!par->powered_down)
8318c2ecf20Sopenharmony_ci		return 0;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	lx_restore_regs(par);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	par->powered_down = 0;
8368c2ecf20Sopenharmony_ci	return 0;
8378c2ecf20Sopenharmony_ci}
838