1 /*
2  * Copyright 2012 Nouveau Community
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: Martin Peres
23  */
24 #include <subdev/bios.h>
25 #include <subdev/bios/bit.h>
26 #include <subdev/bios/volt.h>
27 
28 u16
29 nvbios_volt_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
30 {
31 	struct bit_entry bit_P;
32 	u16 volt = 0x0000;
33 
34 	if (!bit_entry(bios, 'P', &bit_P)) {
35 		if (bit_P.version == 2)
36 			volt = nvbios_rd16(bios, bit_P.offset + 0x0c);
37 		else
38 		if (bit_P.version == 1)
39 			volt = nvbios_rd16(bios, bit_P.offset + 0x10);
40 
41 		if (volt) {
42 			*ver = nvbios_rd08(bios, volt + 0);
43 			switch (*ver) {
44 			case 0x12:
45 				*hdr = 5;
46 				*cnt = nvbios_rd08(bios, volt + 2);
47 				*len = nvbios_rd08(bios, volt + 1);
48 				return volt;
49 			case 0x20:
50 				*hdr = nvbios_rd08(bios, volt + 1);
51 				*cnt = nvbios_rd08(bios, volt + 2);
52 				*len = nvbios_rd08(bios, volt + 3);
53 				return volt;
54 			case 0x30:
55 			case 0x40:
56 			case 0x50:
57 				*hdr = nvbios_rd08(bios, volt + 1);
58 				*cnt = nvbios_rd08(bios, volt + 3);
59 				*len = nvbios_rd08(bios, volt + 2);
60 				return volt;
61 			}
62 		}
63 	}
64 
65 	return 0x0000;
66 }
67 
68 u16
69 nvbios_volt_parse(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
70 		  struct nvbios_volt *info)
71 {
72 	u16 volt = nvbios_volt_table(bios, ver, hdr, cnt, len);
73 	memset(info, 0x00, sizeof(*info));
74 	switch (!!volt * *ver) {
75 	case 0x12:
76 		info->type    = NVBIOS_VOLT_GPIO;
77 		info->vidmask = nvbios_rd08(bios, volt + 0x04);
78 		break;
79 	case 0x20:
80 		info->type    = NVBIOS_VOLT_GPIO;
81 		info->vidmask = nvbios_rd08(bios, volt + 0x05);
82 		break;
83 	case 0x30:
84 		info->type    = NVBIOS_VOLT_GPIO;
85 		info->vidmask = nvbios_rd08(bios, volt + 0x04);
86 		break;
87 	case 0x40:
88 		info->type    = NVBIOS_VOLT_GPIO;
89 		info->base    = nvbios_rd32(bios, volt + 0x04);
90 		info->step    = nvbios_rd16(bios, volt + 0x08);
91 		info->vidmask = nvbios_rd08(bios, volt + 0x0b);
92 		/*XXX*/
93 		info->min     = 0;
94 		info->max     = info->base;
95 		break;
96 	case 0x50:
97 		info->min     = nvbios_rd32(bios, volt + 0x0a);
98 		info->max     = nvbios_rd32(bios, volt + 0x0e);
99 		info->base    = nvbios_rd32(bios, volt + 0x12) & 0x00ffffff;
100 
101 		/* offset 4 seems to be a flag byte */
102 		if (nvbios_rd32(bios, volt + 0x4) & 1) {
103 			info->type      = NVBIOS_VOLT_PWM;
104 			info->pwm_freq  = nvbios_rd32(bios, volt + 0x5) / 1000;
105 			info->pwm_range = nvbios_rd32(bios, volt + 0x16);
106 		} else {
107 			info->type      = NVBIOS_VOLT_GPIO;
108 			info->vidmask   = nvbios_rd08(bios, volt + 0x06);
109 			info->step      = nvbios_rd16(bios, volt + 0x16);
110 		}
111 		break;
112 	}
113 	return volt;
114 }
115 
116 u16
117 nvbios_volt_entry(struct nvkm_bios *bios, int idx, u8 *ver, u8 *len)
118 {
119 	u8  hdr, cnt;
120 	u16 volt = nvbios_volt_table(bios, ver, &hdr, &cnt, len);
121 	if (volt && idx < cnt) {
122 		volt = volt + hdr + (idx * *len);
123 		return volt;
124 	}
125 	return 0x0000;
126 }
127 
128 u16
129 nvbios_volt_entry_parse(struct nvkm_bios *bios, int idx, u8 *ver, u8 *len,
130 			struct nvbios_volt_entry *info)
131 {
132 	u16 volt = nvbios_volt_entry(bios, idx, ver, len);
133 	memset(info, 0x00, sizeof(*info));
134 	switch (!!volt * *ver) {
135 	case 0x12:
136 	case 0x20:
137 		info->voltage = nvbios_rd08(bios, volt + 0x00) * 10000;
138 		info->vid     = nvbios_rd08(bios, volt + 0x01);
139 		break;
140 	case 0x30:
141 		info->voltage = nvbios_rd08(bios, volt + 0x00) * 10000;
142 		info->vid     = nvbios_rd08(bios, volt + 0x01) >> 2;
143 		break;
144 	case 0x40:
145 	case 0x50:
146 		break;
147 	}
148 	return volt;
149 }
150