Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2009 |
| 4 | * Marvell Semiconductor <www.marvell.com> |
| 5 | * Written-by: Prafulla Wadaskar <prafulla@marvell.com> |
| 6 | * |
| 7 | * Derived from drivers/spi/mpc8xxx_spi.c |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <spi.h> |
Lei Wen | a7efd71 | 2011-10-18 20:11:42 +0530 | [diff] [blame] | 15 | #include <asm/io.h> |
Stefan Roese | 3dc23f7 | 2014-10-22 12:13:06 +0200 | [diff] [blame] | 16 | #include <asm/arch/soc.h> |
Trevor Woerner | bb0fb4c | 2020-05-06 08:02:40 -0400 | [diff] [blame] | 17 | #ifdef CONFIG_ARCH_KIRKWOOD |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 18 | #include <asm/arch/mpp.h> |
Stefan Roese | 4aceea2 | 2014-10-22 12:13:10 +0200 | [diff] [blame] | 19 | #endif |
Stefan Roese | 3e972cb | 2014-10-22 12:13:07 +0200 | [diff] [blame] | 20 | #include <asm/arch-mvebu/spi.h> |
Prafulla Wadaskar | 5710de4 | 2009-05-30 01:13:33 +0530 | [diff] [blame] | 21 | |
Bhargav Shah | a58c7ff | 2020-06-18 23:15:13 +0530 | [diff] [blame] | 22 | struct mvebu_spi_dev { |
| 23 | bool is_errata_50mhz_ac; |
| 24 | }; |
| 25 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 26 | struct mvebu_spi_plat { |
Bhargav Shah | a58c7ff | 2020-06-18 23:15:13 +0530 | [diff] [blame] | 27 | struct kwspi_registers *spireg; |
| 28 | bool is_errata_50mhz_ac; |
| 29 | }; |
| 30 | |
| 31 | struct mvebu_spi_priv { |
| 32 | struct kwspi_registers *spireg; |
| 33 | }; |
| 34 | |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 35 | static void _spi_cs_activate(struct kwspi_registers *reg) |
| 36 | { |
| 37 | setbits_le32(®->ctrl, KWSPI_CSN_ACT); |
| 38 | } |
| 39 | |
| 40 | static void _spi_cs_deactivate(struct kwspi_registers *reg) |
| 41 | { |
| 42 | clrbits_le32(®->ctrl, KWSPI_CSN_ACT); |
| 43 | } |
| 44 | |
| 45 | static int _spi_xfer(struct kwspi_registers *reg, unsigned int bitlen, |
| 46 | const void *dout, void *din, unsigned long flags) |
| 47 | { |
| 48 | unsigned int tmpdout, tmpdin; |
| 49 | int tm, isread = 0; |
| 50 | |
| 51 | debug("spi_xfer: dout %p din %p bitlen %u\n", dout, din, bitlen); |
| 52 | |
| 53 | if (flags & SPI_XFER_BEGIN) |
| 54 | _spi_cs_activate(reg); |
| 55 | |
| 56 | /* |
| 57 | * handle data in 8-bit chunks |
| 58 | * TBD: 2byte xfer mode to be enabled |
| 59 | */ |
| 60 | clrsetbits_le32(®->cfg, KWSPI_XFERLEN_MASK, KWSPI_XFERLEN_1BYTE); |
| 61 | |
| 62 | while (bitlen > 4) { |
| 63 | debug("loopstart bitlen %d\n", bitlen); |
| 64 | tmpdout = 0; |
| 65 | |
| 66 | /* Shift data so it's msb-justified */ |
| 67 | if (dout) |
| 68 | tmpdout = *(u32 *)dout & 0xff; |
| 69 | |
| 70 | clrbits_le32(®->irq_cause, KWSPI_SMEMRDIRQ); |
| 71 | writel(tmpdout, ®->dout); /* Write the data out */ |
| 72 | debug("*** spi_xfer: ... %08x written, bitlen %d\n", |
| 73 | tmpdout, bitlen); |
| 74 | |
| 75 | /* |
| 76 | * Wait for SPI transmit to get out |
| 77 | * or time out (1 second = 1000 ms) |
| 78 | * The NE event must be read and cleared first |
| 79 | */ |
| 80 | for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) { |
| 81 | if (readl(®->irq_cause) & KWSPI_SMEMRDIRQ) { |
| 82 | isread = 1; |
| 83 | tmpdin = readl(®->din); |
| 84 | debug("spi_xfer: din %p..%08x read\n", |
| 85 | din, tmpdin); |
| 86 | |
| 87 | if (din) { |
| 88 | *((u8 *)din) = (u8)tmpdin; |
| 89 | din += 1; |
| 90 | } |
| 91 | if (dout) |
| 92 | dout += 1; |
| 93 | bitlen -= 8; |
| 94 | } |
| 95 | if (isread) |
| 96 | break; |
| 97 | } |
| 98 | if (tm >= KWSPI_TIMEOUT) |
| 99 | printf("*** spi_xfer: Time out during SPI transfer\n"); |
| 100 | |
| 101 | debug("loopend bitlen %d\n", bitlen); |
| 102 | } |
| 103 | |
| 104 | if (flags & SPI_XFER_END) |
| 105 | _spi_cs_deactivate(reg); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 110 | static int mvebu_spi_set_speed(struct udevice *bus, uint hz) |
| 111 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 112 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 113 | struct kwspi_registers *reg = plat->spireg; |
Ken Ma | 037818c | 2021-04-30 15:26:29 +0200 | [diff] [blame^] | 114 | u32 data, divider; |
| 115 | unsigned int spr, sppr; |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 116 | |
Ken Ma | 037818c | 2021-04-30 15:26:29 +0200 | [diff] [blame^] | 117 | /* |
| 118 | * Calculate spi clock prescaller using max_hz. |
| 119 | * SPPR is SPI Baud Rate Pre-selection, it holds bits 5 and 7:6 in |
| 120 | * SPI Interface Configuration Register; |
| 121 | * SPR is SPI Baud Rate Selection, it holds bits 3:0 in SPI Interface |
| 122 | * Configuration Register. |
| 123 | * The SPR together with the SPPR define the SPI CLK frequency as |
| 124 | * follows: |
| 125 | * SPI actual frequency = core_clk / (SPR * (2 ^ SPPR)) |
| 126 | */ |
| 127 | divider = DIV_ROUND_UP(CONFIG_SYS_TCLK, hz); |
| 128 | if (divider < 16) { |
| 129 | /* This is the easy case, divider is less than 16 */ |
| 130 | spr = divider; |
| 131 | sppr = 0; |
| 132 | |
| 133 | } else { |
| 134 | unsigned int two_pow_sppr; |
| 135 | /* |
| 136 | * Find the highest bit set in divider. This and the |
| 137 | * three next bits define SPR (apart from rounding). |
| 138 | * SPPR is then the number of zero bits that must be |
| 139 | * appended: |
| 140 | */ |
| 141 | sppr = fls(divider) - 4; |
| 142 | |
| 143 | /* |
| 144 | * As SPR only has 4 bits, we have to round divider up |
| 145 | * to the next multiple of 2 ** sppr. |
| 146 | */ |
| 147 | two_pow_sppr = 1 << sppr; |
| 148 | divider = (divider + two_pow_sppr - 1) & -two_pow_sppr; |
| 149 | |
| 150 | /* |
| 151 | * recalculate sppr as rounding up divider might have |
| 152 | * increased it enough to change the position of the |
| 153 | * highest set bit. In this case the bit that now |
| 154 | * doesn't make it into SPR is 0, so there is no need to |
| 155 | * round again. |
| 156 | */ |
| 157 | sppr = fls(divider) - 4; |
| 158 | spr = divider >> sppr; |
| 159 | |
| 160 | /* |
| 161 | * Now do range checking. SPR is constructed to have a |
| 162 | * width of 4 bits, so this is fine for sure. So we |
| 163 | * still need to check for sppr to fit into 3 bits: |
| 164 | */ |
| 165 | if (sppr > 7) |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | data = ((sppr & 0x6) << 5) | ((sppr & 0x1) << 4) | spr; |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 170 | |
| 171 | /* program spi clock prescaler using max_hz */ |
| 172 | writel(KWSPI_ADRLEN_3BYTE | data, ®->cfg); |
| 173 | debug("data = 0x%08x\n", data); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Chris Packham | df16881 | 2018-01-22 22:44:20 +1300 | [diff] [blame] | 178 | static void mvebu_spi_50mhz_ac_timing_erratum(struct udevice *bus, uint mode) |
| 179 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 180 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Chris Packham | df16881 | 2018-01-22 22:44:20 +1300 | [diff] [blame] | 181 | struct kwspi_registers *reg = plat->spireg; |
| 182 | u32 data; |
| 183 | |
| 184 | /* |
| 185 | * Erratum description: (Erratum NO. FE-9144572) The device |
| 186 | * SPI interface supports frequencies of up to 50 MHz. |
| 187 | * However, due to this erratum, when the device core clock is |
| 188 | * 250 MHz and the SPI interfaces is configured for 50MHz SPI |
| 189 | * clock and CPOL=CPHA=1 there might occur data corruption on |
| 190 | * reads from the SPI device. |
| 191 | * Erratum Workaround: |
| 192 | * Work in one of the following configurations: |
| 193 | * 1. Set CPOL=CPHA=0 in "SPI Interface Configuration |
| 194 | * Register". |
| 195 | * 2. Set TMISO_SAMPLE value to 0x2 in "SPI Timing Parameters 1 |
| 196 | * Register" before setting the interface. |
| 197 | */ |
| 198 | data = readl(®->timing1); |
| 199 | data &= ~KW_SPI_TMISO_SAMPLE_MASK; |
| 200 | |
| 201 | if (CONFIG_SYS_TCLK == 250000000 && |
| 202 | mode & SPI_CPOL && |
| 203 | mode & SPI_CPHA) |
| 204 | data |= KW_SPI_TMISO_SAMPLE_2; |
| 205 | else |
| 206 | data |= KW_SPI_TMISO_SAMPLE_1; |
| 207 | |
| 208 | writel(data, ®->timing1); |
| 209 | } |
| 210 | |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 211 | static int mvebu_spi_set_mode(struct udevice *bus, uint mode) |
| 212 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 213 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Chris Packham | ebfa18c | 2016-10-27 21:16:05 +1300 | [diff] [blame] | 214 | struct kwspi_registers *reg = plat->spireg; |
| 215 | u32 data = readl(®->cfg); |
| 216 | |
| 217 | data &= ~(KWSPI_CPHA | KWSPI_CPOL | KWSPI_RXLSBF | KWSPI_TXLSBF); |
| 218 | |
| 219 | if (mode & SPI_CPHA) |
| 220 | data |= KWSPI_CPHA; |
| 221 | if (mode & SPI_CPOL) |
| 222 | data |= KWSPI_CPOL; |
| 223 | if (mode & SPI_LSB_FIRST) |
| 224 | data |= (KWSPI_RXLSBF | KWSPI_TXLSBF); |
| 225 | |
| 226 | writel(data, ®->cfg); |
| 227 | |
Jagan Teki | f5ff46f | 2018-03-15 17:03:22 +0530 | [diff] [blame] | 228 | if (plat->is_errata_50mhz_ac) |
Chris Packham | df16881 | 2018-01-22 22:44:20 +1300 | [diff] [blame] | 229 | mvebu_spi_50mhz_ac_timing_erratum(bus, mode); |
| 230 | |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static int mvebu_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 235 | const void *dout, void *din, unsigned long flags) |
| 236 | { |
| 237 | struct udevice *bus = dev->parent; |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 238 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 239 | |
| 240 | return _spi_xfer(plat->spireg, bitlen, dout, din, flags); |
| 241 | } |
| 242 | |
Pascal Linder | f169653 | 2019-06-18 08:41:01 +0200 | [diff] [blame] | 243 | __attribute__((weak)) int mvebu_board_spi_claim_bus(struct udevice *dev) |
| 244 | { |
| 245 | return 0; |
| 246 | } |
| 247 | |
Stefan Roese | 9fc5663 | 2016-02-11 11:37:38 +0100 | [diff] [blame] | 248 | static int mvebu_spi_claim_bus(struct udevice *dev) |
| 249 | { |
| 250 | struct udevice *bus = dev->parent; |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 251 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 9fc5663 | 2016-02-11 11:37:38 +0100 | [diff] [blame] | 252 | |
| 253 | /* Configure the chip-select in the CTRL register */ |
| 254 | clrsetbits_le32(&plat->spireg->ctrl, |
| 255 | KWSPI_CS_MASK << KWSPI_CS_SHIFT, |
| 256 | spi_chip_select(dev) << KWSPI_CS_SHIFT); |
| 257 | |
Pascal Linder | f169653 | 2019-06-18 08:41:01 +0200 | [diff] [blame] | 258 | return mvebu_board_spi_claim_bus(dev); |
| 259 | } |
| 260 | |
| 261 | __attribute__((weak)) int mvebu_board_spi_release_bus(struct udevice *dev) |
| 262 | { |
Stefan Roese | 9fc5663 | 2016-02-11 11:37:38 +0100 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
Pascal Linder | f169653 | 2019-06-18 08:41:01 +0200 | [diff] [blame] | 266 | static int mvebu_spi_release_bus(struct udevice *dev) |
| 267 | { |
| 268 | return mvebu_board_spi_release_bus(dev); |
| 269 | } |
| 270 | |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 271 | static int mvebu_spi_probe(struct udevice *bus) |
| 272 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 273 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 274 | struct kwspi_registers *reg = plat->spireg; |
| 275 | |
| 276 | writel(KWSPI_SMEMRDY, ®->ctrl); |
| 277 | writel(KWSPI_SMEMRDIRQ, ®->irq_cause); |
| 278 | writel(KWSPI_IRQMASK, ®->irq_mask); |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 283 | static int mvebu_spi_of_to_plat(struct udevice *bus) |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 284 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 285 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Jagan Teki | f5ff46f | 2018-03-15 17:03:22 +0530 | [diff] [blame] | 286 | const struct mvebu_spi_dev *drvdata = |
| 287 | (struct mvebu_spi_dev *)dev_get_driver_data(bus); |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 288 | |
Masahiro Yamada | 8613c8d | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 289 | plat->spireg = dev_read_addr_ptr(bus); |
Jagan Teki | f5ff46f | 2018-03-15 17:03:22 +0530 | [diff] [blame] | 290 | plat->is_errata_50mhz_ac = drvdata->is_errata_50mhz_ac; |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static const struct dm_spi_ops mvebu_spi_ops = { |
Stefan Roese | 9fc5663 | 2016-02-11 11:37:38 +0100 | [diff] [blame] | 296 | .claim_bus = mvebu_spi_claim_bus, |
Pascal Linder | f169653 | 2019-06-18 08:41:01 +0200 | [diff] [blame] | 297 | .release_bus = mvebu_spi_release_bus, |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 298 | .xfer = mvebu_spi_xfer, |
| 299 | .set_speed = mvebu_spi_set_speed, |
| 300 | .set_mode = mvebu_spi_set_mode, |
| 301 | /* |
| 302 | * cs_info is not needed, since we require all chip selects to be |
| 303 | * in the device tree explicitly |
| 304 | */ |
| 305 | }; |
| 306 | |
Chris Packham | 4f4dde0 | 2018-08-01 12:19:26 +0530 | [diff] [blame] | 307 | static const struct mvebu_spi_dev armada_spi_dev_data = { |
| 308 | .is_errata_50mhz_ac = false, |
| 309 | }; |
| 310 | |
Chris Packham | df16881 | 2018-01-22 22:44:20 +1300 | [diff] [blame] | 311 | static const struct mvebu_spi_dev armada_xp_spi_dev_data = { |
| 312 | .is_errata_50mhz_ac = false, |
| 313 | }; |
| 314 | |
| 315 | static const struct mvebu_spi_dev armada_375_spi_dev_data = { |
| 316 | .is_errata_50mhz_ac = false, |
| 317 | }; |
| 318 | |
| 319 | static const struct mvebu_spi_dev armada_380_spi_dev_data = { |
| 320 | .is_errata_50mhz_ac = true, |
| 321 | }; |
| 322 | |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 323 | static const struct udevice_id mvebu_spi_ids[] = { |
Chris Packham | df16881 | 2018-01-22 22:44:20 +1300 | [diff] [blame] | 324 | { |
Chris Packham | 4f4dde0 | 2018-08-01 12:19:26 +0530 | [diff] [blame] | 325 | .compatible = "marvell,orion-spi", |
| 326 | .data = (ulong)&armada_spi_dev_data, |
| 327 | }, |
| 328 | { |
Chris Packham | df16881 | 2018-01-22 22:44:20 +1300 | [diff] [blame] | 329 | .compatible = "marvell,armada-375-spi", |
| 330 | .data = (ulong)&armada_375_spi_dev_data |
| 331 | }, |
| 332 | { |
| 333 | .compatible = "marvell,armada-380-spi", |
| 334 | .data = (ulong)&armada_380_spi_dev_data |
| 335 | }, |
| 336 | { |
| 337 | .compatible = "marvell,armada-xp-spi", |
| 338 | .data = (ulong)&armada_xp_spi_dev_data |
| 339 | }, |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 340 | { } |
| 341 | }; |
| 342 | |
| 343 | U_BOOT_DRIVER(mvebu_spi) = { |
| 344 | .name = "mvebu_spi", |
| 345 | .id = UCLASS_SPI, |
| 346 | .of_match = mvebu_spi_ids, |
| 347 | .ops = &mvebu_spi_ops, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 348 | .of_to_plat = mvebu_spi_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 349 | .plat_auto = sizeof(struct mvebu_spi_plat), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 350 | .priv_auto = sizeof(struct mvebu_spi_priv), |
Stefan Roese | 9985bdb | 2015-11-20 13:39:43 +0100 | [diff] [blame] | 351 | .probe = mvebu_spi_probe, |
| 352 | }; |