mtd: rawnand: Pass a nand_chip object to ecc->correct()
[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
704static void marvell_nfc_select_chip(struct mtd_info *mtd, int die_nr)
705{
706 struct nand_chip *chip = mtd_to_nand(mtd);
707 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
708 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
709 u32 ndcr_generic;
710
711 if (chip == nfc->selected_chip && die_nr == marvell_nand->selected_die)
712 return;
713
714 if (die_nr < 0 || die_nr >= marvell_nand->nsels) {
715 nfc->selected_chip = NULL;
716 marvell_nand->selected_die = -1;
717 return;
718 }
719
02f26ecf
MR
720 writel_relaxed(marvell_nand->ndtr0, nfc->regs + NDTR0);
721 writel_relaxed(marvell_nand->ndtr1, nfc->regs + NDTR1);
722
723 /*
724 * Reset the NDCR register to a clean state for this particular chip,
725 * also clear ND_RUN bit.
726 */
727 ndcr_generic = readl_relaxed(nfc->regs + NDCR) &
728 NDCR_GENERIC_FIELDS_MASK & ~NDCR_ND_RUN;
729 writel_relaxed(ndcr_generic | marvell_nand->ndcr, nfc->regs + NDCR);
730
731 /* Also reset the interrupt status register */
732 marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
733
734 nfc->selected_chip = chip;
735 marvell_nand->selected_die = die_nr;
736}
737
738static irqreturn_t marvell_nfc_isr(int irq, void *dev_id)
739{
740 struct marvell_nfc *nfc = dev_id;
741 u32 st = readl_relaxed(nfc->regs + NDSR);
742 u32 ien = (~readl_relaxed(nfc->regs + NDCR)) & NDCR_ALL_INT;
743
744 /*
745 * RDY interrupt mask is one bit in NDCR while there are two status
746 * bit in NDSR (RDY[cs0/cs2] and RDY[cs1/cs3]).
747 */
748 if (st & NDSR_RDY(1))
749 st |= NDSR_RDY(0);
750
751 if (!(st & ien))
752 return IRQ_NONE;
753
754 marvell_nfc_disable_int(nfc, st & NDCR_ALL_INT);
755
756 if (!(st & (NDSR_RDDREQ | NDSR_WRDREQ | NDSR_WRCMDREQ)))
757 complete(&nfc->complete);
758
759 return IRQ_HANDLED;
760}
761
762/* HW ECC related functions */
763static void marvell_nfc_enable_hw_ecc(struct nand_chip *chip)
764{
765 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
766 u32 ndcr = readl_relaxed(nfc->regs + NDCR);
767
768 if (!(ndcr & NDCR_ECC_EN)) {
769 writel_relaxed(ndcr | NDCR_ECC_EN, nfc->regs + NDCR);
770
771 /*
772 * When enabling BCH, set threshold to 0 to always know the
773 * number of corrected bitflips.
774 */
775 if (chip->ecc.algo == NAND_ECC_BCH)
776 writel_relaxed(NDECCCTRL_BCH_EN, nfc->regs + NDECCCTRL);
777 }
778}
779
780static void marvell_nfc_disable_hw_ecc(struct nand_chip *chip)
781{
782 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
783 u32 ndcr = readl_relaxed(nfc->regs + NDCR);
784
785 if (ndcr & NDCR_ECC_EN) {
786 writel_relaxed(ndcr & ~NDCR_ECC_EN, nfc->regs + NDCR);
787 if (chip->ecc.algo == NAND_ECC_BCH)
788 writel_relaxed(0, nfc->regs + NDECCCTRL);
789 }
790}
791
792/* DMA related helpers */
793static void marvell_nfc_enable_dma(struct marvell_nfc *nfc)
794{
795 u32 reg;
796
797 reg = readl_relaxed(nfc->regs + NDCR);
798 writel_relaxed(reg | NDCR_DMA_EN, nfc->regs + NDCR);
799}
800
801static void marvell_nfc_disable_dma(struct marvell_nfc *nfc)
802{
803 u32 reg;
804
805 reg = readl_relaxed(nfc->regs + NDCR);
806 writel_relaxed(reg & ~NDCR_DMA_EN, nfc->regs + NDCR);
807}
808
809/* Read/write PIO/DMA accessors */
810static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,
811 enum dma_data_direction direction,
812 unsigned int len)
813{
814 unsigned int dma_len = min_t(int, ALIGN(len, 32), MAX_CHUNK_SIZE);
815 struct dma_async_tx_descriptor *tx;
816 struct scatterlist sg;
817 dma_cookie_t cookie;
818 int ret;
819
820 marvell_nfc_enable_dma(nfc);
821 /* Prepare the DMA transfer */
822 sg_init_one(&sg, nfc->dma_buf, dma_len);
823 dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
824 tx = dmaengine_prep_slave_sg(nfc->dma_chan, &sg, 1,
825 direction == DMA_FROM_DEVICE ?
826 DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
827 DMA_PREP_INTERRUPT);
828 if (!tx) {
829 dev_err(nfc->dev, "Could not prepare DMA S/G list\n");
830 return -ENXIO;
831 }
832
833 /* Do the task and wait for it to finish */
834 cookie = dmaengine_submit(tx);
835 ret = dma_submit_error(cookie);
836 if (ret)
837 return -EIO;
838
839 dma_async_issue_pending(nfc->dma_chan);
840 ret = marvell_nfc_wait_cmdd(nfc->selected_chip);
841 dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
842 marvell_nfc_disable_dma(nfc);
843 if (ret) {
844 dev_err(nfc->dev, "Timeout waiting for DMA (status: %d)\n",
845 dmaengine_tx_status(nfc->dma_chan, cookie, NULL));
846 dmaengine_terminate_all(nfc->dma_chan);
847 return -ETIMEDOUT;
848 }
849
850 return 0;
851}
852
853static int marvell_nfc_xfer_data_in_pio(struct marvell_nfc *nfc, u8 *in,
854 unsigned int len)
855{
856 unsigned int last_len = len % FIFO_DEPTH;
857 unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
858 int i;
859
860 for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
861 ioread32_rep(nfc->regs + NDDB, in + i, FIFO_REP(FIFO_DEPTH));
862
863 if (last_len) {
864 u8 tmp_buf[FIFO_DEPTH];
865
866 ioread32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
867 memcpy(in + last_full_offset, tmp_buf, last_len);
868 }
869
870 return 0;
871}
872
873static int marvell_nfc_xfer_data_out_pio(struct marvell_nfc *nfc, const u8 *out,
874 unsigned int len)
875{
876 unsigned int last_len = len % FIFO_DEPTH;
877 unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
878 int i;
879
880 for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
881 iowrite32_rep(nfc->regs + NDDB, out + i, FIFO_REP(FIFO_DEPTH));
882
883 if (last_len) {
884 u8 tmp_buf[FIFO_DEPTH];
885
886 memcpy(tmp_buf, out + last_full_offset, last_len);
887 iowrite32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
888 }
889
890 return 0;
891}
892
893static void marvell_nfc_check_empty_chunk(struct nand_chip *chip,
894 u8 *data, int data_len,
895 u8 *spare, int spare_len,
896 u8 *ecc, int ecc_len,
897 unsigned int *max_bitflips)
898{
899 struct mtd_info *mtd = nand_to_mtd(chip);
900 int bf;
901
902 /*
903 * Blank pages (all 0xFF) that have not been written may be recognized
904 * as bad if bitflips occur, so whenever an uncorrectable error occurs,
905 * check if the entire page (with ECC bytes) is actually blank or not.
906 */
907 if (!data)
908 data_len = 0;
909 if (!spare)
910 spare_len = 0;
911 if (!ecc)
912 ecc_len = 0;
913
914 bf = nand_check_erased_ecc_chunk(data, data_len, ecc, ecc_len,
915 spare, spare_len, chip->ecc.strength);
916 if (bf < 0) {
917 mtd->ecc_stats.failed++;
918 return;
919 }
920
921 /* Update the stats and max_bitflips */
922 mtd->ecc_stats.corrected += bf;
923 *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
924}
925
926/*
927 * Check a chunk is correct or not according to hardware ECC engine.
928 * mtd->ecc_stats.corrected is updated, as well as max_bitflips, however
929 * mtd->ecc_stats.failure is not, the function will instead return a non-zero
930 * value indicating that a check on the emptyness of the subpage must be
931 * performed before declaring the subpage corrupted.
932 */
933static int marvell_nfc_hw_ecc_correct(struct nand_chip *chip,
934 unsigned int *max_bitflips)
935{
936 struct mtd_info *mtd = nand_to_mtd(chip);
937 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
938 int bf = 0;
939 u32 ndsr;
940
941 ndsr = readl_relaxed(nfc->regs + NDSR);
942
943 /* Check uncorrectable error flag */
944 if (ndsr & NDSR_UNCERR) {
945 writel_relaxed(ndsr, nfc->regs + NDSR);
946
947 /*
948 * Do not increment ->ecc_stats.failed now, instead, return a
949 * non-zero value to indicate that this chunk was apparently
950 * bad, and it should be check to see if it empty or not. If
951 * the chunk (with ECC bytes) is not declared empty, the calling
952 * function must increment the failure count.
953 */
954 return -EBADMSG;
955 }
956
957 /* Check correctable error flag */
958 if (ndsr & NDSR_CORERR) {
959 writel_relaxed(ndsr, nfc->regs + NDSR);
960
961 if (chip->ecc.algo == NAND_ECC_BCH)
962 bf = NDSR_ERRCNT(ndsr);
963 else
964 bf = 1;
965 }
966
967 /* Update the stats and max_bitflips */
968 mtd->ecc_stats.corrected += bf;
969 *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
970
971 return 0;
972}
973
974/* Hamming read helpers */
975static int marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip *chip,
976 u8 *data_buf, u8 *oob_buf,
977 bool raw, int page)
978{
979 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
980 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
981 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
982 struct marvell_nfc_op nfc_op = {
983 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
984 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
985 NDCB0_DBC |
986 NDCB0_CMD1(NAND_CMD_READ0) |
987 NDCB0_CMD2(NAND_CMD_READSTART),
988 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
989 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
990 };
991 unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
992 int ret;
993
994 /* NFCv2 needs more information about the operation being executed */
995 if (nfc->caps->is_nfcv2)
996 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
997
998 ret = marvell_nfc_prepare_cmd(chip);
999 if (ret)
1000 return ret;
1001
1002 marvell_nfc_send_cmd(chip, &nfc_op);
1003 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1004 "RDDREQ while draining FIFO (data/oob)");
1005 if (ret)
1006 return ret;
1007
1008 /*
1009 * Read the page then the OOB area. Unlike what is shown in current
1010 * documentation, spare bytes are protected by the ECC engine, and must
1011 * be at the beginning of the OOB area or running this driver on legacy
1012 * systems will prevent the discovery of the BBM/BBT.
1013 */
1014 if (nfc->use_dma) {
1015 marvell_nfc_xfer_data_dma(nfc, DMA_FROM_DEVICE,
1016 lt->data_bytes + oob_bytes);
1017 memcpy(data_buf, nfc->dma_buf, lt->data_bytes);
1018 memcpy(oob_buf, nfc->dma_buf + lt->data_bytes, oob_bytes);
1019 } else {
1020 marvell_nfc_xfer_data_in_pio(nfc, data_buf, lt->data_bytes);
1021 marvell_nfc_xfer_data_in_pio(nfc, oob_buf, oob_bytes);
1022 }
1023
1024 ret = marvell_nfc_wait_cmdd(chip);
1025
1026 return ret;
1027}
1028
1029static int marvell_nfc_hw_ecc_hmg_read_page_raw(struct mtd_info *mtd,
1030 struct nand_chip *chip, u8 *buf,
1031 int oob_required, int page)
1032{
1033 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
1034 true, page);
1035}
1036
1037static int marvell_nfc_hw_ecc_hmg_read_page(struct mtd_info *mtd,
1038 struct nand_chip *chip,
1039 u8 *buf, int oob_required,
1040 int page)
1041{
1042 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1043 unsigned int full_sz = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1044 int max_bitflips = 0, ret;
1045 u8 *raw_buf;
1046
1047 marvell_nfc_enable_hw_ecc(chip);
1048 marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, false,
1049 page);
1050 ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
1051 marvell_nfc_disable_hw_ecc(chip);
1052
1053 if (!ret)
1054 return max_bitflips;
1055
1056 /*
1057 * When ECC failures are detected, check if the full page has been
1058 * written or not. Ignore the failure if it is actually empty.
1059 */
1060 raw_buf = kmalloc(full_sz, GFP_KERNEL);
1061 if (!raw_buf)
1062 return -ENOMEM;
1063
1064 marvell_nfc_hw_ecc_hmg_do_read_page(chip, raw_buf, raw_buf +
1065 lt->data_bytes, true, page);
1066 marvell_nfc_check_empty_chunk(chip, raw_buf, full_sz, NULL, 0, NULL, 0,
1067 &max_bitflips);
1068 kfree(raw_buf);
1069
1070 return max_bitflips;
1071}
1072
1073/*
1074 * Spare area in Hamming layouts is not protected by the ECC engine (even if
1075 * it appears before the ECC bytes when reading), the ->read_oob_raw() function
1076 * also stands for ->read_oob().
1077 */
1078static int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct mtd_info *mtd,
1079 struct nand_chip *chip, int page)
1080{
1081 /* Invalidate page cache */
1082 chip->pagebuf = -1;
1083
1084 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, chip->data_buf,
1085 chip->oob_poi, true, page);
1086}
1087
1088/* Hamming write helpers */
1089static int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip *chip,
1090 const u8 *data_buf,
1091 const u8 *oob_buf, bool raw,
1092 int page)
1093{
1094 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1095 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1096 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1097 struct marvell_nfc_op nfc_op = {
1098 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) |
1099 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1100 NDCB0_CMD1(NAND_CMD_SEQIN) |
1101 NDCB0_CMD2(NAND_CMD_PAGEPROG) |
1102 NDCB0_DBC,
1103 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
1104 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
1105 };
1106 unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
1107 int ret;
1108
1109 /* NFCv2 needs more information about the operation being executed */
1110 if (nfc->caps->is_nfcv2)
1111 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
1112
1113 ret = marvell_nfc_prepare_cmd(chip);
1114 if (ret)
1115 return ret;
1116
1117 marvell_nfc_send_cmd(chip, &nfc_op);
1118 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1119 "WRDREQ while loading FIFO (data)");
1120 if (ret)
1121 return ret;
1122
1123 /* Write the page then the OOB area */
1124 if (nfc->use_dma) {
1125 memcpy(nfc->dma_buf, data_buf, lt->data_bytes);
1126 memcpy(nfc->dma_buf + lt->data_bytes, oob_buf, oob_bytes);
1127 marvell_nfc_xfer_data_dma(nfc, DMA_TO_DEVICE, lt->data_bytes +
1128 lt->ecc_bytes + lt->spare_bytes);
1129 } else {
1130 marvell_nfc_xfer_data_out_pio(nfc, data_buf, lt->data_bytes);
1131 marvell_nfc_xfer_data_out_pio(nfc, oob_buf, oob_bytes);
1132 }
1133
1134 ret = marvell_nfc_wait_cmdd(chip);
1135 if (ret)
1136 return ret;
1137
1138 ret = marvell_nfc_wait_op(chip,
b76401fc 1139 PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max));
02f26ecf
MR
1140 return ret;
1141}
1142
1143static int marvell_nfc_hw_ecc_hmg_write_page_raw(struct mtd_info *mtd,
1144 struct nand_chip *chip,
1145 const u8 *buf,
1146 int oob_required, int page)
1147{
1148 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1149 true, page);
1150}
1151
1152static int marvell_nfc_hw_ecc_hmg_write_page(struct mtd_info *mtd,
1153 struct nand_chip *chip,
1154 const u8 *buf,
1155 int oob_required, int page)
1156{
1157 int ret;
1158
1159 marvell_nfc_enable_hw_ecc(chip);
1160 ret = marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1161 false, page);
1162 marvell_nfc_disable_hw_ecc(chip);
1163
1164 return ret;
1165}
1166
1167/*
1168 * Spare area in Hamming layouts is not protected by the ECC engine (even if
1169 * it appears before the ECC bytes when reading), the ->write_oob_raw() function
1170 * also stands for ->write_oob().
1171 */
1172static int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct mtd_info *mtd,
1173 struct nand_chip *chip,
1174 int page)
1175{
1176 /* Invalidate page cache */
1177 chip->pagebuf = -1;
1178
1179 memset(chip->data_buf, 0xFF, mtd->writesize);
1180
1181 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, chip->data_buf,
1182 chip->oob_poi, true, page);
1183}
1184
1185/* BCH read helpers */
1186static int marvell_nfc_hw_ecc_bch_read_page_raw(struct mtd_info *mtd,
1187 struct nand_chip *chip, u8 *buf,
1188 int oob_required, int page)
1189{
1190 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1191 u8 *oob = chip->oob_poi;
1192 int chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1193 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1194 lt->last_spare_bytes;
1195 int data_len = lt->data_bytes;
1196 int spare_len = lt->spare_bytes;
1197 int ecc_len = lt->ecc_bytes;
1198 int chunk;
1199
1200 if (oob_required)
1201 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1202
1203 nand_read_page_op(chip, page, 0, NULL, 0);
1204
1205 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1206 /* Update last chunk length */
1207 if (chunk >= lt->full_chunk_cnt) {
1208 data_len = lt->last_data_bytes;
1209 spare_len = lt->last_spare_bytes;
1210 ecc_len = lt->last_ecc_bytes;
1211 }
1212
1213 /* Read data bytes*/
1214 nand_change_read_column_op(chip, chunk * chunk_size,
1215 buf + (lt->data_bytes * chunk),
1216 data_len, false);
1217
1218 /* Read spare bytes */
1219 nand_read_data_op(chip, oob + (lt->spare_bytes * chunk),
1220 spare_len, false);
1221
1222 /* Read ECC bytes */
1223 nand_read_data_op(chip, oob + ecc_offset +
1224 (ALIGN(lt->ecc_bytes, 32) * chunk),
1225 ecc_len, false);
1226 }
1227
1228 return 0;
1229}
1230
1231static void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip *chip, int chunk,
1232 u8 *data, unsigned int data_len,
1233 u8 *spare, unsigned int spare_len,
1234 int page)
1235{
1236 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1237 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1238 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1239 int i, ret;
1240 struct marvell_nfc_op nfc_op = {
1241 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
1242 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1243 NDCB0_LEN_OVRD,
1244 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
1245 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
1246 .ndcb[3] = data_len + spare_len,
1247 };
1248
1249 ret = marvell_nfc_prepare_cmd(chip);
1250 if (ret)
1251 return;
1252
1253 if (chunk == 0)
1254 nfc_op.ndcb[0] |= NDCB0_DBC |
1255 NDCB0_CMD1(NAND_CMD_READ0) |
1256 NDCB0_CMD2(NAND_CMD_READSTART);
1257
1258 /*
90d61763
BB
1259 * Trigger the monolithic read on the first chunk, then naked read on
1260 * intermediate chunks and finally a last naked read on the last chunk.
02f26ecf 1261 */
90d61763 1262 if (chunk == 0)
02f26ecf 1263 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
90d61763
BB
1264 else if (chunk < lt->nchunks - 1)
1265 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
02f26ecf
MR
1266 else
1267 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1268
1269 marvell_nfc_send_cmd(chip, &nfc_op);
1270
1271 /*
1272 * According to the datasheet, when reading from NDDB
1273 * with BCH enabled, after each 32 bytes reads, we
1274 * have to make sure that the NDSR.RDDREQ bit is set.
1275 *
1276 * Drain the FIFO, 8 32-bit reads at a time, and skip
1277 * the polling on the last read.
1278 *
1279 * Length is a multiple of 32 bytes, hence it is a multiple of 8 too.
1280 */
1281 for (i = 0; i < data_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1282 marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1283 "RDDREQ while draining FIFO (data)");
1284 marvell_nfc_xfer_data_in_pio(nfc, data,
1285 FIFO_DEPTH * BCH_SEQ_READS);
1286 data += FIFO_DEPTH * BCH_SEQ_READS;
1287 }
1288
1289 for (i = 0; i < spare_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1290 marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1291 "RDDREQ while draining FIFO (OOB)");
1292 marvell_nfc_xfer_data_in_pio(nfc, spare,
1293 FIFO_DEPTH * BCH_SEQ_READS);
1294 spare += FIFO_DEPTH * BCH_SEQ_READS;
1295 }
1296}
1297
1298static int marvell_nfc_hw_ecc_bch_read_page(struct mtd_info *mtd,
1299 struct nand_chip *chip,
1300 u8 *buf, int oob_required,
1301 int page)
1302{
1303 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1304 int data_len = lt->data_bytes, spare_len = lt->spare_bytes, ecc_len;
1305 u8 *data = buf, *spare = chip->oob_poi, *ecc;
1306 int max_bitflips = 0;
1307 u32 failure_mask = 0;
1308 int chunk, ecc_offset_in_page, ret;
1309
1310 /*
1311 * With BCH, OOB is not fully used (and thus not read entirely), not
1312 * expected bytes could show up at the end of the OOB buffer if not
1313 * explicitly erased.
1314 */
1315 if (oob_required)
1316 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1317
1318 marvell_nfc_enable_hw_ecc(chip);
1319
1320 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1321 /* Update length for the last chunk */
1322 if (chunk >= lt->full_chunk_cnt) {
1323 data_len = lt->last_data_bytes;
1324 spare_len = lt->last_spare_bytes;
1325 }
1326
1327 /* Read the chunk and detect number of bitflips */
1328 marvell_nfc_hw_ecc_bch_read_chunk(chip, chunk, data, data_len,
1329 spare, spare_len, page);
1330 ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
1331 if (ret)
1332 failure_mask |= BIT(chunk);
1333
1334 data += data_len;
1335 spare += spare_len;
1336 }
1337
1338 marvell_nfc_disable_hw_ecc(chip);
1339
1340 if (!failure_mask)
1341 return max_bitflips;
1342
1343 /*
1344 * Please note that dumping the ECC bytes during a normal read with OOB
1345 * area would add a significant overhead as ECC bytes are "consumed" by
1346 * the controller in normal mode and must be re-read in raw mode. To
1347 * avoid dropping the performances, we prefer not to include them. The
1348 * user should re-read the page in raw mode if ECC bytes are required.
1349 *
1350 * However, for any subpage read error reported by ->correct(), the ECC
1351 * bytes must be read in raw mode and the full subpage must be checked
1352 * to see if it is entirely empty of if there was an actual error.
1353 */
1354 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1355 /* No failure reported for this chunk, move to the next one */
1356 if (!(failure_mask & BIT(chunk)))
1357 continue;
1358
1359 /* Derive ECC bytes positions (in page/buffer) and length */
1360 ecc = chip->oob_poi +
1361 (lt->full_chunk_cnt * lt->spare_bytes) +
1362 lt->last_spare_bytes +
1363 (chunk * ALIGN(lt->ecc_bytes, 32));
1364 ecc_offset_in_page =
1365 (chunk * (lt->data_bytes + lt->spare_bytes +
1366 lt->ecc_bytes)) +
1367 (chunk < lt->full_chunk_cnt ?
1368 lt->data_bytes + lt->spare_bytes :
1369 lt->last_data_bytes + lt->last_spare_bytes);
1370 ecc_len = chunk < lt->full_chunk_cnt ?
1371 lt->ecc_bytes : lt->last_ecc_bytes;
1372
1373 /* Do the actual raw read of the ECC bytes */
1374 nand_change_read_column_op(chip, ecc_offset_in_page,
1375 ecc, ecc_len, false);
1376
1377 /* Derive data/spare bytes positions (in buffer) and length */
1378 data = buf + (chunk * lt->data_bytes);
1379 data_len = chunk < lt->full_chunk_cnt ?
1380 lt->data_bytes : lt->last_data_bytes;
1381 spare = chip->oob_poi + (chunk * (lt->spare_bytes +
1382 lt->ecc_bytes));
1383 spare_len = chunk < lt->full_chunk_cnt ?
1384 lt->spare_bytes : lt->last_spare_bytes;
1385
1386 /* Check the entire chunk (data + spare + ecc) for emptyness */
1387 marvell_nfc_check_empty_chunk(chip, data, data_len, spare,
1388 spare_len, ecc, ecc_len,
1389 &max_bitflips);
1390 }
1391
1392 return max_bitflips;
1393}
1394
1395static int marvell_nfc_hw_ecc_bch_read_oob_raw(struct mtd_info *mtd,
1396 struct nand_chip *chip, int page)
1397{
1398 /* Invalidate page cache */
1399 chip->pagebuf = -1;
1400
1401 return chip->ecc.read_page_raw(mtd, chip, chip->data_buf, true, page);
1402}
1403
1404static int marvell_nfc_hw_ecc_bch_read_oob(struct mtd_info *mtd,
1405 struct nand_chip *chip, int page)
1406{
1407 /* Invalidate page cache */
1408 chip->pagebuf = -1;
1409
1410 return chip->ecc.read_page(mtd, chip, chip->data_buf, true, page);
1411}
1412
1413/* BCH write helpers */
1414static int marvell_nfc_hw_ecc_bch_write_page_raw(struct mtd_info *mtd,
1415 struct nand_chip *chip,
1416 const u8 *buf,
1417 int oob_required, int page)
1418{
1419 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1420 int full_chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1421 int data_len = lt->data_bytes;
1422 int spare_len = lt->spare_bytes;
1423 int ecc_len = lt->ecc_bytes;
02f26ecf
MR
1424 int spare_offset = 0;
1425 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1426 lt->last_spare_bytes;
1427 int chunk;
1428
1429 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1430
1431 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1432 if (chunk >= lt->full_chunk_cnt) {
1433 data_len = lt->last_data_bytes;
1434 spare_len = lt->last_spare_bytes;
1435 ecc_len = lt->last_ecc_bytes;
02f26ecf
MR
1436 }
1437
1438 /* Point to the column of the next chunk */
1439 nand_change_write_column_op(chip, chunk * full_chunk_size,
1440 NULL, 0, false);
1441
1442 /* Write the data */
1443 nand_write_data_op(chip, buf + (chunk * lt->data_bytes),
1444 data_len, false);
1445
1446 if (!oob_required)
1447 continue;
1448
1449 /* Write the spare bytes */
1450 if (spare_len)
1451 nand_write_data_op(chip, chip->oob_poi + spare_offset,
1452 spare_len, false);
1453
1454 /* Write the ECC bytes */
1455 if (ecc_len)
1456 nand_write_data_op(chip, chip->oob_poi + ecc_offset,
1457 ecc_len, false);
1458
1459 spare_offset += spare_len;
1460 ecc_offset += ALIGN(ecc_len, 32);
1461 }
1462
1463 return nand_prog_page_end_op(chip);
1464}
1465
1466static int
1467marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip *chip, int chunk,
1468 const u8 *data, unsigned int data_len,
1469 const u8 *spare, unsigned int spare_len,
1470 int page)
1471{
1472 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1473 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1474 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
a2ee41fd 1475 u32 xtype;
02f26ecf
MR
1476 int ret;
1477 struct marvell_nfc_op nfc_op = {
1478 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | NDCB0_LEN_OVRD,
1479 .ndcb[3] = data_len + spare_len,
1480 };
1481
1482 /*
1483 * First operation dispatches the CMD_SEQIN command, issue the address
1484 * cycles and asks for the first chunk of data.
1485 * All operations in the middle (if any) will issue a naked write and
1486 * also ask for data.
1487 * Last operation (if any) asks for the last chunk of data through a
1488 * last naked write.
1489 */
1490 if (chunk == 0) {
a2ee41fd
MR
1491 if (lt->nchunks == 1)
1492 xtype = XTYPE_MONOLITHIC_RW;
1493 else
1494 xtype = XTYPE_WRITE_DISPATCH;
1495
1496 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(xtype) |
02f26ecf
MR
1497 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1498 NDCB0_CMD1(NAND_CMD_SEQIN);
1499 nfc_op.ndcb[1] |= NDCB1_ADDRS_PAGE(page);
1500 nfc_op.ndcb[2] |= NDCB2_ADDR5_PAGE(page);
1501 } else if (chunk < lt->nchunks - 1) {
1502 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
1503 } else {
1504 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1505 }
1506
1507 /* Always dispatch the PAGEPROG command on the last chunk */
1508 if (chunk == lt->nchunks - 1)
1509 nfc_op.ndcb[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG) | NDCB0_DBC;
1510
1511 ret = marvell_nfc_prepare_cmd(chip);
1512 if (ret)
1513 return ret;
1514
1515 marvell_nfc_send_cmd(chip, &nfc_op);
1516 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1517 "WRDREQ while loading FIFO (data)");
1518 if (ret)
1519 return ret;
1520
1521 /* Transfer the contents */
1522 iowrite32_rep(nfc->regs + NDDB, data, FIFO_REP(data_len));
1523 iowrite32_rep(nfc->regs + NDDB, spare, FIFO_REP(spare_len));
1524
1525 return 0;
1526}
1527
1528static int marvell_nfc_hw_ecc_bch_write_page(struct mtd_info *mtd,
1529 struct nand_chip *chip,
1530 const u8 *buf,
1531 int oob_required, int page)
1532{
1533 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1534 const u8 *data = buf;
1535 const u8 *spare = chip->oob_poi;
1536 int data_len = lt->data_bytes;
1537 int spare_len = lt->spare_bytes;
1538 int chunk, ret;
1539
1540 /* Spare data will be written anyway, so clear it to avoid garbage */
1541 if (!oob_required)
1542 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1543
1544 marvell_nfc_enable_hw_ecc(chip);
1545
1546 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1547 if (chunk >= lt->full_chunk_cnt) {
1548 data_len = lt->last_data_bytes;
1549 spare_len = lt->last_spare_bytes;
1550 }
1551
1552 marvell_nfc_hw_ecc_bch_write_chunk(chip, chunk, data, data_len,
1553 spare, spare_len, page);
1554 data += data_len;
1555 spare += spare_len;
1556
1557 /*
1558 * Waiting only for CMDD or PAGED is not enough, ECC are
1559 * partially written. No flag is set once the operation is
1560 * really finished but the ND_RUN bit is cleared, so wait for it
1561 * before stepping into the next command.
1562 */
1563 marvell_nfc_wait_ndrun(chip);
1564 }
1565
1566 ret = marvell_nfc_wait_op(chip,
b76401fc 1567 PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max));
02f26ecf
MR
1568
1569 marvell_nfc_disable_hw_ecc(chip);
1570
1571 if (ret)
1572 return ret;
1573
1574 return 0;
1575}
1576
1577static int marvell_nfc_hw_ecc_bch_write_oob_raw(struct mtd_info *mtd,
1578 struct nand_chip *chip,
1579 int page)
1580{
1581 /* Invalidate page cache */
1582 chip->pagebuf = -1;
1583
1584 memset(chip->data_buf, 0xFF, mtd->writesize);
1585
1586 return chip->ecc.write_page_raw(mtd, chip, chip->data_buf, true, page);
1587}
1588
1589static int marvell_nfc_hw_ecc_bch_write_oob(struct mtd_info *mtd,
1590 struct nand_chip *chip, int page)
1591{
1592 /* Invalidate page cache */
1593 chip->pagebuf = -1;
1594
1595 memset(chip->data_buf, 0xFF, mtd->writesize);
1596
1597 return chip->ecc.write_page(mtd, chip, chip->data_buf, true, page);
1598}
1599
1600/* NAND framework ->exec_op() hooks and related helpers */
1601static void marvell_nfc_parse_instructions(struct nand_chip *chip,
1602 const struct nand_subop *subop,
1603 struct marvell_nfc_op *nfc_op)
1604{
1605 const struct nand_op_instr *instr = NULL;
1606 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1607 bool first_cmd = true;
1608 unsigned int op_id;
1609 int i;
1610
1611 /* Reset the input structure as most of its fields will be OR'ed */
1612 memset(nfc_op, 0, sizeof(struct marvell_nfc_op));
1613
1614 for (op_id = 0; op_id < subop->ninstrs; op_id++) {
1615 unsigned int offset, naddrs;
1616 const u8 *addrs;
1617 int len = nand_subop_get_data_len(subop, op_id);
1618
1619 instr = &subop->instrs[op_id];
1620
1621 switch (instr->type) {
1622 case NAND_OP_CMD_INSTR:
1623 if (first_cmd)
1624 nfc_op->ndcb[0] |=
1625 NDCB0_CMD1(instr->ctx.cmd.opcode);
1626 else
1627 nfc_op->ndcb[0] |=
1628 NDCB0_CMD2(instr->ctx.cmd.opcode) |
1629 NDCB0_DBC;
1630
1631 nfc_op->cle_ale_delay_ns = instr->delay_ns;
1632 first_cmd = false;
1633 break;
1634
1635 case NAND_OP_ADDR_INSTR:
1636 offset = nand_subop_get_addr_start_off(subop, op_id);
1637 naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
1638 addrs = &instr->ctx.addr.addrs[offset];
1639
1640 nfc_op->ndcb[0] |= NDCB0_ADDR_CYC(naddrs);
1641
1642 for (i = 0; i < min_t(unsigned int, 4, naddrs); i++)
1643 nfc_op->ndcb[1] |= addrs[i] << (8 * i);
1644
1645 if (naddrs >= 5)
1646 nfc_op->ndcb[2] |= NDCB2_ADDR5_CYC(addrs[4]);
1647 if (naddrs >= 6)
1648 nfc_op->ndcb[3] |= NDCB3_ADDR6_CYC(addrs[5]);
1649 if (naddrs == 7)
1650 nfc_op->ndcb[3] |= NDCB3_ADDR7_CYC(addrs[6]);
1651
1652 nfc_op->cle_ale_delay_ns = instr->delay_ns;
1653 break;
1654
1655 case NAND_OP_DATA_IN_INSTR:
1656 nfc_op->data_instr = instr;
1657 nfc_op->data_instr_idx = op_id;
1658 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ);
1659 if (nfc->caps->is_nfcv2) {
1660 nfc_op->ndcb[0] |=
1661 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1662 NDCB0_LEN_OVRD;
1663 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1664 }
1665 nfc_op->data_delay_ns = instr->delay_ns;
1666 break;
1667
1668 case NAND_OP_DATA_OUT_INSTR:
1669 nfc_op->data_instr = instr;
1670 nfc_op->data_instr_idx = op_id;
1671 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE);
1672 if (nfc->caps->is_nfcv2) {
1673 nfc_op->ndcb[0] |=
1674 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1675 NDCB0_LEN_OVRD;
1676 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1677 }
1678 nfc_op->data_delay_ns = instr->delay_ns;
1679 break;
1680
1681 case NAND_OP_WAITRDY_INSTR:
1682 nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
1683 nfc_op->rdy_delay_ns = instr->delay_ns;
1684 break;
1685 }
1686 }
1687}
1688
1689static int marvell_nfc_xfer_data_pio(struct nand_chip *chip,
1690 const struct nand_subop *subop,
1691 struct marvell_nfc_op *nfc_op)
1692{
1693 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1694 const struct nand_op_instr *instr = nfc_op->data_instr;
1695 unsigned int op_id = nfc_op->data_instr_idx;
1696 unsigned int len = nand_subop_get_data_len(subop, op_id);
1697 unsigned int offset = nand_subop_get_data_start_off(subop, op_id);
1698 bool reading = (instr->type == NAND_OP_DATA_IN_INSTR);
1699 int ret;
1700
1701 if (instr->ctx.data.force_8bit)
1702 marvell_nfc_force_byte_access(chip, true);
1703
1704 if (reading) {
1705 u8 *in = instr->ctx.data.buf.in + offset;
1706
1707 ret = marvell_nfc_xfer_data_in_pio(nfc, in, len);
1708 } else {
1709 const u8 *out = instr->ctx.data.buf.out + offset;
1710
1711 ret = marvell_nfc_xfer_data_out_pio(nfc, out, len);
1712 }
1713
1714 if (instr->ctx.data.force_8bit)
1715 marvell_nfc_force_byte_access(chip, false);
1716
1717 return ret;
1718}
1719
1720static int marvell_nfc_monolithic_access_exec(struct nand_chip *chip,
1721 const struct nand_subop *subop)
1722{
1723 struct marvell_nfc_op nfc_op;
1724 bool reading;
1725 int ret;
1726
1727 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1728 reading = (nfc_op.data_instr->type == NAND_OP_DATA_IN_INSTR);
1729
1730 ret = marvell_nfc_prepare_cmd(chip);
1731 if (ret)
1732 return ret;
1733
1734 marvell_nfc_send_cmd(chip, &nfc_op);
1735 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1736 "RDDREQ/WRDREQ while draining raw data");
1737 if (ret)
1738 return ret;
1739
1740 cond_delay(nfc_op.cle_ale_delay_ns);
1741
1742 if (reading) {
1743 if (nfc_op.rdy_timeout_ms) {
1744 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1745 if (ret)
1746 return ret;
1747 }
1748
1749 cond_delay(nfc_op.rdy_delay_ns);
1750 }
1751
1752 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1753 ret = marvell_nfc_wait_cmdd(chip);
1754 if (ret)
1755 return ret;
1756
1757 cond_delay(nfc_op.data_delay_ns);
1758
1759 if (!reading) {
1760 if (nfc_op.rdy_timeout_ms) {
1761 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1762 if (ret)
1763 return ret;
1764 }
1765
1766 cond_delay(nfc_op.rdy_delay_ns);
1767 }
1768
1769 /*
1770 * NDCR ND_RUN bit should be cleared automatically at the end of each
1771 * operation but experience shows that the behavior is buggy when it
1772 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1773 */
1774 if (!reading) {
1775 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1776
1777 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1778 nfc->regs + NDCR);
1779 }
1780
1781 return 0;
1782}
1783
1784static int marvell_nfc_naked_access_exec(struct nand_chip *chip,
1785 const struct nand_subop *subop)
1786{
1787 struct marvell_nfc_op nfc_op;
1788 int ret;
1789
1790 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1791
1792 /*
1793 * Naked access are different in that they need to be flagged as naked
1794 * by the controller. Reset the controller registers fields that inform
1795 * on the type and refill them according to the ongoing operation.
1796 */
1797 nfc_op.ndcb[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK) |
1798 NDCB0_CMD_XTYPE(XTYPE_MASK));
1799 switch (subop->instrs[0].type) {
1800 case NAND_OP_CMD_INSTR:
1801 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD);
1802 break;
1803 case NAND_OP_ADDR_INSTR:
1804 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR);
1805 break;
1806 case NAND_OP_DATA_IN_INSTR:
1807 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ) |
1808 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1809 break;
1810 case NAND_OP_DATA_OUT_INSTR:
1811 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE) |
1812 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1813 break;
1814 default:
1815 /* This should never happen */
1816 break;
1817 }
1818
1819 ret = marvell_nfc_prepare_cmd(chip);
1820 if (ret)
1821 return ret;
1822
1823 marvell_nfc_send_cmd(chip, &nfc_op);
1824
1825 if (!nfc_op.data_instr) {
1826 ret = marvell_nfc_wait_cmdd(chip);
1827 cond_delay(nfc_op.cle_ale_delay_ns);
1828 return ret;
1829 }
1830
1831 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1832 "RDDREQ/WRDREQ while draining raw data");
1833 if (ret)
1834 return ret;
1835
1836 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1837 ret = marvell_nfc_wait_cmdd(chip);
1838 if (ret)
1839 return ret;
1840
1841 /*
1842 * NDCR ND_RUN bit should be cleared automatically at the end of each
1843 * operation but experience shows that the behavior is buggy when it
1844 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1845 */
1846 if (subop->instrs[0].type == NAND_OP_DATA_OUT_INSTR) {
1847 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1848
1849 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1850 nfc->regs + NDCR);
1851 }
1852
1853 return 0;
1854}
1855
1856static int marvell_nfc_naked_waitrdy_exec(struct nand_chip *chip,
1857 const struct nand_subop *subop)
1858{
1859 struct marvell_nfc_op nfc_op;
1860 int ret;
1861
1862 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1863
1864 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1865 cond_delay(nfc_op.rdy_delay_ns);
1866
1867 return ret;
1868}
1869
1870static int marvell_nfc_read_id_type_exec(struct nand_chip *chip,
1871 const struct nand_subop *subop)
1872{
1873 struct marvell_nfc_op nfc_op;
1874 int ret;
1875
1876 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1877 nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1878 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ_ID);
1879
1880 ret = marvell_nfc_prepare_cmd(chip);
1881 if (ret)
1882 return ret;
1883
1884 marvell_nfc_send_cmd(chip, &nfc_op);
1885 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1886 "RDDREQ while reading ID");
1887 if (ret)
1888 return ret;
1889
1890 cond_delay(nfc_op.cle_ale_delay_ns);
1891
1892 if (nfc_op.rdy_timeout_ms) {
1893 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1894 if (ret)
1895 return ret;
1896 }
1897
1898 cond_delay(nfc_op.rdy_delay_ns);
1899
1900 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1901 ret = marvell_nfc_wait_cmdd(chip);
1902 if (ret)
1903 return ret;
1904
1905 cond_delay(nfc_op.data_delay_ns);
1906
1907 return 0;
1908}
1909
1910static int marvell_nfc_read_status_exec(struct nand_chip *chip,
1911 const struct nand_subop *subop)
1912{
1913 struct marvell_nfc_op nfc_op;
1914 int ret;
1915
1916 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1917 nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1918 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_STATUS);
1919
1920 ret = marvell_nfc_prepare_cmd(chip);
1921 if (ret)
1922 return ret;
1923
1924 marvell_nfc_send_cmd(chip, &nfc_op);
1925 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1926 "RDDREQ while reading status");
1927 if (ret)
1928 return ret;
1929
1930 cond_delay(nfc_op.cle_ale_delay_ns);
1931
1932 if (nfc_op.rdy_timeout_ms) {
1933 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1934 if (ret)
1935 return ret;
1936 }
1937
1938 cond_delay(nfc_op.rdy_delay_ns);
1939
1940 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1941 ret = marvell_nfc_wait_cmdd(chip);
1942 if (ret)
1943 return ret;
1944
1945 cond_delay(nfc_op.data_delay_ns);
1946
1947 return 0;
1948}
1949
1950static int marvell_nfc_reset_cmd_type_exec(struct nand_chip *chip,
1951 const struct nand_subop *subop)
1952{
1953 struct marvell_nfc_op nfc_op;
1954 int ret;
1955
1956 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1957 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_RESET);
1958
1959 ret = marvell_nfc_prepare_cmd(chip);
1960 if (ret)
1961 return ret;
1962
1963 marvell_nfc_send_cmd(chip, &nfc_op);
1964 ret = marvell_nfc_wait_cmdd(chip);
1965 if (ret)
1966 return ret;
1967
1968 cond_delay(nfc_op.cle_ale_delay_ns);
1969
1970 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1971 if (ret)
1972 return ret;
1973
1974 cond_delay(nfc_op.rdy_delay_ns);
1975
1976 return 0;
1977}
1978
1979static int marvell_nfc_erase_cmd_type_exec(struct nand_chip *chip,
1980 const struct nand_subop *subop)
1981{
1982 struct marvell_nfc_op nfc_op;
1983 int ret;
1984
1985 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1986 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_ERASE);
1987
1988 ret = marvell_nfc_prepare_cmd(chip);
1989 if (ret)
1990 return ret;
1991
1992 marvell_nfc_send_cmd(chip, &nfc_op);
1993 ret = marvell_nfc_wait_cmdd(chip);
1994 if (ret)
1995 return ret;
1996
1997 cond_delay(nfc_op.cle_ale_delay_ns);
1998
1999 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
2000 if (ret)
2001 return ret;
2002
2003 cond_delay(nfc_op.rdy_delay_ns);
2004
2005 return 0;
2006}
2007
2008static const struct nand_op_parser marvell_nfcv2_op_parser = NAND_OP_PARSER(
2009 /* Monolithic reads/writes */
2010 NAND_OP_PARSER_PATTERN(
2011 marvell_nfc_monolithic_access_exec,
2012 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2013 NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2),
2014 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2015 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
2016 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
2017 NAND_OP_PARSER_PATTERN(
2018 marvell_nfc_monolithic_access_exec,
2019 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2020 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2),
2021 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE),
2022 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2023 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
2024 /* Naked commands */
2025 NAND_OP_PARSER_PATTERN(
2026 marvell_nfc_naked_access_exec,
2027 NAND_OP_PARSER_PAT_CMD_ELEM(false)),
2028 NAND_OP_PARSER_PATTERN(
2029 marvell_nfc_naked_access_exec,
2030 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2)),
2031 NAND_OP_PARSER_PATTERN(
2032 marvell_nfc_naked_access_exec,
2033 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
2034 NAND_OP_PARSER_PATTERN(
2035 marvell_nfc_naked_access_exec,
2036 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE)),
2037 NAND_OP_PARSER_PATTERN(
2038 marvell_nfc_naked_waitrdy_exec,
2039 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2040 );
2041
2042static const struct nand_op_parser marvell_nfcv1_op_parser = NAND_OP_PARSER(
2043 /* Naked commands not supported, use a function for each pattern */
2044 NAND_OP_PARSER_PATTERN(
2045 marvell_nfc_read_id_type_exec,
2046 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2047 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
2048 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 8)),
2049 NAND_OP_PARSER_PATTERN(
2050 marvell_nfc_erase_cmd_type_exec,
2051 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2052 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
2053 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2054 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2055 NAND_OP_PARSER_PATTERN(
2056 marvell_nfc_read_status_exec,
2057 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2058 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)),
2059 NAND_OP_PARSER_PATTERN(
2060 marvell_nfc_reset_cmd_type_exec,
2061 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2062 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2063 NAND_OP_PARSER_PATTERN(
2064 marvell_nfc_naked_waitrdy_exec,
2065 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2066 );
2067
2068static int marvell_nfc_exec_op(struct nand_chip *chip,
2069 const struct nand_operation *op,
2070 bool check_only)
2071{
2072 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2073
2074 if (nfc->caps->is_nfcv2)
2075 return nand_op_parser_exec_op(chip, &marvell_nfcv2_op_parser,
2076 op, check_only);
2077 else
2078 return nand_op_parser_exec_op(chip, &marvell_nfcv1_op_parser,
2079 op, check_only);
2080}
2081
2082/*
2083 * Layouts were broken in old pxa3xx_nand driver, these are supposed to be
2084 * usable.
2085 */
2086static int marvell_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2087 struct mtd_oob_region *oobregion)
2088{
2089 struct nand_chip *chip = mtd_to_nand(mtd);
2090 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2091
2092 if (section)
2093 return -ERANGE;
2094
2095 oobregion->length = (lt->full_chunk_cnt * lt->ecc_bytes) +
2096 lt->last_ecc_bytes;
2097 oobregion->offset = mtd->oobsize - oobregion->length;
2098
2099 return 0;
2100}
2101
2102static int marvell_nand_ooblayout_free(struct mtd_info *mtd, int section,
2103 struct mtd_oob_region *oobregion)
2104{
2105 struct nand_chip *chip = mtd_to_nand(mtd);
2106 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2107
2108 if (section)
2109 return -ERANGE;
2110
2111 /*
2112 * Bootrom looks in bytes 0 & 5 for bad blocks for the
2113 * 4KB page / 4bit BCH combination.
2114 */
2115 if (mtd->writesize == SZ_4K && lt->data_bytes == SZ_2K)
2116 oobregion->offset = 6;
2117 else
2118 oobregion->offset = 2;
2119
2120 oobregion->length = (lt->full_chunk_cnt * lt->spare_bytes) +
2121 lt->last_spare_bytes - oobregion->offset;
2122
2123 return 0;
2124}
2125
2126static const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops = {
2127 .ecc = marvell_nand_ooblayout_ecc,
2128 .free = marvell_nand_ooblayout_free,
2129};
2130
2131static int marvell_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
2132 struct nand_ecc_ctrl *ecc)
2133{
2134 struct nand_chip *chip = mtd_to_nand(mtd);
2135 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2136 const struct marvell_hw_ecc_layout *l;
2137 int i;
2138
2139 if (!nfc->caps->is_nfcv2 &&
2140 (mtd->writesize + mtd->oobsize > MAX_CHUNK_SIZE)) {
2141 dev_err(nfc->dev,
2142 "NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n",
2143 mtd->writesize, MAX_CHUNK_SIZE - mtd->oobsize);
2144 return -ENOTSUPP;
2145 }
2146
2147 to_marvell_nand(chip)->layout = NULL;
2148 for (i = 0; i < ARRAY_SIZE(marvell_nfc_layouts); i++) {
2149 l = &marvell_nfc_layouts[i];
2150 if (mtd->writesize == l->writesize &&
2151 ecc->size == l->chunk && ecc->strength == l->strength) {
2152 to_marvell_nand(chip)->layout = l;
2153 break;
2154 }
2155 }
2156
2157 if (!to_marvell_nand(chip)->layout ||
2158 (!nfc->caps->is_nfcv2 && ecc->strength > 1)) {
2159 dev_err(nfc->dev,
2160 "ECC strength %d at page size %d is not supported\n",
2161 ecc->strength, mtd->writesize);
2162 return -ENOTSUPP;
2163 }
2164
2165 mtd_set_ooblayout(mtd, &marvell_nand_ooblayout_ops);
2166 ecc->steps = l->nchunks;
2167 ecc->size = l->data_bytes;
2168
2169 if (ecc->strength == 1) {
2170 chip->ecc.algo = NAND_ECC_HAMMING;
2171 ecc->read_page_raw = marvell_nfc_hw_ecc_hmg_read_page_raw;
2172 ecc->read_page = marvell_nfc_hw_ecc_hmg_read_page;
2173 ecc->read_oob_raw = marvell_nfc_hw_ecc_hmg_read_oob_raw;
2174 ecc->read_oob = ecc->read_oob_raw;
2175 ecc->write_page_raw = marvell_nfc_hw_ecc_hmg_write_page_raw;
2176 ecc->write_page = marvell_nfc_hw_ecc_hmg_write_page;
2177 ecc->write_oob_raw = marvell_nfc_hw_ecc_hmg_write_oob_raw;
2178 ecc->write_oob = ecc->write_oob_raw;
2179 } else {
2180 chip->ecc.algo = NAND_ECC_BCH;
2181 ecc->strength = 16;
2182 ecc->read_page_raw = marvell_nfc_hw_ecc_bch_read_page_raw;
2183 ecc->read_page = marvell_nfc_hw_ecc_bch_read_page;
2184 ecc->read_oob_raw = marvell_nfc_hw_ecc_bch_read_oob_raw;
2185 ecc->read_oob = marvell_nfc_hw_ecc_bch_read_oob;
2186 ecc->write_page_raw = marvell_nfc_hw_ecc_bch_write_page_raw;
2187 ecc->write_page = marvell_nfc_hw_ecc_bch_write_page;
2188 ecc->write_oob_raw = marvell_nfc_hw_ecc_bch_write_oob_raw;
2189 ecc->write_oob = marvell_nfc_hw_ecc_bch_write_oob;
2190 }
2191
2192 return 0;
2193}
2194
2195static int marvell_nand_ecc_init(struct mtd_info *mtd,
2196 struct nand_ecc_ctrl *ecc)
2197{
2198 struct nand_chip *chip = mtd_to_nand(mtd);
2199 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2200 int ret;
2201
2202 if (ecc->mode != NAND_ECC_NONE && (!ecc->size || !ecc->strength)) {
2203 if (chip->ecc_step_ds && chip->ecc_strength_ds) {
2204 ecc->size = chip->ecc_step_ds;
2205 ecc->strength = chip->ecc_strength_ds;
2206 } else {
2207 dev_info(nfc->dev,
2208 "No minimum ECC strength, using 1b/512B\n");
2209 ecc->size = 512;
2210 ecc->strength = 1;
2211 }
2212 }
2213
2214 switch (ecc->mode) {
2215 case NAND_ECC_HW:
2216 ret = marvell_nand_hw_ecc_ctrl_init(mtd, ecc);
2217 if (ret)
2218 return ret;
2219 break;
2220 case NAND_ECC_NONE:
2221 case NAND_ECC_SOFT:
ed6d0285 2222 case NAND_ECC_ON_DIE:
02f26ecf
MR
2223 if (!nfc->caps->is_nfcv2 && mtd->writesize != SZ_512 &&
2224 mtd->writesize != SZ_2K) {
2225 dev_err(nfc->dev, "NFCv1 cannot write %d bytes pages\n",
2226 mtd->writesize);
2227 return -EINVAL;
2228 }
2229 break;
2230 default:
2231 return -EINVAL;
2232 }
2233
2234 return 0;
2235}
2236
2237static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
2238static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
2239
2240static struct nand_bbt_descr bbt_main_descr = {
2241 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2242 NAND_BBT_2BIT | NAND_BBT_VERSION,
2243 .offs = 8,
2244 .len = 6,
2245 .veroffs = 14,
2246 .maxblocks = 8, /* Last 8 blocks in each chip */
2247 .pattern = bbt_pattern
2248};
2249
2250static struct nand_bbt_descr bbt_mirror_descr = {
2251 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2252 NAND_BBT_2BIT | NAND_BBT_VERSION,
2253 .offs = 8,
2254 .len = 6,
2255 .veroffs = 14,
2256 .maxblocks = 8, /* Last 8 blocks in each chip */
2257 .pattern = bbt_mirror_pattern
2258};
2259
2260static int marvell_nfc_setup_data_interface(struct mtd_info *mtd, int chipnr,
2261 const struct nand_data_interface
2262 *conf)
2263{
2264 struct nand_chip *chip = mtd_to_nand(mtd);
2265 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2266 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
6b6de654 2267 unsigned int period_ns = 1000000000 / clk_get_rate(nfc->core_clk) * 2;
02f26ecf
MR
2268 const struct nand_sdr_timings *sdr;
2269 struct marvell_nfc_timings nfc_tmg;
2270 int read_delay;
2271
2272 sdr = nand_get_sdr_timings(conf);
2273 if (IS_ERR(sdr))
2274 return PTR_ERR(sdr);
2275
2276 /*
2277 * SDR timings are given in pico-seconds while NFC timings must be
2278 * expressed in NAND controller clock cycles, which is half of the
2279 * frequency of the accessible ECC clock retrieved by clk_get_rate().
2280 * This is not written anywhere in the datasheet but was observed
2281 * with an oscilloscope.
2282 *
2283 * NFC datasheet gives equations from which thoses calculations
2284 * are derived, they tend to be slightly more restrictives than the
2285 * given core timings and may improve the overall speed.
2286 */
2287 nfc_tmg.tRP = TO_CYCLES(DIV_ROUND_UP(sdr->tRC_min, 2), period_ns) - 1;
2288 nfc_tmg.tRH = nfc_tmg.tRP;
2289 nfc_tmg.tWP = TO_CYCLES(DIV_ROUND_UP(sdr->tWC_min, 2), period_ns) - 1;
2290 nfc_tmg.tWH = nfc_tmg.tWP;
2291 nfc_tmg.tCS = TO_CYCLES(sdr->tCS_min, period_ns);
2292 nfc_tmg.tCH = TO_CYCLES(sdr->tCH_min, period_ns) - 1;
2293 nfc_tmg.tADL = TO_CYCLES(sdr->tADL_min, period_ns);
2294 /*
2295 * Read delay is the time of propagation from SoC pins to NFC internal
2296 * logic. With non-EDO timings, this is MIN_RD_DEL_CNT clock cycles. In
2297 * EDO mode, an additional delay of tRH must be taken into account so
2298 * the data is sampled on the falling edge instead of the rising edge.
2299 */
2300 read_delay = sdr->tRC_min >= 30000 ?
2301 MIN_RD_DEL_CNT : MIN_RD_DEL_CNT + nfc_tmg.tRH;
2302
2303 nfc_tmg.tAR = TO_CYCLES(sdr->tAR_min, period_ns);
2304 /*
2305 * tWHR and tRHW are supposed to be read to write delays (and vice
2306 * versa) but in some cases, ie. when doing a change column, they must
2307 * be greater than that to be sure tCCS delay is respected.
2308 */
2309 nfc_tmg.tWHR = TO_CYCLES(max_t(int, sdr->tWHR_min, sdr->tCCS_min),
2310 period_ns) - 2,
2311 nfc_tmg.tRHW = TO_CYCLES(max_t(int, sdr->tRHW_min, sdr->tCCS_min),
2312 period_ns);
2313
07ad5a72
MR
2314 /*
2315 * NFCv2: Use WAIT_MODE (wait for RB line), do not rely only on delays.
2316 * NFCv1: No WAIT_MODE, tR must be maximal.
2317 */
2318 if (nfc->caps->is_nfcv2) {
2319 nfc_tmg.tR = TO_CYCLES(sdr->tWB_max, period_ns);
2320 } else {
2321 nfc_tmg.tR = TO_CYCLES64(sdr->tWB_max + sdr->tR_max,
2322 period_ns);
2323 if (nfc_tmg.tR + 3 > nfc_tmg.tCH)
2324 nfc_tmg.tR = nfc_tmg.tCH - 3;
2325 else
2326 nfc_tmg.tR = 0;
2327 }
02f26ecf
MR
2328
2329 if (chipnr < 0)
2330 return 0;
2331
2332 marvell_nand->ndtr0 =
2333 NDTR0_TRP(nfc_tmg.tRP) |
2334 NDTR0_TRH(nfc_tmg.tRH) |
2335 NDTR0_ETRP(nfc_tmg.tRP) |
2336 NDTR0_TWP(nfc_tmg.tWP) |
2337 NDTR0_TWH(nfc_tmg.tWH) |
2338 NDTR0_TCS(nfc_tmg.tCS) |
07ad5a72 2339 NDTR0_TCH(nfc_tmg.tCH);
02f26ecf
MR
2340
2341 marvell_nand->ndtr1 =
2342 NDTR1_TAR(nfc_tmg.tAR) |
2343 NDTR1_TWHR(nfc_tmg.tWHR) |
02f26ecf
MR
2344 NDTR1_TR(nfc_tmg.tR);
2345
07ad5a72
MR
2346 if (nfc->caps->is_nfcv2) {
2347 marvell_nand->ndtr0 |=
2348 NDTR0_RD_CNT_DEL(read_delay) |
2349 NDTR0_SELCNTR |
2350 NDTR0_TADL(nfc_tmg.tADL);
2351
2352 marvell_nand->ndtr1 |=
2353 NDTR1_TRHW(nfc_tmg.tRHW) |
2354 NDTR1_WAIT_MODE;
2355 }
2356
02f26ecf
MR
2357 return 0;
2358}
2359
8831e48b
MR
2360static int marvell_nand_attach_chip(struct nand_chip *chip)
2361{
2362 struct mtd_info *mtd = nand_to_mtd(chip);
2363 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2364 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2365 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(nfc->dev);
2366 int ret;
2367
2368 if (pdata && pdata->flash_bbt)
2369 chip->bbt_options |= NAND_BBT_USE_FLASH;
2370
2371 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
2372 /*
2373 * We'll use a bad block table stored in-flash and don't
2374 * allow writing the bad block marker to the flash.
2375 */
2376 chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
2377 chip->bbt_td = &bbt_main_descr;
2378 chip->bbt_md = &bbt_mirror_descr;
2379 }
2380
2381 /* Save the chip-specific fields of NDCR */
2382 marvell_nand->ndcr = NDCR_PAGE_SZ(mtd->writesize);
2383 if (chip->options & NAND_BUSWIDTH_16)
2384 marvell_nand->ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
2385
2386 /*
2387 * On small page NANDs, only one cycle is needed to pass the
2388 * column address.
2389 */
2390 if (mtd->writesize <= 512) {
2391 marvell_nand->addr_cyc = 1;
2392 } else {
2393 marvell_nand->addr_cyc = 2;
2394 marvell_nand->ndcr |= NDCR_RA_START;
2395 }
2396
2397 /*
2398 * Now add the number of cycles needed to pass the row
2399 * address.
2400 *
2401 * Addressing a chip using CS 2 or 3 should also need the third row
2402 * cycle but due to inconsistance in the documentation and lack of
2403 * hardware to test this situation, this case is not supported.
2404 */
2405 if (chip->options & NAND_ROW_ADDR_3)
2406 marvell_nand->addr_cyc += 3;
2407 else
2408 marvell_nand->addr_cyc += 2;
2409
2410 if (pdata) {
2411 chip->ecc.size = pdata->ecc_step_size;
2412 chip->ecc.strength = pdata->ecc_strength;
2413 }
2414
2415 ret = marvell_nand_ecc_init(mtd, &chip->ecc);
2416 if (ret) {
2417 dev_err(nfc->dev, "ECC init failed: %d\n", ret);
2418 return ret;
2419 }
2420
2421 if (chip->ecc.mode == NAND_ECC_HW) {
2422 /*
2423 * Subpage write not available with hardware ECC, prohibit also
2424 * subpage read as in userspace subpage access would still be
2425 * allowed and subpage write, if used, would lead to numerous
2426 * uncorrectable ECC errors.
2427 */
2428 chip->options |= NAND_NO_SUBPAGE_WRITE;
2429 }
2430
2431 if (pdata || nfc->caps->legacy_of_bindings) {
2432 /*
2433 * We keep the MTD name unchanged to avoid breaking platforms
2434 * where the MTD cmdline parser is used and the bootloader
2435 * has not been updated to use the new naming scheme.
2436 */
2437 mtd->name = "pxa3xx_nand-0";
2438 } else if (!mtd->name) {
2439 /*
2440 * If the new bindings are used and the bootloader has not been
2441 * updated to pass a new mtdparts parameter on the cmdline, you
2442 * should define the following property in your NAND node, ie:
2443 *
2444 * label = "main-storage";
2445 *
2446 * This way, mtd->name will be set by the core when
2447 * nand_set_flash_node() is called.
2448 */
2449 mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL,
2450 "%s:nand.%d", dev_name(nfc->dev),
2451 marvell_nand->sels[0].cs);
2452 if (!mtd->name) {
2453 dev_err(nfc->dev, "Failed to allocate mtd->name\n");
2454 return -ENOMEM;
2455 }
2456 }
2457
2458 return 0;
2459}
2460
2461static const struct nand_controller_ops marvell_nand_controller_ops = {
2462 .attach_chip = marvell_nand_attach_chip,
2463};
2464
02f26ecf
MR
2465static int marvell_nand_chip_init(struct device *dev, struct marvell_nfc *nfc,
2466 struct device_node *np)
2467{
2468 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(dev);
2469 struct marvell_nand_chip *marvell_nand;
2470 struct mtd_info *mtd;
2471 struct nand_chip *chip;
2472 int nsels, ret, i;
2473 u32 cs, rb;
2474
2475 /*
2476 * The legacy "num-cs" property indicates the number of CS on the only
2477 * chip connected to the controller (legacy bindings does not support
f6997bec 2478 * more than one chip). The CS and RB pins are always the #0.
02f26ecf
MR
2479 *
2480 * When not using legacy bindings, a couple of "reg" and "nand-rb"
2481 * properties must be filled. For each chip, expressed as a subnode,
2482 * "reg" points to the CS lines and "nand-rb" to the RB line.
2483 */
f6997bec 2484 if (pdata || nfc->caps->legacy_of_bindings) {
02f26ecf 2485 nsels = 1;
f6997bec
MR
2486 } else {
2487 nsels = of_property_count_elems_of_size(np, "reg", sizeof(u32));
2488 if (nsels <= 0) {
2489 dev_err(dev, "missing/invalid reg property\n");
2490 return -EINVAL;
2491 }
02f26ecf
MR
2492 }
2493
2494 /* Alloc the nand chip structure */
2495 marvell_nand = devm_kzalloc(dev, sizeof(*marvell_nand) +
2496 (nsels *
2497 sizeof(struct marvell_nand_chip_sel)),
2498 GFP_KERNEL);
2499 if (!marvell_nand) {
2500 dev_err(dev, "could not allocate chip structure\n");
2501 return -ENOMEM;
2502 }
2503
2504 marvell_nand->nsels = nsels;
2505 marvell_nand->selected_die = -1;
2506
2507 for (i = 0; i < nsels; i++) {
2508 if (pdata || nfc->caps->legacy_of_bindings) {
2509 /*
2510 * Legacy bindings use the CS lines in natural
2511 * order (0, 1, ...)
2512 */
2513 cs = i;
2514 } else {
2515 /* Retrieve CS id */
2516 ret = of_property_read_u32_index(np, "reg", i, &cs);
2517 if (ret) {
2518 dev_err(dev, "could not retrieve reg property: %d\n",
2519 ret);
2520 return ret;
2521 }
2522 }
2523
2524 if (cs >= nfc->caps->max_cs_nb) {
2525 dev_err(dev, "invalid reg value: %u (max CS = %d)\n",
2526 cs, nfc->caps->max_cs_nb);
2527 return -EINVAL;
2528 }
2529
2530 if (test_and_set_bit(cs, &nfc->assigned_cs)) {
2531 dev_err(dev, "CS %d already assigned\n", cs);
2532 return -EINVAL;
2533 }
2534
2535 /*
2536 * The cs variable represents the chip select id, which must be
2537 * converted in bit fields for NDCB0 and NDCB2 to select the
2538 * right chip. Unfortunately, due to a lack of information on
2539 * the subject and incoherent documentation, the user should not
2540 * use CS1 and CS3 at all as asserting them is not supported in
2541 * a reliable way (due to multiplexing inside ADDR5 field).
2542 */
2543 marvell_nand->sels[i].cs = cs;
2544 switch (cs) {
2545 case 0:
2546 case 2:
2547 marvell_nand->sels[i].ndcb0_csel = 0;
2548 break;
2549 case 1:
2550 case 3:
2551 marvell_nand->sels[i].ndcb0_csel = NDCB0_CSEL;
2552 break;
2553 default:
2554 return -EINVAL;
2555 }
2556
2557 /* Retrieve RB id */
2558 if (pdata || nfc->caps->legacy_of_bindings) {
2559 /* Legacy bindings always use RB #0 */
2560 rb = 0;
2561 } else {
2562 ret = of_property_read_u32_index(np, "nand-rb", i,
2563 &rb);
2564 if (ret) {
2565 dev_err(dev,
2566 "could not retrieve RB property: %d\n",
2567 ret);
2568 return ret;
2569 }
2570 }
2571
2572 if (rb >= nfc->caps->max_rb_nb) {
2573 dev_err(dev, "invalid reg value: %u (max RB = %d)\n",
2574 rb, nfc->caps->max_rb_nb);
2575 return -EINVAL;
2576 }
2577
2578 marvell_nand->sels[i].rb = rb;
2579 }
2580
2581 chip = &marvell_nand->chip;
2582 chip->controller = &nfc->controller;
2583 nand_set_flash_node(chip, np);
2584
2585 chip->exec_op = marvell_nfc_exec_op;
2586 chip->select_chip = marvell_nfc_select_chip;
07ad5a72 2587 if (!of_property_read_bool(np, "marvell,nand-keep-config"))
02f26ecf
MR
2588 chip->setup_data_interface = marvell_nfc_setup_data_interface;
2589
2590 mtd = nand_to_mtd(chip);
2591 mtd->dev.parent = dev;
2592
2593 /*
2594 * Default to HW ECC engine mode. If the nand-ecc-mode property is given
2595 * in the DT node, this entry will be overwritten in nand_scan_ident().
2596 */
2597 chip->ecc.mode = NAND_ECC_HW;
2598
2599 /*
2600 * Save a reference value for timing registers before
2601 * ->setup_data_interface() is called.
2602 */
2603 marvell_nand->ndtr0 = readl_relaxed(nfc->regs + NDTR0);
2604 marvell_nand->ndtr1 = readl_relaxed(nfc->regs + NDTR1);
2605
2606 chip->options |= NAND_BUSWIDTH_AUTO;
02f26ecf 2607
00ad378f 2608 ret = nand_scan(chip, marvell_nand->nsels);
02f26ecf 2609 if (ret) {
8831e48b 2610 dev_err(dev, "could not scan the nand chip\n");
02f26ecf
MR
2611 return ret;
2612 }
2613
2614 if (pdata)
2615 /* Legacy bindings support only one chip */
7576594c 2616 ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
02f26ecf
MR
2617 else
2618 ret = mtd_device_register(mtd, NULL, 0);
2619 if (ret) {
2620 dev_err(dev, "failed to register mtd device: %d\n", ret);
59ac276f 2621 nand_release(chip);
02f26ecf
MR
2622 return ret;
2623 }
2624
2625 list_add_tail(&marvell_nand->node, &nfc->chips);
2626
2627 return 0;
2628}
2629
2630static int marvell_nand_chips_init(struct device *dev, struct marvell_nfc *nfc)
2631{
2632 struct device_node *np = dev->of_node;
2633 struct device_node *nand_np;
2634 int max_cs = nfc->caps->max_cs_nb;
2635 int nchips;
2636 int ret;
2637
2638 if (!np)
2639 nchips = 1;
2640 else
2641 nchips = of_get_child_count(np);
2642
2643 if (nchips > max_cs) {
2644 dev_err(dev, "too many NAND chips: %d (max = %d CS)\n", nchips,
2645 max_cs);
2646 return -EINVAL;
2647 }
2648
2649 /*
2650 * Legacy bindings do not use child nodes to exhibit NAND chip
2651 * properties and layout. Instead, NAND properties are mixed with the
2652 * controller ones, and partitions are defined as direct subnodes of the
2653 * NAND controller node.
2654 */
2655 if (nfc->caps->legacy_of_bindings) {
2656 ret = marvell_nand_chip_init(dev, nfc, np);
2657 return ret;
2658 }
2659
2660 for_each_child_of_node(np, nand_np) {
2661 ret = marvell_nand_chip_init(dev, nfc, nand_np);
2662 if (ret) {
2663 of_node_put(nand_np);
2664 return ret;
2665 }
2666 }
2667
2668 return 0;
2669}
2670
2671static void marvell_nand_chips_cleanup(struct marvell_nfc *nfc)
2672{
2673 struct marvell_nand_chip *entry, *temp;
2674
2675 list_for_each_entry_safe(entry, temp, &nfc->chips, node) {
59ac276f 2676 nand_release(&entry->chip);
02f26ecf
MR
2677 list_del(&entry->node);
2678 }
2679}
2680
2681static int marvell_nfc_init_dma(struct marvell_nfc *nfc)
2682{
2683 struct platform_device *pdev = container_of(nfc->dev,
2684 struct platform_device,
2685 dev);
2686 struct dma_slave_config config = {};
2687 struct resource *r;
02f26ecf
MR
2688 int ret;
2689
2690 if (!IS_ENABLED(CONFIG_PXA_DMA)) {
2691 dev_warn(nfc->dev,
2692 "DMA not enabled in configuration\n");
2693 return -ENOTSUPP;
2694 }
2695
2696 ret = dma_set_mask_and_coherent(nfc->dev, DMA_BIT_MASK(32));
2697 if (ret)
2698 return ret;
2699
ac75a50b 2700 nfc->dma_chan = dma_request_slave_channel(nfc->dev, "data");
02f26ecf
MR
2701 if (!nfc->dma_chan) {
2702 dev_err(nfc->dev,
2703 "Unable to request data DMA channel\n");
2704 return -ENODEV;
2705 }
2706
2707 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2708 if (!r)
2709 return -ENXIO;
2710
2711 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2712 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2713 config.src_addr = r->start + NDDB;
2714 config.dst_addr = r->start + NDDB;
2715 config.src_maxburst = 32;
2716 config.dst_maxburst = 32;
2717 ret = dmaengine_slave_config(nfc->dma_chan, &config);
2718 if (ret < 0) {
2719 dev_err(nfc->dev, "Failed to configure DMA channel\n");
2720 return ret;
2721 }
2722
2723 /*
2724 * DMA must act on length multiple of 32 and this length may be
2725 * bigger than the destination buffer. Use this buffer instead
2726 * for DMA transfers and then copy the desired amount of data to
2727 * the provided buffer.
2728 */
c495a927 2729 nfc->dma_buf = kmalloc(MAX_CHUNK_SIZE, GFP_KERNEL | GFP_DMA);
02f26ecf
MR
2730 if (!nfc->dma_buf)
2731 return -ENOMEM;
2732
2733 nfc->use_dma = true;
2734
2735 return 0;
2736}
2737
bd9c3f9b
DM
2738static void marvell_nfc_reset(struct marvell_nfc *nfc)
2739{
2740 /*
2741 * ECC operations and interruptions are only enabled when specifically
2742 * needed. ECC shall not be activated in the early stages (fails probe).
2743 * Arbiter flag, even if marked as "reserved", must be set (empirical).
2744 * SPARE_EN bit must always be set or ECC bytes will not be at the same
2745 * offset in the read page and this will fail the protection.
2746 */
2747 writel_relaxed(NDCR_ALL_INT | NDCR_ND_ARB_EN | NDCR_SPARE_EN |
2748 NDCR_RD_ID_CNT(NFCV1_READID_LEN), nfc->regs + NDCR);
2749 writel_relaxed(0xFFFFFFFF, nfc->regs + NDSR);
2750 writel_relaxed(0, nfc->regs + NDECCCTRL);
2751}
2752
02f26ecf
MR
2753static int marvell_nfc_init(struct marvell_nfc *nfc)
2754{
2755 struct device_node *np = nfc->dev->of_node;
2756
2757 /*
2758 * Some SoCs like A7k/A8k need to enable manually the NAND
2759 * controller, gated clocks and reset bits to avoid being bootloader
2760 * dependent. This is done through the use of the System Functions
2761 * registers.
2762 */
2763 if (nfc->caps->need_system_controller) {
2764 struct regmap *sysctrl_base =
2765 syscon_regmap_lookup_by_phandle(np,
2766 "marvell,system-controller");
02f26ecf
MR
2767
2768 if (IS_ERR(sysctrl_base))
2769 return PTR_ERR(sysctrl_base);
2770
88aa3bbf
TP
2771 regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX,
2772 GENCONF_SOC_DEVICE_MUX_NFC_EN |
2773 GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST |
2774 GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST |
2775 GENCONF_SOC_DEVICE_MUX_NFC_INT_EN);
02f26ecf 2776
88aa3bbf
TP
2777 regmap_update_bits(sysctrl_base, GENCONF_CLK_GATING_CTRL,
2778 GENCONF_CLK_GATING_CTRL_ND_GATE,
2779 GENCONF_CLK_GATING_CTRL_ND_GATE);
02f26ecf 2780
88aa3bbf
TP
2781 regmap_update_bits(sysctrl_base, GENCONF_ND_CLK_CTRL,
2782 GENCONF_ND_CLK_CTRL_EN,
2783 GENCONF_ND_CLK_CTRL_EN);
02f26ecf
MR
2784 }
2785
2786 /* Configure the DMA if appropriate */
2787 if (!nfc->caps->is_nfcv2)
2788 marvell_nfc_init_dma(nfc);
2789
bd9c3f9b 2790 marvell_nfc_reset(nfc);
02f26ecf
MR
2791
2792 return 0;
2793}
2794
2795static int marvell_nfc_probe(struct platform_device *pdev)
2796{
2797 struct device *dev = &pdev->dev;
2798 struct resource *r;
2799 struct marvell_nfc *nfc;
2800 int ret;
2801 int irq;
2802
2803 nfc = devm_kzalloc(&pdev->dev, sizeof(struct marvell_nfc),
2804 GFP_KERNEL);
2805 if (!nfc)
2806 return -ENOMEM;
2807
2808 nfc->dev = dev;
7da45139 2809 nand_controller_init(&nfc->controller);
8831e48b 2810 nfc->controller.ops = &marvell_nand_controller_ops;
02f26ecf
MR
2811 INIT_LIST_HEAD(&nfc->chips);
2812
2813 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2814 nfc->regs = devm_ioremap_resource(dev, r);
2815 if (IS_ERR(nfc->regs))
2816 return PTR_ERR(nfc->regs);
2817
2818 irq = platform_get_irq(pdev, 0);
2819 if (irq < 0) {
2820 dev_err(dev, "failed to retrieve irq\n");
2821 return irq;
2822 }
2823
6b6de654 2824 nfc->core_clk = devm_clk_get(&pdev->dev, "core");
961ba15c
GC
2825
2826 /* Managed the legacy case (when the first clock was not named) */
6b6de654
BB
2827 if (nfc->core_clk == ERR_PTR(-ENOENT))
2828 nfc->core_clk = devm_clk_get(&pdev->dev, NULL);
961ba15c 2829
6b6de654
BB
2830 if (IS_ERR(nfc->core_clk))
2831 return PTR_ERR(nfc->core_clk);
02f26ecf 2832
6b6de654 2833 ret = clk_prepare_enable(nfc->core_clk);
02f26ecf
MR
2834 if (ret)
2835 return ret;
2836
961ba15c 2837 nfc->reg_clk = devm_clk_get(&pdev->dev, "reg");
f9e64d61
DM
2838 if (IS_ERR(nfc->reg_clk)) {
2839 if (PTR_ERR(nfc->reg_clk) != -ENOENT) {
961ba15c 2840 ret = PTR_ERR(nfc->reg_clk);
6b6de654 2841 goto unprepare_core_clk;
961ba15c 2842 }
f9e64d61
DM
2843
2844 nfc->reg_clk = NULL;
961ba15c
GC
2845 }
2846
f9e64d61
DM
2847 ret = clk_prepare_enable(nfc->reg_clk);
2848 if (ret)
2849 goto unprepare_core_clk;
2850
02f26ecf
MR
2851 marvell_nfc_disable_int(nfc, NDCR_ALL_INT);
2852 marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
2853 ret = devm_request_irq(dev, irq, marvell_nfc_isr,
2854 0, "marvell-nfc", nfc);
2855 if (ret)
961ba15c 2856 goto unprepare_reg_clk;
02f26ecf
MR
2857
2858 /* Get NAND controller capabilities */
2859 if (pdev->id_entry)
2860 nfc->caps = (void *)pdev->id_entry->driver_data;
2861 else
2862 nfc->caps = of_device_get_match_data(&pdev->dev);
2863
2864 if (!nfc->caps) {
2865 dev_err(dev, "Could not retrieve NFC caps\n");
2866 ret = -EINVAL;
961ba15c 2867 goto unprepare_reg_clk;
02f26ecf
MR
2868 }
2869
2870 /* Init the controller and then probe the chips */
2871 ret = marvell_nfc_init(nfc);
2872 if (ret)
961ba15c 2873 goto unprepare_reg_clk;
02f26ecf
MR
2874
2875 platform_set_drvdata(pdev, nfc);
2876
2877 ret = marvell_nand_chips_init(dev, nfc);
2878 if (ret)
961ba15c 2879 goto unprepare_reg_clk;
02f26ecf
MR
2880
2881 return 0;
2882
961ba15c
GC
2883unprepare_reg_clk:
2884 clk_disable_unprepare(nfc->reg_clk);
6b6de654
BB
2885unprepare_core_clk:
2886 clk_disable_unprepare(nfc->core_clk);
02f26ecf
MR
2887
2888 return ret;
2889}
2890
2891static int marvell_nfc_remove(struct platform_device *pdev)
2892{
2893 struct marvell_nfc *nfc = platform_get_drvdata(pdev);
2894
2895 marvell_nand_chips_cleanup(nfc);
2896
2897 if (nfc->use_dma) {
2898 dmaengine_terminate_all(nfc->dma_chan);
2899 dma_release_channel(nfc->dma_chan);
2900 }
2901
961ba15c 2902 clk_disable_unprepare(nfc->reg_clk);
6b6de654 2903 clk_disable_unprepare(nfc->core_clk);
02f26ecf
MR
2904
2905 return 0;
2906}
2907
bd9c3f9b
DM
2908static int __maybe_unused marvell_nfc_suspend(struct device *dev)
2909{
2910 struct marvell_nfc *nfc = dev_get_drvdata(dev);
2911 struct marvell_nand_chip *chip;
2912
2913 list_for_each_entry(chip, &nfc->chips, node)
2914 marvell_nfc_wait_ndrun(&chip->chip);
2915
2916 clk_disable_unprepare(nfc->reg_clk);
2917 clk_disable_unprepare(nfc->core_clk);
2918
2919 return 0;
2920}
2921
2922static int __maybe_unused marvell_nfc_resume(struct device *dev)
2923{
2924 struct marvell_nfc *nfc = dev_get_drvdata(dev);
2925 int ret;
2926
2927 ret = clk_prepare_enable(nfc->core_clk);
2928 if (ret < 0)
2929 return ret;
2930
f9e64d61
DM
2931 ret = clk_prepare_enable(nfc->reg_clk);
2932 if (ret < 0)
2933 return ret;
bd9c3f9b
DM
2934
2935 /*
2936 * Reset nfc->selected_chip so the next command will cause the timing
2937 * registers to be restored in marvell_nfc_select_chip().
2938 */
2939 nfc->selected_chip = NULL;
2940
2941 /* Reset registers that have lost their contents */
2942 marvell_nfc_reset(nfc);
2943
2944 return 0;
2945}
2946
2947static const struct dev_pm_ops marvell_nfc_pm_ops = {
2948 SET_SYSTEM_SLEEP_PM_OPS(marvell_nfc_suspend, marvell_nfc_resume)
2949};
2950
02f26ecf
MR
2951static const struct marvell_nfc_caps marvell_armada_8k_nfc_caps = {
2952 .max_cs_nb = 4,
2953 .max_rb_nb = 2,
2954 .need_system_controller = true,
2955 .is_nfcv2 = true,
2956};
2957
2958static const struct marvell_nfc_caps marvell_armada370_nfc_caps = {
2959 .max_cs_nb = 4,
2960 .max_rb_nb = 2,
2961 .is_nfcv2 = true,
2962};
2963
2964static const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps = {
2965 .max_cs_nb = 2,
2966 .max_rb_nb = 1,
2967 .use_dma = true,
2968};
2969
2970static const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps = {
2971 .max_cs_nb = 4,
2972 .max_rb_nb = 2,
2973 .need_system_controller = true,
2974 .legacy_of_bindings = true,
2975 .is_nfcv2 = true,
2976};
2977
2978static const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps = {
2979 .max_cs_nb = 4,
2980 .max_rb_nb = 2,
2981 .legacy_of_bindings = true,
2982 .is_nfcv2 = true,
2983};
2984
2985static const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps = {
2986 .max_cs_nb = 2,
2987 .max_rb_nb = 1,
2988 .legacy_of_bindings = true,
2989 .use_dma = true,
2990};
2991
2992static const struct platform_device_id marvell_nfc_platform_ids[] = {
2993 {
2994 .name = "pxa3xx-nand",
2995 .driver_data = (kernel_ulong_t)&marvell_pxa3xx_nfc_legacy_caps,
2996 },
2997 { /* sentinel */ },
2998};
2999MODULE_DEVICE_TABLE(platform, marvell_nfc_platform_ids);
3000
3001static const struct of_device_id marvell_nfc_of_ids[] = {
3002 {
3003 .compatible = "marvell,armada-8k-nand-controller",
3004 .data = &marvell_armada_8k_nfc_caps,
3005 },
3006 {
3007 .compatible = "marvell,armada370-nand-controller",
3008 .data = &marvell_armada370_nfc_caps,
3009 },
3010 {
3011 .compatible = "marvell,pxa3xx-nand-controller",
3012 .data = &marvell_pxa3xx_nfc_caps,
3013 },
3014 /* Support for old/deprecated bindings: */
3015 {
3016 .compatible = "marvell,armada-8k-nand",
3017 .data = &marvell_armada_8k_nfc_legacy_caps,
3018 },
3019 {
3020 .compatible = "marvell,armada370-nand",
3021 .data = &marvell_armada370_nfc_legacy_caps,
3022 },
3023 {
3024 .compatible = "marvell,pxa3xx-nand",
3025 .data = &marvell_pxa3xx_nfc_legacy_caps,
3026 },
3027 { /* sentinel */ },
3028};
3029MODULE_DEVICE_TABLE(of, marvell_nfc_of_ids);
3030
3031static struct platform_driver marvell_nfc_driver = {
3032 .driver = {
3033 .name = "marvell-nfc",
3034 .of_match_table = marvell_nfc_of_ids,
bd9c3f9b 3035 .pm = &marvell_nfc_pm_ops,
02f26ecf
MR
3036 },
3037 .id_table = marvell_nfc_platform_ids,
3038 .probe = marvell_nfc_probe,
3039 .remove = marvell_nfc_remove,
3040};
3041module_platform_driver(marvell_nfc_driver);
3042
3043MODULE_LICENSE("GPL");
3044MODULE_DESCRIPTION("Marvell NAND controller driver");