GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / mtd / bcm947xx / nand / brcmnand.c
blobbf7670a414004179190b322c97d4c5ece9e777e0
1 /*
2 * Nortstar NAND controller driver
3 * for Linux NAND library and MTD interface
5 * Copyright (C) 2012, Broadcom Corporation. All Rights Reserved.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
14 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
16 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * This module interfaces the NAND controller and hardware ECC capabilities
20 * tp the generic NAND chip support in the NAND library.
22 * Notes:
23 * This driver depends on generic NAND driver, but works at the
24 * page level for operations.
26 * When a page is written, the ECC calculated also protects the OOB
27 * bytes not taken by ECC, and so the OOB must be combined with any
28 * OOB data that preceded the page-write operation in order for the
29 * ECC to be calculated correctly.
30 * Also, when the page is erased, but OOB data is not, HW ECC will
31 * indicate an error, because it checks OOB too, which calls for some
32 * help from the software in this driver.
34 * TBD:
35 * Block locking/unlocking support, OTP support
37 * $Id: $
40 #include <linux/io.h>
41 #include <linux/ioport.h>
42 #include <linux/delay.h>
43 #include <linux/bug.h>
44 #include <linux/err.h>
46 #include <linux/mtd/mtd.h>
47 #include <linux/mtd/nand.h>
48 #ifdef CONFIG_MTD_PARTITIONS
49 #include <linux/mtd/partitions.h>
50 #endif
52 #include <typedefs.h>
53 #include <osl.h>
54 #include <hndsoc.h>
55 #include <siutils.h>
56 #include <hndnand.h>
57 #include <nand_core.h>
60 * Because the bcm_nflash doesn't consider second chip case,
61 * So in this brcmnand driver we just define NANDC_MAX_CHIPS to 1
62 * And in the brcmnand_select_chip the argument chip_num == -1 means
63 * de-select the device. Since the bcm_nflash doesn't do select_chip,
64 * we must be careful to select other chip in the brcmnand driver.
67 #define NANDC_MAX_CHIPS 1
69 #define DRV_NAME "brcmnand"
70 #define DRV_VERSION "0.1"
71 #define DRV_DESC "Northstar brcmnand NAND Flash Controller driver"
73 /* Mutexing is version-dependent */
74 extern struct nand_hw_control *nand_hwcontrol_lock_init(void);
77 * Driver private control structure
79 struct brcmnand_mtd {
80 si_t *sih;
81 struct mtd_info mtd;
82 struct nand_chip chip;
83 #ifdef CONFIG_MTD_PARTITIONS
84 struct mtd_partition *parts;
85 #endif
86 hndnand_t *nfl;
88 struct nand_ecclayout ecclayout;
89 int cmd_ret; /* saved error code */
90 int page_addr; /* saved page address from SEQIN */
91 unsigned char oob_index;
92 unsigned char id_byte_index;
93 unsigned char last_cmd;
94 unsigned char ecc_level;
95 unsigned char sector_size_shift;
96 unsigned char sec_per_page_shift;
99 /* Single device present */
100 static struct brcmnand_mtd brcmnand;
103 * NAND Controller hardware ECC data size
105 * The following table contains the number of bytes needed for
106 * each of the ECC levels, per "sector", which is either 512 or 1024 bytes.
107 * The actual layout is as follows:
108 * The entire spare area is equally divided into as many sections as there
109 * are sectors per page, and the ECC data is located at the end of each
110 * of these sections.
111 * For example, given a 2K per page and 64 bytes spare device, configured for
112 * sector size 1k and ECC level of 4, the spare area will be divided into 2
113 * sections 32 bytes each, and the last 14 bytes of 32 in each section will
114 * be filled with ECC data.
115 * Note: the name of the algorythm and the number of error bits it can correct
116 * is of no consequence to this driver, therefore omitted.
118 static const struct brcmnand_ecc_size_s {
119 unsigned char sector_size_shift;
120 unsigned char ecc_level;
121 unsigned char ecc_bytes_per_sec;
122 unsigned char reserved;
123 } brcmnand_ecc_sizes [] = {
124 {9, 0, 0},
125 {10, 0, 0},
126 {9, 1, 2},
127 {10, 1, 4},
128 {9, 2, 4},
129 {10, 2, 7},
130 {9, 3, 6},
131 {10, 3, 11},
132 {9, 4, 7},
133 {10, 4, 14},
134 {9, 5, 9},
135 {10, 5, 18},
136 {9, 6, 11},
137 {10, 6, 21},
138 {9, 7, 13},
139 {10, 7, 25},
140 {9, 8, 14},
141 {10, 8, 28},
143 {9, 9, 16},
144 {9, 10, 18},
145 {9, 11, 20},
146 {9, 12, 21},
148 {10, 9, 32},
149 {10, 10, 35},
150 {10, 11, 39},
151 {10, 12, 42},
155 * INTERNAL - Populate the various fields that depend on how
156 * the hardware ECC data is located in the spare area
158 * For this controiller, it is easier to fill-in these
159 * structures at run time.
161 * The bad-block marker is assumed to occupy one byte
162 * at chip->badblockpos, which must be in the first
163 * sector of the spare area, namely it is either
164 * at offset 0 or 5.
165 * Some chips use both for manufacturer's bad block
166 * markers, but we ingore that issue here, and assume only
167 * one byte is used as bad-block marker always.
169 static int
170 brcmnand_hw_ecc_layout(struct brcmnand_mtd *brcmnand)
172 struct nand_ecclayout *layout;
173 unsigned i, j, k;
174 unsigned ecc_per_sec, oob_per_sec;
175 unsigned bbm_pos = brcmnand->chip.badblockpos;
177 DEBUG(MTD_DEBUG_LEVEL1, "%s: ecc_level %d\n",
178 __func__, brcmnand->ecc_level);
180 /* Caclculate spare area per sector size */
181 oob_per_sec = brcmnand->mtd.oobsize >> brcmnand->sec_per_page_shift;
183 /* Try to calculate the amount of ECC bytes per sector with a formula */
184 if (brcmnand->sector_size_shift == 9)
185 ecc_per_sec = ((brcmnand->ecc_level * 14) + 7) >> 3;
186 else if (brcmnand->sector_size_shift == 10)
187 ecc_per_sec = ((brcmnand->ecc_level * 14) + 3) >> 2;
188 else
189 ecc_per_sec = oob_per_sec + 1; /* cause an error if not in table */
191 DEBUG(MTD_DEBUG_LEVEL1, "%s: calc eccbytes %d\n", __func__, ecc_per_sec);
193 /* Now find out the answer according to the table */
194 for (i = 0; i < ARRAY_SIZE(brcmnand_ecc_sizes); i++) {
195 if (brcmnand_ecc_sizes[i].ecc_level == brcmnand->ecc_level &&
196 brcmnand_ecc_sizes[i].sector_size_shift == brcmnand->sector_size_shift) {
197 DEBUG(MTD_DEBUG_LEVEL1, "%s: table eccbytes %d\n", __func__,
198 brcmnand_ecc_sizes[i].ecc_bytes_per_sec);
199 break;
203 /* Table match overrides formula */
204 if (brcmnand_ecc_sizes[i].ecc_level == brcmnand->ecc_level &&
205 brcmnand_ecc_sizes[i].sector_size_shift == brcmnand->sector_size_shift)
206 ecc_per_sec = brcmnand_ecc_sizes[i].ecc_bytes_per_sec;
208 /* Return an error if calculated ECC leaves no room for OOB */
209 if ((brcmnand->sec_per_page_shift != 0 && ecc_per_sec >= oob_per_sec) ||
210 (brcmnand->sec_per_page_shift == 0 && ecc_per_sec >= (oob_per_sec-1))) {
211 printk(KERN_WARNING "%s: ERROR: ECC level %d too high, "
212 "leaves no room for OOB data\n", __func__, brcmnand->ecc_level);
213 return -EINVAL;
216 /* Fill in the needed fields */
217 brcmnand->chip.ecc.size = brcmnand->mtd.writesize >> brcmnand->sec_per_page_shift;
218 brcmnand->chip.ecc.bytes = ecc_per_sec;
219 brcmnand->chip.ecc.steps = 1 << brcmnand->sec_per_page_shift;
220 brcmnand->chip.ecc.total = ecc_per_sec << brcmnand->sec_per_page_shift;
222 /* Build an ecc layout data structure */
223 layout = &brcmnand->ecclayout;
224 memset(layout, 0, sizeof(*layout));
226 /* Total number of bytes used by HW ECC */
227 layout->eccbytes = ecc_per_sec << brcmnand->sec_per_page_shift;
229 /* Location for each of the HW ECC bytes */
230 for (i = j = 0, k = 1;
231 i < ARRAY_SIZE(layout->eccpos) && i < layout->eccbytes;
232 i++, j++) {
233 /* switch sector # */
234 if (j == ecc_per_sec) {
235 j = 0;
236 k ++;
238 /* save position of each HW-generated ECC byte */
239 layout->eccpos[i] = (oob_per_sec * k) - ecc_per_sec + j;
241 /* Check that HW ECC does not overlap bad-block marker */
242 if (bbm_pos == layout->eccpos[i]) {
243 printk(KERN_WARNING "%s: ERROR: ECC level %d too high, "
244 "HW ECC collides with bad-block marker position\n",
245 __func__, brcmnand->ecc_level);
247 return -EINVAL;
251 /* Location of all user-available OOB byte-ranges */
252 for (i = 0; i < ARRAY_SIZE(layout->oobfree); i++) {
253 if (i >= (1 << brcmnand->sec_per_page_shift))
254 break;
255 layout->oobfree[i].offset = oob_per_sec * i;
256 layout->oobfree[i].length = oob_per_sec - ecc_per_sec;
259 /* Bad-block marker must be in the first sector spare area */
260 BUG_ON(bbm_pos >= (layout->oobfree[i].offset+layout->oobfree[i].length));
261 if (i != 0)
262 continue;
264 /* Remove bad-block marker from available byte range */
265 if (bbm_pos == layout->oobfree[i].offset) {
266 layout->oobfree[i].offset += 1;
267 layout->oobfree[i].length -= 1;
269 else if (bbm_pos ==
270 (layout->oobfree[i].offset+layout->oobfree[i].length-1)) {
271 layout->oobfree[i].length -= 1;
273 else {
274 layout->oobfree[i+1].offset = bbm_pos + 1;
275 layout->oobfree[i+1].length =
276 layout->oobfree[i].length - bbm_pos - 1;
277 layout->oobfree[i].length = bbm_pos;
278 i ++;
282 layout->oobavail = ((oob_per_sec - ecc_per_sec) << brcmnand->sec_per_page_shift) - 1;
284 brcmnand->mtd.oobavail = layout->oobavail;
285 brcmnand->chip.ecclayout = layout;
286 brcmnand->chip.ecc.layout = layout;
288 /* Output layout for debugging */
289 printk("Spare area=%d eccbytes %d, ecc bytes located at:\n",
290 brcmnand->mtd.oobsize, layout->eccbytes);
291 for (i = j = 0; i < ARRAY_SIZE(layout->eccpos) && i < layout->eccbytes; i++) {
292 printk(" %d", layout->eccpos[i]);
294 printk("\nAvailable %d bytes at (off,len):\n", layout->oobavail);
295 for (i = 0; i < ARRAY_SIZE(layout->oobfree); i++) {
296 printk("(%d,%d) ",
297 layout->oobfree[i].offset,
298 layout->oobfree[i].length);
300 printk("\n");
302 return 0;
306 * INTERNAL - print NAND chip options
308 * Useful for debugging
310 static void
311 brcmnand_options_print(unsigned opts)
313 unsigned bit;
314 const char * n;
316 printk("Options: ");
317 for (bit = 0; bit < 32; bit ++) {
318 if (0 == (opts & (1<<bit)))
319 continue;
320 switch (1 << bit) {
321 default:
322 printk("OPT_%x", 1 << bit);
323 n = NULL;
324 break;
325 case NAND_NO_AUTOINCR:
326 n = "NO_AUTOINCR"; break;
327 case NAND_BUSWIDTH_16:
328 n = "BUSWIDTH_16"; break;
329 case NAND_NO_PADDING:
330 n = "NO_PADDING"; break;
331 case NAND_CACHEPRG:
332 n = "CACHEPRG"; break;
333 case NAND_COPYBACK:
334 n = "COPYBACK"; break;
335 case NAND_IS_AND:
336 n = "IS_AND"; break;
337 case NAND_4PAGE_ARRAY:
338 n = "4PAGE_ARRAY"; break;
339 case BBT_AUTO_REFRESH:
340 n = "AUTO_REFRESH"; break;
341 case NAND_NO_READRDY:
342 n = "NO_READRDY"; break;
343 case NAND_NO_SUBPAGE_WRITE:
344 n = "NO_SUBPAGE_WRITE"; break;
345 case NAND_BROKEN_XD:
346 n = "BROKEN_XD"; break;
347 case NAND_ROM:
348 n = "ROM"; break;
349 case NAND_USE_FLASH_BBT:
350 n = "USE_FLASH_BBT"; break;
351 case NAND_SKIP_BBTSCAN:
352 n = "SKIP_BBTSCAN"; break;
353 case NAND_OWN_BUFFERS:
354 n = "OWN_BUFFERS"; break;
355 case NAND_SCAN_SILENT_NODEV:
356 n = "SCAN_SILENT_NODEV"; break;
357 case NAND_CONTROLLER_ALLOC:
358 n = "SCAN_SILENT_NODEV"; break;
359 case NAND_BBT_SCAN2NDPAGE:
360 n = "BBT_SCAN2NDPAGE"; break;
361 case NAND_BBT_SCANBYTE1AND6:
362 n = "BBT_SCANBYTE1AND6"; break;
363 case NAND_BBT_SCANLASTPAGE:
364 n = "BBT_SCANLASTPAGE"; break;
366 printk("%s,", n);
368 printk("\n");
372 * NAND Interface - dev_ready
374 * Return 1 if device is ready, 0 otherwise
376 static int
377 brcmnand_dev_ready(struct mtd_info *mtd)
379 struct nand_chip *chip = (struct nand_chip *)mtd->priv;
380 struct brcmnand_mtd *brcmnand = (struct brcmnand_mtd *)chip->priv;
382 return (hndnand_dev_ready(brcmnand->nfl));
386 * NAND Interface - waitfunc
388 static int
389 brcmnand_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
391 int ret, status;
392 struct brcmnand_mtd *brcmnand = chip->priv;
394 /* deliver deferred error code if any */
395 if ((ret = brcmnand->cmd_ret) < 0)
396 brcmnand->cmd_ret = 0;
397 else {
398 if ((ret = hndnand_waitfunc(brcmnand->nfl, &status)) == 0)
399 ret = status;
402 /* Timeout */
403 if (ret < 0) {
404 DEBUG(MTD_DEBUG_LEVEL0, "%s: timeout\n", __func__);
405 return NAND_STATUS_FAIL;
408 DEBUG(MTD_DEBUG_LEVEL3, "%s: status=%#x\n", __func__, ret);
410 return ret;
414 * NAND Interface - read_oob
416 static int
417 brcmnand_read_oob(struct mtd_info *mtd, struct nand_chip *chip, int page, int sndcmd)
419 struct brcmnand_mtd *brcmnand = chip->priv;
420 uint64 nand_addr;
422 nand_addr = ((uint64)page << chip->page_shift);
423 return hndnand_read_oob(brcmnand->nfl, nand_addr, chip->oob_poi);
427 * NAND Interface - write_oob
429 static int
430 brcmnand_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
432 struct brcmnand_mtd *brcmnand = chip->priv;
433 uint64 nand_addr;
434 int status = 0;
436 nand_addr = ((uint64)page << chip->page_shift);
437 hndnand_write_oob(brcmnand->nfl, nand_addr, chip->oob_poi);
439 status = brcmnand_waitfunc(mtd, chip);
441 return status & NAND_STATUS_FAIL ? -EIO : 0;
445 * INTERNAL - read a page, with or without ECC checking
447 static int
448 _brcmnand_read_page_do(struct mtd_info *mtd, struct nand_chip *chip,
449 uint8_t *buf, int page, bool ecc)
451 struct brcmnand_mtd *brcmnand = chip->priv;
452 uint64 nand_addr;
453 uint32 herr = 0, serr = 0;
454 int ret;
456 nand_addr = ((uint64)page << chip->page_shift);
457 ret = hndnand_read_page(brcmnand->nfl, nand_addr, buf, chip->oob_poi, ecc, &herr, &serr);
459 if (ecc && ret == 0) {
460 /* Report hard ECC errors */
461 mtd->ecc_stats.failed += herr;
463 /* Get ECC soft error stats */
464 mtd->ecc_stats.corrected += serr;
466 DEBUG(MTD_DEBUG_LEVEL3, "%s: page=%#x err hard %d soft %d\n", __func__, page,
467 mtd->ecc_stats.failed, mtd->ecc_stats.corrected);
470 return ret;
474 * NAND Interface - read_page_ecc
476 static int
477 brcmnand_read_page_ecc(struct mtd_info *mtd, struct nand_chip *chip,
478 uint8_t *buf, int page)
480 return _brcmnand_read_page_do(mtd, chip, buf, page, TRUE);
484 * NAND Interface - read_page_raw
486 static int
487 brcmnand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
488 uint8_t *buf, int page)
490 return _brcmnand_read_page_do(mtd, chip, buf, page, TRUE);
494 * INTERNAL - do page write, with or without ECC generation enabled
496 static void
497 _brcmnand_write_page_do(struct mtd_info *mtd, struct nand_chip *chip, const uint8_t *buf, bool ecc)
499 struct brcmnand_mtd *brcmnand = chip->priv;
500 uint8_t tmp_poi[NAND_MAX_OOBSIZE];
501 uint64 nand_addr;
502 int i;
504 BUG_ON(mtd->oobsize > sizeof(tmp_poi));
506 /* Retreive pre-existing OOB values */
507 memcpy(tmp_poi, chip->oob_poi, mtd->oobsize);
508 brcmnand->cmd_ret = brcmnand_read_oob(mtd, chip, brcmnand->page_addr, 1);
509 if (brcmnand->cmd_ret < 0)
510 return;
512 /* Apply new OOB data bytes just like they would end up on the chip */
513 for (i = 0; i < mtd->oobsize; i ++)
514 chip->oob_poi[i] &= tmp_poi[i];
516 nand_addr = ((uint64)brcmnand->page_addr << chip->page_shift);
517 brcmnand->cmd_ret = hndnand_write_page(brcmnand->nfl, nand_addr, buf,
518 chip->oob_poi, ecc);
520 return;
524 * NAND Interface = write_page_ecc
526 static void
527 brcmnand_write_page_ecc(struct mtd_info *mtd, struct nand_chip *chip,
528 const uint8_t *buf)
530 _brcmnand_write_page_do(mtd, chip, buf, TRUE);
534 * NAND Interface = write_page_raw
536 static void
537 brcmnand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
538 const uint8_t *buf)
540 printk(KERN_INFO "%s: Enter!\n", __FUNCTION__);
542 _brcmnand_write_page_do(mtd, chip, buf, FALSE);
546 * MTD Interface - read_byte
548 * This function emulates simple controllers behavior
549 * for just a few relevant commands
551 static uint8_t
552 brcmnand_read_byte(struct mtd_info *mtd)
554 struct nand_chip *chip = mtd->priv;
555 struct brcmnand_mtd *brcmnand = chip->priv;
556 uint32 reg;
557 uint8_t b = ~0;
559 switch (brcmnand->last_cmd) {
560 case NAND_CMD_READID:
561 if (brcmnand->id_byte_index < 8) {
562 reg = hndnand_cmd_read_byte(brcmnand->nfl, CMDFUNC_READID,
563 (brcmnand->id_byte_index & 0x4));
564 reg >>= 24 - ((brcmnand->id_byte_index & 3) << 3);
565 b = (uint8_t) (reg & ((1 << 8) - 1));
567 brcmnand->id_byte_index ++;
569 break;
570 case NAND_CMD_READOOB:
571 if (brcmnand->oob_index < mtd->oobsize) {
572 b = chip->oob_poi[brcmnand->oob_index++];
574 break;
575 case NAND_CMD_STATUS:
576 b = (uint8_t)hndnand_cmd_read_byte(brcmnand->nfl, CMDFUNC_STATUS, 0);
577 break;
578 default:
579 BUG_ON(1);
582 DEBUG(MTD_DEBUG_LEVEL3, "%s: %#x\n", __func__, b);
584 return b;
588 * MTD Interface - read_word
590 * Can not be tested without x16 chip, but the SoC does not support x16 i/f.
592 static u16
593 brcmnand_read_word(struct mtd_info *mtd)
595 u16 w = ~0;
597 w = brcmnand_read_byte(mtd);
598 barrier();
599 w |= brcmnand_read_byte(mtd) << 8;
601 DEBUG(MTD_DEBUG_LEVEL0, "%s: %#x\n", __func__, w);
603 return w;
607 * MTD Interface - select a chip from an array
609 static void
610 brcmnand_select_chip(struct mtd_info *mtd, int chip_num)
612 struct nand_chip *chip = mtd->priv;
613 struct brcmnand_mtd *brcmnand = chip->priv;
615 /* chip_num == -1 means de-select the device */
616 hndnand_select_chip(brcmnand->nfl, 0);
620 * NAND Interface - emulate low-level NAND commands
622 * Only a few low-level commands are really needed by generic NAND,
623 * and they do not call for CMD_LL operations the controller can support.
625 static void
626 brcmnand_cmdfunc(struct mtd_info *mtd, unsigned command, int column, int page_addr)
628 struct nand_chip *chip = mtd->priv;
629 struct brcmnand_mtd *brcmnand = chip->priv;
630 uint64 nand_addr;
632 DEBUG(MTD_DEBUG_LEVEL3, "%s: cmd=%#x col=%#x pg=%#x\n", __func__,
633 command, column, page_addr);
635 brcmnand->last_cmd = command;
637 switch (command) {
638 case NAND_CMD_ERASE1:
639 column = 0;
640 BUG_ON(column >= mtd->writesize);
641 nand_addr = (uint64) column | ((uint64)page_addr << chip->page_shift);
642 hndnand_cmdfunc(brcmnand->nfl, nand_addr, CMDFUNC_ERASE1);
643 break;
645 case NAND_CMD_ERASE2:
646 brcmnand->cmd_ret = hndnand_cmdfunc(brcmnand->nfl, 0, CMDFUNC_ERASE2);
647 break;
649 case NAND_CMD_SEQIN:
650 BUG_ON(column >= mtd->writesize);
651 brcmnand->page_addr = page_addr;
652 nand_addr = (uint64) column | ((uint64)page_addr << chip->page_shift);
653 hndnand_cmdfunc(brcmnand->nfl, nand_addr, CMDFUNC_SEQIN);
654 break;
656 case NAND_CMD_READ0:
657 case NAND_CMD_READ1:
658 BUG_ON(column >= mtd->writesize);
659 nand_addr = (uint64) column | ((uint64)page_addr << chip->page_shift);
660 brcmnand->cmd_ret = hndnand_cmdfunc(brcmnand->nfl, nand_addr, CMDFUNC_READ);
661 break;
663 case NAND_CMD_RESET:
664 brcmnand->cmd_ret = hndnand_cmdfunc(brcmnand->nfl, 0, CMDFUNC_RESET);
665 break;
667 case NAND_CMD_READID:
668 brcmnand->cmd_ret = hndnand_cmdfunc(brcmnand->nfl, 0, CMDFUNC_READID);
669 brcmnand->id_byte_index = 0;
670 break;
672 case NAND_CMD_STATUS:
673 brcmnand->cmd_ret = hndnand_cmdfunc(brcmnand->nfl, 0, CMDFUNC_STATUS);
674 break;
676 case NAND_CMD_PAGEPROG:
677 /* Cmd already set from write_page */
678 break;
680 case NAND_CMD_READOOB:
681 /* Emulate simple interface */
682 brcmnand_read_oob(mtd, chip, page_addr, 1);
683 brcmnand->oob_index = 0;
684 break;
686 default:
687 BUG_ON(1);
691 static int
692 brcmnand_scan(struct mtd_info *mtd)
694 struct nand_chip *chip = mtd->priv;
695 struct brcmnand_mtd *brcmnand = chip->priv;
696 hndnand_t *nfl = brcmnand->nfl;
697 int ret;
700 * The spin_lock of chip->controller->lock is enough here,
701 * because we don't start any transation yet.
703 spin_lock(&chip->controller->lock);
704 ret = nand_scan_ident(mtd, NANDC_MAX_CHIPS, NULL);
705 spin_unlock(&chip->controller->lock);
706 if (ret)
707 return ret;
709 DEBUG(MTD_DEBUG_LEVEL0, "%s: scan_ident ret=%d num_chips=%d\n", __func__,
710 ret, chip->numchips);
712 /* Get configuration from first chip */
713 mtd->writesize_shift = chip->page_shift;
714 brcmnand->ecc_level = nfl->ecclevel;
715 brcmnand->sector_size_shift = ffs(nfl->sectorsize) - 1;
716 brcmnand->sec_per_page_shift = mtd->writesize_shift - brcmnand->sector_size_shift;
718 /* Generate ecc layout, will return -EINVAL if OOB space exhausted */
719 if ((ret = brcmnand_hw_ecc_layout(brcmnand)) != 0)
720 return ret;
722 DEBUG(MTD_DEBUG_LEVEL0, "%s: layout.oobavail=%d\n", __func__,
723 chip->ecc.layout->oobavail);
725 ret = nand_scan_tail(mtd);
727 DEBUG(MTD_DEBUG_LEVEL0, "%s: scan_tail ret=%d\n", __func__, ret);
729 if (chip->badblockbits == 0)
730 chip->badblockbits = 8;
731 BUG_ON((1<<chip->page_shift) != mtd->writesize);
733 /* Spit out some key chip parameters as detected by nand_base */
734 DEBUG(MTD_DEBUG_LEVEL0, "%s: erasesize=%d writesize=%d oobsize=%d "
735 " page_shift=%d badblockpos=%d badblockbits=%d\n", __func__,
736 mtd->erasesize, mtd->writesize, mtd->oobsize,
737 chip->page_shift, chip->badblockpos, chip->badblockbits);
739 brcmnand_options_print(chip->options);
741 return ret;
745 * Dummy function to make sure generic NAND does not call anything unexpected.
747 static int
748 brcmnand_dummy_func(struct mtd_info * mtd)
750 BUG_ON(1);
753 #ifdef CONFIG_MTD_PARTITIONS
754 struct mtd_partition brcmnand_parts[] = {
756 .name = "brcmnand",
757 .size = 0,
758 .offset = 0
761 .name = 0,
762 .size = 0,
763 .offset = 0
767 struct mtd_partition *
768 init_brcmnand_mtd_partitions(struct mtd_info *mtd, size_t size)
770 int knldev;
771 int offset = 0;
772 struct nand_chip *chip = mtd->priv;
773 struct brcmnand_mtd *brcmnand = chip->priv;
775 knldev = soc_knl_dev((void *)brcmnand->sih);
776 if (knldev == SOC_KNLDEV_NANDFLASH)
777 offset = NFL_BOOT_OS_SIZE;
779 ASSERT(size > offset);
781 brcmnand_parts[0].offset = offset;
782 brcmnand_parts[0].size = size - offset;
784 return brcmnand_parts;
786 #endif /* CONFIG_MTD_PARTITIONS */
788 static int __init
789 brcmnand_mtd_init(void)
791 int ret = 0;
792 unsigned chip_num;
793 hndnand_t *nfl;
794 struct nand_chip *chip;
795 struct mtd_info *mtd;
796 #ifdef CONFIG_MTD_PARTITIONS
797 struct mtd_partition *parts;
798 int i;
799 #endif
800 uint32 *offset;
801 uint32 reg, size, block, page, ecc_level;
803 printk(KERN_INFO "%s, Version %s (c) Broadcom Inc. 2012\n",
804 DRV_DESC, DRV_VERSION);
806 memset(&brcmnand, 0, sizeof(struct brcmnand_mtd));
808 /* attach to the backplane */
809 if (!(brcmnand.sih = si_kattach(SI_OSH))) {
810 printk(KERN_ERR "brcmnand: error attaching to backplane\n");
811 ret = -EIO;
812 goto fail;
815 /* Initialize serial flash access */
816 if (!(nfl = hndnand_init(brcmnand.sih))) {
817 printk(KERN_ERR "brcmnand: found no supported devices\n");
818 ret = -ENODEV;
819 goto fail;
822 brcmnand.nfl = nfl;
824 /* Software variables init */
825 mtd = &brcmnand.mtd;
826 chip = &brcmnand.chip;
828 mtd->priv = chip;
829 mtd->owner = THIS_MODULE;
830 mtd->name = DRV_NAME;
832 chip->priv = &brcmnand;
833 chip->controller = nand_hwcontrol_lock_init();
834 if (!chip->controller) {
835 ret = -ENOMEM;
836 goto fail;
839 chip->chip_delay = 5; /* not used */
840 chip->IO_ADDR_R = chip->IO_ADDR_W = (void *)~0L;
842 /* CS0 device width */
843 if (nfl->width)
844 chip->options |= NAND_BUSWIDTH_16;
846 chip->options |= NAND_NO_SUBPAGE_WRITE; /* Subpages unsupported */
848 chip->ecc.mode = NAND_ECC_HW;
850 chip->dev_ready = brcmnand_dev_ready;
851 chip->read_byte = brcmnand_read_byte;
852 chip->read_word = brcmnand_read_word;
853 chip->ecc.read_page_raw = brcmnand_read_page_raw;
854 chip->ecc.write_page_raw = brcmnand_write_page_raw;
855 chip->ecc.read_page = brcmnand_read_page_ecc;
856 chip->ecc.write_page = brcmnand_write_page_ecc;
857 chip->ecc.read_oob = brcmnand_read_oob;
858 chip->ecc.write_oob = brcmnand_write_oob;
859 chip->select_chip = brcmnand_select_chip;
860 chip->cmdfunc = brcmnand_cmdfunc;
861 chip->waitfunc = brcmnand_waitfunc;
862 chip->read_buf = (void *)brcmnand_dummy_func;
863 chip->write_buf = (void *)brcmnand_dummy_func;
864 chip->verify_buf = (void *)brcmnand_dummy_func;
866 if (brcmnand_scan(mtd)) {
867 ret = -ENXIO;
868 goto fail;
871 #ifdef CONFIG_MTD_PARTITIONS
872 parts = init_brcmnand_mtd_partitions(&brcmnand.mtd, brcmnand.mtd.size);
873 if (!parts)
874 goto fail;
876 for (i = 0; parts[i].name; i++);
878 ret = add_mtd_partitions(&brcmnand.mtd, parts, i);
879 if (ret < 0) {
880 DEBUG(MTD_DEBUG_LEVEL0, "%s: failed to add mtd partitions\n", __func__);
881 goto fail;
884 brcmnand.parts = parts;
885 #endif /* CONFIG_MTD_PARTITIONS */
887 return 0;
889 fail:
890 return ret;
893 static void __exit
894 brcmnand_mtd_exit(void)
896 #ifdef CONFIG_MTD_PARTITIONS
897 del_mtd_partitions(&brcmnand.mtd);
898 #else
899 del_mtd_device(&brcmnand.mtd);
900 #endif
903 late_initcall(brcmnand_mtd_init);
904 module_exit(brcmnand_mtd_exit);