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