12de9b4d3SEmmanuel Vadot /*- 22de9b4d3SEmmanuel Vadot * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 32de9b4d3SEmmanuel Vadot * 42de9b4d3SEmmanuel Vadot * Copyright (c) 2018 Thomas Skibo <thomasskibo@yahoo.com> 52de9b4d3SEmmanuel Vadot * All rights reserved. 62de9b4d3SEmmanuel Vadot * 72de9b4d3SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without 82de9b4d3SEmmanuel Vadot * modification, are permitted provided that the following conditions 92de9b4d3SEmmanuel Vadot * are met: 102de9b4d3SEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright 112de9b4d3SEmmanuel Vadot * notice, this list of conditions and the following disclaimer. 122de9b4d3SEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright 132de9b4d3SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the 142de9b4d3SEmmanuel Vadot * documentation and/or other materials provided with the distribution. 152de9b4d3SEmmanuel Vadot * 162de9b4d3SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 172de9b4d3SEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 182de9b4d3SEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 192de9b4d3SEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 202de9b4d3SEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 212de9b4d3SEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 222de9b4d3SEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 232de9b4d3SEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 242de9b4d3SEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 252de9b4d3SEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 262de9b4d3SEmmanuel Vadot * SUCH DAMAGE. 272de9b4d3SEmmanuel Vadot */ 282de9b4d3SEmmanuel Vadot 292de9b4d3SEmmanuel Vadot #include <sys/cdefs.h> 302de9b4d3SEmmanuel Vadot __FBSDID("$FreeBSD$"); 312de9b4d3SEmmanuel Vadot 322de9b4d3SEmmanuel Vadot /* 332de9b4d3SEmmanuel Vadot * This is a driver for the Quad-SPI Flash Controller in the Xilinx 342de9b4d3SEmmanuel Vadot * Zynq-7000 SoC. 352de9b4d3SEmmanuel Vadot */ 362de9b4d3SEmmanuel Vadot 372de9b4d3SEmmanuel Vadot #include <sys/param.h> 382de9b4d3SEmmanuel Vadot #include <sys/systm.h> 392de9b4d3SEmmanuel Vadot #include <sys/conf.h> 402de9b4d3SEmmanuel Vadot #include <sys/kernel.h> 412de9b4d3SEmmanuel Vadot #include <sys/module.h> 422de9b4d3SEmmanuel Vadot #include <sys/sysctl.h> 432de9b4d3SEmmanuel Vadot #include <sys/lock.h> 442de9b4d3SEmmanuel Vadot #include <sys/mutex.h> 452de9b4d3SEmmanuel Vadot #include <sys/resource.h> 462de9b4d3SEmmanuel Vadot #include <sys/rman.h> 472de9b4d3SEmmanuel Vadot #include <sys/uio.h> 482de9b4d3SEmmanuel Vadot 492de9b4d3SEmmanuel Vadot #include <machine/bus.h> 502de9b4d3SEmmanuel Vadot #include <machine/resource.h> 512de9b4d3SEmmanuel Vadot #include <machine/stdarg.h> 522de9b4d3SEmmanuel Vadot 532de9b4d3SEmmanuel Vadot #include <dev/fdt/fdt_common.h> 542de9b4d3SEmmanuel Vadot #include <dev/ofw/ofw_bus.h> 552de9b4d3SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h> 562de9b4d3SEmmanuel Vadot 572de9b4d3SEmmanuel Vadot #include <dev/spibus/spi.h> 582de9b4d3SEmmanuel Vadot #include <dev/spibus/spibusvar.h> 592de9b4d3SEmmanuel Vadot 602de9b4d3SEmmanuel Vadot #include <dev/flash/mx25lreg.h> 612de9b4d3SEmmanuel Vadot 622de9b4d3SEmmanuel Vadot #include "spibus_if.h" 632de9b4d3SEmmanuel Vadot 642de9b4d3SEmmanuel Vadot static struct ofw_compat_data compat_data[] = { 652de9b4d3SEmmanuel Vadot {"xlnx,zy7_qspi", 1}, 662de9b4d3SEmmanuel Vadot {"xlnx,zynq-qspi-1.0", 1}, 672de9b4d3SEmmanuel Vadot {NULL, 0} 682de9b4d3SEmmanuel Vadot }; 692de9b4d3SEmmanuel Vadot 702de9b4d3SEmmanuel Vadot struct zy7_qspi_softc { 712de9b4d3SEmmanuel Vadot device_t dev; 722de9b4d3SEmmanuel Vadot device_t child; 732de9b4d3SEmmanuel Vadot struct mtx sc_mtx; 742de9b4d3SEmmanuel Vadot struct resource *mem_res; 752de9b4d3SEmmanuel Vadot struct resource *irq_res; 762de9b4d3SEmmanuel Vadot void *intrhandle; 772de9b4d3SEmmanuel Vadot 782de9b4d3SEmmanuel Vadot uint32_t cfg_reg_shadow; 792de9b4d3SEmmanuel Vadot uint32_t lqspi_cfg_shadow; 802de9b4d3SEmmanuel Vadot uint32_t spi_clock; 812de9b4d3SEmmanuel Vadot uint32_t ref_clock; 822de9b4d3SEmmanuel Vadot unsigned int spi_clk_real_freq; 832de9b4d3SEmmanuel Vadot unsigned int rx_overflows; 842de9b4d3SEmmanuel Vadot unsigned int tx_underflows; 852de9b4d3SEmmanuel Vadot unsigned int interrupts; 862de9b4d3SEmmanuel Vadot unsigned int stray_ints; 872de9b4d3SEmmanuel Vadot struct spi_command *cmd; 882de9b4d3SEmmanuel Vadot int tx_bytes; /* tx_cmd_sz + tx_data_sz */ 892de9b4d3SEmmanuel Vadot int tx_bytes_sent; 902de9b4d3SEmmanuel Vadot int rx_bytes; /* rx_cmd_sz + rx_data_sz */ 912de9b4d3SEmmanuel Vadot int rx_bytes_rcvd; 922de9b4d3SEmmanuel Vadot int busy; 932de9b4d3SEmmanuel Vadot int is_dual; 942de9b4d3SEmmanuel Vadot int is_stacked; 952de9b4d3SEmmanuel Vadot int is_dio; 962de9b4d3SEmmanuel Vadot }; 972de9b4d3SEmmanuel Vadot 982de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DEFAULT_SPI_CLOCK 50000000 992de9b4d3SEmmanuel Vadot 1002de9b4d3SEmmanuel Vadot #define QSPI_SC_LOCK(sc) mtx_lock(&(sc)->sc_mtx) 1012de9b4d3SEmmanuel Vadot #define QSPI_SC_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx) 1022de9b4d3SEmmanuel Vadot #define QSPI_SC_LOCK_INIT(sc) \ 1032de9b4d3SEmmanuel Vadot mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev), NULL, MTX_DEF) 1042de9b4d3SEmmanuel Vadot #define QSPI_SC_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx) 1052de9b4d3SEmmanuel Vadot #define QSPI_SC_ASSERT_LOCKED(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED) 1062de9b4d3SEmmanuel Vadot 1072de9b4d3SEmmanuel Vadot #define RD4(sc, off) (bus_read_4((sc)->mem_res, (off))) 1082de9b4d3SEmmanuel Vadot #define WR4(sc, off, val) (bus_write_4((sc)->mem_res, (off), (val))) 1092de9b4d3SEmmanuel Vadot 1102de9b4d3SEmmanuel Vadot /* 1112de9b4d3SEmmanuel Vadot * QSPI device registers. 1122de9b4d3SEmmanuel Vadot * Reference: Zynq-7000 All Programmable SoC Technical Reference Manual. 1132de9b4d3SEmmanuel Vadot * (v1.12.2) July 1, 2018. Xilinx doc UG585. 1142de9b4d3SEmmanuel Vadot */ 1152de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_REG 0x0000 1162de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_IFMODE (1U << 31) 1172de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_ENDIAN (1 << 26) 1182de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_HOLDB_DR (1 << 19) 1192de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_RSVD1 (1 << 17) /* must be 1 */ 1202de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_MANSTRT (1 << 16) 1212de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_MANSTRTEN (1 << 15) 1222de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_SSFORCE (1 << 14) 1232de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_PCS (1 << 10) 1242de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_REF_CLK (1 << 8) 1252de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_FIFO_WIDTH_MASK (3 << 6) 1262de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_FIFO_WIDTH32 (3 << 6) 1272de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_BAUD_RATE_DIV_MASK (7 << 3) 1282de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_BAUD_RATE_DIV_SHIFT 3 1292de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_BAUD_RATE_DIV(x) ((x) << 3) /* divide by 2<<x */ 1302de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_CLK_PH (1 << 2) /* clock phase */ 1312de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_CLK_POL (1 << 1) /* clock polarity */ 1322de9b4d3SEmmanuel Vadot #define ZY7_QSPI_CONFIG_MODE_SEL (1 << 0) /* master enable */ 1332de9b4d3SEmmanuel Vadot 1342de9b4d3SEmmanuel Vadot #define ZY7_QSPI_INTR_STAT_REG 0x0004 1352de9b4d3SEmmanuel Vadot #define ZY7_QSPI_INTR_EN_REG 0x0008 1362de9b4d3SEmmanuel Vadot #define ZY7_QSPI_INTR_DIS_REG 0x000c 1372de9b4d3SEmmanuel Vadot #define ZY7_QSPI_INTR_MASK_REG 0x0010 1382de9b4d3SEmmanuel Vadot #define ZY7_QSPI_INTR_TX_FIFO_UNDERFLOW (1 << 6) 1392de9b4d3SEmmanuel Vadot #define ZY7_QSPI_INTR_RX_FIFO_FULL (1 << 5) 1402de9b4d3SEmmanuel Vadot #define ZY7_QSPI_INTR_RX_FIFO_NOT_EMPTY (1 << 4) 1412de9b4d3SEmmanuel Vadot #define ZY7_QSPI_INTR_TX_FIFO_FULL (1 << 3) 1422de9b4d3SEmmanuel Vadot #define ZY7_QSPI_INTR_TX_FIFO_NOT_FULL (1 << 2) 1432de9b4d3SEmmanuel Vadot #define ZY7_QSPI_INTR_RX_OVERFLOW (1 << 0) 1442de9b4d3SEmmanuel Vadot 1452de9b4d3SEmmanuel Vadot #define ZY7_QSPI_EN_REG 0x0014 1462de9b4d3SEmmanuel Vadot #define ZY7_SPI_ENABLE 1 1472de9b4d3SEmmanuel Vadot 1482de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_REG 0x0018 1492de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_NSS_MASK (0xffU << 24) 1502de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_NSS_SHIFT 24 1512de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_NSS(x) ((x) << 24) 1522de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_BTWN_MASK (0xff << 16) 1532de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_BTWN_SHIFT 16 1542de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_BTWN(x) ((x) << 16) 1552de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_AFTER_MASK (0xff << 8) 1562de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_AFTER_SHIFT 8 1572de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_AFTER(x) ((x) << 8) 1582de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_INIT_MASK 0xff 1592de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_INIT_SHIFT 0 1602de9b4d3SEmmanuel Vadot #define ZY7_QSPI_DELAY_INIT(x) (x) 1612de9b4d3SEmmanuel Vadot 1622de9b4d3SEmmanuel Vadot #define ZY7_QSPI_TXD0_REG 0x001c 1632de9b4d3SEmmanuel Vadot #define ZY7_QSPI_RX_DATA_REG 0x0020 1642de9b4d3SEmmanuel Vadot 1652de9b4d3SEmmanuel Vadot #define ZY7_QSPI_SLV_IDLE_CT_REG 0x0024 1662de9b4d3SEmmanuel Vadot #define ZY7_QSPI_SLV_IDLE_CT_MASK 0xff 1672de9b4d3SEmmanuel Vadot 1682de9b4d3SEmmanuel Vadot #define ZY7_QSPI_TX_THRESH_REG 0x0028 1692de9b4d3SEmmanuel Vadot #define ZY7_QSPI_RX_THRESH_REG 0x002c 1702de9b4d3SEmmanuel Vadot 1712de9b4d3SEmmanuel Vadot #define ZY7_QSPI_GPIO_REG 0x0030 1722de9b4d3SEmmanuel Vadot #define ZY7_QSPI_GPIO_WP_N 1 1732de9b4d3SEmmanuel Vadot 1742de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LPBK_DLY_ADJ_REG 0x0038 1752de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LPBK_DLY_ADJ_LPBK_SEL (1 << 8) 1762de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LPBK_DLY_ADJ_LPBK_PH (1 << 7) 1772de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LPBK_DLY_ADJ_USE_LPBK (1 << 5) 1782de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LPBK_DLY_ADJ_DLY1_MASK (3 << 3) 1792de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LPBK_DLY_ADJ_DLY1_SHIFT 3 1802de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LPBK_DLY_ADJ_DLY1(x) ((x) << 3) 1812de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LPBK_DLY_ADJ_DLY0_MASK 7 1822de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LPBK_DLY_ADJ_DLY0_SHIFT 0 1832de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LPBK_DLY_ADJ_DLY0(x) (x) 1842de9b4d3SEmmanuel Vadot 1852de9b4d3SEmmanuel Vadot #define ZY7_QSPI_TXD1_REG 0x0080 1862de9b4d3SEmmanuel Vadot #define ZY7_QSPI_TXD2_REG 0x0084 1872de9b4d3SEmmanuel Vadot #define ZY7_QSPI_TXD3_REG 0x0088 1882de9b4d3SEmmanuel Vadot 1892de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_REG 0x00a0 1902de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_LINEAR (1U << 31) 1912de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_TWO_MEM (1 << 30) 1922de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_SEP_BUS (1 << 29) 1932de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_U_PAGE (1 << 28) 1942de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_MODE_EN (1 << 25) 1952de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_MODE_ON (1 << 24) 1962de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_MODE_BITS_MASK (0xff << 16) 1972de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_MODE_BITS_SHIFT 16 1982de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_MODE_BITS(x) ((x) << 16) 1992de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_DUMMY_BYTES_MASK (7 << 8) 2002de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_DUMMY_BYTES_SHIFT 8 2012de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_DUMMY_BYTES(x) ((x) << 8) 2022de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_INST_CODE_MASK 0xff 2032de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_INST_CODE_SHIFT 0 2042de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_CFG_INST_CODE(x) (x) 2052de9b4d3SEmmanuel Vadot 2062de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_STS_REG 0x00a4 2072de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_STS_D_FSM_ERR (1 << 2) 2082de9b4d3SEmmanuel Vadot #define ZY7_QSPI_LQSPI_STS_WR_RECVD (1 << 1) 2092de9b4d3SEmmanuel Vadot 2102de9b4d3SEmmanuel Vadot #define ZY7_QSPI_MOD_ID_REG 0x00fc 2112de9b4d3SEmmanuel Vadot 2122de9b4d3SEmmanuel Vadot static int zy7_qspi_detach(device_t); 2132de9b4d3SEmmanuel Vadot 2142de9b4d3SEmmanuel Vadot /* Fill hardware fifo with command and data bytes. */ 2152de9b4d3SEmmanuel Vadot static void 2162de9b4d3SEmmanuel Vadot zy7_qspi_write_fifo(struct zy7_qspi_softc *sc, int nbytes) 2172de9b4d3SEmmanuel Vadot { 2182de9b4d3SEmmanuel Vadot int n, nvalid; 2192de9b4d3SEmmanuel Vadot uint32_t data; 2202de9b4d3SEmmanuel Vadot 2212de9b4d3SEmmanuel Vadot while (nbytes > 0) { 2222de9b4d3SEmmanuel Vadot nvalid = MIN(4, nbytes); 2232de9b4d3SEmmanuel Vadot data = 0xffffffff; 2242de9b4d3SEmmanuel Vadot 2252de9b4d3SEmmanuel Vadot /* 2262de9b4d3SEmmanuel Vadot * A hardware bug forces us to wait until the tx fifo is 2272de9b4d3SEmmanuel Vadot * empty before writing partial words. We'll come back 2282de9b4d3SEmmanuel Vadot * next tx interrupt. 2292de9b4d3SEmmanuel Vadot */ 2302de9b4d3SEmmanuel Vadot if (nvalid < 4 && (RD4(sc, ZY7_QSPI_INTR_STAT_REG) & 2312de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_TX_FIFO_NOT_FULL) == 0) 2322de9b4d3SEmmanuel Vadot return; 2332de9b4d3SEmmanuel Vadot 2342de9b4d3SEmmanuel Vadot if (sc->tx_bytes_sent < sc->cmd->tx_cmd_sz) { 2352de9b4d3SEmmanuel Vadot /* Writing command. */ 2362de9b4d3SEmmanuel Vadot n = MIN(nvalid, sc->cmd->tx_cmd_sz - 2372de9b4d3SEmmanuel Vadot sc->tx_bytes_sent); 2382de9b4d3SEmmanuel Vadot memcpy(&data, (uint8_t *)sc->cmd->tx_cmd + 2392de9b4d3SEmmanuel Vadot sc->tx_bytes_sent, n); 2402de9b4d3SEmmanuel Vadot 2412de9b4d3SEmmanuel Vadot if (nvalid > n) { 2422de9b4d3SEmmanuel Vadot /* Writing start of data. */ 2432de9b4d3SEmmanuel Vadot memcpy((uint8_t *)&data + n, 2442de9b4d3SEmmanuel Vadot sc->cmd->tx_data, nvalid - n); 2452de9b4d3SEmmanuel Vadot } 2462de9b4d3SEmmanuel Vadot } else 2472de9b4d3SEmmanuel Vadot /* Writing data. */ 2482de9b4d3SEmmanuel Vadot memcpy(&data, (uint8_t *)sc->cmd->tx_data + 2492de9b4d3SEmmanuel Vadot (sc->tx_bytes_sent - sc->cmd->tx_cmd_sz), nvalid); 2502de9b4d3SEmmanuel Vadot 2512de9b4d3SEmmanuel Vadot switch (nvalid) { 2522de9b4d3SEmmanuel Vadot case 1: 2532de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_TXD1_REG, data); 2542de9b4d3SEmmanuel Vadot break; 2552de9b4d3SEmmanuel Vadot case 2: 2562de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_TXD2_REG, data); 2572de9b4d3SEmmanuel Vadot break; 2582de9b4d3SEmmanuel Vadot case 3: 2592de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_TXD3_REG, data); 2602de9b4d3SEmmanuel Vadot break; 2612de9b4d3SEmmanuel Vadot case 4: 2622de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_TXD0_REG, data); 2632de9b4d3SEmmanuel Vadot break; 2642de9b4d3SEmmanuel Vadot } 2652de9b4d3SEmmanuel Vadot 2662de9b4d3SEmmanuel Vadot sc->tx_bytes_sent += nvalid; 2672de9b4d3SEmmanuel Vadot nbytes -= nvalid; 2682de9b4d3SEmmanuel Vadot } 2692de9b4d3SEmmanuel Vadot } 2702de9b4d3SEmmanuel Vadot 2712de9b4d3SEmmanuel Vadot /* Read hardware fifo data into command response and data buffers. */ 2722de9b4d3SEmmanuel Vadot static void 2732de9b4d3SEmmanuel Vadot zy7_qspi_read_fifo(struct zy7_qspi_softc *sc) 2742de9b4d3SEmmanuel Vadot { 2752de9b4d3SEmmanuel Vadot int n, nbytes; 2762de9b4d3SEmmanuel Vadot uint32_t data; 2772de9b4d3SEmmanuel Vadot 2782de9b4d3SEmmanuel Vadot do { 2792de9b4d3SEmmanuel Vadot data = RD4(sc, ZY7_QSPI_RX_DATA_REG); 2802de9b4d3SEmmanuel Vadot nbytes = MIN(4, sc->rx_bytes - sc->rx_bytes_rcvd); 2812de9b4d3SEmmanuel Vadot 2822de9b4d3SEmmanuel Vadot /* 2832de9b4d3SEmmanuel Vadot * Last word in non-word-multiple transfer is packed 2842de9b4d3SEmmanuel Vadot * non-intuitively. 2852de9b4d3SEmmanuel Vadot */ 2862de9b4d3SEmmanuel Vadot if (nbytes < 4) 2872de9b4d3SEmmanuel Vadot data >>= 8 * (4 - nbytes); 2882de9b4d3SEmmanuel Vadot 2892de9b4d3SEmmanuel Vadot if (sc->rx_bytes_rcvd < sc->cmd->rx_cmd_sz) { 2902de9b4d3SEmmanuel Vadot /* Reading command. */ 2912de9b4d3SEmmanuel Vadot n = MIN(nbytes, sc->cmd->rx_cmd_sz - 2922de9b4d3SEmmanuel Vadot sc->rx_bytes_rcvd); 2932de9b4d3SEmmanuel Vadot memcpy((uint8_t *)sc->cmd->rx_cmd + sc->rx_bytes_rcvd, 2942de9b4d3SEmmanuel Vadot &data, n); 2952de9b4d3SEmmanuel Vadot sc->rx_bytes_rcvd += n; 2962de9b4d3SEmmanuel Vadot nbytes -= n; 2972de9b4d3SEmmanuel Vadot data >>= 8 * n; 2982de9b4d3SEmmanuel Vadot } 2992de9b4d3SEmmanuel Vadot 3002de9b4d3SEmmanuel Vadot if (nbytes > 0) { 3012de9b4d3SEmmanuel Vadot /* Reading data. */ 3022de9b4d3SEmmanuel Vadot memcpy((uint8_t *)sc->cmd->rx_data + 3032de9b4d3SEmmanuel Vadot (sc->rx_bytes_rcvd - sc->cmd->rx_cmd_sz), 3042de9b4d3SEmmanuel Vadot &data, nbytes); 3052de9b4d3SEmmanuel Vadot sc->rx_bytes_rcvd += nbytes; 3062de9b4d3SEmmanuel Vadot } 3072de9b4d3SEmmanuel Vadot 3082de9b4d3SEmmanuel Vadot } while (sc->rx_bytes_rcvd < sc->rx_bytes && 3092de9b4d3SEmmanuel Vadot (RD4(sc, ZY7_QSPI_INTR_STAT_REG) & 3102de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_RX_FIFO_NOT_EMPTY) != 0); 3112de9b4d3SEmmanuel Vadot } 3122de9b4d3SEmmanuel Vadot 3132de9b4d3SEmmanuel Vadot /* End a transfer early by draining rx fifo and disabling interrupts. */ 3142de9b4d3SEmmanuel Vadot static void 3152de9b4d3SEmmanuel Vadot zy7_qspi_abort_transfer(struct zy7_qspi_softc *sc) 3162de9b4d3SEmmanuel Vadot { 3172de9b4d3SEmmanuel Vadot /* Drain receive fifo. */ 3182de9b4d3SEmmanuel Vadot while ((RD4(sc, ZY7_QSPI_INTR_STAT_REG) & 3192de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_RX_FIFO_NOT_EMPTY) != 0) 3202de9b4d3SEmmanuel Vadot (void)RD4(sc, ZY7_QSPI_RX_DATA_REG); 3212de9b4d3SEmmanuel Vadot 3222de9b4d3SEmmanuel Vadot /* Shut down interrupts. */ 3232de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_INTR_DIS_REG, 3242de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_RX_OVERFLOW | 3252de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_RX_FIFO_NOT_EMPTY | 3262de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_TX_FIFO_NOT_FULL); 3272de9b4d3SEmmanuel Vadot } 3282de9b4d3SEmmanuel Vadot 3292de9b4d3SEmmanuel Vadot static void 3302de9b4d3SEmmanuel Vadot zy7_qspi_intr(void *arg) 3312de9b4d3SEmmanuel Vadot { 3322de9b4d3SEmmanuel Vadot struct zy7_qspi_softc *sc = (struct zy7_qspi_softc *)arg; 3332de9b4d3SEmmanuel Vadot uint32_t istatus; 3342de9b4d3SEmmanuel Vadot 3352de9b4d3SEmmanuel Vadot QSPI_SC_LOCK(sc); 3362de9b4d3SEmmanuel Vadot 3372de9b4d3SEmmanuel Vadot sc->interrupts++; 3382de9b4d3SEmmanuel Vadot 3392de9b4d3SEmmanuel Vadot istatus = RD4(sc, ZY7_QSPI_INTR_STAT_REG); 3402de9b4d3SEmmanuel Vadot 3412de9b4d3SEmmanuel Vadot /* Stray interrupts can happen if a transfer gets interrupted. */ 3422de9b4d3SEmmanuel Vadot if (!sc->busy) { 3432de9b4d3SEmmanuel Vadot sc->stray_ints++; 3442de9b4d3SEmmanuel Vadot QSPI_SC_UNLOCK(sc); 3452de9b4d3SEmmanuel Vadot return; 3462de9b4d3SEmmanuel Vadot } 3472de9b4d3SEmmanuel Vadot 3482de9b4d3SEmmanuel Vadot if ((istatus & ZY7_QSPI_INTR_RX_OVERFLOW) != 0) { 3492de9b4d3SEmmanuel Vadot device_printf(sc->dev, "rx fifo overflow!\n"); 3502de9b4d3SEmmanuel Vadot sc->rx_overflows++; 3512de9b4d3SEmmanuel Vadot 3522de9b4d3SEmmanuel Vadot /* Clear status bit. */ 3532de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_INTR_STAT_REG, 3542de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_RX_OVERFLOW); 3552de9b4d3SEmmanuel Vadot } 3562de9b4d3SEmmanuel Vadot 3572de9b4d3SEmmanuel Vadot /* Empty receive fifo before any more transmit data is sent. */ 3582de9b4d3SEmmanuel Vadot if (sc->rx_bytes_rcvd < sc->rx_bytes && 3592de9b4d3SEmmanuel Vadot (istatus & ZY7_QSPI_INTR_RX_FIFO_NOT_EMPTY) != 0) { 3602de9b4d3SEmmanuel Vadot zy7_qspi_read_fifo(sc); 3612de9b4d3SEmmanuel Vadot if (sc->rx_bytes_rcvd == sc->rx_bytes) 3622de9b4d3SEmmanuel Vadot /* Disable receive interrupts. */ 3632de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_INTR_DIS_REG, 3642de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_RX_FIFO_NOT_EMPTY | 3652de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_RX_OVERFLOW); 3662de9b4d3SEmmanuel Vadot } 3672de9b4d3SEmmanuel Vadot 3682de9b4d3SEmmanuel Vadot /* 3692de9b4d3SEmmanuel Vadot * Transmit underflows aren't really a bug because a hardware 3702de9b4d3SEmmanuel Vadot * bug forces us to allow the tx fifo to go empty between full 3712de9b4d3SEmmanuel Vadot * and partial fifo writes. Why bother counting? 3722de9b4d3SEmmanuel Vadot */ 3732de9b4d3SEmmanuel Vadot if ((istatus & ZY7_QSPI_INTR_TX_FIFO_UNDERFLOW) != 0) { 3742de9b4d3SEmmanuel Vadot sc->tx_underflows++; 3752de9b4d3SEmmanuel Vadot 3762de9b4d3SEmmanuel Vadot /* Clear status bit. */ 3772de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_INTR_STAT_REG, 3782de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_TX_FIFO_UNDERFLOW); 3792de9b4d3SEmmanuel Vadot } 3802de9b4d3SEmmanuel Vadot 3812de9b4d3SEmmanuel Vadot /* Fill transmit fifo. */ 3822de9b4d3SEmmanuel Vadot if (sc->tx_bytes_sent < sc->tx_bytes && 3832de9b4d3SEmmanuel Vadot (istatus & ZY7_QSPI_INTR_TX_FIFO_NOT_FULL) != 0) { 3842de9b4d3SEmmanuel Vadot zy7_qspi_write_fifo(sc, MIN(240, sc->tx_bytes - 3852de9b4d3SEmmanuel Vadot sc->tx_bytes_sent)); 3862de9b4d3SEmmanuel Vadot 3872de9b4d3SEmmanuel Vadot if (sc->tx_bytes_sent == sc->tx_bytes) { 3882de9b4d3SEmmanuel Vadot /* 3892de9b4d3SEmmanuel Vadot * Disable transmit FIFO interrupt, enable receive 3902de9b4d3SEmmanuel Vadot * FIFO interrupt. 3912de9b4d3SEmmanuel Vadot */ 3922de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_INTR_DIS_REG, 3932de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_TX_FIFO_NOT_FULL); 3942de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_INTR_EN_REG, 3952de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_RX_FIFO_NOT_EMPTY); 3962de9b4d3SEmmanuel Vadot } 3972de9b4d3SEmmanuel Vadot } 3982de9b4d3SEmmanuel Vadot 3992de9b4d3SEmmanuel Vadot /* Finished with transfer? */ 4002de9b4d3SEmmanuel Vadot if (sc->tx_bytes_sent == sc->tx_bytes && 4012de9b4d3SEmmanuel Vadot sc->rx_bytes_rcvd == sc->rx_bytes) { 4022de9b4d3SEmmanuel Vadot /* De-assert CS. */ 4032de9b4d3SEmmanuel Vadot sc->cfg_reg_shadow |= ZY7_QSPI_CONFIG_PCS; 4042de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_CONFIG_REG, sc->cfg_reg_shadow); 4052de9b4d3SEmmanuel Vadot 4062de9b4d3SEmmanuel Vadot wakeup(sc->dev); 4072de9b4d3SEmmanuel Vadot } 4082de9b4d3SEmmanuel Vadot 4092de9b4d3SEmmanuel Vadot QSPI_SC_UNLOCK(sc); 4102de9b4d3SEmmanuel Vadot } 4112de9b4d3SEmmanuel Vadot 4122de9b4d3SEmmanuel Vadot /* Initialize hardware. */ 4132de9b4d3SEmmanuel Vadot static int 4142de9b4d3SEmmanuel Vadot zy7_qspi_init_hw(struct zy7_qspi_softc *sc) 4152de9b4d3SEmmanuel Vadot { 4162de9b4d3SEmmanuel Vadot uint32_t baud_div; 4172de9b4d3SEmmanuel Vadot 4182de9b4d3SEmmanuel Vadot /* Configure LQSPI Config register. Disable linear mode. */ 4192de9b4d3SEmmanuel Vadot sc->lqspi_cfg_shadow = RD4(sc, ZY7_QSPI_LQSPI_CFG_REG); 4202de9b4d3SEmmanuel Vadot sc->lqspi_cfg_shadow &= ~(ZY7_QSPI_LQSPI_CFG_LINEAR | 4212de9b4d3SEmmanuel Vadot ZY7_QSPI_LQSPI_CFG_TWO_MEM | 4222de9b4d3SEmmanuel Vadot ZY7_QSPI_LQSPI_CFG_SEP_BUS); 4232de9b4d3SEmmanuel Vadot if (sc->is_dual) { 4242de9b4d3SEmmanuel Vadot sc->lqspi_cfg_shadow |= ZY7_QSPI_LQSPI_CFG_TWO_MEM; 4252de9b4d3SEmmanuel Vadot if (sc->is_stacked) { 4262de9b4d3SEmmanuel Vadot sc->lqspi_cfg_shadow &= 4272de9b4d3SEmmanuel Vadot ~ZY7_QSPI_LQSPI_CFG_INST_CODE_MASK; 4282de9b4d3SEmmanuel Vadot sc->lqspi_cfg_shadow |= 4292de9b4d3SEmmanuel Vadot ZY7_QSPI_LQSPI_CFG_INST_CODE(sc->is_dio ? 4302de9b4d3SEmmanuel Vadot CMD_READ_DUAL_IO : CMD_READ_QUAD_OUTPUT); 4312de9b4d3SEmmanuel Vadot } else 4322de9b4d3SEmmanuel Vadot sc->lqspi_cfg_shadow |= ZY7_QSPI_LQSPI_CFG_SEP_BUS; 4332de9b4d3SEmmanuel Vadot } 4342de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_LQSPI_CFG_REG, sc->lqspi_cfg_shadow); 4352de9b4d3SEmmanuel Vadot 4362de9b4d3SEmmanuel Vadot /* Find best clock divider. */ 4372de9b4d3SEmmanuel Vadot baud_div = 0; 4382de9b4d3SEmmanuel Vadot while ((sc->ref_clock >> (baud_div + 1)) > sc->spi_clock && 4392de9b4d3SEmmanuel Vadot baud_div < 8) 4402de9b4d3SEmmanuel Vadot baud_div++; 4412de9b4d3SEmmanuel Vadot if (baud_div >= 8) { 4422de9b4d3SEmmanuel Vadot device_printf(sc->dev, "cannot configure clock divider: ref=%d" 4432de9b4d3SEmmanuel Vadot " spi=%d.\n", sc->ref_clock, sc->spi_clock); 4442de9b4d3SEmmanuel Vadot return (EINVAL); 4452de9b4d3SEmmanuel Vadot } 4462de9b4d3SEmmanuel Vadot sc->spi_clk_real_freq = sc->ref_clock >> (baud_div + 1); 4472de9b4d3SEmmanuel Vadot 4482de9b4d3SEmmanuel Vadot /* 4492de9b4d3SEmmanuel Vadot * If divider is 2 (the max speed), use internal loopback master 4502de9b4d3SEmmanuel Vadot * clock for read data. (See section 12.3.1 in ref man.) 4512de9b4d3SEmmanuel Vadot */ 4522de9b4d3SEmmanuel Vadot if (baud_div == 0) 4532de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_LPBK_DLY_ADJ_REG, 4542de9b4d3SEmmanuel Vadot ZY7_QSPI_LPBK_DLY_ADJ_USE_LPBK | 4552de9b4d3SEmmanuel Vadot ZY7_QSPI_LPBK_DLY_ADJ_DLY1(0) | 4562de9b4d3SEmmanuel Vadot ZY7_QSPI_LPBK_DLY_ADJ_DLY0(0)); 4572de9b4d3SEmmanuel Vadot else 4582de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_LPBK_DLY_ADJ_REG, 0); 4592de9b4d3SEmmanuel Vadot 4602de9b4d3SEmmanuel Vadot /* Set up configuration register. */ 4612de9b4d3SEmmanuel Vadot sc->cfg_reg_shadow = 4622de9b4d3SEmmanuel Vadot ZY7_QSPI_CONFIG_IFMODE | 4632de9b4d3SEmmanuel Vadot ZY7_QSPI_CONFIG_HOLDB_DR | 4642de9b4d3SEmmanuel Vadot ZY7_QSPI_CONFIG_RSVD1 | 4652de9b4d3SEmmanuel Vadot ZY7_QSPI_CONFIG_SSFORCE | 4662de9b4d3SEmmanuel Vadot ZY7_QSPI_CONFIG_PCS | 4672de9b4d3SEmmanuel Vadot ZY7_QSPI_CONFIG_FIFO_WIDTH32 | 4682de9b4d3SEmmanuel Vadot ZY7_QSPI_CONFIG_BAUD_RATE_DIV(baud_div) | 4692de9b4d3SEmmanuel Vadot ZY7_QSPI_CONFIG_MODE_SEL; 4702de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_CONFIG_REG, sc->cfg_reg_shadow); 4712de9b4d3SEmmanuel Vadot 4722de9b4d3SEmmanuel Vadot /* 4732de9b4d3SEmmanuel Vadot * Set thresholds. We must use 1 for tx threshold because there 4742de9b4d3SEmmanuel Vadot * is no fifo empty flag and we need one to implement a bug 4752de9b4d3SEmmanuel Vadot * workaround. 4762de9b4d3SEmmanuel Vadot */ 4772de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_TX_THRESH_REG, 1); 4782de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_RX_THRESH_REG, 1); 4792de9b4d3SEmmanuel Vadot 4802de9b4d3SEmmanuel Vadot /* Clear and disable all interrupts. */ 4812de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_INTR_STAT_REG, ~0); 4822de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_INTR_DIS_REG, ~0); 4832de9b4d3SEmmanuel Vadot 4842de9b4d3SEmmanuel Vadot /* Enable SPI. */ 4852de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_EN_REG, ZY7_SPI_ENABLE); 4862de9b4d3SEmmanuel Vadot 4872de9b4d3SEmmanuel Vadot return (0); 4882de9b4d3SEmmanuel Vadot } 4892de9b4d3SEmmanuel Vadot 4902de9b4d3SEmmanuel Vadot static void 4912de9b4d3SEmmanuel Vadot zy7_qspi_add_sysctls(device_t dev) 4922de9b4d3SEmmanuel Vadot { 4932de9b4d3SEmmanuel Vadot struct zy7_qspi_softc *sc = device_get_softc(dev); 4942de9b4d3SEmmanuel Vadot struct sysctl_ctx_list *ctx; 4952de9b4d3SEmmanuel Vadot struct sysctl_oid_list *child; 4962de9b4d3SEmmanuel Vadot 4972de9b4d3SEmmanuel Vadot ctx = device_get_sysctl_ctx(dev); 4982de9b4d3SEmmanuel Vadot child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 4992de9b4d3SEmmanuel Vadot 5002de9b4d3SEmmanuel Vadot SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "spi_clk_real_freq", CTLFLAG_RD, 5012de9b4d3SEmmanuel Vadot &sc->spi_clk_real_freq, 0, "SPI clock real frequency"); 5022de9b4d3SEmmanuel Vadot 5032de9b4d3SEmmanuel Vadot SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "rx_overflows", CTLFLAG_RD, 5042de9b4d3SEmmanuel Vadot &sc->rx_overflows, 0, "RX FIFO overflow events"); 5052de9b4d3SEmmanuel Vadot 5062de9b4d3SEmmanuel Vadot SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "tx_underflows", CTLFLAG_RD, 5072de9b4d3SEmmanuel Vadot &sc->tx_underflows, 0, "TX FIFO underflow events"); 5082de9b4d3SEmmanuel Vadot 5092de9b4d3SEmmanuel Vadot SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "interrupts", CTLFLAG_RD, 5102de9b4d3SEmmanuel Vadot &sc->interrupts, 0, "interrupt calls"); 5112de9b4d3SEmmanuel Vadot 5122de9b4d3SEmmanuel Vadot SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "stray_ints", CTLFLAG_RD, 5132de9b4d3SEmmanuel Vadot &sc->stray_ints, 0, "stray interrupts"); 5142de9b4d3SEmmanuel Vadot } 5152de9b4d3SEmmanuel Vadot 5162de9b4d3SEmmanuel Vadot static int 5172de9b4d3SEmmanuel Vadot zy7_qspi_probe(device_t dev) 5182de9b4d3SEmmanuel Vadot { 5192de9b4d3SEmmanuel Vadot 5202de9b4d3SEmmanuel Vadot if (!ofw_bus_status_okay(dev)) 5212de9b4d3SEmmanuel Vadot return (ENXIO); 5222de9b4d3SEmmanuel Vadot 5232de9b4d3SEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 5242de9b4d3SEmmanuel Vadot return (ENXIO); 5252de9b4d3SEmmanuel Vadot 5262de9b4d3SEmmanuel Vadot device_set_desc(dev, "Zynq Quad-SPI Flash Controller"); 5272de9b4d3SEmmanuel Vadot 5282de9b4d3SEmmanuel Vadot return (BUS_PROBE_DEFAULT); 5292de9b4d3SEmmanuel Vadot } 5302de9b4d3SEmmanuel Vadot 5312de9b4d3SEmmanuel Vadot static int 5322de9b4d3SEmmanuel Vadot zy7_qspi_attach(device_t dev) 5332de9b4d3SEmmanuel Vadot { 5342de9b4d3SEmmanuel Vadot struct zy7_qspi_softc *sc; 5352de9b4d3SEmmanuel Vadot int rid, err; 5362de9b4d3SEmmanuel Vadot phandle_t node; 5372de9b4d3SEmmanuel Vadot pcell_t cell; 5382de9b4d3SEmmanuel Vadot 5392de9b4d3SEmmanuel Vadot sc = device_get_softc(dev); 5402de9b4d3SEmmanuel Vadot sc->dev = dev; 5412de9b4d3SEmmanuel Vadot 5422de9b4d3SEmmanuel Vadot QSPI_SC_LOCK_INIT(sc); 5432de9b4d3SEmmanuel Vadot 5442de9b4d3SEmmanuel Vadot /* Get ref-clock, spi-clock, and other properties. */ 5452de9b4d3SEmmanuel Vadot node = ofw_bus_get_node(dev); 5462de9b4d3SEmmanuel Vadot if (OF_getprop(node, "ref-clock", &cell, sizeof(cell)) > 0) 5472de9b4d3SEmmanuel Vadot sc->ref_clock = fdt32_to_cpu(cell); 5482de9b4d3SEmmanuel Vadot else { 5492de9b4d3SEmmanuel Vadot device_printf(dev, "must have ref-clock property\n"); 5502de9b4d3SEmmanuel Vadot return (ENXIO); 5512de9b4d3SEmmanuel Vadot } 5522de9b4d3SEmmanuel Vadot if (OF_getprop(node, "spi-clock", &cell, sizeof(cell)) > 0) 5532de9b4d3SEmmanuel Vadot sc->spi_clock = fdt32_to_cpu(cell); 5542de9b4d3SEmmanuel Vadot else 5552de9b4d3SEmmanuel Vadot sc->spi_clock = ZY7_QSPI_DEFAULT_SPI_CLOCK; 5562de9b4d3SEmmanuel Vadot if (OF_getprop(node, "is-stacked", &cell, sizeof(cell)) > 0 && 5572de9b4d3SEmmanuel Vadot fdt32_to_cpu(cell) != 0) { 5582de9b4d3SEmmanuel Vadot sc->is_dual = 1; 5592de9b4d3SEmmanuel Vadot sc->is_stacked = 1; 5602de9b4d3SEmmanuel Vadot } else if (OF_getprop(node, "is-dual", &cell, sizeof(cell)) > 0 && 5612de9b4d3SEmmanuel Vadot fdt32_to_cpu(cell) != 0) 5622de9b4d3SEmmanuel Vadot sc->is_dual = 1; 5632de9b4d3SEmmanuel Vadot if (OF_getprop(node, "is-dio", &cell, sizeof(cell)) > 0 && 5642de9b4d3SEmmanuel Vadot fdt32_to_cpu(cell) != 0) 5652de9b4d3SEmmanuel Vadot sc->is_dio = 1; 5662de9b4d3SEmmanuel Vadot 5672de9b4d3SEmmanuel Vadot /* Get memory resource. */ 5682de9b4d3SEmmanuel Vadot rid = 0; 5692de9b4d3SEmmanuel Vadot sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 5702de9b4d3SEmmanuel Vadot RF_ACTIVE); 5712de9b4d3SEmmanuel Vadot if (sc->mem_res == NULL) { 5722de9b4d3SEmmanuel Vadot device_printf(dev, "could not allocate memory resources.\n"); 5732de9b4d3SEmmanuel Vadot zy7_qspi_detach(dev); 5742de9b4d3SEmmanuel Vadot return (ENOMEM); 5752de9b4d3SEmmanuel Vadot } 5762de9b4d3SEmmanuel Vadot 5772de9b4d3SEmmanuel Vadot /* Allocate IRQ. */ 5782de9b4d3SEmmanuel Vadot rid = 0; 5792de9b4d3SEmmanuel Vadot sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 5802de9b4d3SEmmanuel Vadot RF_ACTIVE); 5812de9b4d3SEmmanuel Vadot if (sc->irq_res == NULL) { 5822de9b4d3SEmmanuel Vadot device_printf(dev, "could not allocate IRQ resource.\n"); 5832de9b4d3SEmmanuel Vadot zy7_qspi_detach(dev); 5842de9b4d3SEmmanuel Vadot return (ENOMEM); 5852de9b4d3SEmmanuel Vadot } 5862de9b4d3SEmmanuel Vadot 5872de9b4d3SEmmanuel Vadot /* Activate the interrupt. */ 5882de9b4d3SEmmanuel Vadot err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE, 5892de9b4d3SEmmanuel Vadot NULL, zy7_qspi_intr, sc, &sc->intrhandle); 5902de9b4d3SEmmanuel Vadot if (err) { 5912de9b4d3SEmmanuel Vadot device_printf(dev, "could not setup IRQ.\n"); 5922de9b4d3SEmmanuel Vadot zy7_qspi_detach(dev); 5932de9b4d3SEmmanuel Vadot return (err); 5942de9b4d3SEmmanuel Vadot } 5952de9b4d3SEmmanuel Vadot 5962de9b4d3SEmmanuel Vadot /* Configure the device. */ 5972de9b4d3SEmmanuel Vadot err = zy7_qspi_init_hw(sc); 5982de9b4d3SEmmanuel Vadot if (err) { 5992de9b4d3SEmmanuel Vadot zy7_qspi_detach(dev); 6002de9b4d3SEmmanuel Vadot return (err); 6012de9b4d3SEmmanuel Vadot } 6022de9b4d3SEmmanuel Vadot 6032de9b4d3SEmmanuel Vadot sc->child = device_add_child(dev, "spibus", -1); 6042de9b4d3SEmmanuel Vadot 6052de9b4d3SEmmanuel Vadot zy7_qspi_add_sysctls(dev); 6062de9b4d3SEmmanuel Vadot 6072de9b4d3SEmmanuel Vadot /* Attach spibus driver as a child later when interrupts work. */ 6082de9b4d3SEmmanuel Vadot config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev); 6092de9b4d3SEmmanuel Vadot 6102de9b4d3SEmmanuel Vadot return (0); 6112de9b4d3SEmmanuel Vadot } 6122de9b4d3SEmmanuel Vadot 6132de9b4d3SEmmanuel Vadot static int 6142de9b4d3SEmmanuel Vadot zy7_qspi_detach(device_t dev) 6152de9b4d3SEmmanuel Vadot { 6162de9b4d3SEmmanuel Vadot struct zy7_qspi_softc *sc = device_get_softc(dev); 6172de9b4d3SEmmanuel Vadot 6182de9b4d3SEmmanuel Vadot if (device_is_attached(dev)) 6192de9b4d3SEmmanuel Vadot bus_generic_detach(dev); 6202de9b4d3SEmmanuel Vadot 6212de9b4d3SEmmanuel Vadot /* Delete child bus. */ 6222de9b4d3SEmmanuel Vadot if (sc->child) 6232de9b4d3SEmmanuel Vadot device_delete_child(dev, sc->child); 6242de9b4d3SEmmanuel Vadot 6252de9b4d3SEmmanuel Vadot /* Disable hardware. */ 6262de9b4d3SEmmanuel Vadot if (sc->mem_res != NULL) { 6272de9b4d3SEmmanuel Vadot /* Disable SPI. */ 6282de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_EN_REG, 0); 6292de9b4d3SEmmanuel Vadot 6302de9b4d3SEmmanuel Vadot /* Clear and disable all interrupts. */ 6312de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_INTR_STAT_REG, ~0); 6322de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_INTR_DIS_REG, ~0); 6332de9b4d3SEmmanuel Vadot } 6342de9b4d3SEmmanuel Vadot 6352de9b4d3SEmmanuel Vadot /* Teardown and release interrupt. */ 6362de9b4d3SEmmanuel Vadot if (sc->irq_res != NULL) { 6372de9b4d3SEmmanuel Vadot if (sc->intrhandle) 6382de9b4d3SEmmanuel Vadot bus_teardown_intr(dev, sc->irq_res, sc->intrhandle); 6392de9b4d3SEmmanuel Vadot bus_release_resource(dev, SYS_RES_IRQ, 6402de9b4d3SEmmanuel Vadot rman_get_rid(sc->irq_res), sc->irq_res); 6412de9b4d3SEmmanuel Vadot } 6422de9b4d3SEmmanuel Vadot 6432de9b4d3SEmmanuel Vadot /* Release memory resource. */ 6442de9b4d3SEmmanuel Vadot if (sc->mem_res != NULL) 6452de9b4d3SEmmanuel Vadot bus_release_resource(dev, SYS_RES_MEMORY, 6462de9b4d3SEmmanuel Vadot rman_get_rid(sc->mem_res), sc->mem_res); 6472de9b4d3SEmmanuel Vadot 6482de9b4d3SEmmanuel Vadot QSPI_SC_LOCK_DESTROY(sc); 6492de9b4d3SEmmanuel Vadot 6502de9b4d3SEmmanuel Vadot return (0); 6512de9b4d3SEmmanuel Vadot } 6522de9b4d3SEmmanuel Vadot 6532de9b4d3SEmmanuel Vadot static phandle_t 6542de9b4d3SEmmanuel Vadot zy7_qspi_get_node(device_t bus, device_t dev) 6552de9b4d3SEmmanuel Vadot { 6562de9b4d3SEmmanuel Vadot 6572de9b4d3SEmmanuel Vadot return (ofw_bus_get_node(bus)); 6582de9b4d3SEmmanuel Vadot } 6592de9b4d3SEmmanuel Vadot 6602de9b4d3SEmmanuel Vadot static int 6612de9b4d3SEmmanuel Vadot zy7_qspi_transfer(device_t dev, device_t child, struct spi_command *cmd) 6622de9b4d3SEmmanuel Vadot { 6632de9b4d3SEmmanuel Vadot struct zy7_qspi_softc *sc = device_get_softc(dev); 6642de9b4d3SEmmanuel Vadot int err = 0; 6652de9b4d3SEmmanuel Vadot 6662de9b4d3SEmmanuel Vadot KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz, 6672de9b4d3SEmmanuel Vadot ("TX/RX command sizes should be equal")); 6682de9b4d3SEmmanuel Vadot KASSERT(cmd->tx_data_sz == cmd->rx_data_sz, 6692de9b4d3SEmmanuel Vadot ("TX/RX data sizes should be equal")); 6702de9b4d3SEmmanuel Vadot 6712de9b4d3SEmmanuel Vadot if (sc->is_dual && cmd->tx_data_sz % 2 != 0) { 6722de9b4d3SEmmanuel Vadot device_printf(dev, "driver does not support odd byte data " 6732de9b4d3SEmmanuel Vadot "transfers in dual mode. (sz=%d)\n", cmd->tx_data_sz); 6742de9b4d3SEmmanuel Vadot return (EINVAL); 6752de9b4d3SEmmanuel Vadot } 6762de9b4d3SEmmanuel Vadot 6772de9b4d3SEmmanuel Vadot QSPI_SC_LOCK(sc); 6782de9b4d3SEmmanuel Vadot 6792de9b4d3SEmmanuel Vadot /* Wait for controller available. */ 6802de9b4d3SEmmanuel Vadot while (sc->busy != 0) { 6812de9b4d3SEmmanuel Vadot err = mtx_sleep(dev, &sc->sc_mtx, 0, "zqspi0", 0); 6822de9b4d3SEmmanuel Vadot if (err) { 6832de9b4d3SEmmanuel Vadot QSPI_SC_UNLOCK(sc); 6842de9b4d3SEmmanuel Vadot return (err); 6852de9b4d3SEmmanuel Vadot } 6862de9b4d3SEmmanuel Vadot } 6872de9b4d3SEmmanuel Vadot 6882de9b4d3SEmmanuel Vadot /* Start transfer. */ 6892de9b4d3SEmmanuel Vadot sc->busy = 1; 6902de9b4d3SEmmanuel Vadot sc->cmd = cmd; 6912de9b4d3SEmmanuel Vadot sc->tx_bytes = sc->cmd->tx_cmd_sz + sc->cmd->tx_data_sz; 6922de9b4d3SEmmanuel Vadot sc->tx_bytes_sent = 0; 6932de9b4d3SEmmanuel Vadot sc->rx_bytes = sc->cmd->rx_cmd_sz + sc->cmd->rx_data_sz; 6942de9b4d3SEmmanuel Vadot sc->rx_bytes_rcvd = 0; 6952de9b4d3SEmmanuel Vadot 6962de9b4d3SEmmanuel Vadot /* Enable interrupts. zy7_qspi_intr() will handle transfer. */ 6972de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_INTR_EN_REG, 6982de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_TX_FIFO_NOT_FULL | 6992de9b4d3SEmmanuel Vadot ZY7_QSPI_INTR_RX_OVERFLOW); 7002de9b4d3SEmmanuel Vadot 7012de9b4d3SEmmanuel Vadot #ifdef SPI_XFER_U_PAGE /* XXX: future support for stacked memories. */ 7022de9b4d3SEmmanuel Vadot if (sc->is_stacked) { 7032de9b4d3SEmmanuel Vadot if ((cmd->flags & SPI_XFER_U_PAGE) != 0) 7042de9b4d3SEmmanuel Vadot sc->lqspi_cfg_shadow |= ZY7_QSPI_LQSPI_CFG_U_PAGE; 7052de9b4d3SEmmanuel Vadot else 7062de9b4d3SEmmanuel Vadot sc->lqspi_cfg_shadow &= ~ZY7_QSPI_LQSPI_CFG_U_PAGE; 7072de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_LQSPI_CFG_REG, sc->lqspi_cfg_shadow); 7082de9b4d3SEmmanuel Vadot } 7092de9b4d3SEmmanuel Vadot #endif 7102de9b4d3SEmmanuel Vadot 7112de9b4d3SEmmanuel Vadot /* Assert CS. */ 7122de9b4d3SEmmanuel Vadot sc->cfg_reg_shadow &= ~ZY7_QSPI_CONFIG_PCS; 7132de9b4d3SEmmanuel Vadot WR4(sc, ZY7_QSPI_CONFIG_REG, sc->cfg_reg_shadow); 7142de9b4d3SEmmanuel Vadot 7152de9b4d3SEmmanuel Vadot /* Wait for completion. */ 7162de9b4d3SEmmanuel Vadot err = mtx_sleep(dev, &sc->sc_mtx, 0, "zqspi1", hz * 2); 7172de9b4d3SEmmanuel Vadot if (err) 7182de9b4d3SEmmanuel Vadot zy7_qspi_abort_transfer(sc); 7192de9b4d3SEmmanuel Vadot 7202de9b4d3SEmmanuel Vadot /* Release controller. */ 7212de9b4d3SEmmanuel Vadot sc->busy = 0; 7222de9b4d3SEmmanuel Vadot wakeup_one(dev); 7232de9b4d3SEmmanuel Vadot 7242de9b4d3SEmmanuel Vadot QSPI_SC_UNLOCK(sc); 7252de9b4d3SEmmanuel Vadot 7262de9b4d3SEmmanuel Vadot return (err); 7272de9b4d3SEmmanuel Vadot } 7282de9b4d3SEmmanuel Vadot 7292de9b4d3SEmmanuel Vadot static device_method_t zy7_qspi_methods[] = { 7302de9b4d3SEmmanuel Vadot /* Device interface */ 7312de9b4d3SEmmanuel Vadot DEVMETHOD(device_probe, zy7_qspi_probe), 7322de9b4d3SEmmanuel Vadot DEVMETHOD(device_attach, zy7_qspi_attach), 7332de9b4d3SEmmanuel Vadot DEVMETHOD(device_detach, zy7_qspi_detach), 7342de9b4d3SEmmanuel Vadot 7352de9b4d3SEmmanuel Vadot /* SPI interface */ 7362de9b4d3SEmmanuel Vadot DEVMETHOD(spibus_transfer, zy7_qspi_transfer), 7372de9b4d3SEmmanuel Vadot 7382de9b4d3SEmmanuel Vadot /* ofw_bus interface */ 7392de9b4d3SEmmanuel Vadot DEVMETHOD(ofw_bus_get_node, zy7_qspi_get_node), 7402de9b4d3SEmmanuel Vadot 7412de9b4d3SEmmanuel Vadot DEVMETHOD_END 7422de9b4d3SEmmanuel Vadot }; 7432de9b4d3SEmmanuel Vadot 7442de9b4d3SEmmanuel Vadot static driver_t zy7_qspi_driver = { 7452de9b4d3SEmmanuel Vadot "zy7_qspi", 7462de9b4d3SEmmanuel Vadot zy7_qspi_methods, 7472de9b4d3SEmmanuel Vadot sizeof(struct zy7_qspi_softc), 7482de9b4d3SEmmanuel Vadot }; 7492de9b4d3SEmmanuel Vadot 750*680ccae3SJohn Baldwin DRIVER_MODULE(zy7_qspi, simplebus, zy7_qspi_driver, 0, 0); 7515f31d14aSJohn Baldwin DRIVER_MODULE(ofw_spibus, zy7_qspi, ofw_spibus_driver, 0, 0); 7522de9b4d3SEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data); 7532de9b4d3SEmmanuel Vadot MODULE_DEPEND(zy7_qspi, ofw_spibus, 1, 1, 1); 754