wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * David Mueller, ELSOFT AG, d.mueller@elsoft.ch |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* This code should work for both the S3C2400 and the S3C2410 |
| 9 | * as they seem to have the same I2C controller inside. |
| 10 | * The different address mapping is handled by the s3c24xx.h files below. |
| 11 | */ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 12 | #include <common.h> |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 13 | #include <errno.h> |
| 14 | #include <dm.h> |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 15 | #include <fdtdec.h> |
Piotr Wilczek | b35cd1c | 2012-11-20 02:19:05 +0000 | [diff] [blame] | 16 | #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 17 | #include <asm/arch/clk.h> |
| 18 | #include <asm/arch/cpu.h> |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 19 | #include <asm/arch/pinmux.h> |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 20 | #else |
kevin.morfitt@fearnside-systems.co.uk | e0d8131 | 2009-11-17 18:30:34 +0900 | [diff] [blame] | 21 | #include <asm/arch/s3c24x0_cpu.h> |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 22 | #endif |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 23 | #include <asm/io.h> |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 24 | #include <i2c.h> |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 25 | #include "s3c24x0_i2c.h" |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 26 | |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 29 | /* |
| 30 | * Wait til the byte transfer is completed. |
| 31 | * |
| 32 | * @param i2c- pointer to the appropriate i2c register bank. |
| 33 | * @return I2C_OK, if transmission was ACKED |
| 34 | * I2C_NACK, if transmission was NACKED |
| 35 | * I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS |
| 36 | */ |
| 37 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 38 | static int WaitForXfer(struct s3c24x0_i2c *i2c) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 39 | { |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 40 | ulong start_time = get_timer(0); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 41 | |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 42 | do { |
| 43 | if (readl(&i2c->iiccon) & I2CCON_IRPND) |
| 44 | return (readl(&i2c->iicstat) & I2CSTAT_NACK) ? |
| 45 | I2C_NACK : I2C_OK; |
| 46 | } while (get_timer(start_time) < I2C_TIMEOUT_MS); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 47 | |
Naveen Krishna Ch | 64a191f | 2013-10-15 16:02:44 +0530 | [diff] [blame] | 48 | return I2C_NOK_TOUT; |
| 49 | } |
| 50 | |
Simon Glass | 824802d | 2015-07-02 18:15:46 -0600 | [diff] [blame] | 51 | static void read_write_byte(struct s3c24x0_i2c *i2c) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 52 | { |
Simon Glass | 824802d | 2015-07-02 18:15:46 -0600 | [diff] [blame] | 53 | clrbits_le32(&i2c->iiccon, I2CCON_IRPND); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 54 | } |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 55 | |
| 56 | static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd) |
| 57 | { |
| 58 | ulong freq, pres = 16, div; |
Piotr Wilczek | b35cd1c | 2012-11-20 02:19:05 +0000 | [diff] [blame] | 59 | #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 60 | freq = get_i2c_clk(); |
| 61 | #else |
| 62 | freq = get_PCLK(); |
| 63 | #endif |
| 64 | /* calculate prescaler and divisor values */ |
| 65 | if ((freq / pres / (16 + 1)) > speed) |
| 66 | /* set prescaler to 512 */ |
| 67 | pres = 512; |
| 68 | |
| 69 | div = 0; |
| 70 | while ((freq / pres / (div + 1)) > speed) |
| 71 | div++; |
| 72 | |
| 73 | /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */ |
| 74 | writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon); |
| 75 | |
| 76 | /* init to SLAVE REVEIVE and set slaveaddr */ |
| 77 | writel(0, &i2c->iicstat); |
| 78 | writel(slaveadd, &i2c->iicadd); |
| 79 | /* program Master Transmit (and implicit STOP) */ |
| 80 | writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat); |
| 81 | } |
Naveen Krishna Ch | 64a191f | 2013-10-15 16:02:44 +0530 | [diff] [blame] | 82 | |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 83 | static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed) |
Piotr Wilczek | 58bbd2b | 2013-11-20 10:43:49 +0100 | [diff] [blame] | 84 | { |
Simon Glass | 365c3da | 2016-11-23 06:34:42 -0700 | [diff] [blame] | 85 | struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev); |
Piotr Wilczek | 58bbd2b | 2013-11-20 10:43:49 +0100 | [diff] [blame] | 86 | |
Piotr Wilczek | 58bbd2b | 2013-11-20 10:43:49 +0100 | [diff] [blame] | 87 | i2c_bus->clock_frequency = speed; |
| 88 | |
Simon Glass | b9d7f99 | 2016-11-23 06:34:43 -0700 | [diff] [blame] | 89 | i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency, |
| 90 | CONFIG_SYS_I2C_S3C24X0_SLAVE); |
Piotr Wilczek | 58bbd2b | 2013-11-20 10:43:49 +0100 | [diff] [blame] | 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Naveen Krishna Ch | 64a191f | 2013-10-15 16:02:44 +0530 | [diff] [blame] | 95 | /* |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 96 | * cmd_type is 0 for write, 1 for read. |
| 97 | * |
| 98 | * addr_len can take any value from 0-255, it is only limited |
| 99 | * by the char, we could make it larger if needed. If it is |
| 100 | * 0 we skip the address write cycle. |
| 101 | */ |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 102 | static int i2c_transfer(struct s3c24x0_i2c *i2c, |
| 103 | unsigned char cmd_type, |
| 104 | unsigned char chip, |
| 105 | unsigned char addr[], |
| 106 | unsigned char addr_len, |
| 107 | unsigned char data[], |
| 108 | unsigned short data_len) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 109 | { |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 110 | int i = 0, result; |
| 111 | ulong start_time = get_timer(0); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 112 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 113 | if (data == 0 || data_len == 0) { |
| 114 | /*Don't support data transfer of no length or to address 0 */ |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 115 | debug("i2c_transfer: bad call\n"); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 116 | return I2C_NOK; |
| 117 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 118 | |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 119 | while (readl(&i2c->iicstat) & I2CSTAT_BSY) { |
| 120 | if (get_timer(start_time) > I2C_TIMEOUT_MS) |
| 121 | return I2C_NOK_TOUT; |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 122 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 123 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 124 | writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 125 | |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 126 | /* Get the slave chip address going */ |
| 127 | writel(chip, &i2c->iicds); |
| 128 | if ((cmd_type == I2C_WRITE) || (addr && addr_len)) |
| 129 | writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP, |
| 130 | &i2c->iicstat); |
| 131 | else |
| 132 | writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP, |
| 133 | &i2c->iicstat); |
| 134 | |
| 135 | /* Wait for chip address to transmit. */ |
| 136 | result = WaitForXfer(i2c); |
| 137 | if (result != I2C_OK) |
| 138 | goto bailout; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 139 | |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 140 | /* If register address needs to be transmitted - do it now. */ |
| 141 | if (addr && addr_len) { |
| 142 | while ((i < addr_len) && (result == I2C_OK)) { |
| 143 | writel(addr[i++], &i2c->iicds); |
Simon Glass | 824802d | 2015-07-02 18:15:46 -0600 | [diff] [blame] | 144 | read_write_byte(i2c); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 145 | result = WaitForXfer(i2c); |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 146 | } |
| 147 | i = 0; |
| 148 | if (result != I2C_OK) |
| 149 | goto bailout; |
| 150 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 151 | |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 152 | switch (cmd_type) { |
| 153 | case I2C_WRITE: |
| 154 | while ((i < data_len) && (result == I2C_OK)) { |
| 155 | writel(data[i++], &i2c->iicds); |
Simon Glass | 824802d | 2015-07-02 18:15:46 -0600 | [diff] [blame] | 156 | read_write_byte(i2c); |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 157 | result = WaitForXfer(i2c); |
| 158 | } |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 159 | break; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 160 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 161 | case I2C_READ: |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 162 | if (addr && addr_len) { |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 163 | /* |
| 164 | * Register address has been sent, now send slave chip |
| 165 | * address again to start the actual read transaction. |
| 166 | */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 167 | writel(chip, &i2c->iicds); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 168 | |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 169 | /* Generate a re-START. */ |
Rajeshwari Shinde | e076adf | 2013-02-19 02:19:45 +0000 | [diff] [blame] | 170 | writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP, |
| 171 | &i2c->iicstat); |
Simon Glass | 824802d | 2015-07-02 18:15:46 -0600 | [diff] [blame] | 172 | read_write_byte(i2c); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 173 | result = WaitForXfer(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 174 | |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 175 | if (result != I2C_OK) |
| 176 | goto bailout; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 177 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 178 | |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 179 | while ((i < data_len) && (result == I2C_OK)) { |
| 180 | /* disable ACK for final READ */ |
| 181 | if (i == data_len - 1) |
| 182 | writel(readl(&i2c->iiccon) |
| 183 | & ~I2CCON_ACKGEN, |
| 184 | &i2c->iiccon); |
Simon Glass | 824802d | 2015-07-02 18:15:46 -0600 | [diff] [blame] | 185 | read_write_byte(i2c); |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 186 | result = WaitForXfer(i2c); |
| 187 | data[i++] = readl(&i2c->iicds); |
| 188 | } |
| 189 | if (result == I2C_NACK) |
| 190 | result = I2C_OK; /* Normal terminated read. */ |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 191 | break; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 192 | |
| 193 | default: |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 194 | debug("i2c_transfer: bad call\n"); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 195 | result = I2C_NOK; |
| 196 | break; |
| 197 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 198 | |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 199 | bailout: |
| 200 | /* Send STOP. */ |
| 201 | writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat); |
Simon Glass | 824802d | 2015-07-02 18:15:46 -0600 | [diff] [blame] | 202 | read_write_byte(i2c); |
Naveen Krishna Ch | 40e1e7b | 2013-10-15 16:01:43 +0530 | [diff] [blame] | 203 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 204 | return result; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 207 | static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 208 | { |
Simon Glass | 365c3da | 2016-11-23 06:34:42 -0700 | [diff] [blame] | 209 | struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 210 | uchar buf[1]; |
Naveen Krishna Ch | 64a191f | 2013-10-15 16:02:44 +0530 | [diff] [blame] | 211 | int ret; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 212 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 213 | buf[0] = 0; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 214 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 215 | /* |
| 216 | * What is needed is to send the chip address and verify that the |
| 217 | * address was <ACK>ed (i.e. there was a chip at that address which |
| 218 | * drove the data line low). |
| 219 | */ |
Simon Glass | b9d7f99 | 2016-11-23 06:34:43 -0700 | [diff] [blame] | 220 | ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1, 0, 0, buf, 1); |
Naveen Krishna Ch | 64a191f | 2013-10-15 16:02:44 +0530 | [diff] [blame] | 221 | |
Naveen Krishna Ch | 64a191f | 2013-10-15 16:02:44 +0530 | [diff] [blame] | 222 | return ret != I2C_OK; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Simon Glass | e3b8c86 | 2015-07-02 18:15:47 -0600 | [diff] [blame] | 225 | static int s3c24x0_do_msg(struct s3c24x0_i2c_bus *i2c_bus, struct i2c_msg *msg, |
| 226 | int seq) |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 227 | { |
Simon Glass | e3b8c86 | 2015-07-02 18:15:47 -0600 | [diff] [blame] | 228 | struct s3c24x0_i2c *i2c = i2c_bus->regs; |
| 229 | bool is_read = msg->flags & I2C_M_RD; |
| 230 | uint status; |
| 231 | uint addr; |
| 232 | int ret, i; |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 233 | |
Simon Glass | e3b8c86 | 2015-07-02 18:15:47 -0600 | [diff] [blame] | 234 | if (!seq) |
| 235 | setbits_le32(&i2c->iiccon, I2CCON_ACKGEN); |
| 236 | |
| 237 | /* Get the slave chip address going */ |
| 238 | addr = msg->addr << 1; |
| 239 | writel(addr, &i2c->iicds); |
| 240 | status = I2C_TXRX_ENA | I2C_START_STOP; |
| 241 | if (is_read) |
| 242 | status |= I2C_MODE_MR; |
| 243 | else |
| 244 | status |= I2C_MODE_MT; |
| 245 | writel(status, &i2c->iicstat); |
| 246 | if (seq) |
| 247 | read_write_byte(i2c); |
| 248 | |
| 249 | /* Wait for chip address to transmit */ |
| 250 | ret = WaitForXfer(i2c); |
| 251 | if (ret) |
| 252 | goto err; |
| 253 | |
| 254 | if (is_read) { |
| 255 | for (i = 0; !ret && i < msg->len; i++) { |
| 256 | /* disable ACK for final READ */ |
| 257 | if (i == msg->len - 1) |
| 258 | clrbits_le32(&i2c->iiccon, I2CCON_ACKGEN); |
| 259 | read_write_byte(i2c); |
| 260 | ret = WaitForXfer(i2c); |
| 261 | msg->buf[i] = readl(&i2c->iicds); |
| 262 | } |
| 263 | if (ret == I2C_NACK) |
| 264 | ret = I2C_OK; /* Normal terminated read */ |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 265 | } else { |
Simon Glass | e3b8c86 | 2015-07-02 18:15:47 -0600 | [diff] [blame] | 266 | for (i = 0; !ret && i < msg->len; i++) { |
| 267 | writel(msg->buf[i], &i2c->iicds); |
| 268 | read_write_byte(i2c); |
| 269 | ret = WaitForXfer(i2c); |
| 270 | } |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Simon Glass | e3b8c86 | 2015-07-02 18:15:47 -0600 | [diff] [blame] | 273 | err: |
| 274 | return ret; |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static int s3c24x0_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, |
| 278 | int nmsgs) |
| 279 | { |
| 280 | struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev); |
Simon Glass | e3b8c86 | 2015-07-02 18:15:47 -0600 | [diff] [blame] | 281 | struct s3c24x0_i2c *i2c = i2c_bus->regs; |
| 282 | ulong start_time; |
| 283 | int ret, i; |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 284 | |
Simon Glass | e3b8c86 | 2015-07-02 18:15:47 -0600 | [diff] [blame] | 285 | start_time = get_timer(0); |
| 286 | while (readl(&i2c->iicstat) & I2CSTAT_BSY) { |
| 287 | if (get_timer(start_time) > I2C_TIMEOUT_MS) { |
| 288 | debug("Timeout\n"); |
| 289 | return -ETIMEDOUT; |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 290 | } |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Simon Glass | e3b8c86 | 2015-07-02 18:15:47 -0600 | [diff] [blame] | 293 | for (ret = 0, i = 0; !ret && i < nmsgs; i++) |
| 294 | ret = s3c24x0_do_msg(i2c_bus, &msg[i], i); |
| 295 | |
| 296 | /* Send STOP */ |
| 297 | writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat); |
| 298 | read_write_byte(i2c); |
| 299 | |
| 300 | return ret ? -EREMOTEIO : 0; |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static int s3c_i2c_ofdata_to_platdata(struct udevice *dev) |
| 304 | { |
| 305 | const void *blob = gd->fdt_blob; |
| 306 | struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev); |
Simon Glass | b9d7f99 | 2016-11-23 06:34:43 -0700 | [diff] [blame] | 307 | int node; |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 308 | |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 309 | node = dev->of_offset; |
| 310 | |
Simon Glass | b9d7f99 | 2016-11-23 06:34:43 -0700 | [diff] [blame] | 311 | i2c_bus->regs = (struct s3c24x0_i2c *)dev_get_addr(dev); |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 312 | |
| 313 | i2c_bus->id = pinmux_decode_periph_id(blob, node); |
| 314 | |
| 315 | i2c_bus->clock_frequency = fdtdec_get_int(blob, node, |
Simon Glass | e3b8c86 | 2015-07-02 18:15:47 -0600 | [diff] [blame] | 316 | "clock-frequency", 100000); |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 317 | i2c_bus->node = node; |
| 318 | i2c_bus->bus_num = dev->seq; |
| 319 | |
Simon Glass | b9d7f99 | 2016-11-23 06:34:43 -0700 | [diff] [blame] | 320 | exynos_pinmux_config(i2c_bus->id, 0); |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 321 | |
| 322 | i2c_bus->active = true; |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static const struct dm_i2c_ops s3c_i2c_ops = { |
| 328 | .xfer = s3c24x0_i2c_xfer, |
| 329 | .probe_chip = s3c24x0_i2c_probe, |
| 330 | .set_bus_speed = s3c24x0_i2c_set_bus_speed, |
| 331 | }; |
| 332 | |
| 333 | static const struct udevice_id s3c_i2c_ids[] = { |
Simon Glass | b9d7f99 | 2016-11-23 06:34:43 -0700 | [diff] [blame] | 334 | { .compatible = "samsung,s3c2440-i2c" }, |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 335 | { } |
| 336 | }; |
| 337 | |
| 338 | U_BOOT_DRIVER(i2c_s3c) = { |
| 339 | .name = "i2c_s3c", |
| 340 | .id = UCLASS_I2C, |
| 341 | .of_match = s3c_i2c_ids, |
| 342 | .ofdata_to_platdata = s3c_i2c_ofdata_to_platdata, |
Przemyslaw Marczak | 2a4f811 | 2015-01-27 13:36:36 +0100 | [diff] [blame] | 343 | .priv_auto_alloc_size = sizeof(struct s3c24x0_i2c_bus), |
| 344 | .ops = &s3c_i2c_ops, |
| 345 | }; |