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