1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2010-2015 Broadcom Corporation
7 #include <linux/version.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/platform_device.h>
13 #include <linux/err.h>
14 #include <linux/completion.h>
15 #include <linux/interrupt.h>
16 #include <linux/spinlock.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/ioport.h>
19 #include <linux/bug.h>
20 #include <linux/kernel.h>
21 #include <linux/bitops.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/rawnand.h>
25 #include <linux/mtd/partitions.h>
27 #include <linux/of_platform.h>
28 #include <linux/slab.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 /* 512B flash cache in the NAND controller HW */
90 #define FC_WORDS (FC_BYTES >> 2)
92 #define BRCMNAND_MIN_PAGESIZE 512
93 #define BRCMNAND_MIN_BLOCKSIZE (8 * 1024)
94 #define BRCMNAND_MIN_DEVSIZE (4ULL * 1024 * 1024)
96 #define NAND_CTRL_RDY (INTFC_CTLR_READY | INTFC_FLASH_READY)
97 #define NAND_POLL_STATUS_TIMEOUT_MS 100
99 /* Controller feature flags */
101 BRCMNAND_HAS_1K_SECTORS = BIT(0),
102 BRCMNAND_HAS_PREFETCH = BIT(1),
103 BRCMNAND_HAS_CACHE_MODE = BIT(2),
104 BRCMNAND_HAS_WP = BIT(3),
107 struct brcmnand_controller {
109 struct nand_controller controller;
110 void __iomem *nand_base;
111 void __iomem *nand_fc; /* flash cache */
112 void __iomem *flash_dma_base;
114 unsigned int dma_irq;
117 /* Some SoCs provide custom interrupt status register(s) */
118 struct brcmnand_soc *soc;
120 /* Some SoCs have a gateable clock for the controller */
125 struct completion done;
126 struct completion dma_done;
128 /* List of NAND hosts (one for each chip-select) */
129 struct list_head host_list;
131 struct brcm_nand_dma_desc *dma_desc;
134 /* in-memory cache of the FLASH_CACHE, used only for some commands */
135 u8 flash_cache[FC_BYTES];
137 /* Controller revision details */
138 const u16 *reg_offsets;
139 unsigned int reg_spacing; /* between CS1, CS2, ... regs */
140 const u8 *cs_offsets; /* within each chip-select */
141 const u8 *cs0_offsets; /* within CS0, if different */
142 unsigned int max_block_size;
143 const unsigned int *block_sizes;
144 unsigned int max_page_size;
145 const unsigned int *page_sizes;
146 unsigned int max_oob;
149 /* for low-power standby/resume only */
150 u32 nand_cs_nand_select;
151 u32 nand_cs_nand_xor;
152 u32 corr_stat_threshold;
156 struct brcmnand_cfg {
158 unsigned int block_size;
159 unsigned int page_size;
160 unsigned int spare_area_size;
161 unsigned int device_width;
162 unsigned int col_adr_bytes;
163 unsigned int blk_adr_bytes;
164 unsigned int ful_adr_bytes;
165 unsigned int sector_size_1k;
166 unsigned int ecc_level;
167 /* use for low-power standby/resume only */
175 struct brcmnand_host {
176 struct list_head node;
178 struct nand_chip chip;
179 struct platform_device *pdev;
182 unsigned int last_cmd;
183 unsigned int last_byte;
185 struct brcmnand_cfg hwcfg;
186 struct brcmnand_controller *ctrl;
190 BRCMNAND_CMD_START = 0,
191 BRCMNAND_CMD_EXT_ADDRESS,
192 BRCMNAND_CMD_ADDRESS,
193 BRCMNAND_INTFC_STATUS,
198 BRCMNAND_CS1_BASE, /* CS1 regs, if non-contiguous */
199 BRCMNAND_CORR_THRESHOLD,
200 BRCMNAND_CORR_THRESHOLD_EXT,
201 BRCMNAND_UNCORR_COUNT,
203 BRCMNAND_CORR_EXT_ADDR,
205 BRCMNAND_UNCORR_EXT_ADDR,
206 BRCMNAND_UNCORR_ADDR,
211 BRCMNAND_OOB_READ_BASE,
212 BRCMNAND_OOB_READ_10_BASE, /* offset 0x10, if non-contiguous */
213 BRCMNAND_OOB_WRITE_BASE,
214 BRCMNAND_OOB_WRITE_10_BASE, /* offset 0x10, if non-contiguous */
219 static const u16 brcmnand_regs_v40[] = {
220 [BRCMNAND_CMD_START] = 0x04,
221 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
222 [BRCMNAND_CMD_ADDRESS] = 0x0c,
223 [BRCMNAND_INTFC_STATUS] = 0x6c,
224 [BRCMNAND_CS_SELECT] = 0x14,
225 [BRCMNAND_CS_XOR] = 0x18,
226 [BRCMNAND_LL_OP] = 0x178,
227 [BRCMNAND_CS0_BASE] = 0x40,
228 [BRCMNAND_CS1_BASE] = 0xd0,
229 [BRCMNAND_CORR_THRESHOLD] = 0x84,
230 [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
231 [BRCMNAND_UNCORR_COUNT] = 0,
232 [BRCMNAND_CORR_COUNT] = 0,
233 [BRCMNAND_CORR_EXT_ADDR] = 0x70,
234 [BRCMNAND_CORR_ADDR] = 0x74,
235 [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
236 [BRCMNAND_UNCORR_ADDR] = 0x7c,
237 [BRCMNAND_SEMAPHORE] = 0x58,
238 [BRCMNAND_ID] = 0x60,
239 [BRCMNAND_ID_EXT] = 0x64,
240 [BRCMNAND_LL_RDATA] = 0x17c,
241 [BRCMNAND_OOB_READ_BASE] = 0x20,
242 [BRCMNAND_OOB_READ_10_BASE] = 0x130,
243 [BRCMNAND_OOB_WRITE_BASE] = 0x30,
244 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
245 [BRCMNAND_FC_BASE] = 0x200,
249 static const u16 brcmnand_regs_v50[] = {
250 [BRCMNAND_CMD_START] = 0x04,
251 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
252 [BRCMNAND_CMD_ADDRESS] = 0x0c,
253 [BRCMNAND_INTFC_STATUS] = 0x6c,
254 [BRCMNAND_CS_SELECT] = 0x14,
255 [BRCMNAND_CS_XOR] = 0x18,
256 [BRCMNAND_LL_OP] = 0x178,
257 [BRCMNAND_CS0_BASE] = 0x40,
258 [BRCMNAND_CS1_BASE] = 0xd0,
259 [BRCMNAND_CORR_THRESHOLD] = 0x84,
260 [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
261 [BRCMNAND_UNCORR_COUNT] = 0,
262 [BRCMNAND_CORR_COUNT] = 0,
263 [BRCMNAND_CORR_EXT_ADDR] = 0x70,
264 [BRCMNAND_CORR_ADDR] = 0x74,
265 [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
266 [BRCMNAND_UNCORR_ADDR] = 0x7c,
267 [BRCMNAND_SEMAPHORE] = 0x58,
268 [BRCMNAND_ID] = 0x60,
269 [BRCMNAND_ID_EXT] = 0x64,
270 [BRCMNAND_LL_RDATA] = 0x17c,
271 [BRCMNAND_OOB_READ_BASE] = 0x20,
272 [BRCMNAND_OOB_READ_10_BASE] = 0x130,
273 [BRCMNAND_OOB_WRITE_BASE] = 0x30,
274 [BRCMNAND_OOB_WRITE_10_BASE] = 0x140,
275 [BRCMNAND_FC_BASE] = 0x200,
278 /* BRCMNAND v6.0 - v7.1 */
279 static const u16 brcmnand_regs_v60[] = {
280 [BRCMNAND_CMD_START] = 0x04,
281 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
282 [BRCMNAND_CMD_ADDRESS] = 0x0c,
283 [BRCMNAND_INTFC_STATUS] = 0x14,
284 [BRCMNAND_CS_SELECT] = 0x18,
285 [BRCMNAND_CS_XOR] = 0x1c,
286 [BRCMNAND_LL_OP] = 0x20,
287 [BRCMNAND_CS0_BASE] = 0x50,
288 [BRCMNAND_CS1_BASE] = 0,
289 [BRCMNAND_CORR_THRESHOLD] = 0xc0,
290 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xc4,
291 [BRCMNAND_UNCORR_COUNT] = 0xfc,
292 [BRCMNAND_CORR_COUNT] = 0x100,
293 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
294 [BRCMNAND_CORR_ADDR] = 0x110,
295 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
296 [BRCMNAND_UNCORR_ADDR] = 0x118,
297 [BRCMNAND_SEMAPHORE] = 0x150,
298 [BRCMNAND_ID] = 0x194,
299 [BRCMNAND_ID_EXT] = 0x198,
300 [BRCMNAND_LL_RDATA] = 0x19c,
301 [BRCMNAND_OOB_READ_BASE] = 0x200,
302 [BRCMNAND_OOB_READ_10_BASE] = 0,
303 [BRCMNAND_OOB_WRITE_BASE] = 0x280,
304 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
305 [BRCMNAND_FC_BASE] = 0x400,
309 static const u16 brcmnand_regs_v71[] = {
310 [BRCMNAND_CMD_START] = 0x04,
311 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
312 [BRCMNAND_CMD_ADDRESS] = 0x0c,
313 [BRCMNAND_INTFC_STATUS] = 0x14,
314 [BRCMNAND_CS_SELECT] = 0x18,
315 [BRCMNAND_CS_XOR] = 0x1c,
316 [BRCMNAND_LL_OP] = 0x20,
317 [BRCMNAND_CS0_BASE] = 0x50,
318 [BRCMNAND_CS1_BASE] = 0,
319 [BRCMNAND_CORR_THRESHOLD] = 0xdc,
320 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0,
321 [BRCMNAND_UNCORR_COUNT] = 0xfc,
322 [BRCMNAND_CORR_COUNT] = 0x100,
323 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
324 [BRCMNAND_CORR_ADDR] = 0x110,
325 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
326 [BRCMNAND_UNCORR_ADDR] = 0x118,
327 [BRCMNAND_SEMAPHORE] = 0x150,
328 [BRCMNAND_ID] = 0x194,
329 [BRCMNAND_ID_EXT] = 0x198,
330 [BRCMNAND_LL_RDATA] = 0x19c,
331 [BRCMNAND_OOB_READ_BASE] = 0x200,
332 [BRCMNAND_OOB_READ_10_BASE] = 0,
333 [BRCMNAND_OOB_WRITE_BASE] = 0x280,
334 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
335 [BRCMNAND_FC_BASE] = 0x400,
339 static const u16 brcmnand_regs_v72[] = {
340 [BRCMNAND_CMD_START] = 0x04,
341 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
342 [BRCMNAND_CMD_ADDRESS] = 0x0c,
343 [BRCMNAND_INTFC_STATUS] = 0x14,
344 [BRCMNAND_CS_SELECT] = 0x18,
345 [BRCMNAND_CS_XOR] = 0x1c,
346 [BRCMNAND_LL_OP] = 0x20,
347 [BRCMNAND_CS0_BASE] = 0x50,
348 [BRCMNAND_CS1_BASE] = 0,
349 [BRCMNAND_CORR_THRESHOLD] = 0xdc,
350 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0,
351 [BRCMNAND_UNCORR_COUNT] = 0xfc,
352 [BRCMNAND_CORR_COUNT] = 0x100,
353 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
354 [BRCMNAND_CORR_ADDR] = 0x110,
355 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
356 [BRCMNAND_UNCORR_ADDR] = 0x118,
357 [BRCMNAND_SEMAPHORE] = 0x150,
358 [BRCMNAND_ID] = 0x194,
359 [BRCMNAND_ID_EXT] = 0x198,
360 [BRCMNAND_LL_RDATA] = 0x19c,
361 [BRCMNAND_OOB_READ_BASE] = 0x200,
362 [BRCMNAND_OOB_READ_10_BASE] = 0,
363 [BRCMNAND_OOB_WRITE_BASE] = 0x400,
364 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
365 [BRCMNAND_FC_BASE] = 0x600,
368 enum brcmnand_cs_reg {
369 BRCMNAND_CS_CFG_EXT = 0,
371 BRCMNAND_CS_ACC_CONTROL,
376 /* Per chip-select offsets for v7.1 */
377 static const u8 brcmnand_cs_offsets_v71[] = {
378 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
379 [BRCMNAND_CS_CFG_EXT] = 0x04,
380 [BRCMNAND_CS_CFG] = 0x08,
381 [BRCMNAND_CS_TIMING1] = 0x0c,
382 [BRCMNAND_CS_TIMING2] = 0x10,
385 /* Per chip-select offsets for pre v7.1, except CS0 on <= v5.0 */
386 static const u8 brcmnand_cs_offsets[] = {
387 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
388 [BRCMNAND_CS_CFG_EXT] = 0x04,
389 [BRCMNAND_CS_CFG] = 0x04,
390 [BRCMNAND_CS_TIMING1] = 0x08,
391 [BRCMNAND_CS_TIMING2] = 0x0c,
394 /* Per chip-select offset for <= v5.0 on CS0 only */
395 static const u8 brcmnand_cs_offsets_cs0[] = {
396 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
397 [BRCMNAND_CS_CFG_EXT] = 0x08,
398 [BRCMNAND_CS_CFG] = 0x08,
399 [BRCMNAND_CS_TIMING1] = 0x10,
400 [BRCMNAND_CS_TIMING2] = 0x14,
404 * Bitfields for the CFG and CFG_EXT registers. Pre-v7.1 controllers only had
405 * one config register, but once the bitfields overflowed, newer controllers
406 * (v7.1 and newer) added a CFG_EXT register and shuffled a few fields around.
409 CFG_BLK_ADR_BYTES_SHIFT = 8,
410 CFG_COL_ADR_BYTES_SHIFT = 12,
411 CFG_FUL_ADR_BYTES_SHIFT = 16,
412 CFG_BUS_WIDTH_SHIFT = 23,
413 CFG_BUS_WIDTH = BIT(CFG_BUS_WIDTH_SHIFT),
414 CFG_DEVICE_SIZE_SHIFT = 24,
416 /* Only for pre-v7.1 (with no CFG_EXT register) */
417 CFG_PAGE_SIZE_SHIFT = 20,
418 CFG_BLK_SIZE_SHIFT = 28,
420 /* Only for v7.1+ (with CFG_EXT register) */
421 CFG_EXT_PAGE_SIZE_SHIFT = 0,
422 CFG_EXT_BLK_SIZE_SHIFT = 4,
425 /* BRCMNAND_INTFC_STATUS */
427 INTFC_FLASH_STATUS = GENMASK(7, 0),
429 INTFC_ERASED = BIT(27),
430 INTFC_OOB_VALID = BIT(28),
431 INTFC_CACHE_VALID = BIT(29),
432 INTFC_FLASH_READY = BIT(30),
433 INTFC_CTLR_READY = BIT(31),
436 static inline u32 nand_readreg(struct brcmnand_controller *ctrl, u32 offs)
438 return brcmnand_readl(ctrl->nand_base + offs);
441 static inline void nand_writereg(struct brcmnand_controller *ctrl, u32 offs,
444 brcmnand_writel(val, ctrl->nand_base + offs);
447 static int brcmnand_revision_init(struct brcmnand_controller *ctrl)
449 static const unsigned int block_sizes_v6[] = { 8, 16, 128, 256, 512, 1024, 2048, 0 };
450 static const unsigned int block_sizes_v4[] = { 16, 128, 8, 512, 256, 1024, 2048, 0 };
451 static const unsigned int page_sizes[] = { 512, 2048, 4096, 8192, 0 };
453 ctrl->nand_version = nand_readreg(ctrl, 0) & 0xffff;
455 /* Only support v4.0+? */
456 if (ctrl->nand_version < 0x0400) {
457 dev_err(ctrl->dev, "version %#x not supported\n",
462 /* Register offsets */
463 if (ctrl->nand_version >= 0x0702)
464 ctrl->reg_offsets = brcmnand_regs_v72;
465 else if (ctrl->nand_version >= 0x0701)
466 ctrl->reg_offsets = brcmnand_regs_v71;
467 else if (ctrl->nand_version >= 0x0600)
468 ctrl->reg_offsets = brcmnand_regs_v60;
469 else if (ctrl->nand_version >= 0x0500)
470 ctrl->reg_offsets = brcmnand_regs_v50;
471 else if (ctrl->nand_version >= 0x0400)
472 ctrl->reg_offsets = brcmnand_regs_v40;
474 /* Chip-select stride */
475 if (ctrl->nand_version >= 0x0701)
476 ctrl->reg_spacing = 0x14;
478 ctrl->reg_spacing = 0x10;
480 /* Per chip-select registers */
481 if (ctrl->nand_version >= 0x0701) {
482 ctrl->cs_offsets = brcmnand_cs_offsets_v71;
484 ctrl->cs_offsets = brcmnand_cs_offsets;
486 /* v5.0 and earlier has a different CS0 offset layout */
487 if (ctrl->nand_version <= 0x0500)
488 ctrl->cs0_offsets = brcmnand_cs_offsets_cs0;
491 /* Page / block sizes */
492 if (ctrl->nand_version >= 0x0701) {
493 /* >= v7.1 use nice power-of-2 values! */
494 ctrl->max_page_size = 16 * 1024;
495 ctrl->max_block_size = 2 * 1024 * 1024;
497 ctrl->page_sizes = page_sizes;
498 if (ctrl->nand_version >= 0x0600)
499 ctrl->block_sizes = block_sizes_v6;
501 ctrl->block_sizes = block_sizes_v4;
503 if (ctrl->nand_version < 0x0400) {
504 ctrl->max_page_size = 4096;
505 ctrl->max_block_size = 512 * 1024;
509 /* Maximum spare area sector size (per 512B) */
510 if (ctrl->nand_version >= 0x0702)
512 else if (ctrl->nand_version >= 0x0600)
514 else if (ctrl->nand_version >= 0x0500)
519 /* v6.0 and newer (except v6.1) have prefetch support */
520 if (ctrl->nand_version >= 0x0600 && ctrl->nand_version != 0x0601)
521 ctrl->features |= BRCMNAND_HAS_PREFETCH;
524 * v6.x has cache mode, but it's implemented differently. Ignore it for
527 if (ctrl->nand_version >= 0x0700)
528 ctrl->features |= BRCMNAND_HAS_CACHE_MODE;
530 if (ctrl->nand_version >= 0x0500)
531 ctrl->features |= BRCMNAND_HAS_1K_SECTORS;
533 if (ctrl->nand_version >= 0x0700)
534 ctrl->features |= BRCMNAND_HAS_WP;
535 else if (of_property_read_bool(ctrl->dev->of_node, "brcm,nand-has-wp"))
536 ctrl->features |= BRCMNAND_HAS_WP;
541 static inline u32 brcmnand_read_reg(struct brcmnand_controller *ctrl,
542 enum brcmnand_reg reg)
544 u16 offs = ctrl->reg_offsets[reg];
547 return nand_readreg(ctrl, offs);
552 static inline void brcmnand_write_reg(struct brcmnand_controller *ctrl,
553 enum brcmnand_reg reg, u32 val)
555 u16 offs = ctrl->reg_offsets[reg];
558 nand_writereg(ctrl, offs, val);
561 static inline void brcmnand_rmw_reg(struct brcmnand_controller *ctrl,
562 enum brcmnand_reg reg, u32 mask, unsigned
565 u32 tmp = brcmnand_read_reg(ctrl, reg);
569 brcmnand_write_reg(ctrl, reg, tmp);
572 static inline u32 brcmnand_read_fc(struct brcmnand_controller *ctrl, int word)
574 return __raw_readl(ctrl->nand_fc + word * 4);
577 static inline void brcmnand_write_fc(struct brcmnand_controller *ctrl,
580 __raw_writel(val, ctrl->nand_fc + word * 4);
583 static inline u16 brcmnand_cs_offset(struct brcmnand_controller *ctrl, int cs,
584 enum brcmnand_cs_reg reg)
586 u16 offs_cs0 = ctrl->reg_offsets[BRCMNAND_CS0_BASE];
587 u16 offs_cs1 = ctrl->reg_offsets[BRCMNAND_CS1_BASE];
590 if (cs == 0 && ctrl->cs0_offsets)
591 cs_offs = ctrl->cs0_offsets[reg];
593 cs_offs = ctrl->cs_offsets[reg];
596 return offs_cs1 + (cs - 1) * ctrl->reg_spacing + cs_offs;
598 return offs_cs0 + cs * ctrl->reg_spacing + cs_offs;
601 static inline u32 brcmnand_count_corrected(struct brcmnand_controller *ctrl)
603 if (ctrl->nand_version < 0x0600)
605 return brcmnand_read_reg(ctrl, BRCMNAND_CORR_COUNT);
608 static void brcmnand_wr_corr_thresh(struct brcmnand_host *host, u8 val)
610 struct brcmnand_controller *ctrl = host->ctrl;
611 unsigned int shift = 0, bits;
612 enum brcmnand_reg reg = BRCMNAND_CORR_THRESHOLD;
615 if (ctrl->nand_version >= 0x0702)
617 else if (ctrl->nand_version >= 0x0600)
619 else if (ctrl->nand_version >= 0x0500)
624 if (ctrl->nand_version >= 0x0702) {
626 reg = BRCMNAND_CORR_THRESHOLD_EXT;
627 shift = (cs % 4) * bits;
628 } else if (ctrl->nand_version >= 0x0600) {
630 reg = BRCMNAND_CORR_THRESHOLD_EXT;
631 shift = (cs % 5) * bits;
633 brcmnand_rmw_reg(ctrl, reg, (bits - 1) << shift, shift, val);
636 static inline int brcmnand_cmd_shift(struct brcmnand_controller *ctrl)
638 if (ctrl->nand_version < 0x0602)
643 /***********************************************************************
644 * NAND ACC CONTROL bitfield
646 * Some bits have remained constant throughout hardware revision, while
647 * others have shifted around.
648 ***********************************************************************/
650 /* Constant for all versions (where supported) */
652 /* See BRCMNAND_HAS_CACHE_MODE */
653 ACC_CONTROL_CACHE_MODE = BIT(22),
655 /* See BRCMNAND_HAS_PREFETCH */
656 ACC_CONTROL_PREFETCH = BIT(23),
658 ACC_CONTROL_PAGE_HIT = BIT(24),
659 ACC_CONTROL_WR_PREEMPT = BIT(25),
660 ACC_CONTROL_PARTIAL_PAGE = BIT(26),
661 ACC_CONTROL_RD_ERASED = BIT(27),
662 ACC_CONTROL_FAST_PGM_RDIN = BIT(28),
663 ACC_CONTROL_WR_ECC = BIT(30),
664 ACC_CONTROL_RD_ECC = BIT(31),
667 static inline u32 brcmnand_spare_area_mask(struct brcmnand_controller *ctrl)
669 if (ctrl->nand_version >= 0x0702)
670 return GENMASK(7, 0);
671 else if (ctrl->nand_version >= 0x0600)
672 return GENMASK(6, 0);
674 return GENMASK(5, 0);
677 #define NAND_ACC_CONTROL_ECC_SHIFT 16
678 #define NAND_ACC_CONTROL_ECC_EXT_SHIFT 13
680 static inline u32 brcmnand_ecc_level_mask(struct brcmnand_controller *ctrl)
682 u32 mask = (ctrl->nand_version >= 0x0600) ? 0x1f : 0x0f;
684 mask <<= NAND_ACC_CONTROL_ECC_SHIFT;
686 /* v7.2 includes additional ECC levels */
687 if (ctrl->nand_version >= 0x0702)
688 mask |= 0x7 << NAND_ACC_CONTROL_ECC_EXT_SHIFT;
693 static void brcmnand_set_ecc_enabled(struct brcmnand_host *host, int en)
695 struct brcmnand_controller *ctrl = host->ctrl;
696 u16 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
697 u32 acc_control = nand_readreg(ctrl, offs);
698 u32 ecc_flags = ACC_CONTROL_WR_ECC | ACC_CONTROL_RD_ECC;
701 acc_control |= ecc_flags; /* enable RD/WR ECC */
702 acc_control |= host->hwcfg.ecc_level
703 << NAND_ACC_CONTROL_ECC_SHIFT;
705 acc_control &= ~ecc_flags; /* disable RD/WR ECC */
706 acc_control &= ~brcmnand_ecc_level_mask(ctrl);
709 nand_writereg(ctrl, offs, acc_control);
712 static inline int brcmnand_sector_1k_shift(struct brcmnand_controller *ctrl)
714 if (ctrl->nand_version >= 0x0702)
716 else if (ctrl->nand_version >= 0x0600)
718 else if (ctrl->nand_version >= 0x0500)
724 static int brcmnand_get_sector_size_1k(struct brcmnand_host *host)
726 struct brcmnand_controller *ctrl = host->ctrl;
727 int shift = brcmnand_sector_1k_shift(ctrl);
728 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
729 BRCMNAND_CS_ACC_CONTROL);
734 return (nand_readreg(ctrl, acc_control_offs) >> shift) & 0x1;
737 static void brcmnand_set_sector_size_1k(struct brcmnand_host *host, int val)
739 struct brcmnand_controller *ctrl = host->ctrl;
740 int shift = brcmnand_sector_1k_shift(ctrl);
741 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
742 BRCMNAND_CS_ACC_CONTROL);
748 tmp = nand_readreg(ctrl, acc_control_offs);
749 tmp &= ~(1 << shift);
750 tmp |= (!!val) << shift;
751 nand_writereg(ctrl, acc_control_offs, tmp);
754 /***********************************************************************
756 ***********************************************************************/
759 CS_SELECT_NAND_WP = BIT(29),
760 CS_SELECT_AUTO_DEVICE_ID_CFG = BIT(30),
763 static int bcmnand_ctrl_poll_status(struct brcmnand_controller *ctrl,
764 u32 mask, u32 expected_val,
765 unsigned long timeout_ms)
771 timeout_ms = NAND_POLL_STATUS_TIMEOUT_MS;
773 limit = jiffies + msecs_to_jiffies(timeout_ms);
775 val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS);
776 if ((val & mask) == expected_val)
780 } while (time_after(limit, jiffies));
782 dev_warn(ctrl->dev, "timeout on status poll (expected %x got %x)\n",
783 expected_val, val & mask);
788 static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
790 u32 val = en ? CS_SELECT_NAND_WP : 0;
792 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
795 /***********************************************************************
797 ***********************************************************************/
800 FLASH_DMA_REVISION = 0x00,
801 FLASH_DMA_FIRST_DESC = 0x04,
802 FLASH_DMA_FIRST_DESC_EXT = 0x08,
803 FLASH_DMA_CTRL = 0x0c,
804 FLASH_DMA_MODE = 0x10,
805 FLASH_DMA_STATUS = 0x14,
806 FLASH_DMA_INTERRUPT_DESC = 0x18,
807 FLASH_DMA_INTERRUPT_DESC_EXT = 0x1c,
808 FLASH_DMA_ERROR_STATUS = 0x20,
809 FLASH_DMA_CURRENT_DESC = 0x24,
810 FLASH_DMA_CURRENT_DESC_EXT = 0x28,
813 static inline bool has_flash_dma(struct brcmnand_controller *ctrl)
815 return ctrl->flash_dma_base;
818 static inline bool flash_dma_buf_ok(const void *buf)
820 return buf && !is_vmalloc_addr(buf) &&
821 likely(IS_ALIGNED((uintptr_t)buf, 4));
824 static inline void flash_dma_writel(struct brcmnand_controller *ctrl, u8 offs,
827 brcmnand_writel(val, ctrl->flash_dma_base + offs);
830 static inline u32 flash_dma_readl(struct brcmnand_controller *ctrl, u8 offs)
832 return brcmnand_readl(ctrl->flash_dma_base + offs);
835 /* Low-level operation types: command, address, write, or read */
836 enum brcmnand_llop_type {
843 /***********************************************************************
844 * Internal support functions
845 ***********************************************************************/
847 static inline bool is_hamming_ecc(struct brcmnand_controller *ctrl,
848 struct brcmnand_cfg *cfg)
850 if (ctrl->nand_version <= 0x0701)
851 return cfg->sector_size_1k == 0 && cfg->spare_area_size == 16 &&
852 cfg->ecc_level == 15;
854 return cfg->sector_size_1k == 0 && ((cfg->spare_area_size == 16 &&
855 cfg->ecc_level == 15) ||
856 (cfg->spare_area_size == 28 && cfg->ecc_level == 16));
860 * Set mtd->ooblayout to the appropriate mtd_ooblayout_ops given
861 * the layout/configuration.
862 * Returns -ERRCODE on failure.
864 static int brcmnand_hamming_ooblayout_ecc(struct mtd_info *mtd, int section,
865 struct mtd_oob_region *oobregion)
867 struct nand_chip *chip = mtd_to_nand(mtd);
868 struct brcmnand_host *host = nand_get_controller_data(chip);
869 struct brcmnand_cfg *cfg = &host->hwcfg;
870 int sas = cfg->spare_area_size << cfg->sector_size_1k;
871 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
873 if (section >= sectors)
876 oobregion->offset = (section * sas) + 6;
877 oobregion->length = 3;
882 static int brcmnand_hamming_ooblayout_free(struct mtd_info *mtd, int section,
883 struct mtd_oob_region *oobregion)
885 struct nand_chip *chip = mtd_to_nand(mtd);
886 struct brcmnand_host *host = nand_get_controller_data(chip);
887 struct brcmnand_cfg *cfg = &host->hwcfg;
888 int sas = cfg->spare_area_size << cfg->sector_size_1k;
889 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
891 if (section >= sectors * 2)
894 oobregion->offset = (section / 2) * sas;
897 oobregion->offset += 9;
898 oobregion->length = 7;
900 oobregion->length = 6;
902 /* First sector of each page may have BBI */
905 * Small-page NAND use byte 6 for BBI while large-page
908 if (cfg->page_size > 512)
917 static const struct mtd_ooblayout_ops brcmnand_hamming_ooblayout_ops = {
918 .ecc = brcmnand_hamming_ooblayout_ecc,
919 .free = brcmnand_hamming_ooblayout_free,
922 static int brcmnand_bch_ooblayout_ecc(struct mtd_info *mtd, int section,
923 struct mtd_oob_region *oobregion)
925 struct nand_chip *chip = mtd_to_nand(mtd);
926 struct brcmnand_host *host = nand_get_controller_data(chip);
927 struct brcmnand_cfg *cfg = &host->hwcfg;
928 int sas = cfg->spare_area_size << cfg->sector_size_1k;
929 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
931 if (section >= sectors)
934 oobregion->offset = (section * (sas + 1)) - chip->ecc.bytes;
935 oobregion->length = chip->ecc.bytes;
940 static int brcmnand_bch_ooblayout_free_lp(struct mtd_info *mtd, int section,
941 struct mtd_oob_region *oobregion)
943 struct nand_chip *chip = mtd_to_nand(mtd);
944 struct brcmnand_host *host = nand_get_controller_data(chip);
945 struct brcmnand_cfg *cfg = &host->hwcfg;
946 int sas = cfg->spare_area_size << cfg->sector_size_1k;
947 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
949 if (section >= sectors)
952 if (sas <= chip->ecc.bytes)
955 oobregion->offset = section * sas;
956 oobregion->length = sas - chip->ecc.bytes;
966 static int brcmnand_bch_ooblayout_free_sp(struct mtd_info *mtd, int section,
967 struct mtd_oob_region *oobregion)
969 struct nand_chip *chip = mtd_to_nand(mtd);
970 struct brcmnand_host *host = nand_get_controller_data(chip);
971 struct brcmnand_cfg *cfg = &host->hwcfg;
972 int sas = cfg->spare_area_size << cfg->sector_size_1k;
974 if (section > 1 || sas - chip->ecc.bytes < 6 ||
975 (section && sas - chip->ecc.bytes == 6))
979 oobregion->offset = 0;
980 oobregion->length = 5;
982 oobregion->offset = 6;
983 oobregion->length = sas - chip->ecc.bytes - 6;
989 static const struct mtd_ooblayout_ops brcmnand_bch_lp_ooblayout_ops = {
990 .ecc = brcmnand_bch_ooblayout_ecc,
991 .free = brcmnand_bch_ooblayout_free_lp,
994 static const struct mtd_ooblayout_ops brcmnand_bch_sp_ooblayout_ops = {
995 .ecc = brcmnand_bch_ooblayout_ecc,
996 .free = brcmnand_bch_ooblayout_free_sp,
999 static int brcmstb_choose_ecc_layout(struct brcmnand_host *host)
1001 struct brcmnand_cfg *p = &host->hwcfg;
1002 struct mtd_info *mtd = nand_to_mtd(&host->chip);
1003 struct nand_ecc_ctrl *ecc = &host->chip.ecc;
1004 unsigned int ecc_level = p->ecc_level;
1005 int sas = p->spare_area_size << p->sector_size_1k;
1006 int sectors = p->page_size / (512 << p->sector_size_1k);
1008 if (p->sector_size_1k)
1011 if (is_hamming_ecc(host->ctrl, p)) {
1012 ecc->bytes = 3 * sectors;
1013 mtd_set_ooblayout(mtd, &brcmnand_hamming_ooblayout_ops);
1018 * CONTROLLER_VERSION:
1019 * < v5.0: ECC_REQ = ceil(BCH_T * 13/8)
1020 * >= v5.0: ECC_REQ = ceil(BCH_T * 14/8)
1021 * But we will just be conservative.
1023 ecc->bytes = DIV_ROUND_UP(ecc_level * 14, 8);
1024 if (p->page_size == 512)
1025 mtd_set_ooblayout(mtd, &brcmnand_bch_sp_ooblayout_ops);
1027 mtd_set_ooblayout(mtd, &brcmnand_bch_lp_ooblayout_ops);
1029 if (ecc->bytes >= sas) {
1030 dev_err(&host->pdev->dev,
1031 "error: ECC too large for OOB (ECC bytes %d, spare sector %d)\n",
1039 static void brcmnand_wp(struct mtd_info *mtd, int wp)
1041 struct nand_chip *chip = mtd_to_nand(mtd);
1042 struct brcmnand_host *host = nand_get_controller_data(chip);
1043 struct brcmnand_controller *ctrl = host->ctrl;
1045 if ((ctrl->features & BRCMNAND_HAS_WP) && wp_on == 1) {
1046 static int old_wp = -1;
1050 dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
1055 * make sure ctrl/flash ready before and after
1056 * changing state of #WP pin
1058 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY |
1061 NAND_STATUS_READY, 0);
1065 brcmnand_set_wp(ctrl, wp);
1066 nand_status_op(chip, NULL);
1067 /* NAND_STATUS_WP 0x00 = protected, 0x80 = not protected */
1068 ret = bcmnand_ctrl_poll_status(ctrl,
1074 (wp ? 0 : NAND_STATUS_WP), 0);
1077 dev_err_ratelimited(&host->pdev->dev,
1078 "nand #WP expected %s\n",
1083 /* Helper functions for reading and writing OOB registers */
1084 static inline u8 oob_reg_read(struct brcmnand_controller *ctrl, u32 offs)
1086 u16 offset0, offset10, reg_offs;
1088 offset0 = ctrl->reg_offsets[BRCMNAND_OOB_READ_BASE];
1089 offset10 = ctrl->reg_offsets[BRCMNAND_OOB_READ_10_BASE];
1091 if (offs >= ctrl->max_oob)
1094 if (offs >= 16 && offset10)
1095 reg_offs = offset10 + ((offs - 0x10) & ~0x03);
1097 reg_offs = offset0 + (offs & ~0x03);
1099 return nand_readreg(ctrl, reg_offs) >> (24 - ((offs & 0x03) << 3));
1102 static inline void oob_reg_write(struct brcmnand_controller *ctrl, u32 offs,
1105 u16 offset0, offset10, reg_offs;
1107 offset0 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_BASE];
1108 offset10 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_10_BASE];
1110 if (offs >= ctrl->max_oob)
1113 if (offs >= 16 && offset10)
1114 reg_offs = offset10 + ((offs - 0x10) & ~0x03);
1116 reg_offs = offset0 + (offs & ~0x03);
1118 nand_writereg(ctrl, reg_offs, data);
1122 * read_oob_from_regs - read data from OOB registers
1123 * @ctrl: NAND controller
1124 * @i: sub-page sector index
1125 * @oob: buffer to read to
1126 * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
1127 * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
1129 static int read_oob_from_regs(struct brcmnand_controller *ctrl, int i, u8 *oob,
1130 int sas, int sector_1k)
1132 int tbytes = sas << sector_1k;
1135 /* Adjust OOB values for 1K sector size */
1136 if (sector_1k && (i & 0x01))
1137 tbytes = max(0, tbytes - (int)ctrl->max_oob);
1138 tbytes = min_t(int, tbytes, ctrl->max_oob);
1140 for (j = 0; j < tbytes; j++)
1141 oob[j] = oob_reg_read(ctrl, j);
1146 * write_oob_to_regs - write data to OOB registers
1147 * @i: sub-page sector index
1148 * @oob: buffer to write from
1149 * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
1150 * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
1152 static int write_oob_to_regs(struct brcmnand_controller *ctrl, int i,
1153 const u8 *oob, int sas, int sector_1k)
1155 int tbytes = sas << sector_1k;
1158 /* Adjust OOB values for 1K sector size */
1159 if (sector_1k && (i & 0x01))
1160 tbytes = max(0, tbytes - (int)ctrl->max_oob);
1161 tbytes = min_t(int, tbytes, ctrl->max_oob);
1163 for (j = 0; j < tbytes; j += 4)
1164 oob_reg_write(ctrl, j,
1165 (oob[j + 0] << 24) |
1166 (oob[j + 1] << 16) |
1172 static irqreturn_t brcmnand_ctlrdy_irq(int irq, void *data)
1174 struct brcmnand_controller *ctrl = data;
1176 /* Discard all NAND_CTLRDY interrupts during DMA */
1177 if (ctrl->dma_pending)
1180 complete(&ctrl->done);
1184 /* Handle SoC-specific interrupt hardware */
1185 static irqreturn_t brcmnand_irq(int irq, void *data)
1187 struct brcmnand_controller *ctrl = data;
1189 if (ctrl->soc->ctlrdy_ack(ctrl->soc))
1190 return brcmnand_ctlrdy_irq(irq, data);
1195 static irqreturn_t brcmnand_dma_irq(int irq, void *data)
1197 struct brcmnand_controller *ctrl = data;
1199 complete(&ctrl->dma_done);
1204 static void brcmnand_send_cmd(struct brcmnand_host *host, int cmd)
1206 struct brcmnand_controller *ctrl = host->ctrl;
1209 dev_dbg(ctrl->dev, "send native cmd %d addr_lo 0x%x\n", cmd,
1210 brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS));
1211 BUG_ON(ctrl->cmd_pending != 0);
1212 ctrl->cmd_pending = cmd;
1214 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, 0);
1217 mb(); /* flush previous writes */
1218 brcmnand_write_reg(ctrl, BRCMNAND_CMD_START,
1219 cmd << brcmnand_cmd_shift(ctrl));
1222 /***********************************************************************
1223 * NAND MTD API: read/program/erase
1224 ***********************************************************************/
1226 static void brcmnand_cmd_ctrl(struct nand_chip *chip, int dat,
1229 /* intentionally left blank */
1232 static int brcmnand_waitfunc(struct nand_chip *chip)
1234 struct brcmnand_host *host = nand_get_controller_data(chip);
1235 struct brcmnand_controller *ctrl = host->ctrl;
1236 unsigned long timeo = msecs_to_jiffies(100);
1238 dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending);
1239 if (ctrl->cmd_pending &&
1240 wait_for_completion_timeout(&ctrl->done, timeo) <= 0) {
1241 u32 cmd = brcmnand_read_reg(ctrl, BRCMNAND_CMD_START)
1242 >> brcmnand_cmd_shift(ctrl);
1244 dev_err_ratelimited(ctrl->dev,
1245 "timeout waiting for command %#02x\n", cmd);
1246 dev_err_ratelimited(ctrl->dev, "intfc status %08x\n",
1247 brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS));
1249 ctrl->cmd_pending = 0;
1250 return brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
1259 LLOP_RETURN_IDLE = BIT(31),
1261 LLOP_DATA_MASK = GENMASK(15, 0),
1264 static int brcmnand_low_level_op(struct brcmnand_host *host,
1265 enum brcmnand_llop_type type, u32 data,
1268 struct nand_chip *chip = &host->chip;
1269 struct brcmnand_controller *ctrl = host->ctrl;
1272 tmp = data & LLOP_DATA_MASK;
1275 tmp |= LLOP_WE | LLOP_CLE;
1279 tmp |= LLOP_WE | LLOP_ALE;
1292 tmp |= LLOP_RETURN_IDLE;
1294 dev_dbg(ctrl->dev, "ll_op cmd %#x\n", tmp);
1296 brcmnand_write_reg(ctrl, BRCMNAND_LL_OP, tmp);
1297 (void)brcmnand_read_reg(ctrl, BRCMNAND_LL_OP);
1299 brcmnand_send_cmd(host, CMD_LOW_LEVEL_OP);
1300 return brcmnand_waitfunc(chip);
1303 static void brcmnand_cmdfunc(struct nand_chip *chip, unsigned command,
1304 int column, int page_addr)
1306 struct mtd_info *mtd = nand_to_mtd(chip);
1307 struct brcmnand_host *host = nand_get_controller_data(chip);
1308 struct brcmnand_controller *ctrl = host->ctrl;
1309 u64 addr = (u64)page_addr << chip->page_shift;
1312 if (command == NAND_CMD_READID || command == NAND_CMD_PARAM ||
1313 command == NAND_CMD_RNDOUT)
1315 /* Avoid propagating a negative, don't-care address */
1316 else if (page_addr < 0)
1319 dev_dbg(ctrl->dev, "cmd 0x%x addr 0x%llx\n", command,
1320 (unsigned long long)addr);
1322 host->last_cmd = command;
1323 host->last_byte = 0;
1324 host->last_addr = addr;
1327 case NAND_CMD_RESET:
1328 native_cmd = CMD_FLASH_RESET;
1330 case NAND_CMD_STATUS:
1331 native_cmd = CMD_STATUS_READ;
1333 case NAND_CMD_READID:
1334 native_cmd = CMD_DEVICE_ID_READ;
1336 case NAND_CMD_READOOB:
1337 native_cmd = CMD_SPARE_AREA_READ;
1339 case NAND_CMD_ERASE1:
1340 native_cmd = CMD_BLOCK_ERASE;
1341 brcmnand_wp(mtd, 0);
1343 case NAND_CMD_PARAM:
1344 native_cmd = CMD_PARAMETER_READ;
1346 case NAND_CMD_SET_FEATURES:
1347 case NAND_CMD_GET_FEATURES:
1348 brcmnand_low_level_op(host, LL_OP_CMD, command, false);
1349 brcmnand_low_level_op(host, LL_OP_ADDR, column, false);
1351 case NAND_CMD_RNDOUT:
1352 native_cmd = CMD_PARAMETER_CHANGE_COL;
1353 addr &= ~((u64)(FC_BYTES - 1));
1355 * HW quirk: PARAMETER_CHANGE_COL requires SECTOR_SIZE_1K=0
1356 * NB: hwcfg.sector_size_1k may not be initialized yet
1358 if (brcmnand_get_sector_size_1k(host)) {
1359 host->hwcfg.sector_size_1k =
1360 brcmnand_get_sector_size_1k(host);
1361 brcmnand_set_sector_size_1k(host, 0);
1369 brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
1370 (host->cs << 16) | ((addr >> 32) & 0xffff));
1371 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
1372 brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS, lower_32_bits(addr));
1373 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
1375 brcmnand_send_cmd(host, native_cmd);
1376 brcmnand_waitfunc(chip);
1378 if (native_cmd == CMD_PARAMETER_READ ||
1379 native_cmd == CMD_PARAMETER_CHANGE_COL) {
1380 /* Copy flash cache word-wise */
1381 u32 *flash_cache = (u32 *)ctrl->flash_cache;
1384 brcmnand_soc_data_bus_prepare(ctrl->soc, true);
1387 * Must cache the FLASH_CACHE now, since changes in
1388 * SECTOR_SIZE_1K may invalidate it
1390 for (i = 0; i < FC_WORDS; i++)
1392 * Flash cache is big endian for parameter pages, at
1395 flash_cache[i] = be32_to_cpu(brcmnand_read_fc(ctrl, i));
1397 brcmnand_soc_data_bus_unprepare(ctrl->soc, true);
1399 /* Cleanup from HW quirk: restore SECTOR_SIZE_1K */
1400 if (host->hwcfg.sector_size_1k)
1401 brcmnand_set_sector_size_1k(host,
1402 host->hwcfg.sector_size_1k);
1405 /* Re-enable protection is necessary only after erase */
1406 if (command == NAND_CMD_ERASE1)
1407 brcmnand_wp(mtd, 1);
1410 static uint8_t brcmnand_read_byte(struct nand_chip *chip)
1412 struct brcmnand_host *host = nand_get_controller_data(chip);
1413 struct brcmnand_controller *ctrl = host->ctrl;
1417 switch (host->last_cmd) {
1418 case NAND_CMD_READID:
1419 if (host->last_byte < 4)
1420 ret = brcmnand_read_reg(ctrl, BRCMNAND_ID) >>
1421 (24 - (host->last_byte << 3));
1422 else if (host->last_byte < 8)
1423 ret = brcmnand_read_reg(ctrl, BRCMNAND_ID_EXT) >>
1424 (56 - (host->last_byte << 3));
1427 case NAND_CMD_READOOB:
1428 ret = oob_reg_read(ctrl, host->last_byte);
1431 case NAND_CMD_STATUS:
1432 ret = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
1434 if (wp_on) /* hide WP status */
1435 ret |= NAND_STATUS_WP;
1438 case NAND_CMD_PARAM:
1439 case NAND_CMD_RNDOUT:
1440 addr = host->last_addr + host->last_byte;
1441 offs = addr & (FC_BYTES - 1);
1443 /* At FC_BYTES boundary, switch to next column */
1444 if (host->last_byte > 0 && offs == 0)
1445 nand_change_read_column_op(chip, addr, NULL, 0, false);
1447 ret = ctrl->flash_cache[offs];
1449 case NAND_CMD_GET_FEATURES:
1450 if (host->last_byte >= ONFI_SUBFEATURE_PARAM_LEN) {
1453 bool last = host->last_byte ==
1454 ONFI_SUBFEATURE_PARAM_LEN - 1;
1455 brcmnand_low_level_op(host, LL_OP_RD, 0, last);
1456 ret = brcmnand_read_reg(ctrl, BRCMNAND_LL_RDATA) & 0xff;
1460 dev_dbg(ctrl->dev, "read byte = 0x%02x\n", ret);
1466 static void brcmnand_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
1470 for (i = 0; i < len; i++, buf++)
1471 *buf = brcmnand_read_byte(chip);
1474 static void brcmnand_write_buf(struct nand_chip *chip, const uint8_t *buf,
1478 struct brcmnand_host *host = nand_get_controller_data(chip);
1480 switch (host->last_cmd) {
1481 case NAND_CMD_SET_FEATURES:
1482 for (i = 0; i < len; i++)
1483 brcmnand_low_level_op(host, LL_OP_WR, buf[i],
1493 * Construct a FLASH_DMA descriptor as part of a linked list. You must know the
1494 * following ahead of time:
1495 * - Is this descriptor the beginning or end of a linked list?
1496 * - What is the (DMA) address of the next descriptor in the linked list?
1498 static int brcmnand_fill_dma_desc(struct brcmnand_host *host,
1499 struct brcm_nand_dma_desc *desc, u64 addr,
1500 dma_addr_t buf, u32 len, u8 dma_cmd,
1501 bool begin, bool end,
1502 dma_addr_t next_desc)
1504 memset(desc, 0, sizeof(*desc));
1505 /* Descriptors are written in native byte order (wordwise) */
1506 desc->next_desc = lower_32_bits(next_desc);
1507 desc->next_desc_ext = upper_32_bits(next_desc);
1508 desc->cmd_irq = (dma_cmd << 24) |
1509 (end ? (0x03 << 8) : 0) | /* IRQ | STOP */
1510 (!!begin) | ((!!end) << 1); /* head, tail */
1511 #ifdef CONFIG_CPU_BIG_ENDIAN
1512 desc->cmd_irq |= 0x01 << 12;
1514 desc->dram_addr = lower_32_bits(buf);
1515 desc->dram_addr_ext = upper_32_bits(buf);
1516 desc->tfr_len = len;
1517 desc->total_len = len;
1518 desc->flash_addr = lower_32_bits(addr);
1519 desc->flash_addr_ext = upper_32_bits(addr);
1520 desc->cs = host->cs;
1521 desc->status_valid = 0x01;
1526 * Kick the FLASH_DMA engine, with a given DMA descriptor
1528 static void brcmnand_dma_run(struct brcmnand_host *host, dma_addr_t desc)
1530 struct brcmnand_controller *ctrl = host->ctrl;
1531 unsigned long timeo = msecs_to_jiffies(100);
1533 flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC, lower_32_bits(desc));
1534 (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC);
1535 flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC_EXT, upper_32_bits(desc));
1536 (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC_EXT);
1538 /* Start FLASH_DMA engine */
1539 ctrl->dma_pending = true;
1540 mb(); /* flush previous writes */
1541 flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0x03); /* wake | run */
1543 if (wait_for_completion_timeout(&ctrl->dma_done, timeo) <= 0) {
1545 "timeout waiting for DMA; status %#x, error status %#x\n",
1546 flash_dma_readl(ctrl, FLASH_DMA_STATUS),
1547 flash_dma_readl(ctrl, FLASH_DMA_ERROR_STATUS));
1549 ctrl->dma_pending = false;
1550 flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0); /* force stop */
1553 static int brcmnand_dma_trans(struct brcmnand_host *host, u64 addr, u32 *buf,
1554 u32 len, u8 dma_cmd)
1556 struct brcmnand_controller *ctrl = host->ctrl;
1558 int dir = dma_cmd == CMD_PAGE_READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1560 buf_pa = dma_map_single(ctrl->dev, buf, len, dir);
1561 if (dma_mapping_error(ctrl->dev, buf_pa)) {
1562 dev_err(ctrl->dev, "unable to map buffer for DMA\n");
1566 brcmnand_fill_dma_desc(host, ctrl->dma_desc, addr, buf_pa, len,
1567 dma_cmd, true, true, 0);
1569 brcmnand_dma_run(host, ctrl->dma_pa);
1571 dma_unmap_single(ctrl->dev, buf_pa, len, dir);
1573 if (ctrl->dma_desc->status_valid & FLASH_DMA_ECC_ERROR)
1575 else if (ctrl->dma_desc->status_valid & FLASH_DMA_CORR_ERROR)
1582 * Assumes proper CS is already set
1584 static int brcmnand_read_by_pio(struct mtd_info *mtd, struct nand_chip *chip,
1585 u64 addr, unsigned int trans, u32 *buf,
1586 u8 *oob, u64 *err_addr)
1588 struct brcmnand_host *host = nand_get_controller_data(chip);
1589 struct brcmnand_controller *ctrl = host->ctrl;
1592 /* Clear error addresses */
1593 brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_ADDR, 0);
1594 brcmnand_write_reg(ctrl, BRCMNAND_CORR_ADDR, 0);
1595 brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_EXT_ADDR, 0);
1596 brcmnand_write_reg(ctrl, BRCMNAND_CORR_EXT_ADDR, 0);
1598 brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
1599 (host->cs << 16) | ((addr >> 32) & 0xffff));
1600 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
1602 for (i = 0; i < trans; i++, addr += FC_BYTES) {
1603 brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS,
1604 lower_32_bits(addr));
1605 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
1606 /* SPARE_AREA_READ does not use ECC, so just use PAGE_READ */
1607 brcmnand_send_cmd(host, CMD_PAGE_READ);
1608 brcmnand_waitfunc(chip);
1611 brcmnand_soc_data_bus_prepare(ctrl->soc, false);
1613 for (j = 0; j < FC_WORDS; j++, buf++)
1614 *buf = brcmnand_read_fc(ctrl, j);
1616 brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
1620 oob += read_oob_from_regs(ctrl, i, oob,
1621 mtd->oobsize / trans,
1622 host->hwcfg.sector_size_1k);
1625 *err_addr = brcmnand_read_reg(ctrl,
1626 BRCMNAND_UNCORR_ADDR) |
1627 ((u64)(brcmnand_read_reg(ctrl,
1628 BRCMNAND_UNCORR_EXT_ADDR)
1635 *err_addr = brcmnand_read_reg(ctrl,
1636 BRCMNAND_CORR_ADDR) |
1637 ((u64)(brcmnand_read_reg(ctrl,
1638 BRCMNAND_CORR_EXT_ADDR)
1649 * Check a page to see if it is erased (w/ bitflips) after an uncorrectable ECC
1652 * Because the HW ECC signals an ECC error if an erase paged has even a single
1653 * bitflip, we must check each ECC error to see if it is actually an erased
1654 * page with bitflips, not a truly corrupted page.
1656 * On a real error, return a negative error code (-EBADMSG for ECC error), and
1657 * buf will contain raw data.
1658 * Otherwise, buf gets filled with 0xffs and return the maximum number of
1659 * bitflips-per-ECC-sector to the caller.
1662 static int brcmstb_nand_verify_erased_page(struct mtd_info *mtd,
1663 struct nand_chip *chip, void *buf, u64 addr)
1666 void *oob = chip->oob_poi;
1668 int page = addr >> chip->page_shift;
1672 buf = nand_get_data_buf(chip);
1674 sas = mtd->oobsize / chip->ecc.steps;
1676 /* read without ecc for verification */
1677 ret = chip->ecc.read_page_raw(chip, buf, true, page);
1681 for (i = 0; i < chip->ecc.steps; i++, oob += sas) {
1682 ret = nand_check_erased_ecc_chunk(buf, chip->ecc.size,
1684 chip->ecc.strength);
1688 bitflips = max(bitflips, ret);
1694 static int brcmnand_read(struct mtd_info *mtd, struct nand_chip *chip,
1695 u64 addr, unsigned int trans, u32 *buf, u8 *oob)
1697 struct brcmnand_host *host = nand_get_controller_data(chip);
1698 struct brcmnand_controller *ctrl = host->ctrl;
1703 dev_dbg(ctrl->dev, "read %llx -> %p\n", (unsigned long long)addr, buf);
1706 brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_COUNT, 0);
1708 if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
1709 err = brcmnand_dma_trans(host, addr, buf, trans * FC_BYTES,
1712 if (mtd_is_bitflip_or_eccerr(err))
1719 memset(oob, 0x99, mtd->oobsize);
1721 err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf,
1725 if (mtd_is_eccerr(err)) {
1727 * On controller version and 7.0, 7.1 , DMA read after a
1728 * prior PIO read that reported uncorrectable error,
1729 * the DMA engine captures this error following DMA read
1730 * cleared only on subsequent DMA read, so just retry once
1731 * to clear a possible false error reported for current DMA
1734 if ((ctrl->nand_version == 0x0700) ||
1735 (ctrl->nand_version == 0x0701)) {
1743 * Controller version 7.2 has hw encoder to detect erased page
1744 * bitflips, apply sw verification for older controllers only
1746 if (ctrl->nand_version < 0x0702) {
1747 err = brcmstb_nand_verify_erased_page(mtd, chip, buf,
1749 /* erased page bitflips corrected */
1754 dev_dbg(ctrl->dev, "uncorrectable error at 0x%llx\n",
1755 (unsigned long long)err_addr);
1756 mtd->ecc_stats.failed++;
1757 /* NAND layer expects zero on ECC errors */
1761 if (mtd_is_bitflip(err)) {
1762 unsigned int corrected = brcmnand_count_corrected(ctrl);
1764 dev_dbg(ctrl->dev, "corrected error at 0x%llx\n",
1765 (unsigned long long)err_addr);
1766 mtd->ecc_stats.corrected += corrected;
1767 /* Always exceed the software-imposed threshold */
1768 return max(mtd->bitflip_threshold, corrected);
1774 static int brcmnand_read_page(struct nand_chip *chip, uint8_t *buf,
1775 int oob_required, int page)
1777 struct mtd_info *mtd = nand_to_mtd(chip);
1778 struct brcmnand_host *host = nand_get_controller_data(chip);
1779 u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
1781 nand_read_page_op(chip, page, 0, NULL, 0);
1783 return brcmnand_read(mtd, chip, host->last_addr,
1784 mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
1787 static int brcmnand_read_page_raw(struct nand_chip *chip, uint8_t *buf,
1788 int oob_required, int page)
1790 struct brcmnand_host *host = nand_get_controller_data(chip);
1791 struct mtd_info *mtd = nand_to_mtd(chip);
1792 u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
1795 nand_read_page_op(chip, page, 0, NULL, 0);
1797 brcmnand_set_ecc_enabled(host, 0);
1798 ret = brcmnand_read(mtd, chip, host->last_addr,
1799 mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
1800 brcmnand_set_ecc_enabled(host, 1);
1804 static int brcmnand_read_oob(struct nand_chip *chip, int page)
1806 struct mtd_info *mtd = nand_to_mtd(chip);
1808 return brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
1809 mtd->writesize >> FC_SHIFT,
1810 NULL, (u8 *)chip->oob_poi);
1813 static int brcmnand_read_oob_raw(struct nand_chip *chip, int page)
1815 struct mtd_info *mtd = nand_to_mtd(chip);
1816 struct brcmnand_host *host = nand_get_controller_data(chip);
1818 brcmnand_set_ecc_enabled(host, 0);
1819 brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
1820 mtd->writesize >> FC_SHIFT,
1821 NULL, (u8 *)chip->oob_poi);
1822 brcmnand_set_ecc_enabled(host, 1);
1826 static int brcmnand_write(struct mtd_info *mtd, struct nand_chip *chip,
1827 u64 addr, const u32 *buf, u8 *oob)
1829 struct brcmnand_host *host = nand_get_controller_data(chip);
1830 struct brcmnand_controller *ctrl = host->ctrl;
1831 unsigned int i, j, trans = mtd->writesize >> FC_SHIFT;
1832 int status, ret = 0;
1834 dev_dbg(ctrl->dev, "write %llx <- %p\n", (unsigned long long)addr, buf);
1836 if (unlikely((unsigned long)buf & 0x03)) {
1837 dev_warn(ctrl->dev, "unaligned buffer: %p\n", buf);
1838 buf = (u32 *)((unsigned long)buf & ~0x03);
1841 brcmnand_wp(mtd, 0);
1843 for (i = 0; i < ctrl->max_oob; i += 4)
1844 oob_reg_write(ctrl, i, 0xffffffff);
1846 if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
1847 if (brcmnand_dma_trans(host, addr, (u32 *)buf,
1848 mtd->writesize, CMD_PROGRAM_PAGE))
1853 brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
1854 (host->cs << 16) | ((addr >> 32) & 0xffff));
1855 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
1857 for (i = 0; i < trans; i++, addr += FC_BYTES) {
1858 /* full address MUST be set before populating FC */
1859 brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS,
1860 lower_32_bits(addr));
1861 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
1864 brcmnand_soc_data_bus_prepare(ctrl->soc, false);
1866 for (j = 0; j < FC_WORDS; j++, buf++)
1867 brcmnand_write_fc(ctrl, j, *buf);
1869 brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
1871 for (j = 0; j < FC_WORDS; j++)
1872 brcmnand_write_fc(ctrl, j, 0xffffffff);
1876 oob += write_oob_to_regs(ctrl, i, oob,
1877 mtd->oobsize / trans,
1878 host->hwcfg.sector_size_1k);
1881 /* we cannot use SPARE_AREA_PROGRAM when PARTIAL_PAGE_EN=0 */
1882 brcmnand_send_cmd(host, CMD_PROGRAM_PAGE);
1883 status = brcmnand_waitfunc(chip);
1885 if (status & NAND_STATUS_FAIL) {
1886 dev_info(ctrl->dev, "program failed at %llx\n",
1887 (unsigned long long)addr);
1893 brcmnand_wp(mtd, 1);
1897 static int brcmnand_write_page(struct nand_chip *chip, const uint8_t *buf,
1898 int oob_required, int page)
1900 struct mtd_info *mtd = nand_to_mtd(chip);
1901 struct brcmnand_host *host = nand_get_controller_data(chip);
1902 void *oob = oob_required ? chip->oob_poi : NULL;
1904 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1905 brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
1907 return nand_prog_page_end_op(chip);
1910 static int brcmnand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
1911 int oob_required, int page)
1913 struct mtd_info *mtd = nand_to_mtd(chip);
1914 struct brcmnand_host *host = nand_get_controller_data(chip);
1915 void *oob = oob_required ? chip->oob_poi : NULL;
1917 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1918 brcmnand_set_ecc_enabled(host, 0);
1919 brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
1920 brcmnand_set_ecc_enabled(host, 1);
1922 return nand_prog_page_end_op(chip);
1925 static int brcmnand_write_oob(struct nand_chip *chip, int page)
1927 return brcmnand_write(nand_to_mtd(chip), chip,
1928 (u64)page << chip->page_shift, NULL,
1932 static int brcmnand_write_oob_raw(struct nand_chip *chip, int page)
1934 struct mtd_info *mtd = nand_to_mtd(chip);
1935 struct brcmnand_host *host = nand_get_controller_data(chip);
1938 brcmnand_set_ecc_enabled(host, 0);
1939 ret = brcmnand_write(mtd, chip, (u64)page << chip->page_shift, NULL,
1940 (u8 *)chip->oob_poi);
1941 brcmnand_set_ecc_enabled(host, 1);
1946 /***********************************************************************
1947 * Per-CS setup (1 NAND device)
1948 ***********************************************************************/
1950 static int brcmnand_set_cfg(struct brcmnand_host *host,
1951 struct brcmnand_cfg *cfg)
1953 struct brcmnand_controller *ctrl = host->ctrl;
1954 struct nand_chip *chip = &host->chip;
1955 u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
1956 u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
1957 BRCMNAND_CS_CFG_EXT);
1958 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
1959 BRCMNAND_CS_ACC_CONTROL);
1960 u8 block_size = 0, page_size = 0, device_size = 0;
1963 if (ctrl->block_sizes) {
1966 for (i = 0, found = 0; ctrl->block_sizes[i]; i++)
1967 if (ctrl->block_sizes[i] * 1024 == cfg->block_size) {
1972 dev_warn(ctrl->dev, "invalid block size %u\n",
1977 block_size = ffs(cfg->block_size) - ffs(BRCMNAND_MIN_BLOCKSIZE);
1980 if (cfg->block_size < BRCMNAND_MIN_BLOCKSIZE || (ctrl->max_block_size &&
1981 cfg->block_size > ctrl->max_block_size)) {
1982 dev_warn(ctrl->dev, "invalid block size %u\n",
1987 if (ctrl->page_sizes) {
1990 for (i = 0, found = 0; ctrl->page_sizes[i]; i++)
1991 if (ctrl->page_sizes[i] == cfg->page_size) {
1996 dev_warn(ctrl->dev, "invalid page size %u\n",
2001 page_size = ffs(cfg->page_size) - ffs(BRCMNAND_MIN_PAGESIZE);
2004 if (cfg->page_size < BRCMNAND_MIN_PAGESIZE || (ctrl->max_page_size &&
2005 cfg->page_size > ctrl->max_page_size)) {
2006 dev_warn(ctrl->dev, "invalid page size %u\n", cfg->page_size);
2010 if (fls64(cfg->device_size) < fls64(BRCMNAND_MIN_DEVSIZE)) {
2011 dev_warn(ctrl->dev, "invalid device size 0x%llx\n",
2012 (unsigned long long)cfg->device_size);
2015 device_size = fls64(cfg->device_size) - fls64(BRCMNAND_MIN_DEVSIZE);
2017 tmp = (cfg->blk_adr_bytes << CFG_BLK_ADR_BYTES_SHIFT) |
2018 (cfg->col_adr_bytes << CFG_COL_ADR_BYTES_SHIFT) |
2019 (cfg->ful_adr_bytes << CFG_FUL_ADR_BYTES_SHIFT) |
2020 (!!(cfg->device_width == 16) << CFG_BUS_WIDTH_SHIFT) |
2021 (device_size << CFG_DEVICE_SIZE_SHIFT);
2022 if (cfg_offs == cfg_ext_offs) {
2023 tmp |= (page_size << CFG_PAGE_SIZE_SHIFT) |
2024 (block_size << CFG_BLK_SIZE_SHIFT);
2025 nand_writereg(ctrl, cfg_offs, tmp);
2027 nand_writereg(ctrl, cfg_offs, tmp);
2028 tmp = (page_size << CFG_EXT_PAGE_SIZE_SHIFT) |
2029 (block_size << CFG_EXT_BLK_SIZE_SHIFT);
2030 nand_writereg(ctrl, cfg_ext_offs, tmp);
2033 tmp = nand_readreg(ctrl, acc_control_offs);
2034 tmp &= ~brcmnand_ecc_level_mask(ctrl);
2035 tmp |= cfg->ecc_level << NAND_ACC_CONTROL_ECC_SHIFT;
2036 tmp &= ~brcmnand_spare_area_mask(ctrl);
2037 tmp |= cfg->spare_area_size;
2038 nand_writereg(ctrl, acc_control_offs, tmp);
2040 brcmnand_set_sector_size_1k(host, cfg->sector_size_1k);
2042 /* threshold = ceil(BCH-level * 0.75) */
2043 brcmnand_wr_corr_thresh(host, DIV_ROUND_UP(chip->ecc.strength * 3, 4));
2048 static void brcmnand_print_cfg(struct brcmnand_host *host,
2049 char *buf, struct brcmnand_cfg *cfg)
2052 "%lluMiB total, %uKiB blocks, %u%s pages, %uB OOB, %u-bit",
2053 (unsigned long long)cfg->device_size >> 20,
2054 cfg->block_size >> 10,
2055 cfg->page_size >= 1024 ? cfg->page_size >> 10 : cfg->page_size,
2056 cfg->page_size >= 1024 ? "KiB" : "B",
2057 cfg->spare_area_size, cfg->device_width);
2059 /* Account for Hamming ECC and for BCH 512B vs 1KiB sectors */
2060 if (is_hamming_ecc(host->ctrl, cfg))
2061 sprintf(buf, ", Hamming ECC");
2062 else if (cfg->sector_size_1k)
2063 sprintf(buf, ", BCH-%u (1KiB sector)", cfg->ecc_level << 1);
2065 sprintf(buf, ", BCH-%u", cfg->ecc_level);
2069 * Minimum number of bytes to address a page. Calculated as:
2070 * roundup(log2(size / page-size) / 8)
2072 * NB: the following does not "round up" for non-power-of-2 'size'; but this is
2073 * OK because many other things will break if 'size' is irregular...
2075 static inline int get_blk_adr_bytes(u64 size, u32 writesize)
2077 return ALIGN(ilog2(size) - ilog2(writesize), 8) >> 3;
2080 static int brcmnand_setup_dev(struct brcmnand_host *host)
2082 struct mtd_info *mtd = nand_to_mtd(&host->chip);
2083 struct nand_chip *chip = &host->chip;
2084 struct brcmnand_controller *ctrl = host->ctrl;
2085 struct brcmnand_cfg *cfg = &host->hwcfg;
2087 u32 offs, tmp, oob_sector;
2090 memset(cfg, 0, sizeof(*cfg));
2092 ret = of_property_read_u32(nand_get_flash_node(chip),
2093 "brcm,nand-oob-sector-size",
2096 /* Use detected size */
2097 cfg->spare_area_size = mtd->oobsize /
2098 (mtd->writesize >> FC_SHIFT);
2100 cfg->spare_area_size = oob_sector;
2102 if (cfg->spare_area_size > ctrl->max_oob)
2103 cfg->spare_area_size = ctrl->max_oob;
2105 * Set oobsize to be consistent with controller's spare_area_size, as
2106 * the rest is inaccessible.
2108 mtd->oobsize = cfg->spare_area_size * (mtd->writesize >> FC_SHIFT);
2110 cfg->device_size = mtd->size;
2111 cfg->block_size = mtd->erasesize;
2112 cfg->page_size = mtd->writesize;
2113 cfg->device_width = (chip->options & NAND_BUSWIDTH_16) ? 16 : 8;
2114 cfg->col_adr_bytes = 2;
2115 cfg->blk_adr_bytes = get_blk_adr_bytes(mtd->size, mtd->writesize);
2117 if (chip->ecc.mode != NAND_ECC_HW) {
2118 dev_err(ctrl->dev, "only HW ECC supported; selected: %d\n",
2123 if (chip->ecc.algo == NAND_ECC_UNKNOWN) {
2124 if (chip->ecc.strength == 1 && chip->ecc.size == 512)
2125 /* Default to Hamming for 1-bit ECC, if unspecified */
2126 chip->ecc.algo = NAND_ECC_HAMMING;
2128 /* Otherwise, BCH */
2129 chip->ecc.algo = NAND_ECC_BCH;
2132 if (chip->ecc.algo == NAND_ECC_HAMMING && (chip->ecc.strength != 1 ||
2133 chip->ecc.size != 512)) {
2134 dev_err(ctrl->dev, "invalid Hamming params: %d bits per %d bytes\n",
2135 chip->ecc.strength, chip->ecc.size);
2139 switch (chip->ecc.size) {
2141 if (chip->ecc.algo == NAND_ECC_HAMMING)
2142 cfg->ecc_level = 15;
2144 cfg->ecc_level = chip->ecc.strength;
2145 cfg->sector_size_1k = 0;
2148 if (!(ctrl->features & BRCMNAND_HAS_1K_SECTORS)) {
2149 dev_err(ctrl->dev, "1KB sectors not supported\n");
2152 if (chip->ecc.strength & 0x1) {
2154 "odd ECC not supported with 1KB sectors\n");
2158 cfg->ecc_level = chip->ecc.strength >> 1;
2159 cfg->sector_size_1k = 1;
2162 dev_err(ctrl->dev, "unsupported ECC size: %d\n",
2167 cfg->ful_adr_bytes = cfg->blk_adr_bytes;
2168 if (mtd->writesize > 512)
2169 cfg->ful_adr_bytes += cfg->col_adr_bytes;
2171 cfg->ful_adr_bytes += 1;
2173 ret = brcmnand_set_cfg(host, cfg);
2177 brcmnand_set_ecc_enabled(host, 1);
2179 brcmnand_print_cfg(host, msg, cfg);
2180 dev_info(ctrl->dev, "detected %s\n", msg);
2182 /* Configure ACC_CONTROL */
2183 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
2184 tmp = nand_readreg(ctrl, offs);
2185 tmp &= ~ACC_CONTROL_PARTIAL_PAGE;
2186 tmp &= ~ACC_CONTROL_RD_ERASED;
2188 /* We need to turn on Read from erased paged protected by ECC */
2189 if (ctrl->nand_version >= 0x0702)
2190 tmp |= ACC_CONTROL_RD_ERASED;
2191 tmp &= ~ACC_CONTROL_FAST_PGM_RDIN;
2192 if (ctrl->features & BRCMNAND_HAS_PREFETCH)
2193 tmp &= ~ACC_CONTROL_PREFETCH;
2195 nand_writereg(ctrl, offs, tmp);
2200 static int brcmnand_attach_chip(struct nand_chip *chip)
2202 struct mtd_info *mtd = nand_to_mtd(chip);
2203 struct brcmnand_host *host = nand_get_controller_data(chip);
2206 chip->options |= NAND_NO_SUBPAGE_WRITE;
2208 * Avoid (for instance) kmap()'d buffers from JFFS2, which we can't DMA
2209 * to/from, and have nand_base pass us a bounce buffer instead, as
2212 chip->options |= NAND_USE_BOUNCE_BUFFER;
2214 if (chip->bbt_options & NAND_BBT_USE_FLASH)
2215 chip->bbt_options |= NAND_BBT_NO_OOB;
2217 if (brcmnand_setup_dev(host))
2220 chip->ecc.size = host->hwcfg.sector_size_1k ? 1024 : 512;
2222 /* only use our internal HW threshold */
2223 mtd->bitflip_threshold = 1;
2225 ret = brcmstb_choose_ecc_layout(host);
2230 static const struct nand_controller_ops brcmnand_controller_ops = {
2231 .attach_chip = brcmnand_attach_chip,
2234 static int brcmnand_init_cs(struct brcmnand_host *host, struct device_node *dn)
2236 struct brcmnand_controller *ctrl = host->ctrl;
2237 struct platform_device *pdev = host->pdev;
2238 struct mtd_info *mtd;
2239 struct nand_chip *chip;
2243 ret = of_property_read_u32(dn, "reg", &host->cs);
2245 dev_err(&pdev->dev, "can't get chip-select\n");
2249 mtd = nand_to_mtd(&host->chip);
2252 nand_set_flash_node(chip, dn);
2253 nand_set_controller_data(chip, host);
2254 mtd->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "brcmnand.%d",
2259 mtd->owner = THIS_MODULE;
2260 mtd->dev.parent = &pdev->dev;
2262 chip->legacy.cmd_ctrl = brcmnand_cmd_ctrl;
2263 chip->legacy.cmdfunc = brcmnand_cmdfunc;
2264 chip->legacy.waitfunc = brcmnand_waitfunc;
2265 chip->legacy.read_byte = brcmnand_read_byte;
2266 chip->legacy.read_buf = brcmnand_read_buf;
2267 chip->legacy.write_buf = brcmnand_write_buf;
2269 chip->ecc.mode = NAND_ECC_HW;
2270 chip->ecc.read_page = brcmnand_read_page;
2271 chip->ecc.write_page = brcmnand_write_page;
2272 chip->ecc.read_page_raw = brcmnand_read_page_raw;
2273 chip->ecc.write_page_raw = brcmnand_write_page_raw;
2274 chip->ecc.write_oob_raw = brcmnand_write_oob_raw;
2275 chip->ecc.read_oob_raw = brcmnand_read_oob_raw;
2276 chip->ecc.read_oob = brcmnand_read_oob;
2277 chip->ecc.write_oob = brcmnand_write_oob;
2279 chip->controller = &ctrl->controller;
2282 * The bootloader might have configured 16bit mode but
2283 * NAND READID command only works in 8bit mode. We force
2284 * 8bit mode here to ensure that NAND READID commands works.
2286 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2287 nand_writereg(ctrl, cfg_offs,
2288 nand_readreg(ctrl, cfg_offs) & ~CFG_BUS_WIDTH);
2290 ret = nand_scan(chip, 1);
2294 ret = mtd_device_register(mtd, NULL, 0);
2301 static void brcmnand_save_restore_cs_config(struct brcmnand_host *host,
2304 struct brcmnand_controller *ctrl = host->ctrl;
2305 u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2306 u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
2307 BRCMNAND_CS_CFG_EXT);
2308 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
2309 BRCMNAND_CS_ACC_CONTROL);
2310 u16 t1_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING1);
2311 u16 t2_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING2);
2314 nand_writereg(ctrl, cfg_offs, host->hwcfg.config);
2315 if (cfg_offs != cfg_ext_offs)
2316 nand_writereg(ctrl, cfg_ext_offs,
2317 host->hwcfg.config_ext);
2318 nand_writereg(ctrl, acc_control_offs, host->hwcfg.acc_control);
2319 nand_writereg(ctrl, t1_offs, host->hwcfg.timing_1);
2320 nand_writereg(ctrl, t2_offs, host->hwcfg.timing_2);
2322 host->hwcfg.config = nand_readreg(ctrl, cfg_offs);
2323 if (cfg_offs != cfg_ext_offs)
2324 host->hwcfg.config_ext =
2325 nand_readreg(ctrl, cfg_ext_offs);
2326 host->hwcfg.acc_control = nand_readreg(ctrl, acc_control_offs);
2327 host->hwcfg.timing_1 = nand_readreg(ctrl, t1_offs);
2328 host->hwcfg.timing_2 = nand_readreg(ctrl, t2_offs);
2332 static int brcmnand_suspend(struct device *dev)
2334 struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
2335 struct brcmnand_host *host;
2337 list_for_each_entry(host, &ctrl->host_list, node)
2338 brcmnand_save_restore_cs_config(host, 0);
2340 ctrl->nand_cs_nand_select = brcmnand_read_reg(ctrl, BRCMNAND_CS_SELECT);
2341 ctrl->nand_cs_nand_xor = brcmnand_read_reg(ctrl, BRCMNAND_CS_XOR);
2342 ctrl->corr_stat_threshold =
2343 brcmnand_read_reg(ctrl, BRCMNAND_CORR_THRESHOLD);
2345 if (has_flash_dma(ctrl))
2346 ctrl->flash_dma_mode = flash_dma_readl(ctrl, FLASH_DMA_MODE);
2351 static int brcmnand_resume(struct device *dev)
2353 struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
2354 struct brcmnand_host *host;
2356 if (has_flash_dma(ctrl)) {
2357 flash_dma_writel(ctrl, FLASH_DMA_MODE, ctrl->flash_dma_mode);
2358 flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
2361 brcmnand_write_reg(ctrl, BRCMNAND_CS_SELECT, ctrl->nand_cs_nand_select);
2362 brcmnand_write_reg(ctrl, BRCMNAND_CS_XOR, ctrl->nand_cs_nand_xor);
2363 brcmnand_write_reg(ctrl, BRCMNAND_CORR_THRESHOLD,
2364 ctrl->corr_stat_threshold);
2366 /* Clear/re-enable interrupt */
2367 ctrl->soc->ctlrdy_ack(ctrl->soc);
2368 ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
2371 list_for_each_entry(host, &ctrl->host_list, node) {
2372 struct nand_chip *chip = &host->chip;
2374 brcmnand_save_restore_cs_config(host, 1);
2376 /* Reset the chip, required by some chips after power-up */
2377 nand_reset_op(chip);
2383 const struct dev_pm_ops brcmnand_pm_ops = {
2384 .suspend = brcmnand_suspend,
2385 .resume = brcmnand_resume,
2387 EXPORT_SYMBOL_GPL(brcmnand_pm_ops);
2389 static const struct of_device_id brcmnand_of_match[] = {
2390 { .compatible = "brcm,brcmnand-v4.0" },
2391 { .compatible = "brcm,brcmnand-v5.0" },
2392 { .compatible = "brcm,brcmnand-v6.0" },
2393 { .compatible = "brcm,brcmnand-v6.1" },
2394 { .compatible = "brcm,brcmnand-v6.2" },
2395 { .compatible = "brcm,brcmnand-v7.0" },
2396 { .compatible = "brcm,brcmnand-v7.1" },
2397 { .compatible = "brcm,brcmnand-v7.2" },
2400 MODULE_DEVICE_TABLE(of, brcmnand_of_match);
2402 /***********************************************************************
2403 * Platform driver setup (per controller)
2404 ***********************************************************************/
2406 int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
2408 struct device *dev = &pdev->dev;
2409 struct device_node *dn = dev->of_node, *child;
2410 struct brcmnand_controller *ctrl;
2411 struct resource *res;
2414 /* We only support device-tree instantiation */
2418 if (!of_match_node(brcmnand_of_match, dn))
2421 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
2425 dev_set_drvdata(dev, ctrl);
2428 init_completion(&ctrl->done);
2429 init_completion(&ctrl->dma_done);
2430 nand_controller_init(&ctrl->controller);
2431 ctrl->controller.ops = &brcmnand_controller_ops;
2432 INIT_LIST_HEAD(&ctrl->host_list);
2434 /* NAND register range */
2435 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2436 ctrl->nand_base = devm_ioremap_resource(dev, res);
2437 if (IS_ERR(ctrl->nand_base))
2438 return PTR_ERR(ctrl->nand_base);
2440 /* Enable clock before using NAND registers */
2441 ctrl->clk = devm_clk_get(dev, "nand");
2442 if (!IS_ERR(ctrl->clk)) {
2443 ret = clk_prepare_enable(ctrl->clk);
2447 ret = PTR_ERR(ctrl->clk);
2448 if (ret == -EPROBE_DEFER)
2454 /* Initialize NAND revision */
2455 ret = brcmnand_revision_init(ctrl);
2460 * Most chips have this cache at a fixed offset within 'nand' block.
2461 * Some must specify this region separately.
2463 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand-cache");
2465 ctrl->nand_fc = devm_ioremap_resource(dev, res);
2466 if (IS_ERR(ctrl->nand_fc)) {
2467 ret = PTR_ERR(ctrl->nand_fc);
2471 ctrl->nand_fc = ctrl->nand_base +
2472 ctrl->reg_offsets[BRCMNAND_FC_BASE];
2476 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash-dma");
2478 ctrl->flash_dma_base = devm_ioremap_resource(dev, res);
2479 if (IS_ERR(ctrl->flash_dma_base)) {
2480 ret = PTR_ERR(ctrl->flash_dma_base);
2484 flash_dma_writel(ctrl, FLASH_DMA_MODE, 1); /* linked-list */
2485 flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
2487 /* Allocate descriptor(s) */
2488 ctrl->dma_desc = dmam_alloc_coherent(dev,
2489 sizeof(*ctrl->dma_desc),
2490 &ctrl->dma_pa, GFP_KERNEL);
2491 if (!ctrl->dma_desc) {
2496 ctrl->dma_irq = platform_get_irq(pdev, 1);
2497 if ((int)ctrl->dma_irq < 0) {
2498 dev_err(dev, "missing FLASH_DMA IRQ\n");
2503 ret = devm_request_irq(dev, ctrl->dma_irq,
2504 brcmnand_dma_irq, 0, DRV_NAME,
2507 dev_err(dev, "can't allocate IRQ %d: error %d\n",
2508 ctrl->dma_irq, ret);
2512 dev_info(dev, "enabling FLASH_DMA\n");
2515 /* Disable automatic device ID config, direct addressing */
2516 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT,
2517 CS_SELECT_AUTO_DEVICE_ID_CFG | 0xff, 0, 0);
2518 /* Disable XOR addressing */
2519 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
2521 if (ctrl->features & BRCMNAND_HAS_WP) {
2522 /* Permanently disable write protection */
2524 brcmnand_set_wp(ctrl, false);
2530 ctrl->irq = platform_get_irq(pdev, 0);
2531 if ((int)ctrl->irq < 0) {
2532 dev_err(dev, "no IRQ defined\n");
2538 * Some SoCs integrate this controller (e.g., its interrupt bits) in
2544 ret = devm_request_irq(dev, ctrl->irq, brcmnand_irq, 0,
2547 /* Enable interrupt */
2548 ctrl->soc->ctlrdy_ack(ctrl->soc);
2549 ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
2551 /* Use standard interrupt infrastructure */
2552 ret = devm_request_irq(dev, ctrl->irq, brcmnand_ctlrdy_irq, 0,
2556 dev_err(dev, "can't allocate IRQ %d: error %d\n",
2561 for_each_available_child_of_node(dn, child) {
2562 if (of_device_is_compatible(child, "brcm,nandcs")) {
2563 struct brcmnand_host *host;
2565 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2574 ret = brcmnand_init_cs(host, child);
2576 devm_kfree(dev, host);
2577 continue; /* Try all chip-selects */
2580 list_add_tail(&host->node, &ctrl->host_list);
2584 /* No chip-selects could initialize properly */
2585 if (list_empty(&ctrl->host_list)) {
2593 clk_disable_unprepare(ctrl->clk);
2597 EXPORT_SYMBOL_GPL(brcmnand_probe);
2599 int brcmnand_remove(struct platform_device *pdev)
2601 struct brcmnand_controller *ctrl = dev_get_drvdata(&pdev->dev);
2602 struct brcmnand_host *host;
2604 list_for_each_entry(host, &ctrl->host_list, node)
2605 nand_release(&host->chip);
2607 clk_disable_unprepare(ctrl->clk);
2609 dev_set_drvdata(&pdev->dev, NULL);
2613 EXPORT_SYMBOL_GPL(brcmnand_remove);
2615 MODULE_LICENSE("GPL v2");
2616 MODULE_AUTHOR("Kevin Cernekee");
2617 MODULE_AUTHOR("Brian Norris");
2618 MODULE_DESCRIPTION("NAND driver for Broadcom chips");
2619 MODULE_ALIAS("platform:brcmnand");