123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- #include <common.h>
- #include <errno.h>
- #include <dm.h>
- #include <fdtdec.h>
- #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
- #include <asm/arch/clk.h>
- #include <asm/arch/cpu.h>
- #include <asm/arch/pinmux.h>
- #else
- #include <asm/arch/s3c24x0_cpu.h>
- #endif
- #include <asm/io.h>
- #include <i2c.h>
- #include "s3c24x0_i2c.h"
- DECLARE_GLOBAL_DATA_PTR;
- static int WaitForXfer(struct s3c24x0_i2c *i2c)
- {
- ulong start_time = get_timer(0);
- 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);
- return I2C_NOK_TOUT;
- }
- static void read_write_byte(struct s3c24x0_i2c *i2c)
- {
- clrbits_le32(&i2c->iiccon, I2CCON_IRPND);
- }
- static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
- {
- ulong freq, pres = 16, div;
- #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
- freq = get_i2c_clk();
- #else
- freq = get_PCLK();
- #endif
-
- if ((freq / pres / (16 + 1)) > speed)
-
- pres = 512;
- div = 0;
- while ((freq / pres / (div + 1)) > speed)
- div++;
-
- writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
-
- writel(0, &i2c->iicstat);
- writel(slaveadd, &i2c->iicadd);
-
- writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
- }
- static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
- {
- struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
- i2c_bus->clock_frequency = speed;
- i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
- CONFIG_SYS_I2C_S3C24X0_SLAVE);
- return 0;
- }
- 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)
- {
- int i = 0, result;
- ulong start_time = get_timer(0);
- if (data == 0 || data_len == 0) {
-
- debug("i2c_transfer: bad call\n");
- return I2C_NOK;
- }
- while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
- if (get_timer(start_time) > I2C_TIMEOUT_MS)
- return I2C_NOK_TOUT;
- }
- writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
-
- 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);
-
- result = WaitForXfer(i2c);
- if (result != I2C_OK)
- goto bailout;
-
- if (addr && addr_len) {
- while ((i < addr_len) && (result == I2C_OK)) {
- writel(addr[i++], &i2c->iicds);
- read_write_byte(i2c);
- result = WaitForXfer(i2c);
- }
- i = 0;
- if (result != I2C_OK)
- goto bailout;
- }
- switch (cmd_type) {
- case I2C_WRITE:
- while ((i < data_len) && (result == I2C_OK)) {
- writel(data[i++], &i2c->iicds);
- read_write_byte(i2c);
- result = WaitForXfer(i2c);
- }
- break;
- case I2C_READ:
- if (addr && addr_len) {
-
- writel(chip, &i2c->iicds);
-
- writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
- &i2c->iicstat);
- read_write_byte(i2c);
- result = WaitForXfer(i2c);
- if (result != I2C_OK)
- goto bailout;
- }
- while ((i < data_len) && (result == I2C_OK)) {
-
- if (i == data_len - 1)
- writel(readl(&i2c->iiccon)
- & ~I2CCON_ACKGEN,
- &i2c->iiccon);
- read_write_byte(i2c);
- result = WaitForXfer(i2c);
- data[i++] = readl(&i2c->iicds);
- }
- if (result == I2C_NACK)
- result = I2C_OK;
- break;
- default:
- debug("i2c_transfer: bad call\n");
- result = I2C_NOK;
- break;
- }
- bailout:
-
- writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
- read_write_byte(i2c);
- return result;
- }
- static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
- {
- struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
- uchar buf[1];
- int ret;
- buf[0] = 0;
-
- ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1, 0, 0, buf, 1);
- return ret != I2C_OK;
- }
- static int s3c24x0_do_msg(struct s3c24x0_i2c_bus *i2c_bus, struct i2c_msg *msg,
- int seq)
- {
- struct s3c24x0_i2c *i2c = i2c_bus->regs;
- bool is_read = msg->flags & I2C_M_RD;
- uint status;
- uint addr;
- int ret, i;
- if (!seq)
- setbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
-
- 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);
-
- ret = WaitForXfer(i2c);
- if (ret)
- goto err;
- if (is_read) {
- for (i = 0; !ret && i < msg->len; i++) {
-
- 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;
- } else {
- for (i = 0; !ret && i < msg->len; i++) {
- writel(msg->buf[i], &i2c->iicds);
- read_write_byte(i2c);
- ret = WaitForXfer(i2c);
- }
- }
- err:
- return ret;
- }
- static int s3c24x0_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
- int nmsgs)
- {
- struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
- struct s3c24x0_i2c *i2c = i2c_bus->regs;
- ulong start_time;
- int ret, i;
- start_time = get_timer(0);
- while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
- if (get_timer(start_time) > I2C_TIMEOUT_MS) {
- debug("Timeout\n");
- return -ETIMEDOUT;
- }
- }
- for (ret = 0, i = 0; !ret && i < nmsgs; i++)
- ret = s3c24x0_do_msg(i2c_bus, &msg[i], i);
-
- writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
- read_write_byte(i2c);
- return ret ? -EREMOTEIO : 0;
- }
- 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);
- int node;
- node = dev->of_offset;
- i2c_bus->regs = (struct s3c24x0_i2c *)dev_get_addr(dev);
- i2c_bus->id = pinmux_decode_periph_id(blob, node);
- i2c_bus->clock_frequency = fdtdec_get_int(blob, node,
- "clock-frequency", 100000);
- i2c_bus->node = node;
- i2c_bus->bus_num = dev->seq;
- exynos_pinmux_config(i2c_bus->id, 0);
- 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[] = {
- { .compatible = "samsung,s3c2440-i2c" },
- { }
- };
- 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,
- };
|