brcm47xx: add support for kernel 3.6
[openwrt/svn-archive/archive.git] / target / linux / brcm47xx / patches-3.6 / 052-mtd-add-serial-flash-driver.patch
1 --- a/drivers/mtd/maps/Kconfig
2 +++ b/drivers/mtd/maps/Kconfig
3 @@ -258,6 +258,15 @@ config MTD_BCM47XX_PFLASH
4 help
5 Support for bcm47xx parallel flash
6
7 +config MTD_BCM47XX_SFLASH
8 + tristate "bcm47xx serial flash support"
9 + default y
10 + depends on BCM47XX
11 + select MTD_PARTITIONS
12 + select MTD_BCM47XX_PARTS
13 + help
14 + Support for bcm47xx parallel flash
15 +
16 config MTD_DILNETPC
17 tristate "CFI Flash device mapped on DIL/Net PC"
18 depends on X86 && MTD_CFI_INTELEXT && BROKEN
19 --- a/drivers/mtd/maps/Makefile
20 +++ b/drivers/mtd/maps/Makefile
21 @@ -58,3 +58,4 @@ obj-$(CONFIG_MTD_GPIO_ADDR) += gpio-addr
22 obj-$(CONFIG_MTD_LATCH_ADDR) += latch-addr-flash.o
23 obj-$(CONFIG_MTD_LANTIQ) += lantiq-flash.o
24 obj-$(CONFIG_MTD_BCM47XX_PFLASH)+= bcm47xx-pflash.o
25 +obj-$(CONFIG_MTD_BCM47XX_SFLASH)+= bcm47xx-sflash.o
26 --- /dev/null
27 +++ b/drivers/mtd/maps/bcm47xx-sflash.c
28 @@ -0,0 +1,270 @@
29 +/*
30 + * Broadcom SiliconBackplane chipcommon serial flash interface
31 + *
32 + * Copyright 2011, 2012, Hauke Mehrtens <hauke@hauke-m.de>
33 + * Copyright 2006, Broadcom Corporation
34 + * All Rights Reserved.
35 + *
36 + * Licensed under the GNU/GPL. See COPYING for details.
37 + */
38 +
39 +#define pr_fmt(fmt) "bcm47xx_sflash: " fmt
40 +#include <linux/module.h>
41 +#include <linux/slab.h>
42 +#include <linux/ioport.h>
43 +#include <linux/sched.h>
44 +#include <linux/mtd/mtd.h>
45 +#include <linux/mtd/map.h>
46 +#include <linux/mtd/partitions.h>
47 +#include <linux/errno.h>
48 +#include <linux/delay.h>
49 +#include <linux/platform_device.h>
50 +#include <linux/mtd/bcm47xx_sflash.h>
51 +
52 +static int
53 +sflash_mtd_poll(struct bcm47xx_sflash *sflash, unsigned int offset, int timeout)
54 +{
55 + unsigned long now = jiffies;
56 +
57 + for (;;) {
58 + if (!sflash->poll(sflash, offset)) {
59 + break;
60 + }
61 + if (time_after(jiffies, now + timeout)) {
62 + pr_err("timeout while polling\n");
63 + return -ETIMEDOUT;
64 +
65 + }
66 + cpu_relax();
67 + udelay(1);
68 + }
69 +
70 + return 0;
71 +}
72 +
73 +static int
74 +sflash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
75 +{
76 + struct bcm47xx_sflash *sflash = (struct bcm47xx_sflash *)mtd->priv;
77 +
78 + /* Check address range */
79 + if (!len)
80 + return 0;
81 +
82 + if ((from + len) > mtd->size)
83 + return -EINVAL;
84 +
85 + *retlen = 0;
86 + while (len) {
87 + int ret = sflash->read(sflash, from, len, buf);
88 + if (ret < 0)
89 + return ret;
90 +
91 + from += (loff_t) ret;
92 + len -= ret;
93 + buf += ret;
94 + *retlen += ret;
95 + }
96 +
97 + return 0;
98 +}
99 +
100 +static int
101 +sflash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
102 +{
103 + int bytes;
104 + int ret;
105 + struct bcm47xx_sflash *sflash = (struct bcm47xx_sflash *)mtd->priv;
106 +
107 + /* Check address range */
108 + if (!len)
109 + return 0;
110 +
111 + if ((to + len) > mtd->size)
112 + return -EINVAL;
113 +
114 + *retlen = 0;
115 + while (len) {
116 + ret = sflash->write(sflash, to, len, buf);
117 + if (ret < 0)
118 + return ret;
119 +
120 + bytes = ret;
121 +
122 + ret = sflash_mtd_poll(sflash, (unsigned int) to, HZ / 10);
123 + if (ret)
124 + return ret;
125 +
126 + to += (loff_t) bytes;
127 + len -= bytes;
128 + buf += bytes;
129 + *retlen += bytes;
130 + }
131 +
132 + return 0;
133 +}
134 +
135 +static int
136 +sflash_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
137 +{
138 + struct bcm47xx_sflash *sflash = (struct bcm47xx_sflash *) mtd->priv;
139 + int i, j, ret = 0;
140 + unsigned int addr, len;
141 +
142 + /* Check address range */
143 + if (!erase->len)
144 + return 0;
145 + if ((erase->addr + erase->len) > mtd->size)
146 + return -EINVAL;
147 +
148 + addr = erase->addr;
149 + len = erase->len;
150 +
151 + /* Ensure that requested regions are aligned */
152 + for (i = 0; i < mtd->numeraseregions; i++) {
153 + for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
154 + if (addr == mtd->eraseregions[i].offset +
155 + mtd->eraseregions[i].erasesize * j &&
156 + len >= mtd->eraseregions[i].erasesize) {
157 + ret = sflash->erase(sflash, addr);
158 + if (ret < 0)
159 + break;
160 + ret = sflash_mtd_poll(sflash, addr, 10 * HZ);
161 + if (ret)
162 + break;
163 + addr += mtd->eraseregions[i].erasesize;
164 + len -= mtd->eraseregions[i].erasesize;
165 + }
166 + }
167 + if (ret)
168 + break;
169 + }
170 +
171 + /* Set erase status */
172 + if (ret)
173 + erase->state = MTD_ERASE_FAILED;
174 + else
175 + erase->state = MTD_ERASE_DONE;
176 +
177 + /* Call erase callback */
178 + if (erase->callback)
179 + erase->callback(erase);
180 +
181 + return ret;
182 +}
183 +
184 +static const char *probes[] = { "bcm47xx", NULL };
185 +
186 +static int bcm47xx_sflash_probe(struct platform_device *pdev)
187 +{
188 + struct bcm47xx_sflash *sflash = dev_get_platdata(&pdev->dev);
189 + struct mtd_info *mtd;
190 + struct mtd_erase_region_info *eraseregions;
191 + int ret = 0;
192 +
193 + mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
194 + if (!mtd){
195 + ret = -ENOMEM;
196 + goto err_out;
197 + }
198 +
199 + eraseregions = kzalloc(sizeof(struct mtd_erase_region_info), GFP_KERNEL);
200 + if (!eraseregions) {
201 + ret = -ENOMEM;
202 + goto err_free_mtd;
203 + }
204 +
205 + pr_info("found serial flash: blocksize=%dKB, numblocks=%d, size=%dKB\n",
206 + sflash->blocksize / 1024, sflash->numblocks, sflash->size / 1024);
207 +
208 + /* Setup region info */
209 + eraseregions->offset = 0;
210 + eraseregions->erasesize = sflash->blocksize;
211 + eraseregions->numblocks = sflash->numblocks;
212 + if (eraseregions->erasesize > mtd->erasesize)
213 + mtd->erasesize = eraseregions->erasesize;
214 + mtd->size = sflash->size;
215 + mtd->numeraseregions = 1;
216 +
217 + /* Register with MTD */
218 + mtd->name = "bcm47xx-sflash";
219 + mtd->type = MTD_NORFLASH;
220 + mtd->flags = MTD_CAP_NORFLASH;
221 + mtd->eraseregions = eraseregions;
222 + mtd->_erase = sflash_mtd_erase;
223 + mtd->_read = sflash_mtd_read;
224 + mtd->_write = sflash_mtd_write;
225 + mtd->writesize = 1;
226 + mtd->priv = sflash;
227 + ret = dev_set_drvdata(&pdev->dev, mtd);
228 + mtd->owner = THIS_MODULE;
229 + if (ret) {
230 + pr_err("adding private data failed\n");
231 + goto err_free_eraseregions;
232 + }
233 +
234 + ret = mtd_device_parse_register(mtd, probes, NULL, NULL, 0);
235 +
236 + if (ret) {
237 + pr_err("mtd_device_register failed\n");
238 + goto err_free_eraseregions;
239 + }
240 + return 0;
241 +
242 +err_free_eraseregions:
243 + kfree(eraseregions);
244 +err_free_mtd:
245 + kfree(mtd);
246 +err_out:
247 + return ret;
248 +}
249 +
250 +static int __devexit bcm47xx_sflash_remove(struct platform_device *pdev)
251 +{
252 + struct mtd_info *mtd = dev_get_drvdata(&pdev->dev);
253 +
254 + if (mtd) {
255 + mtd_device_unregister(mtd);
256 + map_destroy(mtd);
257 + kfree(mtd->eraseregions);
258 + kfree(mtd);
259 + dev_set_drvdata(&pdev->dev, NULL);
260 + }
261 + return 0;
262 +}
263 +
264 +static const struct platform_device_id bcm47xx_sflash_table[] = {
265 + { "bcm47xx-sflash", 0 },
266 + { }
267 +};
268 +MODULE_DEVICE_TABLE(platform, bcm47xx_sflash_table);
269 +
270 +static struct platform_driver bcm47xx_sflash_driver = {
271 + .id_table = bcm47xx_sflash_table,
272 + .probe = bcm47xx_sflash_probe,
273 + .remove = __devexit_p(bcm47xx_sflash_remove),
274 + .driver = {
275 + .name = "bcm47xx-sflash",
276 + .owner = THIS_MODULE,
277 + },
278 +};
279 +
280 +static int __init init_bcm47xx_sflash(void)
281 +{
282 + int ret = platform_driver_register(&bcm47xx_sflash_driver);
283 +
284 + if (ret)
285 + pr_err("error registering platform driver: %i\n", ret);
286 + return ret;
287 +}
288 +
289 +static void __exit exit_bcm47xx_sflash(void)
290 +{
291 + platform_driver_unregister(&bcm47xx_sflash_driver);
292 +}
293 +
294 +module_init(init_bcm47xx_sflash);
295 +module_exit(exit_bcm47xx_sflash);
296 +
297 +MODULE_LICENSE("GPL");
298 +MODULE_DESCRIPTION("BCM47XX serial flash driver");
299 --- /dev/null
300 +++ b/include/linux/mtd/bcm47xx_sflash.h
301 @@ -0,0 +1,34 @@
302 +#ifndef LINUX_MTD_BCM47XX_SFLASH_H_
303 +#define LINUX_MTD_BCM47XX_SFLASH_H_
304 +
305 +#include <linux/mtd/mtd.h>
306 +
307 +enum bcm47xx_sflash_type {
308 + BCM47XX_SFLASH_SSB,
309 + BCM47XX_SFLASH_BCMA,
310 +};
311 +
312 +struct ssb_chipcommon;
313 +struct bcma_drv_cc;
314 +
315 +struct bcm47xx_sflash {
316 + enum bcm47xx_sflash_type type;
317 + union {
318 + struct ssb_chipcommon *scc;
319 + struct bcma_drv_cc *bcc;
320 + };
321 +
322 + bool present;
323 + u16 numblocks;
324 + u32 window;
325 + u32 blocksize;
326 + u32 size;
327 +
328 + int (*read)(struct bcm47xx_sflash *dev, u32 offset, u32 len, u8 *buf);
329 + int (*poll)(struct bcm47xx_sflash *dev, u32 offset);
330 + int (*write)(struct bcm47xx_sflash *dev, u32 offset, u32 len, const u8 *buf);
331 + int (*erase)(struct bcm47xx_sflash *dev, u32 offset);
332 +
333 + struct mtd_info *mtd;
334 +};
335 +#endif /* LINUX_MTD_BCM47XX_SFLASH_H_ */