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