1 // SPDX-License-Identifier: GPL-2.0
3 * Marvell NAND flash controller driver
5 * Copyright (C) 2017 Marvell
6 * Author: Miquel RAYNAL <miquel.raynal@free-electrons.com>
9 * This NAND controller driver handles two versions of the hardware,
10 * one is called NFCv1 and is available on PXA SoCs and the other is
11 * called NFCv2 and is available on Armada SoCs.
13 * The main visible difference is that NFCv1 only has Hamming ECC
14 * capabilities, while NFCv2 also embeds a BCH ECC engine. Also, DMA
15 * is not used with NFCv2.
17 * The ECC layouts are depicted in details in Marvell AN-379, but here
18 * is a brief description.
20 * When using Hamming, the data is split in 512B chunks (either 1, 2
21 * or 4) and each chunk will have its own ECC "digest" of 6B at the
22 * beginning of the OOB area and eventually the remaining free OOB
23 * bytes (also called "spare" bytes in the driver). This engine
24 * corrects up to 1 bit per chunk and detects reliably an error if
25 * there are at most 2 bitflips. Here is the page layout used by the
26 * controller when Hamming is chosen:
28 * +-------------------------------------------------------------+
29 * | Data 1 | ... | Data N | ECC 1 | ... | ECCN | Free OOB bytes |
30 * +-------------------------------------------------------------+
32 * When using the BCH engine, there are N identical (data + free OOB +
33 * ECC) sections and potentially an extra one to deal with
34 * configurations where the chosen (data + free OOB + ECC) sizes do
35 * not align with the page (data + OOB) size. ECC bytes are always
36 * 30B per ECC chunk. Here is the page layout used by the controller
39 * +-----------------------------------------
40 * | Data 1 | Free OOB bytes 1 | ECC 1 | ...
41 * +-----------------------------------------
43 * -------------------------------------------
44 * ... | Data N | Free OOB bytes N | ECC N |
45 * -------------------------------------------
47 * --------------------------------------------+
48 * Last Data | Last Free OOB bytes | Last ECC |
49 * --------------------------------------------+
51 * In both cases, the layout seen by the user is always: all data
52 * first, then all free OOB bytes and finally all ECC bytes. With BCH,
53 * ECC bytes are 30B long and are padded with 0xFF to align on 32
56 * The controller has certain limitations that are handled by the
58 * - It can only read 2k at a time. To overcome this limitation, the
59 * driver issues data cycles on the bus, without issuing new
60 * CMD + ADDR cycles. The Marvell term is "naked" operations.
61 * - The ECC strength in BCH mode cannot be tuned. It is fixed 16
62 * bits. What can be tuned is the ECC block size as long as it
63 * stays between 512B and 2kiB. It's usually chosen based on the
64 * chip ECC requirements. For instance, using 2kiB ECC chunks
65 * provides 4b/512B correctability.
66 * - The controller will always treat data bytes, free OOB bytes
67 * and ECC bytes in that order, no matter what the real layout is
68 * (which is usually all data then all OOB bytes). The
69 * marvell_nfc_layouts array below contains the currently
71 * - Because of these weird layouts, the Bad Block Markers can be
72 * located in data section. In this case, the NAND_BBT_NO_OOB_BBM
73 * option must be set to prevent scanning/writing bad block
77 #include <linux/module.h>
78 #include <linux/clk.h>
79 #include <linux/mtd/rawnand.h>
81 #include <linux/iopoll.h>
82 #include <linux/interrupt.h>
83 #include <linux/platform_device.h>
84 #include <linux/slab.h>
85 #include <linux/mfd/syscon.h>
86 #include <linux/regmap.h>
87 #include <asm/unaligned.h>
89 #include <linux/dmaengine.h>
90 #include <linux/dma-mapping.h>
91 #include <linux/dma/pxa-dma.h>
92 #include <linux/platform_data/mtd-nand-pxa3xx.h>
94 /* Data FIFO granularity, FIFO reads/writes must be a multiple of this length */
96 #define FIFO_REP(x) (x / sizeof(u32))
97 #define BCH_SEQ_READS (32 / FIFO_DEPTH)
98 /* NFC does not support transfers of larger chunks at a time */
99 #define MAX_CHUNK_SIZE 2112
100 /* NFCv1 cannot read more that 7 bytes of ID */
101 #define NFCV1_READID_LEN 7
102 /* Polling is done at a pace of POLL_PERIOD us until POLL_TIMEOUT is reached */
103 #define POLL_PERIOD 0
104 #define POLL_TIMEOUT 100000
105 /* Interrupt maximum wait period in ms */
106 #define IRQ_TIMEOUT 1000
107 /* Latency in clock cycles between SoC pins and NFC logic */
108 #define MIN_RD_DEL_CNT 3
109 /* Maximum number of contiguous address cycles */
110 #define MAX_ADDRESS_CYC_NFCV1 5
111 #define MAX_ADDRESS_CYC_NFCV2 7
112 /* System control registers/bits to enable the NAND controller on some SoCs */
113 #define GENCONF_SOC_DEVICE_MUX 0x208
114 #define GENCONF_SOC_DEVICE_MUX_NFC_EN BIT(0)
115 #define GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST BIT(20)
116 #define GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST BIT(21)
117 #define GENCONF_SOC_DEVICE_MUX_NFC_INT_EN BIT(25)
118 #define GENCONF_SOC_DEVICE_MUX_NFC_DEVBUS_ARB_EN BIT(27)
119 #define GENCONF_CLK_GATING_CTRL 0x220
120 #define GENCONF_CLK_GATING_CTRL_ND_GATE BIT(2)
121 #define GENCONF_ND_CLK_CTRL 0x700
122 #define GENCONF_ND_CLK_CTRL_EN BIT(0)
124 /* NAND controller data flash control register */
126 #define NDCR_ALL_INT GENMASK(11, 0)
127 #define NDCR_CS1_CMDDM BIT(7)
128 #define NDCR_CS0_CMDDM BIT(8)
129 #define NDCR_RDYM BIT(11)
130 #define NDCR_ND_ARB_EN BIT(12)
131 #define NDCR_RA_START BIT(15)
132 #define NDCR_RD_ID_CNT(x) (min_t(unsigned int, x, 0x7) << 16)
133 #define NDCR_PAGE_SZ(x) (x >= 2048 ? BIT(24) : 0)
134 #define NDCR_DWIDTH_M BIT(26)
135 #define NDCR_DWIDTH_C BIT(27)
136 #define NDCR_ND_RUN BIT(28)
137 #define NDCR_DMA_EN BIT(29)
138 #define NDCR_ECC_EN BIT(30)
139 #define NDCR_SPARE_EN BIT(31)
140 #define NDCR_GENERIC_FIELDS_MASK (~(NDCR_RA_START | NDCR_PAGE_SZ(2048) | \
141 NDCR_DWIDTH_M | NDCR_DWIDTH_C))
143 /* NAND interface timing parameter 0 register */
145 #define NDTR0_TRP(x) ((min_t(unsigned int, x, 0xF) & 0x7) << 0)
146 #define NDTR0_TRH(x) (min_t(unsigned int, x, 0x7) << 3)
147 #define NDTR0_ETRP(x) ((min_t(unsigned int, x, 0xF) & 0x8) << 3)
148 #define NDTR0_SEL_NRE_EDGE BIT(7)
149 #define NDTR0_TWP(x) (min_t(unsigned int, x, 0x7) << 8)
150 #define NDTR0_TWH(x) (min_t(unsigned int, x, 0x7) << 11)
151 #define NDTR0_TCS(x) (min_t(unsigned int, x, 0x7) << 16)
152 #define NDTR0_TCH(x) (min_t(unsigned int, x, 0x7) << 19)
153 #define NDTR0_RD_CNT_DEL(x) (min_t(unsigned int, x, 0xF) << 22)
154 #define NDTR0_SELCNTR BIT(26)
155 #define NDTR0_TADL(x) (min_t(unsigned int, x, 0x1F) << 27)
157 /* NAND interface timing parameter 1 register */
159 #define NDTR1_TAR(x) (min_t(unsigned int, x, 0xF) << 0)
160 #define NDTR1_TWHR(x) (min_t(unsigned int, x, 0xF) << 4)
161 #define NDTR1_TRHW(x) (min_t(unsigned int, x / 16, 0x3) << 8)
162 #define NDTR1_PRESCALE BIT(14)
163 #define NDTR1_WAIT_MODE BIT(15)
164 #define NDTR1_TR(x) (min_t(unsigned int, x, 0xFFFF) << 16)
166 /* NAND controller status register */
168 #define NDSR_WRCMDREQ BIT(0)
169 #define NDSR_RDDREQ BIT(1)
170 #define NDSR_WRDREQ BIT(2)
171 #define NDSR_CORERR BIT(3)
172 #define NDSR_UNCERR BIT(4)
173 #define NDSR_CMDD(cs) BIT(8 - cs)
174 #define NDSR_RDY(rb) BIT(11 + rb)
175 #define NDSR_ERRCNT(x) ((x >> 16) & 0x1F)
177 /* NAND ECC control register */
178 #define NDECCCTRL 0x28
179 #define NDECCCTRL_BCH_EN BIT(0)
181 /* NAND controller data buffer register */
184 /* NAND controller command buffer 0 register */
186 #define NDCB0_CMD1(x) ((x & 0xFF) << 0)
187 #define NDCB0_CMD2(x) ((x & 0xFF) << 8)
188 #define NDCB0_ADDR_CYC(x) ((x & 0x7) << 16)
189 #define NDCB0_ADDR_GET_NUM_CYC(x) (((x) >> 16) & 0x7)
190 #define NDCB0_DBC BIT(19)
191 #define NDCB0_CMD_TYPE(x) ((x & 0x7) << 21)
192 #define NDCB0_CSEL BIT(24)
193 #define NDCB0_RDY_BYP BIT(27)
194 #define NDCB0_LEN_OVRD BIT(28)
195 #define NDCB0_CMD_XTYPE(x) ((x & 0x7) << 29)
197 /* NAND controller command buffer 1 register */
199 #define NDCB1_COLS(x) ((x & 0xFFFF) << 0)
200 #define NDCB1_ADDRS_PAGE(x) (x << 16)
202 /* NAND controller command buffer 2 register */
204 #define NDCB2_ADDR5_PAGE(x) (((x >> 16) & 0xFF) << 0)
205 #define NDCB2_ADDR5_CYC(x) ((x & 0xFF) << 0)
207 /* NAND controller command buffer 3 register */
209 #define NDCB3_ADDR6_CYC(x) ((x & 0xFF) << 16)
210 #define NDCB3_ADDR7_CYC(x) ((x & 0xFF) << 24)
212 /* NAND controller command buffer 0 register 'type' and 'xtype' fields */
216 #define TYPE_READ_ID 3
217 #define TYPE_STATUS 4
219 #define TYPE_NAKED_CMD 6
220 #define TYPE_NAKED_ADDR 7
222 #define XTYPE_MONOLITHIC_RW 0
223 #define XTYPE_LAST_NAKED_RW 1
224 #define XTYPE_FINAL_COMMAND 3
226 #define XTYPE_WRITE_DISPATCH 4
227 #define XTYPE_NAKED_RW 5
228 #define XTYPE_COMMAND_DISPATCH 6
232 * struct marvell_hw_ecc_layout - layout of Marvell ECC
234 * Marvell ECC engine works differently than the others, in order to limit the
235 * size of the IP, hardware engineers chose to set a fixed strength at 16 bits
236 * per subpage, and depending on a the desired strength needed by the NAND chip,
237 * a particular layout mixing data/spare/ecc is defined, with a possible last
238 * chunk smaller that the others.
240 * @writesize: Full page size on which the layout applies
241 * @chunk: Desired ECC chunk size on which the layout applies
242 * @strength: Desired ECC strength (per chunk size bytes) on which the
244 * @nchunks: Total number of chunks
245 * @full_chunk_cnt: Number of full-sized chunks, which is the number of
246 * repetitions of the pattern:
247 * (data_bytes + spare_bytes + ecc_bytes).
248 * @data_bytes: Number of data bytes per chunk
249 * @spare_bytes: Number of spare bytes per chunk
250 * @ecc_bytes: Number of ecc bytes per chunk
251 * @last_data_bytes: Number of data bytes in the last chunk
252 * @last_spare_bytes: Number of spare bytes in the last chunk
253 * @last_ecc_bytes: Number of ecc bytes in the last chunk
255 struct marvell_hw_ecc_layout
{
260 /* Corresponding layout */
267 int last_spare_bytes
;
271 #define MARVELL_LAYOUT(ws, dc, ds, nc, fcc, db, sb, eb, ldb, lsb, leb) \
277 .full_chunk_cnt = fcc, \
281 .last_data_bytes = ldb, \
282 .last_spare_bytes = lsb, \
283 .last_ecc_bytes = leb, \
286 /* Layouts explained in AN-379_Marvell_SoC_NFC_ECC */
287 static const struct marvell_hw_ecc_layout marvell_nfc_layouts
[] = {
288 MARVELL_LAYOUT( 512, 512, 1, 1, 1, 512, 8, 8, 0, 0, 0),
289 MARVELL_LAYOUT( 2048, 512, 1, 1, 1, 2048, 40, 24, 0, 0, 0),
290 MARVELL_LAYOUT( 2048, 512, 4, 1, 1, 2048, 32, 30, 0, 0, 0),
291 MARVELL_LAYOUT( 2048, 512, 8, 2, 1, 1024, 0, 30,1024,32, 30),
292 MARVELL_LAYOUT( 2048, 512, 8, 2, 1, 1024, 0, 30,1024,64, 30),
293 MARVELL_LAYOUT( 2048, 512, 12, 3, 2, 704, 0, 30,640, 0, 30),
294 MARVELL_LAYOUT( 2048, 512, 16, 5, 4, 512, 0, 30, 0, 32, 30),
295 MARVELL_LAYOUT( 4096, 512, 4, 2, 2, 2048, 32, 30, 0, 0, 0),
296 MARVELL_LAYOUT( 4096, 512, 8, 5, 4, 1024, 0, 30, 0, 64, 30),
297 MARVELL_LAYOUT( 4096, 512, 12, 6, 5, 704, 0, 30,576, 32, 30),
298 MARVELL_LAYOUT( 4096, 512, 16, 9, 8, 512, 0, 30, 0, 32, 30),
299 MARVELL_LAYOUT( 8192, 512, 4, 4, 4, 2048, 0, 30, 0, 0, 0),
300 MARVELL_LAYOUT( 8192, 512, 8, 9, 8, 1024, 0, 30, 0, 160, 30),
301 MARVELL_LAYOUT( 8192, 512, 12, 12, 11, 704, 0, 30,448, 64, 30),
302 MARVELL_LAYOUT( 8192, 512, 16, 17, 16, 512, 0, 30, 0, 32, 30),
306 * struct marvell_nand_chip_sel - CS line description
308 * The Nand Flash Controller has up to 4 CE and 2 RB pins. The CE selection
309 * is made by a field in NDCB0 register, and in another field in NDCB2 register.
310 * The datasheet describes the logic with an error: ADDR5 field is once
311 * declared at the beginning of NDCB2, and another time at its end. Because the
312 * ADDR5 field of NDCB2 may be used by other bytes, it would be more logical
313 * to use the last bit of this field instead of the first ones.
315 * @cs: Wanted CE lane.
316 * @ndcb0_csel: Value of the NDCB0 register with or without the flag
317 * selecting the wanted CE lane. This is set once when
318 * the Device Tree is probed.
319 * @rb: Ready/Busy pin for the flash chip
321 struct marvell_nand_chip_sel
{
328 * struct marvell_nand_chip - stores NAND chip device related information
330 * @chip: Base NAND chip structure
331 * @node: Used to store NAND chips into a list
332 * @layout: NAND layout when using hardware ECC
333 * @ndcr: Controller register value for this NAND chip
334 * @ndtr0: Timing registers 0 value for this NAND chip
335 * @ndtr1: Timing registers 1 value for this NAND chip
336 * @addr_cyc: Amount of cycles needed to pass column address
337 * @selected_die: Current active CS
338 * @nsels: Number of CS lines required by the NAND chip
339 * @sels: Array of CS lines descriptions
341 struct marvell_nand_chip
{
342 struct nand_chip chip
;
343 struct list_head node
;
344 const struct marvell_hw_ecc_layout
*layout
;
351 struct marvell_nand_chip_sel sels
[] __counted_by(nsels
);
354 static inline struct marvell_nand_chip
*to_marvell_nand(struct nand_chip
*chip
)
356 return container_of(chip
, struct marvell_nand_chip
, chip
);
359 static inline struct marvell_nand_chip_sel
*to_nand_sel(struct marvell_nand_chip
362 return &nand
->sels
[nand
->selected_die
];
366 * struct marvell_nfc_caps - NAND controller capabilities for distinction
367 * between compatible strings
369 * @max_cs_nb: Number of Chip Select lines available
370 * @max_rb_nb: Number of Ready/Busy lines available
371 * @need_system_controller: Indicates if the SoC needs to have access to the
372 * system controller (ie. to enable the NAND controller)
373 * @legacy_of_bindings: Indicates if DT parsing must be done using the old
375 * @is_nfcv2: NFCv2 has numerous enhancements compared to NFCv1, ie.
376 * BCH error detection and correction algorithm,
377 * NDCB3 register has been added
378 * @use_dma: Use dma for data transfers
379 * @max_mode_number: Maximum timing mode supported by the controller
381 struct marvell_nfc_caps
{
382 unsigned int max_cs_nb
;
383 unsigned int max_rb_nb
;
384 bool need_system_controller
;
385 bool legacy_of_bindings
;
388 unsigned int max_mode_number
;
392 * struct marvell_nfc - stores Marvell NAND controller information
394 * @controller: Base controller structure
395 * @dev: Parent device (used to print error messages)
396 * @regs: NAND controller registers
397 * @core_clk: Core clock
398 * @reg_clk: Registers clock
399 * @complete: Completion object to wait for NAND controller events
400 * @assigned_cs: Bitmask describing already assigned CS lines
401 * @chips: List containing all the NAND chips attached to
402 * this NAND controller
403 * @selected_chip: Currently selected target chip
404 * @caps: NAND controller capabilities for each compatible string
405 * @use_dma: Whetner DMA is used
406 * @dma_chan: DMA channel (NFCv1 only)
407 * @dma_buf: 32-bit aligned buffer for DMA transfers (NFCv1 only)
410 struct nand_controller controller
;
413 struct clk
*core_clk
;
415 struct completion complete
;
416 unsigned long assigned_cs
;
417 struct list_head chips
;
418 struct nand_chip
*selected_chip
;
419 const struct marvell_nfc_caps
*caps
;
421 /* DMA (NFCv1 only) */
423 struct dma_chan
*dma_chan
;
427 static inline struct marvell_nfc
*to_marvell_nfc(struct nand_controller
*ctrl
)
429 return container_of(ctrl
, struct marvell_nfc
, controller
);
433 * struct marvell_nfc_timings - NAND controller timings expressed in NAND
434 * Controller clock cycles
436 * @tRP: ND_nRE pulse width
437 * @tRH: ND_nRE high duration
438 * @tWP: ND_nWE pulse time
439 * @tWH: ND_nWE high duration
440 * @tCS: Enable signal setup time
441 * @tCH: Enable signal hold time
442 * @tADL: Address to write data delay
443 * @tAR: ND_ALE low to ND_nRE low delay
444 * @tWHR: ND_nWE high to ND_nRE low for status read
445 * @tRHW: ND_nRE high duration, read to write delay
446 * @tR: ND_nWE high to ND_nRE low for read
448 struct marvell_nfc_timings
{
465 * TO_CYCLES() - Derives a duration in numbers of clock cycles.
467 * @ps: Duration in pico-seconds
468 * @period_ns: Clock period in nano-seconds
470 * Convert the duration in nano-seconds, then divide by the period and
471 * return the number of clock periods.
473 #define TO_CYCLES(ps, period_ns) (DIV_ROUND_UP(ps / 1000, period_ns))
474 #define TO_CYCLES64(ps, period_ns) (DIV_ROUND_UP_ULL(div_u64(ps, 1000), \
478 * struct marvell_nfc_op - filled during the parsing of the ->exec_op()
479 * subop subset of instructions.
481 * @ndcb: Array of values written to NDCBx registers
482 * @cle_ale_delay_ns: Optional delay after the last CMD or ADDR cycle
483 * @rdy_timeout_ms: Timeout for waits on Ready/Busy pin
484 * @rdy_delay_ns: Optional delay after waiting for the RB pin
485 * @data_delay_ns: Optional delay after the data xfer
486 * @data_instr_idx: Index of the data instruction in the subop
487 * @data_instr: Pointer to the data instruction in the subop
489 struct marvell_nfc_op
{
491 unsigned int cle_ale_delay_ns
;
492 unsigned int rdy_timeout_ms
;
493 unsigned int rdy_delay_ns
;
494 unsigned int data_delay_ns
;
495 unsigned int data_instr_idx
;
496 const struct nand_op_instr
*data_instr
;
500 * Internal helper to conditionnally apply a delay (from the above structure,
503 static void cond_delay(unsigned int ns
)
511 udelay(DIV_ROUND_UP(ns
, 1000));
515 * The controller has many flags that could generate interrupts, most of them
516 * are disabled and polling is used. For the very slow signals, using interrupts
517 * may relax the CPU charge.
519 static void marvell_nfc_disable_int(struct marvell_nfc
*nfc
, u32 int_mask
)
523 /* Writing 1 disables the interrupt */
524 reg
= readl_relaxed(nfc
->regs
+ NDCR
);
525 writel_relaxed(reg
| int_mask
, nfc
->regs
+ NDCR
);
528 static void marvell_nfc_enable_int(struct marvell_nfc
*nfc
, u32 int_mask
)
532 /* Writing 0 enables the interrupt */
533 reg
= readl_relaxed(nfc
->regs
+ NDCR
);
534 writel_relaxed(reg
& ~int_mask
, nfc
->regs
+ NDCR
);
537 static u32
marvell_nfc_clear_int(struct marvell_nfc
*nfc
, u32 int_mask
)
541 reg
= readl_relaxed(nfc
->regs
+ NDSR
);
542 writel_relaxed(int_mask
, nfc
->regs
+ NDSR
);
544 return reg
& int_mask
;
547 static void marvell_nfc_force_byte_access(struct nand_chip
*chip
,
550 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
554 * Callers of this function do not verify if the NAND is using a 16-bit
555 * an 8-bit bus for normal operations, so we need to take care of that
556 * here by leaving the configuration unchanged if the NAND does not have
557 * the NAND_BUSWIDTH_16 flag set.
559 if (!(chip
->options
& NAND_BUSWIDTH_16
))
562 ndcr
= readl_relaxed(nfc
->regs
+ NDCR
);
565 ndcr
&= ~(NDCR_DWIDTH_M
| NDCR_DWIDTH_C
);
567 ndcr
|= NDCR_DWIDTH_M
| NDCR_DWIDTH_C
;
569 writel_relaxed(ndcr
, nfc
->regs
+ NDCR
);
572 static int marvell_nfc_wait_ndrun(struct nand_chip
*chip
)
574 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
579 * The command is being processed, wait for the ND_RUN bit to be
580 * cleared by the NFC. If not, we must clear it by hand.
582 ret
= readl_relaxed_poll_timeout(nfc
->regs
+ NDCR
, val
,
583 (val
& NDCR_ND_RUN
) == 0,
584 POLL_PERIOD
, POLL_TIMEOUT
);
586 dev_err(nfc
->dev
, "Timeout on NAND controller run mode\n");
587 writel_relaxed(readl(nfc
->regs
+ NDCR
) & ~NDCR_ND_RUN
,
596 * Any time a command has to be sent to the controller, the following sequence
597 * has to be followed:
598 * - call marvell_nfc_prepare_cmd()
599 * -> activate the ND_RUN bit that will kind of 'start a job'
600 * -> wait the signal indicating the NFC is waiting for a command
601 * - send the command (cmd and address cycles)
602 * - enventually send or receive the data
603 * - call marvell_nfc_end_cmd() with the corresponding flag
604 * -> wait the flag to be triggered or cancel the job with a timeout
606 * The following helpers are here to factorize the code a bit so that
607 * specialized functions responsible for executing the actual NAND
608 * operations do not have to replicate the same code blocks.
610 static int marvell_nfc_prepare_cmd(struct nand_chip
*chip
)
612 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
616 /* Poll ND_RUN and clear NDSR before issuing any command */
617 ret
= marvell_nfc_wait_ndrun(chip
);
619 dev_err(nfc
->dev
, "Last operation did not succeed\n");
623 ndcr
= readl_relaxed(nfc
->regs
+ NDCR
);
624 writel_relaxed(readl(nfc
->regs
+ NDSR
), nfc
->regs
+ NDSR
);
626 /* Assert ND_RUN bit and wait the NFC to be ready */
627 writel_relaxed(ndcr
| NDCR_ND_RUN
, nfc
->regs
+ NDCR
);
628 ret
= readl_relaxed_poll_timeout(nfc
->regs
+ NDSR
, val
,
630 POLL_PERIOD
, POLL_TIMEOUT
);
632 dev_err(nfc
->dev
, "Timeout on WRCMDRE\n");
636 /* Command may be written, clear WRCMDREQ status bit */
637 writel_relaxed(NDSR_WRCMDREQ
, nfc
->regs
+ NDSR
);
642 static void marvell_nfc_send_cmd(struct nand_chip
*chip
,
643 struct marvell_nfc_op
*nfc_op
)
645 struct marvell_nand_chip
*marvell_nand
= to_marvell_nand(chip
);
646 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
648 dev_dbg(nfc
->dev
, "\nNDCR: 0x%08x\n"
649 "NDCB0: 0x%08x\nNDCB1: 0x%08x\nNDCB2: 0x%08x\nNDCB3: 0x%08x\n",
650 (u32
)readl_relaxed(nfc
->regs
+ NDCR
), nfc_op
->ndcb
[0],
651 nfc_op
->ndcb
[1], nfc_op
->ndcb
[2], nfc_op
->ndcb
[3]);
653 writel_relaxed(to_nand_sel(marvell_nand
)->ndcb0_csel
| nfc_op
->ndcb
[0],
655 writel_relaxed(nfc_op
->ndcb
[1], nfc
->regs
+ NDCB0
);
656 writel(nfc_op
->ndcb
[2], nfc
->regs
+ NDCB0
);
659 * Write NDCB0 four times only if LEN_OVRD is set or if ADDR6 or ADDR7
660 * fields are used (only available on NFCv2).
662 if (nfc_op
->ndcb
[0] & NDCB0_LEN_OVRD
||
663 NDCB0_ADDR_GET_NUM_CYC(nfc_op
->ndcb
[0]) >= 6) {
664 if (!WARN_ON_ONCE(!nfc
->caps
->is_nfcv2
))
665 writel(nfc_op
->ndcb
[3], nfc
->regs
+ NDCB0
);
669 static int marvell_nfc_end_cmd(struct nand_chip
*chip
, int flag
,
672 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
676 ret
= readl_relaxed_poll_timeout(nfc
->regs
+ NDSR
, val
,
678 POLL_PERIOD
, POLL_TIMEOUT
);
681 dev_err(nfc
->dev
, "Timeout on %s (NDSR: 0x%08x)\n",
684 dmaengine_terminate_all(nfc
->dma_chan
);
689 * DMA function uses this helper to poll on CMDD bits without wanting
690 * them to be cleared.
692 if (nfc
->use_dma
&& (readl_relaxed(nfc
->regs
+ NDCR
) & NDCR_DMA_EN
))
695 writel_relaxed(flag
, nfc
->regs
+ NDSR
);
700 static int marvell_nfc_wait_cmdd(struct nand_chip
*chip
)
702 struct marvell_nand_chip
*marvell_nand
= to_marvell_nand(chip
);
703 int cs_flag
= NDSR_CMDD(to_nand_sel(marvell_nand
)->ndcb0_csel
);
705 return marvell_nfc_end_cmd(chip
, cs_flag
, "CMDD");
708 static int marvell_nfc_poll_status(struct marvell_nfc
*nfc
, u32 mask
,
709 u32 expected_val
, unsigned long timeout_ms
)
714 limit
= jiffies
+ msecs_to_jiffies(timeout_ms
);
716 st
= readl_relaxed(nfc
->regs
+ NDSR
);
717 if (st
& NDSR_RDY(1))
720 if ((st
& mask
) == expected_val
)
724 } while (time_after(limit
, jiffies
));
729 static int marvell_nfc_wait_op(struct nand_chip
*chip
, unsigned int timeout_ms
)
731 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
732 struct mtd_info
*mtd
= nand_to_mtd(chip
);
736 /* Timeout is expressed in ms */
738 timeout_ms
= IRQ_TIMEOUT
;
740 if (mtd
->oops_panic_write
) {
741 ret
= marvell_nfc_poll_status(nfc
, NDSR_RDY(0),
745 init_completion(&nfc
->complete
);
747 marvell_nfc_enable_int(nfc
, NDCR_RDYM
);
748 ret
= wait_for_completion_timeout(&nfc
->complete
,
749 msecs_to_jiffies(timeout_ms
));
750 marvell_nfc_disable_int(nfc
, NDCR_RDYM
);
752 pending
= marvell_nfc_clear_int(nfc
, NDSR_RDY(0) | NDSR_RDY(1));
755 * In case the interrupt was not served in the required time frame,
756 * check if the ISR was not served or if something went actually wrong.
758 if (!ret
&& !pending
) {
759 dev_err(nfc
->dev
, "Timeout waiting for RB signal\n");
766 static void marvell_nfc_select_target(struct nand_chip
*chip
,
769 struct marvell_nand_chip
*marvell_nand
= to_marvell_nand(chip
);
770 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
774 * Reset the NDCR register to a clean state for this particular chip,
775 * also clear ND_RUN bit.
777 ndcr_generic
= readl_relaxed(nfc
->regs
+ NDCR
) &
778 NDCR_GENERIC_FIELDS_MASK
& ~NDCR_ND_RUN
;
779 writel_relaxed(ndcr_generic
| marvell_nand
->ndcr
, nfc
->regs
+ NDCR
);
781 /* Also reset the interrupt status register */
782 marvell_nfc_clear_int(nfc
, NDCR_ALL_INT
);
784 if (chip
== nfc
->selected_chip
&& die_nr
== marvell_nand
->selected_die
)
787 writel_relaxed(marvell_nand
->ndtr0
, nfc
->regs
+ NDTR0
);
788 writel_relaxed(marvell_nand
->ndtr1
, nfc
->regs
+ NDTR1
);
790 nfc
->selected_chip
= chip
;
791 marvell_nand
->selected_die
= die_nr
;
794 static irqreturn_t
marvell_nfc_isr(int irq
, void *dev_id
)
796 struct marvell_nfc
*nfc
= dev_id
;
797 u32 st
= readl_relaxed(nfc
->regs
+ NDSR
);
798 u32 ien
= (~readl_relaxed(nfc
->regs
+ NDCR
)) & NDCR_ALL_INT
;
801 * RDY interrupt mask is one bit in NDCR while there are two status
802 * bit in NDSR (RDY[cs0/cs2] and RDY[cs1/cs3]).
804 if (st
& NDSR_RDY(1))
810 marvell_nfc_disable_int(nfc
, st
& NDCR_ALL_INT
);
812 if (st
& (NDSR_RDY(0) | NDSR_RDY(1)))
813 complete(&nfc
->complete
);
818 /* HW ECC related functions */
819 static void marvell_nfc_enable_hw_ecc(struct nand_chip
*chip
)
821 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
822 u32 ndcr
= readl_relaxed(nfc
->regs
+ NDCR
);
824 if (!(ndcr
& NDCR_ECC_EN
)) {
825 writel_relaxed(ndcr
| NDCR_ECC_EN
, nfc
->regs
+ NDCR
);
828 * When enabling BCH, set threshold to 0 to always know the
829 * number of corrected bitflips.
831 if (chip
->ecc
.algo
== NAND_ECC_ALGO_BCH
)
832 writel_relaxed(NDECCCTRL_BCH_EN
, nfc
->regs
+ NDECCCTRL
);
836 static void marvell_nfc_disable_hw_ecc(struct nand_chip
*chip
)
838 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
839 u32 ndcr
= readl_relaxed(nfc
->regs
+ NDCR
);
841 if (ndcr
& NDCR_ECC_EN
) {
842 writel_relaxed(ndcr
& ~NDCR_ECC_EN
, nfc
->regs
+ NDCR
);
843 if (chip
->ecc
.algo
== NAND_ECC_ALGO_BCH
)
844 writel_relaxed(0, nfc
->regs
+ NDECCCTRL
);
848 /* DMA related helpers */
849 static void marvell_nfc_enable_dma(struct marvell_nfc
*nfc
)
853 reg
= readl_relaxed(nfc
->regs
+ NDCR
);
854 writel_relaxed(reg
| NDCR_DMA_EN
, nfc
->regs
+ NDCR
);
857 static void marvell_nfc_disable_dma(struct marvell_nfc
*nfc
)
861 reg
= readl_relaxed(nfc
->regs
+ NDCR
);
862 writel_relaxed(reg
& ~NDCR_DMA_EN
, nfc
->regs
+ NDCR
);
865 /* Read/write PIO/DMA accessors */
866 static int marvell_nfc_xfer_data_dma(struct marvell_nfc
*nfc
,
867 enum dma_data_direction direction
,
870 unsigned int dma_len
= min_t(int, ALIGN(len
, 32), MAX_CHUNK_SIZE
);
871 struct dma_async_tx_descriptor
*tx
;
872 struct scatterlist sg
;
876 marvell_nfc_enable_dma(nfc
);
877 /* Prepare the DMA transfer */
878 sg_init_one(&sg
, nfc
->dma_buf
, dma_len
);
879 ret
= dma_map_sg(nfc
->dma_chan
->device
->dev
, &sg
, 1, direction
);
881 dev_err(nfc
->dev
, "Could not map DMA S/G list\n");
885 tx
= dmaengine_prep_slave_sg(nfc
->dma_chan
, &sg
, 1,
886 direction
== DMA_FROM_DEVICE
?
887 DMA_DEV_TO_MEM
: DMA_MEM_TO_DEV
,
890 dev_err(nfc
->dev
, "Could not prepare DMA S/G list\n");
891 dma_unmap_sg(nfc
->dma_chan
->device
->dev
, &sg
, 1, direction
);
895 /* Do the task and wait for it to finish */
896 cookie
= dmaengine_submit(tx
);
897 ret
= dma_submit_error(cookie
);
901 dma_async_issue_pending(nfc
->dma_chan
);
902 ret
= marvell_nfc_wait_cmdd(nfc
->selected_chip
);
903 dma_unmap_sg(nfc
->dma_chan
->device
->dev
, &sg
, 1, direction
);
904 marvell_nfc_disable_dma(nfc
);
906 dev_err(nfc
->dev
, "Timeout waiting for DMA (status: %d)\n",
907 dmaengine_tx_status(nfc
->dma_chan
, cookie
, NULL
));
908 dmaengine_terminate_all(nfc
->dma_chan
);
915 static int marvell_nfc_xfer_data_in_pio(struct marvell_nfc
*nfc
, u8
*in
,
918 unsigned int last_len
= len
% FIFO_DEPTH
;
919 unsigned int last_full_offset
= round_down(len
, FIFO_DEPTH
);
922 for (i
= 0; i
< last_full_offset
; i
+= FIFO_DEPTH
)
923 ioread32_rep(nfc
->regs
+ NDDB
, in
+ i
, FIFO_REP(FIFO_DEPTH
));
926 u8 tmp_buf
[FIFO_DEPTH
];
928 ioread32_rep(nfc
->regs
+ NDDB
, tmp_buf
, FIFO_REP(FIFO_DEPTH
));
929 memcpy(in
+ last_full_offset
, tmp_buf
, last_len
);
935 static int marvell_nfc_xfer_data_out_pio(struct marvell_nfc
*nfc
, const u8
*out
,
938 unsigned int last_len
= len
% FIFO_DEPTH
;
939 unsigned int last_full_offset
= round_down(len
, FIFO_DEPTH
);
942 for (i
= 0; i
< last_full_offset
; i
+= FIFO_DEPTH
)
943 iowrite32_rep(nfc
->regs
+ NDDB
, out
+ i
, FIFO_REP(FIFO_DEPTH
));
946 u8 tmp_buf
[FIFO_DEPTH
];
948 memcpy(tmp_buf
, out
+ last_full_offset
, last_len
);
949 iowrite32_rep(nfc
->regs
+ NDDB
, tmp_buf
, FIFO_REP(FIFO_DEPTH
));
955 static void marvell_nfc_check_empty_chunk(struct nand_chip
*chip
,
956 u8
*data
, int data_len
,
957 u8
*spare
, int spare_len
,
958 u8
*ecc
, int ecc_len
,
959 unsigned int *max_bitflips
)
961 struct mtd_info
*mtd
= nand_to_mtd(chip
);
965 * Blank pages (all 0xFF) that have not been written may be recognized
966 * as bad if bitflips occur, so whenever an uncorrectable error occurs,
967 * check if the entire page (with ECC bytes) is actually blank or not.
976 bf
= nand_check_erased_ecc_chunk(data
, data_len
, ecc
, ecc_len
,
977 spare
, spare_len
, chip
->ecc
.strength
);
979 mtd
->ecc_stats
.failed
++;
983 /* Update the stats and max_bitflips */
984 mtd
->ecc_stats
.corrected
+= bf
;
985 *max_bitflips
= max_t(unsigned int, *max_bitflips
, bf
);
989 * Check if a chunk is correct or not according to the hardware ECC engine.
990 * mtd->ecc_stats.corrected is updated, as well as max_bitflips, however
991 * mtd->ecc_stats.failure is not, the function will instead return a non-zero
992 * value indicating that a check on the emptyness of the subpage must be
993 * performed before actually declaring the subpage as "corrupted".
995 static int marvell_nfc_hw_ecc_check_bitflips(struct nand_chip
*chip
,
996 unsigned int *max_bitflips
)
998 struct mtd_info
*mtd
= nand_to_mtd(chip
);
999 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
1003 ndsr
= readl_relaxed(nfc
->regs
+ NDSR
);
1005 /* Check uncorrectable error flag */
1006 if (ndsr
& NDSR_UNCERR
) {
1007 writel_relaxed(ndsr
, nfc
->regs
+ NDSR
);
1010 * Do not increment ->ecc_stats.failed now, instead, return a
1011 * non-zero value to indicate that this chunk was apparently
1012 * bad, and it should be check to see if it empty or not. If
1013 * the chunk (with ECC bytes) is not declared empty, the calling
1014 * function must increment the failure count.
1019 /* Check correctable error flag */
1020 if (ndsr
& NDSR_CORERR
) {
1021 writel_relaxed(ndsr
, nfc
->regs
+ NDSR
);
1023 if (chip
->ecc
.algo
== NAND_ECC_ALGO_BCH
)
1024 bf
= NDSR_ERRCNT(ndsr
);
1029 /* Update the stats and max_bitflips */
1030 mtd
->ecc_stats
.corrected
+= bf
;
1031 *max_bitflips
= max_t(unsigned int, *max_bitflips
, bf
);
1036 /* Hamming read helpers */
1037 static int marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip
*chip
,
1038 u8
*data_buf
, u8
*oob_buf
,
1041 struct marvell_nand_chip
*marvell_nand
= to_marvell_nand(chip
);
1042 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
1043 const struct marvell_hw_ecc_layout
*lt
= to_marvell_nand(chip
)->layout
;
1044 struct marvell_nfc_op nfc_op
= {
1045 .ndcb
[0] = NDCB0_CMD_TYPE(TYPE_READ
) |
1046 NDCB0_ADDR_CYC(marvell_nand
->addr_cyc
) |
1048 NDCB0_CMD1(NAND_CMD_READ0
) |
1049 NDCB0_CMD2(NAND_CMD_READSTART
),
1050 .ndcb
[1] = NDCB1_ADDRS_PAGE(page
),
1051 .ndcb
[2] = NDCB2_ADDR5_PAGE(page
),
1053 unsigned int oob_bytes
= lt
->spare_bytes
+ (raw
? lt
->ecc_bytes
: 0);
1056 /* NFCv2 needs more information about the operation being executed */
1057 if (nfc
->caps
->is_nfcv2
)
1058 nfc_op
.ndcb
[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW
);
1060 ret
= marvell_nfc_prepare_cmd(chip
);
1064 marvell_nfc_send_cmd(chip
, &nfc_op
);
1065 ret
= marvell_nfc_end_cmd(chip
, NDSR_RDDREQ
,
1066 "RDDREQ while draining FIFO (data/oob)");
1071 * Read the page then the OOB area. Unlike what is shown in current
1072 * documentation, spare bytes are protected by the ECC engine, and must
1073 * be at the beginning of the OOB area or running this driver on legacy
1074 * systems will prevent the discovery of the BBM/BBT.
1077 marvell_nfc_xfer_data_dma(nfc
, DMA_FROM_DEVICE
,
1078 lt
->data_bytes
+ oob_bytes
);
1079 memcpy(data_buf
, nfc
->dma_buf
, lt
->data_bytes
);
1080 memcpy(oob_buf
, nfc
->dma_buf
+ lt
->data_bytes
, oob_bytes
);
1082 marvell_nfc_xfer_data_in_pio(nfc
, data_buf
, lt
->data_bytes
);
1083 marvell_nfc_xfer_data_in_pio(nfc
, oob_buf
, oob_bytes
);
1086 ret
= marvell_nfc_wait_cmdd(chip
);
1090 static int marvell_nfc_hw_ecc_hmg_read_page_raw(struct nand_chip
*chip
, u8
*buf
,
1091 int oob_required
, int page
)
1093 marvell_nfc_select_target(chip
, chip
->cur_cs
);
1094 return marvell_nfc_hw_ecc_hmg_do_read_page(chip
, buf
, chip
->oob_poi
,
1098 static int marvell_nfc_hw_ecc_hmg_read_page(struct nand_chip
*chip
, u8
*buf
,
1099 int oob_required
, int page
)
1101 const struct marvell_hw_ecc_layout
*lt
= to_marvell_nand(chip
)->layout
;
1102 unsigned int full_sz
= lt
->data_bytes
+ lt
->spare_bytes
+ lt
->ecc_bytes
;
1103 int max_bitflips
= 0, ret
;
1106 marvell_nfc_select_target(chip
, chip
->cur_cs
);
1107 marvell_nfc_enable_hw_ecc(chip
);
1108 marvell_nfc_hw_ecc_hmg_do_read_page(chip
, buf
, chip
->oob_poi
, false,
1110 ret
= marvell_nfc_hw_ecc_check_bitflips(chip
, &max_bitflips
);
1111 marvell_nfc_disable_hw_ecc(chip
);
1114 return max_bitflips
;
1117 * When ECC failures are detected, check if the full page has been
1118 * written or not. Ignore the failure if it is actually empty.
1120 raw_buf
= kmalloc(full_sz
, GFP_KERNEL
);
1124 marvell_nfc_hw_ecc_hmg_do_read_page(chip
, raw_buf
, raw_buf
+
1125 lt
->data_bytes
, true, page
);
1126 marvell_nfc_check_empty_chunk(chip
, raw_buf
, full_sz
, NULL
, 0, NULL
, 0,
1130 return max_bitflips
;
1134 * Spare area in Hamming layouts is not protected by the ECC engine (even if
1135 * it appears before the ECC bytes when reading), the ->read_oob_raw() function
1136 * also stands for ->read_oob().
1138 static int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct nand_chip
*chip
, int page
)
1140 u8
*buf
= nand_get_data_buf(chip
);
1142 marvell_nfc_select_target(chip
, chip
->cur_cs
);
1143 return marvell_nfc_hw_ecc_hmg_do_read_page(chip
, buf
, chip
->oob_poi
,
1147 /* Hamming write helpers */
1148 static int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip
*chip
,
1150 const u8
*oob_buf
, bool raw
,
1153 const struct nand_sdr_timings
*sdr
=
1154 nand_get_sdr_timings(nand_get_interface_config(chip
));
1155 struct marvell_nand_chip
*marvell_nand
= to_marvell_nand(chip
);
1156 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
1157 const struct marvell_hw_ecc_layout
*lt
= to_marvell_nand(chip
)->layout
;
1158 struct marvell_nfc_op nfc_op
= {
1159 .ndcb
[0] = NDCB0_CMD_TYPE(TYPE_WRITE
) |
1160 NDCB0_ADDR_CYC(marvell_nand
->addr_cyc
) |
1161 NDCB0_CMD1(NAND_CMD_SEQIN
) |
1162 NDCB0_CMD2(NAND_CMD_PAGEPROG
) |
1164 .ndcb
[1] = NDCB1_ADDRS_PAGE(page
),
1165 .ndcb
[2] = NDCB2_ADDR5_PAGE(page
),
1167 unsigned int oob_bytes
= lt
->spare_bytes
+ (raw
? lt
->ecc_bytes
: 0);
1171 /* NFCv2 needs more information about the operation being executed */
1172 if (nfc
->caps
->is_nfcv2
)
1173 nfc_op
.ndcb
[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW
);
1175 ret
= marvell_nfc_prepare_cmd(chip
);
1179 marvell_nfc_send_cmd(chip
, &nfc_op
);
1180 ret
= marvell_nfc_end_cmd(chip
, NDSR_WRDREQ
,
1181 "WRDREQ while loading FIFO (data)");
1185 /* Write the page then the OOB area */
1187 memcpy(nfc
->dma_buf
, data_buf
, lt
->data_bytes
);
1188 memcpy(nfc
->dma_buf
+ lt
->data_bytes
, oob_buf
, oob_bytes
);
1189 marvell_nfc_xfer_data_dma(nfc
, DMA_TO_DEVICE
, lt
->data_bytes
+
1190 lt
->ecc_bytes
+ lt
->spare_bytes
);
1192 marvell_nfc_xfer_data_out_pio(nfc
, data_buf
, lt
->data_bytes
);
1193 marvell_nfc_xfer_data_out_pio(nfc
, oob_buf
, oob_bytes
);
1196 ret
= marvell_nfc_wait_cmdd(chip
);
1200 ret
= marvell_nfc_wait_op(chip
,
1201 PSEC_TO_MSEC(sdr
->tPROG_max
));
1205 /* Check write status on the chip side */
1206 ret
= nand_status_op(chip
, &status
);
1210 if (status
& NAND_STATUS_FAIL
)
1216 static int marvell_nfc_hw_ecc_hmg_write_page_raw(struct nand_chip
*chip
,
1218 int oob_required
, int page
)
1220 marvell_nfc_select_target(chip
, chip
->cur_cs
);
1221 return marvell_nfc_hw_ecc_hmg_do_write_page(chip
, buf
, chip
->oob_poi
,
1225 static int marvell_nfc_hw_ecc_hmg_write_page(struct nand_chip
*chip
,
1227 int oob_required
, int page
)
1231 marvell_nfc_select_target(chip
, chip
->cur_cs
);
1232 marvell_nfc_enable_hw_ecc(chip
);
1233 ret
= marvell_nfc_hw_ecc_hmg_do_write_page(chip
, buf
, chip
->oob_poi
,
1235 marvell_nfc_disable_hw_ecc(chip
);
1241 * Spare area in Hamming layouts is not protected by the ECC engine (even if
1242 * it appears before the ECC bytes when reading), the ->write_oob_raw() function
1243 * also stands for ->write_oob().
1245 static int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct nand_chip
*chip
,
1248 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1249 u8
*buf
= nand_get_data_buf(chip
);
1251 memset(buf
, 0xFF, mtd
->writesize
);
1253 marvell_nfc_select_target(chip
, chip
->cur_cs
);
1254 return marvell_nfc_hw_ecc_hmg_do_write_page(chip
, buf
, chip
->oob_poi
,
1258 /* BCH read helpers */
1259 static int marvell_nfc_hw_ecc_bch_read_page_raw(struct nand_chip
*chip
, u8
*buf
,
1260 int oob_required
, int page
)
1262 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1263 const struct marvell_hw_ecc_layout
*lt
= to_marvell_nand(chip
)->layout
;
1264 u8
*oob
= chip
->oob_poi
;
1265 int chunk_size
= lt
->data_bytes
+ lt
->spare_bytes
+ lt
->ecc_bytes
;
1266 int ecc_offset
= (lt
->full_chunk_cnt
* lt
->spare_bytes
) +
1267 lt
->last_spare_bytes
;
1268 int data_len
= lt
->data_bytes
;
1269 int spare_len
= lt
->spare_bytes
;
1270 int ecc_len
= lt
->ecc_bytes
;
1273 marvell_nfc_select_target(chip
, chip
->cur_cs
);
1276 memset(chip
->oob_poi
, 0xFF, mtd
->oobsize
);
1278 nand_read_page_op(chip
, page
, 0, NULL
, 0);
1280 for (chunk
= 0; chunk
< lt
->nchunks
; chunk
++) {
1281 /* Update last chunk length */
1282 if (chunk
>= lt
->full_chunk_cnt
) {
1283 data_len
= lt
->last_data_bytes
;
1284 spare_len
= lt
->last_spare_bytes
;
1285 ecc_len
= lt
->last_ecc_bytes
;
1288 /* Read data bytes*/
1289 nand_change_read_column_op(chip
, chunk
* chunk_size
,
1290 buf
+ (lt
->data_bytes
* chunk
),
1293 /* Read spare bytes */
1294 nand_read_data_op(chip
, oob
+ (lt
->spare_bytes
* chunk
),
1295 spare_len
, false, false);
1297 /* Read ECC bytes */
1298 nand_read_data_op(chip
, oob
+ ecc_offset
+
1299 (ALIGN(lt
->ecc_bytes
, 32) * chunk
),
1300 ecc_len
, false, false);
1306 static void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip
*chip
, int chunk
,
1307 u8
*data
, unsigned int data_len
,
1308 u8
*spare
, unsigned int spare_len
,
1311 struct marvell_nand_chip
*marvell_nand
= to_marvell_nand(chip
);
1312 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
1313 const struct marvell_hw_ecc_layout
*lt
= to_marvell_nand(chip
)->layout
;
1315 struct marvell_nfc_op nfc_op
= {
1316 .ndcb
[0] = NDCB0_CMD_TYPE(TYPE_READ
) |
1317 NDCB0_ADDR_CYC(marvell_nand
->addr_cyc
) |
1319 .ndcb
[1] = NDCB1_ADDRS_PAGE(page
),
1320 .ndcb
[2] = NDCB2_ADDR5_PAGE(page
),
1321 .ndcb
[3] = data_len
+ spare_len
,
1324 ret
= marvell_nfc_prepare_cmd(chip
);
1329 nfc_op
.ndcb
[0] |= NDCB0_DBC
|
1330 NDCB0_CMD1(NAND_CMD_READ0
) |
1331 NDCB0_CMD2(NAND_CMD_READSTART
);
1334 * Trigger the monolithic read on the first chunk, then naked read on
1335 * intermediate chunks and finally a last naked read on the last chunk.
1338 nfc_op
.ndcb
[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW
);
1339 else if (chunk
< lt
->nchunks
- 1)
1340 nfc_op
.ndcb
[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW
);
1342 nfc_op
.ndcb
[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW
);
1344 marvell_nfc_send_cmd(chip
, &nfc_op
);
1347 * According to the datasheet, when reading from NDDB
1348 * with BCH enabled, after each 32 bytes reads, we
1349 * have to make sure that the NDSR.RDDREQ bit is set.
1351 * Drain the FIFO, 8 32-bit reads at a time, and skip
1352 * the polling on the last read.
1354 * Length is a multiple of 32 bytes, hence it is a multiple of 8 too.
1356 for (i
= 0; i
< data_len
; i
+= FIFO_DEPTH
* BCH_SEQ_READS
) {
1357 marvell_nfc_end_cmd(chip
, NDSR_RDDREQ
,
1358 "RDDREQ while draining FIFO (data)");
1359 marvell_nfc_xfer_data_in_pio(nfc
, data
,
1360 FIFO_DEPTH
* BCH_SEQ_READS
);
1361 data
+= FIFO_DEPTH
* BCH_SEQ_READS
;
1364 for (i
= 0; i
< spare_len
; i
+= FIFO_DEPTH
* BCH_SEQ_READS
) {
1365 marvell_nfc_end_cmd(chip
, NDSR_RDDREQ
,
1366 "RDDREQ while draining FIFO (OOB)");
1367 marvell_nfc_xfer_data_in_pio(nfc
, spare
,
1368 FIFO_DEPTH
* BCH_SEQ_READS
);
1369 spare
+= FIFO_DEPTH
* BCH_SEQ_READS
;
1373 static int marvell_nfc_hw_ecc_bch_read_page(struct nand_chip
*chip
,
1374 u8
*buf
, int oob_required
,
1377 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1378 const struct marvell_hw_ecc_layout
*lt
= to_marvell_nand(chip
)->layout
;
1379 int data_len
= lt
->data_bytes
, spare_len
= lt
->spare_bytes
;
1380 u8
*data
= buf
, *spare
= chip
->oob_poi
;
1381 int max_bitflips
= 0;
1382 u32 failure_mask
= 0;
1385 marvell_nfc_select_target(chip
, chip
->cur_cs
);
1388 * With BCH, OOB is not fully used (and thus not read entirely), not
1389 * expected bytes could show up at the end of the OOB buffer if not
1390 * explicitly erased.
1393 memset(chip
->oob_poi
, 0xFF, mtd
->oobsize
);
1395 marvell_nfc_enable_hw_ecc(chip
);
1397 for (chunk
= 0; chunk
< lt
->nchunks
; chunk
++) {
1398 /* Update length for the last chunk */
1399 if (chunk
>= lt
->full_chunk_cnt
) {
1400 data_len
= lt
->last_data_bytes
;
1401 spare_len
= lt
->last_spare_bytes
;
1404 /* Read the chunk and detect number of bitflips */
1405 marvell_nfc_hw_ecc_bch_read_chunk(chip
, chunk
, data
, data_len
,
1406 spare
, spare_len
, page
);
1407 ret
= marvell_nfc_hw_ecc_check_bitflips(chip
, &max_bitflips
);
1409 failure_mask
|= BIT(chunk
);
1415 marvell_nfc_disable_hw_ecc(chip
);
1418 return max_bitflips
;
1421 * Please note that dumping the ECC bytes during a normal read with OOB
1422 * area would add a significant overhead as ECC bytes are "consumed" by
1423 * the controller in normal mode and must be re-read in raw mode. To
1424 * avoid dropping the performances, we prefer not to include them. The
1425 * user should re-read the page in raw mode if ECC bytes are required.
1429 * In case there is any subpage read error, we usually re-read only ECC
1430 * bytes in raw mode and check if the whole page is empty. In this case,
1431 * it is normal that the ECC check failed and we just ignore the error.
1433 * However, it has been empirically observed that for some layouts (e.g
1434 * 2k page, 8b strength per 512B chunk), the controller tries to correct
1435 * bits and may create itself bitflips in the erased area. To overcome
1436 * this strange behavior, the whole page is re-read in raw mode, not
1437 * only the ECC bytes.
1439 for (chunk
= 0; chunk
< lt
->nchunks
; chunk
++) {
1440 int data_off_in_page
, spare_off_in_page
, ecc_off_in_page
;
1441 int data_off
, spare_off
, ecc_off
;
1442 int data_len
, spare_len
, ecc_len
;
1444 /* No failure reported for this chunk, move to the next one */
1445 if (!(failure_mask
& BIT(chunk
)))
1448 data_off_in_page
= chunk
* (lt
->data_bytes
+ lt
->spare_bytes
+
1450 spare_off_in_page
= data_off_in_page
+
1451 (chunk
< lt
->full_chunk_cnt
? lt
->data_bytes
:
1452 lt
->last_data_bytes
);
1453 ecc_off_in_page
= spare_off_in_page
+
1454 (chunk
< lt
->full_chunk_cnt
? lt
->spare_bytes
:
1455 lt
->last_spare_bytes
);
1457 data_off
= chunk
* lt
->data_bytes
;
1458 spare_off
= chunk
* lt
->spare_bytes
;
1459 ecc_off
= (lt
->full_chunk_cnt
* lt
->spare_bytes
) +
1460 lt
->last_spare_bytes
+
1461 (chunk
* (lt
->ecc_bytes
+ 2));
1463 data_len
= chunk
< lt
->full_chunk_cnt
? lt
->data_bytes
:
1464 lt
->last_data_bytes
;
1465 spare_len
= chunk
< lt
->full_chunk_cnt
? lt
->spare_bytes
:
1466 lt
->last_spare_bytes
;
1467 ecc_len
= chunk
< lt
->full_chunk_cnt
? lt
->ecc_bytes
:
1471 * Only re-read the ECC bytes, unless we are using the 2k/8b
1472 * layout which is buggy in the sense that the ECC engine will
1473 * try to correct data bytes anyway, creating bitflips. In this
1474 * case, re-read the entire page.
1476 if (lt
->writesize
== 2048 && lt
->strength
== 8) {
1477 nand_change_read_column_op(chip
, data_off_in_page
,
1478 buf
+ data_off
, data_len
,
1480 nand_change_read_column_op(chip
, spare_off_in_page
,
1481 chip
->oob_poi
+ spare_off
, spare_len
,
1485 nand_change_read_column_op(chip
, ecc_off_in_page
,
1486 chip
->oob_poi
+ ecc_off
, ecc_len
,
1489 /* Check the entire chunk (data + spare + ecc) for emptyness */
1490 marvell_nfc_check_empty_chunk(chip
, buf
+ data_off
, data_len
,
1491 chip
->oob_poi
+ spare_off
, spare_len
,
1492 chip
->oob_poi
+ ecc_off
, ecc_len
,
1496 return max_bitflips
;
1499 static int marvell_nfc_hw_ecc_bch_read_oob_raw(struct nand_chip
*chip
, int page
)
1501 u8
*buf
= nand_get_data_buf(chip
);
1503 return chip
->ecc
.read_page_raw(chip
, buf
, true, page
);
1506 static int marvell_nfc_hw_ecc_bch_read_oob(struct nand_chip
*chip
, int page
)
1508 u8
*buf
= nand_get_data_buf(chip
);
1510 return chip
->ecc
.read_page(chip
, buf
, true, page
);
1513 /* BCH write helpers */
1514 static int marvell_nfc_hw_ecc_bch_write_page_raw(struct nand_chip
*chip
,
1516 int oob_required
, int page
)
1518 const struct marvell_hw_ecc_layout
*lt
= to_marvell_nand(chip
)->layout
;
1519 int full_chunk_size
= lt
->data_bytes
+ lt
->spare_bytes
+ lt
->ecc_bytes
;
1520 int data_len
= lt
->data_bytes
;
1521 int spare_len
= lt
->spare_bytes
;
1522 int ecc_len
= lt
->ecc_bytes
;
1523 int spare_offset
= 0;
1524 int ecc_offset
= (lt
->full_chunk_cnt
* lt
->spare_bytes
) +
1525 lt
->last_spare_bytes
;
1528 marvell_nfc_select_target(chip
, chip
->cur_cs
);
1530 nand_prog_page_begin_op(chip
, page
, 0, NULL
, 0);
1532 for (chunk
= 0; chunk
< lt
->nchunks
; chunk
++) {
1533 if (chunk
>= lt
->full_chunk_cnt
) {
1534 data_len
= lt
->last_data_bytes
;
1535 spare_len
= lt
->last_spare_bytes
;
1536 ecc_len
= lt
->last_ecc_bytes
;
1539 /* Point to the column of the next chunk */
1540 nand_change_write_column_op(chip
, chunk
* full_chunk_size
,
1543 /* Write the data */
1544 nand_write_data_op(chip
, buf
+ (chunk
* lt
->data_bytes
),
1550 /* Write the spare bytes */
1552 nand_write_data_op(chip
, chip
->oob_poi
+ spare_offset
,
1555 /* Write the ECC bytes */
1557 nand_write_data_op(chip
, chip
->oob_poi
+ ecc_offset
,
1560 spare_offset
+= spare_len
;
1561 ecc_offset
+= ALIGN(ecc_len
, 32);
1564 return nand_prog_page_end_op(chip
);
1568 marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip
*chip
, int chunk
,
1569 const u8
*data
, unsigned int data_len
,
1570 const u8
*spare
, unsigned int spare_len
,
1573 struct marvell_nand_chip
*marvell_nand
= to_marvell_nand(chip
);
1574 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
1575 const struct marvell_hw_ecc_layout
*lt
= to_marvell_nand(chip
)->layout
;
1578 struct marvell_nfc_op nfc_op
= {
1579 .ndcb
[0] = NDCB0_CMD_TYPE(TYPE_WRITE
) | NDCB0_LEN_OVRD
,
1580 .ndcb
[3] = data_len
+ spare_len
,
1584 * First operation dispatches the CMD_SEQIN command, issue the address
1585 * cycles and asks for the first chunk of data.
1586 * All operations in the middle (if any) will issue a naked write and
1587 * also ask for data.
1588 * Last operation (if any) asks for the last chunk of data through a
1592 if (lt
->nchunks
== 1)
1593 xtype
= XTYPE_MONOLITHIC_RW
;
1595 xtype
= XTYPE_WRITE_DISPATCH
;
1597 nfc_op
.ndcb
[0] |= NDCB0_CMD_XTYPE(xtype
) |
1598 NDCB0_ADDR_CYC(marvell_nand
->addr_cyc
) |
1599 NDCB0_CMD1(NAND_CMD_SEQIN
);
1600 nfc_op
.ndcb
[1] |= NDCB1_ADDRS_PAGE(page
);
1601 nfc_op
.ndcb
[2] |= NDCB2_ADDR5_PAGE(page
);
1602 } else if (chunk
< lt
->nchunks
- 1) {
1603 nfc_op
.ndcb
[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW
);
1605 nfc_op
.ndcb
[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW
);
1608 /* Always dispatch the PAGEPROG command on the last chunk */
1609 if (chunk
== lt
->nchunks
- 1)
1610 nfc_op
.ndcb
[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG
) | NDCB0_DBC
;
1612 ret
= marvell_nfc_prepare_cmd(chip
);
1616 marvell_nfc_send_cmd(chip
, &nfc_op
);
1617 ret
= marvell_nfc_end_cmd(chip
, NDSR_WRDREQ
,
1618 "WRDREQ while loading FIFO (data)");
1622 /* Transfer the contents */
1623 iowrite32_rep(nfc
->regs
+ NDDB
, data
, FIFO_REP(data_len
));
1624 iowrite32_rep(nfc
->regs
+ NDDB
, spare
, FIFO_REP(spare_len
));
1629 static int marvell_nfc_hw_ecc_bch_write_page(struct nand_chip
*chip
,
1631 int oob_required
, int page
)
1633 const struct nand_sdr_timings
*sdr
=
1634 nand_get_sdr_timings(nand_get_interface_config(chip
));
1635 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1636 const struct marvell_hw_ecc_layout
*lt
= to_marvell_nand(chip
)->layout
;
1637 const u8
*data
= buf
;
1638 const u8
*spare
= chip
->oob_poi
;
1639 int data_len
= lt
->data_bytes
;
1640 int spare_len
= lt
->spare_bytes
;
1644 marvell_nfc_select_target(chip
, chip
->cur_cs
);
1646 /* Spare data will be written anyway, so clear it to avoid garbage */
1648 memset(chip
->oob_poi
, 0xFF, mtd
->oobsize
);
1650 marvell_nfc_enable_hw_ecc(chip
);
1652 for (chunk
= 0; chunk
< lt
->nchunks
; chunk
++) {
1653 if (chunk
>= lt
->full_chunk_cnt
) {
1654 data_len
= lt
->last_data_bytes
;
1655 spare_len
= lt
->last_spare_bytes
;
1658 marvell_nfc_hw_ecc_bch_write_chunk(chip
, chunk
, data
, data_len
,
1659 spare
, spare_len
, page
);
1664 * Waiting only for CMDD or PAGED is not enough, ECC are
1665 * partially written. No flag is set once the operation is
1666 * really finished but the ND_RUN bit is cleared, so wait for it
1667 * before stepping into the next command.
1669 marvell_nfc_wait_ndrun(chip
);
1672 ret
= marvell_nfc_wait_op(chip
, PSEC_TO_MSEC(sdr
->tPROG_max
));
1674 marvell_nfc_disable_hw_ecc(chip
);
1679 /* Check write status on the chip side */
1680 ret
= nand_status_op(chip
, &status
);
1684 if (status
& NAND_STATUS_FAIL
)
1690 static int marvell_nfc_hw_ecc_bch_write_oob_raw(struct nand_chip
*chip
,
1693 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1694 u8
*buf
= nand_get_data_buf(chip
);
1696 memset(buf
, 0xFF, mtd
->writesize
);
1698 return chip
->ecc
.write_page_raw(chip
, buf
, true, page
);
1701 static int marvell_nfc_hw_ecc_bch_write_oob(struct nand_chip
*chip
, int page
)
1703 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1704 u8
*buf
= nand_get_data_buf(chip
);
1706 memset(buf
, 0xFF, mtd
->writesize
);
1708 return chip
->ecc
.write_page(chip
, buf
, true, page
);
1711 /* NAND framework ->exec_op() hooks and related helpers */
1712 static void marvell_nfc_parse_instructions(struct nand_chip
*chip
,
1713 const struct nand_subop
*subop
,
1714 struct marvell_nfc_op
*nfc_op
)
1716 const struct nand_op_instr
*instr
= NULL
;
1717 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
1718 bool first_cmd
= true;
1722 /* Reset the input structure as most of its fields will be OR'ed */
1723 memset(nfc_op
, 0, sizeof(struct marvell_nfc_op
));
1725 for (op_id
= 0; op_id
< subop
->ninstrs
; op_id
++) {
1726 unsigned int offset
, naddrs
;
1730 instr
= &subop
->instrs
[op_id
];
1732 switch (instr
->type
) {
1733 case NAND_OP_CMD_INSTR
:
1736 NDCB0_CMD1(instr
->ctx
.cmd
.opcode
);
1739 NDCB0_CMD2(instr
->ctx
.cmd
.opcode
) |
1742 nfc_op
->cle_ale_delay_ns
= instr
->delay_ns
;
1746 case NAND_OP_ADDR_INSTR
:
1747 offset
= nand_subop_get_addr_start_off(subop
, op_id
);
1748 naddrs
= nand_subop_get_num_addr_cyc(subop
, op_id
);
1749 addrs
= &instr
->ctx
.addr
.addrs
[offset
];
1751 nfc_op
->ndcb
[0] |= NDCB0_ADDR_CYC(naddrs
);
1753 for (i
= 0; i
< min_t(unsigned int, 4, naddrs
); i
++)
1754 nfc_op
->ndcb
[1] |= addrs
[i
] << (8 * i
);
1757 nfc_op
->ndcb
[2] |= NDCB2_ADDR5_CYC(addrs
[4]);
1759 nfc_op
->ndcb
[3] |= NDCB3_ADDR6_CYC(addrs
[5]);
1761 nfc_op
->ndcb
[3] |= NDCB3_ADDR7_CYC(addrs
[6]);
1763 nfc_op
->cle_ale_delay_ns
= instr
->delay_ns
;
1766 case NAND_OP_DATA_IN_INSTR
:
1767 nfc_op
->data_instr
= instr
;
1768 nfc_op
->data_instr_idx
= op_id
;
1769 nfc_op
->ndcb
[0] |= NDCB0_CMD_TYPE(TYPE_READ
);
1770 if (nfc
->caps
->is_nfcv2
) {
1772 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW
) |
1774 len
= nand_subop_get_data_len(subop
, op_id
);
1775 nfc_op
->ndcb
[3] |= round_up(len
, FIFO_DEPTH
);
1777 nfc_op
->data_delay_ns
= instr
->delay_ns
;
1780 case NAND_OP_DATA_OUT_INSTR
:
1781 nfc_op
->data_instr
= instr
;
1782 nfc_op
->data_instr_idx
= op_id
;
1783 nfc_op
->ndcb
[0] |= NDCB0_CMD_TYPE(TYPE_WRITE
);
1784 if (nfc
->caps
->is_nfcv2
) {
1786 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW
) |
1788 len
= nand_subop_get_data_len(subop
, op_id
);
1789 nfc_op
->ndcb
[3] |= round_up(len
, FIFO_DEPTH
);
1791 nfc_op
->data_delay_ns
= instr
->delay_ns
;
1794 case NAND_OP_WAITRDY_INSTR
:
1795 nfc_op
->rdy_timeout_ms
= instr
->ctx
.waitrdy
.timeout_ms
;
1796 nfc_op
->rdy_delay_ns
= instr
->delay_ns
;
1802 static int marvell_nfc_xfer_data_pio(struct nand_chip
*chip
,
1803 const struct nand_subop
*subop
,
1804 struct marvell_nfc_op
*nfc_op
)
1806 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
1807 const struct nand_op_instr
*instr
= nfc_op
->data_instr
;
1808 unsigned int op_id
= nfc_op
->data_instr_idx
;
1809 unsigned int len
= nand_subop_get_data_len(subop
, op_id
);
1810 unsigned int offset
= nand_subop_get_data_start_off(subop
, op_id
);
1811 bool reading
= (instr
->type
== NAND_OP_DATA_IN_INSTR
);
1814 if (instr
->ctx
.data
.force_8bit
)
1815 marvell_nfc_force_byte_access(chip
, true);
1818 u8
*in
= instr
->ctx
.data
.buf
.in
+ offset
;
1820 ret
= marvell_nfc_xfer_data_in_pio(nfc
, in
, len
);
1822 const u8
*out
= instr
->ctx
.data
.buf
.out
+ offset
;
1824 ret
= marvell_nfc_xfer_data_out_pio(nfc
, out
, len
);
1827 if (instr
->ctx
.data
.force_8bit
)
1828 marvell_nfc_force_byte_access(chip
, false);
1833 static int marvell_nfc_monolithic_access_exec(struct nand_chip
*chip
,
1834 const struct nand_subop
*subop
)
1836 struct marvell_nfc_op nfc_op
;
1840 marvell_nfc_parse_instructions(chip
, subop
, &nfc_op
);
1841 reading
= (nfc_op
.data_instr
->type
== NAND_OP_DATA_IN_INSTR
);
1843 ret
= marvell_nfc_prepare_cmd(chip
);
1847 marvell_nfc_send_cmd(chip
, &nfc_op
);
1848 ret
= marvell_nfc_end_cmd(chip
, NDSR_RDDREQ
| NDSR_WRDREQ
,
1849 "RDDREQ/WRDREQ while draining raw data");
1853 cond_delay(nfc_op
.cle_ale_delay_ns
);
1856 if (nfc_op
.rdy_timeout_ms
) {
1857 ret
= marvell_nfc_wait_op(chip
, nfc_op
.rdy_timeout_ms
);
1862 cond_delay(nfc_op
.rdy_delay_ns
);
1865 marvell_nfc_xfer_data_pio(chip
, subop
, &nfc_op
);
1866 ret
= marvell_nfc_wait_cmdd(chip
);
1870 cond_delay(nfc_op
.data_delay_ns
);
1873 if (nfc_op
.rdy_timeout_ms
) {
1874 ret
= marvell_nfc_wait_op(chip
, nfc_op
.rdy_timeout_ms
);
1879 cond_delay(nfc_op
.rdy_delay_ns
);
1883 * NDCR ND_RUN bit should be cleared automatically at the end of each
1884 * operation but experience shows that the behavior is buggy when it
1885 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1888 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
1890 writel_relaxed(readl(nfc
->regs
+ NDCR
) & ~NDCR_ND_RUN
,
1897 static int marvell_nfc_naked_access_exec(struct nand_chip
*chip
,
1898 const struct nand_subop
*subop
)
1900 struct marvell_nfc_op nfc_op
;
1903 marvell_nfc_parse_instructions(chip
, subop
, &nfc_op
);
1906 * Naked access are different in that they need to be flagged as naked
1907 * by the controller. Reset the controller registers fields that inform
1908 * on the type and refill them according to the ongoing operation.
1910 nfc_op
.ndcb
[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK
) |
1911 NDCB0_CMD_XTYPE(XTYPE_MASK
));
1912 switch (subop
->instrs
[0].type
) {
1913 case NAND_OP_CMD_INSTR
:
1914 nfc_op
.ndcb
[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD
);
1916 case NAND_OP_ADDR_INSTR
:
1917 nfc_op
.ndcb
[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR
);
1919 case NAND_OP_DATA_IN_INSTR
:
1920 nfc_op
.ndcb
[0] |= NDCB0_CMD_TYPE(TYPE_READ
) |
1921 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW
);
1923 case NAND_OP_DATA_OUT_INSTR
:
1924 nfc_op
.ndcb
[0] |= NDCB0_CMD_TYPE(TYPE_WRITE
) |
1925 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW
);
1928 /* This should never happen */
1932 ret
= marvell_nfc_prepare_cmd(chip
);
1936 marvell_nfc_send_cmd(chip
, &nfc_op
);
1938 if (!nfc_op
.data_instr
) {
1939 ret
= marvell_nfc_wait_cmdd(chip
);
1940 cond_delay(nfc_op
.cle_ale_delay_ns
);
1944 ret
= marvell_nfc_end_cmd(chip
, NDSR_RDDREQ
| NDSR_WRDREQ
,
1945 "RDDREQ/WRDREQ while draining raw data");
1949 marvell_nfc_xfer_data_pio(chip
, subop
, &nfc_op
);
1950 ret
= marvell_nfc_wait_cmdd(chip
);
1955 * NDCR ND_RUN bit should be cleared automatically at the end of each
1956 * operation but experience shows that the behavior is buggy when it
1957 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1959 if (subop
->instrs
[0].type
== NAND_OP_DATA_OUT_INSTR
) {
1960 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
1962 writel_relaxed(readl(nfc
->regs
+ NDCR
) & ~NDCR_ND_RUN
,
1969 static int marvell_nfc_naked_waitrdy_exec(struct nand_chip
*chip
,
1970 const struct nand_subop
*subop
)
1972 struct marvell_nfc_op nfc_op
;
1975 marvell_nfc_parse_instructions(chip
, subop
, &nfc_op
);
1977 ret
= marvell_nfc_wait_op(chip
, nfc_op
.rdy_timeout_ms
);
1978 cond_delay(nfc_op
.rdy_delay_ns
);
1983 static int marvell_nfc_read_id_type_exec(struct nand_chip
*chip
,
1984 const struct nand_subop
*subop
)
1986 struct marvell_nfc_op nfc_op
;
1989 marvell_nfc_parse_instructions(chip
, subop
, &nfc_op
);
1990 nfc_op
.ndcb
[0] &= ~NDCB0_CMD_TYPE(TYPE_READ
);
1991 nfc_op
.ndcb
[0] |= NDCB0_CMD_TYPE(TYPE_READ_ID
);
1993 ret
= marvell_nfc_prepare_cmd(chip
);
1997 marvell_nfc_send_cmd(chip
, &nfc_op
);
1998 ret
= marvell_nfc_end_cmd(chip
, NDSR_RDDREQ
,
1999 "RDDREQ while reading ID");
2003 cond_delay(nfc_op
.cle_ale_delay_ns
);
2005 if (nfc_op
.rdy_timeout_ms
) {
2006 ret
= marvell_nfc_wait_op(chip
, nfc_op
.rdy_timeout_ms
);
2011 cond_delay(nfc_op
.rdy_delay_ns
);
2013 marvell_nfc_xfer_data_pio(chip
, subop
, &nfc_op
);
2014 ret
= marvell_nfc_wait_cmdd(chip
);
2018 cond_delay(nfc_op
.data_delay_ns
);
2023 static int marvell_nfc_read_status_exec(struct nand_chip
*chip
,
2024 const struct nand_subop
*subop
)
2026 struct marvell_nfc_op nfc_op
;
2029 marvell_nfc_parse_instructions(chip
, subop
, &nfc_op
);
2030 nfc_op
.ndcb
[0] &= ~NDCB0_CMD_TYPE(TYPE_READ
);
2031 nfc_op
.ndcb
[0] |= NDCB0_CMD_TYPE(TYPE_STATUS
);
2033 ret
= marvell_nfc_prepare_cmd(chip
);
2037 marvell_nfc_send_cmd(chip
, &nfc_op
);
2038 ret
= marvell_nfc_end_cmd(chip
, NDSR_RDDREQ
,
2039 "RDDREQ while reading status");
2043 cond_delay(nfc_op
.cle_ale_delay_ns
);
2045 if (nfc_op
.rdy_timeout_ms
) {
2046 ret
= marvell_nfc_wait_op(chip
, nfc_op
.rdy_timeout_ms
);
2051 cond_delay(nfc_op
.rdy_delay_ns
);
2053 marvell_nfc_xfer_data_pio(chip
, subop
, &nfc_op
);
2054 ret
= marvell_nfc_wait_cmdd(chip
);
2058 cond_delay(nfc_op
.data_delay_ns
);
2063 static int marvell_nfc_reset_cmd_type_exec(struct nand_chip
*chip
,
2064 const struct nand_subop
*subop
)
2066 struct marvell_nfc_op nfc_op
;
2069 marvell_nfc_parse_instructions(chip
, subop
, &nfc_op
);
2070 nfc_op
.ndcb
[0] |= NDCB0_CMD_TYPE(TYPE_RESET
);
2072 ret
= marvell_nfc_prepare_cmd(chip
);
2076 marvell_nfc_send_cmd(chip
, &nfc_op
);
2077 ret
= marvell_nfc_wait_cmdd(chip
);
2081 cond_delay(nfc_op
.cle_ale_delay_ns
);
2083 ret
= marvell_nfc_wait_op(chip
, nfc_op
.rdy_timeout_ms
);
2087 cond_delay(nfc_op
.rdy_delay_ns
);
2092 static int marvell_nfc_erase_cmd_type_exec(struct nand_chip
*chip
,
2093 const struct nand_subop
*subop
)
2095 struct marvell_nfc_op nfc_op
;
2098 marvell_nfc_parse_instructions(chip
, subop
, &nfc_op
);
2099 nfc_op
.ndcb
[0] |= NDCB0_CMD_TYPE(TYPE_ERASE
);
2101 ret
= marvell_nfc_prepare_cmd(chip
);
2105 marvell_nfc_send_cmd(chip
, &nfc_op
);
2106 ret
= marvell_nfc_wait_cmdd(chip
);
2110 cond_delay(nfc_op
.cle_ale_delay_ns
);
2112 ret
= marvell_nfc_wait_op(chip
, nfc_op
.rdy_timeout_ms
);
2116 cond_delay(nfc_op
.rdy_delay_ns
);
2121 static const struct nand_op_parser marvell_nfcv2_op_parser
= NAND_OP_PARSER(
2122 /* Monolithic reads/writes */
2123 NAND_OP_PARSER_PATTERN(
2124 marvell_nfc_monolithic_access_exec
,
2125 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2126 NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2
),
2127 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2128 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
2129 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE
)),
2130 NAND_OP_PARSER_PATTERN(
2131 marvell_nfc_monolithic_access_exec
,
2132 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2133 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2
),
2134 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE
),
2135 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2136 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
2137 /* Naked commands */
2138 NAND_OP_PARSER_PATTERN(
2139 marvell_nfc_naked_access_exec
,
2140 NAND_OP_PARSER_PAT_CMD_ELEM(false)),
2141 NAND_OP_PARSER_PATTERN(
2142 marvell_nfc_naked_access_exec
,
2143 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2
)),
2144 NAND_OP_PARSER_PATTERN(
2145 marvell_nfc_naked_access_exec
,
2146 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE
)),
2147 NAND_OP_PARSER_PATTERN(
2148 marvell_nfc_naked_access_exec
,
2149 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE
)),
2150 NAND_OP_PARSER_PATTERN(
2151 marvell_nfc_naked_waitrdy_exec
,
2152 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2155 static const struct nand_op_parser marvell_nfcv1_op_parser
= NAND_OP_PARSER(
2156 /* Naked commands not supported, use a function for each pattern */
2157 NAND_OP_PARSER_PATTERN(
2158 marvell_nfc_read_id_type_exec
,
2159 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2160 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1
),
2161 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 8)),
2162 NAND_OP_PARSER_PATTERN(
2163 marvell_nfc_erase_cmd_type_exec
,
2164 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2165 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1
),
2166 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2167 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2168 NAND_OP_PARSER_PATTERN(
2169 marvell_nfc_read_status_exec
,
2170 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2171 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)),
2172 NAND_OP_PARSER_PATTERN(
2173 marvell_nfc_reset_cmd_type_exec
,
2174 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2175 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2176 NAND_OP_PARSER_PATTERN(
2177 marvell_nfc_naked_waitrdy_exec
,
2178 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2181 static int marvell_nfc_exec_op(struct nand_chip
*chip
,
2182 const struct nand_operation
*op
,
2185 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
2188 marvell_nfc_select_target(chip
, op
->cs
);
2190 if (nfc
->caps
->is_nfcv2
)
2191 return nand_op_parser_exec_op(chip
, &marvell_nfcv2_op_parser
,
2194 return nand_op_parser_exec_op(chip
, &marvell_nfcv1_op_parser
,
2199 * Layouts were broken in old pxa3xx_nand driver, these are supposed to be
2202 static int marvell_nand_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
2203 struct mtd_oob_region
*oobregion
)
2205 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2206 const struct marvell_hw_ecc_layout
*lt
= to_marvell_nand(chip
)->layout
;
2211 oobregion
->length
= (lt
->full_chunk_cnt
* lt
->ecc_bytes
) +
2213 oobregion
->offset
= mtd
->oobsize
- oobregion
->length
;
2218 static int marvell_nand_ooblayout_free(struct mtd_info
*mtd
, int section
,
2219 struct mtd_oob_region
*oobregion
)
2221 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2222 const struct marvell_hw_ecc_layout
*lt
= to_marvell_nand(chip
)->layout
;
2228 * Bootrom looks in bytes 0 & 5 for bad blocks for the
2229 * 4KB page / 4bit BCH combination.
2231 if (mtd
->writesize
== SZ_4K
&& lt
->data_bytes
== SZ_2K
)
2232 oobregion
->offset
= 6;
2234 oobregion
->offset
= 2;
2236 oobregion
->length
= (lt
->full_chunk_cnt
* lt
->spare_bytes
) +
2237 lt
->last_spare_bytes
- oobregion
->offset
;
2242 static const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops
= {
2243 .ecc
= marvell_nand_ooblayout_ecc
,
2244 .free
= marvell_nand_ooblayout_free
,
2247 static int marvell_nand_hw_ecc_controller_init(struct mtd_info
*mtd
,
2248 struct nand_ecc_ctrl
*ecc
)
2250 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2251 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
2252 const struct marvell_hw_ecc_layout
*l
;
2255 if (!nfc
->caps
->is_nfcv2
&&
2256 (mtd
->writesize
+ mtd
->oobsize
> MAX_CHUNK_SIZE
)) {
2258 "NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n",
2259 mtd
->writesize
, MAX_CHUNK_SIZE
- mtd
->oobsize
);
2263 to_marvell_nand(chip
)->layout
= NULL
;
2264 for (i
= 0; i
< ARRAY_SIZE(marvell_nfc_layouts
); i
++) {
2265 l
= &marvell_nfc_layouts
[i
];
2266 if (mtd
->writesize
== l
->writesize
&&
2267 ecc
->size
== l
->chunk
&& ecc
->strength
== l
->strength
) {
2268 to_marvell_nand(chip
)->layout
= l
;
2273 if (!to_marvell_nand(chip
)->layout
||
2274 (!nfc
->caps
->is_nfcv2
&& ecc
->strength
> 1)) {
2276 "ECC strength %d at page size %d is not supported\n",
2277 ecc
->strength
, mtd
->writesize
);
2281 /* Special care for the layout 2k/8-bit/512B */
2282 if (l
->writesize
== 2048 && l
->strength
== 8) {
2283 if (mtd
->oobsize
< 128) {
2284 dev_err(nfc
->dev
, "Requested layout needs at least 128 OOB bytes\n");
2287 chip
->bbt_options
|= NAND_BBT_NO_OOB_BBM
;
2291 mtd_set_ooblayout(mtd
, &marvell_nand_ooblayout_ops
);
2292 ecc
->steps
= l
->nchunks
;
2293 ecc
->size
= l
->data_bytes
;
2295 if (ecc
->strength
== 1) {
2296 chip
->ecc
.algo
= NAND_ECC_ALGO_HAMMING
;
2297 ecc
->read_page_raw
= marvell_nfc_hw_ecc_hmg_read_page_raw
;
2298 ecc
->read_page
= marvell_nfc_hw_ecc_hmg_read_page
;
2299 ecc
->read_oob_raw
= marvell_nfc_hw_ecc_hmg_read_oob_raw
;
2300 ecc
->read_oob
= ecc
->read_oob_raw
;
2301 ecc
->write_page_raw
= marvell_nfc_hw_ecc_hmg_write_page_raw
;
2302 ecc
->write_page
= marvell_nfc_hw_ecc_hmg_write_page
;
2303 ecc
->write_oob_raw
= marvell_nfc_hw_ecc_hmg_write_oob_raw
;
2304 ecc
->write_oob
= ecc
->write_oob_raw
;
2306 chip
->ecc
.algo
= NAND_ECC_ALGO_BCH
;
2308 ecc
->read_page_raw
= marvell_nfc_hw_ecc_bch_read_page_raw
;
2309 ecc
->read_page
= marvell_nfc_hw_ecc_bch_read_page
;
2310 ecc
->read_oob_raw
= marvell_nfc_hw_ecc_bch_read_oob_raw
;
2311 ecc
->read_oob
= marvell_nfc_hw_ecc_bch_read_oob
;
2312 ecc
->write_page_raw
= marvell_nfc_hw_ecc_bch_write_page_raw
;
2313 ecc
->write_page
= marvell_nfc_hw_ecc_bch_write_page
;
2314 ecc
->write_oob_raw
= marvell_nfc_hw_ecc_bch_write_oob_raw
;
2315 ecc
->write_oob
= marvell_nfc_hw_ecc_bch_write_oob
;
2321 static int marvell_nand_ecc_init(struct mtd_info
*mtd
,
2322 struct nand_ecc_ctrl
*ecc
)
2324 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2325 const struct nand_ecc_props
*requirements
=
2326 nanddev_get_ecc_requirements(&chip
->base
);
2327 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
2330 if (ecc
->engine_type
!= NAND_ECC_ENGINE_TYPE_NONE
&&
2331 (!ecc
->size
|| !ecc
->strength
)) {
2332 if (requirements
->step_size
&& requirements
->strength
) {
2333 ecc
->size
= requirements
->step_size
;
2334 ecc
->strength
= requirements
->strength
;
2337 "No minimum ECC strength, using 1b/512B\n");
2343 switch (ecc
->engine_type
) {
2344 case NAND_ECC_ENGINE_TYPE_ON_HOST
:
2345 ret
= marvell_nand_hw_ecc_controller_init(mtd
, ecc
);
2349 case NAND_ECC_ENGINE_TYPE_NONE
:
2350 case NAND_ECC_ENGINE_TYPE_SOFT
:
2351 case NAND_ECC_ENGINE_TYPE_ON_DIE
:
2352 if (!nfc
->caps
->is_nfcv2
&& mtd
->writesize
!= SZ_512
&&
2353 mtd
->writesize
!= SZ_2K
) {
2354 dev_err(nfc
->dev
, "NFCv1 cannot write %d bytes pages\n",
2366 static u8 bbt_pattern
[] = {'M', 'V', 'B', 'b', 't', '0' };
2367 static u8 bbt_mirror_pattern
[] = {'1', 't', 'b', 'B', 'V', 'M' };
2369 static struct nand_bbt_descr bbt_main_descr
= {
2370 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
|
2371 NAND_BBT_2BIT
| NAND_BBT_VERSION
,
2375 .maxblocks
= 8, /* Last 8 blocks in each chip */
2376 .pattern
= bbt_pattern
2379 static struct nand_bbt_descr bbt_mirror_descr
= {
2380 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
|
2381 NAND_BBT_2BIT
| NAND_BBT_VERSION
,
2385 .maxblocks
= 8, /* Last 8 blocks in each chip */
2386 .pattern
= bbt_mirror_pattern
2389 static int marvell_nfc_setup_interface(struct nand_chip
*chip
, int chipnr
,
2390 const struct nand_interface_config
*conf
)
2392 struct marvell_nand_chip
*marvell_nand
= to_marvell_nand(chip
);
2393 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
2394 unsigned int period_ns
= 1000000000 / clk_get_rate(nfc
->core_clk
) * 2;
2395 const struct nand_sdr_timings
*sdr
;
2396 struct marvell_nfc_timings nfc_tmg
;
2399 sdr
= nand_get_sdr_timings(conf
);
2401 return PTR_ERR(sdr
);
2403 if (nfc
->caps
->max_mode_number
&& nfc
->caps
->max_mode_number
< conf
->timings
.mode
)
2407 * SDR timings are given in pico-seconds while NFC timings must be
2408 * expressed in NAND controller clock cycles, which is half of the
2409 * frequency of the accessible ECC clock retrieved by clk_get_rate().
2410 * This is not written anywhere in the datasheet but was observed
2411 * with an oscilloscope.
2413 * NFC datasheet gives equations from which thoses calculations
2414 * are derived, they tend to be slightly more restrictives than the
2415 * given core timings and may improve the overall speed.
2417 nfc_tmg
.tRP
= TO_CYCLES(DIV_ROUND_UP(sdr
->tRC_min
, 2), period_ns
) - 1;
2418 nfc_tmg
.tRH
= nfc_tmg
.tRP
;
2419 nfc_tmg
.tWP
= TO_CYCLES(DIV_ROUND_UP(sdr
->tWC_min
, 2), period_ns
) - 1;
2420 nfc_tmg
.tWH
= nfc_tmg
.tWP
;
2421 nfc_tmg
.tCS
= TO_CYCLES(sdr
->tCS_min
, period_ns
);
2422 nfc_tmg
.tCH
= TO_CYCLES(sdr
->tCH_min
, period_ns
) - 1;
2423 nfc_tmg
.tADL
= TO_CYCLES(sdr
->tADL_min
, period_ns
);
2425 * Read delay is the time of propagation from SoC pins to NFC internal
2426 * logic. With non-EDO timings, this is MIN_RD_DEL_CNT clock cycles. In
2427 * EDO mode, an additional delay of tRH must be taken into account so
2428 * the data is sampled on the falling edge instead of the rising edge.
2430 read_delay
= sdr
->tRC_min
>= 30000 ?
2431 MIN_RD_DEL_CNT
: MIN_RD_DEL_CNT
+ nfc_tmg
.tRH
;
2433 nfc_tmg
.tAR
= TO_CYCLES(sdr
->tAR_min
, period_ns
);
2435 * tWHR and tRHW are supposed to be read to write delays (and vice
2436 * versa) but in some cases, ie. when doing a change column, they must
2437 * be greater than that to be sure tCCS delay is respected.
2439 nfc_tmg
.tWHR
= TO_CYCLES(max_t(int, sdr
->tWHR_min
, sdr
->tCCS_min
),
2441 nfc_tmg
.tRHW
= TO_CYCLES(max_t(int, sdr
->tRHW_min
, sdr
->tCCS_min
),
2445 * NFCv2: Use WAIT_MODE (wait for RB line), do not rely only on delays.
2446 * NFCv1: No WAIT_MODE, tR must be maximal.
2448 if (nfc
->caps
->is_nfcv2
) {
2449 nfc_tmg
.tR
= TO_CYCLES(sdr
->tWB_max
, period_ns
);
2451 nfc_tmg
.tR
= TO_CYCLES64(sdr
->tWB_max
+ sdr
->tR_max
,
2453 if (nfc_tmg
.tR
+ 3 > nfc_tmg
.tCH
)
2454 nfc_tmg
.tR
= nfc_tmg
.tCH
- 3;
2462 marvell_nand
->ndtr0
=
2463 NDTR0_TRP(nfc_tmg
.tRP
) |
2464 NDTR0_TRH(nfc_tmg
.tRH
) |
2465 NDTR0_ETRP(nfc_tmg
.tRP
) |
2466 NDTR0_TWP(nfc_tmg
.tWP
) |
2467 NDTR0_TWH(nfc_tmg
.tWH
) |
2468 NDTR0_TCS(nfc_tmg
.tCS
) |
2469 NDTR0_TCH(nfc_tmg
.tCH
);
2471 marvell_nand
->ndtr1
=
2472 NDTR1_TAR(nfc_tmg
.tAR
) |
2473 NDTR1_TWHR(nfc_tmg
.tWHR
) |
2474 NDTR1_TR(nfc_tmg
.tR
);
2476 if (nfc
->caps
->is_nfcv2
) {
2477 marvell_nand
->ndtr0
|=
2478 NDTR0_RD_CNT_DEL(read_delay
) |
2480 NDTR0_TADL(nfc_tmg
.tADL
);
2482 marvell_nand
->ndtr1
|=
2483 NDTR1_TRHW(nfc_tmg
.tRHW
) |
2488 * Reset nfc->selected_chip so the next command will cause the timing
2489 * registers to be updated in marvell_nfc_select_target().
2491 nfc
->selected_chip
= NULL
;
2496 static int marvell_nand_attach_chip(struct nand_chip
*chip
)
2498 struct mtd_info
*mtd
= nand_to_mtd(chip
);
2499 struct marvell_nand_chip
*marvell_nand
= to_marvell_nand(chip
);
2500 struct marvell_nfc
*nfc
= to_marvell_nfc(chip
->controller
);
2501 struct pxa3xx_nand_platform_data
*pdata
= dev_get_platdata(nfc
->dev
);
2504 if (pdata
&& pdata
->flash_bbt
)
2505 chip
->bbt_options
|= NAND_BBT_USE_FLASH
;
2507 if (chip
->bbt_options
& NAND_BBT_USE_FLASH
) {
2509 * We'll use a bad block table stored in-flash and don't
2510 * allow writing the bad block marker to the flash.
2512 chip
->bbt_options
|= NAND_BBT_NO_OOB_BBM
;
2513 chip
->bbt_td
= &bbt_main_descr
;
2514 chip
->bbt_md
= &bbt_mirror_descr
;
2517 /* Save the chip-specific fields of NDCR */
2518 marvell_nand
->ndcr
= NDCR_PAGE_SZ(mtd
->writesize
);
2519 if (chip
->options
& NAND_BUSWIDTH_16
)
2520 marvell_nand
->ndcr
|= NDCR_DWIDTH_M
| NDCR_DWIDTH_C
;
2523 * On small page NANDs, only one cycle is needed to pass the
2526 if (mtd
->writesize
<= 512) {
2527 marvell_nand
->addr_cyc
= 1;
2529 marvell_nand
->addr_cyc
= 2;
2530 marvell_nand
->ndcr
|= NDCR_RA_START
;
2534 * Now add the number of cycles needed to pass the row
2537 * Addressing a chip using CS 2 or 3 should also need the third row
2538 * cycle but due to inconsistance in the documentation and lack of
2539 * hardware to test this situation, this case is not supported.
2541 if (chip
->options
& NAND_ROW_ADDR_3
)
2542 marvell_nand
->addr_cyc
+= 3;
2544 marvell_nand
->addr_cyc
+= 2;
2547 chip
->ecc
.size
= pdata
->ecc_step_size
;
2548 chip
->ecc
.strength
= pdata
->ecc_strength
;
2551 ret
= marvell_nand_ecc_init(mtd
, &chip
->ecc
);
2553 dev_err(nfc
->dev
, "ECC init failed: %d\n", ret
);
2557 if (chip
->ecc
.engine_type
== NAND_ECC_ENGINE_TYPE_ON_HOST
) {
2559 * Subpage write not available with hardware ECC, prohibit also
2560 * subpage read as in userspace subpage access would still be
2561 * allowed and subpage write, if used, would lead to numerous
2562 * uncorrectable ECC errors.
2564 chip
->options
|= NAND_NO_SUBPAGE_WRITE
;
2567 if (pdata
|| nfc
->caps
->legacy_of_bindings
) {
2569 * We keep the MTD name unchanged to avoid breaking platforms
2570 * where the MTD cmdline parser is used and the bootloader
2571 * has not been updated to use the new naming scheme.
2573 mtd
->name
= "pxa3xx_nand-0";
2574 } else if (!mtd
->name
) {
2576 * If the new bindings are used and the bootloader has not been
2577 * updated to pass a new mtdparts parameter on the cmdline, you
2578 * should define the following property in your NAND node, ie:
2580 * label = "main-storage";
2582 * This way, mtd->name will be set by the core when
2583 * nand_set_flash_node() is called.
2585 mtd
->name
= devm_kasprintf(nfc
->dev
, GFP_KERNEL
,
2586 "%s:nand.%d", dev_name(nfc
->dev
),
2587 marvell_nand
->sels
[0].cs
);
2589 dev_err(nfc
->dev
, "Failed to allocate mtd->name\n");
2597 static const struct nand_controller_ops marvell_nand_controller_ops
= {
2598 .attach_chip
= marvell_nand_attach_chip
,
2599 .exec_op
= marvell_nfc_exec_op
,
2600 .setup_interface
= marvell_nfc_setup_interface
,
2603 static int marvell_nand_chip_init(struct device
*dev
, struct marvell_nfc
*nfc
,
2604 struct device_node
*np
)
2606 struct pxa3xx_nand_platform_data
*pdata
= dev_get_platdata(dev
);
2607 struct marvell_nand_chip
*marvell_nand
;
2608 struct mtd_info
*mtd
;
2609 struct nand_chip
*chip
;
2614 * The legacy "num-cs" property indicates the number of CS on the only
2615 * chip connected to the controller (legacy bindings does not support
2616 * more than one chip). The CS and RB pins are always the #0.
2618 * When not using legacy bindings, a couple of "reg" and "nand-rb"
2619 * properties must be filled. For each chip, expressed as a subnode,
2620 * "reg" points to the CS lines and "nand-rb" to the RB line.
2622 if (pdata
|| nfc
->caps
->legacy_of_bindings
) {
2625 nsels
= of_property_count_elems_of_size(np
, "reg", sizeof(u32
));
2627 dev_err(dev
, "missing/invalid reg property\n");
2632 /* Alloc the nand chip structure */
2633 marvell_nand
= devm_kzalloc(dev
,
2634 struct_size(marvell_nand
, sels
, nsels
),
2636 if (!marvell_nand
) {
2637 dev_err(dev
, "could not allocate chip structure\n");
2641 marvell_nand
->nsels
= nsels
;
2642 marvell_nand
->selected_die
= -1;
2644 for (i
= 0; i
< nsels
; i
++) {
2645 if (pdata
|| nfc
->caps
->legacy_of_bindings
) {
2647 * Legacy bindings use the CS lines in natural
2652 /* Retrieve CS id */
2653 ret
= of_property_read_u32_index(np
, "reg", i
, &cs
);
2655 dev_err(dev
, "could not retrieve reg property: %d\n",
2661 if (cs
>= nfc
->caps
->max_cs_nb
) {
2662 dev_err(dev
, "invalid reg value: %u (max CS = %d)\n",
2663 cs
, nfc
->caps
->max_cs_nb
);
2667 if (test_and_set_bit(cs
, &nfc
->assigned_cs
)) {
2668 dev_err(dev
, "CS %d already assigned\n", cs
);
2673 * The cs variable represents the chip select id, which must be
2674 * converted in bit fields for NDCB0 and NDCB2 to select the
2675 * right chip. Unfortunately, due to a lack of information on
2676 * the subject and incoherent documentation, the user should not
2677 * use CS1 and CS3 at all as asserting them is not supported in
2678 * a reliable way (due to multiplexing inside ADDR5 field).
2680 marvell_nand
->sels
[i
].cs
= cs
;
2684 marvell_nand
->sels
[i
].ndcb0_csel
= 0;
2688 marvell_nand
->sels
[i
].ndcb0_csel
= NDCB0_CSEL
;
2694 /* Retrieve RB id */
2695 if (pdata
|| nfc
->caps
->legacy_of_bindings
) {
2696 /* Legacy bindings always use RB #0 */
2699 ret
= of_property_read_u32_index(np
, "nand-rb", i
,
2703 "could not retrieve RB property: %d\n",
2709 if (rb
>= nfc
->caps
->max_rb_nb
) {
2710 dev_err(dev
, "invalid reg value: %u (max RB = %d)\n",
2711 rb
, nfc
->caps
->max_rb_nb
);
2715 marvell_nand
->sels
[i
].rb
= rb
;
2718 chip
= &marvell_nand
->chip
;
2719 chip
->controller
= &nfc
->controller
;
2720 nand_set_flash_node(chip
, np
);
2722 if (of_property_read_bool(np
, "marvell,nand-keep-config"))
2723 chip
->options
|= NAND_KEEP_TIMINGS
;
2725 mtd
= nand_to_mtd(chip
);
2726 mtd
->dev
.parent
= dev
;
2729 * Save a reference value for timing registers before
2730 * ->setup_interface() is called.
2732 marvell_nand
->ndtr0
= readl_relaxed(nfc
->regs
+ NDTR0
);
2733 marvell_nand
->ndtr1
= readl_relaxed(nfc
->regs
+ NDTR1
);
2735 chip
->options
|= NAND_BUSWIDTH_AUTO
;
2737 ret
= nand_scan(chip
, marvell_nand
->nsels
);
2739 dev_err(dev
, "could not scan the nand chip\n");
2744 /* Legacy bindings support only one chip */
2745 ret
= mtd_device_register(mtd
, pdata
->parts
, pdata
->nr_parts
);
2747 ret
= mtd_device_register(mtd
, NULL
, 0);
2749 dev_err(dev
, "failed to register mtd device: %d\n", ret
);
2754 list_add_tail(&marvell_nand
->node
, &nfc
->chips
);
2759 static void marvell_nand_chips_cleanup(struct marvell_nfc
*nfc
)
2761 struct marvell_nand_chip
*entry
, *temp
;
2762 struct nand_chip
*chip
;
2765 list_for_each_entry_safe(entry
, temp
, &nfc
->chips
, node
) {
2766 chip
= &entry
->chip
;
2767 ret
= mtd_device_unregister(nand_to_mtd(chip
));
2770 list_del(&entry
->node
);
2774 static int marvell_nand_chips_init(struct device
*dev
, struct marvell_nfc
*nfc
)
2776 struct device_node
*np
= dev
->of_node
;
2777 struct device_node
*nand_np
;
2778 int max_cs
= nfc
->caps
->max_cs_nb
;
2785 nchips
= of_get_child_count(np
);
2787 if (nchips
> max_cs
) {
2788 dev_err(dev
, "too many NAND chips: %d (max = %d CS)\n", nchips
,
2794 * Legacy bindings do not use child nodes to exhibit NAND chip
2795 * properties and layout. Instead, NAND properties are mixed with the
2796 * controller ones, and partitions are defined as direct subnodes of the
2797 * NAND controller node.
2799 if (nfc
->caps
->legacy_of_bindings
) {
2800 ret
= marvell_nand_chip_init(dev
, nfc
, np
);
2804 for_each_child_of_node(np
, nand_np
) {
2805 ret
= marvell_nand_chip_init(dev
, nfc
, nand_np
);
2807 of_node_put(nand_np
);
2815 marvell_nand_chips_cleanup(nfc
);
2820 static int marvell_nfc_init_dma(struct marvell_nfc
*nfc
)
2822 struct platform_device
*pdev
= container_of(nfc
->dev
,
2823 struct platform_device
,
2825 struct dma_slave_config config
= {};
2829 if (!IS_ENABLED(CONFIG_PXA_DMA
)) {
2831 "DMA not enabled in configuration\n");
2835 ret
= dma_set_mask_and_coherent(nfc
->dev
, DMA_BIT_MASK(32));
2839 nfc
->dma_chan
= dma_request_chan(nfc
->dev
, "data");
2840 if (IS_ERR(nfc
->dma_chan
)) {
2841 ret
= PTR_ERR(nfc
->dma_chan
);
2842 nfc
->dma_chan
= NULL
;
2843 return dev_err_probe(nfc
->dev
, ret
, "DMA channel request failed\n");
2846 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2849 goto release_channel
;
2852 config
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
2853 config
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
2854 config
.src_addr
= r
->start
+ NDDB
;
2855 config
.dst_addr
= r
->start
+ NDDB
;
2856 config
.src_maxburst
= 32;
2857 config
.dst_maxburst
= 32;
2858 ret
= dmaengine_slave_config(nfc
->dma_chan
, &config
);
2860 dev_err(nfc
->dev
, "Failed to configure DMA channel\n");
2861 goto release_channel
;
2865 * DMA must act on length multiple of 32 and this length may be
2866 * bigger than the destination buffer. Use this buffer instead
2867 * for DMA transfers and then copy the desired amount of data to
2868 * the provided buffer.
2870 nfc
->dma_buf
= kmalloc(MAX_CHUNK_SIZE
, GFP_KERNEL
| GFP_DMA
);
2871 if (!nfc
->dma_buf
) {
2873 goto release_channel
;
2876 nfc
->use_dma
= true;
2881 dma_release_channel(nfc
->dma_chan
);
2882 nfc
->dma_chan
= NULL
;
2887 static void marvell_nfc_reset(struct marvell_nfc
*nfc
)
2890 * ECC operations and interruptions are only enabled when specifically
2891 * needed. ECC shall not be activated in the early stages (fails probe).
2892 * Arbiter flag, even if marked as "reserved", must be set (empirical).
2893 * SPARE_EN bit must always be set or ECC bytes will not be at the same
2894 * offset in the read page and this will fail the protection.
2896 writel_relaxed(NDCR_ALL_INT
| NDCR_ND_ARB_EN
| NDCR_SPARE_EN
|
2897 NDCR_RD_ID_CNT(NFCV1_READID_LEN
), nfc
->regs
+ NDCR
);
2898 writel_relaxed(0xFFFFFFFF, nfc
->regs
+ NDSR
);
2899 writel_relaxed(0, nfc
->regs
+ NDECCCTRL
);
2902 static int marvell_nfc_init(struct marvell_nfc
*nfc
)
2904 struct device_node
*np
= nfc
->dev
->of_node
;
2907 * Some SoCs like A7k/A8k need to enable manually the NAND
2908 * controller, gated clocks and reset bits to avoid being bootloader
2909 * dependent. This is done through the use of the System Functions
2912 if (nfc
->caps
->need_system_controller
) {
2913 struct regmap
*sysctrl_base
=
2914 syscon_regmap_lookup_by_phandle(np
,
2915 "marvell,system-controller");
2917 if (IS_ERR(sysctrl_base
))
2918 return PTR_ERR(sysctrl_base
);
2920 regmap_write(sysctrl_base
, GENCONF_SOC_DEVICE_MUX
,
2921 GENCONF_SOC_DEVICE_MUX_NFC_EN
|
2922 GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST
|
2923 GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST
|
2924 GENCONF_SOC_DEVICE_MUX_NFC_INT_EN
|
2925 GENCONF_SOC_DEVICE_MUX_NFC_DEVBUS_ARB_EN
);
2927 regmap_update_bits(sysctrl_base
, GENCONF_CLK_GATING_CTRL
,
2928 GENCONF_CLK_GATING_CTRL_ND_GATE
,
2929 GENCONF_CLK_GATING_CTRL_ND_GATE
);
2932 /* Configure the DMA if appropriate */
2933 if (!nfc
->caps
->is_nfcv2
)
2934 marvell_nfc_init_dma(nfc
);
2936 marvell_nfc_reset(nfc
);
2941 static int marvell_nfc_probe(struct platform_device
*pdev
)
2943 struct device
*dev
= &pdev
->dev
;
2944 struct marvell_nfc
*nfc
;
2948 nfc
= devm_kzalloc(&pdev
->dev
, sizeof(struct marvell_nfc
),
2954 nand_controller_init(&nfc
->controller
);
2955 nfc
->controller
.ops
= &marvell_nand_controller_ops
;
2956 INIT_LIST_HEAD(&nfc
->chips
);
2958 nfc
->regs
= devm_platform_ioremap_resource(pdev
, 0);
2959 if (IS_ERR(nfc
->regs
))
2960 return PTR_ERR(nfc
->regs
);
2962 irq
= platform_get_irq(pdev
, 0);
2966 nfc
->core_clk
= devm_clk_get(&pdev
->dev
, "core");
2968 /* Managed the legacy case (when the first clock was not named) */
2969 if (nfc
->core_clk
== ERR_PTR(-ENOENT
))
2970 nfc
->core_clk
= devm_clk_get(&pdev
->dev
, NULL
);
2972 if (IS_ERR(nfc
->core_clk
))
2973 return PTR_ERR(nfc
->core_clk
);
2975 ret
= clk_prepare_enable(nfc
->core_clk
);
2979 nfc
->reg_clk
= devm_clk_get(&pdev
->dev
, "reg");
2980 if (IS_ERR(nfc
->reg_clk
)) {
2981 if (PTR_ERR(nfc
->reg_clk
) != -ENOENT
) {
2982 ret
= PTR_ERR(nfc
->reg_clk
);
2983 goto unprepare_core_clk
;
2986 nfc
->reg_clk
= NULL
;
2989 ret
= clk_prepare_enable(nfc
->reg_clk
);
2991 goto unprepare_core_clk
;
2993 marvell_nfc_disable_int(nfc
, NDCR_ALL_INT
);
2994 marvell_nfc_clear_int(nfc
, NDCR_ALL_INT
);
2995 ret
= devm_request_irq(dev
, irq
, marvell_nfc_isr
,
2996 0, "marvell-nfc", nfc
);
2998 goto unprepare_reg_clk
;
3000 /* Get NAND controller capabilities */
3002 nfc
->caps
= (void *)pdev
->id_entry
->driver_data
;
3004 nfc
->caps
= of_device_get_match_data(&pdev
->dev
);
3007 dev_err(dev
, "Could not retrieve NFC caps\n");
3009 goto unprepare_reg_clk
;
3012 /* Init the controller and then probe the chips */
3013 ret
= marvell_nfc_init(nfc
);
3015 goto unprepare_reg_clk
;
3017 platform_set_drvdata(pdev
, nfc
);
3019 ret
= marvell_nand_chips_init(dev
, nfc
);
3027 dma_release_channel(nfc
->dma_chan
);
3029 clk_disable_unprepare(nfc
->reg_clk
);
3031 clk_disable_unprepare(nfc
->core_clk
);
3036 static void marvell_nfc_remove(struct platform_device
*pdev
)
3038 struct marvell_nfc
*nfc
= platform_get_drvdata(pdev
);
3040 marvell_nand_chips_cleanup(nfc
);
3043 dmaengine_terminate_all(nfc
->dma_chan
);
3044 dma_release_channel(nfc
->dma_chan
);
3047 clk_disable_unprepare(nfc
->reg_clk
);
3048 clk_disable_unprepare(nfc
->core_clk
);
3051 static int __maybe_unused
marvell_nfc_suspend(struct device
*dev
)
3053 struct marvell_nfc
*nfc
= dev_get_drvdata(dev
);
3054 struct marvell_nand_chip
*chip
;
3056 list_for_each_entry(chip
, &nfc
->chips
, node
)
3057 marvell_nfc_wait_ndrun(&chip
->chip
);
3059 clk_disable_unprepare(nfc
->reg_clk
);
3060 clk_disable_unprepare(nfc
->core_clk
);
3065 static int __maybe_unused
marvell_nfc_resume(struct device
*dev
)
3067 struct marvell_nfc
*nfc
= dev_get_drvdata(dev
);
3070 ret
= clk_prepare_enable(nfc
->core_clk
);
3074 ret
= clk_prepare_enable(nfc
->reg_clk
);
3076 clk_disable_unprepare(nfc
->core_clk
);
3081 * Reset nfc->selected_chip so the next command will cause the timing
3082 * registers to be restored in marvell_nfc_select_target().
3084 nfc
->selected_chip
= NULL
;
3086 /* Reset registers that have lost their contents */
3087 marvell_nfc_reset(nfc
);
3092 static const struct dev_pm_ops marvell_nfc_pm_ops
= {
3093 SET_SYSTEM_SLEEP_PM_OPS(marvell_nfc_suspend
, marvell_nfc_resume
)
3096 static const struct marvell_nfc_caps marvell_armada_8k_nfc_caps
= {
3099 .need_system_controller
= true,
3103 static const struct marvell_nfc_caps marvell_ac5_caps
= {
3107 .max_mode_number
= 3,
3110 static const struct marvell_nfc_caps marvell_armada370_nfc_caps
= {
3116 static const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps
= {
3122 static const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps
= {
3125 .need_system_controller
= true,
3126 .legacy_of_bindings
= true,
3130 static const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps
= {
3133 .legacy_of_bindings
= true,
3137 static const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps
= {
3140 .legacy_of_bindings
= true,
3144 static const struct platform_device_id marvell_nfc_platform_ids
[] = {
3146 .name
= "pxa3xx-nand",
3147 .driver_data
= (kernel_ulong_t
)&marvell_pxa3xx_nfc_legacy_caps
,
3151 MODULE_DEVICE_TABLE(platform
, marvell_nfc_platform_ids
);
3153 static const struct of_device_id marvell_nfc_of_ids
[] = {
3155 .compatible
= "marvell,armada-8k-nand-controller",
3156 .data
= &marvell_armada_8k_nfc_caps
,
3159 .compatible
= "marvell,ac5-nand-controller",
3160 .data
= &marvell_ac5_caps
,
3163 .compatible
= "marvell,armada370-nand-controller",
3164 .data
= &marvell_armada370_nfc_caps
,
3167 .compatible
= "marvell,pxa3xx-nand-controller",
3168 .data
= &marvell_pxa3xx_nfc_caps
,
3170 /* Support for old/deprecated bindings: */
3172 .compatible
= "marvell,armada-8k-nand",
3173 .data
= &marvell_armada_8k_nfc_legacy_caps
,
3176 .compatible
= "marvell,armada370-nand",
3177 .data
= &marvell_armada370_nfc_legacy_caps
,
3180 .compatible
= "marvell,pxa3xx-nand",
3181 .data
= &marvell_pxa3xx_nfc_legacy_caps
,
3185 MODULE_DEVICE_TABLE(of
, marvell_nfc_of_ids
);
3187 static struct platform_driver marvell_nfc_driver
= {
3189 .name
= "marvell-nfc",
3190 .of_match_table
= marvell_nfc_of_ids
,
3191 .pm
= &marvell_nfc_pm_ops
,
3193 .id_table
= marvell_nfc_platform_ids
,
3194 .probe
= marvell_nfc_probe
,
3195 .remove_new
= marvell_nfc_remove
,
3197 module_platform_driver(marvell_nfc_driver
);
3199 MODULE_LICENSE("GPL");
3200 MODULE_DESCRIPTION("Marvell NAND controller driver");