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>
53 #include <geom/geom_disk.h>
55 #include <machine/bus.h>
57 #include <dev/fdt/simplebus.h>
58 #include <dev/fdt/fdt_common.h>
59 #include <dev/ofw/ofw_bus_subr.h>
60 #include <dev/ofw/openfirm.h>
62 #include <dev/flash/cqspi.h>
63 #include <dev/flash/mx25lreg.h>
64 #include <dev/xdma/xdma.h>
72 #define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__)
74 #define dprintf(fmt, ...)
77 #define CQSPI_SECTORSIZE 512
78 #define TX_QUEUE_SIZE 16
79 #define RX_QUEUE_SIZE 16
81 #define READ4(_sc, _reg) bus_read_4((_sc)->res[0], _reg)
82 #define READ2(_sc, _reg) bus_read_2((_sc)->res[0], _reg)
83 #define READ1(_sc, _reg) bus_read_1((_sc)->res[0], _reg)
84 #define WRITE4(_sc, _reg, _val) bus_write_4((_sc)->res[0], _reg, _val)
85 #define WRITE2(_sc, _reg, _val) bus_write_2((_sc)->res[0], _reg, _val)
86 #define WRITE1(_sc, _reg, _val) bus_write_1((_sc)->res[0], _reg, _val)
87 #define READ_DATA_4(_sc, _reg) bus_read_4((_sc)->res[1], _reg)
88 #define READ_DATA_1(_sc, _reg) bus_read_1((_sc)->res[1], _reg)
89 #define WRITE_DATA_4(_sc, _reg, _val) bus_write_4((_sc)->res[1], _reg, _val)
90 #define WRITE_DATA_1(_sc, _reg, _val) bus_write_1((_sc)->res[1], _reg, _val)
95 struct resource *res[3];
97 bus_space_handle_t bsh;
100 uint8_t write_op_done;
104 uint32_t trigger_address;
108 xdma_controller_t *xdma_tx;
109 xdma_channel_t *xchan_tx;
112 xdma_controller_t *xdma_rx;
113 xdma_channel_t *xchan_rx;
116 struct intr_config_hook config_intrhook;
120 #define CQSPI_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
121 #define CQSPI_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
122 #define CQSPI_LOCK_INIT(_sc) \
123 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
125 #define CQSPI_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
126 #define CQSPI_ASSERT_LOCKED(_sc) \
127 mtx_assert(&_sc->sc_mtx, MA_OWNED);
128 #define CQSPI_ASSERT_UNLOCKED(_sc) \
129 mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
131 static struct resource_spec cqspi_spec[] = {
132 { SYS_RES_MEMORY, 0, RF_ACTIVE },
133 { SYS_RES_MEMORY, 1, RF_ACTIVE },
134 { SYS_RES_IRQ, 0, RF_ACTIVE },
138 static struct ofw_compat_data compat_data[] = {
139 { "cdns,qspi-nor", 1 },
144 cqspi_intr(void *arg)
146 struct cqspi_softc *sc;
151 pending = READ4(sc, CQSPI_IRQSTAT);
153 dprintf("%s: IRQSTAT %x\n", __func__, pending);
155 if (pending & (IRQMASK_INDOPDONE | IRQMASK_INDXFRLVL |
156 IRQMASK_INDSRAMFULL)) {
157 /* TODO: PIO operation done */
160 WRITE4(sc, CQSPI_IRQSTAT, pending);
164 cqspi_xdma_tx_intr(void *arg, xdma_transfer_status_t *status)
166 struct xdma_transfer_status st;
167 struct cqspi_softc *sc;
174 dprintf("%s\n", __func__);
179 ret = xdma_dequeue_bio(sc->xchan_tx, &bp, &st);
183 sc->write_op_done = 1;
188 device_printf(sc->dev,
189 "Warning: more than 1 tx bio dequeued\n");
191 wakeup(&sc->xdma_tx);
197 cqspi_xdma_rx_intr(void *arg, xdma_transfer_status_t *status)
199 struct xdma_transfer_status st;
200 struct cqspi_softc *sc;
207 dprintf("%s\n", __func__);
212 ret = xdma_dequeue_bio(sc->xchan_rx, &bp, &st);
216 sc->read_op_done = 1;
221 device_printf(sc->dev,
222 "Warning: more than 1 rx bio dequeued\n");
224 wakeup(&sc->xdma_rx);
230 cqspi_wait_for_completion(struct cqspi_softc *sc)
237 for (i = timeout; i > 0; i--) {
238 if ((READ4(sc, CQSPI_FLASHCMD) & FLASHCMD_CMDEXECSTAT) == 0) {
244 device_printf(sc->dev, "%s: cmd timed out: %x\n",
245 __func__, READ4(sc, CQSPI_FLASHCMD));
253 cqspi_cmd_write_addr(struct cqspi_softc *sc, uint8_t cmd,
254 uint32_t addr, uint32_t len)
259 dprintf("%s: %x\n", __func__, cmd);
261 WRITE4(sc, CQSPI_FLASHCMDADDR, addr);
262 reg = (cmd << FLASHCMD_CMDOPCODE_S);
263 reg |= (FLASHCMD_ENCMDADDR);
264 reg |= ((len - 1) << FLASHCMD_NUMADDRBYTES_S);
265 WRITE4(sc, CQSPI_FLASHCMD, reg);
267 reg |= FLASHCMD_EXECCMD;
268 WRITE4(sc, CQSPI_FLASHCMD, reg);
270 ret = cqspi_wait_for_completion(sc);
276 cqspi_cmd_write(struct cqspi_softc *sc, uint8_t cmd,
277 uint8_t *addr, uint32_t len)
282 reg = (cmd << FLASHCMD_CMDOPCODE_S);
283 WRITE4(sc, CQSPI_FLASHCMD, reg);
284 reg |= FLASHCMD_EXECCMD;
285 WRITE4(sc, CQSPI_FLASHCMD, reg);
287 ret = cqspi_wait_for_completion(sc);
293 cqspi_cmd_read(struct cqspi_softc *sc, uint8_t cmd,
294 uint8_t *addr, uint32_t len)
303 device_printf(sc->dev, "Failed to read data\n");
307 dprintf("%s: %x\n", __func__, cmd);
309 buf = (uint8_t *)addr;
311 reg = (cmd << FLASHCMD_CMDOPCODE_S);
312 reg |= ((len - 1) << FLASHCMD_NUMRDDATABYTES_S);
313 reg |= FLASHCMD_ENRDDATA;
314 WRITE4(sc, CQSPI_FLASHCMD, reg);
316 reg |= FLASHCMD_EXECCMD;
317 WRITE4(sc, CQSPI_FLASHCMD, reg);
319 ret = cqspi_wait_for_completion(sc);
321 device_printf(sc->dev, "%s: cmd failed: %x\n",
326 data = READ4(sc, CQSPI_FLASHCMDRDDATALO);
328 for (i = 0; i < len; i++)
329 buf[i] = (data >> (i * 8)) & 0xff;
335 cqspi_wait_ready(struct cqspi_softc *sc)
341 ret = cqspi_cmd_read(sc, CMD_READ_STATUS, &data, 1);
342 } while (data & STATUS_WIP);
348 cqspi_write_reg(device_t dev, device_t child,
349 uint8_t opcode, uint8_t *addr, uint32_t len)
351 struct cqspi_softc *sc;
354 sc = device_get_softc(dev);
356 ret = cqspi_cmd_write(sc, opcode, addr, len);
362 cqspi_read_reg(device_t dev, device_t child,
363 uint8_t opcode, uint8_t *addr, uint32_t len)
365 struct cqspi_softc *sc;
368 sc = device_get_softc(dev);
370 ret = cqspi_cmd_read(sc, opcode, addr, len);
376 cqspi_wait_idle(struct cqspi_softc *sc)
381 reg = READ4(sc, CQSPI_CFG);
382 if (reg & CFG_IDLE) {
391 cqspi_erase(device_t dev, device_t child, off_t offset)
393 struct cqspi_softc *sc;
396 sc = device_get_softc(dev);
399 cqspi_wait_ready(sc);
400 ret = cqspi_cmd_write(sc, CMD_WRITE_ENABLE, 0, 0);
403 cqspi_wait_ready(sc);
404 ret = cqspi_cmd_write_addr(sc, CMD_QUAD_SECTOR_ERASE, offset, 4);
412 cqspi_write(device_t dev, device_t child, struct bio *bp,
413 off_t offset, caddr_t data, off_t count)
415 struct cqspi_softc *sc;
418 dprintf("%s: offset 0x%llx count %lld bytes\n",
419 __func__, offset, count);
421 sc = device_get_softc(dev);
423 cqspi_wait_ready(sc);
424 reg = cqspi_cmd_write(sc, CMD_WRITE_ENABLE, 0, 0);
427 cqspi_wait_ready(sc);
430 reg = DMAPER_NUMSGLREQBYTES_4;
431 reg |= DMAPER_NUMBURSTREQBYTES_4;
432 WRITE4(sc, CQSPI_DMAPER, reg);
434 WRITE4(sc, CQSPI_INDWRWATER, 64);
435 WRITE4(sc, CQSPI_INDWR, INDRD_IND_OPS_DONE_STATUS);
436 WRITE4(sc, CQSPI_INDWR, 0);
438 WRITE4(sc, CQSPI_INDWRCNT, count);
439 WRITE4(sc, CQSPI_INDWRSTADDR, offset);
441 reg = (0 << DEVWR_DUMMYWRCLKS_S);
442 reg |= DEVWR_DATA_WIDTH_QUAD;
443 reg |= DEVWR_ADDR_WIDTH_SINGLE;
444 reg |= (CMD_QUAD_PAGE_PROGRAM << DEVWR_WROPCODE_S);
445 WRITE4(sc, CQSPI_DEVWR, reg);
447 reg = DEVRD_DATA_WIDTH_QUAD;
448 reg |= DEVRD_ADDR_WIDTH_SINGLE;
449 reg |= DEVRD_INST_WIDTH_SINGLE;
450 WRITE4(sc, CQSPI_DEVRD, reg);
452 xdma_enqueue_bio(sc->xchan_tx, &bp,
453 sc->sram_phys, 4, 4, XDMA_MEM_TO_DEV);
454 xdma_queue_submit(sc->xchan_tx);
456 sc->write_op_done = 0;
458 WRITE4(sc, CQSPI_INDWR, INDRD_START);
460 while (sc->write_op_done == 0)
461 tsleep(&sc->xdma_tx, PCATCH | PZERO, "spi", hz/2);
469 cqspi_read(device_t dev, device_t child, struct bio *bp,
470 off_t offset, caddr_t data, off_t count)
472 struct cqspi_softc *sc;
475 sc = device_get_softc(dev);
477 dprintf("%s: offset 0x%llx count %lld bytes\n",
478 __func__, offset, count);
482 reg = DMAPER_NUMSGLREQBYTES_4;
483 reg |= DMAPER_NUMBURSTREQBYTES_4;
484 WRITE4(sc, CQSPI_DMAPER, reg);
486 WRITE4(sc, CQSPI_INDRDWATER, 64);
487 WRITE4(sc, CQSPI_INDRD, INDRD_IND_OPS_DONE_STATUS);
488 WRITE4(sc, CQSPI_INDRD, 0);
490 WRITE4(sc, CQSPI_INDRDCNT, count);
491 WRITE4(sc, CQSPI_INDRDSTADDR, offset);
493 reg = (0 << DEVRD_DUMMYRDCLKS_S);
494 reg |= DEVRD_DATA_WIDTH_QUAD;
495 reg |= DEVRD_ADDR_WIDTH_SINGLE;
496 reg |= DEVRD_INST_WIDTH_SINGLE;
497 reg |= DEVRD_ENMODEBITS;
498 reg |= (CMD_READ_4B_QUAD_OUTPUT << DEVRD_RDOPCODE_S);
499 WRITE4(sc, CQSPI_DEVRD, reg);
501 WRITE4(sc, CQSPI_MODEBIT, 0xff);
502 WRITE4(sc, CQSPI_IRQMASK, 0);
504 xdma_enqueue_bio(sc->xchan_rx, &bp, sc->sram_phys, 4, 4,
506 xdma_queue_submit(sc->xchan_rx);
508 sc->read_op_done = 0;
510 WRITE4(sc, CQSPI_INDRD, INDRD_START);
512 while (sc->read_op_done == 0)
513 tsleep(&sc->xdma_rx, PCATCH | PZERO, "spi", hz/2);
521 cqspi_init(struct cqspi_softc *sc)
523 pcell_t dts_value[1];
528 device_printf(sc->dev, "Module ID %x\n",
529 READ4(sc, CQSPI_MODULEID));
531 if ((node = ofw_bus_get_node(sc->dev)) == -1) {
535 if ((len = OF_getproplen(node, "cdns,fifo-depth")) <= 0) {
538 OF_getencprop(node, "cdns,fifo-depth", dts_value, len);
539 sc->fifo_depth = dts_value[0];
541 if ((len = OF_getproplen(node, "cdns,fifo-width")) <= 0) {
544 OF_getencprop(node, "cdns,fifo-width", dts_value, len);
545 sc->fifo_width = dts_value[0];
547 if ((len = OF_getproplen(node, "cdns,trigger-address")) <= 0) {
550 OF_getencprop(node, "cdns,trigger-address", dts_value, len);
551 sc->trigger_address = dts_value[0];
553 /* Disable controller */
554 reg = READ4(sc, CQSPI_CFG);
556 WRITE4(sc, CQSPI_CFG, reg);
558 reg = READ4(sc, CQSPI_DEVSZ);
559 reg &= ~(DEVSZ_NUMADDRBYTES_M);
560 reg |= ((4 - 1) - DEVSZ_NUMADDRBYTES_S);
561 WRITE4(sc, CQSPI_DEVSZ, reg);
563 WRITE4(sc, CQSPI_SRAMPART, sc->fifo_depth/2);
565 /* TODO: calculate baud rate and delay values. */
567 reg = READ4(sc, CQSPI_CFG);
568 /* Configure baud rate */
569 reg &= ~(CFG_BAUD_M);
572 WRITE4(sc, CQSPI_CFG, reg);
574 reg = (3 << DELAY_NSS_S);
575 reg |= (3 << DELAY_BTWN_S);
576 reg |= (1 << DELAY_AFTER_S);
577 reg |= (1 << DELAY_INIT_S);
578 WRITE4(sc, CQSPI_DELAY, reg);
580 READ4(sc, CQSPI_RDDATACAP);
581 reg &= ~(RDDATACAP_DELAY_M);
582 reg |= (1 << RDDATACAP_DELAY_S);
583 WRITE4(sc, CQSPI_RDDATACAP, reg);
585 /* Enable controller */
586 reg = READ4(sc, CQSPI_CFG);
588 WRITE4(sc, CQSPI_CFG, reg);
594 cqspi_add_devices(device_t dev)
596 phandle_t child, node;
600 node = ofw_bus_get_node(dev);
602 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
604 simplebus_add_device(dev, child, 0, NULL, -1, NULL);
605 if (child_dev == NULL) {
609 error = device_probe_and_attach(child_dev);
611 printf("can't probe and attach: %d\n", error);
619 cqspi_delayed_attach(void *arg)
621 struct cqspi_softc *sc;
625 cqspi_add_devices(sc->dev);
626 bus_generic_attach(sc->dev);
628 config_intrhook_disestablish(&sc->config_intrhook);
632 cqspi_probe(device_t dev)
635 if (!ofw_bus_status_okay(dev)) {
639 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) {
643 device_set_desc(dev, "Cadence Quad SPI controller");
649 cqspi_attach(device_t dev)
651 struct cqspi_softc *sc;
655 sc = device_get_softc(dev);
658 if (bus_alloc_resources(dev, cqspi_spec, sc->res)) {
659 device_printf(dev, "could not allocate resources\n");
663 /* Memory interface */
664 sc->bst = rman_get_bustag(sc->res[0]);
665 sc->bsh = rman_get_bushandle(sc->res[0]);
667 sc->sram_phys = rman_get_start(sc->res[1]);
669 /* Setup interrupt handlers */
670 if (bus_setup_intr(sc->dev, sc->res[2], INTR_TYPE_BIO | INTR_MPSAFE,
671 NULL, cqspi_intr, sc, &sc->ih)) {
672 device_printf(sc->dev, "Unable to setup intr\n");
680 /* Get xDMA controller. */
681 sc->xdma_tx = xdma_ofw_get(sc->dev, "tx");
682 if (sc->xdma_tx == NULL) {
683 device_printf(dev, "Can't find DMA controller.\n");
687 sc->xdma_rx = xdma_ofw_get(sc->dev, "rx");
688 if (sc->xdma_rx == NULL) {
689 device_printf(dev, "Can't find DMA controller.\n");
693 /* Alloc xDMA virtual channels. */
694 sc->xchan_tx = xdma_channel_alloc(sc->xdma_tx, caps);
695 if (sc->xchan_tx == NULL) {
696 device_printf(dev, "Can't alloc virtual DMA channel.\n");
700 sc->xchan_rx = xdma_channel_alloc(sc->xdma_rx, caps);
701 if (sc->xchan_rx == NULL) {
702 device_printf(dev, "Can't alloc virtual DMA channel.\n");
706 /* Setup xDMA interrupt handlers. */
707 error = xdma_setup_intr(sc->xchan_tx, cqspi_xdma_tx_intr,
710 device_printf(sc->dev,
711 "Can't setup xDMA interrupt handler.\n");
715 error = xdma_setup_intr(sc->xchan_rx, cqspi_xdma_rx_intr,
718 device_printf(sc->dev,
719 "Can't setup xDMA interrupt handler.\n");
723 xdma_prep_sg(sc->xchan_tx, TX_QUEUE_SIZE, MAXPHYS, 8, 16, 0,
724 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR);
725 xdma_prep_sg(sc->xchan_rx, TX_QUEUE_SIZE, MAXPHYS, 8, 16, 0,
726 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR);
730 sc->config_intrhook.ich_func = cqspi_delayed_attach;
731 sc->config_intrhook.ich_arg = sc;
732 if (config_intrhook_establish(&sc->config_intrhook) != 0) {
733 device_printf(dev, "config_intrhook_establish failed\n");
741 cqspi_detach(device_t dev)
747 static device_method_t cqspi_methods[] = {
748 /* Device interface */
749 DEVMETHOD(device_probe, cqspi_probe),
750 DEVMETHOD(device_attach, cqspi_attach),
751 DEVMETHOD(device_detach, cqspi_detach),
753 /* Quad SPI Flash Interface */
754 DEVMETHOD(qspi_read_reg, cqspi_read_reg),
755 DEVMETHOD(qspi_write_reg, cqspi_write_reg),
756 DEVMETHOD(qspi_read, cqspi_read),
757 DEVMETHOD(qspi_write, cqspi_write),
758 DEVMETHOD(qspi_erase, cqspi_erase),
763 static devclass_t cqspi_devclass;
765 DEFINE_CLASS_1(cqspi, cqspi_driver, cqspi_methods,
766 sizeof(struct cqspi_softc), simplebus_driver);
768 DRIVER_MODULE(cqspi, simplebus, cqspi_driver, cqspi_devclass, 0, 0);