Do a major clean-up of the BUSDMA architecture. A large number of
[dragonfly.git] / sys / dev / video / bktr / bktr_i2c.c
CommitLineData
a35cc233
JS
1/*
2 * Copyright (c) 1998, 2001 Nicolas Souchu
984263bc
MD
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
a35cc233 26 * $FreeBSD: src/sys/dev/bktr/bktr_i2c.c,v 1.25 2003/12/08 07:59:18 obrien Exp $
1f7ab7c9 27 * $DragonFly: src/sys/dev/video/bktr/bktr_i2c.c,v 1.8 2006/10/25 20:56:02 dillon Exp $
984263bc
MD
28 */
29
30/*
31 * I2C support for the bti2c chipset.
32 *
33 * From brooktree848.c <fsmp@freefall.org>
34 */
35
a35cc233 36#include "opt_bktr.h"
984263bc
MD
37
38#include <sys/param.h>
39#include <sys/kernel.h>
a35cc233 40#include <sys/malloc.h>
984263bc
MD
41#include <sys/systm.h>
42#include <sys/module.h>
43#include <sys/bus.h>
44#include <sys/uio.h>
9183fa5b 45#include <sys/selinfo.h>
984263bc 46
a35cc233
JS
47#include <bus/pci/pcivar.h>
48#include <bus/pci/pcireg.h>
984263bc 49
a35cc233
JS
50#include <dev/video/meteor/ioctl_meteor.h>
51#include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
52#include <dev/video/bktr/bktr_reg.h>
53#include <dev/video/bktr/bktr_i2c.h>
984263bc 54
1f2de5d4 55#include <bus/smbus/smbconf.h>
a35cc233 56#include <bus/iicbus/iiconf.h>
984263bc
MD
57
58#define I2C_DELAY 40
59
a35cc233
JS
60/* Compilation is void if BKTR_USE_FREEBSD_SMBUS is not
61 * defined. This allows bktr owners to have smbus active for there
62 * motherboard and still use their bktr without smbus.
63 */
64#if defined(BKTR_USE_FREEBSD_SMBUS)
65
984263bc
MD
66#define BTI2C_DEBUG(x) if (bti2c_debug) (x)
67static int bti2c_debug = 0;
68
984263bc
MD
69/*
70 * Call this to pass the address of the bktr device to the
71 * bti2c_i2c layer and initialize all the I2C bus architecture
72 */
a35cc233 73int bt848_i2c_attach(device_t dev)
984263bc 74{
a35cc233
JS
75 struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
76 struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
77
78 sc->smbus = device_add_child(dev, "smbus", -1);
79 sc->iicbb = device_add_child(dev, "iicbb", -1);
80
81 if (!sc->iicbb || !sc->smbus)
82 return ENXIO;
83
84 bus_generic_attach(dev);
984263bc
MD
85
86 return (0);
87};
88
a35cc233 89int bt848_i2c_detach(device_t dev)
984263bc 90{
a35cc233
JS
91 struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
92 struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
93 int error = 0;
984263bc 94
a35cc233
JS
95 if ((error = bus_generic_detach(dev)))
96 goto error;
984263bc 97
a35cc233
JS
98 if (sc->iicbb && (error = device_delete_child(dev, sc->iicbb)))
99 goto error;
984263bc 100
a35cc233
JS
101 if (sc->smbus && (error = device_delete_child(dev, sc->smbus)))
102 goto error;
984263bc 103
a35cc233
JS
104error:
105 return (error);
984263bc
MD
106}
107
a35cc233 108int bti2c_smb_callback(device_t dev, int index, caddr_t *data)
984263bc 109{
a35cc233
JS
110 struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
111 struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
984263bc 112 int error = 0;
984263bc
MD
113
114 /* test each time if we already have/haven't the iicbus
115 * to avoid deadlocks
116 */
117 switch (index) {
118 case SMB_REQUEST_BUS:
a35cc233
JS
119 /* XXX test & set */
120 if (!sc->bus_owned) {
121 sc->bus_owned = 1;
122 } else
123 error = EWOULDBLOCK;
984263bc
MD
124 break;
125
126 case SMB_RELEASE_BUS:
a35cc233
JS
127 /* XXX test & set */
128 if (sc->bus_owned) {
129 sc->bus_owned = 0;
130 } else
131 error = EINVAL;
984263bc
MD
132 break;
133
134 default:
135 error = EINVAL;
136 }
137
138 return (error);
139}
140
a35cc233 141int bti2c_iic_callback(device_t dev, int index, caddr_t *data)
984263bc 142{
a35cc233
JS
143 struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
144 struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
984263bc 145 int error = 0;
984263bc
MD
146
147 /* test each time if we already have/haven't the smbus
148 * to avoid deadlocks
149 */
150 switch (index) {
151 case IIC_REQUEST_BUS:
a35cc233
JS
152 /* XXX test & set */
153 if (!sc->bus_owned) {
154 sc->bus_owned = 1;
155 } else
156 error = EWOULDBLOCK;
984263bc
MD
157 break;
158
159 case IIC_RELEASE_BUS:
a35cc233
JS
160 /* XXX test & set */
161 if (sc->bus_owned) {
162 sc->bus_owned = 0;
163 } else
164 error = EINVAL;
984263bc
MD
165 break;
166
167 default:
168 error = EINVAL;
169 }
170
171 return (error);
172}
173
a35cc233 174int bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
984263bc
MD
175{
176 if (oldaddr)
177 *oldaddr = 0; /* XXX */
178
179 return (IIC_ENOADDR);
180}
181
a35cc233 182void bti2c_iic_setsda(device_t dev, int val)
984263bc 183{
a35cc233
JS
184 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
185 int clock;
186
187 clock = INL(sc, BKTR_I2C_DATA_CTL) & 0x2;
188
189 if (val)
190 OUTL(sc, BKTR_I2C_DATA_CTL, clock | 1);
191 else
192 OUTL(sc, BKTR_I2C_DATA_CTL, clock);
984263bc 193
984263bc
MD
194 DELAY(I2C_DELAY);
195
196 return;
197}
198
a35cc233
JS
199void bti2c_iic_setscl(device_t dev, int val)
200{
201 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
202 int data;
203
204 data = INL(sc, BKTR_I2C_DATA_CTL) & 0x1;
205
206 if (val)
207 OUTL(sc, BKTR_I2C_DATA_CTL, 0x2 | data);
208 else
209 OUTL(sc, BKTR_I2C_DATA_CTL, data);
210
211 DELAY(I2C_DELAY);
212
213 return;
214}
215
216int
217bti2c_iic_getsda(device_t dev)
984263bc 218{
a35cc233 219 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
984263bc 220
a35cc233
JS
221 return (INL(sc,BKTR_I2C_DATA_CTL) & 0x1);
222}
223
224int
225bti2c_iic_getscl(device_t dev)
226{
227 return (0);
984263bc
MD
228}
229
230static int
a35cc233 231bti2c_write(struct bktr_softc *sc, u_long data)
984263bc
MD
232{
233 u_long x;
234
235 /* clear status bits */
a35cc233 236 OUTL(sc, BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
984263bc
MD
237
238 BTI2C_DEBUG(printf("w%lx", data));
239
240 /* write the address and data */
a35cc233 241 OUTL(sc, BKTR_I2C_DATA_CTL, data);
984263bc
MD
242
243 /* wait for completion */
244 for ( x = 0x7fffffff; x; --x ) { /* safety valve */
a35cc233 245 if ( INL(sc, BKTR_INT_STAT) & BT848_INT_I2CDONE )
984263bc
MD
246 break;
247 }
248
249 /* check for ACK */
a35cc233 250 if ( !x || !( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK) ) {
984263bc 251 BTI2C_DEBUG(printf("%c%c", (!x)?'+':'-',
a35cc233 252 (!( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
984263bc
MD
253 return (SMB_ENOACK);
254 }
255 BTI2C_DEBUG(printf("+"));
256
257 /* return OK */
258 return( 0 );
259}
260
a35cc233 261int
984263bc
MD
262bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
263{
a35cc233 264 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
984263bc
MD
265 u_long data;
266
267 data = ((slave & 0xff) << 24) | ((byte & 0xff) << 16) | (u_char)cmd;
268
269 return (bti2c_write(sc, data));
270}
271
272/*
273 * byte1 becomes low byte of word
274 * byte2 becomes high byte of word
275 */
a35cc233 276int
984263bc
MD
277bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word)
278{
a35cc233 279 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
984263bc
MD
280 u_long data;
281 char low, high;
282
283 low = (char)(word & 0xff);
284 high = (char)((word & 0xff00) >> 8);
285
286 data = ((slave & 0xff) << 24) | ((low & 0xff) << 16) |
287 ((high & 0xff) << 8) | BT848_DATA_CTL_I2CW3B | (u_char)cmd;
288
289 return (bti2c_write(sc, data));
290}
291
292/*
293 * The Bt878 and Bt879 differed on the treatment of i2c commands
294 */
a35cc233 295int
984263bc
MD
296bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
297{
a35cc233 298 struct bktr_softc *sc = (struct bktr_softc *)device_get_softc(dev);
984263bc
MD
299 u_long x;
300
301 /* clear status bits */
302 OUTL(sc,BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
303
fc6d0222 304 OUTL(sc,BKTR_I2C_DATA_CTL, ((slave & 0xff) << 24) | (u_char)cmd);
984263bc
MD
305
306 BTI2C_DEBUG(printf("r%lx/", (u_long)(((slave & 0xff) << 24) | (u_char)cmd)));
307
308 /* wait for completion */
309 for ( x = 0x7fffffff; x; --x ) { /* safety valve */
310 if ( INL(sc,BKTR_INT_STAT) & BT848_INT_I2CDONE )
311 break;
312 }
313
314 /* check for ACK */
315 if ( !x || !(INL(sc,BKTR_INT_STAT) & BT848_INT_RACK) ) {
316 BTI2C_DEBUG(printf("r%c%c", (!x)?'+':'-',
317 (!( INL(sc,BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
318 return (SMB_ENOACK);
319 }
320
321 *byte = (char)((INL(sc,BKTR_I2C_DATA_CTL) >> 8) & 0xff);
322 BTI2C_DEBUG(printf("r%x+", *byte));
323
324 return (0);
325}
326
a35cc233 327#endif /* defined(BKTR_USE_FREEBSD_SMBUS) */