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