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