blob: 2ece9f419591be3459b392442e316479cbce1372 [file] [log] [blame]
wdenk1fe2c702003-03-06 21:55:29 +00001/*
2 * (C) Copyright 2002
3 * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk1fe2c702003-03-06 21:55:29 +00006 */
7
8/* This code should work for both the S3C2400 and the S3C2410
9 * as they seem to have the same I2C controller inside.
10 * The different address mapping is handled by the s3c24xx.h files below.
11 */
wdenk1fe2c702003-03-06 21:55:29 +000012#include <common.h>
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +010013#include <errno.h>
14#include <dm.h>
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +000015#include <fdtdec.h>
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +000016#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000017#include <asm/arch/clk.h>
18#include <asm/arch/cpu.h>
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +000019#include <asm/arch/pinmux.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000020#else
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090021#include <asm/arch/s3c24x0_cpu.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000022#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090023#include <asm/io.h>
wdenk1fe2c702003-03-06 21:55:29 +000024#include <i2c.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000025#include "s3c24x0_i2c.h"
wdenk1fe2c702003-03-06 21:55:29 +000026
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +010027DECLARE_GLOBAL_DATA_PTR;
28
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +053029/*
30 * Wait til the byte transfer is completed.
31 *
32 * @param i2c- pointer to the appropriate i2c register bank.
33 * @return I2C_OK, if transmission was ACKED
34 * I2C_NACK, if transmission was NACKED
35 * I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS
36 */
37
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000038static int WaitForXfer(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +000039{
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +053040 ulong start_time = get_timer(0);
wdenk1fe2c702003-03-06 21:55:29 +000041
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +053042 do {
43 if (readl(&i2c->iiccon) & I2CCON_IRPND)
44 return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
45 I2C_NACK : I2C_OK;
46 } while (get_timer(start_time) < I2C_TIMEOUT_MS);
wdenk1fe2c702003-03-06 21:55:29 +000047
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +053048 return I2C_NOK_TOUT;
49}
50
Simon Glass824802d2015-07-02 18:15:46 -060051static void read_write_byte(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +000052{
Simon Glass824802d2015-07-02 18:15:46 -060053 clrbits_le32(&i2c->iiccon, I2CCON_IRPND);
wdenk1fe2c702003-03-06 21:55:29 +000054}
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000055
56static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
57{
58 ulong freq, pres = 16, div;
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +000059#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000060 freq = get_i2c_clk();
61#else
62 freq = get_PCLK();
63#endif
64 /* calculate prescaler and divisor values */
65 if ((freq / pres / (16 + 1)) > speed)
66 /* set prescaler to 512 */
67 pres = 512;
68
69 div = 0;
70 while ((freq / pres / (div + 1)) > speed)
71 div++;
72
73 /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
74 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
75
76 /* init to SLAVE REVEIVE and set slaveaddr */
77 writel(0, &i2c->iicstat);
78 writel(slaveadd, &i2c->iicadd);
79 /* program Master Transmit (and implicit STOP) */
80 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
81}
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +053082
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +010083static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +010084{
Simon Glass365c3da2016-11-23 06:34:42 -070085 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +010086
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +010087 i2c_bus->clock_frequency = speed;
88
Simon Glassb9d7f992016-11-23 06:34:43 -070089 i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
90 CONFIG_SYS_I2C_S3C24X0_SLAVE);
Piotr Wilczek58bbd2b2013-11-20 10:43:49 +010091
92 return 0;
93}
94
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +053095/*
wdenk49c3f672003-10-08 22:33:00 +000096 * cmd_type is 0 for write, 1 for read.
97 *
98 * addr_len can take any value from 0-255, it is only limited
99 * by the char, we could make it larger if needed. If it is
100 * 0 we skip the address write cycle.
101 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000102static int i2c_transfer(struct s3c24x0_i2c *i2c,
103 unsigned char cmd_type,
104 unsigned char chip,
105 unsigned char addr[],
106 unsigned char addr_len,
107 unsigned char data[],
108 unsigned short data_len)
wdenk1fe2c702003-03-06 21:55:29 +0000109{
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530110 int i = 0, result;
111 ulong start_time = get_timer(0);
wdenk1fe2c702003-03-06 21:55:29 +0000112
wdenk49c3f672003-10-08 22:33:00 +0000113 if (data == 0 || data_len == 0) {
114 /*Don't support data transfer of no length or to address 0 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000115 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000116 return I2C_NOK;
117 }
wdenk1fe2c702003-03-06 21:55:29 +0000118
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530119 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
120 if (get_timer(start_time) > I2C_TIMEOUT_MS)
121 return I2C_NOK_TOUT;
wdenk49c3f672003-10-08 22:33:00 +0000122 }
wdenk1fe2c702003-03-06 21:55:29 +0000123
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000124 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
wdenk1fe2c702003-03-06 21:55:29 +0000125
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530126 /* Get the slave chip address going */
127 writel(chip, &i2c->iicds);
128 if ((cmd_type == I2C_WRITE) || (addr && addr_len))
129 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
130 &i2c->iicstat);
131 else
132 writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
133 &i2c->iicstat);
134
135 /* Wait for chip address to transmit. */
136 result = WaitForXfer(i2c);
137 if (result != I2C_OK)
138 goto bailout;
wdenk1fe2c702003-03-06 21:55:29 +0000139
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530140 /* If register address needs to be transmitted - do it now. */
141 if (addr && addr_len) {
142 while ((i < addr_len) && (result == I2C_OK)) {
143 writel(addr[i++], &i2c->iicds);
Simon Glass824802d2015-07-02 18:15:46 -0600144 read_write_byte(i2c);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000145 result = WaitForXfer(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530146 }
147 i = 0;
148 if (result != I2C_OK)
149 goto bailout;
150 }
wdenk1fe2c702003-03-06 21:55:29 +0000151
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530152 switch (cmd_type) {
153 case I2C_WRITE:
154 while ((i < data_len) && (result == I2C_OK)) {
155 writel(data[i++], &i2c->iicds);
Simon Glass824802d2015-07-02 18:15:46 -0600156 read_write_byte(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530157 result = WaitForXfer(i2c);
158 }
wdenk49c3f672003-10-08 22:33:00 +0000159 break;
wdenk1fe2c702003-03-06 21:55:29 +0000160
wdenk7539dea2003-06-19 23:01:32 +0000161 case I2C_READ:
wdenk49c3f672003-10-08 22:33:00 +0000162 if (addr && addr_len) {
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530163 /*
164 * Register address has been sent, now send slave chip
165 * address again to start the actual read transaction.
166 */
C Nauman383c43e2010-10-26 23:04:31 +0900167 writel(chip, &i2c->iicds);
wdenk1fe2c702003-03-06 21:55:29 +0000168
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530169 /* Generate a re-START. */
Rajeshwari Shindee076adf2013-02-19 02:19:45 +0000170 writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
171 &i2c->iicstat);
Simon Glass824802d2015-07-02 18:15:46 -0600172 read_write_byte(i2c);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000173 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000174
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530175 if (result != I2C_OK)
176 goto bailout;
wdenk1fe2c702003-03-06 21:55:29 +0000177 }
wdenk1fe2c702003-03-06 21:55:29 +0000178
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530179 while ((i < data_len) && (result == I2C_OK)) {
180 /* disable ACK for final READ */
181 if (i == data_len - 1)
182 writel(readl(&i2c->iiccon)
183 & ~I2CCON_ACKGEN,
184 &i2c->iiccon);
Simon Glass824802d2015-07-02 18:15:46 -0600185 read_write_byte(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530186 result = WaitForXfer(i2c);
187 data[i++] = readl(&i2c->iicds);
188 }
189 if (result == I2C_NACK)
190 result = I2C_OK; /* Normal terminated read. */
wdenk49c3f672003-10-08 22:33:00 +0000191 break;
wdenk1fe2c702003-03-06 21:55:29 +0000192
193 default:
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000194 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000195 result = I2C_NOK;
196 break;
197 }
wdenk1fe2c702003-03-06 21:55:29 +0000198
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530199bailout:
200 /* Send STOP. */
201 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Simon Glass824802d2015-07-02 18:15:46 -0600202 read_write_byte(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530203
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000204 return result;
wdenk1fe2c702003-03-06 21:55:29 +0000205}
206
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100207static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
wdenk1fe2c702003-03-06 21:55:29 +0000208{
Simon Glass365c3da2016-11-23 06:34:42 -0700209 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
wdenk49c3f672003-10-08 22:33:00 +0000210 uchar buf[1];
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530211 int ret;
wdenk1fe2c702003-03-06 21:55:29 +0000212
wdenk49c3f672003-10-08 22:33:00 +0000213 buf[0] = 0;
wdenk1fe2c702003-03-06 21:55:29 +0000214
wdenk49c3f672003-10-08 22:33:00 +0000215 /*
216 * What is needed is to send the chip address and verify that the
217 * address was <ACK>ed (i.e. there was a chip at that address which
218 * drove the data line low).
219 */
Simon Glassb9d7f992016-11-23 06:34:43 -0700220 ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1, 0, 0, buf, 1);
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530221
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530222 return ret != I2C_OK;
wdenk1fe2c702003-03-06 21:55:29 +0000223}
224
Simon Glasse3b8c862015-07-02 18:15:47 -0600225static int s3c24x0_do_msg(struct s3c24x0_i2c_bus *i2c_bus, struct i2c_msg *msg,
226 int seq)
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100227{
Simon Glasse3b8c862015-07-02 18:15:47 -0600228 struct s3c24x0_i2c *i2c = i2c_bus->regs;
229 bool is_read = msg->flags & I2C_M_RD;
230 uint status;
231 uint addr;
232 int ret, i;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100233
Simon Glasse3b8c862015-07-02 18:15:47 -0600234 if (!seq)
235 setbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
236
237 /* Get the slave chip address going */
238 addr = msg->addr << 1;
239 writel(addr, &i2c->iicds);
240 status = I2C_TXRX_ENA | I2C_START_STOP;
241 if (is_read)
242 status |= I2C_MODE_MR;
243 else
244 status |= I2C_MODE_MT;
245 writel(status, &i2c->iicstat);
246 if (seq)
247 read_write_byte(i2c);
248
249 /* Wait for chip address to transmit */
250 ret = WaitForXfer(i2c);
251 if (ret)
252 goto err;
253
254 if (is_read) {
255 for (i = 0; !ret && i < msg->len; i++) {
256 /* disable ACK for final READ */
257 if (i == msg->len - 1)
258 clrbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
259 read_write_byte(i2c);
260 ret = WaitForXfer(i2c);
261 msg->buf[i] = readl(&i2c->iicds);
262 }
263 if (ret == I2C_NACK)
264 ret = I2C_OK; /* Normal terminated read */
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100265 } else {
Simon Glasse3b8c862015-07-02 18:15:47 -0600266 for (i = 0; !ret && i < msg->len; i++) {
267 writel(msg->buf[i], &i2c->iicds);
268 read_write_byte(i2c);
269 ret = WaitForXfer(i2c);
270 }
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100271 }
272
Simon Glasse3b8c862015-07-02 18:15:47 -0600273err:
274 return ret;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100275}
276
277static int s3c24x0_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
278 int nmsgs)
279{
280 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
Simon Glasse3b8c862015-07-02 18:15:47 -0600281 struct s3c24x0_i2c *i2c = i2c_bus->regs;
282 ulong start_time;
283 int ret, i;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100284
Simon Glasse3b8c862015-07-02 18:15:47 -0600285 start_time = get_timer(0);
286 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
287 if (get_timer(start_time) > I2C_TIMEOUT_MS) {
288 debug("Timeout\n");
289 return -ETIMEDOUT;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100290 }
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100291 }
292
Simon Glasse3b8c862015-07-02 18:15:47 -0600293 for (ret = 0, i = 0; !ret && i < nmsgs; i++)
294 ret = s3c24x0_do_msg(i2c_bus, &msg[i], i);
295
296 /* Send STOP */
297 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
298 read_write_byte(i2c);
299
300 return ret ? -EREMOTEIO : 0;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100301}
302
303static int s3c_i2c_ofdata_to_platdata(struct udevice *dev)
304{
305 const void *blob = gd->fdt_blob;
306 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
Simon Glassb9d7f992016-11-23 06:34:43 -0700307 int node;
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100308
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100309 node = dev->of_offset;
310
Simon Glassb9d7f992016-11-23 06:34:43 -0700311 i2c_bus->regs = (struct s3c24x0_i2c *)dev_get_addr(dev);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100312
313 i2c_bus->id = pinmux_decode_periph_id(blob, node);
314
315 i2c_bus->clock_frequency = fdtdec_get_int(blob, node,
Simon Glasse3b8c862015-07-02 18:15:47 -0600316 "clock-frequency", 100000);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100317 i2c_bus->node = node;
318 i2c_bus->bus_num = dev->seq;
319
Simon Glassb9d7f992016-11-23 06:34:43 -0700320 exynos_pinmux_config(i2c_bus->id, 0);
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100321
322 i2c_bus->active = true;
323
324 return 0;
325}
326
327static const struct dm_i2c_ops s3c_i2c_ops = {
328 .xfer = s3c24x0_i2c_xfer,
329 .probe_chip = s3c24x0_i2c_probe,
330 .set_bus_speed = s3c24x0_i2c_set_bus_speed,
331};
332
333static const struct udevice_id s3c_i2c_ids[] = {
Simon Glassb9d7f992016-11-23 06:34:43 -0700334 { .compatible = "samsung,s3c2440-i2c" },
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100335 { }
336};
337
338U_BOOT_DRIVER(i2c_s3c) = {
339 .name = "i2c_s3c",
340 .id = UCLASS_I2C,
341 .of_match = s3c_i2c_ids,
342 .ofdata_to_platdata = s3c_i2c_ofdata_to_platdata,
Przemyslaw Marczak2a4f8112015-01-27 13:36:36 +0100343 .priv_auto_alloc_size = sizeof(struct s3c24x0_i2c_bus),
344 .ops = &s3c_i2c_ops,
345};