ar71xx: support for D-LINK DIR-615 rev. E4
[openwrt.git] / target / linux / brcm47xx / patches-3.0 / 0019-mtd-bcm47xx-add-serial-flash-driver.patch
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
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 | 267 ++++++++++++++++++++++++++++++
14 4 files changed, 280 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 @@ -273,6 +273,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 @@ -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
61 --- /dev/null
62 +++ b/drivers/mtd/maps/bcm47xx-sflash.c
63 @@ -0,0 +1,267 @@
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 + struct mtd_partition *parts;
235 + int num_partitions = 0;
236 +
237 + mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
238 + if (!mtd)
239 + return -ENOMEM;
240 +
241 + regions = kzalloc(sizeof(struct mtd_erase_region_info), GFP_KERNEL);
242 + if (!mtd)
243 + return -ENOMEM;
244 +
245 + pr_info("found serial flash: blocksize=%dKB, numblocks=%d, size=%dKB\n",
246 + sflash->blocksize/1024, sflash->numblocks, sflash->size / 1024);
247 +
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;
256 +
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;
268 +
269 + num_partitions = parse_mtd_partitions(mtd, probes, &parts, 0);
270 + if (num_partitions < 0) {
271 + ret = num_partitions;
272 + goto err_destroy;
273 + }
274 +
275 + ret = mtd_device_register(mtd, parts, num_partitions);
276 +
277 +// ret = mtd_device_parse_register(bcm47xx_mtd, "bcm47xx", NULL, NULL, 0);
278 +
279 + if (ret) {
280 + pr_err("mtd_device_register failed\n");
281 + return ret;
282 + }
283 + sflash->mtd = mtd;
284 + return 0;
285 +
286 +err_destroy:
287 + map_destroy(mtd);
288 + return ret;
289 +}
290 +
291 +static int __devexit bcm47xx_sflash_remove(struct platform_device *pdev)
292 +{
293 + struct bcm47xx_sflash *sflash = dev_get_platdata(&pdev->dev);
294 +
295 + if (sflash) {
296 + mtd_device_unregister(sflash->mtd);
297 + map_destroy(sflash->mtd);
298 + kfree(sflash->mtd->eraseregions);
299 + kfree(sflash->mtd);
300 + }
301 + return 0;
302 +}
303 +
304 +static struct platform_driver bcm47xx_sflash_driver = {
305 + .remove = __devexit_p(bcm47xx_sflash_remove),
306 + .driver = {
307 + .name = "bcm47xx_sflash",
308 + .owner = THIS_MODULE,
309 + },
310 +};
311 +
312 +static int __init init_bcm47xx_sflash(void)
313 +{
314 + int ret = platform_driver_probe(&bcm47xx_sflash_driver, bcm47xx_sflash_probe);
315 +
316 + if (ret)
317 + pr_err("error registering platform driver: %i\n", ret);
318 + return ret;
319 +}
320 +
321 +static void __exit exit_bcm47xx_sflash(void)
322 +{
323 + platform_driver_unregister(&bcm47xx_sflash_driver);
324 +}
325 +
326 +module_init(init_bcm47xx_sflash);
327 +module_exit(exit_bcm47xx_sflash);
328 +
329 +MODULE_LICENSE("GPL");
330 +MODULE_DESCRIPTION("BCM47XX parallel flash driver");
This page took 0.053774 seconds and 5 git commands to generate.