2 * Copyright (c) 2016 Michael Zhilin <mizhka@gmail.com>
3 * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14 * redistribution must be conditioned upon including a substantially
15 * similar Disclaimer requirement for further binary redistribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
21 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
23 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGES.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/errno.h>
42 #include <machine/bus.h>
44 #include <dev/bhnd/bhndvar.h>
46 #include <dev/spibus/spi.h>
48 #include "bhnd_chipc_if.h"
50 #include "spibus_if.h"
54 #include "chipc_slicer.h"
56 #include "chipc_spi.h"
58 static int chipc_spi_probe(device_t dev);
59 static int chipc_spi_attach(device_t dev);
60 static int chipc_spi_detach(device_t dev);
61 static int chipc_spi_transfer(device_t dev, device_t child,
62 struct spi_command *cmd);
63 static int chipc_spi_txrx(struct chipc_spi_softc *sc, uint8_t in,
65 static int chipc_spi_wait(struct chipc_spi_softc *sc);
68 chipc_spi_probe(device_t dev)
70 device_set_desc(dev, "Broadcom ChipCommon SPI");
71 return (BUS_PROBE_NOWILDCARD);
75 chipc_spi_attach(device_t dev)
77 struct chipc_spi_softc *sc;
78 struct chipc_caps *ccaps;
81 const char *flash_name;
84 sc = device_get_softc(dev);
86 /* Allocate SPI controller registers */
88 sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
90 if (sc->sc_res == NULL) {
91 device_printf(dev, "failed to allocate device registers\n");
95 /* Allocate flash shadow region */
97 sc->sc_flash_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
98 &sc->sc_flash_rid, RF_ACTIVE);
99 if (sc->sc_flash_res == NULL) {
100 device_printf(dev, "failed to allocate flash region\n");
108 * XXX: This should be replaced with a DEVICE_IDENTIFY implementation
109 * in chipc-specific subclasses of the mx25l and at45d drivers.
111 if ((spibus = device_add_child(dev, "spibus", -1)) == NULL) {
112 device_printf(dev, "failed to add spibus\n");
117 /* Let spibus perform full attach before we try to call
119 if ((error = bus_generic_attach(dev)))
122 /* Determine flash type and add the flash child */
123 ccaps = BHND_CHIPC_GET_CAPS(device_get_parent(dev));
124 flash_name = chipc_sflash_device_name(ccaps->flash_type);
125 if (flash_name != NULL) {
126 flash_dev = BUS_ADD_CHILD(spibus, 0, flash_name, -1);
127 if (flash_dev == NULL) {
128 device_printf(dev, "failed to add %s\n", flash_name);
133 chipc_register_slicer(ccaps->flash_type);
135 if ((error = device_probe_and_attach(flash_dev))) {
136 device_printf(dev, "failed to attach %s: %d\n",
145 device_delete_children(dev);
147 if (sc->sc_res != NULL)
148 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid,
151 if (sc->sc_flash_res != NULL)
152 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_flash_rid,
159 chipc_spi_detach(device_t dev)
161 struct chipc_spi_softc *sc;
164 sc = device_get_softc(dev);
166 if ((error = bus_generic_detach(dev)))
169 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, sc->sc_res);
170 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_flash_rid,
176 chipc_spi_wait(struct chipc_spi_softc *sc)
180 for (i = CHIPC_SPI_MAXTRIES; i > 0; i--)
181 if (!(SPI_READ(sc, CHIPC_SPI_FLASHCTL) & CHIPC_SPI_FLASHCTL_START))
187 BHND_WARN_DEV(sc->sc_dev, "busy: CTL=0x%x DATA=0x%x",
188 SPI_READ(sc, CHIPC_SPI_FLASHCTL),
189 SPI_READ(sc, CHIPC_SPI_FLASHDATA));
194 chipc_spi_txrx(struct chipc_spi_softc *sc, uint8_t out, uint8_t* in)
198 ctl = CHIPC_SPI_FLASHCTL_START | CHIPC_SPI_FLASHCTL_CSACTIVE | out;
199 SPI_BARRIER_WRITE(sc);
200 SPI_WRITE(sc, CHIPC_SPI_FLASHCTL, ctl);
201 SPI_BARRIER_WRITE(sc);
203 if (chipc_spi_wait(sc))
206 *in = SPI_READ(sc, CHIPC_SPI_FLASHDATA) & 0xff;
211 chipc_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
213 struct chipc_spi_softc *sc;
218 sc = device_get_softc(dev);
219 KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz,
220 ("TX/RX command sizes should be equal"));
221 KASSERT(cmd->tx_data_sz == cmd->rx_data_sz,
222 ("TX/RX data sizes should be equal"));
224 if (cmd->tx_cmd_sz == 0) {
225 BHND_DEBUG_DEV(child, "size of command is ZERO");
229 SPI_BARRIER_WRITE(sc);
230 SPI_WRITE(sc, CHIPC_SPI_FLASHADDR, 0);
231 SPI_BARRIER_WRITE(sc);
236 buf_out = (uint8_t *)cmd->tx_cmd;
237 buf_in = (uint8_t *)cmd->rx_cmd;
238 for (i = 0; i < cmd->tx_cmd_sz; i++)
239 if (chipc_spi_txrx(sc, buf_out[i], &(buf_in[i])))
243 * Receive/transmit data
245 buf_out = (uint8_t *)cmd->tx_data;
246 buf_in = (uint8_t *)cmd->rx_data;
247 for (i = 0; i < cmd->tx_data_sz; i++)
248 if (chipc_spi_txrx(sc, buf_out[i], &(buf_in[i])))
252 * Clear CS bit and whole control register
254 SPI_BARRIER_WRITE(sc);
255 SPI_WRITE(sc, CHIPC_SPI_FLASHCTL, 0);
256 SPI_BARRIER_WRITE(sc);
261 static device_method_t chipc_spi_methods[] = {
262 DEVMETHOD(device_probe, chipc_spi_probe),
263 DEVMETHOD(device_attach, chipc_spi_attach),
264 DEVMETHOD(device_detach, chipc_spi_detach),
267 DEVMETHOD(spibus_transfer, chipc_spi_transfer),
271 static driver_t chipc_spi_driver = {
274 sizeof(struct chipc_spi_softc),
277 static devclass_t chipc_spi_devclass;
279 DRIVER_MODULE(chipc_spi, bhnd_chipc, chipc_spi_driver, chipc_spi_devclass,