1 From 2e2951220bf63e05449c03a95453680da1029e44 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 08/15] mtd: bcm47xx: add serial flash driver
6 sflash get the sflash ops from platform device
8 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
10 arch/mips/include/asm/mach-bcm47xx/bus.h | 3 +
11 drivers/mtd/maps/Kconfig | 9 +
12 drivers/mtd/maps/Makefile | 1 +
13 drivers/mtd/maps/bcm47xx-sflash.c | 252 ++++++++++++++++++++++++++++++
14 4 files changed, 265 insertions(+), 0 deletions(-)
15 create mode 100644 drivers/mtd/maps/bcm47xx-sflash.c
17 --- a/arch/mips/include/asm/mach-bcm47xx/bus.h
18 +++ b/arch/mips/include/asm/mach-bcm47xx/bus.h
21 #include <linux/ssb/ssb.h>
22 #include <linux/bcma/bcma.h>
23 +#include <linux/mtd/mtd.h>
26 struct bcm47xx_sflash {
27 @@ -29,6 +30,8 @@ struct bcm47xx_sflash {
28 u32 blocksize; /* Block size */
29 u32 numblocks; /* Number of blocks */
30 u32 size; /* Total size in bytes */
32 + struct mtd_info *mtd;
35 void bcm47xx_sflash_struct_bcma_init(struct bcm47xx_sflash *sflash, struct bcma_drv_cc *bcc);
36 --- a/drivers/mtd/maps/Kconfig
37 +++ b/drivers/mtd/maps/Kconfig
38 @@ -266,6 +266,15 @@ config MTD_BCM47XX_PFLASH
40 Support for bcm47xx parallel flash
42 +config MTD_BCM47XX_SFLASH
43 + tristate "bcm47xx serial flash support"
46 + select MTD_PARTITIONS
47 + select MTD_BCM47XX_PARTS
49 + Support for bcm47xx parallel flash
52 tristate "CFI Flash device mapped on DIL/Net PC"
53 depends on X86 && MTD_CFI_INTELEXT && BROKEN
54 --- a/drivers/mtd/maps/Makefile
55 +++ b/drivers/mtd/maps/Makefile
56 @@ -59,3 +59,4 @@ obj-$(CONFIG_MTD_BCM963XX) += bcm963xx-f
57 obj-$(CONFIG_MTD_LATCH_ADDR) += latch-addr-flash.o
58 obj-$(CONFIG_MTD_LANTIQ) += lantiq-flash.o
59 obj-$(CONFIG_MTD_BCM47XX_PFLASH)+= bcm47xx-pflash.o
60 +obj-$(CONFIG_MTD_BCM47XX_SFLASH)+= bcm47xx-sflash.o
62 +++ b/drivers/mtd/maps/bcm47xx-sflash.c
65 + * Broadcom SiliconBackplane chipcommon serial flash interface
67 + * Copyright 2006, Broadcom Corporation
68 + * All Rights Reserved.
70 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
71 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
72 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
73 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
78 +#define pr_fmt(fmt) "bcm47xx_sflash: " fmt
79 +#include <linux/module.h>
80 +#include <linux/slab.h>
81 +#include <linux/ioport.h>
82 +#include <linux/sched.h>
83 +#include <linux/mtd/mtd.h>
84 +#include <linux/mtd/map.h>
85 +#include <linux/mtd/partitions.h>
86 +#include <linux/errno.h>
87 +#include <linux/delay.h>
88 +#include <linux/platform_device.h>
93 +sflash_mtd_poll(struct bcm47xx_sflash *sflash, unsigned int offset, int timeout)
95 + unsigned long now = jiffies;
99 + if (!sflash->poll(sflash, offset)) {
103 + if (time_after(jiffies, now + timeout)) {
104 + pr_err("timeout while polling\n");
115 +sflash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
117 + struct bcm47xx_sflash *sflash = (struct bcm47xx_sflash *) mtd->priv;
119 + /* Check address range */
123 + if ((from + len) > mtd->size)
129 + int ret = sflash->read(sflash, from, len, buf);
133 + from += (loff_t) ret;
143 +sflash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
145 + struct bcm47xx_sflash *sflash = (struct bcm47xx_sflash *) mtd->priv;
147 + /* Check address range */
151 + if ((to + len) > mtd->size)
157 + int ret = sflash->write(sflash, to, len, buf);
163 + ret = sflash_mtd_poll(sflash, (unsigned int) to, HZ / 10);
167 + to += (loff_t) bytes;
177 +sflash_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
179 + struct bcm47xx_sflash *sflash = (struct bcm47xx_sflash *) mtd->priv;
181 + unsigned int addr, len;
183 + /* Check address range */
186 + if ((erase->addr + erase->len) > mtd->size)
189 + addr = erase->addr;
192 + /* Ensure that requested regions are aligned */
193 + for (i = 0; i < mtd->numeraseregions; i++) {
194 + for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
195 + if (addr == mtd->eraseregions[i].offset +
196 + mtd->eraseregions[i].erasesize * j &&
197 + len >= mtd->eraseregions[i].erasesize) {
198 + ret = sflash->erase(sflash, addr);
201 + ret = sflash_mtd_poll(sflash, addr, 10 * HZ);
204 + addr += mtd->eraseregions[i].erasesize;
205 + len -= mtd->eraseregions[i].erasesize;
212 + /* Set erase status */
214 + erase->state = MTD_ERASE_FAILED;
216 + erase->state = MTD_ERASE_DONE;
218 + /* Call erase callback */
219 + if (erase->callback)
220 + erase->callback(erase);
225 +static const char *probes[] = { "bcm47xx", NULL };
227 +static int bcm47xx_sflash_probe(struct platform_device *pdev)
229 + struct bcm47xx_sflash *sflash = dev_get_platdata(&pdev->dev);
230 + struct mtd_info *mtd;
231 + struct mtd_erase_region_info *regions;
234 + mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
238 + regions = kzalloc(sizeof(struct mtd_erase_region_info), GFP_KERNEL);
242 + pr_info("found serial flash: blocksize=%dKB, numblocks=%d, size=%dKB\n",
243 + sflash->blocksize/1024, sflash->numblocks, sflash->size / 1024);
245 + /* Setup region info */
246 + regions->offset = 0;
247 + regions->erasesize = sflash->blocksize;
248 + regions->numblocks = sflash->numblocks;
249 + if (regions->erasesize > mtd->erasesize)
250 + mtd->erasesize = regions->erasesize;
251 + mtd->size = sflash->size;
252 + mtd->numeraseregions = 1;
254 + /* Register with MTD */
255 + mtd->name = "bcm47xx-sflash";
256 + mtd->type = MTD_NORFLASH;
257 + mtd->flags = MTD_CAP_NORFLASH;
258 + mtd->eraseregions = regions;
259 + mtd->erase = sflash_mtd_erase;
260 + mtd->read = sflash_mtd_read;
261 + mtd->write = sflash_mtd_write;
262 + mtd->writesize = 1;
263 + mtd->priv = sflash;
264 + mtd->owner = THIS_MODULE;
266 + ret = mtd_device_parse_register(mtd, probes, NULL, NULL, 0);
269 + pr_err("mtd_device_register failed\n");
276 +static int __devexit bcm47xx_sflash_remove(struct platform_device *pdev)
278 + struct bcm47xx_sflash *sflash = dev_get_platdata(&pdev->dev);
281 + mtd_device_unregister(sflash->mtd);
282 + map_destroy(sflash->mtd);
283 + kfree(sflash->mtd->eraseregions);
284 + kfree(sflash->mtd);
289 +static struct platform_driver bcm47xx_sflash_driver = {
290 + .remove = __devexit_p(bcm47xx_sflash_remove),
292 + .name = "bcm47xx_sflash",
293 + .owner = THIS_MODULE,
297 +static int __init init_bcm47xx_sflash(void)
299 + int ret = platform_driver_probe(&bcm47xx_sflash_driver, bcm47xx_sflash_probe);
302 + pr_err("error registering platform driver: %i\n", ret);
306 +static void __exit exit_bcm47xx_sflash(void)
308 + platform_driver_unregister(&bcm47xx_sflash_driver);
311 +module_init(init_bcm47xx_sflash);
312 +module_exit(exit_bcm47xx_sflash);
314 +MODULE_LICENSE("GPL");
315 +MODULE_DESCRIPTION("BCM47XX parallel flash driver");