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