2003-03-06 21:55:29 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2002
|
|
|
|
* David Mueller, ELSOFT AG, d.mueller@elsoft.ch
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2003-03-06 21:55:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* This code should work for both the S3C2400 and the S3C2410
|
|
|
|
* as they seem to have the same I2C controller inside.
|
|
|
|
* The different address mapping is handled by the s3c24xx.h files below.
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
2015-01-27 12:36:36 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <dm.h>
|
2012-12-26 20:03:12 +00:00
|
|
|
#include <fdtdec.h>
|
2012-11-20 02:19:05 +00:00
|
|
|
#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
|
2012-07-23 21:23:53 +00:00
|
|
|
#include <asm/arch/clk.h>
|
|
|
|
#include <asm/arch/cpu.h>
|
2012-12-26 20:03:12 +00:00
|
|
|
#include <asm/arch/pinmux.h>
|
2012-07-23 21:23:53 +00:00
|
|
|
#else
|
2009-11-17 09:30:34 +00:00
|
|
|
#include <asm/arch/s3c24x0_cpu.h>
|
2012-07-23 21:23:53 +00:00
|
|
|
#endif
|
2009-10-10 04:33:11 +00:00
|
|
|
#include <asm/io.h>
|
2003-03-06 21:55:29 +00:00
|
|
|
#include <i2c.h>
|
2012-07-23 21:23:53 +00:00
|
|
|
#include "s3c24x0_i2c.h"
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2015-01-27 12:36:36 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2013-10-15 10:31:43 +00:00
|
|
|
/*
|
|
|
|
* Wait til the byte transfer is completed.
|
|
|
|
*
|
|
|
|
* @param i2c- pointer to the appropriate i2c register bank.
|
|
|
|
* @return I2C_OK, if transmission was ACKED
|
|
|
|
* I2C_NACK, if transmission was NACKED
|
|
|
|
* I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS
|
|
|
|
*/
|
|
|
|
|
2012-07-23 21:23:53 +00:00
|
|
|
static int WaitForXfer(struct s3c24x0_i2c *i2c)
|
2003-03-06 21:55:29 +00:00
|
|
|
{
|
2013-10-15 10:31:43 +00:00
|
|
|
ulong start_time = get_timer(0);
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2013-10-15 10:31:43 +00:00
|
|
|
do {
|
|
|
|
if (readl(&i2c->iiccon) & I2CCON_IRPND)
|
|
|
|
return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
|
|
|
|
I2C_NACK : I2C_OK;
|
|
|
|
} while (get_timer(start_time) < I2C_TIMEOUT_MS);
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2013-10-15 10:31:43 +00:00
|
|
|
return I2C_NOK_TOUT;
|
2003-03-06 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:15:46 +00:00
|
|
|
static void read_write_byte(struct s3c24x0_i2c *i2c)
|
2003-03-06 21:55:29 +00:00
|
|
|
{
|
2015-07-03 00:15:46 +00:00
|
|
|
clrbits_le32(&i2c->iiccon, I2CCON_IRPND);
|
2003-03-06 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2012-07-23 21:23:53 +00:00
|
|
|
static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
|
|
|
|
{
|
|
|
|
ulong freq, pres = 16, div;
|
2012-11-20 02:19:05 +00:00
|
|
|
#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
|
2012-07-23 21:23:53 +00:00
|
|
|
freq = get_i2c_clk();
|
|
|
|
#else
|
|
|
|
freq = get_PCLK();
|
|
|
|
#endif
|
|
|
|
/* calculate prescaler and divisor values */
|
|
|
|
if ((freq / pres / (16 + 1)) > speed)
|
|
|
|
/* set prescaler to 512 */
|
|
|
|
pres = 512;
|
|
|
|
|
|
|
|
div = 0;
|
|
|
|
while ((freq / pres / (div + 1)) > speed)
|
|
|
|
div++;
|
|
|
|
|
|
|
|
/* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
|
|
|
|
writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
|
|
|
|
|
|
|
|
/* init to SLAVE REVEIVE and set slaveaddr */
|
|
|
|
writel(0, &i2c->iicstat);
|
|
|
|
writel(slaveadd, &i2c->iicadd);
|
|
|
|
/* program Master Transmit (and implicit STOP) */
|
|
|
|
writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
|
|
|
|
}
|
|
|
|
|
2015-01-27 12:36:36 +00:00
|
|
|
static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
|
2013-11-20 09:43:49 +00:00
|
|
|
{
|
2016-11-23 13:34:42 +00:00
|
|
|
struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
|
2013-11-20 09:43:49 +00:00
|
|
|
|
|
|
|
i2c_bus->clock_frequency = speed;
|
|
|
|
|
2016-11-23 13:34:43 +00:00
|
|
|
i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
|
|
|
|
CONFIG_SYS_I2C_S3C24X0_SLAVE);
|
2013-11-20 09:43:49 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-06 21:55:29 +00:00
|
|
|
/*
|
2003-10-08 22:33:00 +00:00
|
|
|
* cmd_type is 0 for write, 1 for read.
|
|
|
|
*
|
|
|
|
* addr_len can take any value from 0-255, it is only limited
|
|
|
|
* by the char, we could make it larger if needed. If it is
|
|
|
|
* 0 we skip the address write cycle.
|
|
|
|
*/
|
2012-07-23 21:23:53 +00:00
|
|
|
static int i2c_transfer(struct s3c24x0_i2c *i2c,
|
|
|
|
unsigned char cmd_type,
|
|
|
|
unsigned char chip,
|
|
|
|
unsigned char addr[],
|
|
|
|
unsigned char addr_len,
|
|
|
|
unsigned char data[],
|
|
|
|
unsigned short data_len)
|
2003-03-06 21:55:29 +00:00
|
|
|
{
|
2013-10-15 10:31:43 +00:00
|
|
|
int i = 0, result;
|
|
|
|
ulong start_time = get_timer(0);
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2003-10-08 22:33:00 +00:00
|
|
|
if (data == 0 || data_len == 0) {
|
|
|
|
/*Don't support data transfer of no length or to address 0 */
|
2012-07-23 21:23:53 +00:00
|
|
|
debug("i2c_transfer: bad call\n");
|
2003-10-08 22:33:00 +00:00
|
|
|
return I2C_NOK;
|
|
|
|
}
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2013-10-15 10:31:43 +00:00
|
|
|
while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
|
|
|
|
if (get_timer(start_time) > I2C_TIMEOUT_MS)
|
|
|
|
return I2C_NOK_TOUT;
|
2003-10-08 22:33:00 +00:00
|
|
|
}
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2012-07-23 21:23:53 +00:00
|
|
|
writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2013-10-15 10:31:43 +00:00
|
|
|
/* Get the slave chip address going */
|
|
|
|
writel(chip, &i2c->iicds);
|
|
|
|
if ((cmd_type == I2C_WRITE) || (addr && addr_len))
|
|
|
|
writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
|
|
|
|
&i2c->iicstat);
|
|
|
|
else
|
|
|
|
writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
|
|
|
|
&i2c->iicstat);
|
|
|
|
|
|
|
|
/* Wait for chip address to transmit. */
|
|
|
|
result = WaitForXfer(i2c);
|
|
|
|
if (result != I2C_OK)
|
|
|
|
goto bailout;
|
|
|
|
|
|
|
|
/* If register address needs to be transmitted - do it now. */
|
|
|
|
if (addr && addr_len) {
|
|
|
|
while ((i < addr_len) && (result == I2C_OK)) {
|
|
|
|
writel(addr[i++], &i2c->iicds);
|
2015-07-03 00:15:46 +00:00
|
|
|
read_write_byte(i2c);
|
2013-10-15 10:31:43 +00:00
|
|
|
result = WaitForXfer(i2c);
|
2003-03-06 21:55:29 +00:00
|
|
|
}
|
2013-10-15 10:31:43 +00:00
|
|
|
i = 0;
|
|
|
|
if (result != I2C_OK)
|
|
|
|
goto bailout;
|
|
|
|
}
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2013-10-15 10:31:43 +00:00
|
|
|
switch (cmd_type) {
|
|
|
|
case I2C_WRITE:
|
|
|
|
while ((i < data_len) && (result == I2C_OK)) {
|
|
|
|
writel(data[i++], &i2c->iicds);
|
2015-07-03 00:15:46 +00:00
|
|
|
read_write_byte(i2c);
|
2012-07-23 21:23:53 +00:00
|
|
|
result = WaitForXfer(i2c);
|
2013-10-15 10:31:43 +00:00
|
|
|
}
|
2003-10-08 22:33:00 +00:00
|
|
|
break;
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2003-06-19 23:01:32 +00:00
|
|
|
case I2C_READ:
|
2003-10-08 22:33:00 +00:00
|
|
|
if (addr && addr_len) {
|
2013-10-15 10:31:43 +00:00
|
|
|
/*
|
|
|
|
* Register address has been sent, now send slave chip
|
|
|
|
* address again to start the actual read transaction.
|
|
|
|
*/
|
2010-10-26 14:04:31 +00:00
|
|
|
writel(chip, &i2c->iicds);
|
2013-10-15 10:31:43 +00:00
|
|
|
|
|
|
|
/* Generate a re-START. */
|
2013-02-19 02:19:45 +00:00
|
|
|
writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
|
|
|
|
&i2c->iicstat);
|
2015-07-03 00:15:46 +00:00
|
|
|
read_write_byte(i2c);
|
2012-07-23 21:23:53 +00:00
|
|
|
result = WaitForXfer(i2c);
|
2003-10-08 22:33:00 +00:00
|
|
|
|
2013-10-15 10:31:43 +00:00
|
|
|
if (result != I2C_OK)
|
|
|
|
goto bailout;
|
2003-03-06 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2013-10-15 10:31:43 +00:00
|
|
|
while ((i < data_len) && (result == I2C_OK)) {
|
|
|
|
/* disable ACK for final READ */
|
|
|
|
if (i == data_len - 1)
|
|
|
|
writel(readl(&i2c->iiccon)
|
|
|
|
& ~I2CCON_ACKGEN,
|
|
|
|
&i2c->iiccon);
|
2015-07-03 00:15:46 +00:00
|
|
|
read_write_byte(i2c);
|
2013-10-15 10:31:43 +00:00
|
|
|
result = WaitForXfer(i2c);
|
|
|
|
data[i++] = readl(&i2c->iicds);
|
|
|
|
}
|
|
|
|
if (result == I2C_NACK)
|
|
|
|
result = I2C_OK; /* Normal terminated read. */
|
2003-10-08 22:33:00 +00:00
|
|
|
break;
|
2003-03-06 21:55:29 +00:00
|
|
|
|
|
|
|
default:
|
2012-07-23 21:23:53 +00:00
|
|
|
debug("i2c_transfer: bad call\n");
|
2003-10-08 22:33:00 +00:00
|
|
|
result = I2C_NOK;
|
|
|
|
break;
|
|
|
|
}
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2013-10-15 10:31:43 +00:00
|
|
|
bailout:
|
|
|
|
/* Send STOP. */
|
|
|
|
writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
|
2015-07-03 00:15:46 +00:00
|
|
|
read_write_byte(i2c);
|
2013-10-15 10:31:43 +00:00
|
|
|
|
2012-07-23 21:23:53 +00:00
|
|
|
return result;
|
2003-03-06 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2015-01-27 12:36:36 +00:00
|
|
|
static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
|
2003-03-06 21:55:29 +00:00
|
|
|
{
|
2016-11-23 13:34:42 +00:00
|
|
|
struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
|
2003-10-08 22:33:00 +00:00
|
|
|
uchar buf[1];
|
2013-10-15 10:32:44 +00:00
|
|
|
int ret;
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2003-10-08 22:33:00 +00:00
|
|
|
buf[0] = 0;
|
2003-03-06 21:55:29 +00:00
|
|
|
|
2003-10-08 22:33:00 +00:00
|
|
|
/*
|
|
|
|
* What is needed is to send the chip address and verify that the
|
|
|
|
* address was <ACK>ed (i.e. there was a chip at that address which
|
|
|
|
* drove the data line low).
|
|
|
|
*/
|
2016-11-23 13:34:43 +00:00
|
|
|
ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1, 0, 0, buf, 1);
|
2013-10-15 10:32:44 +00:00
|
|
|
|
|
|
|
return ret != I2C_OK;
|
2003-03-06 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:15:47 +00:00
|
|
|
static int s3c24x0_do_msg(struct s3c24x0_i2c_bus *i2c_bus, struct i2c_msg *msg,
|
|
|
|
int seq)
|
2015-01-27 12:36:36 +00:00
|
|
|
{
|
2015-07-03 00:15:47 +00:00
|
|
|
struct s3c24x0_i2c *i2c = i2c_bus->regs;
|
|
|
|
bool is_read = msg->flags & I2C_M_RD;
|
|
|
|
uint status;
|
|
|
|
uint addr;
|
|
|
|
int ret, i;
|
2015-01-27 12:36:36 +00:00
|
|
|
|
2015-07-03 00:15:47 +00:00
|
|
|
if (!seq)
|
|
|
|
setbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
|
|
|
|
|
|
|
|
/* Get the slave chip address going */
|
|
|
|
addr = msg->addr << 1;
|
|
|
|
writel(addr, &i2c->iicds);
|
|
|
|
status = I2C_TXRX_ENA | I2C_START_STOP;
|
|
|
|
if (is_read)
|
|
|
|
status |= I2C_MODE_MR;
|
|
|
|
else
|
|
|
|
status |= I2C_MODE_MT;
|
|
|
|
writel(status, &i2c->iicstat);
|
|
|
|
if (seq)
|
|
|
|
read_write_byte(i2c);
|
|
|
|
|
|
|
|
/* Wait for chip address to transmit */
|
|
|
|
ret = WaitForXfer(i2c);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (is_read) {
|
|
|
|
for (i = 0; !ret && i < msg->len; i++) {
|
|
|
|
/* disable ACK for final READ */
|
|
|
|
if (i == msg->len - 1)
|
|
|
|
clrbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
|
|
|
|
read_write_byte(i2c);
|
|
|
|
ret = WaitForXfer(i2c);
|
|
|
|
msg->buf[i] = readl(&i2c->iicds);
|
|
|
|
}
|
|
|
|
if (ret == I2C_NACK)
|
|
|
|
ret = I2C_OK; /* Normal terminated read */
|
2015-01-27 12:36:36 +00:00
|
|
|
} else {
|
2015-07-03 00:15:47 +00:00
|
|
|
for (i = 0; !ret && i < msg->len; i++) {
|
|
|
|
writel(msg->buf[i], &i2c->iicds);
|
|
|
|
read_write_byte(i2c);
|
|
|
|
ret = WaitForXfer(i2c);
|
|
|
|
}
|
2015-01-27 12:36:36 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:15:47 +00:00
|
|
|
err:
|
|
|
|
return ret;
|
2015-01-27 12:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int s3c24x0_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
|
|
|
|
int nmsgs)
|
|
|
|
{
|
|
|
|
struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
|
2015-07-03 00:15:47 +00:00
|
|
|
struct s3c24x0_i2c *i2c = i2c_bus->regs;
|
|
|
|
ulong start_time;
|
|
|
|
int ret, i;
|
2015-01-27 12:36:36 +00:00
|
|
|
|
2015-07-03 00:15:47 +00:00
|
|
|
start_time = get_timer(0);
|
|
|
|
while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
|
|
|
|
if (get_timer(start_time) > I2C_TIMEOUT_MS) {
|
|
|
|
debug("Timeout\n");
|
|
|
|
return -ETIMEDOUT;
|
2015-01-27 12:36:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 00:15:47 +00:00
|
|
|
for (ret = 0, i = 0; !ret && i < nmsgs; i++)
|
|
|
|
ret = s3c24x0_do_msg(i2c_bus, &msg[i], i);
|
|
|
|
|
|
|
|
/* Send STOP */
|
|
|
|
writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
|
|
|
|
read_write_byte(i2c);
|
|
|
|
|
|
|
|
return ret ? -EREMOTEIO : 0;
|
2015-01-27 12:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int s3c_i2c_ofdata_to_platdata(struct udevice *dev)
|
|
|
|
{
|
|
|
|
const void *blob = gd->fdt_blob;
|
|
|
|
struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
|
2016-11-23 13:34:43 +00:00
|
|
|
int node;
|
2015-01-27 12:36:36 +00:00
|
|
|
|
|
|
|
node = dev->of_offset;
|
|
|
|
|
2016-11-23 13:34:43 +00:00
|
|
|
i2c_bus->regs = (struct s3c24x0_i2c *)dev_get_addr(dev);
|
2015-01-27 12:36:36 +00:00
|
|
|
|
|
|
|
i2c_bus->id = pinmux_decode_periph_id(blob, node);
|
|
|
|
|
|
|
|
i2c_bus->clock_frequency = fdtdec_get_int(blob, node,
|
2015-07-03 00:15:47 +00:00
|
|
|
"clock-frequency", 100000);
|
2015-01-27 12:36:36 +00:00
|
|
|
i2c_bus->node = node;
|
|
|
|
i2c_bus->bus_num = dev->seq;
|
|
|
|
|
2016-11-23 13:34:43 +00:00
|
|
|
exynos_pinmux_config(i2c_bus->id, 0);
|
2015-01-27 12:36:36 +00:00
|
|
|
|
|
|
|
i2c_bus->active = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_i2c_ops s3c_i2c_ops = {
|
|
|
|
.xfer = s3c24x0_i2c_xfer,
|
|
|
|
.probe_chip = s3c24x0_i2c_probe,
|
|
|
|
.set_bus_speed = s3c24x0_i2c_set_bus_speed,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id s3c_i2c_ids[] = {
|
2016-11-23 13:34:43 +00:00
|
|
|
{ .compatible = "samsung,s3c2440-i2c" },
|
2015-01-27 12:36:36 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(i2c_s3c) = {
|
|
|
|
.name = "i2c_s3c",
|
|
|
|
.id = UCLASS_I2C,
|
|
|
|
.of_match = s3c_i2c_ids,
|
|
|
|
.ofdata_to_platdata = s3c_i2c_ofdata_to_platdata,
|
|
|
|
.priv_auto_alloc_size = sizeof(struct s3c24x0_i2c_bus),
|
|
|
|
.ops = &s3c_i2c_ops,
|
|
|
|
};
|