]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/sifive/fu540_spi.c
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / sys / riscv / sifive / fu540_spi.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Axiado Corporation
5  * All rights reserved.
6  *
7  * This software was developed in part by Philip Paeps and Kristof Provost
8  * under contract for Axiado Corporation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/rman.h>
43
44 #include <machine/bus.h>
45 #include <machine/cpu.h>
46
47 #include <dev/extres/clk/clk.h>
48
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 #include <dev/ofw/openfirm.h>
52
53 #include <dev/spibus/spi.h>
54 #include <dev/spibus/spibusvar.h>
55
56 #include "spibus_if.h"
57
58 #if 1
59 #define DBGPRINT(dev, fmt, args...) \
60         device_printf(dev, "%s: " fmt "\n", __func__, ## args)
61 #else
62 #define DBGPRINT(dev, fmt, args...)
63 #endif
64
65 static struct resource_spec fuspi_spec[] = {
66         { SYS_RES_MEMORY, 0, RF_ACTIVE },
67         RESOURCE_SPEC_END
68 };
69
70 struct fuspi_softc {
71         device_t                dev;
72         device_t                parent;
73
74         struct mtx              mtx;
75
76         struct resource         *res;
77         bus_space_tag_t         bst;
78         bus_space_handle_t      bsh;
79
80         void                    *ih;
81
82         clk_t                   clk;
83         uint32_t                cs_max;
84 };
85
86 #define FUSPI_LOCK(sc)                  mtx_lock(&(sc)->mtx)
87 #define FUSPI_UNLOCK(sc)                mtx_unlock(&(sc)->mtx)
88 #define FUSPI_ASSERT_LOCKED(sc)         mtx_assert(&(sc)->mtx, MA_OWNED);
89 #define FUSPI_ASSERT_UNLOCKED(sc)       mtx_assert(&(sc)->mtx, MA_NOTOWNED);
90
91 /*
92  * Register offsets.
93  * From Sifive-Unleashed-FU540-C000-v1.0.pdf page 101.
94  */
95 #define FUSPI_REG_SCKDIV        0x00 /* Serial clock divisor */
96 #define FUSPI_REG_SCKMODE       0x04 /* Serial clock mode */
97 #define FUSPI_REG_CSID          0x10 /* Chip select ID */
98 #define FUSPI_REG_CSDEF         0x14 /* Chip select default */
99 #define FUSPI_REG_CSMODE        0x18 /* Chip select mode */
100 #define FUSPI_REG_DELAY0        0x28 /* Delay control 0 */
101 #define FUSPI_REG_DELAY1        0x2C /* Delay control 1 */
102 #define FUSPI_REG_FMT           0x40 /* Frame format */
103 #define FUSPI_REG_TXDATA        0x48 /* Tx FIFO data */
104 #define FUSPI_REG_RXDATA        0x4C /* Rx FIFO data */
105 #define FUSPI_REG_TXMARK        0x50 /* Tx FIFO watermark */
106 #define FUSPI_REG_RXMARK        0x54 /* Rx FIFO watermark */
107 #define FUSPI_REG_FCTRL         0x60 /* SPI flash interface control* */
108 #define FUSPI_REG_FFMT          0x64 /* SPI flash instruction format* */
109 #define FUSPI_REG_IE            0x70 /* SPI interrupt enable */
110 #define FUSPI_REG_IP            0x74 /* SPI interrupt pending */
111
112 #define FUSPI_SCKDIV_MASK       0xfff
113
114 #define FUSPI_CSDEF_ALL         ((1 << sc->cs_max)-1)
115
116 #define FUSPI_CSMODE_AUTO       0x0U
117 #define FUSPI_CSMODE_HOLD       0x2U
118 #define FUSPI_CSMODE_OFF        0x3U
119
120 #define FUSPI_TXDATA_DATA_MASK  0xff
121 #define FUSPI_TXDATA_FULL       (1 << 31)
122
123 #define FUSPI_RXDATA_DATA_MASK  0xff
124 #define FUSPI_RXDATA_EMPTY      (1 << 31)
125
126 #define FUSPI_SCKMODE_PHA       (1 << 0)
127 #define FUSPI_SCKMODE_POL       (1 << 1)
128
129 #define FUSPI_FMT_PROTO_SINGLE  0x0U
130 #define FUSPI_FMT_PROTO_DUAL    0x1U
131 #define FUSPI_FMT_PROTO_QUAD    0x2U
132 #define FUSPI_FMT_PROTO_MASK    0x3U
133 #define FUSPI_FMT_ENDIAN        (1 << 2)
134 #define FUSPI_FMT_DIR           (1 << 3)
135 #define FUSPI_FMT_LEN(x)        ((uint32_t)(x) << 16)
136 #define FUSPI_FMT_LEN_MASK      (0xfU << 16)
137
138 #define FUSPI_FIFO_DEPTH        8
139
140 #define FUSPI_READ(_sc, _reg)           \
141     bus_space_read_4((_sc)->bst, (_sc)->bsh, (_reg))
142 #define FUSPI_WRITE(_sc, _reg, _val)    \
143     bus_space_write_4((_sc)->bst, (_sc)->bsh, (_reg), (_val))
144
145 static void
146 fuspi_tx(struct fuspi_softc *sc, uint8_t *buf, uint32_t bufsiz)
147 {
148         uint32_t val;
149         uint8_t *p, *end;
150
151         KASSERT(buf != NULL, ("TX buffer cannot be NULL"));
152
153         end = buf + bufsiz;
154         for (p = buf; p < end; p++) {
155                 do {
156                         val = FUSPI_READ(sc, FUSPI_REG_TXDATA);
157                 } while (val & FUSPI_TXDATA_FULL);
158                 val = *p;
159                 FUSPI_WRITE(sc, FUSPI_REG_TXDATA, val);
160         }
161 }
162
163 static void
164 fuspi_rx(struct fuspi_softc *sc, uint8_t *buf, uint32_t bufsiz)
165 {
166         uint32_t val;
167         uint8_t *p, *end;
168
169         KASSERT(buf != NULL, ("RX buffer cannot be NULL"));
170         KASSERT(bufsiz <= FUSPI_FIFO_DEPTH,
171             ("Cannot receive more than %d bytes at a time\n",
172             FUSPI_FIFO_DEPTH));
173
174         end = buf + bufsiz;
175         for (p = buf; p < end; p++) {
176                 do {
177                         val = FUSPI_READ(sc, FUSPI_REG_RXDATA);
178                 } while (val & FUSPI_RXDATA_EMPTY);
179                 *p = val & FUSPI_RXDATA_DATA_MASK;
180         };
181 }
182
183 static int
184 fuspi_xfer_buf(struct fuspi_softc *sc, uint8_t *rxbuf, uint8_t *txbuf,
185     uint32_t txlen, uint32_t rxlen)
186 {
187         uint32_t bytes;
188
189         KASSERT(txlen == rxlen, ("TX and RX lengths must be equal"));
190         KASSERT(rxbuf != NULL, ("RX buffer cannot be NULL"));
191         KASSERT(txbuf != NULL, ("TX buffer cannot be NULL"));
192
193         while (txlen) {
194                 bytes = (txlen > FUSPI_FIFO_DEPTH) ? FUSPI_FIFO_DEPTH : txlen;
195                 fuspi_tx(sc, txbuf, bytes);
196                 txbuf += bytes;
197                 fuspi_rx(sc, rxbuf, bytes);
198                 rxbuf += bytes;
199                 txlen -= bytes;
200         }
201
202         return (0);
203 }
204
205 static int
206 fuspi_setup(struct fuspi_softc *sc, uint32_t cs, uint32_t mode,
207     uint32_t freq)
208 {
209         uint32_t csmode, fmt, sckdiv, sckmode;
210         uint64_t clock;
211         int ret;
212
213         FUSPI_ASSERT_LOCKED(sc);
214
215         ret = clk_get_freq(sc->clk, &clock);
216         if (ret) {
217                 device_printf(sc->dev, "Cannot get clock frequency: %d\n", ret);
218                 return (ret);
219         }
220
221         /*
222          * Fsck = Fin / 2 * (div + 1)
223          * -> div = Fin / (2 * Fsck) - 1
224          */
225         sckdiv = (howmany(clock >> 1, freq) - 1) & FUSPI_SCKDIV_MASK;
226         FUSPI_WRITE(sc, FUSPI_REG_SCKDIV, sckdiv);
227
228         switch (mode) {
229         case SPIBUS_MODE_CPHA:
230                 sckmode = FUSPI_SCKMODE_PHA;
231                 break;
232         case SPIBUS_MODE_CPOL:
233                 sckmode = FUSPI_SCKMODE_POL;
234                 break;
235         case SPIBUS_MODE_CPOL_CPHA:
236                 sckmode = FUSPI_SCKMODE_PHA | FUSPI_SCKMODE_POL;
237                 break;
238         }
239         FUSPI_WRITE(sc, FUSPI_REG_SCKMODE, sckmode);
240
241         csmode = FUSPI_CSMODE_HOLD;
242         if (cs & SPIBUS_CS_HIGH)
243                 csmode = FUSPI_CSMODE_AUTO;
244         FUSPI_WRITE(sc, FUSPI_REG_CSMODE, csmode);
245
246         FUSPI_WRITE(sc, FUSPI_REG_CSID, cs & ~SPIBUS_CS_HIGH);
247
248         fmt = FUSPI_FMT_PROTO_SINGLE | FUSPI_FMT_LEN(8);
249         FUSPI_WRITE(sc, FUSPI_REG_FMT, fmt);
250
251         return (0);
252 }
253
254 static int
255 fuspi_transfer(device_t dev, device_t child, struct spi_command *cmd)
256 {
257         struct fuspi_softc *sc;
258         uint32_t clock, cs, csdef, mode;
259         int err;
260
261         KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz,
262             ("TX and RX command sizes must be equal"));
263         KASSERT(cmd->tx_data_sz == cmd->rx_data_sz,
264             ("TX and RX data sizes must be equal"));
265
266         sc = device_get_softc(dev);
267         spibus_get_cs(child, &cs);
268         spibus_get_clock(child, &clock);
269         spibus_get_mode(child, &mode);
270
271         if (cs > sc->cs_max) {
272                 device_printf(sc->dev, "Invalid chip select %u\n", cs);
273                 return (EINVAL);
274         }
275
276         FUSPI_LOCK(sc);
277         device_busy(sc->dev);
278
279         err = fuspi_setup(sc, cs, mode, clock);
280         if (err != 0) {
281                 FUSPI_UNLOCK(sc);
282                 return (err);
283         }
284
285         err = 0;
286         if (cmd->tx_cmd_sz > 0)
287                 err = fuspi_xfer_buf(sc, cmd->rx_cmd, cmd->tx_cmd,
288                     cmd->tx_cmd_sz, cmd->rx_cmd_sz);
289         if (cmd->tx_data_sz > 0 && err == 0)
290                 err = fuspi_xfer_buf(sc, cmd->rx_data, cmd->tx_data,
291                     cmd->tx_data_sz, cmd->rx_data_sz);
292
293         /* Deassert chip select. */
294         csdef = FUSPI_CSDEF_ALL & ~(1 << cs);
295         FUSPI_WRITE(sc, FUSPI_REG_CSDEF, csdef);
296         FUSPI_WRITE(sc, FUSPI_REG_CSDEF, FUSPI_CSDEF_ALL);
297
298         device_unbusy(sc->dev);
299         FUSPI_UNLOCK(sc);
300
301         return (err);
302 }
303
304 static int
305 fuspi_attach(device_t dev)
306 {
307         struct fuspi_softc *sc;
308         int error;
309
310         sc = device_get_softc(dev);
311         sc->dev = dev;
312
313         mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
314
315         error = bus_alloc_resources(dev, fuspi_spec, &sc->res);
316         if (error) {
317                 device_printf(dev, "Couldn't allocate resources\n");
318                 goto fail;
319         }
320         sc->bst = rman_get_bustag(sc->res);
321         sc->bsh = rman_get_bushandle(sc->res);
322
323         error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
324         if (error) {
325                 device_printf(dev, "Couldn't allocate clock: %d\n", error);
326                 goto fail;
327         }
328         error = clk_enable(sc->clk);
329         if (error) {
330                 device_printf(dev, "Couldn't enable clock: %d\n", error);
331                 goto fail;
332         }
333
334         /*
335          * From Sifive-Unleashed-FU540-C000-v1.0.pdf page 103:
336          * csdef is cs_width bits wide and all ones on reset.
337          */
338         sc->cs_max = FUSPI_READ(sc, FUSPI_REG_CSDEF);
339
340         /*
341          * We don't support the direct-mapped flash interface.
342          * Disable it.
343          */
344         FUSPI_WRITE(sc, FUSPI_REG_FCTRL, 0x0);
345
346         /* Probe and attach the spibus when interrupts are available. */
347         sc->parent = device_add_child(dev, "spibus", -1);
348         config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
349
350         return (0);
351
352 fail1:
353         bus_release_resources(dev, fuspi_spec, &sc->res);
354
355 fail:
356         mtx_destroy(&sc->mtx);
357         return (error);
358 }
359
360 static int
361 fuspi_probe(device_t dev)
362 {
363
364         if (!ofw_bus_status_okay(dev))
365                 return (ENXIO);
366
367         if (!ofw_bus_is_compatible(dev, "sifive,spi0"))
368                 return (ENXIO);
369
370         device_set_desc(dev, "SiFive FU540 SPI controller");
371
372         return (BUS_PROBE_DEFAULT);
373 }
374
375 static phandle_t
376 fuspi_get_node(device_t bus, device_t dev)
377 {
378
379         return (ofw_bus_get_node(bus));
380 }
381
382 static device_method_t fuspi_methods[] = {
383         DEVMETHOD(device_probe,         fuspi_probe),
384         DEVMETHOD(device_attach,        fuspi_attach),
385
386         DEVMETHOD(spibus_transfer,      fuspi_transfer),
387
388         DEVMETHOD(ofw_bus_get_node,     fuspi_get_node),
389
390         DEVMETHOD_END
391 };
392
393 static driver_t fuspi_driver = {
394         "fu540spi",
395         fuspi_methods,
396         sizeof(struct fuspi_softc)
397 };
398
399 static devclass_t fuspi_devclass;
400
401 DRIVER_MODULE(fu540spi, simplebus, fuspi_driver, fuspi_devclass, 0, 0);
402 DRIVER_MODULE(ofw_spibus, fu540spi, ofw_spibus_driver, ofw_spibus_devclass, 0, 0);
403 MODULE_DEPEND(fu540spi, ofw_spibus, 1, 1, 1);