1212#include <string.h>
1313#include "flash.h"
1414#include "eboot_command.h"
15+ #include "spi_vendors.h"
1516#include <uzlib.h>
1617
1718extern unsigned char _gzip_dict ;
@@ -114,12 +115,10 @@ int uzlib_flash_read_cb(struct uzlib_uncomp *m)
114115}
115116
116117unsigned char gzip_dict [32768 ];
117- uint8_t buffer2 [FLASH_SECTOR_SIZE ]; // no room for this on the stack
118118
119119int copy_raw (const uint32_t src_addr ,
120120 const uint32_t dst_addr ,
121- const uint32_t size ,
122- const bool verify )
121+ const uint32_t size )
123122{
124123 // require regions to be aligned
125124 if ((src_addr & 0xfff ) != 0 ||
@@ -159,10 +158,8 @@ int copy_raw(const uint32_t src_addr,
159158 gzip = true;
160159 }
161160 while (left > 0 ) {
162- if (!verify ) {
163- if (SPIEraseSector (daddr /buffer_size )) {
164- return 2 ;
165- }
161+ if (SPIEraseSector (daddr /buffer_size )) {
162+ return 2 ;
166163 }
167164 if (!gzip ) {
168165 if (SPIRead (saddr , buffer , buffer_size )) {
@@ -182,17 +179,8 @@ int copy_raw(const uint32_t src_addr,
182179 buffer [i ] = 0xff ;
183180 }
184181 }
185- if (verify ) {
186- if (SPIRead (daddr , buffer2 , buffer_size )) {
187- return 4 ;
188- }
189- if (memcmp (buffer , buffer2 , buffer_size )) {
190- return 9 ;
191- }
192- } else {
193- if (SPIWrite (daddr , buffer , buffer_size )) {
194- return 4 ;
195- }
182+ if (SPIWrite (daddr , buffer , buffer_size )) {
183+ return 4 ;
196184 }
197185 saddr += buffer_size ;
198186 daddr += buffer_size ;
@@ -202,6 +190,29 @@ int copy_raw(const uint32_t src_addr,
202190 return 0 ;
203191}
204192
193+ //#define XMC_SUPPORT
194+ #ifdef XMC_SUPPORT
195+ // Define a few SPI0 registers we need access to
196+ #define ESP8266_REG (addr ) *((volatile uint32_t *)(0x60000000+(addr)))
197+ #define SPI0CMD ESP8266_REG(0x200)
198+ #define SPI0CLK ESP8266_REG(0x218)
199+ #define SPI0C ESP8266_REG(0x208)
200+ #define SPI0W0 ESP8266_REG(0x240)
201+
202+ #define SPICMDRDID (1 << 28)
203+
204+ /* spi_flash_get_id()
205+ Returns the flash chip ID - same as the SDK function.
206+ We need our own version as the SDK isn't available here.
207+ */
208+ uint32_t __attribute__((noinline )) spi_flash_get_id () {
209+ SPI0W0 = 0 ;
210+ SPI0CMD = SPICMDRDID ;
211+ while (SPI0CMD ) {}
212+ return SPI0W0 ;
213+ }
214+ #endif // XMC_SUPPORT
215+
205216int main ()
206217{
207218 int res = 9 ;
@@ -224,20 +235,47 @@ int main()
224235 if (cmd .action == ACTION_COPY_RAW ) {
225236 ets_putc ('c' ); ets_putc ('p' ); ets_putc (':' );
226237
238+ #ifdef XMC_SUPPORT
239+ // save the flash access speed registers
240+ uint32_t spi0clk = SPI0CLK ;
241+ uint32_t spi0c = SPI0C ;
242+
243+ uint32_t vendor = spi_flash_get_id () & 0x000000ff ;
244+ if (vendor == SPI_FLASH_VENDOR_XMC ) {
245+ uint32_t flashinfo = 0 ;
246+ if (SPIRead (0 , & flashinfo , 4 )) {
247+ // failed to read the configured flash speed.
248+ // Do not change anything,
249+ } else {
250+ // select an appropriate flash speed
251+ // Register values are those used by ROM
252+ switch ((flashinfo >> 24 ) & 0x0f ) {
253+ case 0x0 : // 40MHz, slow to 20
254+ case 0x1 : // 26MHz, slow to 20
255+ SPI0CLK = 0x00003043 ;
256+ SPI0C = 0x00EAA313 ;
257+ break ;
258+ case 0x2 : // 20MHz, no change
259+ break ;
260+ case 0xf : // 80MHz, slow to 26
261+ SPI0CLK = 0x00002002 ;
262+ SPI0C = 0x00EAA202 ;
263+ break ;
264+ default :
265+ break ;
266+ }
267+ }
268+ }
269+ #endif // XMC_SUPPORT
227270 ets_wdt_disable ();
228- res = copy_raw (cmd .args [0 ], cmd .args [1 ], cmd .args [2 ], false );
271+ res = copy_raw (cmd .args [0 ], cmd .args [1 ], cmd .args [2 ]);
229272 ets_wdt_enable ();
230-
231- ets_putc ('0' + res ); ets_putc ('\n' );
232-
233- // Verify the copy
234- ets_putc ('c' ); ets_putc ('m' ); ets_putc ('p' ); ets_putc (':' );
235- if (res == 0 ) {
236- ets_wdt_disable ();
237- res = copy_raw (cmd .args [0 ], cmd .args [1 ], cmd .args [2 ], true);
238- ets_wdt_enable ();
239- }
240-
273+
274+ #ifdef XMC_SUPPORT
275+ // restore the saved flash access speed registers
276+ SPI0CLK = spi0clk ;
277+ SPI0C = spi0c ;
278+ #endif
241279 ets_putc ('0' + res ); ets_putc ('\n' );
242280 if (res == 0 ) {
243281 cmd .action = ACTION_LOAD_APP ;
0 commit comments