1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2010-2015 Broadcom Corporation
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/platform_device.h>
12 #include <linux/err.h>
13 #include <linux/completion.h>
14 #include <linux/interrupt.h>
15 #include <linux/spinlock.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/ioport.h>
18 #include <linux/bug.h>
19 #include <linux/kernel.h>
20 #include <linux/bitops.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/rawnand.h>
24 #include <linux/mtd/partitions.h>
26 #include <linux/of_platform.h>
27 #include <linux/slab.h>
28 #include <linux/static_key.h>
29 #include <linux/list.h>
30 #include <linux/log2.h>
35 * This flag controls if WP stays on between erase/write commands to mitigate
36 * flash corruption due to power glitches. Values:
37 * 0: NAND_WP is not used or not available
38 * 1: NAND_WP is set by default, cleared for erase/write operations
39 * 2: NAND_WP is always cleared
42 module_param(wp_on, int, 0444);
44 /***********************************************************************
46 ***********************************************************************/
48 #define DRV_NAME "brcmnand"
51 #define CMD_PAGE_READ 0x01
52 #define CMD_SPARE_AREA_READ 0x02
53 #define CMD_STATUS_READ 0x03
54 #define CMD_PROGRAM_PAGE 0x04
55 #define CMD_PROGRAM_SPARE_AREA 0x05
56 #define CMD_COPY_BACK 0x06
57 #define CMD_DEVICE_ID_READ 0x07
58 #define CMD_BLOCK_ERASE 0x08
59 #define CMD_FLASH_RESET 0x09
60 #define CMD_BLOCKS_LOCK 0x0a
61 #define CMD_BLOCKS_LOCK_DOWN 0x0b
62 #define CMD_BLOCKS_UNLOCK 0x0c
63 #define CMD_READ_BLOCKS_LOCK_STATUS 0x0d
64 #define CMD_PARAMETER_READ 0x0e
65 #define CMD_PARAMETER_CHANGE_COL 0x0f
66 #define CMD_LOW_LEVEL_OP 0x10
68 struct brcm_nand_dma_desc {
83 /* Bitfields for brcm_nand_dma_desc::status_valid */
84 #define FLASH_DMA_ECC_ERROR (1 << 8)
85 #define FLASH_DMA_CORR_ERROR (1 << 9)
87 /* Bitfields for DMA_MODE */
88 #define FLASH_DMA_MODE_STOP_ON_ERROR BIT(1) /* stop in Uncorr ECC error */
89 #define FLASH_DMA_MODE_MODE BIT(0) /* link list */
90 #define FLASH_DMA_MODE_MASK (FLASH_DMA_MODE_STOP_ON_ERROR | \
93 /* 512B flash cache in the NAND controller HW */
96 #define FC_WORDS (FC_BYTES >> 2)
98 #define BRCMNAND_MIN_PAGESIZE 512
99 #define BRCMNAND_MIN_BLOCKSIZE (8 * 1024)
100 #define BRCMNAND_MIN_DEVSIZE (4ULL * 1024 * 1024)
102 #define NAND_CTRL_RDY (INTFC_CTLR_READY | INTFC_FLASH_READY)
103 #define NAND_POLL_STATUS_TIMEOUT_MS 100
105 #define EDU_CMD_WRITE 0x00
106 #define EDU_CMD_READ 0x01
107 #define EDU_STATUS_ACTIVE BIT(0)
108 #define EDU_ERR_STATUS_ERRACK BIT(0)
109 #define EDU_DONE_MASK GENMASK(1, 0)
111 #define EDU_CONFIG_MODE_NAND BIT(0)
112 #define EDU_CONFIG_SWAP_BYTE BIT(1)
113 #ifdef CONFIG_CPU_BIG_ENDIAN
114 #define EDU_CONFIG_SWAP_CFG EDU_CONFIG_SWAP_BYTE
116 #define EDU_CONFIG_SWAP_CFG 0
132 static const u16 edu_regs[] = {
134 [EDU_DRAM_ADDR] = 0x04,
135 [EDU_EXT_ADDR] = 0x08,
141 [EDU_ERR_STATUS] = 0x20,
144 /* flash_dma registers */
146 FLASH_DMA_REVISION = 0,
147 FLASH_DMA_FIRST_DESC,
148 FLASH_DMA_FIRST_DESC_EXT,
152 FLASH_DMA_INTERRUPT_DESC,
153 FLASH_DMA_INTERRUPT_DESC_EXT,
154 FLASH_DMA_ERROR_STATUS,
155 FLASH_DMA_CURRENT_DESC,
156 FLASH_DMA_CURRENT_DESC_EXT,
159 /* flash_dma registers v0*/
160 static const u16 flash_dma_regs_v0[] = {
161 [FLASH_DMA_REVISION] = 0x00,
162 [FLASH_DMA_FIRST_DESC] = 0x04,
163 [FLASH_DMA_CTRL] = 0x08,
164 [FLASH_DMA_MODE] = 0x0c,
165 [FLASH_DMA_STATUS] = 0x10,
166 [FLASH_DMA_INTERRUPT_DESC] = 0x14,
167 [FLASH_DMA_ERROR_STATUS] = 0x18,
168 [FLASH_DMA_CURRENT_DESC] = 0x1c,
171 /* flash_dma registers v1*/
172 static const u16 flash_dma_regs_v1[] = {
173 [FLASH_DMA_REVISION] = 0x00,
174 [FLASH_DMA_FIRST_DESC] = 0x04,
175 [FLASH_DMA_FIRST_DESC_EXT] = 0x08,
176 [FLASH_DMA_CTRL] = 0x0c,
177 [FLASH_DMA_MODE] = 0x10,
178 [FLASH_DMA_STATUS] = 0x14,
179 [FLASH_DMA_INTERRUPT_DESC] = 0x18,
180 [FLASH_DMA_INTERRUPT_DESC_EXT] = 0x1c,
181 [FLASH_DMA_ERROR_STATUS] = 0x20,
182 [FLASH_DMA_CURRENT_DESC] = 0x24,
183 [FLASH_DMA_CURRENT_DESC_EXT] = 0x28,
186 /* flash_dma registers v4 */
187 static const u16 flash_dma_regs_v4[] = {
188 [FLASH_DMA_REVISION] = 0x00,
189 [FLASH_DMA_FIRST_DESC] = 0x08,
190 [FLASH_DMA_FIRST_DESC_EXT] = 0x0c,
191 [FLASH_DMA_CTRL] = 0x10,
192 [FLASH_DMA_MODE] = 0x14,
193 [FLASH_DMA_STATUS] = 0x18,
194 [FLASH_DMA_INTERRUPT_DESC] = 0x20,
195 [FLASH_DMA_INTERRUPT_DESC_EXT] = 0x24,
196 [FLASH_DMA_ERROR_STATUS] = 0x28,
197 [FLASH_DMA_CURRENT_DESC] = 0x30,
198 [FLASH_DMA_CURRENT_DESC_EXT] = 0x34,
201 /* Controller feature flags */
203 BRCMNAND_HAS_1K_SECTORS = BIT(0),
204 BRCMNAND_HAS_PREFETCH = BIT(1),
205 BRCMNAND_HAS_CACHE_MODE = BIT(2),
206 BRCMNAND_HAS_WP = BIT(3),
209 struct brcmnand_host;
211 static DEFINE_STATIC_KEY_FALSE(brcmnand_soc_has_ops_key);
213 struct brcmnand_controller {
215 struct nand_controller controller;
216 void __iomem *nand_base;
217 void __iomem *nand_fc; /* flash cache */
218 void __iomem *flash_dma_base;
220 unsigned int dma_irq;
223 /* Some SoCs provide custom interrupt status register(s) */
224 struct brcmnand_soc *soc;
226 /* Some SoCs have a gateable clock for the controller */
232 struct completion done;
233 struct completion dma_done;
234 struct completion edu_done;
236 /* List of NAND hosts (one for each chip-select) */
237 struct list_head host_list;
239 /* EDU info, per-transaction */
240 const u16 *edu_offsets;
241 void __iomem *edu_base;
250 const u16 *flash_dma_offsets;
251 struct brcm_nand_dma_desc *dma_desc;
254 int (*dma_trans)(struct brcmnand_host *host, u64 addr, u32 *buf,
255 u32 len, u8 dma_cmd);
257 /* in-memory cache of the FLASH_CACHE, used only for some commands */
258 u8 flash_cache[FC_BYTES];
260 /* Controller revision details */
261 const u16 *reg_offsets;
262 unsigned int reg_spacing; /* between CS1, CS2, ... regs */
263 const u8 *cs_offsets; /* within each chip-select */
264 const u8 *cs0_offsets; /* within CS0, if different */
265 unsigned int max_block_size;
266 const unsigned int *block_sizes;
267 unsigned int max_page_size;
268 const unsigned int *page_sizes;
269 unsigned int page_size_shift;
270 unsigned int max_oob;
274 /* for low-power standby/resume only */
275 u32 nand_cs_nand_select;
276 u32 nand_cs_nand_xor;
277 u32 corr_stat_threshold;
283 struct brcmnand_cfg {
285 unsigned int block_size;
286 unsigned int page_size;
287 unsigned int spare_area_size;
288 unsigned int device_width;
289 unsigned int col_adr_bytes;
290 unsigned int blk_adr_bytes;
291 unsigned int ful_adr_bytes;
292 unsigned int sector_size_1k;
293 unsigned int ecc_level;
294 /* use for low-power standby/resume only */
302 struct brcmnand_host {
303 struct list_head node;
305 struct nand_chip chip;
306 struct platform_device *pdev;
309 unsigned int last_cmd;
310 unsigned int last_byte;
312 struct brcmnand_cfg hwcfg;
313 struct brcmnand_controller *ctrl;
317 BRCMNAND_CMD_START = 0,
318 BRCMNAND_CMD_EXT_ADDRESS,
319 BRCMNAND_CMD_ADDRESS,
320 BRCMNAND_INTFC_STATUS,
325 BRCMNAND_CS1_BASE, /* CS1 regs, if non-contiguous */
326 BRCMNAND_CORR_THRESHOLD,
327 BRCMNAND_CORR_THRESHOLD_EXT,
328 BRCMNAND_UNCORR_COUNT,
330 BRCMNAND_CORR_EXT_ADDR,
332 BRCMNAND_UNCORR_EXT_ADDR,
333 BRCMNAND_UNCORR_ADDR,
338 BRCMNAND_OOB_READ_BASE,
339 BRCMNAND_OOB_READ_10_BASE, /* offset 0x10, if non-contiguous */
340 BRCMNAND_OOB_WRITE_BASE,
341 BRCMNAND_OOB_WRITE_10_BASE, /* offset 0x10, if non-contiguous */
345 /* BRCMNAND v2.1-v2.2 */
346 static const u16 brcmnand_regs_v21[] = {
347 [BRCMNAND_CMD_START] = 0x04,
348 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
349 [BRCMNAND_CMD_ADDRESS] = 0x0c,
350 [BRCMNAND_INTFC_STATUS] = 0x5c,
351 [BRCMNAND_CS_SELECT] = 0x14,
352 [BRCMNAND_CS_XOR] = 0x18,
353 [BRCMNAND_LL_OP] = 0,
354 [BRCMNAND_CS0_BASE] = 0x40,
355 [BRCMNAND_CS1_BASE] = 0,
356 [BRCMNAND_CORR_THRESHOLD] = 0,
357 [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
358 [BRCMNAND_UNCORR_COUNT] = 0,
359 [BRCMNAND_CORR_COUNT] = 0,
360 [BRCMNAND_CORR_EXT_ADDR] = 0x60,
361 [BRCMNAND_CORR_ADDR] = 0x64,
362 [BRCMNAND_UNCORR_EXT_ADDR] = 0x68,
363 [BRCMNAND_UNCORR_ADDR] = 0x6c,
364 [BRCMNAND_SEMAPHORE] = 0x50,
365 [BRCMNAND_ID] = 0x54,
366 [BRCMNAND_ID_EXT] = 0,
367 [BRCMNAND_LL_RDATA] = 0,
368 [BRCMNAND_OOB_READ_BASE] = 0x20,
369 [BRCMNAND_OOB_READ_10_BASE] = 0,
370 [BRCMNAND_OOB_WRITE_BASE] = 0x30,
371 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
372 [BRCMNAND_FC_BASE] = 0x200,
375 /* BRCMNAND v3.3-v4.0 */
376 static const u16 brcmnand_regs_v33[] = {
377 [BRCMNAND_CMD_START] = 0x04,
378 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
379 [BRCMNAND_CMD_ADDRESS] = 0x0c,
380 [BRCMNAND_INTFC_STATUS] = 0x6c,
381 [BRCMNAND_CS_SELECT] = 0x14,
382 [BRCMNAND_CS_XOR] = 0x18,
383 [BRCMNAND_LL_OP] = 0x178,
384 [BRCMNAND_CS0_BASE] = 0x40,
385 [BRCMNAND_CS1_BASE] = 0xd0,
386 [BRCMNAND_CORR_THRESHOLD] = 0x84,
387 [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
388 [BRCMNAND_UNCORR_COUNT] = 0,
389 [BRCMNAND_CORR_COUNT] = 0,
390 [BRCMNAND_CORR_EXT_ADDR] = 0x70,
391 [BRCMNAND_CORR_ADDR] = 0x74,
392 [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
393 [BRCMNAND_UNCORR_ADDR] = 0x7c,
394 [BRCMNAND_SEMAPHORE] = 0x58,
395 [BRCMNAND_ID] = 0x60,
396 [BRCMNAND_ID_EXT] = 0x64,
397 [BRCMNAND_LL_RDATA] = 0x17c,
398 [BRCMNAND_OOB_READ_BASE] = 0x20,
399 [BRCMNAND_OOB_READ_10_BASE] = 0x130,
400 [BRCMNAND_OOB_WRITE_BASE] = 0x30,
401 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
402 [BRCMNAND_FC_BASE] = 0x200,
406 static const u16 brcmnand_regs_v50[] = {
407 [BRCMNAND_CMD_START] = 0x04,
408 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
409 [BRCMNAND_CMD_ADDRESS] = 0x0c,
410 [BRCMNAND_INTFC_STATUS] = 0x6c,
411 [BRCMNAND_CS_SELECT] = 0x14,
412 [BRCMNAND_CS_XOR] = 0x18,
413 [BRCMNAND_LL_OP] = 0x178,
414 [BRCMNAND_CS0_BASE] = 0x40,
415 [BRCMNAND_CS1_BASE] = 0xd0,
416 [BRCMNAND_CORR_THRESHOLD] = 0x84,
417 [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
418 [BRCMNAND_UNCORR_COUNT] = 0,
419 [BRCMNAND_CORR_COUNT] = 0,
420 [BRCMNAND_CORR_EXT_ADDR] = 0x70,
421 [BRCMNAND_CORR_ADDR] = 0x74,
422 [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
423 [BRCMNAND_UNCORR_ADDR] = 0x7c,
424 [BRCMNAND_SEMAPHORE] = 0x58,
425 [BRCMNAND_ID] = 0x60,
426 [BRCMNAND_ID_EXT] = 0x64,
427 [BRCMNAND_LL_RDATA] = 0x17c,
428 [BRCMNAND_OOB_READ_BASE] = 0x20,
429 [BRCMNAND_OOB_READ_10_BASE] = 0x130,
430 [BRCMNAND_OOB_WRITE_BASE] = 0x30,
431 [BRCMNAND_OOB_WRITE_10_BASE] = 0x140,
432 [BRCMNAND_FC_BASE] = 0x200,
435 /* BRCMNAND v6.0 - v7.1 */
436 static const u16 brcmnand_regs_v60[] = {
437 [BRCMNAND_CMD_START] = 0x04,
438 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
439 [BRCMNAND_CMD_ADDRESS] = 0x0c,
440 [BRCMNAND_INTFC_STATUS] = 0x14,
441 [BRCMNAND_CS_SELECT] = 0x18,
442 [BRCMNAND_CS_XOR] = 0x1c,
443 [BRCMNAND_LL_OP] = 0x20,
444 [BRCMNAND_CS0_BASE] = 0x50,
445 [BRCMNAND_CS1_BASE] = 0,
446 [BRCMNAND_CORR_THRESHOLD] = 0xc0,
447 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xc4,
448 [BRCMNAND_UNCORR_COUNT] = 0xfc,
449 [BRCMNAND_CORR_COUNT] = 0x100,
450 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
451 [BRCMNAND_CORR_ADDR] = 0x110,
452 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
453 [BRCMNAND_UNCORR_ADDR] = 0x118,
454 [BRCMNAND_SEMAPHORE] = 0x150,
455 [BRCMNAND_ID] = 0x194,
456 [BRCMNAND_ID_EXT] = 0x198,
457 [BRCMNAND_LL_RDATA] = 0x19c,
458 [BRCMNAND_OOB_READ_BASE] = 0x200,
459 [BRCMNAND_OOB_READ_10_BASE] = 0,
460 [BRCMNAND_OOB_WRITE_BASE] = 0x280,
461 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
462 [BRCMNAND_FC_BASE] = 0x400,
466 static const u16 brcmnand_regs_v71[] = {
467 [BRCMNAND_CMD_START] = 0x04,
468 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
469 [BRCMNAND_CMD_ADDRESS] = 0x0c,
470 [BRCMNAND_INTFC_STATUS] = 0x14,
471 [BRCMNAND_CS_SELECT] = 0x18,
472 [BRCMNAND_CS_XOR] = 0x1c,
473 [BRCMNAND_LL_OP] = 0x20,
474 [BRCMNAND_CS0_BASE] = 0x50,
475 [BRCMNAND_CS1_BASE] = 0,
476 [BRCMNAND_CORR_THRESHOLD] = 0xdc,
477 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0,
478 [BRCMNAND_UNCORR_COUNT] = 0xfc,
479 [BRCMNAND_CORR_COUNT] = 0x100,
480 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
481 [BRCMNAND_CORR_ADDR] = 0x110,
482 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
483 [BRCMNAND_UNCORR_ADDR] = 0x118,
484 [BRCMNAND_SEMAPHORE] = 0x150,
485 [BRCMNAND_ID] = 0x194,
486 [BRCMNAND_ID_EXT] = 0x198,
487 [BRCMNAND_LL_RDATA] = 0x19c,
488 [BRCMNAND_OOB_READ_BASE] = 0x200,
489 [BRCMNAND_OOB_READ_10_BASE] = 0,
490 [BRCMNAND_OOB_WRITE_BASE] = 0x280,
491 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
492 [BRCMNAND_FC_BASE] = 0x400,
496 static const u16 brcmnand_regs_v72[] = {
497 [BRCMNAND_CMD_START] = 0x04,
498 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
499 [BRCMNAND_CMD_ADDRESS] = 0x0c,
500 [BRCMNAND_INTFC_STATUS] = 0x14,
501 [BRCMNAND_CS_SELECT] = 0x18,
502 [BRCMNAND_CS_XOR] = 0x1c,
503 [BRCMNAND_LL_OP] = 0x20,
504 [BRCMNAND_CS0_BASE] = 0x50,
505 [BRCMNAND_CS1_BASE] = 0,
506 [BRCMNAND_CORR_THRESHOLD] = 0xdc,
507 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0,
508 [BRCMNAND_UNCORR_COUNT] = 0xfc,
509 [BRCMNAND_CORR_COUNT] = 0x100,
510 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
511 [BRCMNAND_CORR_ADDR] = 0x110,
512 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
513 [BRCMNAND_UNCORR_ADDR] = 0x118,
514 [BRCMNAND_SEMAPHORE] = 0x150,
515 [BRCMNAND_ID] = 0x194,
516 [BRCMNAND_ID_EXT] = 0x198,
517 [BRCMNAND_LL_RDATA] = 0x19c,
518 [BRCMNAND_OOB_READ_BASE] = 0x200,
519 [BRCMNAND_OOB_READ_10_BASE] = 0,
520 [BRCMNAND_OOB_WRITE_BASE] = 0x400,
521 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
522 [BRCMNAND_FC_BASE] = 0x600,
525 enum brcmnand_cs_reg {
526 BRCMNAND_CS_CFG_EXT = 0,
528 BRCMNAND_CS_ACC_CONTROL,
533 /* Per chip-select offsets for v7.1 */
534 static const u8 brcmnand_cs_offsets_v71[] = {
535 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
536 [BRCMNAND_CS_CFG_EXT] = 0x04,
537 [BRCMNAND_CS_CFG] = 0x08,
538 [BRCMNAND_CS_TIMING1] = 0x0c,
539 [BRCMNAND_CS_TIMING2] = 0x10,
542 /* Per chip-select offsets for pre v7.1, except CS0 on <= v5.0 */
543 static const u8 brcmnand_cs_offsets[] = {
544 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
545 [BRCMNAND_CS_CFG_EXT] = 0x04,
546 [BRCMNAND_CS_CFG] = 0x04,
547 [BRCMNAND_CS_TIMING1] = 0x08,
548 [BRCMNAND_CS_TIMING2] = 0x0c,
551 /* Per chip-select offset for <= v5.0 on CS0 only */
552 static const u8 brcmnand_cs_offsets_cs0[] = {
553 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
554 [BRCMNAND_CS_CFG_EXT] = 0x08,
555 [BRCMNAND_CS_CFG] = 0x08,
556 [BRCMNAND_CS_TIMING1] = 0x10,
557 [BRCMNAND_CS_TIMING2] = 0x14,
561 * Bitfields for the CFG and CFG_EXT registers. Pre-v7.1 controllers only had
562 * one config register, but once the bitfields overflowed, newer controllers
563 * (v7.1 and newer) added a CFG_EXT register and shuffled a few fields around.
566 CFG_BLK_ADR_BYTES_SHIFT = 8,
567 CFG_COL_ADR_BYTES_SHIFT = 12,
568 CFG_FUL_ADR_BYTES_SHIFT = 16,
569 CFG_BUS_WIDTH_SHIFT = 23,
570 CFG_BUS_WIDTH = BIT(CFG_BUS_WIDTH_SHIFT),
571 CFG_DEVICE_SIZE_SHIFT = 24,
574 CFG_PAGE_SIZE_SHIFT_v2_1 = 30,
576 /* Only for pre-v7.1 (with no CFG_EXT register) */
577 CFG_PAGE_SIZE_SHIFT = 20,
578 CFG_BLK_SIZE_SHIFT = 28,
580 /* Only for v7.1+ (with CFG_EXT register) */
581 CFG_EXT_PAGE_SIZE_SHIFT = 0,
582 CFG_EXT_BLK_SIZE_SHIFT = 4,
585 /* BRCMNAND_INTFC_STATUS */
587 INTFC_FLASH_STATUS = GENMASK(7, 0),
589 INTFC_ERASED = BIT(27),
590 INTFC_OOB_VALID = BIT(28),
591 INTFC_CACHE_VALID = BIT(29),
592 INTFC_FLASH_READY = BIT(30),
593 INTFC_CTLR_READY = BIT(31),
596 /***********************************************************************
597 * NAND ACC CONTROL bitfield
599 * Some bits have remained constant throughout hardware revision, while
600 * others have shifted around.
601 ***********************************************************************/
603 /* Constant for all versions (where supported) */
605 /* See BRCMNAND_HAS_CACHE_MODE */
606 ACC_CONTROL_CACHE_MODE = BIT(22),
608 /* See BRCMNAND_HAS_PREFETCH */
609 ACC_CONTROL_PREFETCH = BIT(23),
611 ACC_CONTROL_PAGE_HIT = BIT(24),
612 ACC_CONTROL_WR_PREEMPT = BIT(25),
613 ACC_CONTROL_PARTIAL_PAGE = BIT(26),
614 ACC_CONTROL_RD_ERASED = BIT(27),
615 ACC_CONTROL_FAST_PGM_RDIN = BIT(28),
616 ACC_CONTROL_WR_ECC = BIT(30),
617 ACC_CONTROL_RD_ECC = BIT(31),
620 #define ACC_CONTROL_ECC_SHIFT 16
622 #define ACC_CONTROL_ECC_EXT_SHIFT 13
624 static inline bool brcmnand_non_mmio_ops(struct brcmnand_controller *ctrl)
626 return static_branch_unlikely(&brcmnand_soc_has_ops_key);
629 static inline u32 nand_readreg(struct brcmnand_controller *ctrl, u32 offs)
631 if (brcmnand_non_mmio_ops(ctrl))
632 return brcmnand_soc_read(ctrl->soc, offs);
633 return brcmnand_readl(ctrl->nand_base + offs);
636 static inline void nand_writereg(struct brcmnand_controller *ctrl, u32 offs,
639 if (brcmnand_non_mmio_ops(ctrl))
640 brcmnand_soc_write(ctrl->soc, val, offs);
642 brcmnand_writel(val, ctrl->nand_base + offs);
645 static int brcmnand_revision_init(struct brcmnand_controller *ctrl)
647 static const unsigned int block_sizes_v6[] = { 8, 16, 128, 256, 512, 1024, 2048, 0 };
648 static const unsigned int block_sizes_v4[] = { 16, 128, 8, 512, 256, 1024, 2048, 0 };
649 static const unsigned int block_sizes_v2_2[] = { 16, 128, 8, 512, 256, 0 };
650 static const unsigned int block_sizes_v2_1[] = { 16, 128, 8, 512, 0 };
651 static const unsigned int page_sizes_v3_4[] = { 512, 2048, 4096, 8192, 0 };
652 static const unsigned int page_sizes_v2_2[] = { 512, 2048, 4096, 0 };
653 static const unsigned int page_sizes_v2_1[] = { 512, 2048, 0 };
655 ctrl->nand_version = nand_readreg(ctrl, 0) & 0xffff;
657 /* Only support v2.1+ */
658 if (ctrl->nand_version < 0x0201) {
659 dev_err(ctrl->dev, "version %#x not supported\n",
664 /* Register offsets */
665 if (ctrl->nand_version >= 0x0702)
666 ctrl->reg_offsets = brcmnand_regs_v72;
667 else if (ctrl->nand_version == 0x0701)
668 ctrl->reg_offsets = brcmnand_regs_v71;
669 else if (ctrl->nand_version >= 0x0600)
670 ctrl->reg_offsets = brcmnand_regs_v60;
671 else if (ctrl->nand_version >= 0x0500)
672 ctrl->reg_offsets = brcmnand_regs_v50;
673 else if (ctrl->nand_version >= 0x0303)
674 ctrl->reg_offsets = brcmnand_regs_v33;
675 else if (ctrl->nand_version >= 0x0201)
676 ctrl->reg_offsets = brcmnand_regs_v21;
678 /* Chip-select stride */
679 if (ctrl->nand_version >= 0x0701)
680 ctrl->reg_spacing = 0x14;
682 ctrl->reg_spacing = 0x10;
684 /* Per chip-select registers */
685 if (ctrl->nand_version >= 0x0701) {
686 ctrl->cs_offsets = brcmnand_cs_offsets_v71;
688 ctrl->cs_offsets = brcmnand_cs_offsets;
690 /* v3.3-5.0 have a different CS0 offset layout */
691 if (ctrl->nand_version >= 0x0303 &&
692 ctrl->nand_version <= 0x0500)
693 ctrl->cs0_offsets = brcmnand_cs_offsets_cs0;
696 /* Page / block sizes */
697 if (ctrl->nand_version >= 0x0701) {
698 /* >= v7.1 use nice power-of-2 values! */
699 ctrl->max_page_size = 16 * 1024;
700 ctrl->max_block_size = 2 * 1024 * 1024;
702 if (ctrl->nand_version >= 0x0304)
703 ctrl->page_sizes = page_sizes_v3_4;
704 else if (ctrl->nand_version >= 0x0202)
705 ctrl->page_sizes = page_sizes_v2_2;
707 ctrl->page_sizes = page_sizes_v2_1;
709 if (ctrl->nand_version >= 0x0202)
710 ctrl->page_size_shift = CFG_PAGE_SIZE_SHIFT;
712 ctrl->page_size_shift = CFG_PAGE_SIZE_SHIFT_v2_1;
714 if (ctrl->nand_version >= 0x0600)
715 ctrl->block_sizes = block_sizes_v6;
716 else if (ctrl->nand_version >= 0x0400)
717 ctrl->block_sizes = block_sizes_v4;
718 else if (ctrl->nand_version >= 0x0202)
719 ctrl->block_sizes = block_sizes_v2_2;
721 ctrl->block_sizes = block_sizes_v2_1;
723 if (ctrl->nand_version < 0x0400) {
724 if (ctrl->nand_version < 0x0202)
725 ctrl->max_page_size = 2048;
727 ctrl->max_page_size = 4096;
728 ctrl->max_block_size = 512 * 1024;
732 /* Maximum spare area sector size (per 512B) */
733 if (ctrl->nand_version == 0x0702)
735 else if (ctrl->nand_version >= 0x0600)
737 else if (ctrl->nand_version >= 0x0500)
742 /* v6.0 and newer (except v6.1) have prefetch support */
743 if (ctrl->nand_version >= 0x0600 && ctrl->nand_version != 0x0601)
744 ctrl->features |= BRCMNAND_HAS_PREFETCH;
747 * v6.x has cache mode, but it's implemented differently. Ignore it for
750 if (ctrl->nand_version >= 0x0700)
751 ctrl->features |= BRCMNAND_HAS_CACHE_MODE;
753 if (ctrl->nand_version >= 0x0500)
754 ctrl->features |= BRCMNAND_HAS_1K_SECTORS;
756 if (ctrl->nand_version >= 0x0700)
757 ctrl->features |= BRCMNAND_HAS_WP;
758 else if (of_property_read_bool(ctrl->dev->of_node, "brcm,nand-has-wp"))
759 ctrl->features |= BRCMNAND_HAS_WP;
761 /* v7.2 has different ecc level shift in the acc register */
762 if (ctrl->nand_version == 0x0702)
763 ctrl->ecc_level_shift = ACC_CONTROL_ECC_EXT_SHIFT;
765 ctrl->ecc_level_shift = ACC_CONTROL_ECC_SHIFT;
770 static void brcmnand_flash_dma_revision_init(struct brcmnand_controller *ctrl)
772 /* flash_dma register offsets */
773 if (ctrl->nand_version >= 0x0703)
774 ctrl->flash_dma_offsets = flash_dma_regs_v4;
775 else if (ctrl->nand_version == 0x0602)
776 ctrl->flash_dma_offsets = flash_dma_regs_v0;
778 ctrl->flash_dma_offsets = flash_dma_regs_v1;
781 static inline u32 brcmnand_read_reg(struct brcmnand_controller *ctrl,
782 enum brcmnand_reg reg)
784 u16 offs = ctrl->reg_offsets[reg];
787 return nand_readreg(ctrl, offs);
792 static inline void brcmnand_write_reg(struct brcmnand_controller *ctrl,
793 enum brcmnand_reg reg, u32 val)
795 u16 offs = ctrl->reg_offsets[reg];
798 nand_writereg(ctrl, offs, val);
801 static inline void brcmnand_rmw_reg(struct brcmnand_controller *ctrl,
802 enum brcmnand_reg reg, u32 mask, unsigned
805 u32 tmp = brcmnand_read_reg(ctrl, reg);
809 brcmnand_write_reg(ctrl, reg, tmp);
812 static inline u32 brcmnand_read_fc(struct brcmnand_controller *ctrl, int word)
814 if (brcmnand_non_mmio_ops(ctrl))
815 return brcmnand_soc_read(ctrl->soc, BRCMNAND_NON_MMIO_FC_ADDR);
816 return __raw_readl(ctrl->nand_fc + word * 4);
819 static inline void brcmnand_write_fc(struct brcmnand_controller *ctrl,
822 if (brcmnand_non_mmio_ops(ctrl))
823 brcmnand_soc_write(ctrl->soc, val, BRCMNAND_NON_MMIO_FC_ADDR);
825 __raw_writel(val, ctrl->nand_fc + word * 4);
828 static inline void edu_writel(struct brcmnand_controller *ctrl,
829 enum edu_reg reg, u32 val)
831 u16 offs = ctrl->edu_offsets[reg];
833 brcmnand_writel(val, ctrl->edu_base + offs);
836 static inline u32 edu_readl(struct brcmnand_controller *ctrl,
839 u16 offs = ctrl->edu_offsets[reg];
841 return brcmnand_readl(ctrl->edu_base + offs);
844 static void brcmnand_clear_ecc_addr(struct brcmnand_controller *ctrl)
847 /* Clear error addresses */
848 brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_ADDR, 0);
849 brcmnand_write_reg(ctrl, BRCMNAND_CORR_ADDR, 0);
850 brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_EXT_ADDR, 0);
851 brcmnand_write_reg(ctrl, BRCMNAND_CORR_EXT_ADDR, 0);
854 static u64 brcmnand_get_uncorrecc_addr(struct brcmnand_controller *ctrl)
858 err_addr = brcmnand_read_reg(ctrl, BRCMNAND_UNCORR_ADDR);
859 err_addr |= ((u64)(brcmnand_read_reg(ctrl,
860 BRCMNAND_UNCORR_EXT_ADDR)
866 static u64 brcmnand_get_correcc_addr(struct brcmnand_controller *ctrl)
870 err_addr = brcmnand_read_reg(ctrl, BRCMNAND_CORR_ADDR);
871 err_addr |= ((u64)(brcmnand_read_reg(ctrl,
872 BRCMNAND_CORR_EXT_ADDR)
878 static void brcmnand_set_cmd_addr(struct mtd_info *mtd, u64 addr)
880 struct nand_chip *chip = mtd_to_nand(mtd);
881 struct brcmnand_host *host = nand_get_controller_data(chip);
882 struct brcmnand_controller *ctrl = host->ctrl;
884 brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
885 (host->cs << 16) | ((addr >> 32) & 0xffff));
886 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
887 brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS,
888 lower_32_bits(addr));
889 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
892 static inline u16 brcmnand_cs_offset(struct brcmnand_controller *ctrl, int cs,
893 enum brcmnand_cs_reg reg)
895 u16 offs_cs0 = ctrl->reg_offsets[BRCMNAND_CS0_BASE];
896 u16 offs_cs1 = ctrl->reg_offsets[BRCMNAND_CS1_BASE];
899 if (cs == 0 && ctrl->cs0_offsets)
900 cs_offs = ctrl->cs0_offsets[reg];
902 cs_offs = ctrl->cs_offsets[reg];
905 return offs_cs1 + (cs - 1) * ctrl->reg_spacing + cs_offs;
907 return offs_cs0 + cs * ctrl->reg_spacing + cs_offs;
910 static inline u32 brcmnand_count_corrected(struct brcmnand_controller *ctrl)
912 if (ctrl->nand_version < 0x0600)
914 return brcmnand_read_reg(ctrl, BRCMNAND_CORR_COUNT);
917 static void brcmnand_wr_corr_thresh(struct brcmnand_host *host, u8 val)
919 struct brcmnand_controller *ctrl = host->ctrl;
920 unsigned int shift = 0, bits;
921 enum brcmnand_reg reg = BRCMNAND_CORR_THRESHOLD;
924 if (!ctrl->reg_offsets[reg])
927 if (ctrl->nand_version == 0x0702)
929 else if (ctrl->nand_version >= 0x0600)
931 else if (ctrl->nand_version >= 0x0500)
936 if (ctrl->nand_version >= 0x0702) {
938 reg = BRCMNAND_CORR_THRESHOLD_EXT;
939 shift = (cs % 4) * bits;
940 } else if (ctrl->nand_version >= 0x0600) {
942 reg = BRCMNAND_CORR_THRESHOLD_EXT;
943 shift = (cs % 5) * bits;
945 brcmnand_rmw_reg(ctrl, reg, (bits - 1) << shift, shift, val);
948 static inline int brcmnand_cmd_shift(struct brcmnand_controller *ctrl)
950 if (ctrl->nand_version < 0x0602)
955 static inline u32 brcmnand_spare_area_mask(struct brcmnand_controller *ctrl)
957 if (ctrl->nand_version == 0x0702)
958 return GENMASK(7, 0);
959 else if (ctrl->nand_version >= 0x0600)
960 return GENMASK(6, 0);
961 else if (ctrl->nand_version >= 0x0303)
962 return GENMASK(5, 0);
964 return GENMASK(4, 0);
967 static inline u32 brcmnand_ecc_level_mask(struct brcmnand_controller *ctrl)
969 u32 mask = (ctrl->nand_version >= 0x0600) ? 0x1f : 0x0f;
971 mask <<= ACC_CONTROL_ECC_SHIFT;
973 /* v7.2 includes additional ECC levels */
974 if (ctrl->nand_version == 0x0702)
975 mask |= 0x7 << ACC_CONTROL_ECC_EXT_SHIFT;
980 static void brcmnand_set_ecc_enabled(struct brcmnand_host *host, int en)
982 struct brcmnand_controller *ctrl = host->ctrl;
983 u16 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
984 u32 acc_control = nand_readreg(ctrl, offs);
985 u32 ecc_flags = ACC_CONTROL_WR_ECC | ACC_CONTROL_RD_ECC;
988 acc_control |= ecc_flags; /* enable RD/WR ECC */
989 acc_control &= ~brcmnand_ecc_level_mask(ctrl);
990 acc_control |= host->hwcfg.ecc_level << ctrl->ecc_level_shift;
992 acc_control &= ~ecc_flags; /* disable RD/WR ECC */
993 acc_control &= ~brcmnand_ecc_level_mask(ctrl);
996 nand_writereg(ctrl, offs, acc_control);
999 static inline int brcmnand_sector_1k_shift(struct brcmnand_controller *ctrl)
1001 if (ctrl->nand_version >= 0x0702)
1003 else if (ctrl->nand_version >= 0x0600)
1005 else if (ctrl->nand_version >= 0x0500)
1011 static int brcmnand_get_sector_size_1k(struct brcmnand_host *host)
1013 struct brcmnand_controller *ctrl = host->ctrl;
1014 int shift = brcmnand_sector_1k_shift(ctrl);
1015 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
1016 BRCMNAND_CS_ACC_CONTROL);
1021 return (nand_readreg(ctrl, acc_control_offs) >> shift) & 0x1;
1024 static void brcmnand_set_sector_size_1k(struct brcmnand_host *host, int val)
1026 struct brcmnand_controller *ctrl = host->ctrl;
1027 int shift = brcmnand_sector_1k_shift(ctrl);
1028 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
1029 BRCMNAND_CS_ACC_CONTROL);
1035 tmp = nand_readreg(ctrl, acc_control_offs);
1036 tmp &= ~(1 << shift);
1037 tmp |= (!!val) << shift;
1038 nand_writereg(ctrl, acc_control_offs, tmp);
1041 /***********************************************************************
1043 ***********************************************************************/
1046 CS_SELECT_NAND_WP = BIT(29),
1047 CS_SELECT_AUTO_DEVICE_ID_CFG = BIT(30),
1050 static int bcmnand_ctrl_poll_status(struct brcmnand_controller *ctrl,
1051 u32 mask, u32 expected_val,
1052 unsigned long timeout_ms)
1054 unsigned long limit;
1058 timeout_ms = NAND_POLL_STATUS_TIMEOUT_MS;
1060 limit = jiffies + msecs_to_jiffies(timeout_ms);
1062 val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS);
1063 if ((val & mask) == expected_val)
1067 } while (time_after(limit, jiffies));
1070 * do a final check after time out in case the CPU was busy and the driver
1071 * did not get enough time to perform the polling to avoid false alarms
1073 val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS);
1074 if ((val & mask) == expected_val)
1077 dev_warn(ctrl->dev, "timeout on status poll (expected %x got %x)\n",
1078 expected_val, val & mask);
1083 static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
1085 u32 val = en ? CS_SELECT_NAND_WP : 0;
1087 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
1090 /***********************************************************************
1092 ***********************************************************************/
1094 static inline bool has_flash_dma(struct brcmnand_controller *ctrl)
1096 return ctrl->flash_dma_base;
1099 static inline bool has_edu(struct brcmnand_controller *ctrl)
1101 return ctrl->edu_base;
1104 static inline bool use_dma(struct brcmnand_controller *ctrl)
1106 return has_flash_dma(ctrl) || has_edu(ctrl);
1109 static inline void disable_ctrl_irqs(struct brcmnand_controller *ctrl)
1111 if (ctrl->pio_poll_mode)
1114 if (has_flash_dma(ctrl)) {
1115 ctrl->flash_dma_base = NULL;
1116 disable_irq(ctrl->dma_irq);
1119 disable_irq(ctrl->irq);
1120 ctrl->pio_poll_mode = true;
1123 static inline bool flash_dma_buf_ok(const void *buf)
1125 return buf && !is_vmalloc_addr(buf) &&
1126 likely(IS_ALIGNED((uintptr_t)buf, 4));
1129 static inline void flash_dma_writel(struct brcmnand_controller *ctrl,
1130 enum flash_dma_reg dma_reg, u32 val)
1132 u16 offs = ctrl->flash_dma_offsets[dma_reg];
1134 brcmnand_writel(val, ctrl->flash_dma_base + offs);
1137 static inline u32 flash_dma_readl(struct brcmnand_controller *ctrl,
1138 enum flash_dma_reg dma_reg)
1140 u16 offs = ctrl->flash_dma_offsets[dma_reg];
1142 return brcmnand_readl(ctrl->flash_dma_base + offs);
1145 /* Low-level operation types: command, address, write, or read */
1146 enum brcmnand_llop_type {
1153 /***********************************************************************
1154 * Internal support functions
1155 ***********************************************************************/
1157 static inline bool is_hamming_ecc(struct brcmnand_controller *ctrl,
1158 struct brcmnand_cfg *cfg)
1160 if (ctrl->nand_version <= 0x0701)
1161 return cfg->sector_size_1k == 0 && cfg->spare_area_size == 16 &&
1162 cfg->ecc_level == 15;
1164 return cfg->sector_size_1k == 0 && ((cfg->spare_area_size == 16 &&
1165 cfg->ecc_level == 15) ||
1166 (cfg->spare_area_size == 28 && cfg->ecc_level == 16));
1170 * Set mtd->ooblayout to the appropriate mtd_ooblayout_ops given
1171 * the layout/configuration.
1172 * Returns -ERRCODE on failure.
1174 static int brcmnand_hamming_ooblayout_ecc(struct mtd_info *mtd, int section,
1175 struct mtd_oob_region *oobregion)
1177 struct nand_chip *chip = mtd_to_nand(mtd);
1178 struct brcmnand_host *host = nand_get_controller_data(chip);
1179 struct brcmnand_cfg *cfg = &host->hwcfg;
1180 int sas = cfg->spare_area_size << cfg->sector_size_1k;
1181 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
1183 if (section >= sectors)
1186 oobregion->offset = (section * sas) + 6;
1187 oobregion->length = 3;
1192 static int brcmnand_hamming_ooblayout_free(struct mtd_info *mtd, int section,
1193 struct mtd_oob_region *oobregion)
1195 struct nand_chip *chip = mtd_to_nand(mtd);
1196 struct brcmnand_host *host = nand_get_controller_data(chip);
1197 struct brcmnand_cfg *cfg = &host->hwcfg;
1198 int sas = cfg->spare_area_size << cfg->sector_size_1k;
1199 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
1202 if (section > sectors)
1205 next = (section * sas);
1206 if (section < sectors)
1210 oobregion->offset = ((section - 1) * sas) + 9;
1212 if (cfg->page_size > 512) {
1213 /* Large page NAND uses first 2 bytes for BBI */
1214 oobregion->offset = 2;
1216 /* Small page NAND uses last byte before ECC for BBI */
1217 oobregion->offset = 0;
1222 oobregion->length = next - oobregion->offset;
1227 static const struct mtd_ooblayout_ops brcmnand_hamming_ooblayout_ops = {
1228 .ecc = brcmnand_hamming_ooblayout_ecc,
1229 .free = brcmnand_hamming_ooblayout_free,
1232 static int brcmnand_bch_ooblayout_ecc(struct mtd_info *mtd, int section,
1233 struct mtd_oob_region *oobregion)
1235 struct nand_chip *chip = mtd_to_nand(mtd);
1236 struct brcmnand_host *host = nand_get_controller_data(chip);
1237 struct brcmnand_cfg *cfg = &host->hwcfg;
1238 int sas = cfg->spare_area_size << cfg->sector_size_1k;
1239 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
1241 if (section >= sectors)
1244 oobregion->offset = ((section + 1) * sas) - chip->ecc.bytes;
1245 oobregion->length = chip->ecc.bytes;
1250 static int brcmnand_bch_ooblayout_free_lp(struct mtd_info *mtd, int section,
1251 struct mtd_oob_region *oobregion)
1253 struct nand_chip *chip = mtd_to_nand(mtd);
1254 struct brcmnand_host *host = nand_get_controller_data(chip);
1255 struct brcmnand_cfg *cfg = &host->hwcfg;
1256 int sas = cfg->spare_area_size << cfg->sector_size_1k;
1257 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
1259 if (section >= sectors)
1262 if (sas <= chip->ecc.bytes)
1265 oobregion->offset = section * sas;
1266 oobregion->length = sas - chip->ecc.bytes;
1269 oobregion->offset++;
1270 oobregion->length--;
1276 static int brcmnand_bch_ooblayout_free_sp(struct mtd_info *mtd, int section,
1277 struct mtd_oob_region *oobregion)
1279 struct nand_chip *chip = mtd_to_nand(mtd);
1280 struct brcmnand_host *host = nand_get_controller_data(chip);
1281 struct brcmnand_cfg *cfg = &host->hwcfg;
1282 int sas = cfg->spare_area_size << cfg->sector_size_1k;
1284 if (section > 1 || sas - chip->ecc.bytes < 6 ||
1285 (section && sas - chip->ecc.bytes == 6))
1289 oobregion->offset = 0;
1290 oobregion->length = 5;
1292 oobregion->offset = 6;
1293 oobregion->length = sas - chip->ecc.bytes - 6;
1299 static const struct mtd_ooblayout_ops brcmnand_bch_lp_ooblayout_ops = {
1300 .ecc = brcmnand_bch_ooblayout_ecc,
1301 .free = brcmnand_bch_ooblayout_free_lp,
1304 static const struct mtd_ooblayout_ops brcmnand_bch_sp_ooblayout_ops = {
1305 .ecc = brcmnand_bch_ooblayout_ecc,
1306 .free = brcmnand_bch_ooblayout_free_sp,
1309 static int brcmstb_choose_ecc_layout(struct brcmnand_host *host)
1311 struct brcmnand_cfg *p = &host->hwcfg;
1312 struct mtd_info *mtd = nand_to_mtd(&host->chip);
1313 struct nand_ecc_ctrl *ecc = &host->chip.ecc;
1314 unsigned int ecc_level = p->ecc_level;
1315 int sas = p->spare_area_size << p->sector_size_1k;
1316 int sectors = p->page_size / (512 << p->sector_size_1k);
1318 if (p->sector_size_1k)
1321 if (is_hamming_ecc(host->ctrl, p)) {
1322 ecc->bytes = 3 * sectors;
1323 mtd_set_ooblayout(mtd, &brcmnand_hamming_ooblayout_ops);
1328 * CONTROLLER_VERSION:
1329 * < v5.0: ECC_REQ = ceil(BCH_T * 13/8)
1330 * >= v5.0: ECC_REQ = ceil(BCH_T * 14/8)
1331 * But we will just be conservative.
1333 ecc->bytes = DIV_ROUND_UP(ecc_level * 14, 8);
1334 if (p->page_size == 512)
1335 mtd_set_ooblayout(mtd, &brcmnand_bch_sp_ooblayout_ops);
1337 mtd_set_ooblayout(mtd, &brcmnand_bch_lp_ooblayout_ops);
1339 if (ecc->bytes >= sas) {
1340 dev_err(&host->pdev->dev,
1341 "error: ECC too large for OOB (ECC bytes %d, spare sector %d)\n",
1349 static void brcmnand_wp(struct mtd_info *mtd, int wp)
1351 struct nand_chip *chip = mtd_to_nand(mtd);
1352 struct brcmnand_host *host = nand_get_controller_data(chip);
1353 struct brcmnand_controller *ctrl = host->ctrl;
1355 if ((ctrl->features & BRCMNAND_HAS_WP) && wp_on == 1) {
1356 static int old_wp = -1;
1360 dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
1365 * make sure ctrl/flash ready before and after
1366 * changing state of #WP pin
1368 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY |
1371 NAND_STATUS_READY, 0);
1375 brcmnand_set_wp(ctrl, wp);
1376 nand_status_op(chip, NULL);
1377 /* NAND_STATUS_WP 0x00 = protected, 0x80 = not protected */
1378 ret = bcmnand_ctrl_poll_status(ctrl,
1384 (wp ? 0 : NAND_STATUS_WP), 0);
1387 dev_err_ratelimited(&host->pdev->dev,
1388 "nand #WP expected %s\n",
1393 /* Helper functions for reading and writing OOB registers */
1394 static inline u8 oob_reg_read(struct brcmnand_controller *ctrl, u32 offs)
1396 u16 offset0, offset10, reg_offs;
1398 offset0 = ctrl->reg_offsets[BRCMNAND_OOB_READ_BASE];
1399 offset10 = ctrl->reg_offsets[BRCMNAND_OOB_READ_10_BASE];
1401 if (offs >= ctrl->max_oob)
1404 if (offs >= 16 && offset10)
1405 reg_offs = offset10 + ((offs - 0x10) & ~0x03);
1407 reg_offs = offset0 + (offs & ~0x03);
1409 return nand_readreg(ctrl, reg_offs) >> (24 - ((offs & 0x03) << 3));
1412 static inline void oob_reg_write(struct brcmnand_controller *ctrl, u32 offs,
1415 u16 offset0, offset10, reg_offs;
1417 offset0 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_BASE];
1418 offset10 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_10_BASE];
1420 if (offs >= ctrl->max_oob)
1423 if (offs >= 16 && offset10)
1424 reg_offs = offset10 + ((offs - 0x10) & ~0x03);
1426 reg_offs = offset0 + (offs & ~0x03);
1428 nand_writereg(ctrl, reg_offs, data);
1432 * read_oob_from_regs - read data from OOB registers
1433 * @ctrl: NAND controller
1434 * @i: sub-page sector index
1435 * @oob: buffer to read to
1436 * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
1437 * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
1439 static int read_oob_from_regs(struct brcmnand_controller *ctrl, int i, u8 *oob,
1440 int sas, int sector_1k)
1442 int tbytes = sas << sector_1k;
1445 /* Adjust OOB values for 1K sector size */
1446 if (sector_1k && (i & 0x01))
1447 tbytes = max(0, tbytes - (int)ctrl->max_oob);
1448 tbytes = min_t(int, tbytes, ctrl->max_oob);
1450 for (j = 0; j < tbytes; j++)
1451 oob[j] = oob_reg_read(ctrl, j);
1456 * write_oob_to_regs - write data to OOB registers
1457 * @i: sub-page sector index
1458 * @oob: buffer to write from
1459 * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
1460 * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
1462 static int write_oob_to_regs(struct brcmnand_controller *ctrl, int i,
1463 const u8 *oob, int sas, int sector_1k)
1465 int tbytes = sas << sector_1k;
1467 u32 last = 0xffffffff;
1468 u8 *plast = (u8 *)&last;
1470 /* Adjust OOB values for 1K sector size */
1471 if (sector_1k && (i & 0x01))
1472 tbytes = max(0, tbytes - (int)ctrl->max_oob);
1473 tbytes = min_t(int, tbytes, ctrl->max_oob);
1476 * tbytes may not be multiple of words. Make sure we don't read out of
1477 * the boundary and stop at last word.
1479 for (j = 0; (j + 3) < tbytes; j += 4)
1480 oob_reg_write(ctrl, j,
1481 (oob[j + 0] << 24) |
1482 (oob[j + 1] << 16) |
1486 /* handle the remaing bytes */
1488 plast[k++] = oob[j++];
1491 oob_reg_write(ctrl, (tbytes & ~0x3), (__force u32)cpu_to_be32(last));
1496 static void brcmnand_edu_init(struct brcmnand_controller *ctrl)
1498 /* initialize edu */
1499 edu_writel(ctrl, EDU_ERR_STATUS, 0);
1500 edu_readl(ctrl, EDU_ERR_STATUS);
1501 edu_writel(ctrl, EDU_DONE, 0);
1502 edu_writel(ctrl, EDU_DONE, 0);
1503 edu_writel(ctrl, EDU_DONE, 0);
1504 edu_writel(ctrl, EDU_DONE, 0);
1505 edu_readl(ctrl, EDU_DONE);
1509 static irqreturn_t brcmnand_edu_irq(int irq, void *data)
1511 struct brcmnand_controller *ctrl = data;
1513 if (ctrl->edu_count) {
1515 while (!(edu_readl(ctrl, EDU_DONE) & EDU_DONE_MASK))
1517 edu_writel(ctrl, EDU_DONE, 0);
1518 edu_readl(ctrl, EDU_DONE);
1521 if (ctrl->edu_count) {
1522 ctrl->edu_dram_addr += FC_BYTES;
1523 ctrl->edu_ext_addr += FC_BYTES;
1525 edu_writel(ctrl, EDU_DRAM_ADDR, (u32)ctrl->edu_dram_addr);
1526 edu_readl(ctrl, EDU_DRAM_ADDR);
1527 edu_writel(ctrl, EDU_EXT_ADDR, ctrl->edu_ext_addr);
1528 edu_readl(ctrl, EDU_EXT_ADDR);
1530 mb(); /* flush previous writes */
1531 edu_writel(ctrl, EDU_CMD, ctrl->edu_cmd);
1532 edu_readl(ctrl, EDU_CMD);
1537 complete(&ctrl->edu_done);
1542 static irqreturn_t brcmnand_ctlrdy_irq(int irq, void *data)
1544 struct brcmnand_controller *ctrl = data;
1546 /* Discard all NAND_CTLRDY interrupts during DMA */
1547 if (ctrl->dma_pending)
1550 /* check if you need to piggy back on the ctrlrdy irq */
1551 if (ctrl->edu_pending) {
1552 if (irq == ctrl->irq && ((int)ctrl->edu_irq >= 0))
1553 /* Discard interrupts while using dedicated edu irq */
1556 /* no registered edu irq, call handler */
1557 return brcmnand_edu_irq(irq, data);
1560 complete(&ctrl->done);
1564 /* Handle SoC-specific interrupt hardware */
1565 static irqreturn_t brcmnand_irq(int irq, void *data)
1567 struct brcmnand_controller *ctrl = data;
1569 if (ctrl->soc->ctlrdy_ack(ctrl->soc))
1570 return brcmnand_ctlrdy_irq(irq, data);
1575 static irqreturn_t brcmnand_dma_irq(int irq, void *data)
1577 struct brcmnand_controller *ctrl = data;
1579 complete(&ctrl->dma_done);
1584 static void brcmnand_send_cmd(struct brcmnand_host *host, int cmd)
1586 struct brcmnand_controller *ctrl = host->ctrl;
1590 cmd_addr = brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
1592 dev_dbg(ctrl->dev, "send native cmd %d addr 0x%llx\n", cmd, cmd_addr);
1595 * If we came here through _panic_write and there is a pending
1596 * command, try to wait for it. If it times out, rather than
1597 * hitting BUG_ON, just return so we don't crash while crashing.
1599 if (oops_in_progress) {
1600 if (ctrl->cmd_pending &&
1601 bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, 0))
1604 BUG_ON(ctrl->cmd_pending != 0);
1605 ctrl->cmd_pending = cmd;
1607 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, 0);
1610 mb(); /* flush previous writes */
1611 brcmnand_write_reg(ctrl, BRCMNAND_CMD_START,
1612 cmd << brcmnand_cmd_shift(ctrl));
1615 /***********************************************************************
1616 * NAND MTD API: read/program/erase
1617 ***********************************************************************/
1619 static void brcmnand_cmd_ctrl(struct nand_chip *chip, int dat,
1622 /* intentionally left blank */
1625 static bool brcmstb_nand_wait_for_completion(struct nand_chip *chip)
1627 struct brcmnand_host *host = nand_get_controller_data(chip);
1628 struct brcmnand_controller *ctrl = host->ctrl;
1629 struct mtd_info *mtd = nand_to_mtd(chip);
1633 if (mtd->oops_panic_write) {
1634 /* switch to interrupt polling and PIO mode */
1635 disable_ctrl_irqs(ctrl);
1636 sts = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY,
1638 err = (sts < 0) ? true : false;
1640 unsigned long timeo = msecs_to_jiffies(
1641 NAND_POLL_STATUS_TIMEOUT_MS);
1642 /* wait for completion interrupt */
1643 sts = wait_for_completion_timeout(&ctrl->done, timeo);
1644 err = (sts <= 0) ? true : false;
1650 static int brcmnand_waitfunc(struct nand_chip *chip)
1652 struct brcmnand_host *host = nand_get_controller_data(chip);
1653 struct brcmnand_controller *ctrl = host->ctrl;
1656 dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending);
1657 if (ctrl->cmd_pending)
1658 err = brcmstb_nand_wait_for_completion(chip);
1661 u32 cmd = brcmnand_read_reg(ctrl, BRCMNAND_CMD_START)
1662 >> brcmnand_cmd_shift(ctrl);
1664 dev_err_ratelimited(ctrl->dev,
1665 "timeout waiting for command %#02x\n", cmd);
1666 dev_err_ratelimited(ctrl->dev, "intfc status %08x\n",
1667 brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS));
1669 ctrl->cmd_pending = 0;
1670 return brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
1679 LLOP_RETURN_IDLE = BIT(31),
1681 LLOP_DATA_MASK = GENMASK(15, 0),
1684 static int brcmnand_low_level_op(struct brcmnand_host *host,
1685 enum brcmnand_llop_type type, u32 data,
1688 struct nand_chip *chip = &host->chip;
1689 struct brcmnand_controller *ctrl = host->ctrl;
1692 tmp = data & LLOP_DATA_MASK;
1695 tmp |= LLOP_WE | LLOP_CLE;
1699 tmp |= LLOP_WE | LLOP_ALE;
1712 tmp |= LLOP_RETURN_IDLE;
1714 dev_dbg(ctrl->dev, "ll_op cmd %#x\n", tmp);
1716 brcmnand_write_reg(ctrl, BRCMNAND_LL_OP, tmp);
1717 (void)brcmnand_read_reg(ctrl, BRCMNAND_LL_OP);
1719 brcmnand_send_cmd(host, CMD_LOW_LEVEL_OP);
1720 return brcmnand_waitfunc(chip);
1723 static void brcmnand_cmdfunc(struct nand_chip *chip, unsigned command,
1724 int column, int page_addr)
1726 struct mtd_info *mtd = nand_to_mtd(chip);
1727 struct brcmnand_host *host = nand_get_controller_data(chip);
1728 struct brcmnand_controller *ctrl = host->ctrl;
1729 u64 addr = (u64)page_addr << chip->page_shift;
1732 if (command == NAND_CMD_READID || command == NAND_CMD_PARAM ||
1733 command == NAND_CMD_RNDOUT)
1735 /* Avoid propagating a negative, don't-care address */
1736 else if (page_addr < 0)
1739 dev_dbg(ctrl->dev, "cmd 0x%x addr 0x%llx\n", command,
1740 (unsigned long long)addr);
1742 host->last_cmd = command;
1743 host->last_byte = 0;
1744 host->last_addr = addr;
1747 case NAND_CMD_RESET:
1748 native_cmd = CMD_FLASH_RESET;
1750 case NAND_CMD_STATUS:
1751 native_cmd = CMD_STATUS_READ;
1753 case NAND_CMD_READID:
1754 native_cmd = CMD_DEVICE_ID_READ;
1756 case NAND_CMD_READOOB:
1757 native_cmd = CMD_SPARE_AREA_READ;
1759 case NAND_CMD_ERASE1:
1760 native_cmd = CMD_BLOCK_ERASE;
1761 brcmnand_wp(mtd, 0);
1763 case NAND_CMD_PARAM:
1764 native_cmd = CMD_PARAMETER_READ;
1766 case NAND_CMD_SET_FEATURES:
1767 case NAND_CMD_GET_FEATURES:
1768 brcmnand_low_level_op(host, LL_OP_CMD, command, false);
1769 brcmnand_low_level_op(host, LL_OP_ADDR, column, false);
1771 case NAND_CMD_RNDOUT:
1772 native_cmd = CMD_PARAMETER_CHANGE_COL;
1773 addr &= ~((u64)(FC_BYTES - 1));
1775 * HW quirk: PARAMETER_CHANGE_COL requires SECTOR_SIZE_1K=0
1776 * NB: hwcfg.sector_size_1k may not be initialized yet
1778 if (brcmnand_get_sector_size_1k(host)) {
1779 host->hwcfg.sector_size_1k =
1780 brcmnand_get_sector_size_1k(host);
1781 brcmnand_set_sector_size_1k(host, 0);
1789 brcmnand_set_cmd_addr(mtd, addr);
1790 brcmnand_send_cmd(host, native_cmd);
1791 brcmnand_waitfunc(chip);
1793 if (native_cmd == CMD_PARAMETER_READ ||
1794 native_cmd == CMD_PARAMETER_CHANGE_COL) {
1795 /* Copy flash cache word-wise */
1796 u32 *flash_cache = (u32 *)ctrl->flash_cache;
1799 brcmnand_soc_data_bus_prepare(ctrl->soc, true);
1802 * Must cache the FLASH_CACHE now, since changes in
1803 * SECTOR_SIZE_1K may invalidate it
1805 for (i = 0; i < FC_WORDS; i++)
1807 * Flash cache is big endian for parameter pages, at
1810 flash_cache[i] = be32_to_cpu(brcmnand_read_fc(ctrl, i));
1812 brcmnand_soc_data_bus_unprepare(ctrl->soc, true);
1814 /* Cleanup from HW quirk: restore SECTOR_SIZE_1K */
1815 if (host->hwcfg.sector_size_1k)
1816 brcmnand_set_sector_size_1k(host,
1817 host->hwcfg.sector_size_1k);
1820 /* Re-enable protection is necessary only after erase */
1821 if (command == NAND_CMD_ERASE1)
1822 brcmnand_wp(mtd, 1);
1825 static uint8_t brcmnand_read_byte(struct nand_chip *chip)
1827 struct brcmnand_host *host = nand_get_controller_data(chip);
1828 struct brcmnand_controller *ctrl = host->ctrl;
1832 switch (host->last_cmd) {
1833 case NAND_CMD_READID:
1834 if (host->last_byte < 4)
1835 ret = brcmnand_read_reg(ctrl, BRCMNAND_ID) >>
1836 (24 - (host->last_byte << 3));
1837 else if (host->last_byte < 8)
1838 ret = brcmnand_read_reg(ctrl, BRCMNAND_ID_EXT) >>
1839 (56 - (host->last_byte << 3));
1842 case NAND_CMD_READOOB:
1843 ret = oob_reg_read(ctrl, host->last_byte);
1846 case NAND_CMD_STATUS:
1847 ret = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
1849 if (wp_on) /* hide WP status */
1850 ret |= NAND_STATUS_WP;
1853 case NAND_CMD_PARAM:
1854 case NAND_CMD_RNDOUT:
1855 addr = host->last_addr + host->last_byte;
1856 offs = addr & (FC_BYTES - 1);
1858 /* At FC_BYTES boundary, switch to next column */
1859 if (host->last_byte > 0 && offs == 0)
1860 nand_change_read_column_op(chip, addr, NULL, 0, false);
1862 ret = ctrl->flash_cache[offs];
1864 case NAND_CMD_GET_FEATURES:
1865 if (host->last_byte >= ONFI_SUBFEATURE_PARAM_LEN) {
1868 bool last = host->last_byte ==
1869 ONFI_SUBFEATURE_PARAM_LEN - 1;
1870 brcmnand_low_level_op(host, LL_OP_RD, 0, last);
1871 ret = brcmnand_read_reg(ctrl, BRCMNAND_LL_RDATA) & 0xff;
1875 dev_dbg(ctrl->dev, "read byte = 0x%02x\n", ret);
1881 static void brcmnand_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
1885 for (i = 0; i < len; i++, buf++)
1886 *buf = brcmnand_read_byte(chip);
1889 static void brcmnand_write_buf(struct nand_chip *chip, const uint8_t *buf,
1893 struct brcmnand_host *host = nand_get_controller_data(chip);
1895 switch (host->last_cmd) {
1896 case NAND_CMD_SET_FEATURES:
1897 for (i = 0; i < len; i++)
1898 brcmnand_low_level_op(host, LL_OP_WR, buf[i],
1910 static int brcmnand_edu_trans(struct brcmnand_host *host, u64 addr, u32 *buf,
1913 struct brcmnand_controller *ctrl = host->ctrl;
1914 unsigned long timeo = msecs_to_jiffies(200);
1916 int dir = (cmd == CMD_PAGE_READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1917 u8 edu_cmd = (cmd == CMD_PAGE_READ ? EDU_CMD_READ : EDU_CMD_WRITE);
1918 unsigned int trans = len >> FC_SHIFT;
1921 pa = dma_map_single(ctrl->dev, buf, len, dir);
1922 if (dma_mapping_error(ctrl->dev, pa)) {
1923 dev_err(ctrl->dev, "unable to map buffer for EDU DMA\n");
1927 ctrl->edu_pending = true;
1928 ctrl->edu_dram_addr = pa;
1929 ctrl->edu_ext_addr = addr;
1930 ctrl->edu_cmd = edu_cmd;
1931 ctrl->edu_count = trans;
1933 edu_writel(ctrl, EDU_DRAM_ADDR, (u32)ctrl->edu_dram_addr);
1934 edu_readl(ctrl, EDU_DRAM_ADDR);
1935 edu_writel(ctrl, EDU_EXT_ADDR, ctrl->edu_ext_addr);
1936 edu_readl(ctrl, EDU_EXT_ADDR);
1937 edu_writel(ctrl, EDU_LENGTH, FC_BYTES);
1938 edu_readl(ctrl, EDU_LENGTH);
1940 /* Start edu engine */
1941 mb(); /* flush previous writes */
1942 edu_writel(ctrl, EDU_CMD, ctrl->edu_cmd);
1943 edu_readl(ctrl, EDU_CMD);
1945 if (wait_for_completion_timeout(&ctrl->edu_done, timeo) <= 0) {
1947 "timeout waiting for EDU; status %#x, error status %#x\n",
1948 edu_readl(ctrl, EDU_STATUS),
1949 edu_readl(ctrl, EDU_ERR_STATUS));
1952 dma_unmap_single(ctrl->dev, pa, len, dir);
1954 /* for program page check NAND status */
1955 if (((brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
1956 INTFC_FLASH_STATUS) & NAND_STATUS_FAIL) &&
1957 edu_cmd == EDU_CMD_WRITE) {
1958 dev_info(ctrl->dev, "program failed at %llx\n",
1959 (unsigned long long)addr);
1963 /* Make sure the EDU status is clean */
1964 if (edu_readl(ctrl, EDU_STATUS) & EDU_STATUS_ACTIVE)
1965 dev_warn(ctrl->dev, "EDU still active: %#x\n",
1966 edu_readl(ctrl, EDU_STATUS));
1968 if (unlikely(edu_readl(ctrl, EDU_ERR_STATUS) & EDU_ERR_STATUS_ERRACK)) {
1969 dev_warn(ctrl->dev, "EDU RBUS error at addr %llx\n",
1970 (unsigned long long)addr);
1974 ctrl->edu_pending = false;
1975 brcmnand_edu_init(ctrl);
1976 edu_writel(ctrl, EDU_STOP, 0); /* force stop */
1977 edu_readl(ctrl, EDU_STOP);
1979 if (!ret && edu_cmd == EDU_CMD_READ) {
1983 * check for ECC errors here, subpage ECC errors are
1984 * retained in ECC error address register
1986 err_addr = brcmnand_get_uncorrecc_addr(ctrl);
1988 err_addr = brcmnand_get_correcc_addr(ctrl);
1999 * Construct a FLASH_DMA descriptor as part of a linked list. You must know the
2000 * following ahead of time:
2001 * - Is this descriptor the beginning or end of a linked list?
2002 * - What is the (DMA) address of the next descriptor in the linked list?
2004 static int brcmnand_fill_dma_desc(struct brcmnand_host *host,
2005 struct brcm_nand_dma_desc *desc, u64 addr,
2006 dma_addr_t buf, u32 len, u8 dma_cmd,
2007 bool begin, bool end,
2008 dma_addr_t next_desc)
2010 memset(desc, 0, sizeof(*desc));
2011 /* Descriptors are written in native byte order (wordwise) */
2012 desc->next_desc = lower_32_bits(next_desc);
2013 desc->next_desc_ext = upper_32_bits(next_desc);
2014 desc->cmd_irq = (dma_cmd << 24) |
2015 (end ? (0x03 << 8) : 0) | /* IRQ | STOP */
2016 (!!begin) | ((!!end) << 1); /* head, tail */
2017 #ifdef CONFIG_CPU_BIG_ENDIAN
2018 desc->cmd_irq |= 0x01 << 12;
2020 desc->dram_addr = lower_32_bits(buf);
2021 desc->dram_addr_ext = upper_32_bits(buf);
2022 desc->tfr_len = len;
2023 desc->total_len = len;
2024 desc->flash_addr = lower_32_bits(addr);
2025 desc->flash_addr_ext = upper_32_bits(addr);
2026 desc->cs = host->cs;
2027 desc->status_valid = 0x01;
2032 * Kick the FLASH_DMA engine, with a given DMA descriptor
2034 static void brcmnand_dma_run(struct brcmnand_host *host, dma_addr_t desc)
2036 struct brcmnand_controller *ctrl = host->ctrl;
2037 unsigned long timeo = msecs_to_jiffies(100);
2039 flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC, lower_32_bits(desc));
2040 (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC);
2041 if (ctrl->nand_version > 0x0602) {
2042 flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC_EXT,
2043 upper_32_bits(desc));
2044 (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC_EXT);
2047 /* Start FLASH_DMA engine */
2048 ctrl->dma_pending = true;
2049 mb(); /* flush previous writes */
2050 flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0x03); /* wake | run */
2052 if (wait_for_completion_timeout(&ctrl->dma_done, timeo) <= 0) {
2054 "timeout waiting for DMA; status %#x, error status %#x\n",
2055 flash_dma_readl(ctrl, FLASH_DMA_STATUS),
2056 flash_dma_readl(ctrl, FLASH_DMA_ERROR_STATUS));
2058 ctrl->dma_pending = false;
2059 flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0); /* force stop */
2062 static int brcmnand_dma_trans(struct brcmnand_host *host, u64 addr, u32 *buf,
2063 u32 len, u8 dma_cmd)
2065 struct brcmnand_controller *ctrl = host->ctrl;
2067 int dir = dma_cmd == CMD_PAGE_READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2069 buf_pa = dma_map_single(ctrl->dev, buf, len, dir);
2070 if (dma_mapping_error(ctrl->dev, buf_pa)) {
2071 dev_err(ctrl->dev, "unable to map buffer for DMA\n");
2075 brcmnand_fill_dma_desc(host, ctrl->dma_desc, addr, buf_pa, len,
2076 dma_cmd, true, true, 0);
2078 brcmnand_dma_run(host, ctrl->dma_pa);
2080 dma_unmap_single(ctrl->dev, buf_pa, len, dir);
2082 if (ctrl->dma_desc->status_valid & FLASH_DMA_ECC_ERROR)
2084 else if (ctrl->dma_desc->status_valid & FLASH_DMA_CORR_ERROR)
2091 * Assumes proper CS is already set
2093 static int brcmnand_read_by_pio(struct mtd_info *mtd, struct nand_chip *chip,
2094 u64 addr, unsigned int trans, u32 *buf,
2095 u8 *oob, u64 *err_addr)
2097 struct brcmnand_host *host = nand_get_controller_data(chip);
2098 struct brcmnand_controller *ctrl = host->ctrl;
2101 brcmnand_clear_ecc_addr(ctrl);
2103 for (i = 0; i < trans; i++, addr += FC_BYTES) {
2104 brcmnand_set_cmd_addr(mtd, addr);
2105 /* SPARE_AREA_READ does not use ECC, so just use PAGE_READ */
2106 brcmnand_send_cmd(host, CMD_PAGE_READ);
2107 brcmnand_waitfunc(chip);
2110 brcmnand_soc_data_bus_prepare(ctrl->soc, false);
2112 for (j = 0; j < FC_WORDS; j++, buf++)
2113 *buf = brcmnand_read_fc(ctrl, j);
2115 brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
2119 oob += read_oob_from_regs(ctrl, i, oob,
2120 mtd->oobsize / trans,
2121 host->hwcfg.sector_size_1k);
2123 if (ret != -EBADMSG) {
2124 *err_addr = brcmnand_get_uncorrecc_addr(ctrl);
2131 *err_addr = brcmnand_get_correcc_addr(ctrl);
2142 * Check a page to see if it is erased (w/ bitflips) after an uncorrectable ECC
2145 * Because the HW ECC signals an ECC error if an erase paged has even a single
2146 * bitflip, we must check each ECC error to see if it is actually an erased
2147 * page with bitflips, not a truly corrupted page.
2149 * On a real error, return a negative error code (-EBADMSG for ECC error), and
2150 * buf will contain raw data.
2151 * Otherwise, buf gets filled with 0xffs and return the maximum number of
2152 * bitflips-per-ECC-sector to the caller.
2155 static int brcmstb_nand_verify_erased_page(struct mtd_info *mtd,
2156 struct nand_chip *chip, void *buf, u64 addr)
2158 struct mtd_oob_region ecc;
2161 int page = addr >> chip->page_shift;
2167 buf = nand_get_data_buf(chip);
2169 /* read without ecc for verification */
2170 ret = chip->ecc.read_page_raw(chip, buf, true, page);
2174 for (i = 0; i < chip->ecc.steps; i++) {
2175 ecc_chunk = buf + chip->ecc.size * i;
2177 mtd_ooblayout_ecc(mtd, i, &ecc);
2178 ecc_bytes = chip->oob_poi + ecc.offset;
2180 ret = nand_check_erased_ecc_chunk(ecc_chunk, chip->ecc.size,
2181 ecc_bytes, ecc.length,
2183 chip->ecc.strength);
2187 bitflips = max(bitflips, ret);
2193 static int brcmnand_read(struct mtd_info *mtd, struct nand_chip *chip,
2194 u64 addr, unsigned int trans, u32 *buf, u8 *oob)
2196 struct brcmnand_host *host = nand_get_controller_data(chip);
2197 struct brcmnand_controller *ctrl = host->ctrl;
2201 bool edu_err = false;
2203 dev_dbg(ctrl->dev, "read %llx -> %p\n", (unsigned long long)addr, buf);
2206 brcmnand_clear_ecc_addr(ctrl);
2208 if (ctrl->dma_trans && !oob && flash_dma_buf_ok(buf)) {
2209 err = ctrl->dma_trans(host, addr, buf,
2214 if (mtd_is_bitflip_or_eccerr(err))
2220 if (has_edu(ctrl) && err_addr)
2225 memset(oob, 0x99, mtd->oobsize);
2227 err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf,
2231 if (mtd_is_eccerr(err)) {
2233 * On controller version and 7.0, 7.1 , DMA read after a
2234 * prior PIO read that reported uncorrectable error,
2235 * the DMA engine captures this error following DMA read
2236 * cleared only on subsequent DMA read, so just retry once
2237 * to clear a possible false error reported for current DMA
2240 if ((ctrl->nand_version == 0x0700) ||
2241 (ctrl->nand_version == 0x0701)) {
2249 * Controller version 7.2 has hw encoder to detect erased page
2250 * bitflips, apply sw verification for older controllers only
2252 if (ctrl->nand_version < 0x0702) {
2253 err = brcmstb_nand_verify_erased_page(mtd, chip, buf,
2255 /* erased page bitflips corrected */
2260 dev_dbg(ctrl->dev, "uncorrectable error at 0x%llx\n",
2261 (unsigned long long)err_addr);
2262 mtd->ecc_stats.failed++;
2263 /* NAND layer expects zero on ECC errors */
2267 if (mtd_is_bitflip(err)) {
2268 unsigned int corrected = brcmnand_count_corrected(ctrl);
2270 /* in case of EDU correctable error we read again using PIO */
2272 err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf,
2275 dev_dbg(ctrl->dev, "corrected error at 0x%llx\n",
2276 (unsigned long long)err_addr);
2277 mtd->ecc_stats.corrected += corrected;
2278 /* Always exceed the software-imposed threshold */
2279 return max(mtd->bitflip_threshold, corrected);
2285 static int brcmnand_read_page(struct nand_chip *chip, uint8_t *buf,
2286 int oob_required, int page)
2288 struct mtd_info *mtd = nand_to_mtd(chip);
2289 struct brcmnand_host *host = nand_get_controller_data(chip);
2290 u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
2292 nand_read_page_op(chip, page, 0, NULL, 0);
2294 return brcmnand_read(mtd, chip, host->last_addr,
2295 mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
2298 static int brcmnand_read_page_raw(struct nand_chip *chip, uint8_t *buf,
2299 int oob_required, int page)
2301 struct brcmnand_host *host = nand_get_controller_data(chip);
2302 struct mtd_info *mtd = nand_to_mtd(chip);
2303 u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
2306 nand_read_page_op(chip, page, 0, NULL, 0);
2308 brcmnand_set_ecc_enabled(host, 0);
2309 ret = brcmnand_read(mtd, chip, host->last_addr,
2310 mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
2311 brcmnand_set_ecc_enabled(host, 1);
2315 static int brcmnand_read_oob(struct nand_chip *chip, int page)
2317 struct mtd_info *mtd = nand_to_mtd(chip);
2319 return brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
2320 mtd->writesize >> FC_SHIFT,
2321 NULL, (u8 *)chip->oob_poi);
2324 static int brcmnand_read_oob_raw(struct nand_chip *chip, int page)
2326 struct mtd_info *mtd = nand_to_mtd(chip);
2327 struct brcmnand_host *host = nand_get_controller_data(chip);
2329 brcmnand_set_ecc_enabled(host, 0);
2330 brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
2331 mtd->writesize >> FC_SHIFT,
2332 NULL, (u8 *)chip->oob_poi);
2333 brcmnand_set_ecc_enabled(host, 1);
2337 static int brcmnand_write(struct mtd_info *mtd, struct nand_chip *chip,
2338 u64 addr, const u32 *buf, u8 *oob)
2340 struct brcmnand_host *host = nand_get_controller_data(chip);
2341 struct brcmnand_controller *ctrl = host->ctrl;
2342 unsigned int i, j, trans = mtd->writesize >> FC_SHIFT;
2343 int status, ret = 0;
2345 dev_dbg(ctrl->dev, "write %llx <- %p\n", (unsigned long long)addr, buf);
2347 if (unlikely((unsigned long)buf & 0x03)) {
2348 dev_warn(ctrl->dev, "unaligned buffer: %p\n", buf);
2349 buf = (u32 *)((unsigned long)buf & ~0x03);
2352 brcmnand_wp(mtd, 0);
2354 for (i = 0; i < ctrl->max_oob; i += 4)
2355 oob_reg_write(ctrl, i, 0xffffffff);
2357 if (use_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
2358 if (ctrl->dma_trans(host, addr, (u32 *)buf, mtd->writesize,
2366 for (i = 0; i < trans; i++, addr += FC_BYTES) {
2367 /* full address MUST be set before populating FC */
2368 brcmnand_set_cmd_addr(mtd, addr);
2371 brcmnand_soc_data_bus_prepare(ctrl->soc, false);
2373 for (j = 0; j < FC_WORDS; j++, buf++)
2374 brcmnand_write_fc(ctrl, j, *buf);
2376 brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
2378 for (j = 0; j < FC_WORDS; j++)
2379 brcmnand_write_fc(ctrl, j, 0xffffffff);
2383 oob += write_oob_to_regs(ctrl, i, oob,
2384 mtd->oobsize / trans,
2385 host->hwcfg.sector_size_1k);
2388 /* we cannot use SPARE_AREA_PROGRAM when PARTIAL_PAGE_EN=0 */
2389 brcmnand_send_cmd(host, CMD_PROGRAM_PAGE);
2390 status = brcmnand_waitfunc(chip);
2392 if (status & NAND_STATUS_FAIL) {
2393 dev_info(ctrl->dev, "program failed at %llx\n",
2394 (unsigned long long)addr);
2400 brcmnand_wp(mtd, 1);
2404 static int brcmnand_write_page(struct nand_chip *chip, const uint8_t *buf,
2405 int oob_required, int page)
2407 struct mtd_info *mtd = nand_to_mtd(chip);
2408 struct brcmnand_host *host = nand_get_controller_data(chip);
2409 void *oob = oob_required ? chip->oob_poi : NULL;
2411 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2412 brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
2414 return nand_prog_page_end_op(chip);
2417 static int brcmnand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
2418 int oob_required, int page)
2420 struct mtd_info *mtd = nand_to_mtd(chip);
2421 struct brcmnand_host *host = nand_get_controller_data(chip);
2422 void *oob = oob_required ? chip->oob_poi : NULL;
2424 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2425 brcmnand_set_ecc_enabled(host, 0);
2426 brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
2427 brcmnand_set_ecc_enabled(host, 1);
2429 return nand_prog_page_end_op(chip);
2432 static int brcmnand_write_oob(struct nand_chip *chip, int page)
2434 return brcmnand_write(nand_to_mtd(chip), chip,
2435 (u64)page << chip->page_shift, NULL,
2439 static int brcmnand_write_oob_raw(struct nand_chip *chip, int page)
2441 struct mtd_info *mtd = nand_to_mtd(chip);
2442 struct brcmnand_host *host = nand_get_controller_data(chip);
2445 brcmnand_set_ecc_enabled(host, 0);
2446 ret = brcmnand_write(mtd, chip, (u64)page << chip->page_shift, NULL,
2447 (u8 *)chip->oob_poi);
2448 brcmnand_set_ecc_enabled(host, 1);
2453 /***********************************************************************
2454 * Per-CS setup (1 NAND device)
2455 ***********************************************************************/
2457 static int brcmnand_set_cfg(struct brcmnand_host *host,
2458 struct brcmnand_cfg *cfg)
2460 struct brcmnand_controller *ctrl = host->ctrl;
2461 struct nand_chip *chip = &host->chip;
2462 u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2463 u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
2464 BRCMNAND_CS_CFG_EXT);
2465 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
2466 BRCMNAND_CS_ACC_CONTROL);
2467 u8 block_size = 0, page_size = 0, device_size = 0;
2470 if (ctrl->block_sizes) {
2473 for (i = 0, found = 0; ctrl->block_sizes[i]; i++)
2474 if (ctrl->block_sizes[i] * 1024 == cfg->block_size) {
2479 dev_warn(ctrl->dev, "invalid block size %u\n",
2484 block_size = ffs(cfg->block_size) - ffs(BRCMNAND_MIN_BLOCKSIZE);
2487 if (cfg->block_size < BRCMNAND_MIN_BLOCKSIZE || (ctrl->max_block_size &&
2488 cfg->block_size > ctrl->max_block_size)) {
2489 dev_warn(ctrl->dev, "invalid block size %u\n",
2494 if (ctrl->page_sizes) {
2497 for (i = 0, found = 0; ctrl->page_sizes[i]; i++)
2498 if (ctrl->page_sizes[i] == cfg->page_size) {
2503 dev_warn(ctrl->dev, "invalid page size %u\n",
2508 page_size = ffs(cfg->page_size) - ffs(BRCMNAND_MIN_PAGESIZE);
2511 if (cfg->page_size < BRCMNAND_MIN_PAGESIZE || (ctrl->max_page_size &&
2512 cfg->page_size > ctrl->max_page_size)) {
2513 dev_warn(ctrl->dev, "invalid page size %u\n", cfg->page_size);
2517 if (fls64(cfg->device_size) < fls64(BRCMNAND_MIN_DEVSIZE)) {
2518 dev_warn(ctrl->dev, "invalid device size 0x%llx\n",
2519 (unsigned long long)cfg->device_size);
2522 device_size = fls64(cfg->device_size) - fls64(BRCMNAND_MIN_DEVSIZE);
2524 tmp = (cfg->blk_adr_bytes << CFG_BLK_ADR_BYTES_SHIFT) |
2525 (cfg->col_adr_bytes << CFG_COL_ADR_BYTES_SHIFT) |
2526 (cfg->ful_adr_bytes << CFG_FUL_ADR_BYTES_SHIFT) |
2527 (!!(cfg->device_width == 16) << CFG_BUS_WIDTH_SHIFT) |
2528 (device_size << CFG_DEVICE_SIZE_SHIFT);
2529 if (cfg_offs == cfg_ext_offs) {
2530 tmp |= (page_size << ctrl->page_size_shift) |
2531 (block_size << CFG_BLK_SIZE_SHIFT);
2532 nand_writereg(ctrl, cfg_offs, tmp);
2534 nand_writereg(ctrl, cfg_offs, tmp);
2535 tmp = (page_size << CFG_EXT_PAGE_SIZE_SHIFT) |
2536 (block_size << CFG_EXT_BLK_SIZE_SHIFT);
2537 nand_writereg(ctrl, cfg_ext_offs, tmp);
2540 tmp = nand_readreg(ctrl, acc_control_offs);
2541 tmp &= ~brcmnand_ecc_level_mask(ctrl);
2542 tmp &= ~brcmnand_spare_area_mask(ctrl);
2543 if (ctrl->nand_version >= 0x0302) {
2544 tmp |= cfg->ecc_level << ctrl->ecc_level_shift;
2545 tmp |= cfg->spare_area_size;
2547 nand_writereg(ctrl, acc_control_offs, tmp);
2549 brcmnand_set_sector_size_1k(host, cfg->sector_size_1k);
2551 /* threshold = ceil(BCH-level * 0.75) */
2552 brcmnand_wr_corr_thresh(host, DIV_ROUND_UP(chip->ecc.strength * 3, 4));
2557 static void brcmnand_print_cfg(struct brcmnand_host *host,
2558 char *buf, struct brcmnand_cfg *cfg)
2561 "%lluMiB total, %uKiB blocks, %u%s pages, %uB OOB, %u-bit",
2562 (unsigned long long)cfg->device_size >> 20,
2563 cfg->block_size >> 10,
2564 cfg->page_size >= 1024 ? cfg->page_size >> 10 : cfg->page_size,
2565 cfg->page_size >= 1024 ? "KiB" : "B",
2566 cfg->spare_area_size, cfg->device_width);
2568 /* Account for Hamming ECC and for BCH 512B vs 1KiB sectors */
2569 if (is_hamming_ecc(host->ctrl, cfg))
2570 sprintf(buf, ", Hamming ECC");
2571 else if (cfg->sector_size_1k)
2572 sprintf(buf, ", BCH-%u (1KiB sector)", cfg->ecc_level << 1);
2574 sprintf(buf, ", BCH-%u", cfg->ecc_level);
2578 * Minimum number of bytes to address a page. Calculated as:
2579 * roundup(log2(size / page-size) / 8)
2581 * NB: the following does not "round up" for non-power-of-2 'size'; but this is
2582 * OK because many other things will break if 'size' is irregular...
2584 static inline int get_blk_adr_bytes(u64 size, u32 writesize)
2586 return ALIGN(ilog2(size) - ilog2(writesize), 8) >> 3;
2589 static int brcmnand_setup_dev(struct brcmnand_host *host)
2591 struct mtd_info *mtd = nand_to_mtd(&host->chip);
2592 struct nand_chip *chip = &host->chip;
2593 const struct nand_ecc_props *requirements =
2594 nanddev_get_ecc_requirements(&chip->base);
2595 struct nand_memory_organization *memorg =
2596 nanddev_get_memorg(&chip->base);
2597 struct brcmnand_controller *ctrl = host->ctrl;
2598 struct brcmnand_cfg *cfg = &host->hwcfg;
2600 u32 offs, tmp, oob_sector;
2603 memset(cfg, 0, sizeof(*cfg));
2605 ret = of_property_read_u32(nand_get_flash_node(chip),
2606 "brcm,nand-oob-sector-size",
2609 /* Use detected size */
2610 cfg->spare_area_size = mtd->oobsize /
2611 (mtd->writesize >> FC_SHIFT);
2613 cfg->spare_area_size = oob_sector;
2615 if (cfg->spare_area_size > ctrl->max_oob)
2616 cfg->spare_area_size = ctrl->max_oob;
2618 * Set mtd and memorg oobsize to be consistent with controller's
2619 * spare_area_size, as the rest is inaccessible.
2621 mtd->oobsize = cfg->spare_area_size * (mtd->writesize >> FC_SHIFT);
2622 memorg->oobsize = mtd->oobsize;
2624 cfg->device_size = mtd->size;
2625 cfg->block_size = mtd->erasesize;
2626 cfg->page_size = mtd->writesize;
2627 cfg->device_width = (chip->options & NAND_BUSWIDTH_16) ? 16 : 8;
2628 cfg->col_adr_bytes = 2;
2629 cfg->blk_adr_bytes = get_blk_adr_bytes(mtd->size, mtd->writesize);
2631 if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST) {
2632 dev_err(ctrl->dev, "only HW ECC supported; selected: %d\n",
2633 chip->ecc.engine_type);
2637 if (chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN) {
2638 if (chip->ecc.strength == 1 && chip->ecc.size == 512)
2639 /* Default to Hamming for 1-bit ECC, if unspecified */
2640 chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
2642 /* Otherwise, BCH */
2643 chip->ecc.algo = NAND_ECC_ALGO_BCH;
2646 if (chip->ecc.algo == NAND_ECC_ALGO_HAMMING &&
2647 (chip->ecc.strength != 1 || chip->ecc.size != 512)) {
2648 dev_err(ctrl->dev, "invalid Hamming params: %d bits per %d bytes\n",
2649 chip->ecc.strength, chip->ecc.size);
2653 if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_NONE &&
2654 (!chip->ecc.size || !chip->ecc.strength)) {
2655 if (requirements->step_size && requirements->strength) {
2656 /* use detected ECC parameters */
2657 chip->ecc.size = requirements->step_size;
2658 chip->ecc.strength = requirements->strength;
2659 dev_info(ctrl->dev, "Using ECC step-size %d, strength %d\n",
2660 chip->ecc.size, chip->ecc.strength);
2664 switch (chip->ecc.size) {
2666 if (chip->ecc.algo == NAND_ECC_ALGO_HAMMING)
2667 cfg->ecc_level = 15;
2669 cfg->ecc_level = chip->ecc.strength;
2670 cfg->sector_size_1k = 0;
2673 if (!(ctrl->features & BRCMNAND_HAS_1K_SECTORS)) {
2674 dev_err(ctrl->dev, "1KB sectors not supported\n");
2677 if (chip->ecc.strength & 0x1) {
2679 "odd ECC not supported with 1KB sectors\n");
2683 cfg->ecc_level = chip->ecc.strength >> 1;
2684 cfg->sector_size_1k = 1;
2687 dev_err(ctrl->dev, "unsupported ECC size: %d\n",
2692 cfg->ful_adr_bytes = cfg->blk_adr_bytes;
2693 if (mtd->writesize > 512)
2694 cfg->ful_adr_bytes += cfg->col_adr_bytes;
2696 cfg->ful_adr_bytes += 1;
2698 ret = brcmnand_set_cfg(host, cfg);
2702 brcmnand_set_ecc_enabled(host, 1);
2704 brcmnand_print_cfg(host, msg, cfg);
2705 dev_info(ctrl->dev, "detected %s\n", msg);
2707 /* Configure ACC_CONTROL */
2708 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
2709 tmp = nand_readreg(ctrl, offs);
2710 tmp &= ~ACC_CONTROL_PARTIAL_PAGE;
2711 tmp &= ~ACC_CONTROL_RD_ERASED;
2713 /* We need to turn on Read from erased paged protected by ECC */
2714 if (ctrl->nand_version >= 0x0702)
2715 tmp |= ACC_CONTROL_RD_ERASED;
2716 tmp &= ~ACC_CONTROL_FAST_PGM_RDIN;
2717 if (ctrl->features & BRCMNAND_HAS_PREFETCH)
2718 tmp &= ~ACC_CONTROL_PREFETCH;
2720 nand_writereg(ctrl, offs, tmp);
2725 static int brcmnand_attach_chip(struct nand_chip *chip)
2727 struct mtd_info *mtd = nand_to_mtd(chip);
2728 struct brcmnand_host *host = nand_get_controller_data(chip);
2731 chip->options |= NAND_NO_SUBPAGE_WRITE;
2733 * Avoid (for instance) kmap()'d buffers from JFFS2, which we can't DMA
2734 * to/from, and have nand_base pass us a bounce buffer instead, as
2737 chip->options |= NAND_USES_DMA;
2739 if (chip->bbt_options & NAND_BBT_USE_FLASH)
2740 chip->bbt_options |= NAND_BBT_NO_OOB;
2742 if (brcmnand_setup_dev(host))
2745 chip->ecc.size = host->hwcfg.sector_size_1k ? 1024 : 512;
2747 /* only use our internal HW threshold */
2748 mtd->bitflip_threshold = 1;
2750 ret = brcmstb_choose_ecc_layout(host);
2752 /* If OOB is written with ECC enabled it will cause ECC errors */
2753 if (is_hamming_ecc(host->ctrl, &host->hwcfg)) {
2754 chip->ecc.write_oob = brcmnand_write_oob_raw;
2755 chip->ecc.read_oob = brcmnand_read_oob_raw;
2761 static const struct nand_controller_ops brcmnand_controller_ops = {
2762 .attach_chip = brcmnand_attach_chip,
2765 static int brcmnand_init_cs(struct brcmnand_host *host, struct device_node *dn)
2767 struct brcmnand_controller *ctrl = host->ctrl;
2768 struct platform_device *pdev = host->pdev;
2769 struct mtd_info *mtd;
2770 struct nand_chip *chip;
2774 ret = of_property_read_u32(dn, "reg", &host->cs);
2776 dev_err(&pdev->dev, "can't get chip-select\n");
2780 mtd = nand_to_mtd(&host->chip);
2783 nand_set_flash_node(chip, dn);
2784 nand_set_controller_data(chip, host);
2785 mtd->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "brcmnand.%d",
2790 mtd->owner = THIS_MODULE;
2791 mtd->dev.parent = &pdev->dev;
2793 chip->legacy.cmd_ctrl = brcmnand_cmd_ctrl;
2794 chip->legacy.cmdfunc = brcmnand_cmdfunc;
2795 chip->legacy.waitfunc = brcmnand_waitfunc;
2796 chip->legacy.read_byte = brcmnand_read_byte;
2797 chip->legacy.read_buf = brcmnand_read_buf;
2798 chip->legacy.write_buf = brcmnand_write_buf;
2800 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
2801 chip->ecc.read_page = brcmnand_read_page;
2802 chip->ecc.write_page = brcmnand_write_page;
2803 chip->ecc.read_page_raw = brcmnand_read_page_raw;
2804 chip->ecc.write_page_raw = brcmnand_write_page_raw;
2805 chip->ecc.write_oob_raw = brcmnand_write_oob_raw;
2806 chip->ecc.read_oob_raw = brcmnand_read_oob_raw;
2807 chip->ecc.read_oob = brcmnand_read_oob;
2808 chip->ecc.write_oob = brcmnand_write_oob;
2810 chip->controller = &ctrl->controller;
2813 * The bootloader might have configured 16bit mode but
2814 * NAND READID command only works in 8bit mode. We force
2815 * 8bit mode here to ensure that NAND READID commands works.
2817 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2818 nand_writereg(ctrl, cfg_offs,
2819 nand_readreg(ctrl, cfg_offs) & ~CFG_BUS_WIDTH);
2821 ret = nand_scan(chip, 1);
2825 ret = mtd_device_register(mtd, NULL, 0);
2832 static void brcmnand_save_restore_cs_config(struct brcmnand_host *host,
2835 struct brcmnand_controller *ctrl = host->ctrl;
2836 u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2837 u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
2838 BRCMNAND_CS_CFG_EXT);
2839 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
2840 BRCMNAND_CS_ACC_CONTROL);
2841 u16 t1_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING1);
2842 u16 t2_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING2);
2845 nand_writereg(ctrl, cfg_offs, host->hwcfg.config);
2846 if (cfg_offs != cfg_ext_offs)
2847 nand_writereg(ctrl, cfg_ext_offs,
2848 host->hwcfg.config_ext);
2849 nand_writereg(ctrl, acc_control_offs, host->hwcfg.acc_control);
2850 nand_writereg(ctrl, t1_offs, host->hwcfg.timing_1);
2851 nand_writereg(ctrl, t2_offs, host->hwcfg.timing_2);
2853 host->hwcfg.config = nand_readreg(ctrl, cfg_offs);
2854 if (cfg_offs != cfg_ext_offs)
2855 host->hwcfg.config_ext =
2856 nand_readreg(ctrl, cfg_ext_offs);
2857 host->hwcfg.acc_control = nand_readreg(ctrl, acc_control_offs);
2858 host->hwcfg.timing_1 = nand_readreg(ctrl, t1_offs);
2859 host->hwcfg.timing_2 = nand_readreg(ctrl, t2_offs);
2863 static int brcmnand_suspend(struct device *dev)
2865 struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
2866 struct brcmnand_host *host;
2868 list_for_each_entry(host, &ctrl->host_list, node)
2869 brcmnand_save_restore_cs_config(host, 0);
2871 ctrl->nand_cs_nand_select = brcmnand_read_reg(ctrl, BRCMNAND_CS_SELECT);
2872 ctrl->nand_cs_nand_xor = brcmnand_read_reg(ctrl, BRCMNAND_CS_XOR);
2873 ctrl->corr_stat_threshold =
2874 brcmnand_read_reg(ctrl, BRCMNAND_CORR_THRESHOLD);
2876 if (has_flash_dma(ctrl))
2877 ctrl->flash_dma_mode = flash_dma_readl(ctrl, FLASH_DMA_MODE);
2878 else if (has_edu(ctrl))
2879 ctrl->edu_config = edu_readl(ctrl, EDU_CONFIG);
2884 static int brcmnand_resume(struct device *dev)
2886 struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
2887 struct brcmnand_host *host;
2889 if (has_flash_dma(ctrl)) {
2890 flash_dma_writel(ctrl, FLASH_DMA_MODE, ctrl->flash_dma_mode);
2891 flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
2894 if (has_edu(ctrl)) {
2895 ctrl->edu_config = edu_readl(ctrl, EDU_CONFIG);
2896 edu_writel(ctrl, EDU_CONFIG, ctrl->edu_config);
2897 edu_readl(ctrl, EDU_CONFIG);
2898 brcmnand_edu_init(ctrl);
2901 brcmnand_write_reg(ctrl, BRCMNAND_CS_SELECT, ctrl->nand_cs_nand_select);
2902 brcmnand_write_reg(ctrl, BRCMNAND_CS_XOR, ctrl->nand_cs_nand_xor);
2903 brcmnand_write_reg(ctrl, BRCMNAND_CORR_THRESHOLD,
2904 ctrl->corr_stat_threshold);
2906 /* Clear/re-enable interrupt */
2907 ctrl->soc->ctlrdy_ack(ctrl->soc);
2908 ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
2911 list_for_each_entry(host, &ctrl->host_list, node) {
2912 struct nand_chip *chip = &host->chip;
2914 brcmnand_save_restore_cs_config(host, 1);
2916 /* Reset the chip, required by some chips after power-up */
2917 nand_reset_op(chip);
2923 const struct dev_pm_ops brcmnand_pm_ops = {
2924 .suspend = brcmnand_suspend,
2925 .resume = brcmnand_resume,
2927 EXPORT_SYMBOL_GPL(brcmnand_pm_ops);
2929 static const struct of_device_id brcmnand_of_match[] = {
2930 { .compatible = "brcm,brcmnand-v2.1" },
2931 { .compatible = "brcm,brcmnand-v2.2" },
2932 { .compatible = "brcm,brcmnand-v4.0" },
2933 { .compatible = "brcm,brcmnand-v5.0" },
2934 { .compatible = "brcm,brcmnand-v6.0" },
2935 { .compatible = "brcm,brcmnand-v6.1" },
2936 { .compatible = "brcm,brcmnand-v6.2" },
2937 { .compatible = "brcm,brcmnand-v7.0" },
2938 { .compatible = "brcm,brcmnand-v7.1" },
2939 { .compatible = "brcm,brcmnand-v7.2" },
2940 { .compatible = "brcm,brcmnand-v7.3" },
2943 MODULE_DEVICE_TABLE(of, brcmnand_of_match);
2945 /***********************************************************************
2946 * Platform driver setup (per controller)
2947 ***********************************************************************/
2948 static int brcmnand_edu_setup(struct platform_device *pdev)
2950 struct device *dev = &pdev->dev;
2951 struct brcmnand_controller *ctrl = dev_get_drvdata(&pdev->dev);
2952 struct resource *res;
2955 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash-edu");
2957 ctrl->edu_base = devm_ioremap_resource(dev, res);
2958 if (IS_ERR(ctrl->edu_base))
2959 return PTR_ERR(ctrl->edu_base);
2961 ctrl->edu_offsets = edu_regs;
2963 edu_writel(ctrl, EDU_CONFIG, EDU_CONFIG_MODE_NAND |
2964 EDU_CONFIG_SWAP_CFG);
2965 edu_readl(ctrl, EDU_CONFIG);
2967 /* initialize edu */
2968 brcmnand_edu_init(ctrl);
2970 ctrl->edu_irq = platform_get_irq_optional(pdev, 1);
2971 if (ctrl->edu_irq < 0) {
2973 "FLASH EDU enabled, using ctlrdy irq\n");
2975 ret = devm_request_irq(dev, ctrl->edu_irq,
2976 brcmnand_edu_irq, 0,
2977 "brcmnand-edu", ctrl);
2979 dev_err(ctrl->dev, "can't allocate IRQ %d: error %d\n",
2980 ctrl->edu_irq, ret);
2984 dev_info(dev, "FLASH EDU enabled using irq %u\n",
2992 int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
2994 struct device *dev = &pdev->dev;
2995 struct device_node *dn = dev->of_node, *child;
2996 struct brcmnand_controller *ctrl;
2997 struct resource *res;
3000 /* We only support device-tree instantiation */
3004 if (!of_match_node(brcmnand_of_match, dn))
3007 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
3011 dev_set_drvdata(dev, ctrl);
3014 /* Enable the static key if the soc provides I/O operations indicating
3015 * that a non-memory mapped IO access path must be used
3017 if (brcmnand_soc_has_ops(ctrl->soc))
3018 static_branch_enable(&brcmnand_soc_has_ops_key);
3020 init_completion(&ctrl->done);
3021 init_completion(&ctrl->dma_done);
3022 init_completion(&ctrl->edu_done);
3023 nand_controller_init(&ctrl->controller);
3024 ctrl->controller.ops = &brcmnand_controller_ops;
3025 INIT_LIST_HEAD(&ctrl->host_list);
3027 /* NAND register range */
3028 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3029 ctrl->nand_base = devm_ioremap_resource(dev, res);
3030 if (IS_ERR(ctrl->nand_base))
3031 return PTR_ERR(ctrl->nand_base);
3033 /* Enable clock before using NAND registers */
3034 ctrl->clk = devm_clk_get(dev, "nand");
3035 if (!IS_ERR(ctrl->clk)) {
3036 ret = clk_prepare_enable(ctrl->clk);
3040 ret = PTR_ERR(ctrl->clk);
3041 if (ret == -EPROBE_DEFER)
3047 /* Initialize NAND revision */
3048 ret = brcmnand_revision_init(ctrl);
3053 * Most chips have this cache at a fixed offset within 'nand' block.
3054 * Some must specify this region separately.
3056 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand-cache");
3058 ctrl->nand_fc = devm_ioremap_resource(dev, res);
3059 if (IS_ERR(ctrl->nand_fc)) {
3060 ret = PTR_ERR(ctrl->nand_fc);
3064 ctrl->nand_fc = ctrl->nand_base +
3065 ctrl->reg_offsets[BRCMNAND_FC_BASE];
3069 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash-dma");
3071 ctrl->flash_dma_base = devm_ioremap_resource(dev, res);
3072 if (IS_ERR(ctrl->flash_dma_base)) {
3073 ret = PTR_ERR(ctrl->flash_dma_base);
3077 /* initialize the dma version */
3078 brcmnand_flash_dma_revision_init(ctrl);
3081 if (ctrl->nand_version >= 0x0700)
3082 ret = dma_set_mask_and_coherent(&pdev->dev,
3085 ret = dma_set_mask_and_coherent(&pdev->dev,
3090 /* linked-list and stop on error */
3091 flash_dma_writel(ctrl, FLASH_DMA_MODE, FLASH_DMA_MODE_MASK);
3092 flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
3094 /* Allocate descriptor(s) */
3095 ctrl->dma_desc = dmam_alloc_coherent(dev,
3096 sizeof(*ctrl->dma_desc),
3097 &ctrl->dma_pa, GFP_KERNEL);
3098 if (!ctrl->dma_desc) {
3103 ctrl->dma_irq = platform_get_irq(pdev, 1);
3104 if ((int)ctrl->dma_irq < 0) {
3105 dev_err(dev, "missing FLASH_DMA IRQ\n");
3110 ret = devm_request_irq(dev, ctrl->dma_irq,
3111 brcmnand_dma_irq, 0, DRV_NAME,
3114 dev_err(dev, "can't allocate IRQ %d: error %d\n",
3115 ctrl->dma_irq, ret);
3119 dev_info(dev, "enabling FLASH_DMA\n");
3120 /* set flash dma transfer function to call */
3121 ctrl->dma_trans = brcmnand_dma_trans;
3123 ret = brcmnand_edu_setup(pdev);
3128 /* set edu transfer function to call */
3129 ctrl->dma_trans = brcmnand_edu_trans;
3132 /* Disable automatic device ID config, direct addressing */
3133 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT,
3134 CS_SELECT_AUTO_DEVICE_ID_CFG | 0xff, 0, 0);
3135 /* Disable XOR addressing */
3136 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
3138 if (ctrl->features & BRCMNAND_HAS_WP) {
3139 /* Permanently disable write protection */
3141 brcmnand_set_wp(ctrl, false);
3147 ctrl->irq = platform_get_irq(pdev, 0);
3148 if ((int)ctrl->irq < 0) {
3149 dev_err(dev, "no IRQ defined\n");
3155 * Some SoCs integrate this controller (e.g., its interrupt bits) in
3161 ret = devm_request_irq(dev, ctrl->irq, brcmnand_irq, 0,
3164 /* Enable interrupt */
3165 ctrl->soc->ctlrdy_ack(ctrl->soc);
3166 ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
3168 /* Use standard interrupt infrastructure */
3169 ret = devm_request_irq(dev, ctrl->irq, brcmnand_ctlrdy_irq, 0,
3173 dev_err(dev, "can't allocate IRQ %d: error %d\n",
3178 for_each_available_child_of_node(dn, child) {
3179 if (of_device_is_compatible(child, "brcm,nandcs")) {
3180 struct brcmnand_host *host;
3182 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
3191 ret = brcmnand_init_cs(host, child);
3193 devm_kfree(dev, host);
3194 continue; /* Try all chip-selects */
3197 list_add_tail(&host->node, &ctrl->host_list);
3201 /* No chip-selects could initialize properly */
3202 if (list_empty(&ctrl->host_list)) {
3210 clk_disable_unprepare(ctrl->clk);
3214 EXPORT_SYMBOL_GPL(brcmnand_probe);
3216 int brcmnand_remove(struct platform_device *pdev)
3218 struct brcmnand_controller *ctrl = dev_get_drvdata(&pdev->dev);
3219 struct brcmnand_host *host;
3220 struct nand_chip *chip;
3223 list_for_each_entry(host, &ctrl->host_list, node) {
3225 ret = mtd_device_unregister(nand_to_mtd(chip));
3230 clk_disable_unprepare(ctrl->clk);
3232 dev_set_drvdata(&pdev->dev, NULL);
3236 EXPORT_SYMBOL_GPL(brcmnand_remove);
3238 MODULE_LICENSE("GPL v2");
3239 MODULE_AUTHOR("Kevin Cernekee");
3240 MODULE_AUTHOR("Brian Norris");
3241 MODULE_DESCRIPTION("NAND driver for Broadcom chips");
3242 MODULE_ALIAS("platform:brcmnand");