* Avoid using FASTSPI_TX to reduce size of object code.

* Use off_t to avoid overflows.
This commit is contained in:
bg- 2006-08-10 16:42:11 +00:00
parent 5dacf0be11
commit 7f7a6ae577

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* @(#)$Id: xmem.c,v 1.1 2006/08/02 14:44:46 bg- Exp $ * @(#)$Id: xmem.c,v 1.2 2006/08/10 16:42:11 bg- Exp $
*/ */
/* /*
@ -47,6 +47,12 @@
#include "dev/spi.h" #include "dev/spi.h"
#include "dev/xmem.h" #include "dev/xmem.h"
#if 0
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...) do {} while (0)
#endif
#define SPI_FLASH_INS_WREN 0x06 #define SPI_FLASH_INS_WREN 0x06
#define SPI_FLASH_INS_WRDI 0x04 #define SPI_FLASH_INS_WRDI 0x04
#define SPI_FLASH_INS_RDSR 0x05 #define SPI_FLASH_INS_RDSR 0x05
@ -59,6 +65,12 @@
#define SPI_FLASH_INS_DP 0xb9 #define SPI_FLASH_INS_DP 0xb9
#define SPI_FLASH_INS_RES 0xab #define SPI_FLASH_INS_RES 0xab
static void
spi_tx(unsigned byte)
{
FASTSPI_TX(byte);
}
static void static void
write_enable(void) write_enable(void)
{ {
@ -67,7 +79,7 @@ write_enable(void)
s = splhigh(); s = splhigh();
SPI_FLASH_ENABLE(); SPI_FLASH_ENABLE();
FASTSPI_TX(SPI_FLASH_INS_WREN); spi_tx(SPI_FLASH_INS_WREN);
SPI_FLASH_DISABLE(); SPI_FLASH_DISABLE();
splx(s); splx(s);
@ -83,7 +95,7 @@ read_status_register(void)
s = splhigh(); s = splhigh();
SPI_FLASH_ENABLE(); SPI_FLASH_ENABLE();
FASTSPI_TX(SPI_FLASH_INS_RDSR); spi_tx(SPI_FLASH_INS_RDSR);
FASTSPI_CLEAR_RX(); FASTSPI_CLEAR_RX();
FASTSPI_RX(u); FASTSPI_RX(u);
@ -95,10 +107,10 @@ read_status_register(void)
} }
/* /*
* Wait for a write operation to finish. * Wait for a write/erase operation to finish.
*/ */
static unsigned static unsigned
eeprom_finish(void) wait_ready(void)
{ {
unsigned u; unsigned u;
do { do {
@ -108,24 +120,24 @@ eeprom_finish(void)
} }
/* /*
* Erase 64kBytes of date, it takes about 1s before WIP goes low! * Erase 64k bytes of data. It takes about 1s before WIP goes low!
*/ */
static void static void
sector_erase(unsigned long offset) erase_sector(off_t offset)
{ {
int s; int s;
eeprom_finish(); wait_ready();
write_enable(); write_enable();
s = splhigh(); s = splhigh();
SPI_FLASH_ENABLE(); SPI_FLASH_ENABLE();
FASTSPI_TX(SPI_FLASH_INS_SE); spi_tx(SPI_FLASH_INS_SE);
FASTSPI_TX(offset >> 16); /* MSB */ spi_tx(offset >> 16); /* MSB */
FASTSPI_TX(offset >> 8); spi_tx(offset >> 8);
FASTSPI_TX(offset >> 0); /* LSB */ spi_tx(offset >> 0); /* LSB */
SPI_FLASH_DISABLE(); SPI_FLASH_DISABLE();
splx(s); splx(s);
@ -153,15 +165,15 @@ xmem_pread(void *_p, int size, off_t offset)
const unsigned char *end = p + size; const unsigned char *end = p + size;
int s; int s;
eeprom_finish(); wait_ready();
s = splhigh(); s = splhigh();
SPI_FLASH_ENABLE(); SPI_FLASH_ENABLE();
FASTSPI_TX(SPI_FLASH_INS_READ); spi_tx(SPI_FLASH_INS_READ);
FASTSPI_TX(offset >> 16); /* MSB */ spi_tx(offset >> 16); /* MSB */
FASTSPI_TX(offset >> 8); spi_tx(offset >> 8);
FASTSPI_TX(offset >> 0); /* LSB */ spi_tx(offset >> 0); /* LSB */
FASTSPI_CLEAR_RX(); FASTSPI_CLEAR_RX();
for(; p < end; p++) { for(; p < end; p++) {
@ -176,25 +188,25 @@ xmem_pread(void *_p, int size, off_t offset)
} }
static const char * static const char *
eeprom_page_program(unsigned long offset, const unsigned char *p, int nbytes) program_page(off_t offset, const unsigned char *p, int nbytes)
{ {
const unsigned char *end = p + nbytes; const unsigned char *end = p + nbytes;
int s; int s;
eeprom_finish(); wait_ready();
write_enable(); write_enable();
s = splhigh(); s = splhigh();
SPI_FLASH_ENABLE(); SPI_FLASH_ENABLE();
FASTSPI_TX(SPI_FLASH_INS_PP); spi_tx(SPI_FLASH_INS_PP);
FASTSPI_TX(offset >> 16); /* MSB */ spi_tx(offset >> 16); /* MSB */
FASTSPI_TX(offset >> 8); spi_tx(offset >> 8);
FASTSPI_TX(offset >> 0); /* LSB */ spi_tx(offset >> 0); /* LSB */
for(; p < end; p++) { for(; p < end; p++) {
FASTSPI_TX(~*p); spi_tx(~*p);
} }
SPI_FLASH_DISABLE(); SPI_FLASH_DISABLE();
@ -206,16 +218,15 @@ eeprom_page_program(unsigned long offset, const unsigned char *p, int nbytes)
int int
xmem_pwrite(const void *_buf, int size, off_t addr) xmem_pwrite(const void *_buf, int size, off_t addr)
{ {
const unsigned char *buf = _buf; const unsigned char *p = _buf;
unsigned i, next_page, end; const off_t end = addr + size;
const char *p = buf; off_t i, next_page;
end = addr + size;
for(i = addr; i < end;) { for(i = addr; i < end;) {
next_page = (i | 0xff) + 1; next_page = (i | 0xff) + 1;
if(next_page > end) if(next_page > end)
next_page = end; next_page = end;
p = eeprom_page_program(i, p, next_page - i); p = program_page(i, p, next_page - i);
i = next_page; i = next_page;
} }
return size; return size;
@ -227,17 +238,17 @@ xmem_erase(long size, off_t addr)
off_t end = addr + size; off_t end = addr + size;
if(size % XMEM_ERASE_UNIT_SIZE != 0) { if(size % XMEM_ERASE_UNIT_SIZE != 0) {
printf("xmem_erase: bad size\n"); PRINTF("xmem_erase: bad size\n");
return -1; return -1;
} }
if(addr % XMEM_ERASE_UNIT_SIZE != 0) { if(addr % XMEM_ERASE_UNIT_SIZE != 0) {
printf("xmem_erase: bad offset\n"); PRINTF("xmem_erase: bad offset\n");
return -1; return -1;
} }
for (; addr < end; addr += XMEM_ERASE_UNIT_SIZE) for (; addr < end; addr += XMEM_ERASE_UNIT_SIZE)
sector_erase(addr); erase_sector(addr);
return size; return size;
} }