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