1 From 8a6398687998886c451c6df381c2320b6dddb3fe Mon Sep 17 00:00:00 2001
2 From: Hauke Mehrtens <hauke@hauke-m.de>
3 Date: Sun, 17 Jul 2011 14:55:45 +0200
4 Subject: [PATCH 16/22] mtd: bcm47xx: add serial flash driver
7 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
9 drivers/mtd/maps/Kconfig | 9 ++
10 drivers/mtd/maps/Makefile | 1 +
11 drivers/mtd/maps/bcm47xx-sflash.c | 270 +++++++++++++++++++++++++++++++++++++
12 3 files changed, 280 insertions(+), 0 deletions(-)
13 create mode 100644 drivers/mtd/maps/bcm47xx-sflash.c
15 --- a/drivers/mtd/maps/Kconfig
16 +++ b/drivers/mtd/maps/Kconfig
17 @@ -273,6 +273,15 @@ config MTD_BCM47XX_PFLASH
19 Support for bcm47xx parallel flash
21 +config MTD_BCM47XX_SFLASH
22 + tristate "bcm47xx serial flash support"
25 + select MTD_PARTITIONS
26 + select MTD_BCM47XX_PARTS
28 + Support for bcm47xx parallel flash
31 tristate "CFI Flash device mapped on DIL/Net PC"
32 depends on X86 && MTD_CFI_INTELEXT && BROKEN
33 --- a/drivers/mtd/maps/Makefile
34 +++ b/drivers/mtd/maps/Makefile
35 @@ -61,3 +61,4 @@ obj-$(CONFIG_MTD_BCM963XX) += bcm963xx-f
36 obj-$(CONFIG_MTD_LATCH_ADDR) += latch-addr-flash.o
37 obj-$(CONFIG_MTD_LANTIQ) += lantiq-flash.o
38 obj-$(CONFIG_MTD_BCM47XX_PFLASH)+= bcm47xx-pflash.o
39 +obj-$(CONFIG_MTD_BCM47XX_SFLASH)+= bcm47xx-sflash.o
41 +++ b/drivers/mtd/maps/bcm47xx-sflash.c
44 + * Broadcom SiliconBackplane chipcommon serial flash interface
46 + * Copyright 2006, Broadcom Corporation
47 + * All Rights Reserved.
49 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
50 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
51 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
52 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
57 +#define pr_fmt(fmt) "bcm47xx_sflash: " fmt
58 +#include <linux/module.h>
59 +#include <linux/slab.h>
60 +#include <linux/ioport.h>
61 +#include <linux/sched.h>
62 +#include <linux/mtd/mtd.h>
63 +#include <linux/mtd/map.h>
64 +#include <linux/mtd/partitions.h>
65 +#include <linux/errno.h>
66 +#include <linux/delay.h>
70 +#include <linux/bcma/bcma.h>
71 +#include <linux/bcma/bcma_driver_chipcommon.h>
72 +#include <linux/platform_device.h>
75 + struct bcma_drv_cc *cc;
76 + struct mtd_info mtd;
77 + struct mtd_erase_region_info region;
80 +static struct sflash_mtd *sflash;
83 +sflash_mtd_poll(struct sflash_mtd *sflash, unsigned int offset, int timeout)
85 + unsigned long now = jiffies;
89 + if (!bcma_sflash_poll(sflash->cc, offset)) {
93 + if (time_after(jiffies, now + timeout)) {
94 + pr_err("timeout while polling\n");
105 +sflash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
107 + struct sflash_mtd *sflash = (struct sflash_mtd *) mtd->priv;
109 + /* Check address range */
113 + if ((from + len) > mtd->size)
119 + int ret = bcma_sflash_read(sflash->cc, from, len, buf);
123 + from += (loff_t) ret;
133 +sflash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
135 + struct sflash_mtd *sflash = (struct sflash_mtd *) mtd->priv;
137 + /* Check address range */
141 + if ((to + len) > mtd->size)
147 + int ret = bcma_sflash_write(sflash->cc, to, len, buf);
153 + ret = sflash_mtd_poll(sflash, (unsigned int) to, HZ / 10);
157 + to += (loff_t) bytes;
167 +sflash_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
169 + struct sflash_mtd *sflash = (struct sflash_mtd *) mtd->priv;
171 + unsigned int addr, len;
173 + /* Check address range */
176 + if ((erase->addr + erase->len) > mtd->size)
179 + addr = erase->addr;
182 + /* Ensure that requested regions are aligned */
183 + for (i = 0; i < mtd->numeraseregions; i++) {
184 + for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
185 + if (addr == mtd->eraseregions[i].offset +
186 + mtd->eraseregions[i].erasesize * j &&
187 + len >= mtd->eraseregions[i].erasesize) {
188 + if ((ret = bcma_sflash_erase(sflash->cc, addr)) < 0)
190 + if ((ret = sflash_mtd_poll(sflash, addr, 10 * HZ)))
192 + addr += mtd->eraseregions[i].erasesize;
193 + len -= mtd->eraseregions[i].erasesize;
200 + /* Set erase status */
202 + erase->state = MTD_ERASE_FAILED;
204 + erase->state = MTD_ERASE_DONE;
206 + /* Call erase callback */
207 + if (erase->callback)
208 + erase->callback(erase);
213 +static const char *probes[] = { "bcm47xx", NULL };
215 +static int bcm47xx_sflash_probe(struct platform_device *pdev)
219 + struct mtd_partition *parts;
220 + int num_partitions = 0;
222 + sflash = kzalloc(sizeof(struct sflash_mtd), GFP_KERNEL);
226 + sflash->cc = &bcm47xx_bus.bcma.bus.drv_cc;
227 + if (sflash->cc->flash_type != BCMA_SFLASH)
230 + pr_info("found serial flash: blocksize=%dKB, numblocks=%d, size=%dKB\n",
231 + sflash->cc->flash.sflash.blocksize/1024,
232 + sflash->cc->flash.sflash.numblocks,
233 + sflash->cc->flash.sflash.size/1024);
235 + /* Setup region info */
236 + sflash->region.offset = 0;
237 + sflash->region.erasesize = sflash->cc->flash.sflash.blocksize;
238 + sflash->region.numblocks = sflash->cc->flash.sflash.numblocks;
239 + if (sflash->region.erasesize > sflash->mtd.erasesize)
240 + sflash->mtd.erasesize = sflash->region.erasesize;
241 + sflash->mtd.size = sflash->cc->flash.sflash.size;
242 + sflash->mtd.numeraseregions = 1;
244 + /* Register with MTD */
245 + sflash->mtd.name = "bcm47xx-sflash";
246 + sflash->mtd.type = MTD_NORFLASH;
247 + sflash->mtd.flags = MTD_CAP_NORFLASH;
248 + sflash->mtd.eraseregions = &sflash->region;
249 + sflash->mtd.erase = sflash_mtd_erase;
250 + sflash->mtd.read = sflash_mtd_read;
251 + sflash->mtd.write = sflash_mtd_write;
252 + sflash->mtd.writesize = 1;
253 + sflash->mtd.priv = sflash;
254 + sflash->mtd.owner = THIS_MODULE;
256 + num_partitions = parse_mtd_partitions(&sflash->mtd, probes, &parts, 0);
257 + if (num_partitions < 0) {
258 + ret = num_partitions;
262 + ret = mtd_device_register(&sflash->mtd, parts, num_partitions);
264 +// ret = mtd_device_parse_register(bcm47xx_mtd, "bcm47xx", NULL, NULL, 0);
267 + pr_err("mtd_device_register failed\n");
273 + map_destroy(&sflash->mtd);
277 +static int __devexit bcm47xx_sflash_remove(struct platform_device *pdev)
280 + mtd_device_unregister(&sflash->mtd);
281 + map_destroy(&sflash->mtd);
286 +static struct platform_driver bcm47xx_sflash_driver = {
287 + .remove = __devexit_p(bcm47xx_sflash_remove),
289 + .name = "bcm47xx_sflash",
290 + .owner = THIS_MODULE,
294 +static int __init init_bcm47xx_sflash(void)
296 + int ret = platform_driver_probe(&bcm47xx_sflash_driver, bcm47xx_sflash_probe);
299 + pr_err("error registering platform driver: %i\n", ret);
303 +static void __exit exit_bcm47xx_sflash(void)
305 + platform_driver_unregister(&bcm47xx_sflash_driver);
308 +module_init(init_bcm47xx_sflash);
309 +module_exit(exit_bcm47xx_sflash);
311 +MODULE_LICENSE("GPL");
312 +MODULE_DESCRIPTION("BCM47XX parallel flash driver");