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