blob: b7b4d9b14da1e2c89643852aa0ab9538671dee82 [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),
Miquel Raynal7fd130f2018-07-19 12:21:19 +0200287 MARVELL_LAYOUT( 2048, 512, 8, 2, 1, 1024, 0, 30,1024,32, 30),
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100288 MARVELL_LAYOUT( 4096, 512, 4, 2, 2, 2048, 32, 30, 0, 0, 0),
289 MARVELL_LAYOUT( 4096, 512, 8, 5, 4, 1024, 0, 30, 0, 64, 30),
Konstantin Porotchkine8237bf2018-09-07 16:34:36 +0200290 MARVELL_LAYOUT( 8192, 512, 4, 4, 4, 2048, 0, 30, 0, 0, 0),
291 MARVELL_LAYOUT( 8192, 512, 8, 9, 8, 1024, 0, 30, 0, 160, 30),
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100292};
293
294/**
295 * The Nand Flash Controller has up to 4 CE and 2 RB pins. The CE selection
296 * is made by a field in NDCB0 register, and in another field in NDCB2 register.
297 * The datasheet describes the logic with an error: ADDR5 field is once
298 * declared at the beginning of NDCB2, and another time at its end. Because the
299 * ADDR5 field of NDCB2 may be used by other bytes, it would be more logical
300 * to use the last bit of this field instead of the first ones.
301 *
302 * @cs: Wanted CE lane.
303 * @ndcb0_csel: Value of the NDCB0 register with or without the flag
304 * selecting the wanted CE lane. This is set once when
305 * the Device Tree is probed.
306 * @rb: Ready/Busy pin for the flash chip
307 */
308struct marvell_nand_chip_sel {
309 unsigned int cs;
310 u32 ndcb0_csel;
311 unsigned int rb;
312};
313
314/**
315 * NAND chip structure: stores NAND chip device related information
316 *
317 * @chip: Base NAND chip structure
318 * @node: Used to store NAND chips into a list
319 * @layout NAND layout when using hardware ECC
320 * @ndcr: Controller register value for this NAND chip
321 * @ndtr0: Timing registers 0 value for this NAND chip
322 * @ndtr1: Timing registers 1 value for this NAND chip
323 * @selected_die: Current active CS
324 * @nsels: Number of CS lines required by the NAND chip
325 * @sels: Array of CS lines descriptions
326 */
327struct marvell_nand_chip {
328 struct nand_chip chip;
329 struct list_head node;
330 const struct marvell_hw_ecc_layout *layout;
331 u32 ndcr;
332 u32 ndtr0;
333 u32 ndtr1;
334 int addr_cyc;
335 int selected_die;
336 unsigned int nsels;
337 struct marvell_nand_chip_sel sels[0];
338};
339
340static inline struct marvell_nand_chip *to_marvell_nand(struct nand_chip *chip)
341{
342 return container_of(chip, struct marvell_nand_chip, chip);
343}
344
345static inline struct marvell_nand_chip_sel *to_nand_sel(struct marvell_nand_chip
346 *nand)
347{
348 return &nand->sels[nand->selected_die];
349}
350
351/**
352 * NAND controller capabilities for distinction between compatible strings
353 *
354 * @max_cs_nb: Number of Chip Select lines available
355 * @max_rb_nb: Number of Ready/Busy lines available
356 * @need_system_controller: Indicates if the SoC needs to have access to the
357 * system controller (ie. to enable the NAND controller)
358 * @legacy_of_bindings: Indicates if DT parsing must be done using the old
359 * fashion way
360 * @is_nfcv2: NFCv2 has numerous enhancements compared to NFCv1, ie.
361 * BCH error detection and correction algorithm,
362 * NDCB3 register has been added
363 * @use_dma: Use dma for data transfers
364 */
365struct marvell_nfc_caps {
366 unsigned int max_cs_nb;
367 unsigned int max_rb_nb;
368 bool need_system_controller;
369 bool legacy_of_bindings;
370 bool is_nfcv2;
371 bool use_dma;
372};
373
374/**
375 * NAND controller structure: stores Marvell NAND controller information
376 *
377 * @controller: Base controller structure
378 * @dev: Parent device (used to print error messages)
379 * @regs: NAND controller registers
Boris Brezillon6b6de652018-03-26 11:53:01 +0200380 * @core_clk: Core clock
Gregory CLEMENT961ba152018-03-13 11:30:16 +0100381 * @reg_clk: Regiters clock
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100382 * @complete: Completion object to wait for NAND controller events
383 * @assigned_cs: Bitmask describing already assigned CS lines
384 * @chips: List containing all the NAND chips attached to
385 * this NAND controller
386 * @caps: NAND controller capabilities for each compatible string
387 * @dma_chan: DMA channel (NFCv1 only)
388 * @dma_buf: 32-bit aligned buffer for DMA transfers (NFCv1 only)
389 */
390struct marvell_nfc {
Miquel Raynal7da45132018-07-17 09:08:02 +0200391 struct nand_controller controller;
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100392 struct device *dev;
393 void __iomem *regs;
Boris Brezillon6b6de652018-03-26 11:53:01 +0200394 struct clk *core_clk;
Gregory CLEMENT961ba152018-03-13 11:30:16 +0100395 struct clk *reg_clk;
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100396 struct completion complete;
397 unsigned long assigned_cs;
398 struct list_head chips;
399 struct nand_chip *selected_chip;
400 const struct marvell_nfc_caps *caps;
401
402 /* DMA (NFCv1 only) */
403 bool use_dma;
404 struct dma_chan *dma_chan;
405 u8 *dma_buf;
406};
407
Miquel Raynal7da45132018-07-17 09:08:02 +0200408static inline struct marvell_nfc *to_marvell_nfc(struct nand_controller *ctrl)
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100409{
410 return container_of(ctrl, struct marvell_nfc, controller);
411}
412
413/**
414 * NAND controller timings expressed in NAND Controller clock cycles
415 *
416 * @tRP: ND_nRE pulse width
417 * @tRH: ND_nRE high duration
418 * @tWP: ND_nWE pulse time
419 * @tWH: ND_nWE high duration
420 * @tCS: Enable signal setup time
421 * @tCH: Enable signal hold time
422 * @tADL: Address to write data delay
423 * @tAR: ND_ALE low to ND_nRE low delay
424 * @tWHR: ND_nWE high to ND_nRE low for status read
425 * @tRHW: ND_nRE high duration, read to write delay
426 * @tR: ND_nWE high to ND_nRE low for read
427 */
428struct marvell_nfc_timings {
429 /* NDTR0 fields */
430 unsigned int tRP;
431 unsigned int tRH;
432 unsigned int tWP;
433 unsigned int tWH;
434 unsigned int tCS;
435 unsigned int tCH;
436 unsigned int tADL;
437 /* NDTR1 fields */
438 unsigned int tAR;
439 unsigned int tWHR;
440 unsigned int tRHW;
441 unsigned int tR;
442};
443
444/**
445 * Derives a duration in numbers of clock cycles.
446 *
447 * @ps: Duration in pico-seconds
448 * @period_ns: Clock period in nano-seconds
449 *
450 * Convert the duration in nano-seconds, then divide by the period and
451 * return the number of clock periods.
452 */
453#define TO_CYCLES(ps, period_ns) (DIV_ROUND_UP(ps / 1000, period_ns))
Miquel Raynal07ad5a72018-01-17 00:19:34 +0100454#define TO_CYCLES64(ps, period_ns) (DIV_ROUND_UP_ULL(div_u64(ps, 1000), \
455 period_ns))
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100456
457/**
458 * NAND driver structure filled during the parsing of the ->exec_op() subop
459 * subset of instructions.
460 *
461 * @ndcb: Array of values written to NDCBx registers
462 * @cle_ale_delay_ns: Optional delay after the last CMD or ADDR cycle
463 * @rdy_timeout_ms: Timeout for waits on Ready/Busy pin
464 * @rdy_delay_ns: Optional delay after waiting for the RB pin
465 * @data_delay_ns: Optional delay after the data xfer
466 * @data_instr_idx: Index of the data instruction in the subop
467 * @data_instr: Pointer to the data instruction in the subop
468 */
469struct marvell_nfc_op {
470 u32 ndcb[4];
471 unsigned int cle_ale_delay_ns;
472 unsigned int rdy_timeout_ms;
473 unsigned int rdy_delay_ns;
474 unsigned int data_delay_ns;
475 unsigned int data_instr_idx;
476 const struct nand_op_instr *data_instr;
477};
478
479/*
480 * Internal helper to conditionnally apply a delay (from the above structure,
481 * most of the time).
482 */
483static void cond_delay(unsigned int ns)
484{
485 if (!ns)
486 return;
487
488 if (ns < 10000)
489 ndelay(ns);
490 else
491 udelay(DIV_ROUND_UP(ns, 1000));
492}
493
494/*
495 * The controller has many flags that could generate interrupts, most of them
496 * are disabled and polling is used. For the very slow signals, using interrupts
497 * may relax the CPU charge.
498 */
499static void marvell_nfc_disable_int(struct marvell_nfc *nfc, u32 int_mask)
500{
501 u32 reg;
502
503 /* Writing 1 disables the interrupt */
504 reg = readl_relaxed(nfc->regs + NDCR);
505 writel_relaxed(reg | int_mask, nfc->regs + NDCR);
506}
507
508static void marvell_nfc_enable_int(struct marvell_nfc *nfc, u32 int_mask)
509{
510 u32 reg;
511
512 /* Writing 0 enables the interrupt */
513 reg = readl_relaxed(nfc->regs + NDCR);
514 writel_relaxed(reg & ~int_mask, nfc->regs + NDCR);
515}
516
517static void marvell_nfc_clear_int(struct marvell_nfc *nfc, u32 int_mask)
518{
519 writel_relaxed(int_mask, nfc->regs + NDSR);
520}
521
522static void marvell_nfc_force_byte_access(struct nand_chip *chip,
523 bool force_8bit)
524{
525 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
526 u32 ndcr;
527
528 /*
529 * Callers of this function do not verify if the NAND is using a 16-bit
530 * an 8-bit bus for normal operations, so we need to take care of that
531 * here by leaving the configuration unchanged if the NAND does not have
532 * the NAND_BUSWIDTH_16 flag set.
533 */
534 if (!(chip->options & NAND_BUSWIDTH_16))
535 return;
536
537 ndcr = readl_relaxed(nfc->regs + NDCR);
538
539 if (force_8bit)
540 ndcr &= ~(NDCR_DWIDTH_M | NDCR_DWIDTH_C);
541 else
542 ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
543
544 writel_relaxed(ndcr, nfc->regs + NDCR);
545}
546
547static int marvell_nfc_wait_ndrun(struct nand_chip *chip)
548{
549 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
550 u32 val;
551 int ret;
552
553 /*
554 * The command is being processed, wait for the ND_RUN bit to be
555 * cleared by the NFC. If not, we must clear it by hand.
556 */
557 ret = readl_relaxed_poll_timeout(nfc->regs + NDCR, val,
558 (val & NDCR_ND_RUN) == 0,
559 POLL_PERIOD, POLL_TIMEOUT);
560 if (ret) {
561 dev_err(nfc->dev, "Timeout on NAND controller run mode\n");
562 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
563 nfc->regs + NDCR);
564 return ret;
565 }
566
567 return 0;
568}
569
570/*
571 * Any time a command has to be sent to the controller, the following sequence
572 * has to be followed:
573 * - call marvell_nfc_prepare_cmd()
574 * -> activate the ND_RUN bit that will kind of 'start a job'
575 * -> wait the signal indicating the NFC is waiting for a command
576 * - send the command (cmd and address cycles)
577 * - enventually send or receive the data
578 * - call marvell_nfc_end_cmd() with the corresponding flag
579 * -> wait the flag to be triggered or cancel the job with a timeout
580 *
581 * The following helpers are here to factorize the code a bit so that
582 * specialized functions responsible for executing the actual NAND
583 * operations do not have to replicate the same code blocks.
584 */
585static int marvell_nfc_prepare_cmd(struct nand_chip *chip)
586{
587 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
588 u32 ndcr, val;
589 int ret;
590
591 /* Poll ND_RUN and clear NDSR before issuing any command */
592 ret = marvell_nfc_wait_ndrun(chip);
593 if (ret) {
Colin Ian Kinga76497d2018-01-19 07:55:31 +0000594 dev_err(nfc->dev, "Last operation did not succeed\n");
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100595 return ret;
596 }
597
598 ndcr = readl_relaxed(nfc->regs + NDCR);
599 writel_relaxed(readl(nfc->regs + NDSR), nfc->regs + NDSR);
600
601 /* Assert ND_RUN bit and wait the NFC to be ready */
602 writel_relaxed(ndcr | NDCR_ND_RUN, nfc->regs + NDCR);
603 ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
604 val & NDSR_WRCMDREQ,
605 POLL_PERIOD, POLL_TIMEOUT);
606 if (ret) {
607 dev_err(nfc->dev, "Timeout on WRCMDRE\n");
608 return -ETIMEDOUT;
609 }
610
611 /* Command may be written, clear WRCMDREQ status bit */
612 writel_relaxed(NDSR_WRCMDREQ, nfc->regs + NDSR);
613
614 return 0;
615}
616
617static void marvell_nfc_send_cmd(struct nand_chip *chip,
618 struct marvell_nfc_op *nfc_op)
619{
620 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
621 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
622
623 dev_dbg(nfc->dev, "\nNDCR: 0x%08x\n"
624 "NDCB0: 0x%08x\nNDCB1: 0x%08x\nNDCB2: 0x%08x\nNDCB3: 0x%08x\n",
625 (u32)readl_relaxed(nfc->regs + NDCR), nfc_op->ndcb[0],
626 nfc_op->ndcb[1], nfc_op->ndcb[2], nfc_op->ndcb[3]);
627
628 writel_relaxed(to_nand_sel(marvell_nand)->ndcb0_csel | nfc_op->ndcb[0],
629 nfc->regs + NDCB0);
630 writel_relaxed(nfc_op->ndcb[1], nfc->regs + NDCB0);
631 writel(nfc_op->ndcb[2], nfc->regs + NDCB0);
632
633 /*
634 * Write NDCB0 four times only if LEN_OVRD is set or if ADDR6 or ADDR7
635 * fields are used (only available on NFCv2).
636 */
637 if (nfc_op->ndcb[0] & NDCB0_LEN_OVRD ||
638 NDCB0_ADDR_GET_NUM_CYC(nfc_op->ndcb[0]) >= 6) {
639 if (!WARN_ON_ONCE(!nfc->caps->is_nfcv2))
640 writel(nfc_op->ndcb[3], nfc->regs + NDCB0);
641 }
642}
643
644static int marvell_nfc_end_cmd(struct nand_chip *chip, int flag,
645 const char *label)
646{
647 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
648 u32 val;
649 int ret;
650
651 ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
652 val & flag,
653 POLL_PERIOD, POLL_TIMEOUT);
654
655 if (ret) {
656 dev_err(nfc->dev, "Timeout on %s (NDSR: 0x%08x)\n",
657 label, val);
658 if (nfc->dma_chan)
659 dmaengine_terminate_all(nfc->dma_chan);
660 return ret;
661 }
662
663 /*
664 * DMA function uses this helper to poll on CMDD bits without wanting
665 * them to be cleared.
666 */
667 if (nfc->use_dma && (readl_relaxed(nfc->regs + NDCR) & NDCR_DMA_EN))
668 return 0;
669
670 writel_relaxed(flag, nfc->regs + NDSR);
671
672 return 0;
673}
674
675static int marvell_nfc_wait_cmdd(struct nand_chip *chip)
676{
677 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
678 int cs_flag = NDSR_CMDD(to_nand_sel(marvell_nand)->ndcb0_csel);
679
680 return marvell_nfc_end_cmd(chip, cs_flag, "CMDD");
681}
682
683static int marvell_nfc_wait_op(struct nand_chip *chip, unsigned int timeout_ms)
684{
685 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
686 int ret;
687
688 /* Timeout is expressed in ms */
689 if (!timeout_ms)
690 timeout_ms = IRQ_TIMEOUT;
691
692 init_completion(&nfc->complete);
693
694 marvell_nfc_enable_int(nfc, NDCR_RDYM);
695 ret = wait_for_completion_timeout(&nfc->complete,
696 msecs_to_jiffies(timeout_ms));
697 marvell_nfc_disable_int(nfc, NDCR_RDYM);
698 marvell_nfc_clear_int(nfc, NDSR_RDY(0) | NDSR_RDY(1));
699 if (!ret) {
700 dev_err(nfc->dev, "Timeout waiting for RB signal\n");
701 return -ETIMEDOUT;
702 }
703
704 return 0;
705}
706
Boris Brezillonb2525142018-11-11 08:55:18 +0100707static void marvell_nfc_select_target(struct nand_chip *chip,
708 unsigned int die_nr)
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100709{
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100710 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
711 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
712 u32 ndcr_generic;
713
714 if (chip == nfc->selected_chip && die_nr == marvell_nand->selected_die)
715 return;
716
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100717 writel_relaxed(marvell_nand->ndtr0, nfc->regs + NDTR0);
718 writel_relaxed(marvell_nand->ndtr1, nfc->regs + NDTR1);
719
720 /*
721 * Reset the NDCR register to a clean state for this particular chip,
722 * also clear ND_RUN bit.
723 */
724 ndcr_generic = readl_relaxed(nfc->regs + NDCR) &
725 NDCR_GENERIC_FIELDS_MASK & ~NDCR_ND_RUN;
726 writel_relaxed(ndcr_generic | marvell_nand->ndcr, nfc->regs + NDCR);
727
728 /* Also reset the interrupt status register */
729 marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
730
731 nfc->selected_chip = chip;
732 marvell_nand->selected_die = die_nr;
733}
734
735static irqreturn_t marvell_nfc_isr(int irq, void *dev_id)
736{
737 struct marvell_nfc *nfc = dev_id;
738 u32 st = readl_relaxed(nfc->regs + NDSR);
739 u32 ien = (~readl_relaxed(nfc->regs + NDCR)) & NDCR_ALL_INT;
740
741 /*
742 * RDY interrupt mask is one bit in NDCR while there are two status
743 * bit in NDSR (RDY[cs0/cs2] and RDY[cs1/cs3]).
744 */
745 if (st & NDSR_RDY(1))
746 st |= NDSR_RDY(0);
747
748 if (!(st & ien))
749 return IRQ_NONE;
750
751 marvell_nfc_disable_int(nfc, st & NDCR_ALL_INT);
752
Miquel Raynal53c83b52018-10-03 11:05:04 +0200753 if (st & (NDSR_RDY(0) | NDSR_RDY(1)))
Miquel Raynal02f26ec2018-01-09 11:36:33 +0100754 complete(&nfc->complete);
755
756 return IRQ_HANDLED;
757}
758
759/* HW ECC related functions */
760static void marvell_nfc_enable_hw_ecc(struct nand_chip *chip)
761{
762 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
763 u32 ndcr = readl_relaxed(nfc->regs + NDCR);
764
765 if (!(ndcr & NDCR_ECC_EN)) {
766 writel_relaxed(ndcr | NDCR_ECC_EN, nfc->regs + NDCR);
767
768 /*
769 * When enabling BCH, set threshold to 0 to always know the
770 * number of corrected bitflips.
771 */
772 if (chip->ecc.algo == NAND_ECC_BCH)
773 writel_relaxed(NDECCCTRL_BCH_EN, nfc->regs + NDECCCTRL);
774 }
775}
776
777static void marvell_nfc_disable_hw_ecc(struct nand_chip *chip)
778{
779 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
780 u32 ndcr = readl_relaxed(nfc->regs + NDCR);
781
782 if (ndcr & NDCR_ECC_EN) {
783 writel_relaxed(ndcr & ~NDCR_ECC_EN, nfc->regs + NDCR);
784 if (chip->ecc.algo == NAND_ECC_BCH)
785 writel_relaxed(0, nfc->regs + NDECCCTRL);
786 }
787}
788
789/* DMA related helpers */
790static void marvell_nfc_enable_dma(struct marvell_nfc *nfc)
791{
792 u32 reg;
793
794 reg = readl_relaxed(nfc->regs + NDCR);
795 writel_relaxed(reg | NDCR_DMA_EN, nfc->regs + NDCR);
796}
797
798static void marvell_nfc_disable_dma(struct marvell_nfc *nfc)
799{
800 u32 reg;
801
802 reg = readl_relaxed(nfc->regs + NDCR);
803 writel_relaxed(reg & ~NDCR_DMA_EN, nfc->regs + NDCR);
804}
805
806/* Read/write PIO/DMA accessors */
807static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,
808 enum dma_data_direction direction,
809 unsigned int len)
810{
811 unsigned int dma_len = min_t(int, ALIGN(len, 32), MAX_CHUNK_SIZE);
812 struct dma_async_tx_descriptor *tx;
813 struct scatterlist sg;
814 dma_cookie_t cookie;
815 int ret;
816
817 marvell_nfc_enable_dma(nfc);
818 /* Prepare the DMA transfer */
819 sg_init_one(&sg, nfc->dma_buf, dma_len);
820 dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
821 tx = dmaengine_prep_slave_sg(nfc->dma_chan, &sg, 1,
822 direction == DMA_FROM_DEVICE ?
823 DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
824 DMA_PREP_INTERRUPT);
825 if (!tx) {
826 dev_err(nfc->dev, "Could not prepare DMA S/G list\n");
827 return -ENXIO;
828 }
829
830 /* Do the task and wait for it to finish */
831 cookie = dmaengine_submit(tx);
832 ret = dma_submit_error(cookie);
833 if (ret)
834 return -EIO;
835
836 dma_async_issue_pending(nfc->dma_chan);
837 ret = marvell_nfc_wait_cmdd(nfc->selected_chip);
838 dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
839 marvell_nfc_disable_dma(nfc);
840 if (ret) {
841 dev_err(nfc->dev, "Timeout waiting for DMA (status: %d)\n",
842 dmaengine_tx_status(nfc->dma_chan, cookie, NULL));
843 dmaengine_terminate_all(nfc->dma_chan);
844 return -ETIMEDOUT;
845 }
846
847 return 0;
848}
849
850static int marvell_nfc_xfer_data_in_pio(struct marvell_nfc *nfc, u8 *in,
851 unsigned int len)
852{
853 unsigned int last_len = len % FIFO_DEPTH;
854 unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
855 int i;
856
857 for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
858 ioread32_rep(nfc->regs + NDDB, in + i, FIFO_REP(FIFO_DEPTH));
859
860 if (last_len) {
861 u8 tmp_buf[FIFO_DEPTH];
862
863 ioread32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
864 memcpy(in + last_full_offset, tmp_buf, last_len);
865 }
866
867 return 0;
868}
869
870static int marvell_nfc_xfer_data_out_pio(struct marvell_nfc *nfc, const u8 *out,
871 unsigned int len)
872{
873 unsigned int last_len = len % FIFO_DEPTH;
874 unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
875 int i;
876
877 for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
878 iowrite32_rep(nfc->regs + NDDB, out + i, FIFO_REP(FIFO_DEPTH));
879
880 if (last_len) {
881 u8 tmp_buf[FIFO_DEPTH];
882
883 memcpy(tmp_buf, out + last_full_offset, last_len);
884 iowrite32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
885 }
886
887 return 0;
888}
889
890static void marvell_nfc_check_empty_chunk(struct nand_chip *chip,
891 u8 *data, int data_len,
892 u8 *spare, int spare_len,
893 u8 *ecc, int ecc_len,
894 unsigned int *max_bitflips)
895{
896 struct mtd_info *mtd = nand_to_mtd(chip);
897 int bf;
898
899 /*
900 * Blank pages (all 0xFF) that have not been written may be recognized
901 * as bad if bitflips occur, so whenever an uncorrectable error occurs,
902 * check if the entire page (with ECC bytes) is actually blank or not.
903 */
904 if (!data)
905 data_len = 0;
906 if (!spare)
907 spare_len = 0;
908 if (!ecc)
909 ecc_len = 0;
910
911 bf = nand_check_erased_ecc_chunk(data, data_len, ecc, ecc_len,
912 spare, spare_len, chip->ecc.strength);
913 if (bf < 0) {
914 mtd->ecc_stats.failed++;
915 return;
916 }
917
918 /* Update the stats and max_bitflips */
919 mtd->ecc_stats.corrected += bf;
920 *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
921}
922
923/*
924 * Check a chunk is correct or not according to hardware ECC engine.
925 * mtd->ecc_stats.corrected is updated, as well as max_bitflips, however
926 * mtd->ecc_stats.failure is not, the function will instead return a non-zero
927 * value indicating that a check on the emptyness of the subpage must be
928 * performed before declaring the subpage corrupted.
929 */
930static int marvell_nfc_hw_ecc_correct(struct nand_chip *chip,
931 unsigned int *max_bitflips)
932{
933 struct mtd_info *mtd = nand_to_mtd(chip);
934 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
935 int bf = 0;
936 u32 ndsr;
937
938 ndsr = readl_relaxed(nfc->regs + NDSR);
939
940 /* Check uncorrectable error flag */
941 if (ndsr & NDSR_UNCERR) {
942 writel_relaxed(ndsr, nfc->regs + NDSR);
943
944 /*
945 * Do not increment ->ecc_stats.failed now, instead, return a
946 * non-zero value to indicate that this chunk was apparently
947 * bad, and it should be check to see if it empty or not. If
948 * the chunk (with ECC bytes) is not declared empty, the calling
949 * function must increment the failure count.
950 */
951 return -EBADMSG;
952 }
953
954 /* Check correctable error flag */
955 if (ndsr & NDSR_CORERR) {
956 writel_relaxed(ndsr, nfc->regs + NDSR);
957
958 if (chip->ecc.algo == NAND_ECC_BCH)
959 bf = NDSR_ERRCNT(ndsr);
960 else
961 bf = 1;
962 }
963
964 /* Update the stats and max_bitflips */
965 mtd->ecc_stats.corrected += bf;
966 *max_bitflips = max_t(unsigned int, *max_bitflips, bf);
967
968 return 0;
969}
970
971/* Hamming read helpers */
972static int marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip *chip,
973 u8 *data_buf, u8 *oob_buf,
974 bool raw, int page)
975{
976 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
977 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
978 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
979 struct marvell_nfc_op nfc_op = {
980 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
981 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
982 NDCB0_DBC |
983 NDCB0_CMD1(NAND_CMD_READ0) |
984 NDCB0_CMD2(NAND_CMD_READSTART),
985 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
986 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
987 };
988 unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
989 int ret;
990
991 /* NFCv2 needs more information about the operation being executed */
992 if (nfc->caps->is_nfcv2)
993 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
994
995 ret = marvell_nfc_prepare_cmd(chip);
996 if (ret)
997 return ret;
998
999 marvell_nfc_send_cmd(chip, &nfc_op);
1000 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1001 "RDDREQ while draining FIFO (data/oob)");
1002 if (ret)
1003 return ret;
1004
1005 /*
1006 * Read the page then the OOB area. Unlike what is shown in current
1007 * documentation, spare bytes are protected by the ECC engine, and must
1008 * be at the beginning of the OOB area or running this driver on legacy
1009 * systems will prevent the discovery of the BBM/BBT.
1010 */
1011 if (nfc->use_dma) {
1012 marvell_nfc_xfer_data_dma(nfc, DMA_FROM_DEVICE,
1013 lt->data_bytes + oob_bytes);
1014 memcpy(data_buf, nfc->dma_buf, lt->data_bytes);
1015 memcpy(oob_buf, nfc->dma_buf + lt->data_bytes, oob_bytes);
1016 } else {
1017 marvell_nfc_xfer_data_in_pio(nfc, data_buf, lt->data_bytes);
1018 marvell_nfc_xfer_data_in_pio(nfc, oob_buf, oob_bytes);
1019 }
1020
1021 ret = marvell_nfc_wait_cmdd(chip);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001022 return ret;
1023}
1024
Boris Brezillonb9761682018-09-06 14:05:20 +02001025static int marvell_nfc_hw_ecc_hmg_read_page_raw(struct nand_chip *chip, u8 *buf,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001026 int oob_required, int page)
1027{
Boris Brezillonb2525142018-11-11 08:55:18 +01001028 marvell_nfc_select_target(chip, chip->cur_cs);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001029 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
1030 true, page);
1031}
1032
Boris Brezillonb9761682018-09-06 14:05:20 +02001033static int marvell_nfc_hw_ecc_hmg_read_page(struct nand_chip *chip, u8 *buf,
1034 int oob_required, int page)
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001035{
1036 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1037 unsigned int full_sz = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1038 int max_bitflips = 0, ret;
1039 u8 *raw_buf;
1040
Boris Brezillonb2525142018-11-11 08:55:18 +01001041 marvell_nfc_select_target(chip, chip->cur_cs);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001042 marvell_nfc_enable_hw_ecc(chip);
1043 marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, false,
1044 page);
1045 ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
1046 marvell_nfc_disable_hw_ecc(chip);
1047
1048 if (!ret)
1049 return max_bitflips;
1050
1051 /*
1052 * When ECC failures are detected, check if the full page has been
1053 * written or not. Ignore the failure if it is actually empty.
1054 */
1055 raw_buf = kmalloc(full_sz, GFP_KERNEL);
1056 if (!raw_buf)
1057 return -ENOMEM;
1058
1059 marvell_nfc_hw_ecc_hmg_do_read_page(chip, raw_buf, raw_buf +
1060 lt->data_bytes, true, page);
1061 marvell_nfc_check_empty_chunk(chip, raw_buf, full_sz, NULL, 0, NULL, 0,
1062 &max_bitflips);
1063 kfree(raw_buf);
1064
1065 return max_bitflips;
1066}
1067
1068/*
1069 * Spare area in Hamming layouts is not protected by the ECC engine (even if
1070 * it appears before the ECC bytes when reading), the ->read_oob_raw() function
1071 * also stands for ->read_oob().
1072 */
Boris Brezillonb9761682018-09-06 14:05:20 +02001073static int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct nand_chip *chip, int page)
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001074{
1075 /* Invalidate page cache */
1076 chip->pagebuf = -1;
1077
Boris Brezillonb2525142018-11-11 08:55:18 +01001078 marvell_nfc_select_target(chip, chip->cur_cs);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001079 return marvell_nfc_hw_ecc_hmg_do_read_page(chip, chip->data_buf,
1080 chip->oob_poi, true, page);
1081}
1082
1083/* Hamming write helpers */
1084static int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip *chip,
1085 const u8 *data_buf,
1086 const u8 *oob_buf, bool raw,
1087 int page)
1088{
1089 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1090 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1091 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1092 struct marvell_nfc_op nfc_op = {
1093 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) |
1094 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1095 NDCB0_CMD1(NAND_CMD_SEQIN) |
1096 NDCB0_CMD2(NAND_CMD_PAGEPROG) |
1097 NDCB0_DBC,
1098 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
1099 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
1100 };
1101 unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
1102 int ret;
1103
1104 /* NFCv2 needs more information about the operation being executed */
1105 if (nfc->caps->is_nfcv2)
1106 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
1107
1108 ret = marvell_nfc_prepare_cmd(chip);
1109 if (ret)
1110 return ret;
1111
1112 marvell_nfc_send_cmd(chip, &nfc_op);
1113 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1114 "WRDREQ while loading FIFO (data)");
1115 if (ret)
1116 return ret;
1117
1118 /* Write the page then the OOB area */
1119 if (nfc->use_dma) {
1120 memcpy(nfc->dma_buf, data_buf, lt->data_bytes);
1121 memcpy(nfc->dma_buf + lt->data_bytes, oob_buf, oob_bytes);
1122 marvell_nfc_xfer_data_dma(nfc, DMA_TO_DEVICE, lt->data_bytes +
1123 lt->ecc_bytes + lt->spare_bytes);
1124 } else {
1125 marvell_nfc_xfer_data_out_pio(nfc, data_buf, lt->data_bytes);
1126 marvell_nfc_xfer_data_out_pio(nfc, oob_buf, oob_bytes);
1127 }
1128
1129 ret = marvell_nfc_wait_cmdd(chip);
1130 if (ret)
1131 return ret;
1132
1133 ret = marvell_nfc_wait_op(chip,
Chris Packhamb76401f2018-05-03 14:21:28 +12001134 PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max));
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001135 return ret;
1136}
1137
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001138static int marvell_nfc_hw_ecc_hmg_write_page_raw(struct nand_chip *chip,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001139 const u8 *buf,
1140 int oob_required, int page)
1141{
Boris Brezillonb2525142018-11-11 08:55:18 +01001142 marvell_nfc_select_target(chip, chip->cur_cs);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001143 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1144 true, page);
1145}
1146
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001147static int marvell_nfc_hw_ecc_hmg_write_page(struct nand_chip *chip,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001148 const u8 *buf,
1149 int oob_required, int page)
1150{
1151 int ret;
1152
Boris Brezillonb2525142018-11-11 08:55:18 +01001153 marvell_nfc_select_target(chip, chip->cur_cs);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001154 marvell_nfc_enable_hw_ecc(chip);
1155 ret = marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1156 false, page);
1157 marvell_nfc_disable_hw_ecc(chip);
1158
1159 return ret;
1160}
1161
1162/*
1163 * Spare area in Hamming layouts is not protected by the ECC engine (even if
1164 * it appears before the ECC bytes when reading), the ->write_oob_raw() function
1165 * also stands for ->write_oob().
1166 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001167static int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct nand_chip *chip,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001168 int page)
1169{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001170 struct mtd_info *mtd = nand_to_mtd(chip);
1171
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001172 /* Invalidate page cache */
1173 chip->pagebuf = -1;
1174
1175 memset(chip->data_buf, 0xFF, mtd->writesize);
1176
Boris Brezillonb2525142018-11-11 08:55:18 +01001177 marvell_nfc_select_target(chip, chip->cur_cs);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001178 return marvell_nfc_hw_ecc_hmg_do_write_page(chip, chip->data_buf,
1179 chip->oob_poi, true, page);
1180}
1181
1182/* BCH read helpers */
Boris Brezillonb9761682018-09-06 14:05:20 +02001183static int marvell_nfc_hw_ecc_bch_read_page_raw(struct nand_chip *chip, u8 *buf,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001184 int oob_required, int page)
1185{
Boris Brezillonb9761682018-09-06 14:05:20 +02001186 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001187 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1188 u8 *oob = chip->oob_poi;
1189 int chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1190 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1191 lt->last_spare_bytes;
1192 int data_len = lt->data_bytes;
1193 int spare_len = lt->spare_bytes;
1194 int ecc_len = lt->ecc_bytes;
1195 int chunk;
1196
Boris Brezillonb2525142018-11-11 08:55:18 +01001197 marvell_nfc_select_target(chip, chip->cur_cs);
1198
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001199 if (oob_required)
1200 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1201
1202 nand_read_page_op(chip, page, 0, NULL, 0);
1203
1204 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1205 /* Update last chunk length */
1206 if (chunk >= lt->full_chunk_cnt) {
1207 data_len = lt->last_data_bytes;
1208 spare_len = lt->last_spare_bytes;
1209 ecc_len = lt->last_ecc_bytes;
1210 }
1211
1212 /* Read data bytes*/
1213 nand_change_read_column_op(chip, chunk * chunk_size,
1214 buf + (lt->data_bytes * chunk),
1215 data_len, false);
1216
1217 /* Read spare bytes */
1218 nand_read_data_op(chip, oob + (lt->spare_bytes * chunk),
1219 spare_len, false);
1220
1221 /* Read ECC bytes */
1222 nand_read_data_op(chip, oob + ecc_offset +
1223 (ALIGN(lt->ecc_bytes, 32) * chunk),
1224 ecc_len, false);
1225 }
1226
1227 return 0;
1228}
1229
1230static void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip *chip, int chunk,
1231 u8 *data, unsigned int data_len,
1232 u8 *spare, unsigned int spare_len,
1233 int page)
1234{
1235 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1236 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1237 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1238 int i, ret;
1239 struct marvell_nfc_op nfc_op = {
1240 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
1241 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1242 NDCB0_LEN_OVRD,
1243 .ndcb[1] = NDCB1_ADDRS_PAGE(page),
1244 .ndcb[2] = NDCB2_ADDR5_PAGE(page),
1245 .ndcb[3] = data_len + spare_len,
1246 };
1247
1248 ret = marvell_nfc_prepare_cmd(chip);
1249 if (ret)
1250 return;
1251
1252 if (chunk == 0)
1253 nfc_op.ndcb[0] |= NDCB0_DBC |
1254 NDCB0_CMD1(NAND_CMD_READ0) |
1255 NDCB0_CMD2(NAND_CMD_READSTART);
1256
1257 /*
Boris Brezillon90d61762018-05-09 09:13:58 +02001258 * Trigger the monolithic read on the first chunk, then naked read on
1259 * intermediate chunks and finally a last naked read on the last chunk.
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001260 */
Boris Brezillon90d61762018-05-09 09:13:58 +02001261 if (chunk == 0)
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001262 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
Boris Brezillon90d61762018-05-09 09:13:58 +02001263 else if (chunk < lt->nchunks - 1)
1264 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001265 else
1266 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1267
1268 marvell_nfc_send_cmd(chip, &nfc_op);
1269
1270 /*
1271 * According to the datasheet, when reading from NDDB
1272 * with BCH enabled, after each 32 bytes reads, we
1273 * have to make sure that the NDSR.RDDREQ bit is set.
1274 *
1275 * Drain the FIFO, 8 32-bit reads at a time, and skip
1276 * the polling on the last read.
1277 *
1278 * Length is a multiple of 32 bytes, hence it is a multiple of 8 too.
1279 */
1280 for (i = 0; i < data_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1281 marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1282 "RDDREQ while draining FIFO (data)");
1283 marvell_nfc_xfer_data_in_pio(nfc, data,
1284 FIFO_DEPTH * BCH_SEQ_READS);
1285 data += FIFO_DEPTH * BCH_SEQ_READS;
1286 }
1287
1288 for (i = 0; i < spare_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1289 marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1290 "RDDREQ while draining FIFO (OOB)");
1291 marvell_nfc_xfer_data_in_pio(nfc, spare,
1292 FIFO_DEPTH * BCH_SEQ_READS);
1293 spare += FIFO_DEPTH * BCH_SEQ_READS;
1294 }
1295}
1296
Boris Brezillonb9761682018-09-06 14:05:20 +02001297static int marvell_nfc_hw_ecc_bch_read_page(struct nand_chip *chip,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001298 u8 *buf, int oob_required,
1299 int page)
1300{
Boris Brezillonb9761682018-09-06 14:05:20 +02001301 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001302 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
Miquel Raynaldbfc6712018-07-19 16:54:23 +02001303 int data_len = lt->data_bytes, spare_len = lt->spare_bytes;
1304 u8 *data = buf, *spare = chip->oob_poi;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001305 int max_bitflips = 0;
1306 u32 failure_mask = 0;
Miquel Raynaldbfc6712018-07-19 16:54:23 +02001307 int chunk, ret;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001308
Boris Brezillonb2525142018-11-11 08:55:18 +01001309 marvell_nfc_select_target(chip, chip->cur_cs);
1310
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001311 /*
1312 * With BCH, OOB is not fully used (and thus not read entirely), not
1313 * expected bytes could show up at the end of the OOB buffer if not
1314 * explicitly erased.
1315 */
1316 if (oob_required)
1317 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1318
1319 marvell_nfc_enable_hw_ecc(chip);
1320
1321 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1322 /* Update length for the last chunk */
1323 if (chunk >= lt->full_chunk_cnt) {
1324 data_len = lt->last_data_bytes;
1325 spare_len = lt->last_spare_bytes;
1326 }
1327
1328 /* Read the chunk and detect number of bitflips */
1329 marvell_nfc_hw_ecc_bch_read_chunk(chip, chunk, data, data_len,
1330 spare, spare_len, page);
1331 ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
1332 if (ret)
1333 failure_mask |= BIT(chunk);
1334
1335 data += data_len;
1336 spare += spare_len;
1337 }
1338
1339 marvell_nfc_disable_hw_ecc(chip);
1340
1341 if (!failure_mask)
1342 return max_bitflips;
1343
1344 /*
1345 * Please note that dumping the ECC bytes during a normal read with OOB
1346 * area would add a significant overhead as ECC bytes are "consumed" by
1347 * the controller in normal mode and must be re-read in raw mode. To
1348 * avoid dropping the performances, we prefer not to include them. The
1349 * user should re-read the page in raw mode if ECC bytes are required.
Miquel Raynaldbfc6712018-07-19 16:54:23 +02001350 */
1351
1352 /*
1353 * In case there is any subpage read error reported by ->correct(), we
1354 * usually re-read only ECC bytes in raw mode and check if the whole
1355 * page is empty. In this case, it is normal that the ECC check failed
1356 * and we just ignore the error.
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001357 *
Miquel Raynal7fd130f2018-07-19 12:21:19 +02001358 * However, it has been empirically observed that for some layouts (e.g
1359 * 2k page, 8b strength per 512B chunk), the controller tries to correct
1360 * bits and may create itself bitflips in the erased area. To overcome
1361 * this strange behavior, the whole page is re-read in raw mode, not
1362 * only the ECC bytes.
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001363 */
1364 for (chunk = 0; chunk < lt->nchunks; chunk++) {
Miquel Raynaldbfc6712018-07-19 16:54:23 +02001365 int data_off_in_page, spare_off_in_page, ecc_off_in_page;
1366 int data_off, spare_off, ecc_off;
1367 int data_len, spare_len, ecc_len;
1368
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001369 /* No failure reported for this chunk, move to the next one */
1370 if (!(failure_mask & BIT(chunk)))
1371 continue;
1372
Miquel Raynaldbfc6712018-07-19 16:54:23 +02001373 data_off_in_page = chunk * (lt->data_bytes + lt->spare_bytes +
1374 lt->ecc_bytes);
1375 spare_off_in_page = data_off_in_page +
1376 (chunk < lt->full_chunk_cnt ? lt->data_bytes :
1377 lt->last_data_bytes);
1378 ecc_off_in_page = spare_off_in_page +
1379 (chunk < lt->full_chunk_cnt ? lt->spare_bytes :
1380 lt->last_spare_bytes);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001381
Miquel Raynaldbfc6712018-07-19 16:54:23 +02001382 data_off = chunk * lt->data_bytes;
1383 spare_off = chunk * lt->spare_bytes;
1384 ecc_off = (lt->full_chunk_cnt * lt->spare_bytes) +
1385 lt->last_spare_bytes +
1386 (chunk * (lt->ecc_bytes + 2));
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001387
Miquel Raynaldbfc6712018-07-19 16:54:23 +02001388 data_len = chunk < lt->full_chunk_cnt ? lt->data_bytes :
1389 lt->last_data_bytes;
1390 spare_len = chunk < lt->full_chunk_cnt ? lt->spare_bytes :
1391 lt->last_spare_bytes;
1392 ecc_len = chunk < lt->full_chunk_cnt ? lt->ecc_bytes :
1393 lt->last_ecc_bytes;
1394
Miquel Raynal7fd130f2018-07-19 12:21:19 +02001395 /*
1396 * Only re-read the ECC bytes, unless we are using the 2k/8b
1397 * layout which is buggy in the sense that the ECC engine will
1398 * try to correct data bytes anyway, creating bitflips. In this
1399 * case, re-read the entire page.
1400 */
1401 if (lt->writesize == 2048 && lt->strength == 8) {
1402 nand_change_read_column_op(chip, data_off_in_page,
1403 buf + data_off, data_len,
1404 false);
1405 nand_change_read_column_op(chip, spare_off_in_page,
1406 chip->oob_poi + spare_off, spare_len,
1407 false);
1408 }
1409
Miquel Raynaldbfc6712018-07-19 16:54:23 +02001410 nand_change_read_column_op(chip, ecc_off_in_page,
1411 chip->oob_poi + ecc_off, ecc_len,
1412 false);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001413
1414 /* Check the entire chunk (data + spare + ecc) for emptyness */
Miquel Raynaldbfc6712018-07-19 16:54:23 +02001415 marvell_nfc_check_empty_chunk(chip, buf + data_off, data_len,
1416 chip->oob_poi + spare_off, spare_len,
1417 chip->oob_poi + ecc_off, ecc_len,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001418 &max_bitflips);
1419 }
1420
1421 return max_bitflips;
1422}
1423
Boris Brezillonb9761682018-09-06 14:05:20 +02001424static int marvell_nfc_hw_ecc_bch_read_oob_raw(struct nand_chip *chip, int page)
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001425{
1426 /* Invalidate page cache */
1427 chip->pagebuf = -1;
1428
Boris Brezillonb9761682018-09-06 14:05:20 +02001429 return chip->ecc.read_page_raw(chip, chip->data_buf, true, page);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001430}
1431
Boris Brezillonb9761682018-09-06 14:05:20 +02001432static int marvell_nfc_hw_ecc_bch_read_oob(struct nand_chip *chip, int page)
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001433{
1434 /* Invalidate page cache */
1435 chip->pagebuf = -1;
1436
Boris Brezillonb9761682018-09-06 14:05:20 +02001437 return chip->ecc.read_page(chip, chip->data_buf, true, page);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001438}
1439
1440/* BCH write helpers */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001441static int marvell_nfc_hw_ecc_bch_write_page_raw(struct nand_chip *chip,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001442 const u8 *buf,
1443 int oob_required, int page)
1444{
1445 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1446 int full_chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1447 int data_len = lt->data_bytes;
1448 int spare_len = lt->spare_bytes;
1449 int ecc_len = lt->ecc_bytes;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001450 int spare_offset = 0;
1451 int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1452 lt->last_spare_bytes;
1453 int chunk;
1454
Boris Brezillonb2525142018-11-11 08:55:18 +01001455 marvell_nfc_select_target(chip, chip->cur_cs);
1456
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001457 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1458
1459 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1460 if (chunk >= lt->full_chunk_cnt) {
1461 data_len = lt->last_data_bytes;
1462 spare_len = lt->last_spare_bytes;
1463 ecc_len = lt->last_ecc_bytes;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001464 }
1465
1466 /* Point to the column of the next chunk */
1467 nand_change_write_column_op(chip, chunk * full_chunk_size,
1468 NULL, 0, false);
1469
1470 /* Write the data */
1471 nand_write_data_op(chip, buf + (chunk * lt->data_bytes),
1472 data_len, false);
1473
1474 if (!oob_required)
1475 continue;
1476
1477 /* Write the spare bytes */
1478 if (spare_len)
1479 nand_write_data_op(chip, chip->oob_poi + spare_offset,
1480 spare_len, false);
1481
1482 /* Write the ECC bytes */
1483 if (ecc_len)
1484 nand_write_data_op(chip, chip->oob_poi + ecc_offset,
1485 ecc_len, false);
1486
1487 spare_offset += spare_len;
1488 ecc_offset += ALIGN(ecc_len, 32);
1489 }
1490
1491 return nand_prog_page_end_op(chip);
1492}
1493
1494static int
1495marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip *chip, int chunk,
1496 const u8 *data, unsigned int data_len,
1497 const u8 *spare, unsigned int spare_len,
1498 int page)
1499{
1500 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1501 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1502 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
Miquel Raynala2ee41f2018-05-03 12:00:27 +02001503 u32 xtype;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001504 int ret;
1505 struct marvell_nfc_op nfc_op = {
1506 .ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | NDCB0_LEN_OVRD,
1507 .ndcb[3] = data_len + spare_len,
1508 };
1509
1510 /*
1511 * First operation dispatches the CMD_SEQIN command, issue the address
1512 * cycles and asks for the first chunk of data.
1513 * All operations in the middle (if any) will issue a naked write and
1514 * also ask for data.
1515 * Last operation (if any) asks for the last chunk of data through a
1516 * last naked write.
1517 */
1518 if (chunk == 0) {
Miquel Raynala2ee41f2018-05-03 12:00:27 +02001519 if (lt->nchunks == 1)
1520 xtype = XTYPE_MONOLITHIC_RW;
1521 else
1522 xtype = XTYPE_WRITE_DISPATCH;
1523
1524 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(xtype) |
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001525 NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1526 NDCB0_CMD1(NAND_CMD_SEQIN);
1527 nfc_op.ndcb[1] |= NDCB1_ADDRS_PAGE(page);
1528 nfc_op.ndcb[2] |= NDCB2_ADDR5_PAGE(page);
1529 } else if (chunk < lt->nchunks - 1) {
1530 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
1531 } else {
1532 nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1533 }
1534
1535 /* Always dispatch the PAGEPROG command on the last chunk */
1536 if (chunk == lt->nchunks - 1)
1537 nfc_op.ndcb[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG) | NDCB0_DBC;
1538
1539 ret = marvell_nfc_prepare_cmd(chip);
1540 if (ret)
1541 return ret;
1542
1543 marvell_nfc_send_cmd(chip, &nfc_op);
1544 ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1545 "WRDREQ while loading FIFO (data)");
1546 if (ret)
1547 return ret;
1548
1549 /* Transfer the contents */
1550 iowrite32_rep(nfc->regs + NDDB, data, FIFO_REP(data_len));
1551 iowrite32_rep(nfc->regs + NDDB, spare, FIFO_REP(spare_len));
1552
1553 return 0;
1554}
1555
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001556static int marvell_nfc_hw_ecc_bch_write_page(struct nand_chip *chip,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001557 const u8 *buf,
1558 int oob_required, int page)
1559{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001560 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001561 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1562 const u8 *data = buf;
1563 const u8 *spare = chip->oob_poi;
1564 int data_len = lt->data_bytes;
1565 int spare_len = lt->spare_bytes;
1566 int chunk, ret;
1567
Boris Brezillonb2525142018-11-11 08:55:18 +01001568 marvell_nfc_select_target(chip, chip->cur_cs);
1569
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001570 /* Spare data will be written anyway, so clear it to avoid garbage */
1571 if (!oob_required)
1572 memset(chip->oob_poi, 0xFF, mtd->oobsize);
1573
1574 marvell_nfc_enable_hw_ecc(chip);
1575
1576 for (chunk = 0; chunk < lt->nchunks; chunk++) {
1577 if (chunk >= lt->full_chunk_cnt) {
1578 data_len = lt->last_data_bytes;
1579 spare_len = lt->last_spare_bytes;
1580 }
1581
1582 marvell_nfc_hw_ecc_bch_write_chunk(chip, chunk, data, data_len,
1583 spare, spare_len, page);
1584 data += data_len;
1585 spare += spare_len;
1586
1587 /*
1588 * Waiting only for CMDD or PAGED is not enough, ECC are
1589 * partially written. No flag is set once the operation is
1590 * really finished but the ND_RUN bit is cleared, so wait for it
1591 * before stepping into the next command.
1592 */
1593 marvell_nfc_wait_ndrun(chip);
1594 }
1595
1596 ret = marvell_nfc_wait_op(chip,
Chris Packhamb76401f2018-05-03 14:21:28 +12001597 PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max));
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001598
1599 marvell_nfc_disable_hw_ecc(chip);
1600
1601 if (ret)
1602 return ret;
1603
1604 return 0;
1605}
1606
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001607static int marvell_nfc_hw_ecc_bch_write_oob_raw(struct nand_chip *chip,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001608 int page)
1609{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001610 struct mtd_info *mtd = nand_to_mtd(chip);
1611
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001612 /* Invalidate page cache */
1613 chip->pagebuf = -1;
1614
1615 memset(chip->data_buf, 0xFF, mtd->writesize);
1616
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001617 return chip->ecc.write_page_raw(chip, chip->data_buf, true, page);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001618}
1619
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001620static int marvell_nfc_hw_ecc_bch_write_oob(struct nand_chip *chip, int page)
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001621{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001622 struct mtd_info *mtd = nand_to_mtd(chip);
1623
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001624 /* Invalidate page cache */
1625 chip->pagebuf = -1;
1626
1627 memset(chip->data_buf, 0xFF, mtd->writesize);
1628
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001629 return chip->ecc.write_page(chip, chip->data_buf, true, page);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001630}
1631
1632/* NAND framework ->exec_op() hooks and related helpers */
1633static void marvell_nfc_parse_instructions(struct nand_chip *chip,
1634 const struct nand_subop *subop,
1635 struct marvell_nfc_op *nfc_op)
1636{
1637 const struct nand_op_instr *instr = NULL;
1638 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1639 bool first_cmd = true;
1640 unsigned int op_id;
1641 int i;
1642
1643 /* Reset the input structure as most of its fields will be OR'ed */
1644 memset(nfc_op, 0, sizeof(struct marvell_nfc_op));
1645
1646 for (op_id = 0; op_id < subop->ninstrs; op_id++) {
1647 unsigned int offset, naddrs;
1648 const u8 *addrs;
Miquel Raynal21a26802018-09-07 16:29:54 +02001649 int len;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001650
1651 instr = &subop->instrs[op_id];
1652
1653 switch (instr->type) {
1654 case NAND_OP_CMD_INSTR:
1655 if (first_cmd)
1656 nfc_op->ndcb[0] |=
1657 NDCB0_CMD1(instr->ctx.cmd.opcode);
1658 else
1659 nfc_op->ndcb[0] |=
1660 NDCB0_CMD2(instr->ctx.cmd.opcode) |
1661 NDCB0_DBC;
1662
1663 nfc_op->cle_ale_delay_ns = instr->delay_ns;
1664 first_cmd = false;
1665 break;
1666
1667 case NAND_OP_ADDR_INSTR:
1668 offset = nand_subop_get_addr_start_off(subop, op_id);
1669 naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
1670 addrs = &instr->ctx.addr.addrs[offset];
1671
1672 nfc_op->ndcb[0] |= NDCB0_ADDR_CYC(naddrs);
1673
1674 for (i = 0; i < min_t(unsigned int, 4, naddrs); i++)
1675 nfc_op->ndcb[1] |= addrs[i] << (8 * i);
1676
1677 if (naddrs >= 5)
1678 nfc_op->ndcb[2] |= NDCB2_ADDR5_CYC(addrs[4]);
1679 if (naddrs >= 6)
1680 nfc_op->ndcb[3] |= NDCB3_ADDR6_CYC(addrs[5]);
1681 if (naddrs == 7)
1682 nfc_op->ndcb[3] |= NDCB3_ADDR7_CYC(addrs[6]);
1683
1684 nfc_op->cle_ale_delay_ns = instr->delay_ns;
1685 break;
1686
1687 case NAND_OP_DATA_IN_INSTR:
1688 nfc_op->data_instr = instr;
1689 nfc_op->data_instr_idx = op_id;
1690 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ);
1691 if (nfc->caps->is_nfcv2) {
1692 nfc_op->ndcb[0] |=
1693 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1694 NDCB0_LEN_OVRD;
Miquel Raynal21a26802018-09-07 16:29:54 +02001695 len = nand_subop_get_data_len(subop, op_id);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001696 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1697 }
1698 nfc_op->data_delay_ns = instr->delay_ns;
1699 break;
1700
1701 case NAND_OP_DATA_OUT_INSTR:
1702 nfc_op->data_instr = instr;
1703 nfc_op->data_instr_idx = op_id;
1704 nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE);
1705 if (nfc->caps->is_nfcv2) {
1706 nfc_op->ndcb[0] |=
1707 NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1708 NDCB0_LEN_OVRD;
Miquel Raynal21a26802018-09-07 16:29:54 +02001709 len = nand_subop_get_data_len(subop, op_id);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01001710 nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1711 }
1712 nfc_op->data_delay_ns = instr->delay_ns;
1713 break;
1714
1715 case NAND_OP_WAITRDY_INSTR:
1716 nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
1717 nfc_op->rdy_delay_ns = instr->delay_ns;
1718 break;
1719 }
1720 }
1721}
1722
1723static int marvell_nfc_xfer_data_pio(struct nand_chip *chip,
1724 const struct nand_subop *subop,
1725 struct marvell_nfc_op *nfc_op)
1726{
1727 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1728 const struct nand_op_instr *instr = nfc_op->data_instr;
1729 unsigned int op_id = nfc_op->data_instr_idx;
1730 unsigned int len = nand_subop_get_data_len(subop, op_id);
1731 unsigned int offset = nand_subop_get_data_start_off(subop, op_id);
1732 bool reading = (instr->type == NAND_OP_DATA_IN_INSTR);
1733 int ret;
1734
1735 if (instr->ctx.data.force_8bit)
1736 marvell_nfc_force_byte_access(chip, true);
1737
1738 if (reading) {
1739 u8 *in = instr->ctx.data.buf.in + offset;
1740
1741 ret = marvell_nfc_xfer_data_in_pio(nfc, in, len);
1742 } else {
1743 const u8 *out = instr->ctx.data.buf.out + offset;
1744
1745 ret = marvell_nfc_xfer_data_out_pio(nfc, out, len);
1746 }
1747
1748 if (instr->ctx.data.force_8bit)
1749 marvell_nfc_force_byte_access(chip, false);
1750
1751 return ret;
1752}
1753
1754static int marvell_nfc_monolithic_access_exec(struct nand_chip *chip,
1755 const struct nand_subop *subop)
1756{
1757 struct marvell_nfc_op nfc_op;
1758 bool reading;
1759 int ret;
1760
1761 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1762 reading = (nfc_op.data_instr->type == NAND_OP_DATA_IN_INSTR);
1763
1764 ret = marvell_nfc_prepare_cmd(chip);
1765 if (ret)
1766 return ret;
1767
1768 marvell_nfc_send_cmd(chip, &nfc_op);
1769 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1770 "RDDREQ/WRDREQ while draining raw data");
1771 if (ret)
1772 return ret;
1773
1774 cond_delay(nfc_op.cle_ale_delay_ns);
1775
1776 if (reading) {
1777 if (nfc_op.rdy_timeout_ms) {
1778 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1779 if (ret)
1780 return ret;
1781 }
1782
1783 cond_delay(nfc_op.rdy_delay_ns);
1784 }
1785
1786 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1787 ret = marvell_nfc_wait_cmdd(chip);
1788 if (ret)
1789 return ret;
1790
1791 cond_delay(nfc_op.data_delay_ns);
1792
1793 if (!reading) {
1794 if (nfc_op.rdy_timeout_ms) {
1795 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1796 if (ret)
1797 return ret;
1798 }
1799
1800 cond_delay(nfc_op.rdy_delay_ns);
1801 }
1802
1803 /*
1804 * NDCR ND_RUN bit should be cleared automatically at the end of each
1805 * operation but experience shows that the behavior is buggy when it
1806 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1807 */
1808 if (!reading) {
1809 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1810
1811 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1812 nfc->regs + NDCR);
1813 }
1814
1815 return 0;
1816}
1817
1818static int marvell_nfc_naked_access_exec(struct nand_chip *chip,
1819 const struct nand_subop *subop)
1820{
1821 struct marvell_nfc_op nfc_op;
1822 int ret;
1823
1824 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1825
1826 /*
1827 * Naked access are different in that they need to be flagged as naked
1828 * by the controller. Reset the controller registers fields that inform
1829 * on the type and refill them according to the ongoing operation.
1830 */
1831 nfc_op.ndcb[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK) |
1832 NDCB0_CMD_XTYPE(XTYPE_MASK));
1833 switch (subop->instrs[0].type) {
1834 case NAND_OP_CMD_INSTR:
1835 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD);
1836 break;
1837 case NAND_OP_ADDR_INSTR:
1838 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR);
1839 break;
1840 case NAND_OP_DATA_IN_INSTR:
1841 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ) |
1842 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1843 break;
1844 case NAND_OP_DATA_OUT_INSTR:
1845 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE) |
1846 NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1847 break;
1848 default:
1849 /* This should never happen */
1850 break;
1851 }
1852
1853 ret = marvell_nfc_prepare_cmd(chip);
1854 if (ret)
1855 return ret;
1856
1857 marvell_nfc_send_cmd(chip, &nfc_op);
1858
1859 if (!nfc_op.data_instr) {
1860 ret = marvell_nfc_wait_cmdd(chip);
1861 cond_delay(nfc_op.cle_ale_delay_ns);
1862 return ret;
1863 }
1864
1865 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1866 "RDDREQ/WRDREQ while draining raw data");
1867 if (ret)
1868 return ret;
1869
1870 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1871 ret = marvell_nfc_wait_cmdd(chip);
1872 if (ret)
1873 return ret;
1874
1875 /*
1876 * NDCR ND_RUN bit should be cleared automatically at the end of each
1877 * operation but experience shows that the behavior is buggy when it
1878 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1879 */
1880 if (subop->instrs[0].type == NAND_OP_DATA_OUT_INSTR) {
1881 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1882
1883 writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1884 nfc->regs + NDCR);
1885 }
1886
1887 return 0;
1888}
1889
1890static int marvell_nfc_naked_waitrdy_exec(struct nand_chip *chip,
1891 const struct nand_subop *subop)
1892{
1893 struct marvell_nfc_op nfc_op;
1894 int ret;
1895
1896 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1897
1898 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1899 cond_delay(nfc_op.rdy_delay_ns);
1900
1901 return ret;
1902}
1903
1904static int marvell_nfc_read_id_type_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_READ_ID);
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 ID");
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_read_status_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_READ);
1952 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_STATUS);
1953
1954 ret = marvell_nfc_prepare_cmd(chip);
1955 if (ret)
1956 return ret;
1957
1958 marvell_nfc_send_cmd(chip, &nfc_op);
1959 ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1960 "RDDREQ while reading status");
1961 if (ret)
1962 return ret;
1963
1964 cond_delay(nfc_op.cle_ale_delay_ns);
1965
1966 if (nfc_op.rdy_timeout_ms) {
1967 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1968 if (ret)
1969 return ret;
1970 }
1971
1972 cond_delay(nfc_op.rdy_delay_ns);
1973
1974 marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1975 ret = marvell_nfc_wait_cmdd(chip);
1976 if (ret)
1977 return ret;
1978
1979 cond_delay(nfc_op.data_delay_ns);
1980
1981 return 0;
1982}
1983
1984static int marvell_nfc_reset_cmd_type_exec(struct nand_chip *chip,
1985 const struct nand_subop *subop)
1986{
1987 struct marvell_nfc_op nfc_op;
1988 int ret;
1989
1990 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1991 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_RESET);
1992
1993 ret = marvell_nfc_prepare_cmd(chip);
1994 if (ret)
1995 return ret;
1996
1997 marvell_nfc_send_cmd(chip, &nfc_op);
1998 ret = marvell_nfc_wait_cmdd(chip);
1999 if (ret)
2000 return ret;
2001
2002 cond_delay(nfc_op.cle_ale_delay_ns);
2003
2004 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
2005 if (ret)
2006 return ret;
2007
2008 cond_delay(nfc_op.rdy_delay_ns);
2009
2010 return 0;
2011}
2012
2013static int marvell_nfc_erase_cmd_type_exec(struct nand_chip *chip,
2014 const struct nand_subop *subop)
2015{
2016 struct marvell_nfc_op nfc_op;
2017 int ret;
2018
2019 marvell_nfc_parse_instructions(chip, subop, &nfc_op);
2020 nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_ERASE);
2021
2022 ret = marvell_nfc_prepare_cmd(chip);
2023 if (ret)
2024 return ret;
2025
2026 marvell_nfc_send_cmd(chip, &nfc_op);
2027 ret = marvell_nfc_wait_cmdd(chip);
2028 if (ret)
2029 return ret;
2030
2031 cond_delay(nfc_op.cle_ale_delay_ns);
2032
2033 ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
2034 if (ret)
2035 return ret;
2036
2037 cond_delay(nfc_op.rdy_delay_ns);
2038
2039 return 0;
2040}
2041
2042static const struct nand_op_parser marvell_nfcv2_op_parser = NAND_OP_PARSER(
2043 /* Monolithic reads/writes */
2044 NAND_OP_PARSER_PATTERN(
2045 marvell_nfc_monolithic_access_exec,
2046 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2047 NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2),
2048 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2049 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
2050 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
2051 NAND_OP_PARSER_PATTERN(
2052 marvell_nfc_monolithic_access_exec,
2053 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2054 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2),
2055 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE),
2056 NAND_OP_PARSER_PAT_CMD_ELEM(true),
2057 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
2058 /* Naked commands */
2059 NAND_OP_PARSER_PATTERN(
2060 marvell_nfc_naked_access_exec,
2061 NAND_OP_PARSER_PAT_CMD_ELEM(false)),
2062 NAND_OP_PARSER_PATTERN(
2063 marvell_nfc_naked_access_exec,
2064 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2)),
2065 NAND_OP_PARSER_PATTERN(
2066 marvell_nfc_naked_access_exec,
2067 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
2068 NAND_OP_PARSER_PATTERN(
2069 marvell_nfc_naked_access_exec,
2070 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE)),
2071 NAND_OP_PARSER_PATTERN(
2072 marvell_nfc_naked_waitrdy_exec,
2073 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2074 );
2075
2076static const struct nand_op_parser marvell_nfcv1_op_parser = NAND_OP_PARSER(
2077 /* Naked commands not supported, use a function for each pattern */
2078 NAND_OP_PARSER_PATTERN(
2079 marvell_nfc_read_id_type_exec,
2080 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2081 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
2082 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 8)),
2083 NAND_OP_PARSER_PATTERN(
2084 marvell_nfc_erase_cmd_type_exec,
2085 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2086 NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
2087 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2088 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2089 NAND_OP_PARSER_PATTERN(
2090 marvell_nfc_read_status_exec,
2091 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2092 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)),
2093 NAND_OP_PARSER_PATTERN(
2094 marvell_nfc_reset_cmd_type_exec,
2095 NAND_OP_PARSER_PAT_CMD_ELEM(false),
2096 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2097 NAND_OP_PARSER_PATTERN(
2098 marvell_nfc_naked_waitrdy_exec,
2099 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2100 );
2101
2102static int marvell_nfc_exec_op(struct nand_chip *chip,
2103 const struct nand_operation *op,
2104 bool check_only)
2105{
2106 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2107
Boris Brezillonb2525142018-11-11 08:55:18 +01002108 marvell_nfc_select_target(chip, op->cs);
2109
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002110 if (nfc->caps->is_nfcv2)
2111 return nand_op_parser_exec_op(chip, &marvell_nfcv2_op_parser,
2112 op, check_only);
2113 else
2114 return nand_op_parser_exec_op(chip, &marvell_nfcv1_op_parser,
2115 op, check_only);
2116}
2117
2118/*
2119 * Layouts were broken in old pxa3xx_nand driver, these are supposed to be
2120 * usable.
2121 */
2122static int marvell_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2123 struct mtd_oob_region *oobregion)
2124{
2125 struct nand_chip *chip = mtd_to_nand(mtd);
2126 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2127
2128 if (section)
2129 return -ERANGE;
2130
2131 oobregion->length = (lt->full_chunk_cnt * lt->ecc_bytes) +
2132 lt->last_ecc_bytes;
2133 oobregion->offset = mtd->oobsize - oobregion->length;
2134
2135 return 0;
2136}
2137
2138static int marvell_nand_ooblayout_free(struct mtd_info *mtd, int section,
2139 struct mtd_oob_region *oobregion)
2140{
2141 struct nand_chip *chip = mtd_to_nand(mtd);
2142 const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2143
2144 if (section)
2145 return -ERANGE;
2146
2147 /*
2148 * Bootrom looks in bytes 0 & 5 for bad blocks for the
2149 * 4KB page / 4bit BCH combination.
2150 */
2151 if (mtd->writesize == SZ_4K && lt->data_bytes == SZ_2K)
2152 oobregion->offset = 6;
2153 else
2154 oobregion->offset = 2;
2155
2156 oobregion->length = (lt->full_chunk_cnt * lt->spare_bytes) +
2157 lt->last_spare_bytes - oobregion->offset;
2158
2159 return 0;
2160}
2161
2162static const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops = {
2163 .ecc = marvell_nand_ooblayout_ecc,
2164 .free = marvell_nand_ooblayout_free,
2165};
2166
2167static int marvell_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
2168 struct nand_ecc_ctrl *ecc)
2169{
2170 struct nand_chip *chip = mtd_to_nand(mtd);
2171 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2172 const struct marvell_hw_ecc_layout *l;
2173 int i;
2174
2175 if (!nfc->caps->is_nfcv2 &&
2176 (mtd->writesize + mtd->oobsize > MAX_CHUNK_SIZE)) {
2177 dev_err(nfc->dev,
2178 "NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n",
2179 mtd->writesize, MAX_CHUNK_SIZE - mtd->oobsize);
2180 return -ENOTSUPP;
2181 }
2182
2183 to_marvell_nand(chip)->layout = NULL;
2184 for (i = 0; i < ARRAY_SIZE(marvell_nfc_layouts); i++) {
2185 l = &marvell_nfc_layouts[i];
2186 if (mtd->writesize == l->writesize &&
2187 ecc->size == l->chunk && ecc->strength == l->strength) {
2188 to_marvell_nand(chip)->layout = l;
2189 break;
2190 }
2191 }
2192
2193 if (!to_marvell_nand(chip)->layout ||
2194 (!nfc->caps->is_nfcv2 && ecc->strength > 1)) {
2195 dev_err(nfc->dev,
2196 "ECC strength %d at page size %d is not supported\n",
2197 ecc->strength, mtd->writesize);
2198 return -ENOTSUPP;
2199 }
2200
Miquel Raynal7fd130f2018-07-19 12:21:19 +02002201 /* Special care for the layout 2k/8-bit/512B */
2202 if (l->writesize == 2048 && l->strength == 8) {
2203 if (mtd->oobsize < 128) {
2204 dev_err(nfc->dev, "Requested layout needs at least 128 OOB bytes\n");
2205 return -ENOTSUPP;
2206 } else {
2207 chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
2208 }
2209 }
2210
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002211 mtd_set_ooblayout(mtd, &marvell_nand_ooblayout_ops);
2212 ecc->steps = l->nchunks;
2213 ecc->size = l->data_bytes;
2214
2215 if (ecc->strength == 1) {
2216 chip->ecc.algo = NAND_ECC_HAMMING;
2217 ecc->read_page_raw = marvell_nfc_hw_ecc_hmg_read_page_raw;
2218 ecc->read_page = marvell_nfc_hw_ecc_hmg_read_page;
2219 ecc->read_oob_raw = marvell_nfc_hw_ecc_hmg_read_oob_raw;
2220 ecc->read_oob = ecc->read_oob_raw;
2221 ecc->write_page_raw = marvell_nfc_hw_ecc_hmg_write_page_raw;
2222 ecc->write_page = marvell_nfc_hw_ecc_hmg_write_page;
2223 ecc->write_oob_raw = marvell_nfc_hw_ecc_hmg_write_oob_raw;
2224 ecc->write_oob = ecc->write_oob_raw;
2225 } else {
2226 chip->ecc.algo = NAND_ECC_BCH;
2227 ecc->strength = 16;
2228 ecc->read_page_raw = marvell_nfc_hw_ecc_bch_read_page_raw;
2229 ecc->read_page = marvell_nfc_hw_ecc_bch_read_page;
2230 ecc->read_oob_raw = marvell_nfc_hw_ecc_bch_read_oob_raw;
2231 ecc->read_oob = marvell_nfc_hw_ecc_bch_read_oob;
2232 ecc->write_page_raw = marvell_nfc_hw_ecc_bch_write_page_raw;
2233 ecc->write_page = marvell_nfc_hw_ecc_bch_write_page;
2234 ecc->write_oob_raw = marvell_nfc_hw_ecc_bch_write_oob_raw;
2235 ecc->write_oob = marvell_nfc_hw_ecc_bch_write_oob;
2236 }
2237
2238 return 0;
2239}
2240
2241static int marvell_nand_ecc_init(struct mtd_info *mtd,
2242 struct nand_ecc_ctrl *ecc)
2243{
2244 struct nand_chip *chip = mtd_to_nand(mtd);
2245 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2246 int ret;
2247
2248 if (ecc->mode != NAND_ECC_NONE && (!ecc->size || !ecc->strength)) {
2249 if (chip->ecc_step_ds && chip->ecc_strength_ds) {
2250 ecc->size = chip->ecc_step_ds;
2251 ecc->strength = chip->ecc_strength_ds;
2252 } else {
2253 dev_info(nfc->dev,
2254 "No minimum ECC strength, using 1b/512B\n");
2255 ecc->size = 512;
2256 ecc->strength = 1;
2257 }
2258 }
2259
2260 switch (ecc->mode) {
2261 case NAND_ECC_HW:
2262 ret = marvell_nand_hw_ecc_ctrl_init(mtd, ecc);
2263 if (ret)
2264 return ret;
2265 break;
2266 case NAND_ECC_NONE:
2267 case NAND_ECC_SOFT:
Chris Packhamed6d0282018-06-25 10:44:43 +12002268 case NAND_ECC_ON_DIE:
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002269 if (!nfc->caps->is_nfcv2 && mtd->writesize != SZ_512 &&
2270 mtd->writesize != SZ_2K) {
2271 dev_err(nfc->dev, "NFCv1 cannot write %d bytes pages\n",
2272 mtd->writesize);
2273 return -EINVAL;
2274 }
2275 break;
2276 default:
2277 return -EINVAL;
2278 }
2279
2280 return 0;
2281}
2282
2283static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
2284static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
2285
2286static struct nand_bbt_descr bbt_main_descr = {
2287 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2288 NAND_BBT_2BIT | NAND_BBT_VERSION,
2289 .offs = 8,
2290 .len = 6,
2291 .veroffs = 14,
2292 .maxblocks = 8, /* Last 8 blocks in each chip */
2293 .pattern = bbt_pattern
2294};
2295
2296static struct nand_bbt_descr bbt_mirror_descr = {
2297 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2298 NAND_BBT_2BIT | NAND_BBT_VERSION,
2299 .offs = 8,
2300 .len = 6,
2301 .veroffs = 14,
2302 .maxblocks = 8, /* Last 8 blocks in each chip */
2303 .pattern = bbt_mirror_pattern
2304};
2305
Boris Brezillon858838b2018-09-06 14:05:33 +02002306static int marvell_nfc_setup_data_interface(struct nand_chip *chip, int chipnr,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002307 const struct nand_data_interface
2308 *conf)
2309{
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002310 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2311 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
Boris Brezillon6b6de652018-03-26 11:53:01 +02002312 unsigned int period_ns = 1000000000 / clk_get_rate(nfc->core_clk) * 2;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002313 const struct nand_sdr_timings *sdr;
2314 struct marvell_nfc_timings nfc_tmg;
2315 int read_delay;
2316
2317 sdr = nand_get_sdr_timings(conf);
2318 if (IS_ERR(sdr))
2319 return PTR_ERR(sdr);
2320
2321 /*
2322 * SDR timings are given in pico-seconds while NFC timings must be
2323 * expressed in NAND controller clock cycles, which is half of the
2324 * frequency of the accessible ECC clock retrieved by clk_get_rate().
2325 * This is not written anywhere in the datasheet but was observed
2326 * with an oscilloscope.
2327 *
2328 * NFC datasheet gives equations from which thoses calculations
2329 * are derived, they tend to be slightly more restrictives than the
2330 * given core timings and may improve the overall speed.
2331 */
2332 nfc_tmg.tRP = TO_CYCLES(DIV_ROUND_UP(sdr->tRC_min, 2), period_ns) - 1;
2333 nfc_tmg.tRH = nfc_tmg.tRP;
2334 nfc_tmg.tWP = TO_CYCLES(DIV_ROUND_UP(sdr->tWC_min, 2), period_ns) - 1;
2335 nfc_tmg.tWH = nfc_tmg.tWP;
2336 nfc_tmg.tCS = TO_CYCLES(sdr->tCS_min, period_ns);
2337 nfc_tmg.tCH = TO_CYCLES(sdr->tCH_min, period_ns) - 1;
2338 nfc_tmg.tADL = TO_CYCLES(sdr->tADL_min, period_ns);
2339 /*
2340 * Read delay is the time of propagation from SoC pins to NFC internal
2341 * logic. With non-EDO timings, this is MIN_RD_DEL_CNT clock cycles. In
2342 * EDO mode, an additional delay of tRH must be taken into account so
2343 * the data is sampled on the falling edge instead of the rising edge.
2344 */
2345 read_delay = sdr->tRC_min >= 30000 ?
2346 MIN_RD_DEL_CNT : MIN_RD_DEL_CNT + nfc_tmg.tRH;
2347
2348 nfc_tmg.tAR = TO_CYCLES(sdr->tAR_min, period_ns);
2349 /*
2350 * tWHR and tRHW are supposed to be read to write delays (and vice
2351 * versa) but in some cases, ie. when doing a change column, they must
2352 * be greater than that to be sure tCCS delay is respected.
2353 */
2354 nfc_tmg.tWHR = TO_CYCLES(max_t(int, sdr->tWHR_min, sdr->tCCS_min),
2355 period_ns) - 2,
2356 nfc_tmg.tRHW = TO_CYCLES(max_t(int, sdr->tRHW_min, sdr->tCCS_min),
2357 period_ns);
2358
Miquel Raynal07ad5a72018-01-17 00:19:34 +01002359 /*
2360 * NFCv2: Use WAIT_MODE (wait for RB line), do not rely only on delays.
2361 * NFCv1: No WAIT_MODE, tR must be maximal.
2362 */
2363 if (nfc->caps->is_nfcv2) {
2364 nfc_tmg.tR = TO_CYCLES(sdr->tWB_max, period_ns);
2365 } else {
2366 nfc_tmg.tR = TO_CYCLES64(sdr->tWB_max + sdr->tR_max,
2367 period_ns);
2368 if (nfc_tmg.tR + 3 > nfc_tmg.tCH)
2369 nfc_tmg.tR = nfc_tmg.tCH - 3;
2370 else
2371 nfc_tmg.tR = 0;
2372 }
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002373
2374 if (chipnr < 0)
2375 return 0;
2376
2377 marvell_nand->ndtr0 =
2378 NDTR0_TRP(nfc_tmg.tRP) |
2379 NDTR0_TRH(nfc_tmg.tRH) |
2380 NDTR0_ETRP(nfc_tmg.tRP) |
2381 NDTR0_TWP(nfc_tmg.tWP) |
2382 NDTR0_TWH(nfc_tmg.tWH) |
2383 NDTR0_TCS(nfc_tmg.tCS) |
Miquel Raynal07ad5a72018-01-17 00:19:34 +01002384 NDTR0_TCH(nfc_tmg.tCH);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002385
2386 marvell_nand->ndtr1 =
2387 NDTR1_TAR(nfc_tmg.tAR) |
2388 NDTR1_TWHR(nfc_tmg.tWHR) |
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002389 NDTR1_TR(nfc_tmg.tR);
2390
Miquel Raynal07ad5a72018-01-17 00:19:34 +01002391 if (nfc->caps->is_nfcv2) {
2392 marvell_nand->ndtr0 |=
2393 NDTR0_RD_CNT_DEL(read_delay) |
2394 NDTR0_SELCNTR |
2395 NDTR0_TADL(nfc_tmg.tADL);
2396
2397 marvell_nand->ndtr1 |=
2398 NDTR1_TRHW(nfc_tmg.tRHW) |
2399 NDTR1_WAIT_MODE;
2400 }
2401
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002402 return 0;
2403}
2404
Miquel Raynal8831e482018-07-20 17:15:05 +02002405static int marvell_nand_attach_chip(struct nand_chip *chip)
2406{
2407 struct mtd_info *mtd = nand_to_mtd(chip);
2408 struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2409 struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2410 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(nfc->dev);
2411 int ret;
2412
2413 if (pdata && pdata->flash_bbt)
2414 chip->bbt_options |= NAND_BBT_USE_FLASH;
2415
2416 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
2417 /*
2418 * We'll use a bad block table stored in-flash and don't
2419 * allow writing the bad block marker to the flash.
2420 */
2421 chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
2422 chip->bbt_td = &bbt_main_descr;
2423 chip->bbt_md = &bbt_mirror_descr;
2424 }
2425
2426 /* Save the chip-specific fields of NDCR */
2427 marvell_nand->ndcr = NDCR_PAGE_SZ(mtd->writesize);
2428 if (chip->options & NAND_BUSWIDTH_16)
2429 marvell_nand->ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
2430
2431 /*
2432 * On small page NANDs, only one cycle is needed to pass the
2433 * column address.
2434 */
2435 if (mtd->writesize <= 512) {
2436 marvell_nand->addr_cyc = 1;
2437 } else {
2438 marvell_nand->addr_cyc = 2;
2439 marvell_nand->ndcr |= NDCR_RA_START;
2440 }
2441
2442 /*
2443 * Now add the number of cycles needed to pass the row
2444 * address.
2445 *
2446 * Addressing a chip using CS 2 or 3 should also need the third row
2447 * cycle but due to inconsistance in the documentation and lack of
2448 * hardware to test this situation, this case is not supported.
2449 */
2450 if (chip->options & NAND_ROW_ADDR_3)
2451 marvell_nand->addr_cyc += 3;
2452 else
2453 marvell_nand->addr_cyc += 2;
2454
2455 if (pdata) {
2456 chip->ecc.size = pdata->ecc_step_size;
2457 chip->ecc.strength = pdata->ecc_strength;
2458 }
2459
2460 ret = marvell_nand_ecc_init(mtd, &chip->ecc);
2461 if (ret) {
2462 dev_err(nfc->dev, "ECC init failed: %d\n", ret);
2463 return ret;
2464 }
2465
2466 if (chip->ecc.mode == NAND_ECC_HW) {
2467 /*
2468 * Subpage write not available with hardware ECC, prohibit also
2469 * subpage read as in userspace subpage access would still be
2470 * allowed and subpage write, if used, would lead to numerous
2471 * uncorrectable ECC errors.
2472 */
2473 chip->options |= NAND_NO_SUBPAGE_WRITE;
2474 }
2475
2476 if (pdata || nfc->caps->legacy_of_bindings) {
2477 /*
2478 * We keep the MTD name unchanged to avoid breaking platforms
2479 * where the MTD cmdline parser is used and the bootloader
2480 * has not been updated to use the new naming scheme.
2481 */
2482 mtd->name = "pxa3xx_nand-0";
2483 } else if (!mtd->name) {
2484 /*
2485 * If the new bindings are used and the bootloader has not been
2486 * updated to pass a new mtdparts parameter on the cmdline, you
2487 * should define the following property in your NAND node, ie:
2488 *
2489 * label = "main-storage";
2490 *
2491 * This way, mtd->name will be set by the core when
2492 * nand_set_flash_node() is called.
2493 */
2494 mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL,
2495 "%s:nand.%d", dev_name(nfc->dev),
2496 marvell_nand->sels[0].cs);
2497 if (!mtd->name) {
2498 dev_err(nfc->dev, "Failed to allocate mtd->name\n");
2499 return -ENOMEM;
2500 }
2501 }
2502
2503 return 0;
2504}
2505
2506static const struct nand_controller_ops marvell_nand_controller_ops = {
2507 .attach_chip = marvell_nand_attach_chip,
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01002508 .exec_op = marvell_nfc_exec_op,
Boris Brezillon7a08dba2018-11-11 08:55:24 +01002509 .setup_data_interface = marvell_nfc_setup_data_interface,
Miquel Raynal8831e482018-07-20 17:15:05 +02002510};
2511
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002512static int marvell_nand_chip_init(struct device *dev, struct marvell_nfc *nfc,
2513 struct device_node *np)
2514{
2515 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(dev);
2516 struct marvell_nand_chip *marvell_nand;
2517 struct mtd_info *mtd;
2518 struct nand_chip *chip;
2519 int nsels, ret, i;
2520 u32 cs, rb;
2521
2522 /*
2523 * The legacy "num-cs" property indicates the number of CS on the only
2524 * chip connected to the controller (legacy bindings does not support
Miquel Raynalf6997be2018-04-25 16:16:32 +02002525 * more than one chip). The CS and RB pins are always the #0.
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002526 *
2527 * When not using legacy bindings, a couple of "reg" and "nand-rb"
2528 * properties must be filled. For each chip, expressed as a subnode,
2529 * "reg" points to the CS lines and "nand-rb" to the RB line.
2530 */
Miquel Raynalf6997be2018-04-25 16:16:32 +02002531 if (pdata || nfc->caps->legacy_of_bindings) {
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002532 nsels = 1;
Miquel Raynalf6997be2018-04-25 16:16:32 +02002533 } else {
2534 nsels = of_property_count_elems_of_size(np, "reg", sizeof(u32));
2535 if (nsels <= 0) {
2536 dev_err(dev, "missing/invalid reg property\n");
2537 return -EINVAL;
2538 }
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002539 }
2540
2541 /* Alloc the nand chip structure */
2542 marvell_nand = devm_kzalloc(dev, sizeof(*marvell_nand) +
2543 (nsels *
2544 sizeof(struct marvell_nand_chip_sel)),
2545 GFP_KERNEL);
2546 if (!marvell_nand) {
2547 dev_err(dev, "could not allocate chip structure\n");
2548 return -ENOMEM;
2549 }
2550
2551 marvell_nand->nsels = nsels;
2552 marvell_nand->selected_die = -1;
2553
2554 for (i = 0; i < nsels; i++) {
2555 if (pdata || nfc->caps->legacy_of_bindings) {
2556 /*
2557 * Legacy bindings use the CS lines in natural
2558 * order (0, 1, ...)
2559 */
2560 cs = i;
2561 } else {
2562 /* Retrieve CS id */
2563 ret = of_property_read_u32_index(np, "reg", i, &cs);
2564 if (ret) {
2565 dev_err(dev, "could not retrieve reg property: %d\n",
2566 ret);
2567 return ret;
2568 }
2569 }
2570
2571 if (cs >= nfc->caps->max_cs_nb) {
2572 dev_err(dev, "invalid reg value: %u (max CS = %d)\n",
2573 cs, nfc->caps->max_cs_nb);
2574 return -EINVAL;
2575 }
2576
2577 if (test_and_set_bit(cs, &nfc->assigned_cs)) {
2578 dev_err(dev, "CS %d already assigned\n", cs);
2579 return -EINVAL;
2580 }
2581
2582 /*
2583 * The cs variable represents the chip select id, which must be
2584 * converted in bit fields for NDCB0 and NDCB2 to select the
2585 * right chip. Unfortunately, due to a lack of information on
2586 * the subject and incoherent documentation, the user should not
2587 * use CS1 and CS3 at all as asserting them is not supported in
2588 * a reliable way (due to multiplexing inside ADDR5 field).
2589 */
2590 marvell_nand->sels[i].cs = cs;
2591 switch (cs) {
2592 case 0:
2593 case 2:
2594 marvell_nand->sels[i].ndcb0_csel = 0;
2595 break;
2596 case 1:
2597 case 3:
2598 marvell_nand->sels[i].ndcb0_csel = NDCB0_CSEL;
2599 break;
2600 default:
2601 return -EINVAL;
2602 }
2603
2604 /* Retrieve RB id */
2605 if (pdata || nfc->caps->legacy_of_bindings) {
2606 /* Legacy bindings always use RB #0 */
2607 rb = 0;
2608 } else {
2609 ret = of_property_read_u32_index(np, "nand-rb", i,
2610 &rb);
2611 if (ret) {
2612 dev_err(dev,
2613 "could not retrieve RB property: %d\n",
2614 ret);
2615 return ret;
2616 }
2617 }
2618
2619 if (rb >= nfc->caps->max_rb_nb) {
2620 dev_err(dev, "invalid reg value: %u (max RB = %d)\n",
2621 rb, nfc->caps->max_rb_nb);
2622 return -EINVAL;
2623 }
2624
2625 marvell_nand->sels[i].rb = rb;
2626 }
2627
2628 chip = &marvell_nand->chip;
2629 chip->controller = &nfc->controller;
2630 nand_set_flash_node(chip, np);
2631
Miquel Raynal07ad5a72018-01-17 00:19:34 +01002632 if (!of_property_read_bool(np, "marvell,nand-keep-config"))
Boris Brezillon7a08dba2018-11-11 08:55:24 +01002633 chip->options |= NAND_KEEP_TIMINGS;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002634
2635 mtd = nand_to_mtd(chip);
2636 mtd->dev.parent = dev;
2637
2638 /*
2639 * Default to HW ECC engine mode. If the nand-ecc-mode property is given
2640 * in the DT node, this entry will be overwritten in nand_scan_ident().
2641 */
2642 chip->ecc.mode = NAND_ECC_HW;
2643
2644 /*
2645 * Save a reference value for timing registers before
2646 * ->setup_data_interface() is called.
2647 */
2648 marvell_nand->ndtr0 = readl_relaxed(nfc->regs + NDTR0);
2649 marvell_nand->ndtr1 = readl_relaxed(nfc->regs + NDTR1);
2650
2651 chip->options |= NAND_BUSWIDTH_AUTO;
Miquel Raynal8831e482018-07-20 17:15:05 +02002652
Boris Brezillon00ad3782018-09-06 14:05:14 +02002653 ret = nand_scan(chip, marvell_nand->nsels);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002654 if (ret) {
Miquel Raynal8831e482018-07-20 17:15:05 +02002655 dev_err(dev, "could not scan the nand chip\n");
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002656 return ret;
2657 }
2658
2659 if (pdata)
2660 /* Legacy bindings support only one chip */
Miquel Raynal75765942018-02-19 23:35:54 +01002661 ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002662 else
2663 ret = mtd_device_register(mtd, NULL, 0);
2664 if (ret) {
2665 dev_err(dev, "failed to register mtd device: %d\n", ret);
Boris Brezillon59ac2762018-09-06 14:05:15 +02002666 nand_release(chip);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002667 return ret;
2668 }
2669
2670 list_add_tail(&marvell_nand->node, &nfc->chips);
2671
2672 return 0;
2673}
2674
2675static int marvell_nand_chips_init(struct device *dev, struct marvell_nfc *nfc)
2676{
2677 struct device_node *np = dev->of_node;
2678 struct device_node *nand_np;
2679 int max_cs = nfc->caps->max_cs_nb;
2680 int nchips;
2681 int ret;
2682
2683 if (!np)
2684 nchips = 1;
2685 else
2686 nchips = of_get_child_count(np);
2687
2688 if (nchips > max_cs) {
2689 dev_err(dev, "too many NAND chips: %d (max = %d CS)\n", nchips,
2690 max_cs);
2691 return -EINVAL;
2692 }
2693
2694 /*
2695 * Legacy bindings do not use child nodes to exhibit NAND chip
2696 * properties and layout. Instead, NAND properties are mixed with the
2697 * controller ones, and partitions are defined as direct subnodes of the
2698 * NAND controller node.
2699 */
2700 if (nfc->caps->legacy_of_bindings) {
2701 ret = marvell_nand_chip_init(dev, nfc, np);
2702 return ret;
2703 }
2704
2705 for_each_child_of_node(np, nand_np) {
2706 ret = marvell_nand_chip_init(dev, nfc, nand_np);
2707 if (ret) {
2708 of_node_put(nand_np);
2709 return ret;
2710 }
2711 }
2712
2713 return 0;
2714}
2715
2716static void marvell_nand_chips_cleanup(struct marvell_nfc *nfc)
2717{
2718 struct marvell_nand_chip *entry, *temp;
2719
2720 list_for_each_entry_safe(entry, temp, &nfc->chips, node) {
Boris Brezillon59ac2762018-09-06 14:05:15 +02002721 nand_release(&entry->chip);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002722 list_del(&entry->node);
2723 }
2724}
2725
2726static int marvell_nfc_init_dma(struct marvell_nfc *nfc)
2727{
2728 struct platform_device *pdev = container_of(nfc->dev,
2729 struct platform_device,
2730 dev);
2731 struct dma_slave_config config = {};
2732 struct resource *r;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002733 int ret;
2734
2735 if (!IS_ENABLED(CONFIG_PXA_DMA)) {
2736 dev_warn(nfc->dev,
2737 "DMA not enabled in configuration\n");
2738 return -ENOTSUPP;
2739 }
2740
2741 ret = dma_set_mask_and_coherent(nfc->dev, DMA_BIT_MASK(32));
2742 if (ret)
2743 return ret;
2744
Robert Jarzmikac75a502018-06-17 19:02:09 +02002745 nfc->dma_chan = dma_request_slave_channel(nfc->dev, "data");
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002746 if (!nfc->dma_chan) {
2747 dev_err(nfc->dev,
2748 "Unable to request data DMA channel\n");
2749 return -ENODEV;
2750 }
2751
2752 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2753 if (!r)
2754 return -ENXIO;
2755
2756 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2757 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2758 config.src_addr = r->start + NDDB;
2759 config.dst_addr = r->start + NDDB;
2760 config.src_maxburst = 32;
2761 config.dst_maxburst = 32;
2762 ret = dmaengine_slave_config(nfc->dma_chan, &config);
2763 if (ret < 0) {
2764 dev_err(nfc->dev, "Failed to configure DMA channel\n");
2765 return ret;
2766 }
2767
2768 /*
2769 * DMA must act on length multiple of 32 and this length may be
2770 * bigger than the destination buffer. Use this buffer instead
2771 * for DMA transfers and then copy the desired amount of data to
2772 * the provided buffer.
2773 */
Miquel Raynalc495a922018-01-19 18:39:01 +01002774 nfc->dma_buf = kmalloc(MAX_CHUNK_SIZE, GFP_KERNEL | GFP_DMA);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002775 if (!nfc->dma_buf)
2776 return -ENOMEM;
2777
2778 nfc->use_dma = true;
2779
2780 return 0;
2781}
2782
Daniel Mackbd9c3f92018-07-08 02:10:06 +02002783static void marvell_nfc_reset(struct marvell_nfc *nfc)
2784{
2785 /*
2786 * ECC operations and interruptions are only enabled when specifically
2787 * needed. ECC shall not be activated in the early stages (fails probe).
2788 * Arbiter flag, even if marked as "reserved", must be set (empirical).
2789 * SPARE_EN bit must always be set or ECC bytes will not be at the same
2790 * offset in the read page and this will fail the protection.
2791 */
2792 writel_relaxed(NDCR_ALL_INT | NDCR_ND_ARB_EN | NDCR_SPARE_EN |
2793 NDCR_RD_ID_CNT(NFCV1_READID_LEN), nfc->regs + NDCR);
2794 writel_relaxed(0xFFFFFFFF, nfc->regs + NDSR);
2795 writel_relaxed(0, nfc->regs + NDECCCTRL);
2796}
2797
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002798static int marvell_nfc_init(struct marvell_nfc *nfc)
2799{
2800 struct device_node *np = nfc->dev->of_node;
2801
2802 /*
2803 * Some SoCs like A7k/A8k need to enable manually the NAND
2804 * controller, gated clocks and reset bits to avoid being bootloader
2805 * dependent. This is done through the use of the System Functions
2806 * registers.
2807 */
2808 if (nfc->caps->need_system_controller) {
2809 struct regmap *sysctrl_base =
2810 syscon_regmap_lookup_by_phandle(np,
2811 "marvell,system-controller");
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002812
2813 if (IS_ERR(sysctrl_base))
2814 return PTR_ERR(sysctrl_base);
2815
Thomas Petazzoni88aa3bb2018-08-02 10:56:25 +02002816 regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX,
2817 GENCONF_SOC_DEVICE_MUX_NFC_EN |
2818 GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST |
2819 GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST |
2820 GENCONF_SOC_DEVICE_MUX_NFC_INT_EN);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002821
Thomas Petazzoni88aa3bb2018-08-02 10:56:25 +02002822 regmap_update_bits(sysctrl_base, GENCONF_CLK_GATING_CTRL,
2823 GENCONF_CLK_GATING_CTRL_ND_GATE,
2824 GENCONF_CLK_GATING_CTRL_ND_GATE);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002825
Thomas Petazzoni88aa3bb2018-08-02 10:56:25 +02002826 regmap_update_bits(sysctrl_base, GENCONF_ND_CLK_CTRL,
2827 GENCONF_ND_CLK_CTRL_EN,
2828 GENCONF_ND_CLK_CTRL_EN);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002829 }
2830
2831 /* Configure the DMA if appropriate */
2832 if (!nfc->caps->is_nfcv2)
2833 marvell_nfc_init_dma(nfc);
2834
Daniel Mackbd9c3f92018-07-08 02:10:06 +02002835 marvell_nfc_reset(nfc);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002836
2837 return 0;
2838}
2839
2840static int marvell_nfc_probe(struct platform_device *pdev)
2841{
2842 struct device *dev = &pdev->dev;
2843 struct resource *r;
2844 struct marvell_nfc *nfc;
2845 int ret;
2846 int irq;
2847
2848 nfc = devm_kzalloc(&pdev->dev, sizeof(struct marvell_nfc),
2849 GFP_KERNEL);
2850 if (!nfc)
2851 return -ENOMEM;
2852
2853 nfc->dev = dev;
Miquel Raynal7da45132018-07-17 09:08:02 +02002854 nand_controller_init(&nfc->controller);
Miquel Raynal8831e482018-07-20 17:15:05 +02002855 nfc->controller.ops = &marvell_nand_controller_ops;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002856 INIT_LIST_HEAD(&nfc->chips);
2857
2858 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2859 nfc->regs = devm_ioremap_resource(dev, r);
2860 if (IS_ERR(nfc->regs))
2861 return PTR_ERR(nfc->regs);
2862
2863 irq = platform_get_irq(pdev, 0);
2864 if (irq < 0) {
2865 dev_err(dev, "failed to retrieve irq\n");
2866 return irq;
2867 }
2868
Boris Brezillon6b6de652018-03-26 11:53:01 +02002869 nfc->core_clk = devm_clk_get(&pdev->dev, "core");
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002870
2871 /* Managed the legacy case (when the first clock was not named) */
Boris Brezillon6b6de652018-03-26 11:53:01 +02002872 if (nfc->core_clk == ERR_PTR(-ENOENT))
2873 nfc->core_clk = devm_clk_get(&pdev->dev, NULL);
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002874
Boris Brezillon6b6de652018-03-26 11:53:01 +02002875 if (IS_ERR(nfc->core_clk))
2876 return PTR_ERR(nfc->core_clk);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002877
Boris Brezillon6b6de652018-03-26 11:53:01 +02002878 ret = clk_prepare_enable(nfc->core_clk);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002879 if (ret)
2880 return ret;
2881
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002882 nfc->reg_clk = devm_clk_get(&pdev->dev, "reg");
Daniel Mackf9e64d62018-07-08 02:10:08 +02002883 if (IS_ERR(nfc->reg_clk)) {
2884 if (PTR_ERR(nfc->reg_clk) != -ENOENT) {
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002885 ret = PTR_ERR(nfc->reg_clk);
Boris Brezillon6b6de652018-03-26 11:53:01 +02002886 goto unprepare_core_clk;
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002887 }
Daniel Mackf9e64d62018-07-08 02:10:08 +02002888
2889 nfc->reg_clk = NULL;
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002890 }
2891
Daniel Mackf9e64d62018-07-08 02:10:08 +02002892 ret = clk_prepare_enable(nfc->reg_clk);
2893 if (ret)
2894 goto unprepare_core_clk;
2895
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002896 marvell_nfc_disable_int(nfc, NDCR_ALL_INT);
2897 marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
2898 ret = devm_request_irq(dev, irq, marvell_nfc_isr,
2899 0, "marvell-nfc", nfc);
2900 if (ret)
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002901 goto unprepare_reg_clk;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002902
2903 /* Get NAND controller capabilities */
2904 if (pdev->id_entry)
2905 nfc->caps = (void *)pdev->id_entry->driver_data;
2906 else
2907 nfc->caps = of_device_get_match_data(&pdev->dev);
2908
2909 if (!nfc->caps) {
2910 dev_err(dev, "Could not retrieve NFC caps\n");
2911 ret = -EINVAL;
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002912 goto unprepare_reg_clk;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002913 }
2914
2915 /* Init the controller and then probe the chips */
2916 ret = marvell_nfc_init(nfc);
2917 if (ret)
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002918 goto unprepare_reg_clk;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002919
2920 platform_set_drvdata(pdev, nfc);
2921
2922 ret = marvell_nand_chips_init(dev, nfc);
2923 if (ret)
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002924 goto unprepare_reg_clk;
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002925
2926 return 0;
2927
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002928unprepare_reg_clk:
2929 clk_disable_unprepare(nfc->reg_clk);
Boris Brezillon6b6de652018-03-26 11:53:01 +02002930unprepare_core_clk:
2931 clk_disable_unprepare(nfc->core_clk);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002932
2933 return ret;
2934}
2935
2936static int marvell_nfc_remove(struct platform_device *pdev)
2937{
2938 struct marvell_nfc *nfc = platform_get_drvdata(pdev);
2939
2940 marvell_nand_chips_cleanup(nfc);
2941
2942 if (nfc->use_dma) {
2943 dmaengine_terminate_all(nfc->dma_chan);
2944 dma_release_channel(nfc->dma_chan);
2945 }
2946
Gregory CLEMENT961ba152018-03-13 11:30:16 +01002947 clk_disable_unprepare(nfc->reg_clk);
Boris Brezillon6b6de652018-03-26 11:53:01 +02002948 clk_disable_unprepare(nfc->core_clk);
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002949
2950 return 0;
2951}
2952
Daniel Mackbd9c3f92018-07-08 02:10:06 +02002953static int __maybe_unused marvell_nfc_suspend(struct device *dev)
2954{
2955 struct marvell_nfc *nfc = dev_get_drvdata(dev);
2956 struct marvell_nand_chip *chip;
2957
2958 list_for_each_entry(chip, &nfc->chips, node)
2959 marvell_nfc_wait_ndrun(&chip->chip);
2960
2961 clk_disable_unprepare(nfc->reg_clk);
2962 clk_disable_unprepare(nfc->core_clk);
2963
2964 return 0;
2965}
2966
2967static int __maybe_unused marvell_nfc_resume(struct device *dev)
2968{
2969 struct marvell_nfc *nfc = dev_get_drvdata(dev);
2970 int ret;
2971
2972 ret = clk_prepare_enable(nfc->core_clk);
2973 if (ret < 0)
2974 return ret;
2975
Daniel Mackf9e64d62018-07-08 02:10:08 +02002976 ret = clk_prepare_enable(nfc->reg_clk);
2977 if (ret < 0)
2978 return ret;
Daniel Mackbd9c3f92018-07-08 02:10:06 +02002979
2980 /*
2981 * Reset nfc->selected_chip so the next command will cause the timing
2982 * registers to be restored in marvell_nfc_select_chip().
2983 */
2984 nfc->selected_chip = NULL;
2985
2986 /* Reset registers that have lost their contents */
2987 marvell_nfc_reset(nfc);
2988
2989 return 0;
2990}
2991
2992static const struct dev_pm_ops marvell_nfc_pm_ops = {
2993 SET_SYSTEM_SLEEP_PM_OPS(marvell_nfc_suspend, marvell_nfc_resume)
2994};
2995
Miquel Raynal02f26ec2018-01-09 11:36:33 +01002996static const struct marvell_nfc_caps marvell_armada_8k_nfc_caps = {
2997 .max_cs_nb = 4,
2998 .max_rb_nb = 2,
2999 .need_system_controller = true,
3000 .is_nfcv2 = true,
3001};
3002
3003static const struct marvell_nfc_caps marvell_armada370_nfc_caps = {
3004 .max_cs_nb = 4,
3005 .max_rb_nb = 2,
3006 .is_nfcv2 = true,
3007};
3008
3009static const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps = {
3010 .max_cs_nb = 2,
3011 .max_rb_nb = 1,
3012 .use_dma = true,
3013};
3014
3015static const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps = {
3016 .max_cs_nb = 4,
3017 .max_rb_nb = 2,
3018 .need_system_controller = true,
3019 .legacy_of_bindings = true,
3020 .is_nfcv2 = true,
3021};
3022
3023static const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps = {
3024 .max_cs_nb = 4,
3025 .max_rb_nb = 2,
3026 .legacy_of_bindings = true,
3027 .is_nfcv2 = true,
3028};
3029
3030static const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps = {
3031 .max_cs_nb = 2,
3032 .max_rb_nb = 1,
3033 .legacy_of_bindings = true,
3034 .use_dma = true,
3035};
3036
3037static const struct platform_device_id marvell_nfc_platform_ids[] = {
3038 {
3039 .name = "pxa3xx-nand",
3040 .driver_data = (kernel_ulong_t)&marvell_pxa3xx_nfc_legacy_caps,
3041 },
3042 { /* sentinel */ },
3043};
3044MODULE_DEVICE_TABLE(platform, marvell_nfc_platform_ids);
3045
3046static const struct of_device_id marvell_nfc_of_ids[] = {
3047 {
3048 .compatible = "marvell,armada-8k-nand-controller",
3049 .data = &marvell_armada_8k_nfc_caps,
3050 },
3051 {
3052 .compatible = "marvell,armada370-nand-controller",
3053 .data = &marvell_armada370_nfc_caps,
3054 },
3055 {
3056 .compatible = "marvell,pxa3xx-nand-controller",
3057 .data = &marvell_pxa3xx_nfc_caps,
3058 },
3059 /* Support for old/deprecated bindings: */
3060 {
3061 .compatible = "marvell,armada-8k-nand",
3062 .data = &marvell_armada_8k_nfc_legacy_caps,
3063 },
3064 {
3065 .compatible = "marvell,armada370-nand",
3066 .data = &marvell_armada370_nfc_legacy_caps,
3067 },
3068 {
3069 .compatible = "marvell,pxa3xx-nand",
3070 .data = &marvell_pxa3xx_nfc_legacy_caps,
3071 },
3072 { /* sentinel */ },
3073};
3074MODULE_DEVICE_TABLE(of, marvell_nfc_of_ids);
3075
3076static struct platform_driver marvell_nfc_driver = {
3077 .driver = {
3078 .name = "marvell-nfc",
3079 .of_match_table = marvell_nfc_of_ids,
Daniel Mackbd9c3f92018-07-08 02:10:06 +02003080 .pm = &marvell_nfc_pm_ops,
Miquel Raynal02f26ec2018-01-09 11:36:33 +01003081 },
3082 .id_table = marvell_nfc_platform_ids,
3083 .probe = marvell_nfc_probe,
3084 .remove = marvell_nfc_remove,
3085};
3086module_platform_driver(marvell_nfc_driver);
3087
3088MODULE_LICENSE("GPL");
3089MODULE_DESCRIPTION("Marvell NAND controller driver");