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