Thomas Gleixner | cb849fc | 2019-06-04 10:10:52 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Driver for Broadcom BRCMSTB, NSP, NS2, Cygnus SPI Controllers |
| 4 | * |
| 5 | * Copyright 2016 Broadcom |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/ioport.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 17 | #include <linux/of.h> |
| 18 | #include <linux/of_irq.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/spi/spi.h> |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 22 | #include <linux/spi/spi-mem.h> |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 23 | #include <linux/sysfs.h> |
| 24 | #include <linux/types.h> |
| 25 | #include "spi-bcm-qspi.h" |
| 26 | |
| 27 | #define DRIVER_NAME "bcm_qspi" |
| 28 | |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 29 | |
| 30 | /* BSPI register offsets */ |
| 31 | #define BSPI_REVISION_ID 0x000 |
| 32 | #define BSPI_SCRATCH 0x004 |
| 33 | #define BSPI_MAST_N_BOOT_CTRL 0x008 |
| 34 | #define BSPI_BUSY_STATUS 0x00c |
| 35 | #define BSPI_INTR_STATUS 0x010 |
| 36 | #define BSPI_B0_STATUS 0x014 |
| 37 | #define BSPI_B0_CTRL 0x018 |
| 38 | #define BSPI_B1_STATUS 0x01c |
| 39 | #define BSPI_B1_CTRL 0x020 |
| 40 | #define BSPI_STRAP_OVERRIDE_CTRL 0x024 |
| 41 | #define BSPI_FLEX_MODE_ENABLE 0x028 |
| 42 | #define BSPI_BITS_PER_CYCLE 0x02c |
| 43 | #define BSPI_BITS_PER_PHASE 0x030 |
| 44 | #define BSPI_CMD_AND_MODE_BYTE 0x034 |
| 45 | #define BSPI_BSPI_FLASH_UPPER_ADDR_BYTE 0x038 |
| 46 | #define BSPI_BSPI_XOR_VALUE 0x03c |
| 47 | #define BSPI_BSPI_XOR_ENABLE 0x040 |
| 48 | #define BSPI_BSPI_PIO_MODE_ENABLE 0x044 |
| 49 | #define BSPI_BSPI_PIO_IODIR 0x048 |
| 50 | #define BSPI_BSPI_PIO_DATA 0x04c |
| 51 | |
| 52 | /* RAF register offsets */ |
| 53 | #define BSPI_RAF_START_ADDR 0x100 |
| 54 | #define BSPI_RAF_NUM_WORDS 0x104 |
| 55 | #define BSPI_RAF_CTRL 0x108 |
| 56 | #define BSPI_RAF_FULLNESS 0x10c |
| 57 | #define BSPI_RAF_WATERMARK 0x110 |
| 58 | #define BSPI_RAF_STATUS 0x114 |
| 59 | #define BSPI_RAF_READ_DATA 0x118 |
| 60 | #define BSPI_RAF_WORD_CNT 0x11c |
| 61 | #define BSPI_RAF_CURR_ADDR 0x120 |
| 62 | |
| 63 | /* Override mode masks */ |
| 64 | #define BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE BIT(0) |
| 65 | #define BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL BIT(1) |
| 66 | #define BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE BIT(2) |
| 67 | #define BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD BIT(3) |
| 68 | #define BSPI_STRAP_OVERRIDE_CTRL_ENDAIN_MODE BIT(4) |
| 69 | |
| 70 | #define BSPI_ADDRLEN_3BYTES 3 |
| 71 | #define BSPI_ADDRLEN_4BYTES 4 |
| 72 | |
| 73 | #define BSPI_RAF_STATUS_FIFO_EMPTY_MASK BIT(1) |
| 74 | |
| 75 | #define BSPI_RAF_CTRL_START_MASK BIT(0) |
| 76 | #define BSPI_RAF_CTRL_CLEAR_MASK BIT(1) |
| 77 | |
| 78 | #define BSPI_BPP_MODE_SELECT_MASK BIT(8) |
| 79 | #define BSPI_BPP_ADDR_SELECT_MASK BIT(16) |
| 80 | |
Rafał Miłecki | 940ec77 | 2018-10-11 09:42:17 +0200 | [diff] [blame] | 81 | #define BSPI_READ_LENGTH 256 |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 82 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 83 | /* MSPI register offsets */ |
| 84 | #define MSPI_SPCR0_LSB 0x000 |
| 85 | #define MSPI_SPCR0_MSB 0x004 |
| 86 | #define MSPI_SPCR1_LSB 0x008 |
| 87 | #define MSPI_SPCR1_MSB 0x00c |
| 88 | #define MSPI_NEWQP 0x010 |
| 89 | #define MSPI_ENDQP 0x014 |
| 90 | #define MSPI_SPCR2 0x018 |
| 91 | #define MSPI_MSPI_STATUS 0x020 |
| 92 | #define MSPI_CPTQP 0x024 |
| 93 | #define MSPI_SPCR3 0x028 |
Florian Fainelli | 3a01f04 | 2020-04-20 15:08:47 -0400 | [diff] [blame^] | 94 | #define MSPI_REV 0x02c |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 95 | #define MSPI_TXRAM 0x040 |
| 96 | #define MSPI_RXRAM 0x0c0 |
| 97 | #define MSPI_CDRAM 0x140 |
| 98 | #define MSPI_WRITE_LOCK 0x180 |
| 99 | |
| 100 | #define MSPI_MASTER_BIT BIT(7) |
| 101 | |
| 102 | #define MSPI_NUM_CDRAM 16 |
| 103 | #define MSPI_CDRAM_CONT_BIT BIT(7) |
| 104 | #define MSPI_CDRAM_BITSE_BIT BIT(6) |
| 105 | #define MSPI_CDRAM_PCS 0xf |
| 106 | |
| 107 | #define MSPI_SPCR2_SPE BIT(6) |
| 108 | #define MSPI_SPCR2_CONT_AFTER_CMD BIT(7) |
| 109 | |
| 110 | #define MSPI_MSPI_STATUS_SPIF BIT(0) |
| 111 | |
| 112 | #define INTR_BASE_BIT_SHIFT 0x02 |
| 113 | #define INTR_COUNT 0x07 |
| 114 | |
| 115 | #define NUM_CHIPSELECT 4 |
| 116 | #define QSPI_SPBR_MIN 8U |
| 117 | #define QSPI_SPBR_MAX 255U |
| 118 | |
| 119 | #define OPCODE_DIOR 0xBB |
| 120 | #define OPCODE_QIOR 0xEB |
| 121 | #define OPCODE_DIOR_4B 0xBC |
| 122 | #define OPCODE_QIOR_4B 0xEC |
| 123 | |
| 124 | #define MAX_CMD_SIZE 6 |
| 125 | |
| 126 | #define ADDR_4MB_MASK GENMASK(22, 0) |
| 127 | |
| 128 | /* stop at end of transfer, no other reason */ |
| 129 | #define TRANS_STATUS_BREAK_NONE 0 |
| 130 | /* stop at end of spi_message */ |
| 131 | #define TRANS_STATUS_BREAK_EOM 1 |
| 132 | /* stop at end of spi_transfer if delay */ |
| 133 | #define TRANS_STATUS_BREAK_DELAY 2 |
| 134 | /* stop at end of spi_transfer if cs_change */ |
| 135 | #define TRANS_STATUS_BREAK_CS_CHANGE 4 |
| 136 | /* stop if we run out of bytes */ |
| 137 | #define TRANS_STATUS_BREAK_NO_BYTES 8 |
| 138 | |
| 139 | /* events that make us stop filling TX slots */ |
| 140 | #define TRANS_STATUS_BREAK_TX (TRANS_STATUS_BREAK_EOM | \ |
| 141 | TRANS_STATUS_BREAK_DELAY | \ |
| 142 | TRANS_STATUS_BREAK_CS_CHANGE) |
| 143 | |
| 144 | /* events that make us deassert CS */ |
| 145 | #define TRANS_STATUS_BREAK_DESELECT (TRANS_STATUS_BREAK_EOM | \ |
| 146 | TRANS_STATUS_BREAK_CS_CHANGE) |
| 147 | |
| 148 | struct bcm_qspi_parms { |
| 149 | u32 speed_hz; |
| 150 | u8 mode; |
| 151 | u8 bits_per_word; |
| 152 | }; |
| 153 | |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 154 | struct bcm_xfer_mode { |
| 155 | bool flex_mode; |
| 156 | unsigned int width; |
| 157 | unsigned int addrlen; |
| 158 | unsigned int hp; |
| 159 | }; |
| 160 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 161 | enum base_type { |
| 162 | MSPI, |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 163 | BSPI, |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 164 | CHIP_SELECT, |
| 165 | BASEMAX, |
| 166 | }; |
| 167 | |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 168 | enum irq_source { |
| 169 | SINGLE_L2, |
| 170 | MUXED_L1, |
| 171 | }; |
| 172 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 173 | struct bcm_qspi_irq { |
| 174 | const char *irq_name; |
| 175 | const irq_handler_t irq_handler; |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 176 | int irq_source; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 177 | u32 mask; |
| 178 | }; |
| 179 | |
| 180 | struct bcm_qspi_dev_id { |
| 181 | const struct bcm_qspi_irq *irqp; |
| 182 | void *dev; |
| 183 | }; |
| 184 | |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 185 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 186 | struct qspi_trans { |
| 187 | struct spi_transfer *trans; |
| 188 | int byte; |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 189 | bool mspi_last_trans; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | struct bcm_qspi { |
| 193 | struct platform_device *pdev; |
| 194 | struct spi_master *master; |
| 195 | struct clk *clk; |
| 196 | u32 base_clk; |
| 197 | u32 max_speed_hz; |
| 198 | void __iomem *base[BASEMAX]; |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 199 | |
| 200 | /* Some SoCs provide custom interrupt status register(s) */ |
| 201 | struct bcm_qspi_soc_intc *soc_intc; |
| 202 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 203 | struct bcm_qspi_parms last_parms; |
| 204 | struct qspi_trans trans_pos; |
| 205 | int curr_cs; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 206 | int bspi_maj_rev; |
| 207 | int bspi_min_rev; |
| 208 | int bspi_enabled; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 209 | const struct spi_mem_op *bspi_rf_op; |
| 210 | u32 bspi_rf_op_idx; |
| 211 | u32 bspi_rf_op_len; |
| 212 | u32 bspi_rf_op_status; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 213 | struct bcm_xfer_mode xfer_mode; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 214 | u32 s3_strap_override_ctrl; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 215 | bool bspi_mode; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 216 | bool big_endian; |
| 217 | int num_irqs; |
| 218 | struct bcm_qspi_dev_id *dev_ids; |
| 219 | struct completion mspi_done; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 220 | struct completion bspi_done; |
Florian Fainelli | 3a01f04 | 2020-04-20 15:08:47 -0400 | [diff] [blame^] | 221 | u8 mspi_maj_rev; |
| 222 | u8 mspi_min_rev; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 223 | }; |
| 224 | |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 225 | static inline bool has_bspi(struct bcm_qspi *qspi) |
| 226 | { |
| 227 | return qspi->bspi_mode; |
| 228 | } |
| 229 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 230 | /* Read qspi controller register*/ |
| 231 | static inline u32 bcm_qspi_read(struct bcm_qspi *qspi, enum base_type type, |
| 232 | unsigned int offset) |
| 233 | { |
| 234 | return bcm_qspi_readl(qspi->big_endian, qspi->base[type] + offset); |
| 235 | } |
| 236 | |
| 237 | /* Write qspi controller register*/ |
| 238 | static inline void bcm_qspi_write(struct bcm_qspi *qspi, enum base_type type, |
| 239 | unsigned int offset, unsigned int data) |
| 240 | { |
| 241 | bcm_qspi_writel(qspi->big_endian, data, qspi->base[type] + offset); |
| 242 | } |
| 243 | |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 244 | /* BSPI helpers */ |
| 245 | static int bcm_qspi_bspi_busy_poll(struct bcm_qspi *qspi) |
| 246 | { |
| 247 | int i; |
| 248 | |
| 249 | /* this should normally finish within 10us */ |
| 250 | for (i = 0; i < 1000; i++) { |
| 251 | if (!(bcm_qspi_read(qspi, BSPI, BSPI_BUSY_STATUS) & 1)) |
| 252 | return 0; |
| 253 | udelay(1); |
| 254 | } |
| 255 | dev_warn(&qspi->pdev->dev, "timeout waiting for !busy_status\n"); |
| 256 | return -EIO; |
| 257 | } |
| 258 | |
| 259 | static inline bool bcm_qspi_bspi_ver_three(struct bcm_qspi *qspi) |
| 260 | { |
| 261 | if (qspi->bspi_maj_rev < 4) |
| 262 | return true; |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | static void bcm_qspi_bspi_flush_prefetch_buffers(struct bcm_qspi *qspi) |
| 267 | { |
| 268 | bcm_qspi_bspi_busy_poll(qspi); |
| 269 | /* Force rising edge for the b0/b1 'flush' field */ |
| 270 | bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 1); |
| 271 | bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 1); |
| 272 | bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 0); |
| 273 | bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 0); |
| 274 | } |
| 275 | |
| 276 | static int bcm_qspi_bspi_lr_is_fifo_empty(struct bcm_qspi *qspi) |
| 277 | { |
| 278 | return (bcm_qspi_read(qspi, BSPI, BSPI_RAF_STATUS) & |
| 279 | BSPI_RAF_STATUS_FIFO_EMPTY_MASK); |
| 280 | } |
| 281 | |
| 282 | static inline u32 bcm_qspi_bspi_lr_read_fifo(struct bcm_qspi *qspi) |
| 283 | { |
| 284 | u32 data = bcm_qspi_read(qspi, BSPI, BSPI_RAF_READ_DATA); |
| 285 | |
| 286 | /* BSPI v3 LR is LE only, convert data to host endianness */ |
| 287 | if (bcm_qspi_bspi_ver_three(qspi)) |
| 288 | data = le32_to_cpu(data); |
| 289 | |
| 290 | return data; |
| 291 | } |
| 292 | |
| 293 | static inline void bcm_qspi_bspi_lr_start(struct bcm_qspi *qspi) |
| 294 | { |
| 295 | bcm_qspi_bspi_busy_poll(qspi); |
| 296 | bcm_qspi_write(qspi, BSPI, BSPI_RAF_CTRL, |
| 297 | BSPI_RAF_CTRL_START_MASK); |
| 298 | } |
| 299 | |
| 300 | static inline void bcm_qspi_bspi_lr_clear(struct bcm_qspi *qspi) |
| 301 | { |
| 302 | bcm_qspi_write(qspi, BSPI, BSPI_RAF_CTRL, |
| 303 | BSPI_RAF_CTRL_CLEAR_MASK); |
| 304 | bcm_qspi_bspi_flush_prefetch_buffers(qspi); |
| 305 | } |
| 306 | |
| 307 | static void bcm_qspi_bspi_lr_data_read(struct bcm_qspi *qspi) |
| 308 | { |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 309 | u32 *buf = (u32 *)qspi->bspi_rf_op->data.buf.in; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 310 | u32 data = 0; |
| 311 | |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 312 | dev_dbg(&qspi->pdev->dev, "xfer %p rx %p rxlen %d\n", qspi->bspi_rf_op, |
| 313 | qspi->bspi_rf_op->data.buf.in, qspi->bspi_rf_op_len); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 314 | while (!bcm_qspi_bspi_lr_is_fifo_empty(qspi)) { |
| 315 | data = bcm_qspi_bspi_lr_read_fifo(qspi); |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 316 | if (likely(qspi->bspi_rf_op_len >= 4) && |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 317 | IS_ALIGNED((uintptr_t)buf, 4)) { |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 318 | buf[qspi->bspi_rf_op_idx++] = data; |
| 319 | qspi->bspi_rf_op_len -= 4; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 320 | } else { |
| 321 | /* Read out remaining bytes, make sure*/ |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 322 | u8 *cbuf = (u8 *)&buf[qspi->bspi_rf_op_idx]; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 323 | |
| 324 | data = cpu_to_le32(data); |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 325 | while (qspi->bspi_rf_op_len) { |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 326 | *cbuf++ = (u8)data; |
| 327 | data >>= 8; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 328 | qspi->bspi_rf_op_len--; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | static void bcm_qspi_bspi_set_xfer_params(struct bcm_qspi *qspi, u8 cmd_byte, |
| 335 | int bpp, int bpc, int flex_mode) |
| 336 | { |
| 337 | bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, 0); |
| 338 | bcm_qspi_write(qspi, BSPI, BSPI_BITS_PER_CYCLE, bpc); |
| 339 | bcm_qspi_write(qspi, BSPI, BSPI_BITS_PER_PHASE, bpp); |
| 340 | bcm_qspi_write(qspi, BSPI, BSPI_CMD_AND_MODE_BYTE, cmd_byte); |
| 341 | bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, flex_mode); |
| 342 | } |
| 343 | |
Kamal Dasu | 054e532 | 2017-07-26 19:20:15 -0400 | [diff] [blame] | 344 | static int bcm_qspi_bspi_set_flex_mode(struct bcm_qspi *qspi, |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 345 | const struct spi_mem_op *op, int hp) |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 346 | { |
| 347 | int bpc = 0, bpp = 0; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 348 | u8 command = op->cmd.opcode; |
Rayagonda Kokatanur | 79629d0 | 2019-08-06 15:37:50 +0530 | [diff] [blame] | 349 | int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE; |
Rafał Miłecki | 0976eda | 2018-10-11 09:40:22 +0200 | [diff] [blame] | 350 | int addrlen = op->addr.nbytes; |
Kamal Dasu | 054e532 | 2017-07-26 19:20:15 -0400 | [diff] [blame] | 351 | int flex_mode = 1; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 352 | |
| 353 | dev_dbg(&qspi->pdev->dev, "set flex mode w %x addrlen %x hp %d\n", |
| 354 | width, addrlen, hp); |
| 355 | |
Kamal Dasu | 054e532 | 2017-07-26 19:20:15 -0400 | [diff] [blame] | 356 | if (addrlen == BSPI_ADDRLEN_4BYTES) |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 357 | bpp = BSPI_BPP_ADDR_SELECT_MASK; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 358 | |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 359 | bpp |= (op->dummy.nbytes * 8) / op->dummy.buswidth; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 360 | |
| 361 | switch (width) { |
| 362 | case SPI_NBITS_SINGLE: |
| 363 | if (addrlen == BSPI_ADDRLEN_3BYTES) |
| 364 | /* default mode, does not need flex_cmd */ |
| 365 | flex_mode = 0; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 366 | break; |
| 367 | case SPI_NBITS_DUAL: |
| 368 | bpc = 0x00000001; |
| 369 | if (hp) { |
| 370 | bpc |= 0x00010100; /* address and mode are 2-bit */ |
| 371 | bpp = BSPI_BPP_MODE_SELECT_MASK; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 372 | } |
| 373 | break; |
| 374 | case SPI_NBITS_QUAD: |
| 375 | bpc = 0x00000002; |
| 376 | if (hp) { |
| 377 | bpc |= 0x00020200; /* address and mode are 4-bit */ |
Kamal Dasu | 054e532 | 2017-07-26 19:20:15 -0400 | [diff] [blame] | 378 | bpp |= BSPI_BPP_MODE_SELECT_MASK; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 379 | } |
| 380 | break; |
| 381 | default: |
Kamal Dasu | 054e532 | 2017-07-26 19:20:15 -0400 | [diff] [blame] | 382 | return -EINVAL; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 383 | } |
| 384 | |
Kamal Dasu | 054e532 | 2017-07-26 19:20:15 -0400 | [diff] [blame] | 385 | bcm_qspi_bspi_set_xfer_params(qspi, command, bpp, bpc, flex_mode); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 386 | |
Kamal Dasu | 054e532 | 2017-07-26 19:20:15 -0400 | [diff] [blame] | 387 | return 0; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 388 | } |
| 389 | |
Kamal Dasu | 054e532 | 2017-07-26 19:20:15 -0400 | [diff] [blame] | 390 | static int bcm_qspi_bspi_set_override(struct bcm_qspi *qspi, |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 391 | const struct spi_mem_op *op, int hp) |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 392 | { |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 393 | int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE; |
| 394 | int addrlen = op->addr.nbytes; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 395 | u32 data = bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL); |
| 396 | |
| 397 | dev_dbg(&qspi->pdev->dev, "set override mode w %x addrlen %x hp %d\n", |
| 398 | width, addrlen, hp); |
| 399 | |
| 400 | switch (width) { |
| 401 | case SPI_NBITS_SINGLE: |
| 402 | /* clear quad/dual mode */ |
| 403 | data &= ~(BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD | |
| 404 | BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL); |
| 405 | break; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 406 | case SPI_NBITS_QUAD: |
| 407 | /* clear dual mode and set quad mode */ |
| 408 | data &= ~BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL; |
| 409 | data |= BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD; |
| 410 | break; |
| 411 | case SPI_NBITS_DUAL: |
| 412 | /* clear quad mode set dual mode */ |
| 413 | data &= ~BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD; |
| 414 | data |= BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL; |
| 415 | break; |
| 416 | default: |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | |
| 420 | if (addrlen == BSPI_ADDRLEN_4BYTES) |
| 421 | /* set 4byte mode*/ |
| 422 | data |= BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE; |
| 423 | else |
| 424 | /* clear 4 byte mode */ |
| 425 | data &= ~BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE; |
| 426 | |
| 427 | /* set the override mode */ |
| 428 | data |= BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE; |
| 429 | bcm_qspi_write(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL, data); |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 430 | bcm_qspi_bspi_set_xfer_params(qspi, op->cmd.opcode, 0, 0, 0); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int bcm_qspi_bspi_set_mode(struct bcm_qspi *qspi, |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 436 | const struct spi_mem_op *op, int hp) |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 437 | { |
| 438 | int error = 0; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 439 | int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE; |
| 440 | int addrlen = op->addr.nbytes; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 441 | |
| 442 | /* default mode */ |
| 443 | qspi->xfer_mode.flex_mode = true; |
| 444 | |
| 445 | if (!bcm_qspi_bspi_ver_three(qspi)) { |
| 446 | u32 val, mask; |
| 447 | |
| 448 | val = bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL); |
| 449 | mask = BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE; |
| 450 | if (val & mask || qspi->s3_strap_override_ctrl & mask) { |
| 451 | qspi->xfer_mode.flex_mode = false; |
Kamal Dasu | 054e532 | 2017-07-26 19:20:15 -0400 | [diff] [blame] | 452 | bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, 0); |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 453 | error = bcm_qspi_bspi_set_override(qspi, op, hp); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | if (qspi->xfer_mode.flex_mode) |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 458 | error = bcm_qspi_bspi_set_flex_mode(qspi, op, hp); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 459 | |
| 460 | if (error) { |
| 461 | dev_warn(&qspi->pdev->dev, |
| 462 | "INVALID COMBINATION: width=%d addrlen=%d hp=%d\n", |
| 463 | width, addrlen, hp); |
| 464 | } else if (qspi->xfer_mode.width != width || |
| 465 | qspi->xfer_mode.addrlen != addrlen || |
| 466 | qspi->xfer_mode.hp != hp) { |
| 467 | qspi->xfer_mode.width = width; |
| 468 | qspi->xfer_mode.addrlen = addrlen; |
| 469 | qspi->xfer_mode.hp = hp; |
| 470 | dev_dbg(&qspi->pdev->dev, |
| 471 | "cs:%d %d-lane output, %d-byte address%s\n", |
| 472 | qspi->curr_cs, |
| 473 | qspi->xfer_mode.width, |
| 474 | qspi->xfer_mode.addrlen, |
| 475 | qspi->xfer_mode.hp != -1 ? ", hp mode" : ""); |
| 476 | } |
| 477 | |
| 478 | return error; |
| 479 | } |
| 480 | |
| 481 | static void bcm_qspi_enable_bspi(struct bcm_qspi *qspi) |
| 482 | { |
Kamal Dasu | 602805f | 2018-04-26 14:48:01 -0400 | [diff] [blame] | 483 | if (!has_bspi(qspi)) |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 484 | return; |
| 485 | |
| 486 | qspi->bspi_enabled = 1; |
| 487 | if ((bcm_qspi_read(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL) & 1) == 0) |
| 488 | return; |
| 489 | |
| 490 | bcm_qspi_bspi_flush_prefetch_buffers(qspi); |
| 491 | udelay(1); |
| 492 | bcm_qspi_write(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL, 0); |
| 493 | udelay(1); |
| 494 | } |
| 495 | |
| 496 | static void bcm_qspi_disable_bspi(struct bcm_qspi *qspi) |
| 497 | { |
Kamal Dasu | 602805f | 2018-04-26 14:48:01 -0400 | [diff] [blame] | 498 | if (!has_bspi(qspi)) |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 499 | return; |
| 500 | |
| 501 | qspi->bspi_enabled = 0; |
| 502 | if ((bcm_qspi_read(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL) & 1)) |
| 503 | return; |
| 504 | |
| 505 | bcm_qspi_bspi_busy_poll(qspi); |
| 506 | bcm_qspi_write(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL, 1); |
| 507 | udelay(1); |
| 508 | } |
| 509 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 510 | static void bcm_qspi_chip_select(struct bcm_qspi *qspi, int cs) |
| 511 | { |
Kamal Dasu | 5eb9a07 | 2018-04-26 14:48:00 -0400 | [diff] [blame] | 512 | u32 rd = 0; |
| 513 | u32 wr = 0; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 514 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 515 | if (qspi->base[CHIP_SELECT]) { |
Kamal Dasu | 5eb9a07 | 2018-04-26 14:48:00 -0400 | [diff] [blame] | 516 | rd = bcm_qspi_read(qspi, CHIP_SELECT, 0); |
| 517 | wr = (rd & ~0xff) | (1 << cs); |
| 518 | if (rd == wr) |
| 519 | return; |
| 520 | bcm_qspi_write(qspi, CHIP_SELECT, 0, wr); |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 521 | usleep_range(10, 20); |
| 522 | } |
Kamal Dasu | 5eb9a07 | 2018-04-26 14:48:00 -0400 | [diff] [blame] | 523 | |
| 524 | dev_dbg(&qspi->pdev->dev, "using cs:%d\n", cs); |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 525 | qspi->curr_cs = cs; |
| 526 | } |
| 527 | |
| 528 | /* MSPI helpers */ |
| 529 | static void bcm_qspi_hw_set_parms(struct bcm_qspi *qspi, |
| 530 | const struct bcm_qspi_parms *xp) |
| 531 | { |
| 532 | u32 spcr, spbr = 0; |
| 533 | |
| 534 | if (xp->speed_hz) |
| 535 | spbr = qspi->base_clk / (2 * xp->speed_hz); |
| 536 | |
| 537 | spcr = clamp_val(spbr, QSPI_SPBR_MIN, QSPI_SPBR_MAX); |
| 538 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR0_LSB, spcr); |
| 539 | |
| 540 | spcr = MSPI_MASTER_BIT; |
| 541 | /* for 16 bit the data should be zero */ |
| 542 | if (xp->bits_per_word != 16) |
| 543 | spcr |= xp->bits_per_word << 2; |
| 544 | spcr |= xp->mode & 3; |
| 545 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR0_MSB, spcr); |
| 546 | |
| 547 | qspi->last_parms = *xp; |
| 548 | } |
| 549 | |
| 550 | static void bcm_qspi_update_parms(struct bcm_qspi *qspi, |
| 551 | struct spi_device *spi, |
| 552 | struct spi_transfer *trans) |
| 553 | { |
| 554 | struct bcm_qspi_parms xp; |
| 555 | |
| 556 | xp.speed_hz = trans->speed_hz; |
| 557 | xp.bits_per_word = trans->bits_per_word; |
| 558 | xp.mode = spi->mode; |
| 559 | |
| 560 | bcm_qspi_hw_set_parms(qspi, &xp); |
| 561 | } |
| 562 | |
| 563 | static int bcm_qspi_setup(struct spi_device *spi) |
| 564 | { |
| 565 | struct bcm_qspi_parms *xp; |
| 566 | |
| 567 | if (spi->bits_per_word > 16) |
| 568 | return -EINVAL; |
| 569 | |
| 570 | xp = spi_get_ctldata(spi); |
| 571 | if (!xp) { |
| 572 | xp = kzalloc(sizeof(*xp), GFP_KERNEL); |
| 573 | if (!xp) |
| 574 | return -ENOMEM; |
| 575 | spi_set_ctldata(spi, xp); |
| 576 | } |
| 577 | xp->speed_hz = spi->max_speed_hz; |
| 578 | xp->mode = spi->mode; |
| 579 | |
| 580 | if (spi->bits_per_word) |
| 581 | xp->bits_per_word = spi->bits_per_word; |
| 582 | else |
| 583 | xp->bits_per_word = 8; |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 588 | static bool bcm_qspi_mspi_transfer_is_last(struct bcm_qspi *qspi, |
| 589 | struct qspi_trans *qt) |
| 590 | { |
| 591 | if (qt->mspi_last_trans && |
| 592 | spi_transfer_is_last(qspi->master, qt->trans)) |
| 593 | return true; |
| 594 | else |
| 595 | return false; |
| 596 | } |
| 597 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 598 | static int update_qspi_trans_byte_count(struct bcm_qspi *qspi, |
| 599 | struct qspi_trans *qt, int flags) |
| 600 | { |
| 601 | int ret = TRANS_STATUS_BREAK_NONE; |
| 602 | |
| 603 | /* count the last transferred bytes */ |
| 604 | if (qt->trans->bits_per_word <= 8) |
| 605 | qt->byte++; |
| 606 | else |
| 607 | qt->byte += 2; |
| 608 | |
| 609 | if (qt->byte >= qt->trans->len) { |
| 610 | /* we're at the end of the spi_transfer */ |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 611 | /* in TX mode, need to pause for a delay or CS change */ |
| 612 | if (qt->trans->delay_usecs && |
| 613 | (flags & TRANS_STATUS_BREAK_DELAY)) |
| 614 | ret |= TRANS_STATUS_BREAK_DELAY; |
| 615 | if (qt->trans->cs_change && |
| 616 | (flags & TRANS_STATUS_BREAK_CS_CHANGE)) |
| 617 | ret |= TRANS_STATUS_BREAK_CS_CHANGE; |
| 618 | if (ret) |
| 619 | goto done; |
| 620 | |
| 621 | dev_dbg(&qspi->pdev->dev, "advance msg exit\n"); |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 622 | if (bcm_qspi_mspi_transfer_is_last(qspi, qt)) |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 623 | ret = TRANS_STATUS_BREAK_EOM; |
| 624 | else |
| 625 | ret = TRANS_STATUS_BREAK_NO_BYTES; |
| 626 | |
| 627 | qt->trans = NULL; |
| 628 | } |
| 629 | |
| 630 | done: |
| 631 | dev_dbg(&qspi->pdev->dev, "trans %p len %d byte %d ret %x\n", |
| 632 | qt->trans, qt->trans ? qt->trans->len : 0, qt->byte, ret); |
| 633 | return ret; |
| 634 | } |
| 635 | |
| 636 | static inline u8 read_rxram_slot_u8(struct bcm_qspi *qspi, int slot) |
| 637 | { |
| 638 | u32 slot_offset = MSPI_RXRAM + (slot << 3) + 0x4; |
| 639 | |
| 640 | /* mask out reserved bits */ |
| 641 | return bcm_qspi_read(qspi, MSPI, slot_offset) & 0xff; |
| 642 | } |
| 643 | |
| 644 | static inline u16 read_rxram_slot_u16(struct bcm_qspi *qspi, int slot) |
| 645 | { |
| 646 | u32 reg_offset = MSPI_RXRAM; |
| 647 | u32 lsb_offset = reg_offset + (slot << 3) + 0x4; |
| 648 | u32 msb_offset = reg_offset + (slot << 3); |
| 649 | |
| 650 | return (bcm_qspi_read(qspi, MSPI, lsb_offset) & 0xff) | |
| 651 | ((bcm_qspi_read(qspi, MSPI, msb_offset) & 0xff) << 8); |
| 652 | } |
| 653 | |
| 654 | static void read_from_hw(struct bcm_qspi *qspi, int slots) |
| 655 | { |
| 656 | struct qspi_trans tp; |
| 657 | int slot; |
| 658 | |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 659 | bcm_qspi_disable_bspi(qspi); |
| 660 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 661 | if (slots > MSPI_NUM_CDRAM) { |
| 662 | /* should never happen */ |
| 663 | dev_err(&qspi->pdev->dev, "%s: too many slots!\n", __func__); |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | tp = qspi->trans_pos; |
| 668 | |
| 669 | for (slot = 0; slot < slots; slot++) { |
| 670 | if (tp.trans->bits_per_word <= 8) { |
| 671 | u8 *buf = tp.trans->rx_buf; |
| 672 | |
| 673 | if (buf) |
| 674 | buf[tp.byte] = read_rxram_slot_u8(qspi, slot); |
| 675 | dev_dbg(&qspi->pdev->dev, "RD %02x\n", |
| 676 | buf ? buf[tp.byte] : 0xff); |
| 677 | } else { |
| 678 | u16 *buf = tp.trans->rx_buf; |
| 679 | |
| 680 | if (buf) |
| 681 | buf[tp.byte / 2] = read_rxram_slot_u16(qspi, |
| 682 | slot); |
| 683 | dev_dbg(&qspi->pdev->dev, "RD %04x\n", |
| 684 | buf ? buf[tp.byte] : 0xffff); |
| 685 | } |
| 686 | |
| 687 | update_qspi_trans_byte_count(qspi, &tp, |
| 688 | TRANS_STATUS_BREAK_NONE); |
| 689 | } |
| 690 | |
| 691 | qspi->trans_pos = tp; |
| 692 | } |
| 693 | |
| 694 | static inline void write_txram_slot_u8(struct bcm_qspi *qspi, int slot, |
| 695 | u8 val) |
| 696 | { |
| 697 | u32 reg_offset = MSPI_TXRAM + (slot << 3); |
| 698 | |
| 699 | /* mask out reserved bits */ |
| 700 | bcm_qspi_write(qspi, MSPI, reg_offset, val); |
| 701 | } |
| 702 | |
| 703 | static inline void write_txram_slot_u16(struct bcm_qspi *qspi, int slot, |
| 704 | u16 val) |
| 705 | { |
| 706 | u32 reg_offset = MSPI_TXRAM; |
| 707 | u32 msb_offset = reg_offset + (slot << 3); |
| 708 | u32 lsb_offset = reg_offset + (slot << 3) + 0x4; |
| 709 | |
| 710 | bcm_qspi_write(qspi, MSPI, msb_offset, (val >> 8)); |
| 711 | bcm_qspi_write(qspi, MSPI, lsb_offset, (val & 0xff)); |
| 712 | } |
| 713 | |
| 714 | static inline u32 read_cdram_slot(struct bcm_qspi *qspi, int slot) |
| 715 | { |
| 716 | return bcm_qspi_read(qspi, MSPI, MSPI_CDRAM + (slot << 2)); |
| 717 | } |
| 718 | |
| 719 | static inline void write_cdram_slot(struct bcm_qspi *qspi, int slot, u32 val) |
| 720 | { |
| 721 | bcm_qspi_write(qspi, MSPI, (MSPI_CDRAM + (slot << 2)), val); |
| 722 | } |
| 723 | |
| 724 | /* Return number of slots written */ |
| 725 | static int write_to_hw(struct bcm_qspi *qspi, struct spi_device *spi) |
| 726 | { |
| 727 | struct qspi_trans tp; |
| 728 | int slot = 0, tstatus = 0; |
| 729 | u32 mspi_cdram = 0; |
| 730 | |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 731 | bcm_qspi_disable_bspi(qspi); |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 732 | tp = qspi->trans_pos; |
| 733 | bcm_qspi_update_parms(qspi, spi, tp.trans); |
| 734 | |
| 735 | /* Run until end of transfer or reached the max data */ |
| 736 | while (!tstatus && slot < MSPI_NUM_CDRAM) { |
| 737 | if (tp.trans->bits_per_word <= 8) { |
| 738 | const u8 *buf = tp.trans->tx_buf; |
| 739 | u8 val = buf ? buf[tp.byte] : 0xff; |
| 740 | |
| 741 | write_txram_slot_u8(qspi, slot, val); |
| 742 | dev_dbg(&qspi->pdev->dev, "WR %02x\n", val); |
| 743 | } else { |
| 744 | const u16 *buf = tp.trans->tx_buf; |
| 745 | u16 val = buf ? buf[tp.byte / 2] : 0xffff; |
| 746 | |
| 747 | write_txram_slot_u16(qspi, slot, val); |
| 748 | dev_dbg(&qspi->pdev->dev, "WR %04x\n", val); |
| 749 | } |
| 750 | mspi_cdram = MSPI_CDRAM_CONT_BIT; |
Kamal Dasu | 5eb9a07 | 2018-04-26 14:48:00 -0400 | [diff] [blame] | 751 | |
| 752 | if (has_bspi(qspi)) |
| 753 | mspi_cdram &= ~1; |
| 754 | else |
| 755 | mspi_cdram |= (~(1 << spi->chip_select) & |
| 756 | MSPI_CDRAM_PCS); |
| 757 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 758 | mspi_cdram |= ((tp.trans->bits_per_word <= 8) ? 0 : |
| 759 | MSPI_CDRAM_BITSE_BIT); |
| 760 | |
| 761 | write_cdram_slot(qspi, slot, mspi_cdram); |
| 762 | |
| 763 | tstatus = update_qspi_trans_byte_count(qspi, &tp, |
| 764 | TRANS_STATUS_BREAK_TX); |
| 765 | slot++; |
| 766 | } |
| 767 | |
| 768 | if (!slot) { |
| 769 | dev_err(&qspi->pdev->dev, "%s: no data to send?", __func__); |
| 770 | goto done; |
| 771 | } |
| 772 | |
| 773 | dev_dbg(&qspi->pdev->dev, "submitting %d slots\n", slot); |
| 774 | bcm_qspi_write(qspi, MSPI, MSPI_NEWQP, 0); |
| 775 | bcm_qspi_write(qspi, MSPI, MSPI_ENDQP, slot - 1); |
| 776 | |
| 777 | if (tstatus & TRANS_STATUS_BREAK_DESELECT) { |
| 778 | mspi_cdram = read_cdram_slot(qspi, slot - 1) & |
| 779 | ~MSPI_CDRAM_CONT_BIT; |
| 780 | write_cdram_slot(qspi, slot - 1, mspi_cdram); |
| 781 | } |
| 782 | |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 783 | if (has_bspi(qspi)) |
| 784 | bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 1); |
| 785 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 786 | /* Must flush previous writes before starting MSPI operation */ |
| 787 | mb(); |
| 788 | /* Set cont | spe | spifie */ |
| 789 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0xe0); |
| 790 | |
| 791 | done: |
| 792 | return slot; |
| 793 | } |
| 794 | |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 795 | static int bcm_qspi_bspi_exec_mem_op(struct spi_device *spi, |
| 796 | const struct spi_mem_op *op) |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 797 | { |
| 798 | struct bcm_qspi *qspi = spi_master_get_devdata(spi->master); |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 799 | u32 addr = 0, len, rdlen, len_words, from = 0; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 800 | int ret = 0; |
| 801 | unsigned long timeo = msecs_to_jiffies(100); |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 802 | struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 803 | |
| 804 | if (bcm_qspi_bspi_ver_three(qspi)) |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 805 | if (op->addr.nbytes == BSPI_ADDRLEN_4BYTES) |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 806 | return -EIO; |
| 807 | |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 808 | from = op->addr.val; |
Chris Packham | 27fb231 | 2019-11-07 17:42:34 +1300 | [diff] [blame] | 809 | if (!spi->cs_gpiod) |
| 810 | bcm_qspi_chip_select(qspi, spi->chip_select); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 811 | bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 0); |
| 812 | |
| 813 | /* |
Kamal Dasu | 345309f | 2017-02-08 15:15:04 -0500 | [diff] [blame] | 814 | * when using flex mode we need to send |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 815 | * the upper address byte to bspi |
| 816 | */ |
| 817 | if (bcm_qspi_bspi_ver_three(qspi) == false) { |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 818 | addr = from & 0xff000000; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 819 | bcm_qspi_write(qspi, BSPI, |
| 820 | BSPI_BSPI_FLASH_UPPER_ADDR_BYTE, addr); |
| 821 | } |
| 822 | |
| 823 | if (!qspi->xfer_mode.flex_mode) |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 824 | addr = from; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 825 | else |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 826 | addr = from & 0x00ffffff; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 827 | |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 828 | if (bcm_qspi_bspi_ver_three(qspi) == true) |
| 829 | addr = (addr + 0xc00000) & 0xffffff; |
| 830 | |
Kamal Dasu | 345309f | 2017-02-08 15:15:04 -0500 | [diff] [blame] | 831 | /* |
| 832 | * read into the entire buffer by breaking the reads |
| 833 | * into RAF buffer read lengths |
| 834 | */ |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 835 | len = op->data.nbytes; |
| 836 | qspi->bspi_rf_op_idx = 0; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 837 | |
Kamal Dasu | 345309f | 2017-02-08 15:15:04 -0500 | [diff] [blame] | 838 | do { |
| 839 | if (len > BSPI_READ_LENGTH) |
| 840 | rdlen = BSPI_READ_LENGTH; |
| 841 | else |
| 842 | rdlen = len; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 843 | |
Kamal Dasu | 345309f | 2017-02-08 15:15:04 -0500 | [diff] [blame] | 844 | reinit_completion(&qspi->bspi_done); |
| 845 | bcm_qspi_enable_bspi(qspi); |
| 846 | len_words = (rdlen + 3) >> 2; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 847 | qspi->bspi_rf_op = op; |
| 848 | qspi->bspi_rf_op_status = 0; |
| 849 | qspi->bspi_rf_op_len = rdlen; |
Kamal Dasu | 345309f | 2017-02-08 15:15:04 -0500 | [diff] [blame] | 850 | dev_dbg(&qspi->pdev->dev, |
| 851 | "bspi xfr addr 0x%x len 0x%x", addr, rdlen); |
| 852 | bcm_qspi_write(qspi, BSPI, BSPI_RAF_START_ADDR, addr); |
| 853 | bcm_qspi_write(qspi, BSPI, BSPI_RAF_NUM_WORDS, len_words); |
| 854 | bcm_qspi_write(qspi, BSPI, BSPI_RAF_WATERMARK, 0); |
| 855 | if (qspi->soc_intc) { |
| 856 | /* |
| 857 | * clear soc MSPI and BSPI interrupts and enable |
| 858 | * BSPI interrupts. |
| 859 | */ |
| 860 | soc_intc->bcm_qspi_int_ack(soc_intc, MSPI_BSPI_DONE); |
| 861 | soc_intc->bcm_qspi_int_set(soc_intc, BSPI_DONE, true); |
| 862 | } |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 863 | |
Kamal Dasu | 345309f | 2017-02-08 15:15:04 -0500 | [diff] [blame] | 864 | /* Must flush previous writes before starting BSPI operation */ |
| 865 | mb(); |
| 866 | bcm_qspi_bspi_lr_start(qspi); |
| 867 | if (!wait_for_completion_timeout(&qspi->bspi_done, timeo)) { |
| 868 | dev_err(&qspi->pdev->dev, "timeout waiting for BSPI\n"); |
| 869 | ret = -ETIMEDOUT; |
| 870 | break; |
| 871 | } |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 872 | |
Kamal Dasu | 345309f | 2017-02-08 15:15:04 -0500 | [diff] [blame] | 873 | /* set msg return length */ |
Kamal Dasu | 345309f | 2017-02-08 15:15:04 -0500 | [diff] [blame] | 874 | addr += rdlen; |
| 875 | len -= rdlen; |
| 876 | } while (len); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 877 | |
| 878 | return ret; |
| 879 | } |
| 880 | |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 881 | static int bcm_qspi_transfer_one(struct spi_master *master, |
| 882 | struct spi_device *spi, |
| 883 | struct spi_transfer *trans) |
| 884 | { |
| 885 | struct bcm_qspi *qspi = spi_master_get_devdata(master); |
| 886 | int slots; |
| 887 | unsigned long timeo = msecs_to_jiffies(100); |
| 888 | |
Chris Packham | 27fb231 | 2019-11-07 17:42:34 +1300 | [diff] [blame] | 889 | if (!spi->cs_gpiod) |
| 890 | bcm_qspi_chip_select(qspi, spi->chip_select); |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 891 | qspi->trans_pos.trans = trans; |
| 892 | qspi->trans_pos.byte = 0; |
| 893 | |
| 894 | while (qspi->trans_pos.byte < trans->len) { |
| 895 | reinit_completion(&qspi->mspi_done); |
| 896 | |
| 897 | slots = write_to_hw(qspi, spi); |
| 898 | if (!wait_for_completion_timeout(&qspi->mspi_done, timeo)) { |
| 899 | dev_err(&qspi->pdev->dev, "timeout waiting for MSPI\n"); |
| 900 | return -ETIMEDOUT; |
| 901 | } |
| 902 | |
| 903 | read_from_hw(qspi, slots); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 904 | } |
Rayagonda Kokatanur | ca10539 | 2019-08-30 09:58:45 +0530 | [diff] [blame] | 905 | bcm_qspi_enable_bspi(qspi); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 906 | |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 907 | return 0; |
| 908 | } |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 909 | |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 910 | static int bcm_qspi_mspi_exec_mem_op(struct spi_device *spi, |
| 911 | const struct spi_mem_op *op) |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 912 | { |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 913 | struct spi_master *master = spi->master; |
| 914 | struct bcm_qspi *qspi = spi_master_get_devdata(master); |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 915 | struct spi_transfer t[2]; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 916 | u8 cmd[6] = { }; |
| 917 | int ret, i; |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 918 | |
| 919 | memset(cmd, 0, sizeof(cmd)); |
| 920 | memset(t, 0, sizeof(t)); |
| 921 | |
| 922 | /* tx */ |
| 923 | /* opcode is in cmd[0] */ |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 924 | cmd[0] = op->cmd.opcode; |
| 925 | for (i = 0; i < op->addr.nbytes; i++) |
| 926 | cmd[1 + i] = op->addr.val >> (8 * (op->addr.nbytes - i - 1)); |
| 927 | |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 928 | t[0].tx_buf = cmd; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 929 | t[0].len = op->addr.nbytes + op->dummy.nbytes + 1; |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 930 | t[0].bits_per_word = spi->bits_per_word; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 931 | t[0].tx_nbits = op->cmd.buswidth; |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 932 | /* lets mspi know that this is not last transfer */ |
| 933 | qspi->trans_pos.mspi_last_trans = false; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 934 | ret = bcm_qspi_transfer_one(master, spi, &t[0]); |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 935 | |
| 936 | /* rx */ |
| 937 | qspi->trans_pos.mspi_last_trans = true; |
| 938 | if (!ret) { |
| 939 | /* rx */ |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 940 | t[1].rx_buf = op->data.buf.in; |
| 941 | t[1].len = op->data.nbytes; |
| 942 | t[1].rx_nbits = op->data.buswidth; |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 943 | t[1].bits_per_word = spi->bits_per_word; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 944 | ret = bcm_qspi_transfer_one(master, spi, &t[1]); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | return ret; |
| 948 | } |
| 949 | |
Boris Brezillon | b645605 | 2018-05-12 08:24:54 +0200 | [diff] [blame] | 950 | static int bcm_qspi_exec_mem_op(struct spi_mem *mem, |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 951 | const struct spi_mem_op *op) |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 952 | { |
Boris Brezillon | b645605 | 2018-05-12 08:24:54 +0200 | [diff] [blame] | 953 | struct spi_device *spi = mem->spi; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 954 | struct bcm_qspi *qspi = spi_master_get_devdata(spi->master); |
| 955 | int ret = 0; |
| 956 | bool mspi_read = false; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 957 | u32 addr = 0, len; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 958 | u_char *buf; |
| 959 | |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 960 | if (!op->data.nbytes || !op->addr.nbytes || op->addr.nbytes > 4 || |
| 961 | op->data.dir != SPI_MEM_DATA_IN) |
| 962 | return -ENOTSUPP; |
| 963 | |
| 964 | buf = op->data.buf.in; |
| 965 | addr = op->addr.val; |
| 966 | len = op->data.nbytes; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 967 | |
| 968 | if (bcm_qspi_bspi_ver_three(qspi) == true) { |
| 969 | /* |
| 970 | * The address coming into this function is a raw flash offset. |
| 971 | * But for BSPI <= V3, we need to convert it to a remapped BSPI |
| 972 | * address. If it crosses a 4MB boundary, just revert back to |
| 973 | * using MSPI. |
| 974 | */ |
| 975 | addr = (addr + 0xc00000) & 0xffffff; |
| 976 | |
| 977 | if ((~ADDR_4MB_MASK & addr) ^ |
| 978 | (~ADDR_4MB_MASK & (addr + len - 1))) |
| 979 | mspi_read = true; |
| 980 | } |
| 981 | |
| 982 | /* non-aligned and very short transfers are handled by MSPI */ |
| 983 | if (!IS_ALIGNED((uintptr_t)addr, 4) || !IS_ALIGNED((uintptr_t)buf, 4) || |
| 984 | len < 4) |
| 985 | mspi_read = true; |
| 986 | |
| 987 | if (mspi_read) |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 988 | return bcm_qspi_mspi_exec_mem_op(spi, op); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 989 | |
Rayagonda Kokatanur | 79629d0 | 2019-08-06 15:37:50 +0530 | [diff] [blame] | 990 | ret = bcm_qspi_bspi_set_mode(qspi, op, 0); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 991 | |
| 992 | if (!ret) |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 993 | ret = bcm_qspi_bspi_exec_mem_op(spi, op); |
| 994 | |
| 995 | return ret; |
| 996 | } |
| 997 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 998 | static void bcm_qspi_cleanup(struct spi_device *spi) |
| 999 | { |
| 1000 | struct bcm_qspi_parms *xp = spi_get_ctldata(spi); |
| 1001 | |
| 1002 | kfree(xp); |
| 1003 | } |
| 1004 | |
| 1005 | static irqreturn_t bcm_qspi_mspi_l2_isr(int irq, void *dev_id) |
| 1006 | { |
| 1007 | struct bcm_qspi_dev_id *qspi_dev_id = dev_id; |
| 1008 | struct bcm_qspi *qspi = qspi_dev_id->dev; |
| 1009 | u32 status = bcm_qspi_read(qspi, MSPI, MSPI_MSPI_STATUS); |
| 1010 | |
| 1011 | if (status & MSPI_MSPI_STATUS_SPIF) { |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1012 | struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1013 | /* clear interrupt */ |
| 1014 | status &= ~MSPI_MSPI_STATUS_SPIF; |
| 1015 | bcm_qspi_write(qspi, MSPI, MSPI_MSPI_STATUS, status); |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1016 | if (qspi->soc_intc) |
| 1017 | soc_intc->bcm_qspi_int_ack(soc_intc, MSPI_DONE); |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1018 | complete(&qspi->mspi_done); |
| 1019 | return IRQ_HANDLED; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1020 | } |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1021 | |
| 1022 | return IRQ_NONE; |
| 1023 | } |
| 1024 | |
| 1025 | static irqreturn_t bcm_qspi_bspi_lr_l2_isr(int irq, void *dev_id) |
| 1026 | { |
| 1027 | struct bcm_qspi_dev_id *qspi_dev_id = dev_id; |
| 1028 | struct bcm_qspi *qspi = qspi_dev_id->dev; |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1029 | struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc; |
| 1030 | u32 status = qspi_dev_id->irqp->mask; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1031 | |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 1032 | if (qspi->bspi_enabled && qspi->bspi_rf_op) { |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1033 | bcm_qspi_bspi_lr_data_read(qspi); |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 1034 | if (qspi->bspi_rf_op_len == 0) { |
| 1035 | qspi->bspi_rf_op = NULL; |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1036 | if (qspi->soc_intc) { |
| 1037 | /* disable soc BSPI interrupt */ |
| 1038 | soc_intc->bcm_qspi_int_set(soc_intc, BSPI_DONE, |
| 1039 | false); |
| 1040 | /* indicate done */ |
| 1041 | status = INTR_BSPI_LR_SESSION_DONE_MASK; |
| 1042 | } |
| 1043 | |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 1044 | if (qspi->bspi_rf_op_status) |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1045 | bcm_qspi_bspi_lr_clear(qspi); |
| 1046 | else |
| 1047 | bcm_qspi_bspi_flush_prefetch_buffers(qspi); |
| 1048 | } |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1049 | |
| 1050 | if (qspi->soc_intc) |
| 1051 | /* clear soc BSPI interrupt */ |
| 1052 | soc_intc->bcm_qspi_int_ack(soc_intc, BSPI_DONE); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1053 | } |
| 1054 | |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1055 | status &= INTR_BSPI_LR_SESSION_DONE_MASK; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 1056 | if (qspi->bspi_enabled && status && qspi->bspi_rf_op_len == 0) |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1057 | complete(&qspi->bspi_done); |
| 1058 | |
| 1059 | return IRQ_HANDLED; |
| 1060 | } |
| 1061 | |
| 1062 | static irqreturn_t bcm_qspi_bspi_lr_err_l2_isr(int irq, void *dev_id) |
| 1063 | { |
| 1064 | struct bcm_qspi_dev_id *qspi_dev_id = dev_id; |
| 1065 | struct bcm_qspi *qspi = qspi_dev_id->dev; |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1066 | struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1067 | |
| 1068 | dev_err(&qspi->pdev->dev, "BSPI INT error\n"); |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 1069 | qspi->bspi_rf_op_status = -EIO; |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1070 | if (qspi->soc_intc) |
| 1071 | /* clear soc interrupt */ |
| 1072 | soc_intc->bcm_qspi_int_ack(soc_intc, BSPI_ERR); |
| 1073 | |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1074 | complete(&qspi->bspi_done); |
| 1075 | return IRQ_HANDLED; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1076 | } |
| 1077 | |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1078 | static irqreturn_t bcm_qspi_l1_isr(int irq, void *dev_id) |
| 1079 | { |
| 1080 | struct bcm_qspi_dev_id *qspi_dev_id = dev_id; |
| 1081 | struct bcm_qspi *qspi = qspi_dev_id->dev; |
| 1082 | struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc; |
| 1083 | irqreturn_t ret = IRQ_NONE; |
| 1084 | |
| 1085 | if (soc_intc) { |
| 1086 | u32 status = soc_intc->bcm_qspi_get_int_status(soc_intc); |
| 1087 | |
| 1088 | if (status & MSPI_DONE) |
| 1089 | ret = bcm_qspi_mspi_l2_isr(irq, dev_id); |
| 1090 | else if (status & BSPI_DONE) |
| 1091 | ret = bcm_qspi_bspi_lr_l2_isr(irq, dev_id); |
| 1092 | else if (status & BSPI_ERR) |
| 1093 | ret = bcm_qspi_bspi_lr_err_l2_isr(irq, dev_id); |
| 1094 | } |
| 1095 | |
| 1096 | return ret; |
| 1097 | } |
| 1098 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1099 | static const struct bcm_qspi_irq qspi_irq_tab[] = { |
| 1100 | { |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1101 | .irq_name = "spi_lr_fullness_reached", |
| 1102 | .irq_handler = bcm_qspi_bspi_lr_l2_isr, |
| 1103 | .mask = INTR_BSPI_LR_FULLNESS_REACHED_MASK, |
| 1104 | }, |
| 1105 | { |
| 1106 | .irq_name = "spi_lr_session_aborted", |
| 1107 | .irq_handler = bcm_qspi_bspi_lr_err_l2_isr, |
| 1108 | .mask = INTR_BSPI_LR_SESSION_ABORTED_MASK, |
| 1109 | }, |
| 1110 | { |
| 1111 | .irq_name = "spi_lr_impatient", |
| 1112 | .irq_handler = bcm_qspi_bspi_lr_err_l2_isr, |
| 1113 | .mask = INTR_BSPI_LR_IMPATIENT_MASK, |
| 1114 | }, |
| 1115 | { |
| 1116 | .irq_name = "spi_lr_session_done", |
| 1117 | .irq_handler = bcm_qspi_bspi_lr_l2_isr, |
| 1118 | .mask = INTR_BSPI_LR_SESSION_DONE_MASK, |
| 1119 | }, |
| 1120 | #ifdef QSPI_INT_DEBUG |
| 1121 | /* this interrupt is for debug purposes only, dont request irq */ |
| 1122 | { |
| 1123 | .irq_name = "spi_lr_overread", |
| 1124 | .irq_handler = bcm_qspi_bspi_lr_err_l2_isr, |
| 1125 | .mask = INTR_BSPI_LR_OVERREAD_MASK, |
| 1126 | }, |
| 1127 | #endif |
| 1128 | { |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1129 | .irq_name = "mspi_done", |
| 1130 | .irq_handler = bcm_qspi_mspi_l2_isr, |
| 1131 | .mask = INTR_MSPI_DONE_MASK, |
| 1132 | }, |
| 1133 | { |
| 1134 | .irq_name = "mspi_halted", |
| 1135 | .irq_handler = bcm_qspi_mspi_l2_isr, |
| 1136 | .mask = INTR_MSPI_HALTED_MASK, |
| 1137 | }, |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1138 | { |
| 1139 | /* single muxed L1 interrupt source */ |
| 1140 | .irq_name = "spi_l1_intr", |
| 1141 | .irq_handler = bcm_qspi_l1_isr, |
| 1142 | .irq_source = MUXED_L1, |
| 1143 | .mask = QSPI_INTERRUPTS_ALL, |
| 1144 | }, |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1145 | }; |
| 1146 | |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1147 | static void bcm_qspi_bspi_init(struct bcm_qspi *qspi) |
| 1148 | { |
| 1149 | u32 val = 0; |
| 1150 | |
| 1151 | val = bcm_qspi_read(qspi, BSPI, BSPI_REVISION_ID); |
| 1152 | qspi->bspi_maj_rev = (val >> 8) & 0xff; |
| 1153 | qspi->bspi_min_rev = val & 0xff; |
| 1154 | if (!(bcm_qspi_bspi_ver_three(qspi))) { |
| 1155 | /* Force mapping of BSPI address -> flash offset */ |
| 1156 | bcm_qspi_write(qspi, BSPI, BSPI_BSPI_XOR_VALUE, 0); |
| 1157 | bcm_qspi_write(qspi, BSPI, BSPI_BSPI_XOR_ENABLE, 1); |
| 1158 | } |
| 1159 | qspi->bspi_enabled = 1; |
| 1160 | bcm_qspi_disable_bspi(qspi); |
| 1161 | bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 0); |
| 1162 | bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 0); |
| 1163 | } |
| 1164 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1165 | static void bcm_qspi_hw_init(struct bcm_qspi *qspi) |
| 1166 | { |
| 1167 | struct bcm_qspi_parms parms; |
| 1168 | |
| 1169 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR1_LSB, 0); |
| 1170 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR1_MSB, 0); |
| 1171 | bcm_qspi_write(qspi, MSPI, MSPI_NEWQP, 0); |
| 1172 | bcm_qspi_write(qspi, MSPI, MSPI_ENDQP, 0); |
| 1173 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0x20); |
| 1174 | |
| 1175 | parms.mode = SPI_MODE_3; |
| 1176 | parms.bits_per_word = 8; |
| 1177 | parms.speed_hz = qspi->max_speed_hz; |
| 1178 | bcm_qspi_hw_set_parms(qspi, &parms); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1179 | |
| 1180 | if (has_bspi(qspi)) |
| 1181 | bcm_qspi_bspi_init(qspi); |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | static void bcm_qspi_hw_uninit(struct bcm_qspi *qspi) |
| 1185 | { |
| 1186 | bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1187 | if (has_bspi(qspi)) |
| 1188 | bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 0); |
| 1189 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1190 | } |
| 1191 | |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 1192 | static const struct spi_controller_mem_ops bcm_qspi_mem_ops = { |
Boris Brezillon | b645605 | 2018-05-12 08:24:54 +0200 | [diff] [blame] | 1193 | .exec_op = bcm_qspi_exec_mem_op, |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 1194 | }; |
| 1195 | |
Florian Fainelli | 3a01f04 | 2020-04-20 15:08:47 -0400 | [diff] [blame^] | 1196 | struct bcm_qspi_data { |
| 1197 | bool has_mspi_rev; |
| 1198 | }; |
| 1199 | |
| 1200 | static const struct bcm_qspi_data bcm_qspi_no_rev_data = { |
| 1201 | .has_mspi_rev = false, |
| 1202 | }; |
| 1203 | |
| 1204 | static const struct bcm_qspi_data bcm_qspi_rev_data = { |
| 1205 | .has_mspi_rev = true, |
| 1206 | }; |
| 1207 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1208 | static const struct of_device_id bcm_qspi_of_match[] = { |
Florian Fainelli | 3a01f04 | 2020-04-20 15:08:47 -0400 | [diff] [blame^] | 1209 | { |
| 1210 | .compatible = "brcm,spi-bcm7425-qspi", |
| 1211 | .data = &bcm_qspi_no_rev_data, |
| 1212 | }, |
| 1213 | { |
| 1214 | .compatible = "brcm,spi-bcm7429-qspi", |
| 1215 | .data = &bcm_qspi_no_rev_data, |
| 1216 | }, |
| 1217 | { |
| 1218 | .compatible = "brcm,spi-bcm7435-qspi", |
| 1219 | .data = &bcm_qspi_no_rev_data, |
| 1220 | }, |
| 1221 | { |
| 1222 | .compatible = "brcm,spi-bcm-qspi", |
| 1223 | .data = &bcm_qspi_rev_data, |
| 1224 | }, |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1225 | {}, |
| 1226 | }; |
| 1227 | MODULE_DEVICE_TABLE(of, bcm_qspi_of_match); |
| 1228 | |
| 1229 | int bcm_qspi_probe(struct platform_device *pdev, |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1230 | struct bcm_qspi_soc_intc *soc_intc) |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1231 | { |
Florian Fainelli | 3a01f04 | 2020-04-20 15:08:47 -0400 | [diff] [blame^] | 1232 | const struct of_device_id *of_id = NULL; |
| 1233 | const struct bcm_qspi_data *data; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1234 | struct device *dev = &pdev->dev; |
| 1235 | struct bcm_qspi *qspi; |
| 1236 | struct spi_master *master; |
| 1237 | struct resource *res; |
| 1238 | int irq, ret = 0, num_ints = 0; |
| 1239 | u32 val; |
Florian Fainelli | 3a01f04 | 2020-04-20 15:08:47 -0400 | [diff] [blame^] | 1240 | u32 rev = 0; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1241 | const char *name = NULL; |
| 1242 | int num_irqs = ARRAY_SIZE(qspi_irq_tab); |
| 1243 | |
| 1244 | /* We only support device-tree instantiation */ |
| 1245 | if (!dev->of_node) |
| 1246 | return -ENODEV; |
| 1247 | |
Florian Fainelli | 3a01f04 | 2020-04-20 15:08:47 -0400 | [diff] [blame^] | 1248 | of_id = of_match_node(bcm_qspi_of_match, dev->of_node); |
| 1249 | if (!of_id) |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1250 | return -ENODEV; |
| 1251 | |
Florian Fainelli | 3a01f04 | 2020-04-20 15:08:47 -0400 | [diff] [blame^] | 1252 | data = of_id->data; |
| 1253 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1254 | master = spi_alloc_master(dev, sizeof(struct bcm_qspi)); |
| 1255 | if (!master) { |
| 1256 | dev_err(dev, "error allocating spi_master\n"); |
| 1257 | return -ENOMEM; |
| 1258 | } |
| 1259 | |
| 1260 | qspi = spi_master_get_devdata(master); |
| 1261 | qspi->pdev = pdev; |
| 1262 | qspi->trans_pos.trans = NULL; |
| 1263 | qspi->trans_pos.byte = 0; |
Kamal Dasu | 81ab52f | 2017-01-30 16:11:16 -0500 | [diff] [blame] | 1264 | qspi->trans_pos.mspi_last_trans = true; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1265 | qspi->master = master; |
| 1266 | |
| 1267 | master->bus_num = -1; |
| 1268 | master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_RX_DUAL | SPI_RX_QUAD; |
| 1269 | master->setup = bcm_qspi_setup; |
| 1270 | master->transfer_one = bcm_qspi_transfer_one; |
Boris Brezillon | 5f195ee | 2018-04-26 18:18:16 +0200 | [diff] [blame] | 1271 | master->mem_ops = &bcm_qspi_mem_ops; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1272 | master->cleanup = bcm_qspi_cleanup; |
| 1273 | master->dev.of_node = dev->of_node; |
| 1274 | master->num_chipselect = NUM_CHIPSELECT; |
Chris Packham | 27fb231 | 2019-11-07 17:42:34 +1300 | [diff] [blame] | 1275 | master->use_gpio_descriptors = true; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1276 | |
| 1277 | qspi->big_endian = of_device_is_big_endian(dev->of_node); |
| 1278 | |
| 1279 | if (!of_property_read_u32(dev->of_node, "num-cs", &val)) |
| 1280 | master->num_chipselect = val; |
| 1281 | |
| 1282 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hif_mspi"); |
| 1283 | if (!res) |
| 1284 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 1285 | "mspi"); |
| 1286 | |
| 1287 | if (res) { |
| 1288 | qspi->base[MSPI] = devm_ioremap_resource(dev, res); |
| 1289 | if (IS_ERR(qspi->base[MSPI])) { |
| 1290 | ret = PTR_ERR(qspi->base[MSPI]); |
Christophe Jaillet | bc3cc75 | 2018-03-13 19:36:58 +0100 | [diff] [blame] | 1291 | goto qspi_resource_err; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1292 | } |
| 1293 | } else { |
Florian Fainelli | c0368e4 | 2017-10-11 14:59:22 -0700 | [diff] [blame] | 1294 | goto qspi_resource_err; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1295 | } |
| 1296 | |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1297 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "bspi"); |
| 1298 | if (res) { |
| 1299 | qspi->base[BSPI] = devm_ioremap_resource(dev, res); |
| 1300 | if (IS_ERR(qspi->base[BSPI])) { |
| 1301 | ret = PTR_ERR(qspi->base[BSPI]); |
Christophe Jaillet | bc3cc75 | 2018-03-13 19:36:58 +0100 | [diff] [blame] | 1302 | goto qspi_resource_err; |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1303 | } |
| 1304 | qspi->bspi_mode = true; |
| 1305 | } else { |
| 1306 | qspi->bspi_mode = false; |
| 1307 | } |
| 1308 | |
| 1309 | dev_info(dev, "using %smspi mode\n", qspi->bspi_mode ? "bspi-" : ""); |
| 1310 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1311 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs_reg"); |
| 1312 | if (res) { |
| 1313 | qspi->base[CHIP_SELECT] = devm_ioremap_resource(dev, res); |
| 1314 | if (IS_ERR(qspi->base[CHIP_SELECT])) { |
| 1315 | ret = PTR_ERR(qspi->base[CHIP_SELECT]); |
Florian Fainelli | c0368e4 | 2017-10-11 14:59:22 -0700 | [diff] [blame] | 1316 | goto qspi_resource_err; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | qspi->dev_ids = kcalloc(num_irqs, sizeof(struct bcm_qspi_dev_id), |
| 1321 | GFP_KERNEL); |
Wei Yongjun | 3bf3eb2 | 2016-09-16 13:45:17 +0000 | [diff] [blame] | 1322 | if (!qspi->dev_ids) { |
| 1323 | ret = -ENOMEM; |
Florian Fainelli | c0368e4 | 2017-10-11 14:59:22 -0700 | [diff] [blame] | 1324 | goto qspi_resource_err; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | for (val = 0; val < num_irqs; val++) { |
| 1328 | irq = -1; |
| 1329 | name = qspi_irq_tab[val].irq_name; |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1330 | if (qspi_irq_tab[val].irq_source == SINGLE_L2) { |
| 1331 | /* get the l2 interrupts */ |
Rayagonda Kokatanur | e9aa3b8 | 2020-01-07 09:39:12 +0530 | [diff] [blame] | 1332 | irq = platform_get_irq_byname_optional(pdev, name); |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1333 | } else if (!num_ints && soc_intc) { |
| 1334 | /* all mspi, bspi intrs muxed to one L1 intr */ |
| 1335 | irq = platform_get_irq(pdev, 0); |
| 1336 | } |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1337 | |
| 1338 | if (irq >= 0) { |
| 1339 | ret = devm_request_irq(&pdev->dev, irq, |
| 1340 | qspi_irq_tab[val].irq_handler, 0, |
| 1341 | name, |
| 1342 | &qspi->dev_ids[val]); |
| 1343 | if (ret < 0) { |
| 1344 | dev_err(&pdev->dev, "IRQ %s not found\n", name); |
| 1345 | goto qspi_probe_err; |
| 1346 | } |
| 1347 | |
| 1348 | qspi->dev_ids[val].dev = qspi; |
| 1349 | qspi->dev_ids[val].irqp = &qspi_irq_tab[val]; |
| 1350 | num_ints++; |
| 1351 | dev_dbg(&pdev->dev, "registered IRQ %s %d\n", |
| 1352 | qspi_irq_tab[val].irq_name, |
| 1353 | irq); |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | if (!num_ints) { |
| 1358 | dev_err(&pdev->dev, "no IRQs registered, cannot init driver\n"); |
Wei Yongjun | 71b8f35 | 2016-09-16 14:00:19 +0000 | [diff] [blame] | 1359 | ret = -EINVAL; |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1360 | goto qspi_probe_err; |
| 1361 | } |
| 1362 | |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1363 | /* |
| 1364 | * Some SoCs integrate spi controller (e.g., its interrupt bits) |
| 1365 | * in specific ways |
| 1366 | */ |
| 1367 | if (soc_intc) { |
| 1368 | qspi->soc_intc = soc_intc; |
| 1369 | soc_intc->bcm_qspi_int_set(soc_intc, MSPI_DONE, true); |
| 1370 | } else { |
| 1371 | qspi->soc_intc = NULL; |
| 1372 | } |
| 1373 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1374 | qspi->clk = devm_clk_get(&pdev->dev, NULL); |
| 1375 | if (IS_ERR(qspi->clk)) { |
| 1376 | dev_warn(dev, "unable to get clock\n"); |
Wei Yongjun | 71b8f35 | 2016-09-16 14:00:19 +0000 | [diff] [blame] | 1377 | ret = PTR_ERR(qspi->clk); |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1378 | goto qspi_probe_err; |
| 1379 | } |
| 1380 | |
| 1381 | ret = clk_prepare_enable(qspi->clk); |
| 1382 | if (ret) { |
| 1383 | dev_err(dev, "failed to prepare clock\n"); |
| 1384 | goto qspi_probe_err; |
| 1385 | } |
| 1386 | |
| 1387 | qspi->base_clk = clk_get_rate(qspi->clk); |
| 1388 | qspi->max_speed_hz = qspi->base_clk / (QSPI_SPBR_MIN * 2); |
| 1389 | |
Florian Fainelli | 3a01f04 | 2020-04-20 15:08:47 -0400 | [diff] [blame^] | 1390 | if (data->has_mspi_rev) { |
| 1391 | rev = bcm_qspi_read(qspi, MSPI, MSPI_REV); |
| 1392 | /* some older revs do not have a MSPI_REV register */ |
| 1393 | if ((rev & 0xff) == 0xff) |
| 1394 | rev = 0; |
| 1395 | } |
| 1396 | |
| 1397 | qspi->mspi_maj_rev = (rev >> 4) & 0xf; |
| 1398 | qspi->mspi_min_rev = rev & 0xf; |
| 1399 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1400 | bcm_qspi_hw_init(qspi); |
| 1401 | init_completion(&qspi->mspi_done); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1402 | init_completion(&qspi->bspi_done); |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1403 | qspi->curr_cs = -1; |
| 1404 | |
| 1405 | platform_set_drvdata(pdev, qspi); |
Kamal Dasu | 4e3b2d2 | 2016-08-24 18:04:25 -0400 | [diff] [blame] | 1406 | |
| 1407 | qspi->xfer_mode.width = -1; |
| 1408 | qspi->xfer_mode.addrlen = -1; |
| 1409 | qspi->xfer_mode.hp = -1; |
| 1410 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1411 | ret = devm_spi_register_master(&pdev->dev, master); |
| 1412 | if (ret < 0) { |
| 1413 | dev_err(dev, "can't register master\n"); |
| 1414 | goto qspi_reg_err; |
| 1415 | } |
| 1416 | |
| 1417 | return 0; |
| 1418 | |
| 1419 | qspi_reg_err: |
| 1420 | bcm_qspi_hw_uninit(qspi); |
| 1421 | clk_disable_unprepare(qspi->clk); |
| 1422 | qspi_probe_err: |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1423 | kfree(qspi->dev_ids); |
Florian Fainelli | c0368e4 | 2017-10-11 14:59:22 -0700 | [diff] [blame] | 1424 | qspi_resource_err: |
| 1425 | spi_master_put(master); |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1426 | return ret; |
| 1427 | } |
| 1428 | /* probe function to be called by SoC specific platform driver probe */ |
| 1429 | EXPORT_SYMBOL_GPL(bcm_qspi_probe); |
| 1430 | |
| 1431 | int bcm_qspi_remove(struct platform_device *pdev) |
| 1432 | { |
| 1433 | struct bcm_qspi *qspi = platform_get_drvdata(pdev); |
| 1434 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1435 | bcm_qspi_hw_uninit(qspi); |
| 1436 | clk_disable_unprepare(qspi->clk); |
| 1437 | kfree(qspi->dev_ids); |
| 1438 | spi_unregister_master(qspi->master); |
| 1439 | |
| 1440 | return 0; |
| 1441 | } |
| 1442 | /* function to be called by SoC specific platform driver remove() */ |
| 1443 | EXPORT_SYMBOL_GPL(bcm_qspi_remove); |
| 1444 | |
Arnd Bergmann | a0319f8b1 | 2016-09-15 17:46:53 +0200 | [diff] [blame] | 1445 | static int __maybe_unused bcm_qspi_suspend(struct device *dev) |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1446 | { |
| 1447 | struct bcm_qspi *qspi = dev_get_drvdata(dev); |
| 1448 | |
Kamal Dasu | 054e532 | 2017-07-26 19:20:15 -0400 | [diff] [blame] | 1449 | /* store the override strap value */ |
| 1450 | if (!bcm_qspi_bspi_ver_three(qspi)) |
| 1451 | qspi->s3_strap_override_ctrl = |
| 1452 | bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL); |
| 1453 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1454 | spi_master_suspend(qspi->master); |
| 1455 | clk_disable(qspi->clk); |
| 1456 | bcm_qspi_hw_uninit(qspi); |
| 1457 | |
| 1458 | return 0; |
| 1459 | }; |
| 1460 | |
Arnd Bergmann | a0319f8b1 | 2016-09-15 17:46:53 +0200 | [diff] [blame] | 1461 | static int __maybe_unused bcm_qspi_resume(struct device *dev) |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1462 | { |
| 1463 | struct bcm_qspi *qspi = dev_get_drvdata(dev); |
| 1464 | int ret = 0; |
| 1465 | |
| 1466 | bcm_qspi_hw_init(qspi); |
| 1467 | bcm_qspi_chip_select(qspi, qspi->curr_cs); |
Kamal Dasu | cc20a38 | 2016-08-24 18:04:29 -0400 | [diff] [blame] | 1468 | if (qspi->soc_intc) |
| 1469 | /* enable MSPI interrupt */ |
| 1470 | qspi->soc_intc->bcm_qspi_int_set(qspi->soc_intc, MSPI_DONE, |
| 1471 | true); |
| 1472 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1473 | ret = clk_enable(qspi->clk); |
| 1474 | if (!ret) |
| 1475 | spi_master_resume(qspi->master); |
| 1476 | |
| 1477 | return ret; |
| 1478 | } |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1479 | |
Arnd Bergmann | a0319f8b1 | 2016-09-15 17:46:53 +0200 | [diff] [blame] | 1480 | SIMPLE_DEV_PM_OPS(bcm_qspi_pm_ops, bcm_qspi_suspend, bcm_qspi_resume); |
| 1481 | |
Kamal Dasu | fa236a7 | 2016-08-24 18:04:23 -0400 | [diff] [blame] | 1482 | /* pm_ops to be called by SoC specific platform driver */ |
| 1483 | EXPORT_SYMBOL_GPL(bcm_qspi_pm_ops); |
| 1484 | |
| 1485 | MODULE_AUTHOR("Kamal Dasu"); |
| 1486 | MODULE_DESCRIPTION("Broadcom QSPI driver"); |
| 1487 | MODULE_LICENSE("GPL v2"); |
| 1488 | MODULE_ALIAS("platform:" DRIVER_NAME); |