mtd: rawnand: marvell: rework BCH engine failure path
[linux-2.6-block.git] / drivers / mtd / nand / raw / marvell_nand.c
CommitLineData
02f26ecf
MR
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Marvell NAND flash controller driver
4 *
5 * Copyright (C) 2017 Marvell
6 * Author: Miquel RAYNAL <miquel.raynal@free-electrons.com>
7 *
33c1c5fe
MR
8 *
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.
12 *
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.
16 *
17 * The ECC layouts are depicted in details in Marvell AN-379, but here
18 * is a brief description.
19 *
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:
27 *
28 * +-------------------------------------------------------------+
29 * | Data 1 | ... | Data N | ECC 1 | ... | ECCN | Free OOB bytes |
30 * +-------------------------------------------------------------+
31 *
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
37 * when BCH is chosen:
38 *
39 * +-----------------------------------------
40 * | Data 1 | Free OOB bytes 1 | ECC 1 | ...
41 * +-----------------------------------------
42 *
43 * -------------------------------------------
44 * ... | Data N | Free OOB bytes N | ECC N |
45 * -------------------------------------------
46 *
47 * --------------------------------------------+
48 * Last Data | Last Free OOB bytes | Last ECC |
49 * --------------------------------------------+
50 *
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
54 * bytes.
55 *
56 * The controller has certain limitations that are handled by the
57 * driver:
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
70 * supported layouts.
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
74 * markers.
02f26ecf
MR
75 */
76
77#include <linux/module.h>
78#include <linux/clk.h>
79#include <linux/mtd/rawnand.h>
80#include <linux/of_platform.h>
81#include <linux/iopoll.h>
82#include <linux/interrupt.h>
83#include <linux/slab.h>
84#include <linux/mfd/syscon.h>
85#include <linux/regmap.h>
86#include <asm/unaligned.h>
87
88#include <linux/dmaengine.h>
89#include <linux/dma-mapping.h>
90#include <linux/dma/pxa-dma.h>
91#include <linux/platform_data/mtd-nand-pxa3xx.h>
92
93/* Data FIFO granularity, FIFO reads/writes must be a multiple of this length */
94#define FIFO_DEPTH 8
95#define FIFO_REP(x) (x / sizeof(u32))
96#define BCH_SEQ_READS (32 / FIFO_DEPTH)
97/* NFC does not support transfers of larger chunks at a time */
98#define MAX_CHUNK_SIZE 2112
99/* NFCv1 cannot read more that 7 bytes of ID */
100#define NFCV1_READID_LEN 7
101/* Polling is done at a pace of POLL_PERIOD us until POLL_TIMEOUT is reached */
102#define POLL_PERIOD 0
103#define POLL_TIMEOUT 100000
104/* Interrupt maximum wait period in ms */
105#define IRQ_TIMEOUT 1000
106/* Latency in clock cycles between SoC pins and NFC logic */
107#define MIN_RD_DEL_CNT 3
108/* Maximum number of contiguous address cycles */
109#define MAX_ADDRESS_CYC_NFCV1 5
110#define MAX_ADDRESS_CYC_NFCV2 7
111/* System control registers/bits to enable the NAND controller on some SoCs */
112#define GENCONF_SOC_DEVICE_MUX 0x208
113#define GENCONF_SOC_DEVICE_MUX_NFC_EN BIT(0)
114#define GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST BIT(20)
115#define GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST BIT(21)
116#define GENCONF_SOC_DEVICE_MUX_NFC_INT_EN BIT(25)
117#define GENCONF_CLK_GATING_CTRL 0x220
118#define GENCONF_CLK_GATING_CTRL_ND_GATE BIT(2)
119#define GENCONF_ND_CLK_CTRL 0x700
120#define GENCONF_ND_CLK_CTRL_EN BIT(0)
121
122/* NAND controller data flash control register */
123#define NDCR 0x00
124#define NDCR_ALL_INT GENMASK(11, 0)
125#define NDCR_CS1_CMDDM BIT(7)
126#define NDCR_CS0_CMDDM BIT(8)
127#define NDCR_RDYM BIT(11)
128#define NDCR_ND_ARB_EN BIT(12)
129#define NDCR_RA_START BIT(15)
130#define NDCR_RD_ID_CNT(x) (min_t(unsigned int, x, 0x7) << 16)
131#define NDCR_PAGE_SZ(x) (x >= 2048 ? BIT(24) : 0)
132#define NDCR_DWIDTH_M BIT(26)
133#define NDCR_DWIDTH_C BIT(27)
134#define NDCR_ND_RUN BIT(28)
135#define NDCR_DMA_EN BIT(29)
136#define NDCR_ECC_EN BIT(30)
137#define NDCR_SPARE_EN BIT(31)
138#define NDCR_GENERIC_FIELDS_MASK (~(NDCR_RA_START | NDCR_PAGE_SZ(2048) | \
139 NDCR_DWIDTH_M | NDCR_DWIDTH_C))
140
141/* NAND interface timing parameter 0 register */
142#define NDTR0 0x04
143#define NDTR0_TRP(x) ((min_t(unsigned int, x, 0xF) & 0x7) << 0)
144#define NDTR0_TRH(x) (min_t(unsigned int, x, 0x7) << 3)
145#define NDTR0_ETRP(x) ((min_t(unsigned int, x, 0xF) & 0x8) << 3)
146#define NDTR0_SEL_NRE_EDGE BIT(7)
147#define NDTR0_TWP(x) (min_t(unsigned int, x, 0x7) << 8)
148#define NDTR0_TWH(x) (min_t(unsigned int, x, 0x7) << 11)
149#define NDTR0_TCS(x) (min_t(unsigned int, x, 0x7) << 16)
150#define NDTR0_TCH(x) (min_t(unsigned int, x, 0x7) << 19)
151#define NDTR0_RD_CNT_DEL(x) (min_t(unsigned int, x, 0xF) << 22)
152#define NDTR0_SELCNTR BIT(26)
153#define NDTR0_TADL(x) (min_t(unsigned int, x, 0x1F) << 27)
154
155/* NAND interface timing parameter 1 register */
156#define NDTR1 0x0C
157#define NDTR1_TAR(x) (min_t(unsigned int, x, 0xF) << 0)
158#define NDTR1_TWHR(x) (min_t(unsigned int, x, 0xF) << 4)
159#define NDTR1_TRHW(x) (min_t(unsigned int, x / 16, 0x3) << 8)
160#define NDTR1_PRESCALE BIT(14)
161#define NDTR1_WAIT_MODE BIT(15)
162#define NDTR1_TR(x) (min_t(unsigned int, x, 0xFFFF) << 16)
163
164/* NAND controller status register */
165#define NDSR 0x14
166#define NDSR_WRCMDREQ BIT(0)
167#define NDSR_RDDREQ BIT(1)
168#define NDSR_WRDREQ BIT(2)
169#define NDSR_CORERR BIT(3)
170#define NDSR_UNCERR BIT(4)
171#define NDSR_CMDD(cs) BIT(8 - cs)
172#define NDSR_RDY(rb) BIT(11 + rb)
173#define NDSR_ERRCNT(x) ((x >> 16) & 0x1F)
174
175/* NAND ECC control register */
176#define NDECCCTRL 0x28
177#define NDECCCTRL_BCH_EN BIT(0)
178
179/* NAND controller data buffer register */
180#define NDDB 0x40
181
182/* NAND controller command buffer 0 register */
183#define NDCB0 0x48
184#define NDCB0_CMD1(x) ((x & 0xFF) << 0)
185#define NDCB0_CMD2(x) ((x & 0xFF) << 8)
186#define NDCB0_ADDR_CYC(x) ((x & 0x7) << 16)
187#define NDCB0_ADDR_GET_NUM_CYC(x) (((x) >> 16) & 0x7)
188#define NDCB0_DBC BIT(19)
189#define NDCB0_CMD_TYPE(x) ((x & 0x7) << 21)
190#define NDCB0_CSEL BIT(24)
191#define NDCB0_RDY_BYP BIT(27)
192#define NDCB0_LEN_OVRD BIT(28)
193#define NDCB0_CMD_XTYPE(x) ((x & 0x7) << 29)
194
195/* NAND controller command buffer 1 register */
196#define NDCB1 0x4C
197#define NDCB1_COLS(x) ((x & 0xFFFF) << 0)
198#define NDCB1_ADDRS_PAGE(x) (x << 16)
199
200/* NAND controller command buffer 2 register */
201#define NDCB2 0x50
202#define NDCB2_ADDR5_PAGE(x) (((x >> 16) & 0xFF) << 0)
203#define NDCB2_ADDR5_CYC(x) ((x & 0xFF) << 0)
204
205/* NAND controller command buffer 3 register */
206#define NDCB3 0x54
207#define NDCB3_ADDR6_CYC(x) ((x & 0xFF) << 16)
208#define NDCB3_ADDR7_CYC(x) ((x & 0xFF) << 24)
209
210/* NAND controller command buffer 0 register 'type' and 'xtype' fields */
211#define TYPE_READ 0
212#define TYPE_WRITE 1
213#define TYPE_ERASE 2
214#define TYPE_READ_ID 3
215#define TYPE_STATUS 4
216#define TYPE_RESET 5
217#define TYPE_NAKED_CMD 6
218#define TYPE_NAKED_ADDR 7
219#define TYPE_MASK 7
220#define XTYPE_MONOLITHIC_RW 0
221#define XTYPE_LAST_NAKED_RW 1
222#define XTYPE_FINAL_COMMAND 3
223#define XTYPE_READ 4
224#define XTYPE_WRITE_DISPATCH 4
225#define XTYPE_NAKED_RW 5
226#define XTYPE_COMMAND_DISPATCH 6
227#define XTYPE_MASK 7
228
229/**
230 * Marvell ECC engine works differently than the others, in order to limit the
231 * size of the IP, hardware engineers chose to set a fixed strength at 16 bits
232 * per subpage, and depending on a the desired strength needed by the NAND chip,
233 * a particular layout mixing data/spare/ecc is defined, with a possible last
234 * chunk smaller that the others.
235 *
236 * @writesize: Full page size on which the layout applies
237 * @chunk: Desired ECC chunk size on which the layout applies
238 * @strength: Desired ECC strength (per chunk size bytes) on which the
239 * layout applies
240 * @nchunks: Total number of chunks
241 * @full_chunk_cnt: Number of full-sized chunks, which is the number of
242 * repetitions of the pattern:
243 * (data_bytes + spare_bytes + ecc_bytes).
244 * @data_bytes: Number of data bytes per chunk
245 * @spare_bytes: Number of spare bytes per chunk
246 * @ecc_bytes: Number of ecc bytes per chunk
247 * @last_data_bytes: Number of data bytes in the last chunk
248 * @last_spare_bytes: Number of spare bytes in the last chunk
249 * @last_ecc_bytes: Number of ecc bytes in the last chunk
250 */
251struct marvell_hw_ecc_layout {
252 /* Constraints */
253 int writesize;
254 int chunk;
255 int strength;
256 /* Corresponding layout */
257 int nchunks;
258 int full_chunk_cnt;
259 int data_bytes;
260 int spare_bytes;
261 int ecc_bytes;
262 int last_data_bytes;
263 int last_spare_bytes;
264 int last_ecc_bytes;
265};
266
267#define MARVELL_LAYOUT(ws, dc, ds, nc, fcc, db, sb, eb, ldb, lsb, leb) \
268 { \
269 .writesize = ws, \
270 .chunk = dc, \
271 .strength = ds, \
272 .nchunks = nc, \
273 .full_chunk_cnt = fcc, \
274 .data_bytes = db, \
275 .spare_bytes = sb, \
276 .ecc_bytes = eb, \
277 .last_data_bytes = ldb, \
278 .last_spare_bytes = lsb, \
279 .last_ecc_bytes = leb, \
280 }
281
282/* Layouts explained in AN-379_Marvell_SoC_NFC_ECC */
283static const struct marvell_hw_ecc_layout marvell_nfc_layouts[] = {
284 MARVELL_LAYOUT( 512, 512, 1, 1, 1, 512, 8, 8, 0, 0, 0),
285 MARVELL_LAYOUT( 2048, 512, 1, 1, 1, 2048, 40, 24, 0, 0, 0),
286 MARVELL_LAYOUT( 2048, 512, 4, 1, 1, 2048, 32, 30, 0, 0, 0),
287 MARVELL_LAYOUT( 4096, 512, 4, 2, 2, 2048, 32, 30, 0, 0, 0),
288 MARVELL_LAYOUT( 4096, 512, 8, 5, 4, 1024, 0, 30, 0, 64, 30),
289};
290
291/**
292 * The Nand Flash Controller has up to 4 CE and 2 RB pins. The CE selection
293 * is made by a field in NDCB0 register, and in another field in NDCB2 register.
294 * The datasheet describes the logic with an error: ADDR5 field is once
295 * declared at the beginning of NDCB2, and another time at its end. Because the
296 * ADDR5 field of NDCB2 may be used by other bytes, it would be more logical
297 * to use the last bit of this field instead of the first ones.
298 *
299 * @cs: Wanted CE lane.
300 * @ndcb0_csel: Value of the NDCB0 register with or without the flag
301 * selecting the wanted CE lane. This is set once when
302 * the Device Tree is probed.
303 * @rb: Ready/Busy pin for the flash chip
304 */
305struct marvell_nand_chip_sel {
306 unsigned int cs;
307 u32 ndcb0_csel;
308 unsigned int rb;
309};
310
311/**
312 * NAND chip structure: stores NAND chip device related information
313 *
314 * @chip: Base NAND chip structure
315 * @node: Used to store NAND chips into a list
316 * @layout NAND layout when using hardware ECC
317 * @ndcr: Controller register value for this NAND chip
318 * @ndtr0: Timing registers 0 value for this NAND chip
319 * @ndtr1: Timing registers 1 value for this NAND chip
320 * @selected_die: Current active CS
321 * @nsels: Number of CS lines required by the NAND chip
322 * @sels: Array of CS lines descriptions
323 */
324struct marvell_nand_chip {
325 struct nand_chip chip;
326 struct list_head node;
327 const struct marvell_hw_ecc_layout *layout;
328 u32 ndcr;
329 u32 ndtr0;
330 u32 ndtr1;
331 int addr_cyc;
332 int selected_die;
333 unsigned int nsels;
334 struct marvell_nand_chip_sel sels[0];
335};
336
337static inline struct marvell_nand_chip *to_marvell_nand(struct nand_chip *chip)
338{
339 return container_of(chip, struct marvell_nand_chip, chip);
340}
341
342static inline struct marvell_nand_chip_sel *to_nand_sel(struct marvell_nand_chip
343 *nand)
344{
345 return &nand->sels[nand->selected_die];
346}
347
348/**
349 * NAND controller capabilities for distinction between compatible strings
350 *
351 * @max_cs_nb: Number of Chip Select lines available
352 * @max_rb_nb: Number of Ready/Busy lines available
353 * @need_system_controller: Indicates if the SoC needs to have access to the
354 * system controller (ie. to enable the NAND controller)
355 * @legacy_of_bindings: Indicates if DT parsing must be done using the old
356 * fashion way
357 * @is_nfcv2: NFCv2 has numerous enhancements compared to NFCv1, ie.
358 * BCH error detection and correction algorithm,
359 * NDCB3 register has been added
360 * @use_dma: Use dma for data transfers
361 */
362struct marvell_nfc_caps {
363 unsigned int max_cs_nb;
364 unsigned int max_rb_nb;
365 bool need_system_controller;
366 bool legacy_of_bindings;
367 bool is_nfcv2;
368 bool use_dma;
369};
370
371/**
372 * NAND controller structure: stores Marvell NAND controller information
373 *
374 * @controller: Base controller structure
375 * @dev: Parent device (used to print error messages)
376 * @regs: NAND controller registers
6b6de654 377 * @core_clk: Core clock
961ba15c 378 * @reg_clk: Regiters clock
02f26ecf
MR
379 * @complete: Completion object to wait for NAND controller events
380 * @assigned_cs: Bitmask describing already assigned CS lines
381 * @chips: List containing all the NAND chips attached to
382 * this NAND controller
383 * @caps: NAND controller capabilities for each compatible string
384 * @dma_chan: DMA channel (NFCv1 only)
385 * @dma_buf: 32-bit aligned buffer for DMA transfers (NFCv1 only)
386 */
387struct marvell_nfc {
7da45139 388 struct nand_controller controller;
02f26ecf
MR
389 struct device *dev;
390 void __iomem *regs;
6b6de654 391 struct clk *core_clk;
961ba15c 392 struct clk *reg_clk;
02f26ecf
MR
393 struct completion complete;
394 unsigned long assigned_cs;
395 struct list_head chips;
396 struct nand_chip *selected_chip;
397 const struct marvell_nfc_caps *caps;
398
399 /* DMA (NFCv1 only) */
400 bool use_dma;
401 struct dma_chan *dma_chan;
402 u8 *dma_buf;
403};
404
7da45139 405static inline struct marvell_nfc *to_marvell_nfc(struct nand_controller *ctrl)
02f26ecf
MR
406{
407 return container_of(ctrl, struct marvell_nfc, controller);
408}
409
410/**
411 * NAND controller timings expressed in NAND Controller clock cycles
412 *
413 * @tRP: ND_nRE pulse width
414 * @tRH: ND_nRE high duration
415 * @tWP: ND_nWE pulse time
416 * @tWH: ND_nWE high duration
417 * @tCS: Enable signal setup time
418 * @tCH: Enable signal hold time
419 * @tADL: Address to write data delay
420 * @tAR: ND_ALE low to ND_nRE low delay
421 * @tWHR: ND_nWE high to ND_nRE low for status read
422 * @tRHW: ND_nRE high duration, read to write delay
423 * @tR: ND_nWE high to ND_nRE low for read
424 */
425struct marvell_nfc_timings {
426 /* NDTR0 fields */
427 unsigned int tRP;
428 unsigned int tRH;
429 unsigned int tWP;
430 unsigned int tWH;
431 unsigned int tCS;
432 unsigned int tCH;
433 unsigned int tADL;
434 /* NDTR1 fields */
435 unsigned int tAR;
436 unsigned int tWHR;
437 unsigned int tRHW;
438 unsigned int tR;
439};
440
441/**
442 * Derives a duration in numbers of clock cycles.
443 *
444 * @ps: Duration in pico-seconds
445 * @period_ns: Clock period in nano-seconds
446 *
447 * Convert the duration in nano-seconds, then divide by the period and
448 * return the number of clock periods.
449 */
450#define TO_CYCLES(ps, period_ns) (DIV_ROUND_UP(ps / 1000, period_ns))
07ad5a72
MR
451#define TO_CYCLES64(ps, period_ns) (DIV_ROUND_UP_ULL(div_u64(ps, 1000), \
452 period_ns))
02f26ecf
MR
453
454/**
455 * NAND driver structure filled during the parsing of the ->exec_op() subop
456 * subset of instructions.
457 *
458 * @ndcb: Array of values written to NDCBx registers
459 * @cle_ale_delay_ns: Optional delay after the last CMD or ADDR cycle
460 * @rdy_timeout_ms: Timeout for waits on Ready/Busy pin
461 * @rdy_delay_ns: Optional delay after waiting for the RB pin
462 * @data_delay_ns: Optional delay after the data xfer
463 * @data_instr_idx: Index of the data instruction in the subop
464 * @data_instr: Pointer to the data instruction in the subop
465 */
466struct marvell_nfc_op {
467 u32 ndcb[4];
468 unsigned int cle_ale_delay_ns;
469 unsigned int rdy_timeout_ms;
470 unsigned int rdy_delay_ns;
471 unsigned int data_delay_ns;
472 unsigned int data_instr_idx;
473 const struct nand_op_instr *data_instr;
474};
475
476/*
477 * Internal helper to conditionnally apply a delay (from the above structure,
478 * most of the time).
479 */
480static void cond_delay(unsigned int ns)
481{
482 if (!ns)
483 return;
484
485 if (ns < 10000)
486 ndelay(ns);
487 else
488 udelay(DIV_ROUND_UP(ns, 1000));
489}
490
491/*
492 * The controller has many flags that could generate interrupts, most of them
493 * are disabled and polling is used. For the very slow signals, using interrupts
494 * may relax the CPU charge.
495 */
496static void marvell_nfc_disable_int(struct marvell_nfc *nfc, u32 int_mask)
497{
498 u32 reg;
499
500 /* Writing 1 disables the interrupt */
501 reg = readl_relaxed(nfc->regs + NDCR);
502 writel_relaxed(reg | int_mask, nfc->regs + NDCR);
503}
504
505static void marvell_nfc_enable_int(struct marvell_nfc *nfc, u32 int_mask)
506{
507 u32 reg;
508
509 /* Writing 0 enables the interrupt */
510 reg = readl_relaxed(nfc->regs + NDCR);
511 writel_relaxed(reg & ~int_mask, nfc->regs + NDCR);
512}
513
514static void marvell_nfc_clear_int(struct marvell_nfc *nfc, u32 int_mask)
515{
516 writel_relaxed(int_mask, nfc->regs + NDSR);
517}
518
519static void marvell_nfc_force_byte_access(struct nand_chip *chip,
520 bool force_8bit)
521{
522 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
523 u32 ndcr;
524
525 /*
526 * Callers of this function do not verify if the NAND is using a 16-bit
527 * an 8-bit bus for normal operations, so we need to take care of that
528 * here by leaving the configuration unchanged if the NAND does not have
529 * the NAND_BUSWIDTH_16 flag set.
530 */
531 if (!(chip->options & NAND_BUSWIDTH_16))
532 return;
533
534 ndcr = readl_relaxed(nfc->regs + NDCR);
535
536 if (force_8bit)
537 ndcr &= ~(NDCR_DWIDTH_M | NDCR_DWIDTH_C);
538 else
539 ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
540
541 writel_relaxed(ndcr, nfc->regs + NDCR);
542}
543
544static int marvell_nfc_wait_ndrun(struct nand_chip *chip)
545{
546 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
547 u32 val;
548 int ret;
549
550 /*
551 * The command is being processed, wait for the ND_RUN bit to be
552 * cleared by the NFC. If not, we must clear it by hand.
553 */
554 ret = readl_relaxed_poll_timeout(nfc->regs + NDCR, val,
555 (val & NDCR_ND_RUN) == 0,
556 POLL_PERIOD, POLL_TIMEOUT);
557 if (ret) {
558 dev_err(nfc->dev, "Timeout on NAND controller run mode\n");
559 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
560 nfc->regs + NDCR);
561 return ret;
562 }
563
564 return 0;
565}
566
567/*
568 * Any time a command has to be sent to the controller, the following sequence
569 * has to be followed:
570 * - call marvell_nfc_prepare_cmd()
571 * -> activate the ND_RUN bit that will kind of 'start a job'
572 * -> wait the signal indicating the NFC is waiting for a command
573 * - send the command (cmd and address cycles)
574 * - enventually send or receive the data
575 * - call marvell_nfc_end_cmd() with the corresponding flag
576 * -> wait the flag to be triggered or cancel the job with a timeout
577 *
578 * The following helpers are here to factorize the code a bit so that
579 * specialized functions responsible for executing the actual NAND
580 * operations do not have to replicate the same code blocks.
581 */
582static int marvell_nfc_prepare_cmd(struct nand_chip *chip)
583{
584 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
585 u32 ndcr, val;
586 int ret;
587
588 /* Poll ND_RUN and clear NDSR before issuing any command */
589 ret = marvell_nfc_wait_ndrun(chip);
590 if (ret) {
a76497dc 591 dev_err(nfc->dev, "Last operation did not succeed\n");
02f26ecf
MR
592 return ret;
593 }
594
595 ndcr = readl_relaxed(nfc->regs + NDCR);
596 writel_relaxed(readl(nfc->regs + NDSR), nfc->regs + NDSR);
597
598 /* Assert ND_RUN bit and wait the NFC to be ready */
599 writel_relaxed(ndcr | NDCR_ND_RUN, nfc->regs + NDCR);
600 ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
601 val & NDSR_WRCMDREQ,
602 POLL_PERIOD, POLL_TIMEOUT);
603 if (ret) {
604 dev_err(nfc->dev, "Timeout on WRCMDRE\n");
605 return -ETIMEDOUT;
606 }
607
608 /* Command may be written, clear WRCMDREQ status bit */
609 writel_relaxed(NDSR_WRCMDREQ, nfc->regs + NDSR);
610
611 return 0;
612}
613
614static void marvell_nfc_send_cmd(struct nand_chip *chip,
615 struct marvell_nfc_op *nfc_op)
616{
617 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
618 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
619
620 dev_dbg(nfc->dev, "\nNDCR: 0x%08x\n"
621 "NDCB0: 0x%08x\nNDCB1: 0x%08x\nNDCB2: 0x%08x\nNDCB3: 0x%08x\n",
622 (u32)readl_relaxed(nfc->regs + NDCR), nfc_op->ndcb[0],
623 nfc_op->ndcb[1], nfc_op->ndcb[2], nfc_op->ndcb[3]);
624
625 writel_relaxed(to_nand_sel(marvell_nand)->ndcb0_csel | nfc_op->ndcb[0],
626 nfc->regs + NDCB0);
627 writel_relaxed(nfc_op->ndcb[1], nfc->regs + NDCB0);
628 writel(nfc_op->ndcb[2], nfc->regs + NDCB0);
629
630 /*
631 * Write NDCB0 four times only if LEN_OVRD is set or if ADDR6 or ADDR7
632 * fields are used (only available on NFCv2).
633 */
634 if (nfc_op->ndcb[0] & NDCB0_LEN_OVRD ||
635 NDCB0_ADDR_GET_NUM_CYC(nfc_op->ndcb[0]) >= 6) {
636 if (!WARN_ON_ONCE(!nfc->caps->is_nfcv2))
637 writel(nfc_op->ndcb[3], nfc->regs + NDCB0);
638 }
639}
640
641static int marvell_nfc_end_cmd(struct nand_chip *chip, int flag,
642 const char *label)
643{
644 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
645 u32 val;
646 int ret;
647
648 ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
649 val & flag,
650 POLL_PERIOD, POLL_TIMEOUT);
651
652 if (ret) {
653 dev_err(nfc->dev, "Timeout on %s (NDSR: 0x%08x)\n",
654 label, val);
655 if (nfc->dma_chan)
656 dmaengine_terminate_all(nfc->dma_chan);
657 return ret;
658 }
659
660 /*
661 * DMA function uses this helper to poll on CMDD bits without wanting
662 * them to be cleared.
663 */
664 if (nfc->use_dma && (readl_relaxed(nfc->regs + NDCR) & NDCR_DMA_EN))
665 return 0;
666
667 writel_relaxed(flag, nfc->regs + NDSR);
668
669 return 0;
670}
671
672static int marvell_nfc_wait_cmdd(struct nand_chip *chip)
673{
674 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
675 int cs_flag = NDSR_CMDD(to_nand_sel(marvell_nand)->ndcb0_csel);
676
677 return marvell_nfc_end_cmd(chip, cs_flag, "CMDD");
678}
679
680static int marvell_nfc_wait_op(struct nand_chip *chip, unsigned int timeout_ms)
681{
682 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
683 int ret;
684
685 /* Timeout is expressed in ms */
686 if (!timeout_ms)
687 timeout_ms = IRQ_TIMEOUT;
688
689 init_completion(&nfc->complete);
690
691 marvell_nfc_enable_int(nfc, NDCR_RDYM);
692 ret = wait_for_completion_timeout(&nfc->complete,
693 msecs_to_jiffies(timeout_ms));
694 marvell_nfc_disable_int(nfc, NDCR_RDYM);
695 marvell_nfc_clear_int(nfc, NDSR_RDY(0) | NDSR_RDY(1));
696 if (!ret) {
697 dev_err(nfc->dev, "Timeout waiting for RB signal\n");
698 return -ETIMEDOUT;
699 }
700
701 return 0;
702}
703
758b56f5 704static void marvell_nfc_select_chip(struct nand_chip *chip, int die_nr)
02f26ecf 705{
02f26ecf
MR
706 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
707 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
708 u32 ndcr_generic;
709
710 if (chip == nfc->selected_chip && die_nr == marvell_nand->selected_die)
711 return;
712
713 if (die_nr < 0 || die_nr >= marvell_nand->nsels) {
714 nfc->selected_chip = NULL;
715 marvell_nand->selected_die = -1;
716 return;
717 }
718
02f26ecf
MR
719 writel_relaxed(marvell_nand->ndtr0, nfc->regs + NDTR0);
720 writel_relaxed(marvell_nand->ndtr1, nfc->regs + NDTR1);
721
722 /*
723 * Reset the NDCR register to a clean state for this particular chip,
724 * also clear ND_RUN bit.
725 */
726 ndcr_generic = readl_relaxed(nfc->regs + NDCR) &
727 NDCR_GENERIC_FIELDS_MASK & ~NDCR_ND_RUN;
728 writel_relaxed(ndcr_generic | marvell_nand->ndcr, nfc->regs + NDCR);
729
730 /* Also reset the interrupt status register */
731 marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
732
733 nfc->selected_chip = chip;
734 marvell_nand->selected_die = die_nr;
735}
736
737static irqreturn_t marvell_nfc_isr(int irq, void *dev_id)
738{
739 struct marvell_nfc *nfc = dev_id;
740 u32 st = readl_relaxed(nfc->regs + NDSR);
741 u32 ien = (~readl_relaxed(nfc->regs + NDCR)) & NDCR_ALL_INT;
742
743 /*
744 * RDY interrupt mask is one bit in NDCR while there are two status
745 * bit in NDSR (RDY[cs0/cs2] and RDY[cs1/cs3]).
746 */
747 if (st & NDSR_RDY(1))
748 st |= NDSR_RDY(0);
749
750 if (!(st & ien))
751 return IRQ_NONE;
752
753 marvell_nfc_disable_int(nfc, st & NDCR_ALL_INT);
754
755 if (!(st & (NDSR_RDDREQ | NDSR_WRDREQ | NDSR_WRCMDREQ)))
756 complete(&nfc->complete);
757
758 return IRQ_HANDLED;
759}
760
761/* HW ECC related functions */
762static void marvell_nfc_enable_hw_ecc(struct nand_chip *chip)
763{
764 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
765 u32 ndcr = readl_relaxed(nfc->regs + NDCR);
766
767 if (!(ndcr & NDCR_ECC_EN)) {
768 writel_relaxed(ndcr | NDCR_ECC_EN, nfc->regs + NDCR);
769
770 /*
771 * When enabling BCH, set threshold to 0 to always know the
772 * number of corrected bitflips.
773 */
774 if (chip->ecc.algo == NAND_ECC_BCH)
775 writel_relaxed(NDECCCTRL_BCH_EN, nfc->regs + NDECCCTRL);
776 }
777}
778
779static void marvell_nfc_disable_hw_ecc(struct nand_chip *chip)
780{
781 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
782 u32 ndcr = readl_relaxed(nfc->regs + NDCR);
783
784 if (ndcr & NDCR_ECC_EN) {
785 writel_relaxed(ndcr & ~NDCR_ECC_EN, nfc->regs + NDCR);
786 if (chip->ecc.algo == NAND_ECC_BCH)
787 writel_relaxed(0, nfc->regs + NDECCCTRL);
788 }
789}
790
791/* DMA related helpers */
792static void marvell_nfc_enable_dma(struct marvell_nfc *nfc)
793{
794 u32 reg;
795
796 reg = readl_relaxed(nfc->regs + NDCR);
797 writel_relaxed(reg | NDCR_DMA_EN, nfc->regs + NDCR);
798}
799
800static void marvell_nfc_disable_dma(struct marvell_nfc *nfc)
801{
802 u32 reg;
803
804 reg = readl_relaxed(nfc->regs + NDCR);
805 writel_relaxed(reg & ~NDCR_DMA_EN, nfc->regs + NDCR);
806}
807
808/* Read/write PIO/DMA accessors */
809static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,
810 enum dma_data_direction direction,
811 unsigned int len)
812{
813 unsigned int dma_len = min_t(int, ALIGN(len, 32), MAX_CHUNK_SIZE);
814 struct dma_async_tx_descriptor *tx;
815 struct scatterlist sg;
816 dma_cookie_t cookie;
817 int ret;
818
819 marvell_nfc_enable_dma(nfc);
820 /* Prepare the DMA transfer */
821 sg_init_one(&sg, nfc->dma_buf, dma_len);
822 dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
823 tx = dmaengine_prep_slave_sg(nfc->dma_chan, &sg, 1,
824 direction == DMA_FROM_DEVICE ?
825 DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
826 DMA_PREP_INTERRUPT);
827 if (!tx) {
828 dev_err(nfc->dev, "Could not prepare DMA S/G list\n");
829 return -ENXIO;
830 }
831
832 /* Do the task and wait for it to finish */
833 cookie = dmaengine_submit(tx);
834 ret = dma_submit_error(cookie);
835 if (ret)
836 return -EIO;
837
838 dma_async_issue_pending(nfc->dma_chan);
839 ret = marvell_nfc_wait_cmdd(nfc->selected_chip);
840 dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
841 marvell_nfc_disable_dma(nfc);
842 if (ret) {
843 dev_err(nfc->dev, "Timeout waiting for DMA (status: %d)\n",
844 dmaengine_tx_status(nfc->dma_chan, cookie, NULL));
845 dmaengine_terminate_all(nfc->dma_chan);
846 return -ETIMEDOUT;
847 }
848
849 return 0;
850}
851
852static int marvell_nfc_xfer_data_in_pio(struct marvell_nfc *nfc, u8 *in,
853 unsigned int len)
854{
855 unsigned int last_len = len % FIFO_DEPTH;
856 unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
857 int i;
858
859 for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
860 ioread32_rep(nfc->regs + NDDB, in + i, FIFO_REP(FIFO_DEPTH));
861
862 if (last_len) {
863 u8 tmp_buf[FIFO_DEPTH];
864
865 ioread32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
866 memcpy(in + last_full_offset, tmp_buf, last_len);
867 }
868
869 return 0;
870}
871
872static int marvell_nfc_xfer_data_out_pio(struct marvell_nfc *nfc, const u8 *out,
873 unsigned int len)
874{
875 unsigned int last_len = len % FIFO_DEPTH;
876 unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
877 int i;
878
879 for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
880 iowrite32_rep(nfc->regs + NDDB, out + i, FIFO_REP(FIFO_DEPTH));
881
882 if (last_len) {
883 u8 tmp_buf[FIFO_DEPTH];
884
885 memcpy(tmp_buf, out + last_full_offset, last_len);
886 iowrite32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
887 }
888
889 return 0;
890}
891
892static void marvell_nfc_check_empty_chunk(struct nand_chip *chip,
893 u8 *data, int data_len,
894 u8 *spare, int spare_len,
895 u8 *ecc, int ecc_len,
896 unsigned int *max_bitflips)
897{
898 struct mtd_info *mtd = nand_to_mtd(chip);
899 int bf;
900
901 /*
902 * Blank pages (all 0xFF) that have not been written may be recognized
903 * as bad if bitflips occur, so whenever an uncorrectable error occurs,
904 * check if the entire page (with ECC bytes) is actually blank or not.
905 */
906 if (!data)
907 data_len = 0;
908 if (!spare)
909 spare_len = 0;
910 if (!ecc)
911 ecc_len = 0;
912
913 bf = nand_check_erased_ecc_chunk(data, data_len, ecc, ecc_len,
914 spare, spare_len, chip->ecc.strength);
915 if (bf < 0) {
916 mtd->ecc_stats.failed++;
917 return;
918 }
919
920 /* Update the stats and max_bitflips */
921 mtd->ecc_stats.corrected += bf;
922 *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
923}
924
925/*
926 * Check a chunk is correct or not according to hardware ECC engine.
927 * mtd->ecc_stats.corrected is updated, as well as max_bitflips, however
928 * mtd->ecc_stats.failure is not, the function will instead return a non-zero
929 * value indicating that a check on the emptyness of the subpage must be
930 * performed before declaring the subpage corrupted.
931 */
932static int marvell_nfc_hw_ecc_correct(struct nand_chip *chip,
933 unsigned int *max_bitflips)
934{
935 struct mtd_info *mtd = nand_to_mtd(chip);
936 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
937 int bf = 0;
938 u32 ndsr;
939
940 ndsr = readl_relaxed(nfc->regs + NDSR);
941
942 /* Check uncorrectable error flag */
943 if (ndsr & NDSR_UNCERR) {
944 writel_relaxed(ndsr, nfc->regs + NDSR);
945
946 /*
947 * Do not increment ->ecc_stats.failed now, instead, return a
948 * non-zero value to indicate that this chunk was apparently
949 * bad, and it should be check to see if it empty or not. If
950 * the chunk (with ECC bytes) is not declared empty, the calling
951 * function must increment the failure count.
952 */
953 return -EBADMSG;
954 }
955
956 /* Check correctable error flag */
957 if (ndsr & NDSR_CORERR) {
958 writel_relaxed(ndsr, nfc->regs + NDSR);
959
960 if (chip->ecc.algo == NAND_ECC_BCH)
961 bf = NDSR_ERRCNT(ndsr);
962 else
963 bf = 1;
964 }
965
966 /* Update the stats and max_bitflips */
967 mtd->ecc_stats.corrected += bf;
968 *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
969
970 return 0;
971}
972
973/* Hamming read helpers */
974static int marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip *chip,
975 u8 *data_buf, u8 *oob_buf,
976 bool raw, int page)
977{
978 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
979 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
980 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
981 struct marvell_nfc_op nfc_op = {
982 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
983 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
984 NDCB0_DBC |
985 NDCB0_CMD1(NAND_CMD_READ0) |
986 NDCB0_CMD2(NAND_CMD_READSTART),
987 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
988 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
989 };
990 unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
991 int ret;
992
993 /* NFCv2 needs more information about the operation being executed */
994 if (nfc->caps->is_nfcv2)
995 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
996
997 ret = marvell_nfc_prepare_cmd(chip);
998 if (ret)
999 return ret;
1000
1001 marvell_nfc_send_cmd(chip, &nfc_op);
1002 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1003 "RDDREQ while draining FIFO (data/oob)");
1004 if (ret)
1005 return ret;
1006
1007 /*
1008 * Read the page then the OOB area. Unlike what is shown in current
1009 * documentation, spare bytes are protected by the ECC engine, and must
1010 * be at the beginning of the OOB area or running this driver on legacy
1011 * systems will prevent the discovery of the BBM/BBT.
1012 */
1013 if (nfc->use_dma) {
1014 marvell_nfc_xfer_data_dma(nfc, DMA_FROM_DEVICE,
1015 lt->data_bytes + oob_bytes);
1016 memcpy(data_buf, nfc->dma_buf, lt->data_bytes);
1017 memcpy(oob_buf, nfc->dma_buf + lt->data_bytes, oob_bytes);
1018 } else {
1019 marvell_nfc_xfer_data_in_pio(nfc, data_buf, lt->data_bytes);
1020 marvell_nfc_xfer_data_in_pio(nfc, oob_buf, oob_bytes);
1021 }
1022
1023 ret = marvell_nfc_wait_cmdd(chip);
1024
1025 return ret;
1026}
1027
b9761687 1028static int marvell_nfc_hw_ecc_hmg_read_page_raw(struct nand_chip *chip, u8 *buf,
02f26ecf
MR
1029 int oob_required, int page)
1030{
1031 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
1032 true, page);
1033}
1034
b9761687
BB
1035static int marvell_nfc_hw_ecc_hmg_read_page(struct nand_chip *chip, u8 *buf,
1036 int oob_required, int page)
02f26ecf
MR
1037{
1038 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1039 unsigned int full_sz = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1040 int max_bitflips = 0, ret;
1041 u8 *raw_buf;
1042
1043 marvell_nfc_enable_hw_ecc(chip);
1044 marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, false,
1045 page);
1046 ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
1047 marvell_nfc_disable_hw_ecc(chip);
1048
1049 if (!ret)
1050 return max_bitflips;
1051
1052 /*
1053 * When ECC failures are detected, check if the full page has been
1054 * written or not. Ignore the failure if it is actually empty.
1055 */
1056 raw_buf = kmalloc(full_sz, GFP_KERNEL);
1057 if (!raw_buf)
1058 return -ENOMEM;
1059
1060 marvell_nfc_hw_ecc_hmg_do_read_page(chip, raw_buf, raw_buf +
1061 lt->data_bytes, true, page);
1062 marvell_nfc_check_empty_chunk(chip, raw_buf, full_sz, NULL, 0, NULL, 0,
1063 &max_bitflips);
1064 kfree(raw_buf);
1065
1066 return max_bitflips;
1067}
1068
1069/*
1070 * Spare area in Hamming layouts is not protected by the ECC engine (even if
1071 * it appears before the ECC bytes when reading), the ->read_oob_raw() function
1072 * also stands for ->read_oob().
1073 */
b9761687 1074static int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct nand_chip *chip, int page)
02f26ecf
MR
1075{
1076 /* Invalidate page cache */
1077 chip->pagebuf = -1;
1078
1079 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, chip->data_buf,
1080 chip->oob_poi, true, page);
1081}
1082
1083/* Hamming write helpers */
1084static int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip *chip,
1085 const u8 *data_buf,
1086 const u8 *oob_buf, bool raw,
1087 int page)
1088{
1089 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1090 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1091 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1092 struct marvell_nfc_op nfc_op = {
1093 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) |
1094 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1095 NDCB0_CMD1(NAND_CMD_SEQIN) |
1096 NDCB0_CMD2(NAND_CMD_PAGEPROG) |
1097 NDCB0_DBC,
1098 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
1099 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
1100 };
1101 unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
1102 int ret;
1103
1104 /* NFCv2 needs more information about the operation being executed */
1105 if (nfc->caps->is_nfcv2)
1106 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
1107
1108 ret = marvell_nfc_prepare_cmd(chip);
1109 if (ret)
1110 return ret;
1111
1112 marvell_nfc_send_cmd(chip, &nfc_op);
1113 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1114 "WRDREQ while loading FIFO (data)");
1115 if (ret)
1116 return ret;
1117
1118 /* Write the page then the OOB area */
1119 if (nfc->use_dma) {
1120 memcpy(nfc->dma_buf, data_buf, lt->data_bytes);
1121 memcpy(nfc->dma_buf + lt->data_bytes, oob_buf, oob_bytes);
1122 marvell_nfc_xfer_data_dma(nfc, DMA_TO_DEVICE, lt->data_bytes +
1123 lt->ecc_bytes + lt->spare_bytes);
1124 } else {
1125 marvell_nfc_xfer_data_out_pio(nfc, data_buf, lt->data_bytes);
1126 marvell_nfc_xfer_data_out_pio(nfc, oob_buf, oob_bytes);
1127 }
1128
1129 ret = marvell_nfc_wait_cmdd(chip);
1130 if (ret)
1131 return ret;
1132
1133 ret = marvell_nfc_wait_op(chip,
b76401fc 1134 PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max));
02f26ecf
MR
1135 return ret;
1136}
1137
767eb6fb 1138static int marvell_nfc_hw_ecc_hmg_write_page_raw(struct nand_chip *chip,
02f26ecf
MR
1139 const u8 *buf,
1140 int oob_required, int page)
1141{
1142 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1143 true, page);
1144}
1145
767eb6fb 1146static int marvell_nfc_hw_ecc_hmg_write_page(struct nand_chip *chip,
02f26ecf
MR
1147 const u8 *buf,
1148 int oob_required, int page)
1149{
1150 int ret;
1151
1152 marvell_nfc_enable_hw_ecc(chip);
1153 ret = marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1154 false, page);
1155 marvell_nfc_disable_hw_ecc(chip);
1156
1157 return ret;
1158}
1159
1160/*
1161 * Spare area in Hamming layouts is not protected by the ECC engine (even if
1162 * it appears before the ECC bytes when reading), the ->write_oob_raw() function
1163 * also stands for ->write_oob().
1164 */
767eb6fb 1165static int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct nand_chip *chip,
02f26ecf
MR
1166 int page)
1167{
767eb6fb
BB
1168 struct mtd_info *mtd = nand_to_mtd(chip);
1169
02f26ecf
MR
1170 /* Invalidate page cache */
1171 chip->pagebuf = -1;
1172
1173 memset(chip->data_buf, 0xFF, mtd->writesize);
1174
1175 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, chip->data_buf,
1176 chip->oob_poi, true, page);
1177}
1178
1179/* BCH read helpers */
b9761687 1180static int marvell_nfc_hw_ecc_bch_read_page_raw(struct nand_chip *chip, u8 *buf,
02f26ecf
MR
1181 int oob_required, int page)
1182{
b9761687 1183 struct mtd_info *mtd = nand_to_mtd(chip);
02f26ecf
MR
1184 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1185 u8 *oob = chip->oob_poi;
1186 int chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1187 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1188 lt->last_spare_bytes;
1189 int data_len = lt->data_bytes;
1190 int spare_len = lt->spare_bytes;
1191 int ecc_len = lt->ecc_bytes;
1192 int chunk;
1193
1194 if (oob_required)
1195 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1196
1197 nand_read_page_op(chip, page, 0, NULL, 0);
1198
1199 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1200 /* Update last chunk length */
1201 if (chunk >= lt->full_chunk_cnt) {
1202 data_len = lt->last_data_bytes;
1203 spare_len = lt->last_spare_bytes;
1204 ecc_len = lt->last_ecc_bytes;
1205 }
1206
1207 /* Read data bytes*/
1208 nand_change_read_column_op(chip, chunk * chunk_size,
1209 buf + (lt->data_bytes * chunk),
1210 data_len, false);
1211
1212 /* Read spare bytes */
1213 nand_read_data_op(chip, oob + (lt->spare_bytes * chunk),
1214 spare_len, false);
1215
1216 /* Read ECC bytes */
1217 nand_read_data_op(chip, oob + ecc_offset +
1218 (ALIGN(lt->ecc_bytes, 32) * chunk),
1219 ecc_len, false);
1220 }
1221
1222 return 0;
1223}
1224
1225static void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip *chip, int chunk,
1226 u8 *data, unsigned int data_len,
1227 u8 *spare, unsigned int spare_len,
1228 int page)
1229{
1230 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1231 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1232 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1233 int i, ret;
1234 struct marvell_nfc_op nfc_op = {
1235 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
1236 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1237 NDCB0_LEN_OVRD,
1238 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
1239 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
1240 .ndcb[3] = data_len + spare_len,
1241 };
1242
1243 ret = marvell_nfc_prepare_cmd(chip);
1244 if (ret)
1245 return;
1246
1247 if (chunk == 0)
1248 nfc_op.ndcb[0] |= NDCB0_DBC |
1249 NDCB0_CMD1(NAND_CMD_READ0) |
1250 NDCB0_CMD2(NAND_CMD_READSTART);
1251
1252 /*
90d61763
BB
1253 * Trigger the monolithic read on the first chunk, then naked read on
1254 * intermediate chunks and finally a last naked read on the last chunk.
02f26ecf 1255 */
90d61763 1256 if (chunk == 0)
02f26ecf 1257 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
90d61763
BB
1258 else if (chunk < lt->nchunks - 1)
1259 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
02f26ecf
MR
1260 else
1261 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1262
1263 marvell_nfc_send_cmd(chip, &nfc_op);
1264
1265 /*
1266 * According to the datasheet, when reading from NDDB
1267 * with BCH enabled, after each 32 bytes reads, we
1268 * have to make sure that the NDSR.RDDREQ bit is set.
1269 *
1270 * Drain the FIFO, 8 32-bit reads at a time, and skip
1271 * the polling on the last read.
1272 *
1273 * Length is a multiple of 32 bytes, hence it is a multiple of 8 too.
1274 */
1275 for (i = 0; i < data_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1276 marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1277 "RDDREQ while draining FIFO (data)");
1278 marvell_nfc_xfer_data_in_pio(nfc, data,
1279 FIFO_DEPTH * BCH_SEQ_READS);
1280 data += FIFO_DEPTH * BCH_SEQ_READS;
1281 }
1282
1283 for (i = 0; i < spare_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1284 marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1285 "RDDREQ while draining FIFO (OOB)");
1286 marvell_nfc_xfer_data_in_pio(nfc, spare,
1287 FIFO_DEPTH * BCH_SEQ_READS);
1288 spare += FIFO_DEPTH * BCH_SEQ_READS;
1289 }
1290}
1291
b9761687 1292static int marvell_nfc_hw_ecc_bch_read_page(struct nand_chip *chip,
02f26ecf
MR
1293 u8 *buf, int oob_required,
1294 int page)
1295{
b9761687 1296 struct mtd_info *mtd = nand_to_mtd(chip);
02f26ecf 1297 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
dbfc6718
MR
1298 int data_len = lt->data_bytes, spare_len = lt->spare_bytes;
1299 u8 *data = buf, *spare = chip->oob_poi;
02f26ecf
MR
1300 int max_bitflips = 0;
1301 u32 failure_mask = 0;
dbfc6718 1302 int chunk, ret;
02f26ecf
MR
1303
1304 /*
1305 * With BCH, OOB is not fully used (and thus not read entirely), not
1306 * expected bytes could show up at the end of the OOB buffer if not
1307 * explicitly erased.
1308 */
1309 if (oob_required)
1310 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1311
1312 marvell_nfc_enable_hw_ecc(chip);
1313
1314 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1315 /* Update length for the last chunk */
1316 if (chunk >= lt->full_chunk_cnt) {
1317 data_len = lt->last_data_bytes;
1318 spare_len = lt->last_spare_bytes;
1319 }
1320
1321 /* Read the chunk and detect number of bitflips */
1322 marvell_nfc_hw_ecc_bch_read_chunk(chip, chunk, data, data_len,
1323 spare, spare_len, page);
1324 ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
1325 if (ret)
1326 failure_mask |= BIT(chunk);
1327
1328 data += data_len;
1329 spare += spare_len;
1330 }
1331
1332 marvell_nfc_disable_hw_ecc(chip);
1333
1334 if (!failure_mask)
1335 return max_bitflips;
1336
1337 /*
1338 * Please note that dumping the ECC bytes during a normal read with OOB
1339 * area would add a significant overhead as ECC bytes are "consumed" by
1340 * the controller in normal mode and must be re-read in raw mode. To
1341 * avoid dropping the performances, we prefer not to include them. The
1342 * user should re-read the page in raw mode if ECC bytes are required.
dbfc6718
MR
1343 */
1344
1345 /*
1346 * In case there is any subpage read error reported by ->correct(), we
1347 * usually re-read only ECC bytes in raw mode and check if the whole
1348 * page is empty. In this case, it is normal that the ECC check failed
1349 * and we just ignore the error.
02f26ecf
MR
1350 *
1351 * However, for any subpage read error reported by ->correct(), the ECC
1352 * bytes must be read in raw mode and the full subpage must be checked
1353 * to see if it is entirely empty of if there was an actual error.
1354 */
1355 for (chunk = 0; chunk < lt->nchunks; chunk++) {
dbfc6718
MR
1356 int data_off_in_page, spare_off_in_page, ecc_off_in_page;
1357 int data_off, spare_off, ecc_off;
1358 int data_len, spare_len, ecc_len;
1359
02f26ecf
MR
1360 /* No failure reported for this chunk, move to the next one */
1361 if (!(failure_mask & BIT(chunk)))
1362 continue;
1363
dbfc6718
MR
1364 data_off_in_page = chunk * (lt->data_bytes + lt->spare_bytes +
1365 lt->ecc_bytes);
1366 spare_off_in_page = data_off_in_page +
1367 (chunk < lt->full_chunk_cnt ? lt->data_bytes :
1368 lt->last_data_bytes);
1369 ecc_off_in_page = spare_off_in_page +
1370 (chunk < lt->full_chunk_cnt ? lt->spare_bytes :
1371 lt->last_spare_bytes);
1372
1373 data_off = chunk * lt->data_bytes;
1374 spare_off = chunk * lt->spare_bytes;
1375 ecc_off = (lt->full_chunk_cnt * lt->spare_bytes) +
1376 lt->last_spare_bytes +
1377 (chunk * (lt->ecc_bytes + 2));
1378
1379 data_len = chunk < lt->full_chunk_cnt ? lt->data_bytes :
1380 lt->last_data_bytes;
1381 spare_len = chunk < lt->full_chunk_cnt ? lt->spare_bytes :
1382 lt->last_spare_bytes;
1383 ecc_len = chunk < lt->full_chunk_cnt ? lt->ecc_bytes :
1384 lt->last_ecc_bytes;
1385
1386 nand_change_read_column_op(chip, ecc_off_in_page,
1387 chip->oob_poi + ecc_off, ecc_len,
1388 false);
02f26ecf
MR
1389
1390 /* Check the entire chunk (data + spare + ecc) for emptyness */
dbfc6718
MR
1391 marvell_nfc_check_empty_chunk(chip, buf + data_off, data_len,
1392 chip->oob_poi + spare_off, spare_len,
1393 chip->oob_poi + ecc_off, ecc_len,
02f26ecf
MR
1394 &max_bitflips);
1395 }
1396
1397 return max_bitflips;
1398}
1399
b9761687 1400static int marvell_nfc_hw_ecc_bch_read_oob_raw(struct nand_chip *chip, int page)
02f26ecf
MR
1401{
1402 /* Invalidate page cache */
1403 chip->pagebuf = -1;
1404
b9761687 1405 return chip->ecc.read_page_raw(chip, chip->data_buf, true, page);
02f26ecf
MR
1406}
1407
b9761687 1408static int marvell_nfc_hw_ecc_bch_read_oob(struct nand_chip *chip, int page)
02f26ecf
MR
1409{
1410 /* Invalidate page cache */
1411 chip->pagebuf = -1;
1412
b9761687 1413 return chip->ecc.read_page(chip, chip->data_buf, true, page);
02f26ecf
MR
1414}
1415
1416/* BCH write helpers */
767eb6fb 1417static int marvell_nfc_hw_ecc_bch_write_page_raw(struct nand_chip *chip,
02f26ecf
MR
1418 const u8 *buf,
1419 int oob_required, int page)
1420{
1421 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1422 int full_chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1423 int data_len = lt->data_bytes;
1424 int spare_len = lt->spare_bytes;
1425 int ecc_len = lt->ecc_bytes;
02f26ecf
MR
1426 int spare_offset = 0;
1427 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1428 lt->last_spare_bytes;
1429 int chunk;
1430
1431 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1432
1433 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1434 if (chunk >= lt->full_chunk_cnt) {
1435 data_len = lt->last_data_bytes;
1436 spare_len = lt->last_spare_bytes;
1437 ecc_len = lt->last_ecc_bytes;
02f26ecf
MR
1438 }
1439
1440 /* Point to the column of the next chunk */
1441 nand_change_write_column_op(chip, chunk * full_chunk_size,
1442 NULL, 0, false);
1443
1444 /* Write the data */
1445 nand_write_data_op(chip, buf + (chunk * lt->data_bytes),
1446 data_len, false);
1447
1448 if (!oob_required)
1449 continue;
1450
1451 /* Write the spare bytes */
1452 if (spare_len)
1453 nand_write_data_op(chip, chip->oob_poi + spare_offset,
1454 spare_len, false);
1455
1456 /* Write the ECC bytes */
1457 if (ecc_len)
1458 nand_write_data_op(chip, chip->oob_poi + ecc_offset,
1459 ecc_len, false);
1460
1461 spare_offset += spare_len;
1462 ecc_offset += ALIGN(ecc_len, 32);
1463 }
1464
1465 return nand_prog_page_end_op(chip);
1466}
1467
1468static int
1469marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip *chip, int chunk,
1470 const u8 *data, unsigned int data_len,
1471 const u8 *spare, unsigned int spare_len,
1472 int page)
1473{
1474 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1475 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1476 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
a2ee41fd 1477 u32 xtype;
02f26ecf
MR
1478 int ret;
1479 struct marvell_nfc_op nfc_op = {
1480 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | NDCB0_LEN_OVRD,
1481 .ndcb[3] = data_len + spare_len,
1482 };
1483
1484 /*
1485 * First operation dispatches the CMD_SEQIN command, issue the address
1486 * cycles and asks for the first chunk of data.
1487 * All operations in the middle (if any) will issue a naked write and
1488 * also ask for data.
1489 * Last operation (if any) asks for the last chunk of data through a
1490 * last naked write.
1491 */
1492 if (chunk == 0) {
a2ee41fd
MR
1493 if (lt->nchunks == 1)
1494 xtype = XTYPE_MONOLITHIC_RW;
1495 else
1496 xtype = XTYPE_WRITE_DISPATCH;
1497
1498 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(xtype) |
02f26ecf
MR
1499 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1500 NDCB0_CMD1(NAND_CMD_SEQIN);
1501 nfc_op.ndcb[1] |= NDCB1_ADDRS_PAGE(page);
1502 nfc_op.ndcb[2] |= NDCB2_ADDR5_PAGE(page);
1503 } else if (chunk < lt->nchunks - 1) {
1504 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
1505 } else {
1506 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1507 }
1508
1509 /* Always dispatch the PAGEPROG command on the last chunk */
1510 if (chunk == lt->nchunks - 1)
1511 nfc_op.ndcb[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG) | NDCB0_DBC;
1512
1513 ret = marvell_nfc_prepare_cmd(chip);
1514 if (ret)
1515 return ret;
1516
1517 marvell_nfc_send_cmd(chip, &nfc_op);
1518 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1519 "WRDREQ while loading FIFO (data)");
1520 if (ret)
1521 return ret;
1522
1523 /* Transfer the contents */
1524 iowrite32_rep(nfc->regs + NDDB, data, FIFO_REP(data_len));
1525 iowrite32_rep(nfc->regs + NDDB, spare, FIFO_REP(spare_len));
1526
1527 return 0;
1528}
1529
767eb6fb 1530static int marvell_nfc_hw_ecc_bch_write_page(struct nand_chip *chip,
02f26ecf
MR
1531 const u8 *buf,
1532 int oob_required, int page)
1533{
767eb6fb 1534 struct mtd_info *mtd = nand_to_mtd(chip);
02f26ecf
MR
1535 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1536 const u8 *data = buf;
1537 const u8 *spare = chip->oob_poi;
1538 int data_len = lt->data_bytes;
1539 int spare_len = lt->spare_bytes;
1540 int chunk, ret;
1541
1542 /* Spare data will be written anyway, so clear it to avoid garbage */
1543 if (!oob_required)
1544 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1545
1546 marvell_nfc_enable_hw_ecc(chip);
1547
1548 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1549 if (chunk >= lt->full_chunk_cnt) {
1550 data_len = lt->last_data_bytes;
1551 spare_len = lt->last_spare_bytes;
1552 }
1553
1554 marvell_nfc_hw_ecc_bch_write_chunk(chip, chunk, data, data_len,
1555 spare, spare_len, page);
1556 data += data_len;
1557 spare += spare_len;
1558
1559 /*
1560 * Waiting only for CMDD or PAGED is not enough, ECC are
1561 * partially written. No flag is set once the operation is
1562 * really finished but the ND_RUN bit is cleared, so wait for it
1563 * before stepping into the next command.
1564 */
1565 marvell_nfc_wait_ndrun(chip);
1566 }
1567
1568 ret = marvell_nfc_wait_op(chip,
b76401fc 1569 PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max));
02f26ecf
MR
1570
1571 marvell_nfc_disable_hw_ecc(chip);
1572
1573 if (ret)
1574 return ret;
1575
1576 return 0;
1577}
1578
767eb6fb 1579static int marvell_nfc_hw_ecc_bch_write_oob_raw(struct nand_chip *chip,
02f26ecf
MR
1580 int page)
1581{
767eb6fb
BB
1582 struct mtd_info *mtd = nand_to_mtd(chip);
1583
02f26ecf
MR
1584 /* Invalidate page cache */
1585 chip->pagebuf = -1;
1586
1587 memset(chip->data_buf, 0xFF, mtd->writesize);
1588
767eb6fb 1589 return chip->ecc.write_page_raw(chip, chip->data_buf, true, page);
02f26ecf
MR
1590}
1591
767eb6fb 1592static int marvell_nfc_hw_ecc_bch_write_oob(struct nand_chip *chip, int page)
02f26ecf 1593{
767eb6fb
BB
1594 struct mtd_info *mtd = nand_to_mtd(chip);
1595
02f26ecf
MR
1596 /* Invalidate page cache */
1597 chip->pagebuf = -1;
1598
1599 memset(chip->data_buf, 0xFF, mtd->writesize);
1600
767eb6fb 1601 return chip->ecc.write_page(chip, chip->data_buf, true, page);
02f26ecf
MR
1602}
1603
1604/* NAND framework ->exec_op() hooks and related helpers */
1605static void marvell_nfc_parse_instructions(struct nand_chip *chip,
1606 const struct nand_subop *subop,
1607 struct marvell_nfc_op *nfc_op)
1608{
1609 const struct nand_op_instr *instr = NULL;
1610 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1611 bool first_cmd = true;
1612 unsigned int op_id;
1613 int i;
1614
1615 /* Reset the input structure as most of its fields will be OR'ed */
1616 memset(nfc_op, 0, sizeof(struct marvell_nfc_op));
1617
1618 for (op_id = 0; op_id < subop->ninstrs; op_id++) {
1619 unsigned int offset, naddrs;
1620 const u8 *addrs;
1621 int len = nand_subop_get_data_len(subop, op_id);
1622
1623 instr = &subop->instrs[op_id];
1624
1625 switch (instr->type) {
1626 case NAND_OP_CMD_INSTR:
1627 if (first_cmd)
1628 nfc_op->ndcb[0] |=
1629 NDCB0_CMD1(instr->ctx.cmd.opcode);
1630 else
1631 nfc_op->ndcb[0] |=
1632 NDCB0_CMD2(instr->ctx.cmd.opcode) |
1633 NDCB0_DBC;
1634
1635 nfc_op->cle_ale_delay_ns = instr->delay_ns;
1636 first_cmd = false;
1637 break;
1638
1639 case NAND_OP_ADDR_INSTR:
1640 offset = nand_subop_get_addr_start_off(subop, op_id);
1641 naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
1642 addrs = &instr->ctx.addr.addrs[offset];
1643
1644 nfc_op->ndcb[0] |= NDCB0_ADDR_CYC(naddrs);
1645
1646 for (i = 0; i < min_t(unsigned int, 4, naddrs); i++)
1647 nfc_op->ndcb[1] |= addrs[i] << (8 * i);
1648
1649 if (naddrs >= 5)
1650 nfc_op->ndcb[2] |= NDCB2_ADDR5_CYC(addrs[4]);
1651 if (naddrs >= 6)
1652 nfc_op->ndcb[3] |= NDCB3_ADDR6_CYC(addrs[5]);
1653 if (naddrs == 7)
1654 nfc_op->ndcb[3] |= NDCB3_ADDR7_CYC(addrs[6]);
1655
1656 nfc_op->cle_ale_delay_ns = instr->delay_ns;
1657 break;
1658
1659 case NAND_OP_DATA_IN_INSTR:
1660 nfc_op->data_instr = instr;
1661 nfc_op->data_instr_idx = op_id;
1662 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ);
1663 if (nfc->caps->is_nfcv2) {
1664 nfc_op->ndcb[0] |=
1665 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1666 NDCB0_LEN_OVRD;
1667 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1668 }
1669 nfc_op->data_delay_ns = instr->delay_ns;
1670 break;
1671
1672 case NAND_OP_DATA_OUT_INSTR:
1673 nfc_op->data_instr = instr;
1674 nfc_op->data_instr_idx = op_id;
1675 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE);
1676 if (nfc->caps->is_nfcv2) {
1677 nfc_op->ndcb[0] |=
1678 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1679 NDCB0_LEN_OVRD;
1680 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1681 }
1682 nfc_op->data_delay_ns = instr->delay_ns;
1683 break;
1684
1685 case NAND_OP_WAITRDY_INSTR:
1686 nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
1687 nfc_op->rdy_delay_ns = instr->delay_ns;
1688 break;
1689 }
1690 }
1691}
1692
1693static int marvell_nfc_xfer_data_pio(struct nand_chip *chip,
1694 const struct nand_subop *subop,
1695 struct marvell_nfc_op *nfc_op)
1696{
1697 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1698 const struct nand_op_instr *instr = nfc_op->data_instr;
1699 unsigned int op_id = nfc_op->data_instr_idx;
1700 unsigned int len = nand_subop_get_data_len(subop, op_id);
1701 unsigned int offset = nand_subop_get_data_start_off(subop, op_id);
1702 bool reading = (instr->type == NAND_OP_DATA_IN_INSTR);
1703 int ret;
1704
1705 if (instr->ctx.data.force_8bit)
1706 marvell_nfc_force_byte_access(chip, true);
1707
1708 if (reading) {
1709 u8 *in = instr->ctx.data.buf.in + offset;
1710
1711 ret = marvell_nfc_xfer_data_in_pio(nfc, in, len);
1712 } else {
1713 const u8 *out = instr->ctx.data.buf.out + offset;
1714
1715 ret = marvell_nfc_xfer_data_out_pio(nfc, out, len);
1716 }
1717
1718 if (instr->ctx.data.force_8bit)
1719 marvell_nfc_force_byte_access(chip, false);
1720
1721 return ret;
1722}
1723
1724static int marvell_nfc_monolithic_access_exec(struct nand_chip *chip,
1725 const struct nand_subop *subop)
1726{
1727 struct marvell_nfc_op nfc_op;
1728 bool reading;
1729 int ret;
1730
1731 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1732 reading = (nfc_op.data_instr->type == NAND_OP_DATA_IN_INSTR);
1733
1734 ret = marvell_nfc_prepare_cmd(chip);
1735 if (ret)
1736 return ret;
1737
1738 marvell_nfc_send_cmd(chip, &nfc_op);
1739 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1740 "RDDREQ/WRDREQ while draining raw data");
1741 if (ret)
1742 return ret;
1743
1744 cond_delay(nfc_op.cle_ale_delay_ns);
1745
1746 if (reading) {
1747 if (nfc_op.rdy_timeout_ms) {
1748 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1749 if (ret)
1750 return ret;
1751 }
1752
1753 cond_delay(nfc_op.rdy_delay_ns);
1754 }
1755
1756 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1757 ret = marvell_nfc_wait_cmdd(chip);
1758 if (ret)
1759 return ret;
1760
1761 cond_delay(nfc_op.data_delay_ns);
1762
1763 if (!reading) {
1764 if (nfc_op.rdy_timeout_ms) {
1765 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1766 if (ret)
1767 return ret;
1768 }
1769
1770 cond_delay(nfc_op.rdy_delay_ns);
1771 }
1772
1773 /*
1774 * NDCR ND_RUN bit should be cleared automatically at the end of each
1775 * operation but experience shows that the behavior is buggy when it
1776 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1777 */
1778 if (!reading) {
1779 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1780
1781 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1782 nfc->regs + NDCR);
1783 }
1784
1785 return 0;
1786}
1787
1788static int marvell_nfc_naked_access_exec(struct nand_chip *chip,
1789 const struct nand_subop *subop)
1790{
1791 struct marvell_nfc_op nfc_op;
1792 int ret;
1793
1794 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1795
1796 /*
1797 * Naked access are different in that they need to be flagged as naked
1798 * by the controller. Reset the controller registers fields that inform
1799 * on the type and refill them according to the ongoing operation.
1800 */
1801 nfc_op.ndcb[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK) |
1802 NDCB0_CMD_XTYPE(XTYPE_MASK));
1803 switch (subop->instrs[0].type) {
1804 case NAND_OP_CMD_INSTR:
1805 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD);
1806 break;
1807 case NAND_OP_ADDR_INSTR:
1808 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR);
1809 break;
1810 case NAND_OP_DATA_IN_INSTR:
1811 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ) |
1812 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1813 break;
1814 case NAND_OP_DATA_OUT_INSTR:
1815 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE) |
1816 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1817 break;
1818 default:
1819 /* This should never happen */
1820 break;
1821 }
1822
1823 ret = marvell_nfc_prepare_cmd(chip);
1824 if (ret)
1825 return ret;
1826
1827 marvell_nfc_send_cmd(chip, &nfc_op);
1828
1829 if (!nfc_op.data_instr) {
1830 ret = marvell_nfc_wait_cmdd(chip);
1831 cond_delay(nfc_op.cle_ale_delay_ns);
1832 return ret;
1833 }
1834
1835 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1836 "RDDREQ/WRDREQ while draining raw data");
1837 if (ret)
1838 return ret;
1839
1840 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1841 ret = marvell_nfc_wait_cmdd(chip);
1842 if (ret)
1843 return ret;
1844
1845 /*
1846 * NDCR ND_RUN bit should be cleared automatically at the end of each
1847 * operation but experience shows that the behavior is buggy when it
1848 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1849 */
1850 if (subop->instrs[0].type == NAND_OP_DATA_OUT_INSTR) {
1851 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1852
1853 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1854 nfc->regs + NDCR);
1855 }
1856
1857 return 0;
1858}
1859
1860static int marvell_nfc_naked_waitrdy_exec(struct nand_chip *chip,
1861 const struct nand_subop *subop)
1862{
1863 struct marvell_nfc_op nfc_op;
1864 int ret;
1865
1866 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1867
1868 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1869 cond_delay(nfc_op.rdy_delay_ns);
1870
1871 return ret;
1872}
1873
1874static int marvell_nfc_read_id_type_exec(struct nand_chip *chip,
1875 const struct nand_subop *subop)
1876{
1877 struct marvell_nfc_op nfc_op;
1878 int ret;
1879
1880 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1881 nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1882 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ_ID);
1883
1884 ret = marvell_nfc_prepare_cmd(chip);
1885 if (ret)
1886 return ret;
1887
1888 marvell_nfc_send_cmd(chip, &nfc_op);
1889 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1890 "RDDREQ while reading ID");
1891 if (ret)
1892 return ret;
1893
1894 cond_delay(nfc_op.cle_ale_delay_ns);
1895
1896 if (nfc_op.rdy_timeout_ms) {
1897 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1898 if (ret)
1899 return ret;
1900 }
1901
1902 cond_delay(nfc_op.rdy_delay_ns);
1903
1904 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1905 ret = marvell_nfc_wait_cmdd(chip);
1906 if (ret)
1907 return ret;
1908
1909 cond_delay(nfc_op.data_delay_ns);
1910
1911 return 0;
1912}
1913
1914static int marvell_nfc_read_status_exec(struct nand_chip *chip,
1915 const struct nand_subop *subop)
1916{
1917 struct marvell_nfc_op nfc_op;
1918 int ret;
1919
1920 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1921 nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1922 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_STATUS);
1923
1924 ret = marvell_nfc_prepare_cmd(chip);
1925 if (ret)
1926 return ret;
1927
1928 marvell_nfc_send_cmd(chip, &nfc_op);
1929 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1930 "RDDREQ while reading status");
1931 if (ret)
1932 return ret;
1933
1934 cond_delay(nfc_op.cle_ale_delay_ns);
1935
1936 if (nfc_op.rdy_timeout_ms) {
1937 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1938 if (ret)
1939 return ret;
1940 }
1941
1942 cond_delay(nfc_op.rdy_delay_ns);
1943
1944 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1945 ret = marvell_nfc_wait_cmdd(chip);
1946 if (ret)
1947 return ret;
1948
1949 cond_delay(nfc_op.data_delay_ns);
1950
1951 return 0;
1952}
1953
1954static int marvell_nfc_reset_cmd_type_exec(struct nand_chip *chip,
1955 const struct nand_subop *subop)
1956{
1957 struct marvell_nfc_op nfc_op;
1958 int ret;
1959
1960 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1961 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_RESET);
1962
1963 ret = marvell_nfc_prepare_cmd(chip);
1964 if (ret)
1965 return ret;
1966
1967 marvell_nfc_send_cmd(chip, &nfc_op);
1968 ret = marvell_nfc_wait_cmdd(chip);
1969 if (ret)
1970 return ret;
1971
1972 cond_delay(nfc_op.cle_ale_delay_ns);
1973
1974 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1975 if (ret)
1976 return ret;
1977
1978 cond_delay(nfc_op.rdy_delay_ns);
1979
1980 return 0;
1981}
1982
1983static int marvell_nfc_erase_cmd_type_exec(struct nand_chip *chip,
1984 const struct nand_subop *subop)
1985{
1986 struct marvell_nfc_op nfc_op;
1987 int ret;
1988
1989 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1990 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_ERASE);
1991
1992 ret = marvell_nfc_prepare_cmd(chip);
1993 if (ret)
1994 return ret;
1995
1996 marvell_nfc_send_cmd(chip, &nfc_op);
1997 ret = marvell_nfc_wait_cmdd(chip);
1998 if (ret)
1999 return ret;
2000
2001 cond_delay(nfc_op.cle_ale_delay_ns);
2002
2003 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
2004 if (ret)
2005 return ret;
2006
2007 cond_delay(nfc_op.rdy_delay_ns);
2008
2009 return 0;
2010}
2011
2012static const struct nand_op_parser marvell_nfcv2_op_parser = NAND_OP_PARSER(
2013 /* Monolithic reads/writes */
2014 NAND_OP_PARSER_PATTERN(
2015 marvell_nfc_monolithic_access_exec,
2016 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2017 NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2),
2018 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2019 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
2020 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
2021 NAND_OP_PARSER_PATTERN(
2022 marvell_nfc_monolithic_access_exec,
2023 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2024 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2),
2025 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE),
2026 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2027 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
2028 /* Naked commands */
2029 NAND_OP_PARSER_PATTERN(
2030 marvell_nfc_naked_access_exec,
2031 NAND_OP_PARSER_PAT_CMD_ELEM(false)),
2032 NAND_OP_PARSER_PATTERN(
2033 marvell_nfc_naked_access_exec,
2034 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2)),
2035 NAND_OP_PARSER_PATTERN(
2036 marvell_nfc_naked_access_exec,
2037 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
2038 NAND_OP_PARSER_PATTERN(
2039 marvell_nfc_naked_access_exec,
2040 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE)),
2041 NAND_OP_PARSER_PATTERN(
2042 marvell_nfc_naked_waitrdy_exec,
2043 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2044 );
2045
2046static const struct nand_op_parser marvell_nfcv1_op_parser = NAND_OP_PARSER(
2047 /* Naked commands not supported, use a function for each pattern */
2048 NAND_OP_PARSER_PATTERN(
2049 marvell_nfc_read_id_type_exec,
2050 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2051 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
2052 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 8)),
2053 NAND_OP_PARSER_PATTERN(
2054 marvell_nfc_erase_cmd_type_exec,
2055 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2056 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
2057 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2058 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2059 NAND_OP_PARSER_PATTERN(
2060 marvell_nfc_read_status_exec,
2061 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2062 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)),
2063 NAND_OP_PARSER_PATTERN(
2064 marvell_nfc_reset_cmd_type_exec,
2065 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2066 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2067 NAND_OP_PARSER_PATTERN(
2068 marvell_nfc_naked_waitrdy_exec,
2069 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2070 );
2071
2072static int marvell_nfc_exec_op(struct nand_chip *chip,
2073 const struct nand_operation *op,
2074 bool check_only)
2075{
2076 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2077
2078 if (nfc->caps->is_nfcv2)
2079 return nand_op_parser_exec_op(chip, &marvell_nfcv2_op_parser,
2080 op, check_only);
2081 else
2082 return nand_op_parser_exec_op(chip, &marvell_nfcv1_op_parser,
2083 op, check_only);
2084}
2085
2086/*
2087 * Layouts were broken in old pxa3xx_nand driver, these are supposed to be
2088 * usable.
2089 */
2090static int marvell_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2091 struct mtd_oob_region *oobregion)
2092{
2093 struct nand_chip *chip = mtd_to_nand(mtd);
2094 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2095
2096 if (section)
2097 return -ERANGE;
2098
2099 oobregion->length = (lt->full_chunk_cnt * lt->ecc_bytes) +
2100 lt->last_ecc_bytes;
2101 oobregion->offset = mtd->oobsize - oobregion->length;
2102
2103 return 0;
2104}
2105
2106static int marvell_nand_ooblayout_free(struct mtd_info *mtd, int section,
2107 struct mtd_oob_region *oobregion)
2108{
2109 struct nand_chip *chip = mtd_to_nand(mtd);
2110 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2111
2112 if (section)
2113 return -ERANGE;
2114
2115 /*
2116 * Bootrom looks in bytes 0 & 5 for bad blocks for the
2117 * 4KB page / 4bit BCH combination.
2118 */
2119 if (mtd->writesize == SZ_4K && lt->data_bytes == SZ_2K)
2120 oobregion->offset = 6;
2121 else
2122 oobregion->offset = 2;
2123
2124 oobregion->length = (lt->full_chunk_cnt * lt->spare_bytes) +
2125 lt->last_spare_bytes - oobregion->offset;
2126
2127 return 0;
2128}
2129
2130static const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops = {
2131 .ecc = marvell_nand_ooblayout_ecc,
2132 .free = marvell_nand_ooblayout_free,
2133};
2134
2135static int marvell_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
2136 struct nand_ecc_ctrl *ecc)
2137{
2138 struct nand_chip *chip = mtd_to_nand(mtd);
2139 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2140 const struct marvell_hw_ecc_layout *l;
2141 int i;
2142
2143 if (!nfc->caps->is_nfcv2 &&
2144 (mtd->writesize + mtd->oobsize > MAX_CHUNK_SIZE)) {
2145 dev_err(nfc->dev,
2146 "NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n",
2147 mtd->writesize, MAX_CHUNK_SIZE - mtd->oobsize);
2148 return -ENOTSUPP;
2149 }
2150
2151 to_marvell_nand(chip)->layout = NULL;
2152 for (i = 0; i < ARRAY_SIZE(marvell_nfc_layouts); i++) {
2153 l = &marvell_nfc_layouts[i];
2154 if (mtd->writesize == l->writesize &&
2155 ecc->size == l->chunk && ecc->strength == l->strength) {
2156 to_marvell_nand(chip)->layout = l;
2157 break;
2158 }
2159 }
2160
2161 if (!to_marvell_nand(chip)->layout ||
2162 (!nfc->caps->is_nfcv2 && ecc->strength > 1)) {
2163 dev_err(nfc->dev,
2164 "ECC strength %d at page size %d is not supported\n",
2165 ecc->strength, mtd->writesize);
2166 return -ENOTSUPP;
2167 }
2168
2169 mtd_set_ooblayout(mtd, &marvell_nand_ooblayout_ops);
2170 ecc->steps = l->nchunks;
2171 ecc->size = l->data_bytes;
2172
2173 if (ecc->strength == 1) {
2174 chip->ecc.algo = NAND_ECC_HAMMING;
2175 ecc->read_page_raw = marvell_nfc_hw_ecc_hmg_read_page_raw;
2176 ecc->read_page = marvell_nfc_hw_ecc_hmg_read_page;
2177 ecc->read_oob_raw = marvell_nfc_hw_ecc_hmg_read_oob_raw;
2178 ecc->read_oob = ecc->read_oob_raw;
2179 ecc->write_page_raw = marvell_nfc_hw_ecc_hmg_write_page_raw;
2180 ecc->write_page = marvell_nfc_hw_ecc_hmg_write_page;
2181 ecc->write_oob_raw = marvell_nfc_hw_ecc_hmg_write_oob_raw;
2182 ecc->write_oob = ecc->write_oob_raw;
2183 } else {
2184 chip->ecc.algo = NAND_ECC_BCH;
2185 ecc->strength = 16;
2186 ecc->read_page_raw = marvell_nfc_hw_ecc_bch_read_page_raw;
2187 ecc->read_page = marvell_nfc_hw_ecc_bch_read_page;
2188 ecc->read_oob_raw = marvell_nfc_hw_ecc_bch_read_oob_raw;
2189 ecc->read_oob = marvell_nfc_hw_ecc_bch_read_oob;
2190 ecc->write_page_raw = marvell_nfc_hw_ecc_bch_write_page_raw;
2191 ecc->write_page = marvell_nfc_hw_ecc_bch_write_page;
2192 ecc->write_oob_raw = marvell_nfc_hw_ecc_bch_write_oob_raw;
2193 ecc->write_oob = marvell_nfc_hw_ecc_bch_write_oob;
2194 }
2195
2196 return 0;
2197}
2198
2199static int marvell_nand_ecc_init(struct mtd_info *mtd,
2200 struct nand_ecc_ctrl *ecc)
2201{
2202 struct nand_chip *chip = mtd_to_nand(mtd);
2203 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2204 int ret;
2205
2206 if (ecc->mode != NAND_ECC_NONE && (!ecc->size || !ecc->strength)) {
2207 if (chip->ecc_step_ds && chip->ecc_strength_ds) {
2208 ecc->size = chip->ecc_step_ds;
2209 ecc->strength = chip->ecc_strength_ds;
2210 } else {
2211 dev_info(nfc->dev,
2212 "No minimum ECC strength, using 1b/512B\n");
2213 ecc->size = 512;
2214 ecc->strength = 1;
2215 }
2216 }
2217
2218 switch (ecc->mode) {
2219 case NAND_ECC_HW:
2220 ret = marvell_nand_hw_ecc_ctrl_init(mtd, ecc);
2221 if (ret)
2222 return ret;
2223 break;
2224 case NAND_ECC_NONE:
2225 case NAND_ECC_SOFT:
ed6d0285 2226 case NAND_ECC_ON_DIE:
02f26ecf
MR
2227 if (!nfc->caps->is_nfcv2 && mtd->writesize != SZ_512 &&
2228 mtd->writesize != SZ_2K) {
2229 dev_err(nfc->dev, "NFCv1 cannot write %d bytes pages\n",
2230 mtd->writesize);
2231 return -EINVAL;
2232 }
2233 break;
2234 default:
2235 return -EINVAL;
2236 }
2237
2238 return 0;
2239}
2240
2241static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
2242static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
2243
2244static struct nand_bbt_descr bbt_main_descr = {
2245 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2246 NAND_BBT_2BIT | NAND_BBT_VERSION,
2247 .offs = 8,
2248 .len = 6,
2249 .veroffs = 14,
2250 .maxblocks = 8, /* Last 8 blocks in each chip */
2251 .pattern = bbt_pattern
2252};
2253
2254static struct nand_bbt_descr bbt_mirror_descr = {
2255 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2256 NAND_BBT_2BIT | NAND_BBT_VERSION,
2257 .offs = 8,
2258 .len = 6,
2259 .veroffs = 14,
2260 .maxblocks = 8, /* Last 8 blocks in each chip */
2261 .pattern = bbt_mirror_pattern
2262};
2263
858838b8 2264static int marvell_nfc_setup_data_interface(struct nand_chip *chip, int chipnr,
02f26ecf
MR
2265 const struct nand_data_interface
2266 *conf)
2267{
02f26ecf
MR
2268 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2269 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
6b6de654 2270 unsigned int period_ns = 1000000000 / clk_get_rate(nfc->core_clk) * 2;
02f26ecf
MR
2271 const struct nand_sdr_timings *sdr;
2272 struct marvell_nfc_timings nfc_tmg;
2273 int read_delay;
2274
2275 sdr = nand_get_sdr_timings(conf);
2276 if (IS_ERR(sdr))
2277 return PTR_ERR(sdr);
2278
2279 /*
2280 * SDR timings are given in pico-seconds while NFC timings must be
2281 * expressed in NAND controller clock cycles, which is half of the
2282 * frequency of the accessible ECC clock retrieved by clk_get_rate().
2283 * This is not written anywhere in the datasheet but was observed
2284 * with an oscilloscope.
2285 *
2286 * NFC datasheet gives equations from which thoses calculations
2287 * are derived, they tend to be slightly more restrictives than the
2288 * given core timings and may improve the overall speed.
2289 */
2290 nfc_tmg.tRP = TO_CYCLES(DIV_ROUND_UP(sdr->tRC_min, 2), period_ns) - 1;
2291 nfc_tmg.tRH = nfc_tmg.tRP;
2292 nfc_tmg.tWP = TO_CYCLES(DIV_ROUND_UP(sdr->tWC_min, 2), period_ns) - 1;
2293 nfc_tmg.tWH = nfc_tmg.tWP;
2294 nfc_tmg.tCS = TO_CYCLES(sdr->tCS_min, period_ns);
2295 nfc_tmg.tCH = TO_CYCLES(sdr->tCH_min, period_ns) - 1;
2296 nfc_tmg.tADL = TO_CYCLES(sdr->tADL_min, period_ns);
2297 /*
2298 * Read delay is the time of propagation from SoC pins to NFC internal
2299 * logic. With non-EDO timings, this is MIN_RD_DEL_CNT clock cycles. In
2300 * EDO mode, an additional delay of tRH must be taken into account so
2301 * the data is sampled on the falling edge instead of the rising edge.
2302 */
2303 read_delay = sdr->tRC_min >= 30000 ?
2304 MIN_RD_DEL_CNT : MIN_RD_DEL_CNT + nfc_tmg.tRH;
2305
2306 nfc_tmg.tAR = TO_CYCLES(sdr->tAR_min, period_ns);
2307 /*
2308 * tWHR and tRHW are supposed to be read to write delays (and vice
2309 * versa) but in some cases, ie. when doing a change column, they must
2310 * be greater than that to be sure tCCS delay is respected.
2311 */
2312 nfc_tmg.tWHR = TO_CYCLES(max_t(int, sdr->tWHR_min, sdr->tCCS_min),
2313 period_ns) - 2,
2314 nfc_tmg.tRHW = TO_CYCLES(max_t(int, sdr->tRHW_min, sdr->tCCS_min),
2315 period_ns);
2316
07ad5a72
MR
2317 /*
2318 * NFCv2: Use WAIT_MODE (wait for RB line), do not rely only on delays.
2319 * NFCv1: No WAIT_MODE, tR must be maximal.
2320 */
2321 if (nfc->caps->is_nfcv2) {
2322 nfc_tmg.tR = TO_CYCLES(sdr->tWB_max, period_ns);
2323 } else {
2324 nfc_tmg.tR = TO_CYCLES64(sdr->tWB_max + sdr->tR_max,
2325 period_ns);
2326 if (nfc_tmg.tR + 3 > nfc_tmg.tCH)
2327 nfc_tmg.tR = nfc_tmg.tCH - 3;
2328 else
2329 nfc_tmg.tR = 0;
2330 }
02f26ecf
MR
2331
2332 if (chipnr < 0)
2333 return 0;
2334
2335 marvell_nand->ndtr0 =
2336 NDTR0_TRP(nfc_tmg.tRP) |
2337 NDTR0_TRH(nfc_tmg.tRH) |
2338 NDTR0_ETRP(nfc_tmg.tRP) |
2339 NDTR0_TWP(nfc_tmg.tWP) |
2340 NDTR0_TWH(nfc_tmg.tWH) |
2341 NDTR0_TCS(nfc_tmg.tCS) |
07ad5a72 2342 NDTR0_TCH(nfc_tmg.tCH);
02f26ecf
MR
2343
2344 marvell_nand->ndtr1 =
2345 NDTR1_TAR(nfc_tmg.tAR) |
2346 NDTR1_TWHR(nfc_tmg.tWHR) |
02f26ecf
MR
2347 NDTR1_TR(nfc_tmg.tR);
2348
07ad5a72
MR
2349 if (nfc->caps->is_nfcv2) {
2350 marvell_nand->ndtr0 |=
2351 NDTR0_RD_CNT_DEL(read_delay) |
2352 NDTR0_SELCNTR |
2353 NDTR0_TADL(nfc_tmg.tADL);
2354
2355 marvell_nand->ndtr1 |=
2356 NDTR1_TRHW(nfc_tmg.tRHW) |
2357 NDTR1_WAIT_MODE;
2358 }
2359
02f26ecf
MR
2360 return 0;
2361}
2362
8831e48b
MR
2363static int marvell_nand_attach_chip(struct nand_chip *chip)
2364{
2365 struct mtd_info *mtd = nand_to_mtd(chip);
2366 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2367 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2368 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(nfc->dev);
2369 int ret;
2370
2371 if (pdata && pdata->flash_bbt)
2372 chip->bbt_options |= NAND_BBT_USE_FLASH;
2373
2374 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
2375 /*
2376 * We'll use a bad block table stored in-flash and don't
2377 * allow writing the bad block marker to the flash.
2378 */
2379 chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
2380 chip->bbt_td = &bbt_main_descr;
2381 chip->bbt_md = &bbt_mirror_descr;
2382 }
2383
2384 /* Save the chip-specific fields of NDCR */
2385 marvell_nand->ndcr = NDCR_PAGE_SZ(mtd->writesize);
2386 if (chip->options & NAND_BUSWIDTH_16)
2387 marvell_nand->ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
2388
2389 /*
2390 * On small page NANDs, only one cycle is needed to pass the
2391 * column address.
2392 */
2393 if (mtd->writesize <= 512) {
2394 marvell_nand->addr_cyc = 1;
2395 } else {
2396 marvell_nand->addr_cyc = 2;
2397 marvell_nand->ndcr |= NDCR_RA_START;
2398 }
2399
2400 /*
2401 * Now add the number of cycles needed to pass the row
2402 * address.
2403 *
2404 * Addressing a chip using CS 2 or 3 should also need the third row
2405 * cycle but due to inconsistance in the documentation and lack of
2406 * hardware to test this situation, this case is not supported.
2407 */
2408 if (chip->options & NAND_ROW_ADDR_3)
2409 marvell_nand->addr_cyc += 3;
2410 else
2411 marvell_nand->addr_cyc += 2;
2412
2413 if (pdata) {
2414 chip->ecc.size = pdata->ecc_step_size;
2415 chip->ecc.strength = pdata->ecc_strength;
2416 }
2417
2418 ret = marvell_nand_ecc_init(mtd, &chip->ecc);
2419 if (ret) {
2420 dev_err(nfc->dev, "ECC init failed: %d\n", ret);
2421 return ret;
2422 }
2423
2424 if (chip->ecc.mode == NAND_ECC_HW) {
2425 /*
2426 * Subpage write not available with hardware ECC, prohibit also
2427 * subpage read as in userspace subpage access would still be
2428 * allowed and subpage write, if used, would lead to numerous
2429 * uncorrectable ECC errors.
2430 */
2431 chip->options |= NAND_NO_SUBPAGE_WRITE;
2432 }
2433
2434 if (pdata || nfc->caps->legacy_of_bindings) {
2435 /*
2436 * We keep the MTD name unchanged to avoid breaking platforms
2437 * where the MTD cmdline parser is used and the bootloader
2438 * has not been updated to use the new naming scheme.
2439 */
2440 mtd->name = "pxa3xx_nand-0";
2441 } else if (!mtd->name) {
2442 /*
2443 * If the new bindings are used and the bootloader has not been
2444 * updated to pass a new mtdparts parameter on the cmdline, you
2445 * should define the following property in your NAND node, ie:
2446 *
2447 * label = "main-storage";
2448 *
2449 * This way, mtd->name will be set by the core when
2450 * nand_set_flash_node() is called.
2451 */
2452 mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL,
2453 "%s:nand.%d", dev_name(nfc->dev),
2454 marvell_nand->sels[0].cs);
2455 if (!mtd->name) {
2456 dev_err(nfc->dev, "Failed to allocate mtd->name\n");
2457 return -ENOMEM;
2458 }
2459 }
2460
2461 return 0;
2462}
2463
2464static const struct nand_controller_ops marvell_nand_controller_ops = {
2465 .attach_chip = marvell_nand_attach_chip,
2466};
2467
02f26ecf
MR
2468static int marvell_nand_chip_init(struct device *dev, struct marvell_nfc *nfc,
2469 struct device_node *np)
2470{
2471 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(dev);
2472 struct marvell_nand_chip *marvell_nand;
2473 struct mtd_info *mtd;
2474 struct nand_chip *chip;
2475 int nsels, ret, i;
2476 u32 cs, rb;
2477
2478 /*
2479 * The legacy "num-cs" property indicates the number of CS on the only
2480 * chip connected to the controller (legacy bindings does not support
f6997bec 2481 * more than one chip). The CS and RB pins are always the #0.
02f26ecf
MR
2482 *
2483 * When not using legacy bindings, a couple of "reg" and "nand-rb"
2484 * properties must be filled. For each chip, expressed as a subnode,
2485 * "reg" points to the CS lines and "nand-rb" to the RB line.
2486 */
f6997bec 2487 if (pdata || nfc->caps->legacy_of_bindings) {
02f26ecf 2488 nsels = 1;
f6997bec
MR
2489 } else {
2490 nsels = of_property_count_elems_of_size(np, "reg", sizeof(u32));
2491 if (nsels <= 0) {
2492 dev_err(dev, "missing/invalid reg property\n");
2493 return -EINVAL;
2494 }
02f26ecf
MR
2495 }
2496
2497 /* Alloc the nand chip structure */
2498 marvell_nand = devm_kzalloc(dev, sizeof(*marvell_nand) +
2499 (nsels *
2500 sizeof(struct marvell_nand_chip_sel)),
2501 GFP_KERNEL);
2502 if (!marvell_nand) {
2503 dev_err(dev, "could not allocate chip structure\n");
2504 return -ENOMEM;
2505 }
2506
2507 marvell_nand->nsels = nsels;
2508 marvell_nand->selected_die = -1;
2509
2510 for (i = 0; i < nsels; i++) {
2511 if (pdata || nfc->caps->legacy_of_bindings) {
2512 /*
2513 * Legacy bindings use the CS lines in natural
2514 * order (0, 1, ...)
2515 */
2516 cs = i;
2517 } else {
2518 /* Retrieve CS id */
2519 ret = of_property_read_u32_index(np, "reg", i, &cs);
2520 if (ret) {
2521 dev_err(dev, "could not retrieve reg property: %d\n",
2522 ret);
2523 return ret;
2524 }
2525 }
2526
2527 if (cs >= nfc->caps->max_cs_nb) {
2528 dev_err(dev, "invalid reg value: %u (max CS = %d)\n",
2529 cs, nfc->caps->max_cs_nb);
2530 return -EINVAL;
2531 }
2532
2533 if (test_and_set_bit(cs, &nfc->assigned_cs)) {
2534 dev_err(dev, "CS %d already assigned\n", cs);
2535 return -EINVAL;
2536 }
2537
2538 /*
2539 * The cs variable represents the chip select id, which must be
2540 * converted in bit fields for NDCB0 and NDCB2 to select the
2541 * right chip. Unfortunately, due to a lack of information on
2542 * the subject and incoherent documentation, the user should not
2543 * use CS1 and CS3 at all as asserting them is not supported in
2544 * a reliable way (due to multiplexing inside ADDR5 field).
2545 */
2546 marvell_nand->sels[i].cs = cs;
2547 switch (cs) {
2548 case 0:
2549 case 2:
2550 marvell_nand->sels[i].ndcb0_csel = 0;
2551 break;
2552 case 1:
2553 case 3:
2554 marvell_nand->sels[i].ndcb0_csel = NDCB0_CSEL;
2555 break;
2556 default:
2557 return -EINVAL;
2558 }
2559
2560 /* Retrieve RB id */
2561 if (pdata || nfc->caps->legacy_of_bindings) {
2562 /* Legacy bindings always use RB #0 */
2563 rb = 0;
2564 } else {
2565 ret = of_property_read_u32_index(np, "nand-rb", i,
2566 &rb);
2567 if (ret) {
2568 dev_err(dev,
2569 "could not retrieve RB property: %d\n",
2570 ret);
2571 return ret;
2572 }
2573 }
2574
2575 if (rb >= nfc->caps->max_rb_nb) {
2576 dev_err(dev, "invalid reg value: %u (max RB = %d)\n",
2577 rb, nfc->caps->max_rb_nb);
2578 return -EINVAL;
2579 }
2580
2581 marvell_nand->sels[i].rb = rb;
2582 }
2583
2584 chip = &marvell_nand->chip;
2585 chip->controller = &nfc->controller;
2586 nand_set_flash_node(chip, np);
2587
2588 chip->exec_op = marvell_nfc_exec_op;
2589 chip->select_chip = marvell_nfc_select_chip;
07ad5a72 2590 if (!of_property_read_bool(np, "marvell,nand-keep-config"))
02f26ecf
MR
2591 chip->setup_data_interface = marvell_nfc_setup_data_interface;
2592
2593 mtd = nand_to_mtd(chip);
2594 mtd->dev.parent = dev;
2595
2596 /*
2597 * Default to HW ECC engine mode. If the nand-ecc-mode property is given
2598 * in the DT node, this entry will be overwritten in nand_scan_ident().
2599 */
2600 chip->ecc.mode = NAND_ECC_HW;
2601
2602 /*
2603 * Save a reference value for timing registers before
2604 * ->setup_data_interface() is called.
2605 */
2606 marvell_nand->ndtr0 = readl_relaxed(nfc->regs + NDTR0);
2607 marvell_nand->ndtr1 = readl_relaxed(nfc->regs + NDTR1);
2608
2609 chip->options |= NAND_BUSWIDTH_AUTO;
02f26ecf 2610
00ad378f 2611 ret = nand_scan(chip, marvell_nand->nsels);
02f26ecf 2612 if (ret) {
8831e48b 2613 dev_err(dev, "could not scan the nand chip\n");
02f26ecf
MR
2614 return ret;
2615 }
2616
2617 if (pdata)
2618 /* Legacy bindings support only one chip */
7576594c 2619 ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
02f26ecf
MR
2620 else
2621 ret = mtd_device_register(mtd, NULL, 0);
2622 if (ret) {
2623 dev_err(dev, "failed to register mtd device: %d\n", ret);
59ac276f 2624 nand_release(chip);
02f26ecf
MR
2625 return ret;
2626 }
2627
2628 list_add_tail(&marvell_nand->node, &nfc->chips);
2629
2630 return 0;
2631}
2632
2633static int marvell_nand_chips_init(struct device *dev, struct marvell_nfc *nfc)
2634{
2635 struct device_node *np = dev->of_node;
2636 struct device_node *nand_np;
2637 int max_cs = nfc->caps->max_cs_nb;
2638 int nchips;
2639 int ret;
2640
2641 if (!np)
2642 nchips = 1;
2643 else
2644 nchips = of_get_child_count(np);
2645
2646 if (nchips > max_cs) {
2647 dev_err(dev, "too many NAND chips: %d (max = %d CS)\n", nchips,
2648 max_cs);
2649 return -EINVAL;
2650 }
2651
2652 /*
2653 * Legacy bindings do not use child nodes to exhibit NAND chip
2654 * properties and layout. Instead, NAND properties are mixed with the
2655 * controller ones, and partitions are defined as direct subnodes of the
2656 * NAND controller node.
2657 */
2658 if (nfc->caps->legacy_of_bindings) {
2659 ret = marvell_nand_chip_init(dev, nfc, np);
2660 return ret;
2661 }
2662
2663 for_each_child_of_node(np, nand_np) {
2664 ret = marvell_nand_chip_init(dev, nfc, nand_np);
2665 if (ret) {
2666 of_node_put(nand_np);
2667 return ret;
2668 }
2669 }
2670
2671 return 0;
2672}
2673
2674static void marvell_nand_chips_cleanup(struct marvell_nfc *nfc)
2675{
2676 struct marvell_nand_chip *entry, *temp;
2677
2678 list_for_each_entry_safe(entry, temp, &nfc->chips, node) {
59ac276f 2679 nand_release(&entry->chip);
02f26ecf
MR
2680 list_del(&entry->node);
2681 }
2682}
2683
2684static int marvell_nfc_init_dma(struct marvell_nfc *nfc)
2685{
2686 struct platform_device *pdev = container_of(nfc->dev,
2687 struct platform_device,
2688 dev);
2689 struct dma_slave_config config = {};
2690 struct resource *r;
02f26ecf
MR
2691 int ret;
2692
2693 if (!IS_ENABLED(CONFIG_PXA_DMA)) {
2694 dev_warn(nfc->dev,
2695 "DMA not enabled in configuration\n");
2696 return -ENOTSUPP;
2697 }
2698
2699 ret = dma_set_mask_and_coherent(nfc->dev, DMA_BIT_MASK(32));
2700 if (ret)
2701 return ret;
2702
ac75a50b 2703 nfc->dma_chan = dma_request_slave_channel(nfc->dev, "data");
02f26ecf
MR
2704 if (!nfc->dma_chan) {
2705 dev_err(nfc->dev,
2706 "Unable to request data DMA channel\n");
2707 return -ENODEV;
2708 }
2709
2710 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2711 if (!r)
2712 return -ENXIO;
2713
2714 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2715 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2716 config.src_addr = r->start + NDDB;
2717 config.dst_addr = r->start + NDDB;
2718 config.src_maxburst = 32;
2719 config.dst_maxburst = 32;
2720 ret = dmaengine_slave_config(nfc->dma_chan, &config);
2721 if (ret < 0) {
2722 dev_err(nfc->dev, "Failed to configure DMA channel\n");
2723 return ret;
2724 }
2725
2726 /*
2727 * DMA must act on length multiple of 32 and this length may be
2728 * bigger than the destination buffer. Use this buffer instead
2729 * for DMA transfers and then copy the desired amount of data to
2730 * the provided buffer.
2731 */
c495a927 2732 nfc->dma_buf = kmalloc(MAX_CHUNK_SIZE, GFP_KERNEL | GFP_DMA);
02f26ecf
MR
2733 if (!nfc->dma_buf)
2734 return -ENOMEM;
2735
2736 nfc->use_dma = true;
2737
2738 return 0;
2739}
2740
bd9c3f9b
DM
2741static void marvell_nfc_reset(struct marvell_nfc *nfc)
2742{
2743 /*
2744 * ECC operations and interruptions are only enabled when specifically
2745 * needed. ECC shall not be activated in the early stages (fails probe).
2746 * Arbiter flag, even if marked as "reserved", must be set (empirical).
2747 * SPARE_EN bit must always be set or ECC bytes will not be at the same
2748 * offset in the read page and this will fail the protection.
2749 */
2750 writel_relaxed(NDCR_ALL_INT | NDCR_ND_ARB_EN | NDCR_SPARE_EN |
2751 NDCR_RD_ID_CNT(NFCV1_READID_LEN), nfc->regs + NDCR);
2752 writel_relaxed(0xFFFFFFFF, nfc->regs + NDSR);
2753 writel_relaxed(0, nfc->regs + NDECCCTRL);
2754}
2755
02f26ecf
MR
2756static int marvell_nfc_init(struct marvell_nfc *nfc)
2757{
2758 struct device_node *np = nfc->dev->of_node;
2759
2760 /*
2761 * Some SoCs like A7k/A8k need to enable manually the NAND
2762 * controller, gated clocks and reset bits to avoid being bootloader
2763 * dependent. This is done through the use of the System Functions
2764 * registers.
2765 */
2766 if (nfc->caps->need_system_controller) {
2767 struct regmap *sysctrl_base =
2768 syscon_regmap_lookup_by_phandle(np,
2769 "marvell,system-controller");
02f26ecf
MR
2770
2771 if (IS_ERR(sysctrl_base))
2772 return PTR_ERR(sysctrl_base);
2773
88aa3bbf
TP
2774 regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX,
2775 GENCONF_SOC_DEVICE_MUX_NFC_EN |
2776 GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST |
2777 GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST |
2778 GENCONF_SOC_DEVICE_MUX_NFC_INT_EN);
02f26ecf 2779
88aa3bbf
TP
2780 regmap_update_bits(sysctrl_base, GENCONF_CLK_GATING_CTRL,
2781 GENCONF_CLK_GATING_CTRL_ND_GATE,
2782 GENCONF_CLK_GATING_CTRL_ND_GATE);
02f26ecf 2783
88aa3bbf
TP
2784 regmap_update_bits(sysctrl_base, GENCONF_ND_CLK_CTRL,
2785 GENCONF_ND_CLK_CTRL_EN,
2786 GENCONF_ND_CLK_CTRL_EN);
02f26ecf
MR
2787 }
2788
2789 /* Configure the DMA if appropriate */
2790 if (!nfc->caps->is_nfcv2)
2791 marvell_nfc_init_dma(nfc);
2792
bd9c3f9b 2793 marvell_nfc_reset(nfc);
02f26ecf
MR
2794
2795 return 0;
2796}
2797
2798static int marvell_nfc_probe(struct platform_device *pdev)
2799{
2800 struct device *dev = &pdev->dev;
2801 struct resource *r;
2802 struct marvell_nfc *nfc;
2803 int ret;
2804 int irq;
2805
2806 nfc = devm_kzalloc(&pdev->dev, sizeof(struct marvell_nfc),
2807 GFP_KERNEL);
2808 if (!nfc)
2809 return -ENOMEM;
2810
2811 nfc->dev = dev;
7da45139 2812 nand_controller_init(&nfc->controller);
8831e48b 2813 nfc->controller.ops = &marvell_nand_controller_ops;
02f26ecf
MR
2814 INIT_LIST_HEAD(&nfc->chips);
2815
2816 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2817 nfc->regs = devm_ioremap_resource(dev, r);
2818 if (IS_ERR(nfc->regs))
2819 return PTR_ERR(nfc->regs);
2820
2821 irq = platform_get_irq(pdev, 0);
2822 if (irq < 0) {
2823 dev_err(dev, "failed to retrieve irq\n");
2824 return irq;
2825 }
2826
6b6de654 2827 nfc->core_clk = devm_clk_get(&pdev->dev, "core");
961ba15c
GC
2828
2829 /* Managed the legacy case (when the first clock was not named) */
6b6de654
BB
2830 if (nfc->core_clk == ERR_PTR(-ENOENT))
2831 nfc->core_clk = devm_clk_get(&pdev->dev, NULL);
961ba15c 2832
6b6de654
BB
2833 if (IS_ERR(nfc->core_clk))
2834 return PTR_ERR(nfc->core_clk);
02f26ecf 2835
6b6de654 2836 ret = clk_prepare_enable(nfc->core_clk);
02f26ecf
MR
2837 if (ret)
2838 return ret;
2839
961ba15c 2840 nfc->reg_clk = devm_clk_get(&pdev->dev, "reg");
f9e64d61
DM
2841 if (IS_ERR(nfc->reg_clk)) {
2842 if (PTR_ERR(nfc->reg_clk) != -ENOENT) {
961ba15c 2843 ret = PTR_ERR(nfc->reg_clk);
6b6de654 2844 goto unprepare_core_clk;
961ba15c 2845 }
f9e64d61
DM
2846
2847 nfc->reg_clk = NULL;
961ba15c
GC
2848 }
2849
f9e64d61
DM
2850 ret = clk_prepare_enable(nfc->reg_clk);
2851 if (ret)
2852 goto unprepare_core_clk;
2853
02f26ecf
MR
2854 marvell_nfc_disable_int(nfc, NDCR_ALL_INT);
2855 marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
2856 ret = devm_request_irq(dev, irq, marvell_nfc_isr,
2857 0, "marvell-nfc", nfc);
2858 if (ret)
961ba15c 2859 goto unprepare_reg_clk;
02f26ecf
MR
2860
2861 /* Get NAND controller capabilities */
2862 if (pdev->id_entry)
2863 nfc->caps = (void *)pdev->id_entry->driver_data;
2864 else
2865 nfc->caps = of_device_get_match_data(&pdev->dev);
2866
2867 if (!nfc->caps) {
2868 dev_err(dev, "Could not retrieve NFC caps\n");
2869 ret = -EINVAL;
961ba15c 2870 goto unprepare_reg_clk;
02f26ecf
MR
2871 }
2872
2873 /* Init the controller and then probe the chips */
2874 ret = marvell_nfc_init(nfc);
2875 if (ret)
961ba15c 2876 goto unprepare_reg_clk;
02f26ecf
MR
2877
2878 platform_set_drvdata(pdev, nfc);
2879
2880 ret = marvell_nand_chips_init(dev, nfc);
2881 if (ret)
961ba15c 2882 goto unprepare_reg_clk;
02f26ecf
MR
2883
2884 return 0;
2885
961ba15c
GC
2886unprepare_reg_clk:
2887 clk_disable_unprepare(nfc->reg_clk);
6b6de654
BB
2888unprepare_core_clk:
2889 clk_disable_unprepare(nfc->core_clk);
02f26ecf
MR
2890
2891 return ret;
2892}
2893
2894static int marvell_nfc_remove(struct platform_device *pdev)
2895{
2896 struct marvell_nfc *nfc = platform_get_drvdata(pdev);
2897
2898 marvell_nand_chips_cleanup(nfc);
2899
2900 if (nfc->use_dma) {
2901 dmaengine_terminate_all(nfc->dma_chan);
2902 dma_release_channel(nfc->dma_chan);
2903 }
2904
961ba15c 2905 clk_disable_unprepare(nfc->reg_clk);
6b6de654 2906 clk_disable_unprepare(nfc->core_clk);
02f26ecf
MR
2907
2908 return 0;
2909}
2910
bd9c3f9b
DM
2911static int __maybe_unused marvell_nfc_suspend(struct device *dev)
2912{
2913 struct marvell_nfc *nfc = dev_get_drvdata(dev);
2914 struct marvell_nand_chip *chip;
2915
2916 list_for_each_entry(chip, &nfc->chips, node)
2917 marvell_nfc_wait_ndrun(&chip->chip);
2918
2919 clk_disable_unprepare(nfc->reg_clk);
2920 clk_disable_unprepare(nfc->core_clk);
2921
2922 return 0;
2923}
2924
2925static int __maybe_unused marvell_nfc_resume(struct device *dev)
2926{
2927 struct marvell_nfc *nfc = dev_get_drvdata(dev);
2928 int ret;
2929
2930 ret = clk_prepare_enable(nfc->core_clk);
2931 if (ret < 0)
2932 return ret;
2933
f9e64d61
DM
2934 ret = clk_prepare_enable(nfc->reg_clk);
2935 if (ret < 0)
2936 return ret;
bd9c3f9b
DM
2937
2938 /*
2939 * Reset nfc->selected_chip so the next command will cause the timing
2940 * registers to be restored in marvell_nfc_select_chip().
2941 */
2942 nfc->selected_chip = NULL;
2943
2944 /* Reset registers that have lost their contents */
2945 marvell_nfc_reset(nfc);
2946
2947 return 0;
2948}
2949
2950static const struct dev_pm_ops marvell_nfc_pm_ops = {
2951 SET_SYSTEM_SLEEP_PM_OPS(marvell_nfc_suspend, marvell_nfc_resume)
2952};
2953
02f26ecf
MR
2954static const struct marvell_nfc_caps marvell_armada_8k_nfc_caps = {
2955 .max_cs_nb = 4,
2956 .max_rb_nb = 2,
2957 .need_system_controller = true,
2958 .is_nfcv2 = true,
2959};
2960
2961static const struct marvell_nfc_caps marvell_armada370_nfc_caps = {
2962 .max_cs_nb = 4,
2963 .max_rb_nb = 2,
2964 .is_nfcv2 = true,
2965};
2966
2967static const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps = {
2968 .max_cs_nb = 2,
2969 .max_rb_nb = 1,
2970 .use_dma = true,
2971};
2972
2973static const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps = {
2974 .max_cs_nb = 4,
2975 .max_rb_nb = 2,
2976 .need_system_controller = true,
2977 .legacy_of_bindings = true,
2978 .is_nfcv2 = true,
2979};
2980
2981static const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps = {
2982 .max_cs_nb = 4,
2983 .max_rb_nb = 2,
2984 .legacy_of_bindings = true,
2985 .is_nfcv2 = true,
2986};
2987
2988static const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps = {
2989 .max_cs_nb = 2,
2990 .max_rb_nb = 1,
2991 .legacy_of_bindings = true,
2992 .use_dma = true,
2993};
2994
2995static const struct platform_device_id marvell_nfc_platform_ids[] = {
2996 {
2997 .name = "pxa3xx-nand",
2998 .driver_data = (kernel_ulong_t)&marvell_pxa3xx_nfc_legacy_caps,
2999 },
3000 { /* sentinel */ },
3001};
3002MODULE_DEVICE_TABLE(platform, marvell_nfc_platform_ids);
3003
3004static const struct of_device_id marvell_nfc_of_ids[] = {
3005 {
3006 .compatible = "marvell,armada-8k-nand-controller",
3007 .data = &marvell_armada_8k_nfc_caps,
3008 },
3009 {
3010 .compatible = "marvell,armada370-nand-controller",
3011 .data = &marvell_armada370_nfc_caps,
3012 },
3013 {
3014 .compatible = "marvell,pxa3xx-nand-controller",
3015 .data = &marvell_pxa3xx_nfc_caps,
3016 },
3017 /* Support for old/deprecated bindings: */
3018 {
3019 .compatible = "marvell,armada-8k-nand",
3020 .data = &marvell_armada_8k_nfc_legacy_caps,
3021 },
3022 {
3023 .compatible = "marvell,armada370-nand",
3024 .data = &marvell_armada370_nfc_legacy_caps,
3025 },
3026 {
3027 .compatible = "marvell,pxa3xx-nand",
3028 .data = &marvell_pxa3xx_nfc_legacy_caps,
3029 },
3030 { /* sentinel */ },
3031};
3032MODULE_DEVICE_TABLE(of, marvell_nfc_of_ids);
3033
3034static struct platform_driver marvell_nfc_driver = {
3035 .driver = {
3036 .name = "marvell-nfc",
3037 .of_match_table = marvell_nfc_of_ids,
bd9c3f9b 3038 .pm = &marvell_nfc_pm_ops,
02f26ecf
MR
3039 },
3040 .id_table = marvell_nfc_platform_ids,
3041 .probe = marvell_nfc_probe,
3042 .remove = marvell_nfc_remove,
3043};
3044module_platform_driver(marvell_nfc_driver);
3045
3046MODULE_LICENSE("GPL");
3047MODULE_DESCRIPTION("Marvell NAND controller driver");