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