blob: 00308b5223f6004bd331c048107e9d5edc6e447b [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
wdenk7539dea2003-06-19 23:01:32 +000089static void SetI2CSCL(int x)
wdenk1fe2c702003-03-06 21:55:29 +000090{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090091 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk7539dea2003-06-19 23:01:32 +000092
wdenkca9bc762003-07-15 07:45:49 +000093#ifdef CONFIG_S3C2410
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000094 writel((readl(&gpio->gpedat) & ~0x4000) |
95 (x & 1) << 14, &gpio->gpedat);
wdenkca9bc762003-07-15 07:45:49 +000096#endif
97#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +090098 writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
wdenkca9bc762003-07-15 07:45:49 +000099#endif
wdenk1fe2c702003-03-06 21:55:29 +0000100}
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000101#endif
wdenk1fe2c702003-03-06 21:55:29 +0000102
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000103static int WaitForXfer(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +0000104{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900105 int i;
wdenk1fe2c702003-03-06 21:55:29 +0000106
wdenk49c3f672003-10-08 22:33:00 +0000107 i = I2C_TIMEOUT * 10000;
C Nauman383c43e2010-10-26 23:04:31 +0900108 while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900109 udelay(100);
wdenk49c3f672003-10-08 22:33:00 +0000110 i--;
111 }
wdenk1fe2c702003-03-06 21:55:29 +0000112
C Nauman383c43e2010-10-26 23:04:31 +0900113 return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
wdenk1fe2c702003-03-06 21:55:29 +0000114}
115
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000116static int IsACK(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +0000117{
C Nauman383c43e2010-10-26 23:04:31 +0900118 return !(readl(&i2c->iicstat) & I2CSTAT_NACK);
wdenk1fe2c702003-03-06 21:55:29 +0000119}
120
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000121static void ReadWriteByte(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +0000122{
C Nauman383c43e2010-10-26 23:04:31 +0900123 writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
wdenk1fe2c702003-03-06 21:55:29 +0000124}
125
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000126static struct s3c24x0_i2c *get_base_i2c(void)
127{
Piotr Wilczekb35cd1c2012-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 Shinde4b4480a2012-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 Wilczekb35cd1c2012-11-20 02:19:05 +0000146#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shinde4b4480a2012-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 Shindec07c11d2012-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.uk1464f4d2009-10-10 13:33:11 +0900197void i2c_init(int speed, int slaveadd)
wdenk1fe2c702003-03-06 21:55:29 +0000198{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000199 struct s3c24x0_i2c *i2c;
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000200#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900201 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000202#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900203 int i;
wdenk1fe2c702003-03-06 21:55:29 +0000204
Rajeshwari Shinde4b4480a2012-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();
wdenk1fe2c702003-03-06 21:55:29 +0000208
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000209 /* wait for some time to give previous transfer a chance to finish */
wdenk49c3f672003-10-08 22:33:00 +0000210 i = I2C_TIMEOUT * 1000;
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000211 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900212 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000213 i--;
214 }
wdenk1fe2c702003-03-06 21:55:29 +0000215
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000216#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
C Nauman383c43e2010-10-26 23:04:31 +0900217 if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
wdenkca9bc762003-07-15 07:45:49 +0000218#ifdef CONFIG_S3C2410
C Nauman383c43e2010-10-26 23:04:31 +0900219 ulong old_gpecon = readl(&gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000220#endif
221#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900222 ulong old_gpecon = readl(&gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000223#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900224 /* bus still busy probably by (most) previously interrupted
225 transfer */
wdenk1fe2c702003-03-06 21:55:29 +0000226
wdenkca9bc762003-07-15 07:45:49 +0000227#ifdef CONFIG_S3C2410
wdenk49c3f672003-10-08 22:33:00 +0000228 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
C Nauman383c43e2010-10-26 23:04:31 +0900229 writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
230 &gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000231#endif
232#ifdef CONFIG_S3C2400
wdenk49c3f672003-10-08 22:33:00 +0000233 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
C Nauman383c43e2010-10-26 23:04:31 +0900234 writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
235 &gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000236#endif
wdenk1fe2c702003-03-06 21:55:29 +0000237
wdenk49c3f672003-10-08 22:33:00 +0000238 /* toggle I2CSCL until bus idle */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900239 SetI2CSCL(0);
240 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000241 i = 10;
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900242 while ((i > 0) && (GetI2CSDA() != 1)) {
243 SetI2CSCL(1);
244 udelay(1000);
245 SetI2CSCL(0);
246 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000247 i--;
248 }
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900249 SetI2CSCL(1);
250 udelay(1000);
wdenk1fe2c702003-03-06 21:55:29 +0000251
wdenk49c3f672003-10-08 22:33:00 +0000252 /* restore pin functions */
wdenkca9bc762003-07-15 07:45:49 +0000253#ifdef CONFIG_S3C2410
C Nauman383c43e2010-10-26 23:04:31 +0900254 writel(old_gpecon, &gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000255#endif
256#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900257 writel(old_gpecon, &gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000258#endif
wdenk49c3f672003-10-08 22:33:00 +0000259 }
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000260#endif /* #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000261 i2c_ch_init(i2c, speed, slaveadd);
wdenk1fe2c702003-03-06 21:55:29 +0000262}
263
264/*
wdenk49c3f672003-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 Shinde4b4480a2012-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)
wdenk1fe2c702003-03-06 21:55:29 +0000278{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900279 int i, result;
wdenk1fe2c702003-03-06 21:55:29 +0000280
wdenk49c3f672003-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 Shinde4b4480a2012-07-23 21:23:53 +0000283 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000284 return I2C_NOK;
285 }
wdenk1fe2c702003-03-06 21:55:29 +0000286
wdenk49c3f672003-10-08 22:33:00 +0000287 /* Check I2C bus idle */
288 i = I2C_TIMEOUT * 1000;
C Nauman383c43e2010-10-26 23:04:31 +0900289 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900290 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000291 i--;
292 }
wdenk1fe2c702003-03-06 21:55:29 +0000293
C Nauman383c43e2010-10-26 23:04:31 +0900294 if (readl(&i2c->iicstat) & I2CSTAT_BSY)
wdenk49c3f672003-10-08 22:33:00 +0000295 return I2C_NOK_TOUT;
wdenk1fe2c702003-03-06 21:55:29 +0000296
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000297 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
wdenk49c3f672003-10-08 22:33:00 +0000298 result = I2C_OK;
wdenk1fe2c702003-03-06 21:55:29 +0000299
wdenk49c3f672003-10-08 22:33:00 +0000300 switch (cmd_type) {
wdenk7539dea2003-06-19 23:01:32 +0000301 case I2C_WRITE:
wdenk49c3f672003-10-08 22:33:00 +0000302 if (addr && addr_len) {
C Nauman383c43e2010-10-26 23:04:31 +0900303 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000304 /* send START */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900305 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Nauman383c43e2010-10-26 23:04:31 +0900306 &i2c->iicstat);
wdenk49c3f672003-10-08 22:33:00 +0000307 i = 0;
308 while ((i < addr_len) && (result == I2C_OK)) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000309 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900310 writel(addr[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000311 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000312 i++;
313 }
314 i = 0;
315 while ((i < data_len) && (result == I2C_OK)) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000316 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900317 writel(data[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000318 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000319 i++;
320 }
321 } else {
C Nauman383c43e2010-10-26 23:04:31 +0900322 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000323 /* send START */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900324 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Nauman383c43e2010-10-26 23:04:31 +0900325 &i2c->iicstat);
wdenk49c3f672003-10-08 22:33:00 +0000326 i = 0;
327 while ((i < data_len) && (result = I2C_OK)) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000328 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900329 writel(data[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000330 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000331 i++;
332 }
wdenk1fe2c702003-03-06 21:55:29 +0000333 }
wdenk1fe2c702003-03-06 21:55:29 +0000334
wdenk49c3f672003-10-08 22:33:00 +0000335 if (result == I2C_OK)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000336 result = WaitForXfer(i2c);
wdenk1fe2c702003-03-06 21:55:29 +0000337
wdenk49c3f672003-10-08 22:33:00 +0000338 /* send STOP */
C Nauman383c43e2010-10-26 23:04:31 +0900339 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000340 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000341 break;
wdenk1fe2c702003-03-06 21:55:29 +0000342
wdenk7539dea2003-06-19 23:01:32 +0000343 case I2C_READ:
wdenk49c3f672003-10-08 22:33:00 +0000344 if (addr && addr_len) {
C Nauman383c43e2010-10-26 23:04:31 +0900345 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
346 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000347 /* send START */
C Nauman383c43e2010-10-26 23:04:31 +0900348 writel(readl(&i2c->iicstat) | I2C_START_STOP,
349 &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000350 result = WaitForXfer(i2c);
351 if (IsACK(i2c)) {
wdenk49c3f672003-10-08 22:33:00 +0000352 i = 0;
353 while ((i < addr_len) && (result == I2C_OK)) {
C Nauman383c43e2010-10-26 23:04:31 +0900354 writel(addr[i], &i2c->iicds);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000355 ReadWriteByte(i2c);
356 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000357 i++;
358 }
wdenk1fe2c702003-03-06 21:55:29 +0000359
C Nauman383c43e2010-10-26 23:04:31 +0900360 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000361 /* resend START */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900362 writel(I2C_MODE_MR | I2C_TXRX_ENA |
C Nauman383c43e2010-10-26 23:04:31 +0900363 I2C_START_STOP, &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000364 ReadWriteByte(i2c);
365 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000366 i = 0;
367 while ((i < data_len) && (result == I2C_OK)) {
368 /* disable ACK for final READ */
369 if (i == data_len - 1)
C Nauman383c43e2010-10-26 23:04:31 +0900370 writel(readl(&i2c->iiccon)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000371 & ~I2CCON_ACKGEN,
372 &i2c->iiccon);
373 ReadWriteByte(i2c);
374 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900375 data[i] = readl(&i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000376 i++;
377 }
378 } else {
379 result = I2C_NACK;
380 }
wdenk1fe2c702003-03-06 21:55:29 +0000381
wdenk1fe2c702003-03-06 21:55:29 +0000382 } else {
C Nauman383c43e2010-10-26 23:04:31 +0900383 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
384 writel(chip, &i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000385 /* send START */
C Nauman383c43e2010-10-26 23:04:31 +0900386 writel(readl(&i2c->iicstat) | I2C_START_STOP,
387 &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000388 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000389
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000390 if (IsACK(i2c)) {
wdenk49c3f672003-10-08 22:33:00 +0000391 i = 0;
392 while ((i < data_len) && (result == I2C_OK)) {
393 /* disable ACK for final READ */
394 if (i == data_len - 1)
C Nauman383c43e2010-10-26 23:04:31 +0900395 writel(readl(&i2c->iiccon) &
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000396 ~I2CCON_ACKGEN,
397 &i2c->iiccon);
398 ReadWriteByte(i2c);
399 result = WaitForXfer(i2c);
C Nauman383c43e2010-10-26 23:04:31 +0900400 data[i] = readl(&i2c->iicds);
wdenk49c3f672003-10-08 22:33:00 +0000401 i++;
402 }
403 } else {
404 result = I2C_NACK;
405 }
wdenk1fe2c702003-03-06 21:55:29 +0000406 }
wdenk1fe2c702003-03-06 21:55:29 +0000407
wdenk49c3f672003-10-08 22:33:00 +0000408 /* send STOP */
C Nauman383c43e2010-10-26 23:04:31 +0900409 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000410 ReadWriteByte(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000411 break;
wdenk1fe2c702003-03-06 21:55:29 +0000412
413 default:
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000414 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000415 result = I2C_NOK;
416 break;
417 }
wdenk1fe2c702003-03-06 21:55:29 +0000418
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000419 return result;
wdenk1fe2c702003-03-06 21:55:29 +0000420}
421
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900422int i2c_probe(uchar chip)
wdenk1fe2c702003-03-06 21:55:29 +0000423{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000424 struct s3c24x0_i2c *i2c;
wdenk49c3f672003-10-08 22:33:00 +0000425 uchar buf[1];
wdenk1fe2c702003-03-06 21:55:29 +0000426
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000427 i2c = get_base_i2c();
wdenk49c3f672003-10-08 22:33:00 +0000428 buf[0] = 0;
wdenk1fe2c702003-03-06 21:55:29 +0000429
wdenk49c3f672003-10-08 22:33:00 +0000430 /*
431 * What is needed is to send the chip address and verify that the
432 * address was <ACK>ed (i.e. there was a chip at that address which
433 * drove the data line low).
434 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000435 return i2c_transfer(i2c, I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
wdenk1fe2c702003-03-06 21:55:29 +0000436}
437
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900438int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1fe2c702003-03-06 21:55:29 +0000439{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000440 struct s3c24x0_i2c *i2c;
wdenk49c3f672003-10-08 22:33:00 +0000441 uchar xaddr[4];
442 int ret;
wdenk1fe2c702003-03-06 21:55:29 +0000443
wdenk49c3f672003-10-08 22:33:00 +0000444 if (alen > 4) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000445 debug("I2C read: addr len %d not supported\n", alen);
wdenk49c3f672003-10-08 22:33:00 +0000446 return 1;
447 }
wdenk1fe2c702003-03-06 21:55:29 +0000448
wdenk49c3f672003-10-08 22:33:00 +0000449 if (alen > 0) {
450 xaddr[0] = (addr >> 24) & 0xFF;
451 xaddr[1] = (addr >> 16) & 0xFF;
452 xaddr[2] = (addr >> 8) & 0xFF;
453 xaddr[3] = addr & 0xFF;
454 }
wdenk1fe2c702003-03-06 21:55:29 +0000455
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200456#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenk49c3f672003-10-08 22:33:00 +0000457 /*
458 * EEPROM chips that implement "address overflow" are ones
459 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
460 * address and the extra bits end up in the "chip address"
461 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
462 * four 256 byte chips.
463 *
464 * Note that we consider the length of the address field to
465 * still be one byte because the extra address bits are
466 * hidden in the chip address.
467 */
468 if (alen > 0)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900469 chip |= ((addr >> (alen * 8)) &
470 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1fe2c702003-03-06 21:55:29 +0000471#endif
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000472 i2c = get_base_i2c();
473 ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen,
474 buffer, len);
475 if (ret != 0) {
476 debug("I2c read: failed %d\n", ret);
wdenk49c3f672003-10-08 22:33:00 +0000477 return 1;
478 }
479 return 0;
wdenk1fe2c702003-03-06 21:55:29 +0000480}
481
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900482int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1fe2c702003-03-06 21:55:29 +0000483{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000484 struct s3c24x0_i2c *i2c;
wdenk49c3f672003-10-08 22:33:00 +0000485 uchar xaddr[4];
wdenk1fe2c702003-03-06 21:55:29 +0000486
wdenk49c3f672003-10-08 22:33:00 +0000487 if (alen > 4) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000488 debug("I2C write: addr len %d not supported\n", alen);
wdenk49c3f672003-10-08 22:33:00 +0000489 return 1;
490 }
wdenk1fe2c702003-03-06 21:55:29 +0000491
wdenk49c3f672003-10-08 22:33:00 +0000492 if (alen > 0) {
493 xaddr[0] = (addr >> 24) & 0xFF;
494 xaddr[1] = (addr >> 16) & 0xFF;
495 xaddr[2] = (addr >> 8) & 0xFF;
496 xaddr[3] = addr & 0xFF;
497 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200498#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenk49c3f672003-10-08 22:33:00 +0000499 /*
500 * EEPROM chips that implement "address overflow" are ones
501 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
502 * address and the extra bits end up in the "chip address"
503 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
504 * four 256 byte chips.
505 *
506 * Note that we consider the length of the address field to
507 * still be one byte because the extra address bits are
508 * hidden in the chip address.
509 */
510 if (alen > 0)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900511 chip |= ((addr >> (alen * 8)) &
512 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1fe2c702003-03-06 21:55:29 +0000513#endif
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000514 i2c = get_base_i2c();
wdenk49c3f672003-10-08 22:33:00 +0000515 return (i2c_transfer
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000516 (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
wdenk49c3f672003-10-08 22:33:00 +0000517 len) != 0);
wdenk1fe2c702003-03-06 21:55:29 +0000518}
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +0000519
520#ifdef CONFIG_OF_CONTROL
521void board_i2c_init(const void *blob)
522{
523 int node_list[CONFIG_MAX_I2C_NUM];
524 int count, i;
525
526 count = fdtdec_find_aliases_for_id(blob, "i2c",
527 COMPAT_SAMSUNG_S3C2440_I2C, node_list,
528 CONFIG_MAX_I2C_NUM);
529
530 for (i = 0; i < count; i++) {
531 struct s3c24x0_i2c_bus *bus;
532 int node = node_list[i];
533
534 if (node <= 0)
535 continue;
536 bus = &i2c_bus[i];
537 bus->regs = (struct s3c24x0_i2c *)
538 fdtdec_get_addr(blob, node, "reg");
539 bus->id = pinmux_decode_periph_id(blob, node);
540 bus->node = node;
541 bus->bus_num = i2c_busses++;
542 exynos_pinmux_config(bus->id, 0);
543 }
544}
545
546static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx)
547{
548 if (bus_idx < i2c_busses)
549 return &i2c_bus[bus_idx];
550
551 debug("Undefined bus: %d\n", bus_idx);
552 return NULL;
553}
554
555int i2c_get_bus_num_fdt(int node)
556{
557 int i;
558
559 for (i = 0; i < i2c_busses; i++) {
560 if (node == i2c_bus[i].node)
561 return i;
562 }
563
564 debug("%s: Can't find any matched I2C bus\n", __func__);
565 return -1;
566}
567
568int i2c_reset_port_fdt(const void *blob, int node)
569{
570 struct s3c24x0_i2c_bus *i2c;
571 int bus;
572
573 bus = i2c_get_bus_num_fdt(node);
574 if (bus < 0) {
575 debug("could not get bus for node %d\n", node);
576 return -1;
577 }
578
579 i2c = get_bus(bus);
580 if (!i2c) {
581 debug("get_bus() failed for node node %d\n", node);
582 return -1;
583 }
584
585 i2c_ch_init(i2c->regs, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
586
587 return 0;
588}
589#endif
590
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900591#endif /* CONFIG_HARD_I2C */