1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2024 AIROHA Inc
4 * Author: Lorenzo Bianconi <lorenzo@kernel.org>
5 * Author: Ray Liu <ray.liu@airoha.com>
8 #include <linux/bitfield.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/errno.h>
14 #include <linux/limits.h>
15 #include <linux/math.h>
16 #include <linux/minmax.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/property.h>
21 #include <linux/regmap.h>
22 #include <linux/sizes.h>
23 #include <linux/spi/spi.h>
24 #include <linux/spi/spi-mem.h>
25 #include <linux/types.h>
26 #include <linux/unaligned.h>
29 #define REG_SPI_CTRL_BASE 0x1FA10000
31 #define REG_SPI_CTRL_READ_MODE 0x0000
32 #define REG_SPI_CTRL_READ_IDLE_EN 0x0004
33 #define REG_SPI_CTRL_SIDLY 0x0008
34 #define REG_SPI_CTRL_CSHEXT 0x000c
35 #define REG_SPI_CTRL_CSLEXT 0x0010
37 #define REG_SPI_CTRL_MTX_MODE_TOG 0x0014
38 #define SPI_CTRL_MTX_MODE_TOG GENMASK(3, 0)
40 #define REG_SPI_CTRL_RDCTL_FSM 0x0018
41 #define SPI_CTRL_RDCTL_FSM GENMASK(3, 0)
43 #define REG_SPI_CTRL_MACMUX_SEL 0x001c
45 #define REG_SPI_CTRL_MANUAL_EN 0x0020
46 #define SPI_CTRL_MANUAL_EN BIT(0)
48 #define REG_SPI_CTRL_OPFIFO_EMPTY 0x0024
49 #define SPI_CTRL_OPFIFO_EMPTY BIT(0)
51 #define REG_SPI_CTRL_OPFIFO_WDATA 0x0028
52 #define SPI_CTRL_OPFIFO_LEN GENMASK(8, 0)
53 #define SPI_CTRL_OPFIFO_OP GENMASK(13, 9)
55 #define REG_SPI_CTRL_OPFIFO_FULL 0x002c
56 #define SPI_CTRL_OPFIFO_FULL BIT(0)
58 #define REG_SPI_CTRL_OPFIFO_WR 0x0030
59 #define SPI_CTRL_OPFIFO_WR BIT(0)
61 #define REG_SPI_CTRL_DFIFO_FULL 0x0034
62 #define SPI_CTRL_DFIFO_FULL BIT(0)
64 #define REG_SPI_CTRL_DFIFO_WDATA 0x0038
65 #define SPI_CTRL_DFIFO_WDATA GENMASK(7, 0)
67 #define REG_SPI_CTRL_DFIFO_EMPTY 0x003c
68 #define SPI_CTRL_DFIFO_EMPTY BIT(0)
70 #define REG_SPI_CTRL_DFIFO_RD 0x0040
71 #define SPI_CTRL_DFIFO_RD BIT(0)
73 #define REG_SPI_CTRL_DFIFO_RDATA 0x0044
74 #define SPI_CTRL_DFIFO_RDATA GENMASK(7, 0)
76 #define REG_SPI_CTRL_DUMMY 0x0080
77 #define SPI_CTRL_CTRL_DUMMY GENMASK(3, 0)
79 #define REG_SPI_CTRL_PROBE_SEL 0x0088
80 #define REG_SPI_CTRL_INTERRUPT 0x0090
81 #define REG_SPI_CTRL_INTERRUPT_EN 0x0094
82 #define REG_SPI_CTRL_SI_CK_SEL 0x009c
83 #define REG_SPI_CTRL_SW_CFGNANDADDR_VAL 0x010c
84 #define REG_SPI_CTRL_SW_CFGNANDADDR_EN 0x0110
85 #define REG_SPI_CTRL_SFC_STRAP 0x0114
87 #define REG_SPI_CTRL_NFI2SPI_EN 0x0130
88 #define SPI_CTRL_NFI2SPI_EN BIT(0)
91 #define REG_SPI_NFI_CNFG 0x0000
92 #define SPI_NFI_DMA_MODE BIT(0)
93 #define SPI_NFI_READ_MODE BIT(1)
94 #define SPI_NFI_DMA_BURST_EN BIT(2)
95 #define SPI_NFI_HW_ECC_EN BIT(8)
96 #define SPI_NFI_AUTO_FDM_EN BIT(9)
97 #define SPI_NFI_OPMODE GENMASK(14, 12)
99 #define REG_SPI_NFI_PAGEFMT 0x0004
100 #define SPI_NFI_PAGE_SIZE GENMASK(1, 0)
101 #define SPI_NFI_SPARE_SIZE GENMASK(5, 4)
103 #define REG_SPI_NFI_CON 0x0008
104 #define SPI_NFI_FIFO_FLUSH BIT(0)
105 #define SPI_NFI_RST BIT(1)
106 #define SPI_NFI_RD_TRIG BIT(8)
107 #define SPI_NFI_WR_TRIG BIT(9)
108 #define SPI_NFI_SEC_NUM GENMASK(15, 12)
110 #define REG_SPI_NFI_INTR_EN 0x0010
111 #define SPI_NFI_RD_DONE_EN BIT(0)
112 #define SPI_NFI_WR_DONE_EN BIT(1)
113 #define SPI_NFI_RST_DONE_EN BIT(2)
114 #define SPI_NFI_ERASE_DONE_EN BIT(3)
115 #define SPI_NFI_BUSY_RETURN_EN BIT(4)
116 #define SPI_NFI_ACCESS_LOCK_EN BIT(5)
117 #define SPI_NFI_AHB_DONE_EN BIT(6)
118 #define SPI_NFI_ALL_IRQ_EN \
119 (SPI_NFI_RD_DONE_EN | SPI_NFI_WR_DONE_EN | \
120 SPI_NFI_RST_DONE_EN | SPI_NFI_ERASE_DONE_EN | \
121 SPI_NFI_BUSY_RETURN_EN | SPI_NFI_ACCESS_LOCK_EN | \
124 #define REG_SPI_NFI_INTR 0x0014
125 #define SPI_NFI_AHB_DONE BIT(6)
127 #define REG_SPI_NFI_CMD 0x0020
129 #define REG_SPI_NFI_ADDR_NOB 0x0030
130 #define SPI_NFI_ROW_ADDR_NOB GENMASK(6, 4)
132 #define REG_SPI_NFI_STA 0x0060
133 #define REG_SPI_NFI_FIFOSTA 0x0064
134 #define REG_SPI_NFI_STRADDR 0x0080
135 #define REG_SPI_NFI_FDM0L 0x00a0
136 #define REG_SPI_NFI_FDM0M 0x00a4
137 #define REG_SPI_NFI_FDM7L 0x00d8
138 #define REG_SPI_NFI_FDM7M 0x00dc
139 #define REG_SPI_NFI_FIFODATA0 0x0190
140 #define REG_SPI_NFI_FIFODATA1 0x0194
141 #define REG_SPI_NFI_FIFODATA2 0x0198
142 #define REG_SPI_NFI_FIFODATA3 0x019c
143 #define REG_SPI_NFI_MASTERSTA 0x0224
145 #define REG_SPI_NFI_SECCUS_SIZE 0x022c
146 #define SPI_NFI_CUS_SEC_SIZE GENMASK(12, 0)
147 #define SPI_NFI_CUS_SEC_SIZE_EN BIT(16)
149 #define REG_SPI_NFI_RD_CTL2 0x0510
150 #define REG_SPI_NFI_RD_CTL3 0x0514
152 #define REG_SPI_NFI_PG_CTL1 0x0524
153 #define SPI_NFI_PG_LOAD_CMD GENMASK(15, 8)
155 #define REG_SPI_NFI_PG_CTL2 0x0528
156 #define REG_SPI_NFI_NOR_PROG_ADDR 0x052c
157 #define REG_SPI_NFI_NOR_RD_ADDR 0x0534
159 #define REG_SPI_NFI_SNF_MISC_CTL 0x0538
160 #define SPI_NFI_DATA_READ_WR_MODE GENMASK(18, 16)
162 #define REG_SPI_NFI_SNF_MISC_CTL2 0x053c
163 #define SPI_NFI_READ_DATA_BYTE_NUM GENMASK(12, 0)
164 #define SPI_NFI_PROG_LOAD_BYTE_NUM GENMASK(28, 16)
166 #define REG_SPI_NFI_SNF_STA_CTL1 0x0550
167 #define SPI_NFI_READ_FROM_CACHE_DONE BIT(25)
168 #define SPI_NFI_LOAD_TO_CACHE_DONE BIT(26)
170 #define REG_SPI_NFI_SNF_STA_CTL2 0x0554
172 #define REG_SPI_NFI_SNF_NFI_CNFG 0x055c
173 #define SPI_NFI_SPI_MODE BIT(0)
175 /* SPI NAND Protocol OP */
176 #define SPI_NAND_OP_GET_FEATURE 0x0f
177 #define SPI_NAND_OP_SET_FEATURE 0x1f
178 #define SPI_NAND_OP_PAGE_READ 0x13
179 #define SPI_NAND_OP_READ_FROM_CACHE_SINGLE 0x03
180 #define SPI_NAND_OP_READ_FROM_CACHE_SINGLE_FAST 0x0b
181 #define SPI_NAND_OP_READ_FROM_CACHE_DUAL 0x3b
182 #define SPI_NAND_OP_READ_FROM_CACHE_QUAD 0x6b
183 #define SPI_NAND_OP_WRITE_ENABLE 0x06
184 #define SPI_NAND_OP_WRITE_DISABLE 0x04
185 #define SPI_NAND_OP_PROGRAM_LOAD_SINGLE 0x02
186 #define SPI_NAND_OP_PROGRAM_LOAD_QUAD 0x32
187 #define SPI_NAND_OP_PROGRAM_LOAD_RAMDOM_SINGLE 0x84
188 #define SPI_NAND_OP_PROGRAM_LOAD_RAMDON_QUAD 0x34
189 #define SPI_NAND_OP_PROGRAM_EXECUTE 0x10
190 #define SPI_NAND_OP_READ_ID 0x9f
191 #define SPI_NAND_OP_BLOCK_ERASE 0xd8
192 #define SPI_NAND_OP_RESET 0xff
193 #define SPI_NAND_OP_DIE_SELECT 0xc2
195 #define SPI_NAND_CACHE_SIZE (SZ_4K + SZ_256)
196 #define SPI_MAX_TRANSFER_SIZE 511
198 enum airoha_snand_mode
{
204 enum airoha_snand_cs
{
209 struct airoha_snand_ctrl
{
211 struct regmap
*regmap_ctrl
;
212 struct regmap
*regmap_nfi
;
223 static int airoha_snand_set_fifo_op(struct airoha_snand_ctrl
*as_ctrl
,
224 u8 op_cmd
, int op_len
)
229 err
= regmap_write(as_ctrl
->regmap_ctrl
, REG_SPI_CTRL_OPFIFO_WDATA
,
230 FIELD_PREP(SPI_CTRL_OPFIFO_LEN
, op_len
) |
231 FIELD_PREP(SPI_CTRL_OPFIFO_OP
, op_cmd
));
235 err
= regmap_read_poll_timeout(as_ctrl
->regmap_ctrl
,
236 REG_SPI_CTRL_OPFIFO_FULL
,
237 val
, !(val
& SPI_CTRL_OPFIFO_FULL
),
238 0, 250 * USEC_PER_MSEC
);
242 err
= regmap_write(as_ctrl
->regmap_ctrl
, REG_SPI_CTRL_OPFIFO_WR
,
247 return regmap_read_poll_timeout(as_ctrl
->regmap_ctrl
,
248 REG_SPI_CTRL_OPFIFO_EMPTY
,
249 val
, (val
& SPI_CTRL_OPFIFO_EMPTY
),
250 0, 250 * USEC_PER_MSEC
);
253 static int airoha_snand_set_cs(struct airoha_snand_ctrl
*as_ctrl
, u8 cs
)
255 return airoha_snand_set_fifo_op(as_ctrl
, cs
, sizeof(cs
));
258 static int airoha_snand_write_data_to_fifo(struct airoha_snand_ctrl
*as_ctrl
,
259 const u8
*data
, int len
)
263 for (i
= 0; i
< len
; i
++) {
267 /* 1. Wait until dfifo is not full */
268 err
= regmap_read_poll_timeout(as_ctrl
->regmap_ctrl
,
269 REG_SPI_CTRL_DFIFO_FULL
, val
,
270 !(val
& SPI_CTRL_DFIFO_FULL
),
271 0, 250 * USEC_PER_MSEC
);
275 /* 2. Write data to register DFIFO_WDATA */
276 err
= regmap_write(as_ctrl
->regmap_ctrl
,
277 REG_SPI_CTRL_DFIFO_WDATA
,
278 FIELD_PREP(SPI_CTRL_DFIFO_WDATA
, data
[i
]));
282 /* 3. Wait until dfifo is not full */
283 err
= regmap_read_poll_timeout(as_ctrl
->regmap_ctrl
,
284 REG_SPI_CTRL_DFIFO_FULL
, val
,
285 !(val
& SPI_CTRL_DFIFO_FULL
),
286 0, 250 * USEC_PER_MSEC
);
294 static int airoha_snand_read_data_from_fifo(struct airoha_snand_ctrl
*as_ctrl
,
299 for (i
= 0; i
< len
; i
++) {
303 /* 1. wait until dfifo is not empty */
304 err
= regmap_read_poll_timeout(as_ctrl
->regmap_ctrl
,
305 REG_SPI_CTRL_DFIFO_EMPTY
, val
,
306 !(val
& SPI_CTRL_DFIFO_EMPTY
),
307 0, 250 * USEC_PER_MSEC
);
311 /* 2. read from dfifo to register DFIFO_RDATA */
312 err
= regmap_read(as_ctrl
->regmap_ctrl
,
313 REG_SPI_CTRL_DFIFO_RDATA
, &val
);
317 ptr
[i
] = FIELD_GET(SPI_CTRL_DFIFO_RDATA
, val
);
318 /* 3. enable register DFIFO_RD to read next byte */
319 err
= regmap_write(as_ctrl
->regmap_ctrl
,
320 REG_SPI_CTRL_DFIFO_RD
, SPI_CTRL_DFIFO_RD
);
328 static int airoha_snand_set_mode(struct airoha_snand_ctrl
*as_ctrl
,
329 enum airoha_snand_mode mode
)
334 case SPI_MODE_MANUAL
: {
337 err
= regmap_write(as_ctrl
->regmap_ctrl
,
338 REG_SPI_CTRL_NFI2SPI_EN
, 0);
342 err
= regmap_write(as_ctrl
->regmap_ctrl
,
343 REG_SPI_CTRL_READ_IDLE_EN
, 0);
347 err
= regmap_read_poll_timeout(as_ctrl
->regmap_ctrl
,
348 REG_SPI_CTRL_RDCTL_FSM
, val
,
349 !(val
& SPI_CTRL_RDCTL_FSM
),
350 0, 250 * USEC_PER_MSEC
);
354 err
= regmap_write(as_ctrl
->regmap_ctrl
,
355 REG_SPI_CTRL_MTX_MODE_TOG
, 9);
359 err
= regmap_write(as_ctrl
->regmap_ctrl
,
360 REG_SPI_CTRL_MANUAL_EN
, SPI_CTRL_MANUAL_EN
);
366 err
= regmap_write(as_ctrl
->regmap_ctrl
,
367 REG_SPI_CTRL_NFI2SPI_EN
,
372 err
= regmap_write(as_ctrl
->regmap_ctrl
,
373 REG_SPI_CTRL_MTX_MODE_TOG
, 0x0);
377 err
= regmap_write(as_ctrl
->regmap_ctrl
,
378 REG_SPI_CTRL_MANUAL_EN
, 0x0);
387 return regmap_write(as_ctrl
->regmap_ctrl
, REG_SPI_CTRL_DUMMY
, 0);
390 static int airoha_snand_write_data(struct airoha_snand_ctrl
*as_ctrl
, u8 cmd
,
391 const u8
*data
, int len
)
395 for (i
= 0; i
< len
; i
+= data_len
) {
398 data_len
= min(len
- i
, SPI_MAX_TRANSFER_SIZE
);
399 err
= airoha_snand_set_fifo_op(as_ctrl
, cmd
, data_len
);
403 err
= airoha_snand_write_data_to_fifo(as_ctrl
, &data
[i
],
412 static int airoha_snand_read_data(struct airoha_snand_ctrl
*as_ctrl
, u8
*data
,
417 for (i
= 0; i
< len
; i
+= data_len
) {
420 data_len
= min(len
- i
, SPI_MAX_TRANSFER_SIZE
);
421 err
= airoha_snand_set_fifo_op(as_ctrl
, 0xc, data_len
);
425 err
= airoha_snand_read_data_from_fifo(as_ctrl
, &data
[i
],
434 static int airoha_snand_nfi_init(struct airoha_snand_ctrl
*as_ctrl
)
438 /* switch to SNFI mode */
439 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_SNF_NFI_CNFG
,
445 return regmap_update_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_INTR_EN
,
446 SPI_NFI_ALL_IRQ_EN
, SPI_NFI_AHB_DONE_EN
);
449 static int airoha_snand_nfi_config(struct airoha_snand_ctrl
*as_ctrl
)
454 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CON
,
455 SPI_NFI_FIFO_FLUSH
| SPI_NFI_RST
);
460 err
= regmap_clear_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CNFG
,
461 SPI_NFI_AUTO_FDM_EN
);
466 err
= regmap_clear_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CNFG
,
472 err
= regmap_set_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CNFG
,
473 SPI_NFI_DMA_BURST_EN
);
478 switch (as_ctrl
->nfi_cfg
.spare_size
) {
480 val
= FIELD_PREP(SPI_NFI_SPARE_SIZE
, 0x1);
483 val
= FIELD_PREP(SPI_NFI_SPARE_SIZE
, 0x2);
486 val
= FIELD_PREP(SPI_NFI_SPARE_SIZE
, 0x3);
489 val
= FIELD_PREP(SPI_NFI_SPARE_SIZE
, 0x0);
493 err
= regmap_update_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_PAGEFMT
,
494 SPI_NFI_SPARE_SIZE
, val
);
498 switch (as_ctrl
->nfi_cfg
.page_size
) {
500 val
= FIELD_PREP(SPI_NFI_PAGE_SIZE
, 0x1);
503 val
= FIELD_PREP(SPI_NFI_PAGE_SIZE
, 0x2);
506 val
= FIELD_PREP(SPI_NFI_PAGE_SIZE
, 0x0);
510 err
= regmap_update_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_PAGEFMT
,
511 SPI_NFI_PAGE_SIZE
, val
);
516 val
= FIELD_PREP(SPI_NFI_SEC_NUM
, as_ctrl
->nfi_cfg
.sec_num
);
517 err
= regmap_update_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CON
,
518 SPI_NFI_SEC_NUM
, val
);
522 /* enable cust sec size */
523 err
= regmap_set_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_SECCUS_SIZE
,
524 SPI_NFI_CUS_SEC_SIZE_EN
);
528 /* set cust sec size */
529 val
= FIELD_PREP(SPI_NFI_CUS_SEC_SIZE
, as_ctrl
->nfi_cfg
.sec_size
);
530 return regmap_update_bits(as_ctrl
->regmap_nfi
,
531 REG_SPI_NFI_SECCUS_SIZE
,
532 SPI_NFI_CUS_SEC_SIZE
, val
);
535 static bool airoha_snand_is_page_ops(const struct spi_mem_op
*op
)
537 if (op
->addr
.nbytes
!= 2)
540 if (op
->addr
.buswidth
!= 1 && op
->addr
.buswidth
!= 2 &&
541 op
->addr
.buswidth
!= 4)
544 switch (op
->data
.dir
) {
545 case SPI_MEM_DATA_IN
:
546 if (op
->dummy
.nbytes
* BITS_PER_BYTE
/ op
->dummy
.buswidth
> 0xf)
549 /* quad in / quad out */
550 if (op
->addr
.buswidth
== 4)
551 return op
->data
.buswidth
== 4;
553 if (op
->addr
.buswidth
== 2)
554 return op
->data
.buswidth
== 2;
557 return op
->data
.buswidth
== 4 || op
->data
.buswidth
== 2 ||
558 op
->data
.buswidth
== 1;
559 case SPI_MEM_DATA_OUT
:
560 return !op
->dummy
.nbytes
&& op
->addr
.buswidth
== 1 &&
561 (op
->data
.buswidth
== 4 || op
->data
.buswidth
== 1);
567 static int airoha_snand_adjust_op_size(struct spi_mem
*mem
,
568 struct spi_mem_op
*op
)
572 if (airoha_snand_is_page_ops(op
)) {
573 struct airoha_snand_ctrl
*as_ctrl
;
575 as_ctrl
= spi_controller_get_devdata(mem
->spi
->controller
);
576 max_len
= as_ctrl
->nfi_cfg
.sec_size
;
577 max_len
+= as_ctrl
->nfi_cfg
.spare_size
;
578 max_len
*= as_ctrl
->nfi_cfg
.sec_num
;
580 if (op
->data
.nbytes
> max_len
)
581 op
->data
.nbytes
= max_len
;
583 max_len
= 1 + op
->addr
.nbytes
+ op
->dummy
.nbytes
;
587 if (op
->data
.nbytes
> 160 - max_len
)
588 op
->data
.nbytes
= 160 - max_len
;
594 static bool airoha_snand_supports_op(struct spi_mem
*mem
,
595 const struct spi_mem_op
*op
)
597 if (!spi_mem_default_supports_op(mem
, op
))
600 if (op
->cmd
.buswidth
!= 1)
603 if (airoha_snand_is_page_ops(op
))
606 return (!op
->addr
.nbytes
|| op
->addr
.buswidth
== 1) &&
607 (!op
->dummy
.nbytes
|| op
->dummy
.buswidth
== 1) &&
608 (!op
->data
.nbytes
|| op
->data
.buswidth
== 1);
611 static int airoha_snand_dirmap_create(struct spi_mem_dirmap_desc
*desc
)
613 u8
*txrx_buf
= spi_get_ctldata(desc
->mem
->spi
);
618 if (desc
->info
.offset
+ desc
->info
.length
> U32_MAX
)
621 if (!airoha_snand_supports_op(desc
->mem
, &desc
->info
.op_tmpl
))
627 static ssize_t
airoha_snand_dirmap_read(struct spi_mem_dirmap_desc
*desc
,
628 u64 offs
, size_t len
, void *buf
)
630 struct spi_mem_op
*op
= &desc
->info
.op_tmpl
;
631 struct spi_device
*spi
= desc
->mem
->spi
;
632 struct airoha_snand_ctrl
*as_ctrl
;
633 u8
*txrx_buf
= spi_get_ctldata(spi
);
638 switch (op
->cmd
.opcode
) {
639 case SPI_NAND_OP_READ_FROM_CACHE_DUAL
:
642 case SPI_NAND_OP_READ_FROM_CACHE_QUAD
:
650 as_ctrl
= spi_controller_get_devdata(spi
->controller
);
651 err
= airoha_snand_set_mode(as_ctrl
, SPI_MODE_DMA
);
655 err
= airoha_snand_nfi_config(as_ctrl
);
659 dma_addr
= dma_map_single(as_ctrl
->dev
, txrx_buf
, SPI_NAND_CACHE_SIZE
,
661 err
= dma_mapping_error(as_ctrl
->dev
, dma_addr
);
666 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_STRADDR
,
669 goto error_dma_unmap
;
671 /* set cust sec size */
672 val
= as_ctrl
->nfi_cfg
.sec_size
* as_ctrl
->nfi_cfg
.sec_num
;
673 val
= FIELD_PREP(SPI_NFI_READ_DATA_BYTE_NUM
, val
);
674 err
= regmap_update_bits(as_ctrl
->regmap_nfi
,
675 REG_SPI_NFI_SNF_MISC_CTL2
,
676 SPI_NFI_READ_DATA_BYTE_NUM
, val
);
678 goto error_dma_unmap
;
680 /* set read command */
681 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_RD_CTL2
,
684 goto error_dma_unmap
;
687 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_SNF_MISC_CTL
,
688 FIELD_PREP(SPI_NFI_DATA_READ_WR_MODE
, rd_mode
));
690 goto error_dma_unmap
;
693 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_RD_CTL3
, 0x0);
695 goto error_dma_unmap
;
698 err
= regmap_update_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CNFG
,
700 FIELD_PREP(SPI_NFI_OPMODE
, 6));
702 goto error_dma_unmap
;
704 err
= regmap_set_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CNFG
,
705 SPI_NFI_READ_MODE
| SPI_NFI_DMA_MODE
);
707 goto error_dma_unmap
;
709 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CMD
, 0x0);
711 goto error_dma_unmap
;
713 /* trigger dma start read */
714 err
= regmap_clear_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CON
,
717 goto error_dma_unmap
;
719 err
= regmap_set_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CON
,
722 goto error_dma_unmap
;
724 err
= regmap_read_poll_timeout(as_ctrl
->regmap_nfi
,
725 REG_SPI_NFI_SNF_STA_CTL1
, val
,
726 (val
& SPI_NFI_READ_FROM_CACHE_DONE
),
727 0, 1 * USEC_PER_SEC
);
729 goto error_dma_unmap
;
732 * SPI_NFI_READ_FROM_CACHE_DONE bit must be written at the end
733 * of dirmap_read operation even if it is already set.
735 err
= regmap_write_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_SNF_STA_CTL1
,
736 SPI_NFI_READ_FROM_CACHE_DONE
,
737 SPI_NFI_READ_FROM_CACHE_DONE
);
739 goto error_dma_unmap
;
741 err
= regmap_read_poll_timeout(as_ctrl
->regmap_nfi
, REG_SPI_NFI_INTR
,
742 val
, (val
& SPI_NFI_AHB_DONE
), 0,
745 goto error_dma_unmap
;
747 /* DMA read need delay for data ready from controller to DRAM */
750 dma_unmap_single(as_ctrl
->dev
, dma_addr
, SPI_NAND_CACHE_SIZE
,
752 err
= airoha_snand_set_mode(as_ctrl
, SPI_MODE_MANUAL
);
756 memcpy(buf
, txrx_buf
+ offs
, len
);
761 dma_unmap_single(as_ctrl
->dev
, dma_addr
, SPI_NAND_CACHE_SIZE
,
766 static ssize_t
airoha_snand_dirmap_write(struct spi_mem_dirmap_desc
*desc
,
767 u64 offs
, size_t len
, const void *buf
)
769 struct spi_mem_op
*op
= &desc
->info
.op_tmpl
;
770 struct spi_device
*spi
= desc
->mem
->spi
;
771 u8
*txrx_buf
= spi_get_ctldata(spi
);
772 struct airoha_snand_ctrl
*as_ctrl
;
777 as_ctrl
= spi_controller_get_devdata(spi
->controller
);
778 err
= airoha_snand_set_mode(as_ctrl
, SPI_MODE_MANUAL
);
782 memcpy(txrx_buf
+ offs
, buf
, len
);
783 dma_addr
= dma_map_single(as_ctrl
->dev
, txrx_buf
, SPI_NAND_CACHE_SIZE
,
785 err
= dma_mapping_error(as_ctrl
->dev
, dma_addr
);
789 err
= airoha_snand_set_mode(as_ctrl
, SPI_MODE_DMA
);
791 goto error_dma_unmap
;
793 err
= airoha_snand_nfi_config(as_ctrl
);
795 goto error_dma_unmap
;
797 if (op
->cmd
.opcode
== SPI_NAND_OP_PROGRAM_LOAD_QUAD
||
798 op
->cmd
.opcode
== SPI_NAND_OP_PROGRAM_LOAD_RAMDON_QUAD
)
803 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_STRADDR
,
806 goto error_dma_unmap
;
808 val
= FIELD_PREP(SPI_NFI_PROG_LOAD_BYTE_NUM
,
809 as_ctrl
->nfi_cfg
.sec_size
* as_ctrl
->nfi_cfg
.sec_num
);
810 err
= regmap_update_bits(as_ctrl
->regmap_nfi
,
811 REG_SPI_NFI_SNF_MISC_CTL2
,
812 SPI_NFI_PROG_LOAD_BYTE_NUM
, val
);
814 goto error_dma_unmap
;
816 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_PG_CTL1
,
817 FIELD_PREP(SPI_NFI_PG_LOAD_CMD
,
820 goto error_dma_unmap
;
822 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_SNF_MISC_CTL
,
823 FIELD_PREP(SPI_NFI_DATA_READ_WR_MODE
, wr_mode
));
825 goto error_dma_unmap
;
827 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_PG_CTL2
, 0x0);
829 goto error_dma_unmap
;
831 err
= regmap_clear_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CNFG
,
834 goto error_dma_unmap
;
836 err
= regmap_update_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CNFG
,
838 FIELD_PREP(SPI_NFI_OPMODE
, 3));
840 goto error_dma_unmap
;
842 err
= regmap_set_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CNFG
,
845 goto error_dma_unmap
;
847 err
= regmap_write(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CMD
, 0x80);
849 goto error_dma_unmap
;
851 err
= regmap_clear_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CON
,
854 goto error_dma_unmap
;
856 err
= regmap_set_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CON
,
859 goto error_dma_unmap
;
861 err
= regmap_read_poll_timeout(as_ctrl
->regmap_nfi
, REG_SPI_NFI_INTR
,
862 val
, (val
& SPI_NFI_AHB_DONE
), 0,
865 goto error_dma_unmap
;
867 err
= regmap_read_poll_timeout(as_ctrl
->regmap_nfi
,
868 REG_SPI_NFI_SNF_STA_CTL1
, val
,
869 (val
& SPI_NFI_LOAD_TO_CACHE_DONE
),
870 0, 1 * USEC_PER_SEC
);
872 goto error_dma_unmap
;
875 * SPI_NFI_LOAD_TO_CACHE_DONE bit must be written at the end
876 * of dirmap_write operation even if it is already set.
878 err
= regmap_write_bits(as_ctrl
->regmap_nfi
, REG_SPI_NFI_SNF_STA_CTL1
,
879 SPI_NFI_LOAD_TO_CACHE_DONE
,
880 SPI_NFI_LOAD_TO_CACHE_DONE
);
882 goto error_dma_unmap
;
884 dma_unmap_single(as_ctrl
->dev
, dma_addr
, SPI_NAND_CACHE_SIZE
,
886 err
= airoha_snand_set_mode(as_ctrl
, SPI_MODE_MANUAL
);
893 dma_unmap_single(as_ctrl
->dev
, dma_addr
, SPI_NAND_CACHE_SIZE
,
898 static int airoha_snand_exec_op(struct spi_mem
*mem
,
899 const struct spi_mem_op
*op
)
901 u8 data
[8], cmd
, opcode
= op
->cmd
.opcode
;
902 struct airoha_snand_ctrl
*as_ctrl
;
905 as_ctrl
= spi_controller_get_devdata(mem
->spi
->controller
);
907 /* switch to manual mode */
908 err
= airoha_snand_set_mode(as_ctrl
, SPI_MODE_MANUAL
);
912 err
= airoha_snand_set_cs(as_ctrl
, SPI_CHIP_SEL_LOW
);
917 err
= airoha_snand_write_data(as_ctrl
, 0x8, &opcode
, sizeof(opcode
));
922 cmd
= opcode
== SPI_NAND_OP_GET_FEATURE
? 0x11 : 0x8;
923 put_unaligned_be64(op
->addr
.val
, data
);
925 for (i
= ARRAY_SIZE(data
) - op
->addr
.nbytes
;
926 i
< ARRAY_SIZE(data
); i
++) {
927 err
= airoha_snand_write_data(as_ctrl
, cmd
, &data
[i
],
935 for (i
= 0; i
< op
->dummy
.nbytes
; i
++) {
936 err
= airoha_snand_write_data(as_ctrl
, 0x8, &data
[0],
943 if (op
->data
.dir
== SPI_MEM_DATA_IN
) {
944 err
= airoha_snand_read_data(as_ctrl
, op
->data
.buf
.in
,
949 err
= airoha_snand_write_data(as_ctrl
, 0x8, op
->data
.buf
.out
,
955 return airoha_snand_set_cs(as_ctrl
, SPI_CHIP_SEL_HIGH
);
958 static const struct spi_controller_mem_ops airoha_snand_mem_ops
= {
959 .adjust_op_size
= airoha_snand_adjust_op_size
,
960 .supports_op
= airoha_snand_supports_op
,
961 .exec_op
= airoha_snand_exec_op
,
962 .dirmap_create
= airoha_snand_dirmap_create
,
963 .dirmap_read
= airoha_snand_dirmap_read
,
964 .dirmap_write
= airoha_snand_dirmap_write
,
967 static int airoha_snand_setup(struct spi_device
*spi
)
969 struct airoha_snand_ctrl
*as_ctrl
;
972 /* prepare device buffer */
973 as_ctrl
= spi_controller_get_devdata(spi
->controller
);
974 txrx_buf
= devm_kzalloc(as_ctrl
->dev
, SPI_NAND_CACHE_SIZE
,
979 spi_set_ctldata(spi
, txrx_buf
);
984 static int airoha_snand_nfi_setup(struct airoha_snand_ctrl
*as_ctrl
)
986 u32 val
, sec_size
, sec_num
;
989 err
= regmap_read(as_ctrl
->regmap_nfi
, REG_SPI_NFI_CON
, &val
);
993 sec_num
= FIELD_GET(SPI_NFI_SEC_NUM
, val
);
995 err
= regmap_read(as_ctrl
->regmap_nfi
, REG_SPI_NFI_SECCUS_SIZE
, &val
);
999 sec_size
= FIELD_GET(SPI_NFI_CUS_SEC_SIZE
, val
);
1001 /* init default value */
1002 as_ctrl
->nfi_cfg
.sec_size
= sec_size
;
1003 as_ctrl
->nfi_cfg
.sec_num
= sec_num
;
1004 as_ctrl
->nfi_cfg
.page_size
= round_down(sec_size
* sec_num
, 1024);
1005 as_ctrl
->nfi_cfg
.spare_size
= 16;
1007 err
= airoha_snand_nfi_init(as_ctrl
);
1011 return airoha_snand_nfi_config(as_ctrl
);
1014 static const struct regmap_config spi_ctrl_regmap_config
= {
1019 .max_register
= REG_SPI_CTRL_NFI2SPI_EN
,
1022 static const struct regmap_config spi_nfi_regmap_config
= {
1027 .max_register
= REG_SPI_NFI_SNF_NFI_CNFG
,
1030 static const struct of_device_id airoha_snand_ids
[] = {
1031 { .compatible
= "airoha,en7581-snand" },
1034 MODULE_DEVICE_TABLE(of
, airoha_snand_ids
);
1036 static int airoha_snand_probe(struct platform_device
*pdev
)
1038 struct airoha_snand_ctrl
*as_ctrl
;
1039 struct device
*dev
= &pdev
->dev
;
1040 struct spi_controller
*ctrl
;
1044 ctrl
= devm_spi_alloc_host(dev
, sizeof(*as_ctrl
));
1048 as_ctrl
= spi_controller_get_devdata(ctrl
);
1051 base
= devm_platform_ioremap_resource(pdev
, 0);
1053 return PTR_ERR(base
);
1055 as_ctrl
->regmap_ctrl
= devm_regmap_init_mmio(dev
, base
,
1056 &spi_ctrl_regmap_config
);
1057 if (IS_ERR(as_ctrl
->regmap_ctrl
))
1058 return dev_err_probe(dev
, PTR_ERR(as_ctrl
->regmap_ctrl
),
1059 "failed to init spi ctrl regmap\n");
1061 base
= devm_platform_ioremap_resource(pdev
, 1);
1063 return PTR_ERR(base
);
1065 as_ctrl
->regmap_nfi
= devm_regmap_init_mmio(dev
, base
,
1066 &spi_nfi_regmap_config
);
1067 if (IS_ERR(as_ctrl
->regmap_nfi
))
1068 return dev_err_probe(dev
, PTR_ERR(as_ctrl
->regmap_nfi
),
1069 "failed to init spi nfi regmap\n");
1071 as_ctrl
->spi_clk
= devm_clk_get_enabled(dev
, "spi");
1072 if (IS_ERR(as_ctrl
->spi_clk
))
1073 return dev_err_probe(dev
, PTR_ERR(as_ctrl
->spi_clk
),
1074 "unable to get spi clk\n");
1076 err
= dma_set_mask(as_ctrl
->dev
, DMA_BIT_MASK(32));
1080 ctrl
->num_chipselect
= 2;
1081 ctrl
->mem_ops
= &airoha_snand_mem_ops
;
1082 ctrl
->bits_per_word_mask
= SPI_BPW_MASK(8);
1083 ctrl
->mode_bits
= SPI_RX_DUAL
;
1084 ctrl
->setup
= airoha_snand_setup
;
1085 device_set_node(&ctrl
->dev
, dev_fwnode(dev
));
1087 err
= airoha_snand_nfi_setup(as_ctrl
);
1091 return devm_spi_register_controller(dev
, ctrl
);
1094 static struct platform_driver airoha_snand_driver
= {
1096 .name
= "airoha-spi",
1097 .of_match_table
= airoha_snand_ids
,
1099 .probe
= airoha_snand_probe
,
1101 module_platform_driver(airoha_snand_driver
);
1103 MODULE_DESCRIPTION("Airoha SPI-NAND Flash Controller Driver");
1104 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
1105 MODULE_AUTHOR("Ray Liu <ray.liu@airoha.com>");
1106 MODULE_LICENSE("GPL");