Merge remote branch 'common/android-2.6.36' into android-tegra-2.6.36
[firefly-linux-kernel-4.4.55.git] / drivers / mtd / devices / tegra_nand.c
1 /*
2  * drivers/mtd/devices/tegra_nand.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  * Author: Dima Zavin <dima@android.com>
6  *         Colin Cross <ccross@android.com>
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * Derived from: drivers/mtd/nand/nand_base.c
18  *               drivers/mtd/nand/pxa3xx.c
19  *
20  * TODO:
21  *      - Add support for 16bit bus width
22  */
23
24 #include <linux/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/io.h>
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/mtd/nand.h>
32 #include <linux/mtd/mtd.h>
33 #include <linux/mtd/partitions.h>
34 #include <linux/platform_device.h>
35 #include <linux/types.h>
36 #include <linux/clk.h>
37 #include <linux/slab.h>
38
39 #include <mach/nand.h>
40
41 #include "tegra_nand.h"
42
43 #define DRIVER_NAME     "tegra_nand"
44 #define DRIVER_DESC     "Nvidia Tegra NAND Flash Controller driver"
45
46 #define MAX_DMA_SZ                      SZ_64K
47 #define ECC_BUF_SZ                      SZ_1K
48
49 /* FIXME: is this right?!
50  * NvRM code says it should be 128 bytes, but that seems awfully small
51  */
52
53 /*#define TEGRA_NAND_DEBUG
54 #define TEGRA_NAND_DEBUG_PEDANTIC*/
55
56 #ifdef TEGRA_NAND_DEBUG
57 #define TEGRA_DBG(fmt, args...) \
58         do { pr_info(fmt, ##args); } while (0)
59 #else
60 #define TEGRA_DBG(fmt, args...)
61 #endif
62
63 /* TODO: will vary with devices, move into appropriate device spcific header */
64 #define SCAN_TIMING_VAL         0x3f0bd214
65 #define SCAN_TIMING2_VAL        0xb
66
67 /* TODO: pull in the register defs (fields, masks, etc) from Nvidia files
68  * so we don't have to redefine them */
69
70 #ifdef CONFIG_MTD_PARTITIONS
71 static const char *part_probes[] = { "cmdlinepart", NULL,  };
72 #endif
73
74 struct tegra_nand_chip {
75         spinlock_t              lock;
76         uint32_t                chipsize;
77         int                     num_chips;
78         int                     curr_chip;
79
80         /* addr >> chip_shift == chip number */
81         uint32_t                chip_shift;
82         /* (addr >> page_shift) & page_mask == page number within chip */
83         uint32_t                page_shift;
84         uint32_t                page_mask;
85         /* column within page */
86         uint32_t                column_mask;
87         /* addr >> block_shift == block number (across the whole mtd dev, not
88          * just a single chip. */
89         uint32_t                block_shift;
90
91         void                    *priv;
92 };
93
94 struct tegra_nand_info {
95         struct tegra_nand_chip          chip;
96         struct mtd_info                 mtd;
97         struct tegra_nand_platform      *plat;
98         struct device                   *dev;
99         struct mtd_partition            *parts;
100
101         /* synchronizes access to accessing the actual NAND controller */
102         struct mutex                    lock;
103
104
105         void                            *oob_dma_buf;
106         dma_addr_t                      oob_dma_addr;
107         /* ecc error vector info (offset into page and data mask to apply */
108         void                            *ecc_buf;
109         dma_addr_t                      ecc_addr;
110         /* ecc error status (page number, err_cnt) */
111         uint32_t                        *ecc_errs;
112         uint32_t                        num_ecc_errs;
113         uint32_t                        max_ecc_errs;
114         spinlock_t                      ecc_lock;
115
116         uint32_t                        command_reg;
117         uint32_t                        config_reg;
118         uint32_t                        dmactrl_reg;
119
120         struct completion               cmd_complete;
121         struct completion               dma_complete;
122
123         /* bad block bitmap: 1 == good, 0 == bad/unknown */
124         unsigned long                   *bb_bitmap;
125
126         struct clk                      *clk;
127 };
128 #define MTD_TO_INFO(mtd)        container_of((mtd), struct tegra_nand_info, mtd)
129
130 /* 64 byte oob block info for large page (== 2KB) device
131  *
132  * OOB flash layout for Tegra with Reed-Solomon 4 symbol correct ECC:
133  *      Skipped bytes(4)
134  *      Main area Ecc(36)
135  *      Tag data(20)
136  *      Tag data Ecc(4)
137  *
138  * Yaffs2 will use 16 tag bytes.
139  */
140
141 static struct nand_ecclayout tegra_nand_oob_64 = {
142         .eccbytes = 36,
143         .eccpos = {
144                 4,  5,  6,  7,  8,  9,  10, 11, 12,
145                 13, 14, 15, 16, 17, 18, 19, 20, 21,
146                 22, 23, 24, 25, 26, 27, 28, 29, 30,
147                 31, 32, 33, 34, 35, 36, 37, 38, 39,
148         },
149         .oobavail = 20,
150         .oobfree = {
151                 { .offset = 40,
152                   .length = 20,
153                 },
154         },
155 };
156
157 static struct nand_flash_dev *
158 find_nand_flash_device(int dev_id)
159 {
160         struct nand_flash_dev *dev = &nand_flash_ids[0];
161
162         while (dev->name && dev->id != dev_id)
163                 dev++;
164         return dev->name ? dev : NULL;
165 }
166
167 static struct nand_manufacturers *
168 find_nand_flash_vendor(int vendor_id)
169 {
170         struct nand_manufacturers *vendor = &nand_manuf_ids[0];
171
172         while (vendor->id && vendor->id != vendor_id)
173                 vendor++;
174         return vendor->id ? vendor : NULL;
175 }
176
177 #define REG_NAME(name)                  { name, #name }
178 static struct {
179         uint32_t addr;
180         char *name;
181 } reg_names[] = {
182         REG_NAME(COMMAND_REG),
183         REG_NAME(STATUS_REG),
184         REG_NAME(ISR_REG),
185         REG_NAME(IER_REG),
186         REG_NAME(CONFIG_REG),
187         REG_NAME(TIMING_REG),
188         REG_NAME(RESP_REG),
189         REG_NAME(TIMING2_REG),
190         REG_NAME(CMD_REG1),
191         REG_NAME(CMD_REG2),
192         REG_NAME(ADDR_REG1),
193         REG_NAME(ADDR_REG2),
194         REG_NAME(DMA_MST_CTRL_REG),
195         REG_NAME(DMA_CFG_A_REG),
196         REG_NAME(DMA_CFG_B_REG),
197         REG_NAME(FIFO_CTRL_REG),
198         REG_NAME(DATA_BLOCK_PTR_REG),
199         REG_NAME(TAG_PTR_REG),
200         REG_NAME(ECC_PTR_REG),
201         REG_NAME(DEC_STATUS_REG),
202         REG_NAME(HWSTATUS_CMD_REG),
203         REG_NAME(HWSTATUS_MASK_REG),
204         { 0, NULL },
205 };
206 #undef REG_NAME
207
208
209 static int
210 dump_nand_regs(void)
211 {
212         int i = 0;
213
214         TEGRA_DBG("%s: dumping registers\n", __func__);
215         while (reg_names[i].name != NULL) {
216                 TEGRA_DBG("%s = 0x%08x\n", reg_names[i].name, readl(reg_names[i].addr));
217                 i++;
218         }
219         TEGRA_DBG("%s: end of reg dump\n", __func__);
220         return 1;
221 }
222
223
224 static inline void
225 enable_ints(struct tegra_nand_info *info, uint32_t mask)
226 {
227         (void)info;
228         writel(readl(IER_REG) | mask, IER_REG);
229 }
230
231
232 static inline void
233 disable_ints(struct tegra_nand_info *info, uint32_t mask)
234 {
235         (void)info;
236         writel(readl(IER_REG) & ~mask, IER_REG);
237 }
238
239
240 static inline void
241 split_addr(struct tegra_nand_info *info, loff_t offset, int *chipnr, uint32_t *page,
242            uint32_t *column)
243 {
244         *chipnr = (int)(offset >> info->chip.chip_shift);
245         *page = (offset >> info->chip.page_shift) & info->chip.page_mask;
246         *column = offset & info->chip.column_mask;
247 }
248
249
250 static irqreturn_t
251 tegra_nand_irq(int irq, void *dev_id)
252 {
253         struct tegra_nand_info *info = dev_id;
254         uint32_t isr;
255         uint32_t ier;
256         uint32_t dma_ctrl;
257         uint32_t tmp;
258
259         isr = readl(ISR_REG);
260         ier = readl(IER_REG);
261         dma_ctrl = readl(DMA_MST_CTRL_REG);
262 #ifdef DEBUG_DUMP_IRQ
263         pr_info("IRQ: ISR=0x%08x IER=0x%08x DMA_IS=%d DMA_IE=%d\n",
264                 isr, ier, !!(dma_ctrl & (1 << 20)), !!(dma_ctrl & (1 << 28)));
265 #endif
266         if (isr & ISR_CMD_DONE) {
267                 if (likely(!(readl(COMMAND_REG) & COMMAND_GO)))
268                         complete(&info->cmd_complete);
269                 else
270                         pr_err("tegra_nand_irq: Spurious cmd done irq!\n");
271         }
272
273         if (isr & ISR_ECC_ERR) {
274                 /* always want to read the decode status so xfers don't stall. */
275                 tmp = readl(DEC_STATUS_REG);
276
277                 /* was ECC check actually enabled */
278                 if ((ier & IER_ECC_ERR)) {
279                         unsigned long flags;
280                         spin_lock_irqsave(&info->ecc_lock, flags);
281                         info->ecc_errs[info->num_ecc_errs++] = tmp;
282                         spin_unlock_irqrestore(&info->ecc_lock, flags);
283                 }
284         }
285
286         if ((dma_ctrl & DMA_CTRL_IS_DMA_DONE) &&
287             (dma_ctrl & DMA_CTRL_IE_DMA_DONE)) {
288                 complete(&info->dma_complete);
289                 writel(dma_ctrl, DMA_MST_CTRL_REG);
290         }
291
292         if ((isr & ISR_UND) && (ier & IER_UND))
293                 pr_err("%s: fifo underrun.\n", __func__);
294
295         if ((isr & ISR_OVR) && (ier & IER_OVR))
296                 pr_err("%s: fifo overrun.\n", __func__);
297
298         /* clear ALL interrupts?! */
299         writel(isr & 0xfffc, ISR_REG);
300
301         return IRQ_HANDLED;
302 }
303
304 static inline int
305 tegra_nand_is_cmd_done(struct tegra_nand_info *info)
306 {
307         return (readl(COMMAND_REG) & COMMAND_GO) ? 0 : 1;
308 }
309
310 static int
311 tegra_nand_wait_cmd_done(struct tegra_nand_info *info)
312 {
313         uint32_t timeout = (2 * HZ); /* TODO: make this realistic */
314         int ret;
315
316         ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
317
318 #ifdef TEGRA_NAND_DEBUG_PEDANTIC
319         BUG_ON(!ret && dump_nand_regs());
320 #endif
321
322         return ret ? 0 : ret;
323 }
324
325 static inline void
326 select_chip(struct tegra_nand_info *info, int chipnr)
327 {
328         BUG_ON(chipnr != -1 && chipnr >= info->plat->max_chips);
329         info->chip.curr_chip = chipnr;
330 }
331
332 static void
333 cfg_hwstatus_mon(struct tegra_nand_info *info)
334 {
335         uint32_t val;
336
337         val = (HWSTATUS_RDSTATUS_MASK(1) |
338                HWSTATUS_RDSTATUS_EXP_VAL(0) |
339                HWSTATUS_RBSY_MASK(NAND_STATUS_READY) |
340                HWSTATUS_RBSY_EXP_VAL(NAND_STATUS_READY));
341         writel(NAND_CMD_STATUS, HWSTATUS_CMD_REG);
342         writel(val, HWSTATUS_MASK_REG);
343 }
344
345 /* Tells the NAND controller to initiate the command. */
346 static int
347 tegra_nand_go(struct tegra_nand_info *info)
348 {
349         BUG_ON(!tegra_nand_is_cmd_done(info));
350
351         INIT_COMPLETION(info->cmd_complete);
352         writel(info->command_reg | COMMAND_GO, COMMAND_REG);
353
354         if (unlikely(tegra_nand_wait_cmd_done(info))) {
355                 /* TODO: abort command if needed? */
356                 pr_err("%s: Timeout while waiting for command\n", __func__);
357                 return -ETIMEDOUT;
358         }
359
360         /* TODO: maybe wait for dma here? */
361         return 0;
362 }
363
364 static void
365 tegra_nand_prep_readid(struct tegra_nand_info *info)
366 {
367         info->command_reg = (COMMAND_CLE | COMMAND_ALE | COMMAND_PIO | COMMAND_RX  |
368                              COMMAND_ALE_BYTE_SIZE(0) | COMMAND_TRANS_SIZE(3) |
369                              (COMMAND_CE(info->chip.curr_chip)));
370         writel(NAND_CMD_READID, CMD_REG1);
371         writel(0, CMD_REG2);
372         writel(0, ADDR_REG1);
373         writel(0, ADDR_REG2);
374         writel(0, CONFIG_REG);
375 }
376
377 static int
378 tegra_nand_cmd_readid(struct tegra_nand_info *info, uint32_t *chip_id)
379 {
380         int err;
381
382 #ifdef TEGRA_NAND_DEBUG_PEDANTIC
383         BUG_ON(info->chip.curr_chip == -1);
384 #endif
385
386         tegra_nand_prep_readid(info);
387         err = tegra_nand_go(info);
388         if (err != 0)
389                 return err;
390
391         *chip_id = readl(RESP_REG);
392         return 0;
393 }
394
395
396 /* assumes right locks are held */
397 static int
398 nand_cmd_get_status(struct tegra_nand_info *info, uint32_t *status)
399 {
400         int err;
401
402         info->command_reg = (COMMAND_CLE | COMMAND_PIO | COMMAND_RX  |
403                              COMMAND_RBSY_CHK | (COMMAND_CE(info->chip.curr_chip)));
404         writel(NAND_CMD_STATUS, CMD_REG1);
405         writel(0, CMD_REG2);
406         writel(0, ADDR_REG1);
407         writel(0, ADDR_REG2);
408         writel(CONFIG_COM_BSY, CONFIG_REG);
409
410         err = tegra_nand_go(info);
411         if (err != 0)
412                 return err;
413
414         *status = readl(RESP_REG) & 0xff;
415         return 0;
416 }
417
418
419 /* must be called with lock held */
420 static int
421 check_block_isbad(struct mtd_info *mtd, loff_t offs)
422 {
423         struct tegra_nand_info *info = MTD_TO_INFO(mtd);
424         uint32_t block = offs >> info->chip.block_shift;
425         int chipnr;
426         uint32_t page;
427         uint32_t column;
428         int ret = 0;
429         int i;
430
431         if (info->bb_bitmap[BIT_WORD(block)] & BIT_MASK(block))
432                 return 0;
433
434         offs &= ~(mtd->erasesize - 1);
435
436         /* Only set COM_BSY. */
437         /* TODO: should come from board file */
438         writel(CONFIG_COM_BSY, CONFIG_REG);
439
440         split_addr(info, offs, &chipnr, &page, &column);
441         select_chip(info, chipnr);
442
443         column = mtd->writesize & 0xffff; /* force to be the offset of OOB */
444
445         /* check fist two pages of the block */
446         for (i = 0; i < 2; ++i) {
447                 info->command_reg =
448                         COMMAND_CE(info->chip.curr_chip) | COMMAND_CLE | COMMAND_ALE |
449                         COMMAND_ALE_BYTE_SIZE(4) | COMMAND_RX | COMMAND_PIO |
450                         COMMAND_TRANS_SIZE(1) | COMMAND_A_VALID | COMMAND_RBSY_CHK |
451                         COMMAND_SEC_CMD;
452                 writel(NAND_CMD_READ0, CMD_REG1);
453                 writel(NAND_CMD_READSTART, CMD_REG2);
454
455                 writel(column | ((page & 0xffff) << 16), ADDR_REG1);
456                 writel((page >> 16) & 0xff, ADDR_REG2);
457
458                 /* ... poison me ... */
459                 writel(0xaa55aa55, RESP_REG);
460                 ret = tegra_nand_go(info);
461                 if (ret != 0) {
462                         pr_info("baaaaaad\n");
463                         goto out;
464                 }
465
466                 if ((readl(RESP_REG) & 0xffff) != 0xffff) {
467                         ret = 1;
468                         goto out;
469                 }
470
471                 /* Note: The assumption here is that we cannot cross chip
472                  * boundary since the we are only looking at the first 2 pages in
473                  * a block, i.e. erasesize > writesize ALWAYS */
474                 page++;
475         }
476
477 out:
478         /* update the bitmap if the block is good */
479         if (ret == 0)
480                 set_bit(block, info->bb_bitmap);
481         return ret;
482 }
483
484
485 static int
486 tegra_nand_block_isbad(struct mtd_info *mtd, loff_t offs)
487 {
488         struct tegra_nand_info *info = MTD_TO_INFO(mtd);
489         int ret;
490
491         if (offs >= mtd->size)
492                 return -EINVAL;
493
494         mutex_lock(&info->lock);
495         ret = check_block_isbad(mtd, offs);
496         mutex_unlock(&info->lock);
497
498 #if 0
499         if (ret > 0)
500                 pr_info("block @ 0x%llx is bad.\n", offs);
501         else if (ret < 0)
502                 pr_err("error checking block @ 0x%llx for badness.\n", offs);
503 #endif
504
505         return ret;
506 }
507
508
509 static int
510 tegra_nand_block_markbad(struct mtd_info *mtd, loff_t offs)
511 {
512         struct tegra_nand_info *info = MTD_TO_INFO(mtd);
513         uint32_t block = offs >> info->chip.block_shift;
514         int chipnr;
515         uint32_t page;
516         uint32_t column;
517         int ret = 0;
518         int i;
519
520         if (offs >= mtd->size)
521                 return -EINVAL;
522
523         pr_info("tegra_nand: setting block %d bad\n", block);
524
525         mutex_lock(&info->lock);
526         offs &= ~(mtd->erasesize - 1);
527
528         /* mark the block bad in our bitmap */
529         clear_bit(block, info->bb_bitmap);
530         mtd->ecc_stats.badblocks++;
531
532         /* Only set COM_BSY. */
533         /* TODO: should come from board file */
534         writel(CONFIG_COM_BSY, CONFIG_REG);
535
536         split_addr(info, offs, &chipnr, &page, &column);
537         select_chip(info, chipnr);
538
539         column = mtd->writesize & 0xffff; /* force to be the offset of OOB */
540
541         /* write to fist two pages in the block */
542         for (i = 0; i < 2; ++i) {
543                 info->command_reg =
544                         COMMAND_CE(info->chip.curr_chip) | COMMAND_CLE | COMMAND_ALE |
545                         COMMAND_ALE_BYTE_SIZE(4) | COMMAND_TX | COMMAND_PIO |
546                         COMMAND_TRANS_SIZE(1) | COMMAND_A_VALID | COMMAND_RBSY_CHK |
547                         COMMAND_AFT_DAT | COMMAND_SEC_CMD;
548                 writel(NAND_CMD_SEQIN, CMD_REG1);
549                 writel(NAND_CMD_PAGEPROG, CMD_REG2);
550
551                 writel(column | ((page & 0xffff) << 16), ADDR_REG1);
552                 writel((page >> 16) & 0xff, ADDR_REG2);
553
554                 writel(0x0, RESP_REG);
555                 ret = tegra_nand_go(info);
556                 if (ret != 0)
557                         goto out;
558
559                 /* TODO: check if the program op worked? */
560                 page++;
561         }
562
563 out:
564         mutex_unlock(&info->lock);
565         return ret;
566 }
567
568
569 static int
570 tegra_nand_erase(struct mtd_info *mtd, struct erase_info *instr)
571 {
572         struct tegra_nand_info *info = MTD_TO_INFO(mtd);
573         uint32_t num_blocks;
574         uint32_t offs;
575         int chipnr;
576         uint32_t page;
577         uint32_t column;
578         uint32_t status = 0;
579
580         TEGRA_DBG("tegra_nand_erase: addr=0x%08llx len=%lld\n", instr->addr,
581                instr->len);
582
583         if ((instr->addr + instr->len) > mtd->size) {
584                 pr_err("tegra_nand_erase: Can't erase past end of device\n");
585                 instr->state = MTD_ERASE_FAILED;
586                 return -EINVAL;
587         }
588
589         if (instr->addr & (mtd->erasesize - 1)) {
590                 pr_err("tegra_nand_erase: addr=0x%08llx not block-aligned\n",
591                        instr->addr);
592                 instr->state = MTD_ERASE_FAILED;
593                 return -EINVAL;
594         }
595
596         if (instr->len & (mtd->erasesize - 1)) {
597                 pr_err("tegra_nand_erase: len=%lld not block-aligned\n",
598                        instr->len);
599                 instr->state = MTD_ERASE_FAILED;
600                 return -EINVAL;
601         }
602
603         instr->fail_addr = 0xffffffff;
604
605         mutex_lock(&info->lock);
606
607         instr->state = MTD_ERASING;
608
609         offs = instr->addr;
610         num_blocks = instr->len >> info->chip.block_shift;
611
612         select_chip(info, -1);
613
614         while (num_blocks--) {
615                 split_addr(info, offs, &chipnr, &page, &column);
616                 if (chipnr != info->chip.curr_chip)
617                         select_chip(info, chipnr);
618                 TEGRA_DBG("tegra_nand_erase: addr=0x%08x, page=0x%08x\n", offs, page);
619
620                 if (check_block_isbad(mtd, offs)) {
621                         pr_info("%s: skipping bad block @ 0x%08x\n", __func__, offs);
622                         goto next_block;
623                 }
624
625                 info->command_reg =
626                         COMMAND_CE(info->chip.curr_chip) | COMMAND_CLE | COMMAND_ALE |
627                         COMMAND_ALE_BYTE_SIZE(2) | COMMAND_RBSY_CHK | COMMAND_SEC_CMD;
628                 writel(NAND_CMD_ERASE1, CMD_REG1);
629                 writel(NAND_CMD_ERASE2, CMD_REG2);
630
631                 writel(page & 0xffffff, ADDR_REG1);
632                 writel(0, ADDR_REG2);
633                 writel(CONFIG_COM_BSY, CONFIG_REG);
634
635                 if (tegra_nand_go(info) != 0) {
636                         instr->fail_addr = offs;
637                         goto out_err;
638                 }
639
640                 /* TODO: do we want a timeout here? */
641                 if ((nand_cmd_get_status(info, &status) != 0) ||
642                     (status & NAND_STATUS_FAIL) ||
643                     ((status & NAND_STATUS_READY) != NAND_STATUS_READY)) {
644                         instr->fail_addr = offs;
645                         pr_info("%s: erase failed @ 0x%08x (stat=0x%08x)\n",
646                                 __func__, offs, status);
647                         goto out_err;
648                 }
649 next_block:
650                 offs += mtd->erasesize;
651         }
652
653         instr->state = MTD_ERASE_DONE;
654         mutex_unlock(&info->lock);
655         mtd_erase_callback(instr);
656         return 0;
657
658 out_err:
659         instr->state = MTD_ERASE_FAILED;
660         mutex_unlock(&info->lock);
661         return -EIO;
662 }
663
664
665 static inline void
666 dump_mtd_oob_ops(struct mtd_oob_ops *ops)
667 {
668         pr_info("%s: oob_ops: mode=%s len=0x%x ooblen=0x%x "
669                 "ooboffs=0x%x dat=0x%p oob=0x%p\n", __func__,
670                 (ops->mode == MTD_OOB_AUTO ? "MTD_OOB_AUTO" :
671                  (ops->mode == MTD_OOB_PLACE ? "MTD_OOB_PLACE" : "MTD_OOB_RAW")),
672                 ops->len, ops->ooblen, ops->ooboffs, ops->datbuf, ops->oobbuf);
673 }
674
675 static int
676 tegra_nand_read(struct mtd_info *mtd, loff_t from, size_t len,
677                 size_t *retlen, uint8_t *buf)
678 {
679         struct mtd_oob_ops ops;
680         int ret;
681
682         pr_debug("%s: read: from=0x%llx len=0x%x\n", __func__, from, len);
683         ops.mode = MTD_OOB_AUTO;
684         ops.len = len;
685         ops.datbuf = buf;
686         ops.oobbuf = NULL;
687         ret = mtd->read_oob(mtd, from, &ops);
688         *retlen = ops.retlen;
689         return ret;
690 }
691
692 static void
693 correct_ecc_errors_on_blank_page(struct tegra_nand_info *info, u8 *datbuf, u8 *oobbuf, unsigned int a_len, unsigned int b_len) {
694         int i;
695         int all_ff = 1;
696         unsigned long flags;
697
698         spin_lock_irqsave(&info->ecc_lock, flags);
699         if (info->num_ecc_errs) {
700                 if (datbuf) {
701                         for (i = 0; i < a_len; i++)
702                                 if (datbuf[i] != 0xFF)
703                                         all_ff = 0;
704                 }
705                 if (oobbuf) {
706                         for (i = 0; i < b_len; i++)
707                                 if (oobbuf[i] != 0xFF)
708                                         all_ff = 0;
709                 }
710                 if (all_ff)
711                         info->num_ecc_errs = 0;
712         }
713         spin_unlock_irqrestore(&info->ecc_lock, flags);
714 }
715
716 static void
717 update_ecc_counts(struct tegra_nand_info *info, int check_oob)
718 {
719         unsigned long flags;
720         int i;
721
722         spin_lock_irqsave(&info->ecc_lock, flags);
723         for (i = 0; i < info->num_ecc_errs; ++i) {
724                 /* correctable */
725                 info->mtd.ecc_stats.corrected +=
726                         DEC_STATUS_ERR_CNT(info->ecc_errs[i]);
727
728                 /* uncorrectable */
729                 if (info->ecc_errs[i] & DEC_STATUS_ECC_FAIL_A)
730                         info->mtd.ecc_stats.failed++;
731                 if (check_oob && (info->ecc_errs[i] & DEC_STATUS_ECC_FAIL_B))
732                         info->mtd.ecc_stats.failed++;
733         }
734         info->num_ecc_errs = 0;
735         spin_unlock_irqrestore(&info->ecc_lock, flags);
736 }
737
738 static inline void
739 clear_regs(struct tegra_nand_info *info)
740 {
741         info->command_reg = 0;
742         info->config_reg = 0;
743         info->dmactrl_reg = 0;
744 }
745
746 static void
747 prep_transfer_dma(struct tegra_nand_info *info, int rx, int do_ecc, uint32_t page,
748                   uint32_t column, dma_addr_t data_dma,
749                   uint32_t data_len, dma_addr_t oob_dma, uint32_t oob_len)
750 {
751         uint32_t tag_sz = oob_len;
752
753 #if 0
754         pr_info("%s: rx=%d ecc=%d  page=%d col=%d data_dma=0x%x "
755                 "data_len=0x%08x oob_dma=0x%x ooblen=%d\n", __func__,
756                 rx, do_ecc, page, column, data_dma, data_len, oob_dma,
757                 oob_len);
758 #endif
759
760         info->command_reg =
761                 COMMAND_CE(info->chip.curr_chip) | COMMAND_CLE | COMMAND_ALE |
762                 COMMAND_ALE_BYTE_SIZE(4) | COMMAND_SEC_CMD | COMMAND_RBSY_CHK |
763                 COMMAND_TRANS_SIZE(8);
764
765         info->config_reg = (CONFIG_PAGE_SIZE_SEL(3) | CONFIG_PIPELINE_EN |
766                             CONFIG_COM_BSY);
767
768         info->dmactrl_reg = (DMA_CTRL_DMA_GO |
769                              DMA_CTRL_DMA_PERF_EN | DMA_CTRL_IE_DMA_DONE |
770                              DMA_CTRL_IS_DMA_DONE | DMA_CTRL_BURST_SIZE(4));
771
772         if (rx) {
773                 if (do_ecc)
774                         info->config_reg |= CONFIG_HW_ERR_CORRECTION;
775                 info->command_reg |= COMMAND_RX;
776                 info->dmactrl_reg |= DMA_CTRL_REUSE_BUFFER;
777                 writel(NAND_CMD_READ0, CMD_REG1);
778                 writel(NAND_CMD_READSTART, CMD_REG2);
779         } else {
780                 info->command_reg |= (COMMAND_TX | COMMAND_AFT_DAT);
781                 info->dmactrl_reg |= DMA_CTRL_DIR; /* DMA_RD == TX */
782                 writel(NAND_CMD_SEQIN, CMD_REG1);
783                 writel(NAND_CMD_PAGEPROG, CMD_REG2);
784         }
785
786         if (data_len) {
787                 if (do_ecc)
788                         info->config_reg |=
789                                 CONFIG_HW_ECC | CONFIG_ECC_SEL | CONFIG_TVALUE(0) |
790                                 CONFIG_SKIP_SPARE | CONFIG_SKIP_SPARE_SEL(0);
791                 info->command_reg |= COMMAND_A_VALID;
792                 info->dmactrl_reg |= DMA_CTRL_DMA_EN_A;
793                 writel(DMA_CFG_BLOCK_SIZE(data_len - 1), DMA_CFG_A_REG);
794                 writel(data_dma, DATA_BLOCK_PTR_REG);
795         } else {
796                 column = info->mtd.writesize;
797                 if (do_ecc)
798                         column += info->mtd.ecclayout->oobfree[0].offset;
799                 writel(0, DMA_CFG_A_REG);
800                 writel(0, DATA_BLOCK_PTR_REG);
801         }
802
803         if (oob_len) {
804                 oob_len = info->mtd.oobavail;
805                 tag_sz = info->mtd.oobavail;
806                 if (do_ecc) {
807                         tag_sz += 4; /* size of tag ecc */
808                         if (rx)
809                                 oob_len += 4; /* size of tag ecc */
810                         info->config_reg |= CONFIG_ECC_EN_TAG;
811                 }
812                 if (data_len && rx)
813                         oob_len += 4; /* num of skipped bytes */
814
815                 info->command_reg |= COMMAND_B_VALID;
816                 info->config_reg |= CONFIG_TAG_BYTE_SIZE(tag_sz - 1);
817                 info->dmactrl_reg |= DMA_CTRL_DMA_EN_B;
818                 writel(DMA_CFG_BLOCK_SIZE(oob_len - 1), DMA_CFG_B_REG);
819                 writel(oob_dma, TAG_PTR_REG);
820         } else {
821                 writel(0, DMA_CFG_B_REG);
822                 writel(0, TAG_PTR_REG);
823         }
824
825         writel((column & 0xffff) | ((page & 0xffff) << 16), ADDR_REG1);
826         writel((page >> 16) & 0xff, ADDR_REG2);
827 }
828
829 static dma_addr_t
830 tegra_nand_dma_map(struct device *dev, void *addr, size_t size,
831                  enum dma_data_direction dir)
832 {
833         struct page *page;
834         unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
835         if (virt_addr_valid(addr))
836                 page = virt_to_page(addr);
837         else {
838                 if (WARN_ON(size + offset > PAGE_SIZE))
839                         return ~0;
840                 page = vmalloc_to_page(addr);
841         }
842         return dma_map_page(dev, page, offset, size, dir);
843 }
844
845 /* if mode == RAW, then we read data only, with no ECC
846  * if mode == PLACE, we read ONLY the OOB data from a raw offset into the spare
847  * area (ooboffs).
848  * if mode == AUTO, we read main data and the OOB data from the oobfree areas as
849  * specified by nand_ecclayout.
850  */
851 static int
852 do_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
853 {
854         struct tegra_nand_info *info = MTD_TO_INFO(mtd);
855         struct mtd_ecc_stats old_ecc_stats;
856         int chipnr;
857         uint32_t page;
858         uint32_t column;
859         uint8_t *datbuf = ops->datbuf;
860         uint8_t *oobbuf = ops->oobbuf;
861         uint32_t len = datbuf ? ops->len : 0;
862         uint32_t ooblen = oobbuf ? ops->ooblen : 0;
863         uint32_t oobsz;
864         uint32_t page_count;
865         int err;
866         int do_ecc = 1;
867         dma_addr_t datbuf_dma_addr = 0;
868
869 #if 0
870         dump_mtd_oob_ops(mtd, ops);
871 #endif
872
873         ops->retlen = 0;
874         ops->oobretlen = 0;
875
876         /* TODO: Worry about reads from non-page boundaries later */
877         if (unlikely(from & info->chip.column_mask)) {
878                 pr_err("%s: Unaligned read (from 0x%llx) not supported\n",
879                        __func__, from);
880                 return -EINVAL;
881         }
882
883         if (likely(ops->mode == MTD_OOB_AUTO)) {
884                 oobsz = mtd->oobavail;
885         } else {
886                 oobsz = mtd->oobsize;
887                 do_ecc = 0;
888         }
889
890         if (unlikely(ops->oobbuf && ops->ooblen > oobsz)) {
891                 pr_err("%s: can't read OOB from multiple pages (%d > %d)\n", __func__,
892                        ops->ooblen, oobsz);
893                 return -EINVAL;
894         } else if (ops->oobbuf) {
895                 page_count = 1;
896         } else {
897                 page_count = max((uint32_t)(ops->len / mtd->writesize), (uint32_t)1);
898         }
899
900         mutex_lock(&info->lock);
901
902         memcpy(&old_ecc_stats, &mtd->ecc_stats, sizeof(old_ecc_stats));
903
904         if (do_ecc) {
905                 enable_ints(info, IER_ECC_ERR);
906                 writel(info->ecc_addr, ECC_PTR_REG);
907         } else
908                 disable_ints(info, IER_ECC_ERR);
909
910         split_addr(info, from, &chipnr, &page, &column);
911         select_chip(info, chipnr);
912
913         /* reset it to point back to beginning of page */
914         from -= column;
915
916         while (page_count--) {
917                 int a_len = min(mtd->writesize - column, len);
918                 int b_len = min(oobsz, ooblen);
919
920 #if 0
921                 pr_info("%s: chip:=%d page=%d col=%d\n", __func__, chipnr,
922                         page, column);
923 #endif
924
925                 clear_regs(info);
926                 if (datbuf)
927                         datbuf_dma_addr = tegra_nand_dma_map(info->dev, datbuf, a_len, DMA_FROM_DEVICE);
928
929                 prep_transfer_dma(info, 1, do_ecc, page, column, datbuf_dma_addr,
930                                   a_len, info->oob_dma_addr,
931                                   b_len);
932                 writel(info->config_reg, CONFIG_REG);
933                 writel(info->dmactrl_reg, DMA_MST_CTRL_REG);
934
935                 INIT_COMPLETION(info->dma_complete);
936                 err = tegra_nand_go(info);
937                 if (err != 0)
938                         goto out_err;
939
940                 if (!wait_for_completion_timeout(&info->dma_complete, 2*HZ)) {
941                         pr_err("%s: dma completion timeout\n", __func__);
942                         dump_nand_regs();
943                         err = -ETIMEDOUT;
944                         goto out_err;
945                 }
946
947                 /*pr_info("tegra_read_oob: DMA complete\n");*/
948
949                 /* if we are here, transfer is done */
950                 if (datbuf)
951                         dma_unmap_page(info->dev, datbuf_dma_addr, a_len, DMA_FROM_DEVICE);
952
953                 if (oobbuf) {
954                         uint32_t ofs = datbuf && oobbuf ? 4 : 0; /* skipped bytes */
955                         memcpy(oobbuf, info->oob_dma_buf + ofs, b_len);
956                 }
957
958                 correct_ecc_errors_on_blank_page(info, datbuf, oobbuf, a_len, b_len);
959
960                 if (datbuf) {
961                         len -= a_len;
962                         datbuf += a_len;
963                         ops->retlen += a_len;
964                 }
965
966                 if (oobbuf) {
967                         ooblen -= b_len;
968                         oobbuf += b_len;
969                         ops->oobretlen += b_len;
970                 }
971
972                 update_ecc_counts(info, oobbuf != NULL);
973
974                 if (!page_count)
975                         break;
976
977                 from += mtd->writesize;
978                 column = 0;
979
980                 split_addr(info, from, &chipnr, &page, &column);
981                 if (chipnr != info->chip.curr_chip)
982                         select_chip(info, chipnr);
983         }
984
985         disable_ints(info, IER_ECC_ERR);
986
987         if (mtd->ecc_stats.failed != old_ecc_stats.failed)
988                 err = -EBADMSG;
989         else if (mtd->ecc_stats.corrected != old_ecc_stats.corrected)
990                 err = -EUCLEAN;
991         else
992                 err = 0;
993
994         mutex_unlock(&info->lock);
995         return err;
996
997 out_err:
998         ops->retlen = 0;
999         ops->oobretlen = 0;
1000
1001         disable_ints(info, IER_ECC_ERR);
1002         mutex_unlock(&info->lock);
1003         return err;
1004 }
1005
1006 /* just does some parameter checking and calls do_read_oob */
1007 static int
1008 tegra_nand_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1009 {
1010         if (ops->datbuf && unlikely((from + ops->len) > mtd->size)) {
1011                 pr_err("%s: Can't read past end of device.\n", __func__);
1012                 return -EINVAL;
1013         }
1014
1015         if (unlikely(ops->oobbuf && !ops->ooblen)) {
1016                 pr_err("%s: Reading 0 bytes from OOB is meaningless\n", __func__);
1017                 return -EINVAL;
1018         }
1019
1020         if (unlikely(ops->mode != MTD_OOB_AUTO)) {
1021                 if (ops->oobbuf && ops->datbuf) {
1022                         pr_err("%s: can't read OOB + Data in non-AUTO mode.\n",
1023                                __func__);
1024                         return -EINVAL;
1025                 }
1026                 if ((ops->mode == MTD_OOB_RAW) && !ops->datbuf) {
1027                         pr_err("%s: Raw mode only supports reading data area.\n",
1028                                __func__);
1029                         return -EINVAL;
1030                 }
1031         }
1032
1033         return do_read_oob(mtd, from, ops);
1034 }
1035
1036 static int
1037 tegra_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1038                  size_t *retlen, const uint8_t *buf)
1039 {
1040         struct mtd_oob_ops ops;
1041         int ret;
1042
1043         pr_debug("%s: write: to=0x%llx len=0x%x\n", __func__, to, len);
1044         ops.mode = MTD_OOB_AUTO;
1045         ops.len = len;
1046         ops.datbuf = (uint8_t *)buf;
1047         ops.oobbuf = NULL;
1048         ret = mtd->write_oob(mtd, to, &ops);
1049         *retlen = ops.retlen;
1050         return ret;
1051 }
1052
1053 static int
1054 do_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
1055 {
1056         struct tegra_nand_info *info = MTD_TO_INFO(mtd);
1057         int chipnr;
1058         uint32_t page;
1059         uint32_t column;
1060         uint8_t *datbuf = ops->datbuf;
1061         uint8_t *oobbuf = ops->oobbuf;
1062         uint32_t len = datbuf ? ops->len : 0;
1063         uint32_t ooblen = oobbuf ? ops->ooblen : 0;
1064         uint32_t oobsz;
1065         uint32_t page_count;
1066         int err;
1067         int do_ecc = 1;
1068         dma_addr_t datbuf_dma_addr = 0;
1069
1070 #if 0
1071         dump_mtd_oob_ops(mtd, ops);
1072 #endif
1073
1074         ops->retlen = 0;
1075         ops->oobretlen = 0;
1076
1077         if (!ops->len)
1078                 return 0;
1079
1080
1081         if (likely(ops->mode == MTD_OOB_AUTO)) {
1082                 oobsz = mtd->oobavail;
1083         } else {
1084                 oobsz = mtd->oobsize;
1085                 do_ecc = 0;
1086         }
1087
1088         if (unlikely(ops->oobbuf && ops->ooblen > oobsz)) {
1089                         pr_err("%s: can't write OOB to multiple pages (%d > %d)\n",
1090                                __func__, ops->ooblen, oobsz);
1091                         return -EINVAL;
1092         } else if (ops->oobbuf) {
1093                 page_count = 1;
1094         } else
1095                 page_count = max((uint32_t)(ops->len / mtd->writesize), (uint32_t)1);
1096
1097         mutex_lock(&info->lock);
1098
1099         split_addr(info, to, &chipnr, &page, &column);
1100         select_chip(info, chipnr);
1101
1102         while (page_count--) {
1103                 int a_len = min(mtd->writesize, len);
1104                 int b_len = min(oobsz, ooblen);
1105
1106                 if (datbuf)
1107                         datbuf_dma_addr = tegra_nand_dma_map(info->dev, datbuf, a_len, DMA_TO_DEVICE);
1108                 if (oobbuf)
1109                         memcpy(info->oob_dma_buf, oobbuf, b_len);
1110
1111                 clear_regs(info);
1112                 prep_transfer_dma(info, 0, do_ecc, page, column, datbuf_dma_addr,
1113                                   a_len, info->oob_dma_addr, b_len);
1114
1115                 writel(info->config_reg, CONFIG_REG);
1116                 writel(info->dmactrl_reg, DMA_MST_CTRL_REG);
1117
1118                 INIT_COMPLETION(info->dma_complete);
1119                 err = tegra_nand_go(info);
1120                 if (err != 0)
1121                         goto out_err;
1122
1123                 if (!wait_for_completion_timeout(&info->dma_complete, 2*HZ)) {
1124                         pr_err("%s: dma completion timeout\n", __func__);
1125                         dump_nand_regs();
1126                         goto out_err;
1127                 }
1128
1129                 if (datbuf) {
1130                         dma_unmap_page(info->dev, datbuf_dma_addr, a_len, DMA_TO_DEVICE);
1131                         len -= a_len;
1132                         datbuf += a_len;
1133                         ops->retlen += a_len;
1134                 }
1135                 if (oobbuf) {
1136                         ooblen -= b_len;
1137                         oobbuf += b_len;
1138                         ops->oobretlen += b_len;
1139                 }
1140
1141                 if (!page_count)
1142                         break;
1143
1144                 to += mtd->writesize;
1145                 column = 0;
1146
1147                 split_addr(info, to, &chipnr, &page, &column);
1148                 if (chipnr != info->chip.curr_chip)
1149                         select_chip(info, chipnr);
1150         }
1151
1152         mutex_unlock(&info->lock);
1153         return err;
1154
1155 out_err:
1156         ops->retlen = 0;
1157         ops->oobretlen = 0;
1158
1159         mutex_unlock(&info->lock);
1160         return err;
1161 }
1162
1163 static int
1164 tegra_nand_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
1165 {
1166         struct tegra_nand_info *info = MTD_TO_INFO(mtd);
1167
1168         if (unlikely(to & info->chip.column_mask)) {
1169                 pr_err("%s: Unaligned write (to 0x%llx) not supported\n",
1170                        __func__, to);
1171                 return -EINVAL;
1172         }
1173
1174         if (unlikely(ops->oobbuf && !ops->ooblen)) {
1175                 pr_err("%s: Writing 0 bytes to OOB is meaningless\n", __func__);
1176                 return -EINVAL;
1177         }
1178
1179         return do_write_oob(mtd, to, ops);
1180 }
1181
1182 static int
1183 tegra_nand_suspend(struct mtd_info *mtd)
1184 {
1185         return 0;
1186 }
1187
1188 static void
1189 tegra_nand_resume(struct mtd_info *mtd)
1190 {
1191 }
1192
1193 static int
1194 scan_bad_blocks(struct tegra_nand_info *info)
1195 {
1196         struct mtd_info *mtd = &info->mtd;
1197         int num_blocks = mtd->size >> info->chip.block_shift;
1198         uint32_t block;
1199         int is_bad = 0;
1200
1201         for (block = 0; block < num_blocks; ++block) {
1202                 /* make sure the bit is cleared, meaning it's bad/unknown before
1203                  * we check. */
1204                 clear_bit(block, info->bb_bitmap);
1205                 is_bad = mtd->block_isbad(mtd, block << info->chip.block_shift);
1206
1207                 if (is_bad == 0)
1208                         set_bit(block, info->bb_bitmap);
1209                 else if (is_bad > 0)
1210                         pr_info("block 0x%08x is bad.\n", block);
1211                 else {
1212                         pr_err("Fatal error (%d) while scanning for "
1213                                "bad blocks\n", is_bad);
1214                         return is_bad;
1215                 }
1216         }
1217         return 0;
1218 }
1219
1220 static void
1221 set_chip_timing(struct tegra_nand_info *info)
1222 {
1223         struct tegra_nand_chip_parms *chip_parms = &info->plat->chip_parms[0];
1224         uint32_t tmp;
1225
1226         /* TODO: Actually search the chip_parms list for the correct device. */
1227         /* TODO: Get the appropriate frequency from the clock subsystem */
1228 #define NAND_CLK_FREQ   108000
1229 #define CNT(t)          (((((t) * NAND_CLK_FREQ) + 1000000 - 1) / 1000000) - 1)
1230         tmp = (TIMING_TRP_RESP(CNT(chip_parms->timing.trp_resp)) |
1231                TIMING_TWB(CNT(chip_parms->timing.twb)) |
1232                TIMING_TCR_TAR_TRR(CNT(chip_parms->timing.tcr_tar_trr)) |
1233                TIMING_TWHR(CNT(chip_parms->timing.twhr)) |
1234                TIMING_TCS(CNT(chip_parms->timing.tcs)) |
1235                TIMING_TWH(CNT(chip_parms->timing.twh)) |
1236                TIMING_TWP(CNT(chip_parms->timing.twp)) |
1237                TIMING_TRH(CNT(chip_parms->timing.trh)) |
1238                TIMING_TRP(CNT(chip_parms->timing.trp)));
1239         writel(tmp, TIMING_REG);
1240         writel(TIMING2_TADL(CNT(chip_parms->timing.tadl)), TIMING2_REG);
1241 #undef CNT
1242 #undef NAND_CLK_FREQ
1243 }
1244
1245 /* Scans for nand flash devices, identifies them, and fills in the
1246  * device info. */
1247 static int
1248 tegra_nand_scan(struct mtd_info *mtd, int maxchips)
1249 {
1250         struct tegra_nand_info *info = MTD_TO_INFO(mtd);
1251         struct nand_flash_dev *dev_info;
1252         struct nand_manufacturers *vendor_info;
1253         uint32_t tmp;
1254         uint32_t dev_id;
1255         uint32_t vendor_id;
1256         uint32_t dev_parms;
1257         uint32_t mlc_parms;
1258         int cnt;
1259         int err = 0;
1260
1261         writel(SCAN_TIMING_VAL, TIMING_REG);
1262         writel(SCAN_TIMING2_VAL, TIMING2_REG);
1263         writel(0, CONFIG_REG);
1264
1265         select_chip(info, 0);
1266         err = tegra_nand_cmd_readid(info, &tmp);
1267         if (err != 0)
1268                 goto out_error;
1269
1270         vendor_id = tmp & 0xff;
1271         dev_id = (tmp >> 8) & 0xff;
1272         mlc_parms = (tmp >> 16) & 0xff;
1273         dev_parms = (tmp >> 24) & 0xff;
1274
1275         dev_info = find_nand_flash_device(dev_id);
1276         if (dev_info == NULL) {
1277                 pr_err("%s: unknown flash device id (0x%02x) found.\n", __func__,
1278                         dev_id);
1279                 err = -ENODEV;
1280                 goto out_error;
1281         }
1282
1283         vendor_info = find_nand_flash_vendor(vendor_id);
1284         if (vendor_info == NULL) {
1285                 pr_err("%s: unknown flash vendor id (0x%02x) found.\n", __func__,
1286                         vendor_id);
1287                 err = -ENODEV;
1288                 goto out_error;
1289         }
1290
1291         /* loop through and see if we can find more devices */
1292         for (cnt = 1; cnt < info->plat->max_chips; ++cnt) {
1293                 select_chip(info, cnt);
1294                 /* TODO: figure out what to do about errors here */
1295                 err = tegra_nand_cmd_readid(info, &tmp);
1296                 if (err != 0)
1297                         goto out_error;
1298                 if ((dev_id != ((tmp >> 8) & 0xff)) ||
1299                     (vendor_id != (tmp & 0xff)))
1300                         break;
1301         }
1302
1303         pr_info("%s: %d NAND chip(s) found (vend=0x%02x, dev=0x%02x) (%s %s)\n",
1304                DRIVER_NAME, cnt, vendor_id, dev_id, vendor_info->name,
1305                dev_info->name);
1306         info->chip.num_chips = cnt;
1307         info->chip.chipsize = dev_info->chipsize << 20;
1308         mtd->size = info->chip.num_chips * info->chip.chipsize;
1309
1310         /* format of 4th id byte returned by READ ID
1311          *   bit     7 = rsvd
1312          *   bit     6 = bus width. 1 == 16bit, 0 == 8bit
1313          *   bits  5:4 = data block size. 64kb * (2^val)
1314          *   bit     3 = rsvd
1315          *   bit     2 = spare area size / 512 bytes. 0 == 8bytes, 1 == 16bytes
1316          *   bits  1:0 = page size. 1kb * (2^val)
1317          */
1318
1319         /* TODO: we should reconcile the information read from chip and
1320          *       the data given to us in tegra_nand_platform->chip_parms??
1321          *       platform data will give us timing information. */
1322
1323         /* page_size */
1324         tmp = dev_parms & 0x3;
1325         mtd->writesize = 1024 << tmp;
1326         info->chip.column_mask = mtd->writesize - 1;
1327
1328         /* Note: See oob layout description of why we only support 2k pages. */
1329         if (mtd->writesize > 2048) {
1330                 pr_err("%s: Large page devices with pagesize > 2kb are NOT "
1331                        "supported\n", __func__);
1332                 goto out_error;
1333         } else if (mtd->writesize < 2048) {
1334                 pr_err("%s: Small page devices are NOT supported\n", __func__);
1335                 goto out_error;
1336         }
1337
1338         /* spare area, must be at least 64 bytes */
1339         tmp = (dev_parms >> 2) & 0x1;
1340         tmp = (8 << tmp) * (mtd->writesize / 512);
1341         if (tmp < 64) {
1342                 pr_err("%s: Spare area (%d bytes) too small\n", __func__, tmp);
1343                 goto out_error;
1344         }
1345         mtd->oobsize = tmp;
1346         mtd->oobavail = tegra_nand_oob_64.oobavail;
1347
1348         /* data block size (erase size) (w/o spare) */
1349         tmp = (dev_parms >> 4) & 0x3;
1350         mtd->erasesize = (64 * 1024) << tmp;
1351         info->chip.block_shift = ffs(mtd->erasesize) - 1;
1352
1353         /* used to select the appropriate chip/page in case multiple devices
1354          * are connected */
1355         info->chip.chip_shift = ffs(info->chip.chipsize) - 1;
1356         info->chip.page_shift = ffs(mtd->writesize) - 1;
1357         info->chip.page_mask =
1358                 (info->chip.chipsize >> info->chip.page_shift) - 1;
1359
1360         /* now fill in the rest of the mtd fields */
1361         mtd->ecclayout = &tegra_nand_oob_64;
1362         mtd->type = MTD_NANDFLASH;
1363         mtd->flags = MTD_CAP_NANDFLASH;
1364
1365         mtd->erase = tegra_nand_erase;
1366         mtd->lock = NULL;
1367         mtd->point = NULL;
1368         mtd->unpoint = NULL;
1369         mtd->read = tegra_nand_read;
1370         mtd->write = tegra_nand_write;
1371         mtd->read_oob = tegra_nand_read_oob;
1372         mtd->write_oob = tegra_nand_write_oob;
1373
1374         mtd->resume = tegra_nand_resume;
1375         mtd->suspend = tegra_nand_suspend;
1376         mtd->block_isbad = tegra_nand_block_isbad;
1377         mtd->block_markbad = tegra_nand_block_markbad;
1378
1379         /* TODO: should take vendor_id/device_id */
1380         set_chip_timing(info);
1381
1382         return 0;
1383
1384 out_error:
1385         pr_err("%s: NAND device scan aborted due to error(s).\n", __func__);
1386         return err;
1387 }
1388
1389 static int __devinit
1390 tegra_nand_probe(struct platform_device *pdev)
1391 {
1392         struct tegra_nand_platform *plat = pdev->dev.platform_data;
1393         struct tegra_nand_info *info = NULL;
1394         struct tegra_nand_chip *chip = NULL;
1395         struct mtd_info *mtd = NULL;
1396         int err = 0;
1397         uint64_t num_erase_blocks;
1398
1399         pr_debug("%s: probing (%p)\n", __func__, pdev);
1400
1401         if (!plat) {
1402                 pr_err("%s: no platform device info\n", __func__);
1403                 return -EINVAL;
1404         } else if (!plat->chip_parms) {
1405                 pr_err("%s: no platform nand parms\n", __func__);
1406                 return -EINVAL;
1407         }
1408
1409         info = kzalloc(sizeof(struct tegra_nand_info), GFP_KERNEL);
1410         if (!info) {
1411                 pr_err("%s: no memory for flash info\n", __func__);
1412                 return -ENOMEM;
1413         }
1414
1415         info->dev = &pdev->dev;
1416         info->plat = plat;
1417
1418         platform_set_drvdata(pdev, info);
1419
1420         init_completion(&info->cmd_complete);
1421         init_completion(&info->dma_complete);
1422
1423         mutex_init(&info->lock);
1424         spin_lock_init(&info->ecc_lock);
1425
1426         chip = &info->chip;
1427         chip->priv = &info->mtd;
1428         chip->curr_chip = -1;
1429
1430         mtd = &info->mtd;
1431         mtd->name = dev_name(&pdev->dev);
1432         mtd->priv = &info->chip;
1433         mtd->owner = THIS_MODULE;
1434
1435         /* HACK: allocate a dma buffer to hold 1 page oob data */
1436         info->oob_dma_buf = dma_alloc_coherent(NULL, 64,
1437                                            &info->oob_dma_addr, GFP_KERNEL);
1438         if (!info->oob_dma_buf) {
1439                 err = -ENOMEM;
1440                 goto out_free_info;
1441         }
1442
1443         /* this will store the ecc error vector info */
1444         info->ecc_buf = dma_alloc_coherent(NULL, ECC_BUF_SZ, &info->ecc_addr,
1445                                            GFP_KERNEL);
1446         if (!info->ecc_buf) {
1447                 err = -ENOMEM;
1448                 goto out_free_dma_buf;
1449         }
1450
1451         /* grab the irq */
1452         if (!(pdev->resource[0].flags & IORESOURCE_IRQ)) {
1453                 pr_err("NAND IRQ resource not defined\n");
1454                 err = -EINVAL;
1455                 goto out_free_ecc_buf;
1456         }
1457
1458         err = request_irq(pdev->resource[0].start, tegra_nand_irq,
1459                           IRQF_SHARED, DRIVER_NAME, info);
1460         if (err) {
1461                 pr_err("Unable to request IRQ %d (%d)\n",
1462                                 pdev->resource[0].start, err);
1463                 goto out_free_ecc_buf;
1464         }
1465
1466         /* TODO: configure pinmux here?? */
1467         info->clk = clk_get(&pdev->dev, NULL);
1468         clk_set_rate(info->clk, 108000000);
1469
1470         cfg_hwstatus_mon(info);
1471
1472         /* clear all pending interrupts */
1473         writel(readl(ISR_REG), ISR_REG);
1474
1475         /* clear dma interrupt */
1476         writel(DMA_CTRL_IS_DMA_DONE, DMA_MST_CTRL_REG);
1477
1478         /* enable interrupts */
1479         disable_ints(info, 0xffffffff);
1480         enable_ints(info, IER_ERR_TRIG_VAL(4) | IER_UND | IER_OVR | IER_CMD_DONE |
1481                     IER_ECC_ERR | IER_GIE);
1482
1483         if (tegra_nand_scan(mtd, plat->max_chips)) {
1484                 err = -ENXIO;
1485                 goto out_dis_irq;
1486         }
1487         pr_info("%s: NVIDIA Tegra NAND controller @ base=0x%08x irq=%d.\n",
1488                 DRIVER_NAME, TEGRA_NAND_PHYS, pdev->resource[0].start);
1489
1490         /* allocate memory to hold the ecc error info */
1491         info->max_ecc_errs = MAX_DMA_SZ / mtd->writesize;
1492         info->ecc_errs = kmalloc(info->max_ecc_errs * sizeof(uint32_t),
1493                                  GFP_KERNEL);
1494         if (!info->ecc_errs) {
1495                 err = -ENOMEM;
1496                 goto out_dis_irq;
1497         }
1498
1499         /* alloc the bad block bitmap */
1500         num_erase_blocks = mtd->size;
1501         do_div(num_erase_blocks, mtd->erasesize);
1502         info->bb_bitmap = kzalloc(BITS_TO_LONGS(num_erase_blocks) *
1503                                   sizeof(unsigned long), GFP_KERNEL);
1504         if (!info->bb_bitmap) {
1505                 err = -ENOMEM;
1506                 goto out_free_ecc;
1507         }
1508
1509         err = scan_bad_blocks(info);
1510         if (err != 0)
1511                 goto out_free_bbbmap;
1512
1513 #if 0
1514         dump_nand_regs();
1515 #endif
1516
1517 #ifdef CONFIG_MTD_PARTITIONS
1518         err = parse_mtd_partitions(mtd, part_probes, &info->parts, 0);
1519         if (err > 0) {
1520                 err = add_mtd_partitions(mtd, info->parts, err);
1521         } else if (err <= 0 && plat->parts) {
1522                 err = add_mtd_partitions(mtd, plat->parts, plat->nr_parts);
1523         } else
1524 #endif
1525                 err = add_mtd_device(mtd);
1526                 if (err != 0)
1527                         goto out_free_bbbmap;
1528
1529         dev_set_drvdata(&pdev->dev, info);
1530
1531         pr_debug("%s: probe done.\n", __func__);
1532         return 0;
1533
1534 out_free_bbbmap:
1535         kfree(info->bb_bitmap);
1536
1537 out_free_ecc:
1538         kfree(info->ecc_errs);
1539
1540 out_dis_irq:
1541         disable_ints(info, 0xffffffff);
1542         free_irq(pdev->resource[0].start, info);
1543
1544 out_free_ecc_buf:
1545         dma_free_coherent(NULL, ECC_BUF_SZ, info->ecc_buf, info->ecc_addr);
1546
1547 out_free_dma_buf:
1548         dma_free_coherent(NULL, 64, info->oob_dma_buf,
1549                           info->oob_dma_addr);
1550
1551 out_free_info:
1552         platform_set_drvdata(pdev, NULL);
1553         kfree(info);
1554
1555         return err;
1556 }
1557
1558 static int __devexit
1559 tegra_nand_remove(struct platform_device *pdev)
1560 {
1561         struct tegra_nand_info *info = dev_get_drvdata(&pdev->dev);
1562
1563         dev_set_drvdata(&pdev->dev, NULL);
1564
1565         if (info) {
1566                 free_irq(pdev->resource[0].start, info);
1567                 kfree(info->bb_bitmap);
1568                 kfree(info->ecc_errs);
1569                 dma_free_coherent(NULL, ECC_BUF_SZ, info->ecc_buf, info->ecc_addr);
1570                 dma_free_coherent(NULL, info->mtd.writesize + info->mtd.oobsize,
1571                                   info->oob_dma_buf, info->oob_dma_addr);
1572                 kfree(info);
1573         }
1574
1575         return 0;
1576 }
1577
1578 static struct platform_driver tegra_nand_driver = {
1579         .probe   = tegra_nand_probe,
1580         .remove  = __devexit_p(tegra_nand_remove),
1581         .suspend = NULL,
1582         .resume  = NULL,
1583         .driver  = {
1584                 .name  = "tegra_nand",
1585                 .owner = THIS_MODULE,
1586         },
1587 };
1588
1589 static int __init
1590 tegra_nand_init(void)
1591 {
1592         return platform_driver_register(&tegra_nand_driver);
1593 }
1594
1595 static void __exit
1596 tegra_nand_exit(void)
1597 {
1598         platform_driver_unregister(&tegra_nand_driver);
1599 }
1600
1601 module_init(tegra_nand_init);
1602 module_exit(tegra_nand_exit);
1603
1604 MODULE_LICENSE("GPL");
1605 MODULE_DESCRIPTION(DRIVER_DESC);