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