1 --- a/drivers/bcma/driver_chipcommon_sflash.c
2 +++ b/drivers/bcma/driver_chipcommon_sflash.c
5 * Broadcom specific AMBA
6 * ChipCommon serial flash interface
7 + * Copyright 2011, Jonas Gorski <jonas.gorski@gmail.com>
8 + * Copyright 2011, 2012, Hauke Mehrtens <hauke@hauke-m.de>
9 + * Copyright 2010, Broadcom Corporation
11 * Licensed under the GNU/GPL. See COPYING for details.
14 #include <linux/platform_device.h>
15 +#include <linux/delay.h>
16 #include <linux/bcma/bcma.h>
17 +#include <linux/bcma/bcma_driver_chipcommon.h>
19 #include "bcma_private.h"
21 +#define NUM_RETRIES 3
23 static struct resource bcma_sflash_resource = {
24 .name = "bcma_sflash",
25 .start = BCMA_SOC_FLASH2,
26 @@ -18,7 +25,7 @@ static struct resource bcma_sflash_resou
29 struct platform_device bcma_sflash_dev = {
30 - .name = "bcma_sflash",
31 + .name = "bcm47xx-sflash",
32 .resource = &bcma_sflash_resource,
35 @@ -30,7 +37,7 @@ struct bcma_sflash_tbl_e {
39 -static struct bcma_sflash_tbl_e bcma_sflash_st_tbl[] = {
40 +static const struct bcma_sflash_tbl_e bcma_sflash_st_tbl[] = {
41 { "M25P20", 0x11, 0x10000, 4, },
42 { "M25P40", 0x12, 0x10000, 8, },
44 @@ -41,7 +48,7 @@ static struct bcma_sflash_tbl_e bcma_sfl
48 -static struct bcma_sflash_tbl_e bcma_sflash_sst_tbl[] = {
49 +static const struct bcma_sflash_tbl_e bcma_sflash_sst_tbl[] = {
50 { "SST25WF512", 1, 0x1000, 16, },
51 { "SST25VF512", 0x48, 0x1000, 16, },
52 { "SST25WF010", 2, 0x1000, 32, },
53 @@ -59,7 +66,7 @@ static struct bcma_sflash_tbl_e bcma_sfl
57 -static struct bcma_sflash_tbl_e bcma_sflash_at_tbl[] = {
58 +static const struct bcma_sflash_tbl_e bcma_sflash_at_tbl[] = {
59 { "AT45DB011", 0xc, 256, 512, },
60 { "AT45DB021", 0x14, 256, 1024, },
61 { "AT45DB041", 0x1c, 256, 2048, },
62 @@ -84,12 +91,230 @@ static void bcma_sflash_cmd(struct bcma_
63 bcma_err(cc->core->bus, "SFLASH control command failed (timeout)!\n");
66 +static void bcma_sflash_write_u8(struct bcma_drv_cc *cc, u32 offset, u8 byte)
68 + bcma_cc_write32(cc, BCMA_CC_FLASHADDR, offset);
69 + bcma_cc_write32(cc, BCMA_CC_FLASHDATA, byte);
72 +/* Read len bytes starting at offset into buf. Returns number of bytes read. */
73 +static int bcma_sflash_read(struct bcm47xx_sflash *dev, u32 offset, u32 len, u8 *buf)
77 + struct bcma_drv_cc *cc = dev->bcc;
82 + if ((offset + len) > cc->sflash.size)
85 + if ((len >= 4) && (offset & 3))
86 + cnt = 4 - (offset & 3);
87 + else if ((len >= 4) && ((u32)buf & 3))
88 + cnt = 4 - ((u32)buf & 3);
92 + from = (u8 *)KSEG0ADDR(BCMA_SOC_FLASH2 + offset);
97 + for (i = 0; i < cnt; i++) {
106 + *(u32 *)to = readl(from);
115 +/* Poll for command completion. Returns zero when complete. */
116 +static int bcma_sflash_poll(struct bcm47xx_sflash *dev, u32 offset)
118 + struct bcma_drv_cc *cc = dev->bcc;
120 + if (offset >= cc->sflash.size)
123 + switch (cc->capabilities & BCMA_CC_CAP_FLASHT) {
124 + case BCMA_CC_FLASHT_STSER:
125 + /* Check for ST Write In Progress bit */
126 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_RDSR);
127 + return bcma_cc_read32(cc, BCMA_CC_FLASHDATA)
128 + & BCMA_CC_FLASHDATA_ST_WIP;
129 + case BCMA_CC_FLASHT_ATSER:
130 + /* Check for Atmel Ready bit */
131 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_AT_STATUS);
132 + return !(bcma_cc_read32(cc, BCMA_CC_FLASHDATA)
133 + & BCMA_CC_FLASHDATA_AT_READY);
140 +static int sflash_st_write(struct bcm47xx_sflash *dev, u32 offset, u32 len,
144 + struct bcma_drv_cc *cc = dev->bcc;
146 + /* Enable writes */
147 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_WREN);
148 + bcma_sflash_write_u8(cc, offset, *buf++);
149 + /* Issue a page program with CSA bit set */
150 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_CSA | BCMA_CC_FLASHCTL_ST_PP);
154 + if ((offset & 255) == 0) {
155 + /* Page boundary, poll droping cs and return */
156 + bcma_cc_write32(cc, BCMA_CC_FLASHCTL, 0);
158 + if (!bcma_sflash_poll(dev, offset)) {
159 + /* Flash rejected command */
164 + /* Write single byte */
165 + bcma_sflash_cmd(cc,
166 + BCMA_CC_FLASHCTL_ST_CSA |
173 + /* All done, drop cs & poll */
174 + bcma_cc_write32(cc, BCMA_CC_FLASHCTL, 0);
176 + if (!bcma_sflash_poll(dev, offset)) {
177 + /* Flash rejected command */
183 +static int sflash_at_write(struct bcm47xx_sflash *dev, u32 offset, u32 len,
186 + struct bcma_drv_cc *cc = dev->bcc;
187 + u32 page, byte, mask;
190 + mask = dev->blocksize - 1;
191 + page = (offset & ~mask) << 1;
192 + byte = offset & mask;
193 + /* Read main memory page into buffer 1 */
194 + if (byte || (len < dev->blocksize)) {
196 + bcma_cc_write32(cc, BCMA_CC_FLASHADDR, page);
197 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_AT_BUF1_LOAD);
198 + /* 250 us for AT45DB321B */
199 + while (i > 0 && bcma_sflash_poll(dev, offset)) {
203 + BUG_ON(!bcma_sflash_poll(dev, offset));
205 + /* Write into buffer 1 */
206 + for (ret = 0; (ret < (int)len) && (byte < dev->blocksize); ret++) {
207 + bcma_sflash_write_u8(cc, byte++, *buf++);
208 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_AT_BUF1_WRITE);
210 + /* Write buffer 1 into main memory page */
211 + bcma_cc_write32(cc, BCMA_CC_FLASHADDR, page);
212 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_AT_BUF1_PROGRAM);
217 +/* Write len bytes starting at offset into buf. Returns number of bytes
218 + * written. Caller should poll for completion.
220 +static int bcma_sflash_write(struct bcm47xx_sflash *dev, u32 offset, u32 len,
223 + int ret = 0, tries = NUM_RETRIES;
224 + struct bcma_drv_cc *cc = dev->bcc;
229 + if ((offset + len) > cc->sflash.size)
232 + switch (cc->capabilities & BCMA_CC_CAP_FLASHT) {
233 + case BCMA_CC_FLASHT_STSER:
235 + ret = sflash_st_write(dev, offset, len, buf);
237 + } while (ret == -EAGAIN && tries > 0);
239 + if (ret == -EAGAIN && tries == 0) {
240 + bcma_info(cc->core->bus, "ST Flash rejected write\n");
244 + case BCMA_CC_FLASHT_ATSER:
245 + ret = sflash_at_write(dev, offset, len, buf);
252 +/* Erase a region. Returns number of bytes scheduled for erasure.
253 + * Caller should poll for completion.
255 +static int bcma_sflash_erase(struct bcm47xx_sflash *dev, u32 offset)
257 + struct bcma_drv_cc *cc = dev->bcc;
259 + if (offset >= cc->sflash.size)
262 + switch (cc->capabilities & BCMA_CC_CAP_FLASHT) {
263 + case BCMA_CC_FLASHT_STSER:
264 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_WREN);
265 + bcma_cc_write32(cc, BCMA_CC_FLASHADDR, offset);
266 + /* Newer flashes have "sub-sectors" which can be erased independently
267 + * with a new command: ST_SSE. The ST_SE command erases 64KB just as
270 + if (dev->blocksize < (64 * 1024))
271 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_SSE);
273 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_SE);
274 + return dev->blocksize;
275 + case BCMA_CC_FLASHT_ATSER:
276 + bcma_cc_write32(cc, BCMA_CC_FLASHADDR, offset << 1);
277 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_AT_PAGE_ERASE);
278 + return dev->blocksize;
284 /* Initialize serial flash access */
285 int bcma_sflash_init(struct bcma_drv_cc *cc)
287 struct bcma_bus *bus = cc->core->bus;
288 - struct bcma_sflash *sflash = &cc->sflash;
289 - struct bcma_sflash_tbl_e *e;
290 + struct bcm47xx_sflash *sflash = &cc->sflash;
291 + const struct bcma_sflash_tbl_e *e;
294 switch (cc->capabilities & BCMA_CC_CAP_FLASHT) {
295 @@ -150,6 +375,12 @@ int bcma_sflash_init(struct bcma_drv_cc
296 sflash->numblocks = e->numblocks;
297 sflash->size = sflash->blocksize * sflash->numblocks;
298 sflash->present = true;
299 + sflash->read = bcma_sflash_read;
300 + sflash->poll = bcma_sflash_poll;
301 + sflash->write = bcma_sflash_write;
302 + sflash->erase = bcma_sflash_erase;
303 + sflash->type = BCM47XX_SFLASH_BCMA;
306 bcma_info(bus, "Found %s serial flash (size: %dKiB, blocksize: 0x%X, blocks: %d)\n",
307 e->name, sflash->size / 1024, sflash->blocksize,
308 --- a/include/linux/bcma/bcma_driver_chipcommon.h
309 +++ b/include/linux/bcma/bcma_driver_chipcommon.h
311 #ifndef LINUX_BCMA_DRIVER_CC_H_
312 #define LINUX_BCMA_DRIVER_CC_H_
314 +#include <linux/mtd/bcm47xx_sflash.h>
316 /** ChipCommon core registers. **/
317 #define BCMA_CC_ID 0x0000
318 #define BCMA_CC_ID_ID 0x0000FFFF
319 @@ -516,17 +518,6 @@ struct bcma_pflash {
323 -#ifdef CONFIG_BCMA_SFLASH
324 -struct bcma_sflash {
331 - struct mtd_info *mtd;
335 #ifdef CONFIG_BCMA_NFLASH
337 @@ -561,7 +552,7 @@ struct bcma_drv_cc {
338 #ifdef CONFIG_BCMA_DRIVER_MIPS
339 struct bcma_pflash pflash;
340 #ifdef CONFIG_BCMA_SFLASH
341 - struct bcma_sflash sflash;
342 + struct bcm47xx_sflash sflash;
344 #ifdef CONFIG_BCMA_NFLASH
345 struct bcma_nflash nflash;