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 <subdev/mxm.h>
26 #include "mxms.h"
27
28 #define ROM16(x) le16_to_cpu(*(u16 *)&(x))
29 #define ROM32(x) le32_to_cpu(*(u32 *)&(x))
30
31 static u8 *
mxms_data(struct nouveau_mxm * mxm)32 mxms_data(struct nouveau_mxm *mxm)
33 {
34 return mxm->mxms;
35
36 }
37
38 u16
mxms_version(struct nouveau_mxm * mxm)39 mxms_version(struct nouveau_mxm *mxm)
40 {
41 u8 *mxms = mxms_data(mxm);
42 u16 version = (mxms[4] << 8) | mxms[5];
43 switch (version ) {
44 case 0x0200:
45 case 0x0201:
46 case 0x0300:
47 return version;
48 default:
49 break;
50 }
51
52 nv_debug(mxm, "unknown version %d.%d\n", mxms[4], mxms[5]);
53 return 0x0000;
54 }
55
56 u16
mxms_headerlen(struct nouveau_mxm * mxm)57 mxms_headerlen(struct nouveau_mxm *mxm)
58 {
59 return 8;
60 }
61
62 u16
mxms_structlen(struct nouveau_mxm * mxm)63 mxms_structlen(struct nouveau_mxm *mxm)
64 {
65 return *(u16 *)&mxms_data(mxm)[6];
66 }
67
68 bool
mxms_checksum(struct nouveau_mxm * mxm)69 mxms_checksum(struct nouveau_mxm *mxm)
70 {
71 u16 size = mxms_headerlen(mxm) + mxms_structlen(mxm);
72 u8 *mxms = mxms_data(mxm), sum = 0;
73 while (size--)
74 sum += *mxms++;
75 if (sum) {
76 nv_debug(mxm, "checksum invalid\n");
77 return false;
78 }
79 return true;
80 }
81
82 bool
mxms_valid(struct nouveau_mxm * mxm)83 mxms_valid(struct nouveau_mxm *mxm)
84 {
85 u8 *mxms = mxms_data(mxm);
86 if (*(u32 *)mxms != 0x5f4d584d) {
87 nv_debug(mxm, "signature invalid\n");
88 return false;
89 }
90
91 if (!mxms_version(mxm) || !mxms_checksum(mxm))
92 return false;
93
94 return true;
95 }
96
97 bool
mxms_foreach(struct nouveau_mxm * mxm,u8 types,bool (* exec)(struct nouveau_mxm *,u8 *,void *),void * info)98 mxms_foreach(struct nouveau_mxm *mxm, u8 types,
99 bool (*exec)(struct nouveau_mxm *, u8 *, void *), void *info)
100 {
101 u8 *mxms = mxms_data(mxm);
102 u8 *desc = mxms + mxms_headerlen(mxm);
103 u8 *fini = desc + mxms_structlen(mxm) - 1;
104 while (desc < fini) {
105 u8 type = desc[0] & 0x0f;
106 u8 headerlen = 0;
107 u8 recordlen = 0;
108 u8 entries = 0;
109
110 switch (type) {
111 case 0: /* Output Device Structure */
112 if (mxms_version(mxm) >= 0x0300)
113 headerlen = 8;
114 else
115 headerlen = 6;
116 break;
117 case 1: /* System Cooling Capability Structure */
118 case 2: /* Thermal Structure */
119 case 3: /* Input Power Structure */
120 headerlen = 4;
121 break;
122 case 4: /* GPIO Device Structure */
123 headerlen = 4;
124 recordlen = 2;
125 entries = (ROM32(desc[0]) & 0x01f00000) >> 20;
126 break;
127 case 5: /* Vendor Specific Structure */
128 headerlen = 8;
129 break;
130 case 6: /* Backlight Control Structure */
131 if (mxms_version(mxm) >= 0x0300) {
132 headerlen = 4;
133 recordlen = 8;
134 entries = (desc[1] & 0xf0) >> 4;
135 } else {
136 headerlen = 8;
137 }
138 break;
139 case 7: /* Fan Control Structure */
140 headerlen = 8;
141 recordlen = 4;
142 entries = desc[1] & 0x07;
143 break;
144 default:
145 nv_debug(mxm, "unknown descriptor type %d\n", type);
146 return false;
147 }
148
149 if (nv_subdev(mxm)->debug >= NV_DBG_DEBUG && (exec == NULL)) {
150 static const char * mxms_desc_name[] = {
151 "ODS", "SCCS", "TS", "IPS",
152 "GSD", "VSS", "BCS", "FCS",
153 };
154 u8 *dump = desc;
155 int i, j;
156
157 nv_debug(mxm, "%4s: ", mxms_desc_name[type]);
158 for (j = headerlen - 1; j >= 0; j--)
159 pr_cont("%02x", dump[j]);
160 pr_cont("\n");
161 dump += headerlen;
162
163 for (i = 0; i < entries; i++, dump += recordlen) {
164 nv_debug(mxm, " ");
165 for (j = recordlen - 1; j >= 0; j--)
166 pr_cont("%02x", dump[j]);
167 pr_cont("\n");
168 }
169 }
170
171 if (types & (1 << type)) {
172 if (!exec(mxm, desc, info))
173 return false;
174 }
175
176 desc += headerlen + (entries * recordlen);
177 }
178
179 return true;
180 }
181
182 void
mxms_output_device(struct nouveau_mxm * mxm,u8 * pdata,struct mxms_odev * desc)183 mxms_output_device(struct nouveau_mxm *mxm, u8 *pdata, struct mxms_odev *desc)
184 {
185 u64 data = ROM32(pdata[0]);
186 if (mxms_version(mxm) >= 0x0300)
187 data |= (u64)ROM16(pdata[4]) << 32;
188
189 desc->outp_type = (data & 0x00000000000000f0ULL) >> 4;
190 desc->ddc_port = (data & 0x0000000000000f00ULL) >> 8;
191 desc->conn_type = (data & 0x000000000001f000ULL) >> 12;
192 desc->dig_conn = (data & 0x0000000000780000ULL) >> 19;
193 }
194