1 --- a/arch/mips/bcm47xx/Kconfig
2 +++ b/arch/mips/bcm47xx/Kconfig
3 @@ -24,6 +24,7 @@ config BCM47XX_BCMA
4 select BCMA_DRIVER_MIPS
5 select BCMA_DRIVER_PCI_HOSTMODE if PCI
10 Add support for new Broadcom BCM47xx boards with Broadcom specific Advanced Microcontroller Bus.
11 --- a/arch/mips/bcm47xx/bus.c
12 +++ b/arch/mips/bcm47xx/bus.c
14 * BCM947xx nvram variable access
16 * Copyright (C) 2011 Hauke Mehrtens <hauke@hauke-m.de>
17 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by the
21 @@ -80,3 +81,9 @@ void bcm47xx_sflash_struct_ssb_init(stru
22 sflash->numblocks = scc->sflash.numblocks;
23 sflash->size = scc->sflash.size;
26 +void bcm47xx_nflash_struct_bcma_init(struct bcm47xx_nflash *nflash, struct bcma_drv_cc *bcc)
28 + nflash->nflash_type = BCM47XX_BUS_TYPE_BCMA;
31 --- a/arch/mips/bcm47xx/nvram.c
32 +++ b/arch/mips/bcm47xx/nvram.c
34 * Copyright (C) 2005 Broadcom Corporation
35 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
36 * Copyright (C) 2010-2011 Hauke Mehrtens <hauke@hauke-m.de>
37 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
39 * This program is free software; you can redistribute it and/or modify it
40 * under the terms of the GNU General Public License as published by the
42 #include <asm/mach-bcm47xx/nvram.h>
43 #include <asm/mach-bcm47xx/bcm47xx.h>
44 #include <asm/mach-bcm47xx/bus.h>
45 +#include <linux/mtd/bcm47xx_nand.h>
47 static char nvram_buf[NVRAM_SPACE];
49 @@ -134,6 +136,51 @@ found:
53 +static int early_nvram_init_nflash(void)
55 + struct nvram_header *header;
59 + u32 flash_size = bcm47xx_nflash.size;
60 + u8 tmpbuf[NFL_SECTOR_SIZE];
64 + /* check if the struct is already initilized */
71 + while (off <= flash_size) {
72 + ret = bcma_nflash_read(bcm47xx_nflash.bcc, off, NFL_SECTOR_SIZE, tmpbuf);
73 + if (ret != NFL_SECTOR_SIZE)
75 + header = (struct nvram_header *)tmpbuf;
76 + if (header->magic == NVRAM_HEADER)
86 + header = (struct nvram_header *) KSEG1ADDR(NAND_FLASH1 + off);
87 + src = (u32 *) header;
88 + dst = (u32 *) nvram_buf;
89 + for (i = 0; i < sizeof(struct nvram_header); i += 4)
91 + for (; i < len && i < NVRAM_SPACE; i += 4)
98 #ifdef CONFIG_BCM47XX_SSB
99 static void early_nvram_init_ssb(void)
101 @@ -164,6 +211,10 @@ static void early_nvram_init_bcma(void)
102 err = early_nvram_init_sflash();
104 printk(KERN_WARNING "can not read from flash: %i\n", err);
106 + err = early_nvram_init_nflash();
108 + printk(KERN_WARNING "can not read from nflash: %i\n", err);
110 printk(KERN_WARNING "unknow flash type\n");
112 --- a/arch/mips/bcm47xx/setup.c
113 +++ b/arch/mips/bcm47xx/setup.c
115 * Copyright (C) 2006 Michael Buesch <m@bues.ch>
116 * Copyright (C) 2010 Waldemar Brodkorb <wbx@openadk.org>
117 * Copyright (C) 2010-2011 Hauke Mehrtens <hauke@hauke-m.de>
118 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
120 * This program is free software; you can redistribute it and/or modify it
121 * under the terms of the GNU General Public License as published by the
122 @@ -46,6 +47,7 @@ enum bcm47xx_bus_type bcm47xx_bus_type;
123 EXPORT_SYMBOL(bcm47xx_bus_type);
125 struct bcm47xx_sflash bcm47xx_sflash;
126 +struct bcm47xx_nflash bcm47xx_nflash;
128 static struct resource bcm47xx_pflash_resource = {
129 .name = "bcm47xx_pflash",
130 @@ -73,6 +75,19 @@ static struct platform_device bcm47xx_sf
134 +static struct resource bcm47xx_nflash_resource = {
135 + .name = "bcm47xx_nflash",
141 +static struct platform_device bcm47xx_nflash_dev = {
142 + .name = "bcm47xx_nflash",
143 + .resource = &bcm47xx_nflash_resource,
144 + .num_resources = 1,
147 static void bcm47xx_machine_restart(char *command)
149 printk(KERN_ALERT "Please stand by while rebooting the system...\n");
150 @@ -369,6 +384,9 @@ static void __init bcm47xx_register_bcma
152 if (bcm47xx_bus.bcma.bus.drv_cc.flash_type == BCMA_SFLASH)
153 bcm47xx_sflash_struct_bcma_init(&bcm47xx_sflash, &bcm47xx_bus.bcma.bus.drv_cc);
155 + if (bcm47xx_bus.bcma.bus.drv_cc.flash_type == BCMA_NFLASH)
156 + bcm47xx_nflash_struct_bcma_init(&bcm47xx_nflash, &bcm47xx_bus.bcma.bus.drv_cc);
159 static int __init bcm47xx_register_flash_bcma(void)
160 @@ -383,6 +401,9 @@ static int __init bcm47xx_register_flash
162 bcm47xx_sflash_dev.dev.platform_data = &bcm47xx_sflash;
163 return platform_device_register(&bcm47xx_sflash_dev);
165 + bcm47xx_nflash_dev.dev.platform_data = &bcm47xx_nflash;
166 + return platform_device_register(&bcm47xx_nflash_dev);
168 printk(KERN_ERR "No flash device found\n");
170 --- a/arch/mips/include/asm/mach-bcm47xx/bus.h
171 +++ b/arch/mips/include/asm/mach-bcm47xx/bus.h
173 * BCM947xx nvram variable access
175 * Copyright (C) 2011 Hauke Mehrtens <hauke@hauke-m.de>
176 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
178 * This program is free software; you can redistribute it and/or modify it
179 * under the terms of the GNU General Public License as published by the
181 #include <linux/bcma/bcma.h>
182 #include <linux/mtd/mtd.h>
184 +#include <linux/mtd/nand.h>
186 struct bcm47xx_sflash {
187 enum bcm47xx_bus_type sflash_type;
188 @@ -29,11 +31,24 @@ struct bcm47xx_sflash {
189 u32 blocksize; /* Block size */
190 u32 numblocks; /* Number of blocks */
191 u32 size; /* Total size in bytes */
193 - struct mtd_info *mtd;
196 void bcm47xx_sflash_struct_bcma_init(struct bcm47xx_sflash *sflash, struct bcma_drv_cc *bcc);
197 void bcm47xx_sflash_struct_ssb_init(struct bcm47xx_sflash *sflash, struct ssb_chipcommon *scc);
199 extern struct bcm47xx_sflash bcm47xx_sflash;
201 +struct bcm47xx_nflash {
202 + enum bcm47xx_bus_type nflash_type;
203 + struct bcma_drv_cc *bcc;
205 + u32 size; /* Total size in bytes */
206 + u32 next_opcode; /* Next expected command from upper NAND layer */
208 + struct mtd_info mtd;
209 + struct nand_chip nand;
212 +void bcm47xx_nflash_struct_bcma_init(struct bcm47xx_nflash *nflash, struct bcma_drv_cc *bcc);
214 +extern struct bcm47xx_nflash bcm47xx_nflash;
215 --- a/drivers/bcma/Kconfig
216 +++ b/drivers/bcma/Kconfig
217 @@ -43,6 +43,11 @@ config BCMA_SFLASH
218 depends on BCMA_DRIVER_MIPS
223 + depends on BCMA_DRIVER_MIPS
226 config BCMA_DRIVER_MIPS
227 bool "BCMA Broadcom MIPS core driver"
228 depends on BCMA && MIPS
229 --- a/drivers/bcma/Makefile
230 +++ b/drivers/bcma/Makefile
232 bcma-y += main.o scan.o core.o sprom.o
233 bcma-y += driver_chipcommon.o driver_chipcommon_pmu.o
234 bcma-$(CONFIG_BCMA_SFLASH) += driver_chipcommon_sflash.o
235 +bcma-$(CONFIG_BCMA_NFLASH) += driver_chipcommon_nflash.o
236 bcma-y += driver_pci.o
237 bcma-$(CONFIG_BCMA_DRIVER_PCI_HOSTMODE) += driver_pci_host.o
238 bcma-$(CONFIG_BCMA_DRIVER_MIPS) += driver_mips.o
239 --- a/drivers/bcma/bcma_private.h
240 +++ b/drivers/bcma/bcma_private.h
241 @@ -46,6 +46,11 @@ u32 bcma_pmu_get_clockcpu(struct bcma_dr
242 int bcma_sflash_init(struct bcma_drv_cc *cc);
243 #endif /* CONFIG_BCMA_SFLASH */
245 +#ifdef CONFIG_BCMA_NFLASH
246 +/* driver_chipcommon_nflash.c */
247 +int bcma_nflash_init(struct bcma_drv_cc *cc);
248 +#endif /* CONFIG_BCMA_NFLASH */
250 #ifdef CONFIG_BCMA_HOST_PCI
252 extern int __init bcma_host_pci_init(void);
254 +++ b/drivers/bcma/driver_chipcommon_nflash.c
257 + * BCMA nand flash interface
259 + * Copyright 2011, Tathagata Das <tathagata@alumnux.com>
260 + * Copyright 2010, Broadcom Corporation
262 + * Licensed under the GNU/GPL. See COPYING for details.
265 +#include <linux/bcma/bcma.h>
266 +#include <linux/bcma/bcma_driver_chipcommon.h>
267 +#include <linux/delay.h>
268 +#include <linux/mtd/bcm47xx_nand.h>
269 +#include <linux/mtd/nand.h>
271 +#include "bcma_private.h"
273 +/* Issue a nand flash command */
274 +static inline void bcma_nflash_cmd(struct bcma_drv_cc *cc, u32 opcode)
276 + bcma_cc_write32(cc, NAND_CMD_START, opcode);
277 + bcma_cc_read32(cc, NAND_CMD_START);
280 +/* Check offset and length */
281 +static int bcma_nflash_offset_is_valid(struct bcma_drv_cc *cc, u32 offset, u32 len, u32 mask)
283 + if ((offset & mask) != 0 || (len & mask) != 0) {
284 + pr_err("%s(): Address is not aligned. offset: %x, len: %x, mask: %x\n", __func__, offset, len, mask);
288 + if ((((offset + len) >> 20) >= cc->nflash.size) &&
289 + (((offset + len) & ((1 << 20) - 1)) != 0)) {
290 + pr_err("%s(): Address is outside Flash memory region. offset: %x, len: %x, mask: %x\n", __func__, offset, len, mask);
297 +/* Read len bytes starting at offset into buf. Returns number of bytes read. */
298 +int bcma_nflash_read(struct bcma_drv_cc *cc, u32 offset, u32 len, u8 *buf)
304 + mask = NFL_SECTOR_SIZE - 1;
305 + if (bcma_nflash_offset_is_valid(cc, offset, len, mask))
311 + bcma_cc_write32(cc, NAND_CMD_ADDR, offset);
312 + bcma_nflash_cmd(cc, NCMD_PAGE_RD);
313 + if (bcma_nflash_poll(cc) < 0)
315 + val = bcma_cc_read32(cc, NAND_INTFC_STATUS);
316 + if ((val & NIST_CACHE_VALID) == 0)
318 + bcma_cc_write32(cc, NAND_CACHE_ADDR, 0);
319 + for (i = 0; i < NFL_SECTOR_SIZE; i += 4, to++) {
320 + *to = bcma_cc_read32(cc, NAND_CACHE_DATA);
322 + res -= NFL_SECTOR_SIZE;
323 + offset += NFL_SECTOR_SIZE;
325 + return (len - res);
328 +#define NF_RETRIES 1000000
330 +/* Poll for command completion. Returns zero when complete. */
331 +int bcma_nflash_poll(struct bcma_drv_cc *cc)
333 + u32 retries = NF_RETRIES;
334 + u32 pollmask = NIST_CTRL_READY|NIST_FLASH_READY;
337 + while (retries--) {
338 + mask = bcma_cc_read32(cc, NAND_INTFC_STATUS) & pollmask;
339 + if (mask == pollmask)
345 + pr_err("bcma_nflash_poll: not ready\n");
352 +/* Write len bytes starting at offset into buf. Returns success (0) or failure (!0).
353 + * Should poll for completion.
355 +int bcma_nflash_write(struct bcma_drv_cc *cc, u32 offset, u32 len,
360 + u32 *from, res, reg;
362 + mask = cc->nflash.pagesize - 1;
363 + if (bcma_nflash_offset_is_valid(cc, offset, len, mask))
366 + /* disable partial page enable */
367 + reg = bcma_cc_read32(cc, NAND_ACC_CONTROL);
368 + reg &= ~NAC_PARTIAL_PAGE_EN;
369 + bcma_cc_write32(cc, NAND_ACC_CONTROL, reg);
374 + bcma_cc_write32(cc, NAND_CACHE_ADDR, 0);
375 + for (i = 0; i < cc->nflash.pagesize; i += 4, from++) {
377 + bcma_cc_write32(cc, NAND_CMD_ADDR, i);
378 + bcma_cc_write32(cc, NAND_CACHE_DATA, *from);
380 + bcma_cc_write32(cc, NAND_CMD_ADDR, offset + cc->nflash.pagesize - 512);
381 + bcma_nflash_cmd(cc, NCMD_PAGE_PROG);
382 + if (bcma_nflash_poll(cc) < 0)
384 + res -= cc->nflash.pagesize;
385 + offset += cc->nflash.pagesize;
391 + return (len - res);
394 +/* Erase a region. Returns success (0) or failure (-1).
395 + * Poll for completion.
397 +int bcma_nflash_erase(struct bcma_drv_cc *cc, u32 offset)
399 + if ((offset >> 20) >= cc->nflash.size)
401 + if ((offset & (cc->nflash.blocksize - 1)) != 0)
404 + bcma_cc_write32(cc, NAND_CMD_ADDR, offset);
405 + bcma_nflash_cmd(cc, NCMD_BLOCK_ERASE);
406 + if (bcma_nflash_poll(cc) < 0)
410 --- a/drivers/bcma/driver_mips.c
411 +++ b/drivers/bcma/driver_mips.c
413 * Copyright 2006, 2007, Michael Buesch <mb@bu3sch.de>
414 * Copyright 2010, Bernhard Loos <bernhardloos@googlemail.com>
415 * Copyright 2011, Hauke Mehrtens <hauke@hauke-m.de>
416 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
418 * Licensed under the GNU/GPL. See COPYING for details.
420 @@ -182,6 +183,17 @@ static void bcma_core_mips_flash_detect(
422 struct bcma_bus *bus = mcore->core->bus;
424 + if (bus->drv_cc.core->id.rev == 38
425 + && (bus->drv_cc.status & (1 << 4)) != 0) {
426 +#ifdef CONFIG_BCMA_NFLASH
427 + pr_info("found nand flash.\n");
428 + bus->drv_cc.flash_type = BCMA_NFLASH;
430 + pr_info("NAND flash not supported.\n");
435 switch (bus->drv_cc.capabilities & BCMA_CC_CAP_FLASHT) {
436 case BCMA_CC_FLASHT_STSER:
437 case BCMA_CC_FLASHT_ATSER:
438 --- a/drivers/mtd/nand/Kconfig
439 +++ b/drivers/mtd/nand/Kconfig
440 @@ -537,4 +537,12 @@ config MTD_NAND_FSMC
441 Enables support for NAND Flash chips on the ST Microelectronics
442 Flexible Static Memory Controller (FSMC)
444 +config MTD_NAND_BCM47XX
445 + tristate "bcm47xx nand flash support"
448 + select MTD_PARTITIONS
450 + Support for bcm47xx nand flash
453 --- a/drivers/mtd/nand/Makefile
454 +++ b/drivers/mtd/nand/Makefile
455 @@ -49,5 +49,6 @@ obj-$(CONFIG_MTD_NAND_MPC5121_NFC) += mp
456 obj-$(CONFIG_MTD_NAND_RICOH) += r852.o
457 obj-$(CONFIG_MTD_NAND_JZ4740) += jz4740_nand.o
458 obj-$(CONFIG_MTD_NAND_GPMI_NAND) += gpmi-nand/
459 +obj-$(CONFIG_MTD_NAND_BCM47XX) += bcm47xx_nand.o
461 nand-objs := nand_base.o nand_bbt.o
463 +++ b/drivers/mtd/nand/bcm47xx_nand.c
466 + * BCMA nand flash interface
468 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
469 + * Copyright 2010, Broadcom Corporation
471 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
472 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
473 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
474 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
478 +#define pr_fmt(fmt) "bcm47xx_nflash: " fmt
479 +#include <linux/module.h>
480 +#include <linux/slab.h>
481 +#include <linux/ioport.h>
482 +#include <linux/sched.h>
483 +#include <linux/mtd/mtd.h>
484 +#include <linux/mtd/map.h>
485 +#include <linux/mtd/partitions.h>
486 +#include <linux/errno.h>
487 +#include <linux/delay.h>
488 +#include <linux/platform_device.h>
489 +#include <bcm47xx.h>
491 +#include <linux/cramfs_fs.h>
492 +#include <linux/romfs_fs.h>
493 +#include <linux/magic.h>
494 +#include <linux/byteorder/generic.h>
495 +#include <linux/mtd/bcm47xx_nand.h>
496 +#include <linux/mtd/nand.h>
498 +static int bcm47xx_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip);
499 +static int bcm47xx_erase(struct mtd_info *mtd, unsigned int addr, unsigned int len);
501 +/* Private Global variable */
502 +static u32 read_offset = 0;
503 +static u32 write_offset;
506 +nflash_mtd_poll(struct bcm47xx_nflash *nflash, unsigned int offset, int timeout)
508 + unsigned long now = jiffies;
512 + if (!bcma_nflash_poll(nflash->bcc)) {
516 + if (time_after(jiffies, now + timeout)) {
517 + pr_err("timeout while polling\n");
528 +bcm47xx_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
530 + struct nand_chip *nchip = (struct nand_chip *)mtd->priv;
531 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
532 + int bytes, ret = 0;
536 + u32 offset, blocksize, mask, off;
537 + u32 skip_bytes = 0;
541 + /* Check address range */
544 + if ((from + len) > mtd->size)
547 + if ((offset & (NFL_SECTOR_SIZE - 1)) != 0) {
548 + extra = offset & (NFL_SECTOR_SIZE - 1);
553 + size = (len + (NFL_SECTOR_SIZE - 1)) & ~(NFL_SECTOR_SIZE - 1);
560 + tmpbuf = (u8 *)kmalloc(size, GFP_KERNEL);
564 + blocksize = mtd->erasesize;
565 + mask = blocksize - 1;
568 + off = offset + skip_bytes;
569 + if ((bytes = bcma_nflash_read(nflash->bcc, off, NFL_SECTOR_SIZE, ptr)) < 0) {
584 + memcpy(buf, tmpbuf+extra, *retlen);
591 +static void bcm47xx_write(struct mtd_info *mtd, u32 to, const u_char *buf, u32 len)
593 + struct nand_chip *nchip = (struct nand_chip *)mtd->priv;
594 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
595 + u32 offset, blocksize, mask, off;
597 + u32 copy_len, write_len, from;
598 + u_char *write_ptr, *block;
602 + /* Check address range */
604 + pr_err("Error: Attempted to write too small data\n");
611 + if ((to + len) > mtd->size) {
612 + pr_err("Error: Attempted to write too large data\n");
619 + blocksize = mtd->erasesize;
620 + if (!(block = kmalloc(blocksize, GFP_KERNEL)))
622 + mask = blocksize - 1;
625 + from = offset & ~mask;
626 + /* Copy existing data into holding block if necessary */
627 + if (((offset & (blocksize-1)) != 0) || (len < blocksize)) {
628 + if ((ret = bcm47xx_read(mtd, from, blocksize, &read_len, block)))
630 + if (read_len != blocksize) {
636 + /* Copy input data into holding block */
637 + copy_len = min(len, blocksize - (offset & mask));
638 + memcpy(block + (offset & mask), ptr, copy_len);
641 + if ((ret = bcm47xx_erase(mtd, off, blocksize)) < 0)
643 + /* Write holding block */
645 + write_len = blocksize;
646 + if ((bytes = bcma_nflash_write(nflash->bcc, (uint)from, (uint)write_len, (u8 *) write_ptr)) != 0) {
650 + offset += copy_len;
651 + if (len < copy_len)
664 +static int bcm47xx_erase(struct mtd_info *mtd, unsigned int addr, unsigned int len)
666 + struct nand_chip *nchip = (struct nand_chip *)mtd->priv;
667 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
669 + /* Check address range */
672 + if ((addr + len) > mtd->size)
675 + if (bcma_nflash_erase(nflash->bcc, addr)) {
676 + pr_err("ERASE: nflash erase error\n");
680 + if (nflash_mtd_poll(nflash, addr, 10 * HZ)) {
681 + pr_err("ERASE: nflash_mtd_poll error\n");
688 +/* This functions is used by upper layer to checks if device is ready */
689 +static int bcm47xx_dev_ready(struct mtd_info *mtd)
694 +/* Issue a nand flash command */
695 +static inline void bcm47xx_nflash_cmd(struct bcma_drv_cc *cc, u32 opcode)
697 + bcma_cc_write32(cc, NAND_CMD_START, opcode);
698 + bcma_cc_read32(cc, NAND_CMD_START);
701 +static void bcm47xx_command(struct mtd_info *mtd, unsigned command,
702 + int column, int page_addr)
704 + struct nand_chip *nchip = (struct nand_chip *)mtd->priv;
705 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
706 + u32 pagesize = 1 << nchip->page_shift;
708 + /* Command pre-processing step */
710 + case NAND_CMD_RESET:
711 + bcm47xx_nflash_cmd(nflash->bcc, NCMD_FLASH_RESET);
714 + case NAND_CMD_STATUS:
715 + nflash->next_opcode = NAND_CMD_STATUS;
720 + case NAND_CMD_READ0:
721 + read_offset = page_addr * pagesize;
722 + nflash->next_opcode = 0;
725 + case NAND_CMD_READOOB:
726 + read_offset = page_addr * pagesize;
727 + nflash->next_opcode = 0;
730 + case NAND_CMD_SEQIN:
731 + write_offset = page_addr * pagesize;
732 + nflash->next_opcode = 0;
735 + case NAND_CMD_PAGEPROG:
736 + nflash->next_opcode = 0;
739 + case NAND_CMD_READID:
740 + read_offset = column;
741 + bcm47xx_nflash_cmd(nflash->bcc, NCMD_ID_RD);
742 + nflash->next_opcode = NAND_DEVID;
745 + case NAND_CMD_ERASE1:
746 + nflash->next_opcode = 0;
747 + bcm47xx_erase(mtd, page_addr*pagesize, pagesize);
750 + case NAND_CMD_ERASE2:
753 + case NAND_CMD_RNDOUT:
754 + if (column > mtd->writesize)
755 + read_offset += (column - mtd->writesize);
757 + read_offset += column;
761 + pr_err("COMMAND not supported %x\n", command);
762 + nflash->next_opcode = 0;
767 +/* This function is used by upper layer for select and
768 + * deselect of the NAND chip.
769 + * It is dummy function. */
770 +static void bcm47xx_select_chip(struct mtd_info *mtd, int chip)
774 +static u_char bcm47xx_read_byte(struct mtd_info *mtd)
776 + struct nand_chip *nchip = mtd->priv;
777 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
781 + if (nflash->next_opcode == 0)
784 + if (nflash->next_opcode == NAND_CMD_STATUS)
785 + return NAND_STATUS_WP;
787 + id = bcma_cc_read32(nflash->bcc, nflash->next_opcode);
789 + if (nflash->next_opcode == NAND_DEVID) {
790 + ret = (id >> (8*read_offset)) & 0xff;
797 +static uint16_t bcm47xx_read_word(struct mtd_info *mtd)
799 + loff_t from = read_offset;
803 + bcm47xx_read(mtd, from, sizeof(buf), &bytes, (u_char *)&buf);
807 +/* Write data of length len to buffer buf. The data to be
808 + * written on NAND Flash is first copied to RAMbuffer. After the Data Input
809 + * Operation by the NFC, the data is written to NAND Flash */
810 +static void bcm47xx_write_buf(struct mtd_info *mtd,
811 + const u_char *buf, int len)
813 + bcm47xx_write(mtd, write_offset, buf, len);
816 +/* Read the data buffer from the NAND Flash. To read the data from NAND
817 + * Flash first the data output cycle is initiated by the NFC, which copies
818 + * the data to RAMbuffer. This data of length len is then copied to buffer buf.
820 +static void bcm47xx_read_buf(struct mtd_info *mtd, u_char *buf, int len)
822 + loff_t from = read_offset;
825 + bcm47xx_read(mtd, from, len, &bytes, buf);
828 +/* Used by the upper layer to verify the data in NAND Flash
829 + * with the data in the buf. */
830 +static int bcm47xx_verify_buf(struct mtd_info *mtd,
831 + const u_char *buf, int len)
836 +static int bcm47xx_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
838 + struct nand_chip *nchip = mtd->priv;
839 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
842 + u32 pagesize = 1 << nchip->page_shift;
843 + u32 blocksize = mtd->erasesize;
845 + if ((ofs >> 20) >= nflash->size)
847 + if ((ofs & (blocksize - 1)) != 0)
850 + for (i = 0; i < 2; i++) {
851 + off = ofs + pagesize;
852 + bcma_cc_write32(nflash->bcc, NAND_CMD_ADDR, off);
853 + bcm47xx_nflash_cmd(nflash->bcc, NCMD_SPARE_RD);
854 + if (bcma_nflash_poll(nflash->bcc) < 0)
856 + if ((bcma_cc_read32(nflash->bcc, NAND_INTFC_STATUS) & NIST_SPARE_VALID) != NIST_SPARE_VALID)
858 + if ((bcma_cc_read32(nflash->bcc, NAND_SPARE_RD0) & 0xff) != 0xff)
864 +const char *part_probes[] = { "cmdlinepart", NULL };
865 +static int bcm47xx_probe(struct platform_device *pdev)
867 + struct nand_chip *nchip;
868 + struct mtd_info *mtd;
869 + struct bcm47xx_nflash *nflash = dev_get_platdata(&pdev->dev);
872 + mtd = &nflash->mtd;
873 + nchip = &nflash->nand;
875 + /* Register with MTD */
877 + mtd->owner = THIS_MODULE;
878 + mtd->dev.parent = &pdev->dev;
880 + /* 50 us command delay time */
881 + nchip->chip_delay = 50;
883 + nchip->priv = nflash;
884 + nchip->dev_ready = bcm47xx_dev_ready;
885 + nchip->cmdfunc = bcm47xx_command;
886 + nchip->select_chip = bcm47xx_select_chip;
887 + nchip->read_byte = bcm47xx_read_byte;
888 + nchip->read_word = bcm47xx_read_word;
889 + nchip->write_buf = bcm47xx_write_buf;
890 + nchip->read_buf = bcm47xx_read_buf;
891 + nchip->verify_buf = bcm47xx_verify_buf;
892 + nchip->block_bad = bcm47xx_block_bad;
893 + nchip->options = NAND_SKIP_BBTSCAN;
896 + nchip->ecc.mode = NAND_ECC_NONE;
898 + /* first scan to find the device and get the page size */
899 + if (nand_scan_ident(mtd, 1, NULL)) {
900 + pr_err("nand_scan_ident failed\n");
904 + nflash->bcc->nflash.size = mtd->size;
905 + nflash->bcc->nflash.pagesize = 1 << nchip->page_shift;
906 + nflash->bcc->nflash.blocksize = mtd->erasesize;
907 + bcm47xx_nflash.size = mtd->size;
909 + /* second phase scan */
910 + if (nand_scan_tail(mtd)) {
911 + pr_err("nand_scan_tail failed\n");
916 + mtd->name = "bcm47xx-nflash";
917 + mtd->flags |= MTD_WRITEABLE;
918 + ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
921 + pr_err("mtd_device_register failed\n");
931 +static int __devexit bcm47xx_remove(struct platform_device *pdev)
933 + struct bcm47xx_nflash *nflash = dev_get_platdata(&pdev->dev);
934 + struct mtd_info *mtd = &nflash->mtd;
937 + /* Release resources, unregister device */
944 +static struct platform_driver bcm47xx_driver = {
945 + .remove = __devexit_p(bcm47xx_remove),
947 + .name = "bcm47xx_nflash",
948 + .owner = THIS_MODULE,
952 +static int __init init_bcm47xx_nflash(void)
954 + int ret = platform_driver_probe(&bcm47xx_driver, bcm47xx_probe);
957 + pr_err("error registering platform driver: %i\n", ret);
961 +static void __exit exit_bcm47xx_nflash(void)
963 + platform_driver_unregister(&bcm47xx_driver);
966 +module_init(init_bcm47xx_nflash);
967 +module_exit(exit_bcm47xx_nflash);
969 +MODULE_LICENSE("GPL");
970 +MODULE_DESCRIPTION("BCM47XX NAND flash driver");
971 --- a/include/linux/bcma/bcma_driver_chipcommon.h
972 +++ b/include/linux/bcma/bcma_driver_chipcommon.h
973 @@ -376,6 +376,7 @@ struct bcma_chipcommon_pmu {
974 enum bcma_flash_type {
981 @@ -392,6 +393,14 @@ struct bcma_sflash {
983 #endif /* CONFIG_BCMA_SFLASH */
985 +#ifdef CONFIG_BCMA_NFLASH
986 +struct bcma_nflash {
987 + u32 blocksize; /* Block size */
988 + u32 pagesize; /* Page size */
989 + u32 size; /* Total size in bytes */
993 struct bcma_serial_port {
995 unsigned long clockspeed;
996 @@ -417,6 +426,9 @@ struct bcma_drv_cc {
997 #ifdef CONFIG_BCMA_SFLASH
998 struct bcma_sflash sflash;
999 #endif /* CONFIG_BCMA_SFLASH */
1000 +#ifdef CONFIG_BCMA_NFLASH
1001 + struct bcma_nflash nflash;
1005 int nr_serial_ports;
1006 @@ -481,4 +493,13 @@ int bcma_sflash_write(struct bcma_drv_cc
1007 int bcma_sflash_erase(struct bcma_drv_cc *cc, u32 offset);
1008 #endif /* CONFIG_BCMA_SFLASH */
1010 +#ifdef CONFIG_BCMA_NFLASH
1011 +/* Chipcommon nflash support. */
1012 +int bcma_nflash_read(struct bcma_drv_cc *cc, u32 offset, u32 len, u8 *buf);
1013 +int bcma_nflash_poll(struct bcma_drv_cc *cc);
1014 +int bcma_nflash_write(struct bcma_drv_cc *cc, u32 offset, u32 len, const u8 *buf);
1015 +int bcma_nflash_erase(struct bcma_drv_cc *cc, u32 offset);
1016 +int bcma_nflash_commit(struct bcma_drv_cc *cc, u32 offset, u32 len, const u8 *buf);
1019 #endif /* LINUX_BCMA_DRIVER_CC_H_ */
1021 +++ b/include/linux/mtd/bcm47xx_nand.h
1024 + * Broadcom chipcommon NAND flash interface
1026 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
1027 + * Copyright (C) 2009, Broadcom Corporation
1028 + * All Rights Reserved.
1030 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
1031 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
1032 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
1033 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
1040 +#define NAND_FLASH1 0x1fc00000 /* MIPS Flash Region 1 */
1042 +/* nand_cmd_start commands */
1043 +#define NCMD_NULL 0
1044 +#define NCMD_PAGE_RD 1
1045 +#define NCMD_SPARE_RD 2
1046 +#define NCMD_STATUS_RD 3
1047 +#define NCMD_PAGE_PROG 4
1048 +#define NCMD_SPARE_PROG 5
1049 +#define NCMD_COPY_BACK 6
1050 +#define NCMD_ID_RD 7
1051 +#define NCMD_BLOCK_ERASE 8
1052 +#define NCMD_FLASH_RESET 9
1053 +#define NCMD_LOCK 0xa
1054 +#define NCMD_LOCK_DOWN 0xb
1055 +#define NCMD_UNLOCK 0xc
1056 +#define NCMD_LOCK_STATUS 0xd
1058 +/* nand_acc_control */
1059 +#define NAC_RD_ECC_EN 0x80000000
1060 +#define NAC_WR_ECC_EN 0x40000000
1061 +#define NAC_RD_ECC_BLK0_EN 0x20000000
1062 +#define NAC_FAST_PGM_RDIN 0x10000000
1063 +#define NAC_RD_ERASED_ECC_EN 0x08000000
1064 +#define NAC_PARTIAL_PAGE_EN 0x04000000
1065 +#define NAC_PAGE_HIT_EN 0x01000000
1066 +#define NAC_ECC_LEVEL0 0x00f00000
1067 +#define NAC_ECC_LEVEL 0x000f0000
1068 +#define NAC_SPARE_SIZE0 0x00003f00
1069 +#define NAC_SPARE_SIZE 0x0000003f
1072 +#define NCF_CONFIG_LOCK 0x80000000
1073 +#define NCF_BLOCK_SIZE_MASK 0x70000000
1074 +#define NCF_BLOCK_SIZE_SHIFT 28
1075 +#define NCF_DEVICE_SIZE_MASK 0x0f000000
1076 +#define NCF_DEVICE_SIZE_SHIFT 24
1077 +#define NCF_DEVICE_WIDTH 0x00800000
1078 +#define NCF_PAGE_SIZE_MASK 0x00300000
1079 +#define NCF_PAGE_SIZE_SHIFT 20
1080 +#define NCF_FULL_ADDR_BYTES_MASK 0x00070000
1081 +#define NCF_FULL_ADDR_BYTES_SHIFT 16
1082 +#define NCF_COL_ADDR_BYTES_MASK 0x00007000
1083 +#define NCF_COL_ADDR_BYTES_SHIFT 12
1084 +#define NCF_BLK_ADDR_BYTES_MASK 0x00000700
1085 +#define NCF_BLK_ADDR_BYTES_SHIFT 8
1087 +/* nand_intfc_status */
1088 +#define NIST_CTRL_READY 0x80000000
1089 +#define NIST_FLASH_READY 0x40000000
1090 +#define NIST_CACHE_VALID 0x20000000
1091 +#define NIST_SPARE_VALID 0x10000000
1092 +#define NIST_ERASED 0x08000000
1093 +#define NIST_STATUS 0x000000ff
1095 +#define NFL_SECTOR_SIZE 512
1097 +#define NFL_TABLE_END 0xffffffff
1098 +#define NFL_BOOT_SIZE 0x200000
1099 +#define NFL_BOOT_OS_SIZE 0x2000000
1101 +/* Nand flash MLC controller registers (corerev >= 38) */
1102 +#define NAND_REVISION 0xC00
1103 +#define NAND_CMD_START 0xC04
1104 +#define NAND_CMD_ADDR_X 0xC08
1105 +#define NAND_CMD_ADDR 0xC0C
1106 +#define NAND_CMD_END_ADDR 0xC10
1107 +#define NAND_CS_NAND_SELECT 0xC14
1108 +#define NAND_CS_NAND_XOR 0xC18
1109 +#define NAND_SPARE_RD0 0xC20
1110 +#define NAND_SPARE_RD4 0xC24
1111 +#define NAND_SPARE_RD8 0xC28
1112 +#define NAND_SPARE_RD12 0xC2C
1113 +#define NAND_SPARE_WR0 0xC30
1114 +#define NAND_SPARE_WR4 0xC34
1115 +#define NAND_SPARE_WR8 0xC38
1116 +#define NAND_SPARE_WR12 0xC3C
1117 +#define NAND_ACC_CONTROL 0xC40
1118 +#define NAND_CONFIG 0xC48
1119 +#define NAND_TIMING_1 0xC50
1120 +#define NAND_TIMING_2 0xC54
1121 +#define NAND_SEMAPHORE 0xC58
1122 +#define NAND_DEVID 0xC60
1123 +#define NAND_DEVID_X 0xC64
1124 +#define NAND_BLOCK_LOCK_STATUS 0xC68
1125 +#define NAND_INTFC_STATUS 0xC6C
1126 +#define NAND_ECC_CORR_ADDR_X 0xC70
1127 +#define NAND_ECC_CORR_ADDR 0xC74
1128 +#define NAND_ECC_UNC_ADDR_X 0xC78
1129 +#define NAND_ECC_UNC_ADDR 0xC7C
1130 +#define NAND_READ_ERROR_COUNT 0xC80
1131 +#define NAND_CORR_STAT_THRESHOLD 0xC84
1132 +#define NAND_READ_ADDR_X 0xC90
1133 +#define NAND_READ_ADDR 0xC94
1134 +#define NAND_PAGE_PROGRAM_ADDR_X 0xC98
1135 +#define NAND_PAGE_PROGRAM_ADDR 0xC9C
1136 +#define NAND_COPY_BACK_ADDR_X 0xCA0
1137 +#define NAND_COPY_BACK_ADDR 0xCA4
1138 +#define NAND_BLOCK_ERASE_ADDR_X 0xCA8
1139 +#define NAND_BLOCK_ERASE_ADDR 0xCAC
1140 +#define NAND_INV_READ_ADDR_X 0xCB0
1141 +#define NAND_INV_READ_ADDR 0xCB4
1142 +#define NAND_BLK_WR_PROTECT 0xCC0
1143 +#define NAND_ACC_CONTROL_CS1 0xCD0
1144 +#define NAND_CONFIG_CS1 0xCD4
1145 +#define NAND_TIMING_1_CS1 0xCD8
1146 +#define NAND_TIMING_2_CS1 0xCDC
1147 +#define NAND_SPARE_RD16 0xD30
1148 +#define NAND_SPARE_RD20 0xD34
1149 +#define NAND_SPARE_RD24 0xD38
1150 +#define NAND_SPARE_RD28 0xD3C
1151 +#define NAND_CACHE_ADDR 0xD40
1152 +#define NAND_CACHE_DATA 0xD44
1153 +#define NAND_CTRL_CONFIG 0xD48
1154 +#define NAND_CTRL_STATUS 0xD4C
1156 +#endif /* _nflash_h_ */