#include <generated/csr.h>
-#include <spiflash.h>
+#if (defined CSR_SPIFLASH_BASE && defined SPIFLASH_PAGE_SIZE)
-#ifdef CSR_SPIFLASH_BASE
+#include <spiflash.h>
-#define PAGE_PROGRAM_CMD (0x02)
-#define WRDI_CMD (0x04)
-#define RDSR_CMD (0x05)
-#define WREN_CMD (0x06)
-#define SE_CMD (0x20)
+#define PAGE_PROGRAM_CMD (0x02)
+#define WRDI_CMD (0x04)
+#define RDSR_CMD (0x05)
+#define WREN_CMD (0x06)
+#define SE_CMD (0x20)
-#define BITBANG_CLK (1 << 1)
+#define BITBANG_CLK (1 << 1)
#define BITBANG_CS_N (1 << 2)
#define BITBANG_DQ_INPUT (1 << 3)
-#define SR_WIP (1)
-
-#define PAGE_SIZE (256)
-#define PAGE_MASK (PAGE_SIZE - 1)
-#define SECTOR_SIZE (4096)
-#define SECTOR_MASK (SECTOR_SIZE - 1)
+#define SR_WIP (1)
static void flash_write_byte(unsigned char b);
static void flash_write_addr(unsigned int addr);
void erase_flash_sector(unsigned int addr)
{
- unsigned int sector_addr = addr & ~(SECTOR_MASK);
+ unsigned int sector_addr = addr & ~(SPIFLASH_SECTOR_SIZE - 1);
spiflash_bitbang_en_write(1);
{
unsigned int i;
- if(len > PAGE_SIZE)
- len = PAGE_SIZE;
+ if(len > SPIFLASH_PAGE_SIZE)
+ len = SPIFLASH_PAGE_SIZE;
spiflash_bitbang_en_write(1);
spiflash_bitbang_en_write(0);
}
-#endif
+#define SPIFLASH_PAGE_MASK (SPIFLASH_PAGE_SIZE - 1)
+
+void write_to_flash(unsigned int addr, unsigned char *c, unsigned int len)
+{
+ unsigned int written = 0;
+
+ if(addr & SPIFLASH_PAGE_MASK) {
+ written = min(SPIFLASH_PAGE_SIZE - (addr & SPIFLASH_PAGE_MASK), len);
+ write_to_flash_page(addr, c, written);
+ c += written;
+ addr += written;
+ len -= written;
+ }
+
+ while(len > 0) {
+ written = min(len, SPIFLASH_PAGE_SIZE);
+ write_to_flash_page(addr, c, written);
+ c += written;
+ addr += written;
+ len -= written;
+ }
+}
+
+#endif /* CSR_SPIFLASH_BASE && SPIFLASH_PAGE_SIZE */
i_CLK=0, i_GSR=0, i_GTS=0, i_KEYCLEARB=0, i_PACK=0,
i_USRCCLKO=spiflash_pads.clk, i_USRCCLKTS=0, i_USRDONEO=1, i_USRDONETS=1)
self.submodules.spiflash = spiflash.SpiFlash(spiflash_pads, dummy=11, div=2)
+ self.add_constant("SPIFLASH_PAGE_SIZE", 256)
+ self.add_constant("SPIFLASH_SECTOR_SIZE", 0x10000)
self.flash_boot_address = 0xb00000
self.register_rom(self.spiflash.bus)