1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014       Panasonic Corporation
4  * Copyright (C) 2013-2014, Altera Corporation <www.altera.com>
5  * Copyright (C) 2009-2010, Intel Corporation and its suppliers.
6  */
7 
8 #include <dm.h>
9 #include <nand.h>
10 #include <linux/bitfield.h>
11 #include <linux/dma-direction.h>
12 #include <linux/errno.h>
13 #include <linux/io.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/mtd/rawnand.h>
16 
17 #include "denali.h"
18 
dma_map_single(void * dev,void * ptr,size_t size,enum dma_data_direction dir)19 static dma_addr_t dma_map_single(void *dev, void *ptr, size_t size,
20 				 enum dma_data_direction dir)
21 {
22 	unsigned long addr = (unsigned long)ptr;
23 
24 	if (dir == DMA_FROM_DEVICE)
25 		invalidate_dcache_range(addr, addr + size);
26 	else
27 		flush_dcache_range(addr, addr + size);
28 
29 	return addr;
30 }
31 
dma_unmap_single(void * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)32 static void dma_unmap_single(void *dev, dma_addr_t addr, size_t size,
33 			     enum dma_data_direction dir)
34 {
35 	if (dir != DMA_TO_DEVICE)
36 		invalidate_dcache_range(addr, addr + size);
37 }
38 
dma_mapping_error(void * dev,dma_addr_t addr)39 static int dma_mapping_error(void *dev, dma_addr_t addr)
40 {
41 	return 0;
42 }
43 
44 #define DENALI_NAND_NAME    "denali-nand"
45 
46 /* for Indexed Addressing */
47 #define DENALI_INDEXED_CTRL	0x00
48 #define DENALI_INDEXED_DATA	0x10
49 
50 #define DENALI_MAP00		(0 << 26)	/* direct access to buffer */
51 #define DENALI_MAP01		(1 << 26)	/* read/write pages in PIO */
52 #define DENALI_MAP10		(2 << 26)	/* high-level control plane */
53 #define DENALI_MAP11		(3 << 26)	/* direct controller access */
54 
55 /* MAP11 access cycle type */
56 #define DENALI_MAP11_CMD	((DENALI_MAP11) | 0)	/* command cycle */
57 #define DENALI_MAP11_ADDR	((DENALI_MAP11) | 1)	/* address cycle */
58 #define DENALI_MAP11_DATA	((DENALI_MAP11) | 2)	/* data cycle */
59 
60 /* MAP10 commands */
61 #define DENALI_ERASE		0x01
62 
63 #define DENALI_BANK(denali)	((denali)->active_bank << 24)
64 
65 #define DENALI_INVALID_BANK	-1
66 #define DENALI_NR_BANKS		4
67 
68 /*
69  * The bus interface clock, clk_x, is phase aligned with the core clock.  The
70  * clk_x is an integral multiple N of the core clk.  The value N is configured
71  * at IP delivery time, and its available value is 4, 5, or 6.  We need to align
72  * to the largest value to make it work with any possible configuration.
73  */
74 #define DENALI_CLK_X_MULT	6
75 
mtd_to_denali(struct mtd_info * mtd)76 static inline struct denali_nand_info *mtd_to_denali(struct mtd_info *mtd)
77 {
78 	return container_of(mtd_to_nand(mtd), struct denali_nand_info, nand);
79 }
80 
81 /*
82  * Direct Addressing - the slave address forms the control information (command
83  * type, bank, block, and page address).  The slave data is the actual data to
84  * be transferred.  This mode requires 28 bits of address region allocated.
85  */
denali_direct_read(struct denali_nand_info * denali,u32 addr)86 static u32 denali_direct_read(struct denali_nand_info *denali, u32 addr)
87 {
88 	return ioread32(denali->host + addr);
89 }
90 
denali_direct_write(struct denali_nand_info * denali,u32 addr,u32 data)91 static void denali_direct_write(struct denali_nand_info *denali, u32 addr,
92 				u32 data)
93 {
94 	iowrite32(data, denali->host + addr);
95 }
96 
97 /*
98  * Indexed Addressing - address translation module intervenes in passing the
99  * control information.  This mode reduces the required address range.  The
100  * control information and transferred data are latched by the registers in
101  * the translation module.
102  */
denali_indexed_read(struct denali_nand_info * denali,u32 addr)103 static u32 denali_indexed_read(struct denali_nand_info *denali, u32 addr)
104 {
105 	iowrite32(addr, denali->host + DENALI_INDEXED_CTRL);
106 	return ioread32(denali->host + DENALI_INDEXED_DATA);
107 }
108 
denali_indexed_write(struct denali_nand_info * denali,u32 addr,u32 data)109 static void denali_indexed_write(struct denali_nand_info *denali, u32 addr,
110 				 u32 data)
111 {
112 	iowrite32(addr, denali->host + DENALI_INDEXED_CTRL);
113 	iowrite32(data, denali->host + DENALI_INDEXED_DATA);
114 }
115 
116 /*
117  * Use the configuration feature register to determine the maximum number of
118  * banks that the hardware supports.
119  */
denali_detect_max_banks(struct denali_nand_info * denali)120 static void denali_detect_max_banks(struct denali_nand_info *denali)
121 {
122 	uint32_t features = ioread32(denali->reg + FEATURES);
123 
124 	denali->max_banks = 1 << FIELD_GET(FEATURES__N_BANKS, features);
125 
126 	/* the encoding changed from rev 5.0 to 5.1 */
127 	if (denali->revision < 0x0501)
128 		denali->max_banks <<= 1;
129 }
130 
denali_enable_irq(struct denali_nand_info * denali)131 static void __maybe_unused denali_enable_irq(struct denali_nand_info *denali)
132 {
133 	int i;
134 
135 	for (i = 0; i < DENALI_NR_BANKS; i++)
136 		iowrite32(U32_MAX, denali->reg + INTR_EN(i));
137 	iowrite32(GLOBAL_INT_EN_FLAG, denali->reg + GLOBAL_INT_ENABLE);
138 }
139 
denali_disable_irq(struct denali_nand_info * denali)140 static void __maybe_unused denali_disable_irq(struct denali_nand_info *denali)
141 {
142 	int i;
143 
144 	for (i = 0; i < DENALI_NR_BANKS; i++)
145 		iowrite32(0, denali->reg + INTR_EN(i));
146 	iowrite32(0, denali->reg + GLOBAL_INT_ENABLE);
147 }
148 
denali_clear_irq(struct denali_nand_info * denali,int bank,uint32_t irq_status)149 static void denali_clear_irq(struct denali_nand_info *denali,
150 			     int bank, uint32_t irq_status)
151 {
152 	/* write one to clear bits */
153 	iowrite32(irq_status, denali->reg + INTR_STATUS(bank));
154 }
155 
denali_clear_irq_all(struct denali_nand_info * denali)156 static void denali_clear_irq_all(struct denali_nand_info *denali)
157 {
158 	int i;
159 
160 	for (i = 0; i < DENALI_NR_BANKS; i++)
161 		denali_clear_irq(denali, i, U32_MAX);
162 }
163 
__denali_check_irq(struct denali_nand_info * denali)164 static void __denali_check_irq(struct denali_nand_info *denali)
165 {
166 	uint32_t irq_status;
167 	int i;
168 
169 	for (i = 0; i < DENALI_NR_BANKS; i++) {
170 		irq_status = ioread32(denali->reg + INTR_STATUS(i));
171 		denali_clear_irq(denali, i, irq_status);
172 
173 		if (i != denali->active_bank)
174 			continue;
175 
176 		denali->irq_status |= irq_status;
177 	}
178 }
179 
denali_reset_irq(struct denali_nand_info * denali)180 static void denali_reset_irq(struct denali_nand_info *denali)
181 {
182 	denali->irq_status = 0;
183 	denali->irq_mask = 0;
184 }
185 
denali_wait_for_irq(struct denali_nand_info * denali,uint32_t irq_mask)186 static uint32_t denali_wait_for_irq(struct denali_nand_info *denali,
187 				    uint32_t irq_mask)
188 {
189 	unsigned long time_left = 1000000;
190 
191 	while (time_left) {
192 		__denali_check_irq(denali);
193 
194 		if (irq_mask & denali->irq_status)
195 			return denali->irq_status;
196 		udelay(1);
197 		time_left--;
198 	}
199 
200 	if (!time_left) {
201 		dev_err(denali->dev, "timeout while waiting for irq 0x%x\n",
202 			irq_mask);
203 		return 0;
204 	}
205 
206 	return denali->irq_status;
207 }
208 
denali_check_irq(struct denali_nand_info * denali)209 static uint32_t denali_check_irq(struct denali_nand_info *denali)
210 {
211 	__denali_check_irq(denali);
212 
213 	return denali->irq_status;
214 }
215 
denali_read_buf(struct mtd_info * mtd,uint8_t * buf,int len)216 static void denali_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
217 {
218 	struct denali_nand_info *denali = mtd_to_denali(mtd);
219 	u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
220 	int i;
221 
222 	for (i = 0; i < len; i++)
223 		buf[i] = denali->host_read(denali, addr);
224 }
225 
denali_write_buf(struct mtd_info * mtd,const uint8_t * buf,int len)226 static void denali_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
227 {
228 	struct denali_nand_info *denali = mtd_to_denali(mtd);
229 	u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
230 	int i;
231 
232 	for (i = 0; i < len; i++)
233 		denali->host_write(denali, addr, buf[i]);
234 }
235 
denali_read_buf16(struct mtd_info * mtd,uint8_t * buf,int len)236 static void denali_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
237 {
238 	struct denali_nand_info *denali = mtd_to_denali(mtd);
239 	u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
240 	uint16_t *buf16 = (uint16_t *)buf;
241 	int i;
242 
243 	for (i = 0; i < len / 2; i++)
244 		buf16[i] = denali->host_read(denali, addr);
245 }
246 
denali_write_buf16(struct mtd_info * mtd,const uint8_t * buf,int len)247 static void denali_write_buf16(struct mtd_info *mtd, const uint8_t *buf,
248 			       int len)
249 {
250 	struct denali_nand_info *denali = mtd_to_denali(mtd);
251 	u32 addr = DENALI_MAP11_DATA | DENALI_BANK(denali);
252 	const uint16_t *buf16 = (const uint16_t *)buf;
253 	int i;
254 
255 	for (i = 0; i < len / 2; i++)
256 		denali->host_write(denali, addr, buf16[i]);
257 }
258 
denali_read_byte(struct mtd_info * mtd)259 static uint8_t denali_read_byte(struct mtd_info *mtd)
260 {
261 	uint8_t byte;
262 
263 	denali_read_buf(mtd, &byte, 1);
264 
265 	return byte;
266 }
267 
denali_write_byte(struct mtd_info * mtd,uint8_t byte)268 static void denali_write_byte(struct mtd_info *mtd, uint8_t byte)
269 {
270 	denali_write_buf(mtd, &byte, 1);
271 }
272 
denali_read_word(struct mtd_info * mtd)273 static uint16_t denali_read_word(struct mtd_info *mtd)
274 {
275 	uint16_t word;
276 
277 	denali_read_buf16(mtd, (uint8_t *)&word, 2);
278 
279 	return word;
280 }
281 
denali_cmd_ctrl(struct mtd_info * mtd,int dat,unsigned int ctrl)282 static void denali_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
283 {
284 	struct denali_nand_info *denali = mtd_to_denali(mtd);
285 	uint32_t type;
286 
287 	if (ctrl & NAND_CLE)
288 		type = DENALI_MAP11_CMD;
289 	else if (ctrl & NAND_ALE)
290 		type = DENALI_MAP11_ADDR;
291 	else
292 		return;
293 
294 	/*
295 	 * Some commands are followed by chip->dev_ready or chip->waitfunc.
296 	 * irq_status must be cleared here to catch the R/B# interrupt later.
297 	 */
298 	if (ctrl & NAND_CTRL_CHANGE)
299 		denali_reset_irq(denali);
300 
301 	denali->host_write(denali, DENALI_BANK(denali) | type, dat);
302 }
303 
denali_dev_ready(struct mtd_info * mtd)304 static int denali_dev_ready(struct mtd_info *mtd)
305 {
306 	struct denali_nand_info *denali = mtd_to_denali(mtd);
307 
308 	return !!(denali_check_irq(denali) & INTR__INT_ACT);
309 }
310 
denali_check_erased_page(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,unsigned long uncor_ecc_flags,unsigned int max_bitflips)311 static int denali_check_erased_page(struct mtd_info *mtd,
312 				    struct nand_chip *chip, uint8_t *buf,
313 				    unsigned long uncor_ecc_flags,
314 				    unsigned int max_bitflips)
315 {
316 	uint8_t *ecc_code = chip->buffers->ecccode;
317 	int ecc_steps = chip->ecc.steps;
318 	int ecc_size = chip->ecc.size;
319 	int ecc_bytes = chip->ecc.bytes;
320 	int i, ret, stat;
321 
322 	ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
323 					 chip->ecc.total);
324 	if (ret)
325 		return ret;
326 
327 	for (i = 0; i < ecc_steps; i++) {
328 		if (!(uncor_ecc_flags & BIT(i)))
329 			continue;
330 
331 		stat = nand_check_erased_ecc_chunk(buf, ecc_size,
332 						  ecc_code, ecc_bytes,
333 						  NULL, 0,
334 						  chip->ecc.strength);
335 		if (stat < 0) {
336 			mtd->ecc_stats.failed++;
337 		} else {
338 			mtd->ecc_stats.corrected += stat;
339 			max_bitflips = max_t(unsigned int, max_bitflips, stat);
340 		}
341 
342 		buf += ecc_size;
343 		ecc_code += ecc_bytes;
344 	}
345 
346 	return max_bitflips;
347 }
348 
denali_hw_ecc_fixup(struct mtd_info * mtd,struct denali_nand_info * denali,unsigned long * uncor_ecc_flags)349 static int denali_hw_ecc_fixup(struct mtd_info *mtd,
350 			       struct denali_nand_info *denali,
351 			       unsigned long *uncor_ecc_flags)
352 {
353 	struct nand_chip *chip = mtd_to_nand(mtd);
354 	int bank = denali->active_bank;
355 	uint32_t ecc_cor;
356 	unsigned int max_bitflips;
357 
358 	ecc_cor = ioread32(denali->reg + ECC_COR_INFO(bank));
359 	ecc_cor >>= ECC_COR_INFO__SHIFT(bank);
360 
361 	if (ecc_cor & ECC_COR_INFO__UNCOR_ERR) {
362 		/*
363 		 * This flag is set when uncorrectable error occurs at least in
364 		 * one ECC sector.  We can not know "how many sectors", or
365 		 * "which sector(s)".  We need erase-page check for all sectors.
366 		 */
367 		*uncor_ecc_flags = GENMASK(chip->ecc.steps - 1, 0);
368 		return 0;
369 	}
370 
371 	max_bitflips = FIELD_GET(ECC_COR_INFO__MAX_ERRORS, ecc_cor);
372 
373 	/*
374 	 * The register holds the maximum of per-sector corrected bitflips.
375 	 * This is suitable for the return value of the ->read_page() callback.
376 	 * Unfortunately, we can not know the total number of corrected bits in
377 	 * the page.  Increase the stats by max_bitflips. (compromised solution)
378 	 */
379 	mtd->ecc_stats.corrected += max_bitflips;
380 
381 	return max_bitflips;
382 }
383 
denali_sw_ecc_fixup(struct mtd_info * mtd,struct denali_nand_info * denali,unsigned long * uncor_ecc_flags,uint8_t * buf)384 static int denali_sw_ecc_fixup(struct mtd_info *mtd,
385 			       struct denali_nand_info *denali,
386 			       unsigned long *uncor_ecc_flags, uint8_t *buf)
387 {
388 	unsigned int ecc_size = denali->nand.ecc.size;
389 	unsigned int bitflips = 0;
390 	unsigned int max_bitflips = 0;
391 	uint32_t err_addr, err_cor_info;
392 	unsigned int err_byte, err_sector, err_device;
393 	uint8_t err_cor_value;
394 	unsigned int prev_sector = 0;
395 	uint32_t irq_status;
396 
397 	denali_reset_irq(denali);
398 
399 	do {
400 		err_addr = ioread32(denali->reg + ECC_ERROR_ADDRESS);
401 		err_sector = FIELD_GET(ECC_ERROR_ADDRESS__SECTOR, err_addr);
402 		err_byte = FIELD_GET(ECC_ERROR_ADDRESS__OFFSET, err_addr);
403 
404 		err_cor_info = ioread32(denali->reg + ERR_CORRECTION_INFO);
405 		err_cor_value = FIELD_GET(ERR_CORRECTION_INFO__BYTE,
406 					  err_cor_info);
407 		err_device = FIELD_GET(ERR_CORRECTION_INFO__DEVICE,
408 				       err_cor_info);
409 
410 		/* reset the bitflip counter when crossing ECC sector */
411 		if (err_sector != prev_sector)
412 			bitflips = 0;
413 
414 		if (err_cor_info & ERR_CORRECTION_INFO__UNCOR) {
415 			/*
416 			 * Check later if this is a real ECC error, or
417 			 * an erased sector.
418 			 */
419 			*uncor_ecc_flags |= BIT(err_sector);
420 		} else if (err_byte < ecc_size) {
421 			/*
422 			 * If err_byte is larger than ecc_size, means error
423 			 * happened in OOB, so we ignore it. It's no need for
424 			 * us to correct it err_device is represented the NAND
425 			 * error bits are happened in if there are more than
426 			 * one NAND connected.
427 			 */
428 			int offset;
429 			unsigned int flips_in_byte;
430 
431 			offset = (err_sector * ecc_size + err_byte) *
432 					denali->devs_per_cs + err_device;
433 
434 			/* correct the ECC error */
435 			flips_in_byte = hweight8(buf[offset] ^ err_cor_value);
436 			buf[offset] ^= err_cor_value;
437 			mtd->ecc_stats.corrected += flips_in_byte;
438 			bitflips += flips_in_byte;
439 
440 			max_bitflips = max(max_bitflips, bitflips);
441 		}
442 
443 		prev_sector = err_sector;
444 	} while (!(err_cor_info & ERR_CORRECTION_INFO__LAST_ERR));
445 
446 	/*
447 	 * Once handle all ECC errors, controller will trigger an
448 	 * ECC_TRANSACTION_DONE interrupt.
449 	 */
450 	irq_status = denali_wait_for_irq(denali, INTR__ECC_TRANSACTION_DONE);
451 	if (!(irq_status & INTR__ECC_TRANSACTION_DONE))
452 		return -EIO;
453 
454 	return max_bitflips;
455 }
456 
denali_setup_dma64(struct denali_nand_info * denali,dma_addr_t dma_addr,int page,int write)457 static void denali_setup_dma64(struct denali_nand_info *denali,
458 			       dma_addr_t dma_addr, int page, int write)
459 {
460 	uint32_t mode;
461 	const int page_count = 1;
462 
463 	mode = DENALI_MAP10 | DENALI_BANK(denali) | page;
464 
465 	/* DMA is a three step process */
466 
467 	/*
468 	 * 1. setup transfer type, interrupt when complete,
469 	 *    burst len = 64 bytes, the number of pages
470 	 */
471 	denali->host_write(denali, mode,
472 			   0x01002000 | (64 << 16) | (write << 8) | page_count);
473 
474 	/* 2. set memory low address */
475 	denali->host_write(denali, mode, lower_32_bits(dma_addr));
476 
477 	/* 3. set memory high address */
478 	denali->host_write(denali, mode, upper_32_bits(dma_addr));
479 }
480 
denali_setup_dma32(struct denali_nand_info * denali,dma_addr_t dma_addr,int page,int write)481 static void denali_setup_dma32(struct denali_nand_info *denali,
482 			       dma_addr_t dma_addr, int page, int write)
483 {
484 	uint32_t mode;
485 	const int page_count = 1;
486 
487 	mode = DENALI_MAP10 | DENALI_BANK(denali);
488 
489 	/* DMA is a four step process */
490 
491 	/* 1. setup transfer type and # of pages */
492 	denali->host_write(denali, mode | page,
493 			   0x2000 | (write << 8) | page_count);
494 
495 	/* 2. set memory high address bits 23:8 */
496 	denali->host_write(denali, mode | ((dma_addr >> 16) << 8), 0x2200);
497 
498 	/* 3. set memory low address bits 23:8 */
499 	denali->host_write(denali, mode | ((dma_addr & 0xffff) << 8), 0x2300);
500 
501 	/* 4. interrupt when complete, burst len = 64 bytes */
502 	denali->host_write(denali, mode | 0x14000, 0x2400);
503 }
504 
denali_pio_read(struct denali_nand_info * denali,void * buf,size_t size,int page,int raw)505 static int denali_pio_read(struct denali_nand_info *denali, void *buf,
506 			   size_t size, int page, int raw)
507 {
508 	u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page;
509 	uint32_t *buf32 = (uint32_t *)buf;
510 	uint32_t irq_status, ecc_err_mask;
511 	int i;
512 
513 	if (denali->caps & DENALI_CAP_HW_ECC_FIXUP)
514 		ecc_err_mask = INTR__ECC_UNCOR_ERR;
515 	else
516 		ecc_err_mask = INTR__ECC_ERR;
517 
518 	denali_reset_irq(denali);
519 
520 	for (i = 0; i < size / 4; i++)
521 		*buf32++ = denali->host_read(denali, addr);
522 
523 	irq_status = denali_wait_for_irq(denali, INTR__PAGE_XFER_INC);
524 	if (!(irq_status & INTR__PAGE_XFER_INC))
525 		return -EIO;
526 
527 	if (irq_status & INTR__ERASED_PAGE)
528 		memset(buf, 0xff, size);
529 
530 	return irq_status & ecc_err_mask ? -EBADMSG : 0;
531 }
532 
denali_pio_write(struct denali_nand_info * denali,const void * buf,size_t size,int page,int raw)533 static int denali_pio_write(struct denali_nand_info *denali,
534 			    const void *buf, size_t size, int page, int raw)
535 {
536 	u32 addr = DENALI_MAP01 | DENALI_BANK(denali) | page;
537 	const uint32_t *buf32 = (uint32_t *)buf;
538 	uint32_t irq_status;
539 	int i;
540 
541 	denali_reset_irq(denali);
542 
543 	for (i = 0; i < size / 4; i++)
544 		denali->host_write(denali, addr, *buf32++);
545 
546 	irq_status = denali_wait_for_irq(denali,
547 				INTR__PROGRAM_COMP | INTR__PROGRAM_FAIL);
548 	if (!(irq_status & INTR__PROGRAM_COMP))
549 		return -EIO;
550 
551 	return 0;
552 }
553 
denali_pio_xfer(struct denali_nand_info * denali,void * buf,size_t size,int page,int raw,int write)554 static int denali_pio_xfer(struct denali_nand_info *denali, void *buf,
555 			   size_t size, int page, int raw, int write)
556 {
557 	if (write)
558 		return denali_pio_write(denali, buf, size, page, raw);
559 	else
560 		return denali_pio_read(denali, buf, size, page, raw);
561 }
562 
denali_dma_xfer(struct denali_nand_info * denali,void * buf,size_t size,int page,int raw,int write)563 static int denali_dma_xfer(struct denali_nand_info *denali, void *buf,
564 			   size_t size, int page, int raw, int write)
565 {
566 	dma_addr_t dma_addr;
567 	uint32_t irq_mask, irq_status, ecc_err_mask;
568 	enum dma_data_direction dir = write ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
569 	int ret = 0;
570 
571 	dma_addr = dma_map_single(denali->dev, buf, size, dir);
572 	if (dma_mapping_error(denali->dev, dma_addr)) {
573 		dev_dbg(denali->dev, "Failed to DMA-map buffer. Trying PIO.\n");
574 		return denali_pio_xfer(denali, buf, size, page, raw, write);
575 	}
576 
577 	if (write) {
578 		/*
579 		 * INTR__PROGRAM_COMP is never asserted for the DMA transfer.
580 		 * We can use INTR__DMA_CMD_COMP instead.  This flag is asserted
581 		 * when the page program is completed.
582 		 */
583 		irq_mask = INTR__DMA_CMD_COMP | INTR__PROGRAM_FAIL;
584 		ecc_err_mask = 0;
585 	} else if (denali->caps & DENALI_CAP_HW_ECC_FIXUP) {
586 		irq_mask = INTR__DMA_CMD_COMP;
587 		ecc_err_mask = INTR__ECC_UNCOR_ERR;
588 	} else {
589 		irq_mask = INTR__DMA_CMD_COMP;
590 		ecc_err_mask = INTR__ECC_ERR;
591 	}
592 
593 	iowrite32(DMA_ENABLE__FLAG, denali->reg + DMA_ENABLE);
594 
595 	denali_reset_irq(denali);
596 	denali->setup_dma(denali, dma_addr, page, write);
597 
598 	irq_status = denali_wait_for_irq(denali, irq_mask);
599 	if (!(irq_status & INTR__DMA_CMD_COMP))
600 		ret = -EIO;
601 	else if (irq_status & ecc_err_mask)
602 		ret = -EBADMSG;
603 
604 	iowrite32(0, denali->reg + DMA_ENABLE);
605 
606 	dma_unmap_single(denali->dev, dma_addr, size, dir);
607 
608 	if (irq_status & INTR__ERASED_PAGE)
609 		memset(buf, 0xff, size);
610 
611 	return ret;
612 }
613 
denali_data_xfer(struct denali_nand_info * denali,void * buf,size_t size,int page,int raw,int write)614 static int denali_data_xfer(struct denali_nand_info *denali, void *buf,
615 			    size_t size, int page, int raw, int write)
616 {
617 	iowrite32(raw ? 0 : ECC_ENABLE__FLAG, denali->reg + ECC_ENABLE);
618 	iowrite32(raw ? TRANSFER_SPARE_REG__FLAG : 0,
619 		  denali->reg + TRANSFER_SPARE_REG);
620 
621 	if (denali->dma_avail)
622 		return denali_dma_xfer(denali, buf, size, page, raw, write);
623 	else
624 		return denali_pio_xfer(denali, buf, size, page, raw, write);
625 }
626 
denali_oob_xfer(struct mtd_info * mtd,struct nand_chip * chip,int page,int write)627 static void denali_oob_xfer(struct mtd_info *mtd, struct nand_chip *chip,
628 			    int page, int write)
629 {
630 	struct denali_nand_info *denali = mtd_to_denali(mtd);
631 	unsigned int start_cmd = write ? NAND_CMD_SEQIN : NAND_CMD_READ0;
632 	unsigned int rnd_cmd = write ? NAND_CMD_RNDIN : NAND_CMD_RNDOUT;
633 	int writesize = mtd->writesize;
634 	int oobsize = mtd->oobsize;
635 	uint8_t *bufpoi = chip->oob_poi;
636 	int ecc_steps = chip->ecc.steps;
637 	int ecc_size = chip->ecc.size;
638 	int ecc_bytes = chip->ecc.bytes;
639 	int oob_skip = denali->oob_skip_bytes;
640 	size_t size = writesize + oobsize;
641 	int i, pos, len;
642 
643 	/* BBM at the beginning of the OOB area */
644 	chip->cmdfunc(mtd, start_cmd, writesize, page);
645 	if (write)
646 		chip->write_buf(mtd, bufpoi, oob_skip);
647 	else
648 		chip->read_buf(mtd, bufpoi, oob_skip);
649 	bufpoi += oob_skip;
650 
651 	/* OOB ECC */
652 	for (i = 0; i < ecc_steps; i++) {
653 		pos = ecc_size + i * (ecc_size + ecc_bytes);
654 		len = ecc_bytes;
655 
656 		if (pos >= writesize)
657 			pos += oob_skip;
658 		else if (pos + len > writesize)
659 			len = writesize - pos;
660 
661 		chip->cmdfunc(mtd, rnd_cmd, pos, -1);
662 		if (write)
663 			chip->write_buf(mtd, bufpoi, len);
664 		else
665 			chip->read_buf(mtd, bufpoi, len);
666 		bufpoi += len;
667 		if (len < ecc_bytes) {
668 			len = ecc_bytes - len;
669 			chip->cmdfunc(mtd, rnd_cmd, writesize + oob_skip, -1);
670 			if (write)
671 				chip->write_buf(mtd, bufpoi, len);
672 			else
673 				chip->read_buf(mtd, bufpoi, len);
674 			bufpoi += len;
675 		}
676 	}
677 
678 	/* OOB free */
679 	len = oobsize - (bufpoi - chip->oob_poi);
680 	chip->cmdfunc(mtd, rnd_cmd, size - len, -1);
681 	if (write)
682 		chip->write_buf(mtd, bufpoi, len);
683 	else
684 		chip->read_buf(mtd, bufpoi, len);
685 }
686 
denali_read_page_raw(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)687 static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
688 				uint8_t *buf, int oob_required, int page)
689 {
690 	struct denali_nand_info *denali = mtd_to_denali(mtd);
691 	int writesize = mtd->writesize;
692 	int oobsize = mtd->oobsize;
693 	int ecc_steps = chip->ecc.steps;
694 	int ecc_size = chip->ecc.size;
695 	int ecc_bytes = chip->ecc.bytes;
696 	void *tmp_buf = denali->buf;
697 	int oob_skip = denali->oob_skip_bytes;
698 	size_t size = writesize + oobsize;
699 	int ret, i, pos, len;
700 
701 	ret = denali_data_xfer(denali, tmp_buf, size, page, 1, 0);
702 	if (ret)
703 		return ret;
704 
705 	/* Arrange the buffer for syndrome payload/ecc layout */
706 	if (buf) {
707 		for (i = 0; i < ecc_steps; i++) {
708 			pos = i * (ecc_size + ecc_bytes);
709 			len = ecc_size;
710 
711 			if (pos >= writesize)
712 				pos += oob_skip;
713 			else if (pos + len > writesize)
714 				len = writesize - pos;
715 
716 			memcpy(buf, tmp_buf + pos, len);
717 			buf += len;
718 			if (len < ecc_size) {
719 				len = ecc_size - len;
720 				memcpy(buf, tmp_buf + writesize + oob_skip,
721 				       len);
722 				buf += len;
723 			}
724 		}
725 	}
726 
727 	if (oob_required) {
728 		uint8_t *oob = chip->oob_poi;
729 
730 		/* BBM at the beginning of the OOB area */
731 		memcpy(oob, tmp_buf + writesize, oob_skip);
732 		oob += oob_skip;
733 
734 		/* OOB ECC */
735 		for (i = 0; i < ecc_steps; i++) {
736 			pos = ecc_size + i * (ecc_size + ecc_bytes);
737 			len = ecc_bytes;
738 
739 			if (pos >= writesize)
740 				pos += oob_skip;
741 			else if (pos + len > writesize)
742 				len = writesize - pos;
743 
744 			memcpy(oob, tmp_buf + pos, len);
745 			oob += len;
746 			if (len < ecc_bytes) {
747 				len = ecc_bytes - len;
748 				memcpy(oob, tmp_buf + writesize + oob_skip,
749 				       len);
750 				oob += len;
751 			}
752 		}
753 
754 		/* OOB free */
755 		len = oobsize - (oob - chip->oob_poi);
756 		memcpy(oob, tmp_buf + size - len, len);
757 	}
758 
759 	return 0;
760 }
761 
denali_read_oob(struct mtd_info * mtd,struct nand_chip * chip,int page)762 static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
763 			   int page)
764 {
765 	denali_oob_xfer(mtd, chip, page, 0);
766 
767 	return 0;
768 }
769 
denali_write_oob(struct mtd_info * mtd,struct nand_chip * chip,int page)770 static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
771 			    int page)
772 {
773 	struct denali_nand_info *denali = mtd_to_denali(mtd);
774 	int status;
775 
776 	denali_reset_irq(denali);
777 
778 	denali_oob_xfer(mtd, chip, page, 1);
779 
780 	chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
781 	status = chip->waitfunc(mtd, chip);
782 
783 	return status & NAND_STATUS_FAIL ? -EIO : 0;
784 }
785 
denali_read_page(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)786 static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
787 			    uint8_t *buf, int oob_required, int page)
788 {
789 	struct denali_nand_info *denali = mtd_to_denali(mtd);
790 	unsigned long uncor_ecc_flags = 0;
791 	int stat = 0;
792 	int ret;
793 
794 	ret = denali_data_xfer(denali, buf, mtd->writesize, page, 0, 0);
795 	if (ret && ret != -EBADMSG)
796 		return ret;
797 
798 	if (denali->caps & DENALI_CAP_HW_ECC_FIXUP)
799 		stat = denali_hw_ecc_fixup(mtd, denali, &uncor_ecc_flags);
800 	else if (ret == -EBADMSG)
801 		stat = denali_sw_ecc_fixup(mtd, denali, &uncor_ecc_flags, buf);
802 
803 	if (stat < 0)
804 		return stat;
805 
806 	if (uncor_ecc_flags) {
807 		ret = denali_read_oob(mtd, chip, page);
808 		if (ret)
809 			return ret;
810 
811 		stat = denali_check_erased_page(mtd, chip, buf,
812 						uncor_ecc_flags, stat);
813 	}
814 
815 	return stat;
816 }
817 
denali_write_page_raw(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)818 static int denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
819 				 const uint8_t *buf, int oob_required, int page)
820 {
821 	struct denali_nand_info *denali = mtd_to_denali(mtd);
822 	int writesize = mtd->writesize;
823 	int oobsize = mtd->oobsize;
824 	int ecc_steps = chip->ecc.steps;
825 	int ecc_size = chip->ecc.size;
826 	int ecc_bytes = chip->ecc.bytes;
827 	void *tmp_buf = denali->buf;
828 	int oob_skip = denali->oob_skip_bytes;
829 	size_t size = writesize + oobsize;
830 	int i, pos, len;
831 
832 	/*
833 	 * Fill the buffer with 0xff first except the full page transfer.
834 	 * This simplifies the logic.
835 	 */
836 	if (!buf || !oob_required)
837 		memset(tmp_buf, 0xff, size);
838 
839 	/* Arrange the buffer for syndrome payload/ecc layout */
840 	if (buf) {
841 		for (i = 0; i < ecc_steps; i++) {
842 			pos = i * (ecc_size + ecc_bytes);
843 			len = ecc_size;
844 
845 			if (pos >= writesize)
846 				pos += oob_skip;
847 			else if (pos + len > writesize)
848 				len = writesize - pos;
849 
850 			memcpy(tmp_buf + pos, buf, len);
851 			buf += len;
852 			if (len < ecc_size) {
853 				len = ecc_size - len;
854 				memcpy(tmp_buf + writesize + oob_skip, buf,
855 				       len);
856 				buf += len;
857 			}
858 		}
859 	}
860 
861 	if (oob_required) {
862 		const uint8_t *oob = chip->oob_poi;
863 
864 		/* BBM at the beginning of the OOB area */
865 		memcpy(tmp_buf + writesize, oob, oob_skip);
866 		oob += oob_skip;
867 
868 		/* OOB ECC */
869 		for (i = 0; i < ecc_steps; i++) {
870 			pos = ecc_size + i * (ecc_size + ecc_bytes);
871 			len = ecc_bytes;
872 
873 			if (pos >= writesize)
874 				pos += oob_skip;
875 			else if (pos + len > writesize)
876 				len = writesize - pos;
877 
878 			memcpy(tmp_buf + pos, oob, len);
879 			oob += len;
880 			if (len < ecc_bytes) {
881 				len = ecc_bytes - len;
882 				memcpy(tmp_buf + writesize + oob_skip, oob,
883 				       len);
884 				oob += len;
885 			}
886 		}
887 
888 		/* OOB free */
889 		len = oobsize - (oob - chip->oob_poi);
890 		memcpy(tmp_buf + size - len, oob, len);
891 	}
892 
893 	return denali_data_xfer(denali, tmp_buf, size, page, 1, 1);
894 }
895 
denali_write_page(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)896 static int denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
897 			     const uint8_t *buf, int oob_required, int page)
898 {
899 	struct denali_nand_info *denali = mtd_to_denali(mtd);
900 
901 	return denali_data_xfer(denali, (void *)buf, mtd->writesize,
902 				page, 0, 1);
903 }
904 
denali_select_chip(struct mtd_info * mtd,int chip)905 static void denali_select_chip(struct mtd_info *mtd, int chip)
906 {
907 	struct denali_nand_info *denali = mtd_to_denali(mtd);
908 
909 	denali->active_bank = chip;
910 }
911 
denali_waitfunc(struct mtd_info * mtd,struct nand_chip * chip)912 static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
913 {
914 	struct denali_nand_info *denali = mtd_to_denali(mtd);
915 	uint32_t irq_status;
916 
917 	/* R/B# pin transitioned from low to high? */
918 	irq_status = denali_wait_for_irq(denali, INTR__INT_ACT);
919 
920 	return irq_status & INTR__INT_ACT ? 0 : NAND_STATUS_FAIL;
921 }
922 
denali_erase(struct mtd_info * mtd,int page)923 static int denali_erase(struct mtd_info *mtd, int page)
924 {
925 	struct denali_nand_info *denali = mtd_to_denali(mtd);
926 	uint32_t irq_status;
927 
928 	denali_reset_irq(denali);
929 
930 	denali->host_write(denali, DENALI_MAP10 | DENALI_BANK(denali) | page,
931 			   DENALI_ERASE);
932 
933 	/* wait for erase to complete or failure to occur */
934 	irq_status = denali_wait_for_irq(denali,
935 					 INTR__ERASE_COMP | INTR__ERASE_FAIL);
936 
937 	return irq_status & INTR__ERASE_COMP ? 0 : NAND_STATUS_FAIL;
938 }
939 
denali_setup_data_interface(struct mtd_info * mtd,int chipnr,const struct nand_data_interface * conf)940 static int denali_setup_data_interface(struct mtd_info *mtd, int chipnr,
941 				       const struct nand_data_interface *conf)
942 {
943 	struct denali_nand_info *denali = mtd_to_denali(mtd);
944 	const struct nand_sdr_timings *timings;
945 	unsigned long t_clk;
946 	int acc_clks, re_2_we, re_2_re, we_2_re, addr_2_data;
947 	int rdwr_en_lo, rdwr_en_hi, rdwr_en_lo_hi, cs_setup;
948 	int addr_2_data_mask;
949 	uint32_t tmp;
950 
951 	timings = nand_get_sdr_timings(conf);
952 	if (IS_ERR(timings))
953 		return PTR_ERR(timings);
954 
955 	/* clk_x period in picoseconds */
956 	t_clk = DIV_ROUND_DOWN_ULL(1000000000000ULL, denali->clk_x_rate);
957 	if (!t_clk)
958 		return -EINVAL;
959 
960 	if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
961 		return 0;
962 
963 	/* tREA -> ACC_CLKS */
964 	acc_clks = DIV_ROUND_UP(timings->tREA_max, t_clk);
965 	acc_clks = min_t(int, acc_clks, ACC_CLKS__VALUE);
966 
967 	tmp = ioread32(denali->reg + ACC_CLKS);
968 	tmp &= ~ACC_CLKS__VALUE;
969 	tmp |= FIELD_PREP(ACC_CLKS__VALUE, acc_clks);
970 	iowrite32(tmp, denali->reg + ACC_CLKS);
971 
972 	/* tRWH -> RE_2_WE */
973 	re_2_we = DIV_ROUND_UP(timings->tRHW_min, t_clk);
974 	re_2_we = min_t(int, re_2_we, RE_2_WE__VALUE);
975 
976 	tmp = ioread32(denali->reg + RE_2_WE);
977 	tmp &= ~RE_2_WE__VALUE;
978 	tmp |= FIELD_PREP(RE_2_WE__VALUE, re_2_we);
979 	iowrite32(tmp, denali->reg + RE_2_WE);
980 
981 	/* tRHZ -> RE_2_RE */
982 	re_2_re = DIV_ROUND_UP(timings->tRHZ_max, t_clk);
983 	re_2_re = min_t(int, re_2_re, RE_2_RE__VALUE);
984 
985 	tmp = ioread32(denali->reg + RE_2_RE);
986 	tmp &= ~RE_2_RE__VALUE;
987 	tmp |= FIELD_PREP(RE_2_RE__VALUE, re_2_re);
988 	iowrite32(tmp, denali->reg + RE_2_RE);
989 
990 	/*
991 	 * tCCS, tWHR -> WE_2_RE
992 	 *
993 	 * With WE_2_RE properly set, the Denali controller automatically takes
994 	 * care of the delay; the driver need not set NAND_WAIT_TCCS.
995 	 */
996 	we_2_re = DIV_ROUND_UP(max(timings->tCCS_min, timings->tWHR_min),
997 			       t_clk);
998 	we_2_re = min_t(int, we_2_re, TWHR2_AND_WE_2_RE__WE_2_RE);
999 
1000 	tmp = ioread32(denali->reg + TWHR2_AND_WE_2_RE);
1001 	tmp &= ~TWHR2_AND_WE_2_RE__WE_2_RE;
1002 	tmp |= FIELD_PREP(TWHR2_AND_WE_2_RE__WE_2_RE, we_2_re);
1003 	iowrite32(tmp, denali->reg + TWHR2_AND_WE_2_RE);
1004 
1005 	/* tADL -> ADDR_2_DATA */
1006 
1007 	/* for older versions, ADDR_2_DATA is only 6 bit wide */
1008 	addr_2_data_mask = TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA;
1009 	if (denali->revision < 0x0501)
1010 		addr_2_data_mask >>= 1;
1011 
1012 	addr_2_data = DIV_ROUND_UP(timings->tADL_min, t_clk);
1013 	addr_2_data = min_t(int, addr_2_data, addr_2_data_mask);
1014 
1015 	tmp = ioread32(denali->reg + TCWAW_AND_ADDR_2_DATA);
1016 	tmp &= ~TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA;
1017 	tmp |= FIELD_PREP(TCWAW_AND_ADDR_2_DATA__ADDR_2_DATA, addr_2_data);
1018 	iowrite32(tmp, denali->reg + TCWAW_AND_ADDR_2_DATA);
1019 
1020 	/* tREH, tWH -> RDWR_EN_HI_CNT */
1021 	rdwr_en_hi = DIV_ROUND_UP(max(timings->tREH_min, timings->tWH_min),
1022 				  t_clk);
1023 	rdwr_en_hi = min_t(int, rdwr_en_hi, RDWR_EN_HI_CNT__VALUE);
1024 
1025 	tmp = ioread32(denali->reg + RDWR_EN_HI_CNT);
1026 	tmp &= ~RDWR_EN_HI_CNT__VALUE;
1027 	tmp |= FIELD_PREP(RDWR_EN_HI_CNT__VALUE, rdwr_en_hi);
1028 	iowrite32(tmp, denali->reg + RDWR_EN_HI_CNT);
1029 
1030 	/* tRP, tWP -> RDWR_EN_LO_CNT */
1031 	rdwr_en_lo = DIV_ROUND_UP(max(timings->tRP_min, timings->tWP_min),
1032 				  t_clk);
1033 	rdwr_en_lo_hi = DIV_ROUND_UP(max(timings->tRC_min, timings->tWC_min),
1034 				     t_clk);
1035 	rdwr_en_lo_hi = max(rdwr_en_lo_hi, DENALI_CLK_X_MULT);
1036 	rdwr_en_lo = max(rdwr_en_lo, rdwr_en_lo_hi - rdwr_en_hi);
1037 	rdwr_en_lo = min_t(int, rdwr_en_lo, RDWR_EN_LO_CNT__VALUE);
1038 
1039 	tmp = ioread32(denali->reg + RDWR_EN_LO_CNT);
1040 	tmp &= ~RDWR_EN_LO_CNT__VALUE;
1041 	tmp |= FIELD_PREP(RDWR_EN_LO_CNT__VALUE, rdwr_en_lo);
1042 	iowrite32(tmp, denali->reg + RDWR_EN_LO_CNT);
1043 
1044 	/* tCS, tCEA -> CS_SETUP_CNT */
1045 	cs_setup = max3((int)DIV_ROUND_UP(timings->tCS_min, t_clk) - rdwr_en_lo,
1046 			(int)DIV_ROUND_UP(timings->tCEA_max, t_clk) - acc_clks,
1047 			0);
1048 	cs_setup = min_t(int, cs_setup, CS_SETUP_CNT__VALUE);
1049 
1050 	tmp = ioread32(denali->reg + CS_SETUP_CNT);
1051 	tmp &= ~CS_SETUP_CNT__VALUE;
1052 	tmp |= FIELD_PREP(CS_SETUP_CNT__VALUE, cs_setup);
1053 	iowrite32(tmp, denali->reg + CS_SETUP_CNT);
1054 
1055 	return 0;
1056 }
1057 
denali_reset_banks(struct denali_nand_info * denali)1058 static void denali_reset_banks(struct denali_nand_info *denali)
1059 {
1060 	u32 irq_status;
1061 	int i;
1062 
1063 	for (i = 0; i < denali->max_banks; i++) {
1064 		denali->active_bank = i;
1065 
1066 		denali_reset_irq(denali);
1067 
1068 		iowrite32(DEVICE_RESET__BANK(i),
1069 			  denali->reg + DEVICE_RESET);
1070 
1071 		irq_status = denali_wait_for_irq(denali,
1072 			INTR__RST_COMP | INTR__INT_ACT | INTR__TIME_OUT);
1073 		if (!(irq_status & INTR__INT_ACT))
1074 			break;
1075 	}
1076 
1077 	dev_dbg(denali->dev, "%d chips connected\n", i);
1078 	denali->max_banks = i;
1079 }
1080 
denali_hw_init(struct denali_nand_info * denali)1081 static void denali_hw_init(struct denali_nand_info *denali)
1082 {
1083 	/*
1084 	 * The REVISION register may not be reliable.  Platforms are allowed to
1085 	 * override it.
1086 	 */
1087 	if (!denali->revision)
1088 		denali->revision = swab16(ioread32(denali->reg + REVISION));
1089 
1090 	/*
1091 	 * tell driver how many bit controller will skip before writing
1092 	 * ECC code in OOB. This is normally used for bad block marker
1093 	 */
1094 	denali->oob_skip_bytes = CONFIG_NAND_DENALI_SPARE_AREA_SKIP_BYTES;
1095 	iowrite32(denali->oob_skip_bytes, denali->reg + SPARE_AREA_SKIP_BYTES);
1096 	denali_detect_max_banks(denali);
1097 	iowrite32(0x0F, denali->reg + RB_PIN_ENABLED);
1098 	iowrite32(CHIP_EN_DONT_CARE__FLAG, denali->reg + CHIP_ENABLE_DONT_CARE);
1099 
1100 	iowrite32(0xffff, denali->reg + SPARE_AREA_MARKER);
1101 }
1102 
denali_calc_ecc_bytes(int step_size,int strength)1103 int denali_calc_ecc_bytes(int step_size, int strength)
1104 {
1105 	/* BCH code.  Denali requires ecc.bytes to be multiple of 2 */
1106 	return DIV_ROUND_UP(strength * fls(step_size * 8), 16) * 2;
1107 }
1108 EXPORT_SYMBOL(denali_calc_ecc_bytes);
1109 
denali_ecc_setup(struct mtd_info * mtd,struct nand_chip * chip,struct denali_nand_info * denali)1110 static int denali_ecc_setup(struct mtd_info *mtd, struct nand_chip *chip,
1111 			    struct denali_nand_info *denali)
1112 {
1113 	int oobavail = mtd->oobsize - denali->oob_skip_bytes;
1114 	int ret;
1115 
1116 	/*
1117 	 * If .size and .strength are already set (usually by DT),
1118 	 * check if they are supported by this controller.
1119 	 */
1120 	if (chip->ecc.size && chip->ecc.strength)
1121 		return nand_check_ecc_caps(chip, denali->ecc_caps, oobavail);
1122 
1123 	/*
1124 	 * We want .size and .strength closest to the chip's requirement
1125 	 * unless NAND_ECC_MAXIMIZE is requested.
1126 	 */
1127 	if (!(chip->ecc.options & NAND_ECC_MAXIMIZE)) {
1128 		ret = nand_match_ecc_req(chip, denali->ecc_caps, oobavail);
1129 		if (!ret)
1130 			return 0;
1131 	}
1132 
1133 	/* Max ECC strength is the last thing we can do */
1134 	return nand_maximize_ecc(chip, denali->ecc_caps, oobavail);
1135 }
1136 
1137 static struct nand_ecclayout nand_oob;
1138 
denali_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)1139 static int denali_ooblayout_ecc(struct mtd_info *mtd, int section,
1140 				struct mtd_oob_region *oobregion)
1141 {
1142 	struct denali_nand_info *denali = mtd_to_denali(mtd);
1143 	struct nand_chip *chip = mtd_to_nand(mtd);
1144 
1145 	if (section)
1146 		return -ERANGE;
1147 
1148 	oobregion->offset = denali->oob_skip_bytes;
1149 	oobregion->length = chip->ecc.total;
1150 
1151 	return 0;
1152 }
1153 
denali_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)1154 static int denali_ooblayout_free(struct mtd_info *mtd, int section,
1155 				 struct mtd_oob_region *oobregion)
1156 {
1157 	struct denali_nand_info *denali = mtd_to_denali(mtd);
1158 	struct nand_chip *chip = mtd_to_nand(mtd);
1159 
1160 	if (section)
1161 		return -ERANGE;
1162 
1163 	oobregion->offset = chip->ecc.total + denali->oob_skip_bytes;
1164 	oobregion->length = mtd->oobsize - oobregion->offset;
1165 
1166 	return 0;
1167 }
1168 
1169 static const struct mtd_ooblayout_ops denali_ooblayout_ops = {
1170 	.ecc = denali_ooblayout_ecc,
1171 	.free = denali_ooblayout_free,
1172 };
1173 
denali_multidev_fixup(struct denali_nand_info * denali)1174 static int denali_multidev_fixup(struct denali_nand_info *denali)
1175 {
1176 	struct nand_chip *chip = &denali->nand;
1177 	struct mtd_info *mtd = nand_to_mtd(chip);
1178 
1179 	/*
1180 	 * Support for multi device:
1181 	 * When the IP configuration is x16 capable and two x8 chips are
1182 	 * connected in parallel, DEVICES_CONNECTED should be set to 2.
1183 	 * In this case, the core framework knows nothing about this fact,
1184 	 * so we should tell it the _logical_ pagesize and anything necessary.
1185 	 */
1186 	denali->devs_per_cs = ioread32(denali->reg + DEVICES_CONNECTED);
1187 
1188 	/*
1189 	 * On some SoCs, DEVICES_CONNECTED is not auto-detected.
1190 	 * For those, DEVICES_CONNECTED is left to 0.  Set 1 if it is the case.
1191 	 */
1192 	if (denali->devs_per_cs == 0) {
1193 		denali->devs_per_cs = 1;
1194 		iowrite32(1, denali->reg + DEVICES_CONNECTED);
1195 	}
1196 
1197 	if (denali->devs_per_cs == 1)
1198 		return 0;
1199 
1200 	if (denali->devs_per_cs != 2) {
1201 		dev_err(denali->dev, "unsupported number of devices %d\n",
1202 			denali->devs_per_cs);
1203 		return -EINVAL;
1204 	}
1205 
1206 	/* 2 chips in parallel */
1207 	mtd->size <<= 1;
1208 	mtd->erasesize <<= 1;
1209 	mtd->writesize <<= 1;
1210 	mtd->oobsize <<= 1;
1211 	chip->chipsize <<= 1;
1212 	chip->page_shift += 1;
1213 	chip->phys_erase_shift += 1;
1214 	chip->bbt_erase_shift += 1;
1215 	chip->chip_shift += 1;
1216 	chip->pagemask <<= 1;
1217 	chip->ecc.size <<= 1;
1218 	chip->ecc.bytes <<= 1;
1219 	chip->ecc.strength <<= 1;
1220 	denali->oob_skip_bytes <<= 1;
1221 
1222 	return 0;
1223 }
1224 
denali_init(struct denali_nand_info * denali)1225 int denali_init(struct denali_nand_info *denali)
1226 {
1227 	struct nand_chip *chip = &denali->nand;
1228 	struct mtd_info *mtd = nand_to_mtd(chip);
1229 	u32 features = ioread32(denali->reg + FEATURES);
1230 	int ret;
1231 
1232 	denali_hw_init(denali);
1233 
1234 	denali_clear_irq_all(denali);
1235 
1236 	denali_reset_banks(denali);
1237 
1238 	denali->active_bank = DENALI_INVALID_BANK;
1239 
1240 	chip->flash_node = dev_of_offset(denali->dev);
1241 	/* Fallback to the default name if DT did not give "label" property */
1242 	if (!mtd->name)
1243 		mtd->name = "denali-nand";
1244 
1245 	chip->select_chip = denali_select_chip;
1246 	chip->read_byte = denali_read_byte;
1247 	chip->write_byte = denali_write_byte;
1248 	chip->read_word = denali_read_word;
1249 	chip->cmd_ctrl = denali_cmd_ctrl;
1250 	chip->dev_ready = denali_dev_ready;
1251 	chip->waitfunc = denali_waitfunc;
1252 
1253 	if (features & FEATURES__INDEX_ADDR) {
1254 		denali->host_read = denali_indexed_read;
1255 		denali->host_write = denali_indexed_write;
1256 	} else {
1257 		denali->host_read = denali_direct_read;
1258 		denali->host_write = denali_direct_write;
1259 	}
1260 
1261 	/* clk rate info is needed for setup_data_interface */
1262 	if (denali->clk_x_rate)
1263 		chip->setup_data_interface = denali_setup_data_interface;
1264 
1265 	ret = nand_scan_ident(mtd, denali->max_banks, NULL);
1266 	if (ret)
1267 		return ret;
1268 
1269 	if (ioread32(denali->reg + FEATURES) & FEATURES__DMA)
1270 		denali->dma_avail = 1;
1271 
1272 	if (denali->dma_avail) {
1273 		chip->buf_align = 16;
1274 		if (denali->caps & DENALI_CAP_DMA_64BIT)
1275 			denali->setup_dma = denali_setup_dma64;
1276 		else
1277 			denali->setup_dma = denali_setup_dma32;
1278 	} else {
1279 		chip->buf_align = 4;
1280 	}
1281 
1282 	chip->options |= NAND_USE_BOUNCE_BUFFER;
1283 	chip->bbt_options |= NAND_BBT_USE_FLASH;
1284 	chip->bbt_options |= NAND_BBT_NO_OOB;
1285 	denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
1286 
1287 	/* no subpage writes on denali */
1288 	chip->options |= NAND_NO_SUBPAGE_WRITE;
1289 
1290 	ret = denali_ecc_setup(mtd, chip, denali);
1291 	if (ret) {
1292 		dev_err(denali->dev, "Failed to setup ECC settings.\n");
1293 		return ret;
1294 	}
1295 
1296 	dev_dbg(denali->dev,
1297 		"chosen ECC settings: step=%d, strength=%d, bytes=%d\n",
1298 		chip->ecc.size, chip->ecc.strength, chip->ecc.bytes);
1299 
1300 	iowrite32(FIELD_PREP(ECC_CORRECTION__ERASE_THRESHOLD, 1) |
1301 		  FIELD_PREP(ECC_CORRECTION__VALUE, chip->ecc.strength),
1302 		  denali->reg + ECC_CORRECTION);
1303 	iowrite32(mtd->erasesize / mtd->writesize,
1304 		  denali->reg + PAGES_PER_BLOCK);
1305 	iowrite32(chip->options & NAND_BUSWIDTH_16 ? 1 : 0,
1306 		  denali->reg + DEVICE_WIDTH);
1307 	iowrite32(chip->options & NAND_ROW_ADDR_3 ? 0 : TWO_ROW_ADDR_CYCLES__FLAG,
1308 		  denali->reg + TWO_ROW_ADDR_CYCLES);
1309 	iowrite32(mtd->writesize, denali->reg + DEVICE_MAIN_AREA_SIZE);
1310 	iowrite32(mtd->oobsize, denali->reg + DEVICE_SPARE_AREA_SIZE);
1311 
1312 	iowrite32(chip->ecc.size, denali->reg + CFG_DATA_BLOCK_SIZE);
1313 	iowrite32(chip->ecc.size, denali->reg + CFG_LAST_DATA_BLOCK_SIZE);
1314 	/* chip->ecc.steps is set by nand_scan_tail(); not available here */
1315 	iowrite32(mtd->writesize / chip->ecc.size,
1316 		  denali->reg + CFG_NUM_DATA_BLOCKS);
1317 
1318 	mtd_set_ooblayout(mtd, &denali_ooblayout_ops);
1319 
1320 	nand_oob.eccbytes = denali->nand.ecc.bytes;
1321 	denali->nand.ecc.layout = &nand_oob;
1322 
1323 	if (chip->options & NAND_BUSWIDTH_16) {
1324 		chip->read_buf = denali_read_buf16;
1325 		chip->write_buf = denali_write_buf16;
1326 	} else {
1327 		chip->read_buf = denali_read_buf;
1328 		chip->write_buf = denali_write_buf;
1329 	}
1330 	chip->ecc.options |= NAND_ECC_CUSTOM_PAGE_ACCESS;
1331 	chip->ecc.read_page = denali_read_page;
1332 	chip->ecc.read_page_raw = denali_read_page_raw;
1333 	chip->ecc.write_page = denali_write_page;
1334 	chip->ecc.write_page_raw = denali_write_page_raw;
1335 	chip->ecc.read_oob = denali_read_oob;
1336 	chip->ecc.write_oob = denali_write_oob;
1337 	chip->erase = denali_erase;
1338 
1339 	ret = denali_multidev_fixup(denali);
1340 	if (ret)
1341 		return ret;
1342 
1343 	/*
1344 	 * This buffer is DMA-mapped by denali_{read,write}_page_raw.  Do not
1345 	 * use devm_kmalloc() because the memory allocated by devm_ does not
1346 	 * guarantee DMA-safe alignment.
1347 	 */
1348 	denali->buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
1349 	if (!denali->buf)
1350 		return -ENOMEM;
1351 
1352 	ret = nand_scan_tail(mtd);
1353 	if (ret)
1354 		goto free_buf;
1355 
1356 	ret = nand_register(0, mtd);
1357 	if (ret) {
1358 		dev_err(denali->dev, "Failed to register MTD: %d\n", ret);
1359 		goto free_buf;
1360 	}
1361 	return 0;
1362 
1363 free_buf:
1364 	kfree(denali->buf);
1365 
1366 	return ret;
1367 }
1368