1 From 6a615274708fff685952139fee00ebccc0cf3266 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 19/26] 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 | 267 ++++++++++++++++++++++++++++++
14 4 files changed, 280 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 @@ -273,6 +273,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 @@ -61,3 +61,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 + struct mtd_partition *parts;
235 + int num_partitions = 0;
237 + mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
241 + regions = kzalloc(sizeof(struct mtd_erase_region_info), GFP_KERNEL);
245 + pr_info("found serial flash: blocksize=%dKB, numblocks=%d, size=%dKB\n",
246 + sflash->blocksize/1024, sflash->numblocks, sflash->size / 1024);
248 + /* Setup region info */
249 + regions->offset = 0;
250 + regions->erasesize = sflash->blocksize;
251 + regions->numblocks = sflash->numblocks;
252 + if (regions->erasesize > mtd->erasesize)
253 + mtd->erasesize = regions->erasesize;
254 + mtd->size = sflash->size;
255 + mtd->numeraseregions = 1;
257 + /* Register with MTD */
258 + mtd->name = "bcm47xx-sflash";
259 + mtd->type = MTD_NORFLASH;
260 + mtd->flags = MTD_CAP_NORFLASH;
261 + mtd->eraseregions = regions;
262 + mtd->erase = sflash_mtd_erase;
263 + mtd->read = sflash_mtd_read;
264 + mtd->write = sflash_mtd_write;
265 + mtd->writesize = 1;
266 + mtd->priv = sflash;
267 + mtd->owner = THIS_MODULE;
269 + num_partitions = parse_mtd_partitions(mtd, probes, &parts, 0);
270 + if (num_partitions < 0) {
271 + ret = num_partitions;
275 + ret = mtd_device_register(mtd, parts, num_partitions);
277 +// ret = mtd_device_parse_register(bcm47xx_mtd, "bcm47xx", NULL, NULL, 0);
280 + pr_err("mtd_device_register failed\n");
291 +static int __devexit bcm47xx_sflash_remove(struct platform_device *pdev)
293 + struct bcm47xx_sflash *sflash = dev_get_platdata(&pdev->dev);
296 + mtd_device_unregister(sflash->mtd);
297 + map_destroy(sflash->mtd);
298 + kfree(sflash->mtd->eraseregions);
299 + kfree(sflash->mtd);
304 +static struct platform_driver bcm47xx_sflash_driver = {
305 + .remove = __devexit_p(bcm47xx_sflash_remove),
307 + .name = "bcm47xx_sflash",
308 + .owner = THIS_MODULE,
312 +static int __init init_bcm47xx_sflash(void)
314 + int ret = platform_driver_probe(&bcm47xx_sflash_driver, bcm47xx_sflash_probe);
317 + pr_err("error registering platform driver: %i\n", ret);
321 +static void __exit exit_bcm47xx_sflash(void)
323 + platform_driver_unregister(&bcm47xx_sflash_driver);
326 +module_init(init_bcm47xx_sflash);
327 +module_exit(exit_bcm47xx_sflash);
329 +MODULE_LICENSE("GPL");
330 +MODULE_DESCRIPTION("BCM47XX parallel flash driver");