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