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