brcm47xx: board detection WNDR3400v2
[openwrt/openwrt.git] / target / linux / brcm47xx / patches-3.8 / 070-bcma-add-functions-to-write-to-serial-flash.patch
1 --- a/drivers/bcma/driver_chipcommon_sflash.c
2 +++ b/drivers/bcma/driver_chipcommon_sflash.c
3 @@ -1,6 +1,9 @@
4 /*
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
10 *
11 * Licensed under the GNU/GPL. See COPYING for details.
12 */
13 @@ -8,7 +11,11 @@
14 #include "bcma_private.h"
15
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>
20 +
21 +#define NUM_RETRIES 3
22
23 static struct resource bcma_sflash_resource = {
24 .name = "bcma_sflash",
25 @@ -18,7 +25,7 @@ static struct resource bcma_sflash_resou
26 };
27
28 struct platform_device bcma_sflash_dev = {
29 - .name = "bcma_sflash",
30 + .name = "bcm47xx-sflash",
31 .resource = &bcma_sflash_resource,
32 .num_resources = 1,
33 };
34 @@ -30,7 +37,7 @@ struct bcma_sflash_tbl_e {
35 u16 numblocks;
36 };
37
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, },
42
43 @@ -41,7 +48,7 @@ static struct bcma_sflash_tbl_e bcma_sfl
44 { 0 },
45 };
46
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
53 { 0 },
54 };
55
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");
63 }
64
65 +static void bcma_sflash_write_u8(struct bcma_drv_cc *cc, u32 offset, u8 byte)
66 +{
67 + bcma_cc_write32(cc, BCMA_CC_FLASHADDR, offset);
68 + bcma_cc_write32(cc, BCMA_CC_FLASHDATA, byte);
69 +}
70 +
71 +/* Poll for command completion. Returns zero when complete. */
72 +static int bcma_sflash_poll(struct bcm47xxsflash *dev, u32 offset)
73 +{
74 + struct bcma_drv_cc *cc = dev->bcc;
75 +
76 + if (offset >= cc->sflash.size)
77 + return -22;
78 +
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);
90 + }
91 +
92 + return 0;
93 +}
94 +
95 +static int sflash_st_write(struct bcm47xxsflash *dev, u32 offset, u32 len,
96 + const u8 *buf)
97 +{
98 + int written = 1;
99 + struct bcma_drv_cc *cc = dev->bcc;
100 +
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);
106 + offset++;
107 + len--;
108 + while (len > 0) {
109 + if ((offset & 255) == 0) {
110 + /* Page boundary, poll droping cs and return */
111 + bcma_cc_write32(cc, BCMA_CC_FLASHCTL, 0);
112 + udelay(1);
113 + if (!bcma_sflash_poll(dev, offset)) {
114 + /* Flash rejected command */
115 + return -EAGAIN;
116 + }
117 + return written;
118 + } else {
119 + /* Write single byte */
120 + bcma_sflash_cmd(cc,
121 + BCMA_CC_FLASHCTL_ST_CSA |
122 + *buf++);
123 + }
124 + written++;
125 + offset++;
126 + len--;
127 + }
128 + /* All done, drop cs & poll */
129 + bcma_cc_write32(cc, BCMA_CC_FLASHCTL, 0);
130 + udelay(1);
131 + if (!bcma_sflash_poll(dev, offset)) {
132 + /* Flash rejected command */
133 + return -EAGAIN;
134 + }
135 + return written;
136 +}
137 +
138 +static int sflash_at_write(struct bcm47xxsflash *dev, u32 offset, u32 len,
139 + const u8 *buf)
140 +{
141 + struct bcma_drv_cc *cc = dev->bcc;
142 + u32 page, byte, mask;
143 + int ret = 0;
144 +
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)) {
150 + int i = 100;
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)) {
155 + udelay(10);
156 + i--;
157 + }
158 + BUG_ON(!bcma_sflash_poll(dev, offset));
159 + }
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);
164 + }
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);
168 +
169 + return ret;
170 +}
171 +
172 +/* Write len bytes starting at offset into buf. Returns number of bytes
173 + * written. Caller should poll for completion.
174 + */
175 +static int bcma_sflash_write(struct bcm47xxsflash *dev, u32 offset, u32 len,
176 + const u8 *buf)
177 +{
178 + int ret = 0, tries = NUM_RETRIES;
179 + struct bcma_drv_cc *cc = dev->bcc;
180 +
181 + if (!len)
182 + return 0;
183 +
184 + if ((offset + len) > cc->sflash.size)
185 + return -EINVAL;
186 +
187 + switch (cc->capabilities & BCMA_CC_CAP_FLASHT) {
188 + case BCMA_CC_FLASHT_STSER:
189 + do {
190 + ret = sflash_st_write(dev, offset, len, buf);
191 + tries--;
192 + } while (ret == -EAGAIN && tries > 0);
193 +
194 + if (ret == -EAGAIN && tries == 0) {
195 + bcma_info(cc->core->bus, "ST Flash rejected write\n");
196 + ret = -EIO;
197 + }
198 + break;
199 + case BCMA_CC_FLASHT_ATSER:
200 + ret = sflash_at_write(dev, offset, len, buf);
201 + break;
202 + }
203 +
204 + return ret;
205 +}
206 +
207 +/* Erase a region. Returns number of bytes scheduled for erasure.
208 + * Caller should poll for completion.
209 + */
210 +static int bcma_sflash_erase(struct bcm47xxsflash *dev, u32 offset)
211 +{
212 + struct bcma_drv_cc *cc = dev->bcc;
213 +
214 + if (offset >= cc->sflash.size)
215 + return -EINVAL;
216 +
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
223 + * before.
224 + */
225 + if (dev->blocksize < (64 * 1024))
226 + bcma_sflash_cmd(cc, BCMA_CC_FLASHCTL_ST_SSE);
227 + else
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;
234 + }
235 +
236 + return 0;
237 +}
238 +
239 /* Initialize serial flash access */
240 int bcma_sflash_init(struct bcma_drv_cc *cc)
241 {
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;
247 u32 id, id2;
248
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;
258 + sflash->bcc = cc;
259
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
264 @@ -3,6 +3,7 @@
265
266 #include <linux/platform_device.h>
267 #include <linux/gpio.h>
268 +#include <linux/mtd/bcm47xxsflash.h>
269
270 /** ChipCommon core registers. **/
271 #define BCMA_CC_ID 0x0000
272 @@ -519,19 +520,6 @@ struct bcma_pflash {
273 u32 window_size;
274 };
275
276 -#ifdef CONFIG_BCMA_SFLASH
277 -struct bcma_sflash {
278 - bool present;
279 - u32 window;
280 - u32 blocksize;
281 - u16 numblocks;
282 - u32 size;
283 -
284 - struct mtd_info *mtd;
285 - void *priv;
286 -};
287 -#endif
288 -
289 #ifdef CONFIG_BCMA_NFLASH
290 struct mtd_info;
291
292 @@ -565,7 +553,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;
298 #endif
299 #ifdef CONFIG_BCMA_NFLASH
300 struct bcma_nflash nflash;