xref: /freebsd/sys/powerpc/powermac/kiic.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
1 /*-
2  * Copyright (c) 2001 Tsubai Masanari.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  *	NetBSD: ki2c.c,v 1.11 2007/12/06 17:00:33 ad Exp
28  *	Id: ki2c.c,v 1.7 2002/10/05 09:56:05 tsubai Exp
29  */
30 
31 /*
32  * 	Support routines for the Keywest I2C controller.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <machine/resource.h>
43 #include <machine/bus.h>
44 #include <sys/rman.h>
45 
46 #include <dev/iicbus/iicbus.h>
47 #include <dev/iicbus/iiconf.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include "iicbus_if.h"
50 
51 /* Keywest I2C Register offsets */
52 #define MODE	0
53 #define CONTROL	1
54 #define STATUS	2
55 #define ISR	3
56 #define IER	4
57 #define ADDR	5
58 #define SUBADDR	6
59 #define DATA	7
60 
61 /* MODE */
62 #define I2C_SPEED	0x03	/* Speed mask */
63 #define  I2C_100kHz	0x00
64 #define  I2C_50kHz	0x01
65 #define  I2C_25kHz	0x02
66 #define I2C_MODE	0x0c	/* Mode mask */
67 #define  I2C_DUMBMODE	0x00	/*  Dumb mode */
68 #define  I2C_STDMODE	0x04	/*  Standard mode */
69 #define  I2C_STDSUBMODE	0x08	/*  Standard mode + sub address */
70 #define  I2C_COMBMODE	0x0c	/*  Combined mode */
71 #define I2C_PORT	0xf0	/* Port mask */
72 
73 /* CONTROL */
74 #define I2C_CT_AAK	0x01	/* Send AAK */
75 #define I2C_CT_ADDR	0x02	/* Send address(es) */
76 #define I2C_CT_STOP	0x04	/* Send STOP */
77 #define I2C_CT_START	0x08	/* Send START */
78 
79 /* STATUS */
80 #define I2C_ST_BUSY	0x01	/* Busy */
81 #define I2C_ST_LASTAAK	0x02	/* Last AAK */
82 #define I2C_ST_LASTRW	0x04	/* Last R/W */
83 #define I2C_ST_SDA	0x08	/* SDA */
84 #define I2C_ST_SCL	0x10	/* SCL */
85 
86 /* ISR/IER */
87 #define I2C_INT_DATA	0x01	/* Data byte sent/received */
88 #define I2C_INT_ADDR	0x02	/* Address sent */
89 #define I2C_INT_STOP	0x04	/* STOP condition sent */
90 #define I2C_INT_START	0x08	/* START condition sent */
91 
92 /* I2C flags */
93 #define I2C_BUSY	0x01
94 #define I2C_READING	0x02
95 #define I2C_ERROR	0x04
96 #define I2C_SELECTED	0x08
97 
98 struct kiic_softc {
99 	device_t 		 sc_dev;
100 	phandle_t		 sc_node;
101 	struct mtx 		 sc_mutex;
102 	struct resource		*sc_reg;
103 	int			 sc_irqrid;
104 	struct resource		*sc_irq;
105 	void			*sc_ih;
106 	u_int 			 sc_regstep;
107 	u_int 			 sc_flags;
108 	u_char			*sc_data;
109 	int 			 sc_resid;
110 	device_t 		 sc_iicbus;
111 };
112 
113 static int 	kiic_probe(device_t dev);
114 static int 	kiic_attach(device_t dev);
115 static void 	kiic_writereg(struct kiic_softc *sc, u_int, u_int);
116 static u_int 	kiic_readreg(struct kiic_softc *, u_int);
117 static void 	kiic_setmode(struct kiic_softc *, u_int);
118 static void 	kiic_setspeed(struct kiic_softc *, u_int);
119 static void 	kiic_intr(void *xsc);
120 static int	kiic_transfer(device_t dev, struct iic_msg *msgs,
121 		    uint32_t nmsgs);
122 static phandle_t kiic_get_node(device_t bus, device_t dev);
123 
124 static device_method_t kiic_methods[] = {
125 	/* device interface */
126 	DEVMETHOD(device_probe, 	kiic_probe),
127 	DEVMETHOD(device_attach, 	kiic_attach),
128 
129 	/* iicbus interface */
130 	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
131 	DEVMETHOD(iicbus_transfer,	kiic_transfer),
132 
133 	/* ofw_bus interface */
134 	DEVMETHOD(ofw_bus_get_node,	kiic_get_node),
135 
136 	{ 0, 0 }
137 };
138 
139 static driver_t kiic_driver = {
140 	"iichb",
141 	kiic_methods,
142 	sizeof(struct kiic_softc)
143 };
144 static devclass_t kiic_devclass;
145 
146 DRIVER_MODULE(kiic, macio, kiic_driver, kiic_devclass, 0, 0);
147 
148 static int
149 kiic_probe(device_t self)
150 {
151 	const char *name;
152 
153 	name = ofw_bus_get_name(self);
154 	if (name && strcmp(name, "i2c") == 0) {
155 		device_set_desc(self, "Keywest I2C controller");
156 		return (0);
157 	}
158 
159 	return (ENXIO);
160 }
161 
162 static int
163 kiic_attach(device_t self)
164 {
165 	struct kiic_softc *sc = device_get_softc(self);
166 	int rid, rate;
167 	phandle_t node;
168 	char name[64];
169 
170 	bzero(sc, sizeof(*sc));
171 	sc->sc_dev = self;
172 
173 	node = ofw_bus_get_node(self);
174 	if (node == 0 || node == -1) {
175 		return (EINVAL);
176 	}
177 
178 	rid = 0;
179 	sc->sc_reg = bus_alloc_resource_any(self, SYS_RES_MEMORY,
180 			&rid, RF_ACTIVE);
181 	if (sc->sc_reg == NULL) {
182 		return (ENOMEM);
183 	}
184 
185 	if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
186 		device_printf(self, "cannot get i2c-rate\n");
187 		return (ENXIO);
188 	}
189 	if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
190 		device_printf(self, "unable to find i2c address step\n");
191 		return (ENXIO);
192 	}
193 
194 	/*
195 	 * Some Keywest I2C devices have their children attached directly
196 	 * underneath them.  Some have a single 'iicbus' child with the
197 	 * devices underneath that.  Sort this out, and make sure that the
198 	 * OFW I2C layer has the correct node.
199 	 */
200 
201 	sc->sc_node = OF_child(node);
202 	if (OF_getprop(sc->sc_node,"name",name,sizeof(name)) > 0) {
203 		if (strcmp(name,"i2c-bus") != 0)
204 			sc->sc_node = node;
205 	}
206 
207 	mtx_init(&sc->sc_mutex, "kiic", NULL, MTX_DEF);
208 
209 	sc->sc_irq = bus_alloc_resource_any(self, SYS_RES_IRQ, &sc->sc_irqrid,
210 	    RF_ACTIVE);
211 	bus_setup_intr(self, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE, NULL,
212 	    kiic_intr, sc, &sc->sc_ih);
213 
214 	kiic_writereg(sc, STATUS, 0);
215 	kiic_writereg(sc, ISR, 0);
216 	kiic_writereg(sc, IER, 0);
217 
218 	kiic_setmode(sc, I2C_STDMODE);
219 	kiic_setspeed(sc, I2C_100kHz);		/* XXX rate */
220 
221 	kiic_writereg(sc, IER, I2C_INT_DATA | I2C_INT_ADDR | I2C_INT_STOP);
222 
223 	/* Add the IIC bus layer */
224 	sc->sc_iicbus = device_add_child(self, "iicbus", -1);
225 
226 	return (bus_generic_attach(self));
227 }
228 
229 static void
230 kiic_writereg(struct kiic_softc *sc, u_int reg, u_int val)
231 {
232 	bus_write_1(sc->sc_reg, sc->sc_regstep * reg, val);
233 	DELAY(10); /* register access delay */
234 }
235 
236 static u_int
237 kiic_readreg(struct kiic_softc *sc, u_int reg)
238 {
239 	return bus_read_1(sc->sc_reg, sc->sc_regstep * reg);
240 }
241 
242 static void
243 kiic_setmode(struct kiic_softc *sc, u_int mode)
244 {
245 	u_int x;
246 
247 	KASSERT((mode & ~I2C_MODE) == 0, ("bad mode"));
248 	x = kiic_readreg(sc, MODE);
249 	x &= ~I2C_MODE;
250 	x |= mode;
251 	kiic_writereg(sc, MODE, x);
252 }
253 
254 static void
255 kiic_setspeed(struct kiic_softc *sc, u_int speed)
256 {
257 	u_int x;
258 
259 	KASSERT((speed & ~I2C_SPEED) == 0, ("bad speed"));
260 	x = kiic_readreg(sc, MODE);
261 	x &= ~I2C_SPEED;
262 	x |= speed;
263 	kiic_writereg(sc, MODE, x);
264 }
265 
266 static void
267 kiic_intr(void *xsc)
268 {
269 	struct kiic_softc *sc = xsc;
270 	u_int isr;
271 	uint32_t x;
272 
273 	mtx_lock(&sc->sc_mutex);
274 	isr = kiic_readreg(sc, ISR);
275 
276 	if (isr & I2C_INT_ADDR) {
277 		sc->sc_flags |= I2C_SELECTED;
278 
279 		if (sc->sc_flags & I2C_READING) {
280 			if (sc->sc_resid > 1) {
281 				x = kiic_readreg(sc, CONTROL);
282 				x |= I2C_CT_AAK;
283 				kiic_writereg(sc, CONTROL, x);
284 			}
285 		} else {
286 			kiic_writereg(sc, DATA, *sc->sc_data++);
287 			sc->sc_resid--;
288 		}
289 	}
290 
291 	if (isr & I2C_INT_DATA) {
292 		if (sc->sc_flags & I2C_READING) {
293 			if (sc->sc_resid > 0) {
294 				*sc->sc_data++ = kiic_readreg(sc, DATA);
295 				sc->sc_resid--;
296 			}
297 
298 		} else {
299 			if (sc->sc_resid == 0) {
300 				x = kiic_readreg(sc, CONTROL);
301 				x |= I2C_CT_STOP;
302 				kiic_writereg(sc, CONTROL, x);
303 			} else {
304 				kiic_writereg(sc, DATA, *sc->sc_data++);
305 				sc->sc_resid--;
306 			}
307 		}
308 	}
309 
310 	if (isr & I2C_INT_STOP) {
311 		kiic_writereg(sc, CONTROL, 0);
312 		sc->sc_flags &= ~I2C_SELECTED;
313 		wakeup(sc->sc_dev);
314 	}
315 
316 	kiic_writereg(sc, ISR, isr);
317 	mtx_unlock(&sc->sc_mutex);
318 }
319 
320 static int
321 kiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
322 {
323 	struct kiic_softc *sc;
324 	int i, x, timo, err;
325 	uint8_t addr;
326 
327 	sc = device_get_softc(dev);
328 	timo = 100;
329 
330 	mtx_lock(&sc->sc_mutex);
331 
332 	if (sc->sc_flags & I2C_BUSY)
333 		mtx_sleep(dev, &sc->sc_mutex, 0, "kiic", timo);
334 
335 	if (sc->sc_flags & I2C_BUSY) {
336 		mtx_unlock(&sc->sc_mutex);
337 		return (ETIMEDOUT);
338 	}
339 
340 	sc->sc_flags = I2C_BUSY;
341 
342 	for (i = 0; i < nmsgs; i++) {
343 		sc->sc_data = msgs[i].buf;
344 		sc->sc_resid = msgs[i].len;
345 		sc->sc_flags = I2C_BUSY;
346 		addr = msgs[i].slave;
347 		timo = 1000 + sc->sc_resid * 200;
348 		timo += 100000;
349 
350 		if (msgs[i].flags & IIC_M_RD) {
351 			sc->sc_flags |= I2C_READING;
352 			addr |= 1;
353 		}
354 
355 		kiic_writereg(sc, ADDR, addr);
356 		kiic_writereg(sc, SUBADDR, 0x04);
357 
358 		x = kiic_readreg(sc, CONTROL) | I2C_CT_ADDR;
359 		kiic_writereg(sc, CONTROL, x);
360 
361 		err = mtx_sleep(dev, &sc->sc_mutex, 0, "kiic", timo);
362 
363 		msgs[i].len -= sc->sc_resid;
364 
365 		if ((sc->sc_flags & I2C_ERROR) || err == EWOULDBLOCK) {
366 			device_printf(sc->sc_dev, "I2C error\n");
367 			sc->sc_flags = 0;
368 			mtx_unlock(&sc->sc_mutex);
369 			return (-1);
370 		}
371 	}
372 
373 	sc->sc_flags = 0;
374 
375 	mtx_unlock(&sc->sc_mutex);
376 
377 	return (0);
378 }
379 
380 static phandle_t
381 kiic_get_node(device_t bus, device_t dev)
382 {
383 	struct kiic_softc *sc;
384 
385 	sc = device_get_softc(bus);
386 	/* We only have one child, the I2C bus, which needs our own node. */
387 
388 	return sc->sc_node;
389 }
390 
391