blob: a81018f3a2f4d5701b7887b1c4171e4e4215bef9 [file] [log] [blame]
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001// 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 *
Miquel Raynal33c1c5f2018-08-05 16:52:56 +02008 *
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.
Miquel Raynal02f26ec2018-01-09 11:36:33 +010075 */
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
Boris Brezillon6b6de652018-03-26 11:53:01 +0200377 * @core_clk: Core clock
Gregory CLEMENT961ba152018-03-13 11:30:16 +0100378 * @reg_clk: Regiters clock
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100379 * @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 {
Miquel Raynal7da45132018-07-17 09:08:02 +0200388 struct nand_controller controller;
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100389 struct device *dev;
390 void __iomem *regs;
Boris Brezillon6b6de652018-03-26 11:53:01 +0200391 struct clk *core_clk;
Gregory CLEMENT961ba152018-03-13 11:30:16 +0100392 struct clk *reg_clk;
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100393 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
Miquel Raynal7da45132018-07-17 09:08:02 +0200405static inline struct marvell_nfc *to_marvell_nfc(struct nand_controller *ctrl)
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100406{
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))
Miquel Raynal07ad5a72018-01-17 00:19:34 +0100451#define TO_CYCLES64(ps, period_ns) (DIV_ROUND_UP_ULL(div_u64(ps, 1000), \
452 period_ns))
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100453
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) {
Colin Ian Kinga76497d2018-01-19 07:55:31 +0000591 dev_err(nfc->dev, "Last operation did not succeed\n");
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100592 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
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100720 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
Boris Brezillonb9761682018-09-06 14:05:20 +02001029static int marvell_nfc_hw_ecc_hmg_read_page_raw(struct nand_chip *chip, u8 *buf,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001030 int oob_required, int page)
1031{
1032 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
1033 true, page);
1034}
1035
Boris Brezillonb9761682018-09-06 14:05:20 +02001036static int marvell_nfc_hw_ecc_hmg_read_page(struct nand_chip *chip, u8 *buf,
1037 int oob_required, int page)
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001038{
1039 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1040 unsigned int full_sz = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1041 int max_bitflips = 0, ret;
1042 u8 *raw_buf;
1043
1044 marvell_nfc_enable_hw_ecc(chip);
1045 marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, false,
1046 page);
1047 ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
1048 marvell_nfc_disable_hw_ecc(chip);
1049
1050 if (!ret)
1051 return max_bitflips;
1052
1053 /*
1054 * When ECC failures are detected, check if the full page has been
1055 * written or not. Ignore the failure if it is actually empty.
1056 */
1057 raw_buf = kmalloc(full_sz, GFP_KERNEL);
1058 if (!raw_buf)
1059 return -ENOMEM;
1060
1061 marvell_nfc_hw_ecc_hmg_do_read_page(chip, raw_buf, raw_buf +
1062 lt->data_bytes, true, page);
1063 marvell_nfc_check_empty_chunk(chip, raw_buf, full_sz, NULL, 0, NULL, 0,
1064 &max_bitflips);
1065 kfree(raw_buf);
1066
1067 return max_bitflips;
1068}
1069
1070/*
1071 * Spare area in Hamming layouts is not protected by the ECC engine (even if
1072 * it appears before the ECC bytes when reading), the ->read_oob_raw() function
1073 * also stands for ->read_oob().
1074 */
Boris Brezillonb9761682018-09-06 14:05:20 +02001075static int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct nand_chip *chip, int page)
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001076{
1077 /* Invalidate page cache */
1078 chip->pagebuf = -1;
1079
1080 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, chip->data_buf,
1081 chip->oob_poi, true, page);
1082}
1083
1084/* Hamming write helpers */
1085static int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip *chip,
1086 const u8 *data_buf,
1087 const u8 *oob_buf, bool raw,
1088 int page)
1089{
1090 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1091 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1092 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1093 struct marvell_nfc_op nfc_op = {
1094 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) |
1095 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1096 NDCB0_CMD1(NAND_CMD_SEQIN) |
1097 NDCB0_CMD2(NAND_CMD_PAGEPROG) |
1098 NDCB0_DBC,
1099 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
1100 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
1101 };
1102 unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
1103 int ret;
1104
1105 /* NFCv2 needs more information about the operation being executed */
1106 if (nfc->caps->is_nfcv2)
1107 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
1108
1109 ret = marvell_nfc_prepare_cmd(chip);
1110 if (ret)
1111 return ret;
1112
1113 marvell_nfc_send_cmd(chip, &nfc_op);
1114 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1115 "WRDREQ while loading FIFO (data)");
1116 if (ret)
1117 return ret;
1118
1119 /* Write the page then the OOB area */
1120 if (nfc->use_dma) {
1121 memcpy(nfc->dma_buf, data_buf, lt->data_bytes);
1122 memcpy(nfc->dma_buf + lt->data_bytes, oob_buf, oob_bytes);
1123 marvell_nfc_xfer_data_dma(nfc, DMA_TO_DEVICE, lt->data_bytes +
1124 lt->ecc_bytes + lt->spare_bytes);
1125 } else {
1126 marvell_nfc_xfer_data_out_pio(nfc, data_buf, lt->data_bytes);
1127 marvell_nfc_xfer_data_out_pio(nfc, oob_buf, oob_bytes);
1128 }
1129
1130 ret = marvell_nfc_wait_cmdd(chip);
1131 if (ret)
1132 return ret;
1133
1134 ret = marvell_nfc_wait_op(chip,
Chris Packhamb76401f2018-05-03 14:21:28 +12001135 PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max));
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001136 return ret;
1137}
1138
1139static int marvell_nfc_hw_ecc_hmg_write_page_raw(struct mtd_info *mtd,
1140 struct nand_chip *chip,
1141 const u8 *buf,
1142 int oob_required, int page)
1143{
1144 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1145 true, page);
1146}
1147
1148static int marvell_nfc_hw_ecc_hmg_write_page(struct mtd_info *mtd,
1149 struct nand_chip *chip,
1150 const u8 *buf,
1151 int oob_required, int page)
1152{
1153 int ret;
1154
1155 marvell_nfc_enable_hw_ecc(chip);
1156 ret = marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1157 false, page);
1158 marvell_nfc_disable_hw_ecc(chip);
1159
1160 return ret;
1161}
1162
1163/*
1164 * Spare area in Hamming layouts is not protected by the ECC engine (even if
1165 * it appears before the ECC bytes when reading), the ->write_oob_raw() function
1166 * also stands for ->write_oob().
1167 */
1168static int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct mtd_info *mtd,
1169 struct nand_chip *chip,
1170 int page)
1171{
1172 /* Invalidate page cache */
1173 chip->pagebuf = -1;
1174
1175 memset(chip->data_buf, 0xFF, mtd->writesize);
1176
1177 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, chip->data_buf,
1178 chip->oob_poi, true, page);
1179}
1180
1181/* BCH read helpers */
Boris Brezillonb9761682018-09-06 14:05:20 +02001182static int marvell_nfc_hw_ecc_bch_read_page_raw(struct nand_chip *chip, u8 *buf,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001183 int oob_required, int page)
1184{
Boris Brezillonb9761682018-09-06 14:05:20 +02001185 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001186 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1187 u8 *oob = chip->oob_poi;
1188 int chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1189 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1190 lt->last_spare_bytes;
1191 int data_len = lt->data_bytes;
1192 int spare_len = lt->spare_bytes;
1193 int ecc_len = lt->ecc_bytes;
1194 int chunk;
1195
1196 if (oob_required)
1197 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1198
1199 nand_read_page_op(chip, page, 0, NULL, 0);
1200
1201 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1202 /* Update last chunk length */
1203 if (chunk >= lt->full_chunk_cnt) {
1204 data_len = lt->last_data_bytes;
1205 spare_len = lt->last_spare_bytes;
1206 ecc_len = lt->last_ecc_bytes;
1207 }
1208
1209 /* Read data bytes*/
1210 nand_change_read_column_op(chip, chunk * chunk_size,
1211 buf + (lt->data_bytes * chunk),
1212 data_len, false);
1213
1214 /* Read spare bytes */
1215 nand_read_data_op(chip, oob + (lt->spare_bytes * chunk),
1216 spare_len, false);
1217
1218 /* Read ECC bytes */
1219 nand_read_data_op(chip, oob + ecc_offset +
1220 (ALIGN(lt->ecc_bytes, 32) * chunk),
1221 ecc_len, false);
1222 }
1223
1224 return 0;
1225}
1226
1227static void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip *chip, int chunk,
1228 u8 *data, unsigned int data_len,
1229 u8 *spare, unsigned int spare_len,
1230 int page)
1231{
1232 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1233 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1234 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1235 int i, ret;
1236 struct marvell_nfc_op nfc_op = {
1237 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
1238 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1239 NDCB0_LEN_OVRD,
1240 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
1241 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
1242 .ndcb[3] = data_len + spare_len,
1243 };
1244
1245 ret = marvell_nfc_prepare_cmd(chip);
1246 if (ret)
1247 return;
1248
1249 if (chunk == 0)
1250 nfc_op.ndcb[0] |= NDCB0_DBC |
1251 NDCB0_CMD1(NAND_CMD_READ0) |
1252 NDCB0_CMD2(NAND_CMD_READSTART);
1253
1254 /*
Boris Brezillon90d61762018-05-09 09:13:58 +02001255 * Trigger the monolithic read on the first chunk, then naked read on
1256 * intermediate chunks and finally a last naked read on the last chunk.
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001257 */
Boris Brezillon90d61762018-05-09 09:13:58 +02001258 if (chunk == 0)
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001259 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
Boris Brezillon90d61762018-05-09 09:13:58 +02001260 else if (chunk < lt->nchunks - 1)
1261 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001262 else
1263 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1264
1265 marvell_nfc_send_cmd(chip, &nfc_op);
1266
1267 /*
1268 * According to the datasheet, when reading from NDDB
1269 * with BCH enabled, after each 32 bytes reads, we
1270 * have to make sure that the NDSR.RDDREQ bit is set.
1271 *
1272 * Drain the FIFO, 8 32-bit reads at a time, and skip
1273 * the polling on the last read.
1274 *
1275 * Length is a multiple of 32 bytes, hence it is a multiple of 8 too.
1276 */
1277 for (i = 0; i < data_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1278 marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1279 "RDDREQ while draining FIFO (data)");
1280 marvell_nfc_xfer_data_in_pio(nfc, data,
1281 FIFO_DEPTH * BCH_SEQ_READS);
1282 data += FIFO_DEPTH * BCH_SEQ_READS;
1283 }
1284
1285 for (i = 0; i < spare_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1286 marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1287 "RDDREQ while draining FIFO (OOB)");
1288 marvell_nfc_xfer_data_in_pio(nfc, spare,
1289 FIFO_DEPTH * BCH_SEQ_READS);
1290 spare += FIFO_DEPTH * BCH_SEQ_READS;
1291 }
1292}
1293
Boris Brezillonb9761682018-09-06 14:05:20 +02001294static int marvell_nfc_hw_ecc_bch_read_page(struct nand_chip *chip,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001295 u8 *buf, int oob_required,
1296 int page)
1297{
Boris Brezillonb9761682018-09-06 14:05:20 +02001298 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001299 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1300 int data_len = lt->data_bytes, spare_len = lt->spare_bytes, ecc_len;
1301 u8 *data = buf, *spare = chip->oob_poi, *ecc;
1302 int max_bitflips = 0;
1303 u32 failure_mask = 0;
1304 int chunk, ecc_offset_in_page, ret;
1305
1306 /*
1307 * With BCH, OOB is not fully used (and thus not read entirely), not
1308 * expected bytes could show up at the end of the OOB buffer if not
1309 * explicitly erased.
1310 */
1311 if (oob_required)
1312 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1313
1314 marvell_nfc_enable_hw_ecc(chip);
1315
1316 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1317 /* Update length for the last chunk */
1318 if (chunk >= lt->full_chunk_cnt) {
1319 data_len = lt->last_data_bytes;
1320 spare_len = lt->last_spare_bytes;
1321 }
1322
1323 /* Read the chunk and detect number of bitflips */
1324 marvell_nfc_hw_ecc_bch_read_chunk(chip, chunk, data, data_len,
1325 spare, spare_len, page);
1326 ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
1327 if (ret)
1328 failure_mask |= BIT(chunk);
1329
1330 data += data_len;
1331 spare += spare_len;
1332 }
1333
1334 marvell_nfc_disable_hw_ecc(chip);
1335
1336 if (!failure_mask)
1337 return max_bitflips;
1338
1339 /*
1340 * Please note that dumping the ECC bytes during a normal read with OOB
1341 * area would add a significant overhead as ECC bytes are "consumed" by
1342 * the controller in normal mode and must be re-read in raw mode. To
1343 * avoid dropping the performances, we prefer not to include them. The
1344 * user should re-read the page in raw mode if ECC bytes are required.
1345 *
1346 * However, for any subpage read error reported by ->correct(), the ECC
1347 * bytes must be read in raw mode and the full subpage must be checked
1348 * to see if it is entirely empty of if there was an actual error.
1349 */
1350 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1351 /* No failure reported for this chunk, move to the next one */
1352 if (!(failure_mask & BIT(chunk)))
1353 continue;
1354
1355 /* Derive ECC bytes positions (in page/buffer) and length */
1356 ecc = chip->oob_poi +
1357 (lt->full_chunk_cnt * lt->spare_bytes) +
1358 lt->last_spare_bytes +
1359 (chunk * ALIGN(lt->ecc_bytes, 32));
1360 ecc_offset_in_page =
1361 (chunk * (lt->data_bytes + lt->spare_bytes +
1362 lt->ecc_bytes)) +
1363 (chunk < lt->full_chunk_cnt ?
1364 lt->data_bytes + lt->spare_bytes :
1365 lt->last_data_bytes + lt->last_spare_bytes);
1366 ecc_len = chunk < lt->full_chunk_cnt ?
1367 lt->ecc_bytes : lt->last_ecc_bytes;
1368
1369 /* Do the actual raw read of the ECC bytes */
1370 nand_change_read_column_op(chip, ecc_offset_in_page,
1371 ecc, ecc_len, false);
1372
1373 /* Derive data/spare bytes positions (in buffer) and length */
1374 data = buf + (chunk * lt->data_bytes);
1375 data_len = chunk < lt->full_chunk_cnt ?
1376 lt->data_bytes : lt->last_data_bytes;
1377 spare = chip->oob_poi + (chunk * (lt->spare_bytes +
1378 lt->ecc_bytes));
1379 spare_len = chunk < lt->full_chunk_cnt ?
1380 lt->spare_bytes : lt->last_spare_bytes;
1381
1382 /* Check the entire chunk (data + spare + ecc) for emptyness */
1383 marvell_nfc_check_empty_chunk(chip, data, data_len, spare,
1384 spare_len, ecc, ecc_len,
1385 &max_bitflips);
1386 }
1387
1388 return max_bitflips;
1389}
1390
Boris Brezillonb9761682018-09-06 14:05:20 +02001391static int marvell_nfc_hw_ecc_bch_read_oob_raw(struct nand_chip *chip, int page)
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001392{
1393 /* Invalidate page cache */
1394 chip->pagebuf = -1;
1395
Boris Brezillonb9761682018-09-06 14:05:20 +02001396 return chip->ecc.read_page_raw(chip, chip->data_buf, true, page);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001397}
1398
Boris Brezillonb9761682018-09-06 14:05:20 +02001399static int marvell_nfc_hw_ecc_bch_read_oob(struct nand_chip *chip, int page)
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001400{
1401 /* Invalidate page cache */
1402 chip->pagebuf = -1;
1403
Boris Brezillonb9761682018-09-06 14:05:20 +02001404 return chip->ecc.read_page(chip, chip->data_buf, true, page);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001405}
1406
1407/* BCH write helpers */
1408static int marvell_nfc_hw_ecc_bch_write_page_raw(struct mtd_info *mtd,
1409 struct nand_chip *chip,
1410 const u8 *buf,
1411 int oob_required, int page)
1412{
1413 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1414 int full_chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1415 int data_len = lt->data_bytes;
1416 int spare_len = lt->spare_bytes;
1417 int ecc_len = lt->ecc_bytes;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001418 int spare_offset = 0;
1419 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1420 lt->last_spare_bytes;
1421 int chunk;
1422
1423 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1424
1425 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1426 if (chunk >= lt->full_chunk_cnt) {
1427 data_len = lt->last_data_bytes;
1428 spare_len = lt->last_spare_bytes;
1429 ecc_len = lt->last_ecc_bytes;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001430 }
1431
1432 /* Point to the column of the next chunk */
1433 nand_change_write_column_op(chip, chunk * full_chunk_size,
1434 NULL, 0, false);
1435
1436 /* Write the data */
1437 nand_write_data_op(chip, buf + (chunk * lt->data_bytes),
1438 data_len, false);
1439
1440 if (!oob_required)
1441 continue;
1442
1443 /* Write the spare bytes */
1444 if (spare_len)
1445 nand_write_data_op(chip, chip->oob_poi + spare_offset,
1446 spare_len, false);
1447
1448 /* Write the ECC bytes */
1449 if (ecc_len)
1450 nand_write_data_op(chip, chip->oob_poi + ecc_offset,
1451 ecc_len, false);
1452
1453 spare_offset += spare_len;
1454 ecc_offset += ALIGN(ecc_len, 32);
1455 }
1456
1457 return nand_prog_page_end_op(chip);
1458}
1459
1460static int
1461marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip *chip, int chunk,
1462 const u8 *data, unsigned int data_len,
1463 const u8 *spare, unsigned int spare_len,
1464 int page)
1465{
1466 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1467 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1468 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
Miquel Raynala2ee41f2018-05-03 12:00:27 +02001469 u32 xtype;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001470 int ret;
1471 struct marvell_nfc_op nfc_op = {
1472 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | NDCB0_LEN_OVRD,
1473 .ndcb[3] = data_len + spare_len,
1474 };
1475
1476 /*
1477 * First operation dispatches the CMD_SEQIN command, issue the address
1478 * cycles and asks for the first chunk of data.
1479 * All operations in the middle (if any) will issue a naked write and
1480 * also ask for data.
1481 * Last operation (if any) asks for the last chunk of data through a
1482 * last naked write.
1483 */
1484 if (chunk == 0) {
Miquel Raynala2ee41f2018-05-03 12:00:27 +02001485 if (lt->nchunks == 1)
1486 xtype = XTYPE_MONOLITHIC_RW;
1487 else
1488 xtype = XTYPE_WRITE_DISPATCH;
1489
1490 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(xtype) |
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001491 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1492 NDCB0_CMD1(NAND_CMD_SEQIN);
1493 nfc_op.ndcb[1] |= NDCB1_ADDRS_PAGE(page);
1494 nfc_op.ndcb[2] |= NDCB2_ADDR5_PAGE(page);
1495 } else if (chunk < lt->nchunks - 1) {
1496 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
1497 } else {
1498 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1499 }
1500
1501 /* Always dispatch the PAGEPROG command on the last chunk */
1502 if (chunk == lt->nchunks - 1)
1503 nfc_op.ndcb[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG) | NDCB0_DBC;
1504
1505 ret = marvell_nfc_prepare_cmd(chip);
1506 if (ret)
1507 return ret;
1508
1509 marvell_nfc_send_cmd(chip, &nfc_op);
1510 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1511 "WRDREQ while loading FIFO (data)");
1512 if (ret)
1513 return ret;
1514
1515 /* Transfer the contents */
1516 iowrite32_rep(nfc->regs + NDDB, data, FIFO_REP(data_len));
1517 iowrite32_rep(nfc->regs + NDDB, spare, FIFO_REP(spare_len));
1518
1519 return 0;
1520}
1521
1522static int marvell_nfc_hw_ecc_bch_write_page(struct mtd_info *mtd,
1523 struct nand_chip *chip,
1524 const u8 *buf,
1525 int oob_required, int page)
1526{
1527 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1528 const u8 *data = buf;
1529 const u8 *spare = chip->oob_poi;
1530 int data_len = lt->data_bytes;
1531 int spare_len = lt->spare_bytes;
1532 int chunk, ret;
1533
1534 /* Spare data will be written anyway, so clear it to avoid garbage */
1535 if (!oob_required)
1536 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1537
1538 marvell_nfc_enable_hw_ecc(chip);
1539
1540 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1541 if (chunk >= lt->full_chunk_cnt) {
1542 data_len = lt->last_data_bytes;
1543 spare_len = lt->last_spare_bytes;
1544 }
1545
1546 marvell_nfc_hw_ecc_bch_write_chunk(chip, chunk, data, data_len,
1547 spare, spare_len, page);
1548 data += data_len;
1549 spare += spare_len;
1550
1551 /*
1552 * Waiting only for CMDD or PAGED is not enough, ECC are
1553 * partially written. No flag is set once the operation is
1554 * really finished but the ND_RUN bit is cleared, so wait for it
1555 * before stepping into the next command.
1556 */
1557 marvell_nfc_wait_ndrun(chip);
1558 }
1559
1560 ret = marvell_nfc_wait_op(chip,
Chris Packhamb76401f2018-05-03 14:21:28 +12001561 PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max));
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001562
1563 marvell_nfc_disable_hw_ecc(chip);
1564
1565 if (ret)
1566 return ret;
1567
1568 return 0;
1569}
1570
1571static int marvell_nfc_hw_ecc_bch_write_oob_raw(struct mtd_info *mtd,
1572 struct nand_chip *chip,
1573 int page)
1574{
1575 /* Invalidate page cache */
1576 chip->pagebuf = -1;
1577
1578 memset(chip->data_buf, 0xFF, mtd->writesize);
1579
1580 return chip->ecc.write_page_raw(mtd, chip, chip->data_buf, true, page);
1581}
1582
1583static int marvell_nfc_hw_ecc_bch_write_oob(struct mtd_info *mtd,
1584 struct nand_chip *chip, int page)
1585{
1586 /* Invalidate page cache */
1587 chip->pagebuf = -1;
1588
1589 memset(chip->data_buf, 0xFF, mtd->writesize);
1590
1591 return chip->ecc.write_page(mtd, chip, chip->data_buf, true, page);
1592}
1593
1594/* NAND framework ->exec_op() hooks and related helpers */
1595static void marvell_nfc_parse_instructions(struct nand_chip *chip,
1596 const struct nand_subop *subop,
1597 struct marvell_nfc_op *nfc_op)
1598{
1599 const struct nand_op_instr *instr = NULL;
1600 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1601 bool first_cmd = true;
1602 unsigned int op_id;
1603 int i;
1604
1605 /* Reset the input structure as most of its fields will be OR'ed */
1606 memset(nfc_op, 0, sizeof(struct marvell_nfc_op));
1607
1608 for (op_id = 0; op_id < subop->ninstrs; op_id++) {
1609 unsigned int offset, naddrs;
1610 const u8 *addrs;
1611 int len = nand_subop_get_data_len(subop, op_id);
1612
1613 instr = &subop->instrs[op_id];
1614
1615 switch (instr->type) {
1616 case NAND_OP_CMD_INSTR:
1617 if (first_cmd)
1618 nfc_op->ndcb[0] |=
1619 NDCB0_CMD1(instr->ctx.cmd.opcode);
1620 else
1621 nfc_op->ndcb[0] |=
1622 NDCB0_CMD2(instr->ctx.cmd.opcode) |
1623 NDCB0_DBC;
1624
1625 nfc_op->cle_ale_delay_ns = instr->delay_ns;
1626 first_cmd = false;
1627 break;
1628
1629 case NAND_OP_ADDR_INSTR:
1630 offset = nand_subop_get_addr_start_off(subop, op_id);
1631 naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
1632 addrs = &instr->ctx.addr.addrs[offset];
1633
1634 nfc_op->ndcb[0] |= NDCB0_ADDR_CYC(naddrs);
1635
1636 for (i = 0; i < min_t(unsigned int, 4, naddrs); i++)
1637 nfc_op->ndcb[1] |= addrs[i] << (8 * i);
1638
1639 if (naddrs >= 5)
1640 nfc_op->ndcb[2] |= NDCB2_ADDR5_CYC(addrs[4]);
1641 if (naddrs >= 6)
1642 nfc_op->ndcb[3] |= NDCB3_ADDR6_CYC(addrs[5]);
1643 if (naddrs == 7)
1644 nfc_op->ndcb[3] |= NDCB3_ADDR7_CYC(addrs[6]);
1645
1646 nfc_op->cle_ale_delay_ns = instr->delay_ns;
1647 break;
1648
1649 case NAND_OP_DATA_IN_INSTR:
1650 nfc_op->data_instr = instr;
1651 nfc_op->data_instr_idx = op_id;
1652 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ);
1653 if (nfc->caps->is_nfcv2) {
1654 nfc_op->ndcb[0] |=
1655 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1656 NDCB0_LEN_OVRD;
1657 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1658 }
1659 nfc_op->data_delay_ns = instr->delay_ns;
1660 break;
1661
1662 case NAND_OP_DATA_OUT_INSTR:
1663 nfc_op->data_instr = instr;
1664 nfc_op->data_instr_idx = op_id;
1665 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE);
1666 if (nfc->caps->is_nfcv2) {
1667 nfc_op->ndcb[0] |=
1668 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1669 NDCB0_LEN_OVRD;
1670 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1671 }
1672 nfc_op->data_delay_ns = instr->delay_ns;
1673 break;
1674
1675 case NAND_OP_WAITRDY_INSTR:
1676 nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
1677 nfc_op->rdy_delay_ns = instr->delay_ns;
1678 break;
1679 }
1680 }
1681}
1682
1683static int marvell_nfc_xfer_data_pio(struct nand_chip *chip,
1684 const struct nand_subop *subop,
1685 struct marvell_nfc_op *nfc_op)
1686{
1687 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1688 const struct nand_op_instr *instr = nfc_op->data_instr;
1689 unsigned int op_id = nfc_op->data_instr_idx;
1690 unsigned int len = nand_subop_get_data_len(subop, op_id);
1691 unsigned int offset = nand_subop_get_data_start_off(subop, op_id);
1692 bool reading = (instr->type == NAND_OP_DATA_IN_INSTR);
1693 int ret;
1694
1695 if (instr->ctx.data.force_8bit)
1696 marvell_nfc_force_byte_access(chip, true);
1697
1698 if (reading) {
1699 u8 *in = instr->ctx.data.buf.in + offset;
1700
1701 ret = marvell_nfc_xfer_data_in_pio(nfc, in, len);
1702 } else {
1703 const u8 *out = instr->ctx.data.buf.out + offset;
1704
1705 ret = marvell_nfc_xfer_data_out_pio(nfc, out, len);
1706 }
1707
1708 if (instr->ctx.data.force_8bit)
1709 marvell_nfc_force_byte_access(chip, false);
1710
1711 return ret;
1712}
1713
1714static int marvell_nfc_monolithic_access_exec(struct nand_chip *chip,
1715 const struct nand_subop *subop)
1716{
1717 struct marvell_nfc_op nfc_op;
1718 bool reading;
1719 int ret;
1720
1721 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1722 reading = (nfc_op.data_instr->type == NAND_OP_DATA_IN_INSTR);
1723
1724 ret = marvell_nfc_prepare_cmd(chip);
1725 if (ret)
1726 return ret;
1727
1728 marvell_nfc_send_cmd(chip, &nfc_op);
1729 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1730 "RDDREQ/WRDREQ while draining raw data");
1731 if (ret)
1732 return ret;
1733
1734 cond_delay(nfc_op.cle_ale_delay_ns);
1735
1736 if (reading) {
1737 if (nfc_op.rdy_timeout_ms) {
1738 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1739 if (ret)
1740 return ret;
1741 }
1742
1743 cond_delay(nfc_op.rdy_delay_ns);
1744 }
1745
1746 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1747 ret = marvell_nfc_wait_cmdd(chip);
1748 if (ret)
1749 return ret;
1750
1751 cond_delay(nfc_op.data_delay_ns);
1752
1753 if (!reading) {
1754 if (nfc_op.rdy_timeout_ms) {
1755 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1756 if (ret)
1757 return ret;
1758 }
1759
1760 cond_delay(nfc_op.rdy_delay_ns);
1761 }
1762
1763 /*
1764 * NDCR ND_RUN bit should be cleared automatically at the end of each
1765 * operation but experience shows that the behavior is buggy when it
1766 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1767 */
1768 if (!reading) {
1769 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1770
1771 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1772 nfc->regs + NDCR);
1773 }
1774
1775 return 0;
1776}
1777
1778static int marvell_nfc_naked_access_exec(struct nand_chip *chip,
1779 const struct nand_subop *subop)
1780{
1781 struct marvell_nfc_op nfc_op;
1782 int ret;
1783
1784 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1785
1786 /*
1787 * Naked access are different in that they need to be flagged as naked
1788 * by the controller. Reset the controller registers fields that inform
1789 * on the type and refill them according to the ongoing operation.
1790 */
1791 nfc_op.ndcb[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK) |
1792 NDCB0_CMD_XTYPE(XTYPE_MASK));
1793 switch (subop->instrs[0].type) {
1794 case NAND_OP_CMD_INSTR:
1795 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD);
1796 break;
1797 case NAND_OP_ADDR_INSTR:
1798 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR);
1799 break;
1800 case NAND_OP_DATA_IN_INSTR:
1801 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ) |
1802 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1803 break;
1804 case NAND_OP_DATA_OUT_INSTR:
1805 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE) |
1806 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1807 break;
1808 default:
1809 /* This should never happen */
1810 break;
1811 }
1812
1813 ret = marvell_nfc_prepare_cmd(chip);
1814 if (ret)
1815 return ret;
1816
1817 marvell_nfc_send_cmd(chip, &nfc_op);
1818
1819 if (!nfc_op.data_instr) {
1820 ret = marvell_nfc_wait_cmdd(chip);
1821 cond_delay(nfc_op.cle_ale_delay_ns);
1822 return ret;
1823 }
1824
1825 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1826 "RDDREQ/WRDREQ while draining raw data");
1827 if (ret)
1828 return ret;
1829
1830 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1831 ret = marvell_nfc_wait_cmdd(chip);
1832 if (ret)
1833 return ret;
1834
1835 /*
1836 * NDCR ND_RUN bit should be cleared automatically at the end of each
1837 * operation but experience shows that the behavior is buggy when it
1838 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1839 */
1840 if (subop->instrs[0].type == NAND_OP_DATA_OUT_INSTR) {
1841 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1842
1843 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1844 nfc->regs + NDCR);
1845 }
1846
1847 return 0;
1848}
1849
1850static int marvell_nfc_naked_waitrdy_exec(struct nand_chip *chip,
1851 const struct nand_subop *subop)
1852{
1853 struct marvell_nfc_op nfc_op;
1854 int ret;
1855
1856 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1857
1858 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1859 cond_delay(nfc_op.rdy_delay_ns);
1860
1861 return ret;
1862}
1863
1864static int marvell_nfc_read_id_type_exec(struct nand_chip *chip,
1865 const struct nand_subop *subop)
1866{
1867 struct marvell_nfc_op nfc_op;
1868 int ret;
1869
1870 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1871 nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1872 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ_ID);
1873
1874 ret = marvell_nfc_prepare_cmd(chip);
1875 if (ret)
1876 return ret;
1877
1878 marvell_nfc_send_cmd(chip, &nfc_op);
1879 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1880 "RDDREQ while reading ID");
1881 if (ret)
1882 return ret;
1883
1884 cond_delay(nfc_op.cle_ale_delay_ns);
1885
1886 if (nfc_op.rdy_timeout_ms) {
1887 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1888 if (ret)
1889 return ret;
1890 }
1891
1892 cond_delay(nfc_op.rdy_delay_ns);
1893
1894 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1895 ret = marvell_nfc_wait_cmdd(chip);
1896 if (ret)
1897 return ret;
1898
1899 cond_delay(nfc_op.data_delay_ns);
1900
1901 return 0;
1902}
1903
1904static int marvell_nfc_read_status_exec(struct nand_chip *chip,
1905 const struct nand_subop *subop)
1906{
1907 struct marvell_nfc_op nfc_op;
1908 int ret;
1909
1910 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1911 nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1912 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_STATUS);
1913
1914 ret = marvell_nfc_prepare_cmd(chip);
1915 if (ret)
1916 return ret;
1917
1918 marvell_nfc_send_cmd(chip, &nfc_op);
1919 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1920 "RDDREQ while reading status");
1921 if (ret)
1922 return ret;
1923
1924 cond_delay(nfc_op.cle_ale_delay_ns);
1925
1926 if (nfc_op.rdy_timeout_ms) {
1927 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1928 if (ret)
1929 return ret;
1930 }
1931
1932 cond_delay(nfc_op.rdy_delay_ns);
1933
1934 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1935 ret = marvell_nfc_wait_cmdd(chip);
1936 if (ret)
1937 return ret;
1938
1939 cond_delay(nfc_op.data_delay_ns);
1940
1941 return 0;
1942}
1943
1944static int marvell_nfc_reset_cmd_type_exec(struct nand_chip *chip,
1945 const struct nand_subop *subop)
1946{
1947 struct marvell_nfc_op nfc_op;
1948 int ret;
1949
1950 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1951 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_RESET);
1952
1953 ret = marvell_nfc_prepare_cmd(chip);
1954 if (ret)
1955 return ret;
1956
1957 marvell_nfc_send_cmd(chip, &nfc_op);
1958 ret = marvell_nfc_wait_cmdd(chip);
1959 if (ret)
1960 return ret;
1961
1962 cond_delay(nfc_op.cle_ale_delay_ns);
1963
1964 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1965 if (ret)
1966 return ret;
1967
1968 cond_delay(nfc_op.rdy_delay_ns);
1969
1970 return 0;
1971}
1972
1973static int marvell_nfc_erase_cmd_type_exec(struct nand_chip *chip,
1974 const struct nand_subop *subop)
1975{
1976 struct marvell_nfc_op nfc_op;
1977 int ret;
1978
1979 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1980 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_ERASE);
1981
1982 ret = marvell_nfc_prepare_cmd(chip);
1983 if (ret)
1984 return ret;
1985
1986 marvell_nfc_send_cmd(chip, &nfc_op);
1987 ret = marvell_nfc_wait_cmdd(chip);
1988 if (ret)
1989 return ret;
1990
1991 cond_delay(nfc_op.cle_ale_delay_ns);
1992
1993 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1994 if (ret)
1995 return ret;
1996
1997 cond_delay(nfc_op.rdy_delay_ns);
1998
1999 return 0;
2000}
2001
2002static const struct nand_op_parser marvell_nfcv2_op_parser = NAND_OP_PARSER(
2003 /* Monolithic reads/writes */
2004 NAND_OP_PARSER_PATTERN(
2005 marvell_nfc_monolithic_access_exec,
2006 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2007 NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2),
2008 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2009 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
2010 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
2011 NAND_OP_PARSER_PATTERN(
2012 marvell_nfc_monolithic_access_exec,
2013 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2014 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2),
2015 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE),
2016 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2017 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
2018 /* Naked commands */
2019 NAND_OP_PARSER_PATTERN(
2020 marvell_nfc_naked_access_exec,
2021 NAND_OP_PARSER_PAT_CMD_ELEM(false)),
2022 NAND_OP_PARSER_PATTERN(
2023 marvell_nfc_naked_access_exec,
2024 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2)),
2025 NAND_OP_PARSER_PATTERN(
2026 marvell_nfc_naked_access_exec,
2027 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
2028 NAND_OP_PARSER_PATTERN(
2029 marvell_nfc_naked_access_exec,
2030 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE)),
2031 NAND_OP_PARSER_PATTERN(
2032 marvell_nfc_naked_waitrdy_exec,
2033 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2034 );
2035
2036static const struct nand_op_parser marvell_nfcv1_op_parser = NAND_OP_PARSER(
2037 /* Naked commands not supported, use a function for each pattern */
2038 NAND_OP_PARSER_PATTERN(
2039 marvell_nfc_read_id_type_exec,
2040 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2041 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
2042 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 8)),
2043 NAND_OP_PARSER_PATTERN(
2044 marvell_nfc_erase_cmd_type_exec,
2045 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2046 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
2047 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2048 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2049 NAND_OP_PARSER_PATTERN(
2050 marvell_nfc_read_status_exec,
2051 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2052 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)),
2053 NAND_OP_PARSER_PATTERN(
2054 marvell_nfc_reset_cmd_type_exec,
2055 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2056 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2057 NAND_OP_PARSER_PATTERN(
2058 marvell_nfc_naked_waitrdy_exec,
2059 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2060 );
2061
2062static int marvell_nfc_exec_op(struct nand_chip *chip,
2063 const struct nand_operation *op,
2064 bool check_only)
2065{
2066 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2067
2068 if (nfc->caps->is_nfcv2)
2069 return nand_op_parser_exec_op(chip, &marvell_nfcv2_op_parser,
2070 op, check_only);
2071 else
2072 return nand_op_parser_exec_op(chip, &marvell_nfcv1_op_parser,
2073 op, check_only);
2074}
2075
2076/*
2077 * Layouts were broken in old pxa3xx_nand driver, these are supposed to be
2078 * usable.
2079 */
2080static int marvell_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2081 struct mtd_oob_region *oobregion)
2082{
2083 struct nand_chip *chip = mtd_to_nand(mtd);
2084 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2085
2086 if (section)
2087 return -ERANGE;
2088
2089 oobregion->length = (lt->full_chunk_cnt * lt->ecc_bytes) +
2090 lt->last_ecc_bytes;
2091 oobregion->offset = mtd->oobsize - oobregion->length;
2092
2093 return 0;
2094}
2095
2096static int marvell_nand_ooblayout_free(struct mtd_info *mtd, int section,
2097 struct mtd_oob_region *oobregion)
2098{
2099 struct nand_chip *chip = mtd_to_nand(mtd);
2100 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2101
2102 if (section)
2103 return -ERANGE;
2104
2105 /*
2106 * Bootrom looks in bytes 0 & 5 for bad blocks for the
2107 * 4KB page / 4bit BCH combination.
2108 */
2109 if (mtd->writesize == SZ_4K && lt->data_bytes == SZ_2K)
2110 oobregion->offset = 6;
2111 else
2112 oobregion->offset = 2;
2113
2114 oobregion->length = (lt->full_chunk_cnt * lt->spare_bytes) +
2115 lt->last_spare_bytes - oobregion->offset;
2116
2117 return 0;
2118}
2119
2120static const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops = {
2121 .ecc = marvell_nand_ooblayout_ecc,
2122 .free = marvell_nand_ooblayout_free,
2123};
2124
2125static int marvell_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
2126 struct nand_ecc_ctrl *ecc)
2127{
2128 struct nand_chip *chip = mtd_to_nand(mtd);
2129 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2130 const struct marvell_hw_ecc_layout *l;
2131 int i;
2132
2133 if (!nfc->caps->is_nfcv2 &&
2134 (mtd->writesize + mtd->oobsize > MAX_CHUNK_SIZE)) {
2135 dev_err(nfc->dev,
2136 "NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n",
2137 mtd->writesize, MAX_CHUNK_SIZE - mtd->oobsize);
2138 return -ENOTSUPP;
2139 }
2140
2141 to_marvell_nand(chip)->layout = NULL;
2142 for (i = 0; i < ARRAY_SIZE(marvell_nfc_layouts); i++) {
2143 l = &marvell_nfc_layouts[i];
2144 if (mtd->writesize == l->writesize &&
2145 ecc->size == l->chunk && ecc->strength == l->strength) {
2146 to_marvell_nand(chip)->layout = l;
2147 break;
2148 }
2149 }
2150
2151 if (!to_marvell_nand(chip)->layout ||
2152 (!nfc->caps->is_nfcv2 && ecc->strength > 1)) {
2153 dev_err(nfc->dev,
2154 "ECC strength %d at page size %d is not supported\n",
2155 ecc->strength, mtd->writesize);
2156 return -ENOTSUPP;
2157 }
2158
2159 mtd_set_ooblayout(mtd, &marvell_nand_ooblayout_ops);
2160 ecc->steps = l->nchunks;
2161 ecc->size = l->data_bytes;
2162
2163 if (ecc->strength == 1) {
2164 chip->ecc.algo = NAND_ECC_HAMMING;
2165 ecc->read_page_raw = marvell_nfc_hw_ecc_hmg_read_page_raw;
2166 ecc->read_page = marvell_nfc_hw_ecc_hmg_read_page;
2167 ecc->read_oob_raw = marvell_nfc_hw_ecc_hmg_read_oob_raw;
2168 ecc->read_oob = ecc->read_oob_raw;
2169 ecc->write_page_raw = marvell_nfc_hw_ecc_hmg_write_page_raw;
2170 ecc->write_page = marvell_nfc_hw_ecc_hmg_write_page;
2171 ecc->write_oob_raw = marvell_nfc_hw_ecc_hmg_write_oob_raw;
2172 ecc->write_oob = ecc->write_oob_raw;
2173 } else {
2174 chip->ecc.algo = NAND_ECC_BCH;
2175 ecc->strength = 16;
2176 ecc->read_page_raw = marvell_nfc_hw_ecc_bch_read_page_raw;
2177 ecc->read_page = marvell_nfc_hw_ecc_bch_read_page;
2178 ecc->read_oob_raw = marvell_nfc_hw_ecc_bch_read_oob_raw;
2179 ecc->read_oob = marvell_nfc_hw_ecc_bch_read_oob;
2180 ecc->write_page_raw = marvell_nfc_hw_ecc_bch_write_page_raw;
2181 ecc->write_page = marvell_nfc_hw_ecc_bch_write_page;
2182 ecc->write_oob_raw = marvell_nfc_hw_ecc_bch_write_oob_raw;
2183 ecc->write_oob = marvell_nfc_hw_ecc_bch_write_oob;
2184 }
2185
2186 return 0;
2187}
2188
2189static int marvell_nand_ecc_init(struct mtd_info *mtd,
2190 struct nand_ecc_ctrl *ecc)
2191{
2192 struct nand_chip *chip = mtd_to_nand(mtd);
2193 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2194 int ret;
2195
2196 if (ecc->mode != NAND_ECC_NONE && (!ecc->size || !ecc->strength)) {
2197 if (chip->ecc_step_ds && chip->ecc_strength_ds) {
2198 ecc->size = chip->ecc_step_ds;
2199 ecc->strength = chip->ecc_strength_ds;
2200 } else {
2201 dev_info(nfc->dev,
2202 "No minimum ECC strength, using 1b/512B\n");
2203 ecc->size = 512;
2204 ecc->strength = 1;
2205 }
2206 }
2207
2208 switch (ecc->mode) {
2209 case NAND_ECC_HW:
2210 ret = marvell_nand_hw_ecc_ctrl_init(mtd, ecc);
2211 if (ret)
2212 return ret;
2213 break;
2214 case NAND_ECC_NONE:
2215 case NAND_ECC_SOFT:
Chris Packhamed6d0282018-06-25 10:44:43 +12002216 case NAND_ECC_ON_DIE:
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002217 if (!nfc->caps->is_nfcv2 && mtd->writesize != SZ_512 &&
2218 mtd->writesize != SZ_2K) {
2219 dev_err(nfc->dev, "NFCv1 cannot write %d bytes pages\n",
2220 mtd->writesize);
2221 return -EINVAL;
2222 }
2223 break;
2224 default:
2225 return -EINVAL;
2226 }
2227
2228 return 0;
2229}
2230
2231static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
2232static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
2233
2234static struct nand_bbt_descr bbt_main_descr = {
2235 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2236 NAND_BBT_2BIT | NAND_BBT_VERSION,
2237 .offs = 8,
2238 .len = 6,
2239 .veroffs = 14,
2240 .maxblocks = 8, /* Last 8 blocks in each chip */
2241 .pattern = bbt_pattern
2242};
2243
2244static struct nand_bbt_descr bbt_mirror_descr = {
2245 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2246 NAND_BBT_2BIT | NAND_BBT_VERSION,
2247 .offs = 8,
2248 .len = 6,
2249 .veroffs = 14,
2250 .maxblocks = 8, /* Last 8 blocks in each chip */
2251 .pattern = bbt_mirror_pattern
2252};
2253
2254static int marvell_nfc_setup_data_interface(struct mtd_info *mtd, int chipnr,
2255 const struct nand_data_interface
2256 *conf)
2257{
2258 struct nand_chip *chip = mtd_to_nand(mtd);
2259 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2260 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
Boris Brezillon6b6de652018-03-26 11:53:01 +02002261 unsigned int period_ns = 1000000000 / clk_get_rate(nfc->core_clk) * 2;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002262 const struct nand_sdr_timings *sdr;
2263 struct marvell_nfc_timings nfc_tmg;
2264 int read_delay;
2265
2266 sdr = nand_get_sdr_timings(conf);
2267 if (IS_ERR(sdr))
2268 return PTR_ERR(sdr);
2269
2270 /*
2271 * SDR timings are given in pico-seconds while NFC timings must be
2272 * expressed in NAND controller clock cycles, which is half of the
2273 * frequency of the accessible ECC clock retrieved by clk_get_rate().
2274 * This is not written anywhere in the datasheet but was observed
2275 * with an oscilloscope.
2276 *
2277 * NFC datasheet gives equations from which thoses calculations
2278 * are derived, they tend to be slightly more restrictives than the
2279 * given core timings and may improve the overall speed.
2280 */
2281 nfc_tmg.tRP = TO_CYCLES(DIV_ROUND_UP(sdr->tRC_min, 2), period_ns) - 1;
2282 nfc_tmg.tRH = nfc_tmg.tRP;
2283 nfc_tmg.tWP = TO_CYCLES(DIV_ROUND_UP(sdr->tWC_min, 2), period_ns) - 1;
2284 nfc_tmg.tWH = nfc_tmg.tWP;
2285 nfc_tmg.tCS = TO_CYCLES(sdr->tCS_min, period_ns);
2286 nfc_tmg.tCH = TO_CYCLES(sdr->tCH_min, period_ns) - 1;
2287 nfc_tmg.tADL = TO_CYCLES(sdr->tADL_min, period_ns);
2288 /*
2289 * Read delay is the time of propagation from SoC pins to NFC internal
2290 * logic. With non-EDO timings, this is MIN_RD_DEL_CNT clock cycles. In
2291 * EDO mode, an additional delay of tRH must be taken into account so
2292 * the data is sampled on the falling edge instead of the rising edge.
2293 */
2294 read_delay = sdr->tRC_min >= 30000 ?
2295 MIN_RD_DEL_CNT : MIN_RD_DEL_CNT + nfc_tmg.tRH;
2296
2297 nfc_tmg.tAR = TO_CYCLES(sdr->tAR_min, period_ns);
2298 /*
2299 * tWHR and tRHW are supposed to be read to write delays (and vice
2300 * versa) but in some cases, ie. when doing a change column, they must
2301 * be greater than that to be sure tCCS delay is respected.
2302 */
2303 nfc_tmg.tWHR = TO_CYCLES(max_t(int, sdr->tWHR_min, sdr->tCCS_min),
2304 period_ns) - 2,
2305 nfc_tmg.tRHW = TO_CYCLES(max_t(int, sdr->tRHW_min, sdr->tCCS_min),
2306 period_ns);
2307
Miquel Raynal07ad5a72018-01-17 00:19:34 +01002308 /*
2309 * NFCv2: Use WAIT_MODE (wait for RB line), do not rely only on delays.
2310 * NFCv1: No WAIT_MODE, tR must be maximal.
2311 */
2312 if (nfc->caps->is_nfcv2) {
2313 nfc_tmg.tR = TO_CYCLES(sdr->tWB_max, period_ns);
2314 } else {
2315 nfc_tmg.tR = TO_CYCLES64(sdr->tWB_max + sdr->tR_max,
2316 period_ns);
2317 if (nfc_tmg.tR + 3 > nfc_tmg.tCH)
2318 nfc_tmg.tR = nfc_tmg.tCH - 3;
2319 else
2320 nfc_tmg.tR = 0;
2321 }
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002322
2323 if (chipnr < 0)
2324 return 0;
2325
2326 marvell_nand->ndtr0 =
2327 NDTR0_TRP(nfc_tmg.tRP) |
2328 NDTR0_TRH(nfc_tmg.tRH) |
2329 NDTR0_ETRP(nfc_tmg.tRP) |
2330 NDTR0_TWP(nfc_tmg.tWP) |
2331 NDTR0_TWH(nfc_tmg.tWH) |
2332 NDTR0_TCS(nfc_tmg.tCS) |
Miquel Raynal07ad5a72018-01-17 00:19:34 +01002333 NDTR0_TCH(nfc_tmg.tCH);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002334
2335 marvell_nand->ndtr1 =
2336 NDTR1_TAR(nfc_tmg.tAR) |
2337 NDTR1_TWHR(nfc_tmg.tWHR) |
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002338 NDTR1_TR(nfc_tmg.tR);
2339
Miquel Raynal07ad5a72018-01-17 00:19:34 +01002340 if (nfc->caps->is_nfcv2) {
2341 marvell_nand->ndtr0 |=
2342 NDTR0_RD_CNT_DEL(read_delay) |
2343 NDTR0_SELCNTR |
2344 NDTR0_TADL(nfc_tmg.tADL);
2345
2346 marvell_nand->ndtr1 |=
2347 NDTR1_TRHW(nfc_tmg.tRHW) |
2348 NDTR1_WAIT_MODE;
2349 }
2350
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002351 return 0;
2352}
2353
Miquel Raynal8831e482018-07-20 17:15:05 +02002354static int marvell_nand_attach_chip(struct nand_chip *chip)
2355{
2356 struct mtd_info *mtd = nand_to_mtd(chip);
2357 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2358 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2359 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(nfc->dev);
2360 int ret;
2361
2362 if (pdata && pdata->flash_bbt)
2363 chip->bbt_options |= NAND_BBT_USE_FLASH;
2364
2365 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
2366 /*
2367 * We'll use a bad block table stored in-flash and don't
2368 * allow writing the bad block marker to the flash.
2369 */
2370 chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
2371 chip->bbt_td = &bbt_main_descr;
2372 chip->bbt_md = &bbt_mirror_descr;
2373 }
2374
2375 /* Save the chip-specific fields of NDCR */
2376 marvell_nand->ndcr = NDCR_PAGE_SZ(mtd->writesize);
2377 if (chip->options & NAND_BUSWIDTH_16)
2378 marvell_nand->ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
2379
2380 /*
2381 * On small page NANDs, only one cycle is needed to pass the
2382 * column address.
2383 */
2384 if (mtd->writesize <= 512) {
2385 marvell_nand->addr_cyc = 1;
2386 } else {
2387 marvell_nand->addr_cyc = 2;
2388 marvell_nand->ndcr |= NDCR_RA_START;
2389 }
2390
2391 /*
2392 * Now add the number of cycles needed to pass the row
2393 * address.
2394 *
2395 * Addressing a chip using CS 2 or 3 should also need the third row
2396 * cycle but due to inconsistance in the documentation and lack of
2397 * hardware to test this situation, this case is not supported.
2398 */
2399 if (chip->options & NAND_ROW_ADDR_3)
2400 marvell_nand->addr_cyc += 3;
2401 else
2402 marvell_nand->addr_cyc += 2;
2403
2404 if (pdata) {
2405 chip->ecc.size = pdata->ecc_step_size;
2406 chip->ecc.strength = pdata->ecc_strength;
2407 }
2408
2409 ret = marvell_nand_ecc_init(mtd, &chip->ecc);
2410 if (ret) {
2411 dev_err(nfc->dev, "ECC init failed: %d\n", ret);
2412 return ret;
2413 }
2414
2415 if (chip->ecc.mode == NAND_ECC_HW) {
2416 /*
2417 * Subpage write not available with hardware ECC, prohibit also
2418 * subpage read as in userspace subpage access would still be
2419 * allowed and subpage write, if used, would lead to numerous
2420 * uncorrectable ECC errors.
2421 */
2422 chip->options |= NAND_NO_SUBPAGE_WRITE;
2423 }
2424
2425 if (pdata || nfc->caps->legacy_of_bindings) {
2426 /*
2427 * We keep the MTD name unchanged to avoid breaking platforms
2428 * where the MTD cmdline parser is used and the bootloader
2429 * has not been updated to use the new naming scheme.
2430 */
2431 mtd->name = "pxa3xx_nand-0";
2432 } else if (!mtd->name) {
2433 /*
2434 * If the new bindings are used and the bootloader has not been
2435 * updated to pass a new mtdparts parameter on the cmdline, you
2436 * should define the following property in your NAND node, ie:
2437 *
2438 * label = "main-storage";
2439 *
2440 * This way, mtd->name will be set by the core when
2441 * nand_set_flash_node() is called.
2442 */
2443 mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL,
2444 "%s:nand.%d", dev_name(nfc->dev),
2445 marvell_nand->sels[0].cs);
2446 if (!mtd->name) {
2447 dev_err(nfc->dev, "Failed to allocate mtd->name\n");
2448 return -ENOMEM;
2449 }
2450 }
2451
2452 return 0;
2453}
2454
2455static const struct nand_controller_ops marvell_nand_controller_ops = {
2456 .attach_chip = marvell_nand_attach_chip,
2457};
2458
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002459static int marvell_nand_chip_init(struct device *dev, struct marvell_nfc *nfc,
2460 struct device_node *np)
2461{
2462 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(dev);
2463 struct marvell_nand_chip *marvell_nand;
2464 struct mtd_info *mtd;
2465 struct nand_chip *chip;
2466 int nsels, ret, i;
2467 u32 cs, rb;
2468
2469 /*
2470 * The legacy "num-cs" property indicates the number of CS on the only
2471 * chip connected to the controller (legacy bindings does not support
Miquel Raynalf6997be2018-04-25 16:16:32 +02002472 * more than one chip). The CS and RB pins are always the #0.
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002473 *
2474 * When not using legacy bindings, a couple of "reg" and "nand-rb"
2475 * properties must be filled. For each chip, expressed as a subnode,
2476 * "reg" points to the CS lines and "nand-rb" to the RB line.
2477 */
Miquel Raynalf6997be2018-04-25 16:16:32 +02002478 if (pdata || nfc->caps->legacy_of_bindings) {
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002479 nsels = 1;
Miquel Raynalf6997be2018-04-25 16:16:32 +02002480 } else {
2481 nsels = of_property_count_elems_of_size(np, "reg", sizeof(u32));
2482 if (nsels <= 0) {
2483 dev_err(dev, "missing/invalid reg property\n");
2484 return -EINVAL;
2485 }
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002486 }
2487
2488 /* Alloc the nand chip structure */
2489 marvell_nand = devm_kzalloc(dev, sizeof(*marvell_nand) +
2490 (nsels *
2491 sizeof(struct marvell_nand_chip_sel)),
2492 GFP_KERNEL);
2493 if (!marvell_nand) {
2494 dev_err(dev, "could not allocate chip structure\n");
2495 return -ENOMEM;
2496 }
2497
2498 marvell_nand->nsels = nsels;
2499 marvell_nand->selected_die = -1;
2500
2501 for (i = 0; i < nsels; i++) {
2502 if (pdata || nfc->caps->legacy_of_bindings) {
2503 /*
2504 * Legacy bindings use the CS lines in natural
2505 * order (0, 1, ...)
2506 */
2507 cs = i;
2508 } else {
2509 /* Retrieve CS id */
2510 ret = of_property_read_u32_index(np, "reg", i, &cs);
2511 if (ret) {
2512 dev_err(dev, "could not retrieve reg property: %d\n",
2513 ret);
2514 return ret;
2515 }
2516 }
2517
2518 if (cs >= nfc->caps->max_cs_nb) {
2519 dev_err(dev, "invalid reg value: %u (max CS = %d)\n",
2520 cs, nfc->caps->max_cs_nb);
2521 return -EINVAL;
2522 }
2523
2524 if (test_and_set_bit(cs, &nfc->assigned_cs)) {
2525 dev_err(dev, "CS %d already assigned\n", cs);
2526 return -EINVAL;
2527 }
2528
2529 /*
2530 * The cs variable represents the chip select id, which must be
2531 * converted in bit fields for NDCB0 and NDCB2 to select the
2532 * right chip. Unfortunately, due to a lack of information on
2533 * the subject and incoherent documentation, the user should not
2534 * use CS1 and CS3 at all as asserting them is not supported in
2535 * a reliable way (due to multiplexing inside ADDR5 field).
2536 */
2537 marvell_nand->sels[i].cs = cs;
2538 switch (cs) {
2539 case 0:
2540 case 2:
2541 marvell_nand->sels[i].ndcb0_csel = 0;
2542 break;
2543 case 1:
2544 case 3:
2545 marvell_nand->sels[i].ndcb0_csel = NDCB0_CSEL;
2546 break;
2547 default:
2548 return -EINVAL;
2549 }
2550
2551 /* Retrieve RB id */
2552 if (pdata || nfc->caps->legacy_of_bindings) {
2553 /* Legacy bindings always use RB #0 */
2554 rb = 0;
2555 } else {
2556 ret = of_property_read_u32_index(np, "nand-rb", i,
2557 &rb);
2558 if (ret) {
2559 dev_err(dev,
2560 "could not retrieve RB property: %d\n",
2561 ret);
2562 return ret;
2563 }
2564 }
2565
2566 if (rb >= nfc->caps->max_rb_nb) {
2567 dev_err(dev, "invalid reg value: %u (max RB = %d)\n",
2568 rb, nfc->caps->max_rb_nb);
2569 return -EINVAL;
2570 }
2571
2572 marvell_nand->sels[i].rb = rb;
2573 }
2574
2575 chip = &marvell_nand->chip;
2576 chip->controller = &nfc->controller;
2577 nand_set_flash_node(chip, np);
2578
2579 chip->exec_op = marvell_nfc_exec_op;
2580 chip->select_chip = marvell_nfc_select_chip;
Miquel Raynal07ad5a72018-01-17 00:19:34 +01002581 if (!of_property_read_bool(np, "marvell,nand-keep-config"))
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002582 chip->setup_data_interface = marvell_nfc_setup_data_interface;
2583
2584 mtd = nand_to_mtd(chip);
2585 mtd->dev.parent = dev;
2586
2587 /*
2588 * Default to HW ECC engine mode. If the nand-ecc-mode property is given
2589 * in the DT node, this entry will be overwritten in nand_scan_ident().
2590 */
2591 chip->ecc.mode = NAND_ECC_HW;
2592
2593 /*
2594 * Save a reference value for timing registers before
2595 * ->setup_data_interface() is called.
2596 */
2597 marvell_nand->ndtr0 = readl_relaxed(nfc->regs + NDTR0);
2598 marvell_nand->ndtr1 = readl_relaxed(nfc->regs + NDTR1);
2599
2600 chip->options |= NAND_BUSWIDTH_AUTO;
Miquel Raynal8831e482018-07-20 17:15:05 +02002601
Boris Brezillon00ad3782018-09-06 14:05:14 +02002602 ret = nand_scan(chip, marvell_nand->nsels);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002603 if (ret) {
Miquel Raynal8831e482018-07-20 17:15:05 +02002604 dev_err(dev, "could not scan the nand chip\n");
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002605 return ret;
2606 }
2607
2608 if (pdata)
2609 /* Legacy bindings support only one chip */
Miquel Raynal75765942018-02-19 23:35:54 +01002610 ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002611 else
2612 ret = mtd_device_register(mtd, NULL, 0);
2613 if (ret) {
2614 dev_err(dev, "failed to register mtd device: %d\n", ret);
Boris Brezillon59ac2762018-09-06 14:05:15 +02002615 nand_release(chip);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002616 return ret;
2617 }
2618
2619 list_add_tail(&marvell_nand->node, &nfc->chips);
2620
2621 return 0;
2622}
2623
2624static int marvell_nand_chips_init(struct device *dev, struct marvell_nfc *nfc)
2625{
2626 struct device_node *np = dev->of_node;
2627 struct device_node *nand_np;
2628 int max_cs = nfc->caps->max_cs_nb;
2629 int nchips;
2630 int ret;
2631
2632 if (!np)
2633 nchips = 1;
2634 else
2635 nchips = of_get_child_count(np);
2636
2637 if (nchips > max_cs) {
2638 dev_err(dev, "too many NAND chips: %d (max = %d CS)\n", nchips,
2639 max_cs);
2640 return -EINVAL;
2641 }
2642
2643 /*
2644 * Legacy bindings do not use child nodes to exhibit NAND chip
2645 * properties and layout. Instead, NAND properties are mixed with the
2646 * controller ones, and partitions are defined as direct subnodes of the
2647 * NAND controller node.
2648 */
2649 if (nfc->caps->legacy_of_bindings) {
2650 ret = marvell_nand_chip_init(dev, nfc, np);
2651 return ret;
2652 }
2653
2654 for_each_child_of_node(np, nand_np) {
2655 ret = marvell_nand_chip_init(dev, nfc, nand_np);
2656 if (ret) {
2657 of_node_put(nand_np);
2658 return ret;
2659 }
2660 }
2661
2662 return 0;
2663}
2664
2665static void marvell_nand_chips_cleanup(struct marvell_nfc *nfc)
2666{
2667 struct marvell_nand_chip *entry, *temp;
2668
2669 list_for_each_entry_safe(entry, temp, &nfc->chips, node) {
Boris Brezillon59ac2762018-09-06 14:05:15 +02002670 nand_release(&entry->chip);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002671 list_del(&entry->node);
2672 }
2673}
2674
2675static int marvell_nfc_init_dma(struct marvell_nfc *nfc)
2676{
2677 struct platform_device *pdev = container_of(nfc->dev,
2678 struct platform_device,
2679 dev);
2680 struct dma_slave_config config = {};
2681 struct resource *r;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002682 int ret;
2683
2684 if (!IS_ENABLED(CONFIG_PXA_DMA)) {
2685 dev_warn(nfc->dev,
2686 "DMA not enabled in configuration\n");
2687 return -ENOTSUPP;
2688 }
2689
2690 ret = dma_set_mask_and_coherent(nfc->dev, DMA_BIT_MASK(32));
2691 if (ret)
2692 return ret;
2693
Robert Jarzmikac75a502018-06-17 19:02:09 +02002694 nfc->dma_chan = dma_request_slave_channel(nfc->dev, "data");
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002695 if (!nfc->dma_chan) {
2696 dev_err(nfc->dev,
2697 "Unable to request data DMA channel\n");
2698 return -ENODEV;
2699 }
2700
2701 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2702 if (!r)
2703 return -ENXIO;
2704
2705 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2706 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2707 config.src_addr = r->start + NDDB;
2708 config.dst_addr = r->start + NDDB;
2709 config.src_maxburst = 32;
2710 config.dst_maxburst = 32;
2711 ret = dmaengine_slave_config(nfc->dma_chan, &config);
2712 if (ret < 0) {
2713 dev_err(nfc->dev, "Failed to configure DMA channel\n");
2714 return ret;
2715 }
2716
2717 /*
2718 * DMA must act on length multiple of 32 and this length may be
2719 * bigger than the destination buffer. Use this buffer instead
2720 * for DMA transfers and then copy the desired amount of data to
2721 * the provided buffer.
2722 */
Miquel Raynalc495a922018-01-19 18:39:01 +01002723 nfc->dma_buf = kmalloc(MAX_CHUNK_SIZE, GFP_KERNEL | GFP_DMA);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002724 if (!nfc->dma_buf)
2725 return -ENOMEM;
2726
2727 nfc->use_dma = true;
2728
2729 return 0;
2730}
2731
Daniel Mackbd9c3f92018-07-08 02:10:06 +02002732static void marvell_nfc_reset(struct marvell_nfc *nfc)
2733{
2734 /*
2735 * ECC operations and interruptions are only enabled when specifically
2736 * needed. ECC shall not be activated in the early stages (fails probe).
2737 * Arbiter flag, even if marked as "reserved", must be set (empirical).
2738 * SPARE_EN bit must always be set or ECC bytes will not be at the same
2739 * offset in the read page and this will fail the protection.
2740 */
2741 writel_relaxed(NDCR_ALL_INT | NDCR_ND_ARB_EN | NDCR_SPARE_EN |
2742 NDCR_RD_ID_CNT(NFCV1_READID_LEN), nfc->regs + NDCR);
2743 writel_relaxed(0xFFFFFFFF, nfc->regs + NDSR);
2744 writel_relaxed(0, nfc->regs + NDECCCTRL);
2745}
2746
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002747static int marvell_nfc_init(struct marvell_nfc *nfc)
2748{
2749 struct device_node *np = nfc->dev->of_node;
2750
2751 /*
2752 * Some SoCs like A7k/A8k need to enable manually the NAND
2753 * controller, gated clocks and reset bits to avoid being bootloader
2754 * dependent. This is done through the use of the System Functions
2755 * registers.
2756 */
2757 if (nfc->caps->need_system_controller) {
2758 struct regmap *sysctrl_base =
2759 syscon_regmap_lookup_by_phandle(np,
2760 "marvell,system-controller");
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002761
2762 if (IS_ERR(sysctrl_base))
2763 return PTR_ERR(sysctrl_base);
2764
Thomas Petazzoni88aa3bb2018-08-02 10:56:25 +02002765 regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX,
2766 GENCONF_SOC_DEVICE_MUX_NFC_EN |
2767 GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST |
2768 GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST |
2769 GENCONF_SOC_DEVICE_MUX_NFC_INT_EN);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002770
Thomas Petazzoni88aa3bb2018-08-02 10:56:25 +02002771 regmap_update_bits(sysctrl_base, GENCONF_CLK_GATING_CTRL,
2772 GENCONF_CLK_GATING_CTRL_ND_GATE,
2773 GENCONF_CLK_GATING_CTRL_ND_GATE);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002774
Thomas Petazzoni88aa3bb2018-08-02 10:56:25 +02002775 regmap_update_bits(sysctrl_base, GENCONF_ND_CLK_CTRL,
2776 GENCONF_ND_CLK_CTRL_EN,
2777 GENCONF_ND_CLK_CTRL_EN);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002778 }
2779
2780 /* Configure the DMA if appropriate */
2781 if (!nfc->caps->is_nfcv2)
2782 marvell_nfc_init_dma(nfc);
2783
Daniel Mackbd9c3f92018-07-08 02:10:06 +02002784 marvell_nfc_reset(nfc);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002785
2786 return 0;
2787}
2788
2789static int marvell_nfc_probe(struct platform_device *pdev)
2790{
2791 struct device *dev = &pdev->dev;
2792 struct resource *r;
2793 struct marvell_nfc *nfc;
2794 int ret;
2795 int irq;
2796
2797 nfc = devm_kzalloc(&pdev->dev, sizeof(struct marvell_nfc),
2798 GFP_KERNEL);
2799 if (!nfc)
2800 return -ENOMEM;
2801
2802 nfc->dev = dev;
Miquel Raynal7da45132018-07-17 09:08:02 +02002803 nand_controller_init(&nfc->controller);
Miquel Raynal8831e482018-07-20 17:15:05 +02002804 nfc->controller.ops = &marvell_nand_controller_ops;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002805 INIT_LIST_HEAD(&nfc->chips);
2806
2807 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2808 nfc->regs = devm_ioremap_resource(dev, r);
2809 if (IS_ERR(nfc->regs))
2810 return PTR_ERR(nfc->regs);
2811
2812 irq = platform_get_irq(pdev, 0);
2813 if (irq < 0) {
2814 dev_err(dev, "failed to retrieve irq\n");
2815 return irq;
2816 }
2817
Boris Brezillon6b6de652018-03-26 11:53:01 +02002818 nfc->core_clk = devm_clk_get(&pdev->dev, "core");
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002819
2820 /* Managed the legacy case (when the first clock was not named) */
Boris Brezillon6b6de652018-03-26 11:53:01 +02002821 if (nfc->core_clk == ERR_PTR(-ENOENT))
2822 nfc->core_clk = devm_clk_get(&pdev->dev, NULL);
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002823
Boris Brezillon6b6de652018-03-26 11:53:01 +02002824 if (IS_ERR(nfc->core_clk))
2825 return PTR_ERR(nfc->core_clk);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002826
Boris Brezillon6b6de652018-03-26 11:53:01 +02002827 ret = clk_prepare_enable(nfc->core_clk);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002828 if (ret)
2829 return ret;
2830
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002831 nfc->reg_clk = devm_clk_get(&pdev->dev, "reg");
Daniel Mackf9e64d62018-07-08 02:10:08 +02002832 if (IS_ERR(nfc->reg_clk)) {
2833 if (PTR_ERR(nfc->reg_clk) != -ENOENT) {
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002834 ret = PTR_ERR(nfc->reg_clk);
Boris Brezillon6b6de652018-03-26 11:53:01 +02002835 goto unprepare_core_clk;
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002836 }
Daniel Mackf9e64d62018-07-08 02:10:08 +02002837
2838 nfc->reg_clk = NULL;
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002839 }
2840
Daniel Mackf9e64d62018-07-08 02:10:08 +02002841 ret = clk_prepare_enable(nfc->reg_clk);
2842 if (ret)
2843 goto unprepare_core_clk;
2844
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002845 marvell_nfc_disable_int(nfc, NDCR_ALL_INT);
2846 marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
2847 ret = devm_request_irq(dev, irq, marvell_nfc_isr,
2848 0, "marvell-nfc", nfc);
2849 if (ret)
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002850 goto unprepare_reg_clk;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002851
2852 /* Get NAND controller capabilities */
2853 if (pdev->id_entry)
2854 nfc->caps = (void *)pdev->id_entry->driver_data;
2855 else
2856 nfc->caps = of_device_get_match_data(&pdev->dev);
2857
2858 if (!nfc->caps) {
2859 dev_err(dev, "Could not retrieve NFC caps\n");
2860 ret = -EINVAL;
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002861 goto unprepare_reg_clk;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002862 }
2863
2864 /* Init the controller and then probe the chips */
2865 ret = marvell_nfc_init(nfc);
2866 if (ret)
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002867 goto unprepare_reg_clk;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002868
2869 platform_set_drvdata(pdev, nfc);
2870
2871 ret = marvell_nand_chips_init(dev, nfc);
2872 if (ret)
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002873 goto unprepare_reg_clk;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002874
2875 return 0;
2876
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002877unprepare_reg_clk:
2878 clk_disable_unprepare(nfc->reg_clk);
Boris Brezillon6b6de652018-03-26 11:53:01 +02002879unprepare_core_clk:
2880 clk_disable_unprepare(nfc->core_clk);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002881
2882 return ret;
2883}
2884
2885static int marvell_nfc_remove(struct platform_device *pdev)
2886{
2887 struct marvell_nfc *nfc = platform_get_drvdata(pdev);
2888
2889 marvell_nand_chips_cleanup(nfc);
2890
2891 if (nfc->use_dma) {
2892 dmaengine_terminate_all(nfc->dma_chan);
2893 dma_release_channel(nfc->dma_chan);
2894 }
2895
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002896 clk_disable_unprepare(nfc->reg_clk);
Boris Brezillon6b6de652018-03-26 11:53:01 +02002897 clk_disable_unprepare(nfc->core_clk);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002898
2899 return 0;
2900}
2901
Daniel Mackbd9c3f92018-07-08 02:10:06 +02002902static int __maybe_unused marvell_nfc_suspend(struct device *dev)
2903{
2904 struct marvell_nfc *nfc = dev_get_drvdata(dev);
2905 struct marvell_nand_chip *chip;
2906
2907 list_for_each_entry(chip, &nfc->chips, node)
2908 marvell_nfc_wait_ndrun(&chip->chip);
2909
2910 clk_disable_unprepare(nfc->reg_clk);
2911 clk_disable_unprepare(nfc->core_clk);
2912
2913 return 0;
2914}
2915
2916static int __maybe_unused marvell_nfc_resume(struct device *dev)
2917{
2918 struct marvell_nfc *nfc = dev_get_drvdata(dev);
2919 int ret;
2920
2921 ret = clk_prepare_enable(nfc->core_clk);
2922 if (ret < 0)
2923 return ret;
2924
Daniel Mackf9e64d62018-07-08 02:10:08 +02002925 ret = clk_prepare_enable(nfc->reg_clk);
2926 if (ret < 0)
2927 return ret;
Daniel Mackbd9c3f92018-07-08 02:10:06 +02002928
2929 /*
2930 * Reset nfc->selected_chip so the next command will cause the timing
2931 * registers to be restored in marvell_nfc_select_chip().
2932 */
2933 nfc->selected_chip = NULL;
2934
2935 /* Reset registers that have lost their contents */
2936 marvell_nfc_reset(nfc);
2937
2938 return 0;
2939}
2940
2941static const struct dev_pm_ops marvell_nfc_pm_ops = {
2942 SET_SYSTEM_SLEEP_PM_OPS(marvell_nfc_suspend, marvell_nfc_resume)
2943};
2944
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002945static const struct marvell_nfc_caps marvell_armada_8k_nfc_caps = {
2946 .max_cs_nb = 4,
2947 .max_rb_nb = 2,
2948 .need_system_controller = true,
2949 .is_nfcv2 = true,
2950};
2951
2952static const struct marvell_nfc_caps marvell_armada370_nfc_caps = {
2953 .max_cs_nb = 4,
2954 .max_rb_nb = 2,
2955 .is_nfcv2 = true,
2956};
2957
2958static const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps = {
2959 .max_cs_nb = 2,
2960 .max_rb_nb = 1,
2961 .use_dma = true,
2962};
2963
2964static const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps = {
2965 .max_cs_nb = 4,
2966 .max_rb_nb = 2,
2967 .need_system_controller = true,
2968 .legacy_of_bindings = true,
2969 .is_nfcv2 = true,
2970};
2971
2972static const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps = {
2973 .max_cs_nb = 4,
2974 .max_rb_nb = 2,
2975 .legacy_of_bindings = true,
2976 .is_nfcv2 = true,
2977};
2978
2979static const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps = {
2980 .max_cs_nb = 2,
2981 .max_rb_nb = 1,
2982 .legacy_of_bindings = true,
2983 .use_dma = true,
2984};
2985
2986static const struct platform_device_id marvell_nfc_platform_ids[] = {
2987 {
2988 .name = "pxa3xx-nand",
2989 .driver_data = (kernel_ulong_t)&marvell_pxa3xx_nfc_legacy_caps,
2990 },
2991 { /* sentinel */ },
2992};
2993MODULE_DEVICE_TABLE(platform, marvell_nfc_platform_ids);
2994
2995static const struct of_device_id marvell_nfc_of_ids[] = {
2996 {
2997 .compatible = "marvell,armada-8k-nand-controller",
2998 .data = &marvell_armada_8k_nfc_caps,
2999 },
3000 {
3001 .compatible = "marvell,armada370-nand-controller",
3002 .data = &marvell_armada370_nfc_caps,
3003 },
3004 {
3005 .compatible = "marvell,pxa3xx-nand-controller",
3006 .data = &marvell_pxa3xx_nfc_caps,
3007 },
3008 /* Support for old/deprecated bindings: */
3009 {
3010 .compatible = "marvell,armada-8k-nand",
3011 .data = &marvell_armada_8k_nfc_legacy_caps,
3012 },
3013 {
3014 .compatible = "marvell,armada370-nand",
3015 .data = &marvell_armada370_nfc_legacy_caps,
3016 },
3017 {
3018 .compatible = "marvell,pxa3xx-nand",
3019 .data = &marvell_pxa3xx_nfc_legacy_caps,
3020 },
3021 { /* sentinel */ },
3022};
3023MODULE_DEVICE_TABLE(of, marvell_nfc_of_ids);
3024
3025static struct platform_driver marvell_nfc_driver = {
3026 .driver = {
3027 .name = "marvell-nfc",
3028 .of_match_table = marvell_nfc_of_ids,
Daniel Mackbd9c3f92018-07-08 02:10:06 +02003029 .pm = &marvell_nfc_pm_ops,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01003030 },
3031 .id_table = marvell_nfc_platform_ids,
3032 .probe = marvell_nfc_probe,
3033 .remove = marvell_nfc_remove,
3034};
3035module_platform_driver(marvell_nfc_driver);
3036
3037MODULE_LICENSE("GPL");
3038MODULE_DESCRIPTION("Marvell NAND controller driver");