]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
1cb8e980 WD |
2 | /* |
3 | * (C) Copyright 2002 | |
4 | * David Mueller, ELSOFT AG, [email protected] | |
1cb8e980 WD |
5 | */ |
6 | ||
8dfcbaa6 PM |
7 | #include <errno.h> |
8 | #include <dm.h> | |
a9d2ae70 | 9 | #include <fdtdec.h> |
03de305e | 10 | #include <time.h> |
f7ae49fc | 11 | #include <log.h> |
101f4e66 | 12 | #if IS_ENABLED(CONFIG_ARCH_EXYNOS4) || IS_ENABLED(CONFIG_ARCH_EXYNOS5) |
ab7e52bb RS |
13 | #include <asm/arch/clk.h> |
14 | #include <asm/arch/cpu.h> | |
a9d2ae70 | 15 | #include <asm/arch/pinmux.h> |
101f4e66 | 16 | #endif |
401d1c4f | 17 | #include <asm/global_data.h> |
eb0ae7f5 | 18 | #include <asm/io.h> |
1cb8e980 | 19 | #include <i2c.h> |
101f4e66 | 20 | #include <clk.h> |
ab7e52bb | 21 | #include "s3c24x0_i2c.h" |
1cb8e980 | 22 | |
8dfcbaa6 PM |
23 | DECLARE_GLOBAL_DATA_PTR; |
24 | ||
e4e24020 NKC |
25 | /* |
26 | * Wait til the byte transfer is completed. | |
27 | * | |
28 | * @param i2c- pointer to the appropriate i2c register bank. | |
185f812c | 29 | * Return: I2C_OK, if transmission was ACKED |
e4e24020 NKC |
30 | * I2C_NACK, if transmission was NACKED |
31 | * I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS | |
32 | */ | |
33 | ||
ab7e52bb | 34 | static int WaitForXfer(struct s3c24x0_i2c *i2c) |
1cb8e980 | 35 | { |
e4e24020 | 36 | ulong start_time = get_timer(0); |
1cb8e980 | 37 | |
e4e24020 NKC |
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); | |
1cb8e980 | 43 | |
e4e24020 | 44 | return I2C_NOK_TOUT; |
1cb8e980 WD |
45 | } |
46 | ||
26ea7685 | 47 | static void read_write_byte(struct s3c24x0_i2c *i2c) |
1cb8e980 | 48 | { |
26ea7685 | 49 | clrbits_le32(&i2c->iiccon, I2CCON_IRPND); |
1cb8e980 WD |
50 | } |
51 | ||
101f4e66 | 52 | static int i2c_ch_init(struct udevice *dev, int speed, int slaveadd) |
ab7e52bb | 53 | { |
101f4e66 DV |
54 | struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev); |
55 | struct s3c24x0_i2c *i2c = i2c_bus->regs; | |
ab7e52bb | 56 | ulong freq, pres = 16, div; |
101f4e66 DV |
57 | |
58 | #if IS_ENABLED(CONFIG_ARCH_EXYNOS4) || defined(CONFIG_ARCH_EXYNOS5) | |
ab7e52bb | 59 | freq = get_i2c_clk(); |
101f4e66 DV |
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 | |
ab7e52bb RS |
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); | |
101f4e66 | 86 | return 0; |
ab7e52bb RS |
87 | } |
88 | ||
0283da44 TR |
89 | #define SYS_I2C_S3C24X0_SLAVE_ADDR 0 |
90 | ||
8dfcbaa6 | 91 | static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed) |
2d8f1e27 | 92 | { |
9a1bff69 | 93 | struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev); |
2d8f1e27 | 94 | |
2d8f1e27 PW |
95 | i2c_bus->clock_frequency = speed; |
96 | ||
101f4e66 DV |
97 | if (i2c_ch_init(dev, i2c_bus->clock_frequency, |
98 | SYS_I2C_S3C24X0_SLAVE_ADDR)) | |
99 | return -EFAULT; | |
2d8f1e27 PW |
100 | |
101 | return 0; | |
102 | } | |
103 | ||
1cb8e980 | 104 | /* |
fc3e2165 WD |
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 | */ | |
ab7e52bb RS |
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) | |
1cb8e980 | 118 | { |
e4e24020 NKC |
119 | int i = 0, result; |
120 | ulong start_time = get_timer(0); | |
1cb8e980 | 121 | |
fc3e2165 WD |
122 | if (data == 0 || data_len == 0) { |
123 | /*Don't support data transfer of no length or to address 0 */ | |
ab7e52bb | 124 | debug("i2c_transfer: bad call\n"); |
fc3e2165 WD |
125 | return I2C_NOK; |
126 | } | |
1cb8e980 | 127 | |
e4e24020 NKC |
128 | while (readl(&i2c->iicstat) & I2CSTAT_BSY) { |
129 | if (get_timer(start_time) > I2C_TIMEOUT_MS) | |
130 | return I2C_NOK_TOUT; | |
fc3e2165 | 131 | } |
1cb8e980 | 132 | |
ab7e52bb | 133 | writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon); |
1cb8e980 | 134 | |
e4e24020 NKC |
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; | |
148 | ||
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); | |
26ea7685 | 153 | read_write_byte(i2c); |
e4e24020 | 154 | result = WaitForXfer(i2c); |
1cb8e980 | 155 | } |
e4e24020 NKC |
156 | i = 0; |
157 | if (result != I2C_OK) | |
158 | goto bailout; | |
159 | } | |
1cb8e980 | 160 | |
e4e24020 NKC |
161 | switch (cmd_type) { |
162 | case I2C_WRITE: | |
163 | while ((i < data_len) && (result == I2C_OK)) { | |
164 | writel(data[i++], &i2c->iicds); | |
26ea7685 | 165 | read_write_byte(i2c); |
ab7e52bb | 166 | result = WaitForXfer(i2c); |
e4e24020 | 167 | } |
fc3e2165 | 168 | break; |
1cb8e980 | 169 | |
48b42616 | 170 | case I2C_READ: |
fc3e2165 | 171 | if (addr && addr_len) { |
e4e24020 NKC |
172 | /* |
173 | * Register address has been sent, now send slave chip | |
174 | * address again to start the actual read transaction. | |
175 | */ | |
d9abba82 | 176 | writel(chip, &i2c->iicds); |
e4e24020 NKC |
177 | |
178 | /* Generate a re-START. */ | |
cb466c05 RS |
179 | writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP, |
180 | &i2c->iicstat); | |
26ea7685 | 181 | read_write_byte(i2c); |
ab7e52bb | 182 | result = WaitForXfer(i2c); |
fc3e2165 | 183 | |
e4e24020 NKC |
184 | if (result != I2C_OK) |
185 | goto bailout; | |
1cb8e980 | 186 | } |
1cb8e980 | 187 | |
e4e24020 NKC |
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); | |
26ea7685 | 194 | read_write_byte(i2c); |
e4e24020 NKC |
195 | result = WaitForXfer(i2c); |
196 | data[i++] = readl(&i2c->iicds); | |
197 | } | |
198 | if (result == I2C_NACK) | |
199 | result = I2C_OK; /* Normal terminated read. */ | |
fc3e2165 | 200 | break; |
1cb8e980 WD |
201 | |
202 | default: | |
ab7e52bb | 203 | debug("i2c_transfer: bad call\n"); |
fc3e2165 WD |
204 | result = I2C_NOK; |
205 | break; | |
206 | } | |
1cb8e980 | 207 | |
e4e24020 NKC |
208 | bailout: |
209 | /* Send STOP. */ | |
210 | writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat); | |
26ea7685 | 211 | read_write_byte(i2c); |
e4e24020 | 212 | |
ab7e52bb | 213 | return result; |
1cb8e980 WD |
214 | } |
215 | ||
8dfcbaa6 | 216 | static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags) |
1cb8e980 | 217 | { |
9a1bff69 | 218 | struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev); |
fc3e2165 | 219 | uchar buf[1]; |
296a461d | 220 | int ret; |
1cb8e980 | 221 | |
fc3e2165 | 222 | buf[0] = 0; |
1cb8e980 | 223 | |
fc3e2165 WD |
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 | */ | |
37b8eb37 | 229 | ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1, 0, 0, buf, 1); |
296a461d | 230 | |
296a461d | 231 | return ret != I2C_OK; |
1cb8e980 WD |
232 | } |
233 | ||
45d9ae87 SG |
234 | static int s3c24x0_do_msg(struct s3c24x0_i2c_bus *i2c_bus, struct i2c_msg *msg, |
235 | int seq) | |
8dfcbaa6 | 236 | { |
45d9ae87 SG |
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; | |
8dfcbaa6 | 242 | |
45d9ae87 SG |
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 */ | |
8dfcbaa6 | 274 | } else { |
45d9ae87 SG |
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 | } | |
8dfcbaa6 PM |
280 | } |
281 | ||
45d9ae87 SG |
282 | err: |
283 | return ret; | |
8dfcbaa6 PM |
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); | |
45d9ae87 SG |
290 | struct s3c24x0_i2c *i2c = i2c_bus->regs; |
291 | ulong start_time; | |
292 | int ret, i; | |
8dfcbaa6 | 293 | |
45d9ae87 SG |
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; | |
8dfcbaa6 | 299 | } |
8dfcbaa6 PM |
300 | } |
301 | ||
45d9ae87 SG |
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; | |
8dfcbaa6 PM |
310 | } |
311 | ||
d1998a9f | 312 | static int s3c_i2c_of_to_plat(struct udevice *dev) |
8dfcbaa6 | 313 | { |
101f4e66 | 314 | #if IS_ENABLED(CONFIG_ARCH_EXYNOS4) || IS_ENABLED(CONFIG_ARCH_EXYNOS5) |
8dfcbaa6 | 315 | const void *blob = gd->fdt_blob; |
101f4e66 | 316 | #endif |
8dfcbaa6 | 317 | struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev); |
37b8eb37 | 318 | int node; |
8dfcbaa6 | 319 | |
e160f7d4 | 320 | node = dev_of_offset(dev); |
8dfcbaa6 | 321 | |
8613c8d8 | 322 | i2c_bus->regs = dev_read_addr_ptr(dev); |
8dfcbaa6 | 323 | |
101f4e66 | 324 | #if IS_ENABLED(CONFIG_ARCH_EXYNOS4) || IS_ENABLED(CONFIG_ARCH_EXYNOS5) |
8dfcbaa6 | 325 | i2c_bus->id = pinmux_decode_periph_id(blob, node); |
101f4e66 | 326 | #endif |
8dfcbaa6 | 327 | |
f3d46152 SG |
328 | i2c_bus->clock_frequency = |
329 | dev_read_u32_default(dev, "clock-frequency", | |
330 | I2C_SPEED_STANDARD_RATE); | |
8dfcbaa6 | 331 | i2c_bus->node = node; |
8b85dfc6 | 332 | i2c_bus->bus_num = dev_seq(dev); |
8dfcbaa6 | 333 | |
101f4e66 | 334 | #if IS_ENABLED(CONFIG_ARCH_EXYNOS4) || IS_ENABLED(CONFIG_ARCH_EXYNOS5) |
37b8eb37 | 335 | exynos_pinmux_config(i2c_bus->id, 0); |
101f4e66 | 336 | #endif |
8dfcbaa6 PM |
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[] = { | |
37b8eb37 | 350 | { .compatible = "samsung,s3c2440-i2c" }, |
8dfcbaa6 PM |
351 | { } |
352 | }; | |
353 | ||
354 | U_BOOT_DRIVER(i2c_s3c) = { | |
355 | .name = "i2c_s3c", | |
356 | .id = UCLASS_I2C, | |
357 | .of_match = s3c_i2c_ids, | |
d1998a9f | 358 | .of_to_plat = s3c_i2c_of_to_plat, |
41575d8e | 359 | .priv_auto = sizeof(struct s3c24x0_i2c_bus), |
8dfcbaa6 PM |
360 | .ops = &s3c_i2c_ops, |
361 | }; |