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