1 --- a/arch/mips/bcm47xx/Kconfig
2 +++ b/arch/mips/bcm47xx/Kconfig
3 @@ -25,6 +25,7 @@ config BCM47XX_BCMA
4 select BCMA_HOST_PCI if PCI
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 @@ -46,6 +47,12 @@ void bcm47xx_sflash_struct_bcma_init(str
22 sflash->numblocks = bcc->sflash.numblocks;
23 sflash->size = bcc->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;
33 #ifdef CONFIG_BCM47XX_SSB
34 --- a/arch/mips/bcm47xx/nvram.c
35 +++ b/arch/mips/bcm47xx/nvram.c
37 * Copyright (C) 2005 Broadcom Corporation
38 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
39 * Copyright (C) 2010-2011 Hauke Mehrtens <hauke@hauke-m.de>
40 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
42 * This program is free software; you can redistribute it and/or modify it
43 * under the terms of the GNU General Public License as published by the
45 #include <asm/mach-bcm47xx/nvram.h>
46 #include <asm/mach-bcm47xx/bcm47xx.h>
47 #include <asm/mach-bcm47xx/bus.h>
48 +#include <linux/mtd/bcm47xx_nand.h>
50 static char nvram_buf[NVRAM_SPACE];
52 @@ -160,6 +162,51 @@ static void early_nvram_init_ssb(void)
55 #ifdef CONFIG_BCM47XX_BCMA
56 +static int early_nvram_init_nflash(void)
58 + struct nvram_header *header;
62 + u32 flash_size = bcm47xx_nflash.size;
63 + u8 tmpbuf[NFL_SECTOR_SIZE];
67 + /* check if the struct is already initilized */
74 + while (off <= flash_size) {
75 + ret = bcma_nflash_read(bcm47xx_nflash.bcc, off, NFL_SECTOR_SIZE, tmpbuf);
76 + if (ret != NFL_SECTOR_SIZE)
78 + header = (struct nvram_header *)tmpbuf;
79 + if (header->magic == NVRAM_HEADER)
89 + header = (struct nvram_header *) KSEG1ADDR(NAND_FLASH1 + off);
90 + src = (u32 *) header;
91 + dst = (u32 *) nvram_buf;
92 + for (i = 0; i < sizeof(struct nvram_header); i += 4)
94 + for (; i < len && i < NVRAM_SPACE; i += 4)
101 static void early_nvram_init_bcma(void)
104 @@ -173,6 +220,11 @@ static void early_nvram_init_bcma(void)
106 printk(KERN_WARNING "can not read from flash: %i\n", err);
109 + err = early_nvram_init_nflash();
111 + printk(KERN_WARNING "can not read from nflash: %i\n", err);
114 printk(KERN_WARNING "unknow flash type\n");
116 --- a/arch/mips/bcm47xx/setup.c
117 +++ b/arch/mips/bcm47xx/setup.c
119 * Copyright (C) 2006 Michael Buesch <m@bues.ch>
120 * Copyright (C) 2010 Waldemar Brodkorb <wbx@openadk.org>
121 * Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
122 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
124 * This program is free software; you can redistribute it and/or modify it
125 * under the terms of the GNU General Public License as published by the
126 @@ -234,6 +235,21 @@ static int bcm47xx_get_sprom_bcma(struct
130 +struct bcm47xx_nflash bcm47xx_nflash;
132 +static struct resource bcm47xx_nflash_resource = {
133 + .name = "bcm47xx_nflash",
139 +static struct platform_device bcm47xx_nflash_dev = {
140 + .name = "bcm47xx_nflash",
141 + .resource = &bcm47xx_nflash_resource,
142 + .num_resources = 1,
145 static void __init bcm47xx_register_bcma(void)
148 @@ -248,6 +264,9 @@ static void __init bcm47xx_register_bcma
150 if (bcm47xx_bus.bcma.bus.drv_cc.flash_type == BCMA_SFLASH)
151 bcm47xx_sflash_struct_bcma_init(&bcm47xx_sflash, &bcm47xx_bus.bcma.bus.drv_cc);
153 + if (bcm47xx_bus.bcma.bus.drv_cc.flash_type == BCMA_NFLASH)
154 + bcm47xx_nflash_struct_bcma_init(&bcm47xx_nflash, &bcm47xx_bus.bcma.bus.drv_cc);
156 bcm47xx_fill_bcma_boardinfo(&bcm47xx_bus.bcma.bus.boardinfo, NULL);
158 @@ -264,6 +283,9 @@ static int __init bcm47xx_register_flash
160 bcm47xx_sflash_dev.dev.platform_data = &bcm47xx_sflash;
161 return platform_device_register(&bcm47xx_sflash_dev);
163 + bcm47xx_nflash_dev.dev.platform_data = &bcm47xx_nflash;
164 + return platform_device_register(&bcm47xx_nflash_dev);
166 printk(KERN_ERR "No flash device found\n");
168 --- a/arch/mips/include/asm/mach-bcm47xx/bus.h
169 +++ b/arch/mips/include/asm/mach-bcm47xx/bus.h
171 * BCM947xx nvram variable access
173 * Copyright (C) 2011 Hauke Mehrtens <hauke@hauke-m.de>
174 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
176 * This program is free software; you can redistribute it and/or modify it
177 * under the terms of the GNU General Public License as published by the
179 #include <linux/ssb/ssb.h>
180 #include <linux/bcma/bcma.h>
182 +#include <linux/mtd/nand.h>
184 struct bcm47xx_sflash {
185 enum bcm47xx_bus_type sflash_type;
186 @@ -34,3 +36,18 @@ void bcm47xx_sflash_struct_bcma_init(str
187 void bcm47xx_sflash_struct_ssb_init(struct bcm47xx_sflash *sflash, struct ssb_chipcommon *scc);
189 extern struct bcm47xx_sflash bcm47xx_sflash;
191 +struct bcm47xx_nflash {
192 + enum bcm47xx_bus_type nflash_type;
193 + struct bcma_drv_cc *bcc;
195 + u32 size; /* Total size in bytes */
196 + u32 next_opcode; /* Next expected command from upper NAND layer */
198 + struct mtd_info mtd;
199 + struct nand_chip nand;
202 +void bcm47xx_nflash_struct_bcma_init(struct bcm47xx_nflash *nflash, struct bcma_drv_cc *bcc);
204 +extern struct bcm47xx_nflash bcm47xx_nflash;
205 --- a/drivers/bcma/Kconfig
206 +++ b/drivers/bcma/Kconfig
207 @@ -43,6 +43,11 @@ config BCMA_SFLASH
208 depends on BCMA_DRIVER_MIPS
213 + depends on BCMA_DRIVER_MIPS
216 config BCMA_DRIVER_MIPS
217 bool "BCMA Broadcom MIPS core driver"
218 depends on BCMA && MIPS
219 --- a/drivers/bcma/Makefile
220 +++ b/drivers/bcma/Makefile
222 bcma-y += main.o scan.o core.o sprom.o
223 bcma-y += driver_chipcommon.o driver_chipcommon_pmu.o
224 bcma-$(CONFIG_BCMA_SFLASH) += driver_chipcommon_sflash.o
225 +bcma-$(CONFIG_BCMA_NFLASH) += driver_chipcommon_nflash.o
226 bcma-y += driver_pci.o
227 bcma-$(CONFIG_BCMA_DRIVER_PCI_HOSTMODE) += driver_pci_host.o
228 bcma-$(CONFIG_BCMA_DRIVER_MIPS) += driver_mips.o
229 --- a/drivers/bcma/bcma_private.h
230 +++ b/drivers/bcma/bcma_private.h
231 @@ -56,6 +56,11 @@ u32 bcma_pmu_get_clockcpu(struct bcma_dr
232 int bcma_sflash_init(struct bcma_drv_cc *cc);
233 #endif /* CONFIG_BCMA_SFLASH */
235 +#ifdef CONFIG_BCMA_NFLASH
236 +/* driver_chipcommon_nflash.c */
237 +int bcma_nflash_init(struct bcma_drv_cc *cc);
238 +#endif /* CONFIG_BCMA_NFLASH */
240 #ifdef CONFIG_BCMA_HOST_PCI
242 extern int __init bcma_host_pci_init(void);
244 +++ b/drivers/bcma/driver_chipcommon_nflash.c
247 + * BCMA nand flash interface
249 + * Copyright 2011, Tathagata Das <tathagata@alumnux.com>
250 + * Copyright 2010, Broadcom Corporation
252 + * Licensed under the GNU/GPL. See COPYING for details.
255 +#include <linux/bcma/bcma.h>
256 +#include <linux/bcma/bcma_driver_chipcommon.h>
257 +#include <linux/delay.h>
258 +#include <linux/mtd/bcm47xx_nand.h>
259 +#include <linux/mtd/nand.h>
261 +#include "bcma_private.h"
263 +/* Issue a nand flash command */
264 +static inline void bcma_nflash_cmd(struct bcma_drv_cc *cc, u32 opcode)
266 + bcma_cc_write32(cc, NAND_CMD_START, opcode);
267 + bcma_cc_read32(cc, NAND_CMD_START);
270 +/* Check offset and length */
271 +static int bcma_nflash_offset_is_valid(struct bcma_drv_cc *cc, u32 offset, u32 len, u32 mask)
273 + if ((offset & mask) != 0 || (len & mask) != 0) {
274 + pr_err("%s(): Address is not aligned. offset: %x, len: %x, mask: %x\n", __func__, offset, len, mask);
278 + if ((((offset + len) >> 20) >= cc->nflash.size) &&
279 + (((offset + len) & ((1 << 20) - 1)) != 0)) {
280 + pr_err("%s(): Address is outside Flash memory region. offset: %x, len: %x, mask: %x\n", __func__, offset, len, mask);
287 +/* Read len bytes starting at offset into buf. Returns number of bytes read. */
288 +int bcma_nflash_read(struct bcma_drv_cc *cc, u32 offset, u32 len, u8 *buf)
294 + mask = NFL_SECTOR_SIZE - 1;
295 + if (bcma_nflash_offset_is_valid(cc, offset, len, mask))
301 + bcma_cc_write32(cc, NAND_CMD_ADDR, offset);
302 + bcma_nflash_cmd(cc, NCMD_PAGE_RD);
303 + if (bcma_nflash_poll(cc) < 0)
305 + val = bcma_cc_read32(cc, NAND_INTFC_STATUS);
306 + if ((val & NIST_CACHE_VALID) == 0)
308 + bcma_cc_write32(cc, NAND_CACHE_ADDR, 0);
309 + for (i = 0; i < NFL_SECTOR_SIZE; i += 4, to++) {
310 + *to = bcma_cc_read32(cc, NAND_CACHE_DATA);
312 + res -= NFL_SECTOR_SIZE;
313 + offset += NFL_SECTOR_SIZE;
315 + return (len - res);
318 +#define NF_RETRIES 1000000
320 +/* Poll for command completion. Returns zero when complete. */
321 +int bcma_nflash_poll(struct bcma_drv_cc *cc)
323 + u32 retries = NF_RETRIES;
324 + u32 pollmask = NIST_CTRL_READY|NIST_FLASH_READY;
327 + while (retries--) {
328 + mask = bcma_cc_read32(cc, NAND_INTFC_STATUS) & pollmask;
329 + if (mask == pollmask)
335 + pr_err("bcma_nflash_poll: not ready\n");
342 +/* Write len bytes starting at offset into buf. Returns success (0) or failure (!0).
343 + * Should poll for completion.
345 +int bcma_nflash_write(struct bcma_drv_cc *cc, u32 offset, u32 len,
350 + u32 *from, res, reg;
352 + mask = cc->nflash.pagesize - 1;
353 + if (bcma_nflash_offset_is_valid(cc, offset, len, mask))
356 + /* disable partial page enable */
357 + reg = bcma_cc_read32(cc, NAND_ACC_CONTROL);
358 + reg &= ~NAC_PARTIAL_PAGE_EN;
359 + bcma_cc_write32(cc, NAND_ACC_CONTROL, reg);
364 + bcma_cc_write32(cc, NAND_CACHE_ADDR, 0);
365 + for (i = 0; i < cc->nflash.pagesize; i += 4, from++) {
367 + bcma_cc_write32(cc, NAND_CMD_ADDR, i);
368 + bcma_cc_write32(cc, NAND_CACHE_DATA, *from);
370 + bcma_cc_write32(cc, NAND_CMD_ADDR, offset + cc->nflash.pagesize - 512);
371 + bcma_nflash_cmd(cc, NCMD_PAGE_PROG);
372 + if (bcma_nflash_poll(cc) < 0)
374 + res -= cc->nflash.pagesize;
375 + offset += cc->nflash.pagesize;
381 + return (len - res);
384 +/* Erase a region. Returns success (0) or failure (-1).
385 + * Poll for completion.
387 +int bcma_nflash_erase(struct bcma_drv_cc *cc, u32 offset)
389 + if ((offset >> 20) >= cc->nflash.size)
391 + if ((offset & (cc->nflash.blocksize - 1)) != 0)
394 + bcma_cc_write32(cc, NAND_CMD_ADDR, offset);
395 + bcma_nflash_cmd(cc, NCMD_BLOCK_ERASE);
396 + if (bcma_nflash_poll(cc) < 0)
400 --- a/drivers/bcma/driver_mips.c
401 +++ b/drivers/bcma/driver_mips.c
403 * Copyright 2006, 2007, Michael Buesch <mb@bu3sch.de>
404 * Copyright 2010, Bernhard Loos <bernhardloos@googlemail.com>
405 * Copyright 2011, Hauke Mehrtens <hauke@hauke-m.de>
406 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
408 * Licensed under the GNU/GPL. See COPYING for details.
410 @@ -182,6 +183,17 @@ static void bcma_core_mips_flash_detect(
412 struct bcma_bus *bus = mcore->core->bus;
414 + if (bus->drv_cc.core->id.rev == 38
415 + && (bus->drv_cc.status & (1 << 4)) != 0) {
416 +#ifdef CONFIG_BCMA_NFLASH
417 + pr_info("found nand flash.\n");
418 + bus->drv_cc.flash_type = BCMA_NFLASH;
420 + pr_info("NAND flash not supported.\n");
425 switch (bus->drv_cc.capabilities & BCMA_CC_CAP_FLASHT) {
426 case BCMA_CC_FLASHT_STSER:
427 case BCMA_CC_FLASHT_ATSER:
428 --- a/drivers/mtd/nand/Kconfig
429 +++ b/drivers/mtd/nand/Kconfig
430 @@ -536,4 +536,12 @@ config MTD_NAND_FSMC
431 Enables support for NAND Flash chips on the ST Microelectronics
432 Flexible Static Memory Controller (FSMC)
434 +config MTD_NAND_BCM47XX
435 + tristate "bcm47xx nand flash support"
437 + depends on BCM47XX && BCMA_NFLASH
438 + select MTD_PARTITIONS
440 + Support for bcm47xx nand flash
443 --- a/drivers/mtd/nand/Makefile
444 +++ b/drivers/mtd/nand/Makefile
445 @@ -49,5 +49,6 @@ obj-$(CONFIG_MTD_NAND_MPC5121_NFC) += mp
446 obj-$(CONFIG_MTD_NAND_RICOH) += r852.o
447 obj-$(CONFIG_MTD_NAND_JZ4740) += jz4740_nand.o
448 obj-$(CONFIG_MTD_NAND_GPMI_NAND) += gpmi-nand/
449 +obj-$(CONFIG_MTD_NAND_BCM47XX) += bcm47xx_nand.o
451 nand-objs := nand_base.o nand_bbt.o
453 +++ b/drivers/mtd/nand/bcm47xx_nand.c
456 + * BCMA nand flash interface
458 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
459 + * Copyright 2010, Broadcom Corporation
461 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
462 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
463 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
464 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
468 +#define pr_fmt(fmt) "bcm47xx_nflash: " fmt
469 +#include <linux/module.h>
470 +#include <linux/slab.h>
471 +#include <linux/ioport.h>
472 +#include <linux/sched.h>
473 +#include <linux/mtd/mtd.h>
474 +#include <linux/mtd/map.h>
475 +#include <linux/mtd/partitions.h>
476 +#include <linux/errno.h>
477 +#include <linux/delay.h>
478 +#include <linux/platform_device.h>
479 +#include <bcm47xx.h>
481 +#include <linux/cramfs_fs.h>
482 +#include <linux/romfs_fs.h>
483 +#include <linux/magic.h>
484 +#include <linux/byteorder/generic.h>
485 +#include <linux/mtd/bcm47xx_nand.h>
486 +#include <linux/mtd/nand.h>
488 +static int bcm47xx_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip);
489 +static int bcm47xx_erase(struct mtd_info *mtd, unsigned int addr, unsigned int len);
491 +/* Private Global variable */
492 +static u32 read_offset = 0;
493 +static u32 write_offset;
496 +nflash_mtd_poll(struct bcm47xx_nflash *nflash, unsigned int offset, int timeout)
498 + unsigned long now = jiffies;
502 + if (!bcma_nflash_poll(nflash->bcc)) {
506 + if (time_after(jiffies, now + timeout)) {
507 + pr_err("timeout while polling\n");
518 +bcm47xx_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
520 + struct nand_chip *nchip = (struct nand_chip *)mtd->priv;
521 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
522 + int bytes, ret = 0;
526 + u32 offset, blocksize, mask, off;
527 + u32 skip_bytes = 0;
531 + /* Check address range */
534 + if ((from + len) > mtd->size)
537 + if ((offset & (NFL_SECTOR_SIZE - 1)) != 0) {
538 + extra = offset & (NFL_SECTOR_SIZE - 1);
543 + size = (len + (NFL_SECTOR_SIZE - 1)) & ~(NFL_SECTOR_SIZE - 1);
550 + tmpbuf = (u8 *)kmalloc(size, GFP_KERNEL);
554 + blocksize = mtd->erasesize;
555 + mask = blocksize - 1;
558 + off = offset + skip_bytes;
559 + if ((bytes = bcma_nflash_read(nflash->bcc, off, NFL_SECTOR_SIZE, ptr)) < 0) {
574 + memcpy(buf, tmpbuf+extra, *retlen);
581 +static void bcm47xx_write(struct mtd_info *mtd, u32 to, const u_char *buf, u32 len)
583 + struct nand_chip *nchip = (struct nand_chip *)mtd->priv;
584 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
585 + u32 offset, blocksize, mask, off;
587 + u32 copy_len, write_len, from;
588 + u_char *write_ptr, *block;
592 + /* Check address range */
594 + pr_err("Error: Attempted to write too small data\n");
601 + if ((to + len) > mtd->size) {
602 + pr_err("Error: Attempted to write too large data\n");
609 + blocksize = mtd->erasesize;
610 + if (!(block = kmalloc(blocksize, GFP_KERNEL)))
612 + mask = blocksize - 1;
615 + from = offset & ~mask;
616 + /* Copy existing data into holding block if necessary */
617 + if (((offset & (blocksize-1)) != 0) || (len < blocksize)) {
618 + if ((ret = bcm47xx_read(mtd, from, blocksize, &read_len, block)))
620 + if (read_len != blocksize) {
626 + /* Copy input data into holding block */
627 + copy_len = min(len, blocksize - (offset & mask));
628 + memcpy(block + (offset & mask), ptr, copy_len);
631 + if ((ret = bcm47xx_erase(mtd, off, blocksize)) < 0)
633 + /* Write holding block */
635 + write_len = blocksize;
636 + if ((bytes = bcma_nflash_write(nflash->bcc, (uint)from, (uint)write_len, (u8 *) write_ptr)) != 0) {
640 + offset += copy_len;
641 + if (len < copy_len)
654 +static int bcm47xx_erase(struct mtd_info *mtd, unsigned int addr, unsigned int len)
656 + struct nand_chip *nchip = (struct nand_chip *)mtd->priv;
657 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
659 + /* Check address range */
662 + if ((addr + len) > mtd->size)
665 + if (bcma_nflash_erase(nflash->bcc, addr)) {
666 + pr_err("ERASE: nflash erase error\n");
670 + if (nflash_mtd_poll(nflash, addr, 10 * HZ)) {
671 + pr_err("ERASE: nflash_mtd_poll error\n");
678 +/* This functions is used by upper layer to checks if device is ready */
679 +static int bcm47xx_dev_ready(struct mtd_info *mtd)
684 +/* Issue a nand flash command */
685 +static inline void bcm47xx_nflash_cmd(struct bcma_drv_cc *cc, u32 opcode)
687 + bcma_cc_write32(cc, NAND_CMD_START, opcode);
688 + bcma_cc_read32(cc, NAND_CMD_START);
691 +static void bcm47xx_command(struct mtd_info *mtd, unsigned command,
692 + int column, int page_addr)
694 + struct nand_chip *nchip = (struct nand_chip *)mtd->priv;
695 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
696 + u32 pagesize = 1 << nchip->page_shift;
698 + /* Command pre-processing step */
700 + case NAND_CMD_RESET:
701 + bcm47xx_nflash_cmd(nflash->bcc, NCMD_FLASH_RESET);
704 + case NAND_CMD_STATUS:
705 + nflash->next_opcode = NAND_CMD_STATUS;
710 + case NAND_CMD_READ0:
711 + read_offset = page_addr * pagesize;
712 + nflash->next_opcode = 0;
715 + case NAND_CMD_READOOB:
716 + read_offset = page_addr * pagesize;
717 + nflash->next_opcode = 0;
720 + case NAND_CMD_SEQIN:
721 + write_offset = page_addr * pagesize;
722 + nflash->next_opcode = 0;
725 + case NAND_CMD_PAGEPROG:
726 + nflash->next_opcode = 0;
729 + case NAND_CMD_READID:
730 + read_offset = column;
731 + bcm47xx_nflash_cmd(nflash->bcc, NCMD_ID_RD);
732 + nflash->next_opcode = NAND_DEVID;
735 + case NAND_CMD_ERASE1:
736 + nflash->next_opcode = 0;
737 + bcm47xx_erase(mtd, page_addr*pagesize, pagesize);
740 + case NAND_CMD_ERASE2:
743 + case NAND_CMD_RNDOUT:
744 + if (column > mtd->writesize)
745 + read_offset += (column - mtd->writesize);
747 + read_offset += column;
751 + pr_err("COMMAND not supported %x\n", command);
752 + nflash->next_opcode = 0;
757 +/* This function is used by upper layer for select and
758 + * deselect of the NAND chip.
759 + * It is dummy function. */
760 +static void bcm47xx_select_chip(struct mtd_info *mtd, int chip)
764 +static u_char bcm47xx_read_byte(struct mtd_info *mtd)
766 + struct nand_chip *nchip = mtd->priv;
767 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
771 + if (nflash->next_opcode == 0)
774 + if (nflash->next_opcode == NAND_CMD_STATUS)
775 + return NAND_STATUS_WP;
777 + id = bcma_cc_read32(nflash->bcc, nflash->next_opcode);
779 + if (nflash->next_opcode == NAND_DEVID) {
780 + ret = (id >> (8*read_offset)) & 0xff;
787 +static uint16_t bcm47xx_read_word(struct mtd_info *mtd)
789 + loff_t from = read_offset;
793 + bcm47xx_read(mtd, from, sizeof(buf), &bytes, (u_char *)&buf);
797 +/* Write data of length len to buffer buf. The data to be
798 + * written on NAND Flash is first copied to RAMbuffer. After the Data Input
799 + * Operation by the NFC, the data is written to NAND Flash */
800 +static void bcm47xx_write_buf(struct mtd_info *mtd,
801 + const u_char *buf, int len)
803 + bcm47xx_write(mtd, write_offset, buf, len);
806 +/* Read the data buffer from the NAND Flash. To read the data from NAND
807 + * Flash first the data output cycle is initiated by the NFC, which copies
808 + * the data to RAMbuffer. This data of length len is then copied to buffer buf.
810 +static void bcm47xx_read_buf(struct mtd_info *mtd, u_char *buf, int len)
812 + loff_t from = read_offset;
815 + bcm47xx_read(mtd, from, len, &bytes, buf);
818 +/* Used by the upper layer to verify the data in NAND Flash
819 + * with the data in the buf. */
820 +static int bcm47xx_verify_buf(struct mtd_info *mtd,
821 + const u_char *buf, int len)
826 +static int bcm47xx_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
828 + struct nand_chip *nchip = mtd->priv;
829 + struct bcm47xx_nflash *nflash = (struct bcm47xx_nflash *)nchip->priv;
832 + u32 pagesize = 1 << nchip->page_shift;
833 + u32 blocksize = mtd->erasesize;
835 + if ((ofs >> 20) >= nflash->size)
837 + if ((ofs & (blocksize - 1)) != 0)
840 + for (i = 0; i < 2; i++) {
841 + off = ofs + pagesize;
842 + bcma_cc_write32(nflash->bcc, NAND_CMD_ADDR, off);
843 + bcm47xx_nflash_cmd(nflash->bcc, NCMD_SPARE_RD);
844 + if (bcma_nflash_poll(nflash->bcc) < 0)
846 + if ((bcma_cc_read32(nflash->bcc, NAND_INTFC_STATUS) & NIST_SPARE_VALID) != NIST_SPARE_VALID)
848 + if ((bcma_cc_read32(nflash->bcc, NAND_SPARE_RD0) & 0xff) != 0xff)
854 +const char *part_probes[] = { "cmdlinepart", NULL };
855 +static int bcm47xx_probe(struct platform_device *pdev)
857 + struct nand_chip *nchip;
858 + struct mtd_info *mtd;
859 + struct bcm47xx_nflash *nflash = dev_get_platdata(&pdev->dev);
862 + mtd = &nflash->mtd;
863 + nchip = &nflash->nand;
865 + /* Register with MTD */
867 + mtd->owner = THIS_MODULE;
868 + mtd->dev.parent = &pdev->dev;
870 + /* 50 us command delay time */
871 + nchip->chip_delay = 50;
873 + nchip->priv = nflash;
874 + nchip->dev_ready = bcm47xx_dev_ready;
875 + nchip->cmdfunc = bcm47xx_command;
876 + nchip->select_chip = bcm47xx_select_chip;
877 + nchip->read_byte = bcm47xx_read_byte;
878 + nchip->read_word = bcm47xx_read_word;
879 + nchip->write_buf = bcm47xx_write_buf;
880 + nchip->read_buf = bcm47xx_read_buf;
881 + nchip->verify_buf = bcm47xx_verify_buf;
882 + nchip->block_bad = bcm47xx_block_bad;
883 + nchip->options = NAND_SKIP_BBTSCAN;
886 + nchip->ecc.mode = NAND_ECC_NONE;
888 + /* first scan to find the device and get the page size */
889 + if (nand_scan_ident(mtd, 1, NULL)) {
890 + pr_err("nand_scan_ident failed\n");
894 + nflash->bcc->nflash.size = mtd->size;
895 + nflash->bcc->nflash.pagesize = 1 << nchip->page_shift;
896 + nflash->bcc->nflash.blocksize = mtd->erasesize;
897 + bcm47xx_nflash.size = mtd->size;
899 + /* second phase scan */
900 + if (nand_scan_tail(mtd)) {
901 + pr_err("nand_scan_tail failed\n");
906 + mtd->name = "bcm47xx-nflash";
907 + mtd->flags |= MTD_WRITEABLE;
908 + ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
911 + pr_err("mtd_device_register failed\n");
921 +static int __devexit bcm47xx_remove(struct platform_device *pdev)
923 + struct bcm47xx_nflash *nflash = dev_get_platdata(&pdev->dev);
924 + struct mtd_info *mtd = &nflash->mtd;
927 + /* Release resources, unregister device */
934 +static struct platform_driver bcm47xx_driver = {
935 + .remove = __devexit_p(bcm47xx_remove),
937 + .name = "bcm47xx_nflash",
938 + .owner = THIS_MODULE,
942 +static int __init init_bcm47xx_nflash(void)
944 + int ret = platform_driver_probe(&bcm47xx_driver, bcm47xx_probe);
947 + pr_err("error registering platform driver: %i\n", ret);
951 +static void __exit exit_bcm47xx_nflash(void)
953 + platform_driver_unregister(&bcm47xx_driver);
956 +module_init(init_bcm47xx_nflash);
957 +module_exit(exit_bcm47xx_nflash);
959 +MODULE_LICENSE("GPL");
960 +MODULE_DESCRIPTION("BCM47XX NAND flash driver");
961 --- a/include/linux/bcma/bcma_driver_chipcommon.h
962 +++ b/include/linux/bcma/bcma_driver_chipcommon.h
963 @@ -436,6 +436,7 @@ struct bcma_chipcommon_pmu {
964 enum bcma_flash_type {
971 @@ -452,6 +453,14 @@ struct bcma_sflash {
973 #endif /* CONFIG_BCMA_SFLASH */
975 +#ifdef CONFIG_BCMA_NFLASH
976 +struct bcma_nflash {
977 + u32 blocksize; /* Block size */
978 + u32 pagesize; /* Page size */
979 + u32 size; /* Total size in bytes */
983 struct bcma_serial_port {
985 unsigned long clockspeed;
986 @@ -477,6 +486,9 @@ struct bcma_drv_cc {
987 #ifdef CONFIG_BCMA_SFLASH
988 struct bcma_sflash sflash;
989 #endif /* CONFIG_BCMA_SFLASH */
990 +#ifdef CONFIG_BCMA_NFLASH
991 + struct bcma_nflash nflash;
996 @@ -542,4 +554,13 @@ int bcma_sflash_write(struct bcma_drv_cc
997 int bcma_sflash_erase(struct bcma_drv_cc *cc, u32 offset);
998 #endif /* CONFIG_BCMA_SFLASH */
1000 +#ifdef CONFIG_BCMA_NFLASH
1001 +/* Chipcommon nflash support. */
1002 +int bcma_nflash_read(struct bcma_drv_cc *cc, u32 offset, u32 len, u8 *buf);
1003 +int bcma_nflash_poll(struct bcma_drv_cc *cc);
1004 +int bcma_nflash_write(struct bcma_drv_cc *cc, u32 offset, u32 len, const u8 *buf);
1005 +int bcma_nflash_erase(struct bcma_drv_cc *cc, u32 offset);
1006 +int bcma_nflash_commit(struct bcma_drv_cc *cc, u32 offset, u32 len, const u8 *buf);
1009 #endif /* LINUX_BCMA_DRIVER_CC_H_ */
1011 +++ b/include/linux/mtd/bcm47xx_nand.h
1014 + * Broadcom chipcommon NAND flash interface
1016 + * Copyright (C) 2011-2012 Tathagata Das <tathagata@alumnux.com>
1017 + * Copyright (C) 2009, Broadcom Corporation
1018 + * All Rights Reserved.
1020 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
1021 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
1022 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
1023 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
1030 +#define NAND_FLASH1 0x1fc00000 /* MIPS Flash Region 1 */
1032 +/* nand_cmd_start commands */
1033 +#define NCMD_NULL 0
1034 +#define NCMD_PAGE_RD 1
1035 +#define NCMD_SPARE_RD 2
1036 +#define NCMD_STATUS_RD 3
1037 +#define NCMD_PAGE_PROG 4
1038 +#define NCMD_SPARE_PROG 5
1039 +#define NCMD_COPY_BACK 6
1040 +#define NCMD_ID_RD 7
1041 +#define NCMD_BLOCK_ERASE 8
1042 +#define NCMD_FLASH_RESET 9
1043 +#define NCMD_LOCK 0xa
1044 +#define NCMD_LOCK_DOWN 0xb
1045 +#define NCMD_UNLOCK 0xc
1046 +#define NCMD_LOCK_STATUS 0xd
1048 +/* nand_acc_control */
1049 +#define NAC_RD_ECC_EN 0x80000000
1050 +#define NAC_WR_ECC_EN 0x40000000
1051 +#define NAC_RD_ECC_BLK0_EN 0x20000000
1052 +#define NAC_FAST_PGM_RDIN 0x10000000
1053 +#define NAC_RD_ERASED_ECC_EN 0x08000000
1054 +#define NAC_PARTIAL_PAGE_EN 0x04000000
1055 +#define NAC_PAGE_HIT_EN 0x01000000
1056 +#define NAC_ECC_LEVEL0 0x00f00000
1057 +#define NAC_ECC_LEVEL 0x000f0000
1058 +#define NAC_SPARE_SIZE0 0x00003f00
1059 +#define NAC_SPARE_SIZE 0x0000003f
1062 +#define NCF_CONFIG_LOCK 0x80000000
1063 +#define NCF_BLOCK_SIZE_MASK 0x70000000
1064 +#define NCF_BLOCK_SIZE_SHIFT 28
1065 +#define NCF_DEVICE_SIZE_MASK 0x0f000000
1066 +#define NCF_DEVICE_SIZE_SHIFT 24
1067 +#define NCF_DEVICE_WIDTH 0x00800000
1068 +#define NCF_PAGE_SIZE_MASK 0x00300000
1069 +#define NCF_PAGE_SIZE_SHIFT 20
1070 +#define NCF_FULL_ADDR_BYTES_MASK 0x00070000
1071 +#define NCF_FULL_ADDR_BYTES_SHIFT 16
1072 +#define NCF_COL_ADDR_BYTES_MASK 0x00007000
1073 +#define NCF_COL_ADDR_BYTES_SHIFT 12
1074 +#define NCF_BLK_ADDR_BYTES_MASK 0x00000700
1075 +#define NCF_BLK_ADDR_BYTES_SHIFT 8
1077 +/* nand_intfc_status */
1078 +#define NIST_CTRL_READY 0x80000000
1079 +#define NIST_FLASH_READY 0x40000000
1080 +#define NIST_CACHE_VALID 0x20000000
1081 +#define NIST_SPARE_VALID 0x10000000
1082 +#define NIST_ERASED 0x08000000
1083 +#define NIST_STATUS 0x000000ff
1085 +#define NFL_SECTOR_SIZE 512
1087 +#define NFL_TABLE_END 0xffffffff
1088 +#define NFL_BOOT_SIZE 0x200000
1089 +#define NFL_BOOT_OS_SIZE 0x2000000
1091 +/* Nand flash MLC controller registers (corerev >= 38) */
1092 +#define NAND_REVISION 0xC00
1093 +#define NAND_CMD_START 0xC04
1094 +#define NAND_CMD_ADDR_X 0xC08
1095 +#define NAND_CMD_ADDR 0xC0C
1096 +#define NAND_CMD_END_ADDR 0xC10
1097 +#define NAND_CS_NAND_SELECT 0xC14
1098 +#define NAND_CS_NAND_XOR 0xC18
1099 +#define NAND_SPARE_RD0 0xC20
1100 +#define NAND_SPARE_RD4 0xC24
1101 +#define NAND_SPARE_RD8 0xC28
1102 +#define NAND_SPARE_RD12 0xC2C
1103 +#define NAND_SPARE_WR0 0xC30
1104 +#define NAND_SPARE_WR4 0xC34
1105 +#define NAND_SPARE_WR8 0xC38
1106 +#define NAND_SPARE_WR12 0xC3C
1107 +#define NAND_ACC_CONTROL 0xC40
1108 +#define NAND_CONFIG 0xC48
1109 +#define NAND_TIMING_1 0xC50
1110 +#define NAND_TIMING_2 0xC54
1111 +#define NAND_SEMAPHORE 0xC58
1112 +#define NAND_DEVID 0xC60
1113 +#define NAND_DEVID_X 0xC64
1114 +#define NAND_BLOCK_LOCK_STATUS 0xC68
1115 +#define NAND_INTFC_STATUS 0xC6C
1116 +#define NAND_ECC_CORR_ADDR_X 0xC70
1117 +#define NAND_ECC_CORR_ADDR 0xC74
1118 +#define NAND_ECC_UNC_ADDR_X 0xC78
1119 +#define NAND_ECC_UNC_ADDR 0xC7C
1120 +#define NAND_READ_ERROR_COUNT 0xC80
1121 +#define NAND_CORR_STAT_THRESHOLD 0xC84
1122 +#define NAND_READ_ADDR_X 0xC90
1123 +#define NAND_READ_ADDR 0xC94
1124 +#define NAND_PAGE_PROGRAM_ADDR_X 0xC98
1125 +#define NAND_PAGE_PROGRAM_ADDR 0xC9C
1126 +#define NAND_COPY_BACK_ADDR_X 0xCA0
1127 +#define NAND_COPY_BACK_ADDR 0xCA4
1128 +#define NAND_BLOCK_ERASE_ADDR_X 0xCA8
1129 +#define NAND_BLOCK_ERASE_ADDR 0xCAC
1130 +#define NAND_INV_READ_ADDR_X 0xCB0
1131 +#define NAND_INV_READ_ADDR 0xCB4
1132 +#define NAND_BLK_WR_PROTECT 0xCC0
1133 +#define NAND_ACC_CONTROL_CS1 0xCD0
1134 +#define NAND_CONFIG_CS1 0xCD4
1135 +#define NAND_TIMING_1_CS1 0xCD8
1136 +#define NAND_TIMING_2_CS1 0xCDC
1137 +#define NAND_SPARE_RD16 0xD30
1138 +#define NAND_SPARE_RD20 0xD34
1139 +#define NAND_SPARE_RD24 0xD38
1140 +#define NAND_SPARE_RD28 0xD3C
1141 +#define NAND_CACHE_ADDR 0xD40
1142 +#define NAND_CACHE_DATA 0xD44
1143 +#define NAND_CTRL_CONFIG 0xD48
1144 +#define NAND_CTRL_STATUS 0xD4C
1146 +#endif /* _nflash_h_ */