2 * Copyright (C) 2010 Francisco Jerez.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <subdev/vga.h>
33 nv04_devinit_meminit(struct nouveau_devinit
*devinit
)
35 struct nv04_devinit_priv
*priv
= (void *)devinit
;
36 u32 patt
= 0xdeadbeef;
37 struct io_mapping
*fb
;
40 /* Map the framebuffer aperture */
41 fb
= fbmem_init(nv_device(priv
));
43 nv_error(priv
, "failed to map fb\n");
47 /* Sequencer and refresh off */
48 nv_wrvgas(priv
, 0, 1, nv_rdvgas(priv
, 0, 1) | 0x20);
49 nv_mask(priv
, NV04_PFB_DEBUG_0
, 0, NV04_PFB_DEBUG_0_REFRESH_OFF
);
51 nv_mask(priv
, NV04_PFB_BOOT_0
, ~0,
52 NV04_PFB_BOOT_0_RAM_AMOUNT_16MB
|
53 NV04_PFB_BOOT_0_RAM_WIDTH_128
|
54 NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_16MBIT
);
56 for (i
= 0; i
< 4; i
++)
57 fbmem_poke(fb
, 4 * i
, patt
);
59 fbmem_poke(fb
, 0x400000, patt
+ 1);
61 if (fbmem_peek(fb
, 0) == patt
+ 1) {
62 nv_mask(priv
, NV04_PFB_BOOT_0
,
63 NV04_PFB_BOOT_0_RAM_TYPE
,
64 NV04_PFB_BOOT_0_RAM_TYPE_SDRAM_16MBIT
);
65 nv_mask(priv
, NV04_PFB_DEBUG_0
,
66 NV04_PFB_DEBUG_0_REFRESH_OFF
, 0);
68 for (i
= 0; i
< 4; i
++)
69 fbmem_poke(fb
, 4 * i
, patt
);
71 if ((fbmem_peek(fb
, 0xc) & 0xffff) != (patt
& 0xffff))
72 nv_mask(priv
, NV04_PFB_BOOT_0
,
73 NV04_PFB_BOOT_0_RAM_WIDTH_128
|
74 NV04_PFB_BOOT_0_RAM_AMOUNT
,
75 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB
);
77 if ((fbmem_peek(fb
, 0xc) & 0xffff0000) != (patt
& 0xffff0000)) {
78 nv_mask(priv
, NV04_PFB_BOOT_0
,
79 NV04_PFB_BOOT_0_RAM_WIDTH_128
|
80 NV04_PFB_BOOT_0_RAM_AMOUNT
,
81 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB
);
83 if (fbmem_peek(fb
, 0) != patt
) {
84 if (fbmem_readback(fb
, 0x800000, patt
))
85 nv_mask(priv
, NV04_PFB_BOOT_0
,
86 NV04_PFB_BOOT_0_RAM_AMOUNT
,
87 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB
);
89 nv_mask(priv
, NV04_PFB_BOOT_0
,
90 NV04_PFB_BOOT_0_RAM_AMOUNT
,
91 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB
);
93 nv_mask(priv
, NV04_PFB_BOOT_0
, NV04_PFB_BOOT_0_RAM_TYPE
,
94 NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_8MBIT
);
96 if (!fbmem_readback(fb
, 0x800000, patt
)) {
97 nv_mask(priv
, NV04_PFB_BOOT_0
, NV04_PFB_BOOT_0_RAM_AMOUNT
,
98 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB
);
102 /* Refresh on, sequencer on */
103 nv_mask(priv
, NV04_PFB_DEBUG_0
, NV04_PFB_DEBUG_0_REFRESH_OFF
, 0);
104 nv_wrvgas(priv
, 0, 1, nv_rdvgas(priv
, 0, 1) & ~0x20);
109 powerctrl_1_shift(int chip_version
, int reg
)
113 if (chip_version
< 0x17 || chip_version
== 0x1a || chip_version
== 0x20)
128 * the shift for vpll regs is only used for nv3x chips with a single
131 if (shift
> 4 && (chip_version
< 0x32 || chip_version
== 0x35 ||
132 chip_version
== 0x36 || chip_version
>= 0x40))
139 setPLL_single(struct nouveau_devinit
*devinit
, u32 reg
,
140 struct nouveau_pll_vals
*pv
)
142 int chip_version
= nouveau_bios(devinit
)->version
.chip
;
143 uint32_t oldpll
= nv_rd32(devinit
, reg
);
144 int oldN
= (oldpll
>> 8) & 0xff, oldM
= oldpll
& 0xff;
145 uint32_t pll
= (oldpll
& 0xfff80000) | pv
->log2P
<< 16 | pv
->NM1
;
146 uint32_t saved_powerctrl_1
= 0;
147 int shift_powerctrl_1
= powerctrl_1_shift(chip_version
, reg
);
150 return; /* already set */
152 if (shift_powerctrl_1
>= 0) {
153 saved_powerctrl_1
= nv_rd32(devinit
, 0x001584);
154 nv_wr32(devinit
, 0x001584,
155 (saved_powerctrl_1
& ~(0xf << shift_powerctrl_1
)) |
156 1 << shift_powerctrl_1
);
159 if (oldM
&& pv
->M1
&& (oldN
/ oldM
< pv
->N1
/ pv
->M1
))
160 /* upclock -- write new post divider first */
161 nv_wr32(devinit
, reg
, pv
->log2P
<< 16 | (oldpll
& 0xffff));
163 /* downclock -- write new NM first */
164 nv_wr32(devinit
, reg
, (oldpll
& 0xffff0000) | pv
->NM1
);
166 if ((chip_version
< 0x17 || chip_version
== 0x1a) &&
167 chip_version
!= 0x11)
168 /* wait a bit on older chips */
170 nv_rd32(devinit
, reg
);
172 /* then write the other half as well */
173 nv_wr32(devinit
, reg
, pll
);
175 if (shift_powerctrl_1
>= 0)
176 nv_wr32(devinit
, 0x001584, saved_powerctrl_1
);
180 new_ramdac580(uint32_t reg1
, bool ss
, uint32_t ramdac580
)
182 bool head_a
= (reg1
== 0x680508);
184 if (ss
) /* single stage pll mode */
185 ramdac580
|= head_a
? 0x00000100 : 0x10000000;
187 ramdac580
&= head_a
? 0xfffffeff : 0xefffffff;
193 setPLL_double_highregs(struct nouveau_devinit
*devinit
, u32 reg1
,
194 struct nouveau_pll_vals
*pv
)
196 int chip_version
= nouveau_bios(devinit
)->version
.chip
;
197 bool nv3035
= chip_version
== 0x30 || chip_version
== 0x35;
198 uint32_t reg2
= reg1
+ ((reg1
== 0x680520) ? 0x5c : 0x70);
199 uint32_t oldpll1
= nv_rd32(devinit
, reg1
);
200 uint32_t oldpll2
= !nv3035
? nv_rd32(devinit
, reg2
) : 0;
201 uint32_t pll1
= (oldpll1
& 0xfff80000) | pv
->log2P
<< 16 | pv
->NM1
;
202 uint32_t pll2
= (oldpll2
& 0x7fff0000) | 1 << 31 | pv
->NM2
;
203 uint32_t oldramdac580
= 0, ramdac580
= 0;
204 bool single_stage
= !pv
->NM2
|| pv
->N2
== pv
->M2
; /* nv41+ only */
205 uint32_t saved_powerctrl_1
= 0, savedc040
= 0;
206 int shift_powerctrl_1
= powerctrl_1_shift(chip_version
, reg1
);
208 /* model specific additions to generic pll1 and pll2 set up above */
210 pll1
= (pll1
& 0xfcc7ffff) | (pv
->N2
& 0x18) << 21 |
211 (pv
->N2
& 0x7) << 19 | 8 << 4 | (pv
->M2
& 7) << 4;
214 if (chip_version
> 0x40 && reg1
>= 0x680508) { /* !nv40 */
215 oldramdac580
= nv_rd32(devinit
, 0x680580);
216 ramdac580
= new_ramdac580(reg1
, single_stage
, oldramdac580
);
217 if (oldramdac580
!= ramdac580
)
218 oldpll1
= ~0; /* force mismatch */
220 /* magic value used by nvidia in single stage mode */
223 if (chip_version
> 0x70)
224 /* magic bits set by the blob (but not the bios) on g71-73 */
225 pll1
= (pll1
& 0x7fffffff) | (single_stage
? 0x4 : 0xc) << 28;
227 if (oldpll1
== pll1
&& oldpll2
== pll2
)
228 return; /* already set */
230 if (shift_powerctrl_1
>= 0) {
231 saved_powerctrl_1
= nv_rd32(devinit
, 0x001584);
232 nv_wr32(devinit
, 0x001584,
233 (saved_powerctrl_1
& ~(0xf << shift_powerctrl_1
)) |
234 1 << shift_powerctrl_1
);
237 if (chip_version
>= 0x40) {
251 savedc040
= nv_rd32(devinit
, 0xc040);
252 if (shift_c040
!= 14)
253 nv_wr32(devinit
, 0xc040, savedc040
& ~(3 << shift_c040
));
256 if (oldramdac580
!= ramdac580
)
257 nv_wr32(devinit
, 0x680580, ramdac580
);
260 nv_wr32(devinit
, reg2
, pll2
);
261 nv_wr32(devinit
, reg1
, pll1
);
263 if (shift_powerctrl_1
>= 0)
264 nv_wr32(devinit
, 0x001584, saved_powerctrl_1
);
265 if (chip_version
>= 0x40)
266 nv_wr32(devinit
, 0xc040, savedc040
);
270 setPLL_double_lowregs(struct nouveau_devinit
*devinit
, u32 NMNMreg
,
271 struct nouveau_pll_vals
*pv
)
273 /* When setting PLLs, there is a merry game of disabling and enabling
274 * various bits of hardware during the process. This function is a
275 * synthesis of six nv4x traces, nearly each card doing a subtly
276 * different thing. With luck all the necessary bits for each card are
277 * combined herein. Without luck it deviates from each card's formula
278 * so as to not work on any :)
281 uint32_t Preg
= NMNMreg
- 4;
282 bool mpll
= Preg
== 0x4020;
283 uint32_t oldPval
= nv_rd32(devinit
, Preg
);
284 uint32_t NMNM
= pv
->NM2
<< 16 | pv
->NM1
;
285 uint32_t Pval
= (oldPval
& (mpll
? ~(0x77 << 16) : ~(7 << 16))) |
286 0xc << 28 | pv
->log2P
<< 16;
287 uint32_t saved4600
= 0;
288 /* some cards have different maskc040s */
289 uint32_t maskc040
= ~(3 << 14), savedc040
;
290 bool single_stage
= !pv
->NM2
|| pv
->N2
== pv
->M2
;
292 if (nv_rd32(devinit
, NMNMreg
) == NMNM
&& (oldPval
& 0xc0070000) == Pval
)
298 maskc040
= ~(0xc << 24);
301 struct nvbios_pll info
;
304 if (nvbios_pll_parse(nouveau_bios(devinit
), Preg
, &info
))
307 Pval2
= pv
->log2P
+ info
.bias_p
;
308 if (Pval2
> info
.max_p
)
310 Pval
|= 1 << 28 | Pval2
<< 20;
312 saved4600
= nv_rd32(devinit
, 0x4600);
313 nv_wr32(devinit
, 0x4600, saved4600
| 8 << 28);
316 Pval
|= mpll
? 1 << 12 : 1 << 8;
318 nv_wr32(devinit
, Preg
, oldPval
| 1 << 28);
319 nv_wr32(devinit
, Preg
, Pval
& ~(4 << 28));
322 nv_wr32(devinit
, 0x4020, Pval
& ~(0xc << 28));
323 nv_wr32(devinit
, 0x4038, Pval
& ~(0xc << 28));
326 savedc040
= nv_rd32(devinit
, 0xc040);
327 nv_wr32(devinit
, 0xc040, savedc040
& maskc040
);
329 nv_wr32(devinit
, NMNMreg
, NMNM
);
330 if (NMNMreg
== 0x4024)
331 nv_wr32(devinit
, 0x403c, NMNM
);
333 nv_wr32(devinit
, Preg
, Pval
);
336 nv_wr32(devinit
, 0x4020, Pval
);
337 nv_wr32(devinit
, 0x4038, Pval
);
338 nv_wr32(devinit
, 0x4600, saved4600
);
341 nv_wr32(devinit
, 0xc040, savedc040
);
344 nv_wr32(devinit
, 0x4020, Pval
& ~(1 << 28));
345 nv_wr32(devinit
, 0x4038, Pval
& ~(1 << 28));
350 nv04_devinit_pll_set(struct nouveau_devinit
*devinit
, u32 type
, u32 freq
)
352 struct nouveau_bios
*bios
= nouveau_bios(devinit
);
353 struct nouveau_pll_vals pv
;
354 struct nvbios_pll info
;
355 int cv
= bios
->version
.chip
;
356 int N1
, M1
, N2
, M2
, P
;
359 ret
= nvbios_pll_parse(bios
, type
> 0x405c ? type
: type
- 4, &info
);
363 ret
= nv04_pll_calc(nv_subdev(devinit
), &info
, freq
,
364 &N1
, &M1
, &N2
, &M2
, &P
);
368 pv
.refclk
= info
.refclk
;
375 if (cv
== 0x30 || cv
== 0x31 || cv
== 0x35 || cv
== 0x36 ||
378 setPLL_double_highregs(devinit
, type
, &pv
);
380 setPLL_double_lowregs(devinit
, type
, &pv
);
382 setPLL_single(devinit
, type
, &pv
);
388 nv04_devinit_fini(struct nouveau_object
*object
, bool suspend
)
390 struct nv04_devinit_priv
*priv
= (void *)object
;
393 /* make i2c busses accessible */
394 nv_mask(priv
, 0x000200, 0x00000001, 0x00000001);
396 ret
= nouveau_devinit_fini(&priv
->base
, suspend
);
402 priv
->owner
= nv_rdvgaowner(priv
);
403 nv_wrvgaowner(priv
, 0);
409 nv04_devinit_init(struct nouveau_object
*object
)
411 struct nv04_devinit_priv
*priv
= (void *)object
;
413 if (!priv
->base
.post
) {
414 u32 htotal
= nv_rdvgac(priv
, 0, 0x06);
415 htotal
|= (nv_rdvgac(priv
, 0, 0x07) & 0x01) << 8;
416 htotal
|= (nv_rdvgac(priv
, 0, 0x07) & 0x20) << 4;
417 htotal
|= (nv_rdvgac(priv
, 0, 0x25) & 0x01) << 10;
418 htotal
|= (nv_rdvgac(priv
, 0, 0x41) & 0x01) << 11;
420 nv_info(priv
, "adaptor not initialised\n");
421 priv
->base
.post
= true;
425 return nouveau_devinit_init(&priv
->base
);
429 nv04_devinit_dtor(struct nouveau_object
*object
)
431 struct nv04_devinit_priv
*priv
= (void *)object
;
433 /* restore vga owner saved at first init */
434 nv_wrvgaowner(priv
, priv
->owner
);
436 nouveau_devinit_destroy(&priv
->base
);
440 nv04_devinit_ctor(struct nouveau_object
*parent
, struct nouveau_object
*engine
,
441 struct nouveau_oclass
*oclass
, void *data
, u32 size
,
442 struct nouveau_object
**pobject
)
444 struct nv04_devinit_priv
*priv
;
447 ret
= nouveau_devinit_create(parent
, engine
, oclass
, &priv
);
448 *pobject
= nv_object(priv
);
456 struct nouveau_oclass
*
457 nv04_devinit_oclass
= &(struct nouveau_devinit_impl
) {
458 .base
.handle
= NV_SUBDEV(DEVINIT
, 0x04),
459 .base
.ofuncs
= &(struct nouveau_ofuncs
) {
460 .ctor
= nv04_devinit_ctor
,
461 .dtor
= nv04_devinit_dtor
,
462 .init
= nv04_devinit_init
,
463 .fini
= nv04_devinit_fini
,
465 .meminit
= nv04_devinit_meminit
,
466 .pll_set
= nv04_devinit_pll_set
,