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 #include <subdev/bios.h>
25 #include <subdev/bios/bit.h>
26 #include <subdev/bios/cstep.h>
27 
28 u32
29 nvbios_cstepTe(struct nvkm_bios *bios,
30 	       u8 *ver, u8 *hdr, u8 *cnt, u8 *len, u8 *xnr, u8 *xsz)
31 {
32 	struct bit_entry bit_P;
33 	u32 cstep = 0;
34 
35 	if (!bit_entry(bios, 'P', &bit_P)) {
36 		if (bit_P.version == 2 && bit_P.length >= 0x38)
37 			cstep = nvbios_rd32(bios, bit_P.offset + 0x34);
38 
39 		if (cstep) {
40 			*ver = nvbios_rd08(bios, cstep + 0);
41 			switch (*ver) {
42 			case 0x10:
43 				*hdr = nvbios_rd08(bios, cstep + 1);
44 				*cnt = nvbios_rd08(bios, cstep + 3);
45 				*len = nvbios_rd08(bios, cstep + 2);
46 				*xnr = nvbios_rd08(bios, cstep + 5);
47 				*xsz = nvbios_rd08(bios, cstep + 4);
48 				return cstep;
49 			default:
50 				break;
51 			}
52 		}
53 	}
54 
55 	return 0;
56 }
57 
58 u32
59 nvbios_cstepEe(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr)
60 {
61 	u8  cnt, len, xnr, xsz;
62 	u32 data = nvbios_cstepTe(bios, ver, hdr, &cnt, &len, &xnr, &xsz);
63 	if (data && idx < cnt) {
64 		data = data + *hdr + (idx * len);
65 		*hdr = len;
66 		return data;
67 	}
68 	return 0;
69 }
70 
71 u32
72 nvbios_cstepEp(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr,
73 	       struct nvbios_cstepE *info)
74 {
75 	u32 data = nvbios_cstepEe(bios, idx, ver, hdr);
76 	memset(info, 0x00, sizeof(*info));
77 	if (data) {
78 		info->pstate = (nvbios_rd16(bios, data + 0x00) & 0x01e0) >> 5;
79 		info->index   = nvbios_rd08(bios, data + 0x03);
80 	}
81 	return data;
82 }
83 
84 u32
85 nvbios_cstepEm(struct nvkm_bios *bios, u8 pstate, u8 *ver, u8 *hdr,
86 	       struct nvbios_cstepE *info)
87 {
88 	u32 data, idx = 0;
89 	while ((data = nvbios_cstepEp(bios, idx++, ver, hdr, info))) {
90 		if (info->pstate == pstate)
91 			break;
92 	}
93 	return data;
94 }
95 
96 u32
97 nvbios_cstepXe(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr)
98 {
99 	u8  cnt, len, xnr, xsz;
100 	u32 data = nvbios_cstepTe(bios, ver, hdr, &cnt, &len, &xnr, &xsz);
101 	if (data && idx < xnr) {
102 		data = data + *hdr + (cnt * len) + (idx * xsz);
103 		*hdr = xsz;
104 		return data;
105 	}
106 	return 0;
107 }
108 
109 u32
110 nvbios_cstepXp(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr,
111 	       struct nvbios_cstepX *info)
112 {
113 	u32 data = nvbios_cstepXe(bios, idx, ver, hdr);
114 	memset(info, 0x00, sizeof(*info));
115 	if (data) {
116 		info->freq    = nvbios_rd16(bios, data + 0x00) * 1000;
117 		info->unkn[0] = nvbios_rd08(bios, data + 0x02);
118 		info->unkn[1] = nvbios_rd08(bios, data + 0x03);
119 		info->voltage = nvbios_rd08(bios, data + 0x04);
120 	}
121 	return data;
122 }
123