blob: 26c075513d72c221627dd87a2184ff60499922b3 [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 Shindeab7e52b2012-07-23 21:23:53 +000030#ifdef CONFIG_EXYNOS5
31#include <asm/arch/clk.h>
32#include <asm/arch/cpu.h>
33#else
kevin.morfitt@fearnside-systems.co.ukac678042009-11-17 18:30:34 +090034#include <asm/arch/s3c24x0_cpu.h>
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000035#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090036#include <asm/io.h>
wdenk1cb8e982003-03-06 21:55:29 +000037#include <i2c.h>
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000038#include "s3c24x0_i2c.h"
wdenk1cb8e982003-03-06 21:55:29 +000039
40#ifdef CONFIG_HARD_I2C
41
wdenk48b42612003-06-19 23:01:32 +000042#define I2C_WRITE 0
43#define I2C_READ 1
wdenk1cb8e982003-03-06 21:55:29 +000044
wdenk48b42612003-06-19 23:01:32 +000045#define I2C_OK 0
46#define I2C_NOK 1
47#define I2C_NACK 2
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090048#define I2C_NOK_LA 3 /* Lost arbitration */
49#define I2C_NOK_TOUT 4 /* time out */
wdenk1cb8e982003-03-06 21:55:29 +000050
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090051#define I2CSTAT_BSY 0x20 /* Busy bit */
52#define I2CSTAT_NACK 0x01 /* Nack bit */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000053#define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090054#define I2CCON_IRPND 0x10 /* Interrupt pending bit */
55#define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
56#define I2C_MODE_MR 0x80 /* Master Receive Mode */
57#define I2C_START_STOP 0x20 /* START / STOP */
58#define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
wdenk1cb8e982003-03-06 21:55:29 +000059
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090060#define I2C_TIMEOUT 1 /* 1 second */
wdenk1cb8e982003-03-06 21:55:29 +000061
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000062
63static unsigned int g_current_bus; /* Stores Current I2C Bus */
64
65#ifndef CONFIG_EXYNOS5
wdenk48b42612003-06-19 23:01:32 +000066static int GetI2CSDA(void)
wdenk1cb8e982003-03-06 21:55:29 +000067{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090068 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk48b42612003-06-19 23:01:32 +000069
wdenk6dff5522003-07-15 07:45:49 +000070#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +090071 return (readl(&gpio->gpedat) & 0x8000) >> 15;
wdenk6dff5522003-07-15 07:45:49 +000072#endif
73#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +090074 return (readl(&gpio->pgdat) & 0x0020) >> 5;
wdenk6dff5522003-07-15 07:45:49 +000075#endif
wdenk1cb8e982003-03-06 21:55:29 +000076}
77
wdenk06d01db2003-03-14 20:47:52 +000078#if 0
wdenk48b42612003-06-19 23:01:32 +000079static void SetI2CSDA(int x)
wdenk1cb8e982003-03-06 21:55:29 +000080{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090081 rGPEDAT = (rGPEDAT & ~0x8000) | (x & 1) << 15;
wdenk1cb8e982003-03-06 21:55:29 +000082}
wdenk06d01db2003-03-14 20:47:52 +000083#endif
wdenk1cb8e982003-03-06 21:55:29 +000084
wdenk48b42612003-06-19 23:01:32 +000085static void SetI2CSCL(int x)
wdenk1cb8e982003-03-06 21:55:29 +000086{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090087 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk48b42612003-06-19 23:01:32 +000088
wdenk6dff5522003-07-15 07:45:49 +000089#ifdef CONFIG_S3C2410
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000090 writel((readl(&gpio->gpedat) & ~0x4000) |
91 (x & 1) << 14, &gpio->gpedat);
wdenk6dff5522003-07-15 07:45:49 +000092#endif
93#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +090094 writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
wdenk6dff5522003-07-15 07:45:49 +000095#endif
wdenk1cb8e982003-03-06 21:55:29 +000096}
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000097#endif
wdenk1cb8e982003-03-06 21:55:29 +000098
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000099static int WaitForXfer(struct s3c24x0_i2c *i2c)
wdenk1cb8e982003-03-06 21:55:29 +0000100{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900101 int i;
wdenk1cb8e982003-03-06 21:55:29 +0000102
wdenkfc3e2162003-10-08 22:33:00 +0000103 i = I2C_TIMEOUT * 10000;
C Naumand9abba82010-10-26 23:04:31 +0900104 while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900105 udelay(100);
wdenkfc3e2162003-10-08 22:33:00 +0000106 i--;
107 }
wdenk1cb8e982003-03-06 21:55:29 +0000108
C Naumand9abba82010-10-26 23:04:31 +0900109 return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
wdenk1cb8e982003-03-06 21:55:29 +0000110}
111
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000112static int IsACK(struct s3c24x0_i2c *i2c)
wdenk1cb8e982003-03-06 21:55:29 +0000113{
C Naumand9abba82010-10-26 23:04:31 +0900114 return !(readl(&i2c->iicstat) & I2CSTAT_NACK);
wdenk1cb8e982003-03-06 21:55:29 +0000115}
116
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000117static void ReadWriteByte(struct s3c24x0_i2c *i2c)
wdenk1cb8e982003-03-06 21:55:29 +0000118{
C Naumand9abba82010-10-26 23:04:31 +0900119 writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
wdenk1cb8e982003-03-06 21:55:29 +0000120}
121
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000122static struct s3c24x0_i2c *get_base_i2c(void)
123{
124#ifdef CONFIG_EXYNOS5
125 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
126 + (EXYNOS5_I2C_SPACING
127 * g_current_bus));
128 return i2c;
129#else
130 return s3c24x0_get_base_i2c();
131#endif
132}
133
134static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
135{
136 ulong freq, pres = 16, div;
137#ifdef CONFIG_EXYNOS5
138 freq = get_i2c_clk();
139#else
140 freq = get_PCLK();
141#endif
142 /* calculate prescaler and divisor values */
143 if ((freq / pres / (16 + 1)) > speed)
144 /* set prescaler to 512 */
145 pres = 512;
146
147 div = 0;
148 while ((freq / pres / (div + 1)) > speed)
149 div++;
150
151 /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
152 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
153
154 /* init to SLAVE REVEIVE and set slaveaddr */
155 writel(0, &i2c->iicstat);
156 writel(slaveadd, &i2c->iicadd);
157 /* program Master Transmit (and implicit STOP) */
158 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
159}
160
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900161void i2c_init(int speed, int slaveadd)
wdenk1cb8e982003-03-06 21:55:29 +0000162{
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000163 struct s3c24x0_i2c *i2c;
164#ifndef CONFIG_EXYNOS5
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900165 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000166#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900167 int i;
wdenk1cb8e982003-03-06 21:55:29 +0000168
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000169 /* By default i2c channel 0 is the current bus */
170 g_current_bus = 0;
171 i2c = get_base_i2c();
wdenk1cb8e982003-03-06 21:55:29 +0000172
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000173 /* wait for some time to give previous transfer a chance to finish */
wdenkfc3e2162003-10-08 22:33:00 +0000174 i = I2C_TIMEOUT * 1000;
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000175 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900176 udelay(1000);
wdenk1cb8e982003-03-06 21:55:29 +0000177 i--;
178 }
wdenk1cb8e982003-03-06 21:55:29 +0000179
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000180#ifndef CONFIG_EXYNOS5
C Naumand9abba82010-10-26 23:04:31 +0900181 if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
wdenk6dff5522003-07-15 07:45:49 +0000182#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +0900183 ulong old_gpecon = readl(&gpio->gpecon);
wdenk6dff5522003-07-15 07:45:49 +0000184#endif
185#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +0900186 ulong old_gpecon = readl(&gpio->pgcon);
wdenk6dff5522003-07-15 07:45:49 +0000187#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900188 /* bus still busy probably by (most) previously interrupted
189 transfer */
wdenk1cb8e982003-03-06 21:55:29 +0000190
wdenkfc3e2162003-10-08 22:33:00 +0000191#ifdef CONFIG_S3C2410
192 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
C Naumand9abba82010-10-26 23:04:31 +0900193 writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
194 &gpio->gpecon);
wdenkfc3e2162003-10-08 22:33:00 +0000195#endif
196#ifdef CONFIG_S3C2400
197 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
C Naumand9abba82010-10-26 23:04:31 +0900198 writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
199 &gpio->pgcon);
wdenkfc3e2162003-10-08 22:33:00 +0000200#endif
wdenk1cb8e982003-03-06 21:55:29 +0000201
wdenkfc3e2162003-10-08 22:33:00 +0000202 /* toggle I2CSCL until bus idle */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900203 SetI2CSCL(0);
204 udelay(1000);
wdenkfc3e2162003-10-08 22:33:00 +0000205 i = 10;
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900206 while ((i > 0) && (GetI2CSDA() != 1)) {
207 SetI2CSCL(1);
208 udelay(1000);
209 SetI2CSCL(0);
210 udelay(1000);
wdenkfc3e2162003-10-08 22:33:00 +0000211 i--;
212 }
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900213 SetI2CSCL(1);
214 udelay(1000);
wdenk1cb8e982003-03-06 21:55:29 +0000215
wdenkfc3e2162003-10-08 22:33:00 +0000216 /* restore pin functions */
217#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +0900218 writel(old_gpecon, &gpio->gpecon);
wdenkfc3e2162003-10-08 22:33:00 +0000219#endif
220#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +0900221 writel(old_gpecon, &gpio->pgcon);
wdenkfc3e2162003-10-08 22:33:00 +0000222#endif
223 }
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000224#endif /* #ifndef CONFIG_EXYNOS5 */
225 i2c_ch_init(i2c, speed, slaveadd);
wdenk1cb8e982003-03-06 21:55:29 +0000226}
227
228/*
wdenkfc3e2162003-10-08 22:33:00 +0000229 * cmd_type is 0 for write, 1 for read.
230 *
231 * addr_len can take any value from 0-255, it is only limited
232 * by the char, we could make it larger if needed. If it is
233 * 0 we skip the address write cycle.
234 */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000235static int i2c_transfer(struct s3c24x0_i2c *i2c,
236 unsigned char cmd_type,
237 unsigned char chip,
238 unsigned char addr[],
239 unsigned char addr_len,
240 unsigned char data[],
241 unsigned short data_len)
wdenk1cb8e982003-03-06 21:55:29 +0000242{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900243 int i, result;
wdenk1cb8e982003-03-06 21:55:29 +0000244
wdenkfc3e2162003-10-08 22:33:00 +0000245 if (data == 0 || data_len == 0) {
246 /*Don't support data transfer of no length or to address 0 */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000247 debug("i2c_transfer: bad call\n");
wdenkfc3e2162003-10-08 22:33:00 +0000248 return I2C_NOK;
249 }
wdenk1cb8e982003-03-06 21:55:29 +0000250
wdenkfc3e2162003-10-08 22:33:00 +0000251 /* Check I2C bus idle */
252 i = I2C_TIMEOUT * 1000;
C Naumand9abba82010-10-26 23:04:31 +0900253 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900254 udelay(1000);
wdenkfc3e2162003-10-08 22:33:00 +0000255 i--;
256 }
wdenk1cb8e982003-03-06 21:55:29 +0000257
C Naumand9abba82010-10-26 23:04:31 +0900258 if (readl(&i2c->iicstat) & I2CSTAT_BSY)
wdenkfc3e2162003-10-08 22:33:00 +0000259 return I2C_NOK_TOUT;
wdenk1cb8e982003-03-06 21:55:29 +0000260
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000261 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
wdenkfc3e2162003-10-08 22:33:00 +0000262 result = I2C_OK;
wdenk1cb8e982003-03-06 21:55:29 +0000263
wdenkfc3e2162003-10-08 22:33:00 +0000264 switch (cmd_type) {
wdenk48b42612003-06-19 23:01:32 +0000265 case I2C_WRITE:
wdenkfc3e2162003-10-08 22:33:00 +0000266 if (addr && addr_len) {
C Naumand9abba82010-10-26 23:04:31 +0900267 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000268 /* send START */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900269 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Naumand9abba82010-10-26 23:04:31 +0900270 &i2c->iicstat);
wdenkfc3e2162003-10-08 22:33:00 +0000271 i = 0;
272 while ((i < addr_len) && (result == I2C_OK)) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000273 result = WaitForXfer(i2c);
C Naumand9abba82010-10-26 23:04:31 +0900274 writel(addr[i], &i2c->iicds);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000275 ReadWriteByte(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000276 i++;
277 }
278 i = 0;
279 while ((i < data_len) && (result == I2C_OK)) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000280 result = WaitForXfer(i2c);
C Naumand9abba82010-10-26 23:04:31 +0900281 writel(data[i], &i2c->iicds);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000282 ReadWriteByte(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000283 i++;
284 }
285 } else {
C Naumand9abba82010-10-26 23:04:31 +0900286 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000287 /* send START */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900288 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Naumand9abba82010-10-26 23:04:31 +0900289 &i2c->iicstat);
wdenkfc3e2162003-10-08 22:33:00 +0000290 i = 0;
291 while ((i < data_len) && (result = I2C_OK)) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000292 result = WaitForXfer(i2c);
C Naumand9abba82010-10-26 23:04:31 +0900293 writel(data[i], &i2c->iicds);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000294 ReadWriteByte(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000295 i++;
296 }
wdenk1cb8e982003-03-06 21:55:29 +0000297 }
wdenk1cb8e982003-03-06 21:55:29 +0000298
wdenkfc3e2162003-10-08 22:33:00 +0000299 if (result == I2C_OK)
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000300 result = WaitForXfer(i2c);
wdenk1cb8e982003-03-06 21:55:29 +0000301
wdenkfc3e2162003-10-08 22:33:00 +0000302 /* send STOP */
C Naumand9abba82010-10-26 23:04:31 +0900303 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000304 ReadWriteByte(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000305 break;
wdenk1cb8e982003-03-06 21:55:29 +0000306
wdenk48b42612003-06-19 23:01:32 +0000307 case I2C_READ:
wdenkfc3e2162003-10-08 22:33:00 +0000308 if (addr && addr_len) {
C Naumand9abba82010-10-26 23:04:31 +0900309 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
310 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000311 /* send START */
C Naumand9abba82010-10-26 23:04:31 +0900312 writel(readl(&i2c->iicstat) | I2C_START_STOP,
313 &i2c->iicstat);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000314 result = WaitForXfer(i2c);
315 if (IsACK(i2c)) {
wdenkfc3e2162003-10-08 22:33:00 +0000316 i = 0;
317 while ((i < addr_len) && (result == I2C_OK)) {
C Naumand9abba82010-10-26 23:04:31 +0900318 writel(addr[i], &i2c->iicds);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000319 ReadWriteByte(i2c);
320 result = WaitForXfer(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000321 i++;
322 }
wdenk1cb8e982003-03-06 21:55:29 +0000323
C Naumand9abba82010-10-26 23:04:31 +0900324 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000325 /* resend START */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900326 writel(I2C_MODE_MR | I2C_TXRX_ENA |
C Naumand9abba82010-10-26 23:04:31 +0900327 I2C_START_STOP, &i2c->iicstat);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000328 ReadWriteByte(i2c);
329 result = WaitForXfer(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000330 i = 0;
331 while ((i < data_len) && (result == I2C_OK)) {
332 /* disable ACK for final READ */
333 if (i == data_len - 1)
C Naumand9abba82010-10-26 23:04:31 +0900334 writel(readl(&i2c->iiccon)
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000335 & ~I2CCON_ACKGEN,
336 &i2c->iiccon);
337 ReadWriteByte(i2c);
338 result = WaitForXfer(i2c);
C Naumand9abba82010-10-26 23:04:31 +0900339 data[i] = readl(&i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000340 i++;
341 }
342 } else {
343 result = I2C_NACK;
344 }
345
wdenk1cb8e982003-03-06 21:55:29 +0000346 } else {
C Naumand9abba82010-10-26 23:04:31 +0900347 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
348 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000349 /* send START */
C Naumand9abba82010-10-26 23:04:31 +0900350 writel(readl(&i2c->iicstat) | I2C_START_STOP,
351 &i2c->iicstat);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000352 result = WaitForXfer(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000353
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000354 if (IsACK(i2c)) {
wdenkfc3e2162003-10-08 22:33:00 +0000355 i = 0;
356 while ((i < data_len) && (result == I2C_OK)) {
357 /* disable ACK for final READ */
358 if (i == data_len - 1)
C Naumand9abba82010-10-26 23:04:31 +0900359 writel(readl(&i2c->iiccon) &
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000360 ~I2CCON_ACKGEN,
361 &i2c->iiccon);
362 ReadWriteByte(i2c);
363 result = WaitForXfer(i2c);
C Naumand9abba82010-10-26 23:04:31 +0900364 data[i] = readl(&i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000365 i++;
366 }
367 } else {
368 result = I2C_NACK;
369 }
wdenk1cb8e982003-03-06 21:55:29 +0000370 }
371
wdenkfc3e2162003-10-08 22:33:00 +0000372 /* send STOP */
C Naumand9abba82010-10-26 23:04:31 +0900373 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000374 ReadWriteByte(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000375 break;
wdenk1cb8e982003-03-06 21:55:29 +0000376
377 default:
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000378 debug("i2c_transfer: bad call\n");
wdenkfc3e2162003-10-08 22:33:00 +0000379 result = I2C_NOK;
380 break;
381 }
wdenk1cb8e982003-03-06 21:55:29 +0000382
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000383 return result;
wdenk1cb8e982003-03-06 21:55:29 +0000384}
385
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900386int i2c_probe(uchar chip)
wdenk1cb8e982003-03-06 21:55:29 +0000387{
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000388 struct s3c24x0_i2c *i2c;
wdenkfc3e2162003-10-08 22:33:00 +0000389 uchar buf[1];
wdenk1cb8e982003-03-06 21:55:29 +0000390
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000391 i2c = get_base_i2c();
wdenkfc3e2162003-10-08 22:33:00 +0000392 buf[0] = 0;
wdenk1cb8e982003-03-06 21:55:29 +0000393
wdenkfc3e2162003-10-08 22:33:00 +0000394 /*
395 * What is needed is to send the chip address and verify that the
396 * address was <ACK>ed (i.e. there was a chip at that address which
397 * drove the data line low).
398 */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000399 return i2c_transfer(i2c, I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
wdenk1cb8e982003-03-06 21:55:29 +0000400}
401
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900402int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1cb8e982003-03-06 21:55:29 +0000403{
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000404 struct s3c24x0_i2c *i2c;
wdenkfc3e2162003-10-08 22:33:00 +0000405 uchar xaddr[4];
406 int ret;
wdenk1cb8e982003-03-06 21:55:29 +0000407
wdenkfc3e2162003-10-08 22:33:00 +0000408 if (alen > 4) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000409 debug("I2C read: addr len %d not supported\n", alen);
wdenkfc3e2162003-10-08 22:33:00 +0000410 return 1;
411 }
wdenk1cb8e982003-03-06 21:55:29 +0000412
wdenkfc3e2162003-10-08 22:33:00 +0000413 if (alen > 0) {
414 xaddr[0] = (addr >> 24) & 0xFF;
415 xaddr[1] = (addr >> 16) & 0xFF;
416 xaddr[2] = (addr >> 8) & 0xFF;
417 xaddr[3] = addr & 0xFF;
418 }
wdenk1cb8e982003-03-06 21:55:29 +0000419
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200420#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenkfc3e2162003-10-08 22:33:00 +0000421 /*
422 * EEPROM chips that implement "address overflow" are ones
423 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
424 * address and the extra bits end up in the "chip address"
425 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
426 * four 256 byte chips.
427 *
428 * Note that we consider the length of the address field to
429 * still be one byte because the extra address bits are
430 * hidden in the chip address.
431 */
432 if (alen > 0)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900433 chip |= ((addr >> (alen * 8)) &
434 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1cb8e982003-03-06 21:55:29 +0000435#endif
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000436 i2c = get_base_i2c();
437 ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen,
438 buffer, len);
439 if (ret != 0) {
440 debug("I2c read: failed %d\n", ret);
wdenkfc3e2162003-10-08 22:33:00 +0000441 return 1;
442 }
443 return 0;
wdenk1cb8e982003-03-06 21:55:29 +0000444}
445
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900446int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1cb8e982003-03-06 21:55:29 +0000447{
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000448 struct s3c24x0_i2c *i2c;
wdenkfc3e2162003-10-08 22:33:00 +0000449 uchar xaddr[4];
wdenk1cb8e982003-03-06 21:55:29 +0000450
wdenkfc3e2162003-10-08 22:33:00 +0000451 if (alen > 4) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000452 debug("I2C write: addr len %d not supported\n", alen);
wdenkfc3e2162003-10-08 22:33:00 +0000453 return 1;
454 }
wdenk1cb8e982003-03-06 21:55:29 +0000455
wdenkfc3e2162003-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 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200462#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenkfc3e2162003-10-08 22:33:00 +0000463 /*
464 * EEPROM chips that implement "address overflow" are ones
465 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
466 * address and the extra bits end up in the "chip address"
467 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
468 * four 256 byte chips.
469 *
470 * Note that we consider the length of the address field to
471 * still be one byte because the extra address bits are
472 * hidden in the chip address.
473 */
474 if (alen > 0)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900475 chip |= ((addr >> (alen * 8)) &
476 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1cb8e982003-03-06 21:55:29 +0000477#endif
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000478 i2c = get_base_i2c();
wdenkfc3e2162003-10-08 22:33:00 +0000479 return (i2c_transfer
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000480 (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
wdenkfc3e2162003-10-08 22:33:00 +0000481 len) != 0);
wdenk1cb8e982003-03-06 21:55:29 +0000482}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900483#endif /* CONFIG_HARD_I2C */