2 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include "opt_platform.h"
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
42 #include <machine/bus.h>
44 #include <arm/at91/at91_twireg.h>
45 #include <arm/at91/at91var.h>
47 #include <dev/iicbus/iiconf.h>
48 #include <dev/iicbus/iicbus.h>
49 #include "iicbus_if.h"
52 #include <dev/fdt/fdt_common.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
57 #define TWI_SLOW_CLOCK 1500
58 #define TWI_FAST_CLOCK 45000
59 #define TWI_FASTEST_CLOCK 90000
63 device_t dev; /* Myself */
64 void *intrhand; /* Interrupt handle */
65 struct resource *irq_res; /* IRQ resource */
66 struct resource *mem_res; /* Memory resource */
67 struct mtx sc_mtx; /* basically a perimeter lock */
68 volatile uint32_t flags;
75 static inline uint32_t
76 RD4(struct at91_twi_softc *sc, bus_size_t off)
79 return bus_read_4(sc->mem_res, off);
83 WR4(struct at91_twi_softc *sc, bus_size_t off, uint32_t val)
86 bus_write_4(sc->mem_res, off, val);
89 #define AT91_TWI_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
90 #define AT91_TWI_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
91 #define AT91_TWI_LOCK_INIT(_sc) \
92 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
94 #define AT91_TWI_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
95 #define AT91_TWI_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
96 #define AT91_TWI_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
97 #define TWI_DEF_CLK 100000
99 static devclass_t at91_twi_devclass;
101 /* bus entry points */
103 static int at91_twi_probe(device_t dev);
104 static int at91_twi_attach(device_t dev);
105 static int at91_twi_detach(device_t dev);
106 static void at91_twi_intr(void *);
108 /* helper routines */
109 static int at91_twi_activate(device_t dev);
110 static void at91_twi_deactivate(device_t dev);
113 at91_twi_probe(device_t dev)
116 /* XXXX need a whole list, since there's at least 4 different ones */
117 if (!ofw_bus_is_compatible(dev, "atmel,at91sam9g20-i2c"))
120 device_set_desc(dev, "TWI");
125 at91_twi_attach(device_t dev)
127 struct at91_twi_softc *sc = device_get_softc(dev);
131 err = at91_twi_activate(dev);
135 AT91_TWI_LOCK_INIT(sc);
139 * Disable devices need to hold their resources, so return now and not attach
140 * the iicbus, setup interrupt handlers, etc.
142 if (!ofw_bus_status_okay(dev))
147 * Activate the interrupt
149 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
150 NULL, at91_twi_intr, sc, &sc->intrhand);
152 AT91_TWI_LOCK_DESTROY(sc);
155 sc->cwgr = TWI_CWGR_CKDIV(8 * at91_master_clock / TWI_FASTEST_CLOCK) |
156 TWI_CWGR_CHDIV(TWI_CWGR_DIV(TWI_DEF_CLK)) |
157 TWI_CWGR_CLDIV(TWI_CWGR_DIV(TWI_DEF_CLK));
158 WR4(sc, TWI_CR, TWI_CR_SWRST);
159 WR4(sc, TWI_CR, TWI_CR_MSEN | TWI_CR_SVDIS);
160 WR4(sc, TWI_CWGR, sc->cwgr);
162 if ((sc->iicbus = device_add_child(dev, "iicbus", -1)) == NULL)
163 device_printf(dev, "could not allocate iicbus instance\n");
164 /* probe and attach the iicbus */
165 bus_generic_attach(dev);
168 at91_twi_deactivate(dev);
173 at91_twi_detach(device_t dev)
175 struct at91_twi_softc *sc;
178 sc = device_get_softc(dev);
179 at91_twi_deactivate(dev);
180 if (sc->iicbus && (rv = device_delete_child(dev, sc->iicbus)) != 0)
183 AT91_TWI_LOCK_DESTROY(sc);
189 at91_twi_activate(device_t dev)
191 struct at91_twi_softc *sc;
194 sc = device_get_softc(dev);
196 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
198 if (sc->mem_res == NULL)
201 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
203 if (sc->irq_res == NULL)
207 at91_twi_deactivate(dev);
212 at91_twi_deactivate(device_t dev)
214 struct at91_twi_softc *sc;
216 sc = device_get_softc(dev);
218 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
220 bus_generic_detach(sc->dev);
222 bus_release_resource(dev, SYS_RES_MEMORY,
223 rman_get_rid(sc->mem_res), sc->mem_res);
226 bus_release_resource(dev, SYS_RES_IRQ,
227 rman_get_rid(sc->irq_res), sc->irq_res);
233 at91_twi_intr(void *xsc)
235 struct at91_twi_softc *sc = xsc;
238 status = RD4(sc, TWI_SR);
242 sc->flags |= status & (TWI_SR_OVRE | TWI_SR_UNRE | TWI_SR_NACK);
243 if (status & TWI_SR_RXRDY)
244 sc->flags |= TWI_SR_RXRDY;
245 if (status & TWI_SR_TXRDY)
246 sc->flags |= TWI_SR_TXRDY;
247 if (status & TWI_SR_TXCOMP)
248 sc->flags |= TWI_SR_TXCOMP;
249 WR4(sc, TWI_IDR, status);
256 at91_twi_wait(struct at91_twi_softc *sc, uint32_t bit)
259 int counter = 100000;
262 AT91_TWI_ASSERT_LOCKED(sc);
263 while (!((sr = RD4(sc, TWI_SR)) & bit) && counter-- > 0 &&
268 else if (sr & TWI_SR_NACK)
269 err = ENXIO; // iic nack convention
274 at91_twi_rst_card(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
276 struct at91_twi_softc *sc;
279 sc = device_get_softc(dev);
282 *oldaddr = sc->twi_addr;
286 * speeds are for 1.5kb/s, 45kb/s and 90kb/s.
290 clk = TWI_SLOW_CLOCK;
294 clk = TWI_FAST_CLOCK;
300 clk = TWI_FASTEST_CLOCK;
303 sc->cwgr = TWI_CWGR_CKDIV(1) | TWI_CWGR_CHDIV(TWI_CWGR_DIV(clk)) |
304 TWI_CWGR_CLDIV(TWI_CWGR_DIV(clk));
305 WR4(sc, TWI_CR, TWI_CR_SWRST);
306 WR4(sc, TWI_CR, TWI_CR_MSEN | TWI_CR_SVDIS);
307 WR4(sc, TWI_CWGR, sc->cwgr);
314 at91_twi_callback(device_t dev, int index, caddr_t data)
319 case IIC_REQUEST_BUS:
322 case IIC_RELEASE_BUS:
333 at91_twi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
335 struct at91_twi_softc *sc;
341 sc = device_get_softc(dev);
344 for (i = 0; i < nmsgs; i++) {
346 * The linux atmel driver doesn't use the internal device
347 * address feature of twi. A separate i2c message needs to
348 * be written to use this.
349 * See http://lists.arm.linux.org.uk/pipermail/linux-arm-kernel/2004-September/024411.html
350 * for details. Upon reflection, we could use this as an
351 * optimization, but it is unclear the code bloat will
352 * result in faster/better operations.
354 rdwr = (msgs[i].flags & IIC_M_RD) ? TWI_MMR_MREAD : 0;
355 WR4(sc, TWI_MMR, TWI_MMR_DADR(msgs[i].slave) | rdwr);
358 /* zero byte transfers aren't allowed */
359 if (len == 0 || buf == NULL) {
363 if (len == 1 && msgs[i].flags & IIC_M_RD)
364 WR4(sc, TWI_CR, TWI_CR_START | TWI_CR_STOP);
366 WR4(sc, TWI_CR, TWI_CR_START);
367 if (msgs[i].flags & IIC_M_RD) {
368 sr = RD4(sc, TWI_SR);
369 while (!(sr & TWI_SR_TXCOMP)) {
370 if ((sr = RD4(sc, TWI_SR)) & TWI_SR_RXRDY) {
372 *buf++ = RD4(sc, TWI_RHR) & 0xff;
374 WR4(sc, TWI_CR, TWI_CR_STOP);
377 if (len > 0 || (sr & TWI_SR_NACK)) {
378 err = ENXIO; // iic nack convention
383 if ((err = at91_twi_wait(sc, TWI_SR_TXRDY)))
385 WR4(sc, TWI_THR, *buf++);
387 WR4(sc, TWI_CR, TWI_CR_STOP);
389 if ((err = at91_twi_wait(sc, TWI_SR_TXCOMP)))
394 WR4(sc, TWI_CR, TWI_CR_SWRST);
395 WR4(sc, TWI_CR, TWI_CR_MSEN | TWI_CR_SVDIS);
396 WR4(sc, TWI_CWGR, sc->cwgr);
402 static device_method_t at91_twi_methods[] = {
403 /* Device interface */
404 DEVMETHOD(device_probe, at91_twi_probe),
405 DEVMETHOD(device_attach, at91_twi_attach),
406 DEVMETHOD(device_detach, at91_twi_detach),
408 /* iicbus interface */
409 DEVMETHOD(iicbus_callback, at91_twi_callback),
410 DEVMETHOD(iicbus_reset, at91_twi_rst_card),
411 DEVMETHOD(iicbus_transfer, at91_twi_transfer),
415 static driver_t at91_twi_driver = {
418 sizeof(struct at91_twi_softc),
422 DRIVER_MODULE(at91_twi, simplebus, at91_twi_driver, at91_twi_devclass, NULL,
425 DRIVER_MODULE(at91_twi, atmelarm, at91_twi_driver, at91_twi_devclass, NULL,
428 DRIVER_MODULE(iicbus, at91_twi, iicbus_driver, iicbus_devclass, NULL, NULL);
429 MODULE_DEPEND(at91_twi, iicbus, 1, 1, 1);