1 /* $NetBSD: nouveau_nvkm_subdev_mxm_mxms.c,v 1.4 2021/12/18 23:45:41 riastradh Exp $ */
2
3 /*
4 * Copyright 2012 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_mxm_mxms.c,v 1.4 2021/12/18 23:45:41 riastradh Exp $");
28
29 #include "mxms.h"
30
31 #define ROM16(x) get_unaligned_le16(&(x))
32 #define ROM32(x) get_unaligned_le32(&(x))
33
34 static u8 *
mxms_data(struct nvkm_mxm * mxm)35 mxms_data(struct nvkm_mxm *mxm)
36 {
37 return mxm->mxms;
38
39 }
40
41 u16
mxms_version(struct nvkm_mxm * mxm)42 mxms_version(struct nvkm_mxm *mxm)
43 {
44 u8 *mxms = mxms_data(mxm);
45 u16 version = (mxms[4] << 8) | mxms[5];
46 switch (version ) {
47 case 0x0200:
48 case 0x0201:
49 case 0x0300:
50 return version;
51 default:
52 break;
53 }
54
55 nvkm_debug(&mxm->subdev, "unknown version %d.%d\n", mxms[4], mxms[5]);
56 return 0x0000;
57 }
58
59 u16
mxms_headerlen(struct nvkm_mxm * mxm)60 mxms_headerlen(struct nvkm_mxm *mxm)
61 {
62 return 8;
63 }
64
65 u16
mxms_structlen(struct nvkm_mxm * mxm)66 mxms_structlen(struct nvkm_mxm *mxm)
67 {
68 return *(u16 *)&mxms_data(mxm)[6];
69 }
70
71 bool
mxms_checksum(struct nvkm_mxm * mxm)72 mxms_checksum(struct nvkm_mxm *mxm)
73 {
74 u16 size = mxms_headerlen(mxm) + mxms_structlen(mxm);
75 u8 *mxms = mxms_data(mxm), sum = 0;
76 while (size--)
77 sum += *mxms++;
78 if (sum) {
79 nvkm_debug(&mxm->subdev, "checksum invalid\n");
80 return false;
81 }
82 return true;
83 }
84
85 bool
mxms_valid(struct nvkm_mxm * mxm)86 mxms_valid(struct nvkm_mxm *mxm)
87 {
88 u8 *mxms = mxms_data(mxm);
89 if (*(u32 *)mxms != 0x5f4d584d) {
90 nvkm_debug(&mxm->subdev, "signature invalid\n");
91 return false;
92 }
93
94 if (!mxms_version(mxm) || !mxms_checksum(mxm))
95 return false;
96
97 return true;
98 }
99
100 bool
mxms_foreach(struct nvkm_mxm * mxm,u8 types,bool (* exec)(struct nvkm_mxm *,u8 *,void *),void * info)101 mxms_foreach(struct nvkm_mxm *mxm, u8 types,
102 bool (*exec)(struct nvkm_mxm *, u8 *, void *), void *info)
103 {
104 struct nvkm_subdev *subdev = &mxm->subdev;
105 u8 *mxms = mxms_data(mxm);
106 u8 *desc = mxms + mxms_headerlen(mxm);
107 u8 *fini = desc + mxms_structlen(mxm) - 1;
108 while (desc < fini) {
109 u8 type = desc[0] & 0x0f;
110 u8 headerlen = 0;
111 u8 recordlen = 0;
112 u8 entries = 0;
113
114 switch (type) {
115 case 0: /* Output Device Structure */
116 if (mxms_version(mxm) >= 0x0300)
117 headerlen = 8;
118 else
119 headerlen = 6;
120 break;
121 case 1: /* System Cooling Capability Structure */
122 case 2: /* Thermal Structure */
123 case 3: /* Input Power Structure */
124 headerlen = 4;
125 break;
126 case 4: /* GPIO Device Structure */
127 headerlen = 4;
128 recordlen = 2;
129 entries = (ROM32(desc[0]) & 0x01f00000) >> 20;
130 break;
131 case 5: /* Vendor Specific Structure */
132 headerlen = 8;
133 break;
134 case 6: /* Backlight Control Structure */
135 if (mxms_version(mxm) >= 0x0300) {
136 headerlen = 4;
137 recordlen = 8;
138 entries = (desc[1] & 0xf0) >> 4;
139 } else {
140 headerlen = 8;
141 }
142 break;
143 case 7: /* Fan Control Structure */
144 headerlen = 8;
145 recordlen = 4;
146 entries = desc[1] & 0x07;
147 break;
148 default:
149 nvkm_debug(subdev, "unknown descriptor type %d\n", type);
150 return false;
151 }
152
153 if (mxm->subdev.debug >= NV_DBG_DEBUG && (exec == NULL)) {
154 static const char * mxms_desc[] = {
155 "ODS", "SCCS", "TS", "IPS",
156 "GSD", "VSS", "BCS", "FCS",
157 };
158 u8 *dump = desc;
159 char data[32], *ptr;
160 int i, j;
161
162 for (j = headerlen - 1, ptr = data; j >= 0; j--)
163 ptr += snprintf(ptr, sizeof data - (ptr - data),
164 "%02x", dump[j]);
165 dump += headerlen;
166
167 nvkm_debug(subdev, "%4s: %s\n", mxms_desc[type], data);
168 for (i = 0; i < entries; i++, dump += recordlen) {
169 for (j = recordlen - 1, ptr = data; j >= 0; j--)
170 ptr += snprintf(ptr, sizeof data -
171 (ptr - data), "%02x", dump[j]);
172 nvkm_debug(subdev, " %s\n", data);
173 }
174 }
175
176 if ((types & (1 << type)) && (exec != NULL)) {
177 if (!exec(mxm, desc, info))
178 return false;
179 }
180
181 desc += headerlen + (entries * recordlen);
182 }
183
184 return true;
185 }
186
187 void
mxms_output_device(struct nvkm_mxm * mxm,u8 * pdata,struct mxms_odev * desc)188 mxms_output_device(struct nvkm_mxm *mxm, u8 *pdata, struct mxms_odev *desc)
189 {
190 u64 data = ROM32(pdata[0]);
191 if (mxms_version(mxm) >= 0x0300)
192 data |= (u64)ROM16(pdata[4]) << 32;
193
194 desc->outp_type = (data & 0x00000000000000f0ULL) >> 4;
195 desc->ddc_port = (data & 0x0000000000000f00ULL) >> 8;
196 desc->conn_type = (data & 0x000000000001f000ULL) >> 12;
197 desc->dig_conn = (data & 0x0000000000780000ULL) >> 19;
198 }
199