1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright © 2010-2015 Broadcom Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
20 #include <linux/ioport.h>
21 #include <linux/completion.h>
22 #include <linux/errno.h>
23 #include <linux/log2.h>
24 #include <asm/processor.h>
28 #include "brcmnand_compat.h"
31 * This flag controls if WP stays on between erase/write commands to mitigate
32 * flash corruption due to power glitches. Values:
33 * 0: NAND_WP is not used or not available
34 * 1: NAND_WP is set by default, cleared for erase/write operations
35 * 2: NAND_WP is always cleared
38 module_param(wp_on, int, 0444);
40 /***********************************************************************
42 ***********************************************************************/
44 #define DRV_NAME "brcmnand"
47 #define CMD_PAGE_READ 0x01
48 #define CMD_SPARE_AREA_READ 0x02
49 #define CMD_STATUS_READ 0x03
50 #define CMD_PROGRAM_PAGE 0x04
51 #define CMD_PROGRAM_SPARE_AREA 0x05
52 #define CMD_COPY_BACK 0x06
53 #define CMD_DEVICE_ID_READ 0x07
54 #define CMD_BLOCK_ERASE 0x08
55 #define CMD_FLASH_RESET 0x09
56 #define CMD_BLOCKS_LOCK 0x0a
57 #define CMD_BLOCKS_LOCK_DOWN 0x0b
58 #define CMD_BLOCKS_UNLOCK 0x0c
59 #define CMD_READ_BLOCKS_LOCK_STATUS 0x0d
60 #define CMD_PARAMETER_READ 0x0e
61 #define CMD_PARAMETER_CHANGE_COL 0x0f
62 #define CMD_LOW_LEVEL_OP 0x10
64 struct brcm_nand_dma_desc {
79 /* Bitfields for brcm_nand_dma_desc::status_valid */
80 #define FLASH_DMA_ECC_ERROR (1 << 8)
81 #define FLASH_DMA_CORR_ERROR (1 << 9)
83 /* 512B flash cache in the NAND controller HW */
86 #define FC_WORDS (FC_BYTES >> 2)
88 #define BRCMNAND_MIN_PAGESIZE 512
89 #define BRCMNAND_MIN_BLOCKSIZE (8 * 1024)
90 #define BRCMNAND_MIN_DEVSIZE (4ULL * 1024 * 1024)
92 #define NAND_CTRL_RDY (INTFC_CTLR_READY | INTFC_FLASH_READY)
93 #define NAND_POLL_STATUS_TIMEOUT_MS 100
95 /* Controller feature flags */
97 BRCMNAND_HAS_1K_SECTORS = BIT(0),
98 BRCMNAND_HAS_PREFETCH = BIT(1),
99 BRCMNAND_HAS_CACHE_MODE = BIT(2),
100 BRCMNAND_HAS_WP = BIT(3),
103 struct brcmnand_controller {
108 #endif /* __UBOOT__ */
109 struct nand_hw_control 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;
180 struct platform_device *pdev;
182 struct udevice *pdev;
183 #endif /* __UBOOT__ */
186 unsigned int last_cmd;
187 unsigned int last_byte;
189 struct brcmnand_cfg hwcfg;
190 struct brcmnand_controller *ctrl;
194 BRCMNAND_CMD_START = 0,
195 BRCMNAND_CMD_EXT_ADDRESS,
196 BRCMNAND_CMD_ADDRESS,
197 BRCMNAND_INTFC_STATUS,
202 BRCMNAND_CS1_BASE, /* CS1 regs, if non-contiguous */
203 BRCMNAND_CORR_THRESHOLD,
204 BRCMNAND_CORR_THRESHOLD_EXT,
205 BRCMNAND_UNCORR_COUNT,
207 BRCMNAND_CORR_EXT_ADDR,
209 BRCMNAND_UNCORR_EXT_ADDR,
210 BRCMNAND_UNCORR_ADDR,
215 BRCMNAND_OOB_READ_BASE,
216 BRCMNAND_OOB_READ_10_BASE, /* offset 0x10, if non-contiguous */
217 BRCMNAND_OOB_WRITE_BASE,
218 BRCMNAND_OOB_WRITE_10_BASE, /* offset 0x10, if non-contiguous */
223 static const u16 brcmnand_regs_v40[] = {
224 [BRCMNAND_CMD_START] = 0x04,
225 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
226 [BRCMNAND_CMD_ADDRESS] = 0x0c,
227 [BRCMNAND_INTFC_STATUS] = 0x6c,
228 [BRCMNAND_CS_SELECT] = 0x14,
229 [BRCMNAND_CS_XOR] = 0x18,
230 [BRCMNAND_LL_OP] = 0x178,
231 [BRCMNAND_CS0_BASE] = 0x40,
232 [BRCMNAND_CS1_BASE] = 0xd0,
233 [BRCMNAND_CORR_THRESHOLD] = 0x84,
234 [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
235 [BRCMNAND_UNCORR_COUNT] = 0,
236 [BRCMNAND_CORR_COUNT] = 0,
237 [BRCMNAND_CORR_EXT_ADDR] = 0x70,
238 [BRCMNAND_CORR_ADDR] = 0x74,
239 [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
240 [BRCMNAND_UNCORR_ADDR] = 0x7c,
241 [BRCMNAND_SEMAPHORE] = 0x58,
242 [BRCMNAND_ID] = 0x60,
243 [BRCMNAND_ID_EXT] = 0x64,
244 [BRCMNAND_LL_RDATA] = 0x17c,
245 [BRCMNAND_OOB_READ_BASE] = 0x20,
246 [BRCMNAND_OOB_READ_10_BASE] = 0x130,
247 [BRCMNAND_OOB_WRITE_BASE] = 0x30,
248 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
249 [BRCMNAND_FC_BASE] = 0x200,
253 static const u16 brcmnand_regs_v50[] = {
254 [BRCMNAND_CMD_START] = 0x04,
255 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
256 [BRCMNAND_CMD_ADDRESS] = 0x0c,
257 [BRCMNAND_INTFC_STATUS] = 0x6c,
258 [BRCMNAND_CS_SELECT] = 0x14,
259 [BRCMNAND_CS_XOR] = 0x18,
260 [BRCMNAND_LL_OP] = 0x178,
261 [BRCMNAND_CS0_BASE] = 0x40,
262 [BRCMNAND_CS1_BASE] = 0xd0,
263 [BRCMNAND_CORR_THRESHOLD] = 0x84,
264 [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
265 [BRCMNAND_UNCORR_COUNT] = 0,
266 [BRCMNAND_CORR_COUNT] = 0,
267 [BRCMNAND_CORR_EXT_ADDR] = 0x70,
268 [BRCMNAND_CORR_ADDR] = 0x74,
269 [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
270 [BRCMNAND_UNCORR_ADDR] = 0x7c,
271 [BRCMNAND_SEMAPHORE] = 0x58,
272 [BRCMNAND_ID] = 0x60,
273 [BRCMNAND_ID_EXT] = 0x64,
274 [BRCMNAND_LL_RDATA] = 0x17c,
275 [BRCMNAND_OOB_READ_BASE] = 0x20,
276 [BRCMNAND_OOB_READ_10_BASE] = 0x130,
277 [BRCMNAND_OOB_WRITE_BASE] = 0x30,
278 [BRCMNAND_OOB_WRITE_10_BASE] = 0x140,
279 [BRCMNAND_FC_BASE] = 0x200,
282 /* BRCMNAND v6.0 - v7.1 */
283 static const u16 brcmnand_regs_v60[] = {
284 [BRCMNAND_CMD_START] = 0x04,
285 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
286 [BRCMNAND_CMD_ADDRESS] = 0x0c,
287 [BRCMNAND_INTFC_STATUS] = 0x14,
288 [BRCMNAND_CS_SELECT] = 0x18,
289 [BRCMNAND_CS_XOR] = 0x1c,
290 [BRCMNAND_LL_OP] = 0x20,
291 [BRCMNAND_CS0_BASE] = 0x50,
292 [BRCMNAND_CS1_BASE] = 0,
293 [BRCMNAND_CORR_THRESHOLD] = 0xc0,
294 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xc4,
295 [BRCMNAND_UNCORR_COUNT] = 0xfc,
296 [BRCMNAND_CORR_COUNT] = 0x100,
297 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
298 [BRCMNAND_CORR_ADDR] = 0x110,
299 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
300 [BRCMNAND_UNCORR_ADDR] = 0x118,
301 [BRCMNAND_SEMAPHORE] = 0x150,
302 [BRCMNAND_ID] = 0x194,
303 [BRCMNAND_ID_EXT] = 0x198,
304 [BRCMNAND_LL_RDATA] = 0x19c,
305 [BRCMNAND_OOB_READ_BASE] = 0x200,
306 [BRCMNAND_OOB_READ_10_BASE] = 0,
307 [BRCMNAND_OOB_WRITE_BASE] = 0x280,
308 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
309 [BRCMNAND_FC_BASE] = 0x400,
313 static const u16 brcmnand_regs_v71[] = {
314 [BRCMNAND_CMD_START] = 0x04,
315 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
316 [BRCMNAND_CMD_ADDRESS] = 0x0c,
317 [BRCMNAND_INTFC_STATUS] = 0x14,
318 [BRCMNAND_CS_SELECT] = 0x18,
319 [BRCMNAND_CS_XOR] = 0x1c,
320 [BRCMNAND_LL_OP] = 0x20,
321 [BRCMNAND_CS0_BASE] = 0x50,
322 [BRCMNAND_CS1_BASE] = 0,
323 [BRCMNAND_CORR_THRESHOLD] = 0xdc,
324 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0,
325 [BRCMNAND_UNCORR_COUNT] = 0xfc,
326 [BRCMNAND_CORR_COUNT] = 0x100,
327 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
328 [BRCMNAND_CORR_ADDR] = 0x110,
329 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
330 [BRCMNAND_UNCORR_ADDR] = 0x118,
331 [BRCMNAND_SEMAPHORE] = 0x150,
332 [BRCMNAND_ID] = 0x194,
333 [BRCMNAND_ID_EXT] = 0x198,
334 [BRCMNAND_LL_RDATA] = 0x19c,
335 [BRCMNAND_OOB_READ_BASE] = 0x200,
336 [BRCMNAND_OOB_READ_10_BASE] = 0,
337 [BRCMNAND_OOB_WRITE_BASE] = 0x280,
338 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
339 [BRCMNAND_FC_BASE] = 0x400,
343 static const u16 brcmnand_regs_v72[] = {
344 [BRCMNAND_CMD_START] = 0x04,
345 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
346 [BRCMNAND_CMD_ADDRESS] = 0x0c,
347 [BRCMNAND_INTFC_STATUS] = 0x14,
348 [BRCMNAND_CS_SELECT] = 0x18,
349 [BRCMNAND_CS_XOR] = 0x1c,
350 [BRCMNAND_LL_OP] = 0x20,
351 [BRCMNAND_CS0_BASE] = 0x50,
352 [BRCMNAND_CS1_BASE] = 0,
353 [BRCMNAND_CORR_THRESHOLD] = 0xdc,
354 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0,
355 [BRCMNAND_UNCORR_COUNT] = 0xfc,
356 [BRCMNAND_CORR_COUNT] = 0x100,
357 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
358 [BRCMNAND_CORR_ADDR] = 0x110,
359 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
360 [BRCMNAND_UNCORR_ADDR] = 0x118,
361 [BRCMNAND_SEMAPHORE] = 0x150,
362 [BRCMNAND_ID] = 0x194,
363 [BRCMNAND_ID_EXT] = 0x198,
364 [BRCMNAND_LL_RDATA] = 0x19c,
365 [BRCMNAND_OOB_READ_BASE] = 0x200,
366 [BRCMNAND_OOB_READ_10_BASE] = 0,
367 [BRCMNAND_OOB_WRITE_BASE] = 0x400,
368 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
369 [BRCMNAND_FC_BASE] = 0x600,
372 enum brcmnand_cs_reg {
373 BRCMNAND_CS_CFG_EXT = 0,
375 BRCMNAND_CS_ACC_CONTROL,
380 /* Per chip-select offsets for v7.1 */
381 static const u8 brcmnand_cs_offsets_v71[] = {
382 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
383 [BRCMNAND_CS_CFG_EXT] = 0x04,
384 [BRCMNAND_CS_CFG] = 0x08,
385 [BRCMNAND_CS_TIMING1] = 0x0c,
386 [BRCMNAND_CS_TIMING2] = 0x10,
389 /* Per chip-select offsets for pre v7.1, except CS0 on <= v5.0 */
390 static const u8 brcmnand_cs_offsets[] = {
391 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
392 [BRCMNAND_CS_CFG_EXT] = 0x04,
393 [BRCMNAND_CS_CFG] = 0x04,
394 [BRCMNAND_CS_TIMING1] = 0x08,
395 [BRCMNAND_CS_TIMING2] = 0x0c,
398 /* Per chip-select offset for <= v5.0 on CS0 only */
399 static const u8 brcmnand_cs_offsets_cs0[] = {
400 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
401 [BRCMNAND_CS_CFG_EXT] = 0x08,
402 [BRCMNAND_CS_CFG] = 0x08,
403 [BRCMNAND_CS_TIMING1] = 0x10,
404 [BRCMNAND_CS_TIMING2] = 0x14,
408 * Bitfields for the CFG and CFG_EXT registers. Pre-v7.1 controllers only had
409 * one config register, but once the bitfields overflowed, newer controllers
410 * (v7.1 and newer) added a CFG_EXT register and shuffled a few fields around.
413 CFG_BLK_ADR_BYTES_SHIFT = 8,
414 CFG_COL_ADR_BYTES_SHIFT = 12,
415 CFG_FUL_ADR_BYTES_SHIFT = 16,
416 CFG_BUS_WIDTH_SHIFT = 23,
417 CFG_BUS_WIDTH = BIT(CFG_BUS_WIDTH_SHIFT),
418 CFG_DEVICE_SIZE_SHIFT = 24,
420 /* Only for pre-v7.1 (with no CFG_EXT register) */
421 CFG_PAGE_SIZE_SHIFT = 20,
422 CFG_BLK_SIZE_SHIFT = 28,
424 /* Only for v7.1+ (with CFG_EXT register) */
425 CFG_EXT_PAGE_SIZE_SHIFT = 0,
426 CFG_EXT_BLK_SIZE_SHIFT = 4,
429 /* BRCMNAND_INTFC_STATUS */
431 INTFC_FLASH_STATUS = GENMASK(7, 0),
433 INTFC_ERASED = BIT(27),
434 INTFC_OOB_VALID = BIT(28),
435 INTFC_CACHE_VALID = BIT(29),
436 INTFC_FLASH_READY = BIT(30),
437 INTFC_CTLR_READY = BIT(31),
440 static inline u32 nand_readreg(struct brcmnand_controller *ctrl, u32 offs)
442 return brcmnand_readl(ctrl->nand_base + offs);
445 static inline void nand_writereg(struct brcmnand_controller *ctrl, u32 offs,
448 brcmnand_writel(val, ctrl->nand_base + offs);
451 static int brcmnand_revision_init(struct brcmnand_controller *ctrl)
453 static const unsigned int block_sizes_v6[] = { 8, 16, 128, 256, 512, 1024, 2048, 0 };
454 static const unsigned int block_sizes_v4[] = { 16, 128, 8, 512, 256, 1024, 2048, 0 };
455 static const unsigned int page_sizes[] = { 512, 2048, 4096, 8192, 0 };
457 ctrl->nand_version = nand_readreg(ctrl, 0) & 0xffff;
459 /* Only support v4.0+? */
460 if (ctrl->nand_version < 0x0400) {
461 dev_err(ctrl->dev, "version %#x not supported\n",
466 /* Register offsets */
467 if (ctrl->nand_version >= 0x0702)
468 ctrl->reg_offsets = brcmnand_regs_v72;
469 else if (ctrl->nand_version >= 0x0701)
470 ctrl->reg_offsets = brcmnand_regs_v71;
471 else if (ctrl->nand_version >= 0x0600)
472 ctrl->reg_offsets = brcmnand_regs_v60;
473 else if (ctrl->nand_version >= 0x0500)
474 ctrl->reg_offsets = brcmnand_regs_v50;
475 else if (ctrl->nand_version >= 0x0400)
476 ctrl->reg_offsets = brcmnand_regs_v40;
478 /* Chip-select stride */
479 if (ctrl->nand_version >= 0x0701)
480 ctrl->reg_spacing = 0x14;
482 ctrl->reg_spacing = 0x10;
484 /* Per chip-select registers */
485 if (ctrl->nand_version >= 0x0701) {
486 ctrl->cs_offsets = brcmnand_cs_offsets_v71;
488 ctrl->cs_offsets = brcmnand_cs_offsets;
490 /* v5.0 and earlier has a different CS0 offset layout */
491 if (ctrl->nand_version <= 0x0500)
492 ctrl->cs0_offsets = brcmnand_cs_offsets_cs0;
495 /* Page / block sizes */
496 if (ctrl->nand_version >= 0x0701) {
497 /* >= v7.1 use nice power-of-2 values! */
498 ctrl->max_page_size = 16 * 1024;
499 ctrl->max_block_size = 2 * 1024 * 1024;
501 ctrl->page_sizes = page_sizes;
502 if (ctrl->nand_version >= 0x0600)
503 ctrl->block_sizes = block_sizes_v6;
505 ctrl->block_sizes = block_sizes_v4;
507 if (ctrl->nand_version < 0x0400) {
508 ctrl->max_page_size = 4096;
509 ctrl->max_block_size = 512 * 1024;
513 /* Maximum spare area sector size (per 512B) */
514 if (ctrl->nand_version >= 0x0702)
516 else if (ctrl->nand_version >= 0x0600)
518 else if (ctrl->nand_version >= 0x0500)
523 /* v6.0 and newer (except v6.1) have prefetch support */
524 if (ctrl->nand_version >= 0x0600 && ctrl->nand_version != 0x0601)
525 ctrl->features |= BRCMNAND_HAS_PREFETCH;
528 * v6.x has cache mode, but it's implemented differently. Ignore it for
531 if (ctrl->nand_version >= 0x0700)
532 ctrl->features |= BRCMNAND_HAS_CACHE_MODE;
534 if (ctrl->nand_version >= 0x0500)
535 ctrl->features |= BRCMNAND_HAS_1K_SECTORS;
537 if (ctrl->nand_version >= 0x0700)
538 ctrl->features |= BRCMNAND_HAS_WP;
540 else if (of_property_read_bool(ctrl->dev->of_node, "brcm,nand-has-wp"))
542 else if (dev_read_bool(ctrl->dev, "brcm,nand-has-wp"))
543 #endif /* __UBOOT__ */
544 ctrl->features |= BRCMNAND_HAS_WP;
549 static inline u32 brcmnand_read_reg(struct brcmnand_controller *ctrl,
550 enum brcmnand_reg reg)
552 u16 offs = ctrl->reg_offsets[reg];
555 return nand_readreg(ctrl, offs);
560 static inline void brcmnand_write_reg(struct brcmnand_controller *ctrl,
561 enum brcmnand_reg reg, u32 val)
563 u16 offs = ctrl->reg_offsets[reg];
566 nand_writereg(ctrl, offs, val);
569 static inline void brcmnand_rmw_reg(struct brcmnand_controller *ctrl,
570 enum brcmnand_reg reg, u32 mask, unsigned
573 u32 tmp = brcmnand_read_reg(ctrl, reg);
577 brcmnand_write_reg(ctrl, reg, tmp);
580 static inline u32 brcmnand_read_fc(struct brcmnand_controller *ctrl, int word)
582 return __raw_readl(ctrl->nand_fc + word * 4);
585 static inline void brcmnand_write_fc(struct brcmnand_controller *ctrl,
588 __raw_writel(val, ctrl->nand_fc + word * 4);
591 static inline u16 brcmnand_cs_offset(struct brcmnand_controller *ctrl, int cs,
592 enum brcmnand_cs_reg reg)
594 u16 offs_cs0 = ctrl->reg_offsets[BRCMNAND_CS0_BASE];
595 u16 offs_cs1 = ctrl->reg_offsets[BRCMNAND_CS1_BASE];
598 if (cs == 0 && ctrl->cs0_offsets)
599 cs_offs = ctrl->cs0_offsets[reg];
601 cs_offs = ctrl->cs_offsets[reg];
604 return offs_cs1 + (cs - 1) * ctrl->reg_spacing + cs_offs;
606 return offs_cs0 + cs * ctrl->reg_spacing + cs_offs;
609 static inline u32 brcmnand_count_corrected(struct brcmnand_controller *ctrl)
611 if (ctrl->nand_version < 0x0600)
613 return brcmnand_read_reg(ctrl, BRCMNAND_CORR_COUNT);
616 static void brcmnand_wr_corr_thresh(struct brcmnand_host *host, u8 val)
618 struct brcmnand_controller *ctrl = host->ctrl;
619 unsigned int shift = 0, bits;
620 enum brcmnand_reg reg = BRCMNAND_CORR_THRESHOLD;
623 if (ctrl->nand_version >= 0x0702)
625 else if (ctrl->nand_version >= 0x0600)
627 else if (ctrl->nand_version >= 0x0500)
632 if (ctrl->nand_version >= 0x0702) {
634 reg = BRCMNAND_CORR_THRESHOLD_EXT;
635 shift = (cs % 4) * bits;
636 } else if (ctrl->nand_version >= 0x0600) {
638 reg = BRCMNAND_CORR_THRESHOLD_EXT;
639 shift = (cs % 5) * bits;
641 brcmnand_rmw_reg(ctrl, reg, (bits - 1) << shift, shift, val);
644 static inline int brcmnand_cmd_shift(struct brcmnand_controller *ctrl)
646 if (ctrl->nand_version < 0x0602)
651 /***********************************************************************
652 * NAND ACC CONTROL bitfield
654 * Some bits have remained constant throughout hardware revision, while
655 * others have shifted around.
656 ***********************************************************************/
658 /* Constant for all versions (where supported) */
660 /* See BRCMNAND_HAS_CACHE_MODE */
661 ACC_CONTROL_CACHE_MODE = BIT(22),
663 /* See BRCMNAND_HAS_PREFETCH */
664 ACC_CONTROL_PREFETCH = BIT(23),
666 ACC_CONTROL_PAGE_HIT = BIT(24),
667 ACC_CONTROL_WR_PREEMPT = BIT(25),
668 ACC_CONTROL_PARTIAL_PAGE = BIT(26),
669 ACC_CONTROL_RD_ERASED = BIT(27),
670 ACC_CONTROL_FAST_PGM_RDIN = BIT(28),
671 ACC_CONTROL_WR_ECC = BIT(30),
672 ACC_CONTROL_RD_ECC = BIT(31),
675 static inline u32 brcmnand_spare_area_mask(struct brcmnand_controller *ctrl)
677 if (ctrl->nand_version >= 0x0702)
678 return GENMASK(7, 0);
679 else if (ctrl->nand_version >= 0x0600)
680 return GENMASK(6, 0);
682 return GENMASK(5, 0);
685 #define NAND_ACC_CONTROL_ECC_SHIFT 16
686 #define NAND_ACC_CONTROL_ECC_EXT_SHIFT 13
688 static inline u32 brcmnand_ecc_level_mask(struct brcmnand_controller *ctrl)
690 u32 mask = (ctrl->nand_version >= 0x0600) ? 0x1f : 0x0f;
692 mask <<= NAND_ACC_CONTROL_ECC_SHIFT;
694 /* v7.2 includes additional ECC levels */
695 if (ctrl->nand_version >= 0x0702)
696 mask |= 0x7 << NAND_ACC_CONTROL_ECC_EXT_SHIFT;
701 static void brcmnand_set_ecc_enabled(struct brcmnand_host *host, int en)
703 struct brcmnand_controller *ctrl = host->ctrl;
704 u16 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
705 u32 acc_control = nand_readreg(ctrl, offs);
706 u32 ecc_flags = ACC_CONTROL_WR_ECC | ACC_CONTROL_RD_ECC;
709 acc_control |= ecc_flags; /* enable RD/WR ECC */
710 acc_control |= host->hwcfg.ecc_level
711 << NAND_ACC_CONTROL_ECC_SHIFT;
713 acc_control &= ~ecc_flags; /* disable RD/WR ECC */
714 acc_control &= ~brcmnand_ecc_level_mask(ctrl);
717 nand_writereg(ctrl, offs, acc_control);
720 static inline int brcmnand_sector_1k_shift(struct brcmnand_controller *ctrl)
722 if (ctrl->nand_version >= 0x0702)
724 else if (ctrl->nand_version >= 0x0600)
726 else if (ctrl->nand_version >= 0x0500)
732 static int brcmnand_get_sector_size_1k(struct brcmnand_host *host)
734 struct brcmnand_controller *ctrl = host->ctrl;
735 int shift = brcmnand_sector_1k_shift(ctrl);
736 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
737 BRCMNAND_CS_ACC_CONTROL);
742 return (nand_readreg(ctrl, acc_control_offs) >> shift) & 0x1;
745 static void brcmnand_set_sector_size_1k(struct brcmnand_host *host, int val)
747 struct brcmnand_controller *ctrl = host->ctrl;
748 int shift = brcmnand_sector_1k_shift(ctrl);
749 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
750 BRCMNAND_CS_ACC_CONTROL);
756 tmp = nand_readreg(ctrl, acc_control_offs);
757 tmp &= ~(1 << shift);
758 tmp |= (!!val) << shift;
759 nand_writereg(ctrl, acc_control_offs, tmp);
762 /***********************************************************************
764 ***********************************************************************/
767 CS_SELECT_NAND_WP = BIT(29),
768 CS_SELECT_AUTO_DEVICE_ID_CFG = BIT(30),
771 static int bcmnand_ctrl_poll_status(struct brcmnand_controller *ctrl,
772 u32 mask, u32 expected_val,
773 unsigned long timeout_ms)
780 timeout_ms = NAND_POLL_STATUS_TIMEOUT_MS;
782 limit = jiffies + msecs_to_jiffies(timeout_ms);
784 val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS);
785 if ((val & mask) == expected_val)
789 } while (time_after(limit, jiffies));
791 unsigned long base, limit;
795 timeout_ms = NAND_POLL_STATUS_TIMEOUT_MS;
798 limit = CONFIG_SYS_HZ * timeout_ms / 1000;
800 val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS);
801 if ((val & mask) == expected_val)
805 } while (get_timer(base) < limit);
806 #endif /* __UBOOT__ */
808 dev_warn(ctrl->dev, "timeout on status poll (expected %x got %x)\n",
809 expected_val, val & mask);
814 static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
816 u32 val = en ? CS_SELECT_NAND_WP : 0;
818 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
821 /***********************************************************************
823 ***********************************************************************/
826 FLASH_DMA_REVISION = 0x00,
827 FLASH_DMA_FIRST_DESC = 0x04,
828 FLASH_DMA_FIRST_DESC_EXT = 0x08,
829 FLASH_DMA_CTRL = 0x0c,
830 FLASH_DMA_MODE = 0x10,
831 FLASH_DMA_STATUS = 0x14,
832 FLASH_DMA_INTERRUPT_DESC = 0x18,
833 FLASH_DMA_INTERRUPT_DESC_EXT = 0x1c,
834 FLASH_DMA_ERROR_STATUS = 0x20,
835 FLASH_DMA_CURRENT_DESC = 0x24,
836 FLASH_DMA_CURRENT_DESC_EXT = 0x28,
839 static inline bool has_flash_dma(struct brcmnand_controller *ctrl)
841 return ctrl->flash_dma_base;
844 static inline bool flash_dma_buf_ok(const void *buf)
847 return buf && !is_vmalloc_addr(buf) &&
848 likely(IS_ALIGNED((uintptr_t)buf, 4));
850 return buf && likely(IS_ALIGNED((uintptr_t)buf, 4));
851 #endif /* __UBOOT__ */
854 static inline void flash_dma_writel(struct brcmnand_controller *ctrl, u8 offs,
857 brcmnand_writel(val, ctrl->flash_dma_base + offs);
860 static inline u32 flash_dma_readl(struct brcmnand_controller *ctrl, u8 offs)
862 return brcmnand_readl(ctrl->flash_dma_base + offs);
865 /* Low-level operation types: command, address, write, or read */
866 enum brcmnand_llop_type {
873 /***********************************************************************
874 * Internal support functions
875 ***********************************************************************/
877 static inline bool is_hamming_ecc(struct brcmnand_controller *ctrl,
878 struct brcmnand_cfg *cfg)
880 if (ctrl->nand_version <= 0x0701)
881 return cfg->sector_size_1k == 0 && cfg->spare_area_size == 16 &&
882 cfg->ecc_level == 15;
884 return cfg->sector_size_1k == 0 && ((cfg->spare_area_size == 16 &&
885 cfg->ecc_level == 15) ||
886 (cfg->spare_area_size == 28 && cfg->ecc_level == 16));
890 * Set mtd->ooblayout to the appropriate mtd_ooblayout_ops given
891 * the layout/configuration.
892 * Returns -ERRCODE on failure.
894 static int brcmnand_hamming_ooblayout_ecc(struct mtd_info *mtd, int section,
895 struct mtd_oob_region *oobregion)
897 struct nand_chip *chip = mtd_to_nand(mtd);
898 struct brcmnand_host *host = nand_get_controller_data(chip);
899 struct brcmnand_cfg *cfg = &host->hwcfg;
900 int sas = cfg->spare_area_size << cfg->sector_size_1k;
901 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
903 if (section >= sectors)
906 oobregion->offset = (section * sas) + 6;
907 oobregion->length = 3;
912 static int brcmnand_hamming_ooblayout_free(struct mtd_info *mtd, int section,
913 struct mtd_oob_region *oobregion)
915 struct nand_chip *chip = mtd_to_nand(mtd);
916 struct brcmnand_host *host = nand_get_controller_data(chip);
917 struct brcmnand_cfg *cfg = &host->hwcfg;
918 int sas = cfg->spare_area_size << cfg->sector_size_1k;
919 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
921 if (section >= sectors * 2)
924 oobregion->offset = (section / 2) * sas;
927 oobregion->offset += 9;
928 oobregion->length = 7;
930 oobregion->length = 6;
932 /* First sector of each page may have BBI */
935 * Small-page NAND use byte 6 for BBI while large-page
938 if (cfg->page_size > 512)
947 static const struct mtd_ooblayout_ops brcmnand_hamming_ooblayout_ops = {
948 .ecc = brcmnand_hamming_ooblayout_ecc,
949 .free = brcmnand_hamming_ooblayout_free,
952 static int brcmnand_bch_ooblayout_ecc(struct mtd_info *mtd, int section,
953 struct mtd_oob_region *oobregion)
955 struct nand_chip *chip = mtd_to_nand(mtd);
956 struct brcmnand_host *host = nand_get_controller_data(chip);
957 struct brcmnand_cfg *cfg = &host->hwcfg;
958 int sas = cfg->spare_area_size << cfg->sector_size_1k;
959 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
961 if (section >= sectors)
964 oobregion->offset = (section * (sas + 1)) - chip->ecc.bytes;
965 oobregion->length = chip->ecc.bytes;
970 static int brcmnand_bch_ooblayout_free_lp(struct mtd_info *mtd, int section,
971 struct mtd_oob_region *oobregion)
973 struct nand_chip *chip = mtd_to_nand(mtd);
974 struct brcmnand_host *host = nand_get_controller_data(chip);
975 struct brcmnand_cfg *cfg = &host->hwcfg;
976 int sas = cfg->spare_area_size << cfg->sector_size_1k;
977 int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
979 if (section >= sectors)
982 if (sas <= chip->ecc.bytes)
985 oobregion->offset = section * sas;
986 oobregion->length = sas - chip->ecc.bytes;
996 static int brcmnand_bch_ooblayout_free_sp(struct mtd_info *mtd, int section,
997 struct mtd_oob_region *oobregion)
999 struct nand_chip *chip = mtd_to_nand(mtd);
1000 struct brcmnand_host *host = nand_get_controller_data(chip);
1001 struct brcmnand_cfg *cfg = &host->hwcfg;
1002 int sas = cfg->spare_area_size << cfg->sector_size_1k;
1004 if (section > 1 || sas - chip->ecc.bytes < 6 ||
1005 (section && sas - chip->ecc.bytes == 6))
1009 oobregion->offset = 0;
1010 oobregion->length = 5;
1012 oobregion->offset = 6;
1013 oobregion->length = sas - chip->ecc.bytes - 6;
1019 static const struct mtd_ooblayout_ops brcmnand_bch_lp_ooblayout_ops = {
1020 .ecc = brcmnand_bch_ooblayout_ecc,
1021 .free = brcmnand_bch_ooblayout_free_lp,
1024 static const struct mtd_ooblayout_ops brcmnand_bch_sp_ooblayout_ops = {
1025 .ecc = brcmnand_bch_ooblayout_ecc,
1026 .free = brcmnand_bch_ooblayout_free_sp,
1029 static int brcmstb_choose_ecc_layout(struct brcmnand_host *host)
1031 struct brcmnand_cfg *p = &host->hwcfg;
1032 struct mtd_info *mtd = nand_to_mtd(&host->chip);
1033 struct nand_ecc_ctrl *ecc = &host->chip.ecc;
1034 unsigned int ecc_level = p->ecc_level;
1035 int sas = p->spare_area_size << p->sector_size_1k;
1036 int sectors = p->page_size / (512 << p->sector_size_1k);
1038 if (p->sector_size_1k)
1041 if (is_hamming_ecc(host->ctrl, p)) {
1042 ecc->bytes = 3 * sectors;
1043 mtd_set_ooblayout(mtd, &brcmnand_hamming_ooblayout_ops);
1048 * CONTROLLER_VERSION:
1049 * < v5.0: ECC_REQ = ceil(BCH_T * 13/8)
1050 * >= v5.0: ECC_REQ = ceil(BCH_T * 14/8)
1051 * But we will just be conservative.
1053 ecc->bytes = DIV_ROUND_UP(ecc_level * 14, 8);
1054 if (p->page_size == 512)
1055 mtd_set_ooblayout(mtd, &brcmnand_bch_sp_ooblayout_ops);
1057 mtd_set_ooblayout(mtd, &brcmnand_bch_lp_ooblayout_ops);
1059 if (ecc->bytes >= sas) {
1060 dev_err(&host->pdev->dev,
1061 "error: ECC too large for OOB (ECC bytes %d, spare sector %d)\n",
1069 static void brcmnand_wp(struct mtd_info *mtd, int wp)
1071 struct nand_chip *chip = mtd_to_nand(mtd);
1072 struct brcmnand_host *host = nand_get_controller_data(chip);
1073 struct brcmnand_controller *ctrl = host->ctrl;
1075 if ((ctrl->features & BRCMNAND_HAS_WP) && wp_on == 1) {
1076 static int old_wp = -1;
1080 dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
1085 * make sure ctrl/flash ready before and after
1086 * changing state of #WP pin
1088 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY |
1091 NAND_STATUS_READY, 0);
1095 brcmnand_set_wp(ctrl, wp);
1096 nand_status_op(chip, NULL);
1097 /* NAND_STATUS_WP 0x00 = protected, 0x80 = not protected */
1098 ret = bcmnand_ctrl_poll_status(ctrl,
1104 (wp ? 0 : NAND_STATUS_WP), 0);
1107 dev_err_ratelimited(&host->pdev->dev,
1108 "nand #WP expected %s\n",
1112 dev_err(&host->pdev->dev,
1113 "nand #WP expected %s\n",
1115 #endif /* __UBOOT__ */
1119 /* Helper functions for reading and writing OOB registers */
1120 static inline u8 oob_reg_read(struct brcmnand_controller *ctrl, u32 offs)
1122 u16 offset0, offset10, reg_offs;
1124 offset0 = ctrl->reg_offsets[BRCMNAND_OOB_READ_BASE];
1125 offset10 = ctrl->reg_offsets[BRCMNAND_OOB_READ_10_BASE];
1127 if (offs >= ctrl->max_oob)
1130 if (offs >= 16 && offset10)
1131 reg_offs = offset10 + ((offs - 0x10) & ~0x03);
1133 reg_offs = offset0 + (offs & ~0x03);
1135 return nand_readreg(ctrl, reg_offs) >> (24 - ((offs & 0x03) << 3));
1138 static inline void oob_reg_write(struct brcmnand_controller *ctrl, u32 offs,
1141 u16 offset0, offset10, reg_offs;
1143 offset0 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_BASE];
1144 offset10 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_10_BASE];
1146 if (offs >= ctrl->max_oob)
1149 if (offs >= 16 && offset10)
1150 reg_offs = offset10 + ((offs - 0x10) & ~0x03);
1152 reg_offs = offset0 + (offs & ~0x03);
1154 nand_writereg(ctrl, reg_offs, data);
1158 * read_oob_from_regs - read data from OOB registers
1159 * @ctrl: NAND controller
1160 * @i: sub-page sector index
1161 * @oob: buffer to read to
1162 * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
1163 * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
1165 static int read_oob_from_regs(struct brcmnand_controller *ctrl, int i, u8 *oob,
1166 int sas, int sector_1k)
1168 int tbytes = sas << sector_1k;
1171 /* Adjust OOB values for 1K sector size */
1172 if (sector_1k && (i & 0x01))
1173 tbytes = max(0, tbytes - (int)ctrl->max_oob);
1174 tbytes = min_t(int, tbytes, ctrl->max_oob);
1176 for (j = 0; j < tbytes; j++)
1177 oob[j] = oob_reg_read(ctrl, j);
1182 * write_oob_to_regs - write data to OOB registers
1183 * @i: sub-page sector index
1184 * @oob: buffer to write from
1185 * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
1186 * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
1188 static int write_oob_to_regs(struct brcmnand_controller *ctrl, int i,
1189 const u8 *oob, int sas, int sector_1k)
1191 int tbytes = sas << sector_1k;
1194 /* Adjust OOB values for 1K sector size */
1195 if (sector_1k && (i & 0x01))
1196 tbytes = max(0, tbytes - (int)ctrl->max_oob);
1197 tbytes = min_t(int, tbytes, ctrl->max_oob);
1199 for (j = 0; j < tbytes; j += 4)
1200 oob_reg_write(ctrl, j,
1201 (oob[j + 0] << 24) |
1202 (oob[j + 1] << 16) |
1209 static irqreturn_t brcmnand_ctlrdy_irq(int irq, void *data)
1211 struct brcmnand_controller *ctrl = data;
1213 /* Discard all NAND_CTLRDY interrupts during DMA */
1214 if (ctrl->dma_pending)
1217 complete(&ctrl->done);
1221 /* Handle SoC-specific interrupt hardware */
1222 static irqreturn_t brcmnand_irq(int irq, void *data)
1224 struct brcmnand_controller *ctrl = data;
1226 if (ctrl->soc->ctlrdy_ack(ctrl->soc))
1227 return brcmnand_ctlrdy_irq(irq, data);
1232 static irqreturn_t brcmnand_dma_irq(int irq, void *data)
1234 struct brcmnand_controller *ctrl = data;
1236 complete(&ctrl->dma_done);
1240 #endif /* __UBOOT__ */
1242 static void brcmnand_send_cmd(struct brcmnand_host *host, int cmd)
1244 struct brcmnand_controller *ctrl = host->ctrl;
1247 dev_dbg(ctrl->dev, "send native cmd %d addr_lo 0x%x\n", cmd,
1248 brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS));
1249 BUG_ON(ctrl->cmd_pending != 0);
1250 ctrl->cmd_pending = cmd;
1252 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, 0);
1255 mb(); /* flush previous writes */
1256 brcmnand_write_reg(ctrl, BRCMNAND_CMD_START,
1257 cmd << brcmnand_cmd_shift(ctrl));
1260 /***********************************************************************
1261 * NAND MTD API: read/program/erase
1262 ***********************************************************************/
1264 static void brcmnand_cmd_ctrl(struct mtd_info *mtd, int dat,
1267 /* intentionally left blank */
1270 static int brcmnand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1272 struct nand_chip *chip = mtd_to_nand(mtd);
1273 struct brcmnand_host *host = nand_get_controller_data(chip);
1274 struct brcmnand_controller *ctrl = host->ctrl;
1277 unsigned long timeo = msecs_to_jiffies(100);
1279 dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending);
1280 if (ctrl->cmd_pending &&
1281 wait_for_completion_timeout(&ctrl->done, timeo) <= 0) {
1282 u32 cmd = brcmnand_read_reg(ctrl, BRCMNAND_CMD_START)
1283 >> brcmnand_cmd_shift(ctrl);
1285 dev_err_ratelimited(ctrl->dev,
1286 "timeout waiting for command %#02x\n", cmd);
1287 dev_err_ratelimited(ctrl->dev, "intfc status %08x\n",
1288 brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS));
1291 unsigned long timeo = 100; /* 100 msec */
1294 dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending);
1296 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, timeo);
1298 #endif /* __UBOOT__ */
1300 ctrl->cmd_pending = 0;
1301 return brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
1310 LLOP_RETURN_IDLE = BIT(31),
1312 LLOP_DATA_MASK = GENMASK(15, 0),
1315 static int brcmnand_low_level_op(struct brcmnand_host *host,
1316 enum brcmnand_llop_type type, u32 data,
1319 struct mtd_info *mtd = nand_to_mtd(&host->chip);
1320 struct nand_chip *chip = &host->chip;
1321 struct brcmnand_controller *ctrl = host->ctrl;
1324 tmp = data & LLOP_DATA_MASK;
1327 tmp |= LLOP_WE | LLOP_CLE;
1331 tmp |= LLOP_WE | LLOP_ALE;
1344 tmp |= LLOP_RETURN_IDLE;
1346 dev_dbg(ctrl->dev, "ll_op cmd %#x\n", tmp);
1348 brcmnand_write_reg(ctrl, BRCMNAND_LL_OP, tmp);
1349 (void)brcmnand_read_reg(ctrl, BRCMNAND_LL_OP);
1351 brcmnand_send_cmd(host, CMD_LOW_LEVEL_OP);
1352 return brcmnand_waitfunc(mtd, chip);
1355 static void brcmnand_cmdfunc(struct mtd_info *mtd, unsigned command,
1356 int column, int page_addr)
1358 struct nand_chip *chip = mtd_to_nand(mtd);
1359 struct brcmnand_host *host = nand_get_controller_data(chip);
1360 struct brcmnand_controller *ctrl = host->ctrl;
1361 u64 addr = (u64)page_addr << chip->page_shift;
1364 if (command == NAND_CMD_READID || command == NAND_CMD_PARAM ||
1365 command == NAND_CMD_RNDOUT)
1367 /* Avoid propagating a negative, don't-care address */
1368 else if (page_addr < 0)
1371 dev_dbg(ctrl->dev, "cmd 0x%x addr 0x%llx\n", command,
1372 (unsigned long long)addr);
1374 host->last_cmd = command;
1375 host->last_byte = 0;
1376 host->last_addr = addr;
1379 case NAND_CMD_RESET:
1380 native_cmd = CMD_FLASH_RESET;
1382 case NAND_CMD_STATUS:
1383 native_cmd = CMD_STATUS_READ;
1385 case NAND_CMD_READID:
1386 native_cmd = CMD_DEVICE_ID_READ;
1388 case NAND_CMD_READOOB:
1389 native_cmd = CMD_SPARE_AREA_READ;
1391 case NAND_CMD_ERASE1:
1392 native_cmd = CMD_BLOCK_ERASE;
1393 brcmnand_wp(mtd, 0);
1395 case NAND_CMD_PARAM:
1396 native_cmd = CMD_PARAMETER_READ;
1398 case NAND_CMD_SET_FEATURES:
1399 case NAND_CMD_GET_FEATURES:
1400 brcmnand_low_level_op(host, LL_OP_CMD, command, false);
1401 brcmnand_low_level_op(host, LL_OP_ADDR, column, false);
1403 case NAND_CMD_RNDOUT:
1404 native_cmd = CMD_PARAMETER_CHANGE_COL;
1405 addr &= ~((u64)(FC_BYTES - 1));
1407 * HW quirk: PARAMETER_CHANGE_COL requires SECTOR_SIZE_1K=0
1408 * NB: hwcfg.sector_size_1k may not be initialized yet
1410 if (brcmnand_get_sector_size_1k(host)) {
1411 host->hwcfg.sector_size_1k =
1412 brcmnand_get_sector_size_1k(host);
1413 brcmnand_set_sector_size_1k(host, 0);
1421 brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
1422 (host->cs << 16) | ((addr >> 32) & 0xffff));
1423 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
1424 brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS, lower_32_bits(addr));
1425 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
1427 brcmnand_send_cmd(host, native_cmd);
1428 brcmnand_waitfunc(mtd, chip);
1430 if (native_cmd == CMD_PARAMETER_READ ||
1431 native_cmd == CMD_PARAMETER_CHANGE_COL) {
1432 /* Copy flash cache word-wise */
1433 u32 *flash_cache = (u32 *)ctrl->flash_cache;
1436 brcmnand_soc_data_bus_prepare(ctrl->soc, true);
1439 * Must cache the FLASH_CACHE now, since changes in
1440 * SECTOR_SIZE_1K may invalidate it
1442 for (i = 0; i < FC_WORDS; i++)
1444 * Flash cache is big endian for parameter pages, at
1447 flash_cache[i] = be32_to_cpu(brcmnand_read_fc(ctrl, i));
1449 brcmnand_soc_data_bus_unprepare(ctrl->soc, true);
1451 /* Cleanup from HW quirk: restore SECTOR_SIZE_1K */
1452 if (host->hwcfg.sector_size_1k)
1453 brcmnand_set_sector_size_1k(host,
1454 host->hwcfg.sector_size_1k);
1457 /* Re-enable protection is necessary only after erase */
1458 if (command == NAND_CMD_ERASE1)
1459 brcmnand_wp(mtd, 1);
1462 static uint8_t brcmnand_read_byte(struct mtd_info *mtd)
1464 struct nand_chip *chip = mtd_to_nand(mtd);
1465 struct brcmnand_host *host = nand_get_controller_data(chip);
1466 struct brcmnand_controller *ctrl = host->ctrl;
1470 switch (host->last_cmd) {
1471 case NAND_CMD_READID:
1472 if (host->last_byte < 4)
1473 ret = brcmnand_read_reg(ctrl, BRCMNAND_ID) >>
1474 (24 - (host->last_byte << 3));
1475 else if (host->last_byte < 8)
1476 ret = brcmnand_read_reg(ctrl, BRCMNAND_ID_EXT) >>
1477 (56 - (host->last_byte << 3));
1480 case NAND_CMD_READOOB:
1481 ret = oob_reg_read(ctrl, host->last_byte);
1484 case NAND_CMD_STATUS:
1485 ret = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
1487 if (wp_on) /* hide WP status */
1488 ret |= NAND_STATUS_WP;
1491 case NAND_CMD_PARAM:
1492 case NAND_CMD_RNDOUT:
1493 addr = host->last_addr + host->last_byte;
1494 offs = addr & (FC_BYTES - 1);
1496 /* At FC_BYTES boundary, switch to next column */
1497 if (host->last_byte > 0 && offs == 0)
1498 nand_change_read_column_op(chip, addr, NULL, 0, false);
1500 ret = ctrl->flash_cache[offs];
1502 case NAND_CMD_GET_FEATURES:
1503 if (host->last_byte >= ONFI_SUBFEATURE_PARAM_LEN) {
1506 bool last = host->last_byte ==
1507 ONFI_SUBFEATURE_PARAM_LEN - 1;
1508 brcmnand_low_level_op(host, LL_OP_RD, 0, last);
1509 ret = brcmnand_read_reg(ctrl, BRCMNAND_LL_RDATA) & 0xff;
1513 dev_dbg(ctrl->dev, "read byte = 0x%02x\n", ret);
1519 static void brcmnand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1523 for (i = 0; i < len; i++, buf++)
1524 *buf = brcmnand_read_byte(mtd);
1527 static void brcmnand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
1531 struct nand_chip *chip = mtd_to_nand(mtd);
1532 struct brcmnand_host *host = nand_get_controller_data(chip);
1534 switch (host->last_cmd) {
1535 case NAND_CMD_SET_FEATURES:
1536 for (i = 0; i < len; i++)
1537 brcmnand_low_level_op(host, LL_OP_WR, buf[i],
1547 * Construct a FLASH_DMA descriptor as part of a linked list. You must know the
1548 * following ahead of time:
1549 * - Is this descriptor the beginning or end of a linked list?
1550 * - What is the (DMA) address of the next descriptor in the linked list?
1553 static int brcmnand_fill_dma_desc(struct brcmnand_host *host,
1554 struct brcm_nand_dma_desc *desc, u64 addr,
1555 dma_addr_t buf, u32 len, u8 dma_cmd,
1556 bool begin, bool end,
1557 dma_addr_t next_desc)
1559 memset(desc, 0, sizeof(*desc));
1560 /* Descriptors are written in native byte order (wordwise) */
1561 desc->next_desc = lower_32_bits(next_desc);
1562 desc->next_desc_ext = upper_32_bits(next_desc);
1563 desc->cmd_irq = (dma_cmd << 24) |
1564 (end ? (0x03 << 8) : 0) | /* IRQ | STOP */
1565 (!!begin) | ((!!end) << 1); /* head, tail */
1566 #ifdef CONFIG_CPU_BIG_ENDIAN
1567 desc->cmd_irq |= 0x01 << 12;
1569 desc->dram_addr = lower_32_bits(buf);
1570 desc->dram_addr_ext = upper_32_bits(buf);
1571 desc->tfr_len = len;
1572 desc->total_len = len;
1573 desc->flash_addr = lower_32_bits(addr);
1574 desc->flash_addr_ext = upper_32_bits(addr);
1575 desc->cs = host->cs;
1576 desc->status_valid = 0x01;
1581 * Kick the FLASH_DMA engine, with a given DMA descriptor
1583 static void brcmnand_dma_run(struct brcmnand_host *host, dma_addr_t desc)
1585 struct brcmnand_controller *ctrl = host->ctrl;
1586 unsigned long timeo = msecs_to_jiffies(100);
1588 flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC, lower_32_bits(desc));
1589 (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC);
1590 flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC_EXT, upper_32_bits(desc));
1591 (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC_EXT);
1593 /* Start FLASH_DMA engine */
1594 ctrl->dma_pending = true;
1595 mb(); /* flush previous writes */
1596 flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0x03); /* wake | run */
1598 if (wait_for_completion_timeout(&ctrl->dma_done, timeo) <= 0) {
1600 "timeout waiting for DMA; status %#x, error status %#x\n",
1601 flash_dma_readl(ctrl, FLASH_DMA_STATUS),
1602 flash_dma_readl(ctrl, FLASH_DMA_ERROR_STATUS));
1604 ctrl->dma_pending = false;
1605 flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0); /* force stop */
1608 static int brcmnand_dma_trans(struct brcmnand_host *host, u64 addr, u32 *buf,
1609 u32 len, u8 dma_cmd)
1611 struct brcmnand_controller *ctrl = host->ctrl;
1613 int dir = dma_cmd == CMD_PAGE_READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1615 buf_pa = dma_map_single(ctrl->dev, buf, len, dir);
1616 if (dma_mapping_error(ctrl->dev, buf_pa)) {
1617 dev_err(ctrl->dev, "unable to map buffer for DMA\n");
1621 brcmnand_fill_dma_desc(host, ctrl->dma_desc, addr, buf_pa, len,
1622 dma_cmd, true, true, 0);
1624 brcmnand_dma_run(host, ctrl->dma_pa);
1626 dma_unmap_single(ctrl->dev, buf_pa, len, dir);
1628 if (ctrl->dma_desc->status_valid & FLASH_DMA_ECC_ERROR)
1630 else if (ctrl->dma_desc->status_valid & FLASH_DMA_CORR_ERROR)
1635 #endif /* __UBOOT__ */
1638 * Assumes proper CS is already set
1640 static int brcmnand_read_by_pio(struct mtd_info *mtd, struct nand_chip *chip,
1641 u64 addr, unsigned int trans, u32 *buf,
1642 u8 *oob, u64 *err_addr)
1644 struct brcmnand_host *host = nand_get_controller_data(chip);
1645 struct brcmnand_controller *ctrl = host->ctrl;
1648 /* Clear error addresses */
1649 brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_ADDR, 0);
1650 brcmnand_write_reg(ctrl, BRCMNAND_CORR_ADDR, 0);
1651 brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_EXT_ADDR, 0);
1652 brcmnand_write_reg(ctrl, BRCMNAND_CORR_EXT_ADDR, 0);
1654 brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
1655 (host->cs << 16) | ((addr >> 32) & 0xffff));
1656 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
1658 for (i = 0; i < trans; i++, addr += FC_BYTES) {
1659 brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS,
1660 lower_32_bits(addr));
1661 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
1662 /* SPARE_AREA_READ does not use ECC, so just use PAGE_READ */
1663 brcmnand_send_cmd(host, CMD_PAGE_READ);
1664 brcmnand_waitfunc(mtd, chip);
1667 brcmnand_soc_data_bus_prepare(ctrl->soc, false);
1669 for (j = 0; j < FC_WORDS; j++, buf++)
1670 *buf = brcmnand_read_fc(ctrl, j);
1672 brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
1676 oob += read_oob_from_regs(ctrl, i, oob,
1677 mtd->oobsize / trans,
1678 host->hwcfg.sector_size_1k);
1681 *err_addr = brcmnand_read_reg(ctrl,
1682 BRCMNAND_UNCORR_ADDR) |
1683 ((u64)(brcmnand_read_reg(ctrl,
1684 BRCMNAND_UNCORR_EXT_ADDR)
1691 *err_addr = brcmnand_read_reg(ctrl,
1692 BRCMNAND_CORR_ADDR) |
1693 ((u64)(brcmnand_read_reg(ctrl,
1694 BRCMNAND_CORR_EXT_ADDR)
1705 * Check a page to see if it is erased (w/ bitflips) after an uncorrectable ECC
1708 * Because the HW ECC signals an ECC error if an erase paged has even a single
1709 * bitflip, we must check each ECC error to see if it is actually an erased
1710 * page with bitflips, not a truly corrupted page.
1712 * On a real error, return a negative error code (-EBADMSG for ECC error), and
1713 * buf will contain raw data.
1714 * Otherwise, buf gets filled with 0xffs and return the maximum number of
1715 * bitflips-per-ECC-sector to the caller.
1718 static int brcmstb_nand_verify_erased_page(struct mtd_info *mtd,
1719 struct nand_chip *chip, void *buf, u64 addr)
1722 void *oob = chip->oob_poi;
1724 int page = addr >> chip->page_shift;
1729 buf = chip->data_buf;
1731 buf = chip->buffers->databuf;
1733 /* Invalidate page cache */
1737 sas = mtd->oobsize / chip->ecc.steps;
1739 /* read without ecc for verification */
1740 ret = chip->ecc.read_page_raw(mtd, chip, buf, true, page);
1744 for (i = 0; i < chip->ecc.steps; i++, oob += sas) {
1745 ret = nand_check_erased_ecc_chunk(buf, chip->ecc.size,
1747 chip->ecc.strength);
1751 bitflips = max(bitflips, ret);
1757 static int brcmnand_read(struct mtd_info *mtd, struct nand_chip *chip,
1758 u64 addr, unsigned int trans, u32 *buf, u8 *oob)
1760 struct brcmnand_host *host = nand_get_controller_data(chip);
1761 struct brcmnand_controller *ctrl = host->ctrl;
1766 dev_dbg(ctrl->dev, "read %llx -> %p\n", (unsigned long long)addr, buf);
1769 brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_COUNT, 0);
1772 if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
1773 err = brcmnand_dma_trans(host, addr, buf, trans * FC_BYTES,
1776 if (mtd_is_bitflip_or_eccerr(err))
1783 memset(oob, 0x99, mtd->oobsize);
1785 err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf,
1790 memset(oob, 0x99, mtd->oobsize);
1792 err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf,
1794 #endif /* __UBOOT__ */
1796 if (mtd_is_eccerr(err)) {
1798 * On controller version and 7.0, 7.1 , DMA read after a
1799 * prior PIO read that reported uncorrectable error,
1800 * the DMA engine captures this error following DMA read
1801 * cleared only on subsequent DMA read, so just retry once
1802 * to clear a possible false error reported for current DMA
1805 if ((ctrl->nand_version == 0x0700) ||
1806 (ctrl->nand_version == 0x0701)) {
1814 * Controller version 7.2 has hw encoder to detect erased page
1815 * bitflips, apply sw verification for older controllers only
1817 if (ctrl->nand_version < 0x0702) {
1818 err = brcmstb_nand_verify_erased_page(mtd, chip, buf,
1820 /* erased page bitflips corrected */
1825 dev_dbg(ctrl->dev, "uncorrectable error at 0x%llx\n",
1826 (unsigned long long)err_addr);
1827 mtd->ecc_stats.failed++;
1828 /* NAND layer expects zero on ECC errors */
1832 if (mtd_is_bitflip(err)) {
1833 unsigned int corrected = brcmnand_count_corrected(ctrl);
1835 dev_dbg(ctrl->dev, "corrected error at 0x%llx\n",
1836 (unsigned long long)err_addr);
1837 mtd->ecc_stats.corrected += corrected;
1838 /* Always exceed the software-imposed threshold */
1839 return max(mtd->bitflip_threshold, corrected);
1845 static int brcmnand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1846 uint8_t *buf, int oob_required, int page)
1848 struct brcmnand_host *host = nand_get_controller_data(chip);
1849 u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
1851 nand_read_page_op(chip, page, 0, NULL, 0);
1853 return brcmnand_read(mtd, chip, host->last_addr,
1854 mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
1857 static int brcmnand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1858 uint8_t *buf, int oob_required, int page)
1860 struct brcmnand_host *host = nand_get_controller_data(chip);
1861 u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
1864 nand_read_page_op(chip, page, 0, NULL, 0);
1866 brcmnand_set_ecc_enabled(host, 0);
1867 ret = brcmnand_read(mtd, chip, host->last_addr,
1868 mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
1869 brcmnand_set_ecc_enabled(host, 1);
1873 static int brcmnand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1876 return brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
1877 mtd->writesize >> FC_SHIFT,
1878 NULL, (u8 *)chip->oob_poi);
1881 static int brcmnand_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1884 struct brcmnand_host *host = nand_get_controller_data(chip);
1886 brcmnand_set_ecc_enabled(host, 0);
1887 brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
1888 mtd->writesize >> FC_SHIFT,
1889 NULL, (u8 *)chip->oob_poi);
1890 brcmnand_set_ecc_enabled(host, 1);
1894 static int brcmnand_write(struct mtd_info *mtd, struct nand_chip *chip,
1895 u64 addr, const u32 *buf, u8 *oob)
1897 struct brcmnand_host *host = nand_get_controller_data(chip);
1898 struct brcmnand_controller *ctrl = host->ctrl;
1899 unsigned int i, j, trans = mtd->writesize >> FC_SHIFT;
1900 int status, ret = 0;
1902 dev_dbg(ctrl->dev, "write %llx <- %p\n", (unsigned long long)addr, buf);
1904 if (unlikely((unsigned long)buf & 0x03)) {
1905 dev_warn(ctrl->dev, "unaligned buffer: %p\n", buf);
1906 buf = (u32 *)((unsigned long)buf & ~0x03);
1909 brcmnand_wp(mtd, 0);
1911 for (i = 0; i < ctrl->max_oob; i += 4)
1912 oob_reg_write(ctrl, i, 0xffffffff);
1915 if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
1916 if (brcmnand_dma_trans(host, addr, (u32 *)buf,
1917 mtd->writesize, CMD_PROGRAM_PAGE))
1921 #endif /* __UBOOT__ */
1923 brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
1924 (host->cs << 16) | ((addr >> 32) & 0xffff));
1925 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
1927 for (i = 0; i < trans; i++, addr += FC_BYTES) {
1928 /* full address MUST be set before populating FC */
1929 brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS,
1930 lower_32_bits(addr));
1931 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
1934 brcmnand_soc_data_bus_prepare(ctrl->soc, false);
1936 for (j = 0; j < FC_WORDS; j++, buf++)
1937 brcmnand_write_fc(ctrl, j, *buf);
1939 brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
1941 for (j = 0; j < FC_WORDS; j++)
1942 brcmnand_write_fc(ctrl, j, 0xffffffff);
1946 oob += write_oob_to_regs(ctrl, i, oob,
1947 mtd->oobsize / trans,
1948 host->hwcfg.sector_size_1k);
1951 /* we cannot use SPARE_AREA_PROGRAM when PARTIAL_PAGE_EN=0 */
1952 brcmnand_send_cmd(host, CMD_PROGRAM_PAGE);
1953 status = brcmnand_waitfunc(mtd, chip);
1955 if (status & NAND_STATUS_FAIL) {
1956 dev_info(ctrl->dev, "program failed at %llx\n",
1957 (unsigned long long)addr);
1963 brcmnand_wp(mtd, 1);
1967 static int brcmnand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1968 const uint8_t *buf, int oob_required, int page)
1970 struct brcmnand_host *host = nand_get_controller_data(chip);
1971 void *oob = oob_required ? chip->oob_poi : NULL;
1973 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1974 brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
1976 return nand_prog_page_end_op(chip);
1979 static int brcmnand_write_page_raw(struct mtd_info *mtd,
1980 struct nand_chip *chip, const uint8_t *buf,
1981 int oob_required, int page)
1983 struct brcmnand_host *host = nand_get_controller_data(chip);
1984 void *oob = oob_required ? chip->oob_poi : NULL;
1986 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1987 brcmnand_set_ecc_enabled(host, 0);
1988 brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
1989 brcmnand_set_ecc_enabled(host, 1);
1991 return nand_prog_page_end_op(chip);
1994 static int brcmnand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1997 return brcmnand_write(mtd, chip, (u64)page << chip->page_shift,
1998 NULL, chip->oob_poi);
2001 static int brcmnand_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
2004 struct brcmnand_host *host = nand_get_controller_data(chip);
2007 brcmnand_set_ecc_enabled(host, 0);
2008 ret = brcmnand_write(mtd, chip, (u64)page << chip->page_shift, NULL,
2009 (u8 *)chip->oob_poi);
2010 brcmnand_set_ecc_enabled(host, 1);
2015 /***********************************************************************
2016 * Per-CS setup (1 NAND device)
2017 ***********************************************************************/
2019 static int brcmnand_set_cfg(struct brcmnand_host *host,
2020 struct brcmnand_cfg *cfg)
2022 struct brcmnand_controller *ctrl = host->ctrl;
2023 struct nand_chip *chip = &host->chip;
2024 u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2025 u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
2026 BRCMNAND_CS_CFG_EXT);
2027 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
2028 BRCMNAND_CS_ACC_CONTROL);
2029 u8 block_size = 0, page_size = 0, device_size = 0;
2032 if (ctrl->block_sizes) {
2035 for (i = 0, found = 0; ctrl->block_sizes[i]; i++)
2036 if (ctrl->block_sizes[i] * 1024 == cfg->block_size) {
2041 dev_warn(ctrl->dev, "invalid block size %u\n",
2046 block_size = ffs(cfg->block_size) - ffs(BRCMNAND_MIN_BLOCKSIZE);
2049 if (cfg->block_size < BRCMNAND_MIN_BLOCKSIZE || (ctrl->max_block_size &&
2050 cfg->block_size > ctrl->max_block_size)) {
2051 dev_warn(ctrl->dev, "invalid block size %u\n",
2056 if (ctrl->page_sizes) {
2059 for (i = 0, found = 0; ctrl->page_sizes[i]; i++)
2060 if (ctrl->page_sizes[i] == cfg->page_size) {
2065 dev_warn(ctrl->dev, "invalid page size %u\n",
2070 page_size = ffs(cfg->page_size) - ffs(BRCMNAND_MIN_PAGESIZE);
2073 if (cfg->page_size < BRCMNAND_MIN_PAGESIZE || (ctrl->max_page_size &&
2074 cfg->page_size > ctrl->max_page_size)) {
2075 dev_warn(ctrl->dev, "invalid page size %u\n", cfg->page_size);
2079 if (fls64(cfg->device_size) < fls64(BRCMNAND_MIN_DEVSIZE)) {
2080 dev_warn(ctrl->dev, "invalid device size 0x%llx\n",
2081 (unsigned long long)cfg->device_size);
2084 device_size = fls64(cfg->device_size) - fls64(BRCMNAND_MIN_DEVSIZE);
2086 tmp = (cfg->blk_adr_bytes << CFG_BLK_ADR_BYTES_SHIFT) |
2087 (cfg->col_adr_bytes << CFG_COL_ADR_BYTES_SHIFT) |
2088 (cfg->ful_adr_bytes << CFG_FUL_ADR_BYTES_SHIFT) |
2089 (!!(cfg->device_width == 16) << CFG_BUS_WIDTH_SHIFT) |
2090 (device_size << CFG_DEVICE_SIZE_SHIFT);
2091 if (cfg_offs == cfg_ext_offs) {
2092 tmp |= (page_size << CFG_PAGE_SIZE_SHIFT) |
2093 (block_size << CFG_BLK_SIZE_SHIFT);
2094 nand_writereg(ctrl, cfg_offs, tmp);
2096 nand_writereg(ctrl, cfg_offs, tmp);
2097 tmp = (page_size << CFG_EXT_PAGE_SIZE_SHIFT) |
2098 (block_size << CFG_EXT_BLK_SIZE_SHIFT);
2099 nand_writereg(ctrl, cfg_ext_offs, tmp);
2102 tmp = nand_readreg(ctrl, acc_control_offs);
2103 tmp &= ~brcmnand_ecc_level_mask(ctrl);
2104 tmp |= cfg->ecc_level << NAND_ACC_CONTROL_ECC_SHIFT;
2105 tmp &= ~brcmnand_spare_area_mask(ctrl);
2106 tmp |= cfg->spare_area_size;
2107 nand_writereg(ctrl, acc_control_offs, tmp);
2109 brcmnand_set_sector_size_1k(host, cfg->sector_size_1k);
2111 /* threshold = ceil(BCH-level * 0.75) */
2112 brcmnand_wr_corr_thresh(host, DIV_ROUND_UP(chip->ecc.strength * 3, 4));
2117 static void brcmnand_print_cfg(struct brcmnand_host *host,
2118 char *buf, struct brcmnand_cfg *cfg)
2121 "%lluMiB total, %uKiB blocks, %u%s pages, %uB OOB, %u-bit",
2122 (unsigned long long)cfg->device_size >> 20,
2123 cfg->block_size >> 10,
2124 cfg->page_size >= 1024 ? cfg->page_size >> 10 : cfg->page_size,
2125 cfg->page_size >= 1024 ? "KiB" : "B",
2126 cfg->spare_area_size, cfg->device_width);
2128 /* Account for Hamming ECC and for BCH 512B vs 1KiB sectors */
2129 if (is_hamming_ecc(host->ctrl, cfg))
2130 sprintf(buf, ", Hamming ECC");
2131 else if (cfg->sector_size_1k)
2132 sprintf(buf, ", BCH-%u (1KiB sector)", cfg->ecc_level << 1);
2134 sprintf(buf, ", BCH-%u", cfg->ecc_level);
2138 * Minimum number of bytes to address a page. Calculated as:
2139 * roundup(log2(size / page-size) / 8)
2141 * NB: the following does not "round up" for non-power-of-2 'size'; but this is
2142 * OK because many other things will break if 'size' is irregular...
2144 static inline int get_blk_adr_bytes(u64 size, u32 writesize)
2146 return ALIGN(ilog2(size) - ilog2(writesize), 8) >> 3;
2149 static int brcmnand_setup_dev(struct brcmnand_host *host)
2151 struct mtd_info *mtd = nand_to_mtd(&host->chip);
2152 struct nand_chip *chip = &host->chip;
2153 struct brcmnand_controller *ctrl = host->ctrl;
2154 struct brcmnand_cfg *cfg = &host->hwcfg;
2156 u32 offs, tmp, oob_sector;
2159 memset(cfg, 0, sizeof(*cfg));
2162 ret = of_property_read_u32(nand_get_flash_node(chip),
2163 "brcm,nand-oob-sector-size",
2166 ret = ofnode_read_u32(nand_get_flash_node(chip),
2167 "brcm,nand-oob-sector-size",
2169 #endif /* __UBOOT__ */
2171 /* Use detected size */
2172 cfg->spare_area_size = mtd->oobsize /
2173 (mtd->writesize >> FC_SHIFT);
2175 cfg->spare_area_size = oob_sector;
2177 if (cfg->spare_area_size > ctrl->max_oob)
2178 cfg->spare_area_size = ctrl->max_oob;
2180 * Set oobsize to be consistent with controller's spare_area_size, as
2181 * the rest is inaccessible.
2183 mtd->oobsize = cfg->spare_area_size * (mtd->writesize >> FC_SHIFT);
2185 cfg->device_size = mtd->size;
2186 cfg->block_size = mtd->erasesize;
2187 cfg->page_size = mtd->writesize;
2188 cfg->device_width = (chip->options & NAND_BUSWIDTH_16) ? 16 : 8;
2189 cfg->col_adr_bytes = 2;
2190 cfg->blk_adr_bytes = get_blk_adr_bytes(mtd->size, mtd->writesize);
2192 if (chip->ecc.mode != NAND_ECC_HW) {
2193 dev_err(ctrl->dev, "only HW ECC supported; selected: %d\n",
2198 if (chip->ecc.algo == NAND_ECC_UNKNOWN) {
2199 if (chip->ecc.strength == 1 && chip->ecc.size == 512)
2200 /* Default to Hamming for 1-bit ECC, if unspecified */
2201 chip->ecc.algo = NAND_ECC_HAMMING;
2203 /* Otherwise, BCH */
2204 chip->ecc.algo = NAND_ECC_BCH;
2207 if (chip->ecc.algo == NAND_ECC_HAMMING && (chip->ecc.strength != 1 ||
2208 chip->ecc.size != 512)) {
2209 dev_err(ctrl->dev, "invalid Hamming params: %d bits per %d bytes\n",
2210 chip->ecc.strength, chip->ecc.size);
2214 switch (chip->ecc.size) {
2216 if (chip->ecc.algo == NAND_ECC_HAMMING)
2217 cfg->ecc_level = 15;
2219 cfg->ecc_level = chip->ecc.strength;
2220 cfg->sector_size_1k = 0;
2223 if (!(ctrl->features & BRCMNAND_HAS_1K_SECTORS)) {
2224 dev_err(ctrl->dev, "1KB sectors not supported\n");
2227 if (chip->ecc.strength & 0x1) {
2229 "odd ECC not supported with 1KB sectors\n");
2233 cfg->ecc_level = chip->ecc.strength >> 1;
2234 cfg->sector_size_1k = 1;
2237 dev_err(ctrl->dev, "unsupported ECC size: %d\n",
2242 cfg->ful_adr_bytes = cfg->blk_adr_bytes;
2243 if (mtd->writesize > 512)
2244 cfg->ful_adr_bytes += cfg->col_adr_bytes;
2246 cfg->ful_adr_bytes += 1;
2248 ret = brcmnand_set_cfg(host, cfg);
2252 brcmnand_set_ecc_enabled(host, 1);
2254 brcmnand_print_cfg(host, msg, cfg);
2255 dev_info(ctrl->dev, "detected %s\n", msg);
2257 /* Configure ACC_CONTROL */
2258 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
2259 tmp = nand_readreg(ctrl, offs);
2260 tmp &= ~ACC_CONTROL_PARTIAL_PAGE;
2261 tmp &= ~ACC_CONTROL_RD_ERASED;
2263 /* We need to turn on Read from erased paged protected by ECC */
2264 if (ctrl->nand_version >= 0x0702)
2265 tmp |= ACC_CONTROL_RD_ERASED;
2266 tmp &= ~ACC_CONTROL_FAST_PGM_RDIN;
2267 if (ctrl->features & BRCMNAND_HAS_PREFETCH)
2268 tmp &= ~ACC_CONTROL_PREFETCH;
2270 nand_writereg(ctrl, offs, tmp);
2276 static int brcmnand_init_cs(struct brcmnand_host *host, struct device_node *dn)
2278 static int brcmnand_init_cs(struct brcmnand_host *host, ofnode dn)
2281 struct brcmnand_controller *ctrl = host->ctrl;
2283 struct platform_device *pdev = host->pdev;
2285 struct udevice *pdev = host->pdev;
2286 #endif /* __UBOOT__ */
2287 struct mtd_info *mtd;
2288 struct nand_chip *chip;
2293 ret = of_property_read_u32(dn, "reg", &host->cs);
2295 ret = ofnode_read_s32(dn, "reg", &host->cs);
2298 dev_err(&pdev->dev, "can't get chip-select\n");
2302 mtd = nand_to_mtd(&host->chip);
2305 nand_set_flash_node(chip, dn);
2306 nand_set_controller_data(chip, host);
2308 mtd->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "brcmnand.%d",
2311 mtd->name = devm_kasprintf(pdev, GFP_KERNEL, "brcmnand.%d",
2313 #endif /* __UBOOT__ */
2317 mtd->owner = THIS_MODULE;
2319 mtd->dev.parent = &pdev->dev;
2321 mtd->dev->parent = pdev;
2322 #endif /* __UBOOT__ */
2324 chip->IO_ADDR_R = (void __iomem *)0xdeadbeef;
2325 chip->IO_ADDR_W = (void __iomem *)0xdeadbeef;
2327 chip->cmd_ctrl = brcmnand_cmd_ctrl;
2328 chip->cmdfunc = brcmnand_cmdfunc;
2329 chip->waitfunc = brcmnand_waitfunc;
2330 chip->read_byte = brcmnand_read_byte;
2331 chip->read_buf = brcmnand_read_buf;
2332 chip->write_buf = brcmnand_write_buf;
2334 chip->ecc.mode = NAND_ECC_HW;
2335 chip->ecc.read_page = brcmnand_read_page;
2336 chip->ecc.write_page = brcmnand_write_page;
2337 chip->ecc.read_page_raw = brcmnand_read_page_raw;
2338 chip->ecc.write_page_raw = brcmnand_write_page_raw;
2339 chip->ecc.write_oob_raw = brcmnand_write_oob_raw;
2340 chip->ecc.read_oob_raw = brcmnand_read_oob_raw;
2341 chip->ecc.read_oob = brcmnand_read_oob;
2342 chip->ecc.write_oob = brcmnand_write_oob;
2344 chip->controller = &ctrl->controller;
2347 * The bootloader might have configured 16bit mode but
2348 * NAND READID command only works in 8bit mode. We force
2349 * 8bit mode here to ensure that NAND READID commands works.
2351 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2352 nand_writereg(ctrl, cfg_offs,
2353 nand_readreg(ctrl, cfg_offs) & ~CFG_BUS_WIDTH);
2355 ret = nand_scan_ident(mtd, 1, NULL);
2359 chip->options |= NAND_NO_SUBPAGE_WRITE;
2361 * Avoid (for instance) kmap()'d buffers from JFFS2, which we can't DMA
2362 * to/from, and have nand_base pass us a bounce buffer instead, as
2365 chip->options |= NAND_USE_BOUNCE_BUFFER;
2367 if (chip->bbt_options & NAND_BBT_USE_FLASH)
2368 chip->bbt_options |= NAND_BBT_NO_OOB;
2370 if (brcmnand_setup_dev(host))
2373 chip->ecc.size = host->hwcfg.sector_size_1k ? 1024 : 512;
2374 /* only use our internal HW threshold */
2375 mtd->bitflip_threshold = 1;
2377 ret = brcmstb_choose_ecc_layout(host);
2381 ret = nand_scan_tail(mtd);
2386 ret = mtd_device_register(mtd, NULL, 0);
2390 ret = nand_register(0, mtd);
2391 #endif /* __UBOOT__ */
2397 static void brcmnand_save_restore_cs_config(struct brcmnand_host *host,
2400 struct brcmnand_controller *ctrl = host->ctrl;
2401 u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2402 u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
2403 BRCMNAND_CS_CFG_EXT);
2404 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
2405 BRCMNAND_CS_ACC_CONTROL);
2406 u16 t1_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING1);
2407 u16 t2_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING2);
2410 nand_writereg(ctrl, cfg_offs, host->hwcfg.config);
2411 if (cfg_offs != cfg_ext_offs)
2412 nand_writereg(ctrl, cfg_ext_offs,
2413 host->hwcfg.config_ext);
2414 nand_writereg(ctrl, acc_control_offs, host->hwcfg.acc_control);
2415 nand_writereg(ctrl, t1_offs, host->hwcfg.timing_1);
2416 nand_writereg(ctrl, t2_offs, host->hwcfg.timing_2);
2418 host->hwcfg.config = nand_readreg(ctrl, cfg_offs);
2419 if (cfg_offs != cfg_ext_offs)
2420 host->hwcfg.config_ext =
2421 nand_readreg(ctrl, cfg_ext_offs);
2422 host->hwcfg.acc_control = nand_readreg(ctrl, acc_control_offs);
2423 host->hwcfg.timing_1 = nand_readreg(ctrl, t1_offs);
2424 host->hwcfg.timing_2 = nand_readreg(ctrl, t2_offs);
2428 static int brcmnand_suspend(struct device *dev)
2430 struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
2431 struct brcmnand_host *host;
2433 list_for_each_entry(host, &ctrl->host_list, node)
2434 brcmnand_save_restore_cs_config(host, 0);
2436 ctrl->nand_cs_nand_select = brcmnand_read_reg(ctrl, BRCMNAND_CS_SELECT);
2437 ctrl->nand_cs_nand_xor = brcmnand_read_reg(ctrl, BRCMNAND_CS_XOR);
2438 ctrl->corr_stat_threshold =
2439 brcmnand_read_reg(ctrl, BRCMNAND_CORR_THRESHOLD);
2441 if (has_flash_dma(ctrl))
2442 ctrl->flash_dma_mode = flash_dma_readl(ctrl, FLASH_DMA_MODE);
2447 static int brcmnand_resume(struct device *dev)
2449 struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
2450 struct brcmnand_host *host;
2452 if (has_flash_dma(ctrl)) {
2453 flash_dma_writel(ctrl, FLASH_DMA_MODE, ctrl->flash_dma_mode);
2454 flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
2457 brcmnand_write_reg(ctrl, BRCMNAND_CS_SELECT, ctrl->nand_cs_nand_select);
2458 brcmnand_write_reg(ctrl, BRCMNAND_CS_XOR, ctrl->nand_cs_nand_xor);
2459 brcmnand_write_reg(ctrl, BRCMNAND_CORR_THRESHOLD,
2460 ctrl->corr_stat_threshold);
2462 /* Clear/re-enable interrupt */
2463 ctrl->soc->ctlrdy_ack(ctrl->soc);
2464 ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
2467 list_for_each_entry(host, &ctrl->host_list, node) {
2468 struct nand_chip *chip = &host->chip;
2470 brcmnand_save_restore_cs_config(host, 1);
2472 /* Reset the chip, required by some chips after power-up */
2473 nand_reset_op(chip);
2479 const struct dev_pm_ops brcmnand_pm_ops = {
2480 .suspend = brcmnand_suspend,
2481 .resume = brcmnand_resume,
2483 EXPORT_SYMBOL_GPL(brcmnand_pm_ops);
2485 static const struct of_device_id brcmnand_of_match[] = {
2486 { .compatible = "brcm,brcmnand-v4.0" },
2487 { .compatible = "brcm,brcmnand-v5.0" },
2488 { .compatible = "brcm,brcmnand-v6.0" },
2489 { .compatible = "brcm,brcmnand-v6.1" },
2490 { .compatible = "brcm,brcmnand-v6.2" },
2491 { .compatible = "brcm,brcmnand-v7.0" },
2492 { .compatible = "brcm,brcmnand-v7.1" },
2493 { .compatible = "brcm,brcmnand-v7.2" },
2496 MODULE_DEVICE_TABLE(of, brcmnand_of_match);
2497 #endif /* __UBOOT__ */
2499 /***********************************************************************
2500 * Platform driver setup (per controller)
2501 ***********************************************************************/
2504 int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
2506 int brcmnand_probe(struct udevice *dev, struct brcmnand_soc *soc)
2507 #endif /* __UBOOT__ */
2510 struct device *dev = &pdev->dev;
2511 struct device_node *dn = dev->of_node, *child;
2514 struct udevice *pdev = dev;
2515 #endif /* __UBOOT__ */
2516 struct brcmnand_controller *ctrl;
2518 struct resource *res;
2520 struct resource res;
2521 #endif /* __UBOOT__ */
2525 /* We only support device-tree instantiation */
2529 if (!of_match_node(brcmnand_of_match, dn))
2531 #endif /* __UBOOT__ */
2533 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
2538 dev_set_drvdata(dev, ctrl);
2541 * in u-boot, the data for the driver is allocated before probing
2542 * so to keep the reference to ctrl, we store it in the variable soc
2545 #endif /* __UBOOT__ */
2548 init_completion(&ctrl->done);
2549 init_completion(&ctrl->dma_done);
2550 nand_hw_control_init(&ctrl->controller);
2551 INIT_LIST_HEAD(&ctrl->host_list);
2553 /* NAND register range */
2555 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2556 ctrl->nand_base = devm_ioremap_resource(dev, res);
2558 dev_read_resource(pdev, 0, &res);
2559 ctrl->nand_base = devm_ioremap(pdev, res.start, resource_size(&res));
2561 if (IS_ERR(ctrl->nand_base))
2562 return PTR_ERR(ctrl->nand_base);
2564 /* Enable clock before using NAND registers */
2565 ctrl->clk = devm_clk_get(dev, "nand");
2566 if (!IS_ERR(ctrl->clk)) {
2567 ret = clk_prepare_enable(ctrl->clk);
2571 ret = PTR_ERR(ctrl->clk);
2572 if (ret == -EPROBE_DEFER)
2578 /* Initialize NAND revision */
2579 ret = brcmnand_revision_init(ctrl);
2584 * Most chips have this cache at a fixed offset within 'nand' block.
2585 * Some must specify this region separately.
2588 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand-cache");
2590 ctrl->nand_fc = devm_ioremap_resource(dev, res);
2591 if (IS_ERR(ctrl->nand_fc)) {
2592 ret = PTR_ERR(ctrl->nand_fc);
2596 ctrl->nand_fc = ctrl->nand_base +
2597 ctrl->reg_offsets[BRCMNAND_FC_BASE];
2600 if (!dev_read_resource_byname(pdev, "nand-cache", &res)) {
2601 ctrl->nand_fc = devm_ioremap(dev, res.start,
2602 resource_size(&res));
2603 if (IS_ERR(ctrl->nand_fc)) {
2604 ret = PTR_ERR(ctrl->nand_fc);
2608 ctrl->nand_fc = ctrl->nand_base +
2609 ctrl->reg_offsets[BRCMNAND_FC_BASE];
2615 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash-dma");
2617 ctrl->flash_dma_base = devm_ioremap_resource(dev, res);
2618 if (IS_ERR(ctrl->flash_dma_base)) {
2619 ret = PTR_ERR(ctrl->flash_dma_base);
2623 flash_dma_writel(ctrl, FLASH_DMA_MODE, 1); /* linked-list */
2624 flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
2626 /* Allocate descriptor(s) */
2627 ctrl->dma_desc = dmam_alloc_coherent(dev,
2628 sizeof(*ctrl->dma_desc),
2629 &ctrl->dma_pa, GFP_KERNEL);
2630 if (!ctrl->dma_desc) {
2635 ctrl->dma_irq = platform_get_irq(pdev, 1);
2636 if ((int)ctrl->dma_irq < 0) {
2637 dev_err(dev, "missing FLASH_DMA IRQ\n");
2642 ret = devm_request_irq(dev, ctrl->dma_irq,
2643 brcmnand_dma_irq, 0, DRV_NAME,
2646 dev_err(dev, "can't allocate IRQ %d: error %d\n",
2647 ctrl->dma_irq, ret);
2651 dev_info(dev, "enabling FLASH_DMA\n");
2653 #endif /* __UBOOT__ */
2655 /* Disable automatic device ID config, direct addressing */
2656 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT,
2657 CS_SELECT_AUTO_DEVICE_ID_CFG | 0xff, 0, 0);
2658 /* Disable XOR addressing */
2659 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
2661 if (ctrl->features & BRCMNAND_HAS_WP) {
2662 /* Permanently disable write protection */
2664 brcmnand_set_wp(ctrl, false);
2671 ctrl->irq = platform_get_irq(pdev, 0);
2672 if ((int)ctrl->irq < 0) {
2673 dev_err(dev, "no IRQ defined\n");
2679 * Some SoCs integrate this controller (e.g., its interrupt bits) in
2685 ret = devm_request_irq(dev, ctrl->irq, brcmnand_irq, 0,
2688 /* Enable interrupt */
2689 ctrl->soc->ctlrdy_ack(ctrl->soc);
2690 ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
2692 /* Use standard interrupt infrastructure */
2693 ret = devm_request_irq(dev, ctrl->irq, brcmnand_ctlrdy_irq, 0,
2697 dev_err(dev, "can't allocate IRQ %d: error %d\n",
2701 #endif /* __UBOOT__ */
2704 for_each_available_child_of_node(dn, child) {
2705 if (of_device_is_compatible(child, "brcm,nandcs")) {
2706 struct brcmnand_host *host;
2708 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2717 ret = brcmnand_init_cs(host, child);
2719 devm_kfree(dev, host);
2720 continue; /* Try all chip-selects */
2723 list_add_tail(&host->node, &ctrl->host_list);
2727 ofnode_for_each_subnode(child, dev_ofnode(dev)) {
2728 if (ofnode_device_is_compatible(child, "brcm,nandcs")) {
2729 struct brcmnand_host *host;
2731 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2739 ret = brcmnand_init_cs(host, child);
2741 devm_kfree(dev, host);
2742 continue; /* Try all chip-selects */
2745 list_add_tail(&host->node, &ctrl->host_list);
2748 #endif /* __UBOOT__ */
2752 clk_disable_unprepare(ctrl->clk);
2755 clk_disable(ctrl->clk);
2756 #endif /* __UBOOT__ */
2760 EXPORT_SYMBOL_GPL(brcmnand_probe);
2763 int brcmnand_remove(struct platform_device *pdev)
2765 struct brcmnand_controller *ctrl = dev_get_drvdata(&pdev->dev);
2766 struct brcmnand_host *host;
2768 list_for_each_entry(host, &ctrl->host_list, node)
2769 nand_release(nand_to_mtd(&host->chip));
2771 clk_disable_unprepare(ctrl->clk);
2773 dev_set_drvdata(&pdev->dev, NULL);
2778 int brcmnand_remove(struct udevice *pdev)
2782 #endif /* __UBOOT__ */
2783 EXPORT_SYMBOL_GPL(brcmnand_remove);
2785 MODULE_LICENSE("GPL v2");
2786 MODULE_AUTHOR("Kevin Cernekee");
2787 MODULE_AUTHOR("Brian Norris");
2788 MODULE_DESCRIPTION("NAND driver for Broadcom chips");
2789 MODULE_ALIAS("platform:brcmnand");