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