spiflash: use SoC defines, add write_to_flash function
authorSebastien Bourdeauducq <sb@m-labs.hk>
Mon, 27 Apr 2015 05:42:32 +0000 (13:42 +0800)
committerSebastien Bourdeauducq <sb@m-labs.hk>
Mon, 27 Apr 2015 05:42:32 +0000 (13:42 +0800)
software/include/base/spiflash.h
software/libbase/spiflash.c
targets/kc705.py

index 2708cdb3b958540ad5db8e5b45776c87e919076a..ce5a1d9bbadae4109571aee8549dbbac3721515f 100644 (file)
@@ -3,5 +3,6 @@
 
 void write_to_flash_page(unsigned int addr, unsigned char *c, unsigned int len);
 void erase_flash_sector(unsigned int addr);
+void write_to_flash(unsigned int addr, unsigned char *c, unsigned int len);
 
 #endif /* __SPIFLASH_H */
index b2080248f93e34be2dcfc0b6a075b4219f0fb0d1..3d4e3204aa543b478a875fe5ecfdcb7f58b63a83 100644 (file)
@@ -1,25 +1,20 @@
 #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);
@@ -76,7 +71,7 @@ static void wait_for_device_ready(void)
 
 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);
 
@@ -98,8 +93,8 @@ void write_to_flash_page(unsigned int addr, unsigned char *c, unsigned int len)
 {
     unsigned int i;
 
-    if(len > PAGE_SIZE)
-        len = PAGE_SIZE;
+    if(len > SPIFLASH_PAGE_SIZE)
+        len = SPIFLASH_PAGE_SIZE;
 
     spiflash_bitbang_en_write(1);
 
@@ -120,4 +115,27 @@ void write_to_flash_page(unsigned int addr, unsigned char *c, unsigned int len)
     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 */
index b0d477fd6abc3fbede86aa2235a86aa12cba3821..4cd792d66b0c461628743a2c050f868d10a63a46 100644 (file)
@@ -98,6 +98,8 @@ class BaseSoC(SDRAMSoC):
                                       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)