Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2019, STMicroelectronics - All Rights Reserved |
| 4 | * |
| 5 | * Driver for STMicroelectronics Serial peripheral interface (SPI) |
| 6 | */ |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 7 | |
| 8 | #define LOG_CATEGORY UCLASS_SPI |
| 9 | |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 10 | #include <clk.h> |
| 11 | #include <dm.h> |
| 12 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 15 | #include <reset.h> |
| 16 | #include <spi.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 19 | #include <linux/delay.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 20 | #include <linux/printk.h> |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 21 | |
| 22 | #include <asm/io.h> |
| 23 | #include <asm/gpio.h> |
| 24 | #include <linux/bitfield.h> |
| 25 | #include <linux/iopoll.h> |
| 26 | |
| 27 | /* STM32 SPI registers */ |
| 28 | #define STM32_SPI_CR1 0x00 |
| 29 | #define STM32_SPI_CR2 0x04 |
| 30 | #define STM32_SPI_CFG1 0x08 |
| 31 | #define STM32_SPI_CFG2 0x0C |
| 32 | #define STM32_SPI_SR 0x14 |
| 33 | #define STM32_SPI_IFCR 0x18 |
| 34 | #define STM32_SPI_TXDR 0x20 |
| 35 | #define STM32_SPI_RXDR 0x30 |
| 36 | #define STM32_SPI_I2SCFGR 0x50 |
| 37 | |
| 38 | /* STM32_SPI_CR1 bit fields */ |
| 39 | #define SPI_CR1_SPE BIT(0) |
| 40 | #define SPI_CR1_MASRX BIT(8) |
| 41 | #define SPI_CR1_CSTART BIT(9) |
| 42 | #define SPI_CR1_CSUSP BIT(10) |
| 43 | #define SPI_CR1_HDDIR BIT(11) |
| 44 | #define SPI_CR1_SSI BIT(12) |
| 45 | |
| 46 | /* STM32_SPI_CR2 bit fields */ |
| 47 | #define SPI_CR2_TSIZE GENMASK(15, 0) |
| 48 | |
| 49 | /* STM32_SPI_CFG1 bit fields */ |
| 50 | #define SPI_CFG1_DSIZE GENMASK(4, 0) |
| 51 | #define SPI_CFG1_DSIZE_MIN 3 |
| 52 | #define SPI_CFG1_FTHLV_SHIFT 5 |
| 53 | #define SPI_CFG1_FTHLV GENMASK(8, 5) |
| 54 | #define SPI_CFG1_MBR_SHIFT 28 |
| 55 | #define SPI_CFG1_MBR GENMASK(30, 28) |
| 56 | #define SPI_CFG1_MBR_MIN 0 |
| 57 | #define SPI_CFG1_MBR_MAX FIELD_GET(SPI_CFG1_MBR, SPI_CFG1_MBR) |
| 58 | |
| 59 | /* STM32_SPI_CFG2 bit fields */ |
| 60 | #define SPI_CFG2_COMM_SHIFT 17 |
| 61 | #define SPI_CFG2_COMM GENMASK(18, 17) |
| 62 | #define SPI_CFG2_MASTER BIT(22) |
| 63 | #define SPI_CFG2_LSBFRST BIT(23) |
| 64 | #define SPI_CFG2_CPHA BIT(24) |
| 65 | #define SPI_CFG2_CPOL BIT(25) |
| 66 | #define SPI_CFG2_SSM BIT(26) |
| 67 | #define SPI_CFG2_AFCNTR BIT(31) |
| 68 | |
| 69 | /* STM32_SPI_SR bit fields */ |
| 70 | #define SPI_SR_RXP BIT(0) |
| 71 | #define SPI_SR_TXP BIT(1) |
| 72 | #define SPI_SR_EOT BIT(3) |
| 73 | #define SPI_SR_TXTF BIT(4) |
| 74 | #define SPI_SR_OVR BIT(6) |
| 75 | #define SPI_SR_SUSP BIT(11) |
| 76 | #define SPI_SR_RXPLVL_SHIFT 13 |
| 77 | #define SPI_SR_RXPLVL GENMASK(14, 13) |
| 78 | #define SPI_SR_RXWNE BIT(15) |
| 79 | |
| 80 | /* STM32_SPI_IFCR bit fields */ |
| 81 | #define SPI_IFCR_ALL GENMASK(11, 3) |
| 82 | |
| 83 | /* STM32_SPI_I2SCFGR bit fields */ |
| 84 | #define SPI_I2SCFGR_I2SMOD BIT(0) |
| 85 | |
| 86 | #define MAX_CS_COUNT 4 |
| 87 | |
| 88 | /* SPI Master Baud Rate min/max divisor */ |
| 89 | #define STM32_MBR_DIV_MIN (2 << SPI_CFG1_MBR_MIN) |
| 90 | #define STM32_MBR_DIV_MAX (2 << SPI_CFG1_MBR_MAX) |
| 91 | |
| 92 | #define STM32_SPI_TIMEOUT_US 100000 |
| 93 | |
| 94 | /* SPI Communication mode */ |
| 95 | #define SPI_FULL_DUPLEX 0 |
| 96 | #define SPI_SIMPLEX_TX 1 |
| 97 | #define SPI_SIMPLEX_RX 2 |
| 98 | #define SPI_HALF_DUPLEX 3 |
| 99 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 100 | struct stm32_spi_plat { |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 101 | void __iomem *base; |
| 102 | struct clk clk; |
| 103 | struct reset_ctl rst_ctl; |
| 104 | struct gpio_desc cs_gpios[MAX_CS_COUNT]; |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | struct stm32_spi_priv { |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 108 | ulong bus_clk_rate; |
| 109 | unsigned int fifo_size; |
| 110 | unsigned int cur_bpw; |
| 111 | unsigned int cur_hz; |
| 112 | unsigned int cur_xferlen; /* current transfer length in bytes */ |
Patrick Delaunay | 5043c4b | 2019-06-21 15:26:58 +0200 | [diff] [blame] | 113 | unsigned int tx_len; /* number of data to be written in bytes */ |
| 114 | unsigned int rx_len; /* number of data to be read in bytes */ |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 115 | const void *tx_buf; /* data to be written, or NULL */ |
| 116 | void *rx_buf; /* data to be read, or NULL */ |
| 117 | u32 cur_mode; |
| 118 | bool cs_high; |
| 119 | }; |
| 120 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 121 | static void stm32_spi_write_txfifo(struct udevice *bus) |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 122 | { |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 123 | struct stm32_spi_priv *priv = dev_get_priv(bus); |
| 124 | struct stm32_spi_plat *plat = dev_get_plat(bus); |
| 125 | void __iomem *base = plat->base; |
| 126 | |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 127 | while ((priv->tx_len > 0) && |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 128 | (readl(base + STM32_SPI_SR) & SPI_SR_TXP)) { |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 129 | u32 offs = priv->cur_xferlen - priv->tx_len; |
| 130 | |
| 131 | if (priv->tx_len >= sizeof(u32) && |
| 132 | IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u32))) { |
| 133 | const u32 *tx_buf32 = (const u32 *)(priv->tx_buf + offs); |
| 134 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 135 | writel(*tx_buf32, base + STM32_SPI_TXDR); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 136 | priv->tx_len -= sizeof(u32); |
| 137 | } else if (priv->tx_len >= sizeof(u16) && |
| 138 | IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u16))) { |
| 139 | const u16 *tx_buf16 = (const u16 *)(priv->tx_buf + offs); |
| 140 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 141 | writew(*tx_buf16, base + STM32_SPI_TXDR); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 142 | priv->tx_len -= sizeof(u16); |
| 143 | } else { |
| 144 | const u8 *tx_buf8 = (const u8 *)(priv->tx_buf + offs); |
| 145 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 146 | writeb(*tx_buf8, base + STM32_SPI_TXDR); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 147 | priv->tx_len -= sizeof(u8); |
| 148 | } |
| 149 | } |
| 150 | |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 151 | log_debug("%d bytes left\n", priv->tx_len); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 154 | static void stm32_spi_read_rxfifo(struct udevice *bus) |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 155 | { |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 156 | struct stm32_spi_priv *priv = dev_get_priv(bus); |
| 157 | struct stm32_spi_plat *plat = dev_get_plat(bus); |
| 158 | void __iomem *base = plat->base; |
| 159 | u32 sr = readl(base + STM32_SPI_SR); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 160 | u32 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT; |
| 161 | |
| 162 | while ((priv->rx_len > 0) && |
| 163 | ((sr & SPI_SR_RXP) || |
| 164 | ((sr & SPI_SR_EOT) && ((sr & SPI_SR_RXWNE) || (rxplvl > 0))))) { |
| 165 | u32 offs = priv->cur_xferlen - priv->rx_len; |
| 166 | |
| 167 | if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u32)) && |
| 168 | (priv->rx_len >= sizeof(u32) || (sr & SPI_SR_RXWNE))) { |
| 169 | u32 *rx_buf32 = (u32 *)(priv->rx_buf + offs); |
| 170 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 171 | *rx_buf32 = readl(base + STM32_SPI_RXDR); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 172 | priv->rx_len -= sizeof(u32); |
| 173 | } else if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u16)) && |
| 174 | (priv->rx_len >= sizeof(u16) || |
| 175 | (!(sr & SPI_SR_RXWNE) && |
| 176 | (rxplvl >= 2 || priv->cur_bpw > 8)))) { |
| 177 | u16 *rx_buf16 = (u16 *)(priv->rx_buf + offs); |
| 178 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 179 | *rx_buf16 = readw(base + STM32_SPI_RXDR); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 180 | priv->rx_len -= sizeof(u16); |
| 181 | } else { |
| 182 | u8 *rx_buf8 = (u8 *)(priv->rx_buf + offs); |
| 183 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 184 | *rx_buf8 = readb(base + STM32_SPI_RXDR); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 185 | priv->rx_len -= sizeof(u8); |
| 186 | } |
| 187 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 188 | sr = readl(base + STM32_SPI_SR); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 189 | rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT; |
| 190 | } |
| 191 | |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 192 | log_debug("%d bytes left\n", priv->rx_len); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 195 | static int stm32_spi_enable(void __iomem *base) |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 196 | { |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 197 | log_debug("\n"); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 198 | |
| 199 | /* Enable the SPI hardware */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 200 | setbits_le32(base + STM32_SPI_CR1, SPI_CR1_SPE); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 205 | static int stm32_spi_disable(void __iomem *base) |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 206 | { |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 207 | log_debug("\n"); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 208 | |
| 209 | /* Disable the SPI hardware */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 210 | clrbits_le32(base + STM32_SPI_CR1, SPI_CR1_SPE); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int stm32_spi_claim_bus(struct udevice *slave) |
| 216 | { |
| 217 | struct udevice *bus = dev_get_parent(slave); |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 218 | struct stm32_spi_plat *plat = dev_get_plat(bus); |
| 219 | void __iomem *base = plat->base; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 220 | |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 221 | dev_dbg(slave, "\n"); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 222 | |
| 223 | /* Enable the SPI hardware */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 224 | return stm32_spi_enable(base); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int stm32_spi_release_bus(struct udevice *slave) |
| 228 | { |
| 229 | struct udevice *bus = dev_get_parent(slave); |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 230 | struct stm32_spi_plat *plat = dev_get_plat(bus); |
| 231 | void __iomem *base = plat->base; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 232 | |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 233 | dev_dbg(slave, "\n"); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 234 | |
| 235 | /* Disable the SPI hardware */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 236 | return stm32_spi_disable(base); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void stm32_spi_stopxfer(struct udevice *dev) |
| 240 | { |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 241 | struct stm32_spi_plat *plat = dev_get_plat(dev); |
| 242 | void __iomem *base = plat->base; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 243 | u32 cr1, sr; |
| 244 | int ret; |
| 245 | |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 246 | dev_dbg(dev, "\n"); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 247 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 248 | cr1 = readl(base + STM32_SPI_CR1); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 249 | |
| 250 | if (!(cr1 & SPI_CR1_SPE)) |
| 251 | return; |
| 252 | |
| 253 | /* Wait on EOT or suspend the flow */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 254 | ret = readl_poll_timeout(base + STM32_SPI_SR, sr, |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 255 | !(sr & SPI_SR_EOT), 100000); |
| 256 | if (ret < 0) { |
| 257 | if (cr1 & SPI_CR1_CSTART) { |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 258 | writel(cr1 | SPI_CR1_CSUSP, base + STM32_SPI_CR1); |
| 259 | if (readl_poll_timeout(base + STM32_SPI_SR, |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 260 | sr, !(sr & SPI_SR_SUSP), |
| 261 | 100000) < 0) |
| 262 | dev_err(dev, "Suspend request timeout\n"); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* clear status flags */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 267 | setbits_le32(base + STM32_SPI_IFCR, SPI_IFCR_ALL); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static int stm32_spi_set_cs(struct udevice *dev, unsigned int cs, bool enable) |
| 271 | { |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 272 | struct stm32_spi_plat *plat = dev_get_plat(dev); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 273 | struct stm32_spi_priv *priv = dev_get_priv(dev); |
| 274 | |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 275 | dev_dbg(dev, "cs=%d enable=%d\n", cs, enable); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 276 | |
| 277 | if (cs >= MAX_CS_COUNT) |
| 278 | return -ENODEV; |
| 279 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 280 | if (!dm_gpio_is_valid(&plat->cs_gpios[cs])) |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 281 | return -EINVAL; |
| 282 | |
| 283 | if (priv->cs_high) |
| 284 | enable = !enable; |
| 285 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 286 | return dm_gpio_set_value(&plat->cs_gpios[cs], enable ? 1 : 0); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static int stm32_spi_set_mode(struct udevice *bus, uint mode) |
| 290 | { |
| 291 | struct stm32_spi_priv *priv = dev_get_priv(bus); |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 292 | struct stm32_spi_plat *plat = dev_get_plat(bus); |
| 293 | void __iomem *base = plat->base; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 294 | u32 cfg2_clrb = 0, cfg2_setb = 0; |
| 295 | |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 296 | dev_dbg(bus, "mode=%d\n", mode); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 297 | |
| 298 | if (mode & SPI_CPOL) |
| 299 | cfg2_setb |= SPI_CFG2_CPOL; |
| 300 | else |
| 301 | cfg2_clrb |= SPI_CFG2_CPOL; |
| 302 | |
| 303 | if (mode & SPI_CPHA) |
| 304 | cfg2_setb |= SPI_CFG2_CPHA; |
| 305 | else |
| 306 | cfg2_clrb |= SPI_CFG2_CPHA; |
| 307 | |
| 308 | if (mode & SPI_LSB_FIRST) |
| 309 | cfg2_setb |= SPI_CFG2_LSBFRST; |
| 310 | else |
| 311 | cfg2_clrb |= SPI_CFG2_LSBFRST; |
| 312 | |
| 313 | if (cfg2_clrb || cfg2_setb) |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 314 | clrsetbits_le32(base + STM32_SPI_CFG2, |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 315 | cfg2_clrb, cfg2_setb); |
| 316 | |
| 317 | if (mode & SPI_CS_HIGH) |
| 318 | priv->cs_high = true; |
| 319 | else |
| 320 | priv->cs_high = false; |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static int stm32_spi_set_fthlv(struct udevice *dev, u32 xfer_len) |
| 325 | { |
| 326 | struct stm32_spi_priv *priv = dev_get_priv(dev); |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 327 | struct stm32_spi_plat *plat = dev_get_plat(dev); |
| 328 | void __iomem *base = plat->base; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 329 | u32 fthlv, half_fifo; |
| 330 | |
| 331 | /* data packet should not exceed 1/2 of fifo space */ |
| 332 | half_fifo = (priv->fifo_size / 2); |
| 333 | |
| 334 | /* data_packet should not exceed transfer length */ |
| 335 | fthlv = (half_fifo > xfer_len) ? xfer_len : half_fifo; |
| 336 | |
| 337 | /* align packet size with data registers access */ |
| 338 | fthlv -= (fthlv % 4); |
| 339 | |
| 340 | if (!fthlv) |
| 341 | fthlv = 1; |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 342 | clrsetbits_le32(base + STM32_SPI_CFG1, SPI_CFG1_FTHLV, |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 343 | (fthlv - 1) << SPI_CFG1_FTHLV_SHIFT); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int stm32_spi_set_speed(struct udevice *bus, uint hz) |
| 349 | { |
| 350 | struct stm32_spi_priv *priv = dev_get_priv(bus); |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 351 | struct stm32_spi_plat *plat = dev_get_plat(bus); |
| 352 | void __iomem *base = plat->base; |
Patrick Delaunay | 5043c4b | 2019-06-21 15:26:58 +0200 | [diff] [blame] | 353 | u32 mbrdiv; |
| 354 | long div; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 355 | |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 356 | dev_dbg(bus, "hz=%d\n", hz); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 357 | |
| 358 | if (priv->cur_hz == hz) |
| 359 | return 0; |
| 360 | |
| 361 | div = DIV_ROUND_UP(priv->bus_clk_rate, hz); |
| 362 | |
| 363 | if (div < STM32_MBR_DIV_MIN || |
| 364 | div > STM32_MBR_DIV_MAX) |
| 365 | return -EINVAL; |
| 366 | |
| 367 | /* Determine the first power of 2 greater than or equal to div */ |
| 368 | if (div & (div - 1)) |
| 369 | mbrdiv = fls(div); |
| 370 | else |
| 371 | mbrdiv = fls(div) - 1; |
| 372 | |
Patrick Delaunay | 5043c4b | 2019-06-21 15:26:58 +0200 | [diff] [blame] | 373 | if (!mbrdiv) |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 374 | return -EINVAL; |
| 375 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 376 | clrsetbits_le32(base + STM32_SPI_CFG1, SPI_CFG1_MBR, |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 377 | (mbrdiv - 1) << SPI_CFG1_MBR_SHIFT); |
| 378 | |
| 379 | priv->cur_hz = hz; |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static int stm32_spi_xfer(struct udevice *slave, unsigned int bitlen, |
| 385 | const void *dout, void *din, unsigned long flags) |
| 386 | { |
| 387 | struct udevice *bus = dev_get_parent(slave); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 388 | struct dm_spi_slave_plat *slave_plat; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 389 | struct stm32_spi_priv *priv = dev_get_priv(bus); |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 390 | struct stm32_spi_plat *plat = dev_get_plat(bus); |
| 391 | void __iomem *base = plat->base; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 392 | u32 sr; |
| 393 | u32 ifcr = 0; |
| 394 | u32 xferlen; |
| 395 | u32 mode; |
| 396 | int xfer_status = 0; |
| 397 | |
| 398 | xferlen = bitlen / 8; |
| 399 | |
| 400 | if (xferlen <= SPI_CR2_TSIZE) |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 401 | writel(xferlen, base + STM32_SPI_CR2); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 402 | else |
| 403 | return -EMSGSIZE; |
| 404 | |
| 405 | priv->tx_buf = dout; |
| 406 | priv->rx_buf = din; |
| 407 | priv->tx_len = priv->tx_buf ? bitlen / 8 : 0; |
| 408 | priv->rx_len = priv->rx_buf ? bitlen / 8 : 0; |
| 409 | |
| 410 | mode = SPI_FULL_DUPLEX; |
| 411 | if (!priv->tx_buf) |
| 412 | mode = SPI_SIMPLEX_RX; |
| 413 | else if (!priv->rx_buf) |
| 414 | mode = SPI_SIMPLEX_TX; |
| 415 | |
| 416 | if (priv->cur_xferlen != xferlen || priv->cur_mode != mode) { |
| 417 | priv->cur_mode = mode; |
| 418 | priv->cur_xferlen = xferlen; |
| 419 | |
| 420 | /* Disable the SPI hardware to unlock CFG1/CFG2 registers */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 421 | stm32_spi_disable(base); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 422 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 423 | clrsetbits_le32(base + STM32_SPI_CFG2, SPI_CFG2_COMM, |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 424 | mode << SPI_CFG2_COMM_SHIFT); |
| 425 | |
| 426 | stm32_spi_set_fthlv(bus, xferlen); |
| 427 | |
| 428 | /* Enable the SPI hardware */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 429 | stm32_spi_enable(base); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 432 | dev_dbg(bus, "priv->tx_len=%d priv->rx_len=%d\n", |
| 433 | priv->tx_len, priv->rx_len); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 434 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 435 | slave_plat = dev_get_parent_plat(slave); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 436 | if (flags & SPI_XFER_BEGIN) |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 437 | stm32_spi_set_cs(bus, slave_plat->cs[0], false); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 438 | |
| 439 | /* Be sure to have data in fifo before starting data transfer */ |
| 440 | if (priv->tx_buf) |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 441 | stm32_spi_write_txfifo(bus); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 442 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 443 | setbits_le32(base + STM32_SPI_CR1, SPI_CR1_CSTART); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 444 | |
| 445 | while (1) { |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 446 | sr = readl(base + STM32_SPI_SR); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 447 | |
| 448 | if (sr & SPI_SR_OVR) { |
| 449 | dev_err(bus, "Overrun: RX data lost\n"); |
| 450 | xfer_status = -EIO; |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | if (sr & SPI_SR_SUSP) { |
| 455 | dev_warn(bus, "System too slow is limiting data throughput\n"); |
| 456 | |
| 457 | if (priv->rx_buf && priv->rx_len > 0) |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 458 | stm32_spi_read_rxfifo(bus); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 459 | |
| 460 | ifcr |= SPI_SR_SUSP; |
| 461 | } |
| 462 | |
| 463 | if (sr & SPI_SR_TXTF) |
| 464 | ifcr |= SPI_SR_TXTF; |
| 465 | |
| 466 | if (sr & SPI_SR_TXP) |
| 467 | if (priv->tx_buf && priv->tx_len > 0) |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 468 | stm32_spi_write_txfifo(bus); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 469 | |
| 470 | if (sr & SPI_SR_RXP) |
| 471 | if (priv->rx_buf && priv->rx_len > 0) |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 472 | stm32_spi_read_rxfifo(bus); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 473 | |
| 474 | if (sr & SPI_SR_EOT) { |
| 475 | if (priv->rx_buf && priv->rx_len > 0) |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 476 | stm32_spi_read_rxfifo(bus); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 477 | break; |
| 478 | } |
| 479 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 480 | writel(ifcr, base + STM32_SPI_IFCR); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /* clear status flags */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 484 | setbits_le32(base + STM32_SPI_IFCR, SPI_IFCR_ALL); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 485 | stm32_spi_stopxfer(bus); |
| 486 | |
| 487 | if (flags & SPI_XFER_END) |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 488 | stm32_spi_set_cs(bus, slave_plat->cs[0], true); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 489 | |
| 490 | return xfer_status; |
| 491 | } |
| 492 | |
| 493 | static int stm32_spi_get_fifo_size(struct udevice *dev) |
| 494 | { |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 495 | struct stm32_spi_plat *plat = dev_get_plat(dev); |
| 496 | void __iomem *base = plat->base; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 497 | u32 count = 0; |
| 498 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 499 | stm32_spi_enable(base); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 500 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 501 | while (readl(base + STM32_SPI_SR) & SPI_SR_TXP) |
| 502 | writeb(++count, base + STM32_SPI_TXDR); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 503 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 504 | stm32_spi_disable(base); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 505 | |
Patrick Delaunay | 7380f9e | 2020-11-06 19:01:52 +0100 | [diff] [blame] | 506 | dev_dbg(dev, "%d x 8-bit fifo size\n", count); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 507 | |
| 508 | return count; |
| 509 | } |
| 510 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 511 | static int stm32_spi_of_to_plat(struct udevice *dev) |
| 512 | { |
| 513 | struct stm32_spi_plat *plat = dev_get_plat(dev); |
| 514 | int ret; |
| 515 | |
| 516 | plat->base = dev_read_addr_ptr(dev); |
| 517 | if (!plat->base) { |
| 518 | dev_err(dev, "can't get registers base address\n"); |
| 519 | return -ENOENT; |
| 520 | } |
| 521 | |
| 522 | ret = clk_get_by_index(dev, 0, &plat->clk); |
| 523 | if (ret < 0) |
| 524 | return ret; |
| 525 | |
| 526 | ret = reset_get_by_index(dev, 0, &plat->rst_ctl); |
| 527 | if (ret < 0) |
Sean Anderson | d318eb3 | 2023-12-16 14:38:42 -0500 | [diff] [blame] | 528 | return ret; |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 529 | |
| 530 | ret = gpio_request_list_by_name(dev, "cs-gpios", plat->cs_gpios, |
| 531 | ARRAY_SIZE(plat->cs_gpios), 0); |
| 532 | if (ret < 0) { |
| 533 | dev_err(dev, "Can't get %s cs gpios: %d", dev->name, ret); |
Sean Anderson | d318eb3 | 2023-12-16 14:38:42 -0500 | [diff] [blame] | 534 | return -ENOENT; |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | return 0; |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 540 | static int stm32_spi_probe(struct udevice *dev) |
| 541 | { |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 542 | struct stm32_spi_plat *plat = dev_get_plat(dev); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 543 | struct stm32_spi_priv *priv = dev_get_priv(dev); |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 544 | void __iomem *base = plat->base; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 545 | unsigned long clk_rate; |
| 546 | int ret; |
Patrick Delaunay | 5043c4b | 2019-06-21 15:26:58 +0200 | [diff] [blame] | 547 | unsigned int i; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 548 | |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 549 | /* enable clock */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 550 | ret = clk_enable(&plat->clk); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 551 | if (ret < 0) |
| 552 | return ret; |
| 553 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 554 | clk_rate = clk_get_rate(&plat->clk); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 555 | if (!clk_rate) { |
| 556 | ret = -EINVAL; |
| 557 | goto clk_err; |
| 558 | } |
| 559 | |
| 560 | priv->bus_clk_rate = clk_rate; |
| 561 | |
| 562 | /* perform reset */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 563 | reset_assert(&plat->rst_ctl); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 564 | udelay(2); |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 565 | reset_deassert(&plat->rst_ctl); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 566 | |
| 567 | priv->fifo_size = stm32_spi_get_fifo_size(dev); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 568 | priv->cur_mode = SPI_FULL_DUPLEX; |
| 569 | priv->cur_xferlen = 0; |
| 570 | priv->cur_bpw = SPI_DEFAULT_WORDLEN; |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 571 | clrsetbits_le32(base + STM32_SPI_CFG1, SPI_CFG1_DSIZE, |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 572 | priv->cur_bpw - 1); |
| 573 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 574 | for (i = 0; i < ARRAY_SIZE(plat->cs_gpios); i++) { |
| 575 | if (!dm_gpio_is_valid(&plat->cs_gpios[i])) |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 576 | continue; |
| 577 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 578 | dm_gpio_set_dir_flags(&plat->cs_gpios[i], |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 579 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
| 580 | } |
| 581 | |
| 582 | /* Ensure I2SMOD bit is kept cleared */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 583 | clrbits_le32(base + STM32_SPI_I2SCFGR, SPI_I2SCFGR_I2SMOD); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 584 | |
| 585 | /* |
| 586 | * - SS input value high |
| 587 | * - transmitter half duplex direction |
| 588 | * - automatic communication suspend when RX-Fifo is full |
| 589 | */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 590 | setbits_le32(base + STM32_SPI_CR1, |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 591 | SPI_CR1_SSI | SPI_CR1_HDDIR | SPI_CR1_MASRX); |
| 592 | |
| 593 | /* |
| 594 | * - Set the master mode (default Motorola mode) |
| 595 | * - Consider 1 master/n slaves configuration and |
| 596 | * SS input value is determined by the SSI bit |
| 597 | * - keep control of all associated GPIOs |
| 598 | */ |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 599 | setbits_le32(base + STM32_SPI_CFG2, |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 600 | SPI_CFG2_MASTER | SPI_CFG2_SSM | SPI_CFG2_AFCNTR); |
| 601 | |
| 602 | return 0; |
| 603 | |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 604 | clk_err: |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 605 | clk_disable(&plat->clk); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 606 | |
| 607 | return ret; |
| 608 | }; |
| 609 | |
| 610 | static int stm32_spi_remove(struct udevice *dev) |
| 611 | { |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 612 | struct stm32_spi_plat *plat = dev_get_plat(dev); |
| 613 | void __iomem *base = plat->base; |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 614 | int ret; |
| 615 | |
| 616 | stm32_spi_stopxfer(dev); |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 617 | stm32_spi_disable(base); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 618 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 619 | ret = reset_assert(&plat->rst_ctl); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 620 | if (ret < 0) |
| 621 | return ret; |
| 622 | |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 623 | reset_free(&plat->rst_ctl); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 624 | |
Sean Anderson | d318eb3 | 2023-12-16 14:38:42 -0500 | [diff] [blame] | 625 | return clk_disable(&plat->clk); |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | static const struct dm_spi_ops stm32_spi_ops = { |
| 629 | .claim_bus = stm32_spi_claim_bus, |
| 630 | .release_bus = stm32_spi_release_bus, |
| 631 | .set_mode = stm32_spi_set_mode, |
| 632 | .set_speed = stm32_spi_set_speed, |
| 633 | .xfer = stm32_spi_xfer, |
| 634 | }; |
| 635 | |
| 636 | static const struct udevice_id stm32_spi_ids[] = { |
| 637 | { .compatible = "st,stm32h7-spi", }, |
| 638 | { } |
| 639 | }; |
| 640 | |
| 641 | U_BOOT_DRIVER(stm32_spi) = { |
| 642 | .name = "stm32_spi", |
| 643 | .id = UCLASS_SPI, |
| 644 | .of_match = stm32_spi_ids, |
| 645 | .ops = &stm32_spi_ops, |
Patrice Chotard | a9f8bbd | 2021-08-03 11:16:40 +0200 | [diff] [blame] | 646 | .of_to_plat = stm32_spi_of_to_plat, |
| 647 | .plat_auto = sizeof(struct stm32_spi_plat), |
| 648 | .priv_auto = sizeof(struct stm32_spi_priv), |
Patrice Chotard | 0b08bf8 | 2019-04-30 18:08:28 +0200 | [diff] [blame] | 649 | .probe = stm32_spi_probe, |
| 650 | .remove = stm32_spi_remove, |
| 651 | }; |