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