123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- #include <common.h>
- #include <linux/types.h>
- #include <linux/io.h>
- #include <linux/sizes.h>
- #include <linux/errno.h>
- #include <dm/device.h>
- #include <dm/root.h>
- #include <i2c.h>
- #include <fdtdec.h>
- struct uniphier_i2c_regs {
- u32 dtrm;
- #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;
- #define I2C_DREC_STS (1 << 12)
- #define I2C_DREC_LRB (1 << 11)
- #define I2C_DREC_LAB (1 << 9)
- u32 myad;
- u32 clk;
- u32 brst;
- #define I2C_BRST_FOEN (1 << 1)
- #define I2C_BRST_BRST (1 << 0)
- u32 hold;
- u32 bsts;
- u32 noise;
- u32 setup;
- };
- #define IOBUS_FREQ 100000000
- struct uniphier_i2c_dev {
- struct uniphier_i2c_regs __iomem *regs;
- unsigned long input_clk;
- unsigned long wait_us;
- };
- static int uniphier_i2c_probe(struct udevice *dev)
- {
- fdt_addr_t addr;
- struct uniphier_i2c_dev *priv = dev_get_priv(dev);
- addr = dev_get_addr(dev);
- if (addr == FDT_ADDR_T_NONE)
- return -EINVAL;
- priv->regs = devm_ioremap(dev, addr, SZ_64);
- if (!priv->regs)
- return -ENOMEM;
- priv->input_clk = IOBUS_FREQ;
-
- writel(0x3, &priv->regs->brst);
- return 0;
- }
- static int send_and_recv_byte(struct uniphier_i2c_dev *dev, u32 dtrm)
- {
- writel(dtrm, &dev->regs->dtrm);
-
- udelay(dev->wait_us);
- return readl(&dev->regs->drec);
- }
- static int send_byte(struct uniphier_i2c_dev *dev, u32 dtrm, bool *stop)
- {
- int ret = 0;
- u32 drec;
- drec = send_and_recv_byte(dev, dtrm);
- if (drec & I2C_DREC_LAB) {
- debug("uniphier_i2c: bus arbitration failed\n");
- *stop = false;
- ret = -EREMOTEIO;
- }
- if (drec & I2C_DREC_LRB) {
- debug("uniphier_i2c: slave did not return ACK\n");
- ret = -EREMOTEIO;
- }
- return ret;
- }
- static int uniphier_i2c_transmit(struct uniphier_i2c_dev *dev, uint addr,
- uint len, const u8 *buf, bool *stop)
- {
- int ret;
- debug("%s: addr = %x, len = %d\n", __func__, addr, len);
- ret = send_byte(dev, I2C_DTRM_STA | I2C_DTRM_NACK | addr << 1, stop);
- if (ret < 0)
- goto fail;
- while (len--) {
- ret = send_byte(dev, I2C_DTRM_NACK | *buf++, stop);
- if (ret < 0)
- goto fail;
- }
- fail:
- if (*stop)
- writel(I2C_DTRM_STO | I2C_DTRM_NACK, &dev->regs->dtrm);
- return ret;
- }
- static int uniphier_i2c_receive(struct uniphier_i2c_dev *dev, uint addr,
- uint len, u8 *buf, bool *stop)
- {
- int ret;
- debug("%s: addr = %x, len = %d\n", __func__, addr, len);
- ret = send_byte(dev, I2C_DTRM_STA | I2C_DTRM_NACK |
- I2C_DTRM_RD | addr << 1, stop);
- if (ret < 0)
- goto fail;
- while (len--)
- *buf++ = send_and_recv_byte(dev, len ? 0 : I2C_DTRM_NACK);
- fail:
- if (*stop)
- writel(I2C_DTRM_STO | I2C_DTRM_NACK, &dev->regs->dtrm);
- return ret;
- }
- static int uniphier_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
- int nmsgs)
- {
- int ret = 0;
- struct uniphier_i2c_dev *dev = dev_get_priv(bus);
- bool stop;
- for (; nmsgs > 0; nmsgs--, msg++) {
-
- stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
- if (msg->flags & I2C_M_RD)
- ret = uniphier_i2c_receive(dev, msg->addr, msg->len,
- msg->buf, &stop);
- else
- ret = uniphier_i2c_transmit(dev, msg->addr, msg->len,
- msg->buf, &stop);
- if (ret < 0)
- break;
- }
- return ret;
- }
- static int uniphier_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
- {
- struct uniphier_i2c_dev *priv = dev_get_priv(bus);
-
- if (speed > 400000)
- return -EINVAL;
-
- writel(0x1, &priv->regs->brst);
- writel((priv->input_clk / speed / 2 << 16) | (priv->input_clk / speed),
- &priv->regs->clk);
- writel(0x3, &priv->regs->brst);
-
- 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[] = {
- { .compatible = "socionext,uniphier-i2c" },
- { }
- };
- U_BOOT_DRIVER(uniphier_i2c) = {
- .name = "uniphier-i2c",
- .id = UCLASS_I2C,
- .of_match = uniphier_i2c_of_match,
- .probe = uniphier_i2c_probe,
- .priv_auto_alloc_size = sizeof(struct uniphier_i2c_dev),
- .ops = &uniphier_i2c_ops,
- };
|