blob: 1f157c35a3a370ed27cc812ef667557b5263b5c6 [file] [log] [blame]
Thomas Gleixnercb849fc2019-06-04 10:10:52 +02001// SPDX-License-Identifier: GPL-2.0-only
Kamal Dasufa236a72016-08-24 18:04:23 -04002/*
3 * Driver for Broadcom BRCMSTB, NSP, NS2, Cygnus SPI Controllers
4 *
5 * Copyright 2016 Broadcom
Kamal Dasufa236a72016-08-24 18:04:23 -04006 */
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 Dasufa236a72016-08-24 18:04:23 -040017#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 Brezillon5f195ee2018-04-26 18:18:16 +020022#include <linux/spi/spi-mem.h>
Kamal Dasufa236a72016-08-24 18:04:23 -040023#include <linux/sysfs.h>
24#include <linux/types.h>
25#include "spi-bcm-qspi.h"
26
27#define DRIVER_NAME "bcm_qspi"
28
Kamal Dasu4e3b2d22016-08-24 18:04:25 -040029
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łecki940ec772018-10-11 09:42:17 +020081#define BSPI_READ_LENGTH 256
Kamal Dasu4e3b2d22016-08-24 18:04:25 -040082
Kamal Dasufa236a72016-08-24 18:04:23 -040083/* 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 Fainelli3a01f042020-04-20 15:08:47 -040094#define MSPI_REV 0x02c
Kamal Dasufa236a72016-08-24 18:04:23 -040095#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
148struct bcm_qspi_parms {
149 u32 speed_hz;
150 u8 mode;
151 u8 bits_per_word;
152};
153
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400154struct bcm_xfer_mode {
155 bool flex_mode;
156 unsigned int width;
157 unsigned int addrlen;
158 unsigned int hp;
159};
160
Kamal Dasufa236a72016-08-24 18:04:23 -0400161enum base_type {
162 MSPI,
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400163 BSPI,
Kamal Dasufa236a72016-08-24 18:04:23 -0400164 CHIP_SELECT,
165 BASEMAX,
166};
167
Kamal Dasucc20a382016-08-24 18:04:29 -0400168enum irq_source {
169 SINGLE_L2,
170 MUXED_L1,
171};
172
Kamal Dasufa236a72016-08-24 18:04:23 -0400173struct bcm_qspi_irq {
174 const char *irq_name;
175 const irq_handler_t irq_handler;
Kamal Dasucc20a382016-08-24 18:04:29 -0400176 int irq_source;
Kamal Dasufa236a72016-08-24 18:04:23 -0400177 u32 mask;
178};
179
180struct bcm_qspi_dev_id {
181 const struct bcm_qspi_irq *irqp;
182 void *dev;
183};
184
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500185
Kamal Dasufa236a72016-08-24 18:04:23 -0400186struct qspi_trans {
187 struct spi_transfer *trans;
188 int byte;
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500189 bool mspi_last_trans;
Kamal Dasufa236a72016-08-24 18:04:23 -0400190};
191
192struct 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 Dasucc20a382016-08-24 18:04:29 -0400199
200 /* Some SoCs provide custom interrupt status register(s) */
201 struct bcm_qspi_soc_intc *soc_intc;
202
Kamal Dasufa236a72016-08-24 18:04:23 -0400203 struct bcm_qspi_parms last_parms;
204 struct qspi_trans trans_pos;
205 int curr_cs;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400206 int bspi_maj_rev;
207 int bspi_min_rev;
208 int bspi_enabled;
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200209 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 Dasu4e3b2d22016-08-24 18:04:25 -0400213 struct bcm_xfer_mode xfer_mode;
Kamal Dasufa236a72016-08-24 18:04:23 -0400214 u32 s3_strap_override_ctrl;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400215 bool bspi_mode;
Kamal Dasufa236a72016-08-24 18:04:23 -0400216 bool big_endian;
217 int num_irqs;
218 struct bcm_qspi_dev_id *dev_ids;
219 struct completion mspi_done;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400220 struct completion bspi_done;
Florian Fainelli3a01f042020-04-20 15:08:47 -0400221 u8 mspi_maj_rev;
222 u8 mspi_min_rev;
Kamal Dasufa236a72016-08-24 18:04:23 -0400223};
224
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400225static inline bool has_bspi(struct bcm_qspi *qspi)
226{
227 return qspi->bspi_mode;
228}
229
Kamal Dasufa236a72016-08-24 18:04:23 -0400230/* Read qspi controller register*/
231static 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*/
238static 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 Dasu4e3b2d22016-08-24 18:04:25 -0400244/* BSPI helpers */
245static 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
259static 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
266static 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
276static 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
282static 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
293static 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
300static 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
307static void bcm_qspi_bspi_lr_data_read(struct bcm_qspi *qspi)
308{
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200309 u32 *buf = (u32 *)qspi->bspi_rf_op->data.buf.in;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400310 u32 data = 0;
311
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200312 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 Dasu4e3b2d22016-08-24 18:04:25 -0400314 while (!bcm_qspi_bspi_lr_is_fifo_empty(qspi)) {
315 data = bcm_qspi_bspi_lr_read_fifo(qspi);
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200316 if (likely(qspi->bspi_rf_op_len >= 4) &&
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400317 IS_ALIGNED((uintptr_t)buf, 4)) {
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200318 buf[qspi->bspi_rf_op_idx++] = data;
319 qspi->bspi_rf_op_len -= 4;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400320 } else {
321 /* Read out remaining bytes, make sure*/
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200322 u8 *cbuf = (u8 *)&buf[qspi->bspi_rf_op_idx];
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400323
324 data = cpu_to_le32(data);
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200325 while (qspi->bspi_rf_op_len) {
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400326 *cbuf++ = (u8)data;
327 data >>= 8;
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200328 qspi->bspi_rf_op_len--;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400329 }
330 }
331 }
332}
333
334static 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 Dasu054e5322017-07-26 19:20:15 -0400344static int bcm_qspi_bspi_set_flex_mode(struct bcm_qspi *qspi,
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200345 const struct spi_mem_op *op, int hp)
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400346{
347 int bpc = 0, bpp = 0;
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200348 u8 command = op->cmd.opcode;
Rayagonda Kokatanur79629d02019-08-06 15:37:50 +0530349 int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE;
Rafał Miłecki0976eda2018-10-11 09:40:22 +0200350 int addrlen = op->addr.nbytes;
Kamal Dasu054e5322017-07-26 19:20:15 -0400351 int flex_mode = 1;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400352
353 dev_dbg(&qspi->pdev->dev, "set flex mode w %x addrlen %x hp %d\n",
354 width, addrlen, hp);
355
Kamal Dasu054e5322017-07-26 19:20:15 -0400356 if (addrlen == BSPI_ADDRLEN_4BYTES)
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400357 bpp = BSPI_BPP_ADDR_SELECT_MASK;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400358
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200359 bpp |= (op->dummy.nbytes * 8) / op->dummy.buswidth;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400360
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 Dasu4e3b2d22016-08-24 18:04:25 -0400366 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 Dasu4e3b2d22016-08-24 18:04:25 -0400372 }
373 break;
374 case SPI_NBITS_QUAD:
375 bpc = 0x00000002;
376 if (hp) {
377 bpc |= 0x00020200; /* address and mode are 4-bit */
Kamal Dasu054e5322017-07-26 19:20:15 -0400378 bpp |= BSPI_BPP_MODE_SELECT_MASK;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400379 }
380 break;
381 default:
Kamal Dasu054e5322017-07-26 19:20:15 -0400382 return -EINVAL;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400383 }
384
Kamal Dasu054e5322017-07-26 19:20:15 -0400385 bcm_qspi_bspi_set_xfer_params(qspi, command, bpp, bpc, flex_mode);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400386
Kamal Dasu054e5322017-07-26 19:20:15 -0400387 return 0;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400388}
389
Kamal Dasu054e5322017-07-26 19:20:15 -0400390static int bcm_qspi_bspi_set_override(struct bcm_qspi *qspi,
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200391 const struct spi_mem_op *op, int hp)
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400392{
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200393 int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE;
394 int addrlen = op->addr.nbytes;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400395 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 Dasu4e3b2d22016-08-24 18:04:25 -0400406 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 Brezillon5f195ee2018-04-26 18:18:16 +0200430 bcm_qspi_bspi_set_xfer_params(qspi, op->cmd.opcode, 0, 0, 0);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400431
432 return 0;
433}
434
435static int bcm_qspi_bspi_set_mode(struct bcm_qspi *qspi,
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200436 const struct spi_mem_op *op, int hp)
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400437{
438 int error = 0;
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200439 int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE;
440 int addrlen = op->addr.nbytes;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400441
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 Dasu054e5322017-07-26 19:20:15 -0400452 bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, 0);
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200453 error = bcm_qspi_bspi_set_override(qspi, op, hp);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400454 }
455 }
456
457 if (qspi->xfer_mode.flex_mode)
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200458 error = bcm_qspi_bspi_set_flex_mode(qspi, op, hp);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400459
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
481static void bcm_qspi_enable_bspi(struct bcm_qspi *qspi)
482{
Kamal Dasu602805f2018-04-26 14:48:01 -0400483 if (!has_bspi(qspi))
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400484 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
496static void bcm_qspi_disable_bspi(struct bcm_qspi *qspi)
497{
Kamal Dasu602805f2018-04-26 14:48:01 -0400498 if (!has_bspi(qspi))
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400499 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 Dasufa236a72016-08-24 18:04:23 -0400510static void bcm_qspi_chip_select(struct bcm_qspi *qspi, int cs)
511{
Kamal Dasu5eb9a072018-04-26 14:48:00 -0400512 u32 rd = 0;
513 u32 wr = 0;
Kamal Dasufa236a72016-08-24 18:04:23 -0400514
Kamal Dasufa236a72016-08-24 18:04:23 -0400515 if (qspi->base[CHIP_SELECT]) {
Kamal Dasu5eb9a072018-04-26 14:48:00 -0400516 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 Dasufa236a72016-08-24 18:04:23 -0400521 usleep_range(10, 20);
522 }
Kamal Dasu5eb9a072018-04-26 14:48:00 -0400523
524 dev_dbg(&qspi->pdev->dev, "using cs:%d\n", cs);
Kamal Dasufa236a72016-08-24 18:04:23 -0400525 qspi->curr_cs = cs;
526}
527
528/* MSPI helpers */
529static 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
550static 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
563static 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 Dasu81ab52f2017-01-30 16:11:16 -0500588static 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 Dasufa236a72016-08-24 18:04:23 -0400598static 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 Dasufa236a72016-08-24 18:04:23 -0400611 /* 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 Dasu81ab52f2017-01-30 16:11:16 -0500622 if (bcm_qspi_mspi_transfer_is_last(qspi, qt))
Kamal Dasufa236a72016-08-24 18:04:23 -0400623 ret = TRANS_STATUS_BREAK_EOM;
624 else
625 ret = TRANS_STATUS_BREAK_NO_BYTES;
626
627 qt->trans = NULL;
628 }
629
630done:
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
636static 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
644static 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
654static void read_from_hw(struct bcm_qspi *qspi, int slots)
655{
656 struct qspi_trans tp;
657 int slot;
658
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400659 bcm_qspi_disable_bspi(qspi);
660
Kamal Dasufa236a72016-08-24 18:04:23 -0400661 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
694static 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
703static 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
714static 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
719static 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 */
725static 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 Dasu4e3b2d22016-08-24 18:04:25 -0400731 bcm_qspi_disable_bspi(qspi);
Kamal Dasufa236a72016-08-24 18:04:23 -0400732 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 Dasu5eb9a072018-04-26 14:48:00 -0400751
752 if (has_bspi(qspi))
753 mspi_cdram &= ~1;
754 else
755 mspi_cdram |= (~(1 << spi->chip_select) &
756 MSPI_CDRAM_PCS);
757
Kamal Dasufa236a72016-08-24 18:04:23 -0400758 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 Dasu4e3b2d22016-08-24 18:04:25 -0400783 if (has_bspi(qspi))
784 bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 1);
785
Kamal Dasufa236a72016-08-24 18:04:23 -0400786 /* 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
791done:
792 return slot;
793}
794
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200795static int bcm_qspi_bspi_exec_mem_op(struct spi_device *spi,
796 const struct spi_mem_op *op)
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400797{
798 struct bcm_qspi *qspi = spi_master_get_devdata(spi->master);
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200799 u32 addr = 0, len, rdlen, len_words, from = 0;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400800 int ret = 0;
801 unsigned long timeo = msecs_to_jiffies(100);
Kamal Dasucc20a382016-08-24 18:04:29 -0400802 struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400803
804 if (bcm_qspi_bspi_ver_three(qspi))
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200805 if (op->addr.nbytes == BSPI_ADDRLEN_4BYTES)
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400806 return -EIO;
807
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200808 from = op->addr.val;
Chris Packham27fb2312019-11-07 17:42:34 +1300809 if (!spi->cs_gpiod)
810 bcm_qspi_chip_select(qspi, spi->chip_select);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400811 bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 0);
812
813 /*
Kamal Dasu345309f2017-02-08 15:15:04 -0500814 * when using flex mode we need to send
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400815 * the upper address byte to bspi
816 */
817 if (bcm_qspi_bspi_ver_three(qspi) == false) {
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200818 addr = from & 0xff000000;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400819 bcm_qspi_write(qspi, BSPI,
820 BSPI_BSPI_FLASH_UPPER_ADDR_BYTE, addr);
821 }
822
823 if (!qspi->xfer_mode.flex_mode)
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200824 addr = from;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400825 else
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200826 addr = from & 0x00ffffff;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400827
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400828 if (bcm_qspi_bspi_ver_three(qspi) == true)
829 addr = (addr + 0xc00000) & 0xffffff;
830
Kamal Dasu345309f2017-02-08 15:15:04 -0500831 /*
832 * read into the entire buffer by breaking the reads
833 * into RAF buffer read lengths
834 */
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200835 len = op->data.nbytes;
836 qspi->bspi_rf_op_idx = 0;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400837
Kamal Dasu345309f2017-02-08 15:15:04 -0500838 do {
839 if (len > BSPI_READ_LENGTH)
840 rdlen = BSPI_READ_LENGTH;
841 else
842 rdlen = len;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400843
Kamal Dasu345309f2017-02-08 15:15:04 -0500844 reinit_completion(&qspi->bspi_done);
845 bcm_qspi_enable_bspi(qspi);
846 len_words = (rdlen + 3) >> 2;
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200847 qspi->bspi_rf_op = op;
848 qspi->bspi_rf_op_status = 0;
849 qspi->bspi_rf_op_len = rdlen;
Kamal Dasu345309f2017-02-08 15:15:04 -0500850 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 Dasucc20a382016-08-24 18:04:29 -0400863
Kamal Dasu345309f2017-02-08 15:15:04 -0500864 /* 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 Dasu4e3b2d22016-08-24 18:04:25 -0400872
Kamal Dasu345309f2017-02-08 15:15:04 -0500873 /* set msg return length */
Kamal Dasu345309f2017-02-08 15:15:04 -0500874 addr += rdlen;
875 len -= rdlen;
876 } while (len);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400877
878 return ret;
879}
880
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500881static 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 Packham27fb2312019-11-07 17:42:34 +1300889 if (!spi->cs_gpiod)
890 bcm_qspi_chip_select(qspi, spi->chip_select);
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500891 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 Dasu4e3b2d22016-08-24 18:04:25 -0400904 }
Rayagonda Kokatanurca105392019-08-30 09:58:45 +0530905 bcm_qspi_enable_bspi(qspi);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400906
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500907 return 0;
908}
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400909
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200910static int bcm_qspi_mspi_exec_mem_op(struct spi_device *spi,
911 const struct spi_mem_op *op)
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500912{
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200913 struct spi_master *master = spi->master;
914 struct bcm_qspi *qspi = spi_master_get_devdata(master);
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500915 struct spi_transfer t[2];
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200916 u8 cmd[6] = { };
917 int ret, i;
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500918
919 memset(cmd, 0, sizeof(cmd));
920 memset(t, 0, sizeof(t));
921
922 /* tx */
923 /* opcode is in cmd[0] */
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200924 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 Dasu81ab52f2017-01-30 16:11:16 -0500928 t[0].tx_buf = cmd;
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200929 t[0].len = op->addr.nbytes + op->dummy.nbytes + 1;
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500930 t[0].bits_per_word = spi->bits_per_word;
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200931 t[0].tx_nbits = op->cmd.buswidth;
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500932 /* lets mspi know that this is not last transfer */
933 qspi->trans_pos.mspi_last_trans = false;
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200934 ret = bcm_qspi_transfer_one(master, spi, &t[0]);
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500935
936 /* rx */
937 qspi->trans_pos.mspi_last_trans = true;
938 if (!ret) {
939 /* rx */
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200940 t[1].rx_buf = op->data.buf.in;
941 t[1].len = op->data.nbytes;
942 t[1].rx_nbits = op->data.buswidth;
Kamal Dasu81ab52f2017-01-30 16:11:16 -0500943 t[1].bits_per_word = spi->bits_per_word;
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200944 ret = bcm_qspi_transfer_one(master, spi, &t[1]);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400945 }
946
947 return ret;
948}
949
Boris Brezillonb6456052018-05-12 08:24:54 +0200950static int bcm_qspi_exec_mem_op(struct spi_mem *mem,
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200951 const struct spi_mem_op *op)
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400952{
Boris Brezillonb6456052018-05-12 08:24:54 +0200953 struct spi_device *spi = mem->spi;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400954 struct bcm_qspi *qspi = spi_master_get_devdata(spi->master);
955 int ret = 0;
956 bool mspi_read = false;
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200957 u32 addr = 0, len;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400958 u_char *buf;
959
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200960 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 Dasu4e3b2d22016-08-24 18:04:25 -0400967
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 Brezillon5f195ee2018-04-26 18:18:16 +0200988 return bcm_qspi_mspi_exec_mem_op(spi, op);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400989
Rayagonda Kokatanur79629d02019-08-06 15:37:50 +0530990 ret = bcm_qspi_bspi_set_mode(qspi, op, 0);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400991
992 if (!ret)
Boris Brezillon5f195ee2018-04-26 18:18:16 +0200993 ret = bcm_qspi_bspi_exec_mem_op(spi, op);
994
995 return ret;
996}
997
Kamal Dasufa236a72016-08-24 18:04:23 -0400998static 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
1005static 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 Dasucc20a382016-08-24 18:04:29 -04001012 struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
Kamal Dasufa236a72016-08-24 18:04:23 -04001013 /* clear interrupt */
1014 status &= ~MSPI_MSPI_STATUS_SPIF;
1015 bcm_qspi_write(qspi, MSPI, MSPI_MSPI_STATUS, status);
Kamal Dasucc20a382016-08-24 18:04:29 -04001016 if (qspi->soc_intc)
1017 soc_intc->bcm_qspi_int_ack(soc_intc, MSPI_DONE);
Kamal Dasufa236a72016-08-24 18:04:23 -04001018 complete(&qspi->mspi_done);
1019 return IRQ_HANDLED;
Kamal Dasufa236a72016-08-24 18:04:23 -04001020 }
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001021
1022 return IRQ_NONE;
1023}
1024
1025static 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 Dasucc20a382016-08-24 18:04:29 -04001029 struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
1030 u32 status = qspi_dev_id->irqp->mask;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001031
Boris Brezillon5f195ee2018-04-26 18:18:16 +02001032 if (qspi->bspi_enabled && qspi->bspi_rf_op) {
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001033 bcm_qspi_bspi_lr_data_read(qspi);
Boris Brezillon5f195ee2018-04-26 18:18:16 +02001034 if (qspi->bspi_rf_op_len == 0) {
1035 qspi->bspi_rf_op = NULL;
Kamal Dasucc20a382016-08-24 18:04:29 -04001036 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 Brezillon5f195ee2018-04-26 18:18:16 +02001044 if (qspi->bspi_rf_op_status)
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001045 bcm_qspi_bspi_lr_clear(qspi);
1046 else
1047 bcm_qspi_bspi_flush_prefetch_buffers(qspi);
1048 }
Kamal Dasucc20a382016-08-24 18:04:29 -04001049
1050 if (qspi->soc_intc)
1051 /* clear soc BSPI interrupt */
1052 soc_intc->bcm_qspi_int_ack(soc_intc, BSPI_DONE);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001053 }
1054
Kamal Dasucc20a382016-08-24 18:04:29 -04001055 status &= INTR_BSPI_LR_SESSION_DONE_MASK;
Boris Brezillon5f195ee2018-04-26 18:18:16 +02001056 if (qspi->bspi_enabled && status && qspi->bspi_rf_op_len == 0)
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001057 complete(&qspi->bspi_done);
1058
1059 return IRQ_HANDLED;
1060}
1061
1062static 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 Dasucc20a382016-08-24 18:04:29 -04001066 struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001067
1068 dev_err(&qspi->pdev->dev, "BSPI INT error\n");
Boris Brezillon5f195ee2018-04-26 18:18:16 +02001069 qspi->bspi_rf_op_status = -EIO;
Kamal Dasucc20a382016-08-24 18:04:29 -04001070 if (qspi->soc_intc)
1071 /* clear soc interrupt */
1072 soc_intc->bcm_qspi_int_ack(soc_intc, BSPI_ERR);
1073
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001074 complete(&qspi->bspi_done);
1075 return IRQ_HANDLED;
Kamal Dasufa236a72016-08-24 18:04:23 -04001076}
1077
Kamal Dasucc20a382016-08-24 18:04:29 -04001078static 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 Dasufa236a72016-08-24 18:04:23 -04001099static const struct bcm_qspi_irq qspi_irq_tab[] = {
1100 {
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001101 .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 Dasufa236a72016-08-24 18:04:23 -04001129 .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 Dasucc20a382016-08-24 18:04:29 -04001138 {
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 Dasufa236a72016-08-24 18:04:23 -04001145};
1146
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001147static 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 Dasufa236a72016-08-24 18:04:23 -04001165static 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 Dasu4e3b2d22016-08-24 18:04:25 -04001179
1180 if (has_bspi(qspi))
1181 bcm_qspi_bspi_init(qspi);
Kamal Dasufa236a72016-08-24 18:04:23 -04001182}
1183
1184static void bcm_qspi_hw_uninit(struct bcm_qspi *qspi)
1185{
1186 bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001187 if (has_bspi(qspi))
1188 bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 0);
1189
Kamal Dasufa236a72016-08-24 18:04:23 -04001190}
1191
Boris Brezillon5f195ee2018-04-26 18:18:16 +02001192static const struct spi_controller_mem_ops bcm_qspi_mem_ops = {
Boris Brezillonb6456052018-05-12 08:24:54 +02001193 .exec_op = bcm_qspi_exec_mem_op,
Boris Brezillon5f195ee2018-04-26 18:18:16 +02001194};
1195
Florian Fainelli3a01f042020-04-20 15:08:47 -04001196struct bcm_qspi_data {
1197 bool has_mspi_rev;
1198};
1199
1200static const struct bcm_qspi_data bcm_qspi_no_rev_data = {
1201 .has_mspi_rev = false,
1202};
1203
1204static const struct bcm_qspi_data bcm_qspi_rev_data = {
1205 .has_mspi_rev = true,
1206};
1207
Kamal Dasufa236a72016-08-24 18:04:23 -04001208static const struct of_device_id bcm_qspi_of_match[] = {
Florian Fainelli3a01f042020-04-20 15:08:47 -04001209 {
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 Dasufa236a72016-08-24 18:04:23 -04001225 {},
1226};
1227MODULE_DEVICE_TABLE(of, bcm_qspi_of_match);
1228
1229int bcm_qspi_probe(struct platform_device *pdev,
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001230 struct bcm_qspi_soc_intc *soc_intc)
Kamal Dasufa236a72016-08-24 18:04:23 -04001231{
Florian Fainelli3a01f042020-04-20 15:08:47 -04001232 const struct of_device_id *of_id = NULL;
1233 const struct bcm_qspi_data *data;
Kamal Dasufa236a72016-08-24 18:04:23 -04001234 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 Fainelli3a01f042020-04-20 15:08:47 -04001240 u32 rev = 0;
Kamal Dasufa236a72016-08-24 18:04:23 -04001241 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 Fainelli3a01f042020-04-20 15:08:47 -04001248 of_id = of_match_node(bcm_qspi_of_match, dev->of_node);
1249 if (!of_id)
Kamal Dasufa236a72016-08-24 18:04:23 -04001250 return -ENODEV;
1251
Florian Fainelli3a01f042020-04-20 15:08:47 -04001252 data = of_id->data;
1253
Kamal Dasufa236a72016-08-24 18:04:23 -04001254 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 Dasu81ab52f2017-01-30 16:11:16 -05001264 qspi->trans_pos.mspi_last_trans = true;
Kamal Dasufa236a72016-08-24 18:04:23 -04001265 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 Brezillon5f195ee2018-04-26 18:18:16 +02001271 master->mem_ops = &bcm_qspi_mem_ops;
Kamal Dasufa236a72016-08-24 18:04:23 -04001272 master->cleanup = bcm_qspi_cleanup;
1273 master->dev.of_node = dev->of_node;
1274 master->num_chipselect = NUM_CHIPSELECT;
Chris Packham27fb2312019-11-07 17:42:34 +13001275 master->use_gpio_descriptors = true;
Kamal Dasufa236a72016-08-24 18:04:23 -04001276
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 Jailletbc3cc752018-03-13 19:36:58 +01001291 goto qspi_resource_err;
Kamal Dasufa236a72016-08-24 18:04:23 -04001292 }
1293 } else {
Florian Fainellic0368e42017-10-11 14:59:22 -07001294 goto qspi_resource_err;
Kamal Dasufa236a72016-08-24 18:04:23 -04001295 }
1296
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001297 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 Jailletbc3cc752018-03-13 19:36:58 +01001302 goto qspi_resource_err;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001303 }
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 Dasufa236a72016-08-24 18:04:23 -04001311 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 Fainellic0368e42017-10-11 14:59:22 -07001316 goto qspi_resource_err;
Kamal Dasufa236a72016-08-24 18:04:23 -04001317 }
1318 }
1319
1320 qspi->dev_ids = kcalloc(num_irqs, sizeof(struct bcm_qspi_dev_id),
1321 GFP_KERNEL);
Wei Yongjun3bf3eb22016-09-16 13:45:17 +00001322 if (!qspi->dev_ids) {
1323 ret = -ENOMEM;
Florian Fainellic0368e42017-10-11 14:59:22 -07001324 goto qspi_resource_err;
Kamal Dasufa236a72016-08-24 18:04:23 -04001325 }
1326
1327 for (val = 0; val < num_irqs; val++) {
1328 irq = -1;
1329 name = qspi_irq_tab[val].irq_name;
Kamal Dasucc20a382016-08-24 18:04:29 -04001330 if (qspi_irq_tab[val].irq_source == SINGLE_L2) {
1331 /* get the l2 interrupts */
Rayagonda Kokatanure9aa3b82020-01-07 09:39:12 +05301332 irq = platform_get_irq_byname_optional(pdev, name);
Kamal Dasucc20a382016-08-24 18:04:29 -04001333 } 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 Dasufa236a72016-08-24 18:04:23 -04001337
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 Yongjun71b8f352016-09-16 14:00:19 +00001359 ret = -EINVAL;
Kamal Dasufa236a72016-08-24 18:04:23 -04001360 goto qspi_probe_err;
1361 }
1362
Kamal Dasucc20a382016-08-24 18:04:29 -04001363 /*
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 Dasufa236a72016-08-24 18:04:23 -04001374 qspi->clk = devm_clk_get(&pdev->dev, NULL);
1375 if (IS_ERR(qspi->clk)) {
1376 dev_warn(dev, "unable to get clock\n");
Wei Yongjun71b8f352016-09-16 14:00:19 +00001377 ret = PTR_ERR(qspi->clk);
Kamal Dasufa236a72016-08-24 18:04:23 -04001378 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 Fainelli3a01f042020-04-20 15:08:47 -04001390 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 Dasufa236a72016-08-24 18:04:23 -04001400 bcm_qspi_hw_init(qspi);
1401 init_completion(&qspi->mspi_done);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001402 init_completion(&qspi->bspi_done);
Kamal Dasufa236a72016-08-24 18:04:23 -04001403 qspi->curr_cs = -1;
1404
1405 platform_set_drvdata(pdev, qspi);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001406
1407 qspi->xfer_mode.width = -1;
1408 qspi->xfer_mode.addrlen = -1;
1409 qspi->xfer_mode.hp = -1;
1410
Kamal Dasufa236a72016-08-24 18:04:23 -04001411 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
1419qspi_reg_err:
1420 bcm_qspi_hw_uninit(qspi);
1421 clk_disable_unprepare(qspi->clk);
1422qspi_probe_err:
Kamal Dasufa236a72016-08-24 18:04:23 -04001423 kfree(qspi->dev_ids);
Florian Fainellic0368e42017-10-11 14:59:22 -07001424qspi_resource_err:
1425 spi_master_put(master);
Kamal Dasufa236a72016-08-24 18:04:23 -04001426 return ret;
1427}
1428/* probe function to be called by SoC specific platform driver probe */
1429EXPORT_SYMBOL_GPL(bcm_qspi_probe);
1430
1431int bcm_qspi_remove(struct platform_device *pdev)
1432{
1433 struct bcm_qspi *qspi = platform_get_drvdata(pdev);
1434
Kamal Dasufa236a72016-08-24 18:04:23 -04001435 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() */
1443EXPORT_SYMBOL_GPL(bcm_qspi_remove);
1444
Arnd Bergmanna0319f8b12016-09-15 17:46:53 +02001445static int __maybe_unused bcm_qspi_suspend(struct device *dev)
Kamal Dasufa236a72016-08-24 18:04:23 -04001446{
1447 struct bcm_qspi *qspi = dev_get_drvdata(dev);
1448
Kamal Dasu054e5322017-07-26 19:20:15 -04001449 /* 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 Dasufa236a72016-08-24 18:04:23 -04001454 spi_master_suspend(qspi->master);
1455 clk_disable(qspi->clk);
1456 bcm_qspi_hw_uninit(qspi);
1457
1458 return 0;
1459};
1460
Arnd Bergmanna0319f8b12016-09-15 17:46:53 +02001461static int __maybe_unused bcm_qspi_resume(struct device *dev)
Kamal Dasufa236a72016-08-24 18:04:23 -04001462{
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 Dasucc20a382016-08-24 18:04:29 -04001468 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 Dasufa236a72016-08-24 18:04:23 -04001473 ret = clk_enable(qspi->clk);
1474 if (!ret)
1475 spi_master_resume(qspi->master);
1476
1477 return ret;
1478}
Kamal Dasufa236a72016-08-24 18:04:23 -04001479
Arnd Bergmanna0319f8b12016-09-15 17:46:53 +02001480SIMPLE_DEV_PM_OPS(bcm_qspi_pm_ops, bcm_qspi_suspend, bcm_qspi_resume);
1481
Kamal Dasufa236a72016-08-24 18:04:23 -04001482/* pm_ops to be called by SoC specific platform driver */
1483EXPORT_SYMBOL_GPL(bcm_qspi_pm_ops);
1484
1485MODULE_AUTHOR("Kamal Dasu");
1486MODULE_DESCRIPTION("Broadcom QSPI driver");
1487MODULE_LICENSE("GPL v2");
1488MODULE_ALIAS("platform:" DRIVER_NAME);