162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2006 Dave Airlie
362306a36Sopenharmony_ci * Copyright 2007 Maarten Maathuis
462306a36Sopenharmony_ci * Copyright 2007-2009 Stuart Bennett
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
762306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
862306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
962306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1062306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
1162306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1462306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1762306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1862306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1962306a36Sopenharmony_ci * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2062306a36Sopenharmony_ci * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
2162306a36Sopenharmony_ci * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2262306a36Sopenharmony_ci * SOFTWARE.
2362306a36Sopenharmony_ci */
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include "nouveau_drv.h"
2662306a36Sopenharmony_ci#include "hw.h"
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include <subdev/bios/pll.h>
2962306a36Sopenharmony_ci#include <nvif/timer.h>
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#define CHIPSET_NFORCE 0x01a0
3262306a36Sopenharmony_ci#define CHIPSET_NFORCE2 0x01f0
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/*
3562306a36Sopenharmony_ci * misc hw access wrappers/control functions
3662306a36Sopenharmony_ci */
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_civoid
3962306a36Sopenharmony_ciNVWriteVgaSeq(struct drm_device *dev, int head, uint8_t index, uint8_t value)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_SRX, index);
4262306a36Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_SR, value);
4362306a36Sopenharmony_ci}
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ciuint8_t
4662306a36Sopenharmony_ciNVReadVgaSeq(struct drm_device *dev, int head, uint8_t index)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_SRX, index);
4962306a36Sopenharmony_ci	return NVReadPRMVIO(dev, head, NV_PRMVIO_SR);
5062306a36Sopenharmony_ci}
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_civoid
5362306a36Sopenharmony_ciNVWriteVgaGr(struct drm_device *dev, int head, uint8_t index, uint8_t value)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_GRX, index);
5662306a36Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_GX, value);
5762306a36Sopenharmony_ci}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciuint8_t
6062306a36Sopenharmony_ciNVReadVgaGr(struct drm_device *dev, int head, uint8_t index)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_GRX, index);
6362306a36Sopenharmony_ci	return NVReadPRMVIO(dev, head, NV_PRMVIO_GX);
6462306a36Sopenharmony_ci}
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/* CR44 takes values 0 (head A), 3 (head B) and 4 (heads tied)
6762306a36Sopenharmony_ci * it affects only the 8 bit vga io regs, which we access using mmio at
6862306a36Sopenharmony_ci * 0xc{0,2}3c*, 0x60{1,3}3*, and 0x68{1,3}3d*
6962306a36Sopenharmony_ci * in general, the set value of cr44 does not matter: reg access works as
7062306a36Sopenharmony_ci * expected and values can be set for the appropriate head by using a 0x2000
7162306a36Sopenharmony_ci * offset as required
7262306a36Sopenharmony_ci * however:
7362306a36Sopenharmony_ci * a) pre nv40, the head B range of PRMVIO regs at 0xc23c* was not exposed and
7462306a36Sopenharmony_ci *    cr44 must be set to 0 or 3 for accessing values on the correct head
7562306a36Sopenharmony_ci *    through the common 0xc03c* addresses
7662306a36Sopenharmony_ci * b) in tied mode (4) head B is programmed to the values set on head A, and
7762306a36Sopenharmony_ci *    access using the head B addresses can have strange results, ergo we leave
7862306a36Sopenharmony_ci *    tied mode in init once we know to what cr44 should be restored on exit
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci * the owner parameter is slightly abused:
8162306a36Sopenharmony_ci * 0 and 1 are treated as head values and so the set value is (owner * 3)
8262306a36Sopenharmony_ci * other values are treated as literal values to set
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_civoid
8562306a36Sopenharmony_ciNVSetOwner(struct drm_device *dev, int owner)
8662306a36Sopenharmony_ci{
8762306a36Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	if (owner == 1)
9062306a36Sopenharmony_ci		owner *= 3;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	if (drm->client.device.info.chipset == 0x11) {
9362306a36Sopenharmony_ci		/* This might seem stupid, but the blob does it and
9462306a36Sopenharmony_ci		 * omitting it often locks the system up.
9562306a36Sopenharmony_ci		 */
9662306a36Sopenharmony_ci		NVReadVgaCrtc(dev, 0, NV_CIO_SR_LOCK_INDEX);
9762306a36Sopenharmony_ci		NVReadVgaCrtc(dev, 1, NV_CIO_SR_LOCK_INDEX);
9862306a36Sopenharmony_ci	}
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	/* CR44 is always changed on CRTC0 */
10162306a36Sopenharmony_ci	NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_44, owner);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	if (drm->client.device.info.chipset == 0x11) {	/* set me harder */
10462306a36Sopenharmony_ci		NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_2E, owner);
10562306a36Sopenharmony_ci		NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_2E, owner);
10662306a36Sopenharmony_ci	}
10762306a36Sopenharmony_ci}
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_civoid
11062306a36Sopenharmony_ciNVBlankScreen(struct drm_device *dev, int head, bool blank)
11162306a36Sopenharmony_ci{
11262306a36Sopenharmony_ci	unsigned char seq1;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	if (nv_two_heads(dev))
11562306a36Sopenharmony_ci		NVSetOwner(dev, head);
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	seq1 = NVReadVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	NVVgaSeqReset(dev, head, true);
12062306a36Sopenharmony_ci	if (blank)
12162306a36Sopenharmony_ci		NVWriteVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX, seq1 | 0x20);
12262306a36Sopenharmony_ci	else
12362306a36Sopenharmony_ci		NVWriteVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX, seq1 & ~0x20);
12462306a36Sopenharmony_ci	NVVgaSeqReset(dev, head, false);
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci/*
12862306a36Sopenharmony_ci * PLL getting
12962306a36Sopenharmony_ci */
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_cistatic void
13262306a36Sopenharmony_cinouveau_hw_decode_pll(struct drm_device *dev, uint32_t reg1, uint32_t pll1,
13362306a36Sopenharmony_ci		      uint32_t pll2, struct nvkm_pll_vals *pllvals)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	/* to force parsing as single stage (i.e. nv40 vplls) pass pll2 as 0 */
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	/* log2P is & 0x7 as never more than 7, and nv30/35 only uses 3 bits */
14062306a36Sopenharmony_ci	pllvals->log2P = (pll1 >> 16) & 0x7;
14162306a36Sopenharmony_ci	pllvals->N2 = pllvals->M2 = 1;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	if (reg1 <= 0x405c) {
14462306a36Sopenharmony_ci		pllvals->NM1 = pll2 & 0xffff;
14562306a36Sopenharmony_ci		/* single stage NVPLL and VPLLs use 1 << 8, MPLL uses 1 << 12 */
14662306a36Sopenharmony_ci		if (!(pll1 & 0x1100))
14762306a36Sopenharmony_ci			pllvals->NM2 = pll2 >> 16;
14862306a36Sopenharmony_ci	} else {
14962306a36Sopenharmony_ci		pllvals->NM1 = pll1 & 0xffff;
15062306a36Sopenharmony_ci		if (nv_two_reg_pll(dev) && pll2 & NV31_RAMDAC_ENABLE_VCO2)
15162306a36Sopenharmony_ci			pllvals->NM2 = pll2 & 0xffff;
15262306a36Sopenharmony_ci		else if (drm->client.device.info.chipset == 0x30 || drm->client.device.info.chipset == 0x35) {
15362306a36Sopenharmony_ci			pllvals->M1 &= 0xf; /* only 4 bits */
15462306a36Sopenharmony_ci			if (pll1 & NV30_RAMDAC_ENABLE_VCO2) {
15562306a36Sopenharmony_ci				pllvals->M2 = (pll1 >> 4) & 0x7;
15662306a36Sopenharmony_ci				pllvals->N2 = ((pll1 >> 21) & 0x18) |
15762306a36Sopenharmony_ci					      ((pll1 >> 19) & 0x7);
15862306a36Sopenharmony_ci			}
15962306a36Sopenharmony_ci		}
16062306a36Sopenharmony_ci	}
16162306a36Sopenharmony_ci}
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ciint
16462306a36Sopenharmony_cinouveau_hw_get_pllvals(struct drm_device *dev, enum nvbios_pll_type plltype,
16562306a36Sopenharmony_ci		       struct nvkm_pll_vals *pllvals)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
16862306a36Sopenharmony_ci	struct nvif_object *device = &drm->client.device.object;
16962306a36Sopenharmony_ci	struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
17062306a36Sopenharmony_ci	uint32_t reg1, pll1, pll2 = 0;
17162306a36Sopenharmony_ci	struct nvbios_pll pll_lim;
17262306a36Sopenharmony_ci	int ret;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	ret = nvbios_pll_parse(bios, plltype, &pll_lim);
17562306a36Sopenharmony_ci	if (ret || !(reg1 = pll_lim.reg))
17662306a36Sopenharmony_ci		return -ENOENT;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	pll1 = nvif_rd32(device, reg1);
17962306a36Sopenharmony_ci	if (reg1 <= 0x405c)
18062306a36Sopenharmony_ci		pll2 = nvif_rd32(device, reg1 + 4);
18162306a36Sopenharmony_ci	else if (nv_two_reg_pll(dev)) {
18262306a36Sopenharmony_ci		uint32_t reg2 = reg1 + (reg1 == NV_RAMDAC_VPLL2 ? 0x5c : 0x70);
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci		pll2 = nvif_rd32(device, reg2);
18562306a36Sopenharmony_ci	}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS && reg1 >= NV_PRAMDAC_VPLL_COEFF) {
18862306a36Sopenharmony_ci		uint32_t ramdac580 = NVReadRAMDAC(dev, 0, NV_PRAMDAC_580);
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci		/* check whether vpll has been forced into single stage mode */
19162306a36Sopenharmony_ci		if (reg1 == NV_PRAMDAC_VPLL_COEFF) {
19262306a36Sopenharmony_ci			if (ramdac580 & NV_RAMDAC_580_VPLL1_ACTIVE)
19362306a36Sopenharmony_ci				pll2 = 0;
19462306a36Sopenharmony_ci		} else
19562306a36Sopenharmony_ci			if (ramdac580 & NV_RAMDAC_580_VPLL2_ACTIVE)
19662306a36Sopenharmony_ci				pll2 = 0;
19762306a36Sopenharmony_ci	}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	nouveau_hw_decode_pll(dev, reg1, pll1, pll2, pllvals);
20062306a36Sopenharmony_ci	pllvals->refclk = pll_lim.refclk;
20162306a36Sopenharmony_ci	return 0;
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ciint
20562306a36Sopenharmony_cinouveau_hw_pllvals_to_clk(struct nvkm_pll_vals *pv)
20662306a36Sopenharmony_ci{
20762306a36Sopenharmony_ci	/* Avoid divide by zero if called at an inappropriate time */
20862306a36Sopenharmony_ci	if (!pv->M1 || !pv->M2)
20962306a36Sopenharmony_ci		return 0;
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	return pv->N1 * pv->N2 * pv->refclk / (pv->M1 * pv->M2) >> pv->log2P;
21262306a36Sopenharmony_ci}
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ciint
21562306a36Sopenharmony_cinouveau_hw_get_clock(struct drm_device *dev, enum nvbios_pll_type plltype)
21662306a36Sopenharmony_ci{
21762306a36Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev->dev);
21862306a36Sopenharmony_ci	struct nvkm_pll_vals pllvals;
21962306a36Sopenharmony_ci	int ret;
22062306a36Sopenharmony_ci	int domain;
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	domain = pci_domain_nr(pdev->bus);
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	if (plltype == PLL_MEMORY &&
22562306a36Sopenharmony_ci	    (pdev->device & 0x0ff0) == CHIPSET_NFORCE) {
22662306a36Sopenharmony_ci		uint32_t mpllP;
22762306a36Sopenharmony_ci		pci_read_config_dword(pci_get_domain_bus_and_slot(domain, 0, 3),
22862306a36Sopenharmony_ci				      0x6c, &mpllP);
22962306a36Sopenharmony_ci		mpllP = (mpllP >> 8) & 0xf;
23062306a36Sopenharmony_ci		if (!mpllP)
23162306a36Sopenharmony_ci			mpllP = 4;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci		return 400000 / mpllP;
23462306a36Sopenharmony_ci	} else
23562306a36Sopenharmony_ci	if (plltype == PLL_MEMORY &&
23662306a36Sopenharmony_ci	    (pdev->device & 0xff0) == CHIPSET_NFORCE2) {
23762306a36Sopenharmony_ci		uint32_t clock;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci		pci_read_config_dword(pci_get_domain_bus_and_slot(domain, 0, 5),
24062306a36Sopenharmony_ci				      0x4c, &clock);
24162306a36Sopenharmony_ci		return clock / 1000;
24262306a36Sopenharmony_ci	}
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	ret = nouveau_hw_get_pllvals(dev, plltype, &pllvals);
24562306a36Sopenharmony_ci	if (ret)
24662306a36Sopenharmony_ci		return ret;
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	return nouveau_hw_pllvals_to_clk(&pllvals);
24962306a36Sopenharmony_ci}
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_cistatic void
25262306a36Sopenharmony_cinouveau_hw_fix_bad_vpll(struct drm_device *dev, int head)
25362306a36Sopenharmony_ci{
25462306a36Sopenharmony_ci	/* the vpll on an unused head can come up with a random value, way
25562306a36Sopenharmony_ci	 * beyond the pll limits.  for some reason this causes the chip to
25662306a36Sopenharmony_ci	 * lock up when reading the dac palette regs, so set a valid pll here
25762306a36Sopenharmony_ci	 * when such a condition detected.  only seen on nv11 to date
25862306a36Sopenharmony_ci	 */
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
26162306a36Sopenharmony_ci	struct nvif_device *device = &drm->client.device;
26262306a36Sopenharmony_ci	struct nvkm_clk *clk = nvxx_clk(device);
26362306a36Sopenharmony_ci	struct nvkm_bios *bios = nvxx_bios(device);
26462306a36Sopenharmony_ci	struct nvbios_pll pll_lim;
26562306a36Sopenharmony_ci	struct nvkm_pll_vals pv;
26662306a36Sopenharmony_ci	enum nvbios_pll_type pll = head ? PLL_VPLL1 : PLL_VPLL0;
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	if (nvbios_pll_parse(bios, pll, &pll_lim))
26962306a36Sopenharmony_ci		return;
27062306a36Sopenharmony_ci	nouveau_hw_get_pllvals(dev, pll, &pv);
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	if (pv.M1 >= pll_lim.vco1.min_m && pv.M1 <= pll_lim.vco1.max_m &&
27362306a36Sopenharmony_ci	    pv.N1 >= pll_lim.vco1.min_n && pv.N1 <= pll_lim.vco1.max_n &&
27462306a36Sopenharmony_ci	    pv.log2P <= pll_lim.max_p)
27562306a36Sopenharmony_ci		return;
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci	NV_WARN(drm, "VPLL %d outwith limits, attempting to fix\n", head + 1);
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci	/* set lowest clock within static limits */
28062306a36Sopenharmony_ci	pv.M1 = pll_lim.vco1.max_m;
28162306a36Sopenharmony_ci	pv.N1 = pll_lim.vco1.min_n;
28262306a36Sopenharmony_ci	pv.log2P = pll_lim.max_p_usable;
28362306a36Sopenharmony_ci	clk->pll_prog(clk, pll_lim.reg, &pv);
28462306a36Sopenharmony_ci}
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci/*
28762306a36Sopenharmony_ci * vga font save/restore
28862306a36Sopenharmony_ci */
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_cistatic void nouveau_vga_font_io(struct drm_device *dev,
29162306a36Sopenharmony_ci				void __iomem *iovram,
29262306a36Sopenharmony_ci				bool save, unsigned plane)
29362306a36Sopenharmony_ci{
29462306a36Sopenharmony_ci	unsigned i;
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	NVWriteVgaSeq(dev, 0, NV_VIO_SR_PLANE_MASK_INDEX, 1 << plane);
29762306a36Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_READ_MAP_INDEX, plane);
29862306a36Sopenharmony_ci	for (i = 0; i < 16384; i++) {
29962306a36Sopenharmony_ci		if (save) {
30062306a36Sopenharmony_ci			nv04_display(dev)->saved_vga_font[plane][i] =
30162306a36Sopenharmony_ci					ioread32_native(iovram + i * 4);
30262306a36Sopenharmony_ci		} else {
30362306a36Sopenharmony_ci			iowrite32_native(nv04_display(dev)->saved_vga_font[plane][i],
30462306a36Sopenharmony_ci							iovram + i * 4);
30562306a36Sopenharmony_ci		}
30662306a36Sopenharmony_ci	}
30762306a36Sopenharmony_ci}
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_civoid
31062306a36Sopenharmony_cinouveau_hw_save_vga_fonts(struct drm_device *dev, bool save)
31162306a36Sopenharmony_ci{
31262306a36Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
31362306a36Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev->dev);
31462306a36Sopenharmony_ci	uint8_t misc, gr4, gr5, gr6, seq2, seq4;
31562306a36Sopenharmony_ci	bool graphicsmode;
31662306a36Sopenharmony_ci	unsigned plane;
31762306a36Sopenharmony_ci	void __iomem *iovram;
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	if (nv_two_heads(dev))
32062306a36Sopenharmony_ci		NVSetOwner(dev, 0);
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	NVSetEnablePalette(dev, 0, true);
32362306a36Sopenharmony_ci	graphicsmode = NVReadVgaAttr(dev, 0, NV_CIO_AR_MODE_INDEX) & 1;
32462306a36Sopenharmony_ci	NVSetEnablePalette(dev, 0, false);
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	if (graphicsmode) /* graphics mode => framebuffer => no need to save */
32762306a36Sopenharmony_ci		return;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	NV_INFO(drm, "%sing VGA fonts\n", save ? "Sav" : "Restor");
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci	/* map first 64KiB of VRAM, holds VGA fonts etc */
33262306a36Sopenharmony_ci	iovram = ioremap(pci_resource_start(pdev, 1), 65536);
33362306a36Sopenharmony_ci	if (!iovram) {
33462306a36Sopenharmony_ci		NV_ERROR(drm, "Failed to map VRAM, "
33562306a36Sopenharmony_ci					"cannot save/restore VGA fonts.\n");
33662306a36Sopenharmony_ci		return;
33762306a36Sopenharmony_ci	}
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci	if (nv_two_heads(dev))
34062306a36Sopenharmony_ci		NVBlankScreen(dev, 1, true);
34162306a36Sopenharmony_ci	NVBlankScreen(dev, 0, true);
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	/* save control regs */
34462306a36Sopenharmony_ci	misc = NVReadPRMVIO(dev, 0, NV_PRMVIO_MISC__READ);
34562306a36Sopenharmony_ci	seq2 = NVReadVgaSeq(dev, 0, NV_VIO_SR_PLANE_MASK_INDEX);
34662306a36Sopenharmony_ci	seq4 = NVReadVgaSeq(dev, 0, NV_VIO_SR_MEM_MODE_INDEX);
34762306a36Sopenharmony_ci	gr4 = NVReadVgaGr(dev, 0, NV_VIO_GX_READ_MAP_INDEX);
34862306a36Sopenharmony_ci	gr5 = NVReadVgaGr(dev, 0, NV_VIO_GX_MODE_INDEX);
34962306a36Sopenharmony_ci	gr6 = NVReadVgaGr(dev, 0, NV_VIO_GX_MISC_INDEX);
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci	NVWritePRMVIO(dev, 0, NV_PRMVIO_MISC__WRITE, 0x67);
35262306a36Sopenharmony_ci	NVWriteVgaSeq(dev, 0, NV_VIO_SR_MEM_MODE_INDEX, 0x6);
35362306a36Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_MODE_INDEX, 0x0);
35462306a36Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_MISC_INDEX, 0x5);
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	/* store font in planes 0..3 */
35762306a36Sopenharmony_ci	for (plane = 0; plane < 4; plane++)
35862306a36Sopenharmony_ci		nouveau_vga_font_io(dev, iovram, save, plane);
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci	/* restore control regs */
36162306a36Sopenharmony_ci	NVWritePRMVIO(dev, 0, NV_PRMVIO_MISC__WRITE, misc);
36262306a36Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_READ_MAP_INDEX, gr4);
36362306a36Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_MODE_INDEX, gr5);
36462306a36Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_MISC_INDEX, gr6);
36562306a36Sopenharmony_ci	NVWriteVgaSeq(dev, 0, NV_VIO_SR_PLANE_MASK_INDEX, seq2);
36662306a36Sopenharmony_ci	NVWriteVgaSeq(dev, 0, NV_VIO_SR_MEM_MODE_INDEX, seq4);
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci	if (nv_two_heads(dev))
36962306a36Sopenharmony_ci		NVBlankScreen(dev, 1, false);
37062306a36Sopenharmony_ci	NVBlankScreen(dev, 0, false);
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	iounmap(iovram);
37362306a36Sopenharmony_ci}
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci/*
37662306a36Sopenharmony_ci * mode state save/load
37762306a36Sopenharmony_ci */
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_cistatic void
38062306a36Sopenharmony_cird_cio_state(struct drm_device *dev, int head,
38162306a36Sopenharmony_ci	     struct nv04_crtc_reg *crtcstate, int index)
38262306a36Sopenharmony_ci{
38362306a36Sopenharmony_ci	crtcstate->CRTC[index] = NVReadVgaCrtc(dev, head, index);
38462306a36Sopenharmony_ci}
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_cistatic void
38762306a36Sopenharmony_ciwr_cio_state(struct drm_device *dev, int head,
38862306a36Sopenharmony_ci	     struct nv04_crtc_reg *crtcstate, int index)
38962306a36Sopenharmony_ci{
39062306a36Sopenharmony_ci	NVWriteVgaCrtc(dev, head, index, crtcstate->CRTC[index]);
39162306a36Sopenharmony_ci}
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_cistatic void
39462306a36Sopenharmony_cinv_save_state_ramdac(struct drm_device *dev, int head,
39562306a36Sopenharmony_ci		     struct nv04_mode_state *state)
39662306a36Sopenharmony_ci{
39762306a36Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
39862306a36Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
39962306a36Sopenharmony_ci	int i;
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS)
40262306a36Sopenharmony_ci		regp->nv10_cursync = NVReadRAMDAC(dev, head, NV_RAMDAC_NV10_CURSYNC);
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci	nouveau_hw_get_pllvals(dev, head ? PLL_VPLL1 : PLL_VPLL0, &regp->pllvals);
40562306a36Sopenharmony_ci	state->pllsel = NVReadRAMDAC(dev, 0, NV_PRAMDAC_PLL_COEFF_SELECT);
40662306a36Sopenharmony_ci	if (nv_two_heads(dev))
40762306a36Sopenharmony_ci		state->sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK);
40862306a36Sopenharmony_ci	if (drm->client.device.info.chipset == 0x11)
40962306a36Sopenharmony_ci		regp->dither = NVReadRAMDAC(dev, head, NV_RAMDAC_DITHER_NV11);
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	regp->ramdac_gen_ctrl = NVReadRAMDAC(dev, head, NV_PRAMDAC_GENERAL_CONTROL);
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci	if (nv_gf4_disp_arch(dev))
41462306a36Sopenharmony_ci		regp->ramdac_630 = NVReadRAMDAC(dev, head, NV_PRAMDAC_630);
41562306a36Sopenharmony_ci	if (drm->client.device.info.chipset >= 0x30)
41662306a36Sopenharmony_ci		regp->ramdac_634 = NVReadRAMDAC(dev, head, NV_PRAMDAC_634);
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci	regp->tv_setup = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_SETUP);
41962306a36Sopenharmony_ci	regp->tv_vtotal = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_VTOTAL);
42062306a36Sopenharmony_ci	regp->tv_vskew = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_VSKEW);
42162306a36Sopenharmony_ci	regp->tv_vsync_delay = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_VSYNC_DELAY);
42262306a36Sopenharmony_ci	regp->tv_htotal = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_HTOTAL);
42362306a36Sopenharmony_ci	regp->tv_hskew = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_HSKEW);
42462306a36Sopenharmony_ci	regp->tv_hsync_delay = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_HSYNC_DELAY);
42562306a36Sopenharmony_ci	regp->tv_hsync_delay2 = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_HSYNC_DELAY2);
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci	for (i = 0; i < 7; i++) {
42862306a36Sopenharmony_ci		uint32_t ramdac_reg = NV_PRAMDAC_FP_VDISPLAY_END + (i * 4);
42962306a36Sopenharmony_ci		regp->fp_vert_regs[i] = NVReadRAMDAC(dev, head, ramdac_reg);
43062306a36Sopenharmony_ci		regp->fp_horiz_regs[i] = NVReadRAMDAC(dev, head, ramdac_reg + 0x20);
43162306a36Sopenharmony_ci	}
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	if (nv_gf4_disp_arch(dev)) {
43462306a36Sopenharmony_ci		regp->dither = NVReadRAMDAC(dev, head, NV_RAMDAC_FP_DITHER);
43562306a36Sopenharmony_ci		for (i = 0; i < 3; i++) {
43662306a36Sopenharmony_ci			regp->dither_regs[i] = NVReadRAMDAC(dev, head, NV_PRAMDAC_850 + i * 4);
43762306a36Sopenharmony_ci			regp->dither_regs[i + 3] = NVReadRAMDAC(dev, head, NV_PRAMDAC_85C + i * 4);
43862306a36Sopenharmony_ci		}
43962306a36Sopenharmony_ci	}
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci	regp->fp_control = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL);
44262306a36Sopenharmony_ci	regp->fp_debug_0 = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_0);
44362306a36Sopenharmony_ci	if (!nv_gf4_disp_arch(dev) && head == 0) {
44462306a36Sopenharmony_ci		/* early chips don't allow access to PRAMDAC_TMDS_* without
44562306a36Sopenharmony_ci		 * the head A FPCLK on (nv11 even locks up) */
44662306a36Sopenharmony_ci		NVWriteRAMDAC(dev, 0, NV_PRAMDAC_FP_DEBUG_0, regp->fp_debug_0 &
44762306a36Sopenharmony_ci			      ~NV_PRAMDAC_FP_DEBUG_0_PWRDOWN_FPCLK);
44862306a36Sopenharmony_ci	}
44962306a36Sopenharmony_ci	regp->fp_debug_1 = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_1);
45062306a36Sopenharmony_ci	regp->fp_debug_2 = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_2);
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci	regp->fp_margin_color = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_MARGIN_COLOR);
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci	if (nv_gf4_disp_arch(dev))
45562306a36Sopenharmony_ci		regp->ramdac_8c0 = NVReadRAMDAC(dev, head, NV_PRAMDAC_8C0);
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE) {
45862306a36Sopenharmony_ci		regp->ramdac_a20 = NVReadRAMDAC(dev, head, NV_PRAMDAC_A20);
45962306a36Sopenharmony_ci		regp->ramdac_a24 = NVReadRAMDAC(dev, head, NV_PRAMDAC_A24);
46062306a36Sopenharmony_ci		regp->ramdac_a34 = NVReadRAMDAC(dev, head, NV_PRAMDAC_A34);
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci		for (i = 0; i < 38; i++)
46362306a36Sopenharmony_ci			regp->ctv_regs[i] = NVReadRAMDAC(dev, head,
46462306a36Sopenharmony_ci							 NV_PRAMDAC_CTV + 4*i);
46562306a36Sopenharmony_ci	}
46662306a36Sopenharmony_ci}
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_cistatic void
46962306a36Sopenharmony_cinv_load_state_ramdac(struct drm_device *dev, int head,
47062306a36Sopenharmony_ci		     struct nv04_mode_state *state)
47162306a36Sopenharmony_ci{
47262306a36Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
47362306a36Sopenharmony_ci	struct nvkm_clk *clk = nvxx_clk(&drm->client.device);
47462306a36Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
47562306a36Sopenharmony_ci	uint32_t pllreg = head ? NV_RAMDAC_VPLL2 : NV_PRAMDAC_VPLL_COEFF;
47662306a36Sopenharmony_ci	int i;
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS)
47962306a36Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_RAMDAC_NV10_CURSYNC, regp->nv10_cursync);
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci	clk->pll_prog(clk, pllreg, &regp->pllvals);
48262306a36Sopenharmony_ci	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_PLL_COEFF_SELECT, state->pllsel);
48362306a36Sopenharmony_ci	if (nv_two_heads(dev))
48462306a36Sopenharmony_ci		NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, state->sel_clk);
48562306a36Sopenharmony_ci	if (drm->client.device.info.chipset == 0x11)
48662306a36Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_RAMDAC_DITHER_NV11, regp->dither);
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_GENERAL_CONTROL, regp->ramdac_gen_ctrl);
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci	if (nv_gf4_disp_arch(dev))
49162306a36Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_630, regp->ramdac_630);
49262306a36Sopenharmony_ci	if (drm->client.device.info.chipset >= 0x30)
49362306a36Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_634, regp->ramdac_634);
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_SETUP, regp->tv_setup);
49662306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_VTOTAL, regp->tv_vtotal);
49762306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_VSKEW, regp->tv_vskew);
49862306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_VSYNC_DELAY, regp->tv_vsync_delay);
49962306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_HTOTAL, regp->tv_htotal);
50062306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_HSKEW, regp->tv_hskew);
50162306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_HSYNC_DELAY, regp->tv_hsync_delay);
50262306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_HSYNC_DELAY2, regp->tv_hsync_delay2);
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci	for (i = 0; i < 7; i++) {
50562306a36Sopenharmony_ci		uint32_t ramdac_reg = NV_PRAMDAC_FP_VDISPLAY_END + (i * 4);
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci		NVWriteRAMDAC(dev, head, ramdac_reg, regp->fp_vert_regs[i]);
50862306a36Sopenharmony_ci		NVWriteRAMDAC(dev, head, ramdac_reg + 0x20, regp->fp_horiz_regs[i]);
50962306a36Sopenharmony_ci	}
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci	if (nv_gf4_disp_arch(dev)) {
51262306a36Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_RAMDAC_FP_DITHER, regp->dither);
51362306a36Sopenharmony_ci		for (i = 0; i < 3; i++) {
51462306a36Sopenharmony_ci			NVWriteRAMDAC(dev, head, NV_PRAMDAC_850 + i * 4, regp->dither_regs[i]);
51562306a36Sopenharmony_ci			NVWriteRAMDAC(dev, head, NV_PRAMDAC_85C + i * 4, regp->dither_regs[i + 3]);
51662306a36Sopenharmony_ci		}
51762306a36Sopenharmony_ci	}
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL, regp->fp_control);
52062306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_0, regp->fp_debug_0);
52162306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_1, regp->fp_debug_1);
52262306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_2, regp->fp_debug_2);
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_MARGIN_COLOR, regp->fp_margin_color);
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci	if (nv_gf4_disp_arch(dev))
52762306a36Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_8C0, regp->ramdac_8c0);
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE) {
53062306a36Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_A20, regp->ramdac_a20);
53162306a36Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_A24, regp->ramdac_a24);
53262306a36Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_A34, regp->ramdac_a34);
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci		for (i = 0; i < 38; i++)
53562306a36Sopenharmony_ci			NVWriteRAMDAC(dev, head,
53662306a36Sopenharmony_ci				      NV_PRAMDAC_CTV + 4*i, regp->ctv_regs[i]);
53762306a36Sopenharmony_ci	}
53862306a36Sopenharmony_ci}
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_cistatic void
54162306a36Sopenharmony_cinv_save_state_vga(struct drm_device *dev, int head,
54262306a36Sopenharmony_ci		  struct nv04_mode_state *state)
54362306a36Sopenharmony_ci{
54462306a36Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
54562306a36Sopenharmony_ci	int i;
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci	regp->MiscOutReg = NVReadPRMVIO(dev, head, NV_PRMVIO_MISC__READ);
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci	for (i = 0; i < 25; i++)
55062306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, i);
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_ci	NVSetEnablePalette(dev, head, true);
55362306a36Sopenharmony_ci	for (i = 0; i < 21; i++)
55462306a36Sopenharmony_ci		regp->Attribute[i] = NVReadVgaAttr(dev, head, i);
55562306a36Sopenharmony_ci	NVSetEnablePalette(dev, head, false);
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ci	for (i = 0; i < 9; i++)
55862306a36Sopenharmony_ci		regp->Graphics[i] = NVReadVgaGr(dev, head, i);
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci	for (i = 0; i < 5; i++)
56162306a36Sopenharmony_ci		regp->Sequencer[i] = NVReadVgaSeq(dev, head, i);
56262306a36Sopenharmony_ci}
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_cistatic void
56562306a36Sopenharmony_cinv_load_state_vga(struct drm_device *dev, int head,
56662306a36Sopenharmony_ci		  struct nv04_mode_state *state)
56762306a36Sopenharmony_ci{
56862306a36Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
56962306a36Sopenharmony_ci	int i;
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_MISC__WRITE, regp->MiscOutReg);
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci	for (i = 0; i < 5; i++)
57462306a36Sopenharmony_ci		NVWriteVgaSeq(dev, head, i, regp->Sequencer[i]);
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci	nv_lock_vga_crtc_base(dev, head, false);
57762306a36Sopenharmony_ci	for (i = 0; i < 25; i++)
57862306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, i);
57962306a36Sopenharmony_ci	nv_lock_vga_crtc_base(dev, head, true);
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci	for (i = 0; i < 9; i++)
58262306a36Sopenharmony_ci		NVWriteVgaGr(dev, head, i, regp->Graphics[i]);
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_ci	NVSetEnablePalette(dev, head, true);
58562306a36Sopenharmony_ci	for (i = 0; i < 21; i++)
58662306a36Sopenharmony_ci		NVWriteVgaAttr(dev, head, i, regp->Attribute[i]);
58762306a36Sopenharmony_ci	NVSetEnablePalette(dev, head, false);
58862306a36Sopenharmony_ci}
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_cistatic void
59162306a36Sopenharmony_cinv_save_state_ext(struct drm_device *dev, int head,
59262306a36Sopenharmony_ci		  struct nv04_mode_state *state)
59362306a36Sopenharmony_ci{
59462306a36Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
59562306a36Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
59662306a36Sopenharmony_ci	int i;
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_LCD__INDEX);
59962306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_RPC0_INDEX);
60062306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_RPC1_INDEX);
60162306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_LSR_INDEX);
60262306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_PIXEL_INDEX);
60362306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_HEB__INDEX);
60462306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_ENH_INDEX);
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_FF_INDEX);
60762306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_FFLWM__INDEX);
60862306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_21);
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_KELVIN)
61162306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_47);
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_RANKINE)
61462306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, 0x9f);
61562306a36Sopenharmony_ci
61662306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_49);
61762306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR0_INDEX);
61862306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR1_INDEX);
61962306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR2_INDEX);
62062306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_ILACE__INDEX);
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
62362306a36Sopenharmony_ci		regp->crtc_830 = NVReadCRTC(dev, head, NV_PCRTC_830);
62462306a36Sopenharmony_ci		regp->crtc_834 = NVReadCRTC(dev, head, NV_PCRTC_834);
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_ci		if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_RANKINE)
62762306a36Sopenharmony_ci			regp->gpio_ext = NVReadCRTC(dev, head, NV_PCRTC_GPIO_EXT);
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ci		if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE)
63062306a36Sopenharmony_ci			regp->crtc_850 = NVReadCRTC(dev, head, NV_PCRTC_850);
63162306a36Sopenharmony_ci
63262306a36Sopenharmony_ci		if (nv_two_heads(dev))
63362306a36Sopenharmony_ci			regp->crtc_eng_ctrl = NVReadCRTC(dev, head, NV_PCRTC_ENGINE_CTRL);
63462306a36Sopenharmony_ci		regp->cursor_cfg = NVReadCRTC(dev, head, NV_PCRTC_CURSOR_CONFIG);
63562306a36Sopenharmony_ci	}
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ci	regp->crtc_cfg = NVReadCRTC(dev, head, NV_PCRTC_CONFIG);
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_SCRATCH3__INDEX);
64062306a36Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_SCRATCH4__INDEX);
64162306a36Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
64262306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_EBR_INDEX);
64362306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_CSB);
64462306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_4B);
64562306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_TVOUT_LATENCY);
64662306a36Sopenharmony_ci	}
64762306a36Sopenharmony_ci	/* NV11 and NV20 don't have this, they stop at 0x52. */
64862306a36Sopenharmony_ci	if (nv_gf4_disp_arch(dev)) {
64962306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_42);
65062306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_53);
65162306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_54);
65262306a36Sopenharmony_ci
65362306a36Sopenharmony_ci		for (i = 0; i < 0x10; i++)
65462306a36Sopenharmony_ci			regp->CR58[i] = NVReadVgaCrtc5758(dev, head, i);
65562306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_59);
65662306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_5B);
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_85);
65962306a36Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_86);
66062306a36Sopenharmony_ci	}
66162306a36Sopenharmony_ci
66262306a36Sopenharmony_ci	regp->fb_start = NVReadCRTC(dev, head, NV_PCRTC_START);
66362306a36Sopenharmony_ci}
66462306a36Sopenharmony_ci
66562306a36Sopenharmony_cistatic void
66662306a36Sopenharmony_cinv_load_state_ext(struct drm_device *dev, int head,
66762306a36Sopenharmony_ci		  struct nv04_mode_state *state)
66862306a36Sopenharmony_ci{
66962306a36Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
67062306a36Sopenharmony_ci	struct nvif_object *device = &drm->client.device.object;
67162306a36Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
67262306a36Sopenharmony_ci	uint32_t reg900;
67362306a36Sopenharmony_ci	int i;
67462306a36Sopenharmony_ci
67562306a36Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
67662306a36Sopenharmony_ci		if (nv_two_heads(dev))
67762306a36Sopenharmony_ci			/* setting ENGINE_CTRL (EC) *must* come before
67862306a36Sopenharmony_ci			 * CIO_CRE_LCD, as writing CRE_LCD sets bits 16 & 17 in
67962306a36Sopenharmony_ci			 * EC that should not be overwritten by writing stale EC
68062306a36Sopenharmony_ci			 */
68162306a36Sopenharmony_ci			NVWriteCRTC(dev, head, NV_PCRTC_ENGINE_CTRL, regp->crtc_eng_ctrl);
68262306a36Sopenharmony_ci
68362306a36Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_STOP, 1);
68462306a36Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_INTR_EN, 0);
68562306a36Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_OFFSET_BUFF(0), 0);
68662306a36Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_OFFSET_BUFF(1), 0);
68762306a36Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_LIMIT(0), drm->client.device.info.ram_size - 1);
68862306a36Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_LIMIT(1), drm->client.device.info.ram_size - 1);
68962306a36Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_UVPLANE_LIMIT(0), drm->client.device.info.ram_size - 1);
69062306a36Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_UVPLANE_LIMIT(1), drm->client.device.info.ram_size - 1);
69162306a36Sopenharmony_ci		nvif_wr32(device, NV_PBUS_POWERCTRL_2, 0);
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_ci		NVWriteCRTC(dev, head, NV_PCRTC_CURSOR_CONFIG, regp->cursor_cfg);
69462306a36Sopenharmony_ci		NVWriteCRTC(dev, head, NV_PCRTC_830, regp->crtc_830);
69562306a36Sopenharmony_ci		NVWriteCRTC(dev, head, NV_PCRTC_834, regp->crtc_834);
69662306a36Sopenharmony_ci
69762306a36Sopenharmony_ci		if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_RANKINE)
69862306a36Sopenharmony_ci			NVWriteCRTC(dev, head, NV_PCRTC_GPIO_EXT, regp->gpio_ext);
69962306a36Sopenharmony_ci
70062306a36Sopenharmony_ci		if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE) {
70162306a36Sopenharmony_ci			NVWriteCRTC(dev, head, NV_PCRTC_850, regp->crtc_850);
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ci			reg900 = NVReadRAMDAC(dev, head, NV_PRAMDAC_900);
70462306a36Sopenharmony_ci			if (regp->crtc_cfg == NV10_PCRTC_CONFIG_START_ADDRESS_HSYNC)
70562306a36Sopenharmony_ci				NVWriteRAMDAC(dev, head, NV_PRAMDAC_900, reg900 | 0x10000);
70662306a36Sopenharmony_ci			else
70762306a36Sopenharmony_ci				NVWriteRAMDAC(dev, head, NV_PRAMDAC_900, reg900 & ~0x10000);
70862306a36Sopenharmony_ci		}
70962306a36Sopenharmony_ci	}
71062306a36Sopenharmony_ci
71162306a36Sopenharmony_ci	NVWriteCRTC(dev, head, NV_PCRTC_CONFIG, regp->crtc_cfg);
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_RPC0_INDEX);
71462306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_RPC1_INDEX);
71562306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_LSR_INDEX);
71662306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_PIXEL_INDEX);
71762306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_LCD__INDEX);
71862306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_HEB__INDEX);
71962306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_ENH_INDEX);
72062306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_FF_INDEX);
72162306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_FFLWM__INDEX);
72262306a36Sopenharmony_ci
72362306a36Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_KELVIN)
72462306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_47);
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_RANKINE)
72762306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, 0x9f);
72862306a36Sopenharmony_ci
72962306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_49);
73062306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR0_INDEX);
73162306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR1_INDEX);
73262306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR2_INDEX);
73362306a36Sopenharmony_ci	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE)
73462306a36Sopenharmony_ci		nv_fix_nv40_hw_cursor(dev, head);
73562306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_ILACE__INDEX);
73662306a36Sopenharmony_ci
73762306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_SCRATCH3__INDEX);
73862306a36Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_SCRATCH4__INDEX);
73962306a36Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
74062306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_EBR_INDEX);
74162306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_CSB);
74262306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_4B);
74362306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_TVOUT_LATENCY);
74462306a36Sopenharmony_ci	}
74562306a36Sopenharmony_ci	/* NV11 and NV20 stop at 0x52. */
74662306a36Sopenharmony_ci	if (nv_gf4_disp_arch(dev)) {
74762306a36Sopenharmony_ci		if (drm->client.device.info.family < NV_DEVICE_INFO_V0_KELVIN) {
74862306a36Sopenharmony_ci			/* Not waiting for vertical retrace before modifying
74962306a36Sopenharmony_ci			   CRE_53/CRE_54 causes lockups. */
75062306a36Sopenharmony_ci			nvif_msec(&drm->client.device, 650,
75162306a36Sopenharmony_ci				if ( (nvif_rd32(device, NV_PRMCIO_INP0__COLOR) & 8))
75262306a36Sopenharmony_ci					break;
75362306a36Sopenharmony_ci			);
75462306a36Sopenharmony_ci			nvif_msec(&drm->client.device, 650,
75562306a36Sopenharmony_ci				if (!(nvif_rd32(device, NV_PRMCIO_INP0__COLOR) & 8))
75662306a36Sopenharmony_ci					break;
75762306a36Sopenharmony_ci			);
75862306a36Sopenharmony_ci		}
75962306a36Sopenharmony_ci
76062306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_42);
76162306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_53);
76262306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_54);
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci		for (i = 0; i < 0x10; i++)
76562306a36Sopenharmony_ci			NVWriteVgaCrtc5758(dev, head, i, regp->CR58[i]);
76662306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_59);
76762306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_5B);
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_85);
77062306a36Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_86);
77162306a36Sopenharmony_ci	}
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci	NVWriteCRTC(dev, head, NV_PCRTC_START, regp->fb_start);
77462306a36Sopenharmony_ci}
77562306a36Sopenharmony_ci
77662306a36Sopenharmony_cistatic void
77762306a36Sopenharmony_cinv_save_state_palette(struct drm_device *dev, int head,
77862306a36Sopenharmony_ci		      struct nv04_mode_state *state)
77962306a36Sopenharmony_ci{
78062306a36Sopenharmony_ci	struct nvif_object *device = &nouveau_drm(dev)->client.device.object;
78162306a36Sopenharmony_ci	int head_offset = head * NV_PRMDIO_SIZE, i;
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci	nvif_wr08(device, NV_PRMDIO_PIXEL_MASK + head_offset,
78462306a36Sopenharmony_ci				NV_PRMDIO_PIXEL_MASK_MASK);
78562306a36Sopenharmony_ci	nvif_wr08(device, NV_PRMDIO_READ_MODE_ADDRESS + head_offset, 0x0);
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci	for (i = 0; i < 768; i++) {
78862306a36Sopenharmony_ci		state->crtc_reg[head].DAC[i] = nvif_rd08(device,
78962306a36Sopenharmony_ci				NV_PRMDIO_PALETTE_DATA + head_offset);
79062306a36Sopenharmony_ci	}
79162306a36Sopenharmony_ci
79262306a36Sopenharmony_ci	NVSetEnablePalette(dev, head, false);
79362306a36Sopenharmony_ci}
79462306a36Sopenharmony_ci
79562306a36Sopenharmony_civoid
79662306a36Sopenharmony_cinouveau_hw_load_state_palette(struct drm_device *dev, int head,
79762306a36Sopenharmony_ci			      struct nv04_mode_state *state)
79862306a36Sopenharmony_ci{
79962306a36Sopenharmony_ci	struct nvif_object *device = &nouveau_drm(dev)->client.device.object;
80062306a36Sopenharmony_ci	int head_offset = head * NV_PRMDIO_SIZE, i;
80162306a36Sopenharmony_ci
80262306a36Sopenharmony_ci	nvif_wr08(device, NV_PRMDIO_PIXEL_MASK + head_offset,
80362306a36Sopenharmony_ci				NV_PRMDIO_PIXEL_MASK_MASK);
80462306a36Sopenharmony_ci	nvif_wr08(device, NV_PRMDIO_WRITE_MODE_ADDRESS + head_offset, 0x0);
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_ci	for (i = 0; i < 768; i++) {
80762306a36Sopenharmony_ci		nvif_wr08(device, NV_PRMDIO_PALETTE_DATA + head_offset,
80862306a36Sopenharmony_ci				state->crtc_reg[head].DAC[i]);
80962306a36Sopenharmony_ci	}
81062306a36Sopenharmony_ci
81162306a36Sopenharmony_ci	NVSetEnablePalette(dev, head, false);
81262306a36Sopenharmony_ci}
81362306a36Sopenharmony_ci
81462306a36Sopenharmony_civoid nouveau_hw_save_state(struct drm_device *dev, int head,
81562306a36Sopenharmony_ci			   struct nv04_mode_state *state)
81662306a36Sopenharmony_ci{
81762306a36Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
81862306a36Sopenharmony_ci
81962306a36Sopenharmony_ci	if (drm->client.device.info.chipset == 0x11)
82062306a36Sopenharmony_ci		/* NB: no attempt is made to restore the bad pll later on */
82162306a36Sopenharmony_ci		nouveau_hw_fix_bad_vpll(dev, head);
82262306a36Sopenharmony_ci	nv_save_state_ramdac(dev, head, state);
82362306a36Sopenharmony_ci	nv_save_state_vga(dev, head, state);
82462306a36Sopenharmony_ci	nv_save_state_palette(dev, head, state);
82562306a36Sopenharmony_ci	nv_save_state_ext(dev, head, state);
82662306a36Sopenharmony_ci}
82762306a36Sopenharmony_ci
82862306a36Sopenharmony_civoid nouveau_hw_load_state(struct drm_device *dev, int head,
82962306a36Sopenharmony_ci			   struct nv04_mode_state *state)
83062306a36Sopenharmony_ci{
83162306a36Sopenharmony_ci	NVVgaProtect(dev, head, true);
83262306a36Sopenharmony_ci	nv_load_state_ramdac(dev, head, state);
83362306a36Sopenharmony_ci	nv_load_state_ext(dev, head, state);
83462306a36Sopenharmony_ci	nouveau_hw_load_state_palette(dev, head, state);
83562306a36Sopenharmony_ci	nv_load_state_vga(dev, head, state);
83662306a36Sopenharmony_ci	NVVgaProtect(dev, head, false);
83762306a36Sopenharmony_ci}
838