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