blob: 74c9348f7fc4d9bf32c84b16f81429938438246c [file] [log] [blame]
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright © 2010-2015 Broadcom Corporation
4 *
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.
8 *
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.
13 */
14
15#include <common.h>
16#include <asm/io.h>
17#include <memalign.h>
18#include <nand.h>
19#include <clk.h>
Simon Glass9bc15642020-02-03 07:36:16 -070020#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070021#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060022#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060023#include <linux/bug.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070024#include <linux/err.h>
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +010025#include <linux/ioport.h>
26#include <linux/completion.h>
27#include <linux/errno.h>
28#include <linux/log2.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040029#include <linux/mtd/rawnand.h>
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +010030#include <asm/processor.h>
31#include <dm.h>
32
33#include "brcmnand.h"
34#include "brcmnand_compat.h"
35
36/*
37 * This flag controls if WP stays on between erase/write commands to mitigate
38 * flash corruption due to power glitches. Values:
39 * 0: NAND_WP is not used or not available
40 * 1: NAND_WP is set by default, cleared for erase/write operations
41 * 2: NAND_WP is always cleared
42 */
43static int wp_on = 1;
44module_param(wp_on, int, 0444);
45
46/***********************************************************************
47 * Definitions
48 ***********************************************************************/
49
50#define DRV_NAME "brcmnand"
51
52#define CMD_NULL 0x00
53#define CMD_PAGE_READ 0x01
54#define CMD_SPARE_AREA_READ 0x02
55#define CMD_STATUS_READ 0x03
56#define CMD_PROGRAM_PAGE 0x04
57#define CMD_PROGRAM_SPARE_AREA 0x05
58#define CMD_COPY_BACK 0x06
59#define CMD_DEVICE_ID_READ 0x07
60#define CMD_BLOCK_ERASE 0x08
61#define CMD_FLASH_RESET 0x09
62#define CMD_BLOCKS_LOCK 0x0a
63#define CMD_BLOCKS_LOCK_DOWN 0x0b
64#define CMD_BLOCKS_UNLOCK 0x0c
65#define CMD_READ_BLOCKS_LOCK_STATUS 0x0d
66#define CMD_PARAMETER_READ 0x0e
67#define CMD_PARAMETER_CHANGE_COL 0x0f
68#define CMD_LOW_LEVEL_OP 0x10
69
70struct brcm_nand_dma_desc {
71 u32 next_desc;
72 u32 next_desc_ext;
73 u32 cmd_irq;
74 u32 dram_addr;
75 u32 dram_addr_ext;
76 u32 tfr_len;
77 u32 total_len;
78 u32 flash_addr;
79 u32 flash_addr_ext;
80 u32 cs;
81 u32 pad2[5];
82 u32 status_valid;
83} __packed;
84
85/* Bitfields for brcm_nand_dma_desc::status_valid */
86#define FLASH_DMA_ECC_ERROR (1 << 8)
87#define FLASH_DMA_CORR_ERROR (1 << 9)
88
89/* 512B flash cache in the NAND controller HW */
90#define FC_SHIFT 9U
91#define FC_BYTES 512U
92#define FC_WORDS (FC_BYTES >> 2)
93
94#define BRCMNAND_MIN_PAGESIZE 512
95#define BRCMNAND_MIN_BLOCKSIZE (8 * 1024)
96#define BRCMNAND_MIN_DEVSIZE (4ULL * 1024 * 1024)
97
98#define NAND_CTRL_RDY (INTFC_CTLR_READY | INTFC_FLASH_READY)
99#define NAND_POLL_STATUS_TIMEOUT_MS 100
100
101/* Controller feature flags */
102enum {
103 BRCMNAND_HAS_1K_SECTORS = BIT(0),
104 BRCMNAND_HAS_PREFETCH = BIT(1),
105 BRCMNAND_HAS_CACHE_MODE = BIT(2),
106 BRCMNAND_HAS_WP = BIT(3),
107};
108
109struct brcmnand_controller {
110#ifndef __UBOOT__
111 struct device *dev;
112#else
113 struct udevice *dev;
114#endif /* __UBOOT__ */
115 struct nand_hw_control controller;
116 void __iomem *nand_base;
117 void __iomem *nand_fc; /* flash cache */
118 void __iomem *flash_dma_base;
119 unsigned int irq;
120 unsigned int dma_irq;
121 int nand_version;
Philippe Reynes7f28cf62019-03-15 15:14:37 +0100122 int parameter_page_big_endian;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100123
124 /* Some SoCs provide custom interrupt status register(s) */
125 struct brcmnand_soc *soc;
126
127 /* Some SoCs have a gateable clock for the controller */
128 struct clk *clk;
129
130 int cmd_pending;
131 bool dma_pending;
132 struct completion done;
133 struct completion dma_done;
134
135 /* List of NAND hosts (one for each chip-select) */
136 struct list_head host_list;
137
138 struct brcm_nand_dma_desc *dma_desc;
139 dma_addr_t dma_pa;
140
141 /* in-memory cache of the FLASH_CACHE, used only for some commands */
142 u8 flash_cache[FC_BYTES];
143
144 /* Controller revision details */
145 const u16 *reg_offsets;
146 unsigned int reg_spacing; /* between CS1, CS2, ... regs */
147 const u8 *cs_offsets; /* within each chip-select */
148 const u8 *cs0_offsets; /* within CS0, if different */
149 unsigned int max_block_size;
150 const unsigned int *block_sizes;
151 unsigned int max_page_size;
152 const unsigned int *page_sizes;
153 unsigned int max_oob;
154 u32 features;
155
156 /* for low-power standby/resume only */
157 u32 nand_cs_nand_select;
158 u32 nand_cs_nand_xor;
159 u32 corr_stat_threshold;
160 u32 flash_dma_mode;
161};
162
163struct brcmnand_cfg {
164 u64 device_size;
165 unsigned int block_size;
166 unsigned int page_size;
167 unsigned int spare_area_size;
168 unsigned int device_width;
169 unsigned int col_adr_bytes;
170 unsigned int blk_adr_bytes;
171 unsigned int ful_adr_bytes;
172 unsigned int sector_size_1k;
173 unsigned int ecc_level;
174 /* use for low-power standby/resume only */
175 u32 acc_control;
176 u32 config;
177 u32 config_ext;
178 u32 timing_1;
179 u32 timing_2;
180};
181
182struct brcmnand_host {
183 struct list_head node;
184
185 struct nand_chip chip;
186#ifndef __UBOOT__
187 struct platform_device *pdev;
188#else
189 struct udevice *pdev;
190#endif /* __UBOOT__ */
191 int cs;
192
193 unsigned int last_cmd;
194 unsigned int last_byte;
195 u64 last_addr;
196 struct brcmnand_cfg hwcfg;
197 struct brcmnand_controller *ctrl;
198};
199
200enum brcmnand_reg {
201 BRCMNAND_CMD_START = 0,
202 BRCMNAND_CMD_EXT_ADDRESS,
203 BRCMNAND_CMD_ADDRESS,
204 BRCMNAND_INTFC_STATUS,
205 BRCMNAND_CS_SELECT,
206 BRCMNAND_CS_XOR,
207 BRCMNAND_LL_OP,
208 BRCMNAND_CS0_BASE,
209 BRCMNAND_CS1_BASE, /* CS1 regs, if non-contiguous */
210 BRCMNAND_CORR_THRESHOLD,
211 BRCMNAND_CORR_THRESHOLD_EXT,
212 BRCMNAND_UNCORR_COUNT,
213 BRCMNAND_CORR_COUNT,
214 BRCMNAND_CORR_EXT_ADDR,
215 BRCMNAND_CORR_ADDR,
216 BRCMNAND_UNCORR_EXT_ADDR,
217 BRCMNAND_UNCORR_ADDR,
218 BRCMNAND_SEMAPHORE,
219 BRCMNAND_ID,
220 BRCMNAND_ID_EXT,
221 BRCMNAND_LL_RDATA,
222 BRCMNAND_OOB_READ_BASE,
223 BRCMNAND_OOB_READ_10_BASE, /* offset 0x10, if non-contiguous */
224 BRCMNAND_OOB_WRITE_BASE,
225 BRCMNAND_OOB_WRITE_10_BASE, /* offset 0x10, if non-contiguous */
226 BRCMNAND_FC_BASE,
227};
228
229/* BRCMNAND v4.0 */
230static const u16 brcmnand_regs_v40[] = {
231 [BRCMNAND_CMD_START] = 0x04,
232 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
233 [BRCMNAND_CMD_ADDRESS] = 0x0c,
234 [BRCMNAND_INTFC_STATUS] = 0x6c,
235 [BRCMNAND_CS_SELECT] = 0x14,
236 [BRCMNAND_CS_XOR] = 0x18,
237 [BRCMNAND_LL_OP] = 0x178,
238 [BRCMNAND_CS0_BASE] = 0x40,
239 [BRCMNAND_CS1_BASE] = 0xd0,
240 [BRCMNAND_CORR_THRESHOLD] = 0x84,
241 [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
242 [BRCMNAND_UNCORR_COUNT] = 0,
243 [BRCMNAND_CORR_COUNT] = 0,
244 [BRCMNAND_CORR_EXT_ADDR] = 0x70,
245 [BRCMNAND_CORR_ADDR] = 0x74,
246 [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
247 [BRCMNAND_UNCORR_ADDR] = 0x7c,
248 [BRCMNAND_SEMAPHORE] = 0x58,
249 [BRCMNAND_ID] = 0x60,
250 [BRCMNAND_ID_EXT] = 0x64,
251 [BRCMNAND_LL_RDATA] = 0x17c,
252 [BRCMNAND_OOB_READ_BASE] = 0x20,
253 [BRCMNAND_OOB_READ_10_BASE] = 0x130,
254 [BRCMNAND_OOB_WRITE_BASE] = 0x30,
255 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
256 [BRCMNAND_FC_BASE] = 0x200,
257};
258
259/* BRCMNAND v5.0 */
260static const u16 brcmnand_regs_v50[] = {
261 [BRCMNAND_CMD_START] = 0x04,
262 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
263 [BRCMNAND_CMD_ADDRESS] = 0x0c,
264 [BRCMNAND_INTFC_STATUS] = 0x6c,
265 [BRCMNAND_CS_SELECT] = 0x14,
266 [BRCMNAND_CS_XOR] = 0x18,
267 [BRCMNAND_LL_OP] = 0x178,
268 [BRCMNAND_CS0_BASE] = 0x40,
269 [BRCMNAND_CS1_BASE] = 0xd0,
270 [BRCMNAND_CORR_THRESHOLD] = 0x84,
271 [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
272 [BRCMNAND_UNCORR_COUNT] = 0,
273 [BRCMNAND_CORR_COUNT] = 0,
274 [BRCMNAND_CORR_EXT_ADDR] = 0x70,
275 [BRCMNAND_CORR_ADDR] = 0x74,
276 [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
277 [BRCMNAND_UNCORR_ADDR] = 0x7c,
278 [BRCMNAND_SEMAPHORE] = 0x58,
279 [BRCMNAND_ID] = 0x60,
280 [BRCMNAND_ID_EXT] = 0x64,
281 [BRCMNAND_LL_RDATA] = 0x17c,
282 [BRCMNAND_OOB_READ_BASE] = 0x20,
283 [BRCMNAND_OOB_READ_10_BASE] = 0x130,
284 [BRCMNAND_OOB_WRITE_BASE] = 0x30,
285 [BRCMNAND_OOB_WRITE_10_BASE] = 0x140,
286 [BRCMNAND_FC_BASE] = 0x200,
287};
288
289/* BRCMNAND v6.0 - v7.1 */
290static const u16 brcmnand_regs_v60[] = {
291 [BRCMNAND_CMD_START] = 0x04,
292 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
293 [BRCMNAND_CMD_ADDRESS] = 0x0c,
294 [BRCMNAND_INTFC_STATUS] = 0x14,
295 [BRCMNAND_CS_SELECT] = 0x18,
296 [BRCMNAND_CS_XOR] = 0x1c,
297 [BRCMNAND_LL_OP] = 0x20,
298 [BRCMNAND_CS0_BASE] = 0x50,
299 [BRCMNAND_CS1_BASE] = 0,
300 [BRCMNAND_CORR_THRESHOLD] = 0xc0,
301 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xc4,
302 [BRCMNAND_UNCORR_COUNT] = 0xfc,
303 [BRCMNAND_CORR_COUNT] = 0x100,
304 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
305 [BRCMNAND_CORR_ADDR] = 0x110,
306 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
307 [BRCMNAND_UNCORR_ADDR] = 0x118,
308 [BRCMNAND_SEMAPHORE] = 0x150,
309 [BRCMNAND_ID] = 0x194,
310 [BRCMNAND_ID_EXT] = 0x198,
311 [BRCMNAND_LL_RDATA] = 0x19c,
312 [BRCMNAND_OOB_READ_BASE] = 0x200,
313 [BRCMNAND_OOB_READ_10_BASE] = 0,
314 [BRCMNAND_OOB_WRITE_BASE] = 0x280,
315 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
316 [BRCMNAND_FC_BASE] = 0x400,
317};
318
319/* BRCMNAND v7.1 */
320static const u16 brcmnand_regs_v71[] = {
321 [BRCMNAND_CMD_START] = 0x04,
322 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
323 [BRCMNAND_CMD_ADDRESS] = 0x0c,
324 [BRCMNAND_INTFC_STATUS] = 0x14,
325 [BRCMNAND_CS_SELECT] = 0x18,
326 [BRCMNAND_CS_XOR] = 0x1c,
327 [BRCMNAND_LL_OP] = 0x20,
328 [BRCMNAND_CS0_BASE] = 0x50,
329 [BRCMNAND_CS1_BASE] = 0,
330 [BRCMNAND_CORR_THRESHOLD] = 0xdc,
331 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0,
332 [BRCMNAND_UNCORR_COUNT] = 0xfc,
333 [BRCMNAND_CORR_COUNT] = 0x100,
334 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
335 [BRCMNAND_CORR_ADDR] = 0x110,
336 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
337 [BRCMNAND_UNCORR_ADDR] = 0x118,
338 [BRCMNAND_SEMAPHORE] = 0x150,
339 [BRCMNAND_ID] = 0x194,
340 [BRCMNAND_ID_EXT] = 0x198,
341 [BRCMNAND_LL_RDATA] = 0x19c,
342 [BRCMNAND_OOB_READ_BASE] = 0x200,
343 [BRCMNAND_OOB_READ_10_BASE] = 0,
344 [BRCMNAND_OOB_WRITE_BASE] = 0x280,
345 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
346 [BRCMNAND_FC_BASE] = 0x400,
347};
348
349/* BRCMNAND v7.2 */
350static const u16 brcmnand_regs_v72[] = {
351 [BRCMNAND_CMD_START] = 0x04,
352 [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
353 [BRCMNAND_CMD_ADDRESS] = 0x0c,
354 [BRCMNAND_INTFC_STATUS] = 0x14,
355 [BRCMNAND_CS_SELECT] = 0x18,
356 [BRCMNAND_CS_XOR] = 0x1c,
357 [BRCMNAND_LL_OP] = 0x20,
358 [BRCMNAND_CS0_BASE] = 0x50,
359 [BRCMNAND_CS1_BASE] = 0,
360 [BRCMNAND_CORR_THRESHOLD] = 0xdc,
361 [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0,
362 [BRCMNAND_UNCORR_COUNT] = 0xfc,
363 [BRCMNAND_CORR_COUNT] = 0x100,
364 [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
365 [BRCMNAND_CORR_ADDR] = 0x110,
366 [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
367 [BRCMNAND_UNCORR_ADDR] = 0x118,
368 [BRCMNAND_SEMAPHORE] = 0x150,
369 [BRCMNAND_ID] = 0x194,
370 [BRCMNAND_ID_EXT] = 0x198,
371 [BRCMNAND_LL_RDATA] = 0x19c,
372 [BRCMNAND_OOB_READ_BASE] = 0x200,
373 [BRCMNAND_OOB_READ_10_BASE] = 0,
374 [BRCMNAND_OOB_WRITE_BASE] = 0x400,
375 [BRCMNAND_OOB_WRITE_10_BASE] = 0,
376 [BRCMNAND_FC_BASE] = 0x600,
377};
378
379enum brcmnand_cs_reg {
380 BRCMNAND_CS_CFG_EXT = 0,
381 BRCMNAND_CS_CFG,
382 BRCMNAND_CS_ACC_CONTROL,
383 BRCMNAND_CS_TIMING1,
384 BRCMNAND_CS_TIMING2,
385};
386
387/* Per chip-select offsets for v7.1 */
388static const u8 brcmnand_cs_offsets_v71[] = {
389 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
390 [BRCMNAND_CS_CFG_EXT] = 0x04,
391 [BRCMNAND_CS_CFG] = 0x08,
392 [BRCMNAND_CS_TIMING1] = 0x0c,
393 [BRCMNAND_CS_TIMING2] = 0x10,
394};
395
396/* Per chip-select offsets for pre v7.1, except CS0 on <= v5.0 */
397static const u8 brcmnand_cs_offsets[] = {
398 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
399 [BRCMNAND_CS_CFG_EXT] = 0x04,
400 [BRCMNAND_CS_CFG] = 0x04,
401 [BRCMNAND_CS_TIMING1] = 0x08,
402 [BRCMNAND_CS_TIMING2] = 0x0c,
403};
404
405/* Per chip-select offset for <= v5.0 on CS0 only */
406static const u8 brcmnand_cs_offsets_cs0[] = {
407 [BRCMNAND_CS_ACC_CONTROL] = 0x00,
408 [BRCMNAND_CS_CFG_EXT] = 0x08,
409 [BRCMNAND_CS_CFG] = 0x08,
410 [BRCMNAND_CS_TIMING1] = 0x10,
411 [BRCMNAND_CS_TIMING2] = 0x14,
412};
413
414/*
415 * Bitfields for the CFG and CFG_EXT registers. Pre-v7.1 controllers only had
416 * one config register, but once the bitfields overflowed, newer controllers
417 * (v7.1 and newer) added a CFG_EXT register and shuffled a few fields around.
418 */
419enum {
420 CFG_BLK_ADR_BYTES_SHIFT = 8,
421 CFG_COL_ADR_BYTES_SHIFT = 12,
422 CFG_FUL_ADR_BYTES_SHIFT = 16,
423 CFG_BUS_WIDTH_SHIFT = 23,
424 CFG_BUS_WIDTH = BIT(CFG_BUS_WIDTH_SHIFT),
425 CFG_DEVICE_SIZE_SHIFT = 24,
426
427 /* Only for pre-v7.1 (with no CFG_EXT register) */
428 CFG_PAGE_SIZE_SHIFT = 20,
429 CFG_BLK_SIZE_SHIFT = 28,
430
431 /* Only for v7.1+ (with CFG_EXT register) */
432 CFG_EXT_PAGE_SIZE_SHIFT = 0,
433 CFG_EXT_BLK_SIZE_SHIFT = 4,
434};
435
436/* BRCMNAND_INTFC_STATUS */
437enum {
438 INTFC_FLASH_STATUS = GENMASK(7, 0),
439
440 INTFC_ERASED = BIT(27),
441 INTFC_OOB_VALID = BIT(28),
442 INTFC_CACHE_VALID = BIT(29),
443 INTFC_FLASH_READY = BIT(30),
444 INTFC_CTLR_READY = BIT(31),
445};
446
447static inline u32 nand_readreg(struct brcmnand_controller *ctrl, u32 offs)
448{
449 return brcmnand_readl(ctrl->nand_base + offs);
450}
451
452static inline void nand_writereg(struct brcmnand_controller *ctrl, u32 offs,
453 u32 val)
454{
455 brcmnand_writel(val, ctrl->nand_base + offs);
456}
457
458static int brcmnand_revision_init(struct brcmnand_controller *ctrl)
459{
460 static const unsigned int block_sizes_v6[] = { 8, 16, 128, 256, 512, 1024, 2048, 0 };
461 static const unsigned int block_sizes_v4[] = { 16, 128, 8, 512, 256, 1024, 2048, 0 };
462 static const unsigned int page_sizes[] = { 512, 2048, 4096, 8192, 0 };
463
464 ctrl->nand_version = nand_readreg(ctrl, 0) & 0xffff;
465
466 /* Only support v4.0+? */
467 if (ctrl->nand_version < 0x0400) {
468 dev_err(ctrl->dev, "version %#x not supported\n",
469 ctrl->nand_version);
470 return -ENODEV;
471 }
472
473 /* Register offsets */
474 if (ctrl->nand_version >= 0x0702)
475 ctrl->reg_offsets = brcmnand_regs_v72;
476 else if (ctrl->nand_version >= 0x0701)
477 ctrl->reg_offsets = brcmnand_regs_v71;
478 else if (ctrl->nand_version >= 0x0600)
479 ctrl->reg_offsets = brcmnand_regs_v60;
480 else if (ctrl->nand_version >= 0x0500)
481 ctrl->reg_offsets = brcmnand_regs_v50;
482 else if (ctrl->nand_version >= 0x0400)
483 ctrl->reg_offsets = brcmnand_regs_v40;
484
485 /* Chip-select stride */
486 if (ctrl->nand_version >= 0x0701)
487 ctrl->reg_spacing = 0x14;
488 else
489 ctrl->reg_spacing = 0x10;
490
491 /* Per chip-select registers */
492 if (ctrl->nand_version >= 0x0701) {
493 ctrl->cs_offsets = brcmnand_cs_offsets_v71;
494 } else {
495 ctrl->cs_offsets = brcmnand_cs_offsets;
496
497 /* v5.0 and earlier has a different CS0 offset layout */
498 if (ctrl->nand_version <= 0x0500)
499 ctrl->cs0_offsets = brcmnand_cs_offsets_cs0;
500 }
501
502 /* Page / block sizes */
503 if (ctrl->nand_version >= 0x0701) {
504 /* >= v7.1 use nice power-of-2 values! */
505 ctrl->max_page_size = 16 * 1024;
506 ctrl->max_block_size = 2 * 1024 * 1024;
507 } else {
508 ctrl->page_sizes = page_sizes;
509 if (ctrl->nand_version >= 0x0600)
510 ctrl->block_sizes = block_sizes_v6;
511 else
512 ctrl->block_sizes = block_sizes_v4;
513
514 if (ctrl->nand_version < 0x0400) {
515 ctrl->max_page_size = 4096;
516 ctrl->max_block_size = 512 * 1024;
517 }
518 }
519
520 /* Maximum spare area sector size (per 512B) */
521 if (ctrl->nand_version >= 0x0702)
522 ctrl->max_oob = 128;
523 else if (ctrl->nand_version >= 0x0600)
524 ctrl->max_oob = 64;
525 else if (ctrl->nand_version >= 0x0500)
526 ctrl->max_oob = 32;
527 else
528 ctrl->max_oob = 16;
529
530 /* v6.0 and newer (except v6.1) have prefetch support */
531 if (ctrl->nand_version >= 0x0600 && ctrl->nand_version != 0x0601)
532 ctrl->features |= BRCMNAND_HAS_PREFETCH;
533
534 /*
535 * v6.x has cache mode, but it's implemented differently. Ignore it for
536 * now.
537 */
538 if (ctrl->nand_version >= 0x0700)
539 ctrl->features |= BRCMNAND_HAS_CACHE_MODE;
540
541 if (ctrl->nand_version >= 0x0500)
542 ctrl->features |= BRCMNAND_HAS_1K_SECTORS;
543
544 if (ctrl->nand_version >= 0x0700)
545 ctrl->features |= BRCMNAND_HAS_WP;
546#ifndef __UBOOT__
547 else if (of_property_read_bool(ctrl->dev->of_node, "brcm,nand-has-wp"))
548#else
549 else if (dev_read_bool(ctrl->dev, "brcm,nand-has-wp"))
550#endif /* __UBOOT__ */
551 ctrl->features |= BRCMNAND_HAS_WP;
552
553 return 0;
554}
555
556static inline u32 brcmnand_read_reg(struct brcmnand_controller *ctrl,
557 enum brcmnand_reg reg)
558{
559 u16 offs = ctrl->reg_offsets[reg];
560
561 if (offs)
562 return nand_readreg(ctrl, offs);
563 else
564 return 0;
565}
566
567static inline void brcmnand_write_reg(struct brcmnand_controller *ctrl,
568 enum brcmnand_reg reg, u32 val)
569{
570 u16 offs = ctrl->reg_offsets[reg];
571
572 if (offs)
573 nand_writereg(ctrl, offs, val);
574}
575
576static inline void brcmnand_rmw_reg(struct brcmnand_controller *ctrl,
577 enum brcmnand_reg reg, u32 mask, unsigned
578 int shift, u32 val)
579{
580 u32 tmp = brcmnand_read_reg(ctrl, reg);
581
582 tmp &= ~mask;
583 tmp |= val << shift;
584 brcmnand_write_reg(ctrl, reg, tmp);
585}
586
587static inline u32 brcmnand_read_fc(struct brcmnand_controller *ctrl, int word)
588{
589 return __raw_readl(ctrl->nand_fc + word * 4);
590}
591
592static inline void brcmnand_write_fc(struct brcmnand_controller *ctrl,
593 int word, u32 val)
594{
595 __raw_writel(val, ctrl->nand_fc + word * 4);
596}
597
598static inline u16 brcmnand_cs_offset(struct brcmnand_controller *ctrl, int cs,
599 enum brcmnand_cs_reg reg)
600{
601 u16 offs_cs0 = ctrl->reg_offsets[BRCMNAND_CS0_BASE];
602 u16 offs_cs1 = ctrl->reg_offsets[BRCMNAND_CS1_BASE];
603 u8 cs_offs;
604
605 if (cs == 0 && ctrl->cs0_offsets)
606 cs_offs = ctrl->cs0_offsets[reg];
607 else
608 cs_offs = ctrl->cs_offsets[reg];
609
610 if (cs && offs_cs1)
611 return offs_cs1 + (cs - 1) * ctrl->reg_spacing + cs_offs;
612
613 return offs_cs0 + cs * ctrl->reg_spacing + cs_offs;
614}
615
616static inline u32 brcmnand_count_corrected(struct brcmnand_controller *ctrl)
617{
618 if (ctrl->nand_version < 0x0600)
619 return 1;
620 return brcmnand_read_reg(ctrl, BRCMNAND_CORR_COUNT);
621}
622
623static void brcmnand_wr_corr_thresh(struct brcmnand_host *host, u8 val)
624{
625 struct brcmnand_controller *ctrl = host->ctrl;
626 unsigned int shift = 0, bits;
627 enum brcmnand_reg reg = BRCMNAND_CORR_THRESHOLD;
628 int cs = host->cs;
629
630 if (ctrl->nand_version >= 0x0702)
631 bits = 7;
632 else if (ctrl->nand_version >= 0x0600)
633 bits = 6;
634 else if (ctrl->nand_version >= 0x0500)
635 bits = 5;
636 else
637 bits = 4;
638
639 if (ctrl->nand_version >= 0x0702) {
640 if (cs >= 4)
641 reg = BRCMNAND_CORR_THRESHOLD_EXT;
642 shift = (cs % 4) * bits;
643 } else if (ctrl->nand_version >= 0x0600) {
644 if (cs >= 5)
645 reg = BRCMNAND_CORR_THRESHOLD_EXT;
646 shift = (cs % 5) * bits;
647 }
648 brcmnand_rmw_reg(ctrl, reg, (bits - 1) << shift, shift, val);
649}
650
651static inline int brcmnand_cmd_shift(struct brcmnand_controller *ctrl)
652{
653 if (ctrl->nand_version < 0x0602)
654 return 24;
655 return 0;
656}
657
658/***********************************************************************
659 * NAND ACC CONTROL bitfield
660 *
661 * Some bits have remained constant throughout hardware revision, while
662 * others have shifted around.
663 ***********************************************************************/
664
665/* Constant for all versions (where supported) */
666enum {
667 /* See BRCMNAND_HAS_CACHE_MODE */
668 ACC_CONTROL_CACHE_MODE = BIT(22),
669
670 /* See BRCMNAND_HAS_PREFETCH */
671 ACC_CONTROL_PREFETCH = BIT(23),
672
673 ACC_CONTROL_PAGE_HIT = BIT(24),
674 ACC_CONTROL_WR_PREEMPT = BIT(25),
675 ACC_CONTROL_PARTIAL_PAGE = BIT(26),
676 ACC_CONTROL_RD_ERASED = BIT(27),
677 ACC_CONTROL_FAST_PGM_RDIN = BIT(28),
678 ACC_CONTROL_WR_ECC = BIT(30),
679 ACC_CONTROL_RD_ECC = BIT(31),
680};
681
682static inline u32 brcmnand_spare_area_mask(struct brcmnand_controller *ctrl)
683{
684 if (ctrl->nand_version >= 0x0702)
685 return GENMASK(7, 0);
686 else if (ctrl->nand_version >= 0x0600)
687 return GENMASK(6, 0);
688 else
689 return GENMASK(5, 0);
690}
691
692#define NAND_ACC_CONTROL_ECC_SHIFT 16
693#define NAND_ACC_CONTROL_ECC_EXT_SHIFT 13
694
695static inline u32 brcmnand_ecc_level_mask(struct brcmnand_controller *ctrl)
696{
697 u32 mask = (ctrl->nand_version >= 0x0600) ? 0x1f : 0x0f;
698
699 mask <<= NAND_ACC_CONTROL_ECC_SHIFT;
700
701 /* v7.2 includes additional ECC levels */
702 if (ctrl->nand_version >= 0x0702)
703 mask |= 0x7 << NAND_ACC_CONTROL_ECC_EXT_SHIFT;
704
705 return mask;
706}
707
708static void brcmnand_set_ecc_enabled(struct brcmnand_host *host, int en)
709{
710 struct brcmnand_controller *ctrl = host->ctrl;
711 u16 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
712 u32 acc_control = nand_readreg(ctrl, offs);
713 u32 ecc_flags = ACC_CONTROL_WR_ECC | ACC_CONTROL_RD_ECC;
714
715 if (en) {
716 acc_control |= ecc_flags; /* enable RD/WR ECC */
717 acc_control |= host->hwcfg.ecc_level
718 << NAND_ACC_CONTROL_ECC_SHIFT;
719 } else {
720 acc_control &= ~ecc_flags; /* disable RD/WR ECC */
721 acc_control &= ~brcmnand_ecc_level_mask(ctrl);
722 }
723
724 nand_writereg(ctrl, offs, acc_control);
725}
726
727static inline int brcmnand_sector_1k_shift(struct brcmnand_controller *ctrl)
728{
729 if (ctrl->nand_version >= 0x0702)
730 return 9;
731 else if (ctrl->nand_version >= 0x0600)
732 return 7;
733 else if (ctrl->nand_version >= 0x0500)
734 return 6;
735 else
736 return -1;
737}
738
739static int brcmnand_get_sector_size_1k(struct brcmnand_host *host)
740{
741 struct brcmnand_controller *ctrl = host->ctrl;
742 int shift = brcmnand_sector_1k_shift(ctrl);
743 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
744 BRCMNAND_CS_ACC_CONTROL);
745
746 if (shift < 0)
747 return 0;
748
749 return (nand_readreg(ctrl, acc_control_offs) >> shift) & 0x1;
750}
751
752static void brcmnand_set_sector_size_1k(struct brcmnand_host *host, int val)
753{
754 struct brcmnand_controller *ctrl = host->ctrl;
755 int shift = brcmnand_sector_1k_shift(ctrl);
756 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
757 BRCMNAND_CS_ACC_CONTROL);
758 u32 tmp;
759
760 if (shift < 0)
761 return;
762
763 tmp = nand_readreg(ctrl, acc_control_offs);
764 tmp &= ~(1 << shift);
765 tmp |= (!!val) << shift;
766 nand_writereg(ctrl, acc_control_offs, tmp);
767}
768
769/***********************************************************************
770 * CS_NAND_SELECT
771 ***********************************************************************/
772
773enum {
774 CS_SELECT_NAND_WP = BIT(29),
775 CS_SELECT_AUTO_DEVICE_ID_CFG = BIT(30),
776};
777
778static int bcmnand_ctrl_poll_status(struct brcmnand_controller *ctrl,
779 u32 mask, u32 expected_val,
780 unsigned long timeout_ms)
781{
782#ifndef __UBOOT__
783 unsigned long limit;
784 u32 val;
785
786 if (!timeout_ms)
787 timeout_ms = NAND_POLL_STATUS_TIMEOUT_MS;
788
789 limit = jiffies + msecs_to_jiffies(timeout_ms);
790 do {
791 val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS);
792 if ((val & mask) == expected_val)
793 return 0;
794
795 cpu_relax();
796 } while (time_after(limit, jiffies));
797#else
798 unsigned long base, limit;
799 u32 val;
800
801 if (!timeout_ms)
802 timeout_ms = NAND_POLL_STATUS_TIMEOUT_MS;
803
804 base = get_timer(0);
805 limit = CONFIG_SYS_HZ * timeout_ms / 1000;
806 do {
807 val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS);
808 if ((val & mask) == expected_val)
809 return 0;
810
811 cpu_relax();
812 } while (get_timer(base) < limit);
813#endif /* __UBOOT__ */
814
815 dev_warn(ctrl->dev, "timeout on status poll (expected %x got %x)\n",
816 expected_val, val & mask);
817
818 return -ETIMEDOUT;
819}
820
821static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
822{
823 u32 val = en ? CS_SELECT_NAND_WP : 0;
824
825 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
826}
827
828/***********************************************************************
829 * Flash DMA
830 ***********************************************************************/
831
832enum flash_dma_reg {
833 FLASH_DMA_REVISION = 0x00,
834 FLASH_DMA_FIRST_DESC = 0x04,
835 FLASH_DMA_FIRST_DESC_EXT = 0x08,
836 FLASH_DMA_CTRL = 0x0c,
837 FLASH_DMA_MODE = 0x10,
838 FLASH_DMA_STATUS = 0x14,
839 FLASH_DMA_INTERRUPT_DESC = 0x18,
840 FLASH_DMA_INTERRUPT_DESC_EXT = 0x1c,
841 FLASH_DMA_ERROR_STATUS = 0x20,
842 FLASH_DMA_CURRENT_DESC = 0x24,
843 FLASH_DMA_CURRENT_DESC_EXT = 0x28,
844};
845
846static inline bool has_flash_dma(struct brcmnand_controller *ctrl)
847{
848 return ctrl->flash_dma_base;
849}
850
851static inline bool flash_dma_buf_ok(const void *buf)
852{
853#ifndef __UBOOT__
854 return buf && !is_vmalloc_addr(buf) &&
855 likely(IS_ALIGNED((uintptr_t)buf, 4));
856#else
857 return buf && likely(IS_ALIGNED((uintptr_t)buf, 4));
858#endif /* __UBOOT__ */
859}
860
861static inline void flash_dma_writel(struct brcmnand_controller *ctrl, u8 offs,
862 u32 val)
863{
864 brcmnand_writel(val, ctrl->flash_dma_base + offs);
865}
866
867static inline u32 flash_dma_readl(struct brcmnand_controller *ctrl, u8 offs)
868{
869 return brcmnand_readl(ctrl->flash_dma_base + offs);
870}
871
872/* Low-level operation types: command, address, write, or read */
873enum brcmnand_llop_type {
874 LL_OP_CMD,
875 LL_OP_ADDR,
876 LL_OP_WR,
877 LL_OP_RD,
878};
879
880/***********************************************************************
881 * Internal support functions
882 ***********************************************************************/
883
884static inline bool is_hamming_ecc(struct brcmnand_controller *ctrl,
885 struct brcmnand_cfg *cfg)
886{
887 if (ctrl->nand_version <= 0x0701)
888 return cfg->sector_size_1k == 0 && cfg->spare_area_size == 16 &&
889 cfg->ecc_level == 15;
890 else
891 return cfg->sector_size_1k == 0 && ((cfg->spare_area_size == 16 &&
892 cfg->ecc_level == 15) ||
893 (cfg->spare_area_size == 28 && cfg->ecc_level == 16));
894}
895
896/*
William Zhang1100e492019-09-04 10:51:13 -0700897 * Returns a nand_ecclayout strucutre for the given layout/configuration.
898 * Returns NULL on failure.
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100899 */
William Zhang1100e492019-09-04 10:51:13 -0700900static struct nand_ecclayout *brcmnand_create_layout(int ecc_level,
901 struct brcmnand_host *host)
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100902{
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100903 struct brcmnand_cfg *cfg = &host->hwcfg;
William Zhang1100e492019-09-04 10:51:13 -0700904 int i, j;
905 struct nand_ecclayout *layout;
906 int req;
907 int sectors;
908 int sas;
909 int idx1, idx2;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100910
William Zhang1100e492019-09-04 10:51:13 -0700911#ifndef __UBOOT__
912 layout = devm_kzalloc(&host->pdev->dev, sizeof(*layout), GFP_KERNEL);
913#else
914 layout = devm_kzalloc(host->pdev, sizeof(*layout), GFP_KERNEL);
915#endif
916 if (!layout)
917 return NULL;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100918
William Zhang1100e492019-09-04 10:51:13 -0700919 sectors = cfg->page_size / (512 << cfg->sector_size_1k);
920 sas = cfg->spare_area_size << cfg->sector_size_1k;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100921
William Zhang1100e492019-09-04 10:51:13 -0700922 /* Hamming */
923 if (is_hamming_ecc(host->ctrl, cfg)) {
924 for (i = 0, idx1 = 0, idx2 = 0; i < sectors; i++) {
925 /* First sector of each page may have BBI */
926 if (i == 0) {
927 layout->oobfree[idx2].offset = i * sas + 1;
928 /* Small-page NAND use byte 6 for BBI */
929 if (cfg->page_size == 512)
930 layout->oobfree[idx2].offset--;
931 layout->oobfree[idx2].length = 5;
932 } else {
933 layout->oobfree[idx2].offset = i * sas;
934 layout->oobfree[idx2].length = 6;
935 }
936 idx2++;
937 layout->eccpos[idx1++] = i * sas + 6;
938 layout->eccpos[idx1++] = i * sas + 7;
939 layout->eccpos[idx1++] = i * sas + 8;
940 layout->oobfree[idx2].offset = i * sas + 9;
941 layout->oobfree[idx2].length = 7;
942 idx2++;
943 /* Leave zero-terminated entry for OOBFREE */
944 if (idx1 >= MTD_MAX_ECCPOS_ENTRIES_LARGE ||
945 idx2 >= MTD_MAX_OOBFREE_ENTRIES_LARGE - 1)
946 break;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100947 }
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100948
William Zhang1100e492019-09-04 10:51:13 -0700949 return layout;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100950 }
951
William Zhang1100e492019-09-04 10:51:13 -0700952 /*
953 * CONTROLLER_VERSION:
954 * < v5.0: ECC_REQ = ceil(BCH_T * 13/8)
955 * >= v5.0: ECC_REQ = ceil(BCH_T * 14/8)
956 * But we will just be conservative.
957 */
958 req = DIV_ROUND_UP(ecc_level * 14, 8);
959 if (req >= sas) {
Sean Anderson4b5aa002020-09-15 10:44:50 -0400960 dev_err(host->pdev,
William Zhang1100e492019-09-04 10:51:13 -0700961 "error: ECC too large for OOB (ECC bytes %d, spare sector %d)\n",
962 req, sas);
963 return NULL;
964 }
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100965
William Zhang1100e492019-09-04 10:51:13 -0700966 layout->eccbytes = req * sectors;
967 for (i = 0, idx1 = 0, idx2 = 0; i < sectors; i++) {
968 for (j = sas - req; j < sas && idx1 <
969 MTD_MAX_ECCPOS_ENTRIES_LARGE; j++, idx1++)
970 layout->eccpos[idx1] = i * sas + j;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100971
William Zhang1100e492019-09-04 10:51:13 -0700972 /* First sector of each page may have BBI */
973 if (i == 0) {
974 if (cfg->page_size == 512 && (sas - req >= 6)) {
975 /* Small-page NAND use byte 6 for BBI */
976 layout->oobfree[idx2].offset = 0;
977 layout->oobfree[idx2].length = 5;
978 idx2++;
979 if (sas - req > 6) {
980 layout->oobfree[idx2].offset = 6;
981 layout->oobfree[idx2].length =
982 sas - req - 6;
983 idx2++;
984 }
985 } else if (sas > req + 1) {
986 layout->oobfree[idx2].offset = i * sas + 1;
987 layout->oobfree[idx2].length = sas - req - 1;
988 idx2++;
989 }
990 } else if (sas > req) {
991 layout->oobfree[idx2].offset = i * sas;
992 layout->oobfree[idx2].length = sas - req;
993 idx2++;
994 }
995 /* Leave zero-terminated entry for OOBFREE */
996 if (idx1 >= MTD_MAX_ECCPOS_ENTRIES_LARGE ||
997 idx2 >= MTD_MAX_OOBFREE_ENTRIES_LARGE - 1)
998 break;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +0100999 }
1000
William Zhang1100e492019-09-04 10:51:13 -07001001 return layout;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001002}
1003
William Zhang1100e492019-09-04 10:51:13 -07001004static struct nand_ecclayout *brcmstb_choose_ecc_layout(
1005 struct brcmnand_host *host)
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001006{
William Zhang1100e492019-09-04 10:51:13 -07001007 struct nand_ecclayout *layout;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001008 struct brcmnand_cfg *p = &host->hwcfg;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001009 unsigned int ecc_level = p->ecc_level;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001010
1011 if (p->sector_size_1k)
1012 ecc_level <<= 1;
1013
William Zhang1100e492019-09-04 10:51:13 -07001014 layout = brcmnand_create_layout(ecc_level, host);
1015 if (!layout) {
Sean Anderson4b5aa002020-09-15 10:44:50 -04001016 dev_err(host->pdev,
1017 "no proper ecc_layout for this NAND cfg\n");
William Zhang1100e492019-09-04 10:51:13 -07001018 return NULL;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001019 }
1020
William Zhang1100e492019-09-04 10:51:13 -07001021 return layout;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001022}
1023
1024static void brcmnand_wp(struct mtd_info *mtd, int wp)
1025{
1026 struct nand_chip *chip = mtd_to_nand(mtd);
1027 struct brcmnand_host *host = nand_get_controller_data(chip);
1028 struct brcmnand_controller *ctrl = host->ctrl;
1029
1030 if ((ctrl->features & BRCMNAND_HAS_WP) && wp_on == 1) {
1031 static int old_wp = -1;
1032 int ret;
1033
1034 if (old_wp != wp) {
1035 dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
1036 old_wp = wp;
1037 }
1038
1039 /*
1040 * make sure ctrl/flash ready before and after
1041 * changing state of #WP pin
1042 */
1043 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY |
1044 NAND_STATUS_READY,
1045 NAND_CTRL_RDY |
1046 NAND_STATUS_READY, 0);
1047 if (ret)
1048 return;
1049
1050 brcmnand_set_wp(ctrl, wp);
1051 nand_status_op(chip, NULL);
1052 /* NAND_STATUS_WP 0x00 = protected, 0x80 = not protected */
1053 ret = bcmnand_ctrl_poll_status(ctrl,
1054 NAND_CTRL_RDY |
1055 NAND_STATUS_READY |
1056 NAND_STATUS_WP,
1057 NAND_CTRL_RDY |
1058 NAND_STATUS_READY |
1059 (wp ? 0 : NAND_STATUS_WP), 0);
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001060 if (ret)
Sean Anderson4b5aa002020-09-15 10:44:50 -04001061 dev_err(host->pdev, "nand #WP expected %s\n",
1062 wp ? "on" : "off");
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001063 }
1064}
1065
1066/* Helper functions for reading and writing OOB registers */
1067static inline u8 oob_reg_read(struct brcmnand_controller *ctrl, u32 offs)
1068{
1069 u16 offset0, offset10, reg_offs;
1070
1071 offset0 = ctrl->reg_offsets[BRCMNAND_OOB_READ_BASE];
1072 offset10 = ctrl->reg_offsets[BRCMNAND_OOB_READ_10_BASE];
1073
1074 if (offs >= ctrl->max_oob)
1075 return 0x77;
1076
1077 if (offs >= 16 && offset10)
1078 reg_offs = offset10 + ((offs - 0x10) & ~0x03);
1079 else
1080 reg_offs = offset0 + (offs & ~0x03);
1081
1082 return nand_readreg(ctrl, reg_offs) >> (24 - ((offs & 0x03) << 3));
1083}
1084
1085static inline void oob_reg_write(struct brcmnand_controller *ctrl, u32 offs,
1086 u32 data)
1087{
1088 u16 offset0, offset10, reg_offs;
1089
1090 offset0 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_BASE];
1091 offset10 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_10_BASE];
1092
1093 if (offs >= ctrl->max_oob)
1094 return;
1095
1096 if (offs >= 16 && offset10)
1097 reg_offs = offset10 + ((offs - 0x10) & ~0x03);
1098 else
1099 reg_offs = offset0 + (offs & ~0x03);
1100
1101 nand_writereg(ctrl, reg_offs, data);
1102}
1103
1104/*
1105 * read_oob_from_regs - read data from OOB registers
1106 * @ctrl: NAND controller
1107 * @i: sub-page sector index
1108 * @oob: buffer to read to
1109 * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
1110 * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
1111 */
1112static int read_oob_from_regs(struct brcmnand_controller *ctrl, int i, u8 *oob,
1113 int sas, int sector_1k)
1114{
1115 int tbytes = sas << sector_1k;
1116 int j;
1117
1118 /* Adjust OOB values for 1K sector size */
1119 if (sector_1k && (i & 0x01))
1120 tbytes = max(0, tbytes - (int)ctrl->max_oob);
1121 tbytes = min_t(int, tbytes, ctrl->max_oob);
1122
1123 for (j = 0; j < tbytes; j++)
1124 oob[j] = oob_reg_read(ctrl, j);
1125 return tbytes;
1126}
1127
1128/*
1129 * write_oob_to_regs - write data to OOB registers
1130 * @i: sub-page sector index
1131 * @oob: buffer to write from
1132 * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
1133 * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
1134 */
1135static int write_oob_to_regs(struct brcmnand_controller *ctrl, int i,
1136 const u8 *oob, int sas, int sector_1k)
1137{
1138 int tbytes = sas << sector_1k;
1139 int j;
1140
1141 /* Adjust OOB values for 1K sector size */
1142 if (sector_1k && (i & 0x01))
1143 tbytes = max(0, tbytes - (int)ctrl->max_oob);
1144 tbytes = min_t(int, tbytes, ctrl->max_oob);
1145
1146 for (j = 0; j < tbytes; j += 4)
1147 oob_reg_write(ctrl, j,
1148 (oob[j + 0] << 24) |
1149 (oob[j + 1] << 16) |
1150 (oob[j + 2] << 8) |
1151 (oob[j + 3] << 0));
1152 return tbytes;
1153}
1154
1155#ifndef __UBOOT__
1156static irqreturn_t brcmnand_ctlrdy_irq(int irq, void *data)
1157{
1158 struct brcmnand_controller *ctrl = data;
1159
1160 /* Discard all NAND_CTLRDY interrupts during DMA */
1161 if (ctrl->dma_pending)
1162 return IRQ_HANDLED;
1163
1164 complete(&ctrl->done);
1165 return IRQ_HANDLED;
1166}
1167
1168/* Handle SoC-specific interrupt hardware */
1169static irqreturn_t brcmnand_irq(int irq, void *data)
1170{
1171 struct brcmnand_controller *ctrl = data;
1172
1173 if (ctrl->soc->ctlrdy_ack(ctrl->soc))
1174 return brcmnand_ctlrdy_irq(irq, data);
1175
1176 return IRQ_NONE;
1177}
1178
1179static irqreturn_t brcmnand_dma_irq(int irq, void *data)
1180{
1181 struct brcmnand_controller *ctrl = data;
1182
1183 complete(&ctrl->dma_done);
1184
1185 return IRQ_HANDLED;
1186}
1187#endif /* __UBOOT__ */
1188
1189static void brcmnand_send_cmd(struct brcmnand_host *host, int cmd)
1190{
1191 struct brcmnand_controller *ctrl = host->ctrl;
1192 int ret;
1193
1194 dev_dbg(ctrl->dev, "send native cmd %d addr_lo 0x%x\n", cmd,
1195 brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS));
1196 BUG_ON(ctrl->cmd_pending != 0);
1197 ctrl->cmd_pending = cmd;
1198
1199 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, 0);
1200 WARN_ON(ret);
1201
1202 mb(); /* flush previous writes */
1203 brcmnand_write_reg(ctrl, BRCMNAND_CMD_START,
1204 cmd << brcmnand_cmd_shift(ctrl));
1205}
1206
1207/***********************************************************************
1208 * NAND MTD API: read/program/erase
1209 ***********************************************************************/
1210
1211static void brcmnand_cmd_ctrl(struct mtd_info *mtd, int dat,
1212 unsigned int ctrl)
1213{
1214 /* intentionally left blank */
1215}
1216
1217static int brcmnand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1218{
1219 struct nand_chip *chip = mtd_to_nand(mtd);
1220 struct brcmnand_host *host = nand_get_controller_data(chip);
1221 struct brcmnand_controller *ctrl = host->ctrl;
1222
1223#ifndef __UBOOT__
1224 unsigned long timeo = msecs_to_jiffies(100);
1225
1226 dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending);
1227 if (ctrl->cmd_pending &&
1228 wait_for_completion_timeout(&ctrl->done, timeo) <= 0) {
1229 u32 cmd = brcmnand_read_reg(ctrl, BRCMNAND_CMD_START)
1230 >> brcmnand_cmd_shift(ctrl);
1231
1232 dev_err_ratelimited(ctrl->dev,
1233 "timeout waiting for command %#02x\n", cmd);
1234 dev_err_ratelimited(ctrl->dev, "intfc status %08x\n",
1235 brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS));
1236 }
1237#else
1238 unsigned long timeo = 100; /* 100 msec */
1239 int ret;
1240
1241 dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending);
1242
1243 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, timeo);
1244 WARN_ON(ret);
1245#endif /* __UBOOT__ */
1246
1247 ctrl->cmd_pending = 0;
1248 return brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
1249 INTFC_FLASH_STATUS;
1250}
1251
1252enum {
1253 LLOP_RE = BIT(16),
1254 LLOP_WE = BIT(17),
1255 LLOP_ALE = BIT(18),
1256 LLOP_CLE = BIT(19),
1257 LLOP_RETURN_IDLE = BIT(31),
1258
1259 LLOP_DATA_MASK = GENMASK(15, 0),
1260};
1261
1262static int brcmnand_low_level_op(struct brcmnand_host *host,
1263 enum brcmnand_llop_type type, u32 data,
1264 bool last_op)
1265{
1266 struct mtd_info *mtd = nand_to_mtd(&host->chip);
1267 struct nand_chip *chip = &host->chip;
1268 struct brcmnand_controller *ctrl = host->ctrl;
1269 u32 tmp;
1270
1271 tmp = data & LLOP_DATA_MASK;
1272 switch (type) {
1273 case LL_OP_CMD:
1274 tmp |= LLOP_WE | LLOP_CLE;
1275 break;
1276 case LL_OP_ADDR:
1277 /* WE | ALE */
1278 tmp |= LLOP_WE | LLOP_ALE;
1279 break;
1280 case LL_OP_WR:
1281 /* WE */
1282 tmp |= LLOP_WE;
1283 break;
1284 case LL_OP_RD:
1285 /* RE */
1286 tmp |= LLOP_RE;
1287 break;
1288 }
1289 if (last_op)
1290 /* RETURN_IDLE */
1291 tmp |= LLOP_RETURN_IDLE;
1292
1293 dev_dbg(ctrl->dev, "ll_op cmd %#x\n", tmp);
1294
1295 brcmnand_write_reg(ctrl, BRCMNAND_LL_OP, tmp);
1296 (void)brcmnand_read_reg(ctrl, BRCMNAND_LL_OP);
1297
1298 brcmnand_send_cmd(host, CMD_LOW_LEVEL_OP);
1299 return brcmnand_waitfunc(mtd, chip);
1300}
1301
1302static void brcmnand_cmdfunc(struct mtd_info *mtd, unsigned command,
1303 int column, int page_addr)
1304{
1305 struct nand_chip *chip = mtd_to_nand(mtd);
1306 struct brcmnand_host *host = nand_get_controller_data(chip);
1307 struct brcmnand_controller *ctrl = host->ctrl;
1308 u64 addr = (u64)page_addr << chip->page_shift;
1309 int native_cmd = 0;
1310
1311 if (command == NAND_CMD_READID || command == NAND_CMD_PARAM ||
1312 command == NAND_CMD_RNDOUT)
1313 addr = (u64)column;
1314 /* Avoid propagating a negative, don't-care address */
1315 else if (page_addr < 0)
1316 addr = 0;
1317
1318 dev_dbg(ctrl->dev, "cmd 0x%x addr 0x%llx\n", command,
1319 (unsigned long long)addr);
1320
1321 host->last_cmd = command;
1322 host->last_byte = 0;
1323 host->last_addr = addr;
1324
1325 switch (command) {
1326 case NAND_CMD_RESET:
1327 native_cmd = CMD_FLASH_RESET;
1328 break;
1329 case NAND_CMD_STATUS:
1330 native_cmd = CMD_STATUS_READ;
1331 break;
1332 case NAND_CMD_READID:
1333 native_cmd = CMD_DEVICE_ID_READ;
1334 break;
1335 case NAND_CMD_READOOB:
1336 native_cmd = CMD_SPARE_AREA_READ;
1337 break;
1338 case NAND_CMD_ERASE1:
1339 native_cmd = CMD_BLOCK_ERASE;
1340 brcmnand_wp(mtd, 0);
1341 break;
1342 case NAND_CMD_PARAM:
1343 native_cmd = CMD_PARAMETER_READ;
1344 break;
1345 case NAND_CMD_SET_FEATURES:
1346 case NAND_CMD_GET_FEATURES:
1347 brcmnand_low_level_op(host, LL_OP_CMD, command, false);
1348 brcmnand_low_level_op(host, LL_OP_ADDR, column, false);
1349 break;
1350 case NAND_CMD_RNDOUT:
1351 native_cmd = CMD_PARAMETER_CHANGE_COL;
1352 addr &= ~((u64)(FC_BYTES - 1));
1353 /*
1354 * HW quirk: PARAMETER_CHANGE_COL requires SECTOR_SIZE_1K=0
1355 * NB: hwcfg.sector_size_1k may not be initialized yet
1356 */
1357 if (brcmnand_get_sector_size_1k(host)) {
1358 host->hwcfg.sector_size_1k =
1359 brcmnand_get_sector_size_1k(host);
1360 brcmnand_set_sector_size_1k(host, 0);
1361 }
1362 break;
1363 }
1364
1365 if (!native_cmd)
1366 return;
1367
1368 brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
1369 (host->cs << 16) | ((addr >> 32) & 0xffff));
1370 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
1371 brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS, lower_32_bits(addr));
1372 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
1373
1374 brcmnand_send_cmd(host, native_cmd);
1375 brcmnand_waitfunc(mtd, chip);
1376
1377 if (native_cmd == CMD_PARAMETER_READ ||
1378 native_cmd == CMD_PARAMETER_CHANGE_COL) {
1379 /* Copy flash cache word-wise */
1380 u32 *flash_cache = (u32 *)ctrl->flash_cache;
1381 int i;
1382
1383 brcmnand_soc_data_bus_prepare(ctrl->soc, true);
1384
1385 /*
1386 * Must cache the FLASH_CACHE now, since changes in
1387 * SECTOR_SIZE_1K may invalidate it
1388 */
Philippe Reynes7f28cf62019-03-15 15:14:37 +01001389 for (i = 0; i < FC_WORDS; i++) {
1390 u32 fc;
1391
1392 fc = brcmnand_read_fc(ctrl, i);
1393
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001394 /*
1395 * Flash cache is big endian for parameter pages, at
1396 * least on STB SoCs
1397 */
Philippe Reynes7f28cf62019-03-15 15:14:37 +01001398 if (ctrl->parameter_page_big_endian)
1399 flash_cache[i] = be32_to_cpu(fc);
1400 else
1401 flash_cache[i] = le32_to_cpu(fc);
1402 }
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001403
1404 brcmnand_soc_data_bus_unprepare(ctrl->soc, true);
1405
1406 /* Cleanup from HW quirk: restore SECTOR_SIZE_1K */
1407 if (host->hwcfg.sector_size_1k)
1408 brcmnand_set_sector_size_1k(host,
1409 host->hwcfg.sector_size_1k);
1410 }
1411
1412 /* Re-enable protection is necessary only after erase */
1413 if (command == NAND_CMD_ERASE1)
1414 brcmnand_wp(mtd, 1);
1415}
1416
1417static uint8_t brcmnand_read_byte(struct mtd_info *mtd)
1418{
1419 struct nand_chip *chip = mtd_to_nand(mtd);
1420 struct brcmnand_host *host = nand_get_controller_data(chip);
1421 struct brcmnand_controller *ctrl = host->ctrl;
1422 uint8_t ret = 0;
1423 int addr, offs;
1424
1425 switch (host->last_cmd) {
1426 case NAND_CMD_READID:
1427 if (host->last_byte < 4)
1428 ret = brcmnand_read_reg(ctrl, BRCMNAND_ID) >>
1429 (24 - (host->last_byte << 3));
1430 else if (host->last_byte < 8)
1431 ret = brcmnand_read_reg(ctrl, BRCMNAND_ID_EXT) >>
1432 (56 - (host->last_byte << 3));
1433 break;
1434
1435 case NAND_CMD_READOOB:
1436 ret = oob_reg_read(ctrl, host->last_byte);
1437 break;
1438
1439 case NAND_CMD_STATUS:
1440 ret = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
1441 INTFC_FLASH_STATUS;
1442 if (wp_on) /* hide WP status */
1443 ret |= NAND_STATUS_WP;
1444 break;
1445
1446 case NAND_CMD_PARAM:
1447 case NAND_CMD_RNDOUT:
1448 addr = host->last_addr + host->last_byte;
1449 offs = addr & (FC_BYTES - 1);
1450
1451 /* At FC_BYTES boundary, switch to next column */
1452 if (host->last_byte > 0 && offs == 0)
1453 nand_change_read_column_op(chip, addr, NULL, 0, false);
1454
1455 ret = ctrl->flash_cache[offs];
1456 break;
1457 case NAND_CMD_GET_FEATURES:
1458 if (host->last_byte >= ONFI_SUBFEATURE_PARAM_LEN) {
1459 ret = 0;
1460 } else {
1461 bool last = host->last_byte ==
1462 ONFI_SUBFEATURE_PARAM_LEN - 1;
1463 brcmnand_low_level_op(host, LL_OP_RD, 0, last);
1464 ret = brcmnand_read_reg(ctrl, BRCMNAND_LL_RDATA) & 0xff;
1465 }
1466 }
1467
1468 dev_dbg(ctrl->dev, "read byte = 0x%02x\n", ret);
1469 host->last_byte++;
1470
1471 return ret;
1472}
1473
1474static void brcmnand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1475{
1476 int i;
1477
1478 for (i = 0; i < len; i++, buf++)
1479 *buf = brcmnand_read_byte(mtd);
1480}
1481
1482static void brcmnand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
1483 int len)
1484{
1485 int i;
1486 struct nand_chip *chip = mtd_to_nand(mtd);
1487 struct brcmnand_host *host = nand_get_controller_data(chip);
1488
1489 switch (host->last_cmd) {
1490 case NAND_CMD_SET_FEATURES:
1491 for (i = 0; i < len; i++)
1492 brcmnand_low_level_op(host, LL_OP_WR, buf[i],
1493 (i + 1) == len);
1494 break;
1495 default:
1496 BUG();
1497 break;
1498 }
1499}
1500
1501/**
1502 * Construct a FLASH_DMA descriptor as part of a linked list. You must know the
1503 * following ahead of time:
1504 * - Is this descriptor the beginning or end of a linked list?
1505 * - What is the (DMA) address of the next descriptor in the linked list?
1506 */
1507#ifndef __UBOOT__
1508static int brcmnand_fill_dma_desc(struct brcmnand_host *host,
1509 struct brcm_nand_dma_desc *desc, u64 addr,
1510 dma_addr_t buf, u32 len, u8 dma_cmd,
1511 bool begin, bool end,
1512 dma_addr_t next_desc)
1513{
1514 memset(desc, 0, sizeof(*desc));
1515 /* Descriptors are written in native byte order (wordwise) */
1516 desc->next_desc = lower_32_bits(next_desc);
1517 desc->next_desc_ext = upper_32_bits(next_desc);
1518 desc->cmd_irq = (dma_cmd << 24) |
1519 (end ? (0x03 << 8) : 0) | /* IRQ | STOP */
1520 (!!begin) | ((!!end) << 1); /* head, tail */
1521#ifdef CONFIG_CPU_BIG_ENDIAN
1522 desc->cmd_irq |= 0x01 << 12;
1523#endif
1524 desc->dram_addr = lower_32_bits(buf);
1525 desc->dram_addr_ext = upper_32_bits(buf);
1526 desc->tfr_len = len;
1527 desc->total_len = len;
1528 desc->flash_addr = lower_32_bits(addr);
1529 desc->flash_addr_ext = upper_32_bits(addr);
1530 desc->cs = host->cs;
1531 desc->status_valid = 0x01;
1532 return 0;
1533}
1534
1535/**
1536 * Kick the FLASH_DMA engine, with a given DMA descriptor
1537 */
1538static void brcmnand_dma_run(struct brcmnand_host *host, dma_addr_t desc)
1539{
1540 struct brcmnand_controller *ctrl = host->ctrl;
1541 unsigned long timeo = msecs_to_jiffies(100);
1542
1543 flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC, lower_32_bits(desc));
1544 (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC);
1545 flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC_EXT, upper_32_bits(desc));
1546 (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC_EXT);
1547
1548 /* Start FLASH_DMA engine */
1549 ctrl->dma_pending = true;
1550 mb(); /* flush previous writes */
1551 flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0x03); /* wake | run */
1552
1553 if (wait_for_completion_timeout(&ctrl->dma_done, timeo) <= 0) {
1554 dev_err(ctrl->dev,
1555 "timeout waiting for DMA; status %#x, error status %#x\n",
1556 flash_dma_readl(ctrl, FLASH_DMA_STATUS),
1557 flash_dma_readl(ctrl, FLASH_DMA_ERROR_STATUS));
1558 }
1559 ctrl->dma_pending = false;
1560 flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0); /* force stop */
1561}
1562
1563static int brcmnand_dma_trans(struct brcmnand_host *host, u64 addr, u32 *buf,
1564 u32 len, u8 dma_cmd)
1565{
1566 struct brcmnand_controller *ctrl = host->ctrl;
1567 dma_addr_t buf_pa;
1568 int dir = dma_cmd == CMD_PAGE_READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1569
1570 buf_pa = dma_map_single(ctrl->dev, buf, len, dir);
1571 if (dma_mapping_error(ctrl->dev, buf_pa)) {
1572 dev_err(ctrl->dev, "unable to map buffer for DMA\n");
1573 return -ENOMEM;
1574 }
1575
1576 brcmnand_fill_dma_desc(host, ctrl->dma_desc, addr, buf_pa, len,
1577 dma_cmd, true, true, 0);
1578
1579 brcmnand_dma_run(host, ctrl->dma_pa);
1580
1581 dma_unmap_single(ctrl->dev, buf_pa, len, dir);
1582
1583 if (ctrl->dma_desc->status_valid & FLASH_DMA_ECC_ERROR)
1584 return -EBADMSG;
1585 else if (ctrl->dma_desc->status_valid & FLASH_DMA_CORR_ERROR)
1586 return -EUCLEAN;
1587
1588 return 0;
1589}
1590#endif /* __UBOOT__ */
1591
1592/*
1593 * Assumes proper CS is already set
1594 */
1595static int brcmnand_read_by_pio(struct mtd_info *mtd, struct nand_chip *chip,
1596 u64 addr, unsigned int trans, u32 *buf,
1597 u8 *oob, u64 *err_addr)
1598{
1599 struct brcmnand_host *host = nand_get_controller_data(chip);
1600 struct brcmnand_controller *ctrl = host->ctrl;
1601 int i, j, ret = 0;
1602
1603 /* Clear error addresses */
1604 brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_ADDR, 0);
1605 brcmnand_write_reg(ctrl, BRCMNAND_CORR_ADDR, 0);
1606 brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_EXT_ADDR, 0);
1607 brcmnand_write_reg(ctrl, BRCMNAND_CORR_EXT_ADDR, 0);
1608
1609 brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
1610 (host->cs << 16) | ((addr >> 32) & 0xffff));
1611 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
1612
1613 for (i = 0; i < trans; i++, addr += FC_BYTES) {
1614 brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS,
1615 lower_32_bits(addr));
1616 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
1617 /* SPARE_AREA_READ does not use ECC, so just use PAGE_READ */
1618 brcmnand_send_cmd(host, CMD_PAGE_READ);
1619 brcmnand_waitfunc(mtd, chip);
1620
1621 if (likely(buf)) {
1622 brcmnand_soc_data_bus_prepare(ctrl->soc, false);
1623
1624 for (j = 0; j < FC_WORDS; j++, buf++)
1625 *buf = brcmnand_read_fc(ctrl, j);
1626
1627 brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
1628 }
1629
1630 if (oob)
1631 oob += read_oob_from_regs(ctrl, i, oob,
1632 mtd->oobsize / trans,
1633 host->hwcfg.sector_size_1k);
1634
Joel Peshkin8384fd82021-12-20 20:15:47 -08001635 if (ret != -EBADMSG) {
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001636 *err_addr = brcmnand_read_reg(ctrl,
1637 BRCMNAND_UNCORR_ADDR) |
1638 ((u64)(brcmnand_read_reg(ctrl,
1639 BRCMNAND_UNCORR_EXT_ADDR)
1640 & 0xffff) << 32);
1641 if (*err_addr)
1642 ret = -EBADMSG;
1643 }
1644
1645 if (!ret) {
1646 *err_addr = brcmnand_read_reg(ctrl,
1647 BRCMNAND_CORR_ADDR) |
1648 ((u64)(brcmnand_read_reg(ctrl,
1649 BRCMNAND_CORR_EXT_ADDR)
1650 & 0xffff) << 32);
1651 if (*err_addr)
1652 ret = -EUCLEAN;
1653 }
1654 }
1655
1656 return ret;
1657}
1658
1659/*
1660 * Check a page to see if it is erased (w/ bitflips) after an uncorrectable ECC
1661 * error
1662 *
1663 * Because the HW ECC signals an ECC error if an erase paged has even a single
1664 * bitflip, we must check each ECC error to see if it is actually an erased
1665 * page with bitflips, not a truly corrupted page.
1666 *
1667 * On a real error, return a negative error code (-EBADMSG for ECC error), and
1668 * buf will contain raw data.
1669 * Otherwise, buf gets filled with 0xffs and return the maximum number of
1670 * bitflips-per-ECC-sector to the caller.
1671 *
1672 */
1673static int brcmstb_nand_verify_erased_page(struct mtd_info *mtd,
1674 struct nand_chip *chip, void *buf, u64 addr)
1675{
1676 int i, sas;
1677 void *oob = chip->oob_poi;
1678 int bitflips = 0;
1679 int page = addr >> chip->page_shift;
1680 int ret;
1681
1682 if (!buf) {
1683#ifndef __UBOOT__
1684 buf = chip->data_buf;
1685#else
1686 buf = chip->buffers->databuf;
1687#endif
1688 /* Invalidate page cache */
1689 chip->pagebuf = -1;
1690 }
1691
1692 sas = mtd->oobsize / chip->ecc.steps;
1693
1694 /* read without ecc for verification */
1695 ret = chip->ecc.read_page_raw(mtd, chip, buf, true, page);
1696 if (ret)
1697 return ret;
1698
1699 for (i = 0; i < chip->ecc.steps; i++, oob += sas) {
1700 ret = nand_check_erased_ecc_chunk(buf, chip->ecc.size,
1701 oob, sas, NULL, 0,
1702 chip->ecc.strength);
1703 if (ret < 0)
1704 return ret;
1705
1706 bitflips = max(bitflips, ret);
1707 }
1708
1709 return bitflips;
1710}
1711
1712static int brcmnand_read(struct mtd_info *mtd, struct nand_chip *chip,
1713 u64 addr, unsigned int trans, u32 *buf, u8 *oob)
1714{
1715 struct brcmnand_host *host = nand_get_controller_data(chip);
1716 struct brcmnand_controller *ctrl = host->ctrl;
1717 u64 err_addr = 0;
1718 int err;
1719 bool retry = true;
1720
1721 dev_dbg(ctrl->dev, "read %llx -> %p\n", (unsigned long long)addr, buf);
1722
1723try_dmaread:
1724 brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_COUNT, 0);
1725
1726#ifndef __UBOOT__
1727 if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
1728 err = brcmnand_dma_trans(host, addr, buf, trans * FC_BYTES,
1729 CMD_PAGE_READ);
1730 if (err) {
1731 if (mtd_is_bitflip_or_eccerr(err))
1732 err_addr = addr;
1733 else
1734 return -EIO;
1735 }
1736 } else {
1737 if (oob)
1738 memset(oob, 0x99, mtd->oobsize);
1739
1740 err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf,
1741 oob, &err_addr);
1742 }
1743#else
1744 if (oob)
1745 memset(oob, 0x99, mtd->oobsize);
1746
1747 err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf,
1748 oob, &err_addr);
1749#endif /* __UBOOT__ */
1750
1751 if (mtd_is_eccerr(err)) {
1752 /*
1753 * On controller version and 7.0, 7.1 , DMA read after a
1754 * prior PIO read that reported uncorrectable error,
1755 * the DMA engine captures this error following DMA read
1756 * cleared only on subsequent DMA read, so just retry once
1757 * to clear a possible false error reported for current DMA
1758 * read
1759 */
1760 if ((ctrl->nand_version == 0x0700) ||
1761 (ctrl->nand_version == 0x0701)) {
1762 if (retry) {
1763 retry = false;
1764 goto try_dmaread;
1765 }
1766 }
1767
1768 /*
1769 * Controller version 7.2 has hw encoder to detect erased page
1770 * bitflips, apply sw verification for older controllers only
1771 */
1772 if (ctrl->nand_version < 0x0702) {
1773 err = brcmstb_nand_verify_erased_page(mtd, chip, buf,
1774 addr);
1775 /* erased page bitflips corrected */
1776 if (err >= 0)
1777 return err;
1778 }
1779
1780 dev_dbg(ctrl->dev, "uncorrectable error at 0x%llx\n",
1781 (unsigned long long)err_addr);
1782 mtd->ecc_stats.failed++;
1783 /* NAND layer expects zero on ECC errors */
1784 return 0;
1785 }
1786
1787 if (mtd_is_bitflip(err)) {
1788 unsigned int corrected = brcmnand_count_corrected(ctrl);
1789
1790 dev_dbg(ctrl->dev, "corrected error at 0x%llx\n",
1791 (unsigned long long)err_addr);
1792 mtd->ecc_stats.corrected += corrected;
1793 /* Always exceed the software-imposed threshold */
1794 return max(mtd->bitflip_threshold, corrected);
1795 }
1796
1797 return 0;
1798}
1799
1800static int brcmnand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1801 uint8_t *buf, int oob_required, int page)
1802{
1803 struct brcmnand_host *host = nand_get_controller_data(chip);
1804 u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
1805
1806 nand_read_page_op(chip, page, 0, NULL, 0);
1807
1808 return brcmnand_read(mtd, chip, host->last_addr,
1809 mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
1810}
1811
1812static int brcmnand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1813 uint8_t *buf, int oob_required, int page)
1814{
1815 struct brcmnand_host *host = nand_get_controller_data(chip);
1816 u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
1817 int ret;
1818
1819 nand_read_page_op(chip, page, 0, NULL, 0);
1820
1821 brcmnand_set_ecc_enabled(host, 0);
1822 ret = brcmnand_read(mtd, chip, host->last_addr,
1823 mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
1824 brcmnand_set_ecc_enabled(host, 1);
1825 return ret;
1826}
1827
1828static int brcmnand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1829 int page)
1830{
1831 return brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
1832 mtd->writesize >> FC_SHIFT,
1833 NULL, (u8 *)chip->oob_poi);
1834}
1835
1836static int brcmnand_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1837 int page)
1838{
1839 struct brcmnand_host *host = nand_get_controller_data(chip);
1840
1841 brcmnand_set_ecc_enabled(host, 0);
1842 brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
1843 mtd->writesize >> FC_SHIFT,
1844 NULL, (u8 *)chip->oob_poi);
1845 brcmnand_set_ecc_enabled(host, 1);
1846 return 0;
1847}
1848
1849static int brcmnand_write(struct mtd_info *mtd, struct nand_chip *chip,
1850 u64 addr, const u32 *buf, u8 *oob)
1851{
1852 struct brcmnand_host *host = nand_get_controller_data(chip);
1853 struct brcmnand_controller *ctrl = host->ctrl;
1854 unsigned int i, j, trans = mtd->writesize >> FC_SHIFT;
1855 int status, ret = 0;
1856
1857 dev_dbg(ctrl->dev, "write %llx <- %p\n", (unsigned long long)addr, buf);
1858
1859 if (unlikely((unsigned long)buf & 0x03)) {
1860 dev_warn(ctrl->dev, "unaligned buffer: %p\n", buf);
1861 buf = (u32 *)((unsigned long)buf & ~0x03);
1862 }
1863
1864 brcmnand_wp(mtd, 0);
1865
1866 for (i = 0; i < ctrl->max_oob; i += 4)
1867 oob_reg_write(ctrl, i, 0xffffffff);
1868
1869#ifndef __UBOOT__
1870 if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
1871 if (brcmnand_dma_trans(host, addr, (u32 *)buf,
1872 mtd->writesize, CMD_PROGRAM_PAGE))
1873 ret = -EIO;
1874 goto out;
1875 }
1876#endif /* __UBOOT__ */
1877
1878 brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
1879 (host->cs << 16) | ((addr >> 32) & 0xffff));
1880 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
1881
1882 for (i = 0; i < trans; i++, addr += FC_BYTES) {
1883 /* full address MUST be set before populating FC */
1884 brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS,
1885 lower_32_bits(addr));
1886 (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
1887
1888 if (buf) {
1889 brcmnand_soc_data_bus_prepare(ctrl->soc, false);
1890
1891 for (j = 0; j < FC_WORDS; j++, buf++)
1892 brcmnand_write_fc(ctrl, j, *buf);
1893
1894 brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
1895 } else if (oob) {
1896 for (j = 0; j < FC_WORDS; j++)
1897 brcmnand_write_fc(ctrl, j, 0xffffffff);
1898 }
1899
1900 if (oob) {
1901 oob += write_oob_to_regs(ctrl, i, oob,
1902 mtd->oobsize / trans,
1903 host->hwcfg.sector_size_1k);
1904 }
1905
1906 /* we cannot use SPARE_AREA_PROGRAM when PARTIAL_PAGE_EN=0 */
1907 brcmnand_send_cmd(host, CMD_PROGRAM_PAGE);
1908 status = brcmnand_waitfunc(mtd, chip);
1909
1910 if (status & NAND_STATUS_FAIL) {
1911 dev_info(ctrl->dev, "program failed at %llx\n",
1912 (unsigned long long)addr);
1913 ret = -EIO;
1914 goto out;
1915 }
1916 }
1917out:
1918 brcmnand_wp(mtd, 1);
1919 return ret;
1920}
1921
1922static int brcmnand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1923 const uint8_t *buf, int oob_required, int page)
1924{
1925 struct brcmnand_host *host = nand_get_controller_data(chip);
1926 void *oob = oob_required ? chip->oob_poi : NULL;
1927
1928 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1929 brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
1930
1931 return nand_prog_page_end_op(chip);
1932}
1933
1934static int brcmnand_write_page_raw(struct mtd_info *mtd,
1935 struct nand_chip *chip, const uint8_t *buf,
1936 int oob_required, int page)
1937{
1938 struct brcmnand_host *host = nand_get_controller_data(chip);
1939 void *oob = oob_required ? chip->oob_poi : NULL;
1940
1941 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1942 brcmnand_set_ecc_enabled(host, 0);
1943 brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
1944 brcmnand_set_ecc_enabled(host, 1);
1945
1946 return nand_prog_page_end_op(chip);
1947}
1948
1949static int brcmnand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1950 int page)
1951{
1952 return brcmnand_write(mtd, chip, (u64)page << chip->page_shift,
1953 NULL, chip->oob_poi);
1954}
1955
1956static int brcmnand_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1957 int page)
1958{
1959 struct brcmnand_host *host = nand_get_controller_data(chip);
1960 int ret;
1961
1962 brcmnand_set_ecc_enabled(host, 0);
1963 ret = brcmnand_write(mtd, chip, (u64)page << chip->page_shift, NULL,
1964 (u8 *)chip->oob_poi);
1965 brcmnand_set_ecc_enabled(host, 1);
1966
1967 return ret;
1968}
1969
1970/***********************************************************************
1971 * Per-CS setup (1 NAND device)
1972 ***********************************************************************/
1973
1974static int brcmnand_set_cfg(struct brcmnand_host *host,
1975 struct brcmnand_cfg *cfg)
1976{
1977 struct brcmnand_controller *ctrl = host->ctrl;
1978 struct nand_chip *chip = &host->chip;
1979 u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
1980 u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
1981 BRCMNAND_CS_CFG_EXT);
1982 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
1983 BRCMNAND_CS_ACC_CONTROL);
1984 u8 block_size = 0, page_size = 0, device_size = 0;
1985 u32 tmp;
1986
1987 if (ctrl->block_sizes) {
1988 int i, found;
1989
1990 for (i = 0, found = 0; ctrl->block_sizes[i]; i++)
1991 if (ctrl->block_sizes[i] * 1024 == cfg->block_size) {
1992 block_size = i;
1993 found = 1;
1994 }
1995 if (!found) {
1996 dev_warn(ctrl->dev, "invalid block size %u\n",
1997 cfg->block_size);
1998 return -EINVAL;
1999 }
2000 } else {
2001 block_size = ffs(cfg->block_size) - ffs(BRCMNAND_MIN_BLOCKSIZE);
2002 }
2003
2004 if (cfg->block_size < BRCMNAND_MIN_BLOCKSIZE || (ctrl->max_block_size &&
2005 cfg->block_size > ctrl->max_block_size)) {
2006 dev_warn(ctrl->dev, "invalid block size %u\n",
2007 cfg->block_size);
2008 block_size = 0;
2009 }
2010
2011 if (ctrl->page_sizes) {
2012 int i, found;
2013
2014 for (i = 0, found = 0; ctrl->page_sizes[i]; i++)
2015 if (ctrl->page_sizes[i] == cfg->page_size) {
2016 page_size = i;
2017 found = 1;
2018 }
2019 if (!found) {
2020 dev_warn(ctrl->dev, "invalid page size %u\n",
2021 cfg->page_size);
2022 return -EINVAL;
2023 }
2024 } else {
2025 page_size = ffs(cfg->page_size) - ffs(BRCMNAND_MIN_PAGESIZE);
2026 }
2027
2028 if (cfg->page_size < BRCMNAND_MIN_PAGESIZE || (ctrl->max_page_size &&
2029 cfg->page_size > ctrl->max_page_size)) {
2030 dev_warn(ctrl->dev, "invalid page size %u\n", cfg->page_size);
2031 return -EINVAL;
2032 }
2033
2034 if (fls64(cfg->device_size) < fls64(BRCMNAND_MIN_DEVSIZE)) {
2035 dev_warn(ctrl->dev, "invalid device size 0x%llx\n",
2036 (unsigned long long)cfg->device_size);
2037 return -EINVAL;
2038 }
2039 device_size = fls64(cfg->device_size) - fls64(BRCMNAND_MIN_DEVSIZE);
2040
2041 tmp = (cfg->blk_adr_bytes << CFG_BLK_ADR_BYTES_SHIFT) |
2042 (cfg->col_adr_bytes << CFG_COL_ADR_BYTES_SHIFT) |
2043 (cfg->ful_adr_bytes << CFG_FUL_ADR_BYTES_SHIFT) |
2044 (!!(cfg->device_width == 16) << CFG_BUS_WIDTH_SHIFT) |
2045 (device_size << CFG_DEVICE_SIZE_SHIFT);
2046 if (cfg_offs == cfg_ext_offs) {
2047 tmp |= (page_size << CFG_PAGE_SIZE_SHIFT) |
2048 (block_size << CFG_BLK_SIZE_SHIFT);
2049 nand_writereg(ctrl, cfg_offs, tmp);
2050 } else {
2051 nand_writereg(ctrl, cfg_offs, tmp);
2052 tmp = (page_size << CFG_EXT_PAGE_SIZE_SHIFT) |
2053 (block_size << CFG_EXT_BLK_SIZE_SHIFT);
2054 nand_writereg(ctrl, cfg_ext_offs, tmp);
2055 }
2056
2057 tmp = nand_readreg(ctrl, acc_control_offs);
2058 tmp &= ~brcmnand_ecc_level_mask(ctrl);
2059 tmp |= cfg->ecc_level << NAND_ACC_CONTROL_ECC_SHIFT;
2060 tmp &= ~brcmnand_spare_area_mask(ctrl);
2061 tmp |= cfg->spare_area_size;
2062 nand_writereg(ctrl, acc_control_offs, tmp);
2063
2064 brcmnand_set_sector_size_1k(host, cfg->sector_size_1k);
2065
2066 /* threshold = ceil(BCH-level * 0.75) */
2067 brcmnand_wr_corr_thresh(host, DIV_ROUND_UP(chip->ecc.strength * 3, 4));
2068
2069 return 0;
2070}
2071
2072static void brcmnand_print_cfg(struct brcmnand_host *host,
2073 char *buf, struct brcmnand_cfg *cfg)
2074{
2075 buf += sprintf(buf,
2076 "%lluMiB total, %uKiB blocks, %u%s pages, %uB OOB, %u-bit",
2077 (unsigned long long)cfg->device_size >> 20,
2078 cfg->block_size >> 10,
2079 cfg->page_size >= 1024 ? cfg->page_size >> 10 : cfg->page_size,
2080 cfg->page_size >= 1024 ? "KiB" : "B",
2081 cfg->spare_area_size, cfg->device_width);
2082
2083 /* Account for Hamming ECC and for BCH 512B vs 1KiB sectors */
2084 if (is_hamming_ecc(host->ctrl, cfg))
2085 sprintf(buf, ", Hamming ECC");
2086 else if (cfg->sector_size_1k)
2087 sprintf(buf, ", BCH-%u (1KiB sector)", cfg->ecc_level << 1);
2088 else
2089 sprintf(buf, ", BCH-%u", cfg->ecc_level);
2090}
2091
2092/*
2093 * Minimum number of bytes to address a page. Calculated as:
2094 * roundup(log2(size / page-size) / 8)
2095 *
2096 * NB: the following does not "round up" for non-power-of-2 'size'; but this is
2097 * OK because many other things will break if 'size' is irregular...
2098 */
2099static inline int get_blk_adr_bytes(u64 size, u32 writesize)
2100{
2101 return ALIGN(ilog2(size) - ilog2(writesize), 8) >> 3;
2102}
2103
2104static int brcmnand_setup_dev(struct brcmnand_host *host)
2105{
2106 struct mtd_info *mtd = nand_to_mtd(&host->chip);
2107 struct nand_chip *chip = &host->chip;
2108 struct brcmnand_controller *ctrl = host->ctrl;
2109 struct brcmnand_cfg *cfg = &host->hwcfg;
2110 char msg[128];
2111 u32 offs, tmp, oob_sector;
2112 int ret;
2113
2114 memset(cfg, 0, sizeof(*cfg));
2115
2116#ifndef __UBOOT__
2117 ret = of_property_read_u32(nand_get_flash_node(chip),
2118 "brcm,nand-oob-sector-size",
2119 &oob_sector);
2120#else
2121 ret = ofnode_read_u32(nand_get_flash_node(chip),
2122 "brcm,nand-oob-sector-size",
2123 &oob_sector);
2124#endif /* __UBOOT__ */
2125 if (ret) {
2126 /* Use detected size */
2127 cfg->spare_area_size = mtd->oobsize /
2128 (mtd->writesize >> FC_SHIFT);
2129 } else {
2130 cfg->spare_area_size = oob_sector;
2131 }
2132 if (cfg->spare_area_size > ctrl->max_oob)
2133 cfg->spare_area_size = ctrl->max_oob;
2134 /*
2135 * Set oobsize to be consistent with controller's spare_area_size, as
2136 * the rest is inaccessible.
2137 */
2138 mtd->oobsize = cfg->spare_area_size * (mtd->writesize >> FC_SHIFT);
2139
2140 cfg->device_size = mtd->size;
2141 cfg->block_size = mtd->erasesize;
2142 cfg->page_size = mtd->writesize;
2143 cfg->device_width = (chip->options & NAND_BUSWIDTH_16) ? 16 : 8;
2144 cfg->col_adr_bytes = 2;
2145 cfg->blk_adr_bytes = get_blk_adr_bytes(mtd->size, mtd->writesize);
2146
2147 if (chip->ecc.mode != NAND_ECC_HW) {
2148 dev_err(ctrl->dev, "only HW ECC supported; selected: %d\n",
2149 chip->ecc.mode);
2150 return -EINVAL;
2151 }
2152
2153 if (chip->ecc.algo == NAND_ECC_UNKNOWN) {
2154 if (chip->ecc.strength == 1 && chip->ecc.size == 512)
2155 /* Default to Hamming for 1-bit ECC, if unspecified */
2156 chip->ecc.algo = NAND_ECC_HAMMING;
2157 else
2158 /* Otherwise, BCH */
2159 chip->ecc.algo = NAND_ECC_BCH;
2160 }
2161
2162 if (chip->ecc.algo == NAND_ECC_HAMMING && (chip->ecc.strength != 1 ||
2163 chip->ecc.size != 512)) {
2164 dev_err(ctrl->dev, "invalid Hamming params: %d bits per %d bytes\n",
2165 chip->ecc.strength, chip->ecc.size);
2166 return -EINVAL;
2167 }
2168
2169 switch (chip->ecc.size) {
2170 case 512:
2171 if (chip->ecc.algo == NAND_ECC_HAMMING)
2172 cfg->ecc_level = 15;
2173 else
2174 cfg->ecc_level = chip->ecc.strength;
2175 cfg->sector_size_1k = 0;
2176 break;
2177 case 1024:
2178 if (!(ctrl->features & BRCMNAND_HAS_1K_SECTORS)) {
2179 dev_err(ctrl->dev, "1KB sectors not supported\n");
2180 return -EINVAL;
2181 }
2182 if (chip->ecc.strength & 0x1) {
2183 dev_err(ctrl->dev,
2184 "odd ECC not supported with 1KB sectors\n");
2185 return -EINVAL;
2186 }
2187
2188 cfg->ecc_level = chip->ecc.strength >> 1;
2189 cfg->sector_size_1k = 1;
2190 break;
2191 default:
2192 dev_err(ctrl->dev, "unsupported ECC size: %d\n",
2193 chip->ecc.size);
2194 return -EINVAL;
2195 }
2196
2197 cfg->ful_adr_bytes = cfg->blk_adr_bytes;
2198 if (mtd->writesize > 512)
2199 cfg->ful_adr_bytes += cfg->col_adr_bytes;
2200 else
2201 cfg->ful_adr_bytes += 1;
2202
2203 ret = brcmnand_set_cfg(host, cfg);
2204 if (ret)
2205 return ret;
2206
2207 brcmnand_set_ecc_enabled(host, 1);
2208
2209 brcmnand_print_cfg(host, msg, cfg);
2210 dev_info(ctrl->dev, "detected %s\n", msg);
2211
2212 /* Configure ACC_CONTROL */
2213 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
2214 tmp = nand_readreg(ctrl, offs);
2215 tmp &= ~ACC_CONTROL_PARTIAL_PAGE;
2216 tmp &= ~ACC_CONTROL_RD_ERASED;
2217
2218 /* We need to turn on Read from erased paged protected by ECC */
2219 if (ctrl->nand_version >= 0x0702)
2220 tmp |= ACC_CONTROL_RD_ERASED;
2221 tmp &= ~ACC_CONTROL_FAST_PGM_RDIN;
2222 if (ctrl->features & BRCMNAND_HAS_PREFETCH)
2223 tmp &= ~ACC_CONTROL_PREFETCH;
2224
2225 nand_writereg(ctrl, offs, tmp);
2226
2227 return 0;
2228}
2229
2230#ifndef __UBOOT__
2231static int brcmnand_init_cs(struct brcmnand_host *host, struct device_node *dn)
2232#else
2233static int brcmnand_init_cs(struct brcmnand_host *host, ofnode dn)
2234#endif
2235{
2236 struct brcmnand_controller *ctrl = host->ctrl;
2237#ifndef __UBOOT__
2238 struct platform_device *pdev = host->pdev;
2239#else
2240 struct udevice *pdev = host->pdev;
2241#endif /* __UBOOT__ */
2242 struct mtd_info *mtd;
2243 struct nand_chip *chip;
2244 int ret;
2245 u16 cfg_offs;
2246
2247#ifndef __UBOOT__
2248 ret = of_property_read_u32(dn, "reg", &host->cs);
2249#else
2250 ret = ofnode_read_s32(dn, "reg", &host->cs);
2251#endif
2252 if (ret) {
Sean Anderson4b5aa002020-09-15 10:44:50 -04002253 dev_err(pdev, "can't get chip-select\n");
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002254 return -ENXIO;
2255 }
2256
2257 mtd = nand_to_mtd(&host->chip);
2258 chip = &host->chip;
2259
2260 nand_set_flash_node(chip, dn);
2261 nand_set_controller_data(chip, host);
2262#ifndef __UBOOT__
2263 mtd->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "brcmnand.%d",
2264 host->cs);
2265#else
2266 mtd->name = devm_kasprintf(pdev, GFP_KERNEL, "brcmnand.%d",
2267 host->cs);
2268#endif /* __UBOOT__ */
2269 if (!mtd->name)
2270 return -ENOMEM;
2271
2272 mtd->owner = THIS_MODULE;
2273#ifndef __UBOOT__
2274 mtd->dev.parent = &pdev->dev;
2275#else
2276 mtd->dev->parent = pdev;
2277#endif /* __UBOOT__ */
2278
2279 chip->IO_ADDR_R = (void __iomem *)0xdeadbeef;
2280 chip->IO_ADDR_W = (void __iomem *)0xdeadbeef;
2281
2282 chip->cmd_ctrl = brcmnand_cmd_ctrl;
2283 chip->cmdfunc = brcmnand_cmdfunc;
2284 chip->waitfunc = brcmnand_waitfunc;
2285 chip->read_byte = brcmnand_read_byte;
2286 chip->read_buf = brcmnand_read_buf;
2287 chip->write_buf = brcmnand_write_buf;
2288
2289 chip->ecc.mode = NAND_ECC_HW;
2290 chip->ecc.read_page = brcmnand_read_page;
2291 chip->ecc.write_page = brcmnand_write_page;
2292 chip->ecc.read_page_raw = brcmnand_read_page_raw;
2293 chip->ecc.write_page_raw = brcmnand_write_page_raw;
2294 chip->ecc.write_oob_raw = brcmnand_write_oob_raw;
2295 chip->ecc.read_oob_raw = brcmnand_read_oob_raw;
2296 chip->ecc.read_oob = brcmnand_read_oob;
2297 chip->ecc.write_oob = brcmnand_write_oob;
2298
2299 chip->controller = &ctrl->controller;
2300
2301 /*
2302 * The bootloader might have configured 16bit mode but
2303 * NAND READID command only works in 8bit mode. We force
2304 * 8bit mode here to ensure that NAND READID commands works.
2305 */
2306 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2307 nand_writereg(ctrl, cfg_offs,
2308 nand_readreg(ctrl, cfg_offs) & ~CFG_BUS_WIDTH);
2309
2310 ret = nand_scan_ident(mtd, 1, NULL);
2311 if (ret)
2312 return ret;
2313
2314 chip->options |= NAND_NO_SUBPAGE_WRITE;
2315 /*
2316 * Avoid (for instance) kmap()'d buffers from JFFS2, which we can't DMA
2317 * to/from, and have nand_base pass us a bounce buffer instead, as
2318 * needed.
2319 */
2320 chip->options |= NAND_USE_BOUNCE_BUFFER;
2321
2322 if (chip->bbt_options & NAND_BBT_USE_FLASH)
2323 chip->bbt_options |= NAND_BBT_NO_OOB;
2324
2325 if (brcmnand_setup_dev(host))
2326 return -ENXIO;
2327
2328 chip->ecc.size = host->hwcfg.sector_size_1k ? 1024 : 512;
2329 /* only use our internal HW threshold */
2330 mtd->bitflip_threshold = 1;
2331
William Zhang1100e492019-09-04 10:51:13 -07002332 chip->ecc.layout = brcmstb_choose_ecc_layout(host);
2333 if (!chip->ecc.layout)
2334 return -ENXIO;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002335
2336 ret = nand_scan_tail(mtd);
2337 if (ret)
2338 return ret;
2339
2340#ifndef __UBOOT__
2341 ret = mtd_device_register(mtd, NULL, 0);
2342 if (ret)
2343 nand_cleanup(chip);
2344#else
2345 ret = nand_register(0, mtd);
2346#endif /* __UBOOT__ */
2347
2348 return ret;
2349}
2350
2351#ifndef __UBOOT__
2352static void brcmnand_save_restore_cs_config(struct brcmnand_host *host,
2353 int restore)
2354{
2355 struct brcmnand_controller *ctrl = host->ctrl;
2356 u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2357 u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
2358 BRCMNAND_CS_CFG_EXT);
2359 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
2360 BRCMNAND_CS_ACC_CONTROL);
2361 u16 t1_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING1);
2362 u16 t2_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING2);
2363
2364 if (restore) {
2365 nand_writereg(ctrl, cfg_offs, host->hwcfg.config);
2366 if (cfg_offs != cfg_ext_offs)
2367 nand_writereg(ctrl, cfg_ext_offs,
2368 host->hwcfg.config_ext);
2369 nand_writereg(ctrl, acc_control_offs, host->hwcfg.acc_control);
2370 nand_writereg(ctrl, t1_offs, host->hwcfg.timing_1);
2371 nand_writereg(ctrl, t2_offs, host->hwcfg.timing_2);
2372 } else {
2373 host->hwcfg.config = nand_readreg(ctrl, cfg_offs);
2374 if (cfg_offs != cfg_ext_offs)
2375 host->hwcfg.config_ext =
2376 nand_readreg(ctrl, cfg_ext_offs);
2377 host->hwcfg.acc_control = nand_readreg(ctrl, acc_control_offs);
2378 host->hwcfg.timing_1 = nand_readreg(ctrl, t1_offs);
2379 host->hwcfg.timing_2 = nand_readreg(ctrl, t2_offs);
2380 }
2381}
2382
2383static int brcmnand_suspend(struct device *dev)
2384{
2385 struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
2386 struct brcmnand_host *host;
2387
2388 list_for_each_entry(host, &ctrl->host_list, node)
2389 brcmnand_save_restore_cs_config(host, 0);
2390
2391 ctrl->nand_cs_nand_select = brcmnand_read_reg(ctrl, BRCMNAND_CS_SELECT);
2392 ctrl->nand_cs_nand_xor = brcmnand_read_reg(ctrl, BRCMNAND_CS_XOR);
2393 ctrl->corr_stat_threshold =
2394 brcmnand_read_reg(ctrl, BRCMNAND_CORR_THRESHOLD);
2395
2396 if (has_flash_dma(ctrl))
2397 ctrl->flash_dma_mode = flash_dma_readl(ctrl, FLASH_DMA_MODE);
2398
2399 return 0;
2400}
2401
2402static int brcmnand_resume(struct device *dev)
2403{
2404 struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
2405 struct brcmnand_host *host;
2406
2407 if (has_flash_dma(ctrl)) {
2408 flash_dma_writel(ctrl, FLASH_DMA_MODE, ctrl->flash_dma_mode);
2409 flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
2410 }
2411
2412 brcmnand_write_reg(ctrl, BRCMNAND_CS_SELECT, ctrl->nand_cs_nand_select);
2413 brcmnand_write_reg(ctrl, BRCMNAND_CS_XOR, ctrl->nand_cs_nand_xor);
2414 brcmnand_write_reg(ctrl, BRCMNAND_CORR_THRESHOLD,
2415 ctrl->corr_stat_threshold);
2416 if (ctrl->soc) {
2417 /* Clear/re-enable interrupt */
2418 ctrl->soc->ctlrdy_ack(ctrl->soc);
2419 ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
2420 }
2421
2422 list_for_each_entry(host, &ctrl->host_list, node) {
2423 struct nand_chip *chip = &host->chip;
2424
2425 brcmnand_save_restore_cs_config(host, 1);
2426
2427 /* Reset the chip, required by some chips after power-up */
2428 nand_reset_op(chip);
2429 }
2430
2431 return 0;
2432}
2433
2434const struct dev_pm_ops brcmnand_pm_ops = {
2435 .suspend = brcmnand_suspend,
2436 .resume = brcmnand_resume,
2437};
2438EXPORT_SYMBOL_GPL(brcmnand_pm_ops);
2439
2440static const struct of_device_id brcmnand_of_match[] = {
2441 { .compatible = "brcm,brcmnand-v4.0" },
2442 { .compatible = "brcm,brcmnand-v5.0" },
2443 { .compatible = "brcm,brcmnand-v6.0" },
2444 { .compatible = "brcm,brcmnand-v6.1" },
2445 { .compatible = "brcm,brcmnand-v6.2" },
2446 { .compatible = "brcm,brcmnand-v7.0" },
2447 { .compatible = "brcm,brcmnand-v7.1" },
2448 { .compatible = "brcm,brcmnand-v7.2" },
2449 {},
2450};
2451MODULE_DEVICE_TABLE(of, brcmnand_of_match);
2452#endif /* __UBOOT__ */
2453
2454/***********************************************************************
2455 * Platform driver setup (per controller)
2456 ***********************************************************************/
2457
2458#ifndef __UBOOT__
2459int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
2460#else
2461int brcmnand_probe(struct udevice *dev, struct brcmnand_soc *soc)
2462#endif /* __UBOOT__ */
2463{
2464#ifndef __UBOOT__
2465 struct device *dev = &pdev->dev;
2466 struct device_node *dn = dev->of_node, *child;
2467#else
2468 ofnode child;
2469 struct udevice *pdev = dev;
2470#endif /* __UBOOT__ */
2471 struct brcmnand_controller *ctrl;
2472#ifndef __UBOOT__
2473 struct resource *res;
2474#else
2475 struct resource res;
2476#endif /* __UBOOT__ */
2477 int ret;
2478
2479#ifndef __UBOOT__
2480 /* We only support device-tree instantiation */
2481 if (!dn)
2482 return -ENODEV;
2483
2484 if (!of_match_node(brcmnand_of_match, dn))
2485 return -ENODEV;
2486#endif /* __UBOOT__ */
2487
2488 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
2489 if (!ctrl)
2490 return -ENOMEM;
2491
2492#ifndef __UBOOT__
2493 dev_set_drvdata(dev, ctrl);
2494#else
2495 /*
2496 * in u-boot, the data for the driver is allocated before probing
2497 * so to keep the reference to ctrl, we store it in the variable soc
2498 */
2499 soc->ctrl = ctrl;
2500#endif /* __UBOOT__ */
2501 ctrl->dev = dev;
2502
2503 init_completion(&ctrl->done);
2504 init_completion(&ctrl->dma_done);
2505 nand_hw_control_init(&ctrl->controller);
2506 INIT_LIST_HEAD(&ctrl->host_list);
2507
Philippe Reynes7f28cf62019-03-15 15:14:37 +01002508 /* Is parameter page in big endian ? */
2509 ctrl->parameter_page_big_endian =
2510 dev_read_u32_default(dev, "parameter-page-big-endian", 1);
2511
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002512 /* NAND register range */
2513#ifndef __UBOOT__
2514 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2515 ctrl->nand_base = devm_ioremap_resource(dev, res);
2516#else
2517 dev_read_resource(pdev, 0, &res);
2518 ctrl->nand_base = devm_ioremap(pdev, res.start, resource_size(&res));
2519#endif
2520 if (IS_ERR(ctrl->nand_base))
2521 return PTR_ERR(ctrl->nand_base);
2522
2523 /* Enable clock before using NAND registers */
2524 ctrl->clk = devm_clk_get(dev, "nand");
2525 if (!IS_ERR(ctrl->clk)) {
2526 ret = clk_prepare_enable(ctrl->clk);
2527 if (ret)
2528 return ret;
2529 } else {
Simon Glass7ca47bc2021-01-24 14:32:41 -07002530 /* Ignore PTR_ERR(ctrl->clk) */
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002531 ctrl->clk = NULL;
2532 }
2533
2534 /* Initialize NAND revision */
2535 ret = brcmnand_revision_init(ctrl);
2536 if (ret)
2537 goto err;
2538
2539 /*
2540 * Most chips have this cache at a fixed offset within 'nand' block.
2541 * Some must specify this region separately.
2542 */
2543#ifndef __UBOOT__
2544 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand-cache");
2545 if (res) {
2546 ctrl->nand_fc = devm_ioremap_resource(dev, res);
2547 if (IS_ERR(ctrl->nand_fc)) {
2548 ret = PTR_ERR(ctrl->nand_fc);
2549 goto err;
2550 }
2551 } else {
2552 ctrl->nand_fc = ctrl->nand_base +
2553 ctrl->reg_offsets[BRCMNAND_FC_BASE];
2554 }
2555#else
2556 if (!dev_read_resource_byname(pdev, "nand-cache", &res)) {
2557 ctrl->nand_fc = devm_ioremap(dev, res.start,
2558 resource_size(&res));
2559 if (IS_ERR(ctrl->nand_fc)) {
2560 ret = PTR_ERR(ctrl->nand_fc);
2561 goto err;
2562 }
2563 } else {
2564 ctrl->nand_fc = ctrl->nand_base +
2565 ctrl->reg_offsets[BRCMNAND_FC_BASE];
2566 }
2567#endif
2568
2569#ifndef __UBOOT__
2570 /* FLASH_DMA */
2571 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash-dma");
2572 if (res) {
2573 ctrl->flash_dma_base = devm_ioremap_resource(dev, res);
2574 if (IS_ERR(ctrl->flash_dma_base)) {
2575 ret = PTR_ERR(ctrl->flash_dma_base);
2576 goto err;
2577 }
2578
2579 flash_dma_writel(ctrl, FLASH_DMA_MODE, 1); /* linked-list */
2580 flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
2581
2582 /* Allocate descriptor(s) */
2583 ctrl->dma_desc = dmam_alloc_coherent(dev,
2584 sizeof(*ctrl->dma_desc),
2585 &ctrl->dma_pa, GFP_KERNEL);
2586 if (!ctrl->dma_desc) {
2587 ret = -ENOMEM;
2588 goto err;
2589 }
2590
2591 ctrl->dma_irq = platform_get_irq(pdev, 1);
2592 if ((int)ctrl->dma_irq < 0) {
2593 dev_err(dev, "missing FLASH_DMA IRQ\n");
2594 ret = -ENODEV;
2595 goto err;
2596 }
2597
2598 ret = devm_request_irq(dev, ctrl->dma_irq,
2599 brcmnand_dma_irq, 0, DRV_NAME,
2600 ctrl);
2601 if (ret < 0) {
2602 dev_err(dev, "can't allocate IRQ %d: error %d\n",
2603 ctrl->dma_irq, ret);
2604 goto err;
2605 }
2606
2607 dev_info(dev, "enabling FLASH_DMA\n");
2608 }
2609#endif /* __UBOOT__ */
2610
2611 /* Disable automatic device ID config, direct addressing */
2612 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT,
2613 CS_SELECT_AUTO_DEVICE_ID_CFG | 0xff, 0, 0);
2614 /* Disable XOR addressing */
2615 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
2616
Philippe Reynes77669af2019-03-15 15:14:38 +01002617 /* Read the write-protect configuration in the device tree */
2618 wp_on = dev_read_u32_default(dev, "write-protect", wp_on);
2619
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002620 if (ctrl->features & BRCMNAND_HAS_WP) {
2621 /* Permanently disable write protection */
2622 if (wp_on == 2)
2623 brcmnand_set_wp(ctrl, false);
2624 } else {
2625 wp_on = 0;
2626 }
2627
2628#ifndef __UBOOT__
2629 /* IRQ */
2630 ctrl->irq = platform_get_irq(pdev, 0);
2631 if ((int)ctrl->irq < 0) {
2632 dev_err(dev, "no IRQ defined\n");
2633 ret = -ENODEV;
2634 goto err;
2635 }
2636
2637 /*
2638 * Some SoCs integrate this controller (e.g., its interrupt bits) in
2639 * interesting ways
2640 */
2641 if (soc) {
2642 ctrl->soc = soc;
2643
2644 ret = devm_request_irq(dev, ctrl->irq, brcmnand_irq, 0,
2645 DRV_NAME, ctrl);
2646
2647 /* Enable interrupt */
2648 ctrl->soc->ctlrdy_ack(ctrl->soc);
2649 ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
2650 } else {
2651 /* Use standard interrupt infrastructure */
2652 ret = devm_request_irq(dev, ctrl->irq, brcmnand_ctlrdy_irq, 0,
2653 DRV_NAME, ctrl);
2654 }
2655 if (ret < 0) {
2656 dev_err(dev, "can't allocate IRQ %d: error %d\n",
2657 ctrl->irq, ret);
2658 goto err;
2659 }
2660#endif /* __UBOOT__ */
2661
2662#ifndef __UBOOT__
2663 for_each_available_child_of_node(dn, child) {
2664 if (of_device_is_compatible(child, "brcm,nandcs")) {
2665 struct brcmnand_host *host;
2666
2667 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2668 if (!host) {
2669 of_node_put(child);
2670 ret = -ENOMEM;
2671 goto err;
2672 }
2673 host->pdev = pdev;
2674 host->ctrl = ctrl;
2675
2676 ret = brcmnand_init_cs(host, child);
2677 if (ret) {
2678 devm_kfree(dev, host);
2679 continue; /* Try all chip-selects */
2680 }
2681
2682 list_add_tail(&host->node, &ctrl->host_list);
2683 }
2684 }
2685#else
2686 ofnode_for_each_subnode(child, dev_ofnode(dev)) {
2687 if (ofnode_device_is_compatible(child, "brcm,nandcs")) {
2688 struct brcmnand_host *host;
2689
2690 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2691 if (!host) {
2692 ret = -ENOMEM;
2693 goto err;
2694 }
2695 host->pdev = pdev;
2696 host->ctrl = ctrl;
2697
2698 ret = brcmnand_init_cs(host, child);
2699 if (ret) {
2700 devm_kfree(dev, host);
2701 continue; /* Try all chip-selects */
2702 }
2703
2704 list_add_tail(&host->node, &ctrl->host_list);
2705 }
2706 }
2707#endif /* __UBOOT__ */
2708
Álvaro Fernández Rojasd8ae8752020-04-02 10:37:52 +02002709 /* No chip-selects could initialize properly */
2710 if (list_empty(&ctrl->host_list)) {
2711 ret = -ENODEV;
2712 goto err;
2713 }
2714
2715 return 0;
2716
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002717err:
2718#ifndef __UBOOT__
2719 clk_disable_unprepare(ctrl->clk);
2720#else
2721 if (ctrl->clk)
2722 clk_disable(ctrl->clk);
2723#endif /* __UBOOT__ */
2724 return ret;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002725}
2726EXPORT_SYMBOL_GPL(brcmnand_probe);
2727
2728#ifndef __UBOOT__
2729int brcmnand_remove(struct platform_device *pdev)
2730{
2731 struct brcmnand_controller *ctrl = dev_get_drvdata(&pdev->dev);
2732 struct brcmnand_host *host;
2733
2734 list_for_each_entry(host, &ctrl->host_list, node)
2735 nand_release(nand_to_mtd(&host->chip));
2736
2737 clk_disable_unprepare(ctrl->clk);
2738
2739 dev_set_drvdata(&pdev->dev, NULL);
2740
2741 return 0;
2742}
2743#else
2744int brcmnand_remove(struct udevice *pdev)
2745{
2746 return 0;
2747}
2748#endif /* __UBOOT__ */
2749EXPORT_SYMBOL_GPL(brcmnand_remove);
2750
2751MODULE_LICENSE("GPL v2");
2752MODULE_AUTHOR("Kevin Cernekee");
2753MODULE_AUTHOR("Brian Norris");
2754MODULE_DESCRIPTION("NAND driver for Broadcom chips");
2755MODULE_ALIAS("platform:brcmnand");