2 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3 * Copyright (c) 2011, Aleksandr Rybalko <ray@FreeBSD.org>
4 * Copyright (c) 2013, Alexander A. Mityaev <sansan@adm.ua>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <sys/mutex.h>
43 #include <machine/bus.h>
44 #include <machine/cpu.h>
45 //#include <machine/pmap.h>
47 #include <dev/spibus/spi.h>
48 #include <dev/spibus/spibusvar.h>
49 #include "spibus_if.h"
51 #include "opt_platform.h"
54 #include <dev/ofw/openfirm.h>
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
59 #include <mips/rt305x/rt305xreg.h>
60 #include <dev/flash/mx25lreg.h>
62 #undef RT305X_SPI_DEBUG
63 #ifdef RT305X_SPI_DEBUG
64 #define dprintf printf
66 #define dprintf(x, arg...)
70 * register space access macros
72 #define SPI_WRITE(sc, reg, val) do { \
73 bus_write_4(sc->sc_mem_res, (reg), (val)); \
76 #define SPI_READ(sc, reg) bus_read_4(sc->sc_mem_res, (reg))
78 #define SPI_SET_BITS(sc, reg, bits) \
79 SPI_WRITE(sc, reg, SPI_READ(sc, (reg)) | (bits))
81 #define SPI_CLEAR_BITS(sc, reg, bits) \
82 SPI_WRITE(sc, reg, SPI_READ(sc, (reg)) & ~(bits))
84 struct rt305x_spi_softc {
86 struct resource *sc_mem_res;
89 static int rt305x_spi_probe(device_t);
90 static int rt305x_spi_attach(device_t);
91 static int rt305x_spi_detach(device_t);
92 static int rt305x_spi_wait(struct rt305x_spi_softc *);
93 static void rt305x_spi_chip_activate(struct rt305x_spi_softc *);
94 static void rt305x_spi_chip_deactivate(struct rt305x_spi_softc *);
95 static uint8_t rt305x_spi_txrx(struct rt305x_spi_softc *, uint8_t *, int);
96 static int rt305x_spi_transfer(device_t, device_t, struct spi_command *);
98 static phandle_t rt305x_spi_get_node(device_t, device_t);
102 rt305x_spi_probe(device_t dev)
105 if (!ofw_bus_is_compatible(dev, "ralink,rt305x-spi"))
108 device_set_desc(dev, "RT305X SPI");
113 rt305x_spi_attach(device_t dev)
115 struct rt305x_spi_softc *sc = device_get_softc(dev);
120 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
122 if (!sc->sc_mem_res) {
123 device_printf(dev, "Could not map memory\n");
127 if (rt305x_spi_wait(sc)) {
128 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
132 SPI_WRITE(sc, RT305X_SPICFG, MSBFIRST | SPICLKPOL | TX_ON_CLK_FALL |
133 SPI_CLK_DIV8); /* XXX: make it configurable */
135 * W25Q64CV max 104MHz, bus 120-192 MHz, so divide by 2.
136 * Update: divide by 4, DEV2 to fast for flash.
139 device_add_child(dev, "spibus", 0);
140 return (bus_generic_attach(dev));
144 rt305x_spi_detach(device_t dev)
146 struct rt305x_spi_softc *sc = device_get_softc(dev);
148 SPI_SET_BITS(sc, RT305X_SPICTL, HIZSMOSI | CS_HIGH);
151 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
157 rt305x_spi_chip_activate(struct rt305x_spi_softc *sc)
159 // printf("%s\n", __func__);
164 SPI_CLEAR_BITS(sc, RT305X_SPICTL, CS_HIGH | HIZSMOSI);
168 rt305x_spi_chip_deactivate(struct rt305x_spi_softc *sc)
170 // printf("%s\n", __func__);
173 * Put all CSx to high
175 SPI_SET_BITS(sc, RT305X_SPICTL, CS_HIGH | HIZSMOSI);
179 rt305x_spi_wait(struct rt305x_spi_softc *sc)
184 if (!SPI_READ(sc, RT305X_SPIBUSY))
196 rt305x_spi_txrx(struct rt305x_spi_softc *sc, uint8_t *data, int write)
199 if (rt305x_spi_wait(sc))
202 if (write == RT305X_SPI_WRITE) {
203 SPI_WRITE(sc, RT305X_SPIDATA, *data);
204 SPI_SET_BITS(sc, RT305X_SPICTL, START_WRITE);
205 //printf("%s(W:%d)\n", __func__, *data);
206 } else {/* RT305X_SPI_READ */
207 SPI_SET_BITS(sc, RT305X_SPICTL, START_READ);
208 if (rt305x_spi_wait(sc))
211 *data = SPI_READ(sc, RT305X_SPIDATA) & 0xff;
212 //printf("%s(R:%d)\n", __func__, *data);
218 rt305x_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
220 struct rt305x_spi_softc *sc;
221 uint8_t *buf, byte, *tx_buf;
222 struct spibus_ivar *devi = SPIBUS_IVAR(child);
223 int i, sz, error = 0, write = 0;
225 sc = device_get_softc(dev);
231 /* There is always a command to transfer. */
232 tx_buf = (uint8_t *)(cmd->tx_cmd);
234 /* Perform some fixup because RT305X dont support duplex SPI */
240 case CMD_WRITE_ENABLE:
241 case CMD_WRITE_DISABLE:
245 case CMD_READ_STATUS:
253 cmd->rx_cmd_sz = cmd->tx_data_sz = 0;
255 case CMD_SECTOR_ERASE:
257 cmd->rx_cmd_sz = cmd->tx_data_sz = 0;
259 case CMD_PAGE_PROGRAM:
261 cmd->rx_cmd_sz = cmd->rx_data_sz = 0;
265 rt305x_spi_chip_activate(sc);
267 if (cmd->tx_cmd_sz + cmd->rx_cmd_sz) {
268 buf = (uint8_t *)(cmd->rx_cmd);
269 tx_buf = (uint8_t *)(cmd->tx_cmd);
270 sz = cmd->tx_cmd_sz + cmd->rx_cmd_sz;
272 for (i = 0; i < sz; i++) {
273 if(i < cmd->tx_cmd_sz) {
275 error = rt305x_spi_txrx(sc, &byte,
278 goto rt305x_spi_transfer_fail;
281 error = rt305x_spi_txrx(sc, &byte,
284 goto rt305x_spi_transfer_fail;
290 * Transfer/Receive data
293 if (cmd->tx_data_sz + cmd->rx_data_sz) {
294 write = (cmd->tx_data_sz > 0)?1:0;
295 buf = (uint8_t *)(write ? cmd->tx_data : cmd->rx_data);
296 sz = write ? cmd->tx_data_sz : cmd->rx_data_sz;
298 for (i = 0; i < sz; i++) {
300 error = rt305x_spi_txrx(sc, &byte,
301 write?RT305X_SPI_WRITE:RT305X_SPI_READ);
303 goto rt305x_spi_transfer_fail;
307 rt305x_spi_transfer_fail:
308 rt305x_spi_chip_deactivate(sc);
315 rt305x_spi_get_node(device_t bus, device_t dev)
318 /* We only have one child, the SPI bus, which needs our own node. */
319 return (ofw_bus_get_node(bus));
323 static device_method_t rt305x_spi_methods[] = {
324 /* Device interface */
325 DEVMETHOD(device_probe, rt305x_spi_probe),
326 DEVMETHOD(device_attach, rt305x_spi_attach),
327 DEVMETHOD(device_detach, rt305x_spi_detach),
329 DEVMETHOD(spibus_transfer, rt305x_spi_transfer),
332 /* ofw_bus interface */
333 DEVMETHOD(ofw_bus_get_node, rt305x_spi_get_node),
339 static driver_t rt305x_spi_driver = {
341 .methods = rt305x_spi_methods,
342 .size = sizeof(struct rt305x_spi_softc),
345 static devclass_t rt305x_spi_devclass;
347 DRIVER_MODULE(rt305x_spi, obio, rt305x_spi_driver, rt305x_spi_devclass, 0, 0);
349 DRIVER_MODULE(rt305x_spi, simplebus, rt305x_spi_driver, rt305x_spi_devclass, 0, 0);