2005-03-18 11:34:26 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2006-07-21 08:42:28 +00:00
|
|
|
*
|
2005-03-18 11:34:26 +00:00
|
|
|
* $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"
|
2005-08-13 17:32:15 +00:00
|
|
|
#include "logf.h"
|
2005-03-18 11:34:26 +00:00
|
|
|
#include "system.h"
|
2005-07-31 17:31:33 +00:00
|
|
|
#include "i2c-coldfire.h"
|
2005-03-18 11:34:26 +00:00
|
|
|
|
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
/* --- Local functions - declarations --- */
|
|
|
|
|
|
|
|
static int i2c_start(volatile unsigned char *iface);
|
|
|
|
static int i2c_wait_for_slave(volatile unsigned char *iface);
|
|
|
|
static int i2c_outb(volatile unsigned char *iface, unsigned char byte);
|
|
|
|
inline void i2c_stop(volatile unsigned char *iface);
|
2005-03-18 11:34:26 +00:00
|
|
|
|
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
/* --- Public functions - implementation --- */
|
2005-03-18 11:34:26 +00:00
|
|
|
|
|
|
|
void i2c_init(void)
|
|
|
|
{
|
2005-11-18 14:33:06 +00:00
|
|
|
#ifdef IRIVER_H100_SERIES
|
2005-08-13 17:32:15 +00:00
|
|
|
/* The FM chip has no pullup for SCL, so we have to bit-bang the
|
|
|
|
I2C for that one. */
|
|
|
|
or_l(0x00800000, &GPIO1_OUT);
|
|
|
|
or_l(0x00000008, &GPIO_OUT);
|
|
|
|
or_l(0x00800000, &GPIO1_ENABLE);
|
|
|
|
or_l(0x00000008, &GPIO_ENABLE);
|
|
|
|
or_l(0x00800000, &GPIO1_FUNCTION);
|
|
|
|
or_l(0x00000008, &GPIO_FUNCTION);
|
2005-12-15 01:11:19 +00:00
|
|
|
#elif defined(IRIVER_H300_SERIES)
|
|
|
|
/* The FM chip has no pullup for SCL, so we have to bit-bang the
|
|
|
|
I2C for that one. */
|
|
|
|
or_l(0x03000000, &GPIO1_OUT);
|
|
|
|
or_l(0x03000000, &GPIO1_ENABLE);
|
|
|
|
or_l(0x03000000, &GPIO1_FUNCTION);
|
2005-11-18 14:33:06 +00:00
|
|
|
#endif
|
2005-12-15 01:11:19 +00:00
|
|
|
|
2006-04-14 07:56:02 +00:00
|
|
|
/* I2C Clock divisor = 160 => 124.1556 MHz / 2 / 160 = 388.08 kHz */
|
2006-07-21 08:42:28 +00:00
|
|
|
MFDR = 0x0d;
|
2006-04-14 07:56:02 +00:00
|
|
|
MFDR2 = 0x0d;
|
2006-03-28 20:36:04 +00:00
|
|
|
|
2007-03-05 00:04:00 +00:00
|
|
|
#if defined(IAUDIO_X5) || defined(IAUDIO_M5)
|
2006-07-21 08:42:28 +00:00
|
|
|
MBCR = IEN; /* Enable interface */
|
|
|
|
MBCR2 = IEN;
|
2006-03-28 20:36:04 +00:00
|
|
|
#endif
|
2005-08-11 19:00:55 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
#if defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)
|
2005-03-18 11:34:26 +00:00
|
|
|
/* Audio Codec */
|
|
|
|
MBDR = 0; /* iRiver firmware does this */
|
|
|
|
MBCR = IEN; /* Enable interface */
|
2005-07-31 17:31:33 +00:00
|
|
|
#endif
|
2005-03-18 11:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void i2c_close(void)
|
|
|
|
{
|
|
|
|
MBCR = 0;
|
2006-07-21 08:42:28 +00:00
|
|
|
MBCR2 = 0;
|
2005-03-18 11:34:26 +00:00
|
|
|
}
|
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
/*
|
|
|
|
* Writes bytes to a I2C device.
|
2005-03-18 11:34:26 +00:00
|
|
|
*
|
2006-07-21 08:42:28 +00:00
|
|
|
* Returns number of bytes successfully sent or a negative value on error.
|
2005-03-18 11:34:26 +00:00
|
|
|
*/
|
2006-07-21 08:42:28 +00:00
|
|
|
int i2c_write(volatile unsigned char *iface, unsigned char addr,
|
|
|
|
const unsigned char *buf, int count)
|
2005-03-18 11:34:26 +00:00
|
|
|
{
|
2006-07-21 08:42:28 +00:00
|
|
|
int i, rc;
|
2005-03-18 11:34:26 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
if ( ! count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rc = i2c_start(iface);
|
2005-06-08 11:47:16 +00:00
|
|
|
if (rc < 0)
|
2006-07-21 08:42:28 +00:00
|
|
|
return rc;
|
|
|
|
|
|
|
|
rc = i2c_outb(iface, addr & 0xfe);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
2005-03-18 11:34:26 +00:00
|
|
|
{
|
2006-07-21 08:42:28 +00:00
|
|
|
rc = i2c_outb(iface, *buf++);
|
2005-06-08 11:47:16 +00:00
|
|
|
if (rc < 0)
|
2006-07-21 08:42:28 +00:00
|
|
|
return rc;
|
2005-03-18 11:34:26 +00:00
|
|
|
}
|
2006-07-21 08:42:28 +00:00
|
|
|
i2c_stop(iface);
|
|
|
|
|
2005-03-18 11:34:26 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
/*
|
|
|
|
* Reads bytes from a I2C device.
|
|
|
|
*
|
|
|
|
* Returns number of bytes successfully received or a negative value on error.
|
|
|
|
*/
|
|
|
|
int i2c_read(volatile unsigned char *iface, unsigned char addr,
|
|
|
|
unsigned char *buf, int count)
|
2005-03-18 11:34:26 +00:00
|
|
|
{
|
2006-07-21 08:42:28 +00:00
|
|
|
int i, rc;
|
2005-03-18 11:34:26 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
if ( ! count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rc = i2c_start(iface);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2005-03-18 11:34:26 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
rc = i2c_outb(iface, addr | 1);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Switch to Rx mode */
|
|
|
|
iface[O_MBCR] &= ~MTX;
|
|
|
|
iface[O_MBCR] &= ~TXAK;
|
|
|
|
|
|
|
|
/* Dummy read */
|
|
|
|
rc = (int) iface[O_MBDR];
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
rc = i2c_wait_for_slave(iface);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2005-03-18 11:34:26 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
if (i == count-2)
|
|
|
|
/* Don't ACK the next-to-last byte */
|
|
|
|
iface[O_MBCR] |= TXAK;
|
|
|
|
|
|
|
|
if (i == count-1)
|
|
|
|
/* Generate STOP before reading last byte */
|
|
|
|
i2c_stop(iface);
|
2005-07-31 17:31:33 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
*buf++ = iface[O_MBDR];
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2005-03-18 11:34:26 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
/* --- Local functions - implementation --- */
|
2005-07-31 17:31:33 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
/* Begin I2C session on the given interface.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative value on error.
|
|
|
|
*/
|
|
|
|
int i2c_start(volatile unsigned char *iface)
|
|
|
|
{
|
|
|
|
/* Wait for bus to become free */
|
|
|
|
int j = MAX_LOOP;
|
|
|
|
while (--j && (iface[O_MBSR] & IBB))
|
|
|
|
;
|
|
|
|
if (!j)
|
|
|
|
{
|
|
|
|
logf("i2c: bus is busy (iface=%08x)", iface);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate START and prepare for write */
|
|
|
|
iface[O_MBCR] |= (MSTA | TXAK | MTX);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-03-18 11:34:26 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
/* Wait for slave to act on given I2C interface.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative value on error.
|
|
|
|
*/
|
|
|
|
int i2c_wait_for_slave(volatile unsigned char *iface)
|
|
|
|
{
|
|
|
|
int j = MAX_LOOP;
|
|
|
|
while (--j && ! (iface[O_MBSR] & IFF))
|
|
|
|
;
|
|
|
|
if (!j)
|
|
|
|
{
|
|
|
|
logf("i2c: IFF not set (iface=%08x)", iface);
|
|
|
|
i2c_stop(iface);
|
2005-06-08 11:47:16 +00:00
|
|
|
return -2;
|
2006-07-21 08:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear interrupt flag */
|
|
|
|
iface[O_MBSR] &= ~IFF;
|
|
|
|
|
2005-03-18 11:34:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
/* Write the given byte to the given I2C interface.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative value on error.
|
|
|
|
*/
|
|
|
|
int i2c_outb(volatile unsigned char *iface, unsigned char byte)
|
2005-03-18 11:34:26 +00:00
|
|
|
{
|
2006-07-21 08:42:28 +00:00
|
|
|
int rc;
|
2005-03-18 11:34:26 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
iface[O_MBDR] = byte;
|
2005-03-18 11:34:26 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
rc = i2c_wait_for_slave(iface);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Check that transfer is complete */
|
|
|
|
if ( !(iface[O_MBSR] & ICF))
|
|
|
|
{
|
|
|
|
logf("i2c: transfer error (iface=%08x)", iface);
|
|
|
|
i2c_stop(iface);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the byte has been ACKed */
|
|
|
|
if (iface[O_MBSR] & RXAK)
|
|
|
|
{
|
|
|
|
logf("i2c: no ACK (iface=%08x)", iface);
|
|
|
|
i2c_stop(iface);
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
2005-06-08 11:47:16 +00:00
|
|
|
return 0;
|
2005-03-18 11:34:26 +00:00
|
|
|
}
|
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
/* End I2C session on the given interface. */
|
|
|
|
inline void i2c_stop(volatile unsigned char *iface)
|
2005-03-18 11:34:26 +00:00
|
|
|
{
|
2006-07-21 08:42:28 +00:00
|
|
|
iface[O_MBCR] &= ~MSTA;
|
2005-03-18 11:34:26 +00:00
|
|
|
}
|