Commit | Line | Data |
---|---|---|
5fe42d5b RM |
1 | #include <linux/kernel.h> |
2 | #include <linux/module.h> | |
3 | #include <linux/slab.h> | |
bddcb5e7 | 4 | #include <linux/delay.h> |
5651d6aa | 5 | #include <linux/ioport.h> |
5fe42d5b RM |
6 | #include <linux/mtd/mtd.h> |
7 | #include <linux/platform_device.h> | |
8 | #include <linux/bcma/bcma.h> | |
9 | ||
a2f74a7d RM |
10 | #include "bcm47xxsflash.h" |
11 | ||
5fe42d5b RM |
12 | MODULE_LICENSE("GPL"); |
13 | MODULE_DESCRIPTION("Serial flash driver for BCMA bus"); | |
14 | ||
afffeec9 | 15 | static const char * const probes[] = { "bcm47xxpart", NULL }; |
5fe42d5b | 16 | |
bddcb5e7 RM |
17 | /************************************************** |
18 | * Various helpers | |
19 | **************************************************/ | |
20 | ||
21 | static void bcm47xxsflash_cmd(struct bcm47xxsflash *b47s, u32 opcode) | |
22 | { | |
23 | int i; | |
24 | ||
25 | b47s->cc_write(b47s, BCMA_CC_FLASHCTL, BCMA_CC_FLASHCTL_START | opcode); | |
26 | for (i = 0; i < 1000; i++) { | |
27 | if (!(b47s->cc_read(b47s, BCMA_CC_FLASHCTL) & | |
28 | BCMA_CC_FLASHCTL_BUSY)) | |
29 | return; | |
30 | cpu_relax(); | |
31 | } | |
32 | pr_err("Control command failed (timeout)!\n"); | |
33 | } | |
34 | ||
35 | static int bcm47xxsflash_poll(struct bcm47xxsflash *b47s, int timeout) | |
36 | { | |
37 | unsigned long deadline = jiffies + timeout; | |
38 | ||
39 | do { | |
40 | switch (b47s->type) { | |
41 | case BCM47XXSFLASH_TYPE_ST: | |
42 | bcm47xxsflash_cmd(b47s, OPCODE_ST_RDSR); | |
43 | if (!(b47s->cc_read(b47s, BCMA_CC_FLASHDATA) & | |
44 | SR_ST_WIP)) | |
45 | return 0; | |
46 | break; | |
47 | case BCM47XXSFLASH_TYPE_ATMEL: | |
48 | bcm47xxsflash_cmd(b47s, OPCODE_AT_STATUS); | |
49 | if (b47s->cc_read(b47s, BCMA_CC_FLASHDATA) & | |
50 | SR_AT_READY) | |
51 | return 0; | |
52 | break; | |
53 | } | |
54 | ||
55 | cpu_relax(); | |
56 | udelay(1); | |
57 | } while (!time_after_eq(jiffies, deadline)); | |
58 | ||
59 | pr_err("Timeout waiting for flash to be ready!\n"); | |
60 | ||
61 | return -EBUSY; | |
62 | } | |
63 | ||
64 | /************************************************** | |
65 | * MTD ops | |
66 | **************************************************/ | |
67 | ||
c0fcbc56 RM |
68 | static int bcm47xxsflash_erase(struct mtd_info *mtd, struct erase_info *erase) |
69 | { | |
70 | struct bcm47xxsflash *b47s = mtd->priv; | |
71 | int err; | |
72 | ||
73 | switch (b47s->type) { | |
74 | case BCM47XXSFLASH_TYPE_ST: | |
75 | bcm47xxsflash_cmd(b47s, OPCODE_ST_WREN); | |
76 | b47s->cc_write(b47s, BCMA_CC_FLASHADDR, erase->addr); | |
77 | /* Newer flashes have "sub-sectors" which can be erased | |
78 | * independently with a new command: ST_SSE. The ST_SE command | |
79 | * erases 64KB just as before. | |
80 | */ | |
81 | if (b47s->blocksize < (64 * 1024)) | |
82 | bcm47xxsflash_cmd(b47s, OPCODE_ST_SSE); | |
83 | else | |
84 | bcm47xxsflash_cmd(b47s, OPCODE_ST_SE); | |
85 | break; | |
86 | case BCM47XXSFLASH_TYPE_ATMEL: | |
87 | b47s->cc_write(b47s, BCMA_CC_FLASHADDR, erase->addr << 1); | |
88 | bcm47xxsflash_cmd(b47s, OPCODE_AT_PAGE_ERASE); | |
89 | break; | |
90 | } | |
91 | ||
92 | err = bcm47xxsflash_poll(b47s, HZ); | |
93 | if (err) | |
94 | erase->state = MTD_ERASE_FAILED; | |
95 | else | |
96 | erase->state = MTD_ERASE_DONE; | |
97 | ||
c0fcbc56 RM |
98 | return err; |
99 | } | |
100 | ||
5fe42d5b RM |
101 | static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len, |
102 | size_t *retlen, u_char *buf) | |
103 | { | |
a2f74a7d | 104 | struct bcm47xxsflash *b47s = mtd->priv; |
ccc38234 | 105 | size_t orig_len = len; |
5fe42d5b RM |
106 | |
107 | /* Check address range */ | |
108 | if ((from + len) > mtd->size) | |
109 | return -EINVAL; | |
110 | ||
ccc38234 RM |
111 | /* Read as much as possible using fast MMIO window */ |
112 | if (from < BCM47XXSFLASH_WINDOW_SZ) { | |
113 | size_t memcpy_len; | |
5fe42d5b | 114 | |
ccc38234 RM |
115 | memcpy_len = min(len, (size_t)(BCM47XXSFLASH_WINDOW_SZ - from)); |
116 | memcpy_fromio(buf, b47s->window + from, memcpy_len); | |
117 | from += memcpy_len; | |
118 | len -= memcpy_len; | |
119 | buf += memcpy_len; | |
120 | } | |
121 | ||
122 | /* Use indirect access for content out of the window */ | |
123 | for (; len; len--) { | |
124 | b47s->cc_write(b47s, BCMA_CC_FLASHADDR, from++); | |
125 | bcm47xxsflash_cmd(b47s, OPCODE_ST_READ4B); | |
126 | *buf++ = b47s->cc_read(b47s, BCMA_CC_FLASHDATA); | |
127 | } | |
128 | ||
129 | *retlen = orig_len; | |
130 | ||
131 | return orig_len; | |
5fe42d5b RM |
132 | } |
133 | ||
13134f48 RM |
134 | static int bcm47xxsflash_write_st(struct mtd_info *mtd, u32 offset, size_t len, |
135 | const u_char *buf) | |
136 | { | |
137 | struct bcm47xxsflash *b47s = mtd->priv; | |
138 | int written = 0; | |
139 | ||
140 | /* Enable writes */ | |
141 | bcm47xxsflash_cmd(b47s, OPCODE_ST_WREN); | |
142 | ||
143 | /* Write first byte */ | |
144 | b47s->cc_write(b47s, BCMA_CC_FLASHADDR, offset); | |
145 | b47s->cc_write(b47s, BCMA_CC_FLASHDATA, *buf++); | |
146 | ||
147 | /* Program page */ | |
148 | if (b47s->bcma_cc->core->id.rev < 20) { | |
149 | bcm47xxsflash_cmd(b47s, OPCODE_ST_PP); | |
150 | return 1; /* 1B written */ | |
151 | } | |
152 | ||
153 | /* Program page and set CSA (on newer chips we can continue writing) */ | |
154 | bcm47xxsflash_cmd(b47s, OPCODE_ST_CSA | OPCODE_ST_PP); | |
155 | offset++; | |
156 | len--; | |
157 | written++; | |
158 | ||
159 | while (len > 0) { | |
160 | /* Page boundary, another function call is needed */ | |
161 | if ((offset & 0xFF) == 0) | |
162 | break; | |
163 | ||
164 | bcm47xxsflash_cmd(b47s, OPCODE_ST_CSA | *buf++); | |
165 | offset++; | |
166 | len--; | |
167 | written++; | |
168 | } | |
169 | ||
170 | /* All done, drop CSA & poll */ | |
171 | b47s->cc_write(b47s, BCMA_CC_FLASHCTL, 0); | |
172 | udelay(1); | |
173 | if (bcm47xxsflash_poll(b47s, HZ / 10)) | |
174 | pr_err("Flash rejected dropping CSA\n"); | |
175 | ||
176 | return written; | |
177 | } | |
178 | ||
179 | static int bcm47xxsflash_write_at(struct mtd_info *mtd, u32 offset, size_t len, | |
180 | const u_char *buf) | |
181 | { | |
182 | struct bcm47xxsflash *b47s = mtd->priv; | |
183 | u32 mask = b47s->blocksize - 1; | |
184 | u32 page = (offset & ~mask) << 1; | |
185 | u32 byte = offset & mask; | |
186 | int written = 0; | |
187 | ||
188 | /* If we don't overwrite whole page, read it to the buffer first */ | |
189 | if (byte || (len < b47s->blocksize)) { | |
190 | int err; | |
191 | ||
192 | b47s->cc_write(b47s, BCMA_CC_FLASHADDR, page); | |
193 | bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_LOAD); | |
194 | /* 250 us for AT45DB321B */ | |
195 | err = bcm47xxsflash_poll(b47s, HZ / 1000); | |
196 | if (err) { | |
197 | pr_err("Timeout reading page 0x%X info buffer\n", page); | |
198 | return err; | |
199 | } | |
200 | } | |
201 | ||
202 | /* Change buffer content with our data */ | |
203 | while (len > 0) { | |
204 | /* Page boundary, another function call is needed */ | |
205 | if (byte == b47s->blocksize) | |
206 | break; | |
207 | ||
208 | b47s->cc_write(b47s, BCMA_CC_FLASHADDR, byte++); | |
209 | b47s->cc_write(b47s, BCMA_CC_FLASHDATA, *buf++); | |
210 | bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_WRITE); | |
211 | len--; | |
212 | written++; | |
213 | } | |
214 | ||
215 | /* Program page with the buffer content */ | |
216 | b47s->cc_write(b47s, BCMA_CC_FLASHADDR, page); | |
217 | bcm47xxsflash_cmd(b47s, OPCODE_AT_BUF1_PROGRAM); | |
218 | ||
219 | return written; | |
220 | } | |
221 | ||
222 | static int bcm47xxsflash_write(struct mtd_info *mtd, loff_t to, size_t len, | |
223 | size_t *retlen, const u_char *buf) | |
224 | { | |
225 | struct bcm47xxsflash *b47s = mtd->priv; | |
226 | int written; | |
227 | ||
228 | /* Writing functions can return without writing all passed data, for | |
229 | * example when the hardware is too old or when we git page boundary. | |
230 | */ | |
231 | while (len > 0) { | |
232 | switch (b47s->type) { | |
233 | case BCM47XXSFLASH_TYPE_ST: | |
234 | written = bcm47xxsflash_write_st(mtd, to, len, buf); | |
235 | break; | |
236 | case BCM47XXSFLASH_TYPE_ATMEL: | |
237 | written = bcm47xxsflash_write_at(mtd, to, len, buf); | |
238 | break; | |
239 | default: | |
240 | BUG_ON(1); | |
241 | } | |
242 | if (written < 0) { | |
243 | pr_err("Error writing at offset 0x%llX\n", to); | |
244 | return written; | |
245 | } | |
246 | to += (loff_t)written; | |
247 | len -= written; | |
248 | *retlen += written; | |
249 | buf += written; | |
250 | } | |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
eb98198f FK |
255 | static void bcm47xxsflash_fill_mtd(struct bcm47xxsflash *b47s, |
256 | struct device *dev) | |
5fe42d5b | 257 | { |
a2f74a7d RM |
258 | struct mtd_info *mtd = &b47s->mtd; |
259 | ||
260 | mtd->priv = b47s; | |
eb98198f | 261 | mtd->dev.parent = dev; |
5fe42d5b | 262 | mtd->name = "bcm47xxsflash"; |
5fe42d5b | 263 | |
13134f48 RM |
264 | mtd->type = MTD_NORFLASH; |
265 | mtd->flags = MTD_CAP_NORFLASH; | |
c0fcbc56 RM |
266 | mtd->size = b47s->size; |
267 | mtd->erasesize = b47s->blocksize; | |
13134f48 RM |
268 | mtd->writesize = 1; |
269 | mtd->writebufsize = 1; | |
c0fcbc56 RM |
270 | |
271 | mtd->_erase = bcm47xxsflash_erase; | |
272 | mtd->_read = bcm47xxsflash_read; | |
13134f48 | 273 | mtd->_write = bcm47xxsflash_write; |
5fe42d5b RM |
274 | } |
275 | ||
f1a7c9d3 RM |
276 | /************************************************** |
277 | * BCMA | |
278 | **************************************************/ | |
279 | ||
265dfbd9 RM |
280 | static int bcm47xxsflash_bcma_cc_read(struct bcm47xxsflash *b47s, u16 offset) |
281 | { | |
282 | return bcma_cc_read32(b47s->bcma_cc, offset); | |
283 | } | |
284 | ||
285 | static void bcm47xxsflash_bcma_cc_write(struct bcm47xxsflash *b47s, u16 offset, | |
286 | u32 value) | |
287 | { | |
288 | bcma_cc_write32(b47s->bcma_cc, offset, value); | |
289 | } | |
290 | ||
f1a7c9d3 | 291 | static int bcm47xxsflash_bcma_probe(struct platform_device *pdev) |
5fe42d5b | 292 | { |
5651d6aa BN |
293 | struct device *dev = &pdev->dev; |
294 | struct bcma_sflash *sflash = dev_get_platdata(dev); | |
a2f74a7d | 295 | struct bcm47xxsflash *b47s; |
5651d6aa | 296 | struct resource *res; |
5fe42d5b RM |
297 | int err; |
298 | ||
5651d6aa | 299 | b47s = devm_kzalloc(dev, sizeof(*b47s), GFP_KERNEL); |
d2b1bd14 LC |
300 | if (!b47s) |
301 | return -ENOMEM; | |
a2f74a7d | 302 | |
5651d6aa BN |
303 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
304 | if (!res) { | |
305 | dev_err(dev, "invalid resource\n"); | |
306 | return -EINVAL; | |
307 | } | |
308 | if (!devm_request_mem_region(dev, res->start, resource_size(res), | |
309 | res->name)) { | |
310 | dev_err(dev, "can't request region for resource %pR\n", res); | |
311 | return -EBUSY; | |
312 | } | |
5651d6aa | 313 | |
41c81536 | 314 | b47s->bcma_cc = container_of(sflash, struct bcma_drv_cc, sflash); |
265dfbd9 RM |
315 | b47s->cc_read = bcm47xxsflash_bcma_cc_read; |
316 | b47s->cc_write = bcm47xxsflash_bcma_cc_write; | |
41c81536 | 317 | |
64ad4637 RM |
318 | /* |
319 | * On old MIPS devices cache was magically invalidated when needed, | |
320 | * allowing us to use cached access and gain some performance. Trying | |
321 | * the same on ARM based BCM53573 results in flash corruptions, we need | |
322 | * to use uncached access for it. | |
323 | * | |
324 | * It may be arch specific, but right now there is only 1 ARM SoC using | |
325 | * this driver, so let's follow Broadcom's reference code and check | |
326 | * ChipCommon revision. | |
327 | */ | |
328 | if (b47s->bcma_cc->core->id.rev == 54) | |
329 | b47s->window = ioremap_nocache(res->start, resource_size(res)); | |
330 | else | |
331 | b47s->window = ioremap_cache(res->start, resource_size(res)); | |
332 | if (!b47s->window) { | |
333 | dev_err(dev, "ioremap failed for resource %pR\n", res); | |
334 | return -ENOMEM; | |
335 | } | |
336 | ||
1f816bc7 RM |
337 | switch (b47s->bcma_cc->capabilities & BCMA_CC_CAP_FLASHT) { |
338 | case BCMA_CC_FLASHT_STSER: | |
339 | b47s->type = BCM47XXSFLASH_TYPE_ST; | |
340 | break; | |
341 | case BCMA_CC_FLASHT_ATSER: | |
342 | b47s->type = BCM47XXSFLASH_TYPE_ATMEL; | |
343 | break; | |
344 | } | |
345 | ||
a2f74a7d RM |
346 | b47s->blocksize = sflash->blocksize; |
347 | b47s->numblocks = sflash->numblocks; | |
348 | b47s->size = sflash->size; | |
eb98198f | 349 | bcm47xxsflash_fill_mtd(b47s, &pdev->dev); |
5fe42d5b | 350 | |
be5e5099 RM |
351 | platform_set_drvdata(pdev, b47s); |
352 | ||
a2f74a7d | 353 | err = mtd_device_parse_register(&b47s->mtd, probes, NULL, NULL, 0); |
5fe42d5b RM |
354 | if (err) { |
355 | pr_err("Failed to register MTD device: %d\n", err); | |
5651d6aa | 356 | iounmap(b47s->window); |
d2b1bd14 | 357 | return err; |
5fe42d5b RM |
358 | } |
359 | ||
bddcb5e7 RM |
360 | if (bcm47xxsflash_poll(b47s, HZ / 10)) |
361 | pr_warn("Serial flash busy\n"); | |
362 | ||
5fe42d5b | 363 | return 0; |
5fe42d5b RM |
364 | } |
365 | ||
f1a7c9d3 | 366 | static int bcm47xxsflash_bcma_remove(struct platform_device *pdev) |
5fe42d5b | 367 | { |
be5e5099 | 368 | struct bcm47xxsflash *b47s = platform_get_drvdata(pdev); |
5fe42d5b | 369 | |
a2f74a7d | 370 | mtd_device_unregister(&b47s->mtd); |
5651d6aa | 371 | iounmap(b47s->window); |
5fe42d5b RM |
372 | |
373 | return 0; | |
374 | } | |
375 | ||
376 | static struct platform_driver bcma_sflash_driver = { | |
f1a7c9d3 RM |
377 | .probe = bcm47xxsflash_bcma_probe, |
378 | .remove = bcm47xxsflash_bcma_remove, | |
5fe42d5b RM |
379 | .driver = { |
380 | .name = "bcma_sflash", | |
5fe42d5b RM |
381 | }, |
382 | }; | |
383 | ||
f1a7c9d3 RM |
384 | /************************************************** |
385 | * Init | |
386 | **************************************************/ | |
387 | ||
8268df26 | 388 | module_platform_driver(bcma_sflash_driver); |