drm/nouveau/devinit: bump priv ring timeouts before executing scripts
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / core / subdev / devinit / nv50.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/bios.h>
26 #include <subdev/bios/dcb.h>
27 #include <subdev/bios/disp.h>
28 #include <subdev/bios/init.h>
29 #include <subdev/ibus.h>
30 #include <subdev/vga.h>
31
32 #include "nv50.h"
33
34 int
35 nv50_devinit_pll_set(struct nouveau_devinit *devinit, u32 type, u32 freq)
36 {
37         struct nv50_devinit_priv *priv = (void *)devinit;
38         struct nouveau_bios *bios = nouveau_bios(priv);
39         struct nvbios_pll info;
40         int N1, M1, N2, M2, P;
41         int ret;
42
43         ret = nvbios_pll_parse(bios, type, &info);
44         if (ret) {
45                 nv_error(devinit, "failed to retrieve pll data, %d\n", ret);
46                 return ret;
47         }
48
49         ret = nv04_pll_calc(nv_subdev(devinit), &info, freq, &N1, &M1, &N2, &M2, &P);
50         if (!ret) {
51                 nv_error(devinit, "failed pll calculation\n");
52                 return ret;
53         }
54
55         switch (info.type) {
56         case PLL_VPLL0:
57         case PLL_VPLL1:
58                 nv_wr32(priv, info.reg + 0, 0x10000611);
59                 nv_mask(priv, info.reg + 4, 0x00ff00ff, (M1 << 16) | N1);
60                 nv_mask(priv, info.reg + 8, 0x7fff00ff, (P  << 28) |
61                                                         (M2 << 16) | N2);
62                 break;
63         case PLL_MEMORY:
64                 nv_mask(priv, info.reg + 0, 0x01ff0000, (P << 22) |
65                                                         (info.bias_p << 19) |
66                                                         (P << 16));
67                 nv_wr32(priv, info.reg + 4, (N1 << 8) | M1);
68                 break;
69         default:
70                 nv_mask(priv, info.reg + 0, 0x00070000, (P << 16));
71                 nv_wr32(priv, info.reg + 4, (N1 << 8) | M1);
72                 break;
73         }
74
75         return 0;
76 }
77
78 static u64
79 nv50_devinit_disable(struct nouveau_devinit *devinit)
80 {
81         struct nv50_devinit_priv *priv = (void *)devinit;
82         u32 r001540 = nv_rd32(priv, 0x001540);
83         u64 disable = 0ULL;
84
85         if (!(r001540 & 0x40000000))
86                 disable |= (1ULL << NVDEV_ENGINE_MPEG);
87
88         return disable;
89 }
90
91 int
92 nv50_devinit_init(struct nouveau_object *object)
93 {
94         struct nouveau_bios *bios = nouveau_bios(object);
95         struct nouveau_ibus *ibus = nouveau_ibus(object);
96         struct nv50_devinit_priv *priv = (void *)object;
97         struct nvbios_outp info;
98         struct dcb_output outp;
99         u8  ver = 0xff, hdr, cnt, len;
100         int ret, i = 0;
101
102         if (!priv->base.post) {
103                 if (!nv_rdvgac(priv, 0, 0x00) &&
104                     !nv_rdvgac(priv, 0, 0x1a)) {
105                         nv_info(priv, "adaptor not initialised\n");
106                         priv->base.post = true;
107                 }
108         }
109
110         /* some boards appear to require certain priv register timeouts
111          * to be bumped before runing devinit scripts.  not a clue why
112          * the vbios engineers didn't make the scripts just work...
113          */
114         if (priv->base.post && ibus)
115                 nv_ofuncs(ibus)->init(nv_object(ibus));
116
117         ret = nouveau_devinit_init(&priv->base);
118         if (ret)
119                 return ret;
120
121         /* if we ran the init tables, we have to execute the first script
122          * pointer of each dcb entry's display encoder table in order
123          * to properly initialise each encoder.
124          */
125         while (priv->base.post && dcb_outp_parse(bios, i, &ver, &hdr, &outp)) {
126                 if (nvbios_outp_match(bios, outp.hasht, outp.hashm,
127                                      &ver, &hdr, &cnt, &len, &info)) {
128                         struct nvbios_init init = {
129                                 .subdev = nv_subdev(priv),
130                                 .bios = bios,
131                                 .offset = info.script[0],
132                                 .outp = &outp,
133                                 .crtc = -1,
134                                 .execute = 1,
135                         };
136
137                         nvbios_exec(&init);
138                 }
139                 i++;
140         }
141
142         return 0;
143 }
144
145 int
146 nv50_devinit_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
147                   struct nouveau_oclass *oclass, void *data, u32 size,
148                   struct nouveau_object **pobject)
149 {
150         struct nv50_devinit_priv *priv;
151         int ret;
152
153         ret = nouveau_devinit_create(parent, engine, oclass, &priv);
154         *pobject = nv_object(priv);
155         if (ret)
156                 return ret;
157
158         return 0;
159 }
160
161 struct nouveau_oclass *
162 nv50_devinit_oclass = &(struct nouveau_devinit_impl) {
163         .base.handle = NV_SUBDEV(DEVINIT, 0x50),
164         .base.ofuncs = &(struct nouveau_ofuncs) {
165                 .ctor = nv50_devinit_ctor,
166                 .dtor = _nouveau_devinit_dtor,
167                 .init = nv50_devinit_init,
168                 .fini = _nouveau_devinit_fini,
169         },
170         .pll_set = nv50_devinit_pll_set,
171         .disable = nv50_devinit_disable,
172         .post = nvbios_init,
173 }.base;