]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/core/subdev/fb/ramnve0.c
drm/nve0/fb/gddr5: 10f698/69c
[karo-tx-linux.git] / drivers / gpu / drm / nouveau / core / subdev / fb / ramnve0.c
1 /*
2  * Copyright 2013 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #include <subdev/gpio.h>
26
27 #include <subdev/bios.h>
28 #include <subdev/bios/pll.h>
29 #include <subdev/bios/init.h>
30 #include <subdev/bios/rammap.h>
31 #include <subdev/bios/timing.h>
32
33 #include <subdev/clock.h>
34 #include <subdev/clock/pll.h>
35
36 #include <subdev/timer.h>
37
38 #include <core/option.h>
39
40 #include "nvc0.h"
41
42 #include "ramfuc.h"
43
44 /* binary driver only executes this path if the condition (a) is true
45  * for any configuration (combination of rammap+ramcfg+timing) that
46  * can be reached on a given card.  for now, we will execute the branch
47  * unconditionally in the hope that a "false everywhere" in the bios
48  * tables doesn't actually mean "don't touch this".
49  */
50 #define NOTE00(a) 1
51
52 struct nve0_ramfuc {
53         struct ramfuc base;
54
55         struct nvbios_pll refpll;
56         struct nvbios_pll mempll;
57
58         struct ramfuc_reg r_gpioMV;
59         u32 r_funcMV[2];
60         struct ramfuc_reg r_gpio2E;
61         u32 r_func2E[2];
62         struct ramfuc_reg r_gpiotrig;
63
64         struct ramfuc_reg r_0x132020;
65         struct ramfuc_reg r_0x132028;
66         struct ramfuc_reg r_0x132024;
67         struct ramfuc_reg r_0x132030;
68         struct ramfuc_reg r_0x132034;
69         struct ramfuc_reg r_0x132000;
70         struct ramfuc_reg r_0x132004;
71         struct ramfuc_reg r_0x132040;
72
73         struct ramfuc_reg r_0x10f248;
74         struct ramfuc_reg r_0x10f290;
75         struct ramfuc_reg r_0x10f294;
76         struct ramfuc_reg r_0x10f298;
77         struct ramfuc_reg r_0x10f29c;
78         struct ramfuc_reg r_0x10f2a0;
79         struct ramfuc_reg r_0x10f2a4;
80         struct ramfuc_reg r_0x10f2a8;
81         struct ramfuc_reg r_0x10f2ac;
82         struct ramfuc_reg r_0x10f2cc;
83         struct ramfuc_reg r_0x10f2e8;
84         struct ramfuc_reg r_0x10f250;
85         struct ramfuc_reg r_0x10f24c;
86         struct ramfuc_reg r_0x10fec4;
87         struct ramfuc_reg r_0x10fec8;
88         struct ramfuc_reg r_0x10f604;
89         struct ramfuc_reg r_0x10f614;
90         struct ramfuc_reg r_0x10f610;
91         struct ramfuc_reg r_0x100770;
92         struct ramfuc_reg r_0x100778;
93         struct ramfuc_reg r_0x10f224;
94
95         struct ramfuc_reg r_0x10f870;
96         struct ramfuc_reg r_0x10f698;
97         struct ramfuc_reg r_0x10f694;
98         struct ramfuc_reg r_0x10f6b8;
99         struct ramfuc_reg r_0x10f808;
100         struct ramfuc_reg r_0x10f670;
101         struct ramfuc_reg r_0x10f60c;
102         struct ramfuc_reg r_0x10f830;
103         struct ramfuc_reg r_0x1373ec;
104         struct ramfuc_reg r_0x10f800;
105         struct ramfuc_reg r_0x10f82c;
106
107         struct ramfuc_reg r_0x10f978;
108         struct ramfuc_reg r_0x10f910;
109         struct ramfuc_reg r_0x10f914;
110
111         struct ramfuc_reg r_mr[16]; /* MR0 - MR8, MR15 */
112
113         struct ramfuc_reg r_0x62c000;
114
115         struct ramfuc_reg r_0x10f200;
116
117         struct ramfuc_reg r_0x10f210;
118         struct ramfuc_reg r_0x10f310;
119         struct ramfuc_reg r_0x10f314;
120         struct ramfuc_reg r_0x10f318;
121         struct ramfuc_reg r_0x10f090;
122         struct ramfuc_reg r_0x10f69c;
123         struct ramfuc_reg r_0x10f824;
124         struct ramfuc_reg r_0x1373f0;
125         struct ramfuc_reg r_0x1373f4;
126         struct ramfuc_reg r_0x137320;
127         struct ramfuc_reg r_0x10f65c;
128         struct ramfuc_reg r_0x10f6bc;
129         struct ramfuc_reg r_0x100710;
130         struct ramfuc_reg r_0x100750;
131 };
132
133 struct nve0_ram {
134         struct nouveau_ram base;
135         struct nve0_ramfuc fuc;
136
137         u32 parts;
138         u32 pmask;
139         u32 pnuts;
140
141         int from;
142         int mode;
143         int N1, fN1, M1, P1;
144         int N2, M2, P2;
145 };
146
147 /*******************************************************************************
148  * GDDR5
149  ******************************************************************************/
150 static void
151 nve0_ram_train(struct nve0_ramfuc *fuc, u32 mask, u32 data)
152 {
153         struct nve0_ram *ram = container_of(fuc, typeof(*ram), fuc);
154         u32 addr = 0x110974, i;
155
156         ram_mask(fuc, 0x10f910, mask, data);
157         ram_mask(fuc, 0x10f914, mask, data);
158
159         for (i = 0; (data & 0x80000000) && i < ram->parts; addr += 0x1000, i++) {
160                 if (ram->pmask & (1 << i))
161                         continue;
162                 ram_wait(fuc, addr, 0x0000000f, 0x00000000, 500000);
163         }
164 }
165
166 static void
167 r1373f4_init(struct nve0_ramfuc *fuc)
168 {
169         struct nve0_ram *ram = container_of(fuc, typeof(*ram), fuc);
170         const u32 mcoef = ((--ram->P2 << 28) | (ram->N2 << 8) | ram->M2);
171         const u32 rcoef = ((  ram->P1 << 16) | (ram->N1 << 8) | ram->M1);
172         const u32 runk0 = ram->fN1 << 16;
173         const u32 runk1 = ram->fN1;
174
175         if (ram->from == 2) {
176                 ram_mask(fuc, 0x1373f4, 0x00000000, 0x00001100);
177                 ram_mask(fuc, 0x1373f4, 0x00000000, 0x00000010);
178         } else {
179                 ram_mask(fuc, 0x1373f4, 0x00000000, 0x00010010);
180         }
181
182         ram_mask(fuc, 0x1373f4, 0x00000003, 0x00000000);
183         ram_mask(fuc, 0x1373f4, 0x00000010, 0x00000000);
184
185         /* (re)program refpll, if required */
186         if ((ram_rd32(fuc, 0x132024) & 0xffffffff) != rcoef ||
187             (ram_rd32(fuc, 0x132034) & 0x0000ffff) != runk1) {
188                 ram_mask(fuc, 0x132000, 0x00000001, 0x00000000);
189                 ram_mask(fuc, 0x132020, 0x00000001, 0x00000000);
190                 ram_wr32(fuc, 0x137320, 0x00000000);
191                 ram_mask(fuc, 0x132030, 0xffff0000, runk0);
192                 ram_mask(fuc, 0x132034, 0x0000ffff, runk1);
193                 ram_wr32(fuc, 0x132024, rcoef);
194                 ram_mask(fuc, 0x132028, 0x00080000, 0x00080000);
195                 ram_mask(fuc, 0x132020, 0x00000001, 0x00000001);
196                 ram_wait(fuc, 0x137390, 0x00020000, 0x00020000, 64000);
197                 ram_mask(fuc, 0x132028, 0x00080000, 0x00000000);
198         }
199
200         /* (re)program mempll, if required */
201         if (ram->mode == 2) {
202                 ram_mask(fuc, 0x1373f4, 0x00010000, 0x00000000);
203                 ram_mask(fuc, 0x132000, 0x00000001, 0x00000000);
204                 ram_mask(fuc, 0x132004, 0x103fffff, mcoef);
205                 ram_mask(fuc, 0x132000, 0x00000001, 0x00000001);
206                 ram_wait(fuc, 0x137390, 0x00000002, 0x00000002, 64000);
207                 ram_mask(fuc, 0x1373f4, 0x00000000, 0x00001100);
208         } else {
209                 ram_mask(fuc, 0x1373f4, 0x00000000, 0x00010100);
210         }
211
212         ram_mask(fuc, 0x1373f4, 0x00000000, 0x00000010);
213 }
214
215 static void
216 r1373f4_fini(struct nve0_ramfuc *fuc)
217 {
218         struct nve0_ram *ram = container_of(fuc, typeof(*ram), fuc);
219         struct nouveau_ram_data *next = ram->base.next;
220         u8 v0 = next->bios.ramcfg_11_03_c0;
221         u8 v1 = next->bios.ramcfg_11_03_30;
222         u32 tmp;
223
224         tmp = ram_rd32(fuc, 0x1373ec) & ~0x00030000;
225         ram_wr32(fuc, 0x1373ec, tmp | (v1 << 16));
226         ram_mask(fuc, 0x1373f0, (~ram->mode & 3), 0x00000000);
227         if (ram->mode == 2) {
228                 ram_mask(fuc, 0x1373f4, 0x00000003, 0x000000002);
229                 ram_mask(fuc, 0x1373f4, 0x00001100, 0x000000000);
230         } else {
231                 ram_mask(fuc, 0x1373f4, 0x00000003, 0x000000001);
232                 ram_mask(fuc, 0x1373f4, 0x00010000, 0x000000000);
233         }
234         ram_mask(fuc, 0x10f800, 0x00000030, (v0 ^ v1) << 4);
235 }
236
237 static void
238 nve0_ram_nuts(struct nve0_ram *ram, struct ramfuc_reg *reg,
239               u32 _mask, u32 _data, u32 _copy)
240 {
241         struct nve0_fb_priv *priv = (void *)nouveau_fb(ram);
242         struct ramfuc *fuc = &ram->fuc.base;
243         u32 addr = 0x110000 + (reg->addr[0] & 0xfff);
244         u32 mask = _mask | _copy;
245         u32 data = (_data & _mask) | (reg->data & _copy);
246         u32 i;
247
248         for (i = 0; i < 16; i++, addr += 0x1000) {
249                 if (ram->pnuts & (1 << i)) {
250                         u32 prev = nv_rd32(priv, addr);
251                         u32 next = (prev & ~mask) | data;
252                         nouveau_memx_wr32(fuc->memx, addr, next);
253                 }
254         }
255 }
256 #define ram_nuts(s,r,m,d,c)                                                    \
257         nve0_ram_nuts((s), &(s)->fuc.r_##r, (m), (d), (c))
258
259 static int
260 nve0_ram_calc_gddr5(struct nouveau_fb *pfb, u32 freq)
261 {
262         struct nve0_ram *ram = (void *)pfb->ram;
263         struct nve0_ramfuc *fuc = &ram->fuc;
264         struct nouveau_ram_data *next = ram->base.next;
265         int vc = !(next->bios.ramcfg_11_02_08);
266         int mv = !(next->bios.ramcfg_11_02_04);
267         u32 mask, data;
268
269         ram_mask(fuc, 0x10f808, 0x40000000, 0x40000000);
270         ram_wr32(fuc, 0x62c000, 0x0f0f0000);
271
272         /* MR1: turn termination on early, for some reason.. */
273         if ((ram->base.mr[1] & 0x03c) != 0x030) {
274                 ram_mask(fuc, mr[1], 0x03c, ram->base.mr[1] & 0x03c);
275                 ram_nuts(ram, mr[1], 0x03c, ram->base.mr1_nuts & 0x03c, 0x000);
276         }
277
278         if (vc == 1 && ram_have(fuc, gpio2E)) {
279                 u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[1]);
280                 if (temp != ram_rd32(fuc, gpio2E)) {
281                         ram_wr32(fuc, gpiotrig, 1);
282                         ram_nsec(fuc, 20000);
283                 }
284         }
285
286         ram_mask(fuc, 0x10f200, 0x00000800, 0x00000000);
287
288         nve0_ram_train(fuc, 0x01020000, 0x000c0000);
289
290         ram_wr32(fuc, 0x10f210, 0x00000000); /* REFRESH_AUTO = 0 */
291         ram_nsec(fuc, 1000);
292         ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
293         ram_nsec(fuc, 1000);
294
295         ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
296         ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
297         ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
298         ram_wr32(fuc, 0x10f090, 0x00000061);
299         ram_wr32(fuc, 0x10f090, 0xc000007f);
300         ram_nsec(fuc, 1000);
301
302         ram_wr32(fuc, 0x10f698, 0x00000000);
303         ram_wr32(fuc, 0x10f69c, 0x00000000);
304
305         /*XXX: there does appear to be some kind of condition here, simply
306          *     modifying these bits in the vbios from the default pl0
307          *     entries shows no change.  however, the data does appear to
308          *     be correct and may be required for the transition back
309          */
310         mask = 0x800f07e0;
311         data = 0x00030000;
312         if (ram_rd32(fuc, 0x10f978) & 0x00800000)
313                 data |= 0x00040000;
314
315         if (1) {
316                 data |= 0x800807e0;
317                 switch (next->bios.ramcfg_11_03_c0) {
318                 case 3: data &= ~0x00000040; break;
319                 case 2: data &= ~0x00000100; break;
320                 case 1: data &= ~0x80000000; break;
321                 case 0: data &= ~0x00000400; break;
322                 }
323
324                 switch (next->bios.ramcfg_11_03_30) {
325                 case 3: data &= ~0x00000020; break;
326                 case 2: data &= ~0x00000080; break;
327                 case 1: data &= ~0x00080000; break;
328                 case 0: data &= ~0x00000200; break;
329                 }
330         }
331
332         if (next->bios.ramcfg_11_02_80)
333                 mask |= 0x03000000;
334         if (next->bios.ramcfg_11_02_40)
335                 mask |= 0x00002000;
336         if (next->bios.ramcfg_11_07_10)
337                 mask |= 0x00004000;
338         if (next->bios.ramcfg_11_07_08)
339                 mask |= 0x00000003;
340         else {
341                 mask |= 0x34000000;
342                 if (ram_rd32(fuc, 0x10f978) & 0x00800000)
343                         mask |= 0x40000000;
344         }
345         ram_mask(fuc, 0x10f824, mask, data);
346
347         ram_mask(fuc, 0x132040, 0x00010000, 0x00000000);
348
349         if (ram->from == 2 && ram->mode != 2) {
350                 ram_mask(fuc, 0x10f808, 0x00080000, 0x00000000);
351                 ram_mask(fuc, 0x10f200, 0x00008000, 0x00008000);
352                 ram_mask(fuc, 0x10f800, 0x00000000, 0x00000004);
353                 ram_mask(fuc, 0x10f830, 0x00008000, 0x01040010);
354                 ram_mask(fuc, 0x10f830, 0x01000000, 0x00000000);
355                 r1373f4_init(fuc);
356                 ram_mask(fuc, 0x1373f0, 0x00000002, 0x00000001);
357                 r1373f4_fini(fuc);
358                 ram_mask(fuc, 0x10f830, 0x00c00000, 0x00240001);
359         } else
360         if (ram->from != 2 && ram->mode != 2) {
361                 r1373f4_init(fuc);
362                 r1373f4_fini(fuc);
363         }
364
365         if (ram_have(fuc, gpioMV)) {
366                 u32 temp  = ram_mask(fuc, gpioMV, 0x3000, fuc->r_funcMV[mv]);
367                 if (temp != ram_rd32(fuc, gpioMV)) {
368                         ram_wr32(fuc, gpiotrig, 1);
369                         ram_nsec(fuc, 64000);
370                 }
371         }
372
373         if ( (next->bios.ramcfg_11_02_40) ||
374              (next->bios.ramcfg_11_07_10)) {
375                 ram_mask(fuc, 0x132040, 0x00010000, 0x00010000);
376                 ram_nsec(fuc, 20000);
377         }
378
379         if (ram->from != 2 && ram->mode == 2) {
380                 ram_mask(fuc, 0x10f800, 0x00000004, 0x00000000);
381                 ram_mask(fuc, 0x1373f0, 0x00000000, 0x00000002);
382                 ram_mask(fuc, 0x10f830, 0x00800001, 0x00408010);
383                 r1373f4_init(fuc);
384                 r1373f4_fini(fuc);
385                 ram_mask(fuc, 0x10f808, 0x00000000, 0x00080000);
386                 ram_mask(fuc, 0x10f200, 0x00808000, 0x00800000);
387         } else
388         if (ram->from == 2 && ram->mode == 2) {
389                 ram_mask(fuc, 0x10f800, 0x00000004, 0x00000000);
390                 r1373f4_init(fuc);
391                 r1373f4_fini(fuc);
392         }
393
394         if (ram->mode != 2) /*XXX*/ {
395                 if (next->bios.ramcfg_11_07_40)
396                         ram_mask(fuc, 0x10f670, 0x80000000, 0x80000000);
397         }
398
399         ram_wr32(fuc, 0x10f65c, 0x00000011 * next->bios.rammap_11_11_0c);
400         ram_wr32(fuc, 0x10f6b8, 0x01010101 * next->bios.ramcfg_11_09);
401         ram_wr32(fuc, 0x10f6bc, 0x01010101 * next->bios.ramcfg_11_09);
402
403         if (!next->bios.ramcfg_11_07_08 && !next->bios.ramcfg_11_07_04) {
404                 ram_wr32(fuc, 0x10f698, 0x01010101 * next->bios.ramcfg_11_04);
405                 ram_wr32(fuc, 0x10f69c, 0x01010101 * next->bios.ramcfg_11_04);
406         } else
407         if (!next->bios.ramcfg_11_07_08) {
408                 ram_wr32(fuc, 0x10f698, 0x00000000);
409                 ram_wr32(fuc, 0x10f69c, 0x00000000);
410         }
411
412         if (ram->mode != 2) {
413                 u32 data = 0x01000100 * next->bios.ramcfg_11_04;
414                 ram_nuke(fuc, 0x10f694);
415                 ram_mask(fuc, 0x10f694, 0xff00ff00, data);
416         }
417
418         if (ram->mode == 2 && (next->bios.ramcfg_11_08_10))
419                 data = 0x00000080;
420         else
421                 data = 0x00000000;
422         ram_mask(fuc, 0x10f60c, 0x00000080, data);
423
424         mask = 0x00070000;
425         data = 0x00000000;
426         if (!(next->bios.ramcfg_11_02_80))
427                 data |= 0x03000000;
428         if (!(next->bios.ramcfg_11_02_40))
429                 data |= 0x00002000;
430         if (!(next->bios.ramcfg_11_07_10))
431                 data |= 0x00004000;
432         if (!(next->bios.ramcfg_11_07_08))
433                 data |= 0x00000003;
434         else
435                 data |= 0x74000000;
436         ram_mask(fuc, 0x10f824, mask, data);
437
438         if (next->bios.ramcfg_11_01_08)
439                 data = 0x00000000;
440         else
441                 data = 0x00001000;
442         ram_mask(fuc, 0x10f200, 0x00001000, data);
443
444         if (ram_rd32(fuc, 0x10f670) & 0x80000000) {
445                 ram_nsec(fuc, 10000);
446                 ram_mask(fuc, 0x10f670, 0x80000000, 0x00000000);
447         }
448
449         if (next->bios.ramcfg_11_08_01)
450                 data = 0x00100000;
451         else
452                 data = 0x00000000;
453         ram_mask(fuc, 0x10f82c, 0x00100000, data);
454
455         data = 0x00000000;
456         if (next->bios.ramcfg_11_08_08)
457                 data |= 0x00002000;
458         if (next->bios.ramcfg_11_08_04)
459                 data |= 0x00001000;
460         if (next->bios.ramcfg_11_08_02)
461                 data |= 0x00004000;
462         ram_mask(fuc, 0x10f830, 0x00007000, data);
463
464         /* PFB timing */
465         ram_mask(fuc, 0x10f248, 0xffffffff, next->bios.timing[10]);
466         ram_mask(fuc, 0x10f290, 0xffffffff, next->bios.timing[0]);
467         ram_mask(fuc, 0x10f294, 0xffffffff, next->bios.timing[1]);
468         ram_mask(fuc, 0x10f298, 0xffffffff, next->bios.timing[2]);
469         ram_mask(fuc, 0x10f29c, 0xffffffff, next->bios.timing[3]);
470         ram_mask(fuc, 0x10f2a0, 0xffffffff, next->bios.timing[4]);
471         ram_mask(fuc, 0x10f2a4, 0xffffffff, next->bios.timing[5]);
472         ram_mask(fuc, 0x10f2a8, 0xffffffff, next->bios.timing[6]);
473         ram_mask(fuc, 0x10f2ac, 0xffffffff, next->bios.timing[7]);
474         ram_mask(fuc, 0x10f2cc, 0xffffffff, next->bios.timing[8]);
475         ram_mask(fuc, 0x10f2e8, 0xffffffff, next->bios.timing[9]);
476
477         data = mask = 0x00000000;
478         if (NOTE00(ramcfg_02_03 != 0)) {
479                 data |= (next->bios.ramcfg_11_02_03) << 8;
480                 mask |= 0x00000300;
481         }
482         if (NOTE00(ramcfg_01_10)) {
483                 if (next->bios.ramcfg_11_01_10)
484                         data |= 0x70000000;
485                 mask |= 0x70000000;
486         }
487         ram_mask(fuc, 0x10f604, mask, data);
488
489         data = mask = 0x00000000;
490         if (NOTE00(timing_30_07 != 0)) {
491                 data |= (next->bios.timing_20_30_07) << 28;
492                 mask |= 0x70000000;
493         }
494         if (NOTE00(ramcfg_01_01)) {
495                 if (next->bios.ramcfg_11_01_01)
496                         data |= 0x00000100;
497                 mask |= 0x00000100;
498         }
499         ram_mask(fuc, 0x10f614, mask, data);
500
501         data = mask = 0x00000000;
502         if (NOTE00(timing_30_07 != 0)) {
503                 data |= (next->bios.timing_20_30_07) << 28;
504                 mask |= 0x70000000;
505         }
506         if (NOTE00(ramcfg_01_02)) {
507                 if (next->bios.ramcfg_11_01_02)
508                         data |= 0x00000100;
509                 mask |= 0x00000100;
510         }
511         ram_mask(fuc, 0x10f610, mask, data);
512
513         mask = 0x33f00000;
514         data = 0x00000000;
515         if (!(next->bios.ramcfg_11_01_04))
516                 data |= 0x20200000;
517         if (!(next->bios.ramcfg_11_07_80))
518                 data |= 0x12800000;
519         /*XXX: see note above about there probably being some condition
520          *     for the 10f824 stuff that uses ramcfg 3...
521          */
522         if ( (next->bios.ramcfg_11_03_f0)) {
523                 if (next->bios.rammap_11_08_0c) {
524                         if (!(next->bios.ramcfg_11_07_80))
525                                 mask |= 0x00000020;
526                         else
527                                 data |= 0x00000020;
528                         mask |= 0x00000004;
529                 }
530         } else {
531                 mask |= 0x40000020;
532                 data |= 0x00000004;
533         }
534
535         ram_mask(fuc, 0x10f808, mask, data);
536
537         ram_wr32(fuc, 0x10f870, 0x11111111 * next->bios.ramcfg_11_03_0f);
538
539         data = mask = 0x00000000;
540         if (NOTE00(ramcfg_02_03 != 0)) {
541                 data |= next->bios.ramcfg_11_02_03;
542                 mask |= 0x00000003;
543         }
544         if (NOTE00(ramcfg_01_10)) {
545                 if (next->bios.ramcfg_11_01_10)
546                         data |= 0x00000004;
547                 mask |= 0x00000004;
548         }
549
550         if ((ram_mask(fuc, 0x100770, mask, data) & mask & 4) != (data & 4)) {
551                 ram_mask(fuc, 0x100750, 0x00000008, 0x00000008);
552                 ram_wr32(fuc, 0x100710, 0x00000000);
553                 ram_wait(fuc, 0x100710, 0x80000000, 0x80000000, 200000);
554         }
555
556         data = (next->bios.timing_20_30_07) << 8;
557         if (next->bios.ramcfg_11_01_01)
558                 data |= 0x80000000;
559         ram_mask(fuc, 0x100778, 0x00000700, data);
560
561         ram_mask(fuc, 0x10f250, 0x000003f0, next->bios.timing_20_2c_003f << 4);
562         ram_mask(fuc, 0x10f24c, 0x7f000000, next->bios.timing_20_2c_1fc0 << 24);
563         ram_mask(fuc, 0x10f224, 0x001f0000, next->bios.timing_20_30_f8 << 16);
564
565         ram_mask(fuc, 0x10fec4, 0x041e0f07, next->bios.timing_20_31_0800 << 26 |
566                                             next->bios.timing_20_31_0780 << 17 |
567                                             next->bios.timing_20_31_0078 << 8 |
568                                             next->bios.timing_20_31_0007);
569         ram_mask(fuc, 0x10fec8, 0x00000027, next->bios.timing_20_31_8000 << 5 |
570                                             next->bios.timing_20_31_7000);
571
572         ram_wr32(fuc, 0x10f090, 0x4000007e);
573         ram_nsec(fuc, 2000);
574         ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
575         ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
576         ram_wr32(fuc, 0x10f210, 0x80000000); /* REFRESH_AUTO = 1 */
577
578         if ((next->bios.ramcfg_11_08_10) && (ram->mode == 2) /*XXX*/) {
579                 u32 temp = ram_mask(fuc, 0x10f294, 0xff000000, 0x24000000);
580                 nve0_ram_train(fuc, 0xbc0e0000, 0xa4010000); /*XXX*/
581                 ram_nsec(fuc, 1000);
582                 ram_wr32(fuc, 0x10f294, temp);
583         }
584
585         ram_mask(fuc, mr[3], 0xfff, ram->base.mr[3]);
586         ram_wr32(fuc, mr[0], ram->base.mr[0]);
587         ram_mask(fuc, mr[8], 0xfff, ram->base.mr[8]);
588         ram_nsec(fuc, 1000);
589         ram_mask(fuc, mr[1], 0xfff, ram->base.mr[1]);
590         ram_mask(fuc, mr[5], 0xfff, ram->base.mr[5] & ~0x004); /* LP3 later */
591         ram_mask(fuc, mr[6], 0xfff, ram->base.mr[6]);
592         ram_mask(fuc, mr[7], 0xfff, ram->base.mr[7]);
593
594         if (vc == 0 && ram_have(fuc, gpio2E)) {
595                 u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[0]);
596                 if (temp != ram_rd32(fuc, gpio2E)) {
597                         ram_wr32(fuc, gpiotrig, 1);
598                         ram_nsec(fuc, 20000);
599                 }
600         }
601
602         ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
603         ram_wr32(fuc, 0x10f318, 0x00000001); /* NOP? */
604         ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
605         ram_nsec(fuc, 1000);
606         ram_nuts(ram, 0x10f200, 0x00808800, 0x00000000, 0x00808800);
607
608         data  = ram_rd32(fuc, 0x10f978);
609         data &= ~0x00046144;
610         data |=  0x0000000b;
611         if (!(next->bios.ramcfg_11_07_08)) {
612                 if (!(next->bios.ramcfg_11_07_04))
613                         data |= 0x0000200c;
614                 else
615                         data |= 0x00000000;
616         } else {
617                 data |= 0x00040044;
618         }
619         ram_wr32(fuc, 0x10f978, data);
620
621         if (ram->mode == 1) {
622                 data = ram_rd32(fuc, 0x10f830) | 0x00000001;
623                 ram_wr32(fuc, 0x10f830, data);
624         }
625
626         if (!(next->bios.ramcfg_11_07_08)) {
627                 data = 0x88020000;
628                 if ( (next->bios.ramcfg_11_07_04))
629                         data |= 0x10000000;
630                 if (!(next->bios.rammap_11_08_10))
631                         data |= 0x00080000;
632         } else {
633                 data = 0xa40e0000;
634         }
635         nve0_ram_train(fuc, 0xbc0f0000, data);
636         if (1) /* XXX: not always? */
637                 ram_nsec(fuc, 1000);
638
639         if (ram->mode == 2) { /*XXX*/
640                 ram_mask(fuc, 0x10f800, 0x00000004, 0x00000004);
641         }
642
643         /* LP3 */
644         if (ram_mask(fuc, mr[5], 0x004, ram->base.mr[5]) != ram->base.mr[5])
645                 ram_nsec(fuc, 1000);
646
647         if (ram->mode != 2) {
648                 ram_mask(fuc, 0x10f830, 0x01000000, 0x01000000);
649                 ram_mask(fuc, 0x10f830, 0x01000000, 0x00000000);
650         }
651
652         if (next->bios.ramcfg_11_07_02)
653                 nve0_ram_train(fuc, 0x80020000, 0x01000000);
654
655         ram_wr32(fuc, 0x62c000, 0x0f0f0f00);
656
657         if (next->bios.rammap_11_08_01)
658                 data = 0x00000800;
659         else
660                 data = 0x00000000;
661         ram_mask(fuc, 0x10f200, 0x00000800, data);
662         ram_nuts(ram, 0x10f200, 0x00808800, data, 0x00808800);
663         return 0;
664 }
665
666 /*******************************************************************************
667  * DDR3
668  ******************************************************************************/
669
670 static int
671 nve0_ram_calc_sddr3(struct nouveau_fb *pfb, u32 freq)
672 {
673         struct nve0_ram *ram = (void *)pfb->ram;
674         struct nve0_ramfuc *fuc = &ram->fuc;
675         const u32 rcoef = ((  ram->P1 << 16) | (ram->N1 << 8) | ram->M1);
676         const u32 runk0 = ram->fN1 << 16;
677         const u32 runk1 = ram->fN1;
678         struct nouveau_ram_data *next = ram->base.next;
679         int vc = !(next->bios.ramcfg_11_02_08);
680         int mv = !(next->bios.ramcfg_11_02_04);
681         u32 mask, data;
682
683         ram_mask(fuc, 0x10f808, 0x40000000, 0x40000000);
684         ram_wr32(fuc, 0x62c000, 0x0f0f0000);
685
686         if (vc == 1 && ram_have(fuc, gpio2E)) {
687                 u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[1]);
688                 if (temp != ram_rd32(fuc, gpio2E)) {
689                         ram_wr32(fuc, gpiotrig, 1);
690                         ram_nsec(fuc, 20000);
691                 }
692         }
693
694         ram_mask(fuc, 0x10f200, 0x00000800, 0x00000000);
695         if ((next->bios.ramcfg_11_03_f0))
696                 ram_mask(fuc, 0x10f808, 0x04000000, 0x04000000);
697
698         ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
699         ram_wr32(fuc, 0x10f210, 0x00000000); /* REFRESH_AUTO = 0 */
700         ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
701         ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
702         ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
703         ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
704         ram_nsec(fuc, 1000);
705
706         ram_wr32(fuc, 0x10f090, 0x00000060);
707         ram_wr32(fuc, 0x10f090, 0xc000007e);
708
709         /*XXX: there does appear to be some kind of condition here, simply
710          *     modifying these bits in the vbios from the default pl0
711          *     entries shows no change.  however, the data does appear to
712          *     be correct and may be required for the transition back
713          */
714         mask = 0x00010000;
715         data = 0x00010000;
716
717         if (1) {
718                 mask |= 0x800807e0;
719                 data |= 0x800807e0;
720                 switch (next->bios.ramcfg_11_03_c0) {
721                 case 3: data &= ~0x00000040; break;
722                 case 2: data &= ~0x00000100; break;
723                 case 1: data &= ~0x80000000; break;
724                 case 0: data &= ~0x00000400; break;
725                 }
726
727                 switch (next->bios.ramcfg_11_03_30) {
728                 case 3: data &= ~0x00000020; break;
729                 case 2: data &= ~0x00000080; break;
730                 case 1: data &= ~0x00080000; break;
731                 case 0: data &= ~0x00000200; break;
732                 }
733         }
734
735         if (next->bios.ramcfg_11_02_80)
736                 mask |= 0x03000000;
737         if (next->bios.ramcfg_11_02_40)
738                 mask |= 0x00002000;
739         if (next->bios.ramcfg_11_07_10)
740                 mask |= 0x00004000;
741         if (next->bios.ramcfg_11_07_08)
742                 mask |= 0x00000003;
743         else
744                 mask |= 0x14000000;
745         ram_mask(fuc, 0x10f824, mask, data);
746
747         ram_mask(fuc, 0x132040, 0x00010000, 0x00000000);
748
749         ram_mask(fuc, 0x1373f4, 0x00000000, 0x00010010);
750         data  = ram_rd32(fuc, 0x1373ec) & ~0x00030000;
751         data |= (next->bios.ramcfg_11_03_30) << 12;
752         ram_wr32(fuc, 0x1373ec, data);
753         ram_mask(fuc, 0x1373f4, 0x00000003, 0x00000000);
754         ram_mask(fuc, 0x1373f4, 0x00000010, 0x00000000);
755
756         /* (re)program refpll, if required */
757         if ((ram_rd32(fuc, 0x132024) & 0xffffffff) != rcoef ||
758             (ram_rd32(fuc, 0x132034) & 0x0000ffff) != runk1) {
759                 ram_mask(fuc, 0x132000, 0x00000001, 0x00000000);
760                 ram_mask(fuc, 0x132020, 0x00000001, 0x00000000);
761                 ram_wr32(fuc, 0x137320, 0x00000000);
762                 ram_mask(fuc, 0x132030, 0xffff0000, runk0);
763                 ram_mask(fuc, 0x132034, 0x0000ffff, runk1);
764                 ram_wr32(fuc, 0x132024, rcoef);
765                 ram_mask(fuc, 0x132028, 0x00080000, 0x00080000);
766                 ram_mask(fuc, 0x132020, 0x00000001, 0x00000001);
767                 ram_wait(fuc, 0x137390, 0x00020000, 0x00020000, 64000);
768                 ram_mask(fuc, 0x132028, 0x00080000, 0x00000000);
769         }
770
771         ram_mask(fuc, 0x1373f4, 0x00000010, 0x00000010);
772         ram_mask(fuc, 0x1373f4, 0x00000003, 0x00000001);
773         ram_mask(fuc, 0x1373f4, 0x00010000, 0x00000000);
774
775         if (ram_have(fuc, gpioMV)) {
776                 u32 temp  = ram_mask(fuc, gpioMV, 0x3000, fuc->r_funcMV[mv]);
777                 if (temp != ram_rd32(fuc, gpioMV)) {
778                         ram_wr32(fuc, gpiotrig, 1);
779                         ram_nsec(fuc, 64000);
780                 }
781         }
782
783         if ( (next->bios.ramcfg_11_02_40) ||
784              (next->bios.ramcfg_11_07_10)) {
785                 ram_mask(fuc, 0x132040, 0x00010000, 0x00010000);
786                 ram_nsec(fuc, 20000);
787         }
788
789         if (ram->mode != 2) /*XXX*/ {
790                 if (next->bios.ramcfg_11_07_40)
791                         ram_mask(fuc, 0x10f670, 0x80000000, 0x80000000);
792         }
793
794         ram_wr32(fuc, 0x10f65c, 0x00000011 * next->bios.rammap_11_11_0c);
795         ram_wr32(fuc, 0x10f6b8, 0x01010101 * next->bios.ramcfg_11_09);
796         ram_wr32(fuc, 0x10f6bc, 0x01010101 * next->bios.ramcfg_11_09);
797
798         mask = 0x00010000;
799         data = 0x00000000;
800         if (!(next->bios.ramcfg_11_02_80))
801                 data |= 0x03000000;
802         if (!(next->bios.ramcfg_11_02_40))
803                 data |= 0x00002000;
804         if (!(next->bios.ramcfg_11_07_10))
805                 data |= 0x00004000;
806         if (!(next->bios.ramcfg_11_07_08))
807                 data |= 0x00000003;
808         else
809                 data |= 0x14000000;
810         ram_mask(fuc, 0x10f824, mask, data);
811         ram_nsec(fuc, 1000);
812
813         if (next->bios.ramcfg_11_08_01)
814                 data = 0x00100000;
815         else
816                 data = 0x00000000;
817         ram_mask(fuc, 0x10f82c, 0x00100000, data);
818
819         /* PFB timing */
820         ram_mask(fuc, 0x10f248, 0xffffffff, next->bios.timing[10]);
821         ram_mask(fuc, 0x10f290, 0xffffffff, next->bios.timing[0]);
822         ram_mask(fuc, 0x10f294, 0xffffffff, next->bios.timing[1]);
823         ram_mask(fuc, 0x10f298, 0xffffffff, next->bios.timing[2]);
824         ram_mask(fuc, 0x10f29c, 0xffffffff, next->bios.timing[3]);
825         ram_mask(fuc, 0x10f2a0, 0xffffffff, next->bios.timing[4]);
826         ram_mask(fuc, 0x10f2a4, 0xffffffff, next->bios.timing[5]);
827         ram_mask(fuc, 0x10f2a8, 0xffffffff, next->bios.timing[6]);
828         ram_mask(fuc, 0x10f2ac, 0xffffffff, next->bios.timing[7]);
829         ram_mask(fuc, 0x10f2cc, 0xffffffff, next->bios.timing[8]);
830         ram_mask(fuc, 0x10f2e8, 0xffffffff, next->bios.timing[9]);
831
832         mask = 0x33f00000;
833         data = 0x00000000;
834         if (!(next->bios.ramcfg_11_01_04))
835                 data |= 0x20200000;
836         if (!(next->bios.ramcfg_11_07_80))
837                 data |= 0x12800000;
838         /*XXX: see note above about there probably being some condition
839          *     for the 10f824 stuff that uses ramcfg 3...
840          */
841         if ( (next->bios.ramcfg_11_03_f0)) {
842                 if (next->bios.rammap_11_08_0c) {
843                         if (!(next->bios.ramcfg_11_07_80))
844                                 mask |= 0x00000020;
845                         else
846                                 data |= 0x00000020;
847                         mask |= 0x08000004;
848                 }
849                 data |= 0x04000000;
850         } else {
851                 mask |= 0x44000020;
852                 data |= 0x08000004;
853         }
854
855         ram_mask(fuc, 0x10f808, mask, data);
856
857         ram_wr32(fuc, 0x10f870, 0x11111111 * next->bios.ramcfg_11_03_0f);
858
859         ram_mask(fuc, 0x10f250, 0x000003f0, next->bios.timing_20_2c_003f << 4);
860
861         data = (next->bios.timing[10] & 0x7f000000) >> 24;
862         if ( next->bios.timing_20_2c_1fc0 > data)
863                 data = next->bios.timing_20_2c_1fc0;
864         ram_mask(fuc, 0x10f24c, 0x7f000000, data << 24);
865
866         ram_mask(fuc, 0x10f224, 0x001f0000, next->bios.timing_20_30_f8);
867
868         ram_wr32(fuc, 0x10f090, 0x4000007f);
869         ram_nsec(fuc, 1000);
870
871         ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
872         ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
873         ram_wr32(fuc, 0x10f210, 0x80000000); /* REFRESH_AUTO = 1 */
874         ram_nsec(fuc, 1000);
875
876         ram_nuke(fuc, mr[0]);
877         ram_mask(fuc, mr[0], 0x100, 0x100);
878         ram_mask(fuc, mr[0], 0x100, 0x000);
879
880         ram_mask(fuc, mr[2], 0xfff, ram->base.mr[2]);
881         ram_wr32(fuc, mr[0], ram->base.mr[0]);
882         ram_nsec(fuc, 1000);
883
884         ram_nuke(fuc, mr[0]);
885         ram_mask(fuc, mr[0], 0x100, 0x100);
886         ram_mask(fuc, mr[0], 0x100, 0x000);
887
888         if (vc == 0 && ram_have(fuc, gpio2E)) {
889                 u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[0]);
890                 if (temp != ram_rd32(fuc, gpio2E)) {
891                         ram_wr32(fuc, gpiotrig, 1);
892                         ram_nsec(fuc, 20000);
893                 }
894         }
895
896         if (ram->mode != 2) {
897                 ram_mask(fuc, 0x10f830, 0x01000000, 0x01000000);
898                 ram_mask(fuc, 0x10f830, 0x01000000, 0x00000000);
899         }
900
901         ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
902         ram_wr32(fuc, 0x10f318, 0x00000001); /* NOP? */
903         ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
904         ram_nsec(fuc, 1000);
905
906         ram_wr32(fuc, 0x62c000, 0x0f0f0f00);
907
908         if (next->bios.rammap_11_08_01)
909                 data = 0x00000800;
910         else
911                 data = 0x00000000;
912         ram_mask(fuc, 0x10f200, 0x00000800, data);
913         return 0;
914 }
915
916 /*******************************************************************************
917  * main hooks
918  ******************************************************************************/
919
920 static int
921 nve0_ram_calc_data(struct nouveau_fb *pfb, u32 freq,
922                    struct nouveau_ram_data *data)
923 {
924         struct nouveau_bios *bios = nouveau_bios(pfb);
925         struct nve0_ram *ram = (void *)pfb->ram;
926         u8 strap, cnt, len;
927
928         /* lookup memory config data relevant to the target frequency */
929         ram->base.rammap.data = nvbios_rammapEp(bios, freq / 1000,
930                                                &ram->base.rammap.version,
931                                                &ram->base.rammap.size,
932                                                &cnt, &len, &data->bios);
933         if (!ram->base.rammap.data || ram->base.rammap.version != 0x11 ||
934              ram->base.rammap.size < 0x09) {
935                 nv_error(pfb, "invalid/missing rammap entry\n");
936                 return -EINVAL;
937         }
938
939         /* locate specific data set for the attached memory */
940         ram->base.ramcfg.data = nvbios_rammapSp(bios, ram->base.rammap.data,
941                                                 ram->base.rammap.version,
942                                                 ram->base.rammap.size, cnt, len,
943                                                 nvbios_ramcfg_index(bios),
944                                                 &ram->base.ramcfg.version,
945                                                 &ram->base.ramcfg.size,
946                                                 &data->bios);
947         if (!ram->base.ramcfg.data || ram->base.ramcfg.version != 0x11 ||
948              ram->base.ramcfg.size < 0x08) {
949                 nv_error(pfb, "invalid/missing ramcfg entry\n");
950                 return -EINVAL;
951         }
952
953         /* lookup memory timings, if bios says they're present */
954         strap = nv_ro08(bios, ram->base.ramcfg.data + 0x00);
955         if (strap != 0xff) {
956                 ram->base.timing.data =
957                         nvbios_timingEp(bios, strap, &ram->base.timing.version,
958                                        &ram->base.timing.size, &cnt, &len,
959                                        &data->bios);
960                 if (!ram->base.timing.data ||
961                      ram->base.timing.version != 0x20 ||
962                      ram->base.timing.size < 0x33) {
963                         nv_error(pfb, "invalid/missing timing entry\n");
964                         return -EINVAL;
965                 }
966         } else {
967                 ram->base.timing.data = 0;
968         }
969
970         data->freq = freq;
971         return 0;
972 }
973
974 static int
975 nve0_ram_calc_xits(struct nouveau_fb *pfb, struct nouveau_ram_data *next)
976 {
977         struct nve0_ram *ram = (void *)pfb->ram;
978         struct nve0_ramfuc *fuc = &ram->fuc;
979         int refclk, i;
980         int ret;
981
982         ret = ram_init(fuc, pfb);
983         if (ret)
984                 return ret;
985
986         ram->mode = (next->freq > fuc->refpll.vco1.max_freq) ? 2 : 1;
987         ram->from = ram_rd32(fuc, 0x1373f4) & 0x0000000f;
988
989         /* XXX: this is *not* what nvidia do.  on fermi nvidia generally
990          * select, based on some unknown condition, one of the two possible
991          * reference frequencies listed in the vbios table for mempll and
992          * program refpll to that frequency.
993          *
994          * so far, i've seen very weird values being chosen by nvidia on
995          * kepler boards, no idea how/why they're chosen.
996          */
997         refclk = next->freq;
998         if (ram->mode == 2)
999                 refclk = fuc->mempll.refclk;
1000
1001         /* calculate refpll coefficients */
1002         ret = nva3_pll_calc(nv_subdev(pfb), &fuc->refpll, refclk, &ram->N1,
1003                            &ram->fN1, &ram->M1, &ram->P1);
1004         fuc->mempll.refclk = ret;
1005         if (ret <= 0) {
1006                 nv_error(pfb, "unable to calc refpll\n");
1007                 return -EINVAL;
1008         }
1009
1010         /* calculate mempll coefficients, if we're using it */
1011         if (ram->mode == 2) {
1012                 /* post-divider doesn't work... the reg takes the values but
1013                  * appears to completely ignore it.  there *is* a bit at
1014                  * bit 28 that appears to divide the clock by 2 if set.
1015                  */
1016                 fuc->mempll.min_p = 1;
1017                 fuc->mempll.max_p = 2;
1018
1019                 ret = nva3_pll_calc(nv_subdev(pfb), &fuc->mempll, next->freq,
1020                                    &ram->N2, NULL, &ram->M2, &ram->P2);
1021                 if (ret <= 0) {
1022                         nv_error(pfb, "unable to calc mempll\n");
1023                         return -EINVAL;
1024                 }
1025         }
1026
1027         for (i = 0; i < ARRAY_SIZE(fuc->r_mr); i++) {
1028                 if (ram_have(fuc, mr[i]))
1029                         ram->base.mr[i] = ram_rd32(fuc, mr[i]);
1030         }
1031         ram->base.freq = next->freq;
1032
1033         switch (ram->base.type) {
1034         case NV_MEM_TYPE_DDR3:
1035                 ret = nouveau_sddr3_calc(&ram->base);
1036                 if (ret == 0)
1037                         ret = nve0_ram_calc_sddr3(pfb, next->freq);
1038                 break;
1039         case NV_MEM_TYPE_GDDR5:
1040                 ret = nouveau_gddr5_calc(&ram->base, ram->pnuts != 0);
1041                 if (ret == 0)
1042                         ret = nve0_ram_calc_gddr5(pfb, next->freq);
1043                 break;
1044         default:
1045                 ret = -ENOSYS;
1046                 break;
1047         }
1048
1049         return ret;
1050 }
1051
1052 static int
1053 nve0_ram_calc(struct nouveau_fb *pfb, u32 freq)
1054 {
1055         struct nouveau_clock *clk = nouveau_clock(pfb);
1056         struct nve0_ram *ram = (void *)pfb->ram;
1057         struct nouveau_ram_data *xits = &ram->base.xition;
1058         struct nouveau_ram_data *copy;
1059         int ret;
1060
1061         if (ram->base.next == NULL) {
1062                 ret = nve0_ram_calc_data(pfb, clk->read(clk, nv_clk_src_mem),
1063                                         &ram->base.former);
1064                 if (ret)
1065                         return ret;
1066
1067                 ret = nve0_ram_calc_data(pfb, freq, &ram->base.target);
1068                 if (ret)
1069                         return ret;
1070
1071                 if (ram->base.target.freq < ram->base.former.freq) {
1072                         *xits = ram->base.target;
1073                         copy = &ram->base.former;
1074                 } else {
1075                         *xits = ram->base.former;
1076                         copy = &ram->base.target;
1077                 }
1078
1079                 xits->bios.ramcfg_11_02_04 = copy->bios.ramcfg_11_02_04;
1080                 xits->bios.ramcfg_11_02_03 = copy->bios.ramcfg_11_02_03;
1081                 xits->bios.timing_20_30_07 = copy->bios.timing_20_30_07;
1082
1083                 ram->base.next = &ram->base.target;
1084                 if (memcmp(xits, &ram->base.former, sizeof(xits->bios)))
1085                         ram->base.next = &ram->base.xition;
1086         } else {
1087                 BUG_ON(ram->base.next != &ram->base.xition);
1088                 ram->base.next = &ram->base.target;
1089         }
1090
1091         return nve0_ram_calc_xits(pfb, ram->base.next);
1092 }
1093
1094 static int
1095 nve0_ram_prog(struct nouveau_fb *pfb)
1096 {
1097         struct nouveau_device *device = nv_device(pfb);
1098         struct nve0_ram *ram = (void *)pfb->ram;
1099         struct nve0_ramfuc *fuc = &ram->fuc;
1100         ram_exec(fuc, nouveau_boolopt(device->cfgopt, "NvMemExec", false));
1101         return (ram->base.next == &ram->base.xition);
1102 }
1103
1104 static void
1105 nve0_ram_tidy(struct nouveau_fb *pfb)
1106 {
1107         struct nve0_ram *ram = (void *)pfb->ram;
1108         struct nve0_ramfuc *fuc = &ram->fuc;
1109         ram->base.next = NULL;
1110         ram_exec(fuc, false);
1111 }
1112
1113 static int
1114 nve0_ram_init(struct nouveau_object *object)
1115 {
1116         struct nouveau_fb *pfb = (void *)object->parent;
1117         struct nve0_ram *ram   = (void *)object;
1118         struct nouveau_bios *bios = nouveau_bios(pfb);
1119         static const u8  train0[] = {
1120                 0x00, 0xff, 0xff, 0x00, 0xff, 0x00,
1121                 0x00, 0xff, 0xff, 0x00, 0xff, 0x00,
1122         };
1123         static const u32 train1[] = {
1124                 0x00000000, 0xffffffff,
1125                 0x55555555, 0xaaaaaaaa,
1126                 0x33333333, 0xcccccccc,
1127                 0xf0f0f0f0, 0x0f0f0f0f,
1128                 0x00ff00ff, 0xff00ff00,
1129                 0x0000ffff, 0xffff0000,
1130         };
1131         u8  ver, hdr, cnt, len, snr, ssz;
1132         u32 data, save;
1133         int ret, i;
1134
1135         ret = nouveau_ram_init(&ram->base);
1136         if (ret)
1137                 return ret;
1138
1139         /* run a bunch of tables from rammap table.  there's actually
1140          * individual pointers for each rammap entry too, but, nvidia
1141          * seem to just run the last two entries' scripts early on in
1142          * their init, and never again.. we'll just run 'em all once
1143          * for now.
1144          *
1145          * i strongly suspect that each script is for a separate mode
1146          * (likely selected by 0x10f65c's lower bits?), and the
1147          * binary driver skips the one that's already been setup by
1148          * the init tables.
1149          */
1150         data = nvbios_rammapTe(bios, &ver, &hdr, &cnt, &len, &snr, &ssz);
1151         if (!data || hdr < 0x15)
1152                 return -EINVAL;
1153
1154         cnt  = nv_ro08(bios, data + 0x14); /* guess at count */
1155         data = nv_ro32(bios, data + 0x10); /* guess u32... */
1156         save = nv_rd32(pfb, 0x10f65c);
1157         for (i = 0; i < cnt; i++) {
1158                 nv_mask(pfb, 0x10f65c, 0x000000f0, i << 4);
1159                 nvbios_exec(&(struct nvbios_init) {
1160                                 .subdev = nv_subdev(pfb),
1161                                 .bios = bios,
1162                                 .offset = nv_ro32(bios, data), /* guess u32 */
1163                                 .execute = 1,
1164                             });
1165                 data += 4;
1166         }
1167         nv_wr32(pfb, 0x10f65c, save);
1168
1169         switch (ram->base.type) {
1170         case NV_MEM_TYPE_GDDR5:
1171                 for (i = 0; i < 0x30; i++) {
1172                         nv_wr32(pfb, 0x10f968, 0x00000000 | (i << 8));
1173                         nv_wr32(pfb, 0x10f920, 0x00000000 | train0[i % 12]);
1174                         nv_wr32(pfb, 0x10f918,              train1[i % 12]);
1175                         nv_wr32(pfb, 0x10f920, 0x00000100 | train0[i % 12]);
1176                         nv_wr32(pfb, 0x10f918,              train1[i % 12]);
1177
1178                         nv_wr32(pfb, 0x10f96c, 0x00000000 | (i << 8));
1179                         nv_wr32(pfb, 0x10f924, 0x00000000 | train0[i % 12]);
1180                         nv_wr32(pfb, 0x10f91c,              train1[i % 12]);
1181                         nv_wr32(pfb, 0x10f924, 0x00000100 | train0[i % 12]);
1182                         nv_wr32(pfb, 0x10f91c,              train1[i % 12]);
1183                 }
1184
1185                 for (i = 0; i < 0x100; i++) {
1186                         nv_wr32(pfb, 0x10f968, i);
1187                         nv_wr32(pfb, 0x10f900, train1[2 + (i & 1)]);
1188                 }
1189
1190                 for (i = 0; i < 0x100; i++) {
1191                         nv_wr32(pfb, 0x10f96c, i);
1192                         nv_wr32(pfb, 0x10f900, train1[2 + (i & 1)]);
1193                 }
1194                 break;
1195         default:
1196                 break;
1197         }
1198
1199         return 0;
1200 }
1201
1202 static int
1203 nve0_ram_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
1204               struct nouveau_oclass *oclass, void *data, u32 size,
1205               struct nouveau_object **pobject)
1206 {
1207         struct nouveau_fb *pfb = nouveau_fb(parent);
1208         struct nouveau_bios *bios = nouveau_bios(pfb);
1209         struct nouveau_gpio *gpio = nouveau_gpio(pfb);
1210         struct dcb_gpio_func func;
1211         struct nve0_ram *ram;
1212         int ret, i;
1213         u32 tmp;
1214
1215         ret = nvc0_ram_create(parent, engine, oclass, &ram);
1216         *pobject = nv_object(ram);
1217         if (ret)
1218                 return ret;
1219
1220         switch (ram->base.type) {
1221         case NV_MEM_TYPE_DDR3:
1222         case NV_MEM_TYPE_GDDR5:
1223                 ram->base.calc = nve0_ram_calc;
1224                 ram->base.prog = nve0_ram_prog;
1225                 ram->base.tidy = nve0_ram_tidy;
1226                 break;
1227         default:
1228                 nv_warn(pfb, "reclocking of this RAM type is unsupported\n");
1229                 break;
1230         }
1231
1232         /* calculate a mask of differently configured memory partitions,
1233          * because, of course reclocking wasn't complicated enough
1234          * already without having to treat some of them differently to
1235          * the others....
1236          */
1237         ram->parts = nv_rd32(pfb, 0x022438);
1238         ram->pmask = nv_rd32(pfb, 0x022554);
1239         ram->pnuts = 0;
1240         for (i = 0, tmp = 0; i < ram->parts; i++) {
1241                 if (!(ram->pmask & (1 << i))) {
1242                         u32 cfg1 = nv_rd32(pfb, 0x110204 + (i * 0x1000));
1243                         if (tmp && tmp != cfg1) {
1244                                 ram->pnuts |= (1 << i);
1245                                 continue;
1246                         }
1247                         tmp = cfg1;
1248                 }
1249         }
1250
1251         // parse bios data for both pll's
1252         ret = nvbios_pll_parse(bios, 0x0c, &ram->fuc.refpll);
1253         if (ret) {
1254                 nv_error(pfb, "mclk refpll data not found\n");
1255                 return ret;
1256         }
1257
1258         ret = nvbios_pll_parse(bios, 0x04, &ram->fuc.mempll);
1259         if (ret) {
1260                 nv_error(pfb, "mclk pll data not found\n");
1261                 return ret;
1262         }
1263
1264         ret = gpio->find(gpio, 0, 0x18, DCB_GPIO_UNUSED, &func);
1265         if (ret == 0) {
1266                 ram->fuc.r_gpioMV = ramfuc_reg(0x00d610 + (func.line * 0x04));
1267                 ram->fuc.r_funcMV[0] = (func.log[0] ^ 2) << 12;
1268                 ram->fuc.r_funcMV[1] = (func.log[1] ^ 2) << 12;
1269         }
1270
1271         ret = gpio->find(gpio, 0, 0x2e, DCB_GPIO_UNUSED, &func);
1272         if (ret == 0) {
1273                 ram->fuc.r_gpio2E = ramfuc_reg(0x00d610 + (func.line * 0x04));
1274                 ram->fuc.r_func2E[0] = (func.log[0] ^ 2) << 12;
1275                 ram->fuc.r_func2E[1] = (func.log[1] ^ 2) << 12;
1276         }
1277
1278         ram->fuc.r_gpiotrig = ramfuc_reg(0x00d604);
1279
1280         ram->fuc.r_0x132020 = ramfuc_reg(0x132020);
1281         ram->fuc.r_0x132028 = ramfuc_reg(0x132028);
1282         ram->fuc.r_0x132024 = ramfuc_reg(0x132024);
1283         ram->fuc.r_0x132030 = ramfuc_reg(0x132030);
1284         ram->fuc.r_0x132034 = ramfuc_reg(0x132034);
1285         ram->fuc.r_0x132000 = ramfuc_reg(0x132000);
1286         ram->fuc.r_0x132004 = ramfuc_reg(0x132004);
1287         ram->fuc.r_0x132040 = ramfuc_reg(0x132040);
1288
1289         ram->fuc.r_0x10f248 = ramfuc_reg(0x10f248);
1290         ram->fuc.r_0x10f290 = ramfuc_reg(0x10f290);
1291         ram->fuc.r_0x10f294 = ramfuc_reg(0x10f294);
1292         ram->fuc.r_0x10f298 = ramfuc_reg(0x10f298);
1293         ram->fuc.r_0x10f29c = ramfuc_reg(0x10f29c);
1294         ram->fuc.r_0x10f2a0 = ramfuc_reg(0x10f2a0);
1295         ram->fuc.r_0x10f2a4 = ramfuc_reg(0x10f2a4);
1296         ram->fuc.r_0x10f2a8 = ramfuc_reg(0x10f2a8);
1297         ram->fuc.r_0x10f2ac = ramfuc_reg(0x10f2ac);
1298         ram->fuc.r_0x10f2cc = ramfuc_reg(0x10f2cc);
1299         ram->fuc.r_0x10f2e8 = ramfuc_reg(0x10f2e8);
1300         ram->fuc.r_0x10f250 = ramfuc_reg(0x10f250);
1301         ram->fuc.r_0x10f24c = ramfuc_reg(0x10f24c);
1302         ram->fuc.r_0x10fec4 = ramfuc_reg(0x10fec4);
1303         ram->fuc.r_0x10fec8 = ramfuc_reg(0x10fec8);
1304         ram->fuc.r_0x10f604 = ramfuc_reg(0x10f604);
1305         ram->fuc.r_0x10f614 = ramfuc_reg(0x10f614);
1306         ram->fuc.r_0x10f610 = ramfuc_reg(0x10f610);
1307         ram->fuc.r_0x100770 = ramfuc_reg(0x100770);
1308         ram->fuc.r_0x100778 = ramfuc_reg(0x100778);
1309         ram->fuc.r_0x10f224 = ramfuc_reg(0x10f224);
1310
1311         ram->fuc.r_0x10f870 = ramfuc_reg(0x10f870);
1312         ram->fuc.r_0x10f698 = ramfuc_reg(0x10f698);
1313         ram->fuc.r_0x10f694 = ramfuc_reg(0x10f694);
1314         ram->fuc.r_0x10f6b8 = ramfuc_reg(0x10f6b8);
1315         ram->fuc.r_0x10f808 = ramfuc_reg(0x10f808);
1316         ram->fuc.r_0x10f670 = ramfuc_reg(0x10f670);
1317         ram->fuc.r_0x10f60c = ramfuc_reg(0x10f60c);
1318         ram->fuc.r_0x10f830 = ramfuc_reg(0x10f830);
1319         ram->fuc.r_0x1373ec = ramfuc_reg(0x1373ec);
1320         ram->fuc.r_0x10f800 = ramfuc_reg(0x10f800);
1321         ram->fuc.r_0x10f82c = ramfuc_reg(0x10f82c);
1322
1323         ram->fuc.r_0x10f978 = ramfuc_reg(0x10f978);
1324         ram->fuc.r_0x10f910 = ramfuc_reg(0x10f910);
1325         ram->fuc.r_0x10f914 = ramfuc_reg(0x10f914);
1326
1327         switch (ram->base.type) {
1328         case NV_MEM_TYPE_GDDR5:
1329                 ram->fuc.r_mr[0] = ramfuc_reg(0x10f300);
1330                 ram->fuc.r_mr[1] = ramfuc_reg(0x10f330);
1331                 ram->fuc.r_mr[2] = ramfuc_reg(0x10f334);
1332                 ram->fuc.r_mr[3] = ramfuc_reg(0x10f338);
1333                 ram->fuc.r_mr[4] = ramfuc_reg(0x10f33c);
1334                 ram->fuc.r_mr[5] = ramfuc_reg(0x10f340);
1335                 ram->fuc.r_mr[6] = ramfuc_reg(0x10f344);
1336                 ram->fuc.r_mr[7] = ramfuc_reg(0x10f348);
1337                 ram->fuc.r_mr[8] = ramfuc_reg(0x10f354);
1338                 ram->fuc.r_mr[15] = ramfuc_reg(0x10f34c);
1339                 break;
1340         case NV_MEM_TYPE_DDR3:
1341                 ram->fuc.r_mr[0] = ramfuc_reg(0x10f300);
1342                 ram->fuc.r_mr[2] = ramfuc_reg(0x10f320);
1343                 break;
1344         default:
1345                 break;
1346         }
1347
1348         ram->fuc.r_0x62c000 = ramfuc_reg(0x62c000);
1349         ram->fuc.r_0x10f200 = ramfuc_reg(0x10f200);
1350         ram->fuc.r_0x10f210 = ramfuc_reg(0x10f210);
1351         ram->fuc.r_0x10f310 = ramfuc_reg(0x10f310);
1352         ram->fuc.r_0x10f314 = ramfuc_reg(0x10f314);
1353         ram->fuc.r_0x10f318 = ramfuc_reg(0x10f318);
1354         ram->fuc.r_0x10f090 = ramfuc_reg(0x10f090);
1355         ram->fuc.r_0x10f69c = ramfuc_reg(0x10f69c);
1356         ram->fuc.r_0x10f824 = ramfuc_reg(0x10f824);
1357         ram->fuc.r_0x1373f0 = ramfuc_reg(0x1373f0);
1358         ram->fuc.r_0x1373f4 = ramfuc_reg(0x1373f4);
1359         ram->fuc.r_0x137320 = ramfuc_reg(0x137320);
1360         ram->fuc.r_0x10f65c = ramfuc_reg(0x10f65c);
1361         ram->fuc.r_0x10f6bc = ramfuc_reg(0x10f6bc);
1362         ram->fuc.r_0x100710 = ramfuc_reg(0x100710);
1363         ram->fuc.r_0x100750 = ramfuc_reg(0x100750);
1364         return 0;
1365 }
1366
1367 struct nouveau_oclass
1368 nve0_ram_oclass = {
1369         .handle = 0,
1370         .ofuncs = &(struct nouveau_ofuncs) {
1371                 .ctor = nve0_ram_ctor,
1372                 .dtor = _nouveau_ram_dtor,
1373                 .init = nve0_ram_init,
1374                 .fini = _nouveau_ram_fini,
1375         }
1376 };