1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/slab.h>
4 #include <linux/delay.h>
5 #include <linux/ioport.h>
6 #include <linux/mtd/mtd.h>
7 #include <linux/platform_device.h>
8 #include <linux/bcma/bcma.h>
10 #include "bcm47xxsflash.h"
12 MODULE_LICENSE("GPL");
13 MODULE_DESCRIPTION("Serial flash driver for BCMA bus");
15 static const char * const probes
[] = { "bcm47xxpart", NULL
};
17 /**************************************************
19 **************************************************/
21 static void bcm47xxsflash_cmd(struct bcm47xxsflash
*b47s
, u32 opcode
)
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
))
32 pr_err("Control command failed (timeout)!\n");
35 static int bcm47xxsflash_poll(struct bcm47xxsflash
*b47s
, int timeout
)
37 unsigned long deadline
= jiffies
+ timeout
;
41 case BCM47XXSFLASH_TYPE_ST
:
42 bcm47xxsflash_cmd(b47s
, OPCODE_ST_RDSR
);
43 if (!(b47s
->cc_read(b47s
, BCMA_CC_FLASHDATA
) &
47 case BCM47XXSFLASH_TYPE_ATMEL
:
48 bcm47xxsflash_cmd(b47s
, OPCODE_AT_STATUS
);
49 if (b47s
->cc_read(b47s
, BCMA_CC_FLASHDATA
) &
57 } while (!time_after_eq(jiffies
, deadline
));
59 pr_err("Timeout waiting for flash to be ready!\n");
64 /**************************************************
66 **************************************************/
68 static int bcm47xxsflash_erase(struct mtd_info
*mtd
, struct erase_info
*erase
)
70 struct bcm47xxsflash
*b47s
= mtd
->priv
;
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.
81 if (b47s
->blocksize
< (64 * 1024))
82 bcm47xxsflash_cmd(b47s
, OPCODE_ST_SSE
);
84 bcm47xxsflash_cmd(b47s
, OPCODE_ST_SE
);
86 case BCM47XXSFLASH_TYPE_ATMEL
:
87 b47s
->cc_write(b47s
, BCMA_CC_FLASHADDR
, erase
->addr
<< 1);
88 bcm47xxsflash_cmd(b47s
, OPCODE_AT_PAGE_ERASE
);
92 err
= bcm47xxsflash_poll(b47s
, HZ
);
94 erase
->state
= MTD_ERASE_FAILED
;
96 erase
->state
= MTD_ERASE_DONE
;
99 erase
->callback(erase
);
104 static int bcm47xxsflash_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
105 size_t *retlen
, u_char
*buf
)
107 struct bcm47xxsflash
*b47s
= mtd
->priv
;
108 size_t orig_len
= len
;
110 /* Check address range */
111 if ((from
+ len
) > mtd
->size
)
114 /* Read as much as possible using fast MMIO window */
115 if (from
< BCM47XXSFLASH_WINDOW_SZ
) {
118 memcpy_len
= min(len
, (size_t)(BCM47XXSFLASH_WINDOW_SZ
- from
));
119 memcpy_fromio(buf
, b47s
->window
+ from
, memcpy_len
);
125 /* Use indirect access for content out of the window */
127 b47s
->cc_write(b47s
, BCMA_CC_FLASHADDR
, from
++);
128 bcm47xxsflash_cmd(b47s
, OPCODE_ST_READ4B
);
129 *buf
++ = b47s
->cc_read(b47s
, BCMA_CC_FLASHDATA
);
137 static int bcm47xxsflash_write_st(struct mtd_info
*mtd
, u32 offset
, size_t len
,
140 struct bcm47xxsflash
*b47s
= mtd
->priv
;
144 bcm47xxsflash_cmd(b47s
, OPCODE_ST_WREN
);
146 /* Write first byte */
147 b47s
->cc_write(b47s
, BCMA_CC_FLASHADDR
, offset
);
148 b47s
->cc_write(b47s
, BCMA_CC_FLASHDATA
, *buf
++);
151 if (b47s
->bcma_cc
->core
->id
.rev
< 20) {
152 bcm47xxsflash_cmd(b47s
, OPCODE_ST_PP
);
153 return 1; /* 1B written */
156 /* Program page and set CSA (on newer chips we can continue writing) */
157 bcm47xxsflash_cmd(b47s
, OPCODE_ST_CSA
| OPCODE_ST_PP
);
163 /* Page boundary, another function call is needed */
164 if ((offset
& 0xFF) == 0)
167 bcm47xxsflash_cmd(b47s
, OPCODE_ST_CSA
| *buf
++);
173 /* All done, drop CSA & poll */
174 b47s
->cc_write(b47s
, BCMA_CC_FLASHCTL
, 0);
176 if (bcm47xxsflash_poll(b47s
, HZ
/ 10))
177 pr_err("Flash rejected dropping CSA\n");
182 static int bcm47xxsflash_write_at(struct mtd_info
*mtd
, u32 offset
, size_t len
,
185 struct bcm47xxsflash
*b47s
= mtd
->priv
;
186 u32 mask
= b47s
->blocksize
- 1;
187 u32 page
= (offset
& ~mask
) << 1;
188 u32 byte
= offset
& mask
;
191 /* If we don't overwrite whole page, read it to the buffer first */
192 if (byte
|| (len
< b47s
->blocksize
)) {
195 b47s
->cc_write(b47s
, BCMA_CC_FLASHADDR
, page
);
196 bcm47xxsflash_cmd(b47s
, OPCODE_AT_BUF1_LOAD
);
197 /* 250 us for AT45DB321B */
198 err
= bcm47xxsflash_poll(b47s
, HZ
/ 1000);
200 pr_err("Timeout reading page 0x%X info buffer\n", page
);
205 /* Change buffer content with our data */
207 /* Page boundary, another function call is needed */
208 if (byte
== b47s
->blocksize
)
211 b47s
->cc_write(b47s
, BCMA_CC_FLASHADDR
, byte
++);
212 b47s
->cc_write(b47s
, BCMA_CC_FLASHDATA
, *buf
++);
213 bcm47xxsflash_cmd(b47s
, OPCODE_AT_BUF1_WRITE
);
218 /* Program page with the buffer content */
219 b47s
->cc_write(b47s
, BCMA_CC_FLASHADDR
, page
);
220 bcm47xxsflash_cmd(b47s
, OPCODE_AT_BUF1_PROGRAM
);
225 static int bcm47xxsflash_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
226 size_t *retlen
, const u_char
*buf
)
228 struct bcm47xxsflash
*b47s
= mtd
->priv
;
231 /* Writing functions can return without writing all passed data, for
232 * example when the hardware is too old or when we git page boundary.
235 switch (b47s
->type
) {
236 case BCM47XXSFLASH_TYPE_ST
:
237 written
= bcm47xxsflash_write_st(mtd
, to
, len
, buf
);
239 case BCM47XXSFLASH_TYPE_ATMEL
:
240 written
= bcm47xxsflash_write_at(mtd
, to
, len
, buf
);
246 pr_err("Error writing at offset 0x%llX\n", to
);
249 to
+= (loff_t
)written
;
258 static void bcm47xxsflash_fill_mtd(struct bcm47xxsflash
*b47s
,
261 struct mtd_info
*mtd
= &b47s
->mtd
;
264 mtd
->dev
.parent
= dev
;
265 mtd
->name
= "bcm47xxsflash";
267 mtd
->type
= MTD_NORFLASH
;
268 mtd
->flags
= MTD_CAP_NORFLASH
;
269 mtd
->size
= b47s
->size
;
270 mtd
->erasesize
= b47s
->blocksize
;
272 mtd
->writebufsize
= 1;
274 mtd
->_erase
= bcm47xxsflash_erase
;
275 mtd
->_read
= bcm47xxsflash_read
;
276 mtd
->_write
= bcm47xxsflash_write
;
279 /**************************************************
281 **************************************************/
283 static int bcm47xxsflash_bcma_cc_read(struct bcm47xxsflash
*b47s
, u16 offset
)
285 return bcma_cc_read32(b47s
->bcma_cc
, offset
);
288 static void bcm47xxsflash_bcma_cc_write(struct bcm47xxsflash
*b47s
, u16 offset
,
291 bcma_cc_write32(b47s
->bcma_cc
, offset
, value
);
294 static int bcm47xxsflash_bcma_probe(struct platform_device
*pdev
)
296 struct device
*dev
= &pdev
->dev
;
297 struct bcma_sflash
*sflash
= dev_get_platdata(dev
);
298 struct bcm47xxsflash
*b47s
;
299 struct resource
*res
;
302 b47s
= devm_kzalloc(dev
, sizeof(*b47s
), GFP_KERNEL
);
306 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
308 dev_err(dev
, "invalid resource\n");
311 if (!devm_request_mem_region(dev
, res
->start
, resource_size(res
),
313 dev_err(dev
, "can't request region for resource %pR\n", res
);
317 b47s
->bcma_cc
= container_of(sflash
, struct bcma_drv_cc
, sflash
);
318 b47s
->cc_read
= bcm47xxsflash_bcma_cc_read
;
319 b47s
->cc_write
= bcm47xxsflash_bcma_cc_write
;
322 * On old MIPS devices cache was magically invalidated when needed,
323 * allowing us to use cached access and gain some performance. Trying
324 * the same on ARM based BCM53573 results in flash corruptions, we need
325 * to use uncached access for it.
327 * It may be arch specific, but right now there is only 1 ARM SoC using
328 * this driver, so let's follow Broadcom's reference code and check
329 * ChipCommon revision.
331 if (b47s
->bcma_cc
->core
->id
.rev
== 54)
332 b47s
->window
= ioremap_nocache(res
->start
, resource_size(res
));
334 b47s
->window
= ioremap_cache(res
->start
, resource_size(res
));
336 dev_err(dev
, "ioremap failed for resource %pR\n", res
);
340 switch (b47s
->bcma_cc
->capabilities
& BCMA_CC_CAP_FLASHT
) {
341 case BCMA_CC_FLASHT_STSER
:
342 b47s
->type
= BCM47XXSFLASH_TYPE_ST
;
344 case BCMA_CC_FLASHT_ATSER
:
345 b47s
->type
= BCM47XXSFLASH_TYPE_ATMEL
;
349 b47s
->blocksize
= sflash
->blocksize
;
350 b47s
->numblocks
= sflash
->numblocks
;
351 b47s
->size
= sflash
->size
;
352 bcm47xxsflash_fill_mtd(b47s
, &pdev
->dev
);
354 platform_set_drvdata(pdev
, b47s
);
356 err
= mtd_device_parse_register(&b47s
->mtd
, probes
, NULL
, NULL
, 0);
358 pr_err("Failed to register MTD device: %d\n", err
);
359 iounmap(b47s
->window
);
363 if (bcm47xxsflash_poll(b47s
, HZ
/ 10))
364 pr_warn("Serial flash busy\n");
369 static int bcm47xxsflash_bcma_remove(struct platform_device
*pdev
)
371 struct bcm47xxsflash
*b47s
= platform_get_drvdata(pdev
);
373 mtd_device_unregister(&b47s
->mtd
);
374 iounmap(b47s
->window
);
379 static struct platform_driver bcma_sflash_driver
= {
380 .probe
= bcm47xxsflash_bcma_probe
,
381 .remove
= bcm47xxsflash_bcma_remove
,
383 .name
= "bcma_sflash",
387 /**************************************************
389 **************************************************/
391 module_platform_driver(bcma_sflash_driver
);