HID: picolcd: sanity check report size in raw_event() callback
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / core / engine / disp / sornv94.c
1 /*
2  * Copyright 2012 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 <core/os.h>
26 #include <core/class.h>
27
28 #include <subdev/bios.h>
29 #include <subdev/bios/dcb.h>
30 #include <subdev/bios/dp.h>
31 #include <subdev/bios/init.h>
32 #include <subdev/timer.h>
33
34 #include "nv50.h"
35 #include "outpdp.h"
36
37 static inline u32
38 nv94_sor_soff(struct nvkm_output_dp *outp)
39 {
40         return (ffs(outp->base.info.or) - 1) * 0x800;
41 }
42
43 static inline u32
44 nv94_sor_loff(struct nvkm_output_dp *outp)
45 {
46         return nv94_sor_soff(outp) + !(outp->base.info.sorconf.link & 1) * 0x80;
47 }
48
49 static inline u32
50 nv94_sor_dp_lane_map(struct nv50_disp_priv *priv, u8 lane)
51 {
52         static const u8 nvaf[] = { 24, 16, 8, 0 }; /* thanks, apple.. */
53         static const u8 nv94[] = { 16, 8, 0, 24 };
54         if (nv_device(priv)->chipset == 0xaf)
55                 return nvaf[lane];
56         return nv94[lane];
57 }
58
59 static int
60 nv94_sor_dp_pattern(struct nvkm_output_dp *outp, int pattern)
61 {
62         struct nv50_disp_priv *priv = (void *)nouveau_disp(outp);
63         const u32 loff = nv94_sor_loff(outp);
64         nv_mask(priv, 0x61c10c + loff, 0x0f000000, pattern << 24);
65         return 0;
66 }
67
68 int
69 nv94_sor_dp_lnk_pwr(struct nvkm_output_dp *outp, int nr)
70 {
71         struct nv50_disp_priv *priv = (void *)nouveau_disp(outp);
72         const u32 soff = nv94_sor_soff(outp);
73         const u32 loff = nv94_sor_loff(outp);
74         u32 mask = 0, i;
75
76         for (i = 0; i < nr; i++)
77                 mask |= 1 << (nv94_sor_dp_lane_map(priv, i) >> 3);
78
79         nv_mask(priv, 0x61c130 + loff, 0x0000000f, mask);
80         nv_mask(priv, 0x61c034 + soff, 0x80000000, 0x80000000);
81         nv_wait(priv, 0x61c034 + soff, 0x80000000, 0x00000000);
82         return 0;
83 }
84
85 static int
86 nv94_sor_dp_lnk_ctl(struct nvkm_output_dp *outp, int nr, int bw, bool ef)
87 {
88         struct nv50_disp_priv *priv = (void *)nouveau_disp(outp);
89         const u32 soff = nv94_sor_soff(outp);
90         const u32 loff = nv94_sor_loff(outp);
91         u32 dpctrl = 0x00000000;
92         u32 clksor = 0x00000000;
93
94         dpctrl |= ((1 << nr) - 1) << 16;
95         if (ef)
96                 dpctrl |= 0x00004000;
97         if (bw > 0x06)
98                 clksor |= 0x00040000;
99
100         nv_mask(priv, 0x614300 + soff, 0x000c0000, clksor);
101         nv_mask(priv, 0x61c10c + loff, 0x001f4000, dpctrl);
102         return 0;
103 }
104
105 static int
106 nv94_sor_dp_drv_ctl(struct nvkm_output_dp *outp, int ln, int vs, int pe, int pc)
107 {
108         struct nv50_disp_priv *priv = (void *)nouveau_disp(outp);
109         struct nouveau_bios *bios = nouveau_bios(priv);
110         const u32 shift = nv94_sor_dp_lane_map(priv, ln);
111         const u32 loff = nv94_sor_loff(outp);
112         u32 addr, data[3];
113         u8  ver, hdr, cnt, len;
114         struct nvbios_dpout info;
115         struct nvbios_dpcfg ocfg;
116
117         addr = nvbios_dpout_match(bios, outp->base.info.hasht,
118                                         outp->base.info.hashm,
119                                  &ver, &hdr, &cnt, &len, &info);
120         if (!addr)
121                 return -ENODEV;
122
123         addr = nvbios_dpcfg_match(bios, addr, 0, vs, pe,
124                                  &ver, &hdr, &cnt, &len, &ocfg);
125         if (!addr)
126                 return -EINVAL;
127
128         data[0] = nv_rd32(priv, 0x61c118 + loff) & ~(0x000000ff << shift);
129         data[1] = nv_rd32(priv, 0x61c120 + loff) & ~(0x000000ff << shift);
130         data[2] = nv_rd32(priv, 0x61c130 + loff);
131         if ((data[2] & 0x0000ff00) < (ocfg.tx_pu << 8) || ln == 0)
132                 data[2] = (data[2] & ~0x0000ff00) | (ocfg.tx_pu << 8);
133         nv_wr32(priv, 0x61c118 + loff, data[0] | (ocfg.dc << shift));
134         nv_wr32(priv, 0x61c120 + loff, data[1] | (ocfg.pe << shift));
135         nv_wr32(priv, 0x61c130 + loff, data[2] | (ocfg.tx_pu << 8));
136         return 0;
137 }
138
139 struct nvkm_output_dp_impl
140 nv94_sor_dp_impl = {
141         .base.base.handle = DCB_OUTPUT_DP,
142         .base.base.ofuncs = &(struct nouveau_ofuncs) {
143                 .ctor = _nvkm_output_dp_ctor,
144                 .dtor = _nvkm_output_dp_dtor,
145                 .init = _nvkm_output_dp_init,
146                 .fini = _nvkm_output_dp_fini,
147         },
148         .pattern = nv94_sor_dp_pattern,
149         .lnk_pwr = nv94_sor_dp_lnk_pwr,
150         .lnk_ctl = nv94_sor_dp_lnk_ctl,
151         .drv_ctl = nv94_sor_dp_drv_ctl,
152 };