blob: 700d1122639f0cf9c7f08996866bb6b8cde7ed98 [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
1014 dev_warn(ctrl->dev, "timeout on status poll (expected %x got %x)\n",
1015 expected_val, val & mask);
1016
1017 return -ETIMEDOUT;
1018}
1019
1020static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
1021{
1022 u32 val = en ? CS_SELECT_NAND_WP : 0;
1023
1024 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
1025}
1026
1027/***********************************************************************
1028 * Flash DMA
1029 ***********************************************************************/
1030
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001031static inline bool has_flash_dma(struct brcmnand_controller *ctrl)
1032{
1033 return ctrl->flash_dma_base;
1034}
1035
1036static inline bool flash_dma_buf_ok(const void *buf)
1037{
1038#ifndef __UBOOT__
1039 return buf && !is_vmalloc_addr(buf) &&
1040 likely(IS_ALIGNED((uintptr_t)buf, 4));
1041#else
1042 return buf && likely(IS_ALIGNED((uintptr_t)buf, 4));
1043#endif /* __UBOOT__ */
1044}
1045
Kamal Dasuf47b36b2023-02-11 16:29:01 +01001046static inline void flash_dma_writel(struct brcmnand_controller *ctrl,
1047 enum flash_dma_reg dma_reg, u32 val)
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001048{
Kamal Dasuf47b36b2023-02-11 16:29:01 +01001049 u16 offs = ctrl->flash_dma_offsets[dma_reg];
1050
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001051 brcmnand_writel(val, ctrl->flash_dma_base + offs);
1052}
1053
Kamal Dasuf47b36b2023-02-11 16:29:01 +01001054static inline u32 flash_dma_readl(struct brcmnand_controller *ctrl,
1055 enum flash_dma_reg dma_reg)
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 return brcmnand_readl(ctrl->flash_dma_base + offs);
1060}
1061
1062/* Low-level operation types: command, address, write, or read */
1063enum brcmnand_llop_type {
1064 LL_OP_CMD,
1065 LL_OP_ADDR,
1066 LL_OP_WR,
1067 LL_OP_RD,
1068};
1069
1070/***********************************************************************
1071 * Internal support functions
1072 ***********************************************************************/
1073
1074static inline bool is_hamming_ecc(struct brcmnand_controller *ctrl,
1075 struct brcmnand_cfg *cfg)
1076{
1077 if (ctrl->nand_version <= 0x0701)
1078 return cfg->sector_size_1k == 0 && cfg->spare_area_size == 16 &&
1079 cfg->ecc_level == 15;
1080 else
1081 return cfg->sector_size_1k == 0 && ((cfg->spare_area_size == 16 &&
1082 cfg->ecc_level == 15) ||
1083 (cfg->spare_area_size == 28 && cfg->ecc_level == 16));
1084}
1085
1086/*
William Zhang1100e492019-09-04 10:51:13 -07001087 * Returns a nand_ecclayout strucutre for the given layout/configuration.
1088 * Returns NULL on failure.
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001089 */
William Zhang1100e492019-09-04 10:51:13 -07001090static struct nand_ecclayout *brcmnand_create_layout(int ecc_level,
1091 struct brcmnand_host *host)
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001092{
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001093 struct brcmnand_cfg *cfg = &host->hwcfg;
William Zhang1100e492019-09-04 10:51:13 -07001094 int i, j;
1095 struct nand_ecclayout *layout;
1096 int req;
1097 int sectors;
1098 int sas;
1099 int idx1, idx2;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001100
William Zhang1100e492019-09-04 10:51:13 -07001101#ifndef __UBOOT__
1102 layout = devm_kzalloc(&host->pdev->dev, sizeof(*layout), GFP_KERNEL);
1103#else
1104 layout = devm_kzalloc(host->pdev, sizeof(*layout), GFP_KERNEL);
1105#endif
1106 if (!layout)
1107 return NULL;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001108
William Zhang1100e492019-09-04 10:51:13 -07001109 sectors = cfg->page_size / (512 << cfg->sector_size_1k);
1110 sas = cfg->spare_area_size << cfg->sector_size_1k;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001111
William Zhang1100e492019-09-04 10:51:13 -07001112 /* Hamming */
1113 if (is_hamming_ecc(host->ctrl, cfg)) {
1114 for (i = 0, idx1 = 0, idx2 = 0; i < sectors; i++) {
1115 /* First sector of each page may have BBI */
1116 if (i == 0) {
1117 layout->oobfree[idx2].offset = i * sas + 1;
1118 /* Small-page NAND use byte 6 for BBI */
1119 if (cfg->page_size == 512)
1120 layout->oobfree[idx2].offset--;
1121 layout->oobfree[idx2].length = 5;
1122 } else {
1123 layout->oobfree[idx2].offset = i * sas;
1124 layout->oobfree[idx2].length = 6;
1125 }
1126 idx2++;
1127 layout->eccpos[idx1++] = i * sas + 6;
1128 layout->eccpos[idx1++] = i * sas + 7;
1129 layout->eccpos[idx1++] = i * sas + 8;
1130 layout->oobfree[idx2].offset = i * sas + 9;
1131 layout->oobfree[idx2].length = 7;
1132 idx2++;
1133 /* Leave zero-terminated entry for OOBFREE */
1134 if (idx1 >= MTD_MAX_ECCPOS_ENTRIES_LARGE ||
1135 idx2 >= MTD_MAX_OOBFREE_ENTRIES_LARGE - 1)
1136 break;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001137 }
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001138
William Zhang1100e492019-09-04 10:51:13 -07001139 return layout;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001140 }
1141
William Zhang1100e492019-09-04 10:51:13 -07001142 /*
1143 * CONTROLLER_VERSION:
1144 * < v5.0: ECC_REQ = ceil(BCH_T * 13/8)
1145 * >= v5.0: ECC_REQ = ceil(BCH_T * 14/8)
1146 * But we will just be conservative.
1147 */
1148 req = DIV_ROUND_UP(ecc_level * 14, 8);
1149 if (req >= sas) {
Sean Anderson4b5aa002020-09-15 10:44:50 -04001150 dev_err(host->pdev,
William Zhang1100e492019-09-04 10:51:13 -07001151 "error: ECC too large for OOB (ECC bytes %d, spare sector %d)\n",
1152 req, sas);
1153 return NULL;
1154 }
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001155
William Zhang1100e492019-09-04 10:51:13 -07001156 layout->eccbytes = req * sectors;
1157 for (i = 0, idx1 = 0, idx2 = 0; i < sectors; i++) {
1158 for (j = sas - req; j < sas && idx1 <
1159 MTD_MAX_ECCPOS_ENTRIES_LARGE; j++, idx1++)
1160 layout->eccpos[idx1] = i * sas + j;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001161
William Zhang1100e492019-09-04 10:51:13 -07001162 /* First sector of each page may have BBI */
1163 if (i == 0) {
1164 if (cfg->page_size == 512 && (sas - req >= 6)) {
1165 /* Small-page NAND use byte 6 for BBI */
1166 layout->oobfree[idx2].offset = 0;
1167 layout->oobfree[idx2].length = 5;
1168 idx2++;
1169 if (sas - req > 6) {
1170 layout->oobfree[idx2].offset = 6;
1171 layout->oobfree[idx2].length =
1172 sas - req - 6;
1173 idx2++;
1174 }
1175 } else if (sas > req + 1) {
1176 layout->oobfree[idx2].offset = i * sas + 1;
1177 layout->oobfree[idx2].length = sas - req - 1;
1178 idx2++;
1179 }
1180 } else if (sas > req) {
1181 layout->oobfree[idx2].offset = i * sas;
1182 layout->oobfree[idx2].length = sas - req;
1183 idx2++;
1184 }
1185 /* Leave zero-terminated entry for OOBFREE */
1186 if (idx1 >= MTD_MAX_ECCPOS_ENTRIES_LARGE ||
1187 idx2 >= MTD_MAX_OOBFREE_ENTRIES_LARGE - 1)
1188 break;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001189 }
1190
William Zhang1100e492019-09-04 10:51:13 -07001191 return layout;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001192}
1193
William Zhang1100e492019-09-04 10:51:13 -07001194static struct nand_ecclayout *brcmstb_choose_ecc_layout(
1195 struct brcmnand_host *host)
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001196{
William Zhang1100e492019-09-04 10:51:13 -07001197 struct nand_ecclayout *layout;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001198 struct brcmnand_cfg *p = &host->hwcfg;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001199 unsigned int ecc_level = p->ecc_level;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001200
1201 if (p->sector_size_1k)
1202 ecc_level <<= 1;
1203
William Zhang1100e492019-09-04 10:51:13 -07001204 layout = brcmnand_create_layout(ecc_level, host);
1205 if (!layout) {
Sean Anderson4b5aa002020-09-15 10:44:50 -04001206 dev_err(host->pdev,
1207 "no proper ecc_layout for this NAND cfg\n");
William Zhang1100e492019-09-04 10:51:13 -07001208 return NULL;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001209 }
1210
William Zhang1100e492019-09-04 10:51:13 -07001211 return layout;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001212}
1213
1214static void brcmnand_wp(struct mtd_info *mtd, int wp)
1215{
1216 struct nand_chip *chip = mtd_to_nand(mtd);
1217 struct brcmnand_host *host = nand_get_controller_data(chip);
1218 struct brcmnand_controller *ctrl = host->ctrl;
1219
1220 if ((ctrl->features & BRCMNAND_HAS_WP) && wp_on == 1) {
1221 static int old_wp = -1;
1222 int ret;
1223
1224 if (old_wp != wp) {
1225 dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
1226 old_wp = wp;
1227 }
1228
1229 /*
1230 * make sure ctrl/flash ready before and after
1231 * changing state of #WP pin
1232 */
1233 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY |
1234 NAND_STATUS_READY,
1235 NAND_CTRL_RDY |
1236 NAND_STATUS_READY, 0);
1237 if (ret)
1238 return;
1239
1240 brcmnand_set_wp(ctrl, wp);
1241 nand_status_op(chip, NULL);
1242 /* NAND_STATUS_WP 0x00 = protected, 0x80 = not protected */
1243 ret = bcmnand_ctrl_poll_status(ctrl,
1244 NAND_CTRL_RDY |
1245 NAND_STATUS_READY |
1246 NAND_STATUS_WP,
1247 NAND_CTRL_RDY |
1248 NAND_STATUS_READY |
1249 (wp ? 0 : NAND_STATUS_WP), 0);
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001250 if (ret)
Sean Anderson4b5aa002020-09-15 10:44:50 -04001251 dev_err(host->pdev, "nand #WP expected %s\n",
1252 wp ? "on" : "off");
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001253 }
1254}
1255
1256/* Helper functions for reading and writing OOB registers */
1257static inline u8 oob_reg_read(struct brcmnand_controller *ctrl, u32 offs)
1258{
1259 u16 offset0, offset10, reg_offs;
1260
1261 offset0 = ctrl->reg_offsets[BRCMNAND_OOB_READ_BASE];
1262 offset10 = ctrl->reg_offsets[BRCMNAND_OOB_READ_10_BASE];
1263
1264 if (offs >= ctrl->max_oob)
1265 return 0x77;
1266
1267 if (offs >= 16 && offset10)
1268 reg_offs = offset10 + ((offs - 0x10) & ~0x03);
1269 else
1270 reg_offs = offset0 + (offs & ~0x03);
1271
1272 return nand_readreg(ctrl, reg_offs) >> (24 - ((offs & 0x03) << 3));
1273}
1274
1275static inline void oob_reg_write(struct brcmnand_controller *ctrl, u32 offs,
1276 u32 data)
1277{
1278 u16 offset0, offset10, reg_offs;
1279
1280 offset0 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_BASE];
1281 offset10 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_10_BASE];
1282
1283 if (offs >= ctrl->max_oob)
1284 return;
1285
1286 if (offs >= 16 && offset10)
1287 reg_offs = offset10 + ((offs - 0x10) & ~0x03);
1288 else
1289 reg_offs = offset0 + (offs & ~0x03);
1290
1291 nand_writereg(ctrl, reg_offs, data);
1292}
1293
1294/*
1295 * read_oob_from_regs - read data from OOB registers
1296 * @ctrl: NAND controller
1297 * @i: sub-page sector index
1298 * @oob: buffer to read to
1299 * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
1300 * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
1301 */
1302static int read_oob_from_regs(struct brcmnand_controller *ctrl, int i, u8 *oob,
1303 int sas, int sector_1k)
1304{
1305 int tbytes = sas << sector_1k;
1306 int j;
1307
1308 /* Adjust OOB values for 1K sector size */
1309 if (sector_1k && (i & 0x01))
1310 tbytes = max(0, tbytes - (int)ctrl->max_oob);
1311 tbytes = min_t(int, tbytes, ctrl->max_oob);
1312
1313 for (j = 0; j < tbytes; j++)
1314 oob[j] = oob_reg_read(ctrl, j);
1315 return tbytes;
1316}
1317
1318/*
1319 * write_oob_to_regs - write data to OOB registers
1320 * @i: sub-page sector index
1321 * @oob: buffer to write from
1322 * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
1323 * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
1324 */
1325static int write_oob_to_regs(struct brcmnand_controller *ctrl, int i,
1326 const u8 *oob, int sas, int sector_1k)
1327{
1328 int tbytes = sas << sector_1k;
1329 int j;
1330
1331 /* Adjust OOB values for 1K sector size */
1332 if (sector_1k && (i & 0x01))
1333 tbytes = max(0, tbytes - (int)ctrl->max_oob);
1334 tbytes = min_t(int, tbytes, ctrl->max_oob);
1335
1336 for (j = 0; j < tbytes; j += 4)
1337 oob_reg_write(ctrl, j,
1338 (oob[j + 0] << 24) |
1339 (oob[j + 1] << 16) |
1340 (oob[j + 2] << 8) |
1341 (oob[j + 3] << 0));
1342 return tbytes;
1343}
1344
1345#ifndef __UBOOT__
1346static irqreturn_t brcmnand_ctlrdy_irq(int irq, void *data)
1347{
1348 struct brcmnand_controller *ctrl = data;
1349
1350 /* Discard all NAND_CTLRDY interrupts during DMA */
1351 if (ctrl->dma_pending)
1352 return IRQ_HANDLED;
1353
1354 complete(&ctrl->done);
1355 return IRQ_HANDLED;
1356}
1357
1358/* Handle SoC-specific interrupt hardware */
1359static irqreturn_t brcmnand_irq(int irq, void *data)
1360{
1361 struct brcmnand_controller *ctrl = data;
1362
1363 if (ctrl->soc->ctlrdy_ack(ctrl->soc))
1364 return brcmnand_ctlrdy_irq(irq, data);
1365
1366 return IRQ_NONE;
1367}
1368
1369static irqreturn_t brcmnand_dma_irq(int irq, void *data)
1370{
1371 struct brcmnand_controller *ctrl = data;
1372
1373 complete(&ctrl->dma_done);
1374
1375 return IRQ_HANDLED;
1376}
1377#endif /* __UBOOT__ */
1378
1379static void brcmnand_send_cmd(struct brcmnand_host *host, int cmd)
1380{
1381 struct brcmnand_controller *ctrl = host->ctrl;
1382 int ret;
Kamal Dasu299c6832023-02-11 16:29:00 +01001383 u64 cmd_addr;
1384
1385 cmd_addr = brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
1386
1387 dev_dbg(ctrl->dev, "send native cmd %d addr 0x%llx\n", cmd, cmd_addr);
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001388
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001389 BUG_ON(ctrl->cmd_pending != 0);
1390 ctrl->cmd_pending = cmd;
1391
1392 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, 0);
1393 WARN_ON(ret);
1394
1395 mb(); /* flush previous writes */
1396 brcmnand_write_reg(ctrl, BRCMNAND_CMD_START,
1397 cmd << brcmnand_cmd_shift(ctrl));
1398}
1399
1400/***********************************************************************
1401 * NAND MTD API: read/program/erase
1402 ***********************************************************************/
1403
1404static void brcmnand_cmd_ctrl(struct mtd_info *mtd, int dat,
1405 unsigned int ctrl)
1406{
1407 /* intentionally left blank */
1408}
1409
1410static int brcmnand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1411{
1412 struct nand_chip *chip = mtd_to_nand(mtd);
1413 struct brcmnand_host *host = nand_get_controller_data(chip);
1414 struct brcmnand_controller *ctrl = host->ctrl;
1415
1416#ifndef __UBOOT__
1417 unsigned long timeo = msecs_to_jiffies(100);
1418
1419 dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending);
1420 if (ctrl->cmd_pending &&
1421 wait_for_completion_timeout(&ctrl->done, timeo) <= 0) {
1422 u32 cmd = brcmnand_read_reg(ctrl, BRCMNAND_CMD_START)
1423 >> brcmnand_cmd_shift(ctrl);
1424
1425 dev_err_ratelimited(ctrl->dev,
1426 "timeout waiting for command %#02x\n", cmd);
1427 dev_err_ratelimited(ctrl->dev, "intfc status %08x\n",
1428 brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS));
1429 }
1430#else
1431 unsigned long timeo = 100; /* 100 msec */
1432 int ret;
1433
1434 dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending);
1435
1436 ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, timeo);
1437 WARN_ON(ret);
1438#endif /* __UBOOT__ */
1439
1440 ctrl->cmd_pending = 0;
1441 return brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
1442 INTFC_FLASH_STATUS;
1443}
1444
1445enum {
1446 LLOP_RE = BIT(16),
1447 LLOP_WE = BIT(17),
1448 LLOP_ALE = BIT(18),
1449 LLOP_CLE = BIT(19),
1450 LLOP_RETURN_IDLE = BIT(31),
1451
1452 LLOP_DATA_MASK = GENMASK(15, 0),
1453};
1454
1455static int brcmnand_low_level_op(struct brcmnand_host *host,
1456 enum brcmnand_llop_type type, u32 data,
1457 bool last_op)
1458{
1459 struct mtd_info *mtd = nand_to_mtd(&host->chip);
1460 struct nand_chip *chip = &host->chip;
1461 struct brcmnand_controller *ctrl = host->ctrl;
1462 u32 tmp;
1463
1464 tmp = data & LLOP_DATA_MASK;
1465 switch (type) {
1466 case LL_OP_CMD:
1467 tmp |= LLOP_WE | LLOP_CLE;
1468 break;
1469 case LL_OP_ADDR:
1470 /* WE | ALE */
1471 tmp |= LLOP_WE | LLOP_ALE;
1472 break;
1473 case LL_OP_WR:
1474 /* WE */
1475 tmp |= LLOP_WE;
1476 break;
1477 case LL_OP_RD:
1478 /* RE */
1479 tmp |= LLOP_RE;
1480 break;
1481 }
1482 if (last_op)
1483 /* RETURN_IDLE */
1484 tmp |= LLOP_RETURN_IDLE;
1485
1486 dev_dbg(ctrl->dev, "ll_op cmd %#x\n", tmp);
1487
1488 brcmnand_write_reg(ctrl, BRCMNAND_LL_OP, tmp);
1489 (void)brcmnand_read_reg(ctrl, BRCMNAND_LL_OP);
1490
1491 brcmnand_send_cmd(host, CMD_LOW_LEVEL_OP);
1492 return brcmnand_waitfunc(mtd, chip);
1493}
1494
1495static void brcmnand_cmdfunc(struct mtd_info *mtd, unsigned command,
1496 int column, int page_addr)
1497{
1498 struct nand_chip *chip = mtd_to_nand(mtd);
1499 struct brcmnand_host *host = nand_get_controller_data(chip);
1500 struct brcmnand_controller *ctrl = host->ctrl;
1501 u64 addr = (u64)page_addr << chip->page_shift;
1502 int native_cmd = 0;
1503
1504 if (command == NAND_CMD_READID || command == NAND_CMD_PARAM ||
1505 command == NAND_CMD_RNDOUT)
1506 addr = (u64)column;
1507 /* Avoid propagating a negative, don't-care address */
1508 else if (page_addr < 0)
1509 addr = 0;
1510
1511 dev_dbg(ctrl->dev, "cmd 0x%x addr 0x%llx\n", command,
1512 (unsigned long long)addr);
1513
1514 host->last_cmd = command;
1515 host->last_byte = 0;
1516 host->last_addr = addr;
1517
1518 switch (command) {
1519 case NAND_CMD_RESET:
1520 native_cmd = CMD_FLASH_RESET;
1521 break;
1522 case NAND_CMD_STATUS:
1523 native_cmd = CMD_STATUS_READ;
1524 break;
1525 case NAND_CMD_READID:
1526 native_cmd = CMD_DEVICE_ID_READ;
1527 break;
1528 case NAND_CMD_READOOB:
1529 native_cmd = CMD_SPARE_AREA_READ;
1530 break;
1531 case NAND_CMD_ERASE1:
1532 native_cmd = CMD_BLOCK_ERASE;
1533 brcmnand_wp(mtd, 0);
1534 break;
1535 case NAND_CMD_PARAM:
1536 native_cmd = CMD_PARAMETER_READ;
1537 break;
1538 case NAND_CMD_SET_FEATURES:
1539 case NAND_CMD_GET_FEATURES:
1540 brcmnand_low_level_op(host, LL_OP_CMD, command, false);
1541 brcmnand_low_level_op(host, LL_OP_ADDR, column, false);
1542 break;
1543 case NAND_CMD_RNDOUT:
1544 native_cmd = CMD_PARAMETER_CHANGE_COL;
1545 addr &= ~((u64)(FC_BYTES - 1));
1546 /*
1547 * HW quirk: PARAMETER_CHANGE_COL requires SECTOR_SIZE_1K=0
1548 * NB: hwcfg.sector_size_1k may not be initialized yet
1549 */
1550 if (brcmnand_get_sector_size_1k(host)) {
1551 host->hwcfg.sector_size_1k =
1552 brcmnand_get_sector_size_1k(host);
1553 brcmnand_set_sector_size_1k(host, 0);
1554 }
1555 break;
1556 }
1557
1558 if (!native_cmd)
1559 return;
1560
Kamal Dasu299c6832023-02-11 16:29:00 +01001561 brcmnand_set_cmd_addr(mtd, addr);
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001562 brcmnand_send_cmd(host, native_cmd);
1563 brcmnand_waitfunc(mtd, chip);
1564
1565 if (native_cmd == CMD_PARAMETER_READ ||
1566 native_cmd == CMD_PARAMETER_CHANGE_COL) {
1567 /* Copy flash cache word-wise */
1568 u32 *flash_cache = (u32 *)ctrl->flash_cache;
1569 int i;
1570
1571 brcmnand_soc_data_bus_prepare(ctrl->soc, true);
1572
1573 /*
1574 * Must cache the FLASH_CACHE now, since changes in
1575 * SECTOR_SIZE_1K may invalidate it
1576 */
Philippe Reynes7f28cf62019-03-15 15:14:37 +01001577 for (i = 0; i < FC_WORDS; i++) {
1578 u32 fc;
1579
1580 fc = brcmnand_read_fc(ctrl, i);
1581
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001582 /*
1583 * Flash cache is big endian for parameter pages, at
1584 * least on STB SoCs
1585 */
Philippe Reynes7f28cf62019-03-15 15:14:37 +01001586 if (ctrl->parameter_page_big_endian)
1587 flash_cache[i] = be32_to_cpu(fc);
1588 else
1589 flash_cache[i] = le32_to_cpu(fc);
1590 }
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001591
1592 brcmnand_soc_data_bus_unprepare(ctrl->soc, true);
1593
1594 /* Cleanup from HW quirk: restore SECTOR_SIZE_1K */
1595 if (host->hwcfg.sector_size_1k)
1596 brcmnand_set_sector_size_1k(host,
1597 host->hwcfg.sector_size_1k);
1598 }
1599
1600 /* Re-enable protection is necessary only after erase */
1601 if (command == NAND_CMD_ERASE1)
1602 brcmnand_wp(mtd, 1);
1603}
1604
1605static uint8_t brcmnand_read_byte(struct mtd_info *mtd)
1606{
1607 struct nand_chip *chip = mtd_to_nand(mtd);
1608 struct brcmnand_host *host = nand_get_controller_data(chip);
1609 struct brcmnand_controller *ctrl = host->ctrl;
1610 uint8_t ret = 0;
1611 int addr, offs;
1612
1613 switch (host->last_cmd) {
1614 case NAND_CMD_READID:
1615 if (host->last_byte < 4)
1616 ret = brcmnand_read_reg(ctrl, BRCMNAND_ID) >>
1617 (24 - (host->last_byte << 3));
1618 else if (host->last_byte < 8)
1619 ret = brcmnand_read_reg(ctrl, BRCMNAND_ID_EXT) >>
1620 (56 - (host->last_byte << 3));
1621 break;
1622
1623 case NAND_CMD_READOOB:
1624 ret = oob_reg_read(ctrl, host->last_byte);
1625 break;
1626
1627 case NAND_CMD_STATUS:
1628 ret = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
1629 INTFC_FLASH_STATUS;
1630 if (wp_on) /* hide WP status */
1631 ret |= NAND_STATUS_WP;
1632 break;
1633
1634 case NAND_CMD_PARAM:
1635 case NAND_CMD_RNDOUT:
1636 addr = host->last_addr + host->last_byte;
1637 offs = addr & (FC_BYTES - 1);
1638
1639 /* At FC_BYTES boundary, switch to next column */
1640 if (host->last_byte > 0 && offs == 0)
1641 nand_change_read_column_op(chip, addr, NULL, 0, false);
1642
1643 ret = ctrl->flash_cache[offs];
1644 break;
1645 case NAND_CMD_GET_FEATURES:
1646 if (host->last_byte >= ONFI_SUBFEATURE_PARAM_LEN) {
1647 ret = 0;
1648 } else {
1649 bool last = host->last_byte ==
1650 ONFI_SUBFEATURE_PARAM_LEN - 1;
1651 brcmnand_low_level_op(host, LL_OP_RD, 0, last);
1652 ret = brcmnand_read_reg(ctrl, BRCMNAND_LL_RDATA) & 0xff;
1653 }
1654 }
1655
1656 dev_dbg(ctrl->dev, "read byte = 0x%02x\n", ret);
1657 host->last_byte++;
1658
1659 return ret;
1660}
1661
1662static void brcmnand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1663{
1664 int i;
1665
1666 for (i = 0; i < len; i++, buf++)
1667 *buf = brcmnand_read_byte(mtd);
1668}
1669
1670static void brcmnand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
1671 int len)
1672{
1673 int i;
1674 struct nand_chip *chip = mtd_to_nand(mtd);
1675 struct brcmnand_host *host = nand_get_controller_data(chip);
1676
1677 switch (host->last_cmd) {
1678 case NAND_CMD_SET_FEATURES:
1679 for (i = 0; i < len; i++)
1680 brcmnand_low_level_op(host, LL_OP_WR, buf[i],
1681 (i + 1) == len);
1682 break;
1683 default:
1684 BUG();
1685 break;
1686 }
1687}
1688
1689/**
1690 * Construct a FLASH_DMA descriptor as part of a linked list. You must know the
1691 * following ahead of time:
1692 * - Is this descriptor the beginning or end of a linked list?
1693 * - What is the (DMA) address of the next descriptor in the linked list?
1694 */
1695#ifndef __UBOOT__
1696static int brcmnand_fill_dma_desc(struct brcmnand_host *host,
1697 struct brcm_nand_dma_desc *desc, u64 addr,
1698 dma_addr_t buf, u32 len, u8 dma_cmd,
1699 bool begin, bool end,
1700 dma_addr_t next_desc)
1701{
1702 memset(desc, 0, sizeof(*desc));
1703 /* Descriptors are written in native byte order (wordwise) */
1704 desc->next_desc = lower_32_bits(next_desc);
1705 desc->next_desc_ext = upper_32_bits(next_desc);
1706 desc->cmd_irq = (dma_cmd << 24) |
1707 (end ? (0x03 << 8) : 0) | /* IRQ | STOP */
1708 (!!begin) | ((!!end) << 1); /* head, tail */
Jiaxun Yang0803a272024-07-17 16:07:03 +08001709#ifdef CONFIG_SYS_BIG_ENDIAN
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001710 desc->cmd_irq |= 0x01 << 12;
1711#endif
1712 desc->dram_addr = lower_32_bits(buf);
1713 desc->dram_addr_ext = upper_32_bits(buf);
1714 desc->tfr_len = len;
1715 desc->total_len = len;
1716 desc->flash_addr = lower_32_bits(addr);
1717 desc->flash_addr_ext = upper_32_bits(addr);
1718 desc->cs = host->cs;
1719 desc->status_valid = 0x01;
1720 return 0;
1721}
1722
1723/**
1724 * Kick the FLASH_DMA engine, with a given DMA descriptor
1725 */
1726static void brcmnand_dma_run(struct brcmnand_host *host, dma_addr_t desc)
1727{
1728 struct brcmnand_controller *ctrl = host->ctrl;
1729 unsigned long timeo = msecs_to_jiffies(100);
1730
1731 flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC, lower_32_bits(desc));
1732 (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC);
Kamal Dasub6233f72023-02-11 16:29:03 +01001733 if (ctrl->nand_version > 0x0602) {
1734 flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC_EXT,
1735 upper_32_bits(desc));
1736 (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC_EXT);
1737 }
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001738
1739 /* Start FLASH_DMA engine */
1740 ctrl->dma_pending = true;
1741 mb(); /* flush previous writes */
1742 flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0x03); /* wake | run */
1743
1744 if (wait_for_completion_timeout(&ctrl->dma_done, timeo) <= 0) {
1745 dev_err(ctrl->dev,
1746 "timeout waiting for DMA; status %#x, error status %#x\n",
1747 flash_dma_readl(ctrl, FLASH_DMA_STATUS),
1748 flash_dma_readl(ctrl, FLASH_DMA_ERROR_STATUS));
1749 }
1750 ctrl->dma_pending = false;
1751 flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0); /* force stop */
1752}
1753
1754static int brcmnand_dma_trans(struct brcmnand_host *host, u64 addr, u32 *buf,
1755 u32 len, u8 dma_cmd)
1756{
1757 struct brcmnand_controller *ctrl = host->ctrl;
1758 dma_addr_t buf_pa;
1759 int dir = dma_cmd == CMD_PAGE_READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1760
1761 buf_pa = dma_map_single(ctrl->dev, buf, len, dir);
1762 if (dma_mapping_error(ctrl->dev, buf_pa)) {
1763 dev_err(ctrl->dev, "unable to map buffer for DMA\n");
1764 return -ENOMEM;
1765 }
1766
1767 brcmnand_fill_dma_desc(host, ctrl->dma_desc, addr, buf_pa, len,
1768 dma_cmd, true, true, 0);
1769
1770 brcmnand_dma_run(host, ctrl->dma_pa);
1771
1772 dma_unmap_single(ctrl->dev, buf_pa, len, dir);
1773
1774 if (ctrl->dma_desc->status_valid & FLASH_DMA_ECC_ERROR)
1775 return -EBADMSG;
1776 else if (ctrl->dma_desc->status_valid & FLASH_DMA_CORR_ERROR)
1777 return -EUCLEAN;
1778
1779 return 0;
1780}
1781#endif /* __UBOOT__ */
1782
1783/*
1784 * Assumes proper CS is already set
1785 */
1786static int brcmnand_read_by_pio(struct mtd_info *mtd, struct nand_chip *chip,
1787 u64 addr, unsigned int trans, u32 *buf,
1788 u8 *oob, u64 *err_addr)
1789{
1790 struct brcmnand_host *host = nand_get_controller_data(chip);
1791 struct brcmnand_controller *ctrl = host->ctrl;
1792 int i, j, ret = 0;
1793
Kamal Dasu299c6832023-02-11 16:29:00 +01001794 brcmnand_clear_ecc_addr(ctrl);
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001795
1796 for (i = 0; i < trans; i++, addr += FC_BYTES) {
Kamal Dasu299c6832023-02-11 16:29:00 +01001797 brcmnand_set_cmd_addr(mtd, addr);
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001798 /* SPARE_AREA_READ does not use ECC, so just use PAGE_READ */
1799 brcmnand_send_cmd(host, CMD_PAGE_READ);
1800 brcmnand_waitfunc(mtd, chip);
1801
1802 if (likely(buf)) {
1803 brcmnand_soc_data_bus_prepare(ctrl->soc, false);
1804
1805 for (j = 0; j < FC_WORDS; j++, buf++)
1806 *buf = brcmnand_read_fc(ctrl, j);
1807
1808 brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
1809 }
1810
1811 if (oob)
1812 oob += read_oob_from_regs(ctrl, i, oob,
1813 mtd->oobsize / trans,
1814 host->hwcfg.sector_size_1k);
1815
Joel Peshkin8384fd82021-12-20 20:15:47 -08001816 if (ret != -EBADMSG) {
Kamal Dasu299c6832023-02-11 16:29:00 +01001817 *err_addr = brcmnand_get_uncorrecc_addr(ctrl);
1818
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001819 if (*err_addr)
1820 ret = -EBADMSG;
1821 }
1822
1823 if (!ret) {
Kamal Dasu299c6832023-02-11 16:29:00 +01001824 *err_addr = brcmnand_get_correcc_addr(ctrl);
1825
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001826 if (*err_addr)
1827 ret = -EUCLEAN;
1828 }
1829 }
1830
1831 return ret;
1832}
1833
1834/*
1835 * Check a page to see if it is erased (w/ bitflips) after an uncorrectable ECC
1836 * error
1837 *
1838 * Because the HW ECC signals an ECC error if an erase paged has even a single
1839 * bitflip, we must check each ECC error to see if it is actually an erased
1840 * page with bitflips, not a truly corrupted page.
1841 *
1842 * On a real error, return a negative error code (-EBADMSG for ECC error), and
1843 * buf will contain raw data.
1844 * Otherwise, buf gets filled with 0xffs and return the maximum number of
1845 * bitflips-per-ECC-sector to the caller.
1846 *
1847 */
1848static int brcmstb_nand_verify_erased_page(struct mtd_info *mtd,
1849 struct nand_chip *chip, void *buf, u64 addr)
1850{
Álvaro Fernández Rojas210751f2023-02-11 16:29:04 +01001851 struct mtd_oob_region ecc;
1852 int i;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001853 int bitflips = 0;
1854 int page = addr >> chip->page_shift;
1855 int ret;
Álvaro Fernández Rojas210751f2023-02-11 16:29:04 +01001856 void *ecc_bytes;
Claire Lin2bac5792023-02-11 16:29:02 +01001857 void *ecc_chunk;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001858
1859 if (!buf) {
1860#ifndef __UBOOT__
1861 buf = chip->data_buf;
1862#else
1863 buf = chip->buffers->databuf;
1864#endif
1865 /* Invalidate page cache */
1866 chip->pagebuf = -1;
1867 }
1868
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001869 /* read without ecc for verification */
1870 ret = chip->ecc.read_page_raw(mtd, chip, buf, true, page);
1871 if (ret)
1872 return ret;
1873
Álvaro Fernández Rojas210751f2023-02-11 16:29:04 +01001874 for (i = 0; i < chip->ecc.steps; i++) {
Claire Lin2bac5792023-02-11 16:29:02 +01001875 ecc_chunk = buf + chip->ecc.size * i;
Álvaro Fernández Rojas210751f2023-02-11 16:29:04 +01001876
1877 mtd_ooblayout_ecc(mtd, i, &ecc);
1878 ecc_bytes = chip->oob_poi + ecc.offset;
1879
1880 ret = nand_check_erased_ecc_chunk(ecc_chunk, chip->ecc.size,
1881 ecc_bytes, ecc.length,
1882 NULL, 0,
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001883 chip->ecc.strength);
1884 if (ret < 0)
1885 return ret;
1886
1887 bitflips = max(bitflips, ret);
1888 }
1889
1890 return bitflips;
1891}
1892
1893static int brcmnand_read(struct mtd_info *mtd, struct nand_chip *chip,
1894 u64 addr, unsigned int trans, u32 *buf, u8 *oob)
1895{
1896 struct brcmnand_host *host = nand_get_controller_data(chip);
1897 struct brcmnand_controller *ctrl = host->ctrl;
1898 u64 err_addr = 0;
1899 int err;
1900 bool retry = true;
1901
1902 dev_dbg(ctrl->dev, "read %llx -> %p\n", (unsigned long long)addr, buf);
1903
1904try_dmaread:
Kamal Dasu299c6832023-02-11 16:29:00 +01001905 brcmnand_clear_ecc_addr(ctrl);
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01001906
1907#ifndef __UBOOT__
1908 if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
1909 err = brcmnand_dma_trans(host, addr, buf, trans * FC_BYTES,
1910 CMD_PAGE_READ);
1911 if (err) {
1912 if (mtd_is_bitflip_or_eccerr(err))
1913 err_addr = addr;
1914 else
1915 return -EIO;
1916 }
1917 } else {
1918 if (oob)
1919 memset(oob, 0x99, mtd->oobsize);
1920
1921 err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf,
1922 oob, &err_addr);
1923 }
1924#else
1925 if (oob)
1926 memset(oob, 0x99, mtd->oobsize);
1927
1928 err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf,
1929 oob, &err_addr);
1930#endif /* __UBOOT__ */
1931
1932 if (mtd_is_eccerr(err)) {
1933 /*
1934 * On controller version and 7.0, 7.1 , DMA read after a
1935 * prior PIO read that reported uncorrectable error,
1936 * the DMA engine captures this error following DMA read
1937 * cleared only on subsequent DMA read, so just retry once
1938 * to clear a possible false error reported for current DMA
1939 * read
1940 */
1941 if ((ctrl->nand_version == 0x0700) ||
1942 (ctrl->nand_version == 0x0701)) {
1943 if (retry) {
1944 retry = false;
1945 goto try_dmaread;
1946 }
1947 }
1948
1949 /*
1950 * Controller version 7.2 has hw encoder to detect erased page
1951 * bitflips, apply sw verification for older controllers only
1952 */
1953 if (ctrl->nand_version < 0x0702) {
1954 err = brcmstb_nand_verify_erased_page(mtd, chip, buf,
1955 addr);
1956 /* erased page bitflips corrected */
1957 if (err >= 0)
1958 return err;
1959 }
1960
1961 dev_dbg(ctrl->dev, "uncorrectable error at 0x%llx\n",
1962 (unsigned long long)err_addr);
1963 mtd->ecc_stats.failed++;
1964 /* NAND layer expects zero on ECC errors */
1965 return 0;
1966 }
1967
1968 if (mtd_is_bitflip(err)) {
1969 unsigned int corrected = brcmnand_count_corrected(ctrl);
1970
1971 dev_dbg(ctrl->dev, "corrected error at 0x%llx\n",
1972 (unsigned long long)err_addr);
1973 mtd->ecc_stats.corrected += corrected;
1974 /* Always exceed the software-imposed threshold */
1975 return max(mtd->bitflip_threshold, corrected);
1976 }
1977
1978 return 0;
1979}
1980
1981static int brcmnand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1982 uint8_t *buf, int oob_required, int page)
1983{
1984 struct brcmnand_host *host = nand_get_controller_data(chip);
1985 u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
1986
1987 nand_read_page_op(chip, page, 0, NULL, 0);
1988
1989 return brcmnand_read(mtd, chip, host->last_addr,
1990 mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
1991}
1992
1993static int brcmnand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1994 uint8_t *buf, int oob_required, int page)
1995{
1996 struct brcmnand_host *host = nand_get_controller_data(chip);
1997 u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
1998 int ret;
1999
2000 nand_read_page_op(chip, page, 0, NULL, 0);
2001
2002 brcmnand_set_ecc_enabled(host, 0);
2003 ret = brcmnand_read(mtd, chip, host->last_addr,
2004 mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
2005 brcmnand_set_ecc_enabled(host, 1);
2006 return ret;
2007}
2008
2009static int brcmnand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
2010 int page)
2011{
2012 return brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
2013 mtd->writesize >> FC_SHIFT,
2014 NULL, (u8 *)chip->oob_poi);
2015}
2016
2017static int brcmnand_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
2018 int page)
2019{
2020 struct brcmnand_host *host = nand_get_controller_data(chip);
2021
2022 brcmnand_set_ecc_enabled(host, 0);
2023 brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
2024 mtd->writesize >> FC_SHIFT,
2025 NULL, (u8 *)chip->oob_poi);
2026 brcmnand_set_ecc_enabled(host, 1);
2027 return 0;
2028}
2029
2030static int brcmnand_write(struct mtd_info *mtd, struct nand_chip *chip,
2031 u64 addr, const u32 *buf, u8 *oob)
2032{
2033 struct brcmnand_host *host = nand_get_controller_data(chip);
2034 struct brcmnand_controller *ctrl = host->ctrl;
2035 unsigned int i, j, trans = mtd->writesize >> FC_SHIFT;
2036 int status, ret = 0;
2037
2038 dev_dbg(ctrl->dev, "write %llx <- %p\n", (unsigned long long)addr, buf);
2039
2040 if (unlikely((unsigned long)buf & 0x03)) {
2041 dev_warn(ctrl->dev, "unaligned buffer: %p\n", buf);
2042 buf = (u32 *)((unsigned long)buf & ~0x03);
2043 }
2044
2045 brcmnand_wp(mtd, 0);
2046
2047 for (i = 0; i < ctrl->max_oob; i += 4)
2048 oob_reg_write(ctrl, i, 0xffffffff);
2049
2050#ifndef __UBOOT__
2051 if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
2052 if (brcmnand_dma_trans(host, addr, (u32 *)buf,
2053 mtd->writesize, CMD_PROGRAM_PAGE))
2054 ret = -EIO;
2055 goto out;
2056 }
2057#endif /* __UBOOT__ */
2058
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002059 for (i = 0; i < trans; i++, addr += FC_BYTES) {
2060 /* full address MUST be set before populating FC */
Kamal Dasu299c6832023-02-11 16:29:00 +01002061 brcmnand_set_cmd_addr(mtd, addr);
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002062
2063 if (buf) {
2064 brcmnand_soc_data_bus_prepare(ctrl->soc, false);
2065
2066 for (j = 0; j < FC_WORDS; j++, buf++)
2067 brcmnand_write_fc(ctrl, j, *buf);
2068
2069 brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
2070 } else if (oob) {
2071 for (j = 0; j < FC_WORDS; j++)
2072 brcmnand_write_fc(ctrl, j, 0xffffffff);
2073 }
2074
2075 if (oob) {
2076 oob += write_oob_to_regs(ctrl, i, oob,
2077 mtd->oobsize / trans,
2078 host->hwcfg.sector_size_1k);
2079 }
2080
2081 /* we cannot use SPARE_AREA_PROGRAM when PARTIAL_PAGE_EN=0 */
2082 brcmnand_send_cmd(host, CMD_PROGRAM_PAGE);
2083 status = brcmnand_waitfunc(mtd, chip);
2084
2085 if (status & NAND_STATUS_FAIL) {
2086 dev_info(ctrl->dev, "program failed at %llx\n",
2087 (unsigned long long)addr);
2088 ret = -EIO;
2089 goto out;
2090 }
2091 }
2092out:
2093 brcmnand_wp(mtd, 1);
2094 return ret;
2095}
2096
2097static int brcmnand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
2098 const uint8_t *buf, int oob_required, int page)
2099{
2100 struct brcmnand_host *host = nand_get_controller_data(chip);
2101 void *oob = oob_required ? chip->oob_poi : NULL;
2102
2103 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2104 brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
2105
2106 return nand_prog_page_end_op(chip);
2107}
2108
2109static int brcmnand_write_page_raw(struct mtd_info *mtd,
2110 struct nand_chip *chip, const uint8_t *buf,
2111 int oob_required, int page)
2112{
2113 struct brcmnand_host *host = nand_get_controller_data(chip);
2114 void *oob = oob_required ? chip->oob_poi : NULL;
2115
2116 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2117 brcmnand_set_ecc_enabled(host, 0);
2118 brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
2119 brcmnand_set_ecc_enabled(host, 1);
2120
2121 return nand_prog_page_end_op(chip);
2122}
2123
2124static int brcmnand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
2125 int page)
2126{
2127 return brcmnand_write(mtd, chip, (u64)page << chip->page_shift,
2128 NULL, chip->oob_poi);
2129}
2130
2131static int brcmnand_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
2132 int page)
2133{
2134 struct brcmnand_host *host = nand_get_controller_data(chip);
2135 int ret;
2136
2137 brcmnand_set_ecc_enabled(host, 0);
2138 ret = brcmnand_write(mtd, chip, (u64)page << chip->page_shift, NULL,
2139 (u8 *)chip->oob_poi);
2140 brcmnand_set_ecc_enabled(host, 1);
2141
2142 return ret;
2143}
2144
2145/***********************************************************************
2146 * Per-CS setup (1 NAND device)
2147 ***********************************************************************/
2148
2149static int brcmnand_set_cfg(struct brcmnand_host *host,
2150 struct brcmnand_cfg *cfg)
2151{
2152 struct brcmnand_controller *ctrl = host->ctrl;
2153 struct nand_chip *chip = &host->chip;
2154 u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2155 u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
2156 BRCMNAND_CS_CFG_EXT);
2157 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
2158 BRCMNAND_CS_ACC_CONTROL);
2159 u8 block_size = 0, page_size = 0, device_size = 0;
2160 u32 tmp;
2161
2162 if (ctrl->block_sizes) {
2163 int i, found;
2164
2165 for (i = 0, found = 0; ctrl->block_sizes[i]; i++)
2166 if (ctrl->block_sizes[i] * 1024 == cfg->block_size) {
2167 block_size = i;
2168 found = 1;
2169 }
2170 if (!found) {
2171 dev_warn(ctrl->dev, "invalid block size %u\n",
2172 cfg->block_size);
2173 return -EINVAL;
2174 }
2175 } else {
2176 block_size = ffs(cfg->block_size) - ffs(BRCMNAND_MIN_BLOCKSIZE);
2177 }
2178
2179 if (cfg->block_size < BRCMNAND_MIN_BLOCKSIZE || (ctrl->max_block_size &&
2180 cfg->block_size > ctrl->max_block_size)) {
2181 dev_warn(ctrl->dev, "invalid block size %u\n",
2182 cfg->block_size);
2183 block_size = 0;
2184 }
2185
2186 if (ctrl->page_sizes) {
2187 int i, found;
2188
2189 for (i = 0, found = 0; ctrl->page_sizes[i]; i++)
2190 if (ctrl->page_sizes[i] == cfg->page_size) {
2191 page_size = i;
2192 found = 1;
2193 }
2194 if (!found) {
2195 dev_warn(ctrl->dev, "invalid page size %u\n",
2196 cfg->page_size);
2197 return -EINVAL;
2198 }
2199 } else {
2200 page_size = ffs(cfg->page_size) - ffs(BRCMNAND_MIN_PAGESIZE);
2201 }
2202
2203 if (cfg->page_size < BRCMNAND_MIN_PAGESIZE || (ctrl->max_page_size &&
2204 cfg->page_size > ctrl->max_page_size)) {
2205 dev_warn(ctrl->dev, "invalid page size %u\n", cfg->page_size);
2206 return -EINVAL;
2207 }
2208
2209 if (fls64(cfg->device_size) < fls64(BRCMNAND_MIN_DEVSIZE)) {
2210 dev_warn(ctrl->dev, "invalid device size 0x%llx\n",
2211 (unsigned long long)cfg->device_size);
2212 return -EINVAL;
2213 }
2214 device_size = fls64(cfg->device_size) - fls64(BRCMNAND_MIN_DEVSIZE);
2215
2216 tmp = (cfg->blk_adr_bytes << CFG_BLK_ADR_BYTES_SHIFT) |
2217 (cfg->col_adr_bytes << CFG_COL_ADR_BYTES_SHIFT) |
2218 (cfg->ful_adr_bytes << CFG_FUL_ADR_BYTES_SHIFT) |
2219 (!!(cfg->device_width == 16) << CFG_BUS_WIDTH_SHIFT) |
2220 (device_size << CFG_DEVICE_SIZE_SHIFT);
2221 if (cfg_offs == cfg_ext_offs) {
Álvaro Fernández Rojas738d7362023-02-11 16:29:08 +01002222 tmp |= (page_size << ctrl->page_size_shift) |
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002223 (block_size << CFG_BLK_SIZE_SHIFT);
2224 nand_writereg(ctrl, cfg_offs, tmp);
2225 } else {
2226 nand_writereg(ctrl, cfg_offs, tmp);
2227 tmp = (page_size << CFG_EXT_PAGE_SIZE_SHIFT) |
2228 (block_size << CFG_EXT_BLK_SIZE_SHIFT);
2229 nand_writereg(ctrl, cfg_ext_offs, tmp);
2230 }
2231
2232 tmp = nand_readreg(ctrl, acc_control_offs);
2233 tmp &= ~brcmnand_ecc_level_mask(ctrl);
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002234 tmp &= ~brcmnand_spare_area_mask(ctrl);
Álvaro Fernández Rojas738d7362023-02-11 16:29:08 +01002235 if (ctrl->nand_version >= 0x0302) {
William Zhang26f66e62024-09-16 11:58:43 +02002236 tmp |= cfg->ecc_level << ctrl->ecc_level_shift;
Álvaro Fernández Rojas738d7362023-02-11 16:29:08 +01002237 tmp |= cfg->spare_area_size;
2238 }
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002239 nand_writereg(ctrl, acc_control_offs, tmp);
2240
2241 brcmnand_set_sector_size_1k(host, cfg->sector_size_1k);
2242
2243 /* threshold = ceil(BCH-level * 0.75) */
2244 brcmnand_wr_corr_thresh(host, DIV_ROUND_UP(chip->ecc.strength * 3, 4));
2245
2246 return 0;
2247}
2248
2249static void brcmnand_print_cfg(struct brcmnand_host *host,
2250 char *buf, struct brcmnand_cfg *cfg)
2251{
2252 buf += sprintf(buf,
2253 "%lluMiB total, %uKiB blocks, %u%s pages, %uB OOB, %u-bit",
2254 (unsigned long long)cfg->device_size >> 20,
2255 cfg->block_size >> 10,
2256 cfg->page_size >= 1024 ? cfg->page_size >> 10 : cfg->page_size,
2257 cfg->page_size >= 1024 ? "KiB" : "B",
2258 cfg->spare_area_size, cfg->device_width);
2259
2260 /* Account for Hamming ECC and for BCH 512B vs 1KiB sectors */
2261 if (is_hamming_ecc(host->ctrl, cfg))
2262 sprintf(buf, ", Hamming ECC");
2263 else if (cfg->sector_size_1k)
2264 sprintf(buf, ", BCH-%u (1KiB sector)", cfg->ecc_level << 1);
2265 else
2266 sprintf(buf, ", BCH-%u", cfg->ecc_level);
2267}
2268
2269/*
2270 * Minimum number of bytes to address a page. Calculated as:
2271 * roundup(log2(size / page-size) / 8)
2272 *
2273 * NB: the following does not "round up" for non-power-of-2 'size'; but this is
2274 * OK because many other things will break if 'size' is irregular...
2275 */
2276static inline int get_blk_adr_bytes(u64 size, u32 writesize)
2277{
2278 return ALIGN(ilog2(size) - ilog2(writesize), 8) >> 3;
2279}
2280
2281static int brcmnand_setup_dev(struct brcmnand_host *host)
2282{
2283 struct mtd_info *mtd = nand_to_mtd(&host->chip);
2284 struct nand_chip *chip = &host->chip;
2285 struct brcmnand_controller *ctrl = host->ctrl;
2286 struct brcmnand_cfg *cfg = &host->hwcfg;
2287 char msg[128];
2288 u32 offs, tmp, oob_sector;
2289 int ret;
2290
2291 memset(cfg, 0, sizeof(*cfg));
2292
2293#ifndef __UBOOT__
2294 ret = of_property_read_u32(nand_get_flash_node(chip),
2295 "brcm,nand-oob-sector-size",
2296 &oob_sector);
2297#else
2298 ret = ofnode_read_u32(nand_get_flash_node(chip),
2299 "brcm,nand-oob-sector-size",
2300 &oob_sector);
2301#endif /* __UBOOT__ */
2302 if (ret) {
2303 /* Use detected size */
2304 cfg->spare_area_size = mtd->oobsize /
2305 (mtd->writesize >> FC_SHIFT);
2306 } else {
2307 cfg->spare_area_size = oob_sector;
2308 }
2309 if (cfg->spare_area_size > ctrl->max_oob)
2310 cfg->spare_area_size = ctrl->max_oob;
2311 /*
2312 * Set oobsize to be consistent with controller's spare_area_size, as
2313 * the rest is inaccessible.
2314 */
2315 mtd->oobsize = cfg->spare_area_size * (mtd->writesize >> FC_SHIFT);
2316
2317 cfg->device_size = mtd->size;
2318 cfg->block_size = mtd->erasesize;
2319 cfg->page_size = mtd->writesize;
2320 cfg->device_width = (chip->options & NAND_BUSWIDTH_16) ? 16 : 8;
2321 cfg->col_adr_bytes = 2;
2322 cfg->blk_adr_bytes = get_blk_adr_bytes(mtd->size, mtd->writesize);
2323
2324 if (chip->ecc.mode != NAND_ECC_HW) {
2325 dev_err(ctrl->dev, "only HW ECC supported; selected: %d\n",
2326 chip->ecc.mode);
2327 return -EINVAL;
2328 }
2329
2330 if (chip->ecc.algo == NAND_ECC_UNKNOWN) {
2331 if (chip->ecc.strength == 1 && chip->ecc.size == 512)
2332 /* Default to Hamming for 1-bit ECC, if unspecified */
2333 chip->ecc.algo = NAND_ECC_HAMMING;
2334 else
2335 /* Otherwise, BCH */
2336 chip->ecc.algo = NAND_ECC_BCH;
2337 }
2338
2339 if (chip->ecc.algo == NAND_ECC_HAMMING && (chip->ecc.strength != 1 ||
2340 chip->ecc.size != 512)) {
2341 dev_err(ctrl->dev, "invalid Hamming params: %d bits per %d bytes\n",
2342 chip->ecc.strength, chip->ecc.size);
2343 return -EINVAL;
2344 }
2345
2346 switch (chip->ecc.size) {
2347 case 512:
2348 if (chip->ecc.algo == NAND_ECC_HAMMING)
2349 cfg->ecc_level = 15;
2350 else
2351 cfg->ecc_level = chip->ecc.strength;
2352 cfg->sector_size_1k = 0;
2353 break;
2354 case 1024:
2355 if (!(ctrl->features & BRCMNAND_HAS_1K_SECTORS)) {
2356 dev_err(ctrl->dev, "1KB sectors not supported\n");
2357 return -EINVAL;
2358 }
2359 if (chip->ecc.strength & 0x1) {
2360 dev_err(ctrl->dev,
2361 "odd ECC not supported with 1KB sectors\n");
2362 return -EINVAL;
2363 }
2364
2365 cfg->ecc_level = chip->ecc.strength >> 1;
2366 cfg->sector_size_1k = 1;
2367 break;
2368 default:
2369 dev_err(ctrl->dev, "unsupported ECC size: %d\n",
2370 chip->ecc.size);
2371 return -EINVAL;
2372 }
2373
2374 cfg->ful_adr_bytes = cfg->blk_adr_bytes;
2375 if (mtd->writesize > 512)
2376 cfg->ful_adr_bytes += cfg->col_adr_bytes;
2377 else
2378 cfg->ful_adr_bytes += 1;
2379
2380 ret = brcmnand_set_cfg(host, cfg);
2381 if (ret)
2382 return ret;
2383
2384 brcmnand_set_ecc_enabled(host, 1);
2385
2386 brcmnand_print_cfg(host, msg, cfg);
2387 dev_info(ctrl->dev, "detected %s\n", msg);
2388
2389 /* Configure ACC_CONTROL */
2390 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
2391 tmp = nand_readreg(ctrl, offs);
2392 tmp &= ~ACC_CONTROL_PARTIAL_PAGE;
2393 tmp &= ~ACC_CONTROL_RD_ERASED;
2394
2395 /* We need to turn on Read from erased paged protected by ECC */
2396 if (ctrl->nand_version >= 0x0702)
2397 tmp |= ACC_CONTROL_RD_ERASED;
2398 tmp &= ~ACC_CONTROL_FAST_PGM_RDIN;
2399 if (ctrl->features & BRCMNAND_HAS_PREFETCH)
2400 tmp &= ~ACC_CONTROL_PREFETCH;
2401
2402 nand_writereg(ctrl, offs, tmp);
2403
2404 return 0;
2405}
2406
2407#ifndef __UBOOT__
2408static int brcmnand_init_cs(struct brcmnand_host *host, struct device_node *dn)
2409#else
2410static int brcmnand_init_cs(struct brcmnand_host *host, ofnode dn)
2411#endif
2412{
2413 struct brcmnand_controller *ctrl = host->ctrl;
2414#ifndef __UBOOT__
2415 struct platform_device *pdev = host->pdev;
2416#else
2417 struct udevice *pdev = host->pdev;
2418#endif /* __UBOOT__ */
2419 struct mtd_info *mtd;
2420 struct nand_chip *chip;
2421 int ret;
2422 u16 cfg_offs;
2423
2424#ifndef __UBOOT__
2425 ret = of_property_read_u32(dn, "reg", &host->cs);
2426#else
2427 ret = ofnode_read_s32(dn, "reg", &host->cs);
2428#endif
2429 if (ret) {
Sean Anderson4b5aa002020-09-15 10:44:50 -04002430 dev_err(pdev, "can't get chip-select\n");
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002431 return -ENXIO;
2432 }
2433
2434 mtd = nand_to_mtd(&host->chip);
2435 chip = &host->chip;
2436
2437 nand_set_flash_node(chip, dn);
2438 nand_set_controller_data(chip, host);
2439#ifndef __UBOOT__
2440 mtd->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "brcmnand.%d",
2441 host->cs);
2442#else
2443 mtd->name = devm_kasprintf(pdev, GFP_KERNEL, "brcmnand.%d",
2444 host->cs);
2445#endif /* __UBOOT__ */
2446 if (!mtd->name)
2447 return -ENOMEM;
2448
2449 mtd->owner = THIS_MODULE;
2450#ifndef __UBOOT__
2451 mtd->dev.parent = &pdev->dev;
2452#else
2453 mtd->dev->parent = pdev;
2454#endif /* __UBOOT__ */
2455
2456 chip->IO_ADDR_R = (void __iomem *)0xdeadbeef;
2457 chip->IO_ADDR_W = (void __iomem *)0xdeadbeef;
2458
2459 chip->cmd_ctrl = brcmnand_cmd_ctrl;
2460 chip->cmdfunc = brcmnand_cmdfunc;
2461 chip->waitfunc = brcmnand_waitfunc;
2462 chip->read_byte = brcmnand_read_byte;
2463 chip->read_buf = brcmnand_read_buf;
2464 chip->write_buf = brcmnand_write_buf;
2465
2466 chip->ecc.mode = NAND_ECC_HW;
2467 chip->ecc.read_page = brcmnand_read_page;
2468 chip->ecc.write_page = brcmnand_write_page;
2469 chip->ecc.read_page_raw = brcmnand_read_page_raw;
2470 chip->ecc.write_page_raw = brcmnand_write_page_raw;
2471 chip->ecc.write_oob_raw = brcmnand_write_oob_raw;
2472 chip->ecc.read_oob_raw = brcmnand_read_oob_raw;
2473 chip->ecc.read_oob = brcmnand_read_oob;
2474 chip->ecc.write_oob = brcmnand_write_oob;
2475
2476 chip->controller = &ctrl->controller;
2477
2478 /*
2479 * The bootloader might have configured 16bit mode but
2480 * NAND READID command only works in 8bit mode. We force
2481 * 8bit mode here to ensure that NAND READID commands works.
2482 */
2483 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2484 nand_writereg(ctrl, cfg_offs,
2485 nand_readreg(ctrl, cfg_offs) & ~CFG_BUS_WIDTH);
2486
2487 ret = nand_scan_ident(mtd, 1, NULL);
2488 if (ret)
2489 return ret;
2490
2491 chip->options |= NAND_NO_SUBPAGE_WRITE;
2492 /*
2493 * Avoid (for instance) kmap()'d buffers from JFFS2, which we can't DMA
2494 * to/from, and have nand_base pass us a bounce buffer instead, as
2495 * needed.
2496 */
2497 chip->options |= NAND_USE_BOUNCE_BUFFER;
2498
2499 if (chip->bbt_options & NAND_BBT_USE_FLASH)
2500 chip->bbt_options |= NAND_BBT_NO_OOB;
2501
2502 if (brcmnand_setup_dev(host))
2503 return -ENXIO;
2504
2505 chip->ecc.size = host->hwcfg.sector_size_1k ? 1024 : 512;
2506 /* only use our internal HW threshold */
2507 mtd->bitflip_threshold = 1;
2508
William Zhang1100e492019-09-04 10:51:13 -07002509 chip->ecc.layout = brcmstb_choose_ecc_layout(host);
2510 if (!chip->ecc.layout)
2511 return -ENXIO;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002512
2513 ret = nand_scan_tail(mtd);
2514 if (ret)
2515 return ret;
2516
2517#ifndef __UBOOT__
2518 ret = mtd_device_register(mtd, NULL, 0);
2519 if (ret)
2520 nand_cleanup(chip);
2521#else
2522 ret = nand_register(0, mtd);
2523#endif /* __UBOOT__ */
2524
Álvaro Fernández Rojasb3d66d92023-02-11 16:29:09 +01002525 /* If OOB is written with ECC enabled it will cause ECC errors */
2526 if (is_hamming_ecc(host->ctrl, &host->hwcfg)) {
2527 chip->ecc.write_oob = brcmnand_write_oob_raw;
2528 chip->ecc.read_oob = brcmnand_read_oob_raw;
2529 }
2530
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002531 return ret;
2532}
2533
2534#ifndef __UBOOT__
2535static void brcmnand_save_restore_cs_config(struct brcmnand_host *host,
2536 int restore)
2537{
2538 struct brcmnand_controller *ctrl = host->ctrl;
2539 u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
2540 u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
2541 BRCMNAND_CS_CFG_EXT);
2542 u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
2543 BRCMNAND_CS_ACC_CONTROL);
2544 u16 t1_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING1);
2545 u16 t2_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING2);
2546
2547 if (restore) {
2548 nand_writereg(ctrl, cfg_offs, host->hwcfg.config);
2549 if (cfg_offs != cfg_ext_offs)
2550 nand_writereg(ctrl, cfg_ext_offs,
2551 host->hwcfg.config_ext);
2552 nand_writereg(ctrl, acc_control_offs, host->hwcfg.acc_control);
2553 nand_writereg(ctrl, t1_offs, host->hwcfg.timing_1);
2554 nand_writereg(ctrl, t2_offs, host->hwcfg.timing_2);
2555 } else {
2556 host->hwcfg.config = nand_readreg(ctrl, cfg_offs);
2557 if (cfg_offs != cfg_ext_offs)
2558 host->hwcfg.config_ext =
2559 nand_readreg(ctrl, cfg_ext_offs);
2560 host->hwcfg.acc_control = nand_readreg(ctrl, acc_control_offs);
2561 host->hwcfg.timing_1 = nand_readreg(ctrl, t1_offs);
2562 host->hwcfg.timing_2 = nand_readreg(ctrl, t2_offs);
2563 }
2564}
2565
2566static int brcmnand_suspend(struct device *dev)
2567{
2568 struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
2569 struct brcmnand_host *host;
2570
2571 list_for_each_entry(host, &ctrl->host_list, node)
2572 brcmnand_save_restore_cs_config(host, 0);
2573
2574 ctrl->nand_cs_nand_select = brcmnand_read_reg(ctrl, BRCMNAND_CS_SELECT);
2575 ctrl->nand_cs_nand_xor = brcmnand_read_reg(ctrl, BRCMNAND_CS_XOR);
2576 ctrl->corr_stat_threshold =
2577 brcmnand_read_reg(ctrl, BRCMNAND_CORR_THRESHOLD);
2578
2579 if (has_flash_dma(ctrl))
2580 ctrl->flash_dma_mode = flash_dma_readl(ctrl, FLASH_DMA_MODE);
2581
2582 return 0;
2583}
2584
2585static int brcmnand_resume(struct device *dev)
2586{
2587 struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
2588 struct brcmnand_host *host;
2589
2590 if (has_flash_dma(ctrl)) {
2591 flash_dma_writel(ctrl, FLASH_DMA_MODE, ctrl->flash_dma_mode);
2592 flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
2593 }
2594
2595 brcmnand_write_reg(ctrl, BRCMNAND_CS_SELECT, ctrl->nand_cs_nand_select);
2596 brcmnand_write_reg(ctrl, BRCMNAND_CS_XOR, ctrl->nand_cs_nand_xor);
2597 brcmnand_write_reg(ctrl, BRCMNAND_CORR_THRESHOLD,
2598 ctrl->corr_stat_threshold);
2599 if (ctrl->soc) {
2600 /* Clear/re-enable interrupt */
2601 ctrl->soc->ctlrdy_ack(ctrl->soc);
2602 ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
2603 }
2604
2605 list_for_each_entry(host, &ctrl->host_list, node) {
2606 struct nand_chip *chip = &host->chip;
2607
2608 brcmnand_save_restore_cs_config(host, 1);
2609
2610 /* Reset the chip, required by some chips after power-up */
2611 nand_reset_op(chip);
2612 }
2613
2614 return 0;
2615}
2616
2617const struct dev_pm_ops brcmnand_pm_ops = {
2618 .suspend = brcmnand_suspend,
2619 .resume = brcmnand_resume,
2620};
2621EXPORT_SYMBOL_GPL(brcmnand_pm_ops);
2622
2623static const struct of_device_id brcmnand_of_match[] = {
Álvaro Fernández Rojas738d7362023-02-11 16:29:08 +01002624 { .compatible = "brcm,brcmnand-v2.1" },
2625 { .compatible = "brcm,brcmnand-v2.2" },
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002626 { .compatible = "brcm,brcmnand-v4.0" },
2627 { .compatible = "brcm,brcmnand-v5.0" },
2628 { .compatible = "brcm,brcmnand-v6.0" },
2629 { .compatible = "brcm,brcmnand-v6.1" },
2630 { .compatible = "brcm,brcmnand-v6.2" },
2631 { .compatible = "brcm,brcmnand-v7.0" },
2632 { .compatible = "brcm,brcmnand-v7.1" },
2633 { .compatible = "brcm,brcmnand-v7.2" },
Kamal Dasuf47b36b2023-02-11 16:29:01 +01002634 { .compatible = "brcm,brcmnand-v7.3" },
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002635 {},
2636};
2637MODULE_DEVICE_TABLE(of, brcmnand_of_match);
2638#endif /* __UBOOT__ */
2639
2640/***********************************************************************
2641 * Platform driver setup (per controller)
2642 ***********************************************************************/
2643
2644#ifndef __UBOOT__
2645int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
2646#else
2647int brcmnand_probe(struct udevice *dev, struct brcmnand_soc *soc)
2648#endif /* __UBOOT__ */
2649{
2650#ifndef __UBOOT__
2651 struct device *dev = &pdev->dev;
2652 struct device_node *dn = dev->of_node, *child;
2653#else
2654 ofnode child;
2655 struct udevice *pdev = dev;
2656#endif /* __UBOOT__ */
2657 struct brcmnand_controller *ctrl;
2658#ifndef __UBOOT__
2659 struct resource *res;
2660#else
2661 struct resource res;
2662#endif /* __UBOOT__ */
2663 int ret;
2664
2665#ifndef __UBOOT__
2666 /* We only support device-tree instantiation */
2667 if (!dn)
2668 return -ENODEV;
2669
2670 if (!of_match_node(brcmnand_of_match, dn))
2671 return -ENODEV;
2672#endif /* __UBOOT__ */
2673
2674 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
2675 if (!ctrl)
2676 return -ENOMEM;
2677
2678#ifndef __UBOOT__
2679 dev_set_drvdata(dev, ctrl);
2680#else
2681 /*
2682 * in u-boot, the data for the driver is allocated before probing
2683 * so to keep the reference to ctrl, we store it in the variable soc
2684 */
2685 soc->ctrl = ctrl;
2686#endif /* __UBOOT__ */
2687 ctrl->dev = dev;
2688
2689 init_completion(&ctrl->done);
2690 init_completion(&ctrl->dma_done);
2691 nand_hw_control_init(&ctrl->controller);
2692 INIT_LIST_HEAD(&ctrl->host_list);
2693
Philippe Reynes7f28cf62019-03-15 15:14:37 +01002694 /* Is parameter page in big endian ? */
2695 ctrl->parameter_page_big_endian =
2696 dev_read_u32_default(dev, "parameter-page-big-endian", 1);
2697
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002698 /* NAND register range */
2699#ifndef __UBOOT__
2700 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2701 ctrl->nand_base = devm_ioremap_resource(dev, res);
2702#else
2703 dev_read_resource(pdev, 0, &res);
2704 ctrl->nand_base = devm_ioremap(pdev, res.start, resource_size(&res));
2705#endif
2706 if (IS_ERR(ctrl->nand_base))
2707 return PTR_ERR(ctrl->nand_base);
2708
2709 /* Enable clock before using NAND registers */
2710 ctrl->clk = devm_clk_get(dev, "nand");
2711 if (!IS_ERR(ctrl->clk)) {
2712 ret = clk_prepare_enable(ctrl->clk);
2713 if (ret)
2714 return ret;
2715 } else {
Simon Glass7ca47bc2021-01-24 14:32:41 -07002716 /* Ignore PTR_ERR(ctrl->clk) */
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002717 ctrl->clk = NULL;
2718 }
2719
2720 /* Initialize NAND revision */
2721 ret = brcmnand_revision_init(ctrl);
2722 if (ret)
2723 goto err;
2724
2725 /*
2726 * Most chips have this cache at a fixed offset within 'nand' block.
2727 * Some must specify this region separately.
2728 */
2729#ifndef __UBOOT__
2730 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand-cache");
2731 if (res) {
2732 ctrl->nand_fc = devm_ioremap_resource(dev, res);
2733 if (IS_ERR(ctrl->nand_fc)) {
2734 ret = PTR_ERR(ctrl->nand_fc);
2735 goto err;
2736 }
2737 } else {
2738 ctrl->nand_fc = ctrl->nand_base +
2739 ctrl->reg_offsets[BRCMNAND_FC_BASE];
2740 }
2741#else
2742 if (!dev_read_resource_byname(pdev, "nand-cache", &res)) {
2743 ctrl->nand_fc = devm_ioremap(dev, res.start,
2744 resource_size(&res));
2745 if (IS_ERR(ctrl->nand_fc)) {
2746 ret = PTR_ERR(ctrl->nand_fc);
2747 goto err;
2748 }
2749 } else {
2750 ctrl->nand_fc = ctrl->nand_base +
2751 ctrl->reg_offsets[BRCMNAND_FC_BASE];
2752 }
2753#endif
2754
2755#ifndef __UBOOT__
2756 /* FLASH_DMA */
2757 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash-dma");
2758 if (res) {
2759 ctrl->flash_dma_base = devm_ioremap_resource(dev, res);
2760 if (IS_ERR(ctrl->flash_dma_base)) {
2761 ret = PTR_ERR(ctrl->flash_dma_base);
2762 goto err;
2763 }
2764
Kamal Dasuf47b36b2023-02-11 16:29:01 +01002765 /* initialize the dma version */
2766 brcmnand_flash_dma_revision_init(ctrl);
2767
2768 /* linked-list and stop on error */
2769 flash_dma_writel(ctrl, FLASH_DMA_MODE, FLASH_DMA_MODE_MASK);
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002770 flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
2771
2772 /* Allocate descriptor(s) */
2773 ctrl->dma_desc = dmam_alloc_coherent(dev,
2774 sizeof(*ctrl->dma_desc),
2775 &ctrl->dma_pa, GFP_KERNEL);
2776 if (!ctrl->dma_desc) {
2777 ret = -ENOMEM;
2778 goto err;
2779 }
2780
2781 ctrl->dma_irq = platform_get_irq(pdev, 1);
2782 if ((int)ctrl->dma_irq < 0) {
2783 dev_err(dev, "missing FLASH_DMA IRQ\n");
2784 ret = -ENODEV;
2785 goto err;
2786 }
2787
2788 ret = devm_request_irq(dev, ctrl->dma_irq,
2789 brcmnand_dma_irq, 0, DRV_NAME,
2790 ctrl);
2791 if (ret < 0) {
2792 dev_err(dev, "can't allocate IRQ %d: error %d\n",
2793 ctrl->dma_irq, ret);
2794 goto err;
2795 }
2796
2797 dev_info(dev, "enabling FLASH_DMA\n");
2798 }
2799#endif /* __UBOOT__ */
2800
2801 /* Disable automatic device ID config, direct addressing */
2802 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT,
2803 CS_SELECT_AUTO_DEVICE_ID_CFG | 0xff, 0, 0);
2804 /* Disable XOR addressing */
2805 brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
2806
Philippe Reynes77669af2019-03-15 15:14:38 +01002807 /* Read the write-protect configuration in the device tree */
2808 wp_on = dev_read_u32_default(dev, "write-protect", wp_on);
2809
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002810 if (ctrl->features & BRCMNAND_HAS_WP) {
2811 /* Permanently disable write protection */
2812 if (wp_on == 2)
2813 brcmnand_set_wp(ctrl, false);
2814 } else {
2815 wp_on = 0;
2816 }
2817
2818#ifndef __UBOOT__
2819 /* IRQ */
2820 ctrl->irq = platform_get_irq(pdev, 0);
2821 if ((int)ctrl->irq < 0) {
2822 dev_err(dev, "no IRQ defined\n");
2823 ret = -ENODEV;
2824 goto err;
2825 }
2826
2827 /*
2828 * Some SoCs integrate this controller (e.g., its interrupt bits) in
2829 * interesting ways
2830 */
2831 if (soc) {
2832 ctrl->soc = soc;
2833
2834 ret = devm_request_irq(dev, ctrl->irq, brcmnand_irq, 0,
2835 DRV_NAME, ctrl);
2836
2837 /* Enable interrupt */
2838 ctrl->soc->ctlrdy_ack(ctrl->soc);
2839 ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
2840 } else {
2841 /* Use standard interrupt infrastructure */
2842 ret = devm_request_irq(dev, ctrl->irq, brcmnand_ctlrdy_irq, 0,
2843 DRV_NAME, ctrl);
2844 }
2845 if (ret < 0) {
2846 dev_err(dev, "can't allocate IRQ %d: error %d\n",
2847 ctrl->irq, ret);
2848 goto err;
2849 }
2850#endif /* __UBOOT__ */
2851
2852#ifndef __UBOOT__
2853 for_each_available_child_of_node(dn, child) {
2854 if (of_device_is_compatible(child, "brcm,nandcs")) {
2855 struct brcmnand_host *host;
2856
2857 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2858 if (!host) {
2859 of_node_put(child);
2860 ret = -ENOMEM;
2861 goto err;
2862 }
2863 host->pdev = pdev;
2864 host->ctrl = ctrl;
2865
2866 ret = brcmnand_init_cs(host, child);
2867 if (ret) {
2868 devm_kfree(dev, host);
2869 continue; /* Try all chip-selects */
2870 }
2871
2872 list_add_tail(&host->node, &ctrl->host_list);
2873 }
2874 }
2875#else
2876 ofnode_for_each_subnode(child, dev_ofnode(dev)) {
2877 if (ofnode_device_is_compatible(child, "brcm,nandcs")) {
2878 struct brcmnand_host *host;
2879
2880 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2881 if (!host) {
2882 ret = -ENOMEM;
2883 goto err;
2884 }
2885 host->pdev = pdev;
2886 host->ctrl = ctrl;
2887
2888 ret = brcmnand_init_cs(host, child);
2889 if (ret) {
2890 devm_kfree(dev, host);
2891 continue; /* Try all chip-selects */
2892 }
2893
2894 list_add_tail(&host->node, &ctrl->host_list);
2895 }
2896 }
2897#endif /* __UBOOT__ */
2898
Álvaro Fernández Rojasd8ae8752020-04-02 10:37:52 +02002899 /* No chip-selects could initialize properly */
2900 if (list_empty(&ctrl->host_list)) {
2901 ret = -ENODEV;
2902 goto err;
2903 }
2904
2905 return 0;
2906
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002907err:
2908#ifndef __UBOOT__
2909 clk_disable_unprepare(ctrl->clk);
2910#else
2911 if (ctrl->clk)
2912 clk_disable(ctrl->clk);
2913#endif /* __UBOOT__ */
2914 return ret;
Philippe Reynes5aa6cfb2019-03-15 15:14:36 +01002915}
2916EXPORT_SYMBOL_GPL(brcmnand_probe);
2917
2918#ifndef __UBOOT__
2919int brcmnand_remove(struct platform_device *pdev)
2920{
2921 struct brcmnand_controller *ctrl = dev_get_drvdata(&pdev->dev);
2922 struct brcmnand_host *host;
2923
2924 list_for_each_entry(host, &ctrl->host_list, node)
2925 nand_release(nand_to_mtd(&host->chip));
2926
2927 clk_disable_unprepare(ctrl->clk);
2928
2929 dev_set_drvdata(&pdev->dev, NULL);
2930
2931 return 0;
2932}
2933#else
2934int brcmnand_remove(struct udevice *pdev)
2935{
2936 return 0;
2937}
2938#endif /* __UBOOT__ */
2939EXPORT_SYMBOL_GPL(brcmnand_remove);
2940
2941MODULE_LICENSE("GPL v2");
2942MODULE_AUTHOR("Kevin Cernekee");
2943MODULE_AUTHOR("Brian Norris");
2944MODULE_DESCRIPTION("NAND driver for Broadcom chips");
2945MODULE_ALIAS("platform:brcmnand");