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 THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
29 #include <sys/systm.h>
32 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
39 #include <machine/bus.h>
41 #include <arm/at91/at91_spireg.h>
42 #include <arm/at91/at91_spiio.h>
46 device_t dev; /* Myself */
47 void *intrhand; /* Interrupt handle */
48 struct resource *irq_res; /* IRQ resource */
49 struct resource *mem_res; /* Memory resource */
50 struct mtx sc_mtx; /* basically a perimeter lock */
52 #define XFER_PENDING 1 /* true when transfer taking place */
53 #define OPENED 2 /* Device opened */
60 static inline uint32_t
61 RD4(struct at91_spi_softc *sc, bus_size_t off)
63 return bus_read_4(sc->mem_res, off);
67 WR4(struct at91_spi_softc *sc, bus_size_t off, uint32_t val)
69 bus_write_4(sc->mem_res, off, val);
72 #define AT91_SPI_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
73 #define AT91_SPI_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
74 #define AT91_SPI_LOCK_INIT(_sc) \
75 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
77 #define AT91_SPI_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
78 #define AT91_SPI_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
79 #define AT91_SPI_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
80 #define CDEV2SOFTC(dev) ((dev)->si_drv1)
82 static devclass_t at91_spi_devclass;
84 /* bus entry points */
86 static int at91_spi_probe(device_t dev);
87 static int at91_spi_attach(device_t dev);
88 static int at91_spi_detach(device_t dev);
89 static void at91_spi_intr(void *);
92 static int at91_spi_activate(device_t dev);
93 static void at91_spi_deactivate(device_t dev);
96 static d_open_t at91_spi_open;
97 static d_close_t at91_spi_close;
98 static d_ioctl_t at91_spi_ioctl;
100 static struct cdevsw at91_spi_cdevsw =
102 .d_version = D_VERSION,
103 .d_open = at91_spi_open,
104 .d_close = at91_spi_close,
105 .d_ioctl = at91_spi_ioctl
109 at91_spi_probe(device_t dev)
111 device_set_desc(dev, "SPI");
116 at91_spi_attach(device_t dev)
118 struct at91_spi_softc *sc = device_get_softc(dev);
122 err = at91_spi_activate(dev);
126 AT91_SPI_LOCK_INIT(sc);
129 * Activate the interrupt
131 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
132 at91_spi_intr, sc, &sc->intrhand);
134 AT91_SPI_LOCK_DESTROY(sc);
137 sc->cdev = make_dev(&at91_spi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
138 "spi%d", device_get_unit(dev));
139 if (sc->cdev == NULL) {
143 sc->cdev->si_drv1 = sc;
146 sc->cwgr = SPI_CWGR_CKDIV(1) |
147 SPI_CWGR_CHDIV(SPI_CWGR_DIV(SPI_DEF_CLK)) |
148 SPI_CWGR_CLDIV(SPI_CWGR_DIV(SPI_DEF_CLK));
150 WR4(sc, SPI_CR, SPI_CR_SWRST);
151 WR4(sc, SPI_CR, SPI_CR_MSEN | SPI_CR_SVDIS);
152 WR4(sc, SPI_CWGR, sc->cwgr);
156 at91_spi_deactivate(dev);
161 at91_spi_detach(device_t dev)
163 return (EBUSY); /* XXX */
167 at91_spi_activate(device_t dev)
169 struct at91_spi_softc *sc;
172 sc = device_get_softc(dev);
174 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
176 if (sc->mem_res == NULL)
179 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
181 if (sc->mem_res == NULL)
185 at91_spi_deactivate(dev);
190 at91_spi_deactivate(device_t dev)
192 struct at91_spi_softc *sc;
194 sc = device_get_softc(dev);
196 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
198 bus_generic_detach(sc->dev);
200 bus_release_resource(dev, SYS_RES_IOPORT,
201 rman_get_rid(sc->mem_res), sc->mem_res);
204 bus_release_resource(dev, SYS_RES_IRQ,
205 rman_get_rid(sc->irq_res), sc->irq_res);
211 at91_spi_intr(void *xsc)
213 struct at91_spi_softc *sc = xsc;
217 /* Reading the status also clears the interrupt */
218 status = RD4(sc, SPI_SR);
222 if (status & SPI_SR_RXRDY)
224 if (status & SPI_SR_TXCOMP)
226 if (status & SPI_SR_TXRDY)
235 at91_spi_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
237 struct at91_spi_softc *sc;
239 sc = CDEV2SOFTC(dev);
241 if (!(sc->flags & OPENED)) {
244 WR4(sc, SPI_IER, SPI_SR_TXCOMP | SPI_SR_RXRDY | SPI_SR_TXRDY |
245 SPI_SR_OVRE | SPI_SR_UNRE | SPI_SR_NACK);
253 at91_spi_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
255 struct at91_spi_softc *sc;
257 sc = CDEV2SOFTC(dev);
259 sc->flags &= ~OPENED;
261 WR4(sc, SPI_IDR, SPI_SR_TXCOMP | SPI_SR_RXRDY | SPI_SR_TXRDY |
262 SPI_SR_OVRE | SPI_SR_UNRE | SPI_SR_NACK);
269 at91_spi_read_master(struct at91_spi_softc *sc, struct at91_spi_io *xfr)
279 if (xfr->xfer_len > sizeof(buffer))
284 // Master mode, with the right address and interal addr size
285 WR4(sc, SPI_MMR, SPI_MMR_IADRSZ(xfr->iadrsz) | SPI_MMR_MREAD |
286 SPI_MMR_DADR(xfr->dadr));
287 WR4(sc, SPI_IADR, xfr->iadr);
288 WR4(sc, SPI_CR, SPI_CR_START);
290 while (!(sc->flags & RXRDY)) {
291 err = msleep(sc, &sc->sc_mtx, PZERO | PCATCH, "spird",
297 *walker++ = RD4(sc, SPI_RHR) & 0xff;
299 WR4(sc, SPI_CR, SPI_CR_STOP);
300 while (!(sc->flags & TXCOMP)) {
301 err = msleep(sc, &sc->sc_mtx, PZERO | PCATCH, "spird2", 0);
305 sc->flags &= ~TXCOMP;
306 *walker = RD4(sc, SPI_RHR) & 0xff;
309 err = copyout(buffer, xfr->xfer_buf, xfr->xfer_len);
317 at91_spi_write_master(struct at91_spi_softc *sc, struct at91_spi_io *xfr)
327 if (xfr->xfer_len > sizeof(buffer))
332 err = copyin(xfr->xfer_buf, buffer, xfr->xfer_len);
336 /* Setup the xfr for later readback */
340 WR4(sc, SPI_MMR, SPI_MMR_IADRSZ(xfr->iadrsz) | SPI_MMR_MWRITE |
341 SPI_MMR_DADR(xfr->dadr));
342 WR4(sc, SPI_IADR, xfr->iadr++);
343 WR4(sc, SPI_THR, *walker++);
344 WR4(sc, SPI_CR, SPI_CR_START);
346 * If we get signal while waiting for TXRDY, make sure we
347 * try to stop this device
349 while (!(sc->flags & TXRDY)) {
350 err = msleep(sc, &sc->sc_mtx, PZERO | PCATCH, "spiwr",
355 WR4(sc, SPI_CR, SPI_CR_STOP);
358 while (!(sc->flags & TXCOMP)) {
359 err = msleep(sc, &sc->sc_mtx, PZERO | PCATCH, "spiwr2",
365 at91_spi_read_master(sc, xfr);
372 at91_spi_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
376 struct at91_spi_softc *sc;
378 sc = CDEV2SOFTC(dev);
380 while (sc->flags & XFER_PENDING) {
381 err = msleep(sc, &sc->sc_mtx, PZERO | PCATCH,
388 sc->flags |= XFER_PENDING;
394 struct at91_spi_io *xfr = (struct at91_spi_io *)data;
397 case SPI_IO_READ_MASTER:
398 err = at91_spi_read_master(sc, xfr);
400 case SPI_IO_WRITE_MASTER:
401 err = at91_spi_write_master(sc, xfr);
413 struct at91_spi_clock *spick = (struct at91_spi_clock *)data;
415 sc->cwgr = SPI_CWGR_CKDIV(spick->ckdiv) |
416 SPI_CWGR_CHDIV(SPI_CWGR_DIV(spick->high_rate)) |
417 SPI_CWGR_CLDIV(SPI_CWGR_DIV(spick->low_rate));
418 WR4(sc, SPI_CR, SPI_CR_SWRST);
419 WR4(sc, SPI_CR, SPI_CR_MSEN | SPI_CR_SVDIS);
420 WR4(sc, SPI_CWGR, sc->cwgr);
430 sc->flags &= ~XFER_PENDING;
436 static device_method_t at91_spi_methods[] = {
437 /* Device interface */
438 DEVMETHOD(device_probe, at91_spi_probe),
439 DEVMETHOD(device_attach, at91_spi_attach),
440 DEVMETHOD(device_detach, at91_spi_detach),
445 static driver_t at91_spi_driver = {
448 sizeof(struct at91_spi_softc),
451 DRIVER_MODULE(at91_spi, atmelarm, at91_spi_driver, at91_spi_devclass, 0, 0);