blob: 382e4c2ecf5853de2d1e6d96b3ea7bd1a3a8f0b3 [file] [log] [blame]
wdenk1cb8e982003-03-06 21:55:29 +00001/*
2 * (C) Copyright 2002
3 * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/* This code should work for both the S3C2400 and the S3C2410
25 * as they seem to have the same I2C controller inside.
26 * The different address mapping is handled by the s3c24xx.h files below.
27 */
28
29#include <common.h>
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +000030#include <fdtdec.h>
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +000031#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000032#include <asm/arch/clk.h>
33#include <asm/arch/cpu.h>
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +000034#include <asm/arch/pinmux.h>
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000035#else
kevin.morfitt@fearnside-systems.co.ukac678042009-11-17 18:30:34 +090036#include <asm/arch/s3c24x0_cpu.h>
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000037#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090038#include <asm/io.h>
wdenk1cb8e982003-03-06 21:55:29 +000039#include <i2c.h>
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000040#include "s3c24x0_i2c.h"
wdenk1cb8e982003-03-06 21:55:29 +000041
42#ifdef CONFIG_HARD_I2C
43
wdenk48b42612003-06-19 23:01:32 +000044#define I2C_WRITE 0
45#define I2C_READ 1
wdenk1cb8e982003-03-06 21:55:29 +000046
wdenk48b42612003-06-19 23:01:32 +000047#define I2C_OK 0
48#define I2C_NOK 1
49#define I2C_NACK 2
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090050#define I2C_NOK_LA 3 /* Lost arbitration */
51#define I2C_NOK_TOUT 4 /* time out */
wdenk1cb8e982003-03-06 21:55:29 +000052
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090053#define I2CSTAT_BSY 0x20 /* Busy bit */
54#define I2CSTAT_NACK 0x01 /* Nack bit */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000055#define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090056#define I2CCON_IRPND 0x10 /* Interrupt pending bit */
57#define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
58#define I2C_MODE_MR 0x80 /* Master Receive Mode */
59#define I2C_START_STOP 0x20 /* START / STOP */
60#define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
wdenk1cb8e982003-03-06 21:55:29 +000061
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090062#define I2C_TIMEOUT 1 /* 1 second */
wdenk1cb8e982003-03-06 21:55:29 +000063
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000064
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +000065/*
66 * For SPL boot some boards need i2c before SDRAM is initialised so force
67 * variables to live in SRAM
68 */
69static unsigned int g_current_bus __attribute__((section(".data")));
Rajeshwari Shinded04df3c2013-01-13 19:49:36 +000070#ifdef CONFIG_OF_CONTROL
71static int i2c_busses __attribute__((section(".data")));
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +000072static struct s3c24x0_i2c_bus i2c_bus[CONFIG_MAX_I2C_NUM]
73 __attribute__((section(".data")));
Rajeshwari Shinded04df3c2013-01-13 19:49:36 +000074#endif
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000075
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +000076#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
wdenk48b42612003-06-19 23:01:32 +000077static int GetI2CSDA(void)
wdenk1cb8e982003-03-06 21:55:29 +000078{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090079 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk48b42612003-06-19 23:01:32 +000080
wdenk6dff5522003-07-15 07:45:49 +000081#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +090082 return (readl(&gpio->gpedat) & 0x8000) >> 15;
wdenk6dff5522003-07-15 07:45:49 +000083#endif
84#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +090085 return (readl(&gpio->pgdat) & 0x0020) >> 5;
wdenk6dff5522003-07-15 07:45:49 +000086#endif
wdenk1cb8e982003-03-06 21:55:29 +000087}
88
wdenk48b42612003-06-19 23:01:32 +000089static void SetI2CSCL(int x)
wdenk1cb8e982003-03-06 21:55:29 +000090{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090091 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk48b42612003-06-19 23:01:32 +000092
wdenk6dff5522003-07-15 07:45:49 +000093#ifdef CONFIG_S3C2410
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000094 writel((readl(&gpio->gpedat) & ~0x4000) |
95 (x & 1) << 14, &gpio->gpedat);
wdenk6dff5522003-07-15 07:45:49 +000096#endif
97#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +090098 writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
wdenk6dff5522003-07-15 07:45:49 +000099#endif
wdenk1cb8e982003-03-06 21:55:29 +0000100}
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000101#endif
wdenk1cb8e982003-03-06 21:55:29 +0000102
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000103static int WaitForXfer(struct s3c24x0_i2c *i2c)
wdenk1cb8e982003-03-06 21:55:29 +0000104{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900105 int i;
wdenk1cb8e982003-03-06 21:55:29 +0000106
wdenkfc3e2162003-10-08 22:33:00 +0000107 i = I2C_TIMEOUT * 10000;
C Naumand9abba82010-10-26 23:04:31 +0900108 while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900109 udelay(100);
wdenkfc3e2162003-10-08 22:33:00 +0000110 i--;
111 }
wdenk1cb8e982003-03-06 21:55:29 +0000112
C Naumand9abba82010-10-26 23:04:31 +0900113 return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
wdenk1cb8e982003-03-06 21:55:29 +0000114}
115
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000116static int IsACK(struct s3c24x0_i2c *i2c)
wdenk1cb8e982003-03-06 21:55:29 +0000117{
C Naumand9abba82010-10-26 23:04:31 +0900118 return !(readl(&i2c->iicstat) & I2CSTAT_NACK);
wdenk1cb8e982003-03-06 21:55:29 +0000119}
120
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000121static void ReadWriteByte(struct s3c24x0_i2c *i2c)
wdenk1cb8e982003-03-06 21:55:29 +0000122{
C Naumand9abba82010-10-26 23:04:31 +0900123 writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
wdenk1cb8e982003-03-06 21:55:29 +0000124}
125
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000126static struct s3c24x0_i2c *get_base_i2c(void)
127{
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +0000128#ifdef CONFIG_EXYNOS4
129 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
130 + (EXYNOS4_I2C_SPACING
131 * g_current_bus));
132 return i2c;
133#elif defined CONFIG_EXYNOS5
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000134 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
135 + (EXYNOS5_I2C_SPACING
136 * g_current_bus));
137 return i2c;
138#else
139 return s3c24x0_get_base_i2c();
140#endif
141}
142
143static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
144{
145 ulong freq, pres = 16, div;
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +0000146#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000147 freq = get_i2c_clk();
148#else
149 freq = get_PCLK();
150#endif
151 /* calculate prescaler and divisor values */
152 if ((freq / pres / (16 + 1)) > speed)
153 /* set prescaler to 512 */
154 pres = 512;
155
156 div = 0;
157 while ((freq / pres / (div + 1)) > speed)
158 div++;
159
160 /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
161 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
162
163 /* init to SLAVE REVEIVE and set slaveaddr */
164 writel(0, &i2c->iicstat);
165 writel(slaveadd, &i2c->iicadd);
166 /* program Master Transmit (and implicit STOP) */
167 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
168}
169
Rajeshwari Shinde178239d2012-07-23 21:23:54 +0000170/*
171 * MULTI BUS I2C support
172 */
173
174#ifdef CONFIG_I2C_MULTI_BUS
175int i2c_set_bus_num(unsigned int bus)
176{
177 struct s3c24x0_i2c *i2c;
178
179 if ((bus < 0) || (bus >= CONFIG_MAX_I2C_NUM)) {
180 debug("Bad bus: %d\n", bus);
181 return -1;
182 }
183
184 g_current_bus = bus;
185 i2c = get_base_i2c();
186 i2c_ch_init(i2c, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
187
188 return 0;
189}
190
191unsigned int i2c_get_bus_num(void)
192{
193 return g_current_bus;
194}
195#endif
196
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900197void i2c_init(int speed, int slaveadd)
wdenk1cb8e982003-03-06 21:55:29 +0000198{
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000199 struct s3c24x0_i2c *i2c;
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +0000200#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900201 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000202#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900203 int i;
wdenk1cb8e982003-03-06 21:55:29 +0000204
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000205 /* By default i2c channel 0 is the current bus */
206 g_current_bus = 0;
207 i2c = get_base_i2c();
wdenk1cb8e982003-03-06 21:55:29 +0000208
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000209 /* wait for some time to give previous transfer a chance to finish */
wdenkfc3e2162003-10-08 22:33:00 +0000210 i = I2C_TIMEOUT * 1000;
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000211 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900212 udelay(1000);
wdenk1cb8e982003-03-06 21:55:29 +0000213 i--;
214 }
wdenk1cb8e982003-03-06 21:55:29 +0000215
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +0000216#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
C Naumand9abba82010-10-26 23:04:31 +0900217 if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
wdenk6dff5522003-07-15 07:45:49 +0000218#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +0900219 ulong old_gpecon = readl(&gpio->gpecon);
wdenk6dff5522003-07-15 07:45:49 +0000220#endif
221#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +0900222 ulong old_gpecon = readl(&gpio->pgcon);
wdenk6dff5522003-07-15 07:45:49 +0000223#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900224 /* bus still busy probably by (most) previously interrupted
225 transfer */
wdenk1cb8e982003-03-06 21:55:29 +0000226
wdenkfc3e2162003-10-08 22:33:00 +0000227#ifdef CONFIG_S3C2410
228 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
C Naumand9abba82010-10-26 23:04:31 +0900229 writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
230 &gpio->gpecon);
wdenkfc3e2162003-10-08 22:33:00 +0000231#endif
232#ifdef CONFIG_S3C2400
233 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
C Naumand9abba82010-10-26 23:04:31 +0900234 writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
235 &gpio->pgcon);
wdenkfc3e2162003-10-08 22:33:00 +0000236#endif
wdenk1cb8e982003-03-06 21:55:29 +0000237
wdenkfc3e2162003-10-08 22:33:00 +0000238 /* toggle I2CSCL until bus idle */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900239 SetI2CSCL(0);
240 udelay(1000);
wdenkfc3e2162003-10-08 22:33:00 +0000241 i = 10;
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900242 while ((i > 0) && (GetI2CSDA() != 1)) {
243 SetI2CSCL(1);
244 udelay(1000);
245 SetI2CSCL(0);
246 udelay(1000);
wdenkfc3e2162003-10-08 22:33:00 +0000247 i--;
248 }
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900249 SetI2CSCL(1);
250 udelay(1000);
wdenk1cb8e982003-03-06 21:55:29 +0000251
wdenkfc3e2162003-10-08 22:33:00 +0000252 /* restore pin functions */
253#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +0900254 writel(old_gpecon, &gpio->gpecon);
wdenkfc3e2162003-10-08 22:33:00 +0000255#endif
256#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +0900257 writel(old_gpecon, &gpio->pgcon);
wdenkfc3e2162003-10-08 22:33:00 +0000258#endif
259 }
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +0000260#endif /* #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000261 i2c_ch_init(i2c, speed, slaveadd);
wdenk1cb8e982003-03-06 21:55:29 +0000262}
263
264/*
wdenkfc3e2162003-10-08 22:33:00 +0000265 * cmd_type is 0 for write, 1 for read.
266 *
267 * addr_len can take any value from 0-255, it is only limited
268 * by the char, we could make it larger if needed. If it is
269 * 0 we skip the address write cycle.
270 */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000271static int i2c_transfer(struct s3c24x0_i2c *i2c,
272 unsigned char cmd_type,
273 unsigned char chip,
274 unsigned char addr[],
275 unsigned char addr_len,
276 unsigned char data[],
277 unsigned short data_len)
wdenk1cb8e982003-03-06 21:55:29 +0000278{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900279 int i, result;
wdenk1cb8e982003-03-06 21:55:29 +0000280
wdenkfc3e2162003-10-08 22:33:00 +0000281 if (data == 0 || data_len == 0) {
282 /*Don't support data transfer of no length or to address 0 */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000283 debug("i2c_transfer: bad call\n");
wdenkfc3e2162003-10-08 22:33:00 +0000284 return I2C_NOK;
285 }
wdenk1cb8e982003-03-06 21:55:29 +0000286
wdenkfc3e2162003-10-08 22:33:00 +0000287 /* Check I2C bus idle */
288 i = I2C_TIMEOUT * 1000;
C Naumand9abba82010-10-26 23:04:31 +0900289 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900290 udelay(1000);
wdenkfc3e2162003-10-08 22:33:00 +0000291 i--;
292 }
wdenk1cb8e982003-03-06 21:55:29 +0000293
C Naumand9abba82010-10-26 23:04:31 +0900294 if (readl(&i2c->iicstat) & I2CSTAT_BSY)
wdenkfc3e2162003-10-08 22:33:00 +0000295 return I2C_NOK_TOUT;
wdenk1cb8e982003-03-06 21:55:29 +0000296
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000297 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
wdenkfc3e2162003-10-08 22:33:00 +0000298 result = I2C_OK;
wdenk1cb8e982003-03-06 21:55:29 +0000299
wdenkfc3e2162003-10-08 22:33:00 +0000300 switch (cmd_type) {
wdenk48b42612003-06-19 23:01:32 +0000301 case I2C_WRITE:
wdenkfc3e2162003-10-08 22:33:00 +0000302 if (addr && addr_len) {
C Naumand9abba82010-10-26 23:04:31 +0900303 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000304 /* send START */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900305 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Naumand9abba82010-10-26 23:04:31 +0900306 &i2c->iicstat);
wdenkfc3e2162003-10-08 22:33:00 +0000307 i = 0;
308 while ((i < addr_len) && (result == I2C_OK)) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000309 result = WaitForXfer(i2c);
C Naumand9abba82010-10-26 23:04:31 +0900310 writel(addr[i], &i2c->iicds);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000311 ReadWriteByte(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000312 i++;
313 }
314 i = 0;
315 while ((i < data_len) && (result == I2C_OK)) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000316 result = WaitForXfer(i2c);
C Naumand9abba82010-10-26 23:04:31 +0900317 writel(data[i], &i2c->iicds);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000318 ReadWriteByte(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000319 i++;
320 }
321 } else {
C Naumand9abba82010-10-26 23:04:31 +0900322 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000323 /* send START */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900324 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Naumand9abba82010-10-26 23:04:31 +0900325 &i2c->iicstat);
wdenkfc3e2162003-10-08 22:33:00 +0000326 i = 0;
Rajeshwari Shindecb466c02013-02-19 02:19:45 +0000327 while ((i < data_len) && (result == I2C_OK)) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000328 result = WaitForXfer(i2c);
C Naumand9abba82010-10-26 23:04:31 +0900329 writel(data[i], &i2c->iicds);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000330 ReadWriteByte(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000331 i++;
332 }
wdenk1cb8e982003-03-06 21:55:29 +0000333 }
wdenk1cb8e982003-03-06 21:55:29 +0000334
wdenkfc3e2162003-10-08 22:33:00 +0000335 if (result == I2C_OK)
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000336 result = WaitForXfer(i2c);
wdenk1cb8e982003-03-06 21:55:29 +0000337
wdenkfc3e2162003-10-08 22:33:00 +0000338 /* send STOP */
Rajeshwari Shindecb466c02013-02-19 02:19:45 +0000339 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000340 ReadWriteByte(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000341 break;
wdenk1cb8e982003-03-06 21:55:29 +0000342
wdenk48b42612003-06-19 23:01:32 +0000343 case I2C_READ:
wdenkfc3e2162003-10-08 22:33:00 +0000344 if (addr && addr_len) {
C Naumand9abba82010-10-26 23:04:31 +0900345 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000346 /* send START */
Rajeshwari Shindecb466c02013-02-19 02:19:45 +0000347 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
348 &i2c->iicstat);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000349 result = WaitForXfer(i2c);
350 if (IsACK(i2c)) {
wdenkfc3e2162003-10-08 22:33:00 +0000351 i = 0;
352 while ((i < addr_len) && (result == I2C_OK)) {
C Naumand9abba82010-10-26 23:04:31 +0900353 writel(addr[i], &i2c->iicds);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000354 ReadWriteByte(i2c);
355 result = WaitForXfer(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000356 i++;
357 }
wdenk1cb8e982003-03-06 21:55:29 +0000358
C Naumand9abba82010-10-26 23:04:31 +0900359 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000360 /* resend START */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900361 writel(I2C_MODE_MR | I2C_TXRX_ENA |
C Naumand9abba82010-10-26 23:04:31 +0900362 I2C_START_STOP, &i2c->iicstat);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000363 ReadWriteByte(i2c);
364 result = WaitForXfer(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000365 i = 0;
366 while ((i < data_len) && (result == I2C_OK)) {
367 /* disable ACK for final READ */
368 if (i == data_len - 1)
C Naumand9abba82010-10-26 23:04:31 +0900369 writel(readl(&i2c->iiccon)
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000370 & ~I2CCON_ACKGEN,
371 &i2c->iiccon);
372 ReadWriteByte(i2c);
373 result = WaitForXfer(i2c);
C Naumand9abba82010-10-26 23:04:31 +0900374 data[i] = readl(&i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000375 i++;
376 }
377 } else {
378 result = I2C_NACK;
379 }
380
wdenk1cb8e982003-03-06 21:55:29 +0000381 } else {
C Naumand9abba82010-10-26 23:04:31 +0900382 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000383 /* send START */
Rajeshwari Shindecb466c02013-02-19 02:19:45 +0000384 writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
385 &i2c->iicstat);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000386 result = WaitForXfer(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000387
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000388 if (IsACK(i2c)) {
wdenkfc3e2162003-10-08 22:33:00 +0000389 i = 0;
390 while ((i < data_len) && (result == I2C_OK)) {
391 /* disable ACK for final READ */
392 if (i == data_len - 1)
C Naumand9abba82010-10-26 23:04:31 +0900393 writel(readl(&i2c->iiccon) &
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000394 ~I2CCON_ACKGEN,
395 &i2c->iiccon);
396 ReadWriteByte(i2c);
397 result = WaitForXfer(i2c);
C Naumand9abba82010-10-26 23:04:31 +0900398 data[i] = readl(&i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000399 i++;
400 }
401 } else {
402 result = I2C_NACK;
403 }
wdenk1cb8e982003-03-06 21:55:29 +0000404 }
405
wdenkfc3e2162003-10-08 22:33:00 +0000406 /* send STOP */
C Naumand9abba82010-10-26 23:04:31 +0900407 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000408 ReadWriteByte(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000409 break;
wdenk1cb8e982003-03-06 21:55:29 +0000410
411 default:
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000412 debug("i2c_transfer: bad call\n");
wdenkfc3e2162003-10-08 22:33:00 +0000413 result = I2C_NOK;
414 break;
415 }
wdenk1cb8e982003-03-06 21:55:29 +0000416
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000417 return result;
wdenk1cb8e982003-03-06 21:55:29 +0000418}
419
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900420int i2c_probe(uchar chip)
wdenk1cb8e982003-03-06 21:55:29 +0000421{
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000422 struct s3c24x0_i2c *i2c;
wdenkfc3e2162003-10-08 22:33:00 +0000423 uchar buf[1];
wdenk1cb8e982003-03-06 21:55:29 +0000424
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000425 i2c = get_base_i2c();
wdenkfc3e2162003-10-08 22:33:00 +0000426 buf[0] = 0;
wdenk1cb8e982003-03-06 21:55:29 +0000427
wdenkfc3e2162003-10-08 22:33:00 +0000428 /*
429 * What is needed is to send the chip address and verify that the
430 * address was <ACK>ed (i.e. there was a chip at that address which
431 * drove the data line low).
432 */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000433 return i2c_transfer(i2c, I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
wdenk1cb8e982003-03-06 21:55:29 +0000434}
435
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900436int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1cb8e982003-03-06 21:55:29 +0000437{
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000438 struct s3c24x0_i2c *i2c;
wdenkfc3e2162003-10-08 22:33:00 +0000439 uchar xaddr[4];
440 int ret;
wdenk1cb8e982003-03-06 21:55:29 +0000441
wdenkfc3e2162003-10-08 22:33:00 +0000442 if (alen > 4) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000443 debug("I2C read: addr len %d not supported\n", alen);
wdenkfc3e2162003-10-08 22:33:00 +0000444 return 1;
445 }
wdenk1cb8e982003-03-06 21:55:29 +0000446
wdenkfc3e2162003-10-08 22:33:00 +0000447 if (alen > 0) {
448 xaddr[0] = (addr >> 24) & 0xFF;
449 xaddr[1] = (addr >> 16) & 0xFF;
450 xaddr[2] = (addr >> 8) & 0xFF;
451 xaddr[3] = addr & 0xFF;
452 }
wdenk1cb8e982003-03-06 21:55:29 +0000453
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200454#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenkfc3e2162003-10-08 22:33:00 +0000455 /*
456 * EEPROM chips that implement "address overflow" are ones
457 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
458 * address and the extra bits end up in the "chip address"
459 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
460 * four 256 byte chips.
461 *
462 * Note that we consider the length of the address field to
463 * still be one byte because the extra address bits are
464 * hidden in the chip address.
465 */
466 if (alen > 0)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900467 chip |= ((addr >> (alen * 8)) &
468 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1cb8e982003-03-06 21:55:29 +0000469#endif
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000470 i2c = get_base_i2c();
471 ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen,
472 buffer, len);
473 if (ret != 0) {
474 debug("I2c read: failed %d\n", ret);
wdenkfc3e2162003-10-08 22:33:00 +0000475 return 1;
476 }
477 return 0;
wdenk1cb8e982003-03-06 21:55:29 +0000478}
479
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900480int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1cb8e982003-03-06 21:55:29 +0000481{
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000482 struct s3c24x0_i2c *i2c;
wdenkfc3e2162003-10-08 22:33:00 +0000483 uchar xaddr[4];
wdenk1cb8e982003-03-06 21:55:29 +0000484
wdenkfc3e2162003-10-08 22:33:00 +0000485 if (alen > 4) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000486 debug("I2C write: addr len %d not supported\n", alen);
wdenkfc3e2162003-10-08 22:33:00 +0000487 return 1;
488 }
wdenk1cb8e982003-03-06 21:55:29 +0000489
wdenkfc3e2162003-10-08 22:33:00 +0000490 if (alen > 0) {
491 xaddr[0] = (addr >> 24) & 0xFF;
492 xaddr[1] = (addr >> 16) & 0xFF;
493 xaddr[2] = (addr >> 8) & 0xFF;
494 xaddr[3] = addr & 0xFF;
495 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200496#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenkfc3e2162003-10-08 22:33:00 +0000497 /*
498 * EEPROM chips that implement "address overflow" are ones
499 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
500 * address and the extra bits end up in the "chip address"
501 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
502 * four 256 byte chips.
503 *
504 * Note that we consider the length of the address field to
505 * still be one byte because the extra address bits are
506 * hidden in the chip address.
507 */
508 if (alen > 0)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900509 chip |= ((addr >> (alen * 8)) &
510 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1cb8e982003-03-06 21:55:29 +0000511#endif
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000512 i2c = get_base_i2c();
wdenkfc3e2162003-10-08 22:33:00 +0000513 return (i2c_transfer
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000514 (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
wdenkfc3e2162003-10-08 22:33:00 +0000515 len) != 0);
wdenk1cb8e982003-03-06 21:55:29 +0000516}
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000517
Amar1ae76d42013-07-10 10:42:29 +0530518#ifdef CONFIG_OF_CONTROL
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000519void board_i2c_init(const void *blob)
520{
Amar2c07bb92013-04-04 02:27:06 -0400521 int i;
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000522 int node_list[CONFIG_MAX_I2C_NUM];
Amar2c07bb92013-04-04 02:27:06 -0400523 int count;
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000524
525 count = fdtdec_find_aliases_for_id(blob, "i2c",
526 COMPAT_SAMSUNG_S3C2440_I2C, node_list,
527 CONFIG_MAX_I2C_NUM);
528
529 for (i = 0; i < count; i++) {
530 struct s3c24x0_i2c_bus *bus;
531 int node = node_list[i];
532
533 if (node <= 0)
534 continue;
535 bus = &i2c_bus[i];
536 bus->regs = (struct s3c24x0_i2c *)
537 fdtdec_get_addr(blob, node, "reg");
538 bus->id = pinmux_decode_periph_id(blob, node);
539 bus->node = node;
540 bus->bus_num = i2c_busses++;
541 exynos_pinmux_config(bus->id, 0);
542 }
543}
544
545static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx)
546{
547 if (bus_idx < i2c_busses)
548 return &i2c_bus[bus_idx];
549
550 debug("Undefined bus: %d\n", bus_idx);
551 return NULL;
552}
553
554int i2c_get_bus_num_fdt(int node)
555{
556 int i;
557
558 for (i = 0; i < i2c_busses; i++) {
559 if (node == i2c_bus[i].node)
560 return i;
561 }
562
563 debug("%s: Can't find any matched I2C bus\n", __func__);
564 return -1;
565}
566
567int i2c_reset_port_fdt(const void *blob, int node)
568{
569 struct s3c24x0_i2c_bus *i2c;
570 int bus;
571
572 bus = i2c_get_bus_num_fdt(node);
573 if (bus < 0) {
574 debug("could not get bus for node %d\n", node);
575 return -1;
576 }
577
578 i2c = get_bus(bus);
579 if (!i2c) {
580 debug("get_bus() failed for node node %d\n", node);
581 return -1;
582 }
583
584 i2c_ch_init(i2c->regs, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
585
586 return 0;
587}
588#endif
589
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900590#endif /* CONFIG_HARD_I2C */