1 From 980da78179592a3f5f99168bc5af415835aa8c13 Mon Sep 17 00:00:00 2001
2 From: Hauke Mehrtens <hauke@hauke-m.de>
3 Date: Sun, 24 Jul 2011 20:20:36 +0200
4 Subject: [PATCH 15/26] ssb: add serial flash support
7 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
9 drivers/ssb/Kconfig | 6 +
10 drivers/ssb/Makefile | 1 +
11 drivers/ssb/driver_chipcommon_sflash.c | 556 +++++++++++++++++++++++++++++
12 drivers/ssb/driver_mipscore.c | 6 +
13 drivers/ssb/ssb_private.h | 4 +
14 include/linux/ssb/ssb_driver_chipcommon.h | 30 ++-
15 6 files changed, 601 insertions(+), 2 deletions(-)
16 create mode 100644 drivers/ssb/driver_chipcommon_sflash.c
18 --- a/drivers/ssb/Kconfig
19 +++ b/drivers/ssb/Kconfig
20 @@ -137,6 +137,12 @@ config SSB_DRIVER_MIPS
26 + depends on SSB_DRIVER_MIPS
30 # Assumption: We are on embedded, if we compile the MIPS core.
33 --- a/drivers/ssb/Makefile
34 +++ b/drivers/ssb/Makefile
35 @@ -11,6 +11,7 @@ ssb-$(CONFIG_SSB_SDIOHOST) += sdio.o
37 ssb-y += driver_chipcommon.o
38 ssb-y += driver_chipcommon_pmu.o
39 +ssb-$(CONFIG_SSB_SFLASH) += driver_chipcommon_sflash.o
40 ssb-$(CONFIG_SSB_DRIVER_MIPS) += driver_mipscore.o
41 ssb-$(CONFIG_SSB_DRIVER_EXTIF) += driver_extif.o
42 ssb-$(CONFIG_SSB_DRIVER_PCICORE) += driver_pcicore.o
44 +++ b/drivers/ssb/driver_chipcommon_sflash.c
47 + * Broadcom SiliconBackplane chipcommon serial flash interface
49 + * Copyright 2011, Jonas Gorski <jonas.gorski@gmail.com>
50 + * Copyright 2010, Broadcom Corporation
52 + * Licensed under the GNU/GPL. See COPYING for details.
55 +#include <linux/ssb/ssb.h>
56 +#include <linux/ssb/ssb_driver_chipcommon.h>
57 +#include <linux/delay.h>
59 +#include "ssb_private.h"
61 +#define NUM_RETRIES 3
64 +/* Issue a serial flash command */
65 +static inline void ssb_sflash_cmd(struct ssb_chipcommon *cc, u32 opcode)
67 + chipco_write32(cc, SSB_CHIPCO_FLASHCTL,
68 + SSB_CHIPCO_FLASHCTL_START | opcode);
69 + while (chipco_read32(cc, SSB_CHIPCO_FLASHCTL)
70 + & SSB_CHIPCO_FLASHCTL_BUSY)
75 +static inline void ssb_sflash_write_u8(struct ssb_chipcommon *cc,
76 + u32 offset, u8 byte)
78 + chipco_write32(cc, SSB_CHIPCO_FLASHADDR, offset);
79 + chipco_write32(cc, SSB_CHIPCO_FLASHDATA, byte);
82 +/* Initialize serial flash access */
83 +int ssb_sflash_init(struct ssb_chipcommon *cc)
87 + memset(&cc->sflash, 0, sizeof(struct ssb_sflash));
89 + switch (cc->capabilities & SSB_CHIPCO_CAP_FLASHT) {
90 + case SSB_CHIPCO_FLASHT_STSER:
91 + /* Probe for ST chips */
92 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_ST_DP);
93 + chipco_write32(cc, SSB_CHIPCO_FLASHADDR, 0);
94 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_ST_RES);
95 + id = chipco_read32(cc, SSB_CHIPCO_FLASHDATA);
96 + cc->sflash.blocksize = 64 * 1024;
99 + /* ST M25P20 2 Mbit Serial Flash */
100 + cc->sflash.numblocks = 4;
103 + /* ST M25P40 4 Mbit Serial Flash */
104 + cc->sflash.numblocks = 8;
107 + /* ST M25P80 8 Mbit Serial Flash */
108 + cc->sflash.numblocks = 16;
111 + /* ST M25P16 16 Mbit Serial Flash */
112 + cc->sflash.numblocks = 32;
115 + /* ST M25P32 32 Mbit Serial Flash */
116 + cc->sflash.numblocks = 64;
119 + /* ST M25P64 64 Mbit Serial Flash */
120 + cc->sflash.numblocks = 128;
123 + /* ST M25FL128 128 Mbit Serial Flash */
124 + cc->sflash.numblocks = 256;
127 + /* All of the following flashes are SST with
128 + * 4KB subsectors. Others should be added but
129 + * We'll have to revamp the way we identify them
130 + * since RES is not eough to disambiguate them.
132 + cc->sflash.blocksize = 4 * 1024;
133 + chipco_write32(cc, SSB_CHIPCO_FLASHADDR, 1);
134 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_ST_RES);
135 + id2 = chipco_read32(cc, SSB_CHIPCO_FLASHDATA);
138 + /* SST25WF512 512 Kbit Serial Flash */
140 + /* SST25VF512 512 Kbit Serial Flash */
141 + cc->sflash.numblocks = 16;
144 + /* SST25WF010 1 Mbit Serial Flash */
146 + /* SST25VF010 1 Mbit Serial Flash */
147 + cc->sflash.numblocks = 32;
150 + /* SST25WF020 2 Mbit Serial Flash */
152 + /* SST25VF020 2 Mbit Serial Flash */
153 + cc->sflash.numblocks = 64;
156 + /* SST25WF040 4 Mbit Serial Flash */
158 + /* SST25VF040 4 Mbit Serial Flash */
160 + /* SST25VF040B 4 Mbit Serial Flash */
161 + cc->sflash.numblocks = 128;
164 + /* SST25WF080 8 Mbit Serial Flash */
166 + /* SST25VF080B 8 Mbit Serial Flash */
167 + cc->sflash.numblocks = 256;
170 + /* SST25VF016 16 Mbit Serial Flash */
171 + cc->sflash.numblocks = 512;
174 + /* SST25VF032 32 Mbit Serial Flash */
175 + cc->sflash.numblocks = 1024;
178 + /* SST25VF064 64 Mbit Serial Flash */
179 + cc->sflash.numblocks = 2048;
186 + case SSB_CHIPCO_FLASHT_ATSER:
187 + /* Probe for Atmel chips */
188 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_AT_STATUS);
189 + id = chipco_read32(cc, SSB_CHIPCO_FLASHDATA) & 0x3c;
192 + /* Atmel AT45DB011 1Mbit Serial Flash */
193 + cc->sflash.blocksize = 256;
194 + cc->sflash.numblocks = 512;
197 + /* Atmel AT45DB021 2Mbit Serial Flash */
198 + cc->sflash.blocksize = 256;
199 + cc->sflash.numblocks = 1024;
202 + /* Atmel AT45DB041 4Mbit Serial Flash */
203 + cc->sflash.blocksize = 256;
204 + cc->sflash.numblocks = 2048;
207 + /* Atmel AT45DB081 8Mbit Serial Flash */
208 + cc->sflash.blocksize = 256;
209 + cc->sflash.numblocks = 4096;
212 + /* Atmel AT45DB161 16Mbit Serial Flash */
213 + cc->sflash.blocksize = 512;
214 + cc->sflash.numblocks = 4096;
217 + /* Atmel AT45DB321 32Mbit Serial Flash */
218 + cc->sflash.blocksize = 512;
219 + cc->sflash.numblocks = 8192;
222 + /* Atmel AT45DB642 64Mbit Serial Flash */
223 + cc->sflash.blocksize = 1024;
224 + cc->sflash.numblocks = 8192;
230 + cc->sflash.size = cc->sflash.blocksize * cc->sflash.numblocks;
232 + return cc->sflash.size ? 0 : -ENODEV;
235 +/* Read len bytes starting at offset into buf. Returns number of bytes read. */
236 +int ssb_sflash_read(struct ssb_chipcommon *cc, u32 offset, u32 len,
245 + if ((offset + len) > cc->sflash.size)
248 + if ((len >= 4) && (offset & 3))
249 + cnt = 4 - (offset & 3);
250 + else if ((len >= 4) && ((u32)buf & 3))
251 + cnt = 4 - ((u32)buf & 3);
256 + if (cc->dev->id.revision == 12)
257 + from = (u8 *)KSEG1ADDR(SSB_FLASH2 + offset);
259 + from = (u8 *)KSEG0ADDR(SSB_FLASH2 + offset);
264 + for (i = 0; i < cnt; i++) {
273 + *(u32 *)to = readl(from);
282 +/* Poll for command completion. Returns zero when complete. */
283 +int ssb_sflash_poll(struct ssb_chipcommon *cc, u32 offset)
285 + if (offset >= cc->sflash.size)
288 + switch (cc->capabilities & SSB_CHIPCO_CAP_FLASHT) {
289 + case SSB_CHIPCO_FLASHT_STSER:
290 + /* Check for ST Write In Progress bit */
291 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_ST_RDSR);
292 + return chipco_read32(cc, SSB_CHIPCO_FLASHDATA)
293 + & SSB_CHIPCO_FLASHSTA_ST_WIP;
294 + case SSB_CHIPCO_FLASHT_ATSER:
295 + /* Check for Atmel Ready bit */
296 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_AT_STATUS);
297 + return !(chipco_read32(cc, SSB_CHIPCO_FLASHDATA)
298 + & SSB_CHIPCO_FLASHSTA_AT_READY);
305 +static int sflash_st_write(struct ssb_chipcommon *cc, u32 offset, u32 len,
308 + struct ssb_bus *bus = cc->dev->bus;
310 + bool is4712b0 = (bus->chip_id == 0x4712) && (bus->chip_rev == 3);
314 + /* Enable writes */
315 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_ST_WREN);
318 + ssb_sflash_write_u8(cc, offset, *buf++);
319 + /* Set chip select */
320 + chipco_set32(cc, SSB_CHIPCO_GPIOOUT, mask);
321 + /* Issue a page program with the first byte */
322 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_ST_PP);
327 + if ((offset & 255) == 0) {
328 + /* Page boundary, drop cs and return */
329 + chipco_mask32(cc, SSB_CHIPCO_GPIOOUT, ~mask);
331 + if (!ssb_sflash_poll(cc, offset)) {
332 + /* Flash rejected command */
337 + /* Write single byte */
338 + ssb_sflash_cmd(cc, *buf++);
344 + /* All done, drop cs */
345 + chipco_mask32(cc, SSB_CHIPCO_GPIOOUT, ~mask);
347 + if (!ssb_sflash_poll(cc, offset)) {
348 + /* Flash rejected command */
351 + } else if (cc->dev->id.revision >= 20) {
352 + ssb_sflash_write_u8(cc, offset, *buf++);
353 + /* Issue a page program with CSA bit set */
355 + SSB_CHIPCO_FLASHCTL_ST_CSA |
356 + SSB_CHIPCO_FLASHCTL_ST_PP);
361 + if ((offset & 255) == 0) {
362 + /* Page boundary, poll droping cs and return */
363 + chipco_write32(cc, SSB_CHIPCO_FLASHCTL, 0);
365 + if (!ssb_sflash_poll(cc, offset)) {
366 + /* Flash rejected command */
371 + /* Write single byte */
373 + SSB_CHIPCO_FLASHCTL_ST_CSA |
380 + /* All done, drop cs & poll */
381 + chipco_write32(cc, SSB_CHIPCO_FLASHCTL, 0);
383 + if (!ssb_sflash_poll(cc, offset)) {
384 + /* Flash rejected command */
389 + ssb_sflash_write_u8(cc, offset, *buf);
391 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_ST_PP);
396 +static int sflash_at_write(struct ssb_chipcommon *cc, u32 offset, u32 len,
399 + struct ssb_sflash *sfl = &cc->sflash;
400 + u32 page, byte, mask;
402 + mask = sfl->blocksize - 1;
403 + page = (offset & ~mask) << 1;
404 + byte = offset & mask;
405 + /* Read main memory page into buffer 1 */
406 + if (byte || (len < sfl->blocksize)) {
408 + chipco_write32(cc, SSB_CHIPCO_FLASHADDR, page);
409 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_AT_BUF1_LOAD);
410 + /* 250 us for AT45DB321B */
411 + while (i > 0 && ssb_sflash_poll(cc, offset)) {
415 + BUG_ON(!ssb_sflash_poll(cc, offset));
417 + /* Write into buffer 1 */
418 + for (ret = 0; (ret < (int)len) && (byte < sfl->blocksize); ret++) {
419 + ssb_sflash_write_u8(cc, byte++, *buf++);
421 + SSB_CHIPCO_FLASHCTL_AT_BUF1_WRITE);
423 + /* Write buffer 1 into main memory page */
424 + chipco_write32(cc, SSB_CHIPCO_FLASHADDR, page);
425 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_AT_BUF1_PROGRAM);
430 +/* Write len bytes starting at offset into buf. Returns number of bytes
431 + * written. Caller should poll for completion.
433 +int ssb_sflash_write(struct ssb_chipcommon *cc, u32 offset, u32 len,
436 + struct ssb_sflash *sfl;
437 + int ret = 0, tries = NUM_RETRIES;
442 + if ((offset + len) > cc->sflash.size)
446 + switch (cc->capabilities & SSB_CHIPCO_CAP_FLASHT) {
447 + case SSB_CHIPCO_FLASHT_STSER:
449 + ret = sflash_st_write(cc, offset, len, buf);
451 + } while (ret == -EAGAIN && tries > 0);
453 + if (ret == -EAGAIN && tries == 0) {
454 + pr_info("ST Flash rejected write\n");
458 + case SSB_CHIPCO_FLASHT_ATSER:
459 + ret = sflash_at_write(cc, offset, len, buf);
466 +/* Erase a region. Returns number of bytes scheduled for erasure.
467 + * Caller should poll for completion.
469 +int ssb_sflash_erase(struct ssb_chipcommon *cc, u32 offset)
471 + struct ssb_sflash *sfl;
473 + if (offset >= cc->sflash.size)
477 + switch (cc->capabilities & SSB_CHIPCO_CAP_FLASHT) {
478 + case SSB_CHIPCO_FLASHT_STSER:
479 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_ST_WREN);
480 + chipco_write32(cc, SSB_CHIPCO_FLASHADDR, offset);
481 + /* Newer flashes have "sub-sectors" which can be erased independently
482 + * with a new command: ST_SSE. The ST_SE command erases 64KB just as
485 + ssb_sflash_cmd(cc, (sfl->blocksize < (64 * 1024)) ? SSB_CHIPCO_FLASHCTL_ST_SSE : SSB_CHIPCO_FLASHCTL_ST_SE);
486 + return sfl->blocksize;
487 + case SSB_CHIPCO_FLASHT_ATSER:
488 + chipco_write32(cc, SSB_CHIPCO_FLASHADDR, offset << 1);
489 + ssb_sflash_cmd(cc, SSB_CHIPCO_FLASHCTL_AT_PAGE_ERASE);
490 + return sfl->blocksize;
497 + * writes the appropriate range of flash, a NULL buf simply erases
498 + * the region of flash
500 +int ssb_sflash_commit(struct ssb_chipcommon *cc, u32 offset, u32 len,
503 + struct ssb_sflash *sfl;
504 + u8 *block = NULL, *cur_ptr, *blk_ptr;
505 + u32 blocksize = 0, mask, cur_offset, cur_length, cur_retlen, remainder;
506 + u32 blk_offset, blk_len, copied;
507 + int bytes, ret = 0;
509 + /* Check address range */
514 + if ((offset + len) > sfl->size)
517 + blocksize = sfl->blocksize;
518 + mask = blocksize - 1;
520 + /* Allocate a block of mem */
521 + block = kmalloc(blocksize, GFP_KERNEL);
527 + cur_offset = offset & ~mask;
528 + cur_length = blocksize;
531 + remainder = blocksize - (offset & mask);
532 + if (len < remainder)
535 + cur_retlen = remainder;
537 + /* buf == NULL means erase only */
539 + /* Copy existing data into holding block if necessary */
540 + if ((offset & mask) || (len < blocksize)) {
541 + blk_offset = cur_offset;
542 + blk_len = cur_length;
545 + /* Copy entire block */
547 + copied = ssb_sflash_read(cc,
550 + blk_offset += copied;
556 + /* Copy input data into holding block */
557 + memcpy(cur_ptr + (offset & mask), buf, cur_retlen);
561 + ret = ssb_sflash_erase(cc, cur_offset);
565 + while (ssb_sflash_poll(cc, cur_offset));
567 + /* buf == NULL means erase only */
569 + offset += cur_retlen;
574 + /* Write holding block */
575 + while (cur_length > 0) {
576 + bytes = ssb_sflash_write(cc, cur_offset,
577 + cur_length, cur_ptr);
584 + while (ssb_sflash_poll(cc, cur_offset))
587 + cur_offset += bytes;
588 + cur_length -= bytes;
592 + offset += cur_retlen;
602 --- a/drivers/ssb/driver_mipscore.c
603 +++ b/drivers/ssb/driver_mipscore.c
604 @@ -203,7 +203,13 @@ static void ssb_mips_flash_detect(struct
605 switch (bus->chipco.capabilities & SSB_CHIPCO_CAP_FLASHT) {
606 case SSB_CHIPCO_FLASHT_STSER:
607 case SSB_CHIPCO_FLASHT_ATSER:
608 +#ifdef CONFIG_SSB_SFLASH
609 + pr_info("found serial flash.\n");
610 + bus->chipco.flash_type = SSB_SFLASH;
611 + ssb_sflash_init(&bus->chipco);
613 pr_info("serial flash not supported.\n");
614 +#endif /* CONFIG_SSB_SFLASH */
616 case SSB_CHIPCO_FLASHT_PARA:
617 pr_info("found parallel flash.\n");
618 --- a/drivers/ssb/ssb_private.h
619 +++ b/drivers/ssb/ssb_private.h
620 @@ -192,6 +192,10 @@ extern int ssb_devices_freeze(struct ssb
621 extern int ssb_devices_thaw(struct ssb_freeze_context *ctx);
624 +#ifdef CONFIG_SSB_SFLASH
625 +/* driver_chipcommon_sflash.c */
626 +int ssb_sflash_init(struct ssb_chipcommon *cc);
627 +#endif /* CONFIG_SSB_SFLASH */
629 /* b43_pci_bridge.c */
630 #ifdef CONFIG_SSB_B43_PCI_BRIDGE
631 --- a/include/linux/ssb/ssb_driver_chipcommon.h
632 +++ b/include/linux/ssb/ssb_driver_chipcommon.h
634 #define SSB_CHIPCO_FLASHCTL_ST_PP 0x0302 /* Page Program */
635 #define SSB_CHIPCO_FLASHCTL_ST_SE 0x02D8 /* Sector Erase */
636 #define SSB_CHIPCO_FLASHCTL_ST_BE 0x00C7 /* Bulk Erase */
637 -#define SSB_CHIPCO_FLASHCTL_ST_DP 0x00B9 /* Deep Power-down */
638 -#define SSB_CHIPCO_FLASHCTL_ST_RSIG 0x03AB /* Read Electronic Signature */
639 +#define SSB_CHIPCO_FLASHCTL_ST_DP 0x00D9 /* Deep Power-down */
640 +#define SSB_CHIPCO_FLASHCTL_ST_RES 0x03AB /* Read Electronic Signature */
641 +#define SSB_CHIPCO_FLASHCTL_ST_CSA 0x1000 /* Keep chip select asserted */
642 +#define SSB_CHIPCO_FLASHCTL_ST_SSE 0x0220 /* Sub-sector Erase */
644 /* Status register bits for ST flashes */
645 #define SSB_CHIPCO_FLASHSTA_ST_WIP 0x01 /* Write In Progress */
646 @@ -585,6 +587,7 @@ struct ssb_chipcommon_pmu {
647 #ifdef CONFIG_SSB_DRIVER_MIPS
648 enum ssb_flash_type {
654 @@ -592,6 +595,14 @@ struct ssb_pflash {
659 +#ifdef CONFIG_SSB_SFLASH
661 + u32 blocksize; /* Block size */
662 + u32 numblocks; /* Number of blocks */
663 + u32 size; /* Total size in bytes */
665 +#endif /* CONFIG_SSB_SFLASH */
666 #endif /* CONFIG_SSB_DRIVER_MIPS */
668 struct ssb_chipcommon {
669 @@ -605,6 +616,9 @@ struct ssb_chipcommon {
670 enum ssb_flash_type flash_type;
672 struct ssb_pflash pflash;
673 +#ifdef CONFIG_SSB_SFLASH
674 + struct ssb_sflash sflash;
675 +#endif /* CONFIG_SSB_SFLASH */
677 #endif /* CONFIG_SSB_DRIVER_MIPS */
679 @@ -666,6 +680,18 @@ extern int ssb_chipco_serial_init(struct
680 struct ssb_serial_port *ports);
681 #endif /* CONFIG_SSB_SERIAL */
683 +#ifdef CONFIG_SSB_SFLASH
684 +/* Chipcommon sflash support. */
685 +int ssb_sflash_read(struct ssb_chipcommon *cc, u32 offset, u32 len,
687 +int ssb_sflash_poll(struct ssb_chipcommon *cc, u32 offset);
688 +int ssb_sflash_write(struct ssb_chipcommon *cc, u32 offset, u32 len,
690 +int ssb_sflash_erase(struct ssb_chipcommon *cc, u32 offset);
691 +int ssb_sflash_commit(struct ssb_chipcommon *cc, u32 offset, u32 len,
693 +#endif /* CONFIG_SSB_SFLASH */
696 extern void ssb_pmu_init(struct ssb_chipcommon *cc);