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