2006-08-05 16:02:34 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Miika Pekkarinen
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "lcd.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "logf.h"
|
|
|
|
#include "sprintf.h"
|
|
|
|
#include "string.h"
|
|
|
|
|
|
|
|
#include "eeprom_24cxx.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* I2C-functions are copied and ported from fmradio.c.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define SW_I2C_WRITE 0
|
|
|
|
#define SW_I2C_READ 1
|
|
|
|
|
2006-08-15 22:54:06 +00:00
|
|
|
/* h1x0 needs its own i2c driver,
|
|
|
|
h3x0 uses the pcf i2c driver */
|
|
|
|
|
|
|
|
#ifdef IRIVER_H100_SERIES
|
|
|
|
|
2006-08-05 16:02:34 +00:00
|
|
|
/* cute little functions, atomic read-modify-write */
|
|
|
|
|
|
|
|
/* SCL is GPIO, 12 */
|
2006-08-15 22:54:06 +00:00
|
|
|
#define SCL ( 0x00001000 & GPIO_READ)
|
|
|
|
#define SCL_OUT_LO and_l(~0x00001000, &GPIO_OUT)
|
|
|
|
#define SCL_LO or_l( 0x00001000, &GPIO_ENABLE)
|
2006-08-17 18:23:50 +00:00
|
|
|
#define SCL_HI and_l(~0x00001000, &GPIO_ENABLE)
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
/* SDA is GPIO1, 13 */
|
2006-08-15 22:54:06 +00:00
|
|
|
#define SDA ( 0x00002000 & GPIO1_READ)
|
|
|
|
#define SDA_OUT_LO and_l(~0x00002000, &GPIO1_OUT)
|
|
|
|
#define SDA_LO or_l( 0x00002000, &GPIO1_ENABLE)
|
|
|
|
#define SDA_HI and_l(~0x00002000, &GPIO1_ENABLE)
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
/* delay loop to achieve 400kHz at 120MHz CPU frequency */
|
|
|
|
#define DELAY do { int _x; for(_x=0;_x<22;_x++);} while(0)
|
|
|
|
|
|
|
|
static void sw_i2c_init(void)
|
|
|
|
{
|
|
|
|
logf("sw_i2c_init");
|
|
|
|
or_l(0x00001000, &GPIO_FUNCTION);
|
|
|
|
or_l(0x00002000, &GPIO1_FUNCTION);
|
|
|
|
SDA_HI;
|
|
|
|
SCL_HI;
|
2006-08-15 22:54:06 +00:00
|
|
|
SDA_OUT_LO;
|
|
|
|
SCL_OUT_LO;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sw_i2c_start(void)
|
|
|
|
{
|
|
|
|
SCL_LO;
|
|
|
|
DELAY;
|
|
|
|
SDA_HI;
|
|
|
|
DELAY;
|
|
|
|
SCL_HI;
|
|
|
|
DELAY;
|
|
|
|
SDA_LO;
|
|
|
|
DELAY;
|
|
|
|
SCL_LO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sw_i2c_stop(void)
|
|
|
|
{
|
|
|
|
SCL_HI;
|
|
|
|
DELAY;
|
|
|
|
SDA_HI;
|
|
|
|
DELAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sw_i2c_ack(void)
|
|
|
|
{
|
|
|
|
SCL_LO;
|
2006-08-17 18:23:50 +00:00
|
|
|
DELAY;
|
2006-08-05 16:02:34 +00:00
|
|
|
SDA_LO;
|
|
|
|
DELAY;
|
|
|
|
|
|
|
|
SCL_HI;
|
|
|
|
DELAY;
|
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
static bool sw_i2c_getack(void)
|
2006-08-05 16:02:34 +00:00
|
|
|
{
|
2006-08-07 22:30:12 +00:00
|
|
|
bool ret = true;
|
2006-08-05 16:02:34 +00:00
|
|
|
int count = 10;
|
|
|
|
|
|
|
|
SCL_LO;
|
2006-08-17 18:23:50 +00:00
|
|
|
DELAY;
|
2006-08-15 22:54:06 +00:00
|
|
|
SDA_HI; /* sets to input */
|
2006-08-05 16:02:34 +00:00
|
|
|
DELAY;
|
|
|
|
SCL_HI;
|
|
|
|
DELAY;
|
|
|
|
|
|
|
|
while (SDA && count--)
|
|
|
|
DELAY;
|
|
|
|
|
|
|
|
if (SDA)
|
|
|
|
/* ack failed */
|
2006-08-07 22:30:12 +00:00
|
|
|
ret = false;
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
SCL_LO;
|
|
|
|
DELAY;
|
|
|
|
SDA_LO;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sw_i2c_outb(unsigned char byte)
|
|
|
|
{
|
2006-08-07 22:30:12 +00:00
|
|
|
int i;
|
2006-08-05 16:02:34 +00:00
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
/* clock out each bit, MSB first */
|
|
|
|
for ( i=0x80; i; i>>=1 )
|
|
|
|
{
|
|
|
|
SCL_LO;
|
|
|
|
DELAY;
|
|
|
|
if ( i & byte )
|
|
|
|
SDA_HI;
|
|
|
|
else
|
|
|
|
SDA_LO;
|
|
|
|
DELAY;
|
|
|
|
SCL_HI;
|
|
|
|
DELAY;
|
|
|
|
}
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char sw_i2c_inb(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char byte = 0;
|
|
|
|
|
2006-08-15 22:54:06 +00:00
|
|
|
SDA_HI; /* sets to input */
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
/* clock in each bit, MSB first */
|
|
|
|
for ( i=0x80; i; i>>=1 )
|
|
|
|
{
|
|
|
|
SCL_HI;
|
|
|
|
DELAY;
|
|
|
|
if ( SDA )
|
|
|
|
byte |= i;
|
|
|
|
SCL_LO;
|
|
|
|
DELAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw_i2c_ack();
|
|
|
|
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
2006-08-15 22:54:06 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#include "pcf50606.h"
|
|
|
|
|
|
|
|
#define sw_i2c_init() /* no extra init required */
|
|
|
|
#define sw_i2c_start() pcf50606_i2c_start()
|
|
|
|
#define sw_i2c_stop() pcf50606_i2c_stop()
|
|
|
|
#define sw_i2c_ack() pcf50606_i2c_ack(true)
|
|
|
|
#define sw_i2c_getack() pcf50606_i2c_getack()
|
|
|
|
#define sw_i2c_outb(x) pcf50606_i2c_outb(x)
|
|
|
|
#define sw_i2c_inb() pcf50606_i2c_inb(false)
|
|
|
|
|
|
|
|
#endif /* IRIVER_H100_SERIES */
|
|
|
|
|
|
|
|
|
2006-08-05 16:02:34 +00:00
|
|
|
int sw_i2c_write(int location, const unsigned char* buf, int count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
sw_i2c_start();
|
|
|
|
sw_i2c_outb((EEPROM_ADDR & 0xfe) | SW_I2C_WRITE);
|
|
|
|
if (!sw_i2c_getack())
|
|
|
|
{
|
|
|
|
sw_i2c_stop();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw_i2c_outb(location);
|
|
|
|
if (!sw_i2c_getack())
|
|
|
|
{
|
|
|
|
sw_i2c_stop();
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<count; i++)
|
|
|
|
{
|
|
|
|
sw_i2c_outb(buf[i]);
|
|
|
|
if (!sw_i2c_getack())
|
|
|
|
{
|
|
|
|
sw_i2c_stop();
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sw_i2c_stop();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sw_i2c_write_byte(int location, unsigned char byte)
|
|
|
|
{
|
|
|
|
sw_i2c_start();
|
|
|
|
sw_i2c_outb((EEPROM_ADDR & 0xfe) | SW_I2C_WRITE);
|
|
|
|
if (!sw_i2c_getack())
|
|
|
|
{
|
|
|
|
sw_i2c_stop();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw_i2c_outb(location);
|
|
|
|
if (!sw_i2c_getack())
|
|
|
|
{
|
|
|
|
sw_i2c_stop();
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw_i2c_outb(byte);
|
|
|
|
if (!sw_i2c_getack())
|
|
|
|
{
|
|
|
|
sw_i2c_stop();
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw_i2c_stop();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sw_i2c_read(unsigned char location, unsigned char* byte)
|
|
|
|
{
|
|
|
|
sw_i2c_start();
|
|
|
|
sw_i2c_outb((EEPROM_ADDR & 0xfe) | SW_I2C_WRITE);
|
|
|
|
if (!sw_i2c_getack())
|
|
|
|
{
|
|
|
|
sw_i2c_stop();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw_i2c_outb(location);
|
|
|
|
if (!sw_i2c_getack())
|
|
|
|
{
|
|
|
|
sw_i2c_stop();
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw_i2c_start();
|
|
|
|
sw_i2c_outb((EEPROM_ADDR & 0xfe) | SW_I2C_READ);
|
|
|
|
if (!sw_i2c_getack())
|
|
|
|
{
|
|
|
|
sw_i2c_stop();
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
*byte = sw_i2c_inb();
|
|
|
|
sw_i2c_stop();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void eeprom_24cxx_init(void)
|
|
|
|
{
|
|
|
|
sw_i2c_init();
|
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
int eeprom_24cxx_read_byte(unsigned int address, char *c)
|
2006-08-05 16:02:34 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char byte;
|
2006-08-11 19:02:23 +00:00
|
|
|
int count = 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
if (address >= EEPROM_SIZE)
|
|
|
|
{
|
|
|
|
logf("EEPROM address: %d", address);
|
2006-08-07 22:30:12 +00:00
|
|
|
return -9;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*c = 0;
|
2006-08-07 22:30:12 +00:00
|
|
|
do
|
|
|
|
{
|
2006-08-05 16:02:34 +00:00
|
|
|
ret = sw_i2c_read(address, &byte);
|
2006-08-11 19:02:23 +00:00
|
|
|
} while (ret < 0 && count++ < 200);
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2006-08-11 19:02:23 +00:00
|
|
|
logf("EEPROM RFail: %d/%d/%d", ret, address, count);
|
2006-08-07 22:30:12 +00:00
|
|
|
return ret;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-11 19:02:23 +00:00
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
/* keep between {} as logf is whitespace in normal builds */
|
|
|
|
logf("EEPROM rOK: %d retries", count);
|
|
|
|
}
|
|
|
|
|
2006-08-05 16:02:34 +00:00
|
|
|
*c = byte;
|
2006-08-07 22:30:12 +00:00
|
|
|
return 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
int eeprom_24cxx_write_byte(unsigned int address, char c)
|
2006-08-05 16:02:34 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2006-08-11 19:02:23 +00:00
|
|
|
int count = 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
if (address >= EEPROM_SIZE)
|
|
|
|
{
|
|
|
|
logf("EEPROM address: %d", address);
|
2006-08-07 22:30:12 +00:00
|
|
|
return -9;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
do
|
|
|
|
{
|
2006-08-05 16:02:34 +00:00
|
|
|
ret = sw_i2c_write_byte(address, c);
|
2006-08-11 19:02:23 +00:00
|
|
|
} while (ret < 0 && count++ < 200) ;
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
logf("EEPROM WFail: %d/%d", ret, address);
|
2006-08-07 22:30:12 +00:00
|
|
|
return ret;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-11 19:02:23 +00:00
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
/* keep between {} as logf is whitespace in normal builds */
|
|
|
|
logf("EEPROM wOK: %d retries", count);
|
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
return 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
int eeprom_24cxx_read(unsigned char address, void *dest, int length)
|
2006-08-05 16:02:34 +00:00
|
|
|
{
|
|
|
|
char *buf = (char *)dest;
|
2006-08-07 22:30:12 +00:00
|
|
|
int ret = 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
2006-08-07 22:30:12 +00:00
|
|
|
ret = eeprom_24cxx_read_byte(address+i, &buf[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
return ret;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
int eeprom_24cxx_write(unsigned char address, const void *src, int length)
|
2006-08-05 16:02:34 +00:00
|
|
|
{
|
|
|
|
const char *buf = (const char *)src;
|
2006-08-09 12:04:13 +00:00
|
|
|
int count = 5;
|
2006-08-07 22:30:12 +00:00
|
|
|
int i;
|
|
|
|
bool ok;
|
2006-08-05 16:02:34 +00:00
|
|
|
|
|
|
|
while (count-- > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
eeprom_24cxx_write_byte(address+i, buf[i]);
|
|
|
|
|
|
|
|
ok = true;
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
char byte;
|
|
|
|
|
|
|
|
eeprom_24cxx_read_byte(address+i, &byte);
|
|
|
|
if (byte != buf[i])
|
|
|
|
{
|
|
|
|
logf("Verify failed: %d/%d", address+i, count);
|
|
|
|
ok = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok)
|
2006-08-07 22:30:12 +00:00
|
|
|
return 0;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 22:30:12 +00:00
|
|
|
return -1;
|
2006-08-05 16:02:34 +00:00
|
|
|
}
|
|
|
|
|