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> |
| 22 | #include <dm.h> |
| 23 | #include <spi.h> |
| 24 | #include <errno.h> |
| 25 | #include <fdt_support.h> |
| 26 | #include <wait_bit.h> |
| 27 | |
| 28 | #include <asm/bitops.h> |
| 29 | #include <asm/gpio.h> |
| 30 | #include <asm/io.h> |
| 31 | |
| 32 | #include <asm/arch/clock.h> |
| 33 | |
Jagan Teki | 6cb6aa6 | 2019-02-27 20:02:05 +0530 | [diff] [blame^] | 34 | #include <linux/iopoll.h> |
| 35 | |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 36 | #define SUN4I_FIFO_DEPTH 64 |
| 37 | |
| 38 | #define SUN4I_RXDATA_REG 0x00 |
| 39 | |
| 40 | #define SUN4I_TXDATA_REG 0x04 |
| 41 | |
| 42 | #define SUN4I_CTL_REG 0x08 |
| 43 | #define SUN4I_CTL_ENABLE BIT(0) |
| 44 | #define SUN4I_CTL_MASTER BIT(1) |
| 45 | #define SUN4I_CTL_CPHA BIT(2) |
| 46 | #define SUN4I_CTL_CPOL BIT(3) |
| 47 | #define SUN4I_CTL_CS_ACTIVE_LOW BIT(4) |
| 48 | #define SUN4I_CTL_LMTF BIT(6) |
| 49 | #define SUN4I_CTL_TF_RST BIT(8) |
| 50 | #define SUN4I_CTL_RF_RST BIT(9) |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 51 | #define SUN4I_CTL_XCH BIT(10) |
| 52 | #define SUN4I_CTL_CS_MASK 0x3000 |
| 53 | #define SUN4I_CTL_CS(cs) (((cs) << 12) & SUN4I_CTL_CS_MASK) |
| 54 | #define SUN4I_CTL_DHB BIT(15) |
| 55 | #define SUN4I_CTL_CS_MANUAL BIT(16) |
| 56 | #define SUN4I_CTL_CS_LEVEL BIT(17) |
| 57 | #define SUN4I_CTL_TP BIT(18) |
| 58 | |
| 59 | #define SUN4I_INT_CTL_REG 0x0c |
| 60 | #define SUN4I_INT_CTL_RF_F34 BIT(4) |
| 61 | #define SUN4I_INT_CTL_TF_E34 BIT(12) |
| 62 | #define SUN4I_INT_CTL_TC BIT(16) |
| 63 | |
| 64 | #define SUN4I_INT_STA_REG 0x10 |
| 65 | |
| 66 | #define SUN4I_DMA_CTL_REG 0x14 |
| 67 | |
| 68 | #define SUN4I_WAIT_REG 0x18 |
| 69 | |
| 70 | #define SUN4I_CLK_CTL_REG 0x1c |
| 71 | #define SUN4I_CLK_CTL_CDR2_MASK 0xff |
| 72 | #define SUN4I_CLK_CTL_CDR2(div) ((div) & SUN4I_CLK_CTL_CDR2_MASK) |
| 73 | #define SUN4I_CLK_CTL_CDR1_MASK 0xf |
| 74 | #define SUN4I_CLK_CTL_CDR1(div) (((div) & SUN4I_CLK_CTL_CDR1_MASK) << 8) |
| 75 | #define SUN4I_CLK_CTL_DRS BIT(12) |
| 76 | |
| 77 | #define SUN4I_MAX_XFER_SIZE 0xffffff |
| 78 | |
| 79 | #define SUN4I_BURST_CNT_REG 0x20 |
| 80 | #define SUN4I_BURST_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE) |
| 81 | |
| 82 | #define SUN4I_XMIT_CNT_REG 0x24 |
| 83 | #define SUN4I_XMIT_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE) |
| 84 | |
| 85 | #define SUN4I_FIFO_STA_REG 0x28 |
| 86 | #define SUN4I_FIFO_STA_RF_CNT_MASK 0x7f |
| 87 | #define SUN4I_FIFO_STA_RF_CNT_BITS 0 |
| 88 | #define SUN4I_FIFO_STA_TF_CNT_MASK 0x7f |
| 89 | #define SUN4I_FIFO_STA_TF_CNT_BITS 16 |
| 90 | |
| 91 | #define SUN4I_SPI_MAX_RATE 24000000 |
| 92 | #define SUN4I_SPI_MIN_RATE 3000 |
| 93 | #define SUN4I_SPI_DEFAULT_RATE 1000000 |
| 94 | #define SUN4I_SPI_TIMEOUT_US 1000000 |
| 95 | |
| 96 | /* sun4i spi register set */ |
| 97 | struct sun4i_spi_regs { |
| 98 | u32 rxdata; |
| 99 | u32 txdata; |
| 100 | u32 ctl; |
| 101 | u32 intctl; |
| 102 | u32 st; |
| 103 | u32 dmactl; |
| 104 | u32 wait; |
| 105 | u32 cctl; |
| 106 | u32 bc; |
| 107 | u32 tc; |
| 108 | u32 fifo_sta; |
| 109 | }; |
| 110 | |
| 111 | struct sun4i_spi_platdata { |
| 112 | u32 base_addr; |
| 113 | u32 max_hz; |
| 114 | }; |
| 115 | |
| 116 | struct sun4i_spi_priv { |
| 117 | struct sun4i_spi_regs *regs; |
| 118 | u32 freq; |
| 119 | u32 mode; |
| 120 | |
| 121 | const u8 *tx_buf; |
| 122 | u8 *rx_buf; |
| 123 | }; |
| 124 | |
| 125 | DECLARE_GLOBAL_DATA_PTR; |
| 126 | |
| 127 | static inline void sun4i_spi_drain_fifo(struct sun4i_spi_priv *priv, int len) |
| 128 | { |
| 129 | u8 byte; |
| 130 | |
| 131 | while (len--) { |
| 132 | byte = readb(&priv->regs->rxdata); |
Stefan Mavrodiev | 5c1a87d | 2018-12-05 14:27:57 +0200 | [diff] [blame] | 133 | if (priv->rx_buf) |
| 134 | *priv->rx_buf++ = byte; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | static inline void sun4i_spi_fill_fifo(struct sun4i_spi_priv *priv, int len) |
| 139 | { |
| 140 | u8 byte; |
| 141 | |
| 142 | while (len--) { |
| 143 | byte = priv->tx_buf ? *priv->tx_buf++ : 0; |
| 144 | writeb(byte, &priv->regs->txdata); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static void sun4i_spi_set_cs(struct udevice *bus, u8 cs, bool enable) |
| 149 | { |
| 150 | struct sun4i_spi_priv *priv = dev_get_priv(bus); |
| 151 | u32 reg; |
| 152 | |
| 153 | reg = readl(&priv->regs->ctl); |
| 154 | |
| 155 | reg &= ~SUN4I_CTL_CS_MASK; |
| 156 | reg |= SUN4I_CTL_CS(cs); |
| 157 | |
| 158 | if (enable) |
| 159 | reg &= ~SUN4I_CTL_CS_LEVEL; |
| 160 | else |
| 161 | reg |= SUN4I_CTL_CS_LEVEL; |
| 162 | |
| 163 | writel(reg, &priv->regs->ctl); |
| 164 | } |
| 165 | |
| 166 | static int sun4i_spi_parse_pins(struct udevice *dev) |
| 167 | { |
| 168 | const void *fdt = gd->fdt_blob; |
| 169 | const char *pin_name; |
| 170 | const fdt32_t *list; |
| 171 | u32 phandle; |
| 172 | int drive, pull = 0, pin, i; |
| 173 | int offset; |
| 174 | int size; |
| 175 | |
| 176 | list = fdt_getprop(fdt, dev_of_offset(dev), "pinctrl-0", &size); |
| 177 | if (!list) { |
| 178 | printf("WARNING: sun4i_spi: cannot find pinctrl-0 node\n"); |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | |
| 182 | while (size) { |
| 183 | phandle = fdt32_to_cpu(*list++); |
| 184 | size -= sizeof(*list); |
| 185 | |
| 186 | offset = fdt_node_offset_by_phandle(fdt, phandle); |
| 187 | if (offset < 0) |
| 188 | return offset; |
| 189 | |
| 190 | drive = fdt_getprop_u32_default_node(fdt, offset, 0, |
| 191 | "drive-strength", 0); |
| 192 | if (drive) { |
| 193 | if (drive <= 10) |
| 194 | drive = 0; |
| 195 | else if (drive <= 20) |
| 196 | drive = 1; |
| 197 | else if (drive <= 30) |
| 198 | drive = 2; |
| 199 | else |
| 200 | drive = 3; |
| 201 | } else { |
| 202 | drive = fdt_getprop_u32_default_node(fdt, offset, 0, |
| 203 | "allwinner,drive", |
| 204 | 0); |
| 205 | drive = min(drive, 3); |
| 206 | } |
| 207 | |
| 208 | if (fdt_get_property(fdt, offset, "bias-disable", NULL)) |
| 209 | pull = 0; |
| 210 | else if (fdt_get_property(fdt, offset, "bias-pull-up", NULL)) |
| 211 | pull = 1; |
| 212 | else if (fdt_get_property(fdt, offset, "bias-pull-down", NULL)) |
| 213 | pull = 2; |
| 214 | else |
| 215 | pull = fdt_getprop_u32_default_node(fdt, offset, 0, |
| 216 | "allwinner,pull", |
| 217 | 0); |
| 218 | pull = min(pull, 2); |
| 219 | |
| 220 | for (i = 0; ; i++) { |
| 221 | pin_name = fdt_stringlist_get(fdt, offset, |
| 222 | "pins", i, NULL); |
| 223 | if (!pin_name) { |
| 224 | pin_name = fdt_stringlist_get(fdt, offset, |
| 225 | "allwinner,pins", |
| 226 | i, NULL); |
| 227 | if (!pin_name) |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | pin = name_to_gpio(pin_name); |
| 232 | if (pin < 0) |
| 233 | break; |
| 234 | |
| 235 | sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SPI0); |
| 236 | sunxi_gpio_set_drv(pin, drive); |
| 237 | sunxi_gpio_set_pull(pin, pull); |
| 238 | } |
| 239 | } |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static inline void sun4i_spi_enable_clock(void) |
| 244 | { |
| 245 | struct sunxi_ccm_reg *const ccm = |
| 246 | (struct sunxi_ccm_reg *const)SUNXI_CCM_BASE; |
| 247 | |
| 248 | setbits_le32(&ccm->ahb_gate0, (1 << AHB_GATE_OFFSET_SPI0)); |
| 249 | writel((1 << 31), &ccm->spi0_clk_cfg); |
| 250 | } |
| 251 | |
| 252 | static int sun4i_spi_ofdata_to_platdata(struct udevice *bus) |
| 253 | { |
| 254 | struct sun4i_spi_platdata *plat = dev_get_platdata(bus); |
| 255 | int node = dev_of_offset(bus); |
| 256 | |
| 257 | plat->base_addr = devfdt_get_addr(bus); |
| 258 | plat->max_hz = fdtdec_get_int(gd->fdt_blob, node, |
| 259 | "spi-max-frequency", |
| 260 | SUN4I_SPI_DEFAULT_RATE); |
| 261 | |
| 262 | if (plat->max_hz > SUN4I_SPI_MAX_RATE) |
| 263 | plat->max_hz = SUN4I_SPI_MAX_RATE; |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int sun4i_spi_probe(struct udevice *bus) |
| 269 | { |
| 270 | struct sun4i_spi_platdata *plat = dev_get_platdata(bus); |
| 271 | struct sun4i_spi_priv *priv = dev_get_priv(bus); |
| 272 | |
| 273 | sun4i_spi_enable_clock(); |
| 274 | sun4i_spi_parse_pins(bus); |
| 275 | |
| 276 | priv->regs = (struct sun4i_spi_regs *)(uintptr_t)plat->base_addr; |
| 277 | priv->freq = plat->max_hz; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int sun4i_spi_claim_bus(struct udevice *dev) |
| 283 | { |
| 284 | struct sun4i_spi_priv *priv = dev_get_priv(dev->parent); |
| 285 | |
| 286 | writel(SUN4I_CTL_ENABLE | SUN4I_CTL_MASTER | SUN4I_CTL_TP | |
| 287 | SUN4I_CTL_CS_MANUAL | SUN4I_CTL_CS_ACTIVE_LOW, |
| 288 | &priv->regs->ctl); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int sun4i_spi_release_bus(struct udevice *dev) |
| 293 | { |
| 294 | struct sun4i_spi_priv *priv = dev_get_priv(dev->parent); |
| 295 | u32 reg; |
| 296 | |
| 297 | reg = readl(&priv->regs->ctl); |
| 298 | reg &= ~SUN4I_CTL_ENABLE; |
| 299 | writel(reg, &priv->regs->ctl); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int sun4i_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 305 | const void *dout, void *din, unsigned long flags) |
| 306 | { |
| 307 | struct udevice *bus = dev->parent; |
| 308 | struct sun4i_spi_priv *priv = dev_get_priv(bus); |
| 309 | struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); |
| 310 | |
| 311 | u32 len = bitlen / 8; |
Jagan Teki | 6cb6aa6 | 2019-02-27 20:02:05 +0530 | [diff] [blame^] | 312 | u32 reg, rx_fifocnt; |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 313 | u8 nbytes; |
| 314 | int ret; |
| 315 | |
| 316 | priv->tx_buf = dout; |
| 317 | priv->rx_buf = din; |
| 318 | |
| 319 | if (bitlen % 8) { |
| 320 | debug("%s: non byte-aligned SPI transfer.\n", __func__); |
| 321 | return -ENAVAIL; |
| 322 | } |
| 323 | |
| 324 | if (flags & SPI_XFER_BEGIN) |
| 325 | sun4i_spi_set_cs(bus, slave_plat->cs, true); |
| 326 | |
| 327 | reg = readl(&priv->regs->ctl); |
| 328 | |
| 329 | /* Reset FIFOs */ |
| 330 | writel(reg | SUN4I_CTL_RF_RST | SUN4I_CTL_TF_RST, &priv->regs->ctl); |
| 331 | |
| 332 | while (len) { |
| 333 | /* Setup the transfer now... */ |
| 334 | nbytes = min(len, (u32)(SUN4I_FIFO_DEPTH - 1)); |
| 335 | |
| 336 | /* Setup the counters */ |
| 337 | writel(SUN4I_BURST_CNT(nbytes), &priv->regs->bc); |
| 338 | writel(SUN4I_XMIT_CNT(nbytes), &priv->regs->tc); |
| 339 | |
| 340 | /* Fill the TX FIFO */ |
| 341 | sun4i_spi_fill_fifo(priv, nbytes); |
| 342 | |
| 343 | /* Start the transfer */ |
| 344 | reg = readl(&priv->regs->ctl); |
| 345 | writel(reg | SUN4I_CTL_XCH, &priv->regs->ctl); |
| 346 | |
Jagan Teki | 6cb6aa6 | 2019-02-27 20:02:05 +0530 | [diff] [blame^] | 347 | /* Wait till RX FIFO to be empty */ |
| 348 | ret = readl_poll_timeout(&priv->regs->fifo_sta, rx_fifocnt, |
| 349 | (((rx_fifocnt & SUN4I_FIFO_STA_RF_CNT_MASK) >> |
| 350 | SUN4I_FIFO_STA_RF_CNT_BITS) >= nbytes), |
| 351 | SUN4I_SPI_TIMEOUT_US); |
| 352 | if (ret < 0) { |
Stefan Mavrodiev | 7f25d81 | 2018-02-06 15:14:33 +0200 | [diff] [blame] | 353 | printf("ERROR: sun4i_spi: Timeout transferring data\n"); |
| 354 | sun4i_spi_set_cs(bus, slave_plat->cs, false); |
| 355 | return ret; |
| 356 | } |
| 357 | |
| 358 | /* Drain the RX FIFO */ |
| 359 | sun4i_spi_drain_fifo(priv, nbytes); |
| 360 | |
| 361 | len -= nbytes; |
| 362 | } |
| 363 | |
| 364 | if (flags & SPI_XFER_END) |
| 365 | sun4i_spi_set_cs(bus, slave_plat->cs, false); |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static int sun4i_spi_set_speed(struct udevice *dev, uint speed) |
| 371 | { |
| 372 | struct sun4i_spi_platdata *plat = dev_get_platdata(dev); |
| 373 | struct sun4i_spi_priv *priv = dev_get_priv(dev); |
| 374 | unsigned int div; |
| 375 | u32 reg; |
| 376 | |
| 377 | if (speed > plat->max_hz) |
| 378 | speed = plat->max_hz; |
| 379 | |
| 380 | if (speed < SUN4I_SPI_MIN_RATE) |
| 381 | speed = SUN4I_SPI_MIN_RATE; |
| 382 | /* |
| 383 | * Setup clock divider. |
| 384 | * |
| 385 | * We have two choices there. Either we can use the clock |
| 386 | * divide rate 1, which is calculated thanks to this formula: |
| 387 | * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1)) |
| 388 | * Or we can use CDR2, which is calculated with the formula: |
| 389 | * SPI_CLK = MOD_CLK / (2 * (cdr + 1)) |
| 390 | * Whether we use the former or the latter is set through the |
| 391 | * DRS bit. |
| 392 | * |
| 393 | * First try CDR2, and if we can't reach the expected |
| 394 | * frequency, fall back to CDR1. |
| 395 | */ |
| 396 | |
| 397 | div = SUN4I_SPI_MAX_RATE / (2 * speed); |
| 398 | reg = readl(&priv->regs->cctl); |
| 399 | |
| 400 | if (div <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) { |
| 401 | if (div > 0) |
| 402 | div--; |
| 403 | |
| 404 | reg &= ~(SUN4I_CLK_CTL_CDR2_MASK | SUN4I_CLK_CTL_DRS); |
| 405 | reg |= SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS; |
| 406 | } else { |
| 407 | div = __ilog2(SUN4I_SPI_MAX_RATE) - __ilog2(speed); |
| 408 | reg &= ~((SUN4I_CLK_CTL_CDR1_MASK << 8) | SUN4I_CLK_CTL_DRS); |
| 409 | reg |= SUN4I_CLK_CTL_CDR1(div); |
| 410 | } |
| 411 | |
| 412 | priv->freq = speed; |
| 413 | writel(reg, &priv->regs->cctl); |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static int sun4i_spi_set_mode(struct udevice *dev, uint mode) |
| 419 | { |
| 420 | struct sun4i_spi_priv *priv = dev_get_priv(dev); |
| 421 | u32 reg; |
| 422 | |
| 423 | reg = readl(&priv->regs->ctl); |
| 424 | reg &= ~(SUN4I_CTL_CPOL | SUN4I_CTL_CPHA); |
| 425 | |
| 426 | if (mode & SPI_CPOL) |
| 427 | reg |= SUN4I_CTL_CPOL; |
| 428 | |
| 429 | if (mode & SPI_CPHA) |
| 430 | reg |= SUN4I_CTL_CPHA; |
| 431 | |
| 432 | priv->mode = mode; |
| 433 | writel(reg, &priv->regs->ctl); |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static const struct dm_spi_ops sun4i_spi_ops = { |
| 439 | .claim_bus = sun4i_spi_claim_bus, |
| 440 | .release_bus = sun4i_spi_release_bus, |
| 441 | .xfer = sun4i_spi_xfer, |
| 442 | .set_speed = sun4i_spi_set_speed, |
| 443 | .set_mode = sun4i_spi_set_mode, |
| 444 | }; |
| 445 | |
| 446 | static const struct udevice_id sun4i_spi_ids[] = { |
| 447 | { .compatible = "allwinner,sun4i-a10-spi" }, |
| 448 | { } |
| 449 | }; |
| 450 | |
| 451 | U_BOOT_DRIVER(sun4i_spi) = { |
| 452 | .name = "sun4i_spi", |
| 453 | .id = UCLASS_SPI, |
| 454 | .of_match = sun4i_spi_ids, |
| 455 | .ops = &sun4i_spi_ops, |
| 456 | .ofdata_to_platdata = sun4i_spi_ofdata_to_platdata, |
| 457 | .platdata_auto_alloc_size = sizeof(struct sun4i_spi_platdata), |
| 458 | .priv_auto_alloc_size = sizeof(struct sun4i_spi_priv), |
| 459 | .probe = sun4i_spi_probe, |
| 460 | }; |