Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2017 Whitebox Systems / Northend Systems B.V. |
| 3 | * S.J.R. van Schaik <stephan@whiteboxsystems.nl> |
| 4 | * M.B.W. Wajer <merlijn@whiteboxsystems.nl> |
| 5 | * |
| 6 | * (C) Copyright 2017 Olimex Ltd.. |
| 7 | * Stefan Mavrodiev <stefan@olimex.com> |
| 8 | * |
| 9 | * Based on linux spi driver. Original copyright follows: |
| 10 | * linux/drivers/spi/spi-sun4i.c |
| 11 | * |
| 12 | * Copyright (C) 2012 - 2014 Allwinner Tech |
| 13 | * Pan Nan <pannan@allwinnertech.com> |
| 14 | * |
| 15 | * Copyright (C) 2014 Maxime Ripard |
| 16 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 17 | * |
| 18 | * SPDX-License-Identifier: GPL-2.0+ |
| 19 | */ |
| 20 | |
| 21 | #include <common.h> |
Jagan Teki | 8d71a19 | 2019-02-27 20:02:10 +0530 | [diff] [blame] | 22 | #include <clk.h> |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 23 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 24 | #include <log.h> |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 25 | #include <spi.h> |
| 26 | #include <errno.h> |
| 27 | #include <fdt_support.h> |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 28 | #include <reset.h> |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 29 | #include <wait_bit.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 30 | #include <asm/global_data.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 31 | #include <dm/device_compat.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 32 | #include <linux/bitops.h> |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 33 | |
| 34 | #include <asm/bitops.h> |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 35 | #include <asm/io.h> |
| 36 | |
Jagan Teki | 6cb6aa6 | 2019-02-27 20:02:05 +0530 | [diff] [blame] | 37 | #include <linux/iopoll.h> |
| 38 | |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 39 | DECLARE_GLOBAL_DATA_PTR; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 40 | |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 41 | /* sun4i spi registers */ |
| 42 | #define SUN4I_RXDATA_REG 0x00 |
| 43 | #define SUN4I_TXDATA_REG 0x04 |
| 44 | #define SUN4I_CTL_REG 0x08 |
| 45 | #define SUN4I_CLK_CTL_REG 0x1c |
| 46 | #define SUN4I_BURST_CNT_REG 0x20 |
| 47 | #define SUN4I_XMIT_CNT_REG 0x24 |
| 48 | #define SUN4I_FIFO_STA_REG 0x28 |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 49 | |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 50 | /* sun6i spi registers */ |
| 51 | #define SUN6I_GBL_CTL_REG 0x04 |
| 52 | #define SUN6I_TFR_CTL_REG 0x08 |
| 53 | #define SUN6I_FIFO_CTL_REG 0x18 |
| 54 | #define SUN6I_FIFO_STA_REG 0x1c |
| 55 | #define SUN6I_CLK_CTL_REG 0x24 |
| 56 | #define SUN6I_BURST_CNT_REG 0x30 |
| 57 | #define SUN6I_XMIT_CNT_REG 0x34 |
| 58 | #define SUN6I_BURST_CTL_REG 0x38 |
| 59 | #define SUN6I_TXDATA_REG 0x200 |
| 60 | #define SUN6I_RXDATA_REG 0x300 |
| 61 | |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 62 | /* sun spi bits */ |
| 63 | #define SUN4I_CTL_ENABLE BIT(0) |
| 64 | #define SUN4I_CTL_MASTER BIT(1) |
| 65 | #define SUN4I_CLK_CTL_CDR2_MASK 0xff |
| 66 | #define SUN4I_CLK_CTL_CDR2(div) ((div) & SUN4I_CLK_CTL_CDR2_MASK) |
| 67 | #define SUN4I_CLK_CTL_CDR1_MASK 0xf |
| 68 | #define SUN4I_CLK_CTL_CDR1(div) (((div) & SUN4I_CLK_CTL_CDR1_MASK) << 8) |
| 69 | #define SUN4I_CLK_CTL_DRS BIT(12) |
| 70 | #define SUN4I_MAX_XFER_SIZE 0xffffff |
| 71 | #define SUN4I_BURST_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE) |
| 72 | #define SUN4I_XMIT_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE) |
| 73 | #define SUN4I_FIFO_STA_RF_CNT_BITS 0 |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 74 | |
Andre Przywara | 8649995 | 2022-04-26 23:58:53 +0100 | [diff] [blame] | 75 | #ifdef CONFIG_MACH_SUNIV |
| 76 | /* the AHB clock, which we programmed to be 1/3 of PLL_PERIPH@600MHz */ |
| 77 | #define SUNXI_INPUT_CLOCK 200000000 /* 200 MHz */ |
| 78 | #define SUN4I_SPI_MAX_RATE (SUNXI_INPUT_CLOCK / 2) |
| 79 | #else |
Andre Przywara | fcd6d93 | 2022-05-03 02:06:37 +0100 | [diff] [blame] | 80 | /* the SPI mod clock, defaulting to be 1/1 of the HOSC@24MHz */ |
| 81 | #define SUNXI_INPUT_CLOCK 24000000 /* 24 MHz */ |
| 82 | #define SUN4I_SPI_MAX_RATE SUNXI_INPUT_CLOCK |
Andre Przywara | 8649995 | 2022-04-26 23:58:53 +0100 | [diff] [blame] | 83 | #endif |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 84 | #define SUN4I_SPI_MIN_RATE 3000 |
| 85 | #define SUN4I_SPI_DEFAULT_RATE 1000000 |
Icenowy Zheng | 56e497e | 2022-06-28 14:49:24 +0800 | [diff] [blame] | 86 | #define SUN4I_SPI_TIMEOUT_MS 1000 |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 87 | |
| 88 | #define SPI_REG(priv, reg) ((priv)->base + \ |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 89 | (priv)->variant->regs[reg]) |
| 90 | #define SPI_BIT(priv, bit) ((priv)->variant->bits[bit]) |
| 91 | #define SPI_CS(priv, cs) (((cs) << SPI_BIT(priv, SPI_TCR_CS_SEL)) & \ |
| 92 | SPI_BIT(priv, SPI_TCR_CS_MASK)) |
| 93 | |
| 94 | /* sun spi register set */ |
| 95 | enum sun4i_spi_regs { |
| 96 | SPI_GCR, |
| 97 | SPI_TCR, |
| 98 | SPI_FCR, |
| 99 | SPI_FSR, |
| 100 | SPI_CCR, |
| 101 | SPI_BC, |
| 102 | SPI_TC, |
| 103 | SPI_BCTL, |
| 104 | SPI_TXD, |
| 105 | SPI_RXD, |
| 106 | }; |
| 107 | |
| 108 | /* sun spi register bits */ |
| 109 | enum sun4i_spi_bits { |
| 110 | SPI_GCR_TP, |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 111 | SPI_GCR_SRST, |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 112 | SPI_TCR_CPHA, |
| 113 | SPI_TCR_CPOL, |
| 114 | SPI_TCR_CS_ACTIVE_LOW, |
| 115 | SPI_TCR_CS_SEL, |
| 116 | SPI_TCR_CS_MASK, |
| 117 | SPI_TCR_XCH, |
| 118 | SPI_TCR_CS_MANUAL, |
| 119 | SPI_TCR_CS_LEVEL, |
| 120 | SPI_FCR_TF_RST, |
| 121 | SPI_FCR_RF_RST, |
| 122 | SPI_FSR_RF_CNT_MASK, |
| 123 | }; |
| 124 | |
| 125 | struct sun4i_spi_variant { |
| 126 | const unsigned long *regs; |
| 127 | const u32 *bits; |
Jagan Teki | 178fbd2 | 2019-02-27 20:02:09 +0530 | [diff] [blame] | 128 | u32 fifo_depth; |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 129 | bool has_soft_reset; |
| 130 | bool has_burst_ctl; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 131 | }; |
| 132 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 133 | struct sun4i_spi_plat { |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 134 | struct sun4i_spi_variant *variant; |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 135 | u32 base; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 136 | u32 max_hz; |
| 137 | }; |
| 138 | |
| 139 | struct sun4i_spi_priv { |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 140 | struct sun4i_spi_variant *variant; |
Jagan Teki | 8d71a19 | 2019-02-27 20:02:10 +0530 | [diff] [blame] | 141 | struct clk clk_ahb, clk_mod; |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 142 | struct reset_ctl reset; |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 143 | u32 base; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 144 | u32 freq; |
| 145 | u32 mode; |
| 146 | |
| 147 | const u8 *tx_buf; |
| 148 | u8 *rx_buf; |
| 149 | }; |
| 150 | |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 151 | static inline void sun4i_spi_drain_fifo(struct sun4i_spi_priv *priv, int len) |
| 152 | { |
| 153 | u8 byte; |
| 154 | |
| 155 | while (len--) { |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 156 | byte = readb(SPI_REG(priv, SPI_RXD)); |
Stefan Mavrodiev | 5c1a87d | 2018-12-05 14:27:57 +0200 | [diff] [blame] | 157 | if (priv->rx_buf) |
| 158 | *priv->rx_buf++ = byte; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | static inline void sun4i_spi_fill_fifo(struct sun4i_spi_priv *priv, int len) |
| 163 | { |
| 164 | u8 byte; |
| 165 | |
| 166 | while (len--) { |
| 167 | byte = priv->tx_buf ? *priv->tx_buf++ : 0; |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 168 | writeb(byte, SPI_REG(priv, SPI_TXD)); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | static void sun4i_spi_set_cs(struct udevice *bus, u8 cs, bool enable) |
| 173 | { |
| 174 | struct sun4i_spi_priv *priv = dev_get_priv(bus); |
| 175 | u32 reg; |
| 176 | |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 177 | reg = readl(SPI_REG(priv, SPI_TCR)); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 178 | |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 179 | reg &= ~SPI_BIT(priv, SPI_TCR_CS_MASK); |
| 180 | reg |= SPI_CS(priv, cs); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 181 | |
| 182 | if (enable) |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 183 | reg &= ~SPI_BIT(priv, SPI_TCR_CS_LEVEL); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 184 | else |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 185 | reg |= SPI_BIT(priv, SPI_TCR_CS_LEVEL); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 186 | |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 187 | writel(reg, SPI_REG(priv, SPI_TCR)); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Jagan Teki | 8d71a19 | 2019-02-27 20:02:10 +0530 | [diff] [blame] | 190 | static inline int sun4i_spi_set_clock(struct udevice *dev, bool enable) |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 191 | { |
Jagan Teki | 8d71a19 | 2019-02-27 20:02:10 +0530 | [diff] [blame] | 192 | struct sun4i_spi_priv *priv = dev_get_priv(dev); |
| 193 | int ret; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 194 | |
Jagan Teki | 8d71a19 | 2019-02-27 20:02:10 +0530 | [diff] [blame] | 195 | if (!enable) { |
| 196 | clk_disable(&priv->clk_ahb); |
| 197 | clk_disable(&priv->clk_mod); |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 198 | if (reset_valid(&priv->reset)) |
| 199 | reset_assert(&priv->reset); |
Jagan Teki | 8d71a19 | 2019-02-27 20:02:10 +0530 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | ret = clk_enable(&priv->clk_ahb); |
| 204 | if (ret) { |
| 205 | dev_err(dev, "failed to enable ahb clock (ret=%d)\n", ret); |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | ret = clk_enable(&priv->clk_mod); |
| 210 | if (ret) { |
| 211 | dev_err(dev, "failed to enable mod clock (ret=%d)\n", ret); |
| 212 | goto err_ahb; |
| 213 | } |
| 214 | |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 215 | if (reset_valid(&priv->reset)) { |
| 216 | ret = reset_deassert(&priv->reset); |
| 217 | if (ret) { |
| 218 | dev_err(dev, "failed to deassert reset\n"); |
| 219 | goto err_mod; |
| 220 | } |
| 221 | } |
| 222 | |
Jagan Teki | 8d71a19 | 2019-02-27 20:02:10 +0530 | [diff] [blame] | 223 | return 0; |
| 224 | |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 225 | err_mod: |
| 226 | clk_disable(&priv->clk_mod); |
Jagan Teki | 8d71a19 | 2019-02-27 20:02:10 +0530 | [diff] [blame] | 227 | err_ahb: |
| 228 | clk_disable(&priv->clk_ahb); |
| 229 | return ret; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Andre Przywara | 239dfd1 | 2022-05-03 00:07:16 +0100 | [diff] [blame] | 232 | static void sun4i_spi_set_speed_mode(struct udevice *dev) |
| 233 | { |
| 234 | struct sun4i_spi_priv *priv = dev_get_priv(dev); |
| 235 | unsigned int div; |
| 236 | u32 reg; |
| 237 | |
| 238 | /* |
| 239 | * Setup clock divider. |
| 240 | * |
| 241 | * We have two choices there. Either we can use the clock |
| 242 | * divide rate 1, which is calculated thanks to this formula: |
| 243 | * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1)) |
| 244 | * Or we can use CDR2, which is calculated with the formula: |
| 245 | * SPI_CLK = MOD_CLK / (2 * (cdr + 1)) |
| 246 | * Whether we use the former or the latter is set through the |
| 247 | * DRS bit. |
| 248 | * |
| 249 | * First try CDR2, and if we can't reach the expected |
| 250 | * frequency, fall back to CDR1. |
| 251 | */ |
| 252 | |
Andre Przywara | fcd6d93 | 2022-05-03 02:06:37 +0100 | [diff] [blame] | 253 | div = DIV_ROUND_UP(SUNXI_INPUT_CLOCK, priv->freq); |
Andre Przywara | 239dfd1 | 2022-05-03 00:07:16 +0100 | [diff] [blame] | 254 | reg = readl(SPI_REG(priv, SPI_CCR)); |
| 255 | |
Andre Przywara | fcd6d93 | 2022-05-03 02:06:37 +0100 | [diff] [blame] | 256 | if ((div / 2) <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) { |
| 257 | div /= 2; |
Andre Przywara | 239dfd1 | 2022-05-03 00:07:16 +0100 | [diff] [blame] | 258 | if (div > 0) |
| 259 | div--; |
| 260 | |
| 261 | reg &= ~(SUN4I_CLK_CTL_CDR2_MASK | SUN4I_CLK_CTL_DRS); |
| 262 | reg |= SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS; |
| 263 | } else { |
Andre Przywara | fcd6d93 | 2022-05-03 02:06:37 +0100 | [diff] [blame] | 264 | div = fls(div - 1); |
Andre Przywara | 8649995 | 2022-04-26 23:58:53 +0100 | [diff] [blame] | 265 | /* The F1C100s encodes the divider as 2^(n+1) */ |
| 266 | if (IS_ENABLED(CONFIG_MACH_SUNIV)) |
| 267 | div--; |
Andre Przywara | 239dfd1 | 2022-05-03 00:07:16 +0100 | [diff] [blame] | 268 | reg &= ~((SUN4I_CLK_CTL_CDR1_MASK << 8) | SUN4I_CLK_CTL_DRS); |
| 269 | reg |= SUN4I_CLK_CTL_CDR1(div); |
| 270 | } |
| 271 | |
| 272 | writel(reg, SPI_REG(priv, SPI_CCR)); |
| 273 | |
| 274 | reg = readl(SPI_REG(priv, SPI_TCR)); |
| 275 | reg &= ~(SPI_BIT(priv, SPI_TCR_CPOL) | SPI_BIT(priv, SPI_TCR_CPHA)); |
| 276 | |
| 277 | if (priv->mode & SPI_CPOL) |
| 278 | reg |= SPI_BIT(priv, SPI_TCR_CPOL); |
| 279 | |
| 280 | if (priv->mode & SPI_CPHA) |
| 281 | reg |= SPI_BIT(priv, SPI_TCR_CPHA); |
| 282 | |
| 283 | writel(reg, SPI_REG(priv, SPI_TCR)); |
| 284 | } |
| 285 | |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 286 | static int sun4i_spi_claim_bus(struct udevice *dev) |
| 287 | { |
| 288 | struct sun4i_spi_priv *priv = dev_get_priv(dev->parent); |
Jagan Teki | 8d71a19 | 2019-02-27 20:02:10 +0530 | [diff] [blame] | 289 | int ret; |
| 290 | |
| 291 | ret = sun4i_spi_set_clock(dev->parent, true); |
| 292 | if (ret) |
| 293 | return ret; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 294 | |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 295 | setbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE | |
| 296 | SUN4I_CTL_MASTER | SPI_BIT(priv, SPI_GCR_TP)); |
| 297 | |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 298 | if (priv->variant->has_soft_reset) |
| 299 | setbits_le32(SPI_REG(priv, SPI_GCR), |
| 300 | SPI_BIT(priv, SPI_GCR_SRST)); |
| 301 | |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 302 | setbits_le32(SPI_REG(priv, SPI_TCR), SPI_BIT(priv, SPI_TCR_CS_MANUAL) | |
| 303 | SPI_BIT(priv, SPI_TCR_CS_ACTIVE_LOW)); |
Jagan Teki | 8cbf09b | 2019-02-27 20:02:07 +0530 | [diff] [blame] | 304 | |
Andre Przywara | 239dfd1 | 2022-05-03 00:07:16 +0100 | [diff] [blame] | 305 | sun4i_spi_set_speed_mode(dev->parent); |
| 306 | |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int sun4i_spi_release_bus(struct udevice *dev) |
| 311 | { |
| 312 | struct sun4i_spi_priv *priv = dev_get_priv(dev->parent); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 313 | |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 314 | clrbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 315 | |
Jagan Teki | 8d71a19 | 2019-02-27 20:02:10 +0530 | [diff] [blame] | 316 | sun4i_spi_set_clock(dev->parent, false); |
| 317 | |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int sun4i_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 322 | const void *dout, void *din, unsigned long flags) |
| 323 | { |
| 324 | struct udevice *bus = dev->parent; |
| 325 | struct sun4i_spi_priv *priv = dev_get_priv(bus); |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 326 | struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 327 | |
| 328 | u32 len = bitlen / 8; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 329 | u8 nbytes; |
| 330 | int ret; |
| 331 | |
| 332 | priv->tx_buf = dout; |
| 333 | priv->rx_buf = din; |
| 334 | |
| 335 | if (bitlen % 8) { |
| 336 | debug("%s: non byte-aligned SPI transfer.\n", __func__); |
| 337 | return -ENAVAIL; |
| 338 | } |
| 339 | |
| 340 | if (flags & SPI_XFER_BEGIN) |
| 341 | sun4i_spi_set_cs(bus, slave_plat->cs, true); |
| 342 | |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 343 | /* Reset FIFOs */ |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 344 | setbits_le32(SPI_REG(priv, SPI_FCR), SPI_BIT(priv, SPI_FCR_RF_RST) | |
| 345 | SPI_BIT(priv, SPI_FCR_TF_RST)); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 346 | |
| 347 | while (len) { |
| 348 | /* Setup the transfer now... */ |
Jagan Teki | 178fbd2 | 2019-02-27 20:02:09 +0530 | [diff] [blame] | 349 | nbytes = min(len, (priv->variant->fifo_depth - 1)); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 350 | |
| 351 | /* Setup the counters */ |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 352 | writel(SUN4I_BURST_CNT(nbytes), SPI_REG(priv, SPI_BC)); |
| 353 | writel(SUN4I_XMIT_CNT(nbytes), SPI_REG(priv, SPI_TC)); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 354 | |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 355 | if (priv->variant->has_burst_ctl) |
| 356 | writel(SUN4I_BURST_CNT(nbytes), |
| 357 | SPI_REG(priv, SPI_BCTL)); |
| 358 | |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 359 | /* Fill the TX FIFO */ |
| 360 | sun4i_spi_fill_fifo(priv, nbytes); |
| 361 | |
| 362 | /* Start the transfer */ |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 363 | setbits_le32(SPI_REG(priv, SPI_TCR), |
| 364 | SPI_BIT(priv, SPI_TCR_XCH)); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 365 | |
Icenowy Zheng | 56e497e | 2022-06-28 14:49:24 +0800 | [diff] [blame] | 366 | /* Wait for the transfer to be done */ |
| 367 | ret = wait_for_bit_le32((const void *)SPI_REG(priv, SPI_TCR), |
| 368 | SPI_BIT(priv, SPI_TCR_XCH), |
| 369 | false, SUN4I_SPI_TIMEOUT_MS, false); |
Jagan Teki | 6cb6aa6 | 2019-02-27 20:02:05 +0530 | [diff] [blame] | 370 | if (ret < 0) { |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 371 | printf("ERROR: sun4i_spi: Timeout transferring data\n"); |
| 372 | sun4i_spi_set_cs(bus, slave_plat->cs, false); |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | /* Drain the RX FIFO */ |
| 377 | sun4i_spi_drain_fifo(priv, nbytes); |
| 378 | |
| 379 | len -= nbytes; |
| 380 | } |
| 381 | |
| 382 | if (flags & SPI_XFER_END) |
| 383 | sun4i_spi_set_cs(bus, slave_plat->cs, false); |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int sun4i_spi_set_speed(struct udevice *dev, uint speed) |
| 389 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 390 | struct sun4i_spi_plat *plat = dev_get_plat(dev); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 391 | struct sun4i_spi_priv *priv = dev_get_priv(dev); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 392 | |
| 393 | if (speed > plat->max_hz) |
| 394 | speed = plat->max_hz; |
| 395 | |
| 396 | if (speed < SUN4I_SPI_MIN_RATE) |
| 397 | speed = SUN4I_SPI_MIN_RATE; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 398 | |
| 399 | priv->freq = speed; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static int sun4i_spi_set_mode(struct udevice *dev, uint mode) |
| 405 | { |
| 406 | struct sun4i_spi_priv *priv = dev_get_priv(dev); |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 407 | |
| 408 | priv->mode = mode; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static const struct dm_spi_ops sun4i_spi_ops = { |
| 414 | .claim_bus = sun4i_spi_claim_bus, |
| 415 | .release_bus = sun4i_spi_release_bus, |
| 416 | .xfer = sun4i_spi_xfer, |
| 417 | .set_speed = sun4i_spi_set_speed, |
| 418 | .set_mode = sun4i_spi_set_mode, |
| 419 | }; |
| 420 | |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 421 | static int sun4i_spi_probe(struct udevice *bus) |
| 422 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 423 | struct sun4i_spi_plat *plat = dev_get_plat(bus); |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 424 | struct sun4i_spi_priv *priv = dev_get_priv(bus); |
| 425 | int ret; |
| 426 | |
| 427 | ret = clk_get_by_name(bus, "ahb", &priv->clk_ahb); |
| 428 | if (ret) { |
Sean Anderson | 32bbe5b | 2020-09-15 10:45:11 -0400 | [diff] [blame] | 429 | dev_err(bus, "failed to get ahb clock\n"); |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | ret = clk_get_by_name(bus, "mod", &priv->clk_mod); |
| 434 | if (ret) { |
Sean Anderson | 32bbe5b | 2020-09-15 10:45:11 -0400 | [diff] [blame] | 435 | dev_err(bus, "failed to get mod clock\n"); |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | ret = reset_get_by_index(bus, 0, &priv->reset); |
| 440 | if (ret && ret != -ENOENT) { |
Sean Anderson | 32bbe5b | 2020-09-15 10:45:11 -0400 | [diff] [blame] | 441 | dev_err(bus, "failed to get reset\n"); |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 442 | return ret; |
| 443 | } |
| 444 | |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 445 | priv->variant = plat->variant; |
| 446 | priv->base = plat->base; |
| 447 | priv->freq = plat->max_hz; |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 452 | static int sun4i_spi_of_to_plat(struct udevice *bus) |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 453 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 454 | struct sun4i_spi_plat *plat = dev_get_plat(bus); |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 455 | int node = dev_of_offset(bus); |
| 456 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 457 | plat->base = dev_read_addr(bus); |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 458 | plat->variant = (struct sun4i_spi_variant *)dev_get_driver_data(bus); |
| 459 | plat->max_hz = fdtdec_get_int(gd->fdt_blob, node, |
| 460 | "spi-max-frequency", |
| 461 | SUN4I_SPI_DEFAULT_RATE); |
| 462 | |
| 463 | if (plat->max_hz > SUN4I_SPI_MAX_RATE) |
| 464 | plat->max_hz = SUN4I_SPI_MAX_RATE; |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 469 | static const unsigned long sun4i_spi_regs[] = { |
| 470 | [SPI_GCR] = SUN4I_CTL_REG, |
| 471 | [SPI_TCR] = SUN4I_CTL_REG, |
| 472 | [SPI_FCR] = SUN4I_CTL_REG, |
| 473 | [SPI_FSR] = SUN4I_FIFO_STA_REG, |
| 474 | [SPI_CCR] = SUN4I_CLK_CTL_REG, |
| 475 | [SPI_BC] = SUN4I_BURST_CNT_REG, |
| 476 | [SPI_TC] = SUN4I_XMIT_CNT_REG, |
| 477 | [SPI_TXD] = SUN4I_TXDATA_REG, |
| 478 | [SPI_RXD] = SUN4I_RXDATA_REG, |
| 479 | }; |
| 480 | |
| 481 | static const u32 sun4i_spi_bits[] = { |
| 482 | [SPI_GCR_TP] = BIT(18), |
| 483 | [SPI_TCR_CPHA] = BIT(2), |
| 484 | [SPI_TCR_CPOL] = BIT(3), |
| 485 | [SPI_TCR_CS_ACTIVE_LOW] = BIT(4), |
| 486 | [SPI_TCR_XCH] = BIT(10), |
| 487 | [SPI_TCR_CS_SEL] = 12, |
| 488 | [SPI_TCR_CS_MASK] = 0x3000, |
| 489 | [SPI_TCR_CS_MANUAL] = BIT(16), |
| 490 | [SPI_TCR_CS_LEVEL] = BIT(17), |
| 491 | [SPI_FCR_TF_RST] = BIT(8), |
| 492 | [SPI_FCR_RF_RST] = BIT(9), |
| 493 | [SPI_FSR_RF_CNT_MASK] = GENMASK(6, 0), |
| 494 | }; |
| 495 | |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 496 | static const unsigned long sun6i_spi_regs[] = { |
| 497 | [SPI_GCR] = SUN6I_GBL_CTL_REG, |
| 498 | [SPI_TCR] = SUN6I_TFR_CTL_REG, |
| 499 | [SPI_FCR] = SUN6I_FIFO_CTL_REG, |
| 500 | [SPI_FSR] = SUN6I_FIFO_STA_REG, |
| 501 | [SPI_CCR] = SUN6I_CLK_CTL_REG, |
| 502 | [SPI_BC] = SUN6I_BURST_CNT_REG, |
| 503 | [SPI_TC] = SUN6I_XMIT_CNT_REG, |
| 504 | [SPI_BCTL] = SUN6I_BURST_CTL_REG, |
| 505 | [SPI_TXD] = SUN6I_TXDATA_REG, |
| 506 | [SPI_RXD] = SUN6I_RXDATA_REG, |
| 507 | }; |
| 508 | |
| 509 | static const u32 sun6i_spi_bits[] = { |
| 510 | [SPI_GCR_TP] = BIT(7), |
| 511 | [SPI_GCR_SRST] = BIT(31), |
| 512 | [SPI_TCR_CPHA] = BIT(0), |
| 513 | [SPI_TCR_CPOL] = BIT(1), |
| 514 | [SPI_TCR_CS_ACTIVE_LOW] = BIT(2), |
| 515 | [SPI_TCR_CS_SEL] = 4, |
| 516 | [SPI_TCR_CS_MASK] = 0x30, |
| 517 | [SPI_TCR_CS_MANUAL] = BIT(6), |
| 518 | [SPI_TCR_CS_LEVEL] = BIT(7), |
| 519 | [SPI_TCR_XCH] = BIT(31), |
| 520 | [SPI_FCR_RF_RST] = BIT(15), |
| 521 | [SPI_FCR_TF_RST] = BIT(31), |
| 522 | [SPI_FSR_RF_CNT_MASK] = GENMASK(7, 0), |
| 523 | }; |
| 524 | |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 525 | static const struct sun4i_spi_variant sun4i_a10_spi_variant = { |
| 526 | .regs = sun4i_spi_regs, |
| 527 | .bits = sun4i_spi_bits, |
Jagan Teki | 178fbd2 | 2019-02-27 20:02:09 +0530 | [diff] [blame] | 528 | .fifo_depth = 64, |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 529 | }; |
| 530 | |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 531 | static const struct sun4i_spi_variant sun6i_a31_spi_variant = { |
| 532 | .regs = sun6i_spi_regs, |
| 533 | .bits = sun6i_spi_bits, |
| 534 | .fifo_depth = 128, |
| 535 | .has_soft_reset = true, |
| 536 | .has_burst_ctl = true, |
| 537 | }; |
| 538 | |
| 539 | static const struct sun4i_spi_variant sun8i_h3_spi_variant = { |
| 540 | .regs = sun6i_spi_regs, |
| 541 | .bits = sun6i_spi_bits, |
| 542 | .fifo_depth = 64, |
| 543 | .has_soft_reset = true, |
| 544 | .has_burst_ctl = true, |
| 545 | }; |
| 546 | |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 547 | static const struct udevice_id sun4i_spi_ids[] = { |
Jagan Teki | 8d9bf46 | 2019-02-27 20:02:08 +0530 | [diff] [blame] | 548 | { |
| 549 | .compatible = "allwinner,sun4i-a10-spi", |
| 550 | .data = (ulong)&sun4i_a10_spi_variant, |
| 551 | }, |
Jagan Teki | 853f451 | 2019-02-27 20:02:11 +0530 | [diff] [blame] | 552 | { |
| 553 | .compatible = "allwinner,sun6i-a31-spi", |
| 554 | .data = (ulong)&sun6i_a31_spi_variant, |
| 555 | }, |
| 556 | { |
| 557 | .compatible = "allwinner,sun8i-h3-spi", |
| 558 | .data = (ulong)&sun8i_h3_spi_variant, |
| 559 | }, |
Jagan Teki | 903e7cf | 2019-02-27 20:02:12 +0530 | [diff] [blame] | 560 | { /* sentinel */ } |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 561 | }; |
| 562 | |
| 563 | U_BOOT_DRIVER(sun4i_spi) = { |
| 564 | .name = "sun4i_spi", |
| 565 | .id = UCLASS_SPI, |
| 566 | .of_match = sun4i_spi_ids, |
| 567 | .ops = &sun4i_spi_ops, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 568 | .of_to_plat = sun4i_spi_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 569 | .plat_auto = sizeof(struct sun4i_spi_plat), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 570 | .priv_auto = sizeof(struct sun4i_spi_priv), |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 571 | .probe = sun4i_spi_probe, |
| 572 | }; |