1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/completion.h>
4 #include <linux/dma-mapping.h>
5 #include <linux/interrupt.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/platform_device.h>
8 #include <linux/regmap.h>
9 #include <linux/spi/spi.h>
10 #include <linux/spi/spi-mem.h>
13 #define SNAFCFR_DMA_IE BIT(20)
20 #define SNAFDRSAR 0x1c
22 #define SNAFDIR_DMA_IP BIT(0)
25 #define SNAFSR_NFCOS BIT(3)
26 #define SNAFSR_NFDRS BIT(2)
27 #define SNAFSR_NFDWS BIT(1)
29 #define CMR_LEN(len) ((len) - 1)
30 #define CMR_WID(width) (((width) >> 1) << 28)
34 struct regmap
*regmap
;
35 struct completion comp
;
38 static irqreturn_t
rtl_snand_irq(int irq
, void *data
)
40 struct rtl_snand
*snand
= data
;
43 regmap_read(snand
->regmap
, SNAFSR
, &val
);
44 if (val
& (SNAFSR_NFCOS
| SNAFSR_NFDRS
| SNAFSR_NFDWS
))
47 regmap_write(snand
->regmap
, SNAFDIR
, SNAFDIR_DMA_IP
);
48 complete(&snand
->comp
);
53 static bool rtl_snand_supports_op(struct spi_mem
*mem
,
54 const struct spi_mem_op
*op
)
56 if (!spi_mem_default_supports_op(mem
, op
))
58 if (op
->cmd
.nbytes
!= 1 || op
->cmd
.buswidth
!= 1)
63 static void rtl_snand_set_cs(struct rtl_snand
*snand
, int cs
, bool active
)
68 val
= ~(1 << (4 * cs
));
72 regmap_write(snand
->regmap
, SNAFCCR
, val
);
75 static int rtl_snand_wait_ready(struct rtl_snand
*snand
)
79 return regmap_read_poll_timeout(snand
->regmap
, SNAFSR
, val
, !(val
& SNAFSR_NFCOS
),
80 0, 2 * USEC_PER_MSEC
);
83 static int rtl_snand_xfer_head(struct rtl_snand
*snand
, int cs
, const struct spi_mem_op
*op
)
88 rtl_snand_set_cs(snand
, cs
, true);
90 val
= op
->cmd
.opcode
<< 24;
92 if (op
->addr
.nbytes
&& op
->addr
.buswidth
== 1) {
93 val
|= op
->addr
.val
<< ((3 - op
->addr
.nbytes
) * 8);
94 len
+= op
->addr
.nbytes
;
97 ret
= rtl_snand_wait_ready(snand
);
101 ret
= regmap_write(snand
->regmap
, SNAFWCMR
, CMR_LEN(len
));
105 ret
= regmap_write(snand
->regmap
, SNAFWDR
, val
);
109 ret
= rtl_snand_wait_ready(snand
);
113 if (op
->addr
.buswidth
> 1) {
114 val
= op
->addr
.val
<< ((3 - op
->addr
.nbytes
) * 8);
115 len
= op
->addr
.nbytes
;
117 ret
= regmap_write(snand
->regmap
, SNAFWCMR
,
118 CMR_WID(op
->addr
.buswidth
) | CMR_LEN(len
));
122 ret
= regmap_write(snand
->regmap
, SNAFWDR
, val
);
126 ret
= rtl_snand_wait_ready(snand
);
131 if (op
->dummy
.nbytes
) {
134 ret
= regmap_write(snand
->regmap
, SNAFWCMR
,
135 CMR_WID(op
->dummy
.buswidth
) | CMR_LEN(op
->dummy
.nbytes
));
139 ret
= regmap_write(snand
->regmap
, SNAFWDR
, val
);
143 ret
= rtl_snand_wait_ready(snand
);
151 static void rtl_snand_xfer_tail(struct rtl_snand
*snand
, int cs
)
153 rtl_snand_set_cs(snand
, cs
, false);
156 static int rtl_snand_xfer(struct rtl_snand
*snand
, int cs
, const struct spi_mem_op
*op
)
158 unsigned int pos
, nbytes
;
162 ret
= rtl_snand_xfer_head(snand
, cs
, op
);
166 if (op
->data
.dir
== SPI_MEM_DATA_IN
) {
168 len
= op
->data
.nbytes
;
175 ret
= rtl_snand_wait_ready(snand
);
179 ret
= regmap_write(snand
->regmap
, SNAFRCMR
,
180 CMR_WID(op
->data
.buswidth
) | CMR_LEN(nbytes
));
184 ret
= rtl_snand_wait_ready(snand
);
188 ret
= regmap_read(snand
->regmap
, SNAFRDR
, &val
);
192 memcpy(op
->data
.buf
.in
+ pos
, &val
, nbytes
);
196 } else if (op
->data
.dir
== SPI_MEM_DATA_OUT
) {
198 len
= op
->data
.nbytes
;
205 memcpy(&val
, op
->data
.buf
.out
+ pos
, nbytes
);
209 ret
= regmap_write(snand
->regmap
, SNAFWCMR
, CMR_LEN(nbytes
));
213 ret
= regmap_write(snand
->regmap
, SNAFWDR
, val
);
217 ret
= rtl_snand_wait_ready(snand
);
224 rtl_snand_xfer_tail(snand
, cs
);
227 dev_err(snand
->dev
, "transfer failed %d\n", ret
);
232 static int rtl_snand_dma_xfer(struct rtl_snand
*snand
, int cs
, const struct spi_mem_op
*op
)
234 unsigned int pos
, nbytes
;
237 enum dma_data_direction dir
;
238 u32 trig
, len
, maxlen
;
240 ret
= rtl_snand_xfer_head(snand
, cs
, op
);
244 if (op
->data
.dir
== SPI_MEM_DATA_IN
) {
246 dir
= DMA_FROM_DEVICE
;
248 } else if (op
->data
.dir
== SPI_MEM_DATA_OUT
) {
257 buf_dma
= dma_map_single(snand
->dev
, op
->data
.buf
.in
, op
->data
.nbytes
, dir
);
258 ret
= dma_mapping_error(snand
->dev
, buf_dma
);
262 ret
= regmap_write(snand
->regmap
, SNAFDIR
, SNAFDIR_DMA_IP
);
266 ret
= regmap_update_bits(snand
->regmap
, SNAFCFR
, SNAFCFR_DMA_IE
, SNAFCFR_DMA_IE
);
271 len
= op
->data
.nbytes
;
278 reinit_completion(&snand
->comp
);
280 ret
= regmap_write(snand
->regmap
, SNAFDRSAR
, buf_dma
+ pos
);
282 goto out_disable_int
;
286 ret
= regmap_write(snand
->regmap
, SNAFDLR
,
287 CMR_WID(op
->data
.buswidth
) | nbytes
);
289 goto out_disable_int
;
291 ret
= regmap_write(snand
->regmap
, SNAFDTR
, trig
);
293 goto out_disable_int
;
295 if (!wait_for_completion_timeout(&snand
->comp
, usecs_to_jiffies(20000)))
299 goto out_disable_int
;
303 regmap_update_bits(snand
->regmap
, SNAFCFR
, SNAFCFR_DMA_IE
, 0);
305 dma_unmap_single(snand
->dev
, buf_dma
, op
->data
.nbytes
, dir
);
307 rtl_snand_xfer_tail(snand
, cs
);
310 dev_err(snand
->dev
, "transfer failed %d\n", ret
);
315 static bool rtl_snand_dma_op(const struct spi_mem_op
*op
)
317 switch (op
->data
.dir
) {
318 case SPI_MEM_DATA_IN
:
319 case SPI_MEM_DATA_OUT
:
320 return op
->data
.nbytes
> 32;
326 static int rtl_snand_exec_op(struct spi_mem
*mem
, const struct spi_mem_op
*op
)
328 struct rtl_snand
*snand
= spi_controller_get_devdata(mem
->spi
->controller
);
329 int cs
= spi_get_chipselect(mem
->spi
, 0);
331 dev_dbg(snand
->dev
, "cs %d op cmd %02x %d:%d, dummy %d:%d, addr %08llx@%d:%d, data %d:%d\n",
333 op
->cmd
.buswidth
, op
->cmd
.nbytes
, op
->dummy
.buswidth
,
334 op
->dummy
.nbytes
, op
->addr
.val
, op
->addr
.buswidth
,
335 op
->addr
.nbytes
, op
->data
.buswidth
, op
->data
.nbytes
);
337 if (rtl_snand_dma_op(op
))
338 return rtl_snand_dma_xfer(snand
, cs
, op
);
340 return rtl_snand_xfer(snand
, cs
, op
);
343 static const struct spi_controller_mem_ops rtl_snand_mem_ops
= {
344 .supports_op
= rtl_snand_supports_op
,
345 .exec_op
= rtl_snand_exec_op
,
348 static const struct of_device_id rtl_snand_match
[] = {
349 { .compatible
= "realtek,rtl9301-snand" },
350 { .compatible
= "realtek,rtl9302b-snand" },
351 { .compatible
= "realtek,rtl9302c-snand" },
352 { .compatible
= "realtek,rtl9303-snand" },
355 MODULE_DEVICE_TABLE(of
, rtl_snand_match
);
357 static int rtl_snand_probe(struct platform_device
*pdev
)
359 struct rtl_snand
*snand
;
360 struct device
*dev
= &pdev
->dev
;
361 struct spi_controller
*ctrl
;
363 const struct regmap_config rc
= {
367 .cache_type
= REGCACHE_NONE
,
371 ctrl
= devm_spi_alloc_host(dev
, sizeof(*snand
));
375 snand
= spi_controller_get_devdata(ctrl
);
378 base
= devm_platform_ioremap_resource(pdev
, 0);
380 return PTR_ERR(base
);
382 snand
->regmap
= devm_regmap_init_mmio(dev
, base
, &rc
);
383 if (IS_ERR(snand
->regmap
))
384 return PTR_ERR(snand
->regmap
);
386 init_completion(&snand
->comp
);
388 irq
= platform_get_irq(pdev
, 0);
392 ret
= dma_set_mask(snand
->dev
, DMA_BIT_MASK(32));
394 return dev_err_probe(dev
, ret
, "failed to set DMA mask\n");
396 ret
= devm_request_irq(dev
, irq
, rtl_snand_irq
, 0, "rtl-snand", snand
);
398 return dev_err_probe(dev
, ret
, "failed to request irq\n");
400 ctrl
->num_chipselect
= 2;
401 ctrl
->mem_ops
= &rtl_snand_mem_ops
;
402 ctrl
->bits_per_word_mask
= SPI_BPW_MASK(8);
403 ctrl
->mode_bits
= SPI_RX_DUAL
| SPI_RX_QUAD
| SPI_TX_DUAL
| SPI_TX_QUAD
;
404 device_set_node(&ctrl
->dev
, dev_fwnode(dev
));
406 return devm_spi_register_controller(dev
, ctrl
);
409 static struct platform_driver rtl_snand_driver
= {
411 .name
= "realtek-rtl-snand",
412 .of_match_table
= rtl_snand_match
,
414 .probe
= rtl_snand_probe
,
416 module_platform_driver(rtl_snand_driver
);
418 MODULE_DESCRIPTION("Realtek SPI-NAND Flash Controller Driver");
419 MODULE_LICENSE("GPL");