1 /* $NetBSD: nouveau_nvkm_subdev_i2c_auxgm200.c,v 1.2 2021/12/18 23:45:40 riastradh Exp $ */
2
3 /*
4 * Copyright 2015 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 busions 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 <bskeggs@redhat.com>
25 */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_i2c_auxgm200.c,v 1.2 2021/12/18 23:45:40 riastradh Exp $");
28
29 #define gm200_i2c_aux(p) container_of((p), struct gm200_i2c_aux, base)
30 #include "aux.h"
31
32 struct gm200_i2c_aux {
33 struct nvkm_i2c_aux base;
34 int ch;
35 };
36
37 static void
gm200_i2c_aux_fini(struct gm200_i2c_aux * aux)38 gm200_i2c_aux_fini(struct gm200_i2c_aux *aux)
39 {
40 struct nvkm_device *device = aux->base.pad->i2c->subdev.device;
41 nvkm_mask(device, 0x00d954 + (aux->ch * 0x50), 0x00310000, 0x00000000);
42 }
43
44 static int
gm200_i2c_aux_init(struct gm200_i2c_aux * aux)45 gm200_i2c_aux_init(struct gm200_i2c_aux *aux)
46 {
47 struct nvkm_device *device = aux->base.pad->i2c->subdev.device;
48 const u32 unksel = 1; /* nfi which to use, or if it matters.. */
49 const u32 ureq = unksel ? 0x00100000 : 0x00200000;
50 const u32 urep = unksel ? 0x01000000 : 0x02000000;
51 u32 ctrl, timeout;
52
53 /* wait up to 1ms for any previous transaction to be done... */
54 timeout = 1000;
55 do {
56 ctrl = nvkm_rd32(device, 0x00d954 + (aux->ch * 0x50));
57 udelay(1);
58 if (!timeout--) {
59 AUX_ERR(&aux->base, "begin idle timeout %08x", ctrl);
60 return -EBUSY;
61 }
62 } while (ctrl & 0x03010000);
63
64 /* set some magic, and wait up to 1ms for it to appear */
65 nvkm_mask(device, 0x00d954 + (aux->ch * 0x50), 0x00300000, ureq);
66 timeout = 1000;
67 do {
68 ctrl = nvkm_rd32(device, 0x00d954 + (aux->ch * 0x50));
69 udelay(1);
70 if (!timeout--) {
71 AUX_ERR(&aux->base, "magic wait %08x", ctrl);
72 gm200_i2c_aux_fini(aux);
73 return -EBUSY;
74 }
75 } while ((ctrl & 0x03000000) != urep);
76
77 return 0;
78 }
79
80 static int
gm200_i2c_aux_xfer(struct nvkm_i2c_aux * obj,bool retry,u8 type,u32 addr,u8 * data,u8 * size)81 gm200_i2c_aux_xfer(struct nvkm_i2c_aux *obj, bool retry,
82 u8 type, u32 addr, u8 *data, u8 *size)
83 {
84 struct gm200_i2c_aux *aux = gm200_i2c_aux(obj);
85 struct nvkm_device *device = aux->base.pad->i2c->subdev.device;
86 const u32 base = aux->ch * 0x50;
87 u32 ctrl, stat, timeout, retries = 0;
88 u32 xbuf[4] = {};
89 int ret, i;
90
91 AUX_TRACE(&aux->base, "%d: %08x %d", type, addr, *size);
92
93 ret = gm200_i2c_aux_init(aux);
94 if (ret < 0)
95 goto out;
96
97 stat = nvkm_rd32(device, 0x00d958 + base);
98 if (!(stat & 0x10000000)) {
99 AUX_TRACE(&aux->base, "sink not detected");
100 ret = -ENXIO;
101 goto out;
102 }
103
104 if (!(type & 1)) {
105 memcpy(xbuf, data, *size);
106 for (i = 0; i < 16; i += 4) {
107 AUX_TRACE(&aux->base, "wr %08x", xbuf[i / 4]);
108 nvkm_wr32(device, 0x00d930 + base + i, xbuf[i / 4]);
109 }
110 }
111
112 ctrl = nvkm_rd32(device, 0x00d954 + base);
113 ctrl &= ~0x0001f1ff;
114 ctrl |= type << 12;
115 ctrl |= (*size ? (*size - 1) : 0x00000100);
116 nvkm_wr32(device, 0x00d950 + base, addr);
117
118 /* (maybe) retry transaction a number of times on failure... */
119 do {
120 /* reset, and delay a while if this is a retry */
121 nvkm_wr32(device, 0x00d954 + base, 0x80000000 | ctrl);
122 nvkm_wr32(device, 0x00d954 + base, 0x00000000 | ctrl);
123 if (retries)
124 udelay(400);
125
126 /* transaction request, wait up to 1ms for it to complete */
127 nvkm_wr32(device, 0x00d954 + base, 0x00010000 | ctrl);
128
129 timeout = 1000;
130 do {
131 ctrl = nvkm_rd32(device, 0x00d954 + base);
132 udelay(1);
133 if (!timeout--) {
134 AUX_ERR(&aux->base, "timeout %08x", ctrl);
135 ret = -EIO;
136 goto out;
137 }
138 } while (ctrl & 0x00010000);
139 ret = 0;
140
141 /* read status, and check if transaction completed ok */
142 stat = nvkm_mask(device, 0x00d958 + base, 0, 0);
143 if ((stat & 0x000f0000) == 0x00080000 ||
144 (stat & 0x000f0000) == 0x00020000)
145 ret = 1;
146 if ((stat & 0x00000100))
147 ret = -ETIMEDOUT;
148 if ((stat & 0x00000e00))
149 ret = -EIO;
150
151 AUX_TRACE(&aux->base, "%02d %08x %08x", retries, ctrl, stat);
152 } while (ret && retry && retries++ < 32);
153
154 if (type & 1) {
155 for (i = 0; i < 16; i += 4) {
156 xbuf[i / 4] = nvkm_rd32(device, 0x00d940 + base + i);
157 AUX_TRACE(&aux->base, "rd %08x", xbuf[i / 4]);
158 }
159 memcpy(data, xbuf, *size);
160 *size = stat & 0x0000001f;
161 }
162
163 out:
164 gm200_i2c_aux_fini(aux);
165 return ret < 0 ? ret : (stat & 0x000f0000) >> 16;
166 }
167
168 static const struct nvkm_i2c_aux_func
169 gm200_i2c_aux_func = {
170 .address_only = true,
171 .xfer = gm200_i2c_aux_xfer,
172 };
173
174 int
gm200_i2c_aux_new(struct nvkm_i2c_pad * pad,int index,u8 drive,struct nvkm_i2c_aux ** paux)175 gm200_i2c_aux_new(struct nvkm_i2c_pad *pad, int index, u8 drive,
176 struct nvkm_i2c_aux **paux)
177 {
178 struct gm200_i2c_aux *aux;
179
180 if (!(aux = kzalloc(sizeof(*aux), GFP_KERNEL)))
181 return -ENOMEM;
182 *paux = &aux->base;
183
184 nvkm_i2c_aux_ctor(&gm200_i2c_aux_func, pad, index, &aux->base);
185 aux->ch = drive;
186 aux->base.intr = 1 << aux->ch;
187 return 0;
188 }
189