]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - drivers/mtd/nand/mpc5121_nfc.c
imported U-Boot Release 2009.08 from Freescale BSP L2.6.31_10.08.01
[karo-tx-uboot.git] / drivers / mtd / nand / mpc5121_nfc.c
1 /*
2  * Copyright 2004-2008 Freescale Semiconductor, Inc.
3  * Copyright 2009 Semihalf.
4  * (C) Copyright 2009 Stefan Roese <sr@denx.de>
5  *
6  * Based on original driver from Freescale Semiconductor
7  * written by John Rigby <jrigby@freescale.com> on basis
8  * of drivers/mtd/nand/mxc_nand.c. Reworked and extended
9  * Piotr Ziecik <kosmo@semihalf.com>.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23  * MA 02110-1301, USA.
24  */
25
26 #include <common.h>
27 #include <malloc.h>
28
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/nand.h>
31 #include <linux/mtd/nand_ecc.h>
32 #include <linux/mtd/compat.h>
33
34 #include <asm/errno.h>
35 #include <asm/io.h>
36 #include <asm/processor.h>
37 #include <nand.h>
38
39 #define DRV_NAME                "mpc5121_nfc"
40
41 /* Timeouts */
42 #define NFC_RESET_TIMEOUT       1000    /* 1 ms */
43 #define NFC_TIMEOUT             2000    /* 2000 us */
44
45 /* Addresses for NFC MAIN RAM BUFFER areas */
46 #define NFC_MAIN_AREA(n)        ((n) *  0x200)
47
48 /* Addresses for NFC SPARE BUFFER areas */
49 #define NFC_SPARE_BUFFERS       8
50 #define NFC_SPARE_LEN           0x40
51 #define NFC_SPARE_AREA(n)       (0x1000 + ((n) * NFC_SPARE_LEN))
52
53 /* MPC5121 NFC registers */
54 #define NFC_BUF_ADDR            0x1E04
55 #define NFC_FLASH_ADDR          0x1E06
56 #define NFC_FLASH_CMD           0x1E08
57 #define NFC_CONFIG              0x1E0A
58 #define NFC_ECC_STATUS1         0x1E0C
59 #define NFC_ECC_STATUS2         0x1E0E
60 #define NFC_SPAS                0x1E10
61 #define NFC_WRPROT              0x1E12
62 #define NFC_NF_WRPRST           0x1E18
63 #define NFC_CONFIG1             0x1E1A
64 #define NFC_CONFIG2             0x1E1C
65 #define NFC_UNLOCKSTART_BLK0    0x1E20
66 #define NFC_UNLOCKEND_BLK0      0x1E22
67 #define NFC_UNLOCKSTART_BLK1    0x1E24
68 #define NFC_UNLOCKEND_BLK1      0x1E26
69 #define NFC_UNLOCKSTART_BLK2    0x1E28
70 #define NFC_UNLOCKEND_BLK2      0x1E2A
71 #define NFC_UNLOCKSTART_BLK3    0x1E2C
72 #define NFC_UNLOCKEND_BLK3      0x1E2E
73
74 /* Bit Definitions: NFC_BUF_ADDR */
75 #define NFC_RBA_MASK            (7 << 0)
76 #define NFC_ACTIVE_CS_SHIFT     5
77 #define NFC_ACTIVE_CS_MASK      (3 << NFC_ACTIVE_CS_SHIFT)
78
79 /* Bit Definitions: NFC_CONFIG */
80 #define NFC_BLS_UNLOCKED        (1 << 1)
81
82 /* Bit Definitions: NFC_CONFIG1 */
83 #define NFC_ECC_4BIT            (1 << 0)
84 #define NFC_FULL_PAGE_DMA       (1 << 1)
85 #define NFC_SPARE_ONLY          (1 << 2)
86 #define NFC_ECC_ENABLE          (1 << 3)
87 #define NFC_INT_MASK            (1 << 4)
88 #define NFC_BIG_ENDIAN          (1 << 5)
89 #define NFC_RESET               (1 << 6)
90 #define NFC_CE                  (1 << 7)
91 #define NFC_ONE_CYCLE           (1 << 8)
92 #define NFC_PPB_32              (0 << 9)
93 #define NFC_PPB_64              (1 << 9)
94 #define NFC_PPB_128             (2 << 9)
95 #define NFC_PPB_256             (3 << 9)
96 #define NFC_PPB_MASK            (3 << 9)
97 #define NFC_FULL_PAGE_INT       (1 << 11)
98
99 /* Bit Definitions: NFC_CONFIG2 */
100 #define NFC_COMMAND             (1 << 0)
101 #define NFC_ADDRESS             (1 << 1)
102 #define NFC_INPUT               (1 << 2)
103 #define NFC_OUTPUT              (1 << 3)
104 #define NFC_ID                  (1 << 4)
105 #define NFC_STATUS              (1 << 5)
106 #define NFC_CMD_FAIL            (1 << 15)
107 #define NFC_INT                 (1 << 15)
108
109 /* Bit Definitions: NFC_WRPROT */
110 #define NFC_WPC_LOCK_TIGHT      (1 << 0)
111 #define NFC_WPC_LOCK            (1 << 1)
112 #define NFC_WPC_UNLOCK          (1 << 2)
113
114 struct mpc5121_nfc_prv {
115         struct mtd_info mtd;
116         struct nand_chip chip;
117         int irq;
118         void __iomem *regs;
119         struct clk *clk;
120         uint column;
121         int spareonly;
122         int chipsel;
123 };
124
125 int mpc5121_nfc_chip = 0;
126
127 static void mpc5121_nfc_done(struct mtd_info *mtd);
128
129 /* Read NFC register */
130 static inline u16 nfc_read(struct mtd_info *mtd, uint reg)
131 {
132         struct nand_chip *chip = mtd->priv;
133         struct mpc5121_nfc_prv *prv = chip->priv;
134
135         return in_be16(prv->regs + reg);
136 }
137
138 /* Write NFC register */
139 static inline void nfc_write(struct mtd_info *mtd, uint reg, u16 val)
140 {
141         struct nand_chip *chip = mtd->priv;
142         struct mpc5121_nfc_prv *prv = chip->priv;
143
144         out_be16(prv->regs + reg, val);
145 }
146
147 /* Set bits in NFC register */
148 static inline void nfc_set(struct mtd_info *mtd, uint reg, u16 bits)
149 {
150         nfc_write(mtd, reg, nfc_read(mtd, reg) | bits);
151 }
152
153 /* Clear bits in NFC register */
154 static inline void nfc_clear(struct mtd_info *mtd, uint reg, u16 bits)
155 {
156         nfc_write(mtd, reg, nfc_read(mtd, reg) & ~bits);
157 }
158
159 /* Invoke address cycle */
160 static inline void mpc5121_nfc_send_addr(struct mtd_info *mtd, u16 addr)
161 {
162         nfc_write(mtd, NFC_FLASH_ADDR, addr);
163         nfc_write(mtd, NFC_CONFIG2, NFC_ADDRESS);
164         mpc5121_nfc_done(mtd);
165 }
166
167 /* Invoke command cycle */
168 static inline void mpc5121_nfc_send_cmd(struct mtd_info *mtd, u16 cmd)
169 {
170         nfc_write(mtd, NFC_FLASH_CMD, cmd);
171         nfc_write(mtd, NFC_CONFIG2, NFC_COMMAND);
172         mpc5121_nfc_done(mtd);
173 }
174
175 /* Send data from NFC buffers to NAND flash */
176 static inline void mpc5121_nfc_send_prog_page(struct mtd_info *mtd)
177 {
178         nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
179         nfc_write(mtd, NFC_CONFIG2, NFC_INPUT);
180         mpc5121_nfc_done(mtd);
181 }
182
183 /* Receive data from NAND flash */
184 static inline void mpc5121_nfc_send_read_page(struct mtd_info *mtd)
185 {
186         nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
187         nfc_write(mtd, NFC_CONFIG2, NFC_OUTPUT);
188         mpc5121_nfc_done(mtd);
189 }
190
191 /* Receive ID from NAND flash */
192 static inline void mpc5121_nfc_send_read_id(struct mtd_info *mtd)
193 {
194         nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
195         nfc_write(mtd, NFC_CONFIG2, NFC_ID);
196         mpc5121_nfc_done(mtd);
197 }
198
199 /* Receive status from NAND flash */
200 static inline void mpc5121_nfc_send_read_status(struct mtd_info *mtd)
201 {
202         nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
203         nfc_write(mtd, NFC_CONFIG2, NFC_STATUS);
204         mpc5121_nfc_done(mtd);
205 }
206
207 static void mpc5121_nfc_done(struct mtd_info *mtd)
208 {
209         int max_retries = NFC_TIMEOUT;
210
211         while (1) {
212                 max_retries--;
213                 if (nfc_read(mtd, NFC_CONFIG2) & NFC_INT)
214                         break;
215                 udelay(1);
216         }
217
218         if (max_retries <= 0)
219                 printk(KERN_WARNING DRV_NAME
220                        ": Timeout while waiting for completion.\n");
221 }
222
223 /* Do address cycle(s) */
224 static void mpc5121_nfc_addr_cycle(struct mtd_info *mtd, int column, int page)
225 {
226         struct nand_chip *chip = mtd->priv;
227         u32 pagemask = chip->pagemask;
228
229         if (column != -1) {
230                 mpc5121_nfc_send_addr(mtd, column);
231                 if (mtd->writesize > 512)
232                         mpc5121_nfc_send_addr(mtd, column >> 8);
233         }
234
235         if (page != -1) {
236                 do {
237                         mpc5121_nfc_send_addr(mtd, page & 0xFF);
238                         page >>= 8;
239                         pagemask >>= 8;
240                 } while (pagemask);
241         }
242 }
243
244 /* Control chip select signals */
245
246 /*
247  * Selecting the active device:
248  *
249  * This is different than the linux version. Switching between chips
250  * is done via board_nand_select_device(). The Linux select_chip
251  * function used here in U-Boot has only 2 valid chip numbers:
252  *      0 select
253  *      -1 deselect
254  */
255
256 /*
257  * Implement it as a weak default, so that boards with a specific
258  * chip-select routine can use their own function.
259  */
260 void __mpc5121_nfc_select_chip(struct mtd_info *mtd, int chip)
261 {
262         if (chip < 0) {
263                 nfc_clear(mtd, NFC_CONFIG1, NFC_CE);
264                 return;
265         }
266
267         nfc_clear(mtd, NFC_BUF_ADDR, NFC_ACTIVE_CS_MASK);
268         nfc_set(mtd, NFC_BUF_ADDR, (chip << NFC_ACTIVE_CS_SHIFT) &
269                 NFC_ACTIVE_CS_MASK);
270         nfc_set(mtd, NFC_CONFIG1, NFC_CE);
271 }
272 void mpc5121_nfc_select_chip(struct mtd_info *mtd, int chip)
273         __attribute__((weak, alias("__mpc5121_nfc_select_chip")));
274
275 void board_nand_select_device(struct nand_chip *nand, int chip)
276 {
277         /*
278          * Only save this chip number in global variable here. This
279          * will be used later in mpc5121_nfc_select_chip().
280          */
281         mpc5121_nfc_chip = chip;
282 }
283
284 /* Read NAND Ready/Busy signal */
285 static int mpc5121_nfc_dev_ready(struct mtd_info *mtd)
286 {
287         /*
288          * NFC handles ready/busy signal internally. Therefore, this function
289          * always returns status as ready.
290          */
291         return 1;
292 }
293
294 /* Write command to NAND flash */
295 static void mpc5121_nfc_command(struct mtd_info *mtd, unsigned command,
296                                 int column, int page)
297 {
298         struct nand_chip *chip = mtd->priv;
299         struct mpc5121_nfc_prv *prv = chip->priv;
300
301         prv->column = (column >= 0) ? column : 0;
302         prv->spareonly = 0;
303
304         switch (command) {
305         case NAND_CMD_PAGEPROG:
306                 mpc5121_nfc_send_prog_page(mtd);
307                 break;
308                 /*
309                  * NFC does not support sub-page reads and writes,
310                  * so emulate them using full page transfers.
311                  */
312         case NAND_CMD_READ0:
313                 column = 0;
314                 break;
315
316         case NAND_CMD_READ1:
317                 prv->column += 256;
318                 command = NAND_CMD_READ0;
319                 column = 0;
320                 break;
321
322         case NAND_CMD_READOOB:
323                 prv->spareonly = 1;
324                 command = NAND_CMD_READ0;
325                 column = 0;
326                 break;
327
328         case NAND_CMD_SEQIN:
329                 mpc5121_nfc_command(mtd, NAND_CMD_READ0, column, page);
330                 column = 0;
331                 break;
332
333         case NAND_CMD_ERASE1:
334         case NAND_CMD_ERASE2:
335         case NAND_CMD_READID:
336         case NAND_CMD_STATUS:
337                 break;
338
339         default:
340                 return;
341         }
342
343         mpc5121_nfc_send_cmd(mtd, command);
344         mpc5121_nfc_addr_cycle(mtd, column, page);
345
346         switch (command) {
347         case NAND_CMD_READ0:
348                 if (mtd->writesize > 512)
349                         mpc5121_nfc_send_cmd(mtd, NAND_CMD_READSTART);
350                 mpc5121_nfc_send_read_page(mtd);
351                 break;
352
353         case NAND_CMD_READID:
354                 mpc5121_nfc_send_read_id(mtd);
355                 break;
356
357         case NAND_CMD_STATUS:
358                 mpc5121_nfc_send_read_status(mtd);
359                 if (chip->options & NAND_BUSWIDTH_16)
360                         prv->column = 1;
361                 else
362                         prv->column = 0;
363                 break;
364         }
365 }
366
367 /* Copy data from/to NFC spare buffers. */
368 static void mpc5121_nfc_copy_spare(struct mtd_info *mtd, uint offset,
369                                    u8 * buffer, uint size, int wr)
370 {
371         struct nand_chip *nand = mtd->priv;
372         struct mpc5121_nfc_prv *prv = nand->priv;
373         uint o, s, sbsize, blksize;
374
375         /*
376          * NAND spare area is available through NFC spare buffers.
377          * The NFC divides spare area into (page_size / 512) chunks.
378          * Each chunk is placed into separate spare memory area, using
379          * first (spare_size / num_of_chunks) bytes of the buffer.
380          *
381          * For NAND device in which the spare area is not divided fully
382          * by the number of chunks, number of used bytes in each spare
383          * buffer is rounded down to the nearest even number of bytes,
384          * and all remaining bytes are added to the last used spare area.
385          *
386          * For more information read section 26.6.10 of MPC5121e
387          * Microcontroller Reference Manual, Rev. 3.
388          */
389
390         /* Calculate number of valid bytes in each spare buffer */
391         sbsize = (mtd->oobsize / (mtd->writesize / 512)) & ~1;
392
393         while (size) {
394                 /* Calculate spare buffer number */
395                 s = offset / sbsize;
396                 if (s > NFC_SPARE_BUFFERS - 1)
397                         s = NFC_SPARE_BUFFERS - 1;
398
399                 /*
400                  * Calculate offset to requested data block in selected spare
401                  * buffer and its size.
402                  */
403                 o = offset - (s * sbsize);
404                 blksize = min(sbsize - o, size);
405
406                 if (wr)
407                         memcpy_toio(prv->regs + NFC_SPARE_AREA(s) + o,
408                                     buffer, blksize);
409                 else
410                         memcpy_fromio(buffer,
411                                       prv->regs + NFC_SPARE_AREA(s) + o,
412                                       blksize);
413
414                 buffer += blksize;
415                 offset += blksize;
416                 size -= blksize;
417         };
418 }
419
420 /* Copy data from/to NFC main and spare buffers */
421 static void mpc5121_nfc_buf_copy(struct mtd_info *mtd, u_char * buf, int len,
422                                  int wr)
423 {
424         struct nand_chip *chip = mtd->priv;
425         struct mpc5121_nfc_prv *prv = chip->priv;
426         uint c = prv->column;
427         uint l;
428
429         /* Handle spare area access */
430         if (prv->spareonly || c >= mtd->writesize) {
431                 /* Calculate offset from beginning of spare area */
432                 if (c >= mtd->writesize)
433                         c -= mtd->writesize;
434
435                 prv->column += len;
436                 mpc5121_nfc_copy_spare(mtd, c, buf, len, wr);
437                 return;
438         }
439
440         /*
441          * Handle main area access - limit copy length to prevent
442          * crossing main/spare boundary.
443          */
444         l = min((uint) len, mtd->writesize - c);
445         prv->column += l;
446
447         if (wr)
448                 memcpy_toio(prv->regs + NFC_MAIN_AREA(0) + c, buf, l);
449         else
450                 memcpy_fromio(buf, prv->regs + NFC_MAIN_AREA(0) + c, l);
451
452         /* Handle crossing main/spare boundary */
453         if (l != len) {
454                 buf += l;
455                 len -= l;
456                 mpc5121_nfc_buf_copy(mtd, buf, len, wr);
457         }
458 }
459
460 /* Read data from NFC buffers */
461 static void mpc5121_nfc_read_buf(struct mtd_info *mtd, u_char * buf, int len)
462 {
463         mpc5121_nfc_buf_copy(mtd, buf, len, 0);
464 }
465
466 /* Write data to NFC buffers */
467 static void mpc5121_nfc_write_buf(struct mtd_info *mtd,
468                                   const u_char * buf, int len)
469 {
470         mpc5121_nfc_buf_copy(mtd, (u_char *) buf, len, 1);
471 }
472
473 /* Compare buffer with NAND flash */
474 static int mpc5121_nfc_verify_buf(struct mtd_info *mtd,
475                                   const u_char * buf, int len)
476 {
477         u_char tmp[256];
478         uint bsize;
479
480         while (len) {
481                 bsize = min(len, 256);
482                 mpc5121_nfc_read_buf(mtd, tmp, bsize);
483
484                 if (memcmp(buf, tmp, bsize))
485                         return 1;
486
487                 buf += bsize;
488                 len -= bsize;
489         }
490
491         return 0;
492 }
493
494 /* Read byte from NFC buffers */
495 static u8 mpc5121_nfc_read_byte(struct mtd_info *mtd)
496 {
497         u8 tmp;
498
499         mpc5121_nfc_read_buf(mtd, &tmp, sizeof(tmp));
500
501         return tmp;
502 }
503
504 /* Read word from NFC buffers */
505 static u16 mpc5121_nfc_read_word(struct mtd_info *mtd)
506 {
507         u16 tmp;
508
509         mpc5121_nfc_read_buf(mtd, (u_char *) & tmp, sizeof(tmp));
510
511         return tmp;
512 }
513
514 /*
515  * Read NFC configuration from Reset Config Word
516  *
517  * NFC is configured during reset in basis of information stored
518  * in Reset Config Word. There is no other way to set NAND block
519  * size, spare size and bus width.
520  */
521 static int mpc5121_nfc_read_hw_config(struct mtd_info *mtd)
522 {
523         immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
524         struct nand_chip *chip = mtd->priv;
525         uint rcw_pagesize = 0;
526         uint rcw_sparesize = 0;
527         uint rcw_width;
528         uint rcwh;
529         uint romloc, ps;
530
531         rcwh = in_be32(&(im->reset.rcwh));
532
533         /* Bit 6: NFC bus width */
534         rcw_width = ((rcwh >> 6) & 0x1) ? 2 : 1;
535
536         /* Bit 7: NFC Page/Spare size */
537         ps = (rcwh >> 7) & 0x1;
538
539         /* Bits [22:21]: ROM Location */
540         romloc = (rcwh >> 21) & 0x3;
541
542         /* Decode RCW bits */
543         switch ((ps << 2) | romloc) {
544         case 0x00:
545         case 0x01:
546                 rcw_pagesize = 512;
547                 rcw_sparesize = 16;
548                 break;
549         case 0x02:
550         case 0x03:
551                 rcw_pagesize = 4096;
552                 rcw_sparesize = 128;
553                 break;
554         case 0x04:
555         case 0x05:
556                 rcw_pagesize = 2048;
557                 rcw_sparesize = 64;
558                 break;
559         case 0x06:
560         case 0x07:
561                 rcw_pagesize = 4096;
562                 rcw_sparesize = 218;
563                 break;
564         }
565
566         mtd->writesize = rcw_pagesize;
567         mtd->oobsize = rcw_sparesize;
568         if (rcw_width == 2)
569                 chip->options |= NAND_BUSWIDTH_16;
570
571         debug(KERN_NOTICE DRV_NAME ": Configured for "
572               "%u-bit NAND, page size %u with %u spare.\n",
573               rcw_width * 8, rcw_pagesize, rcw_sparesize);
574         return 0;
575 }
576
577 int board_nand_init(struct nand_chip *chip)
578 {
579         struct mpc5121_nfc_prv *prv;
580         struct mtd_info *mtd;
581         int resettime = 0;
582         int retval = 0;
583         int rev;
584         static int chip_nr = 0;
585
586         /*
587          * Check SoC revision. This driver supports only NFC
588          * in MPC5121 revision 2.
589          */
590         rev = (mfspr(SPRN_SVR) >> 4) & 0xF;
591         if (rev != 2) {
592                 printk(KERN_ERR DRV_NAME
593                        ": SoC revision %u is not supported!\n", rev);
594                 return -ENXIO;
595         }
596
597         prv = malloc(sizeof(*prv));
598         if (!prv) {
599                 printk(KERN_ERR DRV_NAME ": Memory exhausted!\n");
600                 return -ENOMEM;
601         }
602
603         mtd = &nand_info[chip_nr++];
604         mtd->priv = chip;
605         chip->priv = prv;
606
607         /* Read NFC configuration from Reset Config Word */
608         retval = mpc5121_nfc_read_hw_config(mtd);
609         if (retval) {
610                 printk(KERN_ERR DRV_NAME ": Unable to read NFC config!\n");
611                 return retval;
612         }
613
614         prv->regs = (void __iomem *)CONFIG_SYS_NAND_BASE;
615         chip->dev_ready = mpc5121_nfc_dev_ready;
616         chip->cmdfunc = mpc5121_nfc_command;
617         chip->read_byte = mpc5121_nfc_read_byte;
618         chip->read_word = mpc5121_nfc_read_word;
619         chip->read_buf = mpc5121_nfc_read_buf;
620         chip->write_buf = mpc5121_nfc_write_buf;
621         chip->verify_buf = mpc5121_nfc_verify_buf;
622         chip->select_chip = mpc5121_nfc_select_chip;
623         chip->options = NAND_NO_AUTOINCR | NAND_USE_FLASH_BBT;
624         chip->ecc.mode = NAND_ECC_SOFT;
625
626         /* Reset NAND Flash controller */
627         nfc_set(mtd, NFC_CONFIG1, NFC_RESET);
628         while (nfc_read(mtd, NFC_CONFIG1) & NFC_RESET) {
629                 if (resettime++ >= NFC_RESET_TIMEOUT) {
630                         printk(KERN_ERR DRV_NAME
631                                ": Timeout while resetting NFC!\n");
632                         retval = -EINVAL;
633                         goto error;
634                 }
635
636                 udelay(1);
637         }
638
639         /* Enable write to NFC memory */
640         nfc_write(mtd, NFC_CONFIG, NFC_BLS_UNLOCKED);
641
642         /* Enable write to all NAND pages */
643         nfc_write(mtd, NFC_UNLOCKSTART_BLK0, 0x0000);
644         nfc_write(mtd, NFC_UNLOCKEND_BLK0, 0xFFFF);
645         nfc_write(mtd, NFC_WRPROT, NFC_WPC_UNLOCK);
646
647         /*
648          * Setup NFC:
649          *      - Big Endian transfers,
650          *      - Interrupt after full page read/write.
651          */
652         nfc_write(mtd, NFC_CONFIG1, NFC_BIG_ENDIAN | NFC_INT_MASK |
653                   NFC_FULL_PAGE_INT);
654
655         /* Set spare area size */
656         nfc_write(mtd, NFC_SPAS, mtd->oobsize >> 1);
657
658         /* Detect NAND chips */
659         if (nand_scan(mtd, 1)) {
660                 printk(KERN_ERR DRV_NAME ": NAND Flash not found !\n");
661                 retval = -ENXIO;
662                 goto error;
663         }
664
665         /* Set erase block size */
666         switch (mtd->erasesize / mtd->writesize) {
667         case 32:
668                 nfc_set(mtd, NFC_CONFIG1, NFC_PPB_32);
669                 break;
670
671         case 64:
672                 nfc_set(mtd, NFC_CONFIG1, NFC_PPB_64);
673                 break;
674
675         case 128:
676                 nfc_set(mtd, NFC_CONFIG1, NFC_PPB_128);
677                 break;
678
679         case 256:
680                 nfc_set(mtd, NFC_CONFIG1, NFC_PPB_256);
681                 break;
682
683         default:
684                 printk(KERN_ERR DRV_NAME ": Unsupported NAND flash!\n");
685                 retval = -ENXIO;
686                 goto error;
687         }
688
689         return 0;
690 error:
691         return retval;
692 }