1c39f472eSBen Skeggs /*
2c39f472eSBen Skeggs  * Copyright (C) 2010 Francisco Jerez.
3c39f472eSBen Skeggs  * All Rights Reserved.
4c39f472eSBen Skeggs  *
5c39f472eSBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining
6c39f472eSBen Skeggs  * a copy of this software and associated documentation files (the
7c39f472eSBen Skeggs  * "Software"), to deal in the Software without restriction, including
8c39f472eSBen Skeggs  * without limitation the rights to use, copy, modify, merge, publish,
9c39f472eSBen Skeggs  * distribute, sublicense, and/or sell copies of the Software, and to
10c39f472eSBen Skeggs  * permit persons to whom the Software is furnished to do so, subject to
11c39f472eSBen Skeggs  * the following conditions:
12c39f472eSBen Skeggs  *
13c39f472eSBen Skeggs  * The above copyright notice and this permission notice (including the
14c39f472eSBen Skeggs  * next paragraph) shall be included in all copies or substantial
15c39f472eSBen Skeggs  * portions of the Software.
16c39f472eSBen Skeggs  *
17c39f472eSBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18c39f472eSBen Skeggs  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19c39f472eSBen Skeggs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20c39f472eSBen Skeggs  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21c39f472eSBen Skeggs  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22c39f472eSBen Skeggs  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23c39f472eSBen Skeggs  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24c39f472eSBen Skeggs  *
25c39f472eSBen Skeggs  */
26a8c4362bSBen Skeggs #include "nv04.h"
27a8c4362bSBen Skeggs #include "fbmem.h"
28c39f472eSBen Skeggs 
29a8c4362bSBen Skeggs #include <subdev/bios.h>
30a8c4362bSBen Skeggs #include <subdev/bios/init.h>
31a8c4362bSBen Skeggs #include <subdev/bios/pll.h>
32a8c4362bSBen Skeggs #include <subdev/clk/pll.h>
33c39f472eSBen Skeggs #include <subdev/vga.h>
34c39f472eSBen Skeggs 
35c39f472eSBen Skeggs static void
nv04_devinit_meminit(struct nvkm_devinit * init)368ac3f64fSBen Skeggs nv04_devinit_meminit(struct nvkm_devinit *init)
37c39f472eSBen Skeggs {
38aa860e4bSBen Skeggs 	struct nvkm_subdev *subdev = &init->subdev;
39aa860e4bSBen Skeggs 	struct nvkm_device *device = subdev->device;
40c39f472eSBen Skeggs 	u32 patt = 0xdeadbeef;
41c39f472eSBen Skeggs 	struct io_mapping *fb;
42c39f472eSBen Skeggs 	int i;
43c39f472eSBen Skeggs 
44c39f472eSBen Skeggs 	/* Map the framebuffer aperture */
458ac3f64fSBen Skeggs 	fb = fbmem_init(device);
46c39f472eSBen Skeggs 	if (!fb) {
47aa860e4bSBen Skeggs 		nvkm_error(subdev, "failed to map fb\n");
48c39f472eSBen Skeggs 		return;
49c39f472eSBen Skeggs 	}
50c39f472eSBen Skeggs 
51c39f472eSBen Skeggs 	/* Sequencer and refresh off */
52a8dae9feSBen Skeggs 	nvkm_wrvgas(device, 0, 1, nvkm_rdvgas(device, 0, 1) | 0x20);
538ac3f64fSBen Skeggs 	nvkm_mask(device, NV04_PFB_DEBUG_0, 0, NV04_PFB_DEBUG_0_REFRESH_OFF);
54c39f472eSBen Skeggs 
558ac3f64fSBen Skeggs 	nvkm_mask(device, NV04_PFB_BOOT_0, ~0,
56c39f472eSBen Skeggs 		      NV04_PFB_BOOT_0_RAM_AMOUNT_16MB |
57c39f472eSBen Skeggs 		      NV04_PFB_BOOT_0_RAM_WIDTH_128 |
58c39f472eSBen Skeggs 		      NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_16MBIT);
59c39f472eSBen Skeggs 
60c39f472eSBen Skeggs 	for (i = 0; i < 4; i++)
61c39f472eSBen Skeggs 		fbmem_poke(fb, 4 * i, patt);
62c39f472eSBen Skeggs 
63c39f472eSBen Skeggs 	fbmem_poke(fb, 0x400000, patt + 1);
64c39f472eSBen Skeggs 
65c39f472eSBen Skeggs 	if (fbmem_peek(fb, 0) == patt + 1) {
668ac3f64fSBen Skeggs 		nvkm_mask(device, NV04_PFB_BOOT_0,
67c39f472eSBen Skeggs 			      NV04_PFB_BOOT_0_RAM_TYPE,
68c39f472eSBen Skeggs 			      NV04_PFB_BOOT_0_RAM_TYPE_SDRAM_16MBIT);
698ac3f64fSBen Skeggs 		nvkm_mask(device, NV04_PFB_DEBUG_0,
70c39f472eSBen Skeggs 			      NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
71c39f472eSBen Skeggs 
72c39f472eSBen Skeggs 		for (i = 0; i < 4; i++)
73c39f472eSBen Skeggs 			fbmem_poke(fb, 4 * i, patt);
74c39f472eSBen Skeggs 
75c39f472eSBen Skeggs 		if ((fbmem_peek(fb, 0xc) & 0xffff) != (patt & 0xffff))
768ac3f64fSBen Skeggs 			nvkm_mask(device, NV04_PFB_BOOT_0,
77c39f472eSBen Skeggs 				      NV04_PFB_BOOT_0_RAM_WIDTH_128 |
78c39f472eSBen Skeggs 				      NV04_PFB_BOOT_0_RAM_AMOUNT,
79c39f472eSBen Skeggs 				      NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
80c39f472eSBen Skeggs 	} else
81c39f472eSBen Skeggs 	if ((fbmem_peek(fb, 0xc) & 0xffff0000) != (patt & 0xffff0000)) {
828ac3f64fSBen Skeggs 		nvkm_mask(device, NV04_PFB_BOOT_0,
83c39f472eSBen Skeggs 			      NV04_PFB_BOOT_0_RAM_WIDTH_128 |
84c39f472eSBen Skeggs 			      NV04_PFB_BOOT_0_RAM_AMOUNT,
85c39f472eSBen Skeggs 			      NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
86c39f472eSBen Skeggs 	} else
87c39f472eSBen Skeggs 	if (fbmem_peek(fb, 0) != patt) {
88c39f472eSBen Skeggs 		if (fbmem_readback(fb, 0x800000, patt))
898ac3f64fSBen Skeggs 			nvkm_mask(device, NV04_PFB_BOOT_0,
90c39f472eSBen Skeggs 				      NV04_PFB_BOOT_0_RAM_AMOUNT,
91c39f472eSBen Skeggs 				      NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
92c39f472eSBen Skeggs 		else
938ac3f64fSBen Skeggs 			nvkm_mask(device, NV04_PFB_BOOT_0,
94c39f472eSBen Skeggs 				      NV04_PFB_BOOT_0_RAM_AMOUNT,
95c39f472eSBen Skeggs 				      NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
96c39f472eSBen Skeggs 
978ac3f64fSBen Skeggs 		nvkm_mask(device, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
98c39f472eSBen Skeggs 			      NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_8MBIT);
99c39f472eSBen Skeggs 	} else
100c39f472eSBen Skeggs 	if (!fbmem_readback(fb, 0x800000, patt)) {
1018ac3f64fSBen Skeggs 		nvkm_mask(device, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
102c39f472eSBen Skeggs 			      NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
103c39f472eSBen Skeggs 
104c39f472eSBen Skeggs 	}
105c39f472eSBen Skeggs 
106c39f472eSBen Skeggs 	/* Refresh on, sequencer on */
1078ac3f64fSBen Skeggs 	nvkm_mask(device, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
108a8dae9feSBen Skeggs 	nvkm_wrvgas(device, 0, 1, nvkm_rdvgas(device, 0, 1) & ~0x20);
109c39f472eSBen Skeggs 	fbmem_fini(fb);
110c39f472eSBen Skeggs }
111c39f472eSBen Skeggs 
112c39f472eSBen Skeggs static int
powerctrl_1_shift(int chip_version,int reg)113c39f472eSBen Skeggs powerctrl_1_shift(int chip_version, int reg)
114c39f472eSBen Skeggs {
115c39f472eSBen Skeggs 	int shift = -4;
116c39f472eSBen Skeggs 
117c39f472eSBen Skeggs 	if (chip_version < 0x17 || chip_version == 0x1a || chip_version == 0x20)
118c39f472eSBen Skeggs 		return shift;
119c39f472eSBen Skeggs 
120c39f472eSBen Skeggs 	switch (reg) {
121c39f472eSBen Skeggs 	case 0x680520:
122f6e7393eSGustavo A. R. Silva 		shift += 4; fallthrough;
123c39f472eSBen Skeggs 	case 0x680508:
124f6e7393eSGustavo A. R. Silva 		shift += 4; fallthrough;
125c39f472eSBen Skeggs 	case 0x680504:
126f6e7393eSGustavo A. R. Silva 		shift += 4; fallthrough;
127c39f472eSBen Skeggs 	case 0x680500:
128c39f472eSBen Skeggs 		shift += 4;
129c39f472eSBen Skeggs 	}
130c39f472eSBen Skeggs 
131c39f472eSBen Skeggs 	/*
132c39f472eSBen Skeggs 	 * the shift for vpll regs is only used for nv3x chips with a single
133c39f472eSBen Skeggs 	 * stage pll
134c39f472eSBen Skeggs 	 */
135c39f472eSBen Skeggs 	if (shift > 4 && (chip_version < 0x32 || chip_version == 0x35 ||
136c39f472eSBen Skeggs 			  chip_version == 0x36 || chip_version >= 0x40))
137c39f472eSBen Skeggs 		shift = -4;
138c39f472eSBen Skeggs 
139c39f472eSBen Skeggs 	return shift;
140c39f472eSBen Skeggs }
141c39f472eSBen Skeggs 
142c39f472eSBen Skeggs void
setPLL_single(struct nvkm_devinit * init,u32 reg,struct nvkm_pll_vals * pv)1438ac3f64fSBen Skeggs setPLL_single(struct nvkm_devinit *init, u32 reg,
144a8c4362bSBen Skeggs 	      struct nvkm_pll_vals *pv)
145c39f472eSBen Skeggs {
1468ac3f64fSBen Skeggs 	struct nvkm_device *device = init->subdev.device;
1478ac3f64fSBen Skeggs 	int chip_version = device->bios->version.chip;
1488ac3f64fSBen Skeggs 	uint32_t oldpll = nvkm_rd32(device, reg);
149c39f472eSBen Skeggs 	int oldN = (oldpll >> 8) & 0xff, oldM = oldpll & 0xff;
150c39f472eSBen Skeggs 	uint32_t pll = (oldpll & 0xfff80000) | pv->log2P << 16 | pv->NM1;
151c39f472eSBen Skeggs 	uint32_t saved_powerctrl_1 = 0;
152c39f472eSBen Skeggs 	int shift_powerctrl_1 = powerctrl_1_shift(chip_version, reg);
153c39f472eSBen Skeggs 
154c39f472eSBen Skeggs 	if (oldpll == pll)
155c39f472eSBen Skeggs 		return;	/* already set */
156c39f472eSBen Skeggs 
157c39f472eSBen Skeggs 	if (shift_powerctrl_1 >= 0) {
1588ac3f64fSBen Skeggs 		saved_powerctrl_1 = nvkm_rd32(device, 0x001584);
1598ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x001584,
160c39f472eSBen Skeggs 			(saved_powerctrl_1 & ~(0xf << shift_powerctrl_1)) |
161c39f472eSBen Skeggs 			1 << shift_powerctrl_1);
162c39f472eSBen Skeggs 	}
163c39f472eSBen Skeggs 
164c39f472eSBen Skeggs 	if (oldM && pv->M1 && (oldN / oldM < pv->N1 / pv->M1))
165c39f472eSBen Skeggs 		/* upclock -- write new post divider first */
1668ac3f64fSBen Skeggs 		nvkm_wr32(device, reg, pv->log2P << 16 | (oldpll & 0xffff));
167c39f472eSBen Skeggs 	else
168c39f472eSBen Skeggs 		/* downclock -- write new NM first */
1698ac3f64fSBen Skeggs 		nvkm_wr32(device, reg, (oldpll & 0xffff0000) | pv->NM1);
170c39f472eSBen Skeggs 
171c39f472eSBen Skeggs 	if ((chip_version < 0x17 || chip_version == 0x1a) &&
172c39f472eSBen Skeggs 	    chip_version != 0x11)
173c39f472eSBen Skeggs 		/* wait a bit on older chips */
174c39f472eSBen Skeggs 		msleep(64);
1758ac3f64fSBen Skeggs 	nvkm_rd32(device, reg);
176c39f472eSBen Skeggs 
177c39f472eSBen Skeggs 	/* then write the other half as well */
1788ac3f64fSBen Skeggs 	nvkm_wr32(device, reg, pll);
179c39f472eSBen Skeggs 
180c39f472eSBen Skeggs 	if (shift_powerctrl_1 >= 0)
1818ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x001584, saved_powerctrl_1);
182c39f472eSBen Skeggs }
183c39f472eSBen Skeggs 
184c39f472eSBen Skeggs static uint32_t
new_ramdac580(uint32_t reg1,bool ss,uint32_t ramdac580)185c39f472eSBen Skeggs new_ramdac580(uint32_t reg1, bool ss, uint32_t ramdac580)
186c39f472eSBen Skeggs {
187c39f472eSBen Skeggs 	bool head_a = (reg1 == 0x680508);
188c39f472eSBen Skeggs 
189c39f472eSBen Skeggs 	if (ss)	/* single stage pll mode */
190c39f472eSBen Skeggs 		ramdac580 |= head_a ? 0x00000100 : 0x10000000;
191c39f472eSBen Skeggs 	else
192c39f472eSBen Skeggs 		ramdac580 &= head_a ? 0xfffffeff : 0xefffffff;
193c39f472eSBen Skeggs 
194c39f472eSBen Skeggs 	return ramdac580;
195c39f472eSBen Skeggs }
196c39f472eSBen Skeggs 
197c39f472eSBen Skeggs void
setPLL_double_highregs(struct nvkm_devinit * init,u32 reg1,struct nvkm_pll_vals * pv)1988ac3f64fSBen Skeggs setPLL_double_highregs(struct nvkm_devinit *init, u32 reg1,
199a8c4362bSBen Skeggs 		       struct nvkm_pll_vals *pv)
200c39f472eSBen Skeggs {
2018ac3f64fSBen Skeggs 	struct nvkm_device *device = init->subdev.device;
2028ac3f64fSBen Skeggs 	int chip_version = device->bios->version.chip;
203c39f472eSBen Skeggs 	bool nv3035 = chip_version == 0x30 || chip_version == 0x35;
204c39f472eSBen Skeggs 	uint32_t reg2 = reg1 + ((reg1 == 0x680520) ? 0x5c : 0x70);
2058ac3f64fSBen Skeggs 	uint32_t oldpll1 = nvkm_rd32(device, reg1);
2068ac3f64fSBen Skeggs 	uint32_t oldpll2 = !nv3035 ? nvkm_rd32(device, reg2) : 0;
207c39f472eSBen Skeggs 	uint32_t pll1 = (oldpll1 & 0xfff80000) | pv->log2P << 16 | pv->NM1;
208c39f472eSBen Skeggs 	uint32_t pll2 = (oldpll2 & 0x7fff0000) | 1 << 31 | pv->NM2;
209c39f472eSBen Skeggs 	uint32_t oldramdac580 = 0, ramdac580 = 0;
210c39f472eSBen Skeggs 	bool single_stage = !pv->NM2 || pv->N2 == pv->M2;	/* nv41+ only */
211c39f472eSBen Skeggs 	uint32_t saved_powerctrl_1 = 0, savedc040 = 0;
212c39f472eSBen Skeggs 	int shift_powerctrl_1 = powerctrl_1_shift(chip_version, reg1);
213c39f472eSBen Skeggs 
214c39f472eSBen Skeggs 	/* model specific additions to generic pll1 and pll2 set up above */
215c39f472eSBen Skeggs 	if (nv3035) {
216c39f472eSBen Skeggs 		pll1 = (pll1 & 0xfcc7ffff) | (pv->N2 & 0x18) << 21 |
217c39f472eSBen Skeggs 		       (pv->N2 & 0x7) << 19 | 8 << 4 | (pv->M2 & 7) << 4;
218c39f472eSBen Skeggs 		pll2 = 0;
219c39f472eSBen Skeggs 	}
220c39f472eSBen Skeggs 	if (chip_version > 0x40 && reg1 >= 0x680508) { /* !nv40 */
2218ac3f64fSBen Skeggs 		oldramdac580 = nvkm_rd32(device, 0x680580);
222c39f472eSBen Skeggs 		ramdac580 = new_ramdac580(reg1, single_stage, oldramdac580);
223c39f472eSBen Skeggs 		if (oldramdac580 != ramdac580)
224c39f472eSBen Skeggs 			oldpll1 = ~0;	/* force mismatch */
225c39f472eSBen Skeggs 		if (single_stage)
226c39f472eSBen Skeggs 			/* magic value used by nvidia in single stage mode */
227c39f472eSBen Skeggs 			pll2 |= 0x011f;
228c39f472eSBen Skeggs 	}
229c39f472eSBen Skeggs 	if (chip_version > 0x70)
230c39f472eSBen Skeggs 		/* magic bits set by the blob (but not the bios) on g71-73 */
231c39f472eSBen Skeggs 		pll1 = (pll1 & 0x7fffffff) | (single_stage ? 0x4 : 0xc) << 28;
232c39f472eSBen Skeggs 
233c39f472eSBen Skeggs 	if (oldpll1 == pll1 && oldpll2 == pll2)
234c39f472eSBen Skeggs 		return;	/* already set */
235c39f472eSBen Skeggs 
236c39f472eSBen Skeggs 	if (shift_powerctrl_1 >= 0) {
2378ac3f64fSBen Skeggs 		saved_powerctrl_1 = nvkm_rd32(device, 0x001584);
2388ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x001584,
239c39f472eSBen Skeggs 			(saved_powerctrl_1 & ~(0xf << shift_powerctrl_1)) |
240c39f472eSBen Skeggs 			1 << shift_powerctrl_1);
241c39f472eSBen Skeggs 	}
242c39f472eSBen Skeggs 
243c39f472eSBen Skeggs 	if (chip_version >= 0x40) {
244c39f472eSBen Skeggs 		int shift_c040 = 14;
245c39f472eSBen Skeggs 
246c39f472eSBen Skeggs 		switch (reg1) {
247c39f472eSBen Skeggs 		case 0x680504:
248f6e7393eSGustavo A. R. Silva 			shift_c040 += 2; fallthrough;
249c39f472eSBen Skeggs 		case 0x680500:
250f6e7393eSGustavo A. R. Silva 			shift_c040 += 2; fallthrough;
251c39f472eSBen Skeggs 		case 0x680520:
252f6e7393eSGustavo A. R. Silva 			shift_c040 += 2; fallthrough;
253c39f472eSBen Skeggs 		case 0x680508:
254c39f472eSBen Skeggs 			shift_c040 += 2;
255c39f472eSBen Skeggs 		}
256c39f472eSBen Skeggs 
2578ac3f64fSBen Skeggs 		savedc040 = nvkm_rd32(device, 0xc040);
258c39f472eSBen Skeggs 		if (shift_c040 != 14)
2598ac3f64fSBen Skeggs 			nvkm_wr32(device, 0xc040, savedc040 & ~(3 << shift_c040));
260c39f472eSBen Skeggs 	}
261c39f472eSBen Skeggs 
262c39f472eSBen Skeggs 	if (oldramdac580 != ramdac580)
2638ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x680580, ramdac580);
264c39f472eSBen Skeggs 
265c39f472eSBen Skeggs 	if (!nv3035)
2668ac3f64fSBen Skeggs 		nvkm_wr32(device, reg2, pll2);
2678ac3f64fSBen Skeggs 	nvkm_wr32(device, reg1, pll1);
268c39f472eSBen Skeggs 
269c39f472eSBen Skeggs 	if (shift_powerctrl_1 >= 0)
2708ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x001584, saved_powerctrl_1);
271c39f472eSBen Skeggs 	if (chip_version >= 0x40)
2728ac3f64fSBen Skeggs 		nvkm_wr32(device, 0xc040, savedc040);
273c39f472eSBen Skeggs }
274c39f472eSBen Skeggs 
275c39f472eSBen Skeggs void
setPLL_double_lowregs(struct nvkm_devinit * init,u32 NMNMreg,struct nvkm_pll_vals * pv)2768ac3f64fSBen Skeggs setPLL_double_lowregs(struct nvkm_devinit *init, u32 NMNMreg,
277a8c4362bSBen Skeggs 		      struct nvkm_pll_vals *pv)
278c39f472eSBen Skeggs {
279c39f472eSBen Skeggs 	/* When setting PLLs, there is a merry game of disabling and enabling
280c39f472eSBen Skeggs 	 * various bits of hardware during the process. This function is a
281c39f472eSBen Skeggs 	 * synthesis of six nv4x traces, nearly each card doing a subtly
282c39f472eSBen Skeggs 	 * different thing. With luck all the necessary bits for each card are
283c39f472eSBen Skeggs 	 * combined herein. Without luck it deviates from each card's formula
284c39f472eSBen Skeggs 	 * so as to not work on any :)
285c39f472eSBen Skeggs 	 */
2868ac3f64fSBen Skeggs 	struct nvkm_device *device = init->subdev.device;
287c39f472eSBen Skeggs 	uint32_t Preg = NMNMreg - 4;
288c39f472eSBen Skeggs 	bool mpll = Preg == 0x4020;
2898ac3f64fSBen Skeggs 	uint32_t oldPval = nvkm_rd32(device, Preg);
290c39f472eSBen Skeggs 	uint32_t NMNM = pv->NM2 << 16 | pv->NM1;
291c39f472eSBen Skeggs 	uint32_t Pval = (oldPval & (mpll ? ~(0x77 << 16) : ~(7 << 16))) |
292c39f472eSBen Skeggs 			0xc << 28 | pv->log2P << 16;
293c39f472eSBen Skeggs 	uint32_t saved4600 = 0;
294c39f472eSBen Skeggs 	/* some cards have different maskc040s */
295c39f472eSBen Skeggs 	uint32_t maskc040 = ~(3 << 14), savedc040;
296c39f472eSBen Skeggs 	bool single_stage = !pv->NM2 || pv->N2 == pv->M2;
297c39f472eSBen Skeggs 
2988ac3f64fSBen Skeggs 	if (nvkm_rd32(device, NMNMreg) == NMNM && (oldPval & 0xc0070000) == Pval)
299c39f472eSBen Skeggs 		return;
300c39f472eSBen Skeggs 
301c39f472eSBen Skeggs 	if (Preg == 0x4000)
302c39f472eSBen Skeggs 		maskc040 = ~0x333;
303c39f472eSBen Skeggs 	if (Preg == 0x4058)
304c39f472eSBen Skeggs 		maskc040 = ~(0xc << 24);
305c39f472eSBen Skeggs 
306c39f472eSBen Skeggs 	if (mpll) {
307c39f472eSBen Skeggs 		struct nvbios_pll info;
308c39f472eSBen Skeggs 		uint8_t Pval2;
309c39f472eSBen Skeggs 
3108ac3f64fSBen Skeggs 		if (nvbios_pll_parse(device->bios, Preg, &info))
311c39f472eSBen Skeggs 			return;
312c39f472eSBen Skeggs 
313c39f472eSBen Skeggs 		Pval2 = pv->log2P + info.bias_p;
314c39f472eSBen Skeggs 		if (Pval2 > info.max_p)
315c39f472eSBen Skeggs 			Pval2 = info.max_p;
316c39f472eSBen Skeggs 		Pval |= 1 << 28 | Pval2 << 20;
317c39f472eSBen Skeggs 
3188ac3f64fSBen Skeggs 		saved4600 = nvkm_rd32(device, 0x4600);
3198ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x4600, saved4600 | 8 << 28);
320c39f472eSBen Skeggs 	}
321c39f472eSBen Skeggs 	if (single_stage)
322c39f472eSBen Skeggs 		Pval |= mpll ? 1 << 12 : 1 << 8;
323c39f472eSBen Skeggs 
3248ac3f64fSBen Skeggs 	nvkm_wr32(device, Preg, oldPval | 1 << 28);
3258ac3f64fSBen Skeggs 	nvkm_wr32(device, Preg, Pval & ~(4 << 28));
326c39f472eSBen Skeggs 	if (mpll) {
327c39f472eSBen Skeggs 		Pval |= 8 << 20;
3288ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x4020, Pval & ~(0xc << 28));
3298ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x4038, Pval & ~(0xc << 28));
330c39f472eSBen Skeggs 	}
331c39f472eSBen Skeggs 
3328ac3f64fSBen Skeggs 	savedc040 = nvkm_rd32(device, 0xc040);
3338ac3f64fSBen Skeggs 	nvkm_wr32(device, 0xc040, savedc040 & maskc040);
334c39f472eSBen Skeggs 
3358ac3f64fSBen Skeggs 	nvkm_wr32(device, NMNMreg, NMNM);
336c39f472eSBen Skeggs 	if (NMNMreg == 0x4024)
3378ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x403c, NMNM);
338c39f472eSBen Skeggs 
3398ac3f64fSBen Skeggs 	nvkm_wr32(device, Preg, Pval);
340c39f472eSBen Skeggs 	if (mpll) {
341c39f472eSBen Skeggs 		Pval &= ~(8 << 20);
3428ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x4020, Pval);
3438ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x4038, Pval);
3448ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x4600, saved4600);
345c39f472eSBen Skeggs 	}
346c39f472eSBen Skeggs 
3478ac3f64fSBen Skeggs 	nvkm_wr32(device, 0xc040, savedc040);
348c39f472eSBen Skeggs 
349c39f472eSBen Skeggs 	if (mpll) {
3508ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x4020, Pval & ~(1 << 28));
3518ac3f64fSBen Skeggs 		nvkm_wr32(device, 0x4038, Pval & ~(1 << 28));
352c39f472eSBen Skeggs 	}
353c39f472eSBen Skeggs }
354c39f472eSBen Skeggs 
355c39f472eSBen Skeggs int
nv04_devinit_pll_set(struct nvkm_devinit * devinit,u32 type,u32 freq)356a8c4362bSBen Skeggs nv04_devinit_pll_set(struct nvkm_devinit *devinit, u32 type, u32 freq)
357c39f472eSBen Skeggs {
35846484438SBen Skeggs 	struct nvkm_subdev *subdev = &devinit->subdev;
35946484438SBen Skeggs 	struct nvkm_bios *bios = subdev->device->bios;
360a8c4362bSBen Skeggs 	struct nvkm_pll_vals pv;
361c39f472eSBen Skeggs 	struct nvbios_pll info;
362c39f472eSBen Skeggs 	int cv = bios->version.chip;
363c39f472eSBen Skeggs 	int N1, M1, N2, M2, P;
364c39f472eSBen Skeggs 	int ret;
365c39f472eSBen Skeggs 
366c39f472eSBen Skeggs 	ret = nvbios_pll_parse(bios, type > 0x405c ? type : type - 4, &info);
367c39f472eSBen Skeggs 	if (ret)
368c39f472eSBen Skeggs 		return ret;
369c39f472eSBen Skeggs 
37046484438SBen Skeggs 	ret = nv04_pll_calc(subdev, &info, freq, &N1, &M1, &N2, &M2, &P);
371c39f472eSBen Skeggs 	if (!ret)
372c39f472eSBen Skeggs 		return -EINVAL;
373c39f472eSBen Skeggs 
374c39f472eSBen Skeggs 	pv.refclk = info.refclk;
375c39f472eSBen Skeggs 	pv.N1 = N1;
376c39f472eSBen Skeggs 	pv.M1 = M1;
377c39f472eSBen Skeggs 	pv.N2 = N2;
378c39f472eSBen Skeggs 	pv.M2 = M2;
379c39f472eSBen Skeggs 	pv.log2P = P;
380c39f472eSBen Skeggs 
381c39f472eSBen Skeggs 	if (cv == 0x30 || cv == 0x31 || cv == 0x35 || cv == 0x36 ||
382c39f472eSBen Skeggs 	    cv >= 0x40) {
383c39f472eSBen Skeggs 		if (type > 0x405c)
384c39f472eSBen Skeggs 			setPLL_double_highregs(devinit, type, &pv);
385c39f472eSBen Skeggs 		else
386c39f472eSBen Skeggs 			setPLL_double_lowregs(devinit, type, &pv);
387c39f472eSBen Skeggs 	} else
388c39f472eSBen Skeggs 		setPLL_single(devinit, type, &pv);
389c39f472eSBen Skeggs 
390c39f472eSBen Skeggs 	return 0;
391c39f472eSBen Skeggs }
392c39f472eSBen Skeggs 
393c39f472eSBen Skeggs int
nv04_devinit_post(struct nvkm_devinit * init,bool execute)394151abd44SBen Skeggs nv04_devinit_post(struct nvkm_devinit *init, bool execute)
395c39f472eSBen Skeggs {
3964bb4a746SBen Skeggs 	return nvbios_post(&init->subdev, execute);
397151abd44SBen Skeggs }
398151abd44SBen Skeggs 
399151abd44SBen Skeggs void
nv04_devinit_preinit(struct nvkm_devinit * base)400151abd44SBen Skeggs nv04_devinit_preinit(struct nvkm_devinit *base)
401151abd44SBen Skeggs {
402151abd44SBen Skeggs 	struct nv04_devinit *init = nv04_devinit(base);
403151abd44SBen Skeggs 	struct nvkm_subdev *subdev = &init->base.subdev;
404151abd44SBen Skeggs 	struct nvkm_device *device = subdev->device;
405c39f472eSBen Skeggs 
406c39f472eSBen Skeggs 	/* make i2c busses accessible */
4078ac3f64fSBen Skeggs 	nvkm_mask(device, 0x000200, 0x00000001, 0x00000001);
408c39f472eSBen Skeggs 
409c39f472eSBen Skeggs 	/* unslave crtcs */
410266f8b5eSBen Skeggs 	if (init->owner < 0)
411a8dae9feSBen Skeggs 		init->owner = nvkm_rdvgaowner(device);
412a8dae9feSBen Skeggs 	nvkm_wrvgaowner(device, 0);
413c39f472eSBen Skeggs 
414266f8b5eSBen Skeggs 	if (!init->base.post) {
415a8dae9feSBen Skeggs 		u32 htotal = nvkm_rdvgac(device, 0, 0x06);
416a8dae9feSBen Skeggs 		htotal |= (nvkm_rdvgac(device, 0, 0x07) & 0x01) << 8;
417a8dae9feSBen Skeggs 		htotal |= (nvkm_rdvgac(device, 0, 0x07) & 0x20) << 4;
418a8dae9feSBen Skeggs 		htotal |= (nvkm_rdvgac(device, 0, 0x25) & 0x01) << 10;
419a8dae9feSBen Skeggs 		htotal |= (nvkm_rdvgac(device, 0, 0x41) & 0x01) << 11;
420c39f472eSBen Skeggs 		if (!htotal) {
421aa860e4bSBen Skeggs 			nvkm_debug(subdev, "adaptor not initialised\n");
422266f8b5eSBen Skeggs 			init->base.post = true;
423c39f472eSBen Skeggs 		}
424c39f472eSBen Skeggs 	}
425c39f472eSBen Skeggs }
426c39f472eSBen Skeggs 
427151abd44SBen Skeggs void *
nv04_devinit_dtor(struct nvkm_devinit * base)428151abd44SBen Skeggs nv04_devinit_dtor(struct nvkm_devinit *base)
429c39f472eSBen Skeggs {
430151abd44SBen Skeggs 	struct nv04_devinit *init = nv04_devinit(base);
431c39f472eSBen Skeggs 	/* restore vga owner saved at first init */
432a8dae9feSBen Skeggs 	nvkm_wrvgaowner(init->base.subdev.device, init->owner);
433151abd44SBen Skeggs 	return init;
434c39f472eSBen Skeggs }
435c39f472eSBen Skeggs 
436c39f472eSBen Skeggs int
nv04_devinit_new_(const struct nvkm_devinit_func * func,struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_devinit ** pinit)437*4a34fd0eSBen Skeggs nv04_devinit_new_(const struct nvkm_devinit_func *func, struct nvkm_device *device,
438*4a34fd0eSBen Skeggs 		  enum nvkm_subdev_type type, int inst, struct nvkm_devinit **pinit)
439c39f472eSBen Skeggs {
440266f8b5eSBen Skeggs 	struct nv04_devinit *init;
441c39f472eSBen Skeggs 
442151abd44SBen Skeggs 	if (!(init = kzalloc(sizeof(*init), GFP_KERNEL)))
443151abd44SBen Skeggs 		return -ENOMEM;
444151abd44SBen Skeggs 	*pinit = &init->base;
445c39f472eSBen Skeggs 
446*4a34fd0eSBen Skeggs 	nvkm_devinit_ctor(func, device, type, inst, &init->base);
447266f8b5eSBen Skeggs 	init->owner = -1;
448c39f472eSBen Skeggs 	return 0;
449c39f472eSBen Skeggs }
450c39f472eSBen Skeggs 
451151abd44SBen Skeggs static const struct nvkm_devinit_func
452151abd44SBen Skeggs nv04_devinit = {
453c39f472eSBen Skeggs 	.dtor = nv04_devinit_dtor,
454151abd44SBen Skeggs 	.preinit = nv04_devinit_preinit,
455151abd44SBen Skeggs 	.post = nv04_devinit_post,
456c39f472eSBen Skeggs 	.meminit = nv04_devinit_meminit,
457c39f472eSBen Skeggs 	.pll_set = nv04_devinit_pll_set,
458151abd44SBen Skeggs };
459151abd44SBen Skeggs 
460151abd44SBen Skeggs int
nv04_devinit_new(struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_devinit ** pinit)461*4a34fd0eSBen Skeggs nv04_devinit_new(struct nvkm_device *device, enum nvkm_subdev_type type, int inst,
462151abd44SBen Skeggs 		 struct nvkm_devinit **pinit)
463151abd44SBen Skeggs {
464*4a34fd0eSBen Skeggs 	return nv04_devinit_new_(&nv04_devinit, device, type, inst, pinit);
465151abd44SBen Skeggs }
466