Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Designware master SPI core controller driver |
| 4 | * |
| 5 | * Copyright (C) 2014 Stefan Roese <sr@denx.de> |
| 6 | * |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 7 | * Very loosely based on the Linux driver: |
| 8 | * drivers/spi/spi-dw.c, which is: |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 9 | * Copyright (c) 2009, Intel Corporation. |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 12 | #define LOG_CATEGORY UCLASS_SPI |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 13 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Horatiu.Vultur@microchip.com | 1b77de4 | 2019-02-25 10:59:54 +0000 | [diff] [blame] | 15 | #include <asm-generic/gpio.h> |
Eugeniy Paltsev | 58c125b | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 16 | #include <clk.h> |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 17 | #include <dm.h> |
| 18 | #include <errno.h> |
| 19 | #include <malloc.h> |
| 20 | #include <spi.h> |
| 21 | #include <fdtdec.h> |
Ley Foon Tan | 6ac5909 | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 22 | #include <reset.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 23 | #include <dm/device_compat.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 24 | #include <linux/bitops.h> |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 25 | #include <linux/compat.h> |
Eugeniy Paltsev | c6b4f03 | 2018-03-22 13:50:43 +0300 | [diff] [blame] | 26 | #include <linux/iopoll.h> |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 27 | #include <asm/io.h> |
| 28 | |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 29 | /* Register offsets */ |
Sean Anderson | 3004034 | 2020-10-16 18:57:47 -0400 | [diff] [blame] | 30 | #define DW_SPI_CTRLR0 0x00 |
| 31 | #define DW_SPI_CTRLR1 0x04 |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 32 | #define DW_SPI_SSIENR 0x08 |
| 33 | #define DW_SPI_MWCR 0x0c |
| 34 | #define DW_SPI_SER 0x10 |
| 35 | #define DW_SPI_BAUDR 0x14 |
Sean Anderson | 3004034 | 2020-10-16 18:57:47 -0400 | [diff] [blame] | 36 | #define DW_SPI_TXFTLR 0x18 |
| 37 | #define DW_SPI_RXFTLR 0x1c |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 38 | #define DW_SPI_TXFLR 0x20 |
| 39 | #define DW_SPI_RXFLR 0x24 |
| 40 | #define DW_SPI_SR 0x28 |
| 41 | #define DW_SPI_IMR 0x2c |
| 42 | #define DW_SPI_ISR 0x30 |
| 43 | #define DW_SPI_RISR 0x34 |
| 44 | #define DW_SPI_TXOICR 0x38 |
| 45 | #define DW_SPI_RXOICR 0x3c |
| 46 | #define DW_SPI_RXUICR 0x40 |
| 47 | #define DW_SPI_MSTICR 0x44 |
| 48 | #define DW_SPI_ICR 0x48 |
| 49 | #define DW_SPI_DMACR 0x4c |
| 50 | #define DW_SPI_DMATDLR 0x50 |
| 51 | #define DW_SPI_DMARDLR 0x54 |
| 52 | #define DW_SPI_IDR 0x58 |
| 53 | #define DW_SPI_VERSION 0x5c |
| 54 | #define DW_SPI_DR 0x60 |
| 55 | |
| 56 | /* Bit fields in CTRLR0 */ |
| 57 | #define SPI_DFS_OFFSET 0 |
| 58 | |
| 59 | #define SPI_FRF_OFFSET 4 |
| 60 | #define SPI_FRF_SPI 0x0 |
| 61 | #define SPI_FRF_SSP 0x1 |
| 62 | #define SPI_FRF_MICROWIRE 0x2 |
| 63 | #define SPI_FRF_RESV 0x3 |
| 64 | |
| 65 | #define SPI_MODE_OFFSET 6 |
| 66 | #define SPI_SCPH_OFFSET 6 |
| 67 | #define SPI_SCOL_OFFSET 7 |
| 68 | |
| 69 | #define SPI_TMOD_OFFSET 8 |
| 70 | #define SPI_TMOD_MASK (0x3 << SPI_TMOD_OFFSET) |
| 71 | #define SPI_TMOD_TR 0x0 /* xmit & recv */ |
| 72 | #define SPI_TMOD_TO 0x1 /* xmit only */ |
| 73 | #define SPI_TMOD_RO 0x2 /* recv only */ |
| 74 | #define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */ |
| 75 | |
| 76 | #define SPI_SLVOE_OFFSET 10 |
| 77 | #define SPI_SRL_OFFSET 11 |
| 78 | #define SPI_CFS_OFFSET 12 |
| 79 | |
| 80 | /* Bit fields in SR, 7 bits */ |
Jagan Teki | 95e77d9 | 2015-10-23 01:01:36 +0530 | [diff] [blame] | 81 | #define SR_MASK GENMASK(6, 0) /* cover 7 bits */ |
Jagan Teki | 431a9f0 | 2015-10-23 01:36:23 +0530 | [diff] [blame] | 82 | #define SR_BUSY BIT(0) |
| 83 | #define SR_TF_NOT_FULL BIT(1) |
| 84 | #define SR_TF_EMPT BIT(2) |
| 85 | #define SR_RF_NOT_EMPT BIT(3) |
| 86 | #define SR_RF_FULL BIT(4) |
| 87 | #define SR_TX_ERR BIT(5) |
| 88 | #define SR_DCOL BIT(6) |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 89 | |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 90 | #define RX_TIMEOUT 1000 /* timeout in ms */ |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 91 | |
| 92 | struct dw_spi_platdata { |
| 93 | s32 frequency; /* Default clock frequency, -1 for none */ |
| 94 | void __iomem *regs; |
| 95 | }; |
| 96 | |
| 97 | struct dw_spi_priv { |
Eugeniy Paltsev | 58c125b | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 98 | struct clk clk; |
Sean Anderson | ddd3450 | 2020-10-16 18:57:49 -0400 | [diff] [blame^] | 99 | struct reset_ctl_bulk resets; |
Eugeniy Paltsev | bcdcb3e | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 100 | struct gpio_desc cs_gpio; /* External chip-select gpio */ |
| 101 | |
Sean Anderson | ddd3450 | 2020-10-16 18:57:49 -0400 | [diff] [blame^] | 102 | void __iomem *regs; |
| 103 | unsigned long bus_clk_rate; |
| 104 | unsigned int freq; /* Default frequency */ |
| 105 | unsigned int mode; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 106 | |
Sean Anderson | ddd3450 | 2020-10-16 18:57:49 -0400 | [diff] [blame^] | 107 | const void *tx; |
| 108 | const void *tx_end; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 109 | void *rx; |
| 110 | void *rx_end; |
Sean Anderson | ddd3450 | 2020-10-16 18:57:49 -0400 | [diff] [blame^] | 111 | u32 fifo_len; /* depth of the FIFO buffer */ |
Ley Foon Tan | 6ac5909 | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 112 | |
Sean Anderson | ddd3450 | 2020-10-16 18:57:49 -0400 | [diff] [blame^] | 113 | int bits_per_word; |
| 114 | int len; |
| 115 | u8 cs; /* chip select pin */ |
| 116 | u8 tmode; /* TR/TO/RO/EEPROM */ |
| 117 | u8 type; /* SPI/SSP/MicroWire */ |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
Eugeniy Paltsev | 4b5f6c5 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 120 | static inline u32 dw_read(struct dw_spi_priv *priv, u32 offset) |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 121 | { |
| 122 | return __raw_readl(priv->regs + offset); |
| 123 | } |
| 124 | |
Eugeniy Paltsev | 4b5f6c5 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 125 | static inline void dw_write(struct dw_spi_priv *priv, u32 offset, u32 val) |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 126 | { |
| 127 | __raw_writel(val, priv->regs + offset); |
| 128 | } |
| 129 | |
Eugeniy Paltsev | bcdcb3e | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 130 | static int request_gpio_cs(struct udevice *bus) |
| 131 | { |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 132 | #if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD) |
Eugeniy Paltsev | bcdcb3e | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 133 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 134 | int ret; |
| 135 | |
| 136 | /* External chip select gpio line is optional */ |
Sean Anderson | 13fc44e | 2020-10-16 18:57:45 -0400 | [diff] [blame] | 137 | ret = gpio_request_by_name(bus, "cs-gpios", 0, &priv->cs_gpio, |
| 138 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
Eugeniy Paltsev | bcdcb3e | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 139 | if (ret == -ENOENT) |
| 140 | return 0; |
| 141 | |
| 142 | if (ret < 0) { |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 143 | dev_err(bus, "Couldn't request gpio! (error %d)\n", ret); |
Eugeniy Paltsev | bcdcb3e | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | if (dm_gpio_is_valid(&priv->cs_gpio)) { |
| 148 | dm_gpio_set_dir_flags(&priv->cs_gpio, |
| 149 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
| 150 | } |
| 151 | |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 152 | dev_dbg(bus, "Using external gpio for CS management\n"); |
Eugeniy Paltsev | bcdcb3e | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 153 | #endif |
| 154 | return 0; |
| 155 | } |
| 156 | |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 157 | static int dw_spi_ofdata_to_platdata(struct udevice *bus) |
| 158 | { |
| 159 | struct dw_spi_platdata *plat = bus->platdata; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 160 | |
Masahiro Yamada | 8613c8d | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 161 | plat->regs = dev_read_addr_ptr(bus); |
Sean Anderson | c785f43 | 2020-10-16 18:57:46 -0400 | [diff] [blame] | 162 | if (!plat->regs) |
| 163 | return -EINVAL; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 164 | |
| 165 | /* Use 500KHz as a suitable default */ |
Simon Goldschmidt | 27c3e07 | 2019-05-09 22:11:57 +0200 | [diff] [blame] | 166 | plat->frequency = dev_read_u32_default(bus, "spi-max-frequency", |
| 167 | 500000); |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 168 | dev_info(bus, "max-frequency=%d\n", plat->frequency); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 169 | |
Eugeniy Paltsev | bcdcb3e | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 170 | return request_gpio_cs(bus); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 173 | /* Restart the controller, disable all interrupts, clean rx fifo */ |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 174 | static void spi_hw_init(struct udevice *bus, struct dw_spi_priv *priv) |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 175 | { |
Sean Anderson | 934beab | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 176 | dw_write(priv, DW_SPI_SSIENR, 0); |
Eugeniy Paltsev | 4b5f6c5 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 177 | dw_write(priv, DW_SPI_IMR, 0xff); |
Sean Anderson | 934beab | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 178 | dw_write(priv, DW_SPI_SSIENR, 1); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * Try to detect the FIFO depth if not set by interface driver, |
| 182 | * the depth could be from 2 to 256 from HW spec |
| 183 | */ |
| 184 | if (!priv->fifo_len) { |
| 185 | u32 fifo; |
| 186 | |
Axel Lin | 52091ad | 2015-02-26 10:45:22 +0800 | [diff] [blame] | 187 | for (fifo = 1; fifo < 256; fifo++) { |
Sean Anderson | 3004034 | 2020-10-16 18:57:47 -0400 | [diff] [blame] | 188 | dw_write(priv, DW_SPI_TXFTLR, fifo); |
| 189 | if (fifo != dw_read(priv, DW_SPI_TXFTLR)) |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | |
Axel Lin | 52091ad | 2015-02-26 10:45:22 +0800 | [diff] [blame] | 193 | priv->fifo_len = (fifo == 1) ? 0 : fifo; |
Sean Anderson | 3004034 | 2020-10-16 18:57:47 -0400 | [diff] [blame] | 194 | dw_write(priv, DW_SPI_TXFTLR, 0); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 195 | } |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 196 | dev_dbg(bus, "fifo_len=%d\n", priv->fifo_len); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Eugeniy Paltsev | 58c125b | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 199 | /* |
| 200 | * We define dw_spi_get_clk function as 'weak' as some targets |
| 201 | * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API |
| 202 | * and implement dw_spi_get_clk their own way in their clock manager. |
| 203 | */ |
| 204 | __weak int dw_spi_get_clk(struct udevice *bus, ulong *rate) |
| 205 | { |
| 206 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 207 | int ret; |
| 208 | |
| 209 | ret = clk_get_by_index(bus, 0, &priv->clk); |
| 210 | if (ret) |
| 211 | return ret; |
| 212 | |
| 213 | ret = clk_enable(&priv->clk); |
| 214 | if (ret && ret != -ENOSYS && ret != -ENOTSUPP) |
| 215 | return ret; |
| 216 | |
| 217 | *rate = clk_get_rate(&priv->clk); |
| 218 | if (!*rate) |
| 219 | goto err_rate; |
| 220 | |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 221 | dev_dbg(bus, "Got clock via device tree: %lu Hz\n", *rate); |
Eugeniy Paltsev | 58c125b | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | |
| 225 | err_rate: |
| 226 | clk_disable(&priv->clk); |
| 227 | clk_free(&priv->clk); |
| 228 | |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
Ley Foon Tan | 6ac5909 | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 232 | static int dw_spi_reset(struct udevice *bus) |
| 233 | { |
| 234 | int ret; |
| 235 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 236 | |
| 237 | ret = reset_get_bulk(bus, &priv->resets); |
| 238 | if (ret) { |
| 239 | /* |
| 240 | * Return 0 if error due to !CONFIG_DM_RESET and reset |
| 241 | * DT property is not present. |
| 242 | */ |
| 243 | if (ret == -ENOENT || ret == -ENOTSUPP) |
| 244 | return 0; |
| 245 | |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 246 | dev_warn(bus, "Couldn't find/assert reset device (error %d)\n", |
| 247 | ret); |
Ley Foon Tan | 6ac5909 | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | ret = reset_deassert_bulk(&priv->resets); |
| 252 | if (ret) { |
| 253 | reset_release_bulk(&priv->resets); |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 254 | dev_err(bus, "Failed to de-assert reset for SPI (error %d)\n", |
| 255 | ret); |
Ley Foon Tan | 6ac5909 | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 262 | static int dw_spi_probe(struct udevice *bus) |
| 263 | { |
| 264 | struct dw_spi_platdata *plat = dev_get_platdata(bus); |
| 265 | struct dw_spi_priv *priv = dev_get_priv(bus); |
Eugeniy Paltsev | 58c125b | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 266 | int ret; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 267 | |
| 268 | priv->regs = plat->regs; |
| 269 | priv->freq = plat->frequency; |
| 270 | |
Eugeniy Paltsev | 58c125b | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 271 | ret = dw_spi_get_clk(bus, &priv->bus_clk_rate); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | |
Ley Foon Tan | 6ac5909 | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 275 | ret = dw_spi_reset(bus); |
| 276 | if (ret) |
| 277 | return ret; |
| 278 | |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 279 | /* Currently only bits_per_word == 8 supported */ |
| 280 | priv->bits_per_word = 8; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 281 | |
| 282 | priv->tmode = 0; /* Tx & Rx */ |
| 283 | |
| 284 | /* Basic HW init */ |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 285 | spi_hw_init(bus, priv); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /* Return the max entries we can fill into tx fifo */ |
| 291 | static inline u32 tx_max(struct dw_spi_priv *priv) |
| 292 | { |
| 293 | u32 tx_left, tx_room, rxtx_gap; |
| 294 | |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 295 | tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3); |
Eugeniy Paltsev | 4b5f6c5 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 296 | tx_room = priv->fifo_len - dw_read(priv, DW_SPI_TXFLR); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * Another concern is about the tx/rx mismatch, we |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 300 | * thought about using (priv->fifo_len - rxflr - txflr) as |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 301 | * one maximum value for tx, but it doesn't cover the |
| 302 | * data which is out of tx/rx fifo and inside the |
| 303 | * shift registers. So a control from sw point of |
| 304 | * view is taken. |
| 305 | */ |
| 306 | rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) / |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 307 | (priv->bits_per_word >> 3); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 308 | |
| 309 | return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap)); |
| 310 | } |
| 311 | |
| 312 | /* Return the max entries we should read out of rx fifo */ |
| 313 | static inline u32 rx_max(struct dw_spi_priv *priv) |
| 314 | { |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 315 | u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 316 | |
Eugeniy Paltsev | 4b5f6c5 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 317 | return min_t(u32, rx_left, dw_read(priv, DW_SPI_RXFLR)); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static void dw_writer(struct dw_spi_priv *priv) |
| 321 | { |
| 322 | u32 max = tx_max(priv); |
Sean Anderson | caf1107 | 2020-10-16 18:57:43 -0400 | [diff] [blame] | 323 | u16 txw = 0xFFFF; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 324 | |
| 325 | while (max--) { |
| 326 | /* Set the tx word if the transfer's original "tx" is not null */ |
| 327 | if (priv->tx_end - priv->len) { |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 328 | if (priv->bits_per_word == 8) |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 329 | txw = *(u8 *)(priv->tx); |
| 330 | else |
| 331 | txw = *(u16 *)(priv->tx); |
| 332 | } |
Eugeniy Paltsev | 4b5f6c5 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 333 | dw_write(priv, DW_SPI_DR, txw); |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 334 | log_content("tx=0x%02x\n", txw); |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 335 | priv->tx += priv->bits_per_word >> 3; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Eugeniy Paltsev | d3d8aae | 2018-03-22 13:50:45 +0300 | [diff] [blame] | 339 | static void dw_reader(struct dw_spi_priv *priv) |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 340 | { |
Eugeniy Paltsev | d3d8aae | 2018-03-22 13:50:45 +0300 | [diff] [blame] | 341 | u32 max = rx_max(priv); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 342 | u16 rxw; |
| 343 | |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 344 | while (max--) { |
Eugeniy Paltsev | 4b5f6c5 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 345 | rxw = dw_read(priv, DW_SPI_DR); |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 346 | log_content("rx=0x%02x\n", rxw); |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 347 | |
Eugeniy Paltsev | d3d8aae | 2018-03-22 13:50:45 +0300 | [diff] [blame] | 348 | /* Care about rx if the transfer's original "rx" is not null */ |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 349 | if (priv->rx_end - priv->len) { |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 350 | if (priv->bits_per_word == 8) |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 351 | *(u8 *)(priv->rx) = rxw; |
| 352 | else |
| 353 | *(u16 *)(priv->rx) = rxw; |
| 354 | } |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 355 | priv->rx += priv->bits_per_word >> 3; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 356 | } |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static int poll_transfer(struct dw_spi_priv *priv) |
| 360 | { |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 361 | do { |
| 362 | dw_writer(priv); |
Eugeniy Paltsev | d3d8aae | 2018-03-22 13:50:45 +0300 | [diff] [blame] | 363 | dw_reader(priv); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 364 | } while (priv->rx_end > priv->rx); |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
Gregory CLEMENT | bea91b0 | 2018-10-09 14:14:07 +0200 | [diff] [blame] | 369 | /* |
| 370 | * We define external_cs_manage function as 'weak' as some targets |
| 371 | * (like MSCC Ocelot) don't control the external CS pin using a GPIO |
| 372 | * controller. These SoCs use specific registers to control by |
| 373 | * software the SPI pins (and especially the CS). |
| 374 | */ |
| 375 | __weak void external_cs_manage(struct udevice *dev, bool on) |
Eugeniy Paltsev | bcdcb3e | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 376 | { |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 377 | #if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD) |
Eugeniy Paltsev | bcdcb3e | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 378 | struct dw_spi_priv *priv = dev_get_priv(dev->parent); |
| 379 | |
| 380 | if (!dm_gpio_is_valid(&priv->cs_gpio)) |
| 381 | return; |
| 382 | |
| 383 | dm_gpio_set_value(&priv->cs_gpio, on ? 1 : 0); |
| 384 | #endif |
| 385 | } |
| 386 | |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 387 | static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 388 | const void *dout, void *din, unsigned long flags) |
| 389 | { |
| 390 | struct udevice *bus = dev->parent; |
| 391 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 392 | const u8 *tx = dout; |
| 393 | u8 *rx = din; |
| 394 | int ret = 0; |
| 395 | u32 cr0 = 0; |
Eugeniy Paltsev | c6b4f03 | 2018-03-22 13:50:43 +0300 | [diff] [blame] | 396 | u32 val; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 397 | u32 cs; |
| 398 | |
| 399 | /* spi core configured to do 8 bit transfers */ |
| 400 | if (bitlen % 8) { |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 401 | dev_err(dev, "Non byte aligned SPI transfer.\n"); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 402 | return -1; |
| 403 | } |
| 404 | |
Eugeniy Paltsev | bcdcb3e | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 405 | /* Start the transaction if necessary. */ |
| 406 | if (flags & SPI_XFER_BEGIN) |
| 407 | external_cs_manage(dev, false); |
| 408 | |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 409 | cr0 = (priv->bits_per_word - 1) | (priv->type << SPI_FRF_OFFSET) | |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 410 | (priv->mode << SPI_MODE_OFFSET) | |
| 411 | (priv->tmode << SPI_TMOD_OFFSET); |
| 412 | |
| 413 | if (rx && tx) |
| 414 | priv->tmode = SPI_TMOD_TR; |
| 415 | else if (rx) |
| 416 | priv->tmode = SPI_TMOD_RO; |
| 417 | else |
Eugeniy Paltsev | fc282c7 | 2018-03-22 13:50:44 +0300 | [diff] [blame] | 418 | /* |
| 419 | * In transmit only mode (SPI_TMOD_TO) input FIFO never gets |
| 420 | * any data which breaks our logic in poll_transfer() above. |
| 421 | */ |
| 422 | priv->tmode = SPI_TMOD_TR; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 423 | |
| 424 | cr0 &= ~SPI_TMOD_MASK; |
| 425 | cr0 |= (priv->tmode << SPI_TMOD_OFFSET); |
| 426 | |
Stefan Roese | a72f802 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 427 | priv->len = bitlen >> 3; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 428 | |
| 429 | priv->tx = (void *)tx; |
| 430 | priv->tx_end = priv->tx + priv->len; |
| 431 | priv->rx = rx; |
| 432 | priv->rx_end = priv->rx + priv->len; |
| 433 | |
| 434 | /* Disable controller before writing control registers */ |
Sean Anderson | 934beab | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 435 | dw_write(priv, DW_SPI_SSIENR, 0); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 436 | |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 437 | dev_dbg(dev, "cr0=%08x rx=%p tx=%p len=%d [bytes]\n", cr0, rx, tx, |
| 438 | priv->len); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 439 | /* Reprogram cr0 only if changed */ |
Sean Anderson | 3004034 | 2020-10-16 18:57:47 -0400 | [diff] [blame] | 440 | if (dw_read(priv, DW_SPI_CTRLR0) != cr0) |
| 441 | dw_write(priv, DW_SPI_CTRLR0, cr0); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | * Configure the desired SS (slave select 0...3) in the controller |
| 445 | * The DW SPI controller will activate and deactivate this CS |
| 446 | * automatically. So no cs_activate() etc is needed in this driver. |
| 447 | */ |
| 448 | cs = spi_chip_select(dev); |
Eugeniy Paltsev | 4b5f6c5 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 449 | dw_write(priv, DW_SPI_SER, 1 << cs); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 450 | |
| 451 | /* Enable controller after writing control registers */ |
Sean Anderson | 934beab | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 452 | dw_write(priv, DW_SPI_SSIENR, 1); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 453 | |
| 454 | /* Start transfer in a polling loop */ |
| 455 | ret = poll_transfer(priv); |
| 456 | |
Eugeniy Paltsev | c6b4f03 | 2018-03-22 13:50:43 +0300 | [diff] [blame] | 457 | /* |
| 458 | * Wait for current transmit operation to complete. |
| 459 | * Otherwise if some data still exists in Tx FIFO it can be |
| 460 | * silently flushed, i.e. dropped on disabling of the controller, |
| 461 | * which happens when writing 0 to DW_SPI_SSIENR which happens |
| 462 | * in the beginning of new transfer. |
| 463 | */ |
| 464 | if (readl_poll_timeout(priv->regs + DW_SPI_SR, val, |
Eugeniy Paltsev | 9b14ac5 | 2018-04-19 17:47:41 +0300 | [diff] [blame] | 465 | (val & SR_TF_EMPT) && !(val & SR_BUSY), |
Eugeniy Paltsev | c6b4f03 | 2018-03-22 13:50:43 +0300 | [diff] [blame] | 466 | RX_TIMEOUT * 1000)) { |
| 467 | ret = -ETIMEDOUT; |
| 468 | } |
| 469 | |
Eugeniy Paltsev | bcdcb3e | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 470 | /* Stop the transaction if necessary */ |
| 471 | if (flags & SPI_XFER_END) |
| 472 | external_cs_manage(dev, true); |
| 473 | |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 474 | return ret; |
| 475 | } |
| 476 | |
| 477 | static int dw_spi_set_speed(struct udevice *bus, uint speed) |
| 478 | { |
| 479 | struct dw_spi_platdata *plat = bus->platdata; |
| 480 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 481 | u16 clk_div; |
| 482 | |
| 483 | if (speed > plat->frequency) |
| 484 | speed = plat->frequency; |
| 485 | |
| 486 | /* Disable controller before writing control registers */ |
Sean Anderson | 934beab | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 487 | dw_write(priv, DW_SPI_SSIENR, 0); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 488 | |
| 489 | /* clk_div doesn't support odd number */ |
Eugeniy Paltsev | 58c125b | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 490 | clk_div = priv->bus_clk_rate / speed; |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 491 | clk_div = (clk_div + 1) & 0xfffe; |
Eugeniy Paltsev | 4b5f6c5 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 492 | dw_write(priv, DW_SPI_BAUDR, clk_div); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 493 | |
| 494 | /* Enable controller after writing control registers */ |
Sean Anderson | 934beab | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 495 | dw_write(priv, DW_SPI_SSIENR, 1); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 496 | |
| 497 | priv->freq = speed; |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 498 | dev_dbg(bus, "speed=%d clk_div=%d\n", priv->freq, clk_div); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int dw_spi_set_mode(struct udevice *bus, uint mode) |
| 504 | { |
| 505 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 506 | |
| 507 | /* |
| 508 | * Can't set mode yet. Since this depends on if rx, tx, or |
| 509 | * rx & tx is requested. So we have to defer this to the |
| 510 | * real transfer function. |
| 511 | */ |
| 512 | priv->mode = mode; |
Sean Anderson | 1b3dd49 | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 513 | dev_dbg(bus, "mode=%d\n", priv->mode); |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Ley Foon Tan | 6ac5909 | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 518 | static int dw_spi_remove(struct udevice *bus) |
| 519 | { |
| 520 | struct dw_spi_priv *priv = dev_get_priv(bus); |
Ley Foon Tan | e7e05fc | 2018-09-19 16:27:19 +0800 | [diff] [blame] | 521 | int ret; |
Ley Foon Tan | 6ac5909 | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 522 | |
Ley Foon Tan | e7e05fc | 2018-09-19 16:27:19 +0800 | [diff] [blame] | 523 | ret = reset_release_bulk(&priv->resets); |
| 524 | if (ret) |
| 525 | return ret; |
| 526 | |
| 527 | #if CONFIG_IS_ENABLED(CLK) |
| 528 | ret = clk_disable(&priv->clk); |
| 529 | if (ret) |
| 530 | return ret; |
| 531 | |
| 532 | ret = clk_free(&priv->clk); |
| 533 | if (ret) |
| 534 | return ret; |
| 535 | #endif |
| 536 | return 0; |
Ley Foon Tan | 6ac5909 | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 537 | } |
| 538 | |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 539 | static const struct dm_spi_ops dw_spi_ops = { |
| 540 | .xfer = dw_spi_xfer, |
| 541 | .set_speed = dw_spi_set_speed, |
| 542 | .set_mode = dw_spi_set_mode, |
| 543 | /* |
| 544 | * cs_info is not needed, since we require all chip selects to be |
| 545 | * in the device tree explicitly |
| 546 | */ |
| 547 | }; |
| 548 | |
| 549 | static const struct udevice_id dw_spi_ids[] = { |
Marek Vasut | 7411486 | 2014-12-31 20:14:55 +0100 | [diff] [blame] | 550 | { .compatible = "snps,dw-apb-ssi" }, |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 551 | { } |
| 552 | }; |
| 553 | |
| 554 | U_BOOT_DRIVER(dw_spi) = { |
| 555 | .name = "dw_spi", |
| 556 | .id = UCLASS_SPI, |
| 557 | .of_match = dw_spi_ids, |
| 558 | .ops = &dw_spi_ops, |
| 559 | .ofdata_to_platdata = dw_spi_ofdata_to_platdata, |
| 560 | .platdata_auto_alloc_size = sizeof(struct dw_spi_platdata), |
| 561 | .priv_auto_alloc_size = sizeof(struct dw_spi_priv), |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 562 | .probe = dw_spi_probe, |
Ley Foon Tan | 6ac5909 | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 563 | .remove = dw_spi_remove, |
Stefan Roese | 5bef6fd | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 564 | }; |