2 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/interrupt.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
43 #include <vm/vm_extern.h>
45 #include <machine/bus.h>
46 #include <machine/cpu.h>
47 #include <machine/pmap.h>
49 #include <dev/spibus/spi.h>
50 #include <dev/spibus/spibusvar.h>
51 #include "spibus_if.h"
53 #include <mips/atheros/ar71xxreg.h>
55 #undef AR71XX_SPI_DEBUG
56 #ifdef AR71XX_SPI_DEBUG
57 #define dprintf printf
59 #define dprintf(x, arg...)
63 * register space access macros
65 #define SPI_WRITE(sc, reg, val) do { \
66 bus_write_4(sc->sc_mem_res, (reg), (val)); \
69 #define SPI_READ(sc, reg) bus_read_4(sc->sc_mem_res, (reg))
71 #define SPI_SET_BITS(sc, reg, bits) \
72 SPI_WRITE(sc, reg, SPI_READ(sc, (reg)) | (bits))
74 #define SPI_CLEAR_BITS(sc, reg, bits) \
75 SPI_WRITE(sc, reg, SPI_READ(sc, (reg)) & ~(bits))
77 struct ar71xx_spi_softc {
79 struct resource *sc_mem_res;
84 ar71xx_spi_probe(device_t dev)
86 device_set_desc(dev, "AR71XX SPI");
87 return (BUS_PROBE_NOWILDCARD);
91 ar71xx_spi_attach(device_t dev)
93 struct ar71xx_spi_softc *sc = device_get_softc(dev);
98 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
100 if (!sc->sc_mem_res) {
101 device_printf(dev, "Could not map memory\n");
106 SPI_WRITE(sc, AR71XX_SPI_FS, 1);
107 sc->sc_reg_ctrl = SPI_READ(sc, AR71XX_SPI_CTRL);
108 SPI_WRITE(sc, AR71XX_SPI_CTRL, 0x43);
109 SPI_WRITE(sc, AR71XX_SPI_IO_CTRL, SPI_IO_CTRL_CSMASK);
111 device_add_child(dev, "spibus", -1);
112 return (bus_generic_attach(dev));
116 ar71xx_spi_chip_activate(struct ar71xx_spi_softc *sc, int cs)
118 uint32_t ioctrl = SPI_IO_CTRL_CSMASK;
120 * Put respective CSx to low
122 ioctrl &= ~(SPI_IO_CTRL_CS0 << cs);
124 SPI_WRITE(sc, AR71XX_SPI_IO_CTRL, ioctrl);
128 ar71xx_spi_chip_deactivate(struct ar71xx_spi_softc *sc, int cs)
131 * Put all CSx to high
133 SPI_WRITE(sc, AR71XX_SPI_IO_CTRL, SPI_IO_CTRL_CSMASK);
137 ar71xx_spi_txrx(struct ar71xx_spi_softc *sc, int cs, uint8_t data)
141 uint32_t ioctrl = SPI_IO_CTRL_CSMASK;
143 * low-level for selected CS
145 ioctrl &= ~(SPI_IO_CTRL_CS0 << cs);
148 for (bit = 7; bit >=0; bit--) {
149 if (data & (1 << bit))
150 iod = ioctrl | SPI_IO_CTRL_DO;
152 iod = ioctrl & ~SPI_IO_CTRL_DO;
153 SPI_WRITE(sc, AR71XX_SPI_IO_CTRL, iod);
154 SPI_WRITE(sc, AR71XX_SPI_IO_CTRL, iod | SPI_IO_CTRL_CLK);
158 * Provide falling edge for connected device by clear clock bit.
160 SPI_WRITE(sc, AR71XX_SPI_IO_CTRL, iod);
161 rds = SPI_READ(sc, AR71XX_SPI_RDS);
167 ar71xx_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
169 struct ar71xx_spi_softc *sc;
170 uint8_t *buf_in, *buf_out;
171 struct spibus_ivar *devi = SPIBUS_IVAR(child);
174 sc = device_get_softc(dev);
176 ar71xx_spi_chip_activate(sc, devi->cs);
178 KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz,
179 ("TX/RX command sizes should be equal"));
180 KASSERT(cmd->tx_data_sz == cmd->rx_data_sz,
181 ("TX/RX data sizes should be equal"));
186 buf_out = (uint8_t *)cmd->tx_cmd;
187 buf_in = (uint8_t *)cmd->rx_cmd;
188 for (i = 0; i < cmd->tx_cmd_sz; i++)
189 buf_in[i] = ar71xx_spi_txrx(sc, devi->cs, buf_out[i]);
192 * Receive/transmit data (depends on command)
194 buf_out = (uint8_t *)cmd->tx_data;
195 buf_in = (uint8_t *)cmd->rx_data;
196 for (i = 0; i < cmd->tx_data_sz; i++)
197 buf_in[i] = ar71xx_spi_txrx(sc, devi->cs, buf_out[i]);
199 ar71xx_spi_chip_deactivate(sc, devi->cs);
205 ar71xx_spi_detach(device_t dev)
207 struct ar71xx_spi_softc *sc = device_get_softc(dev);
209 SPI_WRITE(sc, AR71XX_SPI_CTRL, sc->sc_reg_ctrl);
210 SPI_WRITE(sc, AR71XX_SPI_FS, 0);
213 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
218 static device_method_t ar71xx_spi_methods[] = {
219 /* Device interface */
220 DEVMETHOD(device_probe, ar71xx_spi_probe),
221 DEVMETHOD(device_attach, ar71xx_spi_attach),
222 DEVMETHOD(device_detach, ar71xx_spi_detach),
224 DEVMETHOD(spibus_transfer, ar71xx_spi_transfer),
229 static driver_t ar71xx_spi_driver = {
232 sizeof(struct ar71xx_spi_softc),
235 static devclass_t ar71xx_spi_devclass;
237 DRIVER_MODULE(ar71xx_spi, nexus, ar71xx_spi_driver, ar71xx_spi_devclass, 0, 0);