2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-01-13 03:44:36 +00:00
|
|
|
/*
|
2016-07-19 12:56:13 +00:00
|
|
|
* Copyright (C) 2014 Panasonic Corporation
|
|
|
|
* Copyright (C) 2015-2016 Socionext Inc.
|
|
|
|
* Author: Masahiro Yamada <yamada.masahiro@socionext.com>
|
2015-01-13 03:44:36 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <dm/device_compat.h>
|
2017-10-13 10:21:57 +00:00
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/errno.h>
|
2015-05-29 08:30:00 +00:00
|
|
|
#include <linux/io.h>
|
2016-03-24 13:32:40 +00:00
|
|
|
#include <linux/sizes.h>
|
2017-10-13 10:21:57 +00:00
|
|
|
#include <linux/types.h>
|
|
|
|
#include <dm.h>
|
2015-01-13 03:44:36 +00:00
|
|
|
#include <fdtdec.h>
|
2017-10-13 10:21:57 +00:00
|
|
|
#include <i2c.h>
|
2015-01-13 03:44:36 +00:00
|
|
|
|
|
|
|
struct uniphier_i2c_regs {
|
|
|
|
u32 dtrm; /* data transmission */
|
|
|
|
#define I2C_DTRM_STA (1 << 10)
|
|
|
|
#define I2C_DTRM_STO (1 << 9)
|
|
|
|
#define I2C_DTRM_NACK (1 << 8)
|
|
|
|
#define I2C_DTRM_RD (1 << 0)
|
|
|
|
u32 drec; /* data reception */
|
|
|
|
#define I2C_DREC_STS (1 << 12)
|
|
|
|
#define I2C_DREC_LRB (1 << 11)
|
|
|
|
#define I2C_DREC_LAB (1 << 9)
|
|
|
|
u32 myad; /* slave address */
|
|
|
|
u32 clk; /* clock frequency control */
|
|
|
|
u32 brst; /* bus reset */
|
|
|
|
#define I2C_BRST_FOEN (1 << 1)
|
|
|
|
#define I2C_BRST_BRST (1 << 0)
|
|
|
|
u32 hold; /* hold time control */
|
|
|
|
u32 bsts; /* bus status monitor */
|
|
|
|
u32 noise; /* noise filter control */
|
|
|
|
u32 setup; /* setup time control */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define IOBUS_FREQ 100000000
|
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
struct uniphier_i2c_priv {
|
|
|
|
struct udevice *dev;
|
2015-01-13 03:44:36 +00:00
|
|
|
struct uniphier_i2c_regs __iomem *regs; /* register base */
|
|
|
|
unsigned long input_clk; /* master clock (Hz) */
|
|
|
|
unsigned long wait_us; /* wait for every byte transfer (us) */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int uniphier_i2c_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
fdt_addr_t addr;
|
2017-10-13 10:21:57 +00:00
|
|
|
struct uniphier_i2c_priv *priv = dev_get_priv(dev);
|
2015-01-13 03:44:36 +00:00
|
|
|
|
2020-07-24 12:42:06 +00:00
|
|
|
addr = devfdt_get_addr(dev);
|
2016-03-24 13:32:40 +00:00
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return -EINVAL;
|
2015-01-13 03:44:36 +00:00
|
|
|
|
2016-07-19 12:56:13 +00:00
|
|
|
priv->regs = devm_ioremap(dev, addr, SZ_64);
|
2015-01-13 03:44:36 +00:00
|
|
|
if (!priv->regs)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
priv->input_clk = IOBUS_FREQ;
|
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
priv->dev = dev;
|
|
|
|
|
2015-01-13 03:44:36 +00:00
|
|
|
/* deassert reset */
|
|
|
|
writel(0x3, &priv->regs->brst);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
static int send_and_recv_byte(struct uniphier_i2c_priv *priv, u32 dtrm)
|
2015-01-13 03:44:36 +00:00
|
|
|
{
|
2017-10-13 10:21:57 +00:00
|
|
|
writel(dtrm, &priv->regs->dtrm);
|
2015-01-13 03:44:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This controller only provides interruption to inform the completion
|
|
|
|
* of each byte transfer. (No status register to poll it.)
|
|
|
|
* Unfortunately, U-Boot does not have a good support of interrupt.
|
|
|
|
* Wait for a while.
|
|
|
|
*/
|
2017-10-13 10:21:57 +00:00
|
|
|
udelay(priv->wait_us);
|
2015-01-13 03:44:36 +00:00
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
return readl(&priv->regs->drec);
|
2015-01-13 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
static int send_byte(struct uniphier_i2c_priv *priv, u32 dtrm, bool *stop)
|
2015-01-13 03:44:36 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
u32 drec;
|
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
drec = send_and_recv_byte(priv, dtrm);
|
2015-01-13 03:44:36 +00:00
|
|
|
|
|
|
|
if (drec & I2C_DREC_LAB) {
|
2017-10-13 10:21:57 +00:00
|
|
|
dev_dbg(priv->dev, "uniphier_i2c: bus arbitration failed\n");
|
2015-01-13 03:44:36 +00:00
|
|
|
*stop = false;
|
|
|
|
ret = -EREMOTEIO;
|
|
|
|
}
|
|
|
|
if (drec & I2C_DREC_LRB) {
|
2017-10-13 10:21:57 +00:00
|
|
|
dev_dbg(priv->dev, "uniphier_i2c: slave did not return ACK\n");
|
2015-01-13 03:44:36 +00:00
|
|
|
ret = -EREMOTEIO;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
static int uniphier_i2c_transmit(struct uniphier_i2c_priv *priv, uint addr,
|
2015-01-13 03:44:36 +00:00
|
|
|
uint len, const u8 *buf, bool *stop)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
|
2015-01-13 03:44:36 +00:00
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
ret = send_byte(priv, I2C_DTRM_STA | I2C_DTRM_NACK | addr << 1, stop);
|
2015-01-13 03:44:36 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
while (len--) {
|
2017-10-13 10:21:57 +00:00
|
|
|
ret = send_byte(priv, I2C_DTRM_NACK | *buf++, stop);
|
2015-01-13 03:44:36 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (*stop)
|
2017-10-13 10:21:57 +00:00
|
|
|
writel(I2C_DTRM_STO | I2C_DTRM_NACK, &priv->regs->dtrm);
|
2015-01-13 03:44:36 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
static int uniphier_i2c_receive(struct uniphier_i2c_priv *priv, uint addr,
|
2015-01-13 03:44:36 +00:00
|
|
|
uint len, u8 *buf, bool *stop)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
|
2015-01-13 03:44:36 +00:00
|
|
|
|
2017-10-13 10:21:57 +00:00
|
|
|
ret = send_byte(priv, I2C_DTRM_STA | I2C_DTRM_NACK |
|
2015-01-13 03:44:36 +00:00
|
|
|
I2C_DTRM_RD | addr << 1, stop);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
while (len--)
|
2017-10-13 10:21:57 +00:00
|
|
|
*buf++ = send_and_recv_byte(priv, len ? 0 : I2C_DTRM_NACK);
|
2015-01-13 03:44:36 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (*stop)
|
2017-10-13 10:21:57 +00:00
|
|
|
writel(I2C_DTRM_STO | I2C_DTRM_NACK, &priv->regs->dtrm);
|
2015-01-13 03:44:36 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uniphier_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
|
|
|
|
int nmsgs)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2017-10-13 10:21:57 +00:00
|
|
|
struct uniphier_i2c_priv *priv = dev_get_priv(bus);
|
2015-01-13 03:44:36 +00:00
|
|
|
bool stop;
|
|
|
|
|
|
|
|
for (; nmsgs > 0; nmsgs--, msg++) {
|
|
|
|
/* If next message is read, skip the stop condition */
|
|
|
|
stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
|
|
|
|
|
|
|
|
if (msg->flags & I2C_M_RD)
|
2017-10-13 10:21:57 +00:00
|
|
|
ret = uniphier_i2c_receive(priv, msg->addr, msg->len,
|
2015-01-13 03:44:36 +00:00
|
|
|
msg->buf, &stop);
|
|
|
|
else
|
2017-10-13 10:21:57 +00:00
|
|
|
ret = uniphier_i2c_transmit(priv, msg->addr, msg->len,
|
2015-01-13 03:44:36 +00:00
|
|
|
msg->buf, &stop);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uniphier_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
|
|
|
|
{
|
2017-10-13 10:21:57 +00:00
|
|
|
struct uniphier_i2c_priv *priv = dev_get_priv(bus);
|
2015-01-13 03:44:36 +00:00
|
|
|
|
|
|
|
/* max supported frequency is 400 kHz */
|
2020-01-23 18:48:22 +00:00
|
|
|
if (speed > I2C_SPEED_FAST_RATE)
|
2015-01-13 03:44:36 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* bus reset: make sure the bus is idle when change the frequency */
|
|
|
|
writel(0x1, &priv->regs->brst);
|
|
|
|
|
|
|
|
writel((priv->input_clk / speed / 2 << 16) | (priv->input_clk / speed),
|
|
|
|
&priv->regs->clk);
|
|
|
|
|
|
|
|
writel(0x3, &priv->regs->brst);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Theoretically, each byte can be transferred in
|
|
|
|
* 1000000 * 9 / speed usec. For safety, wait more than double.
|
|
|
|
*/
|
|
|
|
priv->wait_us = 20000000 / speed;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const struct dm_i2c_ops uniphier_i2c_ops = {
|
|
|
|
.xfer = uniphier_i2c_xfer,
|
|
|
|
.set_bus_speed = uniphier_i2c_set_bus_speed,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id uniphier_i2c_of_match[] = {
|
2015-03-11 06:54:46 +00:00
|
|
|
{ .compatible = "socionext,uniphier-i2c" },
|
|
|
|
{ /* sentinel */ }
|
2015-01-13 03:44:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(uniphier_i2c) = {
|
|
|
|
.name = "uniphier-i2c",
|
|
|
|
.id = UCLASS_I2C,
|
|
|
|
.of_match = uniphier_i2c_of_match,
|
|
|
|
.probe = uniphier_i2c_probe,
|
2017-10-13 10:21:57 +00:00
|
|
|
.priv_auto_alloc_size = sizeof(struct uniphier_i2c_priv),
|
2015-01-13 03:44:36 +00:00
|
|
|
.ops = &uniphier_i2c_ops,
|
|
|
|
};
|