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 "bcma_private.h"
16 #include <linux/platform_device.h>
17 +#include <linux/delay.h>
18 #include <linux/bcma/bcma.h>
19 +#include <linux/bcma/bcma_driver_chipcommon.h>
21 +#define NUM_RETRIES 3
23 static struct resource bcma_sflash_resource = {
24 .name = "bcma_sflash",
25 @@ -18,7 +25,7 @@ static struct resource bcma_sflash_resou
28 struct platform_device bcma_sflash_dev = {
29 - .name = "bcma_sflash",
30 + .name = "bcm47xx-sflash",
31 .resource = &bcma_sflash_resource,
34 @@ -30,7 +37,7 @@ struct bcma_sflash_tbl_e {
38 -static struct bcma_sflash_tbl_e bcma_sflash_st_tbl[] = {
39 +static const struct bcma_sflash_tbl_e bcma_sflash_st_tbl[] = {
40 { "M25P20", 0x11, 0x10000, 4, },
41 { "M25P40", 0x12, 0x10000, 8, },
43 @@ -41,7 +48,7 @@ static struct bcma_sflash_tbl_e bcma_sfl
47 -static struct bcma_sflash_tbl_e bcma_sflash_sst_tbl[] = {
48 +static const struct bcma_sflash_tbl_e bcma_sflash_sst_tbl[] = {
49 { "SST25WF512", 1, 0x1000, 16, },
50 { "SST25VF512", 0x48, 0x1000, 16, },
51 { "SST25WF010", 2, 0x1000, 32, },
52 @@ -59,7 +66,7 @@ static struct bcma_sflash_tbl_e bcma_sfl
56 -static struct bcma_sflash_tbl_e bcma_sflash_at_tbl[] = {
57 +static const struct bcma_sflash_tbl_e bcma_sflash_at_tbl[] = {
58 { "AT45DB011", 0xc, 256, 512, },
59 { "AT45DB021", 0x14, 256, 1024, },
60 { "AT45DB041", 0x1c, 256, 2048, },
61 @@ -84,12 +91,186 @@ static void bcma_sflash_cmd(struct bcma_
62 bcma_err(cc->core->bus, "SFLASH control command failed (timeout)!\n");
65 +static void bcma_sflash_write_u8(struct bcma_drv_cc *cc, u32 offset, u8 byte)
67 + bcma_cc_write32(cc, BCMA_CC_FLASHADDR, offset);
68 + bcma_cc_write32(cc, BCMA_CC_FLASHDATA, byte);
71 +/* Poll for command completion. Returns zero when complete. */
72 +static int bcma_sflash_poll(struct bcm47xxsflash *dev, u32 offset)
74 + struct bcma_drv_cc *cc = dev->bcc;
76 + if (offset >= cc->sflash.size)
79 + switch (cc->capabilities & BCMA_CC_CAP_FLASHT) {
80 + case BCMA_CC_FLASHT_STSER:
81 + /* Check for ST Write In Progress bit */
82 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_RDSR);
83 + return bcma_cc_read32(cc, BCMA_CC_FLASHDATA)
84 + & BCMA_CC_FLASHDATA_ST_WIP;
85 + case BCMA_CC_FLASHT_ATSER:
86 + /* Check for Atmel Ready bit */
87 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_AT_STATUS);
88 + return !(bcma_cc_read32(cc, BCMA_CC_FLASHDATA)
89 + & BCMA_CC_FLASHDATA_AT_READY);
95 +static int sflash_st_write(struct bcm47xxsflash *dev, u32 offset, u32 len,
99 + struct bcma_drv_cc *cc = dev->bcc;
101 + /* Enable writes */
102 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_WREN);
103 + bcma_sflash_write_u8(cc, offset, *buf++);
104 + /* Issue a page program with CSA bit set */
105 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_CSA | BCMA_CC_FLASHCTL_ST_PP);
109 + if ((offset & 255) == 0) {
110 + /* Page boundary, poll droping cs and return */
111 + bcma_cc_write32(cc, BCMA_CC_FLASHCTL, 0);
113 + if (!bcma_sflash_poll(dev, offset)) {
114 + /* Flash rejected command */
119 + /* Write single byte */
120 + bcma_sflash_cmd(cc,
121 + BCMA_CC_FLASHCTL_ST_CSA |
128 + /* All done, drop cs & poll */
129 + bcma_cc_write32(cc, BCMA_CC_FLASHCTL, 0);
131 + if (!bcma_sflash_poll(dev, offset)) {
132 + /* Flash rejected command */
138 +static int sflash_at_write(struct bcm47xxsflash *dev, u32 offset, u32 len,
141 + struct bcma_drv_cc *cc = dev->bcc;
142 + u32 page, byte, mask;
145 + mask = dev->blocksize - 1;
146 + page = (offset & ~mask) << 1;
147 + byte = offset & mask;
148 + /* Read main memory page into buffer 1 */
149 + if (byte || (len < dev->blocksize)) {
151 + bcma_cc_write32(cc, BCMA_CC_FLASHADDR, page);
152 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_AT_BUF1_LOAD);
153 + /* 250 us for AT45DB321B */
154 + while (i > 0 && bcma_sflash_poll(dev, offset)) {
158 + BUG_ON(!bcma_sflash_poll(dev, offset));
160 + /* Write into buffer 1 */
161 + for (ret = 0; (ret < (int)len) && (byte < dev->blocksize); ret++) {
162 + bcma_sflash_write_u8(cc, byte++, *buf++);
163 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_AT_BUF1_WRITE);
165 + /* Write buffer 1 into main memory page */
166 + bcma_cc_write32(cc, BCMA_CC_FLASHADDR, page);
167 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_AT_BUF1_PROGRAM);
172 +/* Write len bytes starting at offset into buf. Returns number of bytes
173 + * written. Caller should poll for completion.
175 +static int bcma_sflash_write(struct bcm47xxsflash *dev, u32 offset, u32 len,
178 + int ret = 0, tries = NUM_RETRIES;
179 + struct bcma_drv_cc *cc = dev->bcc;
184 + if ((offset + len) > cc->sflash.size)
187 + switch (cc->capabilities & BCMA_CC_CAP_FLASHT) {
188 + case BCMA_CC_FLASHT_STSER:
190 + ret = sflash_st_write(dev, offset, len, buf);
192 + } while (ret == -EAGAIN && tries > 0);
194 + if (ret == -EAGAIN && tries == 0) {
195 + bcma_info(cc->core->bus, "ST Flash rejected write\n");
199 + case BCMA_CC_FLASHT_ATSER:
200 + ret = sflash_at_write(dev, offset, len, buf);
207 +/* Erase a region. Returns number of bytes scheduled for erasure.
208 + * Caller should poll for completion.
210 +static int bcma_sflash_erase(struct bcm47xxsflash *dev, u32 offset)
212 + struct bcma_drv_cc *cc = dev->bcc;
214 + if (offset >= cc->sflash.size)
217 + switch (cc->capabilities & BCMA_CC_CAP_FLASHT) {
218 + case BCMA_CC_FLASHT_STSER:
219 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_WREN);
220 + bcma_cc_write32(cc, BCMA_CC_FLASHADDR, offset);
221 + /* Newer flashes have "sub-sectors" which can be erased independently
222 + * with a new command: ST_SSE. The ST_SE command erases 64KB just as
225 + if (dev->blocksize < (64 * 1024))
226 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_SSE);
228 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_SE);
229 + return dev->blocksize;
230 + case BCMA_CC_FLASHT_ATSER:
231 + bcma_cc_write32(cc, BCMA_CC_FLASHADDR, offset << 1);
232 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_AT_PAGE_ERASE);
233 + return dev->blocksize;
239 /* Initialize serial flash access */
240 int bcma_sflash_init(struct bcma_drv_cc *cc)
242 struct bcma_bus *bus = cc->core->bus;
243 - struct bcma_sflash *sflash = &cc->sflash;
244 - struct bcma_sflash_tbl_e *e;
245 + struct bcm47xxsflash *sflash = &cc->sflash;
246 + const struct bcma_sflash_tbl_e *e;
249 switch (cc->capabilities & BCMA_CC_CAP_FLASHT) {
250 @@ -150,6 +331,11 @@ int bcma_sflash_init(struct bcma_drv_cc
251 sflash->numblocks = e->numblocks;
252 sflash->size = sflash->blocksize * sflash->numblocks;
253 sflash->present = true;
254 + sflash->poll = bcma_sflash_poll;
255 + sflash->write = bcma_sflash_write;
256 + sflash->erase = bcma_sflash_erase;
257 + sflash->type = BCM47XX_SFLASH_BCMA;
260 bcma_info(bus, "Found %s serial flash (size: %dKiB, blocksize: 0x%X, blocks: %d)\n",
261 e->name, sflash->size / 1024, sflash->blocksize,
262 --- a/include/linux/bcma/bcma_driver_chipcommon.h
263 +++ b/include/linux/bcma/bcma_driver_chipcommon.h
266 #include <linux/platform_device.h>
267 #include <linux/gpio.h>
268 +#include <linux/mtd/bcm47xxsflash.h>
270 /** ChipCommon core registers. **/
271 #define BCMA_CC_ID 0x0000
272 @@ -520,19 +521,6 @@ struct bcma_pflash {
276 -#ifdef CONFIG_BCMA_SFLASH
277 -struct bcma_sflash {
284 - struct mtd_info *mtd;
289 #ifdef CONFIG_BCMA_NFLASH
292 @@ -566,7 +554,7 @@ struct bcma_drv_cc {
293 #ifdef CONFIG_BCMA_DRIVER_MIPS
294 struct bcma_pflash pflash;
295 #ifdef CONFIG_BCMA_SFLASH
296 - struct bcma_sflash sflash;
297 + struct bcm47xxsflash sflash;
299 #ifdef CONFIG_BCMA_NFLASH
300 struct bcma_nflash nflash;