2005-03-18 11:34:26 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Andy Young
|
|
|
|
*
|
|
|
|
* 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 "cpu.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "system.h"
|
2005-07-31 17:31:33 +00:00
|
|
|
#include "i2c-coldfire.h"
|
2005-03-18 11:34:26 +00:00
|
|
|
|
|
|
|
#define I2C_DEVICE_1 ((volatile unsigned char *)&MADR)
|
|
|
|
#define I2C_DEVICE_2 ((volatile unsigned char *)&MADR2)
|
|
|
|
|
|
|
|
/* Local functions definitions */
|
|
|
|
|
|
|
|
static int i2c_write_byte(int device, unsigned char data);
|
|
|
|
static int i2c_gen_start(int device);
|
|
|
|
static void i2c_gen_stop(int device);
|
|
|
|
static volatile unsigned char *i2c_get_addr(int device);
|
|
|
|
|
|
|
|
/* Public functions */
|
|
|
|
|
|
|
|
void i2c_init(void)
|
|
|
|
{
|
2005-08-11 19:00:55 +00:00
|
|
|
/* I2C Clock divisor = 576 => 119.952 MHz / 2 / 576 = 104.125 kHz */
|
|
|
|
MFDR = 0x14;
|
|
|
|
MFDR2 = 0x14;
|
|
|
|
|
2005-07-31 17:31:33 +00:00
|
|
|
#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
|
2005-03-18 11:34:26 +00:00
|
|
|
/* Audio Codec */
|
|
|
|
MBDR = 0; /* iRiver firmware does this */
|
|
|
|
MBCR = IEN; /* Enable interface */
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* FM Tuner */
|
|
|
|
MBDR2 = 0;
|
|
|
|
MBCR2 = IEN;
|
|
|
|
#endif
|
2005-07-31 17:31:33 +00:00
|
|
|
|
|
|
|
#endif
|
2005-03-18 11:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void i2c_close(void)
|
|
|
|
{
|
|
|
|
MBCR = 0;
|
|
|
|
MBCR2 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes bytes to the selected device.
|
|
|
|
*
|
2005-07-31 17:31:33 +00:00
|
|
|
* Use device=1 for bus 1 at 0x40000280
|
|
|
|
* Use device=2 for bus 2 at 0x80000440
|
2005-03-18 11:34:26 +00:00
|
|
|
*
|
|
|
|
* Returns number of bytes successfully send or -1 if START failed
|
|
|
|
*/
|
|
|
|
int i2c_write(int device, unsigned char *buf, int count)
|
|
|
|
{
|
|
|
|
int i;
|
2005-06-08 11:47:16 +00:00
|
|
|
int rc;
|
2005-03-18 11:34:26 +00:00
|
|
|
|
2005-06-08 11:47:16 +00:00
|
|
|
rc = i2c_gen_start(device);
|
|
|
|
if (rc < 0)
|
2005-03-18 11:34:26 +00:00
|
|
|
{
|
|
|
|
DEBUGF("i2c: gen_start failed (d=%d)", device);
|
2005-06-08 11:47:16 +00:00
|
|
|
return rc*10 - 1;
|
2005-03-18 11:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<count; i++)
|
|
|
|
{
|
2005-06-08 11:47:16 +00:00
|
|
|
rc = i2c_write_byte(device, buf[i]);
|
|
|
|
if (rc < 0)
|
2005-03-18 11:34:26 +00:00
|
|
|
{
|
|
|
|
DEBUGF("i2c: write failed at (d=%d,i=%d)", device, i);
|
2005-06-08 11:47:16 +00:00
|
|
|
return rc*10 - 2;
|
2005-03-18 11:34:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i2c_gen_stop(device);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a byte to the interface, returns 0 on success, -1 otherwise. */
|
|
|
|
int i2c_write_byte(int device, unsigned char data)
|
|
|
|
{
|
|
|
|
volatile unsigned char *regs = i2c_get_addr(device);
|
|
|
|
|
|
|
|
long count = 0;
|
|
|
|
|
|
|
|
regs[O_MBDR] = data; /* Write data byte */
|
|
|
|
|
|
|
|
/* Wait for bus busy */
|
|
|
|
while (!(regs[O_MBSR] & IBB) && count < MAX_LOOP)
|
|
|
|
{
|
2005-07-31 17:31:33 +00:00
|
|
|
yield();
|
2005-03-18 11:34:26 +00:00
|
|
|
count++;
|
|
|
|
}
|
2005-07-31 17:31:33 +00:00
|
|
|
|
2005-03-18 11:34:26 +00:00
|
|
|
if (count >= MAX_LOOP)
|
|
|
|
return -1;
|
|
|
|
|
2005-06-08 11:47:16 +00:00
|
|
|
count = 0;
|
2005-07-31 17:31:33 +00:00
|
|
|
|
2005-03-18 11:34:26 +00:00
|
|
|
/* Wait for interrupt flag */
|
|
|
|
while (!(regs[O_MBSR] & IFF) && count < MAX_LOOP)
|
|
|
|
{
|
|
|
|
yield();
|
2005-07-31 17:31:33 +00:00
|
|
|
count++;
|
2005-03-18 11:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count >= MAX_LOOP)
|
2005-06-08 11:47:16 +00:00
|
|
|
return -2;
|
2005-03-18 11:34:26 +00:00
|
|
|
|
|
|
|
regs[O_MBSR] &= ~IFF; /* Clear interrupt flag */
|
2005-07-31 17:31:33 +00:00
|
|
|
|
2005-03-18 11:34:26 +00:00
|
|
|
if (!(regs[O_MBSR] & ICF)) /* Check that transfer is complete */
|
2005-07-31 17:31:33 +00:00
|
|
|
return -3;
|
2005-03-18 11:34:26 +00:00
|
|
|
|
|
|
|
if (regs[O_MBSR] & RXAK) /* Check that the byte has been ACKed */
|
2005-06-08 11:47:16 +00:00
|
|
|
return -4;
|
2005-07-31 17:31:33 +00:00
|
|
|
|
2005-03-18 11:34:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Returns 0 on success, -1 on failure */
|
|
|
|
int i2c_gen_start(int device)
|
|
|
|
{
|
|
|
|
volatile unsigned char *regs = i2c_get_addr(device);
|
|
|
|
long count = 0;
|
2005-07-31 17:31:33 +00:00
|
|
|
|
2005-03-18 11:34:26 +00:00
|
|
|
/* Wait for bus to become free */
|
|
|
|
while ((regs[O_MBSR] & IBB) && (count < MAX_LOOP))
|
2005-06-08 11:47:16 +00:00
|
|
|
{
|
|
|
|
yield();
|
2005-03-18 11:34:26 +00:00
|
|
|
count++;
|
2005-06-08 11:47:16 +00:00
|
|
|
}
|
2005-07-31 17:31:33 +00:00
|
|
|
|
2005-03-18 11:34:26 +00:00
|
|
|
if (count >= MAX_LOOP)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
regs[O_MBCR] |= MSTA | MTX; /* Generate START */
|
|
|
|
|
2005-06-08 11:47:16 +00:00
|
|
|
return 0;
|
2005-03-18 11:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void i2c_gen_stop(int device)
|
|
|
|
{
|
|
|
|
volatile unsigned char *regs = i2c_get_addr(device);
|
|
|
|
regs[O_MBCR] &= ~MSTA; /* Clear MSTA to generate STOP */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
volatile unsigned char *i2c_get_addr(int device)
|
|
|
|
{
|
|
|
|
if (device == 1)
|
|
|
|
return I2C_DEVICE_1;
|
2005-07-31 17:31:33 +00:00
|
|
|
|
|
|
|
return I2C_DEVICE_2;
|
2005-03-18 11:34:26 +00:00
|
|
|
}
|