]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/at91_twi.c
MFV: r313101
[FreeBSD/FreeBSD.git] / sys / arm / at91 / at91_twi.c
1 /*-
2  * Copyright (c) 2006 M. Warner Losh.  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  *
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
23  * SUCH DAMAGE.
24  */
25
26 #include "opt_platform.h"
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/mbuf.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/rman.h>
42 #include <machine/bus.h>
43
44 #include <arm/at91/at91_twireg.h>
45 #include <arm/at91/at91var.h>
46
47 #include <dev/iicbus/iiconf.h>
48 #include <dev/iicbus/iicbus.h>
49 #include "iicbus_if.h"
50
51 #ifdef FDT
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 #endif
55
56 #define TWI_SLOW_CLOCK           1500
57 #define TWI_FAST_CLOCK          45000
58 #define TWI_FASTEST_CLOCK       90000
59
60 struct at91_twi_softc
61 {
62         device_t dev;                   /* Myself */
63         void *intrhand;                 /* Interrupt handle */
64         struct resource *irq_res;       /* IRQ resource */
65         struct resource *mem_res;       /* Memory resource */
66         struct mtx sc_mtx;              /* basically a perimeter lock */
67         volatile uint32_t flags;
68         uint32_t cwgr;
69         int     sc_started;
70         int     twi_addr;
71         device_t iicbus;
72 };
73
74 static inline uint32_t
75 RD4(struct at91_twi_softc *sc, bus_size_t off)
76 {
77
78         return bus_read_4(sc->mem_res, off);
79 }
80
81 static inline void
82 WR4(struct at91_twi_softc *sc, bus_size_t off, uint32_t val)
83 {
84
85         bus_write_4(sc->mem_res, off, val);
86 }
87
88 #define AT91_TWI_LOCK(_sc)              mtx_lock(&(_sc)->sc_mtx)
89 #define AT91_TWI_UNLOCK(_sc)            mtx_unlock(&(_sc)->sc_mtx)
90 #define AT91_TWI_LOCK_INIT(_sc) \
91         mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
92             "twi", MTX_DEF)
93 #define AT91_TWI_LOCK_DESTROY(_sc)      mtx_destroy(&_sc->sc_mtx);
94 #define AT91_TWI_ASSERT_LOCKED(_sc)     mtx_assert(&_sc->sc_mtx, MA_OWNED);
95 #define AT91_TWI_ASSERT_UNLOCKED(_sc)   mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
96 #define TWI_DEF_CLK     100000
97
98 static devclass_t at91_twi_devclass;
99
100 /* bus entry points */
101
102 static int at91_twi_probe(device_t dev);
103 static int at91_twi_attach(device_t dev);
104 static int at91_twi_detach(device_t dev);
105 static void at91_twi_intr(void *);
106
107 /* helper routines */
108 static int at91_twi_activate(device_t dev);
109 static void at91_twi_deactivate(device_t dev);
110
111 static int
112 at91_twi_probe(device_t dev)
113 {
114 #ifdef FDT
115         /* XXXX need a whole list, since there's at least 4 different ones */
116         if (!ofw_bus_is_compatible(dev, "atmel,at91sam9g20-i2c"))
117                 return (ENXIO);
118 #endif
119         device_set_desc(dev, "TWI");
120         return (0);
121 }
122
123 static int
124 at91_twi_attach(device_t dev)
125 {
126         struct at91_twi_softc *sc = device_get_softc(dev);
127         int err;
128
129         sc->dev = dev;
130         err = at91_twi_activate(dev);
131         if (err)
132                 goto out;
133
134         AT91_TWI_LOCK_INIT(sc);
135
136 #ifdef FDT
137         /*
138          * Disable devices need to hold their resources, so return now and not attach
139          * the iicbus, setup interrupt handlers, etc.
140          */
141         if (!ofw_bus_status_okay(dev))
142                 return 0;
143 #endif
144
145         /*
146          * Activate the interrupt
147          */
148         err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
149             NULL, at91_twi_intr, sc, &sc->intrhand);
150         if (err) {
151                 AT91_TWI_LOCK_DESTROY(sc);
152                 goto out;
153         }
154         sc->cwgr = TWI_CWGR_CKDIV(8 * at91_master_clock / TWI_FASTEST_CLOCK) |
155             TWI_CWGR_CHDIV(TWI_CWGR_DIV(TWI_DEF_CLK)) |
156             TWI_CWGR_CLDIV(TWI_CWGR_DIV(TWI_DEF_CLK));
157         WR4(sc, TWI_CR, TWI_CR_SWRST);
158         WR4(sc, TWI_CR, TWI_CR_MSEN | TWI_CR_SVDIS);
159         WR4(sc, TWI_CWGR, sc->cwgr);
160
161         if ((sc->iicbus = device_add_child(dev, "iicbus", -1)) == NULL)
162                 device_printf(dev, "could not allocate iicbus instance\n");
163         /* probe and attach the iicbus */
164         bus_generic_attach(dev);
165 out:
166         if (err)
167                 at91_twi_deactivate(dev);
168         return (err);
169 }
170
171 static int
172 at91_twi_detach(device_t dev)
173 {
174         struct at91_twi_softc *sc;
175         int rv;
176
177         sc = device_get_softc(dev);
178         at91_twi_deactivate(dev);
179         if (sc->iicbus && (rv = device_delete_child(dev, sc->iicbus)) != 0)
180                 return (rv);
181
182         AT91_TWI_LOCK_DESTROY(sc);
183
184         return (0);
185 }
186
187 static int
188 at91_twi_activate(device_t dev)
189 {
190         struct at91_twi_softc *sc;
191         int rid;
192
193         sc = device_get_softc(dev);
194         rid = 0;
195         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
196             RF_ACTIVE);
197         if (sc->mem_res == NULL)
198                 goto errout;
199         rid = 0;
200         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
201             RF_ACTIVE);
202         if (sc->irq_res == NULL)
203                 goto errout;
204         return (0);
205 errout:
206         at91_twi_deactivate(dev);
207         return (ENOMEM);
208 }
209
210 static void
211 at91_twi_deactivate(device_t dev)
212 {
213         struct at91_twi_softc *sc;
214
215         sc = device_get_softc(dev);
216         if (sc->intrhand)
217                 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
218         sc->intrhand = NULL;
219         bus_generic_detach(sc->dev);
220         if (sc->mem_res)
221                 bus_release_resource(dev, SYS_RES_MEMORY,
222                     rman_get_rid(sc->mem_res), sc->mem_res);
223         sc->mem_res = NULL;
224         if (sc->irq_res)
225                 bus_release_resource(dev, SYS_RES_IRQ,
226                     rman_get_rid(sc->irq_res), sc->irq_res);
227         sc->irq_res = NULL;
228         return;
229 }
230
231 static void
232 at91_twi_intr(void *xsc)
233 {
234         struct at91_twi_softc *sc = xsc;
235         uint32_t status;
236
237         status = RD4(sc, TWI_SR);
238         if (status == 0)
239                 return;
240         AT91_TWI_LOCK(sc);
241         sc->flags |= status & (TWI_SR_OVRE | TWI_SR_UNRE | TWI_SR_NACK);
242         if (status & TWI_SR_RXRDY)
243                 sc->flags |= TWI_SR_RXRDY;
244         if (status & TWI_SR_TXRDY)
245                 sc->flags |= TWI_SR_TXRDY;
246         if (status & TWI_SR_TXCOMP)
247                 sc->flags |= TWI_SR_TXCOMP;
248         WR4(sc, TWI_IDR, status);
249         wakeup(sc);
250         AT91_TWI_UNLOCK(sc);
251         return;
252 }
253
254 static int
255 at91_twi_wait(struct at91_twi_softc *sc, uint32_t bit)
256 {
257         int err = 0;
258         int counter = 100000;
259         uint32_t sr;
260
261         AT91_TWI_ASSERT_LOCKED(sc);
262         while (!((sr = RD4(sc, TWI_SR)) & bit) && counter-- > 0 &&
263             !(sr & TWI_SR_NACK))
264                 continue;
265         if (counter <= 0)
266                 err = EBUSY;
267         else if (sr & TWI_SR_NACK)
268                 err = ENXIO;            // iic nack convention
269         return (err);
270 }
271
272 static int
273 at91_twi_rst_card(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
274 {
275         struct at91_twi_softc *sc;
276         int clk;
277
278         sc = device_get_softc(dev);
279         AT91_TWI_LOCK(sc);
280         if (oldaddr)
281                 *oldaddr = sc->twi_addr;
282         sc->twi_addr = addr;
283
284         /*
285          * speeds are for 1.5kb/s, 45kb/s and 90kb/s.
286          */
287         switch (speed) {
288         case IIC_SLOW:
289                 clk = TWI_SLOW_CLOCK;
290                 break;
291
292         case IIC_FAST:
293                 clk = TWI_FAST_CLOCK;
294                 break;
295
296         case IIC_UNKNOWN:
297         case IIC_FASTEST:
298         default:
299                 clk = TWI_FASTEST_CLOCK;
300                 break;
301         }
302         sc->cwgr = TWI_CWGR_CKDIV(1) | TWI_CWGR_CHDIV(TWI_CWGR_DIV(clk)) |
303             TWI_CWGR_CLDIV(TWI_CWGR_DIV(clk));
304         WR4(sc, TWI_CR, TWI_CR_SWRST);
305         WR4(sc, TWI_CR, TWI_CR_MSEN | TWI_CR_SVDIS);
306         WR4(sc, TWI_CWGR, sc->cwgr);
307         AT91_TWI_UNLOCK(sc);
308
309         return 0;
310 }
311
312 static int
313 at91_twi_callback(device_t dev, int index, caddr_t data)
314 {
315         int error = 0;
316
317         switch (index) {
318         case IIC_REQUEST_BUS:
319                 break;
320
321         case IIC_RELEASE_BUS:
322                 break;
323
324         default:
325                 error = EINVAL;
326         }
327
328         return (error);
329 }
330
331 static int
332 at91_twi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
333 {
334         struct at91_twi_softc *sc;
335         int i, len, err;
336         uint32_t rdwr;
337         uint8_t *buf;
338         uint32_t sr;
339
340         sc = device_get_softc(dev);
341         err = 0;
342         AT91_TWI_LOCK(sc);
343         for (i = 0; i < nmsgs; i++) {
344                 /*
345                  * The linux atmel driver doesn't use the internal device
346                  * address feature of twi.  A separate i2c message needs to
347                  * be written to use this.
348                  * See http://lists.arm.linux.org.uk/pipermail/linux-arm-kernel/2004-September/024411.html
349                  * for details.  Upon reflection, we could use this as an
350                  * optimization, but it is unclear the code bloat will
351                  * result in faster/better operations.
352                  */
353                 rdwr = (msgs[i].flags & IIC_M_RD) ? TWI_MMR_MREAD : 0;
354                 WR4(sc, TWI_MMR, TWI_MMR_DADR(msgs[i].slave) | rdwr);
355                 len = msgs[i].len;
356                 buf = msgs[i].buf;
357                 /* zero byte transfers aren't allowed */
358                 if (len == 0 || buf == NULL) {
359                         err = EINVAL;
360                         goto out;
361                 }
362                 if (len == 1 && msgs[i].flags & IIC_M_RD)
363                         WR4(sc, TWI_CR, TWI_CR_START | TWI_CR_STOP);
364                 else
365                         WR4(sc, TWI_CR, TWI_CR_START);
366                 if (msgs[i].flags & IIC_M_RD) {
367                         sr = RD4(sc, TWI_SR);
368                         while (!(sr & TWI_SR_TXCOMP)) {
369                                 if ((sr = RD4(sc, TWI_SR)) & TWI_SR_RXRDY) {
370                                         len--;
371                                         *buf++ = RD4(sc, TWI_RHR) & 0xff;
372                                         if (len == 1)
373                                                 WR4(sc, TWI_CR, TWI_CR_STOP);
374                                 }
375                         }
376                         if (len > 0 || (sr & TWI_SR_NACK)) {
377                                 err = ENXIO;            // iic nack convention
378                                 goto out;
379                         }
380                 } else {
381                         while (len--) {
382                                 if ((err = at91_twi_wait(sc, TWI_SR_TXRDY)))
383                                         goto out;
384                                 WR4(sc, TWI_THR, *buf++);
385                         }
386                         WR4(sc, TWI_CR, TWI_CR_STOP);
387                 }
388                 if ((err = at91_twi_wait(sc, TWI_SR_TXCOMP)))
389                         break;
390         }
391 out:
392         if (err) {
393                 WR4(sc, TWI_CR, TWI_CR_SWRST);
394                 WR4(sc, TWI_CR, TWI_CR_MSEN | TWI_CR_SVDIS);
395                 WR4(sc, TWI_CWGR, sc->cwgr);
396         }
397         AT91_TWI_UNLOCK(sc);
398         return (err);
399 }
400
401 static device_method_t at91_twi_methods[] = {
402         /* Device interface */
403         DEVMETHOD(device_probe,         at91_twi_probe),
404         DEVMETHOD(device_attach,        at91_twi_attach),
405         DEVMETHOD(device_detach,        at91_twi_detach),
406
407         /* iicbus interface */
408         DEVMETHOD(iicbus_callback,      at91_twi_callback),
409         DEVMETHOD(iicbus_reset,         at91_twi_rst_card),
410         DEVMETHOD(iicbus_transfer,      at91_twi_transfer),
411         DEVMETHOD_END
412 };
413
414 static driver_t at91_twi_driver = {
415         "at91_twi",
416         at91_twi_methods,
417         sizeof(struct at91_twi_softc),
418 };
419
420 #ifdef FDT
421 DRIVER_MODULE(at91_twi, simplebus, at91_twi_driver, at91_twi_devclass, NULL,
422     NULL);
423 #else
424 DRIVER_MODULE(at91_twi, atmelarm, at91_twi_driver, at91_twi_devclass, NULL,
425     NULL);
426 #endif
427 DRIVER_MODULE(iicbus, at91_twi, iicbus_driver, iicbus_devclass, NULL, NULL);
428 MODULE_DEPEND(at91_twi, iicbus, 1, 1, 1);