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