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