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>
46 #include <dev/spibus/spi.h>
47 #include <dev/spibus/spibusvar.h>
48 #include "spibus_if.h"
50 #include "opt_platform.h"
52 #include <dev/ofw/openfirm.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
56 #include <mips/mediatek/mtk_spi_v1.h>
57 #include <dev/flash/mx25lreg.h>
61 #define dprintf printf
63 #define dprintf(x, arg...)
67 * register space access macros
69 #define SPI_WRITE(sc, reg, val) do { \
70 bus_write_4(sc->sc_mem_res, (reg), (val)); \
73 #define SPI_READ(sc, reg) bus_read_4(sc->sc_mem_res, (reg))
75 #define SPI_SET_BITS(sc, reg, bits) \
76 SPI_WRITE(sc, reg, SPI_READ(sc, (reg)) | (bits))
78 #define SPI_CLEAR_BITS(sc, reg, bits) \
79 SPI_WRITE(sc, reg, SPI_READ(sc, (reg)) & ~(bits))
81 struct mtk_spi_softc {
83 struct resource *sc_mem_res;
86 static int mtk_spi_probe(device_t);
87 static int mtk_spi_attach(device_t);
88 static int mtk_spi_detach(device_t);
89 static int mtk_spi_wait(struct mtk_spi_softc *);
90 static void mtk_spi_chip_activate(struct mtk_spi_softc *);
91 static void mtk_spi_chip_deactivate(struct mtk_spi_softc *);
92 static uint8_t mtk_spi_txrx(struct mtk_spi_softc *, uint8_t *, int);
93 static int mtk_spi_transfer(device_t, device_t, struct spi_command *);
94 static phandle_t mtk_spi_get_node(device_t, device_t);
96 static struct ofw_compat_data compat_data[] = {
97 { "ralink,rt2880-spi", 1 },
98 { "ralink,rt3050-spi", 1 },
99 { "ralink,rt3352-spi", 1 },
100 { "ralink,rt3883-spi", 1 },
101 { "ralink,rt5350-spi", 1 },
102 { "ralink,mt7620a-spi", 1 },
107 mtk_spi_probe(device_t dev)
110 if (!ofw_bus_status_okay(dev))
113 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
116 device_set_desc(dev, "MTK SPI Controller (v1)");
122 mtk_spi_attach(device_t dev)
124 struct mtk_spi_softc *sc = device_get_softc(dev);
129 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
131 if (!sc->sc_mem_res) {
132 device_printf(dev, "Could not map memory\n");
136 if (mtk_spi_wait(sc)) {
137 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
141 SPI_WRITE(sc, MTK_SPICFG, MSBFIRST | SPICLKPOL | TX_ON_CLK_FALL |
142 SPI_CLK_DIV8); /* XXX: make it configurable */
144 * W25Q64CV max 104MHz, bus 120-192 MHz, so divide by 2.
145 * Update: divide by 4, DEV2 to fast for flash.
148 device_add_child(dev, "spibus", 0);
149 return (bus_generic_attach(dev));
153 mtk_spi_detach(device_t dev)
155 struct mtk_spi_softc *sc = device_get_softc(dev);
157 SPI_SET_BITS(sc, MTK_SPICTL, HIZSMOSI | CS_HIGH);
160 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
166 mtk_spi_chip_activate(struct mtk_spi_softc *sc)
172 SPI_CLEAR_BITS(sc, MTK_SPICTL, CS_HIGH | HIZSMOSI);
176 mtk_spi_chip_deactivate(struct mtk_spi_softc *sc)
180 * Put all CSx to high
182 SPI_SET_BITS(sc, MTK_SPICTL, CS_HIGH | HIZSMOSI);
186 mtk_spi_wait(struct mtk_spi_softc *sc)
191 if (!SPI_READ(sc, MTK_SPIBUSY))
203 mtk_spi_txrx(struct mtk_spi_softc *sc, uint8_t *data, int write)
206 if (mtk_spi_wait(sc))
209 if (write == MTK_SPI_WRITE) {
210 SPI_WRITE(sc, MTK_SPIDATA, *data);
211 SPI_SET_BITS(sc, MTK_SPICTL, START_WRITE);
212 } else {/* MTK_SPI_READ */
213 SPI_SET_BITS(sc, MTK_SPICTL, START_READ);
214 if (mtk_spi_wait(sc))
217 *data = SPI_READ(sc, MTK_SPIDATA) & 0xff;
223 mtk_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
225 struct mtk_spi_softc *sc;
226 uint8_t *buf, byte, *tx_buf;
228 int i, sz, error = 0, write = 0;
230 sc = device_get_softc(dev);
232 spibus_get_cs(child, &cs);
234 cs &= ~SPIBUS_CS_HIGH;
240 /* There is always a command to transfer. */
241 tx_buf = (uint8_t *)(cmd->tx_cmd);
243 /* Perform some fixup because MTK dont support duplex SPI */
249 case CMD_ENTER_4B_MODE:
250 case CMD_EXIT_4B_MODE:
251 case CMD_WRITE_ENABLE:
252 case CMD_WRITE_DISABLE:
256 case CMD_READ_STATUS:
262 cmd->rx_cmd_sz = cmd->tx_data_sz = 0;
264 case CMD_SECTOR_ERASE:
267 case CMD_PAGE_PROGRAM:
268 cmd->rx_cmd_sz = cmd->rx_data_sz = 0;
272 mtk_spi_chip_activate(sc);
274 if (cmd->tx_cmd_sz + cmd->rx_cmd_sz) {
275 buf = (uint8_t *)(cmd->rx_cmd);
276 tx_buf = (uint8_t *)(cmd->tx_cmd);
277 sz = cmd->tx_cmd_sz + cmd->rx_cmd_sz;
279 for (i = 0; i < sz; i++) {
280 if(i < cmd->tx_cmd_sz) {
282 error = mtk_spi_txrx(sc, &byte,
285 goto mtk_spi_transfer_fail;
288 error = mtk_spi_txrx(sc, &byte,
291 goto mtk_spi_transfer_fail;
297 * Transfer/Receive data
300 if (cmd->tx_data_sz + cmd->rx_data_sz) {
301 write = (cmd->tx_data_sz > 0)?1:0;
302 buf = (uint8_t *)(write ? cmd->tx_data : cmd->rx_data);
303 sz = write ? cmd->tx_data_sz : cmd->rx_data_sz;
305 for (i = 0; i < sz; i++) {
307 error = mtk_spi_txrx(sc, &byte,
308 write ? MTK_SPI_WRITE : MTK_SPI_READ);
310 goto mtk_spi_transfer_fail;
314 mtk_spi_transfer_fail:
315 mtk_spi_chip_deactivate(sc);
321 mtk_spi_get_node(device_t bus, device_t dev)
324 /* We only have one child, the SPI bus, which needs our own node. */
325 return (ofw_bus_get_node(bus));
328 static device_method_t mtk_spi_methods[] = {
329 /* Device interface */
330 DEVMETHOD(device_probe, mtk_spi_probe),
331 DEVMETHOD(device_attach, mtk_spi_attach),
332 DEVMETHOD(device_detach, mtk_spi_detach),
334 DEVMETHOD(spibus_transfer, mtk_spi_transfer),
336 /* ofw_bus interface */
337 DEVMETHOD(ofw_bus_get_node, mtk_spi_get_node),
342 static driver_t mtk_spi_driver = {
344 .methods = mtk_spi_methods,
345 .size = sizeof(struct mtk_spi_softc),
348 static devclass_t mtk_spi_devclass;
350 DRIVER_MODULE(mtk_spi_v1, simplebus, mtk_spi_driver, mtk_spi_devclass, 0, 0);