Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Xilinx SPI driver |
| 4 | * |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 5 | * Supports 8 bit SPI transfers only, with or w/o FIFO |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 6 | * |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 7 | * Based on bfin_spi.c, by way of altera_spi.c |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 8 | * Copyright (c) 2015 Jagan Teki <jteki@openedev.com> |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 9 | * Copyright (c) 2012 Stephan Linz <linz@li-pro.net> |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 10 | * Copyright (c) 2010 Graeme Smecher <graeme.smecher@mail.mcgill.ca> |
| 11 | * Copyright (c) 2010 Thomas Chou <thomas@wytron.com.tw> |
| 12 | * Copyright (c) 2005-2008 Analog Devices Inc. |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 13 | */ |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 14 | |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 15 | #include <config.h> |
| 16 | #include <common.h> |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 17 | #include <dm.h> |
| 18 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 19 | #include <log.h> |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 20 | #include <malloc.h> |
| 21 | #include <spi.h> |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 22 | #include <spi-mem.h> |
Jagan Teki | 41fcbba | 2015-06-27 00:51:37 +0530 | [diff] [blame] | 23 | #include <asm/io.h> |
Vipul Kumar | 90098ba | 2018-06-30 08:15:18 +0530 | [diff] [blame] | 24 | #include <wait_bit.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 25 | #include <linux/bitops.h> |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 26 | |
Jagan Teki | 23e281d | 2015-06-27 00:51:26 +0530 | [diff] [blame] | 27 | /* |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 28 | * [0]: http://www.xilinx.com/support/documentation |
Jagan Teki | 23e281d | 2015-06-27 00:51:26 +0530 | [diff] [blame] | 29 | * |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 30 | * Xilinx SPI Register Definitions |
Jagan Teki | 23e281d | 2015-06-27 00:51:26 +0530 | [diff] [blame] | 31 | * [1]: [0]/ip_documentation/xps_spi.pdf |
| 32 | * page 8, Register Descriptions |
| 33 | * [2]: [0]/ip_documentation/axi_spi_ds742.pdf |
| 34 | * page 7, Register Overview Table |
| 35 | */ |
Jagan Teki | 23e281d | 2015-06-27 00:51:26 +0530 | [diff] [blame] | 36 | |
| 37 | /* SPI Control Register (spicr), [1] p9, [2] p8 */ |
Jagan Teki | f0a0141 | 2015-10-23 01:39:31 +0530 | [diff] [blame] | 38 | #define SPICR_LSB_FIRST BIT(9) |
| 39 | #define SPICR_MASTER_INHIBIT BIT(8) |
| 40 | #define SPICR_MANUAL_SS BIT(7) |
| 41 | #define SPICR_RXFIFO_RESEST BIT(6) |
| 42 | #define SPICR_TXFIFO_RESEST BIT(5) |
| 43 | #define SPICR_CPHA BIT(4) |
| 44 | #define SPICR_CPOL BIT(3) |
| 45 | #define SPICR_MASTER_MODE BIT(2) |
| 46 | #define SPICR_SPE BIT(1) |
| 47 | #define SPICR_LOOP BIT(0) |
Jagan Teki | 23e281d | 2015-06-27 00:51:26 +0530 | [diff] [blame] | 48 | |
| 49 | /* SPI Status Register (spisr), [1] p11, [2] p10 */ |
Jagan Teki | f0a0141 | 2015-10-23 01:39:31 +0530 | [diff] [blame] | 50 | #define SPISR_SLAVE_MODE_SELECT BIT(5) |
| 51 | #define SPISR_MODF BIT(4) |
| 52 | #define SPISR_TX_FULL BIT(3) |
| 53 | #define SPISR_TX_EMPTY BIT(2) |
| 54 | #define SPISR_RX_FULL BIT(1) |
| 55 | #define SPISR_RX_EMPTY BIT(0) |
Jagan Teki | 23e281d | 2015-06-27 00:51:26 +0530 | [diff] [blame] | 56 | |
| 57 | /* SPI Data Transmit Register (spidtr), [1] p12, [2] p12 */ |
Jagan Teki | a2888d0 | 2015-10-23 01:03:44 +0530 | [diff] [blame] | 58 | #define SPIDTR_8BIT_MASK GENMASK(7, 0) |
| 59 | #define SPIDTR_16BIT_MASK GENMASK(15, 0) |
| 60 | #define SPIDTR_32BIT_MASK GENMASK(31, 0) |
Jagan Teki | 23e281d | 2015-06-27 00:51:26 +0530 | [diff] [blame] | 61 | |
| 62 | /* SPI Data Receive Register (spidrr), [1] p12, [2] p12 */ |
Jagan Teki | a2888d0 | 2015-10-23 01:03:44 +0530 | [diff] [blame] | 63 | #define SPIDRR_8BIT_MASK GENMASK(7, 0) |
| 64 | #define SPIDRR_16BIT_MASK GENMASK(15, 0) |
| 65 | #define SPIDRR_32BIT_MASK GENMASK(31, 0) |
Jagan Teki | 23e281d | 2015-06-27 00:51:26 +0530 | [diff] [blame] | 66 | |
| 67 | /* SPI Slave Select Register (spissr), [1] p13, [2] p13 */ |
| 68 | #define SPISSR_MASK(cs) (1 << (cs)) |
| 69 | #define SPISSR_ACT(cs) ~SPISSR_MASK(cs) |
| 70 | #define SPISSR_OFF ~0UL |
| 71 | |
Jagan Teki | 23e281d | 2015-06-27 00:51:26 +0530 | [diff] [blame] | 72 | /* SPI Software Reset Register (ssr) */ |
| 73 | #define SPISSR_RESET_VALUE 0x0a |
| 74 | |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 75 | #define XILSPI_MAX_XFER_BITS 8 |
| 76 | #define XILSPI_SPICR_DFLT_ON (SPICR_MANUAL_SS | SPICR_MASTER_MODE | \ |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 77 | SPICR_SPE | SPICR_MASTER_INHIBIT) |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 78 | #define XILSPI_SPICR_DFLT_OFF (SPICR_MASTER_INHIBIT | SPICR_MANUAL_SS) |
| 79 | |
Ashok Reddy Soma | caecfe6 | 2020-05-18 01:11:00 -0600 | [diff] [blame] | 80 | #define XILINX_SPI_IDLE_VAL GENMASK(7, 0) |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 81 | |
Vipul Kumar | 90098ba | 2018-06-30 08:15:18 +0530 | [diff] [blame] | 82 | #define XILINX_SPISR_TIMEOUT 10000 /* in milliseconds */ |
| 83 | |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 84 | /* xilinx spi register set */ |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 85 | struct xilinx_spi_regs { |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 86 | u32 __space0__[7]; |
| 87 | u32 dgier; /* Device Global Interrupt Enable Register (DGIER) */ |
| 88 | u32 ipisr; /* IP Interrupt Status Register (IPISR) */ |
| 89 | u32 __space1__; |
| 90 | u32 ipier; /* IP Interrupt Enable Register (IPIER) */ |
| 91 | u32 __space2__[5]; |
| 92 | u32 srr; /* Softare Reset Register (SRR) */ |
| 93 | u32 __space3__[7]; |
| 94 | u32 spicr; /* SPI Control Register (SPICR) */ |
| 95 | u32 spisr; /* SPI Status Register (SPISR) */ |
| 96 | u32 spidtr; /* SPI Data Transmit Register (SPIDTR) */ |
| 97 | u32 spidrr; /* SPI Data Receive Register (SPIDRR) */ |
| 98 | u32 spissr; /* SPI Slave Select Register (SPISSR) */ |
| 99 | u32 spitfor; /* SPI Transmit FIFO Occupancy Register (SPITFOR) */ |
| 100 | u32 spirfor; /* SPI Receive FIFO Occupancy Register (SPIRFOR) */ |
| 101 | }; |
| 102 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 103 | /* xilinx spi priv */ |
| 104 | struct xilinx_spi_priv { |
| 105 | struct xilinx_spi_regs *regs; |
Jagan Teki | 23e281d | 2015-06-27 00:51:26 +0530 | [diff] [blame] | 106 | unsigned int freq; |
| 107 | unsigned int mode; |
Vipul Kumar | 90098ba | 2018-06-30 08:15:18 +0530 | [diff] [blame] | 108 | unsigned int fifo_depth; |
Vipul Kumar | c7e2aae | 2018-06-30 08:15:19 +0530 | [diff] [blame] | 109 | u8 startup; |
Jagan Teki | 23e281d | 2015-06-27 00:51:26 +0530 | [diff] [blame] | 110 | }; |
| 111 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 112 | static int xilinx_spi_probe(struct udevice *bus) |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 113 | { |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 114 | struct xilinx_spi_priv *priv = dev_get_priv(bus); |
| 115 | struct xilinx_spi_regs *regs = priv->regs; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 116 | |
Vipul Kumar | 646b460 | 2018-06-30 08:15:20 +0530 | [diff] [blame] | 117 | priv->regs = (struct xilinx_spi_regs *)dev_read_addr(bus); |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 118 | |
Vipul Kumar | 646b460 | 2018-06-30 08:15:20 +0530 | [diff] [blame] | 119 | priv->fifo_depth = dev_read_u32_default(bus, "fifo-size", 0); |
Vipul Kumar | 90098ba | 2018-06-30 08:15:18 +0530 | [diff] [blame] | 120 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 121 | writel(SPISSR_RESET_VALUE, ®s->srr); |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 122 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 123 | /* |
| 124 | * Reset RX & TX FIFO |
| 125 | * Enable Manual Slave Select Assertion, |
| 126 | * Set SPI controller into master mode, and enable it |
| 127 | */ |
| 128 | writel(SPICR_RXFIFO_RESEST | SPICR_TXFIFO_RESEST | |
| 129 | SPICR_MANUAL_SS | SPICR_MASTER_MODE | SPICR_SPE, |
| 130 | ®s->spicr); |
| 131 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 132 | return 0; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 135 | static void spi_cs_activate(struct udevice *dev, uint cs) |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 136 | { |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 137 | struct udevice *bus = dev_get_parent(dev); |
| 138 | struct xilinx_spi_priv *priv = dev_get_priv(bus); |
| 139 | struct xilinx_spi_regs *regs = priv->regs; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 140 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 141 | writel(SPISSR_ACT(cs), ®s->spissr); |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 144 | static void spi_cs_deactivate(struct udevice *dev) |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 145 | { |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 146 | struct udevice *bus = dev_get_parent(dev); |
| 147 | struct xilinx_spi_priv *priv = dev_get_priv(bus); |
| 148 | struct xilinx_spi_regs *regs = priv->regs; |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 149 | u32 reg; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 150 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 151 | reg = readl(®s->spicr) | SPICR_RXFIFO_RESEST | SPICR_TXFIFO_RESEST; |
| 152 | writel(reg, ®s->spicr); |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 153 | writel(SPISSR_OFF, ®s->spissr); |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 156 | static int xilinx_spi_claim_bus(struct udevice *dev) |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 157 | { |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 158 | struct udevice *bus = dev_get_parent(dev); |
| 159 | struct xilinx_spi_priv *priv = dev_get_priv(bus); |
| 160 | struct xilinx_spi_regs *regs = priv->regs; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 161 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 162 | writel(SPISSR_OFF, ®s->spissr); |
| 163 | writel(XILSPI_SPICR_DFLT_ON, ®s->spicr); |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 164 | |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 168 | static int xilinx_spi_release_bus(struct udevice *dev) |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 169 | { |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 170 | struct udevice *bus = dev_get_parent(dev); |
| 171 | struct xilinx_spi_priv *priv = dev_get_priv(bus); |
| 172 | struct xilinx_spi_regs *regs = priv->regs; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 173 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 174 | writel(SPISSR_OFF, ®s->spissr); |
| 175 | writel(XILSPI_SPICR_DFLT_OFF, ®s->spicr); |
| 176 | |
| 177 | return 0; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Vipul Kumar | 90098ba | 2018-06-30 08:15:18 +0530 | [diff] [blame] | 180 | static u32 xilinx_spi_fill_txfifo(struct udevice *bus, const u8 *txp, |
| 181 | u32 txbytes) |
| 182 | { |
| 183 | struct xilinx_spi_priv *priv = dev_get_priv(bus); |
| 184 | struct xilinx_spi_regs *regs = priv->regs; |
| 185 | unsigned char d; |
| 186 | u32 i = 0; |
| 187 | |
| 188 | while (txbytes && !(readl(®s->spisr) & SPISR_TX_FULL) && |
| 189 | i < priv->fifo_depth) { |
Ashok Reddy Soma | caecfe6 | 2020-05-18 01:11:00 -0600 | [diff] [blame] | 190 | d = txp ? *txp++ : XILINX_SPI_IDLE_VAL; |
Vipul Kumar | 90098ba | 2018-06-30 08:15:18 +0530 | [diff] [blame] | 191 | debug("spi_xfer: tx:%x ", d); |
| 192 | /* write out and wait for processing (receive data) */ |
| 193 | writel(d & SPIDTR_8BIT_MASK, ®s->spidtr); |
| 194 | txbytes--; |
| 195 | i++; |
| 196 | } |
| 197 | |
| 198 | return i; |
| 199 | } |
| 200 | |
| 201 | static u32 xilinx_spi_read_rxfifo(struct udevice *bus, u8 *rxp, u32 rxbytes) |
| 202 | { |
| 203 | struct xilinx_spi_priv *priv = dev_get_priv(bus); |
| 204 | struct xilinx_spi_regs *regs = priv->regs; |
| 205 | unsigned char d; |
| 206 | unsigned int i = 0; |
| 207 | |
| 208 | while (rxbytes && !(readl(®s->spisr) & SPISR_RX_EMPTY)) { |
| 209 | d = readl(®s->spidrr) & SPIDRR_8BIT_MASK; |
| 210 | if (rxp) |
| 211 | *rxp++ = d; |
| 212 | debug("spi_xfer: rx:%x\n", d); |
| 213 | rxbytes--; |
| 214 | i++; |
| 215 | } |
| 216 | debug("Rx_done\n"); |
| 217 | |
| 218 | return i; |
| 219 | } |
| 220 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 221 | static int start_transfer(struct spi_slave *spi, const void *dout, void *din, u32 len) |
Vipul Kumar | c7e2aae | 2018-06-30 08:15:19 +0530 | [diff] [blame] | 222 | { |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 223 | struct udevice *bus = spi->dev->parent; |
Vipul Kumar | c7e2aae | 2018-06-30 08:15:19 +0530 | [diff] [blame] | 224 | struct xilinx_spi_priv *priv = dev_get_priv(bus); |
| 225 | struct xilinx_spi_regs *regs = priv->regs; |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 226 | u32 count, txbytes, rxbytes; |
| 227 | int reg, ret; |
| 228 | const unsigned char *txp = (const unsigned char *)dout; |
| 229 | unsigned char *rxp = (unsigned char *)din; |
Vipul Kumar | c7e2aae | 2018-06-30 08:15:19 +0530 | [diff] [blame] | 230 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 231 | txbytes = len; |
| 232 | rxbytes = len; |
| 233 | while (txbytes || rxbytes) { |
| 234 | /* Disable master transaction */ |
| 235 | reg = readl(®s->spicr) | SPICR_MASTER_INHIBIT; |
| 236 | writel(reg, ®s->spicr); |
| 237 | count = xilinx_spi_fill_txfifo(bus, txp, txbytes); |
| 238 | /* Enable master transaction */ |
Vipul Kumar | c7e2aae | 2018-06-30 08:15:19 +0530 | [diff] [blame] | 239 | reg = readl(®s->spicr) & ~SPICR_MASTER_INHIBIT; |
| 240 | writel(reg, ®s->spicr); |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 241 | txbytes -= count; |
| 242 | if (txp) |
| 243 | txp += count; |
Vipul Kumar | c7e2aae | 2018-06-30 08:15:19 +0530 | [diff] [blame] | 244 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 245 | ret = wait_for_bit_le32(®s->spisr, SPISR_TX_EMPTY, true, |
| 246 | XILINX_SPISR_TIMEOUT, false); |
| 247 | if (ret < 0) { |
| 248 | printf("XILSPI error: Xfer timeout\n"); |
| 249 | return ret; |
Vipul Kumar | c7e2aae | 2018-06-30 08:15:19 +0530 | [diff] [blame] | 250 | } |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 251 | |
| 252 | reg = readl(®s->spicr) | SPICR_MASTER_INHIBIT; |
| 253 | writel(reg, ®s->spicr); |
| 254 | count = xilinx_spi_read_rxfifo(bus, rxp, rxbytes); |
| 255 | rxbytes -= count; |
| 256 | if (rxp) |
| 257 | rxp += count; |
Vipul Kumar | c7e2aae | 2018-06-30 08:15:19 +0530 | [diff] [blame] | 258 | } |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 259 | |
| 260 | return 0; |
Vipul Kumar | c7e2aae | 2018-06-30 08:15:19 +0530 | [diff] [blame] | 261 | } |
| 262 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 263 | static void xilinx_spi_startup_block(struct spi_slave *spi) |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 264 | { |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 265 | struct dm_spi_slave_plat *slave_plat = |
| 266 | dev_get_parent_plat(spi->dev); |
| 267 | unsigned char txp; |
| 268 | unsigned char rxp[8]; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 269 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 270 | /* |
| 271 | * Perform a dummy read as a work around for |
| 272 | * the startup block issue. |
| 273 | */ |
| 274 | spi_cs_activate(spi->dev, slave_plat->cs); |
| 275 | txp = 0x9f; |
| 276 | start_transfer(spi, (void *)&txp, NULL, 1); |
Jagan Teki | 48a0dbd | 2015-06-27 00:51:27 +0530 | [diff] [blame] | 277 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 278 | start_transfer(spi, NULL, (void *)rxp, 6); |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 279 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 280 | spi_cs_deactivate(spi->dev); |
| 281 | } |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 282 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 283 | static int xilinx_spi_mem_exec_op(struct spi_slave *spi, |
| 284 | const struct spi_mem_op *op) |
| 285 | { |
| 286 | struct dm_spi_slave_plat *slave_plat = |
| 287 | dev_get_parent_plat(spi->dev); |
| 288 | static u32 startup; |
| 289 | u32 dummy_len, ret; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 290 | |
Vipul Kumar | c7e2aae | 2018-06-30 08:15:19 +0530 | [diff] [blame] | 291 | /* |
| 292 | * This is the work around for the startup block issue in |
| 293 | * the spi controller. SPI clock is passing through STARTUP |
| 294 | * block to FLASH. STARTUP block don't provide clock as soon |
| 295 | * as QSPI provides command. So first command fails. |
| 296 | */ |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 297 | if (!startup) { |
| 298 | xilinx_spi_startup_block(spi); |
| 299 | startup++; |
| 300 | } |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 301 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 302 | spi_cs_activate(spi->dev, slave_plat->cs); |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 303 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 304 | if (op->cmd.opcode) { |
| 305 | ret = start_transfer(spi, (void *)&op->cmd.opcode, NULL, 1); |
| 306 | if (ret) |
| 307 | goto done; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 308 | } |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 309 | if (op->addr.nbytes) { |
| 310 | int i; |
| 311 | u8 addr_buf[4]; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 312 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 313 | for (i = 0; i < op->addr.nbytes; i++) |
| 314 | addr_buf[i] = op->addr.val >> |
| 315 | (8 * (op->addr.nbytes - i - 1)); |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 316 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 317 | ret = start_transfer(spi, (void *)addr_buf, NULL, |
| 318 | op->addr.nbytes); |
| 319 | if (ret) |
| 320 | goto done; |
| 321 | } |
| 322 | if (op->dummy.nbytes) { |
T Karthik Reddy | ea0bd08 | 2022-07-16 12:28:47 +0530 | [diff] [blame] | 323 | dummy_len = (op->dummy.nbytes * op->data.buswidth) / |
| 324 | op->dummy.buswidth; |
| 325 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 326 | ret = start_transfer(spi, NULL, NULL, dummy_len); |
| 327 | if (ret) |
| 328 | goto done; |
| 329 | } |
| 330 | if (op->data.nbytes) { |
| 331 | if (op->data.dir == SPI_MEM_DATA_IN) { |
| 332 | ret = start_transfer(spi, NULL, |
| 333 | op->data.buf.in, op->data.nbytes); |
| 334 | } else { |
| 335 | ret = start_transfer(spi, op->data.buf.out, |
| 336 | NULL, op->data.nbytes); |
| 337 | } |
| 338 | if (ret) |
| 339 | goto done; |
| 340 | } |
| 341 | done: |
| 342 | spi_cs_deactivate(spi->dev); |
| 343 | |
| 344 | return ret; |
Stephan Linz | fc77d51 | 2012-07-29 00:25:35 +0200 | [diff] [blame] | 345 | } |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 346 | |
T Karthik Reddy | ea0bd08 | 2022-07-16 12:28:47 +0530 | [diff] [blame] | 347 | static int xilinx_qspi_check_buswidth(struct spi_slave *slave, u8 width) |
| 348 | { |
| 349 | u32 mode = slave->mode; |
| 350 | |
| 351 | switch (width) { |
| 352 | case 1: |
| 353 | return 0; |
| 354 | case 2: |
| 355 | if (mode & SPI_RX_DUAL) |
| 356 | return 0; |
| 357 | break; |
| 358 | case 4: |
| 359 | if (mode & SPI_RX_QUAD) |
| 360 | return 0; |
| 361 | break; |
| 362 | } |
| 363 | |
| 364 | return -EOPNOTSUPP; |
| 365 | } |
| 366 | |
| 367 | bool xilinx_qspi_mem_exec_op(struct spi_slave *slave, |
| 368 | const struct spi_mem_op *op) |
| 369 | { |
| 370 | if (xilinx_qspi_check_buswidth(slave, op->cmd.buswidth)) |
| 371 | return false; |
| 372 | |
| 373 | if (op->addr.nbytes && |
| 374 | xilinx_qspi_check_buswidth(slave, op->addr.buswidth)) |
| 375 | return false; |
| 376 | |
| 377 | if (op->dummy.nbytes && |
| 378 | xilinx_qspi_check_buswidth(slave, op->dummy.buswidth)) |
| 379 | return false; |
| 380 | |
| 381 | if (op->data.dir != SPI_MEM_NO_DATA && |
| 382 | xilinx_qspi_check_buswidth(slave, op->data.buswidth)) |
| 383 | return false; |
| 384 | |
| 385 | return true; |
| 386 | } |
| 387 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 388 | static int xilinx_spi_set_speed(struct udevice *bus, uint speed) |
| 389 | { |
| 390 | struct xilinx_spi_priv *priv = dev_get_priv(bus); |
| 391 | |
| 392 | priv->freq = speed; |
| 393 | |
T Karthik Reddy | c17b307 | 2021-03-17 01:01:50 -0600 | [diff] [blame] | 394 | debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq); |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static int xilinx_spi_set_mode(struct udevice *bus, uint mode) |
| 400 | { |
| 401 | struct xilinx_spi_priv *priv = dev_get_priv(bus); |
| 402 | struct xilinx_spi_regs *regs = priv->regs; |
T Karthik Reddy | c17b307 | 2021-03-17 01:01:50 -0600 | [diff] [blame] | 403 | u32 spicr; |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 404 | |
| 405 | spicr = readl(®s->spicr); |
Jagan Teki | 0dc543f | 2015-09-08 01:26:29 +0530 | [diff] [blame] | 406 | if (mode & SPI_LSB_FIRST) |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 407 | spicr |= SPICR_LSB_FIRST; |
Jagan Teki | 0dc543f | 2015-09-08 01:26:29 +0530 | [diff] [blame] | 408 | if (mode & SPI_CPHA) |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 409 | spicr |= SPICR_CPHA; |
Jagan Teki | 0dc543f | 2015-09-08 01:26:29 +0530 | [diff] [blame] | 410 | if (mode & SPI_CPOL) |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 411 | spicr |= SPICR_CPOL; |
Jagan Teki | 0dc543f | 2015-09-08 01:26:29 +0530 | [diff] [blame] | 412 | if (mode & SPI_LOOP) |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 413 | spicr |= SPICR_LOOP; |
| 414 | |
| 415 | writel(spicr, ®s->spicr); |
| 416 | priv->mode = mode; |
| 417 | |
T Karthik Reddy | c17b307 | 2021-03-17 01:01:50 -0600 | [diff] [blame] | 418 | debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode); |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 423 | static const struct spi_controller_mem_ops xilinx_spi_mem_ops = { |
| 424 | .exec_op = xilinx_spi_mem_exec_op, |
T Karthik Reddy | ea0bd08 | 2022-07-16 12:28:47 +0530 | [diff] [blame] | 425 | .supports_op = xilinx_qspi_mem_exec_op, |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 426 | }; |
| 427 | |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 428 | static const struct dm_spi_ops xilinx_spi_ops = { |
| 429 | .claim_bus = xilinx_spi_claim_bus, |
| 430 | .release_bus = xilinx_spi_release_bus, |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 431 | .set_speed = xilinx_spi_set_speed, |
| 432 | .set_mode = xilinx_spi_set_mode, |
T Karthik Reddy | 5096380 | 2022-07-16 12:28:46 +0530 | [diff] [blame] | 433 | .mem_ops = &xilinx_spi_mem_ops, |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 434 | }; |
| 435 | |
| 436 | static const struct udevice_id xilinx_spi_ids[] = { |
Michal Simek | 7465f31 | 2015-12-11 12:41:14 +0100 | [diff] [blame] | 437 | { .compatible = "xlnx,xps-spi-2.00.a" }, |
| 438 | { .compatible = "xlnx,xps-spi-2.00.b" }, |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 439 | { } |
| 440 | }; |
| 441 | |
| 442 | U_BOOT_DRIVER(xilinx_spi) = { |
| 443 | .name = "xilinx_spi", |
| 444 | .id = UCLASS_SPI, |
| 445 | .of_match = xilinx_spi_ids, |
| 446 | .ops = &xilinx_spi_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 447 | .priv_auto = sizeof(struct xilinx_spi_priv), |
Jagan Teki | fdc2b3d | 2015-06-29 13:15:18 +0530 | [diff] [blame] | 448 | .probe = xilinx_spi_probe, |
| 449 | }; |