summaryrefslogtreecommitdiffstats
path: root/sys/dev/bktr/bktr_i2c.c
blob: 06a65473084edd2016bde2b75dba5e6007bf669a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*-
 * Copyright (c) 1998, 2001 Nicolas Souchu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/*
 * I2C support for the bti2c chipset.
 *
 * From brooktree848.c <fsmp@freefall.org>
 */

#include "opt_bktr.h"

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/uio.h>

#if __FreeBSD_version < 500014
#include <sys/select.h>
#else
#include <sys/selinfo.h>
#endif

#if (__FreeBSD_version < 500000)
#include <pci/pcivar.h>
#include <pci/pcireg.h>
#else
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#endif

#include <machine/bus.h>
#include <sys/bus.h>

#include <dev/bktr/ioctl_meteor.h>
#include <dev/bktr/ioctl_bt848.h>	/* extensions to ioctl_meteor.h */
#include <dev/bktr/bktr_reg.h>
#include <dev/bktr/bktr_i2c.h>

#include <dev/smbus/smbconf.h>
#include <dev/iicbus/iiconf.h>

/* Compilation is void if BKTR_USE_FREEBSD_SMBUS is not
 * defined. This allows bktr owners to have smbus active for there
 * motherboard and still use their bktr without smbus.
 */
#if defined(BKTR_USE_FREEBSD_SMBUS)

#define BTI2C_DEBUG(x)	if (bti2c_debug) (x)
static int bti2c_debug = 0;

/*
 * Call this to pass the address of the bktr device to the
 * bti2c_i2c layer and initialize all the I2C bus architecture
 */
int bt848_i2c_attach(device_t dev)
{
	struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
	struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;

	sc->smbus = device_add_child(dev, "smbus", -1);
	sc->iicbb = device_add_child(dev, "iicbb", -1);

	if (!sc->iicbb || !sc->smbus)
		return ENXIO;

	bus_generic_attach(dev);

	return (0);
};

int bt848_i2c_detach(device_t dev)
{
	struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
	struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
	int error = 0;

	if ((error = bus_generic_detach(dev)))
		goto error;

	if (sc->iicbb && (error = device_delete_child(dev, sc->iicbb)))
		goto error;

	if (sc->smbus && (error = device_delete_child(dev, sc->smbus)))
		goto error;

error:
	return (error);
}

int bti2c_smb_callback(device_t dev, int index, void *data)
{
	struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
	struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
	int error = 0;

	/* test each time if we already have/haven't the iicbus
	 * to avoid deadlocks
	 */
	switch (index) {
	case SMB_REQUEST_BUS:
		/* XXX test & set */
		mtx_lock(&Giant);
		if (!sc->bus_owned) {
			sc->bus_owned = 1;
		} else
			error = EWOULDBLOCK;
		mtx_unlock(&Giant);
		break;

	case SMB_RELEASE_BUS:
		/* XXX test & set */
		mtx_lock(&Giant);
		if (sc->bus_owned) {
			sc->bus_owned = 0;
		} else
			error = EINVAL;
		mtx_unlock(&Giant);
		break;

	default:
		error = EINVAL;
	}

	return (error);
}

int bti2c_iic_callback(device_t dev, int index, caddr_t *data)
{
	struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
	struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
	int error = 0;

	/* test each time if we already have/haven't the smbus
	 * to avoid deadlocks
	 */
	switch (index) {
	case IIC_REQUEST_BUS:
		/* XXX test & set */
		mtx_lock(&Giant);
		if (!sc->bus_owned) {
			sc->bus_owned = 1;
		} else
			error = EWOULDBLOCK;
		mtx_unlock(&Giant);
		break;

	case IIC_RELEASE_BUS:
		/* XXX test & set */
		mtx_lock(&Giant);
		if (sc->bus_owned) {
			sc->bus_owned = 0;
		} else
			error = EINVAL;
		mtx_unlock(&Giant);
		break;

	default:
		error = EINVAL;
	}

	return (error);
}

int bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
{
	mtx_lock(&Giant);
	if (oldaddr)
		*oldaddr = 0;			/* XXX */
	mtx_unlock(&Giant);

	return (IIC_ENOADDR);
}

void bti2c_iic_setsda(device_t dev, int val)
{
	struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
	int clock;

	mtx_lock(&Giant);
	clock = INL(sc, BKTR_I2C_DATA_CTL) & 0x2;

	if (val)
		OUTL(sc, BKTR_I2C_DATA_CTL, clock | 1);
	else
		OUTL(sc, BKTR_I2C_DATA_CTL, clock);
	mtx_unlock(&Giant);

	return;
}

void bti2c_iic_setscl(device_t dev, int val)
{
	struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
	int data;

	mtx_lock(&Giant);
	data = INL(sc, BKTR_I2C_DATA_CTL) & 0x1;

	if (val)
		OUTL(sc, BKTR_I2C_DATA_CTL, 0x2 | data);
	else
		OUTL(sc, BKTR_I2C_DATA_CTL, data);
	mtx_unlock(&Giant);

	return;
}

int
bti2c_iic_getsda(device_t dev)
{
	struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
	int retval;

	mtx_lock(&Giant);
	retval = INL(sc,BKTR_I2C_DATA_CTL) & 0x1;
	mtx_unlock(&Giant);
	return (retval);
}

int
bti2c_iic_getscl(device_t dev)
{
	return (0);
}

static int
bti2c_write(struct bktr_softc *sc, u_long data)
{
	u_long		x;

	mtx_lock(&Giant);

	/* clear status bits */
	OUTL(sc, BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));

	BTI2C_DEBUG(printf("w%lx", data));

	/* write the address and data */
	OUTL(sc, BKTR_I2C_DATA_CTL, data);

	/* wait for completion */
	for ( x = 0x7fffffff; x; --x ) {	/* safety valve */
		if ( INL(sc, BKTR_INT_STAT) & BT848_INT_I2CDONE )
			break;
	}

	/* check for ACK */
	if ( !x || !( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK) ) {
		BTI2C_DEBUG(printf("%c%c", (!x)?'+':'-',
			(!( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
		mtx_unlock(&Giant);
		return (SMB_ENOACK);
	}
	BTI2C_DEBUG(printf("+"));
	mtx_unlock(&Giant);

	/* return OK */
	return( 0 );
}

int
bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
{
	struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
	u_long data;

	data = ((slave & 0xff) << 24) | ((byte & 0xff) << 16) | (u_char)cmd;

	return (bti2c_write(sc, data));
}

/*
 * byte1 becomes low byte of word
 * byte2 becomes high byte of word
 */
int
bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word)
{
	struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
	u_long data;
	char low, high;

	low = (char)(word & 0xff);
	high = (char)((word & 0xff00) >> 8);

	data = ((slave & 0xff) << 24) | ((low & 0xff) << 16) |
		((high & 0xff) << 8) | BT848_DATA_CTL_I2CW3B | (u_char)cmd;

	return (bti2c_write(sc, data));
}

/*
 * The Bt878 and Bt879 differed on the treatment of i2c commands
 */
int
bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
{
	struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
	u_long		x;

	mtx_lock(&Giant);
	/* clear status bits */
	OUTL(sc,BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));

	OUTL(sc,BKTR_I2C_DATA_CTL, ((slave & 0xff) << 24) | (u_char)cmd);

	BTI2C_DEBUG(printf("r%lx/", (u_long)(((slave & 0xff) << 24) | (u_char)cmd)));

	/* wait for completion */
	for ( x = 0x7fffffff; x; --x ) {	/* safety valve */
		if ( INL(sc,BKTR_INT_STAT) & BT848_INT_I2CDONE )
			break;
	}

	/* check for ACK */
	if ( !x || !(INL(sc,BKTR_INT_STAT) & BT848_INT_RACK) ) {
		BTI2C_DEBUG(printf("r%c%c", (!x)?'+':'-',
			(!( INL(sc,BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
		mtx_unlock(&Giant);
		return (SMB_ENOACK);
	}

	*byte = (char)((INL(sc,BKTR_I2C_DATA_CTL) >> 8) & 0xff);
	BTI2C_DEBUG(printf("r%x+", *byte));
	mtx_unlock(&Giant);

	return (0);
}

DRIVER_MODULE(iicbb, bktr, iicbb_driver, iicbb_devclass, 0, 0);
DRIVER_MODULE(smbus, bktr, smbus_driver, smbus_devclass, 0, 0);

#endif /* defined(BKTR_USE_FREEBSD_SMBUS) */
OpenPOWER on IntegriCloud