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