blob: 09f30c227023c06ea2aa95c8a359a3cfc9989d57 [file] [log] [blame]
Rayagonda Kokatanur2ba1bd12022-02-09 14:16:13 -08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2020-2021 Broadcom
4 */
5
Rayagonda Kokatanur2ba1bd12022-02-09 14:16:13 -08006#include <dm.h>
7#include <spi.h>
8#include <spi-mem.h>
9#include <asm/io.h>
10#include <linux/delay.h>
11#include <linux/err.h>
12#include <linux/iopoll.h>
13#include <linux/log2.h>
14
15/* Delay required to change the mode of operation */
16#define BUSY_DELAY_US 1
17#define BUSY_TIMEOUT_US 200000
18#define DWORD_ALIGNED(a) (!(((ulong)(a)) & 3))
19
20/* Chip attributes */
21#define QSPI_AXI_CLK 175000000
22#define SPBR_MIN 8U
23#define SPBR_MAX 255U
24#define NUM_CDRAM 16U
25
26#define CDRAM_PCS0 2
27#define CDRAM_CONT BIT(7)
28#define CDRAM_BITS_EN BIT(6)
29#define CDRAM_QUAD_MODE BIT(8)
30#define CDRAM_RBIT_INPUT BIT(10)
31#define MSPI_SPE BIT(6)
32#define MSPI_CONT_AFTER_CMD BIT(7)
33#define MSPI_MSTR BIT(7)
34
35/* Register fields */
36#define MSPI_SPCR0_MSB_BITS_8 0x00000020
37#define BSPI_RAF_CONTROL_START_MASK 0x00000001
38#define BSPI_RAF_STATUS_SESSION_BUSY_MASK 0x00000001
39#define BSPI_RAF_STATUS_FIFO_EMPTY_MASK 0x00000002
40#define BSPI_STRAP_OVERRIDE_DATA_QUAD_SHIFT 3
41#define BSPI_STRAP_OVERRIDE_4BYTE_SHIFT 2
42#define BSPI_STRAP_OVERRIDE_DATA_DUAL_SHIFT 1
43#define BSPI_STRAP_OVERRIDE_SHIFT 0
44#define BSPI_BPC_DATA_SHIFT 0
45#define BSPI_BPC_MODE_SHIFT 8
46#define BSPI_BPC_ADDR_SHIFT 16
47#define BSPI_BPC_CMD_SHIFT 24
48#define BSPI_BPP_ADDR_SHIFT 16
49
50/* MSPI registers */
51#define MSPI_SPCR0_LSB_REG 0x000
52#define MSPI_SPCR0_MSB_REG 0x004
53#define MSPI_SPCR1_LSB_REG 0x008
54#define MSPI_SPCR1_MSB_REG 0x00c
55#define MSPI_NEWQP_REG 0x010
56#define MSPI_ENDQP_REG 0x014
57#define MSPI_SPCR2_REG 0x018
58#define MSPI_STATUS_REG 0x020
59#define MSPI_CPTQP_REG 0x024
60#define MSPI_TX_REG 0x040
61#define MSPI_RX_REG 0x0c0
62#define MSPI_CDRAM_REG 0x140
63#define MSPI_WRITE_LOCK_REG 0x180
64#define MSPI_DISABLE_FLUSH_GEN_REG 0x184
65
66/* BSPI registers */
67#define BSPI_REVISION_ID_REG 0x000
68#define BSPI_SCRATCH_REG 0x004
69#define BSPI_MAST_N_BOOT_CTRL_REG 0x008
70#define BSPI_BUSY_STATUS_REG 0x00c
71#define BSPI_INTR_STATUS_REG 0x010
72#define BSPI_B0_STATUS_REG 0x014
73#define BSPI_B0_CTRL_REG 0x018
74#define BSPI_B1_STATUS_REG 0x01c
75#define BSPI_B1_CTRL_REG 0x020
76#define BSPI_STRAP_OVERRIDE_CTRL_REG 0x024
77#define BSPI_FLEX_MODE_ENABLE_REG 0x028
78#define BSPI_BITS_PER_CYCLE_REG 0x02C
79#define BSPI_BITS_PER_PHASE_REG 0x030
80#define BSPI_CMD_AND_MODE_BYTE_REG 0x034
81#define BSPI_FLASH_UPPER_ADDR_BYTE_REG 0x038
82#define BSPI_XOR_VALUE_REG 0x03C
83#define BSPI_XOR_ENABLE_REG 0x040
84#define BSPI_PIO_MODE_ENABLE_REG 0x044
85#define BSPI_PIO_IODIR_REG 0x048
86#define BSPI_PIO_DATA_REG 0x04C
87
88/* RAF registers */
89#define BSPI_RAF_START_ADDRESS_REG 0x00
90#define BSPI_RAF_NUM_WORDS_REG 0x04
91#define BSPI_RAF_CTRL_REG 0x08
92#define BSPI_RAF_FULLNESS_REG 0x0C
93#define BSPI_RAF_WATERMARK_REG 0x10
94#define BSPI_RAF_STATUS_REG 0x14
95#define BSPI_RAF_READ_DATA_REG 0x18
96#define BSPI_RAF_WORD_CNT_REG 0x1C
97#define BSPI_RAF_CURR_ADDR_REG 0x20
98
99#define XFER_DUAL BIT(30)
100#define XFER_QUAD BIT(31)
101
102#define FLUSH_BIT BIT(0)
103#define MAST_N_BOOT_BIT BIT(0)
104#define WRITE_LOCK_BIT BIT(0)
105
106#define CEIL(m, n) (((m) + (n) - 1) / (n))
107#define UPPER_BYTE_MASK 0xFF000000
108#define SIZE_16MB 0x001000000
109
110/*
111 * struct bcmspi_priv - qspi private structure
112 *
113 * @bspi_addr: bspi read address
114 * @bspi_4byte_addr: bspi 4 byte address mode
115 * @mspi: mspi registers block address
116 * @bspi: bspi registers block address
117 * @bspi_raf: bspi raf registers block address
118 */
119struct bcmspi_priv {
120 u32 bspi_addr;
121 bool bspi_4byte_addr;
122 fdt_addr_t mspi;
123 fdt_addr_t bspi;
124 fdt_addr_t bspi_raf;
125};
126
127/* BSPI mode */
128
129static void bspi_flush_prefetch_buffers(struct bcmspi_priv *priv)
130{
131 writel(0, priv->bspi + BSPI_B0_CTRL_REG);
132 writel(0, priv->bspi + BSPI_B1_CTRL_REG);
133 writel(FLUSH_BIT, priv->bspi + BSPI_B0_CTRL_REG);
134 writel(FLUSH_BIT, priv->bspi + BSPI_B1_CTRL_REG);
135}
136
137static int bspi_enable(struct bcmspi_priv *priv)
138{
139 /* Disable write lock */
140 writel(0, priv->mspi + MSPI_WRITE_LOCK_REG);
141 /* Flush prefetch buffers */
142 bspi_flush_prefetch_buffers(priv);
143 /* Switch to BSPI */
144 writel(0, priv->bspi + BSPI_MAST_N_BOOT_CTRL_REG);
145
146 return 0;
147}
148
149static int bspi_disable(struct bcmspi_priv *priv)
150{
151 int ret;
152 uint val;
153
154 if ((readl(priv->bspi + BSPI_MAST_N_BOOT_CTRL_REG) & 1) == 0) {
155 ret = readl_poll_timeout(priv->bspi + BSPI_BUSY_STATUS_REG, val, !(val & 1),
156 BUSY_TIMEOUT_US);
157 if (ret) {
158 printf("%s: Failed to disable bspi, device busy\n", __func__);
159 return ret;
160 }
161
162 /* Switch to MSPI */
163 writel(MAST_N_BOOT_BIT, priv->bspi + BSPI_MAST_N_BOOT_CTRL_REG);
164 udelay(BUSY_DELAY_US);
165
166 val = readl(priv->bspi + BSPI_MAST_N_BOOT_CTRL_REG);
167 if (!(val & 1)) {
168 printf("%s: Failed to enable mspi\n", __func__);
169 return -EBUSY;
170 }
171 }
172
173 /* Enable write lock */
174 writel(WRITE_LOCK_BIT, priv->mspi + MSPI_WRITE_LOCK_REG);
175
176 return 0;
177}
178
179static int bspi_read_via_raf(struct bcmspi_priv *priv, u8 *rx, uint bytes)
180{
181 u32 status;
182 uint words;
183 int aligned;
184 int ret;
185
186 /*
187 * Flush data from the previous session (unlikely)
188 * Read outstanding bits in the poll condition to empty FIFO
189 */
190 ret = readl_poll_timeout(priv->bspi_raf + BSPI_RAF_STATUS_REG,
191 status,
192 (!readl(priv->bspi_raf + BSPI_RAF_READ_DATA_REG) &&
193 status & BSPI_RAF_STATUS_FIFO_EMPTY_MASK) &&
194 !(status & BSPI_RAF_STATUS_SESSION_BUSY_MASK),
195 BUSY_TIMEOUT_US);
196 if (ret) {
197 printf("%s: Failed to flush fifo\n", __func__);
198 return ret;
199 }
200
201 /* Transfer is in words */
202 words = CEIL(bytes, 4);
203
204 /* Setup hardware */
205 if (priv->bspi_4byte_addr) {
206 u32 val = priv->bspi_addr & UPPER_BYTE_MASK;
207
208 if (val != readl(priv->bspi + BSPI_FLASH_UPPER_ADDR_BYTE_REG)) {
209 writel(val, priv->bspi + BSPI_FLASH_UPPER_ADDR_BYTE_REG);
210 bspi_flush_prefetch_buffers(priv);
211 }
212 }
213
214 writel(priv->bspi_addr & ~UPPER_BYTE_MASK, priv->bspi_raf + BSPI_RAF_START_ADDRESS_REG);
215 writel(words, priv->bspi_raf + BSPI_RAF_NUM_WORDS_REG);
216 writel(0, priv->bspi_raf + BSPI_RAF_WATERMARK_REG);
217
218 /* Start reading */
219 writel(BSPI_RAF_CONTROL_START_MASK, priv->bspi_raf + BSPI_RAF_CTRL_REG);
220 aligned = DWORD_ALIGNED(rx);
221 while (bytes) {
222 status = readl(priv->bspi_raf + BSPI_RAF_STATUS_REG);
223 if (!(status & BSPI_RAF_STATUS_FIFO_EMPTY_MASK)) {
224 /* RAF is LE only, convert data to host endianness */
225 u32 data = le32_to_cpu(readl(priv->bspi_raf + BSPI_RAF_READ_DATA_REG));
226
227 /* Check if we can use the whole word */
228 if (aligned && bytes >= 4) {
229 *(u32 *)rx = data;
230 rx += 4;
231 bytes -= 4;
232 } else {
233 uint chunk = min(bytes, 4U);
234
235 /* Read out bytes one by one */
236 while (chunk) {
237 *rx++ = (u8)data;
238 data >>= 8;
239 chunk--;
240 bytes--;
241 }
242 }
243
244 continue;
245 }
246 if (!(status & BSPI_RAF_STATUS_SESSION_BUSY_MASK)) {
247 /* FIFO is empty and the session is done */
248 break;
249 }
250 }
251
252 return 0;
253}
254
255static int bspi_read(struct bcmspi_priv *priv, u8 *rx, uint bytes)
256{
257 int ret;
258
259 /* Transfer data */
260 while (bytes > 0) {
261 /* Special handing since RAF cannot go across 16MB boundary */
262 uint trans = bytes;
263 /* Divide into multiple transfers if it goes across the 16MB boundary */
264 if (priv->bspi_4byte_addr && (priv->bspi_addr >> 24) !=
265 ((priv->bspi_addr + bytes) >> 24))
266 trans = SIZE_16MB - (priv->bspi_addr & ~UPPER_BYTE_MASK);
267
268 ret = bspi_read_via_raf(priv, rx, trans);
269 if (ret)
270 return ret;
271
272 priv->bspi_addr += trans;
273 rx += trans;
274 bytes -= trans;
275 }
276
277 bspi_flush_prefetch_buffers(priv);
278 return 0;
279}
280
281static void bspi_set_flex_mode(struct bcmspi_priv *priv, const struct spi_mem_op *op)
282{
283 int bpp = (op->dummy.nbytes * 8) / op->dummy.buswidth;
284 int cmd = op->cmd.opcode;
285 int bpc = ilog2(op->data.buswidth) << BSPI_BPC_DATA_SHIFT |
286 ilog2(op->addr.buswidth) << BSPI_BPC_ADDR_SHIFT |
287 ilog2(op->cmd.buswidth) << BSPI_BPC_CMD_SHIFT;
288 int so = BIT(BSPI_STRAP_OVERRIDE_SHIFT) |
289 (op->data.buswidth > 1) << BSPI_STRAP_OVERRIDE_DATA_DUAL_SHIFT |
290 (op->addr.nbytes > 3) << BSPI_STRAP_OVERRIDE_4BYTE_SHIFT |
291 (op->data.buswidth > 3) << BSPI_STRAP_OVERRIDE_DATA_QUAD_SHIFT;
292
293 /* Disable flex mode first */
294 writel(0, priv->bspi + BSPI_FLEX_MODE_ENABLE_REG);
295
296 /* Configure single, dual or quad mode */
297 writel(bpc, priv->bspi + BSPI_BITS_PER_CYCLE_REG);
298
299 /* Opcode */
300 writel(cmd, priv->bspi + BSPI_CMD_AND_MODE_BYTE_REG);
301
302 /* Count of dummy cycles */
303 writel(bpp, priv->bspi + BSPI_BITS_PER_PHASE_REG);
304
305 /* Enable 4-byte address */
306 if (priv->bspi_4byte_addr) {
307 setbits_le32(priv->bspi + BSPI_BITS_PER_PHASE_REG, BIT(BSPI_BPP_ADDR_SHIFT));
308 } else {
309 clrbits_le32(priv->bspi + BSPI_BITS_PER_PHASE_REG, BIT(BSPI_BPP_ADDR_SHIFT));
310 writel(0, priv->bspi + BSPI_FLASH_UPPER_ADDR_BYTE_REG);
311 }
312
313 /* Enable flex mode to take effect */
314 writel(1, priv->bspi + BSPI_FLEX_MODE_ENABLE_REG);
315
316 /* Flush prefetch buffers since 32MB window BSPI could be used */
317 bspi_flush_prefetch_buffers(priv);
318
319 /* Override the strap settings */
320 writel(so, priv->bspi + BSPI_STRAP_OVERRIDE_CTRL_REG);
321}
322
323static int bspi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
324{
325 struct udevice *bus = dev_get_parent(slave->dev);
326 struct bcmspi_priv *priv = dev_get_priv(bus);
327 int ret = -ENOTSUPP;
328
329 /* BSPI read */
330 if (op->data.dir == SPI_MEM_DATA_IN &&
331 op->data.nbytes && op->addr.nbytes) {
332 priv->bspi_4byte_addr = (op->addr.nbytes > 3);
333 priv->bspi_addr = op->addr.val;
334 bspi_set_flex_mode(priv, op);
335 ret = bspi_read(priv, op->data.buf.in, op->data.nbytes);
336 }
337
338 return ret;
339}
340
341static const struct spi_controller_mem_ops bspi_mem_ops = {
342 .exec_op = bspi_exec_op,
343};
344
345/* MSPI mode */
346
347static int mspi_exec(struct bcmspi_priv *priv, uint bytes, const u8 *tx, u8 *rx, ulong flags)
348{
349 u32 cdr = CDRAM_PCS0 | CDRAM_CONT;
350 bool use_16bits = !(bytes & 1);
351
352 if (flags & XFER_QUAD) {
353 cdr |= CDRAM_QUAD_MODE;
354
355 if (!tx)
356 cdr |= CDRAM_RBIT_INPUT;
357 }
358
359 while (bytes) {
360 uint chunk;
361 uint queues;
362 uint i;
363 uint val;
364 int ret;
365
366 if (use_16bits) {
367 chunk = min(bytes, NUM_CDRAM * 2);
368 queues = (chunk + 1) / 2;
369 bytes -= chunk;
370
371 /* Fill CDRAMs */
372 for (i = 0; i < queues; i++)
373 writel(cdr | CDRAM_BITS_EN, priv->mspi + MSPI_CDRAM_REG + 4 * i);
374
375 /* Fill TXRAMs */
376 for (i = 0; i < chunk; i++)
377 writel(tx ? tx[i] : 0xff, priv->mspi + MSPI_TX_REG + 4 * i);
378 } else {
379 /* Determine how many bytes to process this time */
380 chunk = min(bytes, NUM_CDRAM);
381 queues = chunk;
382 bytes -= chunk;
383
384 /* Fill CDRAMs and TXRAMS */
385 for (i = 0; i < chunk; i++) {
386 writel(cdr, priv->mspi + MSPI_CDRAM_REG + 4 * i);
387 writel(tx ? tx[i] : 0xff, priv->mspi + MSPI_TX_REG + 8 * i);
388 }
389 }
390
391 /* Setup queue pointers */
392 writel(0, priv->mspi + MSPI_NEWQP_REG);
393 writel(queues - 1, priv->mspi + MSPI_ENDQP_REG);
394
395 /* Deassert CS if requested and it's the last transfer */
396 if (bytes == 0 && (flags & SPI_XFER_END))
397 clrbits_le32(priv->mspi + MSPI_CDRAM_REG + ((queues - 1) << 2), CDRAM_CONT);
398
399 /* Kick off */
400 writel(0, priv->mspi + MSPI_STATUS_REG);
401 if (bytes == 0 && (flags & SPI_XFER_END))
402 writel(MSPI_SPE, priv->mspi + MSPI_SPCR2_REG);
403 else
404 writel(MSPI_SPE | MSPI_CONT_AFTER_CMD,
405 priv->mspi + MSPI_SPCR2_REG);
406
407 ret = readl_poll_timeout(priv->mspi + MSPI_STATUS_REG, val, (val & 1),
408 BUSY_TIMEOUT_US);
409 if (ret) {
410 printf("%s: Failed to disable bspi, device busy\n", __func__);
411 return ret;
412 }
413
414 /* Read data out */
415 if (rx) {
416 if (use_16bits) {
417 for (i = 0; i < chunk; i++)
418 rx[i] = readl(priv->mspi + MSPI_RX_REG + 4 * i) & 0xff;
419 } else {
420 for (i = 0; i < chunk; i++)
421 rx[i] = readl(priv->mspi + MSPI_RX_REG + 8 * i + 4) & 0xff;
422 }
423 }
424
425 /* Advance pointers */
426 if (tx)
427 tx += chunk;
428 if (rx)
429 rx += chunk;
430 }
431
432 return 0;
433}
434
435static int mspi_xfer(struct udevice *dev, uint bitlen, const void *dout, void *din, ulong flags)
436{
437 struct udevice *bus = dev_get_parent(dev);
438 struct bcmspi_priv *priv = dev_get_priv(bus);
439 uint bytes;
440 int ret = 0;
441
442 /* we can only transfer multiples of 8 bits */
443 if (bitlen % 8)
444 return -EPROTONOSUPPORT;
445
446 bytes = bitlen / 8;
447
448 if (flags & SPI_XFER_BEGIN) {
449 /* Switch to MSPI */
450 ret = bspi_disable(priv);
451 if (ret)
452 return ret;
453 }
454
455 /* MSPI: Transfer */
456 if (bytes)
457 ret = mspi_exec(priv, bytes, dout, din, flags);
458
459 if (flags & SPI_XFER_END) {
460 /* Switch back to BSPI */
461 ret = bspi_enable(priv);
462 if (ret)
463 return ret;
464 }
465
466 return ret;
467}
468
469/* iProc interface */
470
471static int iproc_qspi_set_speed(struct udevice *bus, uint speed)
472{
473 struct bcmspi_priv *priv = dev_get_priv(bus);
474 uint spbr;
475
476 /* MSPI: SCK configuration */
477 spbr = (QSPI_AXI_CLK - 1) / (2 * speed) + 1;
478 writel(max(min(spbr, SPBR_MAX), SPBR_MIN), priv->mspi + MSPI_SPCR0_LSB_REG);
479
480 return 0;
481}
482
483static int iproc_qspi_set_mode(struct udevice *bus, uint mode)
484{
485 struct bcmspi_priv *priv = dev_get_priv(bus);
486
487 /* MSPI: set master bit and mode */
488 writel(MSPI_MSTR /* Master */ | (mode & 3), priv->mspi + MSPI_SPCR0_MSB_REG);
489
490 return 0;
491}
492
493static int iproc_qspi_claim_bus(struct udevice *dev)
494{
495 /* Nothing to do */
496 return 0;
497}
498
499static int iproc_qspi_release_bus(struct udevice *dev)
500{
501 struct udevice *bus = dev_get_parent(dev);
502 struct bcmspi_priv *priv = dev_get_priv(bus);
503
504 /* Make sure no operation is in progress */
505 writel(0, priv->mspi + MSPI_SPCR2_REG);
506 udelay(BUSY_DELAY_US);
507
508 return 0;
509}
510
511static int iproc_qspi_of_to_plat(struct udevice *bus)
512{
513 struct bcmspi_priv *priv = dev_get_priv(bus);
514
515 priv->bspi = dev_read_addr_name(bus, "bspi");
516 if (IS_ERR((void *)priv->bspi)) {
517 printf("%s: Failed to get bspi base address\n", __func__);
518 return PTR_ERR((void *)priv->bspi);
519 }
520
521 priv->bspi_raf = dev_read_addr_name(bus, "bspi_raf");
522 if (IS_ERR((void *)priv->bspi_raf)) {
523 printf("%s: Failed to get bspi_raf base address\n", __func__);
524 return PTR_ERR((void *)priv->bspi_raf);
525 }
526
527 priv->mspi = dev_read_addr_name(bus, "mspi");
528 if (IS_ERR((void *)priv->mspi)) {
529 printf("%s: Failed to get mspi base address\n", __func__);
530 return PTR_ERR((void *)priv->mspi);
531 }
532
533 return 0;
534}
535
536static int iproc_qspi_probe(struct udevice *bus)
537{
538 struct bcmspi_priv *priv = dev_get_priv(bus);
539
540 /* configure mspi */
541 writel(0, priv->mspi + MSPI_SPCR1_LSB_REG);
542 writel(0, priv->mspi + MSPI_SPCR1_MSB_REG);
543 writel(0, priv->mspi + MSPI_NEWQP_REG);
544 writel(0, priv->mspi + MSPI_ENDQP_REG);
545 writel(0, priv->mspi + MSPI_SPCR2_REG);
546
547 /* configure bspi */
548 bspi_enable(priv);
549
550 return 0;
551}
552
553static const struct dm_spi_ops iproc_qspi_ops = {
554 .claim_bus = iproc_qspi_claim_bus,
555 .release_bus = iproc_qspi_release_bus,
556 .xfer = mspi_xfer,
557 .set_speed = iproc_qspi_set_speed,
558 .set_mode = iproc_qspi_set_mode,
559 .mem_ops = &bspi_mem_ops,
560};
561
562static const struct udevice_id iproc_qspi_ids[] = {
563 { .compatible = "brcm,iproc-qspi" },
564 { }
565};
566
567U_BOOT_DRIVER(iproc_qspi) = {
568 .name = "iproc_qspi",
569 .id = UCLASS_SPI,
570 .of_match = iproc_qspi_ids,
571 .ops = &iproc_qspi_ops,
572 .of_to_plat = iproc_qspi_of_to_plat,
573 .priv_auto = sizeof(struct bcmspi_priv),
574 .probe = iproc_qspi_probe,
575};