2 * Copyright (c) 2017-2018 Ruslan Bukin <br@bsdpad.com>
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Cadence Quad SPI Flash Controller driver.
33 * 4B-addressing mode supported only.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include "opt_platform.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/kthread.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/mutex.h>
54 #include <geom/geom_disk.h>
56 #include <machine/bus.h>
58 #include <dev/fdt/simplebus.h>
59 #include <dev/fdt/fdt_common.h>
60 #include <dev/ofw/ofw_bus_subr.h>
61 #include <dev/ofw/openfirm.h>
63 #include <dev/flash/cqspi.h>
64 #include <dev/flash/mx25lreg.h>
65 #include <dev/xdma/xdma.h>
73 #define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__)
75 #define dprintf(fmt, ...)
78 #define CQSPI_SECTORSIZE 512
79 #define TX_QUEUE_SIZE 16
80 #define RX_QUEUE_SIZE 16
82 #define READ4(_sc, _reg) bus_read_4((_sc)->res[0], _reg)
83 #define READ2(_sc, _reg) bus_read_2((_sc)->res[0], _reg)
84 #define READ1(_sc, _reg) bus_read_1((_sc)->res[0], _reg)
85 #define WRITE4(_sc, _reg, _val) bus_write_4((_sc)->res[0], _reg, _val)
86 #define WRITE2(_sc, _reg, _val) bus_write_2((_sc)->res[0], _reg, _val)
87 #define WRITE1(_sc, _reg, _val) bus_write_1((_sc)->res[0], _reg, _val)
88 #define READ_DATA_4(_sc, _reg) bus_read_4((_sc)->res[1], _reg)
89 #define READ_DATA_1(_sc, _reg) bus_read_1((_sc)->res[1], _reg)
90 #define WRITE_DATA_4(_sc, _reg, _val) bus_write_4((_sc)->res[1], _reg, _val)
91 #define WRITE_DATA_1(_sc, _reg, _val) bus_write_1((_sc)->res[1], _reg, _val)
96 struct resource *res[3];
98 bus_space_handle_t bsh;
100 uint8_t read_op_done;
101 uint8_t write_op_done;
105 uint32_t trigger_address;
109 xdma_controller_t *xdma_tx;
110 xdma_channel_t *xchan_tx;
113 xdma_controller_t *xdma_rx;
114 xdma_channel_t *xchan_rx;
117 struct intr_config_hook config_intrhook;
121 #define CQSPI_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
122 #define CQSPI_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
123 #define CQSPI_LOCK_INIT(_sc) \
124 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
126 #define CQSPI_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
127 #define CQSPI_ASSERT_LOCKED(_sc) \
128 mtx_assert(&_sc->sc_mtx, MA_OWNED);
129 #define CQSPI_ASSERT_UNLOCKED(_sc) \
130 mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
132 static struct resource_spec cqspi_spec[] = {
133 { SYS_RES_MEMORY, 0, RF_ACTIVE },
134 { SYS_RES_MEMORY, 1, RF_ACTIVE },
135 { SYS_RES_IRQ, 0, RF_ACTIVE },
139 static struct ofw_compat_data compat_data[] = {
140 { "cdns,qspi-nor", 1 },
145 cqspi_intr(void *arg)
147 struct cqspi_softc *sc;
152 pending = READ4(sc, CQSPI_IRQSTAT);
154 dprintf("%s: IRQSTAT %x\n", __func__, pending);
156 if (pending & (IRQMASK_INDOPDONE | IRQMASK_INDXFRLVL |
157 IRQMASK_INDSRAMFULL)) {
158 /* TODO: PIO operation done */
161 WRITE4(sc, CQSPI_IRQSTAT, pending);
165 cqspi_xdma_tx_intr(void *arg, xdma_transfer_status_t *status)
167 struct xdma_transfer_status st;
168 struct cqspi_softc *sc;
175 dprintf("%s\n", __func__);
180 ret = xdma_dequeue_bio(sc->xchan_tx, &bp, &st);
184 sc->write_op_done = 1;
189 device_printf(sc->dev,
190 "Warning: more than 1 tx bio dequeued\n");
192 wakeup(&sc->xdma_tx);
198 cqspi_xdma_rx_intr(void *arg, xdma_transfer_status_t *status)
200 struct xdma_transfer_status st;
201 struct cqspi_softc *sc;
208 dprintf("%s\n", __func__);
213 ret = xdma_dequeue_bio(sc->xchan_rx, &bp, &st);
217 sc->read_op_done = 1;
222 device_printf(sc->dev,
223 "Warning: more than 1 rx bio dequeued\n");
225 wakeup(&sc->xdma_rx);
231 cqspi_wait_for_completion(struct cqspi_softc *sc)
238 for (i = timeout; i > 0; i--) {
239 if ((READ4(sc, CQSPI_FLASHCMD) & FLASHCMD_CMDEXECSTAT) == 0) {
245 device_printf(sc->dev, "%s: cmd timed out: %x\n",
246 __func__, READ4(sc, CQSPI_FLASHCMD));
254 cqspi_cmd_write_addr(struct cqspi_softc *sc, uint8_t cmd,
255 uint32_t addr, uint32_t len)
260 dprintf("%s: %x\n", __func__, cmd);
262 WRITE4(sc, CQSPI_FLASHCMDADDR, addr);
263 reg = (cmd << FLASHCMD_CMDOPCODE_S);
264 reg |= (FLASHCMD_ENCMDADDR);
265 reg |= ((len - 1) << FLASHCMD_NUMADDRBYTES_S);
266 WRITE4(sc, CQSPI_FLASHCMD, reg);
268 reg |= FLASHCMD_EXECCMD;
269 WRITE4(sc, CQSPI_FLASHCMD, reg);
271 ret = cqspi_wait_for_completion(sc);
277 cqspi_cmd_write(struct cqspi_softc *sc, uint8_t cmd,
278 uint8_t *addr, uint32_t len)
283 reg = (cmd << FLASHCMD_CMDOPCODE_S);
284 WRITE4(sc, CQSPI_FLASHCMD, reg);
285 reg |= FLASHCMD_EXECCMD;
286 WRITE4(sc, CQSPI_FLASHCMD, reg);
288 ret = cqspi_wait_for_completion(sc);
294 cqspi_cmd_read(struct cqspi_softc *sc, uint8_t cmd,
295 uint8_t *addr, uint32_t len)
304 device_printf(sc->dev, "Failed to read data\n");
308 dprintf("%s: %x\n", __func__, cmd);
310 buf = (uint8_t *)addr;
312 reg = (cmd << FLASHCMD_CMDOPCODE_S);
313 reg |= ((len - 1) << FLASHCMD_NUMRDDATABYTES_S);
314 reg |= FLASHCMD_ENRDDATA;
315 WRITE4(sc, CQSPI_FLASHCMD, reg);
317 reg |= FLASHCMD_EXECCMD;
318 WRITE4(sc, CQSPI_FLASHCMD, reg);
320 ret = cqspi_wait_for_completion(sc);
322 device_printf(sc->dev, "%s: cmd failed: %x\n",
327 data = READ4(sc, CQSPI_FLASHCMDRDDATALO);
329 for (i = 0; i < len; i++)
330 buf[i] = (data >> (i * 8)) & 0xff;
336 cqspi_wait_ready(struct cqspi_softc *sc)
342 ret = cqspi_cmd_read(sc, CMD_READ_STATUS, &data, 1);
343 } while (data & STATUS_WIP);
349 cqspi_write_reg(device_t dev, device_t child,
350 uint8_t opcode, uint8_t *addr, uint32_t len)
352 struct cqspi_softc *sc;
355 sc = device_get_softc(dev);
357 ret = cqspi_cmd_write(sc, opcode, addr, len);
363 cqspi_read_reg(device_t dev, device_t child,
364 uint8_t opcode, uint8_t *addr, uint32_t len)
366 struct cqspi_softc *sc;
369 sc = device_get_softc(dev);
371 ret = cqspi_cmd_read(sc, opcode, addr, len);
377 cqspi_wait_idle(struct cqspi_softc *sc)
382 reg = READ4(sc, CQSPI_CFG);
383 if (reg & CFG_IDLE) {
392 cqspi_erase(device_t dev, device_t child, off_t offset)
394 struct cqspi_softc *sc;
397 sc = device_get_softc(dev);
400 cqspi_wait_ready(sc);
401 ret = cqspi_cmd_write(sc, CMD_WRITE_ENABLE, 0, 0);
404 cqspi_wait_ready(sc);
405 ret = cqspi_cmd_write_addr(sc, CMD_QUAD_SECTOR_ERASE, offset, 4);
413 cqspi_write(device_t dev, device_t child, struct bio *bp,
414 off_t offset, caddr_t data, off_t count)
416 struct cqspi_softc *sc;
419 dprintf("%s: offset 0x%llx count %lld bytes\n",
420 __func__, offset, count);
422 sc = device_get_softc(dev);
424 cqspi_wait_ready(sc);
425 reg = cqspi_cmd_write(sc, CMD_WRITE_ENABLE, 0, 0);
428 cqspi_wait_ready(sc);
431 reg = DMAPER_NUMSGLREQBYTES_4;
432 reg |= DMAPER_NUMBURSTREQBYTES_4;
433 WRITE4(sc, CQSPI_DMAPER, reg);
435 WRITE4(sc, CQSPI_INDWRWATER, 64);
436 WRITE4(sc, CQSPI_INDWR, INDRD_IND_OPS_DONE_STATUS);
437 WRITE4(sc, CQSPI_INDWR, 0);
439 WRITE4(sc, CQSPI_INDWRCNT, count);
440 WRITE4(sc, CQSPI_INDWRSTADDR, offset);
442 reg = (0 << DEVWR_DUMMYWRCLKS_S);
443 reg |= DEVWR_DATA_WIDTH_QUAD;
444 reg |= DEVWR_ADDR_WIDTH_SINGLE;
445 reg |= (CMD_QUAD_PAGE_PROGRAM << DEVWR_WROPCODE_S);
446 WRITE4(sc, CQSPI_DEVWR, reg);
448 reg = DEVRD_DATA_WIDTH_QUAD;
449 reg |= DEVRD_ADDR_WIDTH_SINGLE;
450 reg |= DEVRD_INST_WIDTH_SINGLE;
451 WRITE4(sc, CQSPI_DEVRD, reg);
453 xdma_enqueue_bio(sc->xchan_tx, &bp,
454 sc->sram_phys, 4, 4, XDMA_MEM_TO_DEV);
455 xdma_queue_submit(sc->xchan_tx);
457 sc->write_op_done = 0;
459 WRITE4(sc, CQSPI_INDWR, INDRD_START);
461 while (sc->write_op_done == 0)
462 tsleep(&sc->xdma_tx, PCATCH | PZERO, "spi", hz/2);
470 cqspi_read(device_t dev, device_t child, struct bio *bp,
471 off_t offset, caddr_t data, off_t count)
473 struct cqspi_softc *sc;
476 sc = device_get_softc(dev);
478 dprintf("%s: offset 0x%llx count %lld bytes\n",
479 __func__, offset, count);
483 reg = DMAPER_NUMSGLREQBYTES_4;
484 reg |= DMAPER_NUMBURSTREQBYTES_4;
485 WRITE4(sc, CQSPI_DMAPER, reg);
487 WRITE4(sc, CQSPI_INDRDWATER, 64);
488 WRITE4(sc, CQSPI_INDRD, INDRD_IND_OPS_DONE_STATUS);
489 WRITE4(sc, CQSPI_INDRD, 0);
491 WRITE4(sc, CQSPI_INDRDCNT, count);
492 WRITE4(sc, CQSPI_INDRDSTADDR, offset);
494 reg = (0 << DEVRD_DUMMYRDCLKS_S);
495 reg |= DEVRD_DATA_WIDTH_QUAD;
496 reg |= DEVRD_ADDR_WIDTH_SINGLE;
497 reg |= DEVRD_INST_WIDTH_SINGLE;
498 reg |= DEVRD_ENMODEBITS;
499 reg |= (CMD_READ_4B_QUAD_OUTPUT << DEVRD_RDOPCODE_S);
500 WRITE4(sc, CQSPI_DEVRD, reg);
502 WRITE4(sc, CQSPI_MODEBIT, 0xff);
503 WRITE4(sc, CQSPI_IRQMASK, 0);
505 xdma_enqueue_bio(sc->xchan_rx, &bp, sc->sram_phys, 4, 4,
507 xdma_queue_submit(sc->xchan_rx);
509 sc->read_op_done = 0;
511 WRITE4(sc, CQSPI_INDRD, INDRD_START);
513 while (sc->read_op_done == 0)
514 tsleep(&sc->xdma_rx, PCATCH | PZERO, "spi", hz/2);
522 cqspi_init(struct cqspi_softc *sc)
524 pcell_t dts_value[1];
529 device_printf(sc->dev, "Module ID %x\n",
530 READ4(sc, CQSPI_MODULEID));
532 if ((node = ofw_bus_get_node(sc->dev)) == -1) {
536 if ((len = OF_getproplen(node, "cdns,fifo-depth")) <= 0) {
539 OF_getencprop(node, "cdns,fifo-depth", dts_value, len);
540 sc->fifo_depth = dts_value[0];
542 if ((len = OF_getproplen(node, "cdns,fifo-width")) <= 0) {
545 OF_getencprop(node, "cdns,fifo-width", dts_value, len);
546 sc->fifo_width = dts_value[0];
548 if ((len = OF_getproplen(node, "cdns,trigger-address")) <= 0) {
551 OF_getencprop(node, "cdns,trigger-address", dts_value, len);
552 sc->trigger_address = dts_value[0];
554 /* Disable controller */
555 reg = READ4(sc, CQSPI_CFG);
557 WRITE4(sc, CQSPI_CFG, reg);
559 reg = READ4(sc, CQSPI_DEVSZ);
560 reg &= ~(DEVSZ_NUMADDRBYTES_M);
561 reg |= ((4 - 1) - DEVSZ_NUMADDRBYTES_S);
562 WRITE4(sc, CQSPI_DEVSZ, reg);
564 WRITE4(sc, CQSPI_SRAMPART, sc->fifo_depth/2);
566 /* TODO: calculate baud rate and delay values. */
568 reg = READ4(sc, CQSPI_CFG);
569 /* Configure baud rate */
570 reg &= ~(CFG_BAUD_M);
573 WRITE4(sc, CQSPI_CFG, reg);
575 reg = (3 << DELAY_NSS_S);
576 reg |= (3 << DELAY_BTWN_S);
577 reg |= (1 << DELAY_AFTER_S);
578 reg |= (1 << DELAY_INIT_S);
579 WRITE4(sc, CQSPI_DELAY, reg);
581 READ4(sc, CQSPI_RDDATACAP);
582 reg &= ~(RDDATACAP_DELAY_M);
583 reg |= (1 << RDDATACAP_DELAY_S);
584 WRITE4(sc, CQSPI_RDDATACAP, reg);
586 /* Enable controller */
587 reg = READ4(sc, CQSPI_CFG);
589 WRITE4(sc, CQSPI_CFG, reg);
595 cqspi_add_devices(device_t dev)
597 phandle_t child, node;
601 node = ofw_bus_get_node(dev);
603 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
605 simplebus_add_device(dev, child, 0, NULL, -1, NULL);
606 if (child_dev == NULL) {
610 error = device_probe_and_attach(child_dev);
612 printf("can't probe and attach: %d\n", error);
620 cqspi_delayed_attach(void *arg)
622 struct cqspi_softc *sc;
626 cqspi_add_devices(sc->dev);
627 bus_generic_attach(sc->dev);
629 config_intrhook_disestablish(&sc->config_intrhook);
633 cqspi_probe(device_t dev)
636 if (!ofw_bus_status_okay(dev)) {
640 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) {
644 device_set_desc(dev, "Cadence Quad SPI controller");
650 cqspi_attach(device_t dev)
652 struct cqspi_softc *sc;
656 sc = device_get_softc(dev);
659 if (bus_alloc_resources(dev, cqspi_spec, sc->res)) {
660 device_printf(dev, "could not allocate resources\n");
664 /* Memory interface */
665 sc->bst = rman_get_bustag(sc->res[0]);
666 sc->bsh = rman_get_bushandle(sc->res[0]);
668 sc->sram_phys = rman_get_start(sc->res[1]);
670 /* Setup interrupt handlers */
671 if (bus_setup_intr(sc->dev, sc->res[2], INTR_TYPE_BIO | INTR_MPSAFE,
672 NULL, cqspi_intr, sc, &sc->ih)) {
673 device_printf(sc->dev, "Unable to setup intr\n");
681 /* Get xDMA controller. */
682 sc->xdma_tx = xdma_ofw_get(sc->dev, "tx");
683 if (sc->xdma_tx == NULL) {
684 device_printf(dev, "Can't find DMA controller.\n");
688 sc->xdma_rx = xdma_ofw_get(sc->dev, "rx");
689 if (sc->xdma_rx == NULL) {
690 device_printf(dev, "Can't find DMA controller.\n");
694 /* Alloc xDMA virtual channels. */
695 sc->xchan_tx = xdma_channel_alloc(sc->xdma_tx, caps);
696 if (sc->xchan_tx == NULL) {
697 device_printf(dev, "Can't alloc virtual DMA channel.\n");
701 sc->xchan_rx = xdma_channel_alloc(sc->xdma_rx, caps);
702 if (sc->xchan_rx == NULL) {
703 device_printf(dev, "Can't alloc virtual DMA channel.\n");
707 /* Setup xDMA interrupt handlers. */
708 error = xdma_setup_intr(sc->xchan_tx, cqspi_xdma_tx_intr,
711 device_printf(sc->dev,
712 "Can't setup xDMA interrupt handler.\n");
716 error = xdma_setup_intr(sc->xchan_rx, cqspi_xdma_rx_intr,
719 device_printf(sc->dev,
720 "Can't setup xDMA interrupt handler.\n");
724 xdma_prep_sg(sc->xchan_tx, TX_QUEUE_SIZE, MAXPHYS, 8, 16, 0,
725 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR);
726 xdma_prep_sg(sc->xchan_rx, TX_QUEUE_SIZE, MAXPHYS, 8, 16, 0,
727 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR);
731 sc->config_intrhook.ich_func = cqspi_delayed_attach;
732 sc->config_intrhook.ich_arg = sc;
733 if (config_intrhook_establish(&sc->config_intrhook) != 0) {
734 device_printf(dev, "config_intrhook_establish failed\n");
742 cqspi_detach(device_t dev)
748 static device_method_t cqspi_methods[] = {
749 /* Device interface */
750 DEVMETHOD(device_probe, cqspi_probe),
751 DEVMETHOD(device_attach, cqspi_attach),
752 DEVMETHOD(device_detach, cqspi_detach),
754 /* Quad SPI Flash Interface */
755 DEVMETHOD(qspi_read_reg, cqspi_read_reg),
756 DEVMETHOD(qspi_write_reg, cqspi_write_reg),
757 DEVMETHOD(qspi_read, cqspi_read),
758 DEVMETHOD(qspi_write, cqspi_write),
759 DEVMETHOD(qspi_erase, cqspi_erase),
764 static devclass_t cqspi_devclass;
766 DEFINE_CLASS_1(cqspi, cqspi_driver, cqspi_methods,
767 sizeof(struct cqspi_softc), simplebus_driver);
769 DRIVER_MODULE(cqspi, simplebus, cqspi_driver, cqspi_devclass, 0, 0);