1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2016 Michael Zhilin <mizhka@gmail.com> 5 * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer, 13 * without modification. 14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 15 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 16 * redistribution must be conditioned upon including a substantially 17 * similar Disclaimer requirement for further binary redistribution. 18 * 19 * NO WARRANTY 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 23 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 24 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 25 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 28 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGES. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/errno.h> 38 #include <sys/rman.h> 39 #include <sys/bus.h> 40 41 #include <machine/bus.h> 42 43 #include <dev/bhnd/bhndvar.h> 44 45 #include <dev/spibus/spi.h> 46 47 #include "bhnd_chipc_if.h" 48 49 #include "spibus_if.h" 50 51 #include "chipcreg.h" 52 #include "chipcvar.h" 53 #include "chipc_slicer.h" 54 55 #include "chipc_spi.h" 56 57 static int chipc_spi_probe(device_t dev); 58 static int chipc_spi_attach(device_t dev); 59 static int chipc_spi_detach(device_t dev); 60 static int chipc_spi_transfer(device_t dev, device_t child, 61 struct spi_command *cmd); 62 static int chipc_spi_txrx(struct chipc_spi_softc *sc, uint8_t in, 63 uint8_t* out); 64 static int chipc_spi_wait(struct chipc_spi_softc *sc); 65 66 static int 67 chipc_spi_probe(device_t dev) 68 { 69 device_set_desc(dev, "Broadcom ChipCommon SPI"); 70 return (BUS_PROBE_NOWILDCARD); 71 } 72 73 static int 74 chipc_spi_attach(device_t dev) 75 { 76 struct chipc_spi_softc *sc; 77 struct chipc_caps *ccaps; 78 device_t flash_dev; 79 device_t spibus; 80 const char *flash_name; 81 int error; 82 83 sc = device_get_softc(dev); 84 85 /* Allocate SPI controller registers */ 86 sc->sc_rid = 1; 87 sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid, 88 RF_ACTIVE); 89 if (sc->sc_res == NULL) { 90 device_printf(dev, "failed to allocate device registers\n"); 91 return (ENXIO); 92 } 93 94 /* Allocate flash shadow region */ 95 sc->sc_flash_rid = 0; 96 sc->sc_flash_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 97 &sc->sc_flash_rid, RF_ACTIVE); 98 if (sc->sc_flash_res == NULL) { 99 device_printf(dev, "failed to allocate flash region\n"); 100 error = ENXIO; 101 goto failed; 102 } 103 104 /* 105 * Add flash device 106 * 107 * XXX: This should be replaced with a DEVICE_IDENTIFY implementation 108 * in chipc-specific subclasses of the mx25l and at45d drivers. 109 */ 110 if ((spibus = device_add_child(dev, "spibus", -1)) == NULL) { 111 device_printf(dev, "failed to add spibus\n"); 112 error = ENXIO; 113 goto failed; 114 } 115 116 /* Let spibus perform full attach before we try to call 117 * BUS_ADD_CHILD() */ 118 if ((error = bus_generic_attach(dev))) 119 goto failed; 120 121 /* Determine flash type and add the flash child */ 122 ccaps = BHND_CHIPC_GET_CAPS(device_get_parent(dev)); 123 flash_name = chipc_sflash_device_name(ccaps->flash_type); 124 if (flash_name != NULL) { 125 flash_dev = BUS_ADD_CHILD(spibus, 0, flash_name, -1); 126 if (flash_dev == NULL) { 127 device_printf(dev, "failed to add %s\n", flash_name); 128 error = ENXIO; 129 goto failed; 130 } 131 132 chipc_register_slicer(ccaps->flash_type); 133 134 if ((error = device_probe_and_attach(flash_dev))) { 135 device_printf(dev, "failed to attach %s: %d\n", 136 flash_name, error); 137 goto failed; 138 } 139 } 140 141 return (0); 142 143 failed: 144 device_delete_children(dev); 145 146 if (sc->sc_res != NULL) 147 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, 148 sc->sc_res); 149 150 if (sc->sc_flash_res != NULL) 151 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_flash_rid, 152 sc->sc_flash_res); 153 154 return (error); 155 } 156 157 static int 158 chipc_spi_detach(device_t dev) 159 { 160 struct chipc_spi_softc *sc; 161 int error; 162 163 sc = device_get_softc(dev); 164 165 if ((error = bus_generic_detach(dev))) 166 return (error); 167 168 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, sc->sc_res); 169 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_flash_rid, 170 sc->sc_flash_res); 171 return (0); 172 } 173 174 static int 175 chipc_spi_wait(struct chipc_spi_softc *sc) 176 { 177 int i; 178 179 for (i = CHIPC_SPI_MAXTRIES; i > 0; i--) 180 if (!(SPI_READ(sc, CHIPC_SPI_FLASHCTL) & CHIPC_SPI_FLASHCTL_START)) 181 break; 182 183 if (i > 0) 184 return (0); 185 186 BHND_WARN_DEV(sc->sc_dev, "busy: CTL=0x%x DATA=0x%x", 187 SPI_READ(sc, CHIPC_SPI_FLASHCTL), 188 SPI_READ(sc, CHIPC_SPI_FLASHDATA)); 189 return (-1); 190 } 191 192 static int 193 chipc_spi_txrx(struct chipc_spi_softc *sc, uint8_t out, uint8_t* in) 194 { 195 uint32_t ctl; 196 197 ctl = CHIPC_SPI_FLASHCTL_START | CHIPC_SPI_FLASHCTL_CSACTIVE | out; 198 SPI_BARRIER_WRITE(sc); 199 SPI_WRITE(sc, CHIPC_SPI_FLASHCTL, ctl); 200 SPI_BARRIER_WRITE(sc); 201 202 if (chipc_spi_wait(sc)) 203 return (-1); 204 205 *in = SPI_READ(sc, CHIPC_SPI_FLASHDATA) & 0xff; 206 return (0); 207 } 208 209 static int 210 chipc_spi_transfer(device_t dev, device_t child, struct spi_command *cmd) 211 { 212 struct chipc_spi_softc *sc; 213 uint8_t *buf_in; 214 uint8_t *buf_out; 215 int i; 216 217 sc = device_get_softc(dev); 218 KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz, 219 ("TX/RX command sizes should be equal")); 220 KASSERT(cmd->tx_data_sz == cmd->rx_data_sz, 221 ("TX/RX data sizes should be equal")); 222 223 if (cmd->tx_cmd_sz == 0) { 224 BHND_DEBUG_DEV(child, "size of command is ZERO"); 225 return (EIO); 226 } 227 228 SPI_BARRIER_WRITE(sc); 229 SPI_WRITE(sc, CHIPC_SPI_FLASHADDR, 0); 230 SPI_BARRIER_WRITE(sc); 231 232 /* 233 * Transfer command 234 */ 235 buf_out = (uint8_t *)cmd->tx_cmd; 236 buf_in = (uint8_t *)cmd->rx_cmd; 237 for (i = 0; i < cmd->tx_cmd_sz; i++) 238 if (chipc_spi_txrx(sc, buf_out[i], &(buf_in[i]))) 239 return (EIO); 240 241 /* 242 * Receive/transmit data 243 */ 244 buf_out = (uint8_t *)cmd->tx_data; 245 buf_in = (uint8_t *)cmd->rx_data; 246 for (i = 0; i < cmd->tx_data_sz; i++) 247 if (chipc_spi_txrx(sc, buf_out[i], &(buf_in[i]))) 248 return (EIO); 249 250 /* 251 * Clear CS bit and whole control register 252 */ 253 SPI_BARRIER_WRITE(sc); 254 SPI_WRITE(sc, CHIPC_SPI_FLASHCTL, 0); 255 SPI_BARRIER_WRITE(sc); 256 257 return (0); 258 } 259 260 static device_method_t chipc_spi_methods[] = { 261 DEVMETHOD(device_probe, chipc_spi_probe), 262 DEVMETHOD(device_attach, chipc_spi_attach), 263 DEVMETHOD(device_detach, chipc_spi_detach), 264 265 /* SPI */ 266 DEVMETHOD(spibus_transfer, chipc_spi_transfer), 267 DEVMETHOD_END 268 }; 269 270 static driver_t chipc_spi_driver = { 271 "spi", 272 chipc_spi_methods, 273 sizeof(struct chipc_spi_softc), 274 }; 275 276 DRIVER_MODULE(chipc_spi, bhnd_chipc, chipc_spi_driver, 0, 0); 277