Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Marvell International Ltd. |
| 4 | * |
| 5 | * Copyright (C) 2016 Stefan Roese <sr@denx.de> |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <spi.h> |
Marek Behún | 0afd934 | 2018-04-24 17:21:26 +0200 | [diff] [blame] | 12 | #include <clk.h> |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 13 | #include <wait_bit.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 15 | #include <asm/io.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
George Hilliard | e286380 | 2020-09-30 09:28:21 -0500 | [diff] [blame] | 18 | #include <asm/gpio.h> |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | #define MVEBU_SPI_A3700_XFER_RDY BIT(1) |
| 23 | #define MVEBU_SPI_A3700_FIFO_FLUSH BIT(9) |
| 24 | #define MVEBU_SPI_A3700_BYTE_LEN BIT(5) |
| 25 | #define MVEBU_SPI_A3700_CLK_PHA BIT(6) |
| 26 | #define MVEBU_SPI_A3700_CLK_POL BIT(7) |
| 27 | #define MVEBU_SPI_A3700_FIFO_EN BIT(17) |
| 28 | #define MVEBU_SPI_A3700_SPI_EN_0 BIT(16) |
Marek Behún | 0afd934 | 2018-04-24 17:21:26 +0200 | [diff] [blame] | 29 | #define MVEBU_SPI_A3700_CLK_PRESCALE_MASK 0x1f |
| 30 | |
George Hilliard | e286380 | 2020-09-30 09:28:21 -0500 | [diff] [blame] | 31 | #define MAX_CS_COUNT 4 |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 32 | |
| 33 | /* SPI registers */ |
| 34 | struct spi_reg { |
| 35 | u32 ctrl; /* 0x10600 */ |
| 36 | u32 cfg; /* 0x10604 */ |
| 37 | u32 dout; /* 0x10608 */ |
| 38 | u32 din; /* 0x1060c */ |
| 39 | }; |
| 40 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 41 | struct mvebu_spi_plat { |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 42 | struct spi_reg *spireg; |
Marek Behún | 0afd934 | 2018-04-24 17:21:26 +0200 | [diff] [blame] | 43 | struct clk clk; |
George Hilliard | e286380 | 2020-09-30 09:28:21 -0500 | [diff] [blame] | 44 | struct gpio_desc cs_gpios[MAX_CS_COUNT]; |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 47 | static void spi_cs_activate(struct mvebu_spi_plat *plat, int cs) |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 48 | { |
George Hilliard | e286380 | 2020-09-30 09:28:21 -0500 | [diff] [blame] | 49 | if (CONFIG_IS_ENABLED(DM_GPIO) && dm_gpio_is_valid(&plat->cs_gpios[cs])) |
| 50 | dm_gpio_set_value(&plat->cs_gpios[cs], 1); |
| 51 | else |
| 52 | setbits_le32(&plat->spireg->ctrl, MVEBU_SPI_A3700_SPI_EN_0 << cs); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 55 | static void spi_cs_deactivate(struct mvebu_spi_plat *plat, int cs) |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 56 | { |
George Hilliard | e286380 | 2020-09-30 09:28:21 -0500 | [diff] [blame] | 57 | if (CONFIG_IS_ENABLED(DM_GPIO) && dm_gpio_is_valid(&plat->cs_gpios[cs])) |
| 58 | dm_gpio_set_value(&plat->cs_gpios[cs], 0); |
| 59 | else |
| 60 | clrbits_le32(&plat->spireg->ctrl, MVEBU_SPI_A3700_SPI_EN_0 << cs); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * spi_legacy_shift_byte() - triggers the real SPI transfer |
| 65 | * @bytelen: Indicate how many bytes to transfer. |
| 66 | * @dout: Buffer address of what to send. |
| 67 | * @din: Buffer address of where to receive. |
| 68 | * |
| 69 | * This function triggers the real SPI transfer in legacy mode. It |
| 70 | * will shift out char buffer from @dout, and shift in char buffer to |
| 71 | * @din, if necessary. |
| 72 | * |
| 73 | * This function assumes that only one byte is shifted at one time. |
| 74 | * However, it is not its responisbility to set the transfer type to |
| 75 | * one-byte. Also, it does not guarantee that it will work if transfer |
| 76 | * type becomes two-byte. See spi_set_legacy() for details. |
| 77 | * |
| 78 | * In legacy mode, simply write to the SPI_DOUT register will trigger |
| 79 | * the transfer. |
| 80 | * |
| 81 | * If @dout == NULL, which means no actual data needs to be sent out, |
| 82 | * then the function will shift out 0x00 in order to shift in data. |
| 83 | * The XFER_RDY flag is checked every time before accessing SPI_DOUT |
| 84 | * and SPI_DIN register. |
| 85 | * |
Pengfei Fan | 746271d | 2022-12-09 09:39:50 +0800 | [diff] [blame] | 86 | * The number of transfers to be triggered is decided by @bytelen. |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 87 | * |
| 88 | * Return: 0 - cool |
| 89 | * -ETIMEDOUT - XFER_RDY flag timeout |
| 90 | */ |
| 91 | static int spi_legacy_shift_byte(struct spi_reg *reg, unsigned int bytelen, |
| 92 | const void *dout, void *din) |
| 93 | { |
| 94 | const u8 *dout_8; |
| 95 | u8 *din_8; |
| 96 | int ret; |
| 97 | |
| 98 | /* Use 0x00 as dummy dout */ |
| 99 | const u8 dummy_dout = 0x0; |
| 100 | u32 pending_dout = 0x0; |
| 101 | |
| 102 | /* dout_8: pointer of current dout */ |
| 103 | dout_8 = dout; |
| 104 | /* din_8: pointer of current din */ |
| 105 | din_8 = din; |
| 106 | |
| 107 | while (bytelen) { |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 108 | ret = wait_for_bit_le32(®->ctrl, |
| 109 | MVEBU_SPI_A3700_XFER_RDY, |
| 110 | true,100, false); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 111 | if (ret) |
| 112 | return ret; |
| 113 | |
| 114 | if (dout) |
| 115 | pending_dout = (u32)*dout_8; |
| 116 | else |
| 117 | pending_dout = (u32)dummy_dout; |
| 118 | |
| 119 | /* Trigger the xfer */ |
| 120 | writel(pending_dout, ®->dout); |
| 121 | |
| 122 | if (din) { |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 123 | ret = wait_for_bit_le32(®->ctrl, |
| 124 | MVEBU_SPI_A3700_XFER_RDY, |
| 125 | true, 100, false); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 126 | if (ret) |
| 127 | return ret; |
| 128 | |
| 129 | /* Read what is transferred in */ |
| 130 | *din_8 = (u8)readl(®->din); |
| 131 | } |
| 132 | |
| 133 | /* Don't increment the current pointer if NULL */ |
| 134 | if (dout) |
| 135 | dout_8++; |
| 136 | if (din) |
| 137 | din_8++; |
| 138 | |
| 139 | bytelen--; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int mvebu_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 146 | const void *dout, void *din, unsigned long flags) |
| 147 | { |
| 148 | struct udevice *bus = dev->parent; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 149 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 150 | struct spi_reg *reg = plat->spireg; |
| 151 | unsigned int bytelen; |
| 152 | int ret; |
| 153 | |
| 154 | bytelen = bitlen / 8; |
| 155 | |
| 156 | if (dout && din) |
| 157 | debug("This is a duplex transfer.\n"); |
| 158 | |
| 159 | /* Activate CS */ |
| 160 | if (flags & SPI_XFER_BEGIN) { |
| 161 | debug("SPI: activate cs.\n"); |
George Hilliard | e286380 | 2020-09-30 09:28:21 -0500 | [diff] [blame] | 162 | spi_cs_activate(plat, spi_chip_select(dev)); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Send and/or receive */ |
| 166 | if (dout || din) { |
| 167 | ret = spi_legacy_shift_byte(reg, bytelen, dout, din); |
| 168 | if (ret) |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | /* Deactivate CS */ |
| 173 | if (flags & SPI_XFER_END) { |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 174 | ret = wait_for_bit_le32(®->ctrl, |
| 175 | MVEBU_SPI_A3700_XFER_RDY, |
| 176 | true, 100, false); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 177 | if (ret) |
| 178 | return ret; |
| 179 | |
| 180 | debug("SPI: deactivate cs.\n"); |
George Hilliard | e286380 | 2020-09-30 09:28:21 -0500 | [diff] [blame] | 181 | spi_cs_deactivate(plat, spi_chip_select(dev)); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static int mvebu_spi_set_speed(struct udevice *bus, uint hz) |
| 188 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 189 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 190 | struct spi_reg *reg = plat->spireg; |
Marek Behún | 0afd934 | 2018-04-24 17:21:26 +0200 | [diff] [blame] | 191 | u32 data, prescale; |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 192 | |
| 193 | data = readl(®->cfg); |
| 194 | |
Marek Behún | 0afd934 | 2018-04-24 17:21:26 +0200 | [diff] [blame] | 195 | prescale = DIV_ROUND_UP(clk_get_rate(&plat->clk), hz); |
Marek Behún | a792616 | 2019-07-23 16:49:32 +0200 | [diff] [blame] | 196 | if (prescale > 0xf) |
Marek Behún | 0afd934 | 2018-04-24 17:21:26 +0200 | [diff] [blame] | 197 | prescale = 0x10 + (prescale + 1) / 2; |
Marek Behún | a792616 | 2019-07-23 16:49:32 +0200 | [diff] [blame] | 198 | prescale = min(prescale, 0x1fu); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 199 | |
Marek Behún | 0afd934 | 2018-04-24 17:21:26 +0200 | [diff] [blame] | 200 | data &= ~MVEBU_SPI_A3700_CLK_PRESCALE_MASK; |
| 201 | data |= prescale & MVEBU_SPI_A3700_CLK_PRESCALE_MASK; |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 202 | |
| 203 | writel(data, ®->cfg); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int mvebu_spi_set_mode(struct udevice *bus, uint mode) |
| 209 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 210 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 211 | struct spi_reg *reg = plat->spireg; |
| 212 | |
| 213 | /* |
| 214 | * Set SPI polarity |
| 215 | * 0: Serial interface clock is low when inactive |
| 216 | * 1: Serial interface clock is high when inactive |
| 217 | */ |
| 218 | if (mode & SPI_CPOL) |
| 219 | setbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_POL); |
| 220 | else |
| 221 | clrbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_POL); |
| 222 | if (mode & SPI_CPHA) |
| 223 | setbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_PHA); |
| 224 | else |
| 225 | clrbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_PHA); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int mvebu_spi_probe(struct udevice *bus) |
| 231 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 232 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 233 | struct spi_reg *reg = plat->spireg; |
| 234 | u32 data; |
| 235 | int ret; |
| 236 | |
| 237 | /* |
| 238 | * Settings SPI controller to be working in legacy mode, which |
| 239 | * means use only DO pin (I/O 1) for Data Out, and DI pin (I/O 0) |
| 240 | * for Data In. |
| 241 | */ |
| 242 | |
| 243 | /* Flush read/write FIFO */ |
| 244 | data = readl(®->cfg); |
| 245 | writel(data | MVEBU_SPI_A3700_FIFO_FLUSH, ®->cfg); |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 246 | ret = wait_for_bit_le32(®->cfg, MVEBU_SPI_A3700_FIFO_FLUSH, |
| 247 | false, 1000, false); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 248 | if (ret) |
| 249 | return ret; |
| 250 | |
| 251 | /* Disable FIFO mode */ |
| 252 | data &= ~MVEBU_SPI_A3700_FIFO_EN; |
| 253 | |
| 254 | /* Always shift 1 byte at a time */ |
| 255 | data &= ~MVEBU_SPI_A3700_BYTE_LEN; |
| 256 | |
| 257 | writel(data, ®->cfg); |
| 258 | |
George Hilliard | e286380 | 2020-09-30 09:28:21 -0500 | [diff] [blame] | 259 | /* Set up CS GPIOs in device tree, if any */ |
| 260 | if (CONFIG_IS_ENABLED(DM_GPIO) && gpio_get_list_count(bus, "cs-gpios") > 0) { |
| 261 | int i; |
| 262 | |
| 263 | for (i = 0; i < ARRAY_SIZE(plat->cs_gpios); i++) { |
| 264 | ret = gpio_request_by_name(bus, "cs-gpios", i, &plat->cs_gpios[i], 0); |
| 265 | if (ret < 0 || !dm_gpio_is_valid(&plat->cs_gpios[i])) { |
| 266 | /* Use the native CS function for this line */ |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | ret = dm_gpio_set_dir_flags(&plat->cs_gpios[i], |
| 271 | GPIOD_IS_OUT | GPIOD_ACTIVE_LOW); |
| 272 | if (ret) { |
| 273 | dev_err(bus, "Setting cs %d error\n", i); |
| 274 | return ret; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 282 | static int mvebu_spi_of_to_plat(struct udevice *bus) |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 283 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 284 | struct mvebu_spi_plat *plat = dev_get_plat(bus); |
Marek Behún | 0afd934 | 2018-04-24 17:21:26 +0200 | [diff] [blame] | 285 | int ret; |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 286 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 287 | plat->spireg = dev_read_addr_ptr(bus); |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 288 | |
Marek Behún | 0afd934 | 2018-04-24 17:21:26 +0200 | [diff] [blame] | 289 | ret = clk_get_by_index(bus, 0, &plat->clk); |
| 290 | if (ret) { |
| 291 | dev_err(bus, "cannot get clock\n"); |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 298 | static const struct dm_spi_ops mvebu_spi_ops = { |
| 299 | .xfer = mvebu_spi_xfer, |
| 300 | .set_speed = mvebu_spi_set_speed, |
| 301 | .set_mode = mvebu_spi_set_mode, |
| 302 | /* |
| 303 | * cs_info is not needed, since we require all chip selects to be |
| 304 | * in the device tree explicitly |
| 305 | */ |
| 306 | }; |
| 307 | |
| 308 | static const struct udevice_id mvebu_spi_ids[] = { |
| 309 | { .compatible = "marvell,armada-3700-spi" }, |
| 310 | { } |
| 311 | }; |
| 312 | |
| 313 | U_BOOT_DRIVER(mvebu_spi) = { |
| 314 | .name = "mvebu_spi", |
| 315 | .id = UCLASS_SPI, |
| 316 | .of_match = mvebu_spi_ids, |
| 317 | .ops = &mvebu_spi_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 318 | .of_to_plat = mvebu_spi_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 319 | .plat_auto = sizeof(struct mvebu_spi_plat), |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 320 | .probe = mvebu_spi_probe, |
Stefan Roese | 9ec1c78 | 2016-05-19 15:56:44 +0200 | [diff] [blame] | 321 | }; |