Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Marvell Armada-3700 SPI controller driver |
| 3 | * |
| 4 | * Copyright (C) 2016 Marvell Ltd. |
| 5 | * |
| 6 | * Author: Wilson Ding <dingwei@marvell.com> |
| 7 | * Author: Romain Perier <romain.perier@free-electrons.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/completion.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/of_irq.h> |
| 24 | #include <linux/of_device.h> |
| 25 | #include <linux/pinctrl/consumer.h> |
| 26 | #include <linux/spi/spi.h> |
| 27 | |
| 28 | #define DRIVER_NAME "armada_3700_spi" |
| 29 | |
| 30 | #define A3700_SPI_TIMEOUT 10 |
| 31 | |
| 32 | /* SPI Register Offest */ |
| 33 | #define A3700_SPI_IF_CTRL_REG 0x00 |
| 34 | #define A3700_SPI_IF_CFG_REG 0x04 |
| 35 | #define A3700_SPI_DATA_OUT_REG 0x08 |
| 36 | #define A3700_SPI_DATA_IN_REG 0x0C |
| 37 | #define A3700_SPI_IF_INST_REG 0x10 |
| 38 | #define A3700_SPI_IF_ADDR_REG 0x14 |
| 39 | #define A3700_SPI_IF_RMODE_REG 0x18 |
| 40 | #define A3700_SPI_IF_HDR_CNT_REG 0x1C |
| 41 | #define A3700_SPI_IF_DIN_CNT_REG 0x20 |
| 42 | #define A3700_SPI_IF_TIME_REG 0x24 |
| 43 | #define A3700_SPI_INT_STAT_REG 0x28 |
| 44 | #define A3700_SPI_INT_MASK_REG 0x2C |
| 45 | |
| 46 | /* A3700_SPI_IF_CTRL_REG */ |
| 47 | #define A3700_SPI_EN BIT(16) |
| 48 | #define A3700_SPI_ADDR_NOT_CONFIG BIT(12) |
| 49 | #define A3700_SPI_WFIFO_OVERFLOW BIT(11) |
| 50 | #define A3700_SPI_WFIFO_UNDERFLOW BIT(10) |
| 51 | #define A3700_SPI_RFIFO_OVERFLOW BIT(9) |
| 52 | #define A3700_SPI_RFIFO_UNDERFLOW BIT(8) |
| 53 | #define A3700_SPI_WFIFO_FULL BIT(7) |
| 54 | #define A3700_SPI_WFIFO_EMPTY BIT(6) |
| 55 | #define A3700_SPI_RFIFO_FULL BIT(5) |
| 56 | #define A3700_SPI_RFIFO_EMPTY BIT(4) |
| 57 | #define A3700_SPI_WFIFO_RDY BIT(3) |
| 58 | #define A3700_SPI_RFIFO_RDY BIT(2) |
| 59 | #define A3700_SPI_XFER_RDY BIT(1) |
| 60 | #define A3700_SPI_XFER_DONE BIT(0) |
| 61 | |
| 62 | /* A3700_SPI_IF_CFG_REG */ |
| 63 | #define A3700_SPI_WFIFO_THRS BIT(28) |
| 64 | #define A3700_SPI_RFIFO_THRS BIT(24) |
| 65 | #define A3700_SPI_AUTO_CS BIT(20) |
| 66 | #define A3700_SPI_DMA_RD_EN BIT(18) |
| 67 | #define A3700_SPI_FIFO_MODE BIT(17) |
| 68 | #define A3700_SPI_SRST BIT(16) |
| 69 | #define A3700_SPI_XFER_START BIT(15) |
| 70 | #define A3700_SPI_XFER_STOP BIT(14) |
| 71 | #define A3700_SPI_INST_PIN BIT(13) |
| 72 | #define A3700_SPI_ADDR_PIN BIT(12) |
| 73 | #define A3700_SPI_DATA_PIN1 BIT(11) |
| 74 | #define A3700_SPI_DATA_PIN0 BIT(10) |
| 75 | #define A3700_SPI_FIFO_FLUSH BIT(9) |
| 76 | #define A3700_SPI_RW_EN BIT(8) |
| 77 | #define A3700_SPI_CLK_POL BIT(7) |
| 78 | #define A3700_SPI_CLK_PHA BIT(6) |
| 79 | #define A3700_SPI_BYTE_LEN BIT(5) |
| 80 | #define A3700_SPI_CLK_PRESCALE BIT(0) |
| 81 | #define A3700_SPI_CLK_PRESCALE_MASK (0x1f) |
| 82 | |
| 83 | #define A3700_SPI_WFIFO_THRS_BIT 28 |
| 84 | #define A3700_SPI_RFIFO_THRS_BIT 24 |
| 85 | #define A3700_SPI_FIFO_THRS_MASK 0x7 |
| 86 | |
| 87 | #define A3700_SPI_DATA_PIN_MASK 0x3 |
| 88 | |
| 89 | /* A3700_SPI_IF_HDR_CNT_REG */ |
| 90 | #define A3700_SPI_DUMMY_CNT_BIT 12 |
| 91 | #define A3700_SPI_DUMMY_CNT_MASK 0x7 |
| 92 | #define A3700_SPI_RMODE_CNT_BIT 8 |
| 93 | #define A3700_SPI_RMODE_CNT_MASK 0x3 |
| 94 | #define A3700_SPI_ADDR_CNT_BIT 4 |
| 95 | #define A3700_SPI_ADDR_CNT_MASK 0x7 |
| 96 | #define A3700_SPI_INSTR_CNT_BIT 0 |
| 97 | #define A3700_SPI_INSTR_CNT_MASK 0x3 |
| 98 | |
| 99 | /* A3700_SPI_IF_TIME_REG */ |
| 100 | #define A3700_SPI_CLK_CAPT_EDGE BIT(7) |
| 101 | |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 102 | struct a3700_spi { |
| 103 | struct spi_master *master; |
| 104 | void __iomem *base; |
| 105 | struct clk *clk; |
| 106 | unsigned int irq; |
| 107 | unsigned int flags; |
| 108 | bool xmit_data; |
| 109 | const u8 *tx_buf; |
| 110 | u8 *rx_buf; |
| 111 | size_t buf_len; |
| 112 | u8 byte_len; |
| 113 | u32 wait_mask; |
| 114 | struct completion done; |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static u32 spireg_read(struct a3700_spi *a3700_spi, u32 offset) |
| 118 | { |
| 119 | return readl(a3700_spi->base + offset); |
| 120 | } |
| 121 | |
| 122 | static void spireg_write(struct a3700_spi *a3700_spi, u32 offset, u32 data) |
| 123 | { |
| 124 | writel(data, a3700_spi->base + offset); |
| 125 | } |
| 126 | |
| 127 | static void a3700_spi_auto_cs_unset(struct a3700_spi *a3700_spi) |
| 128 | { |
| 129 | u32 val; |
| 130 | |
| 131 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 132 | val &= ~A3700_SPI_AUTO_CS; |
| 133 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 134 | } |
| 135 | |
| 136 | static void a3700_spi_activate_cs(struct a3700_spi *a3700_spi, unsigned int cs) |
| 137 | { |
| 138 | u32 val; |
| 139 | |
| 140 | val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG); |
| 141 | val |= (A3700_SPI_EN << cs); |
| 142 | spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val); |
| 143 | } |
| 144 | |
| 145 | static void a3700_spi_deactivate_cs(struct a3700_spi *a3700_spi, |
| 146 | unsigned int cs) |
| 147 | { |
| 148 | u32 val; |
| 149 | |
| 150 | val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG); |
| 151 | val &= ~(A3700_SPI_EN << cs); |
| 152 | spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val); |
| 153 | } |
| 154 | |
| 155 | static int a3700_spi_pin_mode_set(struct a3700_spi *a3700_spi, |
Miquel Raynal | 747e1f6 | 2017-09-13 18:21:38 +0200 | [diff] [blame] | 156 | unsigned int pin_mode, bool receiving) |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 157 | { |
| 158 | u32 val; |
| 159 | |
| 160 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 161 | val &= ~(A3700_SPI_INST_PIN | A3700_SPI_ADDR_PIN); |
| 162 | val &= ~(A3700_SPI_DATA_PIN0 | A3700_SPI_DATA_PIN1); |
| 163 | |
| 164 | switch (pin_mode) { |
Romain Perier | cfd6693 | 2016-12-21 11:10:30 +0100 | [diff] [blame] | 165 | case SPI_NBITS_SINGLE: |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 166 | break; |
Romain Perier | cfd6693 | 2016-12-21 11:10:30 +0100 | [diff] [blame] | 167 | case SPI_NBITS_DUAL: |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 168 | val |= A3700_SPI_DATA_PIN0; |
| 169 | break; |
Romain Perier | cfd6693 | 2016-12-21 11:10:30 +0100 | [diff] [blame] | 170 | case SPI_NBITS_QUAD: |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 171 | val |= A3700_SPI_DATA_PIN1; |
Miquel Raynal | 747e1f6 | 2017-09-13 18:21:38 +0200 | [diff] [blame] | 172 | /* RX during address reception uses 4-pin */ |
| 173 | if (receiving) |
| 174 | val |= A3700_SPI_ADDR_PIN; |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 175 | break; |
| 176 | default: |
| 177 | dev_err(&a3700_spi->master->dev, "wrong pin mode %u", pin_mode); |
| 178 | return -EINVAL; |
| 179 | } |
| 180 | |
| 181 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static void a3700_spi_fifo_mode_set(struct a3700_spi *a3700_spi) |
| 187 | { |
| 188 | u32 val; |
| 189 | |
| 190 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 191 | val |= A3700_SPI_FIFO_MODE; |
| 192 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 193 | } |
| 194 | |
| 195 | static void a3700_spi_mode_set(struct a3700_spi *a3700_spi, |
| 196 | unsigned int mode_bits) |
| 197 | { |
| 198 | u32 val; |
| 199 | |
| 200 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 201 | |
| 202 | if (mode_bits & SPI_CPOL) |
| 203 | val |= A3700_SPI_CLK_POL; |
| 204 | else |
| 205 | val &= ~A3700_SPI_CLK_POL; |
| 206 | |
| 207 | if (mode_bits & SPI_CPHA) |
| 208 | val |= A3700_SPI_CLK_PHA; |
| 209 | else |
| 210 | val &= ~A3700_SPI_CLK_PHA; |
| 211 | |
| 212 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 213 | } |
| 214 | |
| 215 | static void a3700_spi_clock_set(struct a3700_spi *a3700_spi, |
| 216 | unsigned int speed_hz, u16 mode) |
| 217 | { |
| 218 | u32 val; |
| 219 | u32 prescale; |
| 220 | |
| 221 | prescale = DIV_ROUND_UP(clk_get_rate(a3700_spi->clk), speed_hz); |
| 222 | |
| 223 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 224 | val = val & ~A3700_SPI_CLK_PRESCALE_MASK; |
| 225 | |
| 226 | val = val | (prescale & A3700_SPI_CLK_PRESCALE_MASK); |
| 227 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 228 | |
| 229 | if (prescale <= 2) { |
| 230 | val = spireg_read(a3700_spi, A3700_SPI_IF_TIME_REG); |
| 231 | val |= A3700_SPI_CLK_CAPT_EDGE; |
| 232 | spireg_write(a3700_spi, A3700_SPI_IF_TIME_REG, val); |
| 233 | } |
| 234 | |
| 235 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 236 | val &= ~(A3700_SPI_CLK_POL | A3700_SPI_CLK_PHA); |
| 237 | |
| 238 | if (mode & SPI_CPOL) |
| 239 | val |= A3700_SPI_CLK_POL; |
| 240 | |
| 241 | if (mode & SPI_CPHA) |
| 242 | val |= A3700_SPI_CLK_PHA; |
| 243 | |
| 244 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 245 | } |
| 246 | |
| 247 | static void a3700_spi_bytelen_set(struct a3700_spi *a3700_spi, unsigned int len) |
| 248 | { |
| 249 | u32 val; |
| 250 | |
| 251 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 252 | if (len == 4) |
| 253 | val |= A3700_SPI_BYTE_LEN; |
| 254 | else |
| 255 | val &= ~A3700_SPI_BYTE_LEN; |
| 256 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 257 | |
| 258 | a3700_spi->byte_len = len; |
| 259 | } |
| 260 | |
| 261 | static int a3700_spi_fifo_flush(struct a3700_spi *a3700_spi) |
| 262 | { |
| 263 | int timeout = A3700_SPI_TIMEOUT; |
| 264 | u32 val; |
| 265 | |
| 266 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 267 | val |= A3700_SPI_FIFO_FLUSH; |
| 268 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 269 | |
| 270 | while (--timeout) { |
| 271 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 272 | if (!(val & A3700_SPI_FIFO_FLUSH)) |
| 273 | return 0; |
| 274 | udelay(1); |
| 275 | } |
| 276 | |
| 277 | return -ETIMEDOUT; |
| 278 | } |
| 279 | |
| 280 | static int a3700_spi_init(struct a3700_spi *a3700_spi) |
| 281 | { |
| 282 | struct spi_master *master = a3700_spi->master; |
| 283 | u32 val; |
| 284 | int i, ret = 0; |
| 285 | |
| 286 | /* Reset SPI unit */ |
| 287 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 288 | val |= A3700_SPI_SRST; |
| 289 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 290 | |
| 291 | udelay(A3700_SPI_TIMEOUT); |
| 292 | |
| 293 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 294 | val &= ~A3700_SPI_SRST; |
| 295 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 296 | |
| 297 | /* Disable AUTO_CS and deactivate all chip-selects */ |
| 298 | a3700_spi_auto_cs_unset(a3700_spi); |
| 299 | for (i = 0; i < master->num_chipselect; i++) |
| 300 | a3700_spi_deactivate_cs(a3700_spi, i); |
| 301 | |
| 302 | /* Enable FIFO mode */ |
| 303 | a3700_spi_fifo_mode_set(a3700_spi); |
| 304 | |
| 305 | /* Set SPI mode */ |
| 306 | a3700_spi_mode_set(a3700_spi, master->mode_bits); |
| 307 | |
| 308 | /* Reset counters */ |
| 309 | spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0); |
| 310 | spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG, 0); |
| 311 | |
| 312 | /* Mask the interrupts and clear cause bits */ |
| 313 | spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0); |
| 314 | spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, ~0U); |
| 315 | |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | static irqreturn_t a3700_spi_interrupt(int irq, void *dev_id) |
| 320 | { |
| 321 | struct spi_master *master = dev_id; |
| 322 | struct a3700_spi *a3700_spi; |
| 323 | u32 cause; |
| 324 | |
| 325 | a3700_spi = spi_master_get_devdata(master); |
| 326 | |
| 327 | /* Get interrupt causes */ |
| 328 | cause = spireg_read(a3700_spi, A3700_SPI_INT_STAT_REG); |
| 329 | |
| 330 | if (!cause || !(a3700_spi->wait_mask & cause)) |
| 331 | return IRQ_NONE; |
| 332 | |
| 333 | /* mask and acknowledge the SPI interrupts */ |
| 334 | spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0); |
| 335 | spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, cause); |
| 336 | |
| 337 | /* Wake up the transfer */ |
Dan Carpenter | 0cc059a | 2016-12-16 12:33:59 +0300 | [diff] [blame] | 338 | complete(&a3700_spi->done); |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 339 | |
| 340 | return IRQ_HANDLED; |
| 341 | } |
| 342 | |
| 343 | static bool a3700_spi_wait_completion(struct spi_device *spi) |
| 344 | { |
| 345 | struct a3700_spi *a3700_spi; |
| 346 | unsigned int timeout; |
| 347 | unsigned int ctrl_reg; |
| 348 | unsigned long timeout_jiffies; |
| 349 | |
| 350 | a3700_spi = spi_master_get_devdata(spi->master); |
| 351 | |
| 352 | /* SPI interrupt is edge-triggered, which means an interrupt will |
| 353 | * be generated only when detecting a specific status bit changed |
| 354 | * from '0' to '1'. So when we start waiting for a interrupt, we |
| 355 | * need to check status bit in control reg first, if it is already 1, |
| 356 | * then we do not need to wait for interrupt |
| 357 | */ |
| 358 | ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG); |
| 359 | if (a3700_spi->wait_mask & ctrl_reg) |
| 360 | return true; |
| 361 | |
| 362 | reinit_completion(&a3700_spi->done); |
| 363 | |
| 364 | spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, |
| 365 | a3700_spi->wait_mask); |
| 366 | |
| 367 | timeout_jiffies = msecs_to_jiffies(A3700_SPI_TIMEOUT); |
| 368 | timeout = wait_for_completion_timeout(&a3700_spi->done, |
| 369 | timeout_jiffies); |
| 370 | |
| 371 | a3700_spi->wait_mask = 0; |
| 372 | |
| 373 | if (timeout) |
| 374 | return true; |
| 375 | |
| 376 | /* there might be the case that right after we checked the |
| 377 | * status bits in this routine and before start to wait for |
| 378 | * interrupt by wait_for_completion_timeout, the interrupt |
| 379 | * happens, to avoid missing it we need to double check |
| 380 | * status bits in control reg, if it is already 1, then |
| 381 | * consider that we have the interrupt successfully and |
| 382 | * return true. |
| 383 | */ |
| 384 | ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG); |
| 385 | if (a3700_spi->wait_mask & ctrl_reg) |
| 386 | return true; |
| 387 | |
| 388 | spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0); |
| 389 | |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | static bool a3700_spi_transfer_wait(struct spi_device *spi, |
| 394 | unsigned int bit_mask) |
| 395 | { |
| 396 | struct a3700_spi *a3700_spi; |
| 397 | |
| 398 | a3700_spi = spi_master_get_devdata(spi->master); |
| 399 | a3700_spi->wait_mask = bit_mask; |
| 400 | |
| 401 | return a3700_spi_wait_completion(spi); |
| 402 | } |
| 403 | |
| 404 | static void a3700_spi_fifo_thres_set(struct a3700_spi *a3700_spi, |
| 405 | unsigned int bytes) |
| 406 | { |
| 407 | u32 val; |
| 408 | |
| 409 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 410 | val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_RFIFO_THRS_BIT); |
| 411 | val |= (bytes - 1) << A3700_SPI_RFIFO_THRS_BIT; |
| 412 | val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_WFIFO_THRS_BIT); |
| 413 | val |= (7 - bytes) << A3700_SPI_WFIFO_THRS_BIT; |
| 414 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 415 | } |
| 416 | |
| 417 | static void a3700_spi_transfer_setup(struct spi_device *spi, |
Romain Perier | 85798e15 | 2016-12-21 11:10:29 +0100 | [diff] [blame] | 418 | struct spi_transfer *xfer) |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 419 | { |
| 420 | struct a3700_spi *a3700_spi; |
| 421 | unsigned int byte_len; |
| 422 | |
| 423 | a3700_spi = spi_master_get_devdata(spi->master); |
| 424 | |
| 425 | a3700_spi_clock_set(a3700_spi, xfer->speed_hz, spi->mode); |
| 426 | |
| 427 | byte_len = xfer->bits_per_word >> 3; |
| 428 | |
| 429 | a3700_spi_fifo_thres_set(a3700_spi, byte_len); |
| 430 | } |
| 431 | |
| 432 | static void a3700_spi_set_cs(struct spi_device *spi, bool enable) |
| 433 | { |
| 434 | struct a3700_spi *a3700_spi = spi_master_get_devdata(spi->master); |
| 435 | |
| 436 | if (!enable) |
| 437 | a3700_spi_activate_cs(a3700_spi, spi->chip_select); |
| 438 | else |
| 439 | a3700_spi_deactivate_cs(a3700_spi, spi->chip_select); |
| 440 | } |
| 441 | |
| 442 | static void a3700_spi_header_set(struct a3700_spi *a3700_spi) |
| 443 | { |
Zachary Zhang | 6fd6fd6 | 2017-09-13 18:21:39 +0200 | [diff] [blame^] | 444 | unsigned int addr_cnt; |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 445 | u32 val = 0; |
| 446 | |
| 447 | /* Clear the header registers */ |
| 448 | spireg_write(a3700_spi, A3700_SPI_IF_INST_REG, 0); |
| 449 | spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, 0); |
| 450 | spireg_write(a3700_spi, A3700_SPI_IF_RMODE_REG, 0); |
Zachary Zhang | 6fd6fd6 | 2017-09-13 18:21:39 +0200 | [diff] [blame^] | 451 | spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0); |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 452 | |
| 453 | /* Set header counters */ |
| 454 | if (a3700_spi->tx_buf) { |
Zachary Zhang | 6fd6fd6 | 2017-09-13 18:21:39 +0200 | [diff] [blame^] | 455 | /* |
| 456 | * when tx data is not 4 bytes aligned, there will be unexpected |
| 457 | * bytes out of SPI output register, since it always shifts out |
| 458 | * as whole 4 bytes. This might cause incorrect transaction with |
| 459 | * some devices. To avoid that, use SPI header count feature to |
| 460 | * transfer up to 3 bytes of data first, and then make the rest |
| 461 | * of data 4-byte aligned. |
| 462 | */ |
| 463 | addr_cnt = a3700_spi->buf_len % 4; |
| 464 | if (addr_cnt) { |
| 465 | val = (addr_cnt & A3700_SPI_ADDR_CNT_MASK) |
| 466 | << A3700_SPI_ADDR_CNT_BIT; |
| 467 | spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, val); |
| 468 | |
| 469 | /* Update the buffer length to be transferred */ |
| 470 | a3700_spi->buf_len -= addr_cnt; |
| 471 | |
| 472 | /* transfer 1~3 bytes through address count */ |
| 473 | val = 0; |
| 474 | while (addr_cnt--) { |
| 475 | val = (val << 8) | a3700_spi->tx_buf[0]; |
| 476 | a3700_spi->tx_buf++; |
| 477 | } |
| 478 | spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, val); |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 479 | } |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 480 | } |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static int a3700_is_wfifo_full(struct a3700_spi *a3700_spi) |
| 484 | { |
| 485 | u32 val; |
| 486 | |
| 487 | val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG); |
| 488 | return (val & A3700_SPI_WFIFO_FULL); |
| 489 | } |
| 490 | |
| 491 | static int a3700_spi_fifo_write(struct a3700_spi *a3700_spi) |
| 492 | { |
| 493 | u32 val; |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 494 | |
| 495 | while (!a3700_is_wfifo_full(a3700_spi) && a3700_spi->buf_len) { |
Zachary Zhang | 6fd6fd6 | 2017-09-13 18:21:39 +0200 | [diff] [blame^] | 496 | val = cpu_to_le32(*(u32 *)a3700_spi->tx_buf); |
| 497 | spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, val); |
| 498 | a3700_spi->buf_len -= 4; |
| 499 | a3700_spi->tx_buf += 4; |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static int a3700_is_rfifo_empty(struct a3700_spi *a3700_spi) |
| 506 | { |
| 507 | u32 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG); |
| 508 | |
| 509 | return (val & A3700_SPI_RFIFO_EMPTY); |
| 510 | } |
| 511 | |
| 512 | static int a3700_spi_fifo_read(struct a3700_spi *a3700_spi) |
| 513 | { |
| 514 | u32 val; |
| 515 | |
| 516 | while (!a3700_is_rfifo_empty(a3700_spi) && a3700_spi->buf_len) { |
| 517 | val = spireg_read(a3700_spi, A3700_SPI_DATA_IN_REG); |
| 518 | if (a3700_spi->buf_len >= 4) { |
| 519 | u32 data = le32_to_cpu(val); |
Romain Perier | 85798e15 | 2016-12-21 11:10:29 +0100 | [diff] [blame] | 520 | |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 521 | memcpy(a3700_spi->rx_buf, &data, 4); |
| 522 | |
| 523 | a3700_spi->buf_len -= 4; |
| 524 | a3700_spi->rx_buf += 4; |
| 525 | } else { |
| 526 | /* |
| 527 | * When remain bytes is not larger than 4, we should |
| 528 | * avoid memory overwriting and just write the left rx |
| 529 | * buffer bytes. |
| 530 | */ |
| 531 | while (a3700_spi->buf_len) { |
| 532 | *a3700_spi->rx_buf = val & 0xff; |
| 533 | val >>= 8; |
| 534 | |
| 535 | a3700_spi->buf_len--; |
| 536 | a3700_spi->rx_buf++; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static void a3700_spi_transfer_abort_fifo(struct a3700_spi *a3700_spi) |
| 545 | { |
| 546 | int timeout = A3700_SPI_TIMEOUT; |
| 547 | u32 val; |
| 548 | |
| 549 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 550 | val |= A3700_SPI_XFER_STOP; |
| 551 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 552 | |
| 553 | while (--timeout) { |
| 554 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 555 | if (!(val & A3700_SPI_XFER_START)) |
| 556 | break; |
| 557 | udelay(1); |
| 558 | } |
| 559 | |
| 560 | a3700_spi_fifo_flush(a3700_spi); |
| 561 | |
| 562 | val &= ~A3700_SPI_XFER_STOP; |
| 563 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 564 | } |
| 565 | |
| 566 | static int a3700_spi_prepare_message(struct spi_master *master, |
| 567 | struct spi_message *message) |
| 568 | { |
| 569 | struct a3700_spi *a3700_spi = spi_master_get_devdata(master); |
| 570 | struct spi_device *spi = message->spi; |
| 571 | int ret; |
| 572 | |
| 573 | ret = clk_enable(a3700_spi->clk); |
| 574 | if (ret) { |
| 575 | dev_err(&spi->dev, "failed to enable clk with error %d\n", ret); |
| 576 | return ret; |
| 577 | } |
| 578 | |
| 579 | /* Flush the FIFOs */ |
| 580 | ret = a3700_spi_fifo_flush(a3700_spi); |
| 581 | if (ret) |
| 582 | return ret; |
| 583 | |
| 584 | a3700_spi_bytelen_set(a3700_spi, 4); |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static int a3700_spi_transfer_one(struct spi_master *master, |
| 590 | struct spi_device *spi, |
| 591 | struct spi_transfer *xfer) |
| 592 | { |
| 593 | struct a3700_spi *a3700_spi = spi_master_get_devdata(master); |
| 594 | int ret = 0, timeout = A3700_SPI_TIMEOUT; |
| 595 | unsigned int nbits = 0; |
| 596 | u32 val; |
| 597 | |
| 598 | a3700_spi_transfer_setup(spi, xfer); |
| 599 | |
| 600 | a3700_spi->tx_buf = xfer->tx_buf; |
| 601 | a3700_spi->rx_buf = xfer->rx_buf; |
| 602 | a3700_spi->buf_len = xfer->len; |
| 603 | |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 604 | if (xfer->tx_buf) |
| 605 | nbits = xfer->tx_nbits; |
| 606 | else if (xfer->rx_buf) |
| 607 | nbits = xfer->rx_nbits; |
| 608 | |
Miquel Raynal | 747e1f6 | 2017-09-13 18:21:38 +0200 | [diff] [blame] | 609 | a3700_spi_pin_mode_set(a3700_spi, nbits, xfer->rx_buf ? true : false); |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 610 | |
Zachary Zhang | 6fd6fd6 | 2017-09-13 18:21:39 +0200 | [diff] [blame^] | 611 | /* Flush the FIFOs */ |
| 612 | a3700_spi_fifo_flush(a3700_spi); |
| 613 | |
| 614 | /* Transfer first bytes of data when buffer is not 4-byte aligned */ |
| 615 | a3700_spi_header_set(a3700_spi); |
| 616 | |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 617 | if (xfer->rx_buf) { |
| 618 | /* Set read data length */ |
| 619 | spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG, |
| 620 | a3700_spi->buf_len); |
| 621 | /* Start READ transfer */ |
| 622 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 623 | val &= ~A3700_SPI_RW_EN; |
| 624 | val |= A3700_SPI_XFER_START; |
| 625 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 626 | } else if (xfer->tx_buf) { |
| 627 | /* Start Write transfer */ |
| 628 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 629 | val |= (A3700_SPI_XFER_START | A3700_SPI_RW_EN); |
| 630 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 631 | |
| 632 | /* |
| 633 | * If there are data to be written to the SPI device, xmit_data |
| 634 | * flag is set true; otherwise the instruction in SPI_INSTR does |
| 635 | * not require data to be written to the SPI device, then |
| 636 | * xmit_data flag is set false. |
| 637 | */ |
| 638 | a3700_spi->xmit_data = (a3700_spi->buf_len != 0); |
| 639 | } |
| 640 | |
| 641 | while (a3700_spi->buf_len) { |
| 642 | if (a3700_spi->tx_buf) { |
| 643 | /* Wait wfifo ready */ |
| 644 | if (!a3700_spi_transfer_wait(spi, |
| 645 | A3700_SPI_WFIFO_RDY)) { |
| 646 | dev_err(&spi->dev, |
| 647 | "wait wfifo ready timed out\n"); |
| 648 | ret = -ETIMEDOUT; |
| 649 | goto error; |
| 650 | } |
| 651 | /* Fill up the wfifo */ |
| 652 | ret = a3700_spi_fifo_write(a3700_spi); |
| 653 | if (ret) |
| 654 | goto error; |
| 655 | } else if (a3700_spi->rx_buf) { |
| 656 | /* Wait rfifo ready */ |
| 657 | if (!a3700_spi_transfer_wait(spi, |
| 658 | A3700_SPI_RFIFO_RDY)) { |
| 659 | dev_err(&spi->dev, |
| 660 | "wait rfifo ready timed out\n"); |
| 661 | ret = -ETIMEDOUT; |
| 662 | goto error; |
| 663 | } |
| 664 | /* Drain out the rfifo */ |
| 665 | ret = a3700_spi_fifo_read(a3700_spi); |
| 666 | if (ret) |
| 667 | goto error; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * Stop a write transfer in fifo mode: |
| 673 | * - wait all the bytes in wfifo to be shifted out |
| 674 | * - set XFER_STOP bit |
| 675 | * - wait XFER_START bit clear |
| 676 | * - clear XFER_STOP bit |
| 677 | * Stop a read transfer in fifo mode: |
| 678 | * - the hardware is to reset the XFER_START bit |
| 679 | * after the number of bytes indicated in DIN_CNT |
| 680 | * register |
| 681 | * - just wait XFER_START bit clear |
| 682 | */ |
| 683 | if (a3700_spi->tx_buf) { |
| 684 | if (a3700_spi->xmit_data) { |
| 685 | /* |
| 686 | * If there are data written to the SPI device, wait |
| 687 | * until SPI_WFIFO_EMPTY is 1 to wait for all data to |
| 688 | * transfer out of write FIFO. |
| 689 | */ |
| 690 | if (!a3700_spi_transfer_wait(spi, |
| 691 | A3700_SPI_WFIFO_EMPTY)) { |
| 692 | dev_err(&spi->dev, "wait wfifo empty timed out\n"); |
| 693 | return -ETIMEDOUT; |
| 694 | } |
Zachary Zhang | 6fd6fd6 | 2017-09-13 18:21:39 +0200 | [diff] [blame^] | 695 | } |
| 696 | |
| 697 | if (!a3700_spi_transfer_wait(spi, A3700_SPI_XFER_RDY)) { |
| 698 | dev_err(&spi->dev, "wait xfer ready timed out\n"); |
| 699 | return -ETIMEDOUT; |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 703 | val |= A3700_SPI_XFER_STOP; |
| 704 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 705 | } |
| 706 | |
| 707 | while (--timeout) { |
| 708 | val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG); |
| 709 | if (!(val & A3700_SPI_XFER_START)) |
| 710 | break; |
| 711 | udelay(1); |
| 712 | } |
| 713 | |
| 714 | if (timeout == 0) { |
| 715 | dev_err(&spi->dev, "wait transfer start clear timed out\n"); |
| 716 | ret = -ETIMEDOUT; |
| 717 | goto error; |
| 718 | } |
| 719 | |
| 720 | val &= ~A3700_SPI_XFER_STOP; |
| 721 | spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val); |
| 722 | goto out; |
| 723 | |
| 724 | error: |
| 725 | a3700_spi_transfer_abort_fifo(a3700_spi); |
| 726 | out: |
| 727 | spi_finalize_current_transfer(master); |
| 728 | |
| 729 | return ret; |
| 730 | } |
| 731 | |
| 732 | static int a3700_spi_unprepare_message(struct spi_master *master, |
| 733 | struct spi_message *message) |
| 734 | { |
| 735 | struct a3700_spi *a3700_spi = spi_master_get_devdata(master); |
| 736 | |
| 737 | clk_disable(a3700_spi->clk); |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | static const struct of_device_id a3700_spi_dt_ids[] = { |
| 743 | { .compatible = "marvell,armada-3700-spi", .data = NULL }, |
| 744 | {}, |
| 745 | }; |
| 746 | |
| 747 | MODULE_DEVICE_TABLE(of, a3700_spi_dt_ids); |
| 748 | |
| 749 | static int a3700_spi_probe(struct platform_device *pdev) |
| 750 | { |
| 751 | struct device *dev = &pdev->dev; |
| 752 | struct device_node *of_node = dev->of_node; |
| 753 | struct resource *res; |
| 754 | struct spi_master *master; |
| 755 | struct a3700_spi *spi; |
| 756 | u32 num_cs = 0; |
Colin Ian King | f6f0083 | 2016-12-13 10:28:12 +0000 | [diff] [blame] | 757 | int irq, ret = 0; |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 758 | |
| 759 | master = spi_alloc_master(dev, sizeof(*spi)); |
| 760 | if (!master) { |
| 761 | dev_err(dev, "master allocation failed\n"); |
| 762 | ret = -ENOMEM; |
| 763 | goto out; |
| 764 | } |
| 765 | |
| 766 | if (of_property_read_u32(of_node, "num-cs", &num_cs)) { |
| 767 | dev_err(dev, "could not find num-cs\n"); |
| 768 | ret = -ENXIO; |
| 769 | goto error; |
| 770 | } |
| 771 | |
| 772 | master->bus_num = pdev->id; |
| 773 | master->dev.of_node = of_node; |
| 774 | master->mode_bits = SPI_MODE_3; |
| 775 | master->num_chipselect = num_cs; |
| 776 | master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(32); |
| 777 | master->prepare_message = a3700_spi_prepare_message; |
| 778 | master->transfer_one = a3700_spi_transfer_one; |
| 779 | master->unprepare_message = a3700_spi_unprepare_message; |
| 780 | master->set_cs = a3700_spi_set_cs; |
| 781 | master->flags = SPI_MASTER_HALF_DUPLEX; |
Dan Carpenter | 42cd4ed | 2016-12-16 12:33:25 +0300 | [diff] [blame] | 782 | master->mode_bits |= (SPI_RX_DUAL | SPI_TX_DUAL | |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 783 | SPI_RX_QUAD | SPI_TX_QUAD); |
| 784 | |
| 785 | platform_set_drvdata(pdev, master); |
| 786 | |
| 787 | spi = spi_master_get_devdata(master); |
| 788 | memset(spi, 0, sizeof(struct a3700_spi)); |
| 789 | |
| 790 | spi->master = master; |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 791 | |
| 792 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 793 | spi->base = devm_ioremap_resource(dev, res); |
| 794 | if (IS_ERR(spi->base)) { |
| 795 | ret = PTR_ERR(spi->base); |
| 796 | goto error; |
| 797 | } |
| 798 | |
Colin Ian King | f6f0083 | 2016-12-13 10:28:12 +0000 | [diff] [blame] | 799 | irq = platform_get_irq(pdev, 0); |
| 800 | if (irq < 0) { |
| 801 | dev_err(dev, "could not get irq: %d\n", irq); |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 802 | ret = -ENXIO; |
| 803 | goto error; |
| 804 | } |
Colin Ian King | f6f0083 | 2016-12-13 10:28:12 +0000 | [diff] [blame] | 805 | spi->irq = irq; |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 806 | |
| 807 | init_completion(&spi->done); |
| 808 | |
| 809 | spi->clk = devm_clk_get(dev, NULL); |
| 810 | if (IS_ERR(spi->clk)) { |
| 811 | dev_err(dev, "could not find clk: %ld\n", PTR_ERR(spi->clk)); |
| 812 | goto error; |
| 813 | } |
| 814 | |
| 815 | ret = clk_prepare(spi->clk); |
| 816 | if (ret) { |
| 817 | dev_err(dev, "could not prepare clk: %d\n", ret); |
| 818 | goto error; |
| 819 | } |
| 820 | |
| 821 | ret = a3700_spi_init(spi); |
| 822 | if (ret) |
| 823 | goto error_clk; |
| 824 | |
| 825 | ret = devm_request_irq(dev, spi->irq, a3700_spi_interrupt, 0, |
| 826 | dev_name(dev), master); |
| 827 | if (ret) { |
| 828 | dev_err(dev, "could not request IRQ: %d\n", ret); |
| 829 | goto error_clk; |
| 830 | } |
| 831 | |
| 832 | ret = devm_spi_register_master(dev, master); |
| 833 | if (ret) { |
| 834 | dev_err(dev, "Failed to register master\n"); |
| 835 | goto error_clk; |
| 836 | } |
| 837 | |
| 838 | return 0; |
| 839 | |
| 840 | error_clk: |
| 841 | clk_disable_unprepare(spi->clk); |
| 842 | error: |
| 843 | spi_master_put(master); |
| 844 | out: |
| 845 | return ret; |
| 846 | } |
| 847 | |
| 848 | static int a3700_spi_remove(struct platform_device *pdev) |
| 849 | { |
| 850 | struct spi_master *master = platform_get_drvdata(pdev); |
| 851 | struct a3700_spi *spi = spi_master_get_devdata(master); |
| 852 | |
| 853 | clk_unprepare(spi->clk); |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | static struct platform_driver a3700_spi_driver = { |
| 859 | .driver = { |
| 860 | .name = DRIVER_NAME, |
Romain Perier | 5762ab7 | 2016-12-08 15:58:44 +0100 | [diff] [blame] | 861 | .of_match_table = of_match_ptr(a3700_spi_dt_ids), |
| 862 | }, |
| 863 | .probe = a3700_spi_probe, |
| 864 | .remove = a3700_spi_remove, |
| 865 | }; |
| 866 | |
| 867 | module_platform_driver(a3700_spi_driver); |
| 868 | |
| 869 | MODULE_DESCRIPTION("Armada-3700 SPI driver"); |
| 870 | MODULE_AUTHOR("Wilson Ding <dingwei@marvell.com>"); |
| 871 | MODULE_LICENSE("GPL"); |
| 872 | MODULE_ALIAS("platform:" DRIVER_NAME); |