brcm47xx: add support for kernel 3.2
[openwrt/svn-archive/archive.git] / target / linux / brcm47xx / patches-3.2 / 0019-mtd-bcm47xx-add-serial-flash-driver.patch
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
5
6 sflash get the sflash ops from platform device
7
8 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
9 ---
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
16
17 --- a/arch/mips/include/asm/mach-bcm47xx/bus.h
18 +++ b/arch/mips/include/asm/mach-bcm47xx/bus.h
19 @@ -11,6 +11,7 @@
20
21 #include <linux/ssb/ssb.h>
22 #include <linux/bcma/bcma.h>
23 +#include <linux/mtd/mtd.h>
24 #include <bcm47xx.h>
25
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 */
31 +
32 + struct mtd_info *mtd;
33 };
34
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
39 help
40 Support for bcm47xx parallel flash
41
42 +config MTD_BCM47XX_SFLASH
43 + tristate "bcm47xx serial flash support"
44 + default y
45 + depends on BCM47XX
46 + select MTD_PARTITIONS
47 + select MTD_BCM47XX_PARTS
48 + help
49 + Support for bcm47xx parallel flash
50 +
51 config MTD_DILNETPC
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
61 --- /dev/null
62 +++ b/drivers/mtd/maps/bcm47xx-sflash.c
63 @@ -0,0 +1,252 @@
64 +/*
65 + * Broadcom SiliconBackplane chipcommon serial flash interface
66 + *
67 + * Copyright 2006, Broadcom Corporation
68 + * All Rights Reserved.
69 + *
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.
74 + *
75 + * $Id$
76 + */
77 +
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>
89 +#include <bcm47xx.h>
90 +#include <bus.h>
91 +
92 +static int
93 +sflash_mtd_poll(struct bcm47xx_sflash *sflash, unsigned int offset, int timeout)
94 +{
95 + unsigned long now = jiffies;
96 + int ret = 0;
97 +
98 + for (;;) {
99 + if (!sflash->poll(sflash, offset)) {
100 + ret = 0;
101 + break;
102 + }
103 + if (time_after(jiffies, now + timeout)) {
104 + pr_err("timeout while polling\n");
105 + ret = -ETIMEDOUT;
106 + break;
107 + }
108 + udelay(1);
109 + }
110 +
111 + return ret;
112 +}
113 +
114 +static int
115 +sflash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
116 +{
117 + struct bcm47xx_sflash *sflash = (struct bcm47xx_sflash *) mtd->priv;
118 +
119 + /* Check address range */
120 + if (!len)
121 + return 0;
122 +
123 + if ((from + len) > mtd->size)
124 + return -EINVAL;
125 +
126 + *retlen = 0;
127 +
128 + while (len) {
129 + int ret = sflash->read(sflash, from, len, buf);
130 + if (ret < 0)
131 + return ret;
132 +
133 + from += (loff_t) ret;
134 + len -= ret;
135 + buf += ret;
136 + *retlen += ret;
137 + }
138 +
139 + return 0;
140 +}
141 +
142 +static int
143 +sflash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
144 +{
145 + struct bcm47xx_sflash *sflash = (struct bcm47xx_sflash *) mtd->priv;
146 +
147 + /* Check address range */
148 + if (!len)
149 + return 0;
150 +
151 + if ((to + len) > mtd->size)
152 + return -EINVAL;
153 +
154 + *retlen = 0;
155 + while (len) {
156 + int bytes;
157 + int ret = sflash->write(sflash, to, len, buf);
158 + if (ret < 0)
159 + return ret;
160 +
161 + bytes = ret;
162 +
163 + ret = sflash_mtd_poll(sflash, (unsigned int) to, HZ / 10);
164 + if (ret)
165 + return ret;
166 +
167 + to += (loff_t) bytes;
168 + len -= bytes;
169 + buf += bytes;
170 + *retlen += bytes;
171 + }
172 +
173 + return 0;
174 +}
175 +
176 +static int
177 +sflash_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
178 +{
179 + struct bcm47xx_sflash *sflash = (struct bcm47xx_sflash *) mtd->priv;
180 + int i, j, ret = 0;
181 + unsigned int addr, len;
182 +
183 + /* Check address range */
184 + if (!erase->len)
185 + return 0;
186 + if ((erase->addr + erase->len) > mtd->size)
187 + return -EINVAL;
188 +
189 + addr = erase->addr;
190 + len = erase->len;
191 +
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);
199 + if (ret < 0)
200 + break;
201 + ret = sflash_mtd_poll(sflash, addr, 10 * HZ);
202 + if (ret)
203 + break;
204 + addr += mtd->eraseregions[i].erasesize;
205 + len -= mtd->eraseregions[i].erasesize;
206 + }
207 + }
208 + if (ret)
209 + break;
210 + }
211 +
212 + /* Set erase status */
213 + if (ret)
214 + erase->state = MTD_ERASE_FAILED;
215 + else
216 + erase->state = MTD_ERASE_DONE;
217 +
218 + /* Call erase callback */
219 + if (erase->callback)
220 + erase->callback(erase);
221 +
222 + return ret;
223 +}
224 +
225 +static const char *probes[] = { "bcm47xx", NULL };
226 +
227 +static int bcm47xx_sflash_probe(struct platform_device *pdev)
228 +{
229 + struct bcm47xx_sflash *sflash = dev_get_platdata(&pdev->dev);
230 + struct mtd_info *mtd;
231 + struct mtd_erase_region_info *regions;
232 + int ret = 0;
233 +
234 + mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
235 + if (!mtd)
236 + return -ENOMEM;
237 +
238 + regions = kzalloc(sizeof(struct mtd_erase_region_info), GFP_KERNEL);
239 + if (!mtd)
240 + return -ENOMEM;
241 +
242 + pr_info("found serial flash: blocksize=%dKB, numblocks=%d, size=%dKB\n",
243 + sflash->blocksize/1024, sflash->numblocks, sflash->size / 1024);
244 +
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;
253 +
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;
265 +
266 + ret = mtd_device_parse_register(mtd, probes, NULL, NULL, 0);
267 +
268 + if (ret) {
269 + pr_err("mtd_device_register failed\n");
270 + return ret;
271 + }
272 + sflash->mtd = mtd;
273 + return 0;
274 +}
275 +
276 +static int __devexit bcm47xx_sflash_remove(struct platform_device *pdev)
277 +{
278 + struct bcm47xx_sflash *sflash = dev_get_platdata(&pdev->dev);
279 +
280 + if (sflash) {
281 + mtd_device_unregister(sflash->mtd);
282 + map_destroy(sflash->mtd);
283 + kfree(sflash->mtd->eraseregions);
284 + kfree(sflash->mtd);
285 + }
286 + return 0;
287 +}
288 +
289 +static struct platform_driver bcm47xx_sflash_driver = {
290 + .remove = __devexit_p(bcm47xx_sflash_remove),
291 + .driver = {
292 + .name = "bcm47xx_sflash",
293 + .owner = THIS_MODULE,
294 + },
295 +};
296 +
297 +static int __init init_bcm47xx_sflash(void)
298 +{
299 + int ret = platform_driver_probe(&bcm47xx_sflash_driver, bcm47xx_sflash_probe);
300 +
301 + if (ret)
302 + pr_err("error registering platform driver: %i\n", ret);
303 + return ret;
304 +}
305 +
306 +static void __exit exit_bcm47xx_sflash(void)
307 +{
308 + platform_driver_unregister(&bcm47xx_sflash_driver);
309 +}
310 +
311 +module_init(init_bcm47xx_sflash);
312 +module_exit(exit_bcm47xx_sflash);
313 +
314 +MODULE_LICENSE("GPL");
315 +MODULE_DESCRIPTION("BCM47XX parallel flash driver");