blob: 7ec01ec62f419e4f5b11c97cd79c102395572530 [file] [log] [blame]
wdenk1fe2c702003-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 Shinde53cfac52012-12-26 20:03:12 +000030#include <fdtdec.h>
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +000031#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000032#include <asm/arch/clk.h>
33#include <asm/arch/cpu.h>
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +000034#include <asm/arch/pinmux.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000035#else
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090036#include <asm/arch/s3c24x0_cpu.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000037#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090038#include <asm/io.h>
wdenk1fe2c702003-03-06 21:55:29 +000039#include <i2c.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000040#include "s3c24x0_i2c.h"
wdenk1fe2c702003-03-06 21:55:29 +000041
42#ifdef CONFIG_HARD_I2C
43
wdenk7539dea2003-06-19 23:01:32 +000044#define I2C_WRITE 0
45#define I2C_READ 1
wdenk1fe2c702003-03-06 21:55:29 +000046
wdenk7539dea2003-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.uk1464f4d2009-10-10 13:33:11 +090050#define I2C_NOK_LA 3 /* Lost arbitration */
51#define I2C_NOK_TOUT 4 /* time out */
wdenk1fe2c702003-03-06 21:55:29 +000052
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090053#define I2CSTAT_BSY 0x20 /* Busy bit */
54#define I2CSTAT_NACK 0x01 /* Nack bit */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000055#define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-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 */
wdenk1fe2c702003-03-06 21:55:29 +000061
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090062#define I2C_TIMEOUT 1 /* 1 second */
wdenk1fe2c702003-03-06 21:55:29 +000063
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000064
Rajeshwari Shinde53cfac52012-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")));
70static struct s3c24x0_i2c_bus i2c_bus[CONFIG_MAX_I2C_NUM]
71 __attribute__((section(".data")));
72static int i2c_busses __attribute__((section(".data")));
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000073
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +000074#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
wdenk7539dea2003-06-19 23:01:32 +000075static int GetI2CSDA(void)
wdenk1fe2c702003-03-06 21:55:29 +000076{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090077 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk7539dea2003-06-19 23:01:32 +000078
wdenkca9bc762003-07-15 07:45:49 +000079#ifdef CONFIG_S3C2410
C Nauman383c43e2010-10-26 23:04:31 +090080 return (readl(&gpio->gpedat) & 0x8000) >> 15;
wdenkca9bc762003-07-15 07:45:49 +000081#endif
82#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +090083 return (readl(&gpio->pgdat) & 0x0020) >> 5;
wdenkca9bc762003-07-15 07:45:49 +000084#endif
wdenk1fe2c702003-03-06 21:55:29 +000085}
86
wdenk6b58f332003-03-14 20:47:52 +000087#if 0
wdenk7539dea2003-06-19 23:01:32 +000088static void SetI2CSDA(int x)
wdenk1fe2c702003-03-06 21:55:29 +000089{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090090 rGPEDAT = (rGPEDAT & ~0x8000) | (x & 1) << 15;
wdenk1fe2c702003-03-06 21:55:29 +000091}
wdenk6b58f332003-03-14 20:47:52 +000092#endif
wdenk1fe2c702003-03-06 21:55:29 +000093
wdenk7539dea2003-06-19 23:01:32 +000094static void SetI2CSCL(int x)
wdenk1fe2c702003-03-06 21:55:29 +000095{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090096 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk7539dea2003-06-19 23:01:32 +000097
wdenkca9bc762003-07-15 07:45:49 +000098#ifdef CONFIG_S3C2410
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000099 writel((readl(&gpio->gpedat) & ~0x4000) |
100 (x & 1) << 14, &gpio->gpedat);
wdenkca9bc762003-07-15 07:45:49 +0000101#endif
102#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900103 writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
wdenkca9bc762003-07-15 07:45:49 +0000104#endif
wdenk1fe2c702003-03-06 21:55:29 +0000105}
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000106#endif
wdenk1fe2c702003-03-06 21:55:29 +0000107
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000108static int WaitForXfer(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +0000109{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900110 int i;
wdenk1fe2c702003-03-06 21:55:29 +0000111
wdenk49c3f672003-10-08 22:33:00 +0000112 i = I2C_TIMEOUT * 10000;
C Nauman383c43e2010-10-26 23:04:31 +0900113 while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900114 udelay(100);
wdenk49c3f672003-10-08 22:33:00 +0000115 i--;
116 }
wdenk1fe2c702003-03-06 21:55:29 +0000117
C Nauman383c43e2010-10-26 23:04:31 +0900118 return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
wdenk1fe2c702003-03-06 21:55:29 +0000119}
120
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000121static int IsACK(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +0000122{
C Nauman383c43e2010-10-26 23:04:31 +0900123 return !(readl(&i2c->iicstat) & I2CSTAT_NACK);
wdenk1fe2c702003-03-06 21:55:29 +0000124}
125
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000126static void ReadWriteByte(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +0000127{
C Nauman383c43e2010-10-26 23:04:31 +0900128 writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
wdenk1fe2c702003-03-06 21:55:29 +0000129}
130
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000131static struct s3c24x0_i2c *get_base_i2c(void)
132{
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000133#ifdef CONFIG_EXYNOS4
134 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
135 + (EXYNOS4_I2C_SPACING
136 * g_current_bus));
137 return i2c;
138#elif defined CONFIG_EXYNOS5
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000139 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
140 + (EXYNOS5_I2C_SPACING
141 * g_current_bus));
142 return i2c;
143#else
144 return s3c24x0_get_base_i2c();
145#endif
146}
147
148static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
149{
150 ulong freq, pres = 16, div;
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000151#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000152 freq = get_i2c_clk();
153#else
154 freq = get_PCLK();
155#endif
156 /* calculate prescaler and divisor values */
157 if ((freq / pres / (16 + 1)) > speed)
158 /* set prescaler to 512 */
159 pres = 512;
160
161 div = 0;
162 while ((freq / pres / (div + 1)) > speed)
163 div++;
164
165 /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
166 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
167
168 /* init to SLAVE REVEIVE and set slaveaddr */
169 writel(0, &i2c->iicstat);
170 writel(slaveadd, &i2c->iicadd);
171 /* program Master Transmit (and implicit STOP) */
172 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
173}
174
Rajeshwari Shindec07c11d2012-07-23 21:23:54 +0000175/*
176 * MULTI BUS I2C support
177 */
178
179#ifdef CONFIG_I2C_MULTI_BUS
180int i2c_set_bus_num(unsigned int bus)
181{
182 struct s3c24x0_i2c *i2c;
183
184 if ((bus < 0) || (bus >= CONFIG_MAX_I2C_NUM)) {
185 debug("Bad bus: %d\n", bus);
186 return -1;
187 }
188
189 g_current_bus = bus;
190 i2c = get_base_i2c();
191 i2c_ch_init(i2c, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
192
193 return 0;
194}
195
196unsigned int i2c_get_bus_num(void)
197{
198 return g_current_bus;
199}
200#endif
201
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900202void i2c_init(int speed, int slaveadd)
wdenk1fe2c702003-03-06 21:55:29 +0000203{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000204 struct s3c24x0_i2c *i2c;
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000205#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900206 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000207#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900208 int i;
wdenk1fe2c702003-03-06 21:55:29 +0000209
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000210 /* By default i2c channel 0 is the current bus */
211 g_current_bus = 0;
212 i2c = get_base_i2c();
wdenk1fe2c702003-03-06 21:55:29 +0000213
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000214 /* wait for some time to give previous transfer a chance to finish */
wdenk49c3f672003-10-08 22:33:00 +0000215 i = I2C_TIMEOUT * 1000;
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000216 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900217 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000218 i--;
219 }
wdenk1fe2c702003-03-06 21:55:29 +0000220
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000221#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
C Nauman383c43e2010-10-26 23:04:31 +0900222 if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
wdenkca9bc762003-07-15 07:45:49 +0000223#ifdef CONFIG_S3C2410
C Nauman383c43e2010-10-26 23:04:31 +0900224 ulong old_gpecon = readl(&gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000225#endif
226#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900227 ulong old_gpecon = readl(&gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000228#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900229 /* bus still busy probably by (most) previously interrupted
230 transfer */
wdenk1fe2c702003-03-06 21:55:29 +0000231
wdenkca9bc762003-07-15 07:45:49 +0000232#ifdef CONFIG_S3C2410
wdenk49c3f672003-10-08 22:33:00 +0000233 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
C Nauman383c43e2010-10-26 23:04:31 +0900234 writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
235 &gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000236#endif
237#ifdef CONFIG_S3C2400
wdenk49c3f672003-10-08 22:33:00 +0000238 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
C Nauman383c43e2010-10-26 23:04:31 +0900239 writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
240 &gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000241#endif
wdenk1fe2c702003-03-06 21:55:29 +0000242
wdenk49c3f672003-10-08 22:33:00 +0000243 /* toggle I2CSCL until bus idle */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900244 SetI2CSCL(0);
245 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000246 i = 10;
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900247 while ((i > 0) && (GetI2CSDA() != 1)) {
248 SetI2CSCL(1);
249 udelay(1000);
250 SetI2CSCL(0);
251 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000252 i--;
253 }
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900254 SetI2CSCL(1);
255 udelay(1000);
wdenk1fe2c702003-03-06 21:55:29 +0000256
wdenk49c3f672003-10-08 22:33:00 +0000257 /* restore pin functions */
wdenkca9bc762003-07-15 07:45:49 +0000258#ifdef CONFIG_S3C2410
C Nauman383c43e2010-10-26 23:04:31 +0900259 writel(old_gpecon, &gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000260#endif
261#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900262 writel(old_gpecon, &gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000263#endif
wdenk49c3f672003-10-08 22:33:00 +0000264 }
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000265#endif /* #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000266 i2c_ch_init(i2c, speed, slaveadd);
wdenk1fe2c702003-03-06 21:55:29 +0000267}
268
269/*
wdenk49c3f672003-10-08 22:33:00 +0000270 * cmd_type is 0 for write, 1 for read.
271 *
272 * addr_len can take any value from 0-255, it is only limited
273 * by the char, we could make it larger if needed. If it is
274 * 0 we skip the address write cycle.
275 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000276static int i2c_transfer(struct s3c24x0_i2c *i2c,
277 unsigned char cmd_type,
278 unsigned char chip,
279 unsigned char addr[],
280 unsigned char addr_len,
281 unsigned char data[],
282 unsigned short data_len)
wdenk1fe2c702003-03-06 21:55:29 +0000283{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900284 int i, result;
wdenk1fe2c702003-03-06 21:55:29 +0000285
wdenk49c3f672003-10-08 22:33:00 +0000286 if (data == 0 || data_len == 0) {
287 /*Don't support data transfer of no length or to address 0 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000288 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000289 return I2C_NOK;
290 }
wdenk1fe2c702003-03-06 21:55:29 +0000291
wdenk49c3f672003-10-08 22:33:00 +0000292 /* Check I2C bus idle */
293 i = I2C_TIMEOUT * 1000;
C Nauman383c43e2010-10-26 23:04:31 +0900294 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900295 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000296 i--;
297 }
wdenk1fe2c702003-03-06 21:55:29 +0000298
C Nauman383c43e2010-10-26 23:04:31 +0900299 if (readl(&i2c->iicstat) & I2CSTAT_BSY)
wdenk49c3f672003-10-08 22:33:00 +0000300 return I2C_NOK_TOUT;
wdenk1fe2c702003-03-06 21:55:29 +0000301
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000302 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
wdenk49c3f672003-10-08 22:33:00 +0000303 result = I2C_OK;
wdenk1fe2c702003-03-06 21:55:29 +0000304
wdenk49c3f672003-10-08 22:33:00 +0000305 switch (cmd_type) {
wdenk7539dea2003-06-19 23:01:32 +0000306 case I2C_WRITE:
wdenk49c3f672003-10-08 22:33:00 +0000307 if (addr && addr_len) {
C Nauman383c43e2010-10-26 23:04:31 +0900308 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000309 /* send START */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900310 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Nauman383c43e2010-10-26 23:04:31 +0900311 &i2c->iicstat);
wdenk49c3f672003-10-08 22:33:00 +0000312 i = 0;
313 while ((i < addr_len) && (result == I2C_OK)) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000314 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900315 writel(addr[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000316 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000317 i++;
318 }
319 i = 0;
320 while ((i < data_len) && (result == I2C_OK)) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000321 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900322 writel(data[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000323 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000324 i++;
325 }
326 } else {
C Nauman383c43e2010-10-26 23:04:31 +0900327 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000328 /* send START */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900329 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Nauman383c43e2010-10-26 23:04:31 +0900330 &i2c->iicstat);
wdenk49c3f672003-10-08 22:33:00 +0000331 i = 0;
332 while ((i < data_len) && (result = I2C_OK)) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000333 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900334 writel(data[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000335 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000336 i++;
337 }
wdenk1fe2c702003-03-06 21:55:29 +0000338 }
wdenk1fe2c702003-03-06 21:55:29 +0000339
wdenk49c3f672003-10-08 22:33:00 +0000340 if (result == I2C_OK)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000341 result = WaitForXfer(i2c);
wdenk1fe2c702003-03-06 21:55:29 +0000342
wdenk49c3f672003-10-08 22:33:00 +0000343 /* send STOP */
C Nauman383c43e2010-10-26 23:04:31 +0900344 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000345 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000346 break;
wdenk1fe2c702003-03-06 21:55:29 +0000347
wdenk7539dea2003-06-19 23:01:32 +0000348 case I2C_READ:
wdenk49c3f672003-10-08 22:33:00 +0000349 if (addr && addr_len) {
C Nauman383c43e2010-10-26 23:04:31 +0900350 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
351 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000352 /* send START */
C Nauman383c43e2010-10-26 23:04:31 +0900353 writel(readl(&i2c->iicstat) | I2C_START_STOP,
354 &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000355 result = WaitForXfer(i2c);
356 if (IsACK(i2c)) {
wdenk49c3f672003-10-08 22:33:00 +0000357 i = 0;
358 while ((i < addr_len) && (result == I2C_OK)) {
C Nauman383c43e2010-10-26 23:04:31 +0900359 writel(addr[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000360 ReadWriteByte(i2c);
361 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000362 i++;
363 }
wdenk1fe2c702003-03-06 21:55:29 +0000364
C Nauman383c43e2010-10-26 23:04:31 +0900365 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000366 /* resend START */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900367 writel(I2C_MODE_MR | I2C_TXRX_ENA |
C Nauman383c43e2010-10-26 23:04:31 +0900368 I2C_START_STOP, &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000369 ReadWriteByte(i2c);
370 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000371 i = 0;
372 while ((i < data_len) && (result == I2C_OK)) {
373 /* disable ACK for final READ */
374 if (i == data_len - 1)
C Nauman383c43e2010-10-26 23:04:31 +0900375 writel(readl(&i2c->iiccon)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000376 & ~I2CCON_ACKGEN,
377 &i2c->iiccon);
378 ReadWriteByte(i2c);
379 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900380 data[i] = readl(&i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000381 i++;
382 }
383 } else {
384 result = I2C_NACK;
385 }
wdenk1fe2c702003-03-06 21:55:29 +0000386
wdenk1fe2c702003-03-06 21:55:29 +0000387 } else {
C Nauman383c43e2010-10-26 23:04:31 +0900388 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
389 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000390 /* send START */
C Nauman383c43e2010-10-26 23:04:31 +0900391 writel(readl(&i2c->iicstat) | I2C_START_STOP,
392 &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000393 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000394
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000395 if (IsACK(i2c)) {
wdenk49c3f672003-10-08 22:33:00 +0000396 i = 0;
397 while ((i < data_len) && (result == I2C_OK)) {
398 /* disable ACK for final READ */
399 if (i == data_len - 1)
C Nauman383c43e2010-10-26 23:04:31 +0900400 writel(readl(&i2c->iiccon) &
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000401 ~I2CCON_ACKGEN,
402 &i2c->iiccon);
403 ReadWriteByte(i2c);
404 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900405 data[i] = readl(&i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000406 i++;
407 }
408 } else {
409 result = I2C_NACK;
410 }
wdenk1fe2c702003-03-06 21:55:29 +0000411 }
wdenk1fe2c702003-03-06 21:55:29 +0000412
wdenk49c3f672003-10-08 22:33:00 +0000413 /* send STOP */
C Nauman383c43e2010-10-26 23:04:31 +0900414 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000415 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000416 break;
wdenk1fe2c702003-03-06 21:55:29 +0000417
418 default:
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000419 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000420 result = I2C_NOK;
421 break;
422 }
wdenk1fe2c702003-03-06 21:55:29 +0000423
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000424 return result;
wdenk1fe2c702003-03-06 21:55:29 +0000425}
426
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900427int i2c_probe(uchar chip)
wdenk1fe2c702003-03-06 21:55:29 +0000428{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000429 struct s3c24x0_i2c *i2c;
wdenk49c3f672003-10-08 22:33:00 +0000430 uchar buf[1];
wdenk1fe2c702003-03-06 21:55:29 +0000431
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000432 i2c = get_base_i2c();
wdenk49c3f672003-10-08 22:33:00 +0000433 buf[0] = 0;
wdenk1fe2c702003-03-06 21:55:29 +0000434
wdenk49c3f672003-10-08 22:33:00 +0000435 /*
436 * What is needed is to send the chip address and verify that the
437 * address was <ACK>ed (i.e. there was a chip at that address which
438 * drove the data line low).
439 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000440 return i2c_transfer(i2c, I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
wdenk1fe2c702003-03-06 21:55:29 +0000441}
442
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900443int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1fe2c702003-03-06 21:55:29 +0000444{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000445 struct s3c24x0_i2c *i2c;
wdenk49c3f672003-10-08 22:33:00 +0000446 uchar xaddr[4];
447 int ret;
wdenk1fe2c702003-03-06 21:55:29 +0000448
wdenk49c3f672003-10-08 22:33:00 +0000449 if (alen > 4) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000450 debug("I2C read: addr len %d not supported\n", alen);
wdenk49c3f672003-10-08 22:33:00 +0000451 return 1;
452 }
wdenk1fe2c702003-03-06 21:55:29 +0000453
wdenk49c3f672003-10-08 22:33:00 +0000454 if (alen > 0) {
455 xaddr[0] = (addr >> 24) & 0xFF;
456 xaddr[1] = (addr >> 16) & 0xFF;
457 xaddr[2] = (addr >> 8) & 0xFF;
458 xaddr[3] = addr & 0xFF;
459 }
wdenk1fe2c702003-03-06 21:55:29 +0000460
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200461#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenk49c3f672003-10-08 22:33:00 +0000462 /*
463 * EEPROM chips that implement "address overflow" are ones
464 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
465 * address and the extra bits end up in the "chip address"
466 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
467 * four 256 byte chips.
468 *
469 * Note that we consider the length of the address field to
470 * still be one byte because the extra address bits are
471 * hidden in the chip address.
472 */
473 if (alen > 0)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900474 chip |= ((addr >> (alen * 8)) &
475 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1fe2c702003-03-06 21:55:29 +0000476#endif
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000477 i2c = get_base_i2c();
478 ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen,
479 buffer, len);
480 if (ret != 0) {
481 debug("I2c read: failed %d\n", ret);
wdenk49c3f672003-10-08 22:33:00 +0000482 return 1;
483 }
484 return 0;
wdenk1fe2c702003-03-06 21:55:29 +0000485}
486
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900487int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1fe2c702003-03-06 21:55:29 +0000488{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000489 struct s3c24x0_i2c *i2c;
wdenk49c3f672003-10-08 22:33:00 +0000490 uchar xaddr[4];
wdenk1fe2c702003-03-06 21:55:29 +0000491
wdenk49c3f672003-10-08 22:33:00 +0000492 if (alen > 4) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000493 debug("I2C write: addr len %d not supported\n", alen);
wdenk49c3f672003-10-08 22:33:00 +0000494 return 1;
495 }
wdenk1fe2c702003-03-06 21:55:29 +0000496
wdenk49c3f672003-10-08 22:33:00 +0000497 if (alen > 0) {
498 xaddr[0] = (addr >> 24) & 0xFF;
499 xaddr[1] = (addr >> 16) & 0xFF;
500 xaddr[2] = (addr >> 8) & 0xFF;
501 xaddr[3] = addr & 0xFF;
502 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200503#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenk49c3f672003-10-08 22:33:00 +0000504 /*
505 * EEPROM chips that implement "address overflow" are ones
506 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
507 * address and the extra bits end up in the "chip address"
508 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
509 * four 256 byte chips.
510 *
511 * Note that we consider the length of the address field to
512 * still be one byte because the extra address bits are
513 * hidden in the chip address.
514 */
515 if (alen > 0)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900516 chip |= ((addr >> (alen * 8)) &
517 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1fe2c702003-03-06 21:55:29 +0000518#endif
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000519 i2c = get_base_i2c();
wdenk49c3f672003-10-08 22:33:00 +0000520 return (i2c_transfer
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000521 (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
wdenk49c3f672003-10-08 22:33:00 +0000522 len) != 0);
wdenk1fe2c702003-03-06 21:55:29 +0000523}
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +0000524
525#ifdef CONFIG_OF_CONTROL
526void board_i2c_init(const void *blob)
527{
528 int node_list[CONFIG_MAX_I2C_NUM];
529 int count, i;
530
531 count = fdtdec_find_aliases_for_id(blob, "i2c",
532 COMPAT_SAMSUNG_S3C2440_I2C, node_list,
533 CONFIG_MAX_I2C_NUM);
534
535 for (i = 0; i < count; i++) {
536 struct s3c24x0_i2c_bus *bus;
537 int node = node_list[i];
538
539 if (node <= 0)
540 continue;
541 bus = &i2c_bus[i];
542 bus->regs = (struct s3c24x0_i2c *)
543 fdtdec_get_addr(blob, node, "reg");
544 bus->id = pinmux_decode_periph_id(blob, node);
545 bus->node = node;
546 bus->bus_num = i2c_busses++;
547 exynos_pinmux_config(bus->id, 0);
548 }
549}
550
551static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx)
552{
553 if (bus_idx < i2c_busses)
554 return &i2c_bus[bus_idx];
555
556 debug("Undefined bus: %d\n", bus_idx);
557 return NULL;
558}
559
560int i2c_get_bus_num_fdt(int node)
561{
562 int i;
563
564 for (i = 0; i < i2c_busses; i++) {
565 if (node == i2c_bus[i].node)
566 return i;
567 }
568
569 debug("%s: Can't find any matched I2C bus\n", __func__);
570 return -1;
571}
572
573int i2c_reset_port_fdt(const void *blob, int node)
574{
575 struct s3c24x0_i2c_bus *i2c;
576 int bus;
577
578 bus = i2c_get_bus_num_fdt(node);
579 if (bus < 0) {
580 debug("could not get bus for node %d\n", node);
581 return -1;
582 }
583
584 i2c = get_bus(bus);
585 if (!i2c) {
586 debug("get_bus() failed for node node %d\n", node);
587 return -1;
588 }
589
590 i2c_ch_init(i2c->regs, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
591
592 return 0;
593}
594#endif
595
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900596#endif /* CONFIG_HARD_I2C */