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
chipc_spi_probe(device_t dev)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
chipc_spi_attach(device_t dev)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 bus_attach_children(dev);
119
120 /* Determine flash type and add the flash child */
121 ccaps = BHND_CHIPC_GET_CAPS(device_get_parent(dev));
122 flash_name = chipc_sflash_device_name(ccaps->flash_type);
123 if (flash_name != NULL) {
124 flash_dev = BUS_ADD_CHILD(spibus, 0, flash_name, DEVICE_UNIT_ANY);
125 if (flash_dev == NULL) {
126 device_printf(dev, "failed to add %s\n", flash_name);
127 error = ENXIO;
128 goto failed;
129 }
130
131 chipc_register_slicer(ccaps->flash_type);
132
133 if ((error = device_probe_and_attach(flash_dev))) {
134 device_printf(dev, "failed to attach %s: %d\n",
135 flash_name, error);
136 goto failed;
137 }
138 }
139
140 return (0);
141
142 failed:
143 device_delete_children(dev);
144
145 if (sc->sc_res != NULL)
146 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid,
147 sc->sc_res);
148
149 if (sc->sc_flash_res != NULL)
150 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_flash_rid,
151 sc->sc_flash_res);
152
153 return (error);
154 }
155
156 static int
chipc_spi_detach(device_t dev)157 chipc_spi_detach(device_t dev)
158 {
159 struct chipc_spi_softc *sc;
160 int error;
161
162 sc = device_get_softc(dev);
163
164 if ((error = bus_generic_detach(dev)))
165 return (error);
166
167 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, sc->sc_res);
168 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_flash_rid,
169 sc->sc_flash_res);
170 return (0);
171 }
172
173 static int
chipc_spi_wait(struct chipc_spi_softc * sc)174 chipc_spi_wait(struct chipc_spi_softc *sc)
175 {
176 int i;
177
178 for (i = CHIPC_SPI_MAXTRIES; i > 0; i--)
179 if (!(SPI_READ(sc, CHIPC_SPI_FLASHCTL) & CHIPC_SPI_FLASHCTL_START))
180 break;
181
182 if (i > 0)
183 return (0);
184
185 BHND_WARN_DEV(sc->sc_dev, "busy: CTL=0x%x DATA=0x%x",
186 SPI_READ(sc, CHIPC_SPI_FLASHCTL),
187 SPI_READ(sc, CHIPC_SPI_FLASHDATA));
188 return (-1);
189 }
190
191 static int
chipc_spi_txrx(struct chipc_spi_softc * sc,uint8_t out,uint8_t * in)192 chipc_spi_txrx(struct chipc_spi_softc *sc, uint8_t out, uint8_t* in)
193 {
194 uint32_t ctl;
195
196 ctl = CHIPC_SPI_FLASHCTL_START | CHIPC_SPI_FLASHCTL_CSACTIVE | out;
197 SPI_BARRIER_WRITE(sc);
198 SPI_WRITE(sc, CHIPC_SPI_FLASHCTL, ctl);
199 SPI_BARRIER_WRITE(sc);
200
201 if (chipc_spi_wait(sc))
202 return (-1);
203
204 *in = SPI_READ(sc, CHIPC_SPI_FLASHDATA) & 0xff;
205 return (0);
206 }
207
208 static int
chipc_spi_transfer(device_t dev,device_t child,struct spi_command * cmd)209 chipc_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
210 {
211 struct chipc_spi_softc *sc;
212 uint8_t *buf_in;
213 uint8_t *buf_out;
214 int i;
215
216 sc = device_get_softc(dev);
217 KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz,
218 ("TX/RX command sizes should be equal"));
219 KASSERT(cmd->tx_data_sz == cmd->rx_data_sz,
220 ("TX/RX data sizes should be equal"));
221
222 if (cmd->tx_cmd_sz == 0) {
223 BHND_DEBUG_DEV(child, "size of command is ZERO");
224 return (EIO);
225 }
226
227 SPI_BARRIER_WRITE(sc);
228 SPI_WRITE(sc, CHIPC_SPI_FLASHADDR, 0);
229 SPI_BARRIER_WRITE(sc);
230
231 /*
232 * Transfer command
233 */
234 buf_out = (uint8_t *)cmd->tx_cmd;
235 buf_in = (uint8_t *)cmd->rx_cmd;
236 for (i = 0; i < cmd->tx_cmd_sz; i++)
237 if (chipc_spi_txrx(sc, buf_out[i], &(buf_in[i])))
238 return (EIO);
239
240 /*
241 * Receive/transmit data
242 */
243 buf_out = (uint8_t *)cmd->tx_data;
244 buf_in = (uint8_t *)cmd->rx_data;
245 for (i = 0; i < cmd->tx_data_sz; i++)
246 if (chipc_spi_txrx(sc, buf_out[i], &(buf_in[i])))
247 return (EIO);
248
249 /*
250 * Clear CS bit and whole control register
251 */
252 SPI_BARRIER_WRITE(sc);
253 SPI_WRITE(sc, CHIPC_SPI_FLASHCTL, 0);
254 SPI_BARRIER_WRITE(sc);
255
256 return (0);
257 }
258
259 static device_method_t chipc_spi_methods[] = {
260 DEVMETHOD(device_probe, chipc_spi_probe),
261 DEVMETHOD(device_attach, chipc_spi_attach),
262 DEVMETHOD(device_detach, chipc_spi_detach),
263
264 /* SPI */
265 DEVMETHOD(spibus_transfer, chipc_spi_transfer),
266 DEVMETHOD_END
267 };
268
269 static driver_t chipc_spi_driver = {
270 "spi",
271 chipc_spi_methods,
272 sizeof(struct chipc_spi_softc),
273 };
274
275 DRIVER_MODULE(chipc_spi, bhnd_chipc, chipc_spi_driver, 0, 0);
276