blob: 769a2ba5ba32bd77a492647900bc86e5626f89d4 [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")));
Rajeshwari Shinde9a611f62013-01-13 19:49:36 +000070#ifdef CONFIG_OF_CONTROL
71static int i2c_busses __attribute__((section(".data")));
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +000072static struct s3c24x0_i2c_bus i2c_bus[CONFIG_MAX_I2C_NUM]
73 __attribute__((section(".data")));
Rajeshwari Shinde9a611f62013-01-13 19:49:36 +000074#endif
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000075
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +000076#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
wdenk7539dea2003-06-19 23:01:32 +000077static int GetI2CSDA(void)
wdenk1fe2c702003-03-06 21:55:29 +000078{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090079 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk7539dea2003-06-19 23:01:32 +000080
wdenkca9bc762003-07-15 07:45:49 +000081#ifdef CONFIG_S3C2410
C Nauman383c43e2010-10-26 23:04:31 +090082 return (readl(&gpio->gpedat) & 0x8000) >> 15;
wdenkca9bc762003-07-15 07:45:49 +000083#endif
84#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +090085 return (readl(&gpio->pgdat) & 0x0020) >> 5;
wdenkca9bc762003-07-15 07:45:49 +000086#endif
wdenk1fe2c702003-03-06 21:55:29 +000087}
88
wdenk6b58f332003-03-14 20:47:52 +000089#if 0
wdenk7539dea2003-06-19 23:01:32 +000090static void SetI2CSDA(int x)
wdenk1fe2c702003-03-06 21:55:29 +000091{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090092 rGPEDAT = (rGPEDAT & ~0x8000) | (x & 1) << 15;
wdenk1fe2c702003-03-06 21:55:29 +000093}
wdenk6b58f332003-03-14 20:47:52 +000094#endif
wdenk1fe2c702003-03-06 21:55:29 +000095
wdenk7539dea2003-06-19 23:01:32 +000096static void SetI2CSCL(int x)
wdenk1fe2c702003-03-06 21:55:29 +000097{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090098 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk7539dea2003-06-19 23:01:32 +000099
wdenkca9bc762003-07-15 07:45:49 +0000100#ifdef CONFIG_S3C2410
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000101 writel((readl(&gpio->gpedat) & ~0x4000) |
102 (x & 1) << 14, &gpio->gpedat);
wdenkca9bc762003-07-15 07:45:49 +0000103#endif
104#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900105 writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
wdenkca9bc762003-07-15 07:45:49 +0000106#endif
wdenk1fe2c702003-03-06 21:55:29 +0000107}
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000108#endif
wdenk1fe2c702003-03-06 21:55:29 +0000109
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000110static int WaitForXfer(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +0000111{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900112 int i;
wdenk1fe2c702003-03-06 21:55:29 +0000113
wdenk49c3f672003-10-08 22:33:00 +0000114 i = I2C_TIMEOUT * 10000;
C Nauman383c43e2010-10-26 23:04:31 +0900115 while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900116 udelay(100);
wdenk49c3f672003-10-08 22:33:00 +0000117 i--;
118 }
wdenk1fe2c702003-03-06 21:55:29 +0000119
C Nauman383c43e2010-10-26 23:04:31 +0900120 return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
wdenk1fe2c702003-03-06 21:55:29 +0000121}
122
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000123static int IsACK(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +0000124{
C Nauman383c43e2010-10-26 23:04:31 +0900125 return !(readl(&i2c->iicstat) & I2CSTAT_NACK);
wdenk1fe2c702003-03-06 21:55:29 +0000126}
127
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000128static void ReadWriteByte(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +0000129{
C Nauman383c43e2010-10-26 23:04:31 +0900130 writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
wdenk1fe2c702003-03-06 21:55:29 +0000131}
132
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000133static struct s3c24x0_i2c *get_base_i2c(void)
134{
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000135#ifdef CONFIG_EXYNOS4
136 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
137 + (EXYNOS4_I2C_SPACING
138 * g_current_bus));
139 return i2c;
140#elif defined CONFIG_EXYNOS5
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000141 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
142 + (EXYNOS5_I2C_SPACING
143 * g_current_bus));
144 return i2c;
145#else
146 return s3c24x0_get_base_i2c();
147#endif
148}
149
150static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
151{
152 ulong freq, pres = 16, div;
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000153#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000154 freq = get_i2c_clk();
155#else
156 freq = get_PCLK();
157#endif
158 /* calculate prescaler and divisor values */
159 if ((freq / pres / (16 + 1)) > speed)
160 /* set prescaler to 512 */
161 pres = 512;
162
163 div = 0;
164 while ((freq / pres / (div + 1)) > speed)
165 div++;
166
167 /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
168 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
169
170 /* init to SLAVE REVEIVE and set slaveaddr */
171 writel(0, &i2c->iicstat);
172 writel(slaveadd, &i2c->iicadd);
173 /* program Master Transmit (and implicit STOP) */
174 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
175}
176
Rajeshwari Shindec07c11d2012-07-23 21:23:54 +0000177/*
178 * MULTI BUS I2C support
179 */
180
181#ifdef CONFIG_I2C_MULTI_BUS
182int i2c_set_bus_num(unsigned int bus)
183{
184 struct s3c24x0_i2c *i2c;
185
186 if ((bus < 0) || (bus >= CONFIG_MAX_I2C_NUM)) {
187 debug("Bad bus: %d\n", bus);
188 return -1;
189 }
190
191 g_current_bus = bus;
192 i2c = get_base_i2c();
193 i2c_ch_init(i2c, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
194
195 return 0;
196}
197
198unsigned int i2c_get_bus_num(void)
199{
200 return g_current_bus;
201}
202#endif
203
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900204void i2c_init(int speed, int slaveadd)
wdenk1fe2c702003-03-06 21:55:29 +0000205{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000206 struct s3c24x0_i2c *i2c;
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000207#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900208 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000209#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900210 int i;
wdenk1fe2c702003-03-06 21:55:29 +0000211
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000212 /* By default i2c channel 0 is the current bus */
213 g_current_bus = 0;
214 i2c = get_base_i2c();
wdenk1fe2c702003-03-06 21:55:29 +0000215
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000216 /* wait for some time to give previous transfer a chance to finish */
wdenk49c3f672003-10-08 22:33:00 +0000217 i = I2C_TIMEOUT * 1000;
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000218 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900219 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000220 i--;
221 }
wdenk1fe2c702003-03-06 21:55:29 +0000222
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000223#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
C Nauman383c43e2010-10-26 23:04:31 +0900224 if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
wdenkca9bc762003-07-15 07:45:49 +0000225#ifdef CONFIG_S3C2410
C Nauman383c43e2010-10-26 23:04:31 +0900226 ulong old_gpecon = readl(&gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000227#endif
228#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900229 ulong old_gpecon = readl(&gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000230#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900231 /* bus still busy probably by (most) previously interrupted
232 transfer */
wdenk1fe2c702003-03-06 21:55:29 +0000233
wdenkca9bc762003-07-15 07:45:49 +0000234#ifdef CONFIG_S3C2410
wdenk49c3f672003-10-08 22:33:00 +0000235 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
C Nauman383c43e2010-10-26 23:04:31 +0900236 writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
237 &gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000238#endif
239#ifdef CONFIG_S3C2400
wdenk49c3f672003-10-08 22:33:00 +0000240 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
C Nauman383c43e2010-10-26 23:04:31 +0900241 writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
242 &gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000243#endif
wdenk1fe2c702003-03-06 21:55:29 +0000244
wdenk49c3f672003-10-08 22:33:00 +0000245 /* toggle I2CSCL until bus idle */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900246 SetI2CSCL(0);
247 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000248 i = 10;
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900249 while ((i > 0) && (GetI2CSDA() != 1)) {
250 SetI2CSCL(1);
251 udelay(1000);
252 SetI2CSCL(0);
253 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000254 i--;
255 }
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900256 SetI2CSCL(1);
257 udelay(1000);
wdenk1fe2c702003-03-06 21:55:29 +0000258
wdenk49c3f672003-10-08 22:33:00 +0000259 /* restore pin functions */
wdenkca9bc762003-07-15 07:45:49 +0000260#ifdef CONFIG_S3C2410
C Nauman383c43e2010-10-26 23:04:31 +0900261 writel(old_gpecon, &gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000262#endif
263#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900264 writel(old_gpecon, &gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000265#endif
wdenk49c3f672003-10-08 22:33:00 +0000266 }
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000267#endif /* #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000268 i2c_ch_init(i2c, speed, slaveadd);
wdenk1fe2c702003-03-06 21:55:29 +0000269}
270
271/*
wdenk49c3f672003-10-08 22:33:00 +0000272 * cmd_type is 0 for write, 1 for read.
273 *
274 * addr_len can take any value from 0-255, it is only limited
275 * by the char, we could make it larger if needed. If it is
276 * 0 we skip the address write cycle.
277 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000278static int i2c_transfer(struct s3c24x0_i2c *i2c,
279 unsigned char cmd_type,
280 unsigned char chip,
281 unsigned char addr[],
282 unsigned char addr_len,
283 unsigned char data[],
284 unsigned short data_len)
wdenk1fe2c702003-03-06 21:55:29 +0000285{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900286 int i, result;
wdenk1fe2c702003-03-06 21:55:29 +0000287
wdenk49c3f672003-10-08 22:33:00 +0000288 if (data == 0 || data_len == 0) {
289 /*Don't support data transfer of no length or to address 0 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000290 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000291 return I2C_NOK;
292 }
wdenk1fe2c702003-03-06 21:55:29 +0000293
wdenk49c3f672003-10-08 22:33:00 +0000294 /* Check I2C bus idle */
295 i = I2C_TIMEOUT * 1000;
C Nauman383c43e2010-10-26 23:04:31 +0900296 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900297 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000298 i--;
299 }
wdenk1fe2c702003-03-06 21:55:29 +0000300
C Nauman383c43e2010-10-26 23:04:31 +0900301 if (readl(&i2c->iicstat) & I2CSTAT_BSY)
wdenk49c3f672003-10-08 22:33:00 +0000302 return I2C_NOK_TOUT;
wdenk1fe2c702003-03-06 21:55:29 +0000303
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000304 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
wdenk49c3f672003-10-08 22:33:00 +0000305 result = I2C_OK;
wdenk1fe2c702003-03-06 21:55:29 +0000306
wdenk49c3f672003-10-08 22:33:00 +0000307 switch (cmd_type) {
wdenk7539dea2003-06-19 23:01:32 +0000308 case I2C_WRITE:
wdenk49c3f672003-10-08 22:33:00 +0000309 if (addr && addr_len) {
C Nauman383c43e2010-10-26 23:04:31 +0900310 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000311 /* send START */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900312 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Nauman383c43e2010-10-26 23:04:31 +0900313 &i2c->iicstat);
wdenk49c3f672003-10-08 22:33:00 +0000314 i = 0;
315 while ((i < addr_len) && (result == I2C_OK)) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000316 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900317 writel(addr[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000318 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000319 i++;
320 }
321 i = 0;
322 while ((i < data_len) && (result == I2C_OK)) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000323 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900324 writel(data[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000325 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000326 i++;
327 }
328 } else {
C Nauman383c43e2010-10-26 23:04:31 +0900329 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000330 /* send START */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900331 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Nauman383c43e2010-10-26 23:04:31 +0900332 &i2c->iicstat);
wdenk49c3f672003-10-08 22:33:00 +0000333 i = 0;
334 while ((i < data_len) && (result = I2C_OK)) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000335 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900336 writel(data[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000337 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000338 i++;
339 }
wdenk1fe2c702003-03-06 21:55:29 +0000340 }
wdenk1fe2c702003-03-06 21:55:29 +0000341
wdenk49c3f672003-10-08 22:33:00 +0000342 if (result == I2C_OK)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000343 result = WaitForXfer(i2c);
wdenk1fe2c702003-03-06 21:55:29 +0000344
wdenk49c3f672003-10-08 22:33:00 +0000345 /* send STOP */
C Nauman383c43e2010-10-26 23:04:31 +0900346 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000347 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000348 break;
wdenk1fe2c702003-03-06 21:55:29 +0000349
wdenk7539dea2003-06-19 23:01:32 +0000350 case I2C_READ:
wdenk49c3f672003-10-08 22:33:00 +0000351 if (addr && addr_len) {
C Nauman383c43e2010-10-26 23:04:31 +0900352 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
353 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000354 /* send START */
C Nauman383c43e2010-10-26 23:04:31 +0900355 writel(readl(&i2c->iicstat) | I2C_START_STOP,
356 &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000357 result = WaitForXfer(i2c);
358 if (IsACK(i2c)) {
wdenk49c3f672003-10-08 22:33:00 +0000359 i = 0;
360 while ((i < addr_len) && (result == I2C_OK)) {
C Nauman383c43e2010-10-26 23:04:31 +0900361 writel(addr[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000362 ReadWriteByte(i2c);
363 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000364 i++;
365 }
wdenk1fe2c702003-03-06 21:55:29 +0000366
C Nauman383c43e2010-10-26 23:04:31 +0900367 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000368 /* resend START */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900369 writel(I2C_MODE_MR | I2C_TXRX_ENA |
C Nauman383c43e2010-10-26 23:04:31 +0900370 I2C_START_STOP, &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000371 ReadWriteByte(i2c);
372 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000373 i = 0;
374 while ((i < data_len) && (result == I2C_OK)) {
375 /* disable ACK for final READ */
376 if (i == data_len - 1)
C Nauman383c43e2010-10-26 23:04:31 +0900377 writel(readl(&i2c->iiccon)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000378 & ~I2CCON_ACKGEN,
379 &i2c->iiccon);
380 ReadWriteByte(i2c);
381 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900382 data[i] = readl(&i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000383 i++;
384 }
385 } else {
386 result = I2C_NACK;
387 }
wdenk1fe2c702003-03-06 21:55:29 +0000388
wdenk1fe2c702003-03-06 21:55:29 +0000389 } else {
C Nauman383c43e2010-10-26 23:04:31 +0900390 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
391 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000392 /* send START */
C Nauman383c43e2010-10-26 23:04:31 +0900393 writel(readl(&i2c->iicstat) | I2C_START_STOP,
394 &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000395 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000396
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000397 if (IsACK(i2c)) {
wdenk49c3f672003-10-08 22:33:00 +0000398 i = 0;
399 while ((i < data_len) && (result == I2C_OK)) {
400 /* disable ACK for final READ */
401 if (i == data_len - 1)
C Nauman383c43e2010-10-26 23:04:31 +0900402 writel(readl(&i2c->iiccon) &
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000403 ~I2CCON_ACKGEN,
404 &i2c->iiccon);
405 ReadWriteByte(i2c);
406 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900407 data[i] = readl(&i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000408 i++;
409 }
410 } else {
411 result = I2C_NACK;
412 }
wdenk1fe2c702003-03-06 21:55:29 +0000413 }
wdenk1fe2c702003-03-06 21:55:29 +0000414
wdenk49c3f672003-10-08 22:33:00 +0000415 /* send STOP */
C Nauman383c43e2010-10-26 23:04:31 +0900416 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000417 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000418 break;
wdenk1fe2c702003-03-06 21:55:29 +0000419
420 default:
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000421 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000422 result = I2C_NOK;
423 break;
424 }
wdenk1fe2c702003-03-06 21:55:29 +0000425
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000426 return result;
wdenk1fe2c702003-03-06 21:55:29 +0000427}
428
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900429int i2c_probe(uchar chip)
wdenk1fe2c702003-03-06 21:55:29 +0000430{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000431 struct s3c24x0_i2c *i2c;
wdenk49c3f672003-10-08 22:33:00 +0000432 uchar buf[1];
wdenk1fe2c702003-03-06 21:55:29 +0000433
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000434 i2c = get_base_i2c();
wdenk49c3f672003-10-08 22:33:00 +0000435 buf[0] = 0;
wdenk1fe2c702003-03-06 21:55:29 +0000436
wdenk49c3f672003-10-08 22:33:00 +0000437 /*
438 * What is needed is to send the chip address and verify that the
439 * address was <ACK>ed (i.e. there was a chip at that address which
440 * drove the data line low).
441 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000442 return i2c_transfer(i2c, I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
wdenk1fe2c702003-03-06 21:55:29 +0000443}
444
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900445int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1fe2c702003-03-06 21:55:29 +0000446{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000447 struct s3c24x0_i2c *i2c;
wdenk49c3f672003-10-08 22:33:00 +0000448 uchar xaddr[4];
449 int ret;
wdenk1fe2c702003-03-06 21:55:29 +0000450
wdenk49c3f672003-10-08 22:33:00 +0000451 if (alen > 4) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000452 debug("I2C read: addr len %d not supported\n", alen);
wdenk49c3f672003-10-08 22:33:00 +0000453 return 1;
454 }
wdenk1fe2c702003-03-06 21:55:29 +0000455
wdenk49c3f672003-10-08 22:33:00 +0000456 if (alen > 0) {
457 xaddr[0] = (addr >> 24) & 0xFF;
458 xaddr[1] = (addr >> 16) & 0xFF;
459 xaddr[2] = (addr >> 8) & 0xFF;
460 xaddr[3] = addr & 0xFF;
461 }
wdenk1fe2c702003-03-06 21:55:29 +0000462
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200463#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenk49c3f672003-10-08 22:33:00 +0000464 /*
465 * EEPROM chips that implement "address overflow" are ones
466 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
467 * address and the extra bits end up in the "chip address"
468 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
469 * four 256 byte chips.
470 *
471 * Note that we consider the length of the address field to
472 * still be one byte because the extra address bits are
473 * hidden in the chip address.
474 */
475 if (alen > 0)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900476 chip |= ((addr >> (alen * 8)) &
477 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1fe2c702003-03-06 21:55:29 +0000478#endif
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000479 i2c = get_base_i2c();
480 ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen,
481 buffer, len);
482 if (ret != 0) {
483 debug("I2c read: failed %d\n", ret);
wdenk49c3f672003-10-08 22:33:00 +0000484 return 1;
485 }
486 return 0;
wdenk1fe2c702003-03-06 21:55:29 +0000487}
488
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900489int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1fe2c702003-03-06 21:55:29 +0000490{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000491 struct s3c24x0_i2c *i2c;
wdenk49c3f672003-10-08 22:33:00 +0000492 uchar xaddr[4];
wdenk1fe2c702003-03-06 21:55:29 +0000493
wdenk49c3f672003-10-08 22:33:00 +0000494 if (alen > 4) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000495 debug("I2C write: addr len %d not supported\n", alen);
wdenk49c3f672003-10-08 22:33:00 +0000496 return 1;
497 }
wdenk1fe2c702003-03-06 21:55:29 +0000498
wdenk49c3f672003-10-08 22:33:00 +0000499 if (alen > 0) {
500 xaddr[0] = (addr >> 24) & 0xFF;
501 xaddr[1] = (addr >> 16) & 0xFF;
502 xaddr[2] = (addr >> 8) & 0xFF;
503 xaddr[3] = addr & 0xFF;
504 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200505#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenk49c3f672003-10-08 22:33:00 +0000506 /*
507 * EEPROM chips that implement "address overflow" are ones
508 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
509 * address and the extra bits end up in the "chip address"
510 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
511 * four 256 byte chips.
512 *
513 * Note that we consider the length of the address field to
514 * still be one byte because the extra address bits are
515 * hidden in the chip address.
516 */
517 if (alen > 0)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900518 chip |= ((addr >> (alen * 8)) &
519 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1fe2c702003-03-06 21:55:29 +0000520#endif
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000521 i2c = get_base_i2c();
wdenk49c3f672003-10-08 22:33:00 +0000522 return (i2c_transfer
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000523 (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
wdenk49c3f672003-10-08 22:33:00 +0000524 len) != 0);
wdenk1fe2c702003-03-06 21:55:29 +0000525}
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +0000526
527#ifdef CONFIG_OF_CONTROL
528void board_i2c_init(const void *blob)
529{
530 int node_list[CONFIG_MAX_I2C_NUM];
531 int count, i;
532
533 count = fdtdec_find_aliases_for_id(blob, "i2c",
534 COMPAT_SAMSUNG_S3C2440_I2C, node_list,
535 CONFIG_MAX_I2C_NUM);
536
537 for (i = 0; i < count; i++) {
538 struct s3c24x0_i2c_bus *bus;
539 int node = node_list[i];
540
541 if (node <= 0)
542 continue;
543 bus = &i2c_bus[i];
544 bus->regs = (struct s3c24x0_i2c *)
545 fdtdec_get_addr(blob, node, "reg");
546 bus->id = pinmux_decode_periph_id(blob, node);
547 bus->node = node;
548 bus->bus_num = i2c_busses++;
549 exynos_pinmux_config(bus->id, 0);
550 }
551}
552
553static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx)
554{
555 if (bus_idx < i2c_busses)
556 return &i2c_bus[bus_idx];
557
558 debug("Undefined bus: %d\n", bus_idx);
559 return NULL;
560}
561
562int i2c_get_bus_num_fdt(int node)
563{
564 int i;
565
566 for (i = 0; i < i2c_busses; i++) {
567 if (node == i2c_bus[i].node)
568 return i;
569 }
570
571 debug("%s: Can't find any matched I2C bus\n", __func__);
572 return -1;
573}
574
575int i2c_reset_port_fdt(const void *blob, int node)
576{
577 struct s3c24x0_i2c_bus *i2c;
578 int bus;
579
580 bus = i2c_get_bus_num_fdt(node);
581 if (bus < 0) {
582 debug("could not get bus for node %d\n", node);
583 return -1;
584 }
585
586 i2c = get_bus(bus);
587 if (!i2c) {
588 debug("get_bus() failed for node node %d\n", node);
589 return -1;
590 }
591
592 i2c_ch_init(i2c->regs, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
593
594 return 0;
595}
596#endif
597
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900598#endif /* CONFIG_HARD_I2C */