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 #include <subdev/bios.h>
25 #include <subdev/bios/dcb.h>
26 #include <subdev/bios/i2c.h>
27 
28 u16
29 dcb_i2c_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
30 {
31 	u16 i2c = 0x0000;
32 	u16 dcb = dcb_table(bios, ver, hdr, cnt, len);
33 	if (dcb) {
34 		if (*ver >= 0x15)
35 			i2c = nv_ro16(bios, dcb + 2);
36 		if (*ver >= 0x30)
37 			i2c = nv_ro16(bios, dcb + 4);
38 	}
39 
40 	if (i2c && *ver >= 0x42) {
41 		nv_warn(bios, "ccb %02x not supported\n", *ver);
42 		return 0x0000;
43 	}
44 
45 	if (i2c && *ver >= 0x30) {
46 		*ver = nv_ro08(bios, i2c + 0);
47 		*hdr = nv_ro08(bios, i2c + 1);
48 		*cnt = nv_ro08(bios, i2c + 2);
49 		*len = nv_ro08(bios, i2c + 3);
50 	} else {
51 		*ver = *ver; /* use DCB version */
52 		*hdr = 0;
53 		*cnt = 16;
54 		*len = 4;
55 	}
56 
57 	return i2c;
58 }
59 
60 u16
61 dcb_i2c_entry(struct nvkm_bios *bios, u8 idx, u8 *ver, u8 *len)
62 {
63 	u8  hdr, cnt;
64 	u16 i2c = dcb_i2c_table(bios, ver, &hdr, &cnt, len);
65 	if (i2c && idx < cnt)
66 		return i2c + hdr + (idx * *len);
67 	return 0x0000;
68 }
69 
70 int
71 dcb_i2c_parse(struct nvkm_bios *bios, u8 idx, struct dcb_i2c_entry *info)
72 {
73 	u8  ver, len;
74 	u16 ent = dcb_i2c_entry(bios, idx, &ver, &len);
75 	if (ent) {
76 		if (ver >= 0x41) {
77 			if (!(nv_ro32(bios, ent) & 0x80000000))
78 				info->type = DCB_I2C_UNUSED;
79 			else
80 				info->type = DCB_I2C_PMGR;
81 		} else
82 		if (ver >= 0x30) {
83 			info->type = nv_ro08(bios, ent + 0x03);
84 		} else {
85 			info->type = nv_ro08(bios, ent + 0x03) & 0x07;
86 			if (info->type == 0x07)
87 				info->type = DCB_I2C_UNUSED;
88 		}
89 
90 		info->drive = DCB_I2C_UNUSED;
91 		info->sense = DCB_I2C_UNUSED;
92 		info->share = DCB_I2C_UNUSED;
93 		info->auxch = DCB_I2C_UNUSED;
94 
95 		switch (info->type) {
96 		case DCB_I2C_NV04_BIT:
97 			info->drive = nv_ro08(bios, ent + 0);
98 			info->sense = nv_ro08(bios, ent + 1);
99 			return 0;
100 		case DCB_I2C_NV4E_BIT:
101 			info->drive = nv_ro08(bios, ent + 1);
102 			return 0;
103 		case DCB_I2C_NVIO_BIT:
104 			info->drive = nv_ro08(bios, ent + 0) & 0x0f;
105 			if (nv_ro08(bios, ent + 1) & 0x01)
106 				info->share = nv_ro08(bios, ent + 1) >> 1;
107 			return 0;
108 		case DCB_I2C_NVIO_AUX:
109 			info->auxch = nv_ro08(bios, ent + 0) & 0x0f;
110 			if (nv_ro08(bios, ent + 1) & 0x01)
111 					info->share = info->auxch;
112 			return 0;
113 		case DCB_I2C_PMGR:
114 			info->drive = (nv_ro16(bios, ent + 0) & 0x01f) >> 0;
115 			if (info->drive == 0x1f)
116 				info->drive = DCB_I2C_UNUSED;
117 			info->auxch = (nv_ro16(bios, ent + 0) & 0x3e0) >> 5;
118 			if (info->auxch == 0x1f)
119 				info->auxch = DCB_I2C_UNUSED;
120 			info->share = info->auxch;
121 			return 0;
122 		case DCB_I2C_UNUSED:
123 			return 0;
124 		default:
125 			nv_warn(bios, "unknown i2c type %d\n", info->type);
126 			info->type = DCB_I2C_UNUSED;
127 			return 0;
128 		}
129 	}
130 
131 	if (bios->bmp_offset && idx < 2) {
132 		/* BMP (from v4.0 has i2c info in the structure, it's in a
133 		 * fixed location on earlier VBIOS
134 		 */
135 		if (nv_ro08(bios, bios->bmp_offset + 5) < 4)
136 			ent = 0x0048;
137 		else
138 			ent = 0x0036 + bios->bmp_offset;
139 
140 		if (idx == 0) {
141 			info->drive = nv_ro08(bios, ent + 4);
142 			if (!info->drive) info->drive = 0x3f;
143 			info->sense = nv_ro08(bios, ent + 5);
144 			if (!info->sense) info->sense = 0x3e;
145 		} else
146 		if (idx == 1) {
147 			info->drive = nv_ro08(bios, ent + 6);
148 			if (!info->drive) info->drive = 0x37;
149 			info->sense = nv_ro08(bios, ent + 7);
150 			if (!info->sense) info->sense = 0x36;
151 		}
152 
153 		info->type  = DCB_I2C_NV04_BIT;
154 		info->share = DCB_I2C_UNUSED;
155 		return 0;
156 	}
157 
158 	return -ENOENT;
159 }
160