2006-08-02 14:44:46 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2006, Swedish Institute of Computer Science
|
2007-11-10 20:45:29 +00:00
|
|
|
|
* All rights reserved.
|
2006-08-02 14:44:46 +00:00
|
|
|
|
*
|
2007-11-10 20:45:29 +00:00
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
|
* without specific prior written permission.
|
2006-08-02 14:44:46 +00:00
|
|
|
|
*
|
2007-11-10 20:45:29 +00:00
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
2006-08-02 14:44:46 +00:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2007-11-10 20:45:29 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file
|
|
|
|
|
* Device driver for the ST M25P80 40MHz 1Mbyte external memory.
|
|
|
|
|
* \author
|
|
|
|
|
* Bj<EFBFBD>rn Gr<EFBFBD>nvall <bg@sics.se>
|
2006-08-02 14:44:46 +00:00
|
|
|
|
*
|
2007-11-10 20:45:29 +00:00
|
|
|
|
* Data is written bit inverted (~-operator) to flash so that
|
|
|
|
|
* unwritten data will read as zeros (UNIX style).
|
2006-08-02 14:44:46 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2011-09-11 15:18:02 +00:00
|
|
|
|
|
|
|
|
|
#include "contiki.h"
|
2006-08-02 14:44:46 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "dev/spi.h"
|
|
|
|
|
#include "dev/xmem.h"
|
2008-01-21 10:28:44 +00:00
|
|
|
|
#include "dev/watchdog.h"
|
2006-08-02 14:44:46 +00:00
|
|
|
|
|
2006-08-10 16:42:11 +00:00
|
|
|
|
#if 0
|
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
|
#else
|
|
|
|
|
#define PRINTF(...) do {} while (0)
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-08-02 14:44:46 +00:00
|
|
|
|
#define SPI_FLASH_INS_WREN 0x06
|
|
|
|
|
#define SPI_FLASH_INS_WRDI 0x04
|
|
|
|
|
#define SPI_FLASH_INS_RDSR 0x05
|
|
|
|
|
#define SPI_FLASH_INS_WRSR 0x01
|
|
|
|
|
#define SPI_FLASH_INS_READ 0x03
|
|
|
|
|
#define SPI_FLASH_INS_FAST_READ 0x0b
|
|
|
|
|
#define SPI_FLASH_INS_PP 0x02
|
|
|
|
|
#define SPI_FLASH_INS_SE 0xd8
|
|
|
|
|
#define SPI_FLASH_INS_BE 0xc7
|
|
|
|
|
#define SPI_FLASH_INS_DP 0xb9
|
|
|
|
|
#define SPI_FLASH_INS_RES 0xab
|
2007-11-10 20:45:29 +00:00
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-08-10 16:42:11 +00:00
|
|
|
|
static void
|
2006-08-02 14:44:46 +00:00
|
|
|
|
write_enable(void)
|
|
|
|
|
{
|
|
|
|
|
int s;
|
|
|
|
|
|
|
|
|
|
s = splhigh();
|
|
|
|
|
SPI_FLASH_ENABLE();
|
|
|
|
|
|
2010-06-23 10:18:05 +00:00
|
|
|
|
SPI_WRITE(SPI_FLASH_INS_WREN);
|
2006-08-02 14:44:46 +00:00
|
|
|
|
|
|
|
|
|
SPI_FLASH_DISABLE();
|
|
|
|
|
splx(s);
|
|
|
|
|
}
|
2007-11-10 20:45:29 +00:00
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-08-02 14:44:46 +00:00
|
|
|
|
static unsigned
|
|
|
|
|
read_status_register(void)
|
|
|
|
|
{
|
|
|
|
|
unsigned char u;
|
|
|
|
|
|
|
|
|
|
int s;
|
|
|
|
|
|
|
|
|
|
s = splhigh();
|
|
|
|
|
SPI_FLASH_ENABLE();
|
|
|
|
|
|
2010-06-23 10:18:05 +00:00
|
|
|
|
SPI_WRITE(SPI_FLASH_INS_RDSR);
|
2006-08-02 14:44:46 +00:00
|
|
|
|
|
2010-06-23 10:18:05 +00:00
|
|
|
|
SPI_FLUSH();
|
|
|
|
|
SPI_READ(u);
|
2006-08-02 14:44:46 +00:00
|
|
|
|
|
|
|
|
|
SPI_FLASH_DISABLE();
|
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
|
|
return u;
|
|
|
|
|
}
|
2007-11-10 20:45:29 +00:00
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-08-02 14:44:46 +00:00
|
|
|
|
/*
|
2006-08-10 16:42:11 +00:00
|
|
|
|
* Wait for a write/erase operation to finish.
|
2006-08-02 14:44:46 +00:00
|
|
|
|
*/
|
|
|
|
|
static unsigned
|
2006-08-10 16:42:11 +00:00
|
|
|
|
wait_ready(void)
|
2006-08-02 14:44:46 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned u;
|
|
|
|
|
do {
|
|
|
|
|
u = read_status_register();
|
2011-01-18 14:03:55 +00:00
|
|
|
|
watchdog_periodic();
|
2006-08-02 14:44:46 +00:00
|
|
|
|
} while(u & 0x01); /* WIP=1, write in progress */
|
|
|
|
|
return u;
|
|
|
|
|
}
|
2007-11-10 20:45:29 +00:00
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-08-02 14:44:46 +00:00
|
|
|
|
/*
|
2006-08-10 16:42:11 +00:00
|
|
|
|
* Erase 64k bytes of data. It takes about 1s before WIP goes low!
|
2006-08-02 14:44:46 +00:00
|
|
|
|
*/
|
|
|
|
|
static void
|
2008-07-03 23:12:10 +00:00
|
|
|
|
erase_sector(unsigned long offset)
|
2006-08-02 14:44:46 +00:00
|
|
|
|
{
|
|
|
|
|
int s;
|
|
|
|
|
|
2011-01-18 14:03:55 +00:00
|
|
|
|
wait_ready();
|
2006-08-02 14:44:46 +00:00
|
|
|
|
write_enable();
|
|
|
|
|
|
|
|
|
|
s = splhigh();
|
|
|
|
|
SPI_FLASH_ENABLE();
|
|
|
|
|
|
2010-06-23 10:18:05 +00:00
|
|
|
|
SPI_WRITE_FAST(SPI_FLASH_INS_SE);
|
|
|
|
|
SPI_WRITE_FAST(offset >> 16); /* MSB */
|
|
|
|
|
SPI_WRITE_FAST(offset >> 8);
|
|
|
|
|
SPI_WRITE_FAST(offset >> 0); /* LSB */
|
2009-09-07 11:31:26 +00:00
|
|
|
|
SPI_WAITFORTx_ENDED();
|
2006-08-02 14:44:46 +00:00
|
|
|
|
|
|
|
|
|
SPI_FLASH_DISABLE();
|
|
|
|
|
splx(s);
|
|
|
|
|
}
|
2007-11-10 20:45:29 +00:00
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-08-02 14:44:46 +00:00
|
|
|
|
/*
|
|
|
|
|
* Initialize external flash *and* SPI bus!
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
xmem_init(void)
|
|
|
|
|
{
|
2010-10-12 22:55:11 +00:00
|
|
|
|
int s;
|
2006-08-02 14:44:46 +00:00
|
|
|
|
spi_init();
|
|
|
|
|
|
|
|
|
|
P4DIR |= BV(FLASH_CS) | BV(FLASH_HOLD) | BV(FLASH_PWR);
|
|
|
|
|
P4OUT |= BV(FLASH_PWR); /* P4.3 Output, turn on power! */
|
|
|
|
|
|
2010-10-12 22:55:11 +00:00
|
|
|
|
/* Release from Deep Power-down */
|
|
|
|
|
s = splhigh();
|
|
|
|
|
SPI_FLASH_ENABLE();
|
|
|
|
|
SPI_WRITE_FAST(SPI_FLASH_INS_RES);
|
|
|
|
|
SPI_WAITFORTx_ENDED();
|
2006-08-02 14:44:46 +00:00
|
|
|
|
SPI_FLASH_DISABLE(); /* Unselect flash. */
|
2010-10-12 22:55:11 +00:00
|
|
|
|
splx(s);
|
|
|
|
|
|
2006-08-02 14:44:46 +00:00
|
|
|
|
SPI_FLASH_UNHOLD();
|
|
|
|
|
}
|
2007-11-10 20:45:29 +00:00
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-08-02 14:44:46 +00:00
|
|
|
|
int
|
2008-07-03 23:12:10 +00:00
|
|
|
|
xmem_pread(void *_p, int size, unsigned long offset)
|
2006-08-02 14:44:46 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned char *p = _p;
|
|
|
|
|
const unsigned char *end = p + size;
|
|
|
|
|
int s;
|
2011-01-18 14:03:55 +00:00
|
|
|
|
|
2006-08-10 16:42:11 +00:00
|
|
|
|
wait_ready();
|
2006-08-02 14:44:46 +00:00
|
|
|
|
|
2009-05-11 15:26:24 +00:00
|
|
|
|
ENERGEST_ON(ENERGEST_TYPE_FLASH_READ);
|
|
|
|
|
|
2006-08-02 14:44:46 +00:00
|
|
|
|
s = splhigh();
|
|
|
|
|
SPI_FLASH_ENABLE();
|
|
|
|
|
|
2010-06-23 10:18:05 +00:00
|
|
|
|
SPI_WRITE_FAST(SPI_FLASH_INS_READ);
|
|
|
|
|
SPI_WRITE_FAST(offset >> 16); /* MSB */
|
|
|
|
|
SPI_WRITE_FAST(offset >> 8);
|
|
|
|
|
SPI_WRITE_FAST(offset >> 0); /* LSB */
|
2009-09-07 11:31:26 +00:00
|
|
|
|
SPI_WAITFORTx_ENDED();
|
2006-08-02 14:44:46 +00:00
|
|
|
|
|
2010-06-23 10:18:05 +00:00
|
|
|
|
SPI_FLUSH();
|
2006-08-02 14:44:46 +00:00
|
|
|
|
for(; p < end; p++) {
|
|
|
|
|
unsigned char u;
|
2010-06-23 10:18:05 +00:00
|
|
|
|
SPI_READ(u);
|
2006-08-02 14:44:46 +00:00
|
|
|
|
*p = ~u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SPI_FLASH_DISABLE();
|
|
|
|
|
splx(s);
|
2009-05-11 15:26:24 +00:00
|
|
|
|
|
|
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_FLASH_READ);
|
|
|
|
|
|
2006-08-02 14:44:46 +00:00
|
|
|
|
return size;
|
|
|
|
|
}
|
2007-11-10 20:45:29 +00:00
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-02-19 08:25:29 +00:00
|
|
|
|
static const unsigned char *
|
2008-07-03 23:12:10 +00:00
|
|
|
|
program_page(unsigned long offset, const unsigned char *p, int nbytes)
|
2006-08-02 14:44:46 +00:00
|
|
|
|
{
|
|
|
|
|
const unsigned char *end = p + nbytes;
|
|
|
|
|
int s;
|
|
|
|
|
|
2006-08-10 16:42:11 +00:00
|
|
|
|
wait_ready();
|
2006-08-02 14:44:46 +00:00
|
|
|
|
write_enable();
|
|
|
|
|
|
|
|
|
|
s = splhigh();
|
|
|
|
|
SPI_FLASH_ENABLE();
|
|
|
|
|
|
2010-06-23 10:18:05 +00:00
|
|
|
|
SPI_WRITE_FAST(SPI_FLASH_INS_PP);
|
|
|
|
|
SPI_WRITE_FAST(offset >> 16); /* MSB */
|
|
|
|
|
SPI_WRITE_FAST(offset >> 8);
|
|
|
|
|
SPI_WRITE_FAST(offset >> 0); /* LSB */
|
2006-08-02 14:44:46 +00:00
|
|
|
|
|
|
|
|
|
for(; p < end; p++) {
|
2010-06-23 10:18:05 +00:00
|
|
|
|
SPI_WRITE_FAST(~*p);
|
2006-08-02 14:44:46 +00:00
|
|
|
|
}
|
2009-09-07 11:31:26 +00:00
|
|
|
|
SPI_WAITFORTx_ENDED();
|
2006-08-02 14:44:46 +00:00
|
|
|
|
|
|
|
|
|
SPI_FLASH_DISABLE();
|
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
2007-11-10 20:45:29 +00:00
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-08-02 14:44:46 +00:00
|
|
|
|
int
|
2008-07-03 23:12:10 +00:00
|
|
|
|
xmem_pwrite(const void *_buf, int size, unsigned long addr)
|
2006-08-02 14:44:46 +00:00
|
|
|
|
{
|
2006-08-10 16:42:11 +00:00
|
|
|
|
const unsigned char *p = _buf;
|
2008-07-03 23:12:10 +00:00
|
|
|
|
const unsigned long end = addr + size;
|
|
|
|
|
unsigned long i, next_page;
|
2009-05-11 15:26:24 +00:00
|
|
|
|
|
|
|
|
|
ENERGEST_ON(ENERGEST_TYPE_FLASH_WRITE);
|
2011-01-18 14:03:55 +00:00
|
|
|
|
|
2006-08-02 14:44:46 +00:00
|
|
|
|
for(i = addr; i < end;) {
|
|
|
|
|
next_page = (i | 0xff) + 1;
|
2008-02-11 10:43:31 +00:00
|
|
|
|
if(next_page > end) {
|
2006-08-02 14:44:46 +00:00
|
|
|
|
next_page = end;
|
2008-02-11 10:43:31 +00:00
|
|
|
|
}
|
2006-08-10 16:42:11 +00:00
|
|
|
|
p = program_page(i, p, next_page - i);
|
2006-08-02 14:44:46 +00:00
|
|
|
|
i = next_page;
|
|
|
|
|
}
|
2009-05-11 15:26:24 +00:00
|
|
|
|
|
|
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_FLASH_WRITE);
|
|
|
|
|
|
2006-08-02 14:44:46 +00:00
|
|
|
|
return size;
|
|
|
|
|
}
|
2007-11-10 20:45:29 +00:00
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-08-02 14:44:46 +00:00
|
|
|
|
int
|
2008-07-03 23:12:10 +00:00
|
|
|
|
xmem_erase(long size, unsigned long addr)
|
2006-08-02 14:44:46 +00:00
|
|
|
|
{
|
2008-07-03 23:12:10 +00:00
|
|
|
|
unsigned long end = addr + size;
|
2006-08-02 14:44:46 +00:00
|
|
|
|
|
|
|
|
|
if(size % XMEM_ERASE_UNIT_SIZE != 0) {
|
2006-08-10 16:42:11 +00:00
|
|
|
|
PRINTF("xmem_erase: bad size\n");
|
2006-08-02 14:44:46 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(addr % XMEM_ERASE_UNIT_SIZE != 0) {
|
2006-08-10 16:42:11 +00:00
|
|
|
|
PRINTF("xmem_erase: bad offset\n");
|
2006-08-02 14:44:46 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-21 10:28:44 +00:00
|
|
|
|
for (; addr < end; addr += XMEM_ERASE_UNIT_SIZE) {
|
2006-08-10 16:42:11 +00:00
|
|
|
|
erase_sector(addr);
|
2008-01-21 10:28:44 +00:00
|
|
|
|
}
|
2006-08-02 14:44:46 +00:00
|
|
|
|
|
|
|
|
|
return size;
|
|
|
|
|
}
|
2007-11-10 20:45:29 +00:00
|
|
|
|
/*---------------------------------------------------------------------------*/
|