1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 Sigma Designs
9 #include <linux/iopoll.h>
10 #include <linux/module.h>
11 #include <linux/mtd/rawnand.h>
12 #include <linux/dmaengine.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/platform_device.h>
16 /* Offsets relative to chip->base */
21 /* Offsets relative to reg_base */
22 #define NFC_STATUS 0x00
23 #define NFC_FLASH_CMD 0x04
24 #define NFC_DEVICE_CFG 0x08
25 #define NFC_TIMING1 0x0c
26 #define NFC_TIMING2 0x10
27 #define NFC_XFER_CFG 0x14
28 #define NFC_PKT_0_CFG 0x18
29 #define NFC_PKT_N_CFG 0x1c
30 #define NFC_BB_CFG 0x20
31 #define NFC_ADDR_PAGE 0x24
32 #define NFC_ADDR_OFFSET 0x28
33 #define NFC_XFER_STATUS 0x2c
35 /* NFC_STATUS values */
36 #define CMD_READY BIT(31)
38 /* NFC_FLASH_CMD values */
42 /* NFC_XFER_STATUS values */
43 #define PAGE_IS_EMPTY BIT(16)
45 /* Offsets relative to mem_base */
46 #define METADATA 0x000
47 #define ERROR_REPORT 0x1c0
50 * Error reports are split in two bytes:
51 * byte 0 for the first packet in the page (PKT_0)
52 * byte 1 for other packets in the page (PKT_N, for N > 0)
53 * ERR_COUNT_PKT_N is the max error count over all but the first packet.
55 #define ERR_COUNT_PKT_0(v) (((v) >> 0) & 0x3f)
56 #define ERR_COUNT_PKT_N(v) (((v) >> 8) & 0x3f)
57 #define DECODE_FAIL_PKT_0(v) (((v) & BIT(7)) == 0)
58 #define DECODE_FAIL_PKT_N(v) (((v) & BIT(15)) == 0)
60 /* Offsets relative to pbus_base */
61 #define PBUS_CS_CTRL 0x83c
62 #define PBUS_PAD_MODE 0x8f0
64 /* PBUS_CS_CTRL values */
65 #define PBUS_IORDY BIT(31)
68 * PBUS_PAD_MODE values
69 * In raw mode, the driver communicates directly with the NAND chips.
70 * In NFC mode, the NAND Flash controller manages the communication.
71 * We use NFC mode for read and write; raw mode for everything else.
74 #define MODE_NFC BIT(31)
76 #define METADATA_SIZE 4
78 #define FIELD_ORDER 15
83 struct nand_controller hw
;
84 void __iomem
*reg_base
;
85 void __iomem
*mem_base
;
86 void __iomem
*pbus_base
;
87 struct tango_chip
*chips
[MAX_CS
];
88 struct dma_chan
*chan
;
92 #define to_tango_nfc(ptr) container_of(ptr, struct tango_nfc, hw)
95 struct nand_chip nand_chip
;
105 #define to_tango_chip(ptr) container_of(ptr, struct tango_chip, nand_chip)
107 #define XFER_CFG(cs, page_count, steps, metadata_size) \
108 ((cs) << 24 | (page_count) << 16 | (steps) << 8 | (metadata_size))
110 #define PKT_CFG(size, strength) ((size) << 16 | (strength))
112 #define BB_CFG(bb_offset, bb_size) ((bb_offset) << 16 | (bb_size))
114 #define TIMING(t0, t1, t2, t3) ((t0) << 24 | (t1) << 16 | (t2) << 8 | (t3))
116 static void tango_cmd_ctrl(struct nand_chip
*chip
, int dat
, unsigned int ctrl
)
118 struct tango_chip
*tchip
= to_tango_chip(chip
);
121 writeb_relaxed(dat
, tchip
->base
+ PBUS_CMD
);
124 writeb_relaxed(dat
, tchip
->base
+ PBUS_ADDR
);
127 static int tango_dev_ready(struct nand_chip
*chip
)
129 struct tango_nfc
*nfc
= to_tango_nfc(chip
->controller
);
131 return readl_relaxed(nfc
->pbus_base
+ PBUS_CS_CTRL
) & PBUS_IORDY
;
134 static u8
tango_read_byte(struct nand_chip
*chip
)
136 struct tango_chip
*tchip
= to_tango_chip(chip
);
138 return readb_relaxed(tchip
->base
+ PBUS_DATA
);
141 static void tango_read_buf(struct nand_chip
*chip
, u8
*buf
, int len
)
143 struct tango_chip
*tchip
= to_tango_chip(chip
);
145 ioread8_rep(tchip
->base
+ PBUS_DATA
, buf
, len
);
148 static void tango_write_buf(struct nand_chip
*chip
, const u8
*buf
, int len
)
150 struct tango_chip
*tchip
= to_tango_chip(chip
);
152 iowrite8_rep(tchip
->base
+ PBUS_DATA
, buf
, len
);
155 static void tango_select_chip(struct nand_chip
*chip
, int idx
)
157 struct tango_nfc
*nfc
= to_tango_nfc(chip
->controller
);
158 struct tango_chip
*tchip
= to_tango_chip(chip
);
161 return; /* No "chip unselect" function */
163 writel_relaxed(tchip
->timing1
, nfc
->reg_base
+ NFC_TIMING1
);
164 writel_relaxed(tchip
->timing2
, nfc
->reg_base
+ NFC_TIMING2
);
165 writel_relaxed(tchip
->xfer_cfg
, nfc
->reg_base
+ NFC_XFER_CFG
);
166 writel_relaxed(tchip
->pkt_0_cfg
, nfc
->reg_base
+ NFC_PKT_0_CFG
);
167 writel_relaxed(tchip
->pkt_n_cfg
, nfc
->reg_base
+ NFC_PKT_N_CFG
);
168 writel_relaxed(tchip
->bb_cfg
, nfc
->reg_base
+ NFC_BB_CFG
);
172 * The controller does not check for bitflips in erased pages,
173 * therefore software must check instead.
175 static int check_erased_page(struct nand_chip
*chip
, u8
*buf
)
177 struct mtd_info
*mtd
= nand_to_mtd(chip
);
178 u8
*meta
= chip
->oob_poi
+ BBM_SIZE
;
179 u8
*ecc
= chip
->oob_poi
+ BBM_SIZE
+ METADATA_SIZE
;
180 const int ecc_size
= chip
->ecc
.bytes
;
181 const int pkt_size
= chip
->ecc
.size
;
182 int i
, res
, meta_len
, bitflips
= 0;
184 for (i
= 0; i
< chip
->ecc
.steps
; ++i
) {
185 meta_len
= i
? 0 : METADATA_SIZE
;
186 res
= nand_check_erased_ecc_chunk(buf
, pkt_size
, ecc
, ecc_size
,
190 mtd
->ecc_stats
.failed
++;
192 mtd
->ecc_stats
.corrected
+= res
;
194 bitflips
= max(res
, bitflips
);
202 static int decode_error_report(struct nand_chip
*chip
)
205 struct mtd_info
*mtd
= nand_to_mtd(chip
);
206 struct tango_nfc
*nfc
= to_tango_nfc(chip
->controller
);
208 status
= readl_relaxed(nfc
->reg_base
+ NFC_XFER_STATUS
);
209 if (status
& PAGE_IS_EMPTY
)
212 res
= readl_relaxed(nfc
->mem_base
+ ERROR_REPORT
);
214 if (DECODE_FAIL_PKT_0(res
) || DECODE_FAIL_PKT_N(res
))
217 /* ERR_COUNT_PKT_N is max, not sum, but that's all we have */
218 mtd
->ecc_stats
.corrected
+=
219 ERR_COUNT_PKT_0(res
) + ERR_COUNT_PKT_N(res
);
221 return max(ERR_COUNT_PKT_0(res
), ERR_COUNT_PKT_N(res
));
224 static void tango_dma_callback(void *arg
)
229 static int do_dma(struct tango_nfc
*nfc
, enum dma_data_direction dir
, int cmd
,
230 const void *buf
, int len
, int page
)
232 void __iomem
*addr
= nfc
->reg_base
+ NFC_STATUS
;
233 struct dma_chan
*chan
= nfc
->chan
;
234 struct dma_async_tx_descriptor
*desc
;
235 enum dma_transfer_direction tdir
;
236 struct scatterlist sg
;
237 struct completion tx_done
;
241 sg_init_one(&sg
, buf
, len
);
242 if (dma_map_sg(chan
->device
->dev
, &sg
, 1, dir
) != 1)
245 tdir
= dir
== DMA_TO_DEVICE
? DMA_MEM_TO_DEV
: DMA_DEV_TO_MEM
;
246 desc
= dmaengine_prep_slave_sg(chan
, &sg
, 1, tdir
, DMA_PREP_INTERRUPT
);
250 desc
->callback
= tango_dma_callback
;
251 desc
->callback_param
= &tx_done
;
252 init_completion(&tx_done
);
254 writel_relaxed(MODE_NFC
, nfc
->pbus_base
+ PBUS_PAD_MODE
);
256 writel_relaxed(page
, nfc
->reg_base
+ NFC_ADDR_PAGE
);
257 writel_relaxed(0, nfc
->reg_base
+ NFC_ADDR_OFFSET
);
258 writel_relaxed(cmd
, nfc
->reg_base
+ NFC_FLASH_CMD
);
260 dmaengine_submit(desc
);
261 dma_async_issue_pending(chan
);
263 res
= wait_for_completion_timeout(&tx_done
, HZ
);
265 err
= readl_poll_timeout(addr
, val
, val
& CMD_READY
, 0, 1000);
267 writel_relaxed(MODE_RAW
, nfc
->pbus_base
+ PBUS_PAD_MODE
);
270 dma_unmap_sg(chan
->device
->dev
, &sg
, 1, dir
);
275 static int tango_read_page(struct nand_chip
*chip
, u8
*buf
,
276 int oob_required
, int page
)
278 struct mtd_info
*mtd
= nand_to_mtd(chip
);
279 struct tango_nfc
*nfc
= to_tango_nfc(chip
->controller
);
280 int err
, res
, len
= mtd
->writesize
;
283 chip
->ecc
.read_oob(chip
, page
);
285 err
= do_dma(nfc
, DMA_FROM_DEVICE
, NFC_READ
, buf
, len
, page
);
289 res
= decode_error_report(chip
);
291 chip
->ecc
.read_oob_raw(chip
, page
);
292 res
= check_erased_page(chip
, buf
);
298 static int tango_write_page(struct nand_chip
*chip
, const u8
*buf
,
299 int oob_required
, int page
)
301 struct mtd_info
*mtd
= nand_to_mtd(chip
);
302 struct tango_nfc
*nfc
= to_tango_nfc(chip
->controller
);
303 int err
, status
, len
= mtd
->writesize
;
305 /* Calling tango_write_oob() would send PAGEPROG twice */
309 writel_relaxed(0xffffffff, nfc
->mem_base
+ METADATA
);
310 err
= do_dma(nfc
, DMA_TO_DEVICE
, NFC_WRITE
, buf
, len
, page
);
314 status
= chip
->legacy
.waitfunc(chip
);
315 if (status
& NAND_STATUS_FAIL
)
321 static void aux_read(struct nand_chip
*chip
, u8
**buf
, int len
, int *pos
)
326 /* skip over "len" bytes */
327 nand_change_read_column_op(chip
, *pos
, NULL
, 0, false);
329 tango_read_buf(chip
, *buf
, len
);
334 static void aux_write(struct nand_chip
*chip
, const u8
**buf
, int len
, int *pos
)
339 /* skip over "len" bytes */
340 nand_change_write_column_op(chip
, *pos
, NULL
, 0, false);
342 tango_write_buf(chip
, *buf
, len
);
348 * Physical page layout (not drawn to scale)
350 * NB: Bad Block Marker area splits PKT_N in two (N1, N2).
352 * +---+-----------------+-------+-----+-----------+-----+----+-------+
353 * | M | PKT_0 | ECC_0 | ... | N1 | BBM | N2 | ECC_N |
354 * +---+-----------------+-------+-----+-----------+-----+----+-------+
356 * Logical page layout:
358 * +-----+---+-------+-----+-------+
359 * oob = | BBM | M | ECC_0 | ... | ECC_N |
360 * +-----+---+-------+-----+-------+
362 * +-----------------+-----+-----------------+
363 * buf = | PKT_0 | ... | PKT_N |
364 * +-----------------+-----+-----------------+
366 static void raw_read(struct nand_chip
*chip
, u8
*buf
, u8
*oob
)
368 struct mtd_info
*mtd
= nand_to_mtd(chip
);
370 const int page_size
= mtd
->writesize
;
371 const int ecc_size
= chip
->ecc
.bytes
;
372 const int pkt_size
= chip
->ecc
.size
;
373 int pos
= 0; /* position within physical page */
374 int rem
= page_size
; /* bytes remaining until BBM area */
379 aux_read(chip
, &oob
, METADATA_SIZE
, &pos
);
381 while (rem
> pkt_size
) {
382 aux_read(chip
, &buf
, pkt_size
, &pos
);
383 aux_read(chip
, &oob
, ecc_size
, &pos
);
384 rem
= page_size
- pos
;
387 aux_read(chip
, &buf
, rem
, &pos
);
388 aux_read(chip
, &oob_orig
, BBM_SIZE
, &pos
);
389 aux_read(chip
, &buf
, pkt_size
- rem
, &pos
);
390 aux_read(chip
, &oob
, ecc_size
, &pos
);
393 static void raw_write(struct nand_chip
*chip
, const u8
*buf
, const u8
*oob
)
395 struct mtd_info
*mtd
= nand_to_mtd(chip
);
396 const u8
*oob_orig
= oob
;
397 const int page_size
= mtd
->writesize
;
398 const int ecc_size
= chip
->ecc
.bytes
;
399 const int pkt_size
= chip
->ecc
.size
;
400 int pos
= 0; /* position within physical page */
401 int rem
= page_size
; /* bytes remaining until BBM area */
406 aux_write(chip
, &oob
, METADATA_SIZE
, &pos
);
408 while (rem
> pkt_size
) {
409 aux_write(chip
, &buf
, pkt_size
, &pos
);
410 aux_write(chip
, &oob
, ecc_size
, &pos
);
411 rem
= page_size
- pos
;
414 aux_write(chip
, &buf
, rem
, &pos
);
415 aux_write(chip
, &oob_orig
, BBM_SIZE
, &pos
);
416 aux_write(chip
, &buf
, pkt_size
- rem
, &pos
);
417 aux_write(chip
, &oob
, ecc_size
, &pos
);
420 static int tango_read_page_raw(struct nand_chip
*chip
, u8
*buf
,
421 int oob_required
, int page
)
423 nand_read_page_op(chip
, page
, 0, NULL
, 0);
424 raw_read(chip
, buf
, chip
->oob_poi
);
428 static int tango_write_page_raw(struct nand_chip
*chip
, const u8
*buf
,
429 int oob_required
, int page
)
431 nand_prog_page_begin_op(chip
, page
, 0, NULL
, 0);
432 raw_write(chip
, buf
, chip
->oob_poi
);
433 return nand_prog_page_end_op(chip
);
436 static int tango_read_oob(struct nand_chip
*chip
, int page
)
438 nand_read_page_op(chip
, page
, 0, NULL
, 0);
439 raw_read(chip
, NULL
, chip
->oob_poi
);
443 static int tango_write_oob(struct nand_chip
*chip
, int page
)
445 nand_prog_page_begin_op(chip
, page
, 0, NULL
, 0);
446 raw_write(chip
, NULL
, chip
->oob_poi
);
447 return nand_prog_page_end_op(chip
);
450 static int oob_ecc(struct mtd_info
*mtd
, int idx
, struct mtd_oob_region
*res
)
452 struct nand_chip
*chip
= mtd_to_nand(mtd
);
453 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
455 if (idx
>= ecc
->steps
)
458 res
->offset
= BBM_SIZE
+ METADATA_SIZE
+ ecc
->bytes
* idx
;
459 res
->length
= ecc
->bytes
;
464 static int oob_free(struct mtd_info
*mtd
, int idx
, struct mtd_oob_region
*res
)
466 return -ERANGE
; /* no free space in spare area */
469 static const struct mtd_ooblayout_ops tango_nand_ooblayout_ops
= {
474 static u32
to_ticks(int kHz
, int ps
)
476 return DIV_ROUND_UP_ULL((u64
)kHz
* ps
, NSEC_PER_SEC
);
479 static int tango_set_timings(struct nand_chip
*chip
, int csline
,
480 const struct nand_data_interface
*conf
)
482 const struct nand_sdr_timings
*sdr
= nand_get_sdr_timings(conf
);
483 struct tango_nfc
*nfc
= to_tango_nfc(chip
->controller
);
484 struct tango_chip
*tchip
= to_tango_chip(chip
);
485 u32 Trdy
, Textw
, Twc
, Twpw
, Tacc
, Thold
, Trpw
, Textr
;
486 int kHz
= nfc
->freq_kHz
;
491 if (csline
== NAND_DATA_IFACE_CHECK_ONLY
)
494 Trdy
= to_ticks(kHz
, sdr
->tCEA_max
- sdr
->tREA_max
);
495 Textw
= to_ticks(kHz
, sdr
->tWB_max
);
496 Twc
= to_ticks(kHz
, sdr
->tWC_min
);
497 Twpw
= to_ticks(kHz
, sdr
->tWC_min
- sdr
->tWP_min
);
499 Tacc
= to_ticks(kHz
, sdr
->tREA_max
);
500 Thold
= to_ticks(kHz
, sdr
->tREH_min
);
501 Trpw
= to_ticks(kHz
, sdr
->tRC_min
- sdr
->tREH_min
);
502 Textr
= to_ticks(kHz
, sdr
->tRHZ_max
);
504 tchip
->timing1
= TIMING(Trdy
, Textw
, Twc
, Twpw
);
505 tchip
->timing2
= TIMING(Tacc
, Thold
, Trpw
, Textr
);
510 static int tango_attach_chip(struct nand_chip
*chip
)
512 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
514 ecc
->mode
= NAND_ECC_HW
;
515 ecc
->algo
= NAND_ECC_BCH
;
516 ecc
->bytes
= DIV_ROUND_UP(ecc
->strength
* FIELD_ORDER
, BITS_PER_BYTE
);
518 ecc
->read_page_raw
= tango_read_page_raw
;
519 ecc
->write_page_raw
= tango_write_page_raw
;
520 ecc
->read_page
= tango_read_page
;
521 ecc
->write_page
= tango_write_page
;
522 ecc
->read_oob
= tango_read_oob
;
523 ecc
->write_oob
= tango_write_oob
;
528 static const struct nand_controller_ops tango_controller_ops
= {
529 .attach_chip
= tango_attach_chip
,
530 .setup_data_interface
= tango_set_timings
,
533 static int chip_init(struct device
*dev
, struct device_node
*np
)
537 struct mtd_info
*mtd
;
538 struct nand_chip
*chip
;
539 struct tango_chip
*tchip
;
540 struct nand_ecc_ctrl
*ecc
;
541 struct tango_nfc
*nfc
= dev_get_drvdata(dev
);
543 tchip
= devm_kzalloc(dev
, sizeof(*tchip
), GFP_KERNEL
);
547 res
= of_property_count_u32_elems(np
, "reg");
552 return -ENOTSUPP
; /* Multi-CS chips are not supported */
554 err
= of_property_read_u32_index(np
, "reg", 0, &cs
);
561 chip
= &tchip
->nand_chip
;
563 mtd
= nand_to_mtd(chip
);
565 chip
->legacy
.read_byte
= tango_read_byte
;
566 chip
->legacy
.write_buf
= tango_write_buf
;
567 chip
->legacy
.read_buf
= tango_read_buf
;
568 chip
->legacy
.select_chip
= tango_select_chip
;
569 chip
->legacy
.cmd_ctrl
= tango_cmd_ctrl
;
570 chip
->legacy
.dev_ready
= tango_dev_ready
;
571 chip
->options
= NAND_USE_BOUNCE_BUFFER
|
572 NAND_NO_SUBPAGE_WRITE
|
574 chip
->controller
= &nfc
->hw
;
575 tchip
->base
= nfc
->pbus_base
+ (cs
* 256);
577 nand_set_flash_node(chip
, np
);
578 mtd_set_ooblayout(mtd
, &tango_nand_ooblayout_ops
);
579 mtd
->dev
.parent
= dev
;
581 err
= nand_scan(chip
, 1);
585 tchip
->xfer_cfg
= XFER_CFG(cs
, 1, ecc
->steps
, METADATA_SIZE
);
586 tchip
->pkt_0_cfg
= PKT_CFG(ecc
->size
+ METADATA_SIZE
, ecc
->strength
);
587 tchip
->pkt_n_cfg
= PKT_CFG(ecc
->size
, ecc
->strength
);
588 tchip
->bb_cfg
= BB_CFG(mtd
->writesize
, BBM_SIZE
);
590 err
= mtd_device_register(mtd
, NULL
, 0);
596 nfc
->chips
[cs
] = tchip
;
601 static int tango_nand_remove(struct platform_device
*pdev
)
604 struct tango_nfc
*nfc
= platform_get_drvdata(pdev
);
606 dma_release_channel(nfc
->chan
);
608 for (cs
= 0; cs
< MAX_CS
; ++cs
) {
610 nand_release(&nfc
->chips
[cs
]->nand_chip
);
616 static int tango_nand_probe(struct platform_device
*pdev
)
620 struct resource
*res
;
621 struct tango_nfc
*nfc
;
622 struct device_node
*np
;
624 nfc
= devm_kzalloc(&pdev
->dev
, sizeof(*nfc
), GFP_KERNEL
);
628 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
629 nfc
->reg_base
= devm_ioremap_resource(&pdev
->dev
, res
);
630 if (IS_ERR(nfc
->reg_base
))
631 return PTR_ERR(nfc
->reg_base
);
633 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
634 nfc
->mem_base
= devm_ioremap_resource(&pdev
->dev
, res
);
635 if (IS_ERR(nfc
->mem_base
))
636 return PTR_ERR(nfc
->mem_base
);
638 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
639 nfc
->pbus_base
= devm_ioremap_resource(&pdev
->dev
, res
);
640 if (IS_ERR(nfc
->pbus_base
))
641 return PTR_ERR(nfc
->pbus_base
);
643 writel_relaxed(MODE_RAW
, nfc
->pbus_base
+ PBUS_PAD_MODE
);
645 clk
= devm_clk_get(&pdev
->dev
, NULL
);
649 nfc
->chan
= dma_request_chan(&pdev
->dev
, "rxtx");
650 if (IS_ERR(nfc
->chan
))
651 return PTR_ERR(nfc
->chan
);
653 platform_set_drvdata(pdev
, nfc
);
654 nand_controller_init(&nfc
->hw
);
655 nfc
->hw
.ops
= &tango_controller_ops
;
656 nfc
->freq_kHz
= clk_get_rate(clk
) / 1000;
658 for_each_child_of_node(pdev
->dev
.of_node
, np
) {
659 err
= chip_init(&pdev
->dev
, np
);
661 tango_nand_remove(pdev
);
670 static const struct of_device_id tango_nand_ids
[] = {
671 { .compatible
= "sigma,smp8758-nand" },
674 MODULE_DEVICE_TABLE(of
, tango_nand_ids
);
676 static struct platform_driver tango_nand_driver
= {
677 .probe
= tango_nand_probe
,
678 .remove
= tango_nand_remove
,
680 .name
= "tango-nand",
681 .of_match_table
= tango_nand_ids
,
685 module_platform_driver(tango_nand_driver
);
687 MODULE_LICENSE("GPL");
688 MODULE_AUTHOR("Sigma Designs");
689 MODULE_DESCRIPTION("Tango4 NAND Flash controller driver");