blob: 3c69dbf409b00fd8cff74cdd321fd6278a821e41 [file] [log] [blame]
wdenk1cb8e982003-03-06 21:55:29 +00001/*
2 * (C) Copyright 2002
3 * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk1cb8e982003-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 */
wdenk1cb8e982003-03-06 21:55:29 +000012#include <common.h>
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +010013#include <errno.h>
14#include <dm.h>
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +000015#include <fdtdec.h>
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +000016#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000017#include <asm/arch/clk.h>
18#include <asm/arch/cpu.h>
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +000019#include <asm/arch/pinmux.h>
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000020#else
kevin.morfitt@fearnside-systems.co.ukac678042009-11-17 18:30:34 +090021#include <asm/arch/s3c24x0_cpu.h>
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000022#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090023#include <asm/io.h>
wdenk1cb8e982003-03-06 21:55:29 +000024#include <i2c.h>
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000025#include "s3c24x0_i2c.h"
wdenk1cb8e982003-03-06 21:55:29 +000026
Jaehoon Chunga2987122017-01-09 14:47:51 +090027#ifndef CONFIG_SYS_I2C_S3C24X0_SLAVE
28#define SYS_I2C_S3C24X0_SLAVE_ADDR 0
29#else
30#define SYS_I2C_S3C24X0_SLAVE_ADDR CONFIG_SYS_I2C_S3C24X0_SLAVE
31#endif
32
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +010033DECLARE_GLOBAL_DATA_PTR;
34
Naveen Krishna Che4e24022013-10-15 16:01:43 +053035/*
36 * Wait til the byte transfer is completed.
37 *
38 * @param i2c- pointer to the appropriate i2c register bank.
39 * @return I2C_OK, if transmission was ACKED
40 * I2C_NACK, if transmission was NACKED
41 * I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS
42 */
43
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000044static int WaitForXfer(struct s3c24x0_i2c *i2c)
wdenk1cb8e982003-03-06 21:55:29 +000045{
Naveen Krishna Che4e24022013-10-15 16:01:43 +053046 ulong start_time = get_timer(0);
wdenk1cb8e982003-03-06 21:55:29 +000047
Naveen Krishna Che4e24022013-10-15 16:01:43 +053048 do {
49 if (readl(&i2c->iiccon) & I2CCON_IRPND)
50 return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
51 I2C_NACK : I2C_OK;
52 } while (get_timer(start_time) < I2C_TIMEOUT_MS);
wdenk1cb8e982003-03-06 21:55:29 +000053
Naveen Krishna Che4e24022013-10-15 16:01:43 +053054 return I2C_NOK_TOUT;
wdenk1cb8e982003-03-06 21:55:29 +000055}
56
Simon Glass26ea7682015-07-02 18:15:46 -060057static void read_write_byte(struct s3c24x0_i2c *i2c)
wdenk1cb8e982003-03-06 21:55:29 +000058{
Simon Glass26ea7682015-07-02 18:15:46 -060059 clrbits_le32(&i2c->iiccon, I2CCON_IRPND);
wdenk1cb8e982003-03-06 21:55:29 +000060}
61
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000062static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
63{
64 ulong freq, pres = 16, div;
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +000065#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000066 freq = get_i2c_clk();
67#else
68 freq = get_PCLK();
69#endif
70 /* calculate prescaler and divisor values */
71 if ((freq / pres / (16 + 1)) > speed)
72 /* set prescaler to 512 */
73 pres = 512;
74
75 div = 0;
76 while ((freq / pres / (div + 1)) > speed)
77 div++;
78
79 /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
80 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
81
82 /* init to SLAVE REVEIVE and set slaveaddr */
83 writel(0, &i2c->iicstat);
84 writel(slaveadd, &i2c->iicadd);
85 /* program Master Transmit (and implicit STOP) */
86 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
87}
88
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +010089static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
Piotr Wilczek2d8f1e22013-11-20 10:43:49 +010090{
Simon Glass9a1bff62016-11-23 06:34:42 -070091 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
Piotr Wilczek2d8f1e22013-11-20 10:43:49 +010092
Piotr Wilczek2d8f1e22013-11-20 10:43:49 +010093 i2c_bus->clock_frequency = speed;
94
Simon Glass37b8eb32016-11-23 06:34:43 -070095 i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
Jaehoon Chunga2987122017-01-09 14:47:51 +090096 SYS_I2C_S3C24X0_SLAVE_ADDR);
Piotr Wilczek2d8f1e22013-11-20 10:43:49 +010097
98 return 0;
99}
100
Naveen Krishna Ch296a4612013-10-15 16:02:44 +0530101/*
wdenkfc3e2162003-10-08 22:33:00 +0000102 * cmd_type is 0 for write, 1 for read.
103 *
104 * addr_len can take any value from 0-255, it is only limited
105 * by the char, we could make it larger if needed. If it is
106 * 0 we skip the address write cycle.
107 */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000108static int i2c_transfer(struct s3c24x0_i2c *i2c,
109 unsigned char cmd_type,
110 unsigned char chip,
111 unsigned char addr[],
112 unsigned char addr_len,
113 unsigned char data[],
114 unsigned short data_len)
wdenk1cb8e982003-03-06 21:55:29 +0000115{
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530116 int i = 0, result;
117 ulong start_time = get_timer(0);
wdenk1cb8e982003-03-06 21:55:29 +0000118
wdenkfc3e2162003-10-08 22:33:00 +0000119 if (data == 0 || data_len == 0) {
120 /*Don't support data transfer of no length or to address 0 */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000121 debug("i2c_transfer: bad call\n");
wdenkfc3e2162003-10-08 22:33:00 +0000122 return I2C_NOK;
123 }
wdenk1cb8e982003-03-06 21:55:29 +0000124
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530125 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
126 if (get_timer(start_time) > I2C_TIMEOUT_MS)
127 return I2C_NOK_TOUT;
wdenkfc3e2162003-10-08 22:33:00 +0000128 }
wdenk1cb8e982003-03-06 21:55:29 +0000129
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000130 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530131
132 /* Get the slave chip address going */
133 writel(chip, &i2c->iicds);
134 if ((cmd_type == I2C_WRITE) || (addr && addr_len))
135 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
136 &i2c->iicstat);
137 else
138 writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
139 &i2c->iicstat);
140
141 /* Wait for chip address to transmit. */
142 result = WaitForXfer(i2c);
143 if (result != I2C_OK)
144 goto bailout;
145
146 /* If register address needs to be transmitted - do it now. */
147 if (addr && addr_len) {
148 while ((i < addr_len) && (result == I2C_OK)) {
149 writel(addr[i++], &i2c->iicds);
Simon Glass26ea7682015-07-02 18:15:46 -0600150 read_write_byte(i2c);
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530151 result = WaitForXfer(i2c);
152 }
153 i = 0;
154 if (result != I2C_OK)
155 goto bailout;
156 }
wdenk1cb8e982003-03-06 21:55:29 +0000157
wdenkfc3e2162003-10-08 22:33:00 +0000158 switch (cmd_type) {
wdenk48b42612003-06-19 23:01:32 +0000159 case I2C_WRITE:
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530160 while ((i < data_len) && (result == I2C_OK)) {
161 writel(data[i++], &i2c->iicds);
Simon Glass26ea7682015-07-02 18:15:46 -0600162 read_write_byte(i2c);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000163 result = WaitForXfer(i2c);
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530164 }
wdenkfc3e2162003-10-08 22:33:00 +0000165 break;
wdenk1cb8e982003-03-06 21:55:29 +0000166
wdenk48b42612003-06-19 23:01:32 +0000167 case I2C_READ:
wdenkfc3e2162003-10-08 22:33:00 +0000168 if (addr && addr_len) {
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530169 /*
170 * Register address has been sent, now send slave chip
171 * address again to start the actual read transaction.
172 */
C Naumand9abba82010-10-26 23:04:31 +0900173 writel(chip, &i2c->iicds);
wdenk1cb8e982003-03-06 21:55:29 +0000174
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530175 /* Generate a re-START. */
Rajeshwari Shindecb466c02013-02-19 02:19:45 +0000176 writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
177 &i2c->iicstat);
Simon Glass26ea7682015-07-02 18:15:46 -0600178 read_write_byte(i2c);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000179 result = WaitForXfer(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000180
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530181 if (result != I2C_OK)
182 goto bailout;
wdenk1cb8e982003-03-06 21:55:29 +0000183 }
184
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530185 while ((i < data_len) && (result == I2C_OK)) {
186 /* disable ACK for final READ */
187 if (i == data_len - 1)
188 writel(readl(&i2c->iiccon)
189 & ~I2CCON_ACKGEN,
190 &i2c->iiccon);
Simon Glass26ea7682015-07-02 18:15:46 -0600191 read_write_byte(i2c);
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530192 result = WaitForXfer(i2c);
193 data[i++] = readl(&i2c->iicds);
194 }
195 if (result == I2C_NACK)
196 result = I2C_OK; /* Normal terminated read. */
wdenkfc3e2162003-10-08 22:33:00 +0000197 break;
wdenk1cb8e982003-03-06 21:55:29 +0000198
199 default:
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000200 debug("i2c_transfer: bad call\n");
wdenkfc3e2162003-10-08 22:33:00 +0000201 result = I2C_NOK;
202 break;
203 }
wdenk1cb8e982003-03-06 21:55:29 +0000204
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530205bailout:
206 /* Send STOP. */
207 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Simon Glass26ea7682015-07-02 18:15:46 -0600208 read_write_byte(i2c);
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530209
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000210 return result;
wdenk1cb8e982003-03-06 21:55:29 +0000211}
212
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100213static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
wdenk1cb8e982003-03-06 21:55:29 +0000214{
Simon Glass9a1bff62016-11-23 06:34:42 -0700215 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
wdenkfc3e2162003-10-08 22:33:00 +0000216 uchar buf[1];
Naveen Krishna Ch296a4612013-10-15 16:02:44 +0530217 int ret;
wdenk1cb8e982003-03-06 21:55:29 +0000218
wdenkfc3e2162003-10-08 22:33:00 +0000219 buf[0] = 0;
wdenk1cb8e982003-03-06 21:55:29 +0000220
wdenkfc3e2162003-10-08 22:33:00 +0000221 /*
222 * What is needed is to send the chip address and verify that the
223 * address was <ACK>ed (i.e. there was a chip at that address which
224 * drove the data line low).
225 */
Simon Glass37b8eb32016-11-23 06:34:43 -0700226 ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1, 0, 0, buf, 1);
Naveen Krishna Ch296a4612013-10-15 16:02:44 +0530227
Naveen Krishna Ch296a4612013-10-15 16:02:44 +0530228 return ret != I2C_OK;
wdenk1cb8e982003-03-06 21:55:29 +0000229}
230
Simon Glass45d9ae82015-07-02 18:15:47 -0600231static int s3c24x0_do_msg(struct s3c24x0_i2c_bus *i2c_bus, struct i2c_msg *msg,
232 int seq)
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100233{
Simon Glass45d9ae82015-07-02 18:15:47 -0600234 struct s3c24x0_i2c *i2c = i2c_bus->regs;
235 bool is_read = msg->flags & I2C_M_RD;
236 uint status;
237 uint addr;
238 int ret, i;
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100239
Simon Glass45d9ae82015-07-02 18:15:47 -0600240 if (!seq)
241 setbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
242
243 /* Get the slave chip address going */
244 addr = msg->addr << 1;
245 writel(addr, &i2c->iicds);
246 status = I2C_TXRX_ENA | I2C_START_STOP;
247 if (is_read)
248 status |= I2C_MODE_MR;
249 else
250 status |= I2C_MODE_MT;
251 writel(status, &i2c->iicstat);
252 if (seq)
253 read_write_byte(i2c);
254
255 /* Wait for chip address to transmit */
256 ret = WaitForXfer(i2c);
257 if (ret)
258 goto err;
259
260 if (is_read) {
261 for (i = 0; !ret && i < msg->len; i++) {
262 /* disable ACK for final READ */
263 if (i == msg->len - 1)
264 clrbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
265 read_write_byte(i2c);
266 ret = WaitForXfer(i2c);
267 msg->buf[i] = readl(&i2c->iicds);
268 }
269 if (ret == I2C_NACK)
270 ret = I2C_OK; /* Normal terminated read */
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100271 } else {
Simon Glass45d9ae82015-07-02 18:15:47 -0600272 for (i = 0; !ret && i < msg->len; i++) {
273 writel(msg->buf[i], &i2c->iicds);
274 read_write_byte(i2c);
275 ret = WaitForXfer(i2c);
276 }
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100277 }
278
Simon Glass45d9ae82015-07-02 18:15:47 -0600279err:
280 return ret;
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100281}
282
283static int s3c24x0_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
284 int nmsgs)
285{
286 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
Simon Glass45d9ae82015-07-02 18:15:47 -0600287 struct s3c24x0_i2c *i2c = i2c_bus->regs;
288 ulong start_time;
289 int ret, i;
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100290
Simon Glass45d9ae82015-07-02 18:15:47 -0600291 start_time = get_timer(0);
292 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
293 if (get_timer(start_time) > I2C_TIMEOUT_MS) {
294 debug("Timeout\n");
295 return -ETIMEDOUT;
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100296 }
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100297 }
298
Simon Glass45d9ae82015-07-02 18:15:47 -0600299 for (ret = 0, i = 0; !ret && i < nmsgs; i++)
300 ret = s3c24x0_do_msg(i2c_bus, &msg[i], i);
301
302 /* Send STOP */
303 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
304 read_write_byte(i2c);
305
306 return ret ? -EREMOTEIO : 0;
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100307}
308
309static int s3c_i2c_ofdata_to_platdata(struct udevice *dev)
310{
311 const void *blob = gd->fdt_blob;
312 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
Simon Glass37b8eb32016-11-23 06:34:43 -0700313 int node;
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100314
Simon Glasse160f7d2017-01-17 16:52:55 -0700315 node = dev_of_offset(dev);
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100316
Simon Glass37b8eb32016-11-23 06:34:43 -0700317 i2c_bus->regs = (struct s3c24x0_i2c *)dev_get_addr(dev);
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100318
319 i2c_bus->id = pinmux_decode_periph_id(blob, node);
320
321 i2c_bus->clock_frequency = fdtdec_get_int(blob, node,
Simon Glass45d9ae82015-07-02 18:15:47 -0600322 "clock-frequency", 100000);
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100323 i2c_bus->node = node;
324 i2c_bus->bus_num = dev->seq;
325
Simon Glass37b8eb32016-11-23 06:34:43 -0700326 exynos_pinmux_config(i2c_bus->id, 0);
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100327
328 i2c_bus->active = true;
329
330 return 0;
331}
332
333static const struct dm_i2c_ops s3c_i2c_ops = {
334 .xfer = s3c24x0_i2c_xfer,
335 .probe_chip = s3c24x0_i2c_probe,
336 .set_bus_speed = s3c24x0_i2c_set_bus_speed,
337};
338
339static const struct udevice_id s3c_i2c_ids[] = {
Simon Glass37b8eb32016-11-23 06:34:43 -0700340 { .compatible = "samsung,s3c2440-i2c" },
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100341 { }
342};
343
344U_BOOT_DRIVER(i2c_s3c) = {
345 .name = "i2c_s3c",
346 .id = UCLASS_I2C,
347 .of_match = s3c_i2c_ids,
348 .ofdata_to_platdata = s3c_i2c_ofdata_to_platdata,
Przemyslaw Marczak8dfcbaa2015-01-27 13:36:36 +0100349 .priv_auto_alloc_size = sizeof(struct s3c24x0_i2c_bus),
350 .ops = &s3c_i2c_ops,
351};