2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
27 g94_aux_stat(struct nvkm_i2c
*i2c
, u32
*hi
, u32
*lo
, u32
*rq
, u32
*tx
)
29 u32 intr
= nv_rd32(i2c
, 0x00e06c);
30 u32 stat
= nv_rd32(i2c
, 0x00e068) & intr
, i
;
31 for (i
= 0, *hi
= *lo
= *rq
= *tx
= 0; i
< 8; i
++) {
32 if ((stat
& (1 << (i
* 4)))) *hi
|= 1 << i
;
33 if ((stat
& (2 << (i
* 4)))) *lo
|= 1 << i
;
34 if ((stat
& (4 << (i
* 4)))) *rq
|= 1 << i
;
35 if ((stat
& (8 << (i
* 4)))) *tx
|= 1 << i
;
37 nv_wr32(i2c
, 0x00e06c, intr
);
41 g94_aux_mask(struct nvkm_i2c
*i2c
, u32 type
, u32 mask
, u32 data
)
43 u32 temp
= nv_rd32(i2c
, 0x00e068), i
;
44 for (i
= 0; i
< 8; i
++) {
45 if (mask
& (1 << i
)) {
46 if (!(data
& (1 << i
))) {
47 temp
&= ~(type
<< (i
* 4));
50 temp
|= type
<< (i
* 4);
53 nv_wr32(i2c
, 0x00e068, temp
);
56 #define AUX_DBG(fmt, args...) nv_debug(aux, "AUXCH(%d): " fmt, ch, ##args)
57 #define AUX_ERR(fmt, args...) nv_error(aux, "AUXCH(%d): " fmt, ch, ##args)
60 auxch_fini(struct nvkm_i2c
*aux
, int ch
)
62 nv_mask(aux
, 0x00e4e4 + (ch
* 0x50), 0x00310000, 0x00000000);
66 auxch_init(struct nvkm_i2c
*aux
, int ch
)
68 const u32 unksel
= 1; /* nfi which to use, or if it matters.. */
69 const u32 ureq
= unksel
? 0x00100000 : 0x00200000;
70 const u32 urep
= unksel
? 0x01000000 : 0x02000000;
73 /* wait up to 1ms for any previous transaction to be done... */
76 ctrl
= nv_rd32(aux
, 0x00e4e4 + (ch
* 0x50));
79 AUX_ERR("begin idle timeout 0x%08x\n", ctrl
);
82 } while (ctrl
& 0x03010000);
84 /* set some magic, and wait up to 1ms for it to appear */
85 nv_mask(aux
, 0x00e4e4 + (ch
* 0x50), 0x00300000, ureq
);
88 ctrl
= nv_rd32(aux
, 0x00e4e4 + (ch
* 0x50));
91 AUX_ERR("magic wait 0x%08x\n", ctrl
);
95 } while ((ctrl
& 0x03000000) != urep
);
101 g94_aux(struct nvkm_i2c_port
*base
, bool retry
,
102 u8 type
, u32 addr
, u8
*data
, u8 size
)
104 struct nvkm_i2c
*aux
= nvkm_i2c(base
);
105 struct nv50_i2c_port
*port
= (void *)base
;
106 u32 ctrl
, stat
, timeout
, retries
;
111 AUX_DBG("%d: 0x%08x %d\n", type
, addr
, size
);
113 ret
= auxch_init(aux
, ch
);
117 stat
= nv_rd32(aux
, 0x00e4e8 + (ch
* 0x50));
118 if (!(stat
& 0x10000000)) {
119 AUX_DBG("sink not detected\n");
125 memcpy(xbuf
, data
, size
);
126 for (i
= 0; i
< 16; i
+= 4) {
127 AUX_DBG("wr 0x%08x\n", xbuf
[i
/ 4]);
128 nv_wr32(aux
, 0x00e4c0 + (ch
* 0x50) + i
, xbuf
[i
/ 4]);
132 ctrl
= nv_rd32(aux
, 0x00e4e4 + (ch
* 0x50));
136 nv_wr32(aux
, 0x00e4e0 + (ch
* 0x50), addr
);
138 /* (maybe) retry transaction a number of times on failure... */
139 for (retries
= 0; !ret
&& retries
< 32; retries
++) {
140 /* reset, and delay a while if this is a retry */
141 nv_wr32(aux
, 0x00e4e4 + (ch
* 0x50), 0x80000000 | ctrl
);
142 nv_wr32(aux
, 0x00e4e4 + (ch
* 0x50), 0x00000000 | ctrl
);
146 /* transaction request, wait up to 1ms for it to complete */
147 nv_wr32(aux
, 0x00e4e4 + (ch
* 0x50), 0x00010000 | ctrl
);
151 ctrl
= nv_rd32(aux
, 0x00e4e4 + (ch
* 0x50));
154 AUX_ERR("tx req timeout 0x%08x\n", ctrl
);
158 } while (ctrl
& 0x00010000);
161 /* read status, and check if transaction completed ok */
162 stat
= nv_mask(aux
, 0x00e4e8 + (ch
* 0x50), 0, 0);
163 if ((stat
& 0x000f0000) == 0x00080000 ||
164 (stat
& 0x000f0000) == 0x00020000)
166 if ((stat
& 0x00000100))
168 if ((stat
& 0x00000e00))
171 AUX_DBG("%02d 0x%08x 0x%08x\n", retries
, ctrl
, stat
);
175 for (i
= 0; i
< 16; i
+= 4) {
176 xbuf
[i
/ 4] = nv_rd32(aux
, 0x00e4d0 + (ch
* 0x50) + i
);
177 AUX_DBG("rd 0x%08x\n", xbuf
[i
/ 4]);
179 memcpy(data
, xbuf
, size
);
184 return ret
< 0 ? ret
: (stat
& 0x000f0000) >> 16;
187 static const struct nvkm_i2c_func
189 .drive_scl
= nv50_i2c_drive_scl
,
190 .drive_sda
= nv50_i2c_drive_sda
,
191 .sense_scl
= nv50_i2c_sense_scl
,
192 .sense_sda
= nv50_i2c_sense_sda
,
196 g94_i2c_port_ctor(struct nvkm_object
*parent
, struct nvkm_object
*engine
,
197 struct nvkm_oclass
*oclass
, void *data
, u32 index
,
198 struct nvkm_object
**pobject
)
200 struct dcb_i2c_entry
*info
= data
;
201 struct nv50_i2c_port
*port
;
204 ret
= nvkm_i2c_port_create(parent
, engine
, oclass
, index
,
205 &nvkm_i2c_bit_algo
, &g94_i2c_func
, &port
);
206 *pobject
= nv_object(port
);
210 if (info
->drive
>= nv50_i2c_addr_nr
)
214 port
->addr
= nv50_i2c_addr
[info
->drive
];
218 static const struct nvkm_i2c_func
224 g94_aux_port_ctor(struct nvkm_object
*parent
, struct nvkm_object
*engine
,
225 struct nvkm_oclass
*oclass
, void *data
, u32 index
,
226 struct nvkm_object
**pobject
)
228 struct dcb_i2c_entry
*info
= data
;
229 struct nv50_i2c_port
*port
;
232 ret
= nvkm_i2c_port_create(parent
, engine
, oclass
, index
,
233 &nvkm_i2c_aux_algo
, &g94_aux_func
, &port
);
234 *pobject
= nv_object(port
);
238 port
->base
.aux
= info
->auxch
;
239 port
->addr
= info
->auxch
;
243 static struct nvkm_oclass
245 { .handle
= NV_I2C_TYPE_DCBI2C(DCB_I2C_NVIO_BIT
),
246 .ofuncs
= &(struct nvkm_ofuncs
) {
247 .ctor
= g94_i2c_port_ctor
,
248 .dtor
= _nvkm_i2c_port_dtor
,
249 .init
= nv50_i2c_port_init
,
250 .fini
= _nvkm_i2c_port_fini
,
253 { .handle
= NV_I2C_TYPE_DCBI2C(DCB_I2C_NVIO_AUX
),
254 .ofuncs
= &(struct nvkm_ofuncs
) {
255 .ctor
= g94_aux_port_ctor
,
256 .dtor
= _nvkm_i2c_port_dtor
,
257 .init
= _nvkm_i2c_port_init
,
258 .fini
= _nvkm_i2c_port_fini
,
265 g94_i2c_oclass
= &(struct nvkm_i2c_impl
) {
266 .base
.handle
= NV_SUBDEV(I2C
, 0x94),
267 .base
.ofuncs
= &(struct nvkm_ofuncs
) {
268 .ctor
= _nvkm_i2c_ctor
,
269 .dtor
= _nvkm_i2c_dtor
,
270 .init
= _nvkm_i2c_init
,
271 .fini
= _nvkm_i2c_fini
,
273 .sclass
= g94_i2c_sclass
,
274 .pad_x
= &nv04_i2c_pad_oclass
,
275 .pad_s
= &g94_i2c_pad_oclass
,
277 .aux_stat
= g94_aux_stat
,
278 .aux_mask
= g94_aux_mask
,