blob: 56f0f69885e4ac785d30f1e63ccfff776f4e3bb5 [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
wdenk1fe2c702003-03-06 21:55:29 +00007#include <common.h>
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +01008#include <errno.h>
9#include <dm.h>
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +000010#include <fdtdec.h>
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +000011#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
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>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000016#else
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090017#include <asm/arch/s3c24x0_cpu.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000018#endif
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090020#include <asm/io.h>
wdenk1fe2c702003-03-06 21:55:29 +000021#include <i2c.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000022#include "s3c24x0_i2c.h"
wdenk1fe2c702003-03-06 21:55:29 +000023
Jaehoon Chung9ac1fdf2017-01-09 14:47:51 +090024#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 Marczak2a4f8112015-01-27 13:36:36 +010030DECLARE_GLOBAL_DATA_PTR;
31
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +053032/*
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 Shinde4b4480a2012-07-23 21:23:53 +000041static int WaitForXfer(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +000042{
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +053043 ulong start_time = get_timer(0);
wdenk1fe2c702003-03-06 21:55:29 +000044
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +053045 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);
wdenk1fe2c702003-03-06 21:55:29 +000050
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +053051 return I2C_NOK_TOUT;
52}
53
Simon Glass824802d2015-07-02 18:15:46 -060054static void read_write_byte(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +000055{
Simon Glass824802d2015-07-02 18:15:46 -060056 clrbits_le32(&i2c->iiccon, I2CCON_IRPND);
wdenk1fe2c702003-03-06 21:55:29 +000057}
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000058
59static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
60{
61 ulong freq, pres = 16, div;
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +000062#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000063 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 Ch64a191f2013-10-15 16:02:44 +053085
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +010086static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +010087{
Simon Glass365c3da2016-11-23 06:34:42 -070088 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +010089
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +010090 i2c_bus->clock_frequency = speed;
91
Simon Glassb9d7f992016-11-23 06:34:43 -070092 i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
Jaehoon Chung9ac1fdf2017-01-09 14:47:51 +090093 SYS_I2C_S3C24X0_SLAVE_ADDR);
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +010094
95 return 0;
96}
97
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +053098/*
wdenk49c3f672003-10-08 22:33:00 +000099 * 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 Shinde4b4480a2012-07-23 21:23:53 +0000105static 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)
wdenk1fe2c702003-03-06 21:55:29 +0000112{
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530113 int i = 0, result;
114 ulong start_time = get_timer(0);
wdenk1fe2c702003-03-06 21:55:29 +0000115
wdenk49c3f672003-10-08 22:33:00 +0000116 if (data == 0 || data_len == 0) {
117 /*Don't support data transfer of no length or to address 0 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000118 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000119 return I2C_NOK;
120 }
wdenk1fe2c702003-03-06 21:55:29 +0000121
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530122 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
123 if (get_timer(start_time) > I2C_TIMEOUT_MS)
124 return I2C_NOK_TOUT;
wdenk49c3f672003-10-08 22:33:00 +0000125 }
wdenk1fe2c702003-03-06 21:55:29 +0000126
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000127 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
wdenk1fe2c702003-03-06 21:55:29 +0000128
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530129 /* 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;
wdenk1fe2c702003-03-06 21:55:29 +0000142
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530143 /* 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 Glass824802d2015-07-02 18:15:46 -0600147 read_write_byte(i2c);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000148 result = WaitForXfer(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530149 }
150 i = 0;
151 if (result != I2C_OK)
152 goto bailout;
153 }
wdenk1fe2c702003-03-06 21:55:29 +0000154
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530155 switch (cmd_type) {
156 case I2C_WRITE:
157 while ((i < data_len) && (result == I2C_OK)) {
158 writel(data[i++], &i2c->iicds);
Simon Glass824802d2015-07-02 18:15:46 -0600159 read_write_byte(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530160 result = WaitForXfer(i2c);
161 }
wdenk49c3f672003-10-08 22:33:00 +0000162 break;
wdenk1fe2c702003-03-06 21:55:29 +0000163
wdenk7539dea2003-06-19 23:01:32 +0000164 case I2C_READ:
wdenk49c3f672003-10-08 22:33:00 +0000165 if (addr && addr_len) {
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530166 /*
167 * Register address has been sent, now send slave chip
168 * address again to start the actual read transaction.
169 */
C Nauman383c43e2010-10-26 23:04:31 +0900170 writel(chip, &i2c->iicds);
wdenk1fe2c702003-03-06 21:55:29 +0000171
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530172 /* Generate a re-START. */
Rajeshwari Shindee076adf2013-02-19 02:19:45 +0000173 writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
174 &i2c->iicstat);
Simon Glass824802d2015-07-02 18:15:46 -0600175 read_write_byte(i2c);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000176 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000177
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530178 if (result != I2C_OK)
179 goto bailout;
wdenk1fe2c702003-03-06 21:55:29 +0000180 }
wdenk1fe2c702003-03-06 21:55:29 +0000181
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530182 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 Glass824802d2015-07-02 18:15:46 -0600188 read_write_byte(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530189 result = WaitForXfer(i2c);
190 data[i++] = readl(&i2c->iicds);
191 }
192 if (result == I2C_NACK)
193 result = I2C_OK; /* Normal terminated read. */
wdenk49c3f672003-10-08 22:33:00 +0000194 break;
wdenk1fe2c702003-03-06 21:55:29 +0000195
196 default:
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000197 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000198 result = I2C_NOK;
199 break;
200 }
wdenk1fe2c702003-03-06 21:55:29 +0000201
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530202bailout:
203 /* Send STOP. */
204 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Simon Glass824802d2015-07-02 18:15:46 -0600205 read_write_byte(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530206
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000207 return result;
wdenk1fe2c702003-03-06 21:55:29 +0000208}
209
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100210static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
wdenk1fe2c702003-03-06 21:55:29 +0000211{
Simon Glass365c3da2016-11-23 06:34:42 -0700212 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
wdenk49c3f672003-10-08 22:33:00 +0000213 uchar buf[1];
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530214 int ret;
wdenk1fe2c702003-03-06 21:55:29 +0000215
wdenk49c3f672003-10-08 22:33:00 +0000216 buf[0] = 0;
wdenk1fe2c702003-03-06 21:55:29 +0000217
wdenk49c3f672003-10-08 22:33:00 +0000218 /*
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 Glassb9d7f992016-11-23 06:34:43 -0700223 ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1, 0, 0, buf, 1);
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530224
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530225 return ret != I2C_OK;
wdenk1fe2c702003-03-06 21:55:29 +0000226}
227
Simon Glasse3b8c862015-07-02 18:15:47 -0600228static int s3c24x0_do_msg(struct s3c24x0_i2c_bus *i2c_bus, struct i2c_msg *msg,
229 int seq)
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100230{
Simon Glasse3b8c862015-07-02 18:15:47 -0600231 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 Marczak2a4f8112015-01-27 13:36:36 +0100236
Simon Glasse3b8c862015-07-02 18:15:47 -0600237 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 Marczak2a4f8112015-01-27 13:36:36 +0100268 } else {
Simon Glasse3b8c862015-07-02 18:15:47 -0600269 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 Marczak2a4f8112015-01-27 13:36:36 +0100274 }
275
Simon Glasse3b8c862015-07-02 18:15:47 -0600276err:
277 return ret;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100278}
279
280static 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 Glasse3b8c862015-07-02 18:15:47 -0600284 struct s3c24x0_i2c *i2c = i2c_bus->regs;
285 ulong start_time;
286 int ret, i;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100287
Simon Glasse3b8c862015-07-02 18:15:47 -0600288 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 Marczak2a4f8112015-01-27 13:36:36 +0100293 }
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100294 }
295
Simon Glasse3b8c862015-07-02 18:15:47 -0600296 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 Marczak2a4f8112015-01-27 13:36:36 +0100304}
305
Simon Glassaad29ae2020-12-03 16:55:21 -0700306static int s3c_i2c_of_to_plat(struct udevice *dev)
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100307{
308 const void *blob = gd->fdt_blob;
309 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
Simon Glassb9d7f992016-11-23 06:34:43 -0700310 int node;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100311
Simon Glassdd79d6e2017-01-17 16:52:55 -0700312 node = dev_of_offset(dev);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100313
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900314 i2c_bus->regs = dev_read_addr_ptr(dev);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100315
316 i2c_bus->id = pinmux_decode_periph_id(blob, node);
317
Simon Glassf0c99c52020-01-23 11:48:22 -0700318 i2c_bus->clock_frequency =
319 dev_read_u32_default(dev, "clock-frequency",
320 I2C_SPEED_STANDARD_RATE);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100321 i2c_bus->node = node;
Simon Glass75e534b2020-12-16 21:20:07 -0700322 i2c_bus->bus_num = dev_seq(dev);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100323
Simon Glassb9d7f992016-11-23 06:34:43 -0700324 exynos_pinmux_config(i2c_bus->id, 0);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100325
326 i2c_bus->active = true;
327
328 return 0;
329}
330
331static 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
337static const struct udevice_id s3c_i2c_ids[] = {
Simon Glassb9d7f992016-11-23 06:34:43 -0700338 { .compatible = "samsung,s3c2440-i2c" },
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100339 { }
340};
341
342U_BOOT_DRIVER(i2c_s3c) = {
343 .name = "i2c_s3c",
344 .id = UCLASS_I2C,
345 .of_match = s3c_i2c_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700346 .of_to_plat = s3c_i2c_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700347 .priv_auto = sizeof(struct s3c24x0_i2c_bus),
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100348 .ops = &s3c_i2c_ops,
349};