2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-03-31 09:40:45 +00:00
|
|
|
/*
|
|
|
|
* LPC32xx I2C interface driver
|
|
|
|
*
|
2015-08-04 21:04:41 +00:00
|
|
|
* (C) Copyright 2014-2015 DENX Software Engineering GmbH
|
2015-03-31 09:40:45 +00:00
|
|
|
* Written-by: Albert ARIBAUD - 3ADEV <albert.aribaud@3adev.fr>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <i2c.h>
|
2016-09-21 02:28:55 +00:00
|
|
|
#include <linux/errno.h>
|
2015-03-31 09:40:45 +00:00
|
|
|
#include <asm/arch/clk.h>
|
2017-03-14 15:24:44 +00:00
|
|
|
#include <asm/arch/i2c.h>
|
2017-03-27 15:13:12 +00:00
|
|
|
#include <dm.h>
|
|
|
|
#include <mapmem.h>
|
2015-03-31 09:40:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Provide default speed and slave if target did not
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(CONFIG_SYS_I2C_LPC32XX_SPEED)
|
|
|
|
#define CONFIG_SYS_I2C_LPC32XX_SPEED 350000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CONFIG_SYS_I2C_LPC32XX_SLAVE)
|
|
|
|
#define CONFIG_SYS_I2C_LPC32XX_SLAVE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* TX register fields */
|
|
|
|
#define LPC32XX_I2C_TX_START 0x00000100
|
|
|
|
#define LPC32XX_I2C_TX_STOP 0x00000200
|
|
|
|
|
|
|
|
/* Control register values */
|
|
|
|
#define LPC32XX_I2C_SOFT_RESET 0x00000100
|
|
|
|
|
|
|
|
/* Status register values */
|
|
|
|
#define LPC32XX_I2C_STAT_TFF 0x00000400
|
|
|
|
#define LPC32XX_I2C_STAT_RFE 0x00000200
|
|
|
|
#define LPC32XX_I2C_STAT_DRMI 0x00000008
|
|
|
|
#define LPC32XX_I2C_STAT_NAI 0x00000004
|
|
|
|
#define LPC32XX_I2C_STAT_TDI 0x00000001
|
|
|
|
|
2017-03-27 15:13:12 +00:00
|
|
|
#ifndef CONFIG_DM_I2C
|
2017-03-27 15:11:36 +00:00
|
|
|
static struct lpc32xx_i2c_base *lpc32xx_i2c[] = {
|
|
|
|
(struct lpc32xx_i2c_base *)I2C1_BASE,
|
|
|
|
(struct lpc32xx_i2c_base *)I2C2_BASE,
|
|
|
|
(struct lpc32xx_i2c_base *)(USB_BASE + 0x300)
|
2015-03-31 09:40:45 +00:00
|
|
|
};
|
2017-03-27 15:13:12 +00:00
|
|
|
#endif
|
2015-03-31 09:40:45 +00:00
|
|
|
|
|
|
|
/* Set I2C bus speed */
|
2017-03-27 15:11:36 +00:00
|
|
|
static unsigned int __i2c_set_bus_speed(struct lpc32xx_i2c_base *base,
|
|
|
|
unsigned int speed, unsigned int chip)
|
2015-03-31 09:40:45 +00:00
|
|
|
{
|
|
|
|
int half_period;
|
|
|
|
|
|
|
|
if (speed == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-08-12 17:22:13 +00:00
|
|
|
/* OTG I2C clock source and CLK registers are different */
|
2017-03-27 15:11:36 +00:00
|
|
|
if (chip == 2) {
|
2015-08-12 17:22:13 +00:00
|
|
|
half_period = (get_periph_clk_rate() / speed) / 2;
|
|
|
|
if (half_period > 0xFF)
|
|
|
|
return -EINVAL;
|
|
|
|
} else {
|
|
|
|
half_period = (get_hclk_clk_rate() / speed) / 2;
|
|
|
|
if (half_period > 0x3FF)
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-03-31 09:40:45 +00:00
|
|
|
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(half_period, &base->clk_hi);
|
|
|
|
writel(half_period, &base->clk_lo);
|
2015-03-31 09:40:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* I2C init called by cmd_i2c when doing 'i2c reset'. */
|
2017-03-27 15:11:36 +00:00
|
|
|
static void __i2c_init(struct lpc32xx_i2c_base *base,
|
|
|
|
int requested_speed, int slaveadd, unsigned int chip)
|
2015-03-31 09:40:45 +00:00
|
|
|
{
|
|
|
|
/* soft reset (auto-clears) */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
|
2015-08-12 17:22:13 +00:00
|
|
|
/* set HI and LO periods for half of the default speed */
|
2017-03-27 15:11:36 +00:00
|
|
|
__i2c_set_bus_speed(base, requested_speed, chip);
|
2015-03-31 09:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* I2C probe called by cmd_i2c when doing 'i2c probe'. */
|
2017-03-27 15:11:36 +00:00
|
|
|
static int __i2c_probe_chip(struct lpc32xx_i2c_base *base, u8 dev)
|
2015-03-31 09:40:45 +00:00
|
|
|
{
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
/* Soft-reset the controller */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
|
|
|
|
while (readl(&base->ctrl) & LPC32XX_I2C_SOFT_RESET)
|
2015-03-31 09:40:45 +00:00
|
|
|
;
|
|
|
|
/* Addre slave for write with start before and stop after */
|
|
|
|
writel((dev<<1) | LPC32XX_I2C_TX_START | LPC32XX_I2C_TX_STOP,
|
2017-03-27 15:11:36 +00:00
|
|
|
&base->tx);
|
2015-03-31 09:40:45 +00:00
|
|
|
/* wait for end of transation */
|
2017-03-27 15:11:36 +00:00
|
|
|
while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
|
2015-03-31 09:40:45 +00:00
|
|
|
;
|
|
|
|
/* was there no acknowledge? */
|
|
|
|
return (stat & LPC32XX_I2C_STAT_NAI) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
|
|
|
|
* Begin write, send address byte(s), begin read, receive data bytes, end.
|
|
|
|
*/
|
2017-03-27 15:11:36 +00:00
|
|
|
static int __i2c_read(struct lpc32xx_i2c_base *base, u8 dev, uint addr,
|
|
|
|
int alen, u8 *data, int length)
|
2015-03-31 09:40:45 +00:00
|
|
|
{
|
|
|
|
int stat, wlen;
|
|
|
|
|
|
|
|
/* Soft-reset the controller */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
|
|
|
|
while (readl(&base->ctrl) & LPC32XX_I2C_SOFT_RESET)
|
2015-03-31 09:40:45 +00:00
|
|
|
;
|
|
|
|
/* do we need to write an address at all? */
|
|
|
|
if (alen) {
|
|
|
|
/* Address slave in write mode */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel((dev<<1) | LPC32XX_I2C_TX_START, &base->tx);
|
2015-03-31 09:40:45 +00:00
|
|
|
/* write address bytes */
|
|
|
|
while (alen--) {
|
|
|
|
/* compute address byte + stop for the last one */
|
|
|
|
int a = (addr >> (8 * alen)) & 0xff;
|
|
|
|
if (!alen)
|
|
|
|
a |= LPC32XX_I2C_TX_STOP;
|
|
|
|
/* Send address byte */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(a, &base->tx);
|
2015-03-31 09:40:45 +00:00
|
|
|
}
|
|
|
|
/* wait for end of transation */
|
2017-03-27 15:11:36 +00:00
|
|
|
while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
|
2015-03-31 09:40:45 +00:00
|
|
|
;
|
|
|
|
/* clear end-of-transaction flag */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(1, &base->stat);
|
2015-03-31 09:40:45 +00:00
|
|
|
}
|
|
|
|
/* do we have to read data at all? */
|
|
|
|
if (length) {
|
|
|
|
/* Address slave in read mode */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(1 | (dev<<1) | LPC32XX_I2C_TX_START, &base->tx);
|
2015-03-31 09:40:45 +00:00
|
|
|
wlen = length;
|
|
|
|
/* get data */
|
|
|
|
while (length | wlen) {
|
|
|
|
/* read status for TFF and RFE */
|
2017-03-27 15:11:36 +00:00
|
|
|
stat = readl(&base->stat);
|
2015-03-31 09:40:45 +00:00
|
|
|
/* must we, can we write a trigger byte? */
|
|
|
|
if ((wlen > 0)
|
|
|
|
& (!(stat & LPC32XX_I2C_STAT_TFF))) {
|
|
|
|
wlen--;
|
|
|
|
/* write trigger byte + stop if last */
|
|
|
|
writel(wlen ? 0 :
|
2017-03-27 15:11:36 +00:00
|
|
|
LPC32XX_I2C_TX_STOP, &base->tx);
|
2015-03-31 09:40:45 +00:00
|
|
|
}
|
|
|
|
/* must we, can we read a data byte? */
|
|
|
|
if ((length > 0)
|
|
|
|
& (!(stat & LPC32XX_I2C_STAT_RFE))) {
|
|
|
|
length--;
|
|
|
|
/* read byte */
|
2017-03-27 15:11:36 +00:00
|
|
|
*(data++) = readl(&base->rx);
|
2015-03-31 09:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-27 17:37:38 +00:00
|
|
|
/* wait for end of transation */
|
2017-03-27 15:11:36 +00:00
|
|
|
while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
|
2015-07-27 17:37:38 +00:00
|
|
|
;
|
|
|
|
/* clear end-of-transaction flag */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(1, &base->stat);
|
2015-03-31 09:40:45 +00:00
|
|
|
}
|
|
|
|
/* success */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
|
|
|
|
* Begin write, send address byte(s), send data bytes, end.
|
|
|
|
*/
|
2017-03-27 15:11:36 +00:00
|
|
|
static int __i2c_write(struct lpc32xx_i2c_base *base, u8 dev, uint addr,
|
|
|
|
int alen, u8 *data, int length)
|
2015-03-31 09:40:45 +00:00
|
|
|
{
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
/* Soft-reset the controller */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
|
|
|
|
while (readl(&base->ctrl) & LPC32XX_I2C_SOFT_RESET)
|
2015-03-31 09:40:45 +00:00
|
|
|
;
|
|
|
|
/* do we need to write anything at all? */
|
|
|
|
if (alen | length)
|
|
|
|
/* Address slave in write mode */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel((dev<<1) | LPC32XX_I2C_TX_START, &base->tx);
|
2015-07-27 17:37:39 +00:00
|
|
|
else
|
|
|
|
return 0;
|
2015-03-31 09:40:45 +00:00
|
|
|
/* write address bytes */
|
|
|
|
while (alen) {
|
|
|
|
/* wait for transmit fifo not full */
|
2017-03-27 15:11:36 +00:00
|
|
|
stat = readl(&base->stat);
|
2015-03-31 09:40:45 +00:00
|
|
|
if (!(stat & LPC32XX_I2C_STAT_TFF)) {
|
|
|
|
alen--;
|
|
|
|
int a = (addr >> (8 * alen)) & 0xff;
|
|
|
|
if (!(alen | length))
|
|
|
|
a |= LPC32XX_I2C_TX_STOP;
|
|
|
|
/* Send address byte */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(a, &base->tx);
|
2015-03-31 09:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (length) {
|
|
|
|
/* wait for transmit fifo not full */
|
2017-03-27 15:11:36 +00:00
|
|
|
stat = readl(&base->stat);
|
2015-03-31 09:40:45 +00:00
|
|
|
if (!(stat & LPC32XX_I2C_STAT_TFF)) {
|
|
|
|
/* compute data byte, add stop if length==0 */
|
|
|
|
length--;
|
|
|
|
int d = *(data++);
|
|
|
|
if (!length)
|
|
|
|
d |= LPC32XX_I2C_TX_STOP;
|
|
|
|
/* Send data byte */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(d, &base->tx);
|
2015-03-31 09:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* wait for end of transation */
|
2017-03-27 15:11:36 +00:00
|
|
|
while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
|
2015-03-31 09:40:45 +00:00
|
|
|
;
|
|
|
|
/* clear end-of-transaction flag */
|
2017-03-27 15:11:36 +00:00
|
|
|
writel(1, &base->stat);
|
2015-03-31 09:40:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-27 15:13:12 +00:00
|
|
|
#ifndef CONFIG_DM_I2C
|
2017-03-14 15:24:40 +00:00
|
|
|
static void lpc32xx_i2c_init(struct i2c_adapter *adap,
|
|
|
|
int requested_speed, int slaveadd)
|
|
|
|
{
|
2017-03-27 15:11:36 +00:00
|
|
|
__i2c_init(lpc32xx_i2c[adap->hwadapnr], requested_speed, slaveadd,
|
|
|
|
adap->hwadapnr);
|
2017-03-14 15:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lpc32xx_i2c_probe_chip(struct i2c_adapter *adap, u8 dev)
|
|
|
|
{
|
2017-03-27 15:11:36 +00:00
|
|
|
return __i2c_probe_chip(lpc32xx_i2c[adap->hwadapnr], dev);
|
2017-03-14 15:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lpc32xx_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
|
|
|
|
int alen, u8 *data, int length)
|
|
|
|
{
|
2017-03-27 15:11:36 +00:00
|
|
|
return __i2c_read(lpc32xx_i2c[adap->hwadapnr], dev, addr,
|
|
|
|
alen, data, length);
|
2017-03-14 15:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lpc32xx_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
|
|
|
|
int alen, u8 *data, int length)
|
|
|
|
{
|
2017-03-27 15:11:36 +00:00
|
|
|
return __i2c_write(lpc32xx_i2c[adap->hwadapnr], dev, addr,
|
|
|
|
alen, data, length);
|
2017-03-14 15:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int lpc32xx_i2c_set_bus_speed(struct i2c_adapter *adap,
|
|
|
|
unsigned int speed)
|
|
|
|
{
|
2017-03-27 15:11:36 +00:00
|
|
|
return __i2c_set_bus_speed(lpc32xx_i2c[adap->hwadapnr], speed,
|
|
|
|
adap->hwadapnr);
|
2017-03-14 15:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_0, lpc32xx_i2c_init, lpc32xx_i2c_probe_chip,
|
2015-03-31 09:40:45 +00:00
|
|
|
lpc32xx_i2c_read, lpc32xx_i2c_write,
|
|
|
|
lpc32xx_i2c_set_bus_speed,
|
|
|
|
CONFIG_SYS_I2C_LPC32XX_SPEED,
|
|
|
|
CONFIG_SYS_I2C_LPC32XX_SLAVE,
|
|
|
|
0)
|
|
|
|
|
2017-03-14 15:24:40 +00:00
|
|
|
U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_1, lpc32xx_i2c_init, lpc32xx_i2c_probe_chip,
|
2015-03-31 09:40:45 +00:00
|
|
|
lpc32xx_i2c_read, lpc32xx_i2c_write,
|
|
|
|
lpc32xx_i2c_set_bus_speed,
|
|
|
|
CONFIG_SYS_I2C_LPC32XX_SPEED,
|
|
|
|
CONFIG_SYS_I2C_LPC32XX_SLAVE,
|
|
|
|
1)
|
2015-08-04 21:04:41 +00:00
|
|
|
|
2017-03-14 15:24:40 +00:00
|
|
|
U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_2, lpc32xx_i2c_init, NULL,
|
2015-08-04 21:04:41 +00:00
|
|
|
lpc32xx_i2c_read, lpc32xx_i2c_write,
|
|
|
|
lpc32xx_i2c_set_bus_speed,
|
|
|
|
100000,
|
|
|
|
0,
|
|
|
|
2)
|
2017-03-27 15:13:12 +00:00
|
|
|
#else /* CONFIG_DM_I2C */
|
|
|
|
static int lpc32xx_i2c_probe(struct udevice *bus)
|
|
|
|
{
|
|
|
|
struct lpc32xx_i2c_dev *dev = dev_get_platdata(bus);
|
2017-03-14 15:24:45 +00:00
|
|
|
bus->seq = dev->index;
|
2017-03-27 15:13:12 +00:00
|
|
|
|
|
|
|
__i2c_init(dev->base, dev->speed, 0, dev->index);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lpc32xx_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
|
|
|
|
u32 chip_flags)
|
|
|
|
{
|
|
|
|
struct lpc32xx_i2c_dev *dev = dev_get_platdata(bus);
|
|
|
|
return __i2c_probe_chip(dev->base, chip_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lpc32xx_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
|
|
|
|
int nmsgs)
|
|
|
|
{
|
|
|
|
struct lpc32xx_i2c_dev *dev = dev_get_platdata(bus);
|
|
|
|
struct i2c_msg *dmsg, *omsg, dummy;
|
|
|
|
uint i = 0, address = 0;
|
|
|
|
|
|
|
|
memset(&dummy, 0, sizeof(struct i2c_msg));
|
|
|
|
|
|
|
|
/* We expect either two messages (one with an offset and one with the
|
|
|
|
* actual data) or one message (just data)
|
|
|
|
*/
|
|
|
|
if (nmsgs > 2 || nmsgs == 0) {
|
|
|
|
debug("%s: Only one or two messages are supported.", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
omsg = nmsgs == 1 ? &dummy : msg;
|
|
|
|
dmsg = nmsgs == 1 ? msg : msg + 1;
|
|
|
|
|
|
|
|
/* the address is expected to be a uint, not a array. */
|
|
|
|
address = omsg->buf[0];
|
|
|
|
for (i = 1; i < omsg->len; i++)
|
|
|
|
address = (address << 8) + omsg->buf[i];
|
|
|
|
|
|
|
|
if (dmsg->flags & I2C_M_RD)
|
|
|
|
return __i2c_read(dev->base, dmsg->addr, address,
|
|
|
|
omsg->len, dmsg->buf, dmsg->len);
|
|
|
|
else
|
|
|
|
return __i2c_write(dev->base, dmsg->addr, address,
|
|
|
|
omsg->len, dmsg->buf, dmsg->len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lpc32xx_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
|
|
|
|
{
|
|
|
|
struct lpc32xx_i2c_dev *dev = dev_get_platdata(bus);
|
|
|
|
return __i2c_set_bus_speed(dev->base, speed, dev->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lpc32xx_i2c_reset(struct udevice *bus)
|
|
|
|
{
|
|
|
|
struct lpc32xx_i2c_dev *dev = dev_get_platdata(bus);
|
|
|
|
|
|
|
|
__i2c_init(dev->base, dev->speed, 0, dev->index);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_i2c_ops lpc32xx_i2c_ops = {
|
|
|
|
.xfer = lpc32xx_i2c_xfer,
|
|
|
|
.probe_chip = lpc32xx_i2c_probe_chip,
|
|
|
|
.deblock = lpc32xx_i2c_reset,
|
|
|
|
.set_bus_speed = lpc32xx_i2c_set_bus_speed,
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(i2c_lpc32xx) = {
|
|
|
|
.id = UCLASS_I2C,
|
|
|
|
.name = "i2c_lpc32xx",
|
|
|
|
.probe = lpc32xx_i2c_probe,
|
|
|
|
.ops = &lpc32xx_i2c_ops,
|
|
|
|
};
|
|
|
|
#endif /* CONFIG_DM_I2C */
|