blob: ade1ad6cef7d98f88d50107b8a681caf5eb7b178 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk1fe2c702003-03-06 21:55:29 +00002/*
3 * (C) Copyright 2002
4 * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
wdenk1fe2c702003-03-06 21:55:29 +00005 */
6
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +01007#include <errno.h>
8#include <dm.h>
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00009#include <fdtdec.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060010#include <time.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
David Virage2ec1422024-08-02 21:19:16 +020012#if IS_ENABLED(CONFIG_ARCH_EXYNOS4) || IS_ENABLED(CONFIG_ARCH_EXYNOS5)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000013#include <asm/arch/clk.h>
14#include <asm/arch/cpu.h>
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +000015#include <asm/arch/pinmux.h>
David Virage2ec1422024-08-02 21:19:16 +020016#endif
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090018#include <asm/io.h>
wdenk1fe2c702003-03-06 21:55:29 +000019#include <i2c.h>
David Virage2ec1422024-08-02 21:19:16 +020020#include <clk.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000021#include "s3c24x0_i2c.h"
wdenk1fe2c702003-03-06 21:55:29 +000022
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +010023DECLARE_GLOBAL_DATA_PTR;
24
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +053025/*
26 * Wait til the byte transfer is completed.
27 *
28 * @param i2c- pointer to the appropriate i2c register bank.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010029 * Return: I2C_OK, if transmission was ACKED
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +053030 * I2C_NACK, if transmission was NACKED
31 * I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS
32 */
33
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000034static int WaitForXfer(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +000035{
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +053036 ulong start_time = get_timer(0);
wdenk1fe2c702003-03-06 21:55:29 +000037
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +053038 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);
wdenk1fe2c702003-03-06 21:55:29 +000043
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +053044 return I2C_NOK_TOUT;
45}
46
Simon Glass824802d2015-07-02 18:15:46 -060047static void read_write_byte(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +000048{
Simon Glass824802d2015-07-02 18:15:46 -060049 clrbits_le32(&i2c->iiccon, I2CCON_IRPND);
wdenk1fe2c702003-03-06 21:55:29 +000050}
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000051
David Virage2ec1422024-08-02 21:19:16 +020052static int i2c_ch_init(struct udevice *dev, int speed, int slaveadd)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000053{
David Virage2ec1422024-08-02 21:19:16 +020054 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
55 struct s3c24x0_i2c *i2c = i2c_bus->regs;
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000056 ulong freq, pres = 16, div;
David Virage2ec1422024-08-02 21:19:16 +020057
58#if IS_ENABLED(CONFIG_ARCH_EXYNOS4) || defined(CONFIG_ARCH_EXYNOS5)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000059 freq = get_i2c_clk();
David Virage2ec1422024-08-02 21:19:16 +020060#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 Shinde4b4480a2012-07-23 21:23:53 +000069 /* 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 Virage2ec1422024-08-02 21:19:16 +020086 return 0;
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000087}
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +053088
Tom Rinifc917de2021-08-17 17:59:42 -040089#define SYS_I2C_S3C24X0_SLAVE_ADDR 0
90
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +010091static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +010092{
Simon Glass365c3da2016-11-23 06:34:42 -070093 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +010094
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +010095 i2c_bus->clock_frequency = speed;
96
David Virage2ec1422024-08-02 21:19:16 +020097 if (i2c_ch_init(dev, i2c_bus->clock_frequency,
98 SYS_I2C_S3C24X0_SLAVE_ADDR))
99 return -EFAULT;
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +0100100
101 return 0;
102}
103
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530104/*
wdenk49c3f672003-10-08 22:33:00 +0000105 * 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 Shinde4b4480a2012-07-23 21:23:53 +0000111static 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)
wdenk1fe2c702003-03-06 21:55:29 +0000118{
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530119 int i = 0, result;
120 ulong start_time = get_timer(0);
wdenk1fe2c702003-03-06 21:55:29 +0000121
wdenk49c3f672003-10-08 22:33:00 +0000122 if (data == 0 || data_len == 0) {
123 /*Don't support data transfer of no length or to address 0 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000124 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000125 return I2C_NOK;
126 }
wdenk1fe2c702003-03-06 21:55:29 +0000127
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530128 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
129 if (get_timer(start_time) > I2C_TIMEOUT_MS)
130 return I2C_NOK_TOUT;
wdenk49c3f672003-10-08 22:33:00 +0000131 }
wdenk1fe2c702003-03-06 21:55:29 +0000132
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000133 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
wdenk1fe2c702003-03-06 21:55:29 +0000134
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530135 /* 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;
wdenk1fe2c702003-03-06 21:55:29 +0000148
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530149 /* 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 Glass824802d2015-07-02 18:15:46 -0600153 read_write_byte(i2c);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000154 result = WaitForXfer(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530155 }
156 i = 0;
157 if (result != I2C_OK)
158 goto bailout;
159 }
wdenk1fe2c702003-03-06 21:55:29 +0000160
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530161 switch (cmd_type) {
162 case I2C_WRITE:
163 while ((i < data_len) && (result == I2C_OK)) {
164 writel(data[i++], &i2c->iicds);
Simon Glass824802d2015-07-02 18:15:46 -0600165 read_write_byte(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530166 result = WaitForXfer(i2c);
167 }
wdenk49c3f672003-10-08 22:33:00 +0000168 break;
wdenk1fe2c702003-03-06 21:55:29 +0000169
wdenk7539dea2003-06-19 23:01:32 +0000170 case I2C_READ:
wdenk49c3f672003-10-08 22:33:00 +0000171 if (addr && addr_len) {
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530172 /*
173 * Register address has been sent, now send slave chip
174 * address again to start the actual read transaction.
175 */
C Nauman383c43e2010-10-26 23:04:31 +0900176 writel(chip, &i2c->iicds);
wdenk1fe2c702003-03-06 21:55:29 +0000177
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530178 /* Generate a re-START. */
Rajeshwari Shindee076adf2013-02-19 02:19:45 +0000179 writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
180 &i2c->iicstat);
Simon Glass824802d2015-07-02 18:15:46 -0600181 read_write_byte(i2c);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000182 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000183
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530184 if (result != I2C_OK)
185 goto bailout;
wdenk1fe2c702003-03-06 21:55:29 +0000186 }
wdenk1fe2c702003-03-06 21:55:29 +0000187
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530188 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 Glass824802d2015-07-02 18:15:46 -0600194 read_write_byte(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530195 result = WaitForXfer(i2c);
196 data[i++] = readl(&i2c->iicds);
197 }
198 if (result == I2C_NACK)
199 result = I2C_OK; /* Normal terminated read. */
wdenk49c3f672003-10-08 22:33:00 +0000200 break;
wdenk1fe2c702003-03-06 21:55:29 +0000201
202 default:
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000203 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000204 result = I2C_NOK;
205 break;
206 }
wdenk1fe2c702003-03-06 21:55:29 +0000207
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530208bailout:
209 /* Send STOP. */
210 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Simon Glass824802d2015-07-02 18:15:46 -0600211 read_write_byte(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530212
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000213 return result;
wdenk1fe2c702003-03-06 21:55:29 +0000214}
215
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100216static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
wdenk1fe2c702003-03-06 21:55:29 +0000217{
Simon Glass365c3da2016-11-23 06:34:42 -0700218 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
wdenk49c3f672003-10-08 22:33:00 +0000219 uchar buf[1];
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530220 int ret;
wdenk1fe2c702003-03-06 21:55:29 +0000221
wdenk49c3f672003-10-08 22:33:00 +0000222 buf[0] = 0;
wdenk1fe2c702003-03-06 21:55:29 +0000223
wdenk49c3f672003-10-08 22:33:00 +0000224 /*
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 Glassb9d7f992016-11-23 06:34:43 -0700229 ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1, 0, 0, buf, 1);
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530230
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530231 return ret != I2C_OK;
wdenk1fe2c702003-03-06 21:55:29 +0000232}
233
Simon Glasse3b8c862015-07-02 18:15:47 -0600234static int s3c24x0_do_msg(struct s3c24x0_i2c_bus *i2c_bus, struct i2c_msg *msg,
235 int seq)
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100236{
Simon Glasse3b8c862015-07-02 18:15:47 -0600237 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 Marczak2a4f8112015-01-27 13:36:36 +0100242
Simon Glasse3b8c862015-07-02 18:15:47 -0600243 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 Marczak2a4f8112015-01-27 13:36:36 +0100274 } else {
Simon Glasse3b8c862015-07-02 18:15:47 -0600275 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 Marczak2a4f8112015-01-27 13:36:36 +0100280 }
281
Simon Glasse3b8c862015-07-02 18:15:47 -0600282err:
283 return ret;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100284}
285
286static 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 Glasse3b8c862015-07-02 18:15:47 -0600290 struct s3c24x0_i2c *i2c = i2c_bus->regs;
291 ulong start_time;
292 int ret, i;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100293
Simon Glasse3b8c862015-07-02 18:15:47 -0600294 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 Marczak2a4f8112015-01-27 13:36:36 +0100299 }
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100300 }
301
Simon Glasse3b8c862015-07-02 18:15:47 -0600302 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 Marczak2a4f8112015-01-27 13:36:36 +0100310}
311
Simon Glassaad29ae2020-12-03 16:55:21 -0700312static int s3c_i2c_of_to_plat(struct udevice *dev)
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100313{
David Virage2ec1422024-08-02 21:19:16 +0200314#if IS_ENABLED(CONFIG_ARCH_EXYNOS4) || IS_ENABLED(CONFIG_ARCH_EXYNOS5)
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100315 const void *blob = gd->fdt_blob;
David Virage2ec1422024-08-02 21:19:16 +0200316#endif
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100317 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
Simon Glassb9d7f992016-11-23 06:34:43 -0700318 int node;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100319
Simon Glassdd79d6e2017-01-17 16:52:55 -0700320 node = dev_of_offset(dev);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100321
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900322 i2c_bus->regs = dev_read_addr_ptr(dev);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100323
David Virage2ec1422024-08-02 21:19:16 +0200324#if IS_ENABLED(CONFIG_ARCH_EXYNOS4) || IS_ENABLED(CONFIG_ARCH_EXYNOS5)
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100325 i2c_bus->id = pinmux_decode_periph_id(blob, node);
David Virage2ec1422024-08-02 21:19:16 +0200326#endif
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100327
Simon Glassf0c99c52020-01-23 11:48:22 -0700328 i2c_bus->clock_frequency =
329 dev_read_u32_default(dev, "clock-frequency",
330 I2C_SPEED_STANDARD_RATE);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100331 i2c_bus->node = node;
Simon Glass75e534b2020-12-16 21:20:07 -0700332 i2c_bus->bus_num = dev_seq(dev);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100333
David Virage2ec1422024-08-02 21:19:16 +0200334#if IS_ENABLED(CONFIG_ARCH_EXYNOS4) || IS_ENABLED(CONFIG_ARCH_EXYNOS5)
Simon Glassb9d7f992016-11-23 06:34:43 -0700335 exynos_pinmux_config(i2c_bus->id, 0);
David Virage2ec1422024-08-02 21:19:16 +0200336#endif
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100337
338 i2c_bus->active = true;
339
340 return 0;
341}
342
343static 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
349static const struct udevice_id s3c_i2c_ids[] = {
Simon Glassb9d7f992016-11-23 06:34:43 -0700350 { .compatible = "samsung,s3c2440-i2c" },
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100351 { }
352};
353
354U_BOOT_DRIVER(i2c_s3c) = {
355 .name = "i2c_s3c",
356 .id = UCLASS_I2C,
357 .of_match = s3c_i2c_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700358 .of_to_plat = s3c_i2c_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700359 .priv_auto = sizeof(struct s3c24x0_i2c_bus),
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100360 .ops = &s3c_i2c_ops,
361};