Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stefan Roese | d987ea6 | 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> |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 6 | * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com> |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 7 | * |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 8 | * Very loosely based on the Linux driver: |
| 9 | * drivers/spi/spi-dw.c, which is: |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 10 | * Copyright (c) 2009, Intel Corporation. |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 13 | #define LOG_CATEGORY UCLASS_SPI |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 14 | #include <common.h> |
Eugeniy Paltsev | 8b841e3 | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 15 | #include <clk.h> |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 16 | #include <dm.h> |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 17 | #include <dm/device_compat.h> |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 18 | #include <errno.h> |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 19 | #include <fdtdec.h> |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 20 | #include <log.h> |
| 21 | #include <malloc.h> |
Ley Foon Tan | fc3382d | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 22 | #include <reset.h> |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 23 | #include <spi.h> |
| 24 | #include <spi-mem.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm-generic/gpio.h> |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 27 | #include <linux/bitfield.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 28 | #include <linux/bitops.h> |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 29 | #include <linux/compat.h> |
Eugeniy Paltsev | 7215ad2 | 2018-03-22 13:50:43 +0300 | [diff] [blame] | 30 | #include <linux/iopoll.h> |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 31 | #include <linux/sizes.h> |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 32 | |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 33 | /* Register offsets */ |
Sean Anderson | 75ea2f6 | 2020-10-16 18:57:47 -0400 | [diff] [blame] | 34 | #define DW_SPI_CTRLR0 0x00 |
| 35 | #define DW_SPI_CTRLR1 0x04 |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 36 | #define DW_SPI_SSIENR 0x08 |
| 37 | #define DW_SPI_MWCR 0x0c |
| 38 | #define DW_SPI_SER 0x10 |
| 39 | #define DW_SPI_BAUDR 0x14 |
Sean Anderson | 75ea2f6 | 2020-10-16 18:57:47 -0400 | [diff] [blame] | 40 | #define DW_SPI_TXFTLR 0x18 |
| 41 | #define DW_SPI_RXFTLR 0x1c |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 42 | #define DW_SPI_TXFLR 0x20 |
| 43 | #define DW_SPI_RXFLR 0x24 |
| 44 | #define DW_SPI_SR 0x28 |
| 45 | #define DW_SPI_IMR 0x2c |
| 46 | #define DW_SPI_ISR 0x30 |
| 47 | #define DW_SPI_RISR 0x34 |
| 48 | #define DW_SPI_TXOICR 0x38 |
| 49 | #define DW_SPI_RXOICR 0x3c |
| 50 | #define DW_SPI_RXUICR 0x40 |
| 51 | #define DW_SPI_MSTICR 0x44 |
| 52 | #define DW_SPI_ICR 0x48 |
| 53 | #define DW_SPI_DMACR 0x4c |
| 54 | #define DW_SPI_DMATDLR 0x50 |
| 55 | #define DW_SPI_DMARDLR 0x54 |
| 56 | #define DW_SPI_IDR 0x58 |
| 57 | #define DW_SPI_VERSION 0x5c |
| 58 | #define DW_SPI_DR 0x60 |
| 59 | |
| 60 | /* Bit fields in CTRLR0 */ |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 61 | /* |
| 62 | * Only present when SSI_MAX_XFER_SIZE=16. This is the default, and the only |
| 63 | * option before version 3.23a. |
| 64 | */ |
| 65 | #define CTRLR0_DFS_MASK GENMASK(3, 0) |
| 66 | |
| 67 | #define CTRLR0_FRF_MASK GENMASK(5, 4) |
| 68 | #define CTRLR0_FRF_SPI 0x0 |
| 69 | #define CTRLR0_FRF_SSP 0x1 |
| 70 | #define CTRLR0_FRF_MICROWIRE 0x2 |
| 71 | #define CTRLR0_FRF_RESV 0x3 |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 72 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 73 | #define CTRLR0_MODE_MASK GENMASK(7, 6) |
| 74 | #define CTRLR0_MODE_SCPH 0x1 |
| 75 | #define CTRLR0_MODE_SCPOL 0x2 |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 76 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 77 | #define CTRLR0_TMOD_MASK GENMASK(9, 8) |
| 78 | #define CTRLR0_TMOD_TR 0x0 /* xmit & recv */ |
| 79 | #define CTRLR0_TMOD_TO 0x1 /* xmit only */ |
| 80 | #define CTRLR0_TMOD_RO 0x2 /* recv only */ |
| 81 | #define CTRLR0_TMOD_EPROMREAD 0x3 /* eeprom read mode */ |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 82 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 83 | #define CTRLR0_SLVOE_OFFSET 10 |
| 84 | #define CTRLR0_SRL_OFFSET 11 |
| 85 | #define CTRLR0_CFS_MASK GENMASK(15, 12) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 86 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 87 | /* Only present when SSI_MAX_XFER_SIZE=32 */ |
| 88 | #define CTRLR0_DFS_32_MASK GENMASK(20, 16) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 89 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 90 | /* The next field is only present on versions after 4.00a */ |
| 91 | #define CTRLR0_SPI_FRF_MASK GENMASK(22, 21) |
| 92 | #define CTRLR0_SPI_FRF_BYTE 0x0 |
| 93 | #define CTRLR0_SPI_FRF_DUAL 0x1 |
| 94 | #define CTRLR0_SPI_FRF_QUAD 0x2 |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 95 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 96 | /* Bit fields in CTRLR0 based on DWC_ssi_databook.pdf v1.01a */ |
| 97 | #define DWC_SSI_CTRLR0_DFS_MASK GENMASK(4, 0) |
| 98 | #define DWC_SSI_CTRLR0_FRF_MASK GENMASK(7, 6) |
| 99 | #define DWC_SSI_CTRLR0_MODE_MASK GENMASK(9, 8) |
| 100 | #define DWC_SSI_CTRLR0_TMOD_MASK GENMASK(11, 10) |
| 101 | #define DWC_SSI_CTRLR0_SRL_OFFSET 13 |
| 102 | #define DWC_SSI_CTRLR0_SPI_FRF_MASK GENMASK(23, 22) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 103 | |
| 104 | /* Bit fields in SR, 7 bits */ |
Jagan Teki | fac4491 | 2015-10-23 01:01:36 +0530 | [diff] [blame] | 105 | #define SR_MASK GENMASK(6, 0) /* cover 7 bits */ |
Jagan Teki | b17746d | 2015-10-23 01:36:23 +0530 | [diff] [blame] | 106 | #define SR_BUSY BIT(0) |
| 107 | #define SR_TF_NOT_FULL BIT(1) |
| 108 | #define SR_TF_EMPT BIT(2) |
| 109 | #define SR_RF_NOT_EMPT BIT(3) |
| 110 | #define SR_RF_FULL BIT(4) |
| 111 | #define SR_TX_ERR BIT(5) |
| 112 | #define SR_DCOL BIT(6) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 113 | |
Maksim Kiselev | 6cf5618 | 2023-12-21 13:13:30 +0300 | [diff] [blame^] | 114 | /* Bit field in RISR */ |
| 115 | #define RISR_INT_RXOI BIT(3) |
| 116 | |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 117 | #define RX_TIMEOUT 1000 /* timeout in ms */ |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 118 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 119 | struct dw_spi_plat { |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 120 | s32 frequency; /* Default clock frequency, -1 for none */ |
| 121 | void __iomem *regs; |
| 122 | }; |
| 123 | |
| 124 | struct dw_spi_priv { |
Eugeniy Paltsev | 8b841e3 | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 125 | struct clk clk; |
Sean Anderson | e11c0b1 | 2020-10-16 18:57:49 -0400 | [diff] [blame] | 126 | struct reset_ctl_bulk resets; |
Eugeniy Paltsev | a7b4de1 | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 127 | struct gpio_desc cs_gpio; /* External chip-select gpio */ |
| 128 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 129 | u32 (*update_cr0)(struct dw_spi_priv *priv); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 130 | |
Sean Anderson | e11c0b1 | 2020-10-16 18:57:49 -0400 | [diff] [blame] | 131 | void __iomem *regs; |
| 132 | unsigned long bus_clk_rate; |
| 133 | unsigned int freq; /* Default frequency */ |
| 134 | unsigned int mode; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 135 | |
Sean Anderson | e11c0b1 | 2020-10-16 18:57:49 -0400 | [diff] [blame] | 136 | const void *tx; |
| 137 | const void *tx_end; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 138 | void *rx; |
| 139 | void *rx_end; |
Sean Anderson | e11c0b1 | 2020-10-16 18:57:49 -0400 | [diff] [blame] | 140 | u32 fifo_len; /* depth of the FIFO buffer */ |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 141 | u32 max_xfer; /* Maximum transfer size (in bits) */ |
Ley Foon Tan | fc3382d | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 142 | |
Sean Anderson | e11c0b1 | 2020-10-16 18:57:49 -0400 | [diff] [blame] | 143 | int bits_per_word; |
| 144 | int len; |
| 145 | u8 cs; /* chip select pin */ |
| 146 | u8 tmode; /* TR/TO/RO/EEPROM */ |
| 147 | u8 type; /* SPI/SSP/MicroWire */ |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 148 | }; |
| 149 | |
Eugeniy Paltsev | e0c8923 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 150 | static inline u32 dw_read(struct dw_spi_priv *priv, u32 offset) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 151 | { |
| 152 | return __raw_readl(priv->regs + offset); |
| 153 | } |
| 154 | |
Eugeniy Paltsev | e0c8923 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 155 | static inline void dw_write(struct dw_spi_priv *priv, u32 offset, u32 val) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 156 | { |
| 157 | __raw_writel(val, priv->regs + offset); |
| 158 | } |
| 159 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 160 | static u32 dw_spi_dw16_update_cr0(struct dw_spi_priv *priv) |
| 161 | { |
| 162 | return FIELD_PREP(CTRLR0_DFS_MASK, priv->bits_per_word - 1) |
| 163 | | FIELD_PREP(CTRLR0_FRF_MASK, priv->type) |
| 164 | | FIELD_PREP(CTRLR0_MODE_MASK, priv->mode) |
| 165 | | FIELD_PREP(CTRLR0_TMOD_MASK, priv->tmode); |
| 166 | } |
| 167 | |
| 168 | static u32 dw_spi_dw32_update_cr0(struct dw_spi_priv *priv) |
| 169 | { |
| 170 | return FIELD_PREP(CTRLR0_DFS_32_MASK, priv->bits_per_word - 1) |
| 171 | | FIELD_PREP(CTRLR0_FRF_MASK, priv->type) |
| 172 | | FIELD_PREP(CTRLR0_MODE_MASK, priv->mode) |
| 173 | | FIELD_PREP(CTRLR0_TMOD_MASK, priv->tmode); |
| 174 | } |
| 175 | |
| 176 | static u32 dw_spi_dwc_update_cr0(struct dw_spi_priv *priv) |
| 177 | { |
| 178 | return FIELD_PREP(DWC_SSI_CTRLR0_DFS_MASK, priv->bits_per_word - 1) |
| 179 | | FIELD_PREP(DWC_SSI_CTRLR0_FRF_MASK, priv->type) |
| 180 | | FIELD_PREP(DWC_SSI_CTRLR0_MODE_MASK, priv->mode) |
| 181 | | FIELD_PREP(DWC_SSI_CTRLR0_TMOD_MASK, priv->tmode); |
| 182 | } |
| 183 | |
| 184 | static int dw_spi_apb_init(struct udevice *bus, struct dw_spi_priv *priv) |
| 185 | { |
| 186 | /* If we read zeros from DFS, then we need to use DFS_32 instead */ |
| 187 | dw_write(priv, DW_SPI_SSIENR, 0); |
| 188 | dw_write(priv, DW_SPI_CTRLR0, 0xffffffff); |
| 189 | if (FIELD_GET(CTRLR0_DFS_MASK, dw_read(priv, DW_SPI_CTRLR0))) { |
| 190 | priv->max_xfer = 16; |
| 191 | priv->update_cr0 = dw_spi_dw16_update_cr0; |
| 192 | } else { |
| 193 | priv->max_xfer = 32; |
| 194 | priv->update_cr0 = dw_spi_dw32_update_cr0; |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
Damien Le Moal | 96b2bdd | 2022-03-01 10:35:43 +0000 | [diff] [blame] | 200 | static int dw_spi_apb_k210_init(struct udevice *bus, struct dw_spi_priv *priv) |
| 201 | { |
| 202 | /* |
| 203 | * The Canaan Kendryte K210 SoC DW apb_ssi v4 spi controller is |
| 204 | * documented to have a 32 word deep TX and RX FIFO, which |
| 205 | * spi_hw_init() detects. However, when the RX FIFO is filled up to |
| 206 | * 32 entries (RXFLR = 32), an RX FIFO overrun error occurs. Avoid |
| 207 | * this problem by force setting fifo_len to 31. |
| 208 | */ |
| 209 | priv->fifo_len = 31; |
| 210 | |
| 211 | return dw_spi_apb_init(bus, priv); |
| 212 | } |
| 213 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 214 | static int dw_spi_dwc_init(struct udevice *bus, struct dw_spi_priv *priv) |
| 215 | { |
| 216 | priv->max_xfer = 32; |
| 217 | priv->update_cr0 = dw_spi_dwc_update_cr0; |
| 218 | return 0; |
| 219 | } |
| 220 | |
Eugeniy Paltsev | a7b4de1 | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 221 | static int request_gpio_cs(struct udevice *bus) |
| 222 | { |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 223 | #if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD) |
Eugeniy Paltsev | a7b4de1 | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 224 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 225 | int ret; |
| 226 | |
| 227 | /* External chip select gpio line is optional */ |
Sean Anderson | 17f69fb | 2020-10-16 18:57:45 -0400 | [diff] [blame] | 228 | ret = gpio_request_by_name(bus, "cs-gpios", 0, &priv->cs_gpio, |
| 229 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
Eugeniy Paltsev | a7b4de1 | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 230 | if (ret == -ENOENT) |
| 231 | return 0; |
| 232 | |
| 233 | if (ret < 0) { |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 234 | dev_err(bus, "Couldn't request gpio! (error %d)\n", ret); |
Eugeniy Paltsev | a7b4de1 | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | if (dm_gpio_is_valid(&priv->cs_gpio)) { |
| 239 | dm_gpio_set_dir_flags(&priv->cs_gpio, |
| 240 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
| 241 | } |
| 242 | |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 243 | dev_dbg(bus, "Using external gpio for CS management\n"); |
Eugeniy Paltsev | a7b4de1 | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 244 | #endif |
| 245 | return 0; |
| 246 | } |
| 247 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 248 | static int dw_spi_of_to_plat(struct udevice *bus) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 249 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 250 | struct dw_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 251 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 252 | plat->regs = dev_read_addr_ptr(bus); |
Sean Anderson | 72097a7 | 2020-10-16 18:57:46 -0400 | [diff] [blame] | 253 | if (!plat->regs) |
| 254 | return -EINVAL; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 255 | |
| 256 | /* Use 500KHz as a suitable default */ |
Simon Goldschmidt | 70cd31b | 2019-05-09 22:11:57 +0200 | [diff] [blame] | 257 | plat->frequency = dev_read_u32_default(bus, "spi-max-frequency", |
| 258 | 500000); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 259 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 260 | if (dev_read_bool(bus, "spi-slave")) |
| 261 | return -EINVAL; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 262 | |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 263 | dev_info(bus, "max-frequency=%d\n", plat->frequency); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 264 | |
| 265 | return request_gpio_cs(bus); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* Restart the controller, disable all interrupts, clean rx fifo */ |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 269 | static void spi_hw_init(struct udevice *bus, struct dw_spi_priv *priv) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 270 | { |
Sean Anderson | f252082 | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 271 | dw_write(priv, DW_SPI_SSIENR, 0); |
Sean Anderson | 5a6f455 | 2022-03-01 10:35:43 +0000 | [diff] [blame] | 272 | dw_write(priv, DW_SPI_IMR, 0); |
Sean Anderson | f252082 | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 273 | dw_write(priv, DW_SPI_SSIENR, 1); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * Try to detect the FIFO depth if not set by interface driver, |
| 277 | * the depth could be from 2 to 256 from HW spec |
| 278 | */ |
| 279 | if (!priv->fifo_len) { |
| 280 | u32 fifo; |
| 281 | |
Axel Lin | 83cfd37 | 2015-02-26 10:45:22 +0800 | [diff] [blame] | 282 | for (fifo = 1; fifo < 256; fifo++) { |
Sean Anderson | 75ea2f6 | 2020-10-16 18:57:47 -0400 | [diff] [blame] | 283 | dw_write(priv, DW_SPI_TXFTLR, fifo); |
| 284 | if (fifo != dw_read(priv, DW_SPI_TXFTLR)) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | |
Axel Lin | 83cfd37 | 2015-02-26 10:45:22 +0800 | [diff] [blame] | 288 | priv->fifo_len = (fifo == 1) ? 0 : fifo; |
Sean Anderson | 75ea2f6 | 2020-10-16 18:57:47 -0400 | [diff] [blame] | 289 | dw_write(priv, DW_SPI_TXFTLR, 0); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 290 | } |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 291 | dev_dbg(bus, "fifo_len=%d\n", priv->fifo_len); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Eugeniy Paltsev | 8b841e3 | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 294 | /* |
| 295 | * We define dw_spi_get_clk function as 'weak' as some targets |
| 296 | * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API |
| 297 | * and implement dw_spi_get_clk their own way in their clock manager. |
| 298 | */ |
| 299 | __weak int dw_spi_get_clk(struct udevice *bus, ulong *rate) |
| 300 | { |
| 301 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 302 | int ret; |
| 303 | |
| 304 | ret = clk_get_by_index(bus, 0, &priv->clk); |
| 305 | if (ret) |
| 306 | return ret; |
| 307 | |
| 308 | ret = clk_enable(&priv->clk); |
| 309 | if (ret && ret != -ENOSYS && ret != -ENOTSUPP) |
| 310 | return ret; |
| 311 | |
| 312 | *rate = clk_get_rate(&priv->clk); |
| 313 | if (!*rate) |
| 314 | goto err_rate; |
| 315 | |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 316 | dev_dbg(bus, "Got clock via device tree: %lu Hz\n", *rate); |
Eugeniy Paltsev | 8b841e3 | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 317 | |
| 318 | return 0; |
| 319 | |
| 320 | err_rate: |
| 321 | clk_disable(&priv->clk); |
| 322 | clk_free(&priv->clk); |
| 323 | |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
Ley Foon Tan | fc3382d | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 327 | static int dw_spi_reset(struct udevice *bus) |
| 328 | { |
| 329 | int ret; |
| 330 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 331 | |
| 332 | ret = reset_get_bulk(bus, &priv->resets); |
| 333 | if (ret) { |
| 334 | /* |
| 335 | * Return 0 if error due to !CONFIG_DM_RESET and reset |
| 336 | * DT property is not present. |
| 337 | */ |
| 338 | if (ret == -ENOENT || ret == -ENOTSUPP) |
| 339 | return 0; |
| 340 | |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 341 | dev_warn(bus, "Couldn't find/assert reset device (error %d)\n", |
| 342 | ret); |
Ley Foon Tan | fc3382d | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | ret = reset_deassert_bulk(&priv->resets); |
| 347 | if (ret) { |
| 348 | reset_release_bulk(&priv->resets); |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 349 | dev_err(bus, "Failed to de-assert reset for SPI (error %d)\n", |
| 350 | ret); |
Ley Foon Tan | fc3382d | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 357 | typedef int (*dw_spi_init_t)(struct udevice *bus, struct dw_spi_priv *priv); |
| 358 | |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 359 | static int dw_spi_probe(struct udevice *bus) |
| 360 | { |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 361 | dw_spi_init_t init = (dw_spi_init_t)dev_get_driver_data(bus); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 362 | struct dw_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 363 | struct dw_spi_priv *priv = dev_get_priv(bus); |
Eugeniy Paltsev | 8b841e3 | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 364 | int ret; |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 365 | u32 version; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 366 | |
| 367 | priv->regs = plat->regs; |
| 368 | priv->freq = plat->frequency; |
| 369 | |
Eugeniy Paltsev | 8b841e3 | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 370 | ret = dw_spi_get_clk(bus, &priv->bus_clk_rate); |
| 371 | if (ret) |
| 372 | return ret; |
| 373 | |
Ley Foon Tan | fc3382d | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 374 | ret = dw_spi_reset(bus); |
| 375 | if (ret) |
| 376 | return ret; |
| 377 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 378 | if (!init) |
| 379 | return -EINVAL; |
| 380 | ret = init(bus, priv); |
| 381 | if (ret) |
| 382 | return ret; |
| 383 | |
| 384 | version = dw_read(priv, DW_SPI_VERSION); |
| 385 | dev_dbg(bus, "ssi_version_id=%c.%c%c%c ssi_max_xfer_size=%u\n", |
| 386 | version >> 24, version >> 16, version >> 8, version, |
| 387 | priv->max_xfer); |
| 388 | |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 389 | /* Currently only bits_per_word == 8 supported */ |
| 390 | priv->bits_per_word = 8; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 391 | |
| 392 | priv->tmode = 0; /* Tx & Rx */ |
| 393 | |
| 394 | /* Basic HW init */ |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 395 | spi_hw_init(bus, priv); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | /* Return the max entries we can fill into tx fifo */ |
| 401 | static inline u32 tx_max(struct dw_spi_priv *priv) |
| 402 | { |
| 403 | u32 tx_left, tx_room, rxtx_gap; |
| 404 | |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 405 | tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3); |
Eugeniy Paltsev | e0c8923 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 406 | tx_room = priv->fifo_len - dw_read(priv, DW_SPI_TXFLR); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | * Another concern is about the tx/rx mismatch, we |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 410 | * thought about using (priv->fifo_len - rxflr - txflr) as |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 411 | * one maximum value for tx, but it doesn't cover the |
| 412 | * data which is out of tx/rx fifo and inside the |
| 413 | * shift registers. So a control from sw point of |
| 414 | * view is taken. |
| 415 | */ |
| 416 | rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) / |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 417 | (priv->bits_per_word >> 3); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 418 | |
| 419 | return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap)); |
| 420 | } |
| 421 | |
| 422 | /* Return the max entries we should read out of rx fifo */ |
| 423 | static inline u32 rx_max(struct dw_spi_priv *priv) |
| 424 | { |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 425 | u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 426 | |
Eugeniy Paltsev | e0c8923 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 427 | return min_t(u32, rx_left, dw_read(priv, DW_SPI_RXFLR)); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static void dw_writer(struct dw_spi_priv *priv) |
| 431 | { |
| 432 | u32 max = tx_max(priv); |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 433 | u32 txw = 0xFFFFFFFF; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 434 | |
| 435 | while (max--) { |
| 436 | /* Set the tx word if the transfer's original "tx" is not null */ |
| 437 | if (priv->tx_end - priv->len) { |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 438 | if (priv->bits_per_word == 8) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 439 | txw = *(u8 *)(priv->tx); |
| 440 | else |
| 441 | txw = *(u16 *)(priv->tx); |
| 442 | } |
Eugeniy Paltsev | e0c8923 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 443 | dw_write(priv, DW_SPI_DR, txw); |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 444 | log_content("tx=0x%02x\n", txw); |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 445 | priv->tx += priv->bits_per_word >> 3; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Eugeniy Paltsev | c5c6d45 | 2018-03-22 13:50:45 +0300 | [diff] [blame] | 449 | static void dw_reader(struct dw_spi_priv *priv) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 450 | { |
Eugeniy Paltsev | c5c6d45 | 2018-03-22 13:50:45 +0300 | [diff] [blame] | 451 | u32 max = rx_max(priv); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 452 | u16 rxw; |
| 453 | |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 454 | while (max--) { |
Eugeniy Paltsev | e0c8923 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 455 | rxw = dw_read(priv, DW_SPI_DR); |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 456 | log_content("rx=0x%02x\n", rxw); |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 457 | |
Eugeniy Paltsev | c5c6d45 | 2018-03-22 13:50:45 +0300 | [diff] [blame] | 458 | /* Care about rx if the transfer's original "rx" is not null */ |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 459 | if (priv->rx_end - priv->len) { |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 460 | if (priv->bits_per_word == 8) |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 461 | *(u8 *)(priv->rx) = rxw; |
| 462 | else |
| 463 | *(u16 *)(priv->rx) = rxw; |
| 464 | } |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 465 | priv->rx += priv->bits_per_word >> 3; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 466 | } |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static int poll_transfer(struct dw_spi_priv *priv) |
| 470 | { |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 471 | do { |
| 472 | dw_writer(priv); |
Eugeniy Paltsev | c5c6d45 | 2018-03-22 13:50:45 +0300 | [diff] [blame] | 473 | dw_reader(priv); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 474 | } while (priv->rx_end > priv->rx); |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
Gregory CLEMENT | f289337 | 2018-10-09 14:14:07 +0200 | [diff] [blame] | 479 | /* |
| 480 | * We define external_cs_manage function as 'weak' as some targets |
| 481 | * (like MSCC Ocelot) don't control the external CS pin using a GPIO |
| 482 | * controller. These SoCs use specific registers to control by |
| 483 | * software the SPI pins (and especially the CS). |
| 484 | */ |
| 485 | __weak void external_cs_manage(struct udevice *dev, bool on) |
Eugeniy Paltsev | a7b4de1 | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 486 | { |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 487 | #if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD) |
Eugeniy Paltsev | a7b4de1 | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 488 | struct dw_spi_priv *priv = dev_get_priv(dev->parent); |
| 489 | |
| 490 | if (!dm_gpio_is_valid(&priv->cs_gpio)) |
| 491 | return; |
| 492 | |
| 493 | dm_gpio_set_value(&priv->cs_gpio, on ? 1 : 0); |
| 494 | #endif |
| 495 | } |
| 496 | |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 497 | static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 498 | const void *dout, void *din, unsigned long flags) |
| 499 | { |
| 500 | struct udevice *bus = dev->parent; |
| 501 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 502 | const u8 *tx = dout; |
| 503 | u8 *rx = din; |
| 504 | int ret = 0; |
| 505 | u32 cr0 = 0; |
Eugeniy Paltsev | 7215ad2 | 2018-03-22 13:50:43 +0300 | [diff] [blame] | 506 | u32 val; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 507 | u32 cs; |
| 508 | |
| 509 | /* spi core configured to do 8 bit transfers */ |
| 510 | if (bitlen % 8) { |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 511 | dev_err(dev, "Non byte aligned SPI transfer.\n"); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 512 | return -1; |
| 513 | } |
| 514 | |
Eugeniy Paltsev | a7b4de1 | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 515 | /* Start the transaction if necessary. */ |
| 516 | if (flags & SPI_XFER_BEGIN) |
| 517 | external_cs_manage(dev, false); |
| 518 | |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 519 | if (rx && tx) |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 520 | priv->tmode = CTRLR0_TMOD_TR; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 521 | else if (rx) |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 522 | priv->tmode = CTRLR0_TMOD_RO; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 523 | else |
Eugeniy Paltsev | 31f5013 | 2018-03-22 13:50:44 +0300 | [diff] [blame] | 524 | /* |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 525 | * In transmit only mode (CTRL0_TMOD_TO) input FIFO never gets |
Eugeniy Paltsev | 31f5013 | 2018-03-22 13:50:44 +0300 | [diff] [blame] | 526 | * any data which breaks our logic in poll_transfer() above. |
| 527 | */ |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 528 | priv->tmode = CTRLR0_TMOD_TR; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 529 | |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 530 | cr0 = priv->update_cr0(priv); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 531 | |
Stefan Roese | 571e2a4 | 2014-11-16 12:47:01 +0100 | [diff] [blame] | 532 | priv->len = bitlen >> 3; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 533 | |
| 534 | priv->tx = (void *)tx; |
| 535 | priv->tx_end = priv->tx + priv->len; |
| 536 | priv->rx = rx; |
| 537 | priv->rx_end = priv->rx + priv->len; |
| 538 | |
| 539 | /* Disable controller before writing control registers */ |
Sean Anderson | f252082 | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 540 | dw_write(priv, DW_SPI_SSIENR, 0); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 541 | |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 542 | dev_dbg(dev, "cr0=%08x rx=%p tx=%p len=%d [bytes]\n", cr0, rx, tx, |
| 543 | priv->len); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 544 | /* Reprogram cr0 only if changed */ |
Sean Anderson | 75ea2f6 | 2020-10-16 18:57:47 -0400 | [diff] [blame] | 545 | if (dw_read(priv, DW_SPI_CTRLR0) != cr0) |
| 546 | dw_write(priv, DW_SPI_CTRLR0, cr0); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 547 | |
| 548 | /* |
| 549 | * Configure the desired SS (slave select 0...3) in the controller |
| 550 | * The DW SPI controller will activate and deactivate this CS |
| 551 | * automatically. So no cs_activate() etc is needed in this driver. |
| 552 | */ |
| 553 | cs = spi_chip_select(dev); |
Eugeniy Paltsev | e0c8923 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 554 | dw_write(priv, DW_SPI_SER, 1 << cs); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 555 | |
| 556 | /* Enable controller after writing control registers */ |
Sean Anderson | f252082 | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 557 | dw_write(priv, DW_SPI_SSIENR, 1); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 558 | |
| 559 | /* Start transfer in a polling loop */ |
| 560 | ret = poll_transfer(priv); |
| 561 | |
Eugeniy Paltsev | 7215ad2 | 2018-03-22 13:50:43 +0300 | [diff] [blame] | 562 | /* |
| 563 | * Wait for current transmit operation to complete. |
| 564 | * Otherwise if some data still exists in Tx FIFO it can be |
| 565 | * silently flushed, i.e. dropped on disabling of the controller, |
| 566 | * which happens when writing 0 to DW_SPI_SSIENR which happens |
| 567 | * in the beginning of new transfer. |
| 568 | */ |
| 569 | if (readl_poll_timeout(priv->regs + DW_SPI_SR, val, |
Eugeniy Paltsev | 208be8f | 2018-04-19 17:47:41 +0300 | [diff] [blame] | 570 | (val & SR_TF_EMPT) && !(val & SR_BUSY), |
Eugeniy Paltsev | 7215ad2 | 2018-03-22 13:50:43 +0300 | [diff] [blame] | 571 | RX_TIMEOUT * 1000)) { |
| 572 | ret = -ETIMEDOUT; |
| 573 | } |
| 574 | |
Eugeniy Paltsev | a7b4de1 | 2018-03-22 13:50:46 +0300 | [diff] [blame] | 575 | /* Stop the transaction if necessary */ |
| 576 | if (flags & SPI_XFER_END) |
| 577 | external_cs_manage(dev, true); |
| 578 | |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 579 | return ret; |
| 580 | } |
| 581 | |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 582 | /* |
| 583 | * This function is necessary for reading SPI flash with the native CS |
| 584 | * c.f. https://lkml.org/lkml/2015/12/23/132 |
| 585 | */ |
| 586 | static int dw_spi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op) |
| 587 | { |
| 588 | bool read = op->data.dir == SPI_MEM_DATA_IN; |
| 589 | int pos, i, ret = 0; |
| 590 | struct udevice *bus = slave->dev->parent; |
| 591 | struct dw_spi_priv *priv = dev_get_priv(bus); |
Niklas Cassel | b7e9207 | 2022-02-08 22:52:43 +0000 | [diff] [blame] | 592 | u8 op_len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 593 | u8 op_buf[op_len]; |
Maksim Kiselev | 6cf5618 | 2023-12-21 13:13:30 +0300 | [diff] [blame^] | 594 | u32 cr0, sts; |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 595 | |
| 596 | if (read) |
| 597 | priv->tmode = CTRLR0_TMOD_EPROMREAD; |
| 598 | else |
| 599 | priv->tmode = CTRLR0_TMOD_TO; |
| 600 | |
| 601 | cr0 = priv->update_cr0(priv); |
| 602 | dev_dbg(bus, "cr0=%08x buf=%p len=%u [bytes]\n", cr0, op->data.buf.in, |
| 603 | op->data.nbytes); |
| 604 | |
| 605 | dw_write(priv, DW_SPI_SSIENR, 0); |
| 606 | dw_write(priv, DW_SPI_CTRLR0, cr0); |
| 607 | if (read) |
| 608 | dw_write(priv, DW_SPI_CTRLR1, op->data.nbytes - 1); |
| 609 | dw_write(priv, DW_SPI_SSIENR, 1); |
| 610 | |
| 611 | /* From spi_mem_exec_op */ |
| 612 | pos = 0; |
| 613 | op_buf[pos++] = op->cmd.opcode; |
| 614 | if (op->addr.nbytes) { |
| 615 | for (i = 0; i < op->addr.nbytes; i++) |
| 616 | op_buf[pos + i] = op->addr.val >> |
| 617 | (8 * (op->addr.nbytes - i - 1)); |
| 618 | |
| 619 | pos += op->addr.nbytes; |
| 620 | } |
| 621 | if (op->dummy.nbytes) |
| 622 | memset(op_buf + pos, 0xff, op->dummy.nbytes); |
| 623 | |
| 624 | external_cs_manage(slave->dev, false); |
| 625 | |
| 626 | priv->tx = &op_buf; |
| 627 | priv->tx_end = priv->tx + op_len; |
| 628 | priv->rx = NULL; |
| 629 | priv->rx_end = NULL; |
| 630 | while (priv->tx != priv->tx_end) |
| 631 | dw_writer(priv); |
| 632 | |
| 633 | /* |
| 634 | * XXX: The following are tight loops! Enabling debug messages may cause |
| 635 | * them to fail because we are not reading/writing the fifo fast enough. |
| 636 | */ |
| 637 | if (read) { |
Maksim Kiselev | 6cf5618 | 2023-12-21 13:13:30 +0300 | [diff] [blame^] | 638 | void *prev_rx = priv->rx = op->data.buf.in; |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 639 | priv->rx_end = priv->rx + op->data.nbytes; |
| 640 | |
| 641 | dw_write(priv, DW_SPI_SER, 1 << spi_chip_select(slave->dev)); |
Maksim Kiselev | 6cf5618 | 2023-12-21 13:13:30 +0300 | [diff] [blame^] | 642 | while (priv->rx != priv->rx_end) { |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 643 | dw_reader(priv); |
Maksim Kiselev | 6cf5618 | 2023-12-21 13:13:30 +0300 | [diff] [blame^] | 644 | if (prev_rx == priv->rx) { |
| 645 | sts = dw_read(priv, DW_SPI_RISR); |
| 646 | if (sts & RISR_INT_RXOI) { |
| 647 | dev_err(bus, "FIFO overflow on Rx\n"); |
| 648 | return -EIO; |
| 649 | } |
| 650 | } |
| 651 | prev_rx = priv->rx; |
| 652 | } |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 653 | } else { |
| 654 | u32 val; |
| 655 | |
| 656 | priv->tx = op->data.buf.out; |
| 657 | priv->tx_end = priv->tx + op->data.nbytes; |
| 658 | |
| 659 | /* Fill up the write fifo before starting the transfer */ |
| 660 | dw_writer(priv); |
| 661 | dw_write(priv, DW_SPI_SER, 1 << spi_chip_select(slave->dev)); |
| 662 | while (priv->tx != priv->tx_end) |
| 663 | dw_writer(priv); |
| 664 | |
| 665 | if (readl_poll_timeout(priv->regs + DW_SPI_SR, val, |
| 666 | (val & SR_TF_EMPT) && !(val & SR_BUSY), |
| 667 | RX_TIMEOUT * 1000)) { |
| 668 | ret = -ETIMEDOUT; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | dw_write(priv, DW_SPI_SER, 0); |
| 673 | external_cs_manage(slave->dev, true); |
| 674 | |
| 675 | dev_dbg(bus, "%u bytes xfered\n", op->data.nbytes); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 676 | return ret; |
| 677 | } |
| 678 | |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 679 | /* The size of ctrl1 limits data transfers to 64K */ |
| 680 | static int dw_spi_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op) |
| 681 | { |
| 682 | op->data.nbytes = min(op->data.nbytes, (unsigned int)SZ_64K); |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | static const struct spi_controller_mem_ops dw_spi_mem_ops = { |
| 688 | .exec_op = dw_spi_exec_op, |
| 689 | .adjust_op_size = dw_spi_adjust_op_size, |
| 690 | }; |
| 691 | |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 692 | static int dw_spi_set_speed(struct udevice *bus, uint speed) |
| 693 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 694 | struct dw_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 695 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 696 | u16 clk_div; |
| 697 | |
| 698 | if (speed > plat->frequency) |
| 699 | speed = plat->frequency; |
| 700 | |
| 701 | /* Disable controller before writing control registers */ |
Sean Anderson | f252082 | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 702 | dw_write(priv, DW_SPI_SSIENR, 0); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 703 | |
| 704 | /* clk_div doesn't support odd number */ |
Eugeniy Paltsev | 8b841e3 | 2017-12-28 15:09:03 +0300 | [diff] [blame] | 705 | clk_div = priv->bus_clk_rate / speed; |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 706 | clk_div = (clk_div + 1) & 0xfffe; |
Eugeniy Paltsev | e0c8923 | 2018-03-22 13:50:47 +0300 | [diff] [blame] | 707 | dw_write(priv, DW_SPI_BAUDR, clk_div); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 708 | |
| 709 | /* Enable controller after writing control registers */ |
Sean Anderson | f252082 | 2020-10-16 18:57:48 -0400 | [diff] [blame] | 710 | dw_write(priv, DW_SPI_SSIENR, 1); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 711 | |
| 712 | priv->freq = speed; |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 713 | dev_dbg(bus, "speed=%d clk_div=%d\n", priv->freq, clk_div); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | static int dw_spi_set_mode(struct udevice *bus, uint mode) |
| 719 | { |
| 720 | struct dw_spi_priv *priv = dev_get_priv(bus); |
| 721 | |
| 722 | /* |
| 723 | * Can't set mode yet. Since this depends on if rx, tx, or |
| 724 | * rx & tx is requested. So we have to defer this to the |
| 725 | * real transfer function. |
| 726 | */ |
| 727 | priv->mode = mode; |
Sean Anderson | 0dfb3ac | 2020-10-16 18:57:44 -0400 | [diff] [blame] | 728 | dev_dbg(bus, "mode=%d\n", priv->mode); |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
Ley Foon Tan | fc3382d | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 733 | static int dw_spi_remove(struct udevice *bus) |
| 734 | { |
| 735 | struct dw_spi_priv *priv = dev_get_priv(bus); |
Ley Foon Tan | d95ab40 | 2018-09-19 16:27:19 +0800 | [diff] [blame] | 736 | int ret; |
| 737 | |
| 738 | ret = reset_release_bulk(&priv->resets); |
| 739 | if (ret) |
| 740 | return ret; |
| 741 | |
| 742 | #if CONFIG_IS_ENABLED(CLK) |
| 743 | ret = clk_disable(&priv->clk); |
| 744 | if (ret) |
| 745 | return ret; |
Ley Foon Tan | fc3382d | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 746 | |
Sean Anderson | e0e8142 | 2022-01-15 17:25:03 -0500 | [diff] [blame] | 747 | clk_free(&priv->clk); |
Ley Foon Tan | d95ab40 | 2018-09-19 16:27:19 +0800 | [diff] [blame] | 748 | if (ret) |
| 749 | return ret; |
| 750 | #endif |
| 751 | return 0; |
Ley Foon Tan | fc3382d | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 752 | } |
| 753 | |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 754 | static const struct dm_spi_ops dw_spi_ops = { |
| 755 | .xfer = dw_spi_xfer, |
Sean Anderson | 16edba3 | 2020-10-16 18:57:53 -0400 | [diff] [blame] | 756 | .mem_ops = &dw_spi_mem_ops, |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 757 | .set_speed = dw_spi_set_speed, |
| 758 | .set_mode = dw_spi_set_mode, |
| 759 | /* |
| 760 | * cs_info is not needed, since we require all chip selects to be |
| 761 | * in the device tree explicitly |
| 762 | */ |
| 763 | }; |
| 764 | |
| 765 | static const struct udevice_id dw_spi_ids[] = { |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 766 | /* Generic compatible strings */ |
| 767 | |
| 768 | { .compatible = "snps,dw-apb-ssi", .data = (ulong)dw_spi_apb_init }, |
| 769 | { .compatible = "snps,dw-apb-ssi-3.20a", .data = (ulong)dw_spi_apb_init }, |
| 770 | { .compatible = "snps,dw-apb-ssi-3.22a", .data = (ulong)dw_spi_apb_init }, |
| 771 | /* First version with SSI_MAX_XFER_SIZE */ |
| 772 | { .compatible = "snps,dw-apb-ssi-3.23a", .data = (ulong)dw_spi_apb_init }, |
| 773 | /* First version with Dual/Quad SPI; unused by this driver */ |
| 774 | { .compatible = "snps,dw-apb-ssi-4.00a", .data = (ulong)dw_spi_apb_init }, |
| 775 | { .compatible = "snps,dw-apb-ssi-4.01", .data = (ulong)dw_spi_apb_init }, |
| 776 | { .compatible = "snps,dwc-ssi-1.01a", .data = (ulong)dw_spi_dwc_init }, |
| 777 | |
| 778 | /* Compatible strings for specific SoCs */ |
| 779 | |
| 780 | /* |
| 781 | * Both the Cyclone V and Arria V share a device tree and have the same |
| 782 | * version of this device. This compatible string is used for those |
| 783 | * devices, and is not used for sofpgas in general. |
| 784 | */ |
| 785 | { .compatible = "altr,socfpga-spi", .data = (ulong)dw_spi_apb_init }, |
| 786 | { .compatible = "altr,socfpga-arria10-spi", .data = (ulong)dw_spi_apb_init }, |
Damien Le Moal | 96b2bdd | 2022-03-01 10:35:43 +0000 | [diff] [blame] | 787 | { .compatible = "canaan,k210-spi", .data = (ulong)dw_spi_apb_k210_init}, |
Damien Le Moal | 6e5a8b7 | 2022-03-01 10:35:39 +0000 | [diff] [blame] | 788 | { .compatible = "canaan,k210-ssi", .data = (ulong)dw_spi_dwc_init }, |
Sean Anderson | debbff8 | 2020-10-16 18:57:51 -0400 | [diff] [blame] | 789 | { .compatible = "intel,stratix10-spi", .data = (ulong)dw_spi_apb_init }, |
| 790 | { .compatible = "intel,agilex-spi", .data = (ulong)dw_spi_apb_init }, |
| 791 | { .compatible = "mscc,ocelot-spi", .data = (ulong)dw_spi_apb_init }, |
| 792 | { .compatible = "mscc,jaguar2-spi", .data = (ulong)dw_spi_apb_init }, |
| 793 | { .compatible = "snps,axs10x-spi", .data = (ulong)dw_spi_apb_init }, |
| 794 | { .compatible = "snps,hsdk-spi", .data = (ulong)dw_spi_apb_init }, |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 795 | { } |
| 796 | }; |
| 797 | |
| 798 | U_BOOT_DRIVER(dw_spi) = { |
| 799 | .name = "dw_spi", |
| 800 | .id = UCLASS_SPI, |
| 801 | .of_match = dw_spi_ids, |
| 802 | .ops = &dw_spi_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 803 | .of_to_plat = dw_spi_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 804 | .plat_auto = sizeof(struct dw_spi_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 805 | .priv_auto = sizeof(struct dw_spi_priv), |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 806 | .probe = dw_spi_probe, |
Ley Foon Tan | fc3382d | 2018-09-07 14:25:29 +0800 | [diff] [blame] | 807 | .remove = dw_spi_remove, |
Stefan Roese | d987ea6 | 2014-11-07 13:50:31 +0100 | [diff] [blame] | 808 | }; |