blob: ba6f39bee11c6f831ffcfa3b9c15e31a6a8748b8 [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>
kevin.morfitt@fearnside-systems.co.ukac678042009-11-17 18:30:34 +090030#include <asm/arch/s3c24x0_cpu.h>
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090031
32#include <asm/io.h>
wdenk1cb8e982003-03-06 21:55:29 +000033#include <i2c.h>
34
35#ifdef CONFIG_HARD_I2C
36
wdenk48b42612003-06-19 23:01:32 +000037#define I2C_WRITE 0
38#define I2C_READ 1
wdenk1cb8e982003-03-06 21:55:29 +000039
wdenk48b42612003-06-19 23:01:32 +000040#define I2C_OK 0
41#define I2C_NOK 1
42#define I2C_NACK 2
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090043#define I2C_NOK_LA 3 /* Lost arbitration */
44#define I2C_NOK_TOUT 4 /* time out */
wdenk1cb8e982003-03-06 21:55:29 +000045
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090046#define I2CSTAT_BSY 0x20 /* Busy bit */
47#define I2CSTAT_NACK 0x01 /* Nack bit */
48#define I2CCON_IRPND 0x10 /* Interrupt pending bit */
49#define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
50#define I2C_MODE_MR 0x80 /* Master Receive Mode */
51#define I2C_START_STOP 0x20 /* START / STOP */
52#define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
wdenk1cb8e982003-03-06 21:55:29 +000053
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090054#define I2C_TIMEOUT 1 /* 1 second */
wdenk1cb8e982003-03-06 21:55:29 +000055
wdenk48b42612003-06-19 23:01:32 +000056static int GetI2CSDA(void)
wdenk1cb8e982003-03-06 21:55:29 +000057{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090058 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk48b42612003-06-19 23:01:32 +000059
wdenk6dff5522003-07-15 07:45:49 +000060#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +090061 return (readl(&gpio->gpedat) & 0x8000) >> 15;
wdenk6dff5522003-07-15 07:45:49 +000062#endif
63#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +090064 return (readl(&gpio->pgdat) & 0x0020) >> 5;
wdenk6dff5522003-07-15 07:45:49 +000065#endif
wdenk1cb8e982003-03-06 21:55:29 +000066}
67
wdenk06d01db2003-03-14 20:47:52 +000068#if 0
wdenk48b42612003-06-19 23:01:32 +000069static void SetI2CSDA(int x)
wdenk1cb8e982003-03-06 21:55:29 +000070{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090071 rGPEDAT = (rGPEDAT & ~0x8000) | (x & 1) << 15;
wdenk1cb8e982003-03-06 21:55:29 +000072}
wdenk06d01db2003-03-14 20:47:52 +000073#endif
wdenk1cb8e982003-03-06 21:55:29 +000074
wdenk48b42612003-06-19 23:01:32 +000075static void SetI2CSCL(int x)
wdenk1cb8e982003-03-06 21:55:29 +000076{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090077 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk48b42612003-06-19 23:01:32 +000078
wdenk6dff5522003-07-15 07:45:49 +000079#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +090080 writel((readl(&gpio->gpedat) & ~0x4000) | (x & 1) << 14, &gpio->gpedat);
wdenk6dff5522003-07-15 07:45:49 +000081#endif
82#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +090083 writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
wdenk6dff5522003-07-15 07:45:49 +000084#endif
wdenk1cb8e982003-03-06 21:55:29 +000085}
86
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090087static int WaitForXfer(void)
wdenk1cb8e982003-03-06 21:55:29 +000088{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090089 struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
90 int i;
wdenk1cb8e982003-03-06 21:55:29 +000091
wdenkfc3e2162003-10-08 22:33:00 +000092 i = I2C_TIMEOUT * 10000;
C Naumand9abba82010-10-26 23:04:31 +090093 while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090094 udelay(100);
wdenkfc3e2162003-10-08 22:33:00 +000095 i--;
96 }
wdenk1cb8e982003-03-06 21:55:29 +000097
C Naumand9abba82010-10-26 23:04:31 +090098 return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
wdenk1cb8e982003-03-06 21:55:29 +000099}
100
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900101static int IsACK(void)
wdenk1cb8e982003-03-06 21:55:29 +0000102{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900103 struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
wdenk48b42612003-06-19 23:01:32 +0000104
C Naumand9abba82010-10-26 23:04:31 +0900105 return !(readl(&i2c->iicstat) & I2CSTAT_NACK);
wdenk1cb8e982003-03-06 21:55:29 +0000106}
107
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900108static void ReadWriteByte(void)
wdenk1cb8e982003-03-06 21:55:29 +0000109{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900110 struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
wdenk48b42612003-06-19 23:01:32 +0000111
C Naumand9abba82010-10-26 23:04:31 +0900112 writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
wdenk1cb8e982003-03-06 21:55:29 +0000113}
114
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900115void i2c_init(int speed, int slaveadd)
wdenk1cb8e982003-03-06 21:55:29 +0000116{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900117 struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
118 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenkfc3e2162003-10-08 22:33:00 +0000119 ulong freq, pres = 16, div;
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900120 int i;
wdenk1cb8e982003-03-06 21:55:29 +0000121
wdenkfc3e2162003-10-08 22:33:00 +0000122 /* wait for some time to give previous transfer a chance to finish */
wdenk1cb8e982003-03-06 21:55:29 +0000123
wdenkfc3e2162003-10-08 22:33:00 +0000124 i = I2C_TIMEOUT * 1000;
C Naumand9abba82010-10-26 23:04:31 +0900125 while ((readl(&i2c->iicstat) && I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900126 udelay(1000);
wdenk1cb8e982003-03-06 21:55:29 +0000127 i--;
128 }
wdenk1cb8e982003-03-06 21:55:29 +0000129
C Naumand9abba82010-10-26 23:04:31 +0900130 if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
wdenk6dff5522003-07-15 07:45:49 +0000131#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +0900132 ulong old_gpecon = readl(&gpio->gpecon);
wdenk6dff5522003-07-15 07:45:49 +0000133#endif
134#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +0900135 ulong old_gpecon = readl(&gpio->pgcon);
wdenk6dff5522003-07-15 07:45:49 +0000136#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900137 /* bus still busy probably by (most) previously interrupted
138 transfer */
wdenk1cb8e982003-03-06 21:55:29 +0000139
wdenkfc3e2162003-10-08 22:33:00 +0000140#ifdef CONFIG_S3C2410
141 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
C Naumand9abba82010-10-26 23:04:31 +0900142 writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
143 &gpio->gpecon);
wdenkfc3e2162003-10-08 22:33:00 +0000144#endif
145#ifdef CONFIG_S3C2400
146 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
C Naumand9abba82010-10-26 23:04:31 +0900147 writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
148 &gpio->pgcon);
wdenkfc3e2162003-10-08 22:33:00 +0000149#endif
wdenk1cb8e982003-03-06 21:55:29 +0000150
wdenkfc3e2162003-10-08 22:33:00 +0000151 /* toggle I2CSCL until bus idle */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900152 SetI2CSCL(0);
153 udelay(1000);
wdenkfc3e2162003-10-08 22:33:00 +0000154 i = 10;
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900155 while ((i > 0) && (GetI2CSDA() != 1)) {
156 SetI2CSCL(1);
157 udelay(1000);
158 SetI2CSCL(0);
159 udelay(1000);
wdenkfc3e2162003-10-08 22:33:00 +0000160 i--;
161 }
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900162 SetI2CSCL(1);
163 udelay(1000);
wdenk1cb8e982003-03-06 21:55:29 +0000164
wdenkfc3e2162003-10-08 22:33:00 +0000165 /* restore pin functions */
166#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +0900167 writel(old_gpecon, &gpio->gpecon);
wdenkfc3e2162003-10-08 22:33:00 +0000168#endif
169#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +0900170 writel(old_gpecon, &gpio->pgcon);
wdenkfc3e2162003-10-08 22:33:00 +0000171#endif
172 }
wdenk1cb8e982003-03-06 21:55:29 +0000173
wdenkfc3e2162003-10-08 22:33:00 +0000174 /* calculate prescaler and divisor values */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900175 freq = get_PCLK();
wdenkfc3e2162003-10-08 22:33:00 +0000176 if ((freq / pres / (16 + 1)) > speed)
177 /* set prescaler to 512 */
178 pres = 512;
179
180 div = 0;
181 while ((freq / pres / (div + 1)) > speed)
182 div++;
183
184 /* set prescaler, divisor according to freq, also set
185 * ACKGEN, IRQ */
C Naumand9abba82010-10-26 23:04:31 +0900186 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
wdenkfc3e2162003-10-08 22:33:00 +0000187
188 /* init to SLAVE REVEIVE and set slaveaddr */
C Naumand9abba82010-10-26 23:04:31 +0900189 writel(0, &i2c->iicstat);
190 writel(slaveadd, &i2c->iicadd);
wdenkfc3e2162003-10-08 22:33:00 +0000191 /* program Master Transmit (and implicit STOP) */
C Naumand9abba82010-10-26 23:04:31 +0900192 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
wdenk1cb8e982003-03-06 21:55:29 +0000193
194}
195
196/*
wdenkfc3e2162003-10-08 22:33:00 +0000197 * cmd_type is 0 for write, 1 for read.
198 *
199 * addr_len can take any value from 0-255, it is only limited
200 * by the char, we could make it larger if needed. If it is
201 * 0 we skip the address write cycle.
202 */
wdenk1cb8e982003-03-06 21:55:29 +0000203static
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900204int i2c_transfer(unsigned char cmd_type,
205 unsigned char chip,
206 unsigned char addr[],
207 unsigned char addr_len,
208 unsigned char data[], unsigned short data_len)
wdenk1cb8e982003-03-06 21:55:29 +0000209{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900210 struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
211 int i, result;
wdenk1cb8e982003-03-06 21:55:29 +0000212
wdenkfc3e2162003-10-08 22:33:00 +0000213 if (data == 0 || data_len == 0) {
214 /*Don't support data transfer of no length or to address 0 */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900215 printf("i2c_transfer: bad call\n");
wdenkfc3e2162003-10-08 22:33:00 +0000216 return I2C_NOK;
217 }
wdenk1cb8e982003-03-06 21:55:29 +0000218
wdenkfc3e2162003-10-08 22:33:00 +0000219 /* Check I2C bus idle */
220 i = I2C_TIMEOUT * 1000;
C Naumand9abba82010-10-26 23:04:31 +0900221 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900222 udelay(1000);
wdenkfc3e2162003-10-08 22:33:00 +0000223 i--;
224 }
wdenk1cb8e982003-03-06 21:55:29 +0000225
C Naumand9abba82010-10-26 23:04:31 +0900226 if (readl(&i2c->iicstat) & I2CSTAT_BSY)
wdenkfc3e2162003-10-08 22:33:00 +0000227 return I2C_NOK_TOUT;
wdenk1cb8e982003-03-06 21:55:29 +0000228
C Naumand9abba82010-10-26 23:04:31 +0900229 writel(readl(&i2c->iiccon) | 0x80, &i2c->iiccon);
wdenkfc3e2162003-10-08 22:33:00 +0000230 result = I2C_OK;
wdenk1cb8e982003-03-06 21:55:29 +0000231
wdenkfc3e2162003-10-08 22:33:00 +0000232 switch (cmd_type) {
wdenk48b42612003-06-19 23:01:32 +0000233 case I2C_WRITE:
wdenkfc3e2162003-10-08 22:33:00 +0000234 if (addr && addr_len) {
C Naumand9abba82010-10-26 23:04:31 +0900235 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000236 /* send START */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900237 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Naumand9abba82010-10-26 23:04:31 +0900238 &i2c->iicstat);
wdenkfc3e2162003-10-08 22:33:00 +0000239 i = 0;
240 while ((i < addr_len) && (result == I2C_OK)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900241 result = WaitForXfer();
C Naumand9abba82010-10-26 23:04:31 +0900242 writel(addr[i], &i2c->iicds);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900243 ReadWriteByte();
wdenkfc3e2162003-10-08 22:33:00 +0000244 i++;
245 }
246 i = 0;
247 while ((i < data_len) && (result == I2C_OK)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900248 result = WaitForXfer();
C Naumand9abba82010-10-26 23:04:31 +0900249 writel(data[i], &i2c->iicds);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900250 ReadWriteByte();
wdenkfc3e2162003-10-08 22:33:00 +0000251 i++;
252 }
253 } else {
C Naumand9abba82010-10-26 23:04:31 +0900254 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000255 /* send START */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900256 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
C Naumand9abba82010-10-26 23:04:31 +0900257 &i2c->iicstat);
wdenkfc3e2162003-10-08 22:33:00 +0000258 i = 0;
259 while ((i < data_len) && (result = I2C_OK)) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900260 result = WaitForXfer();
C Naumand9abba82010-10-26 23:04:31 +0900261 writel(data[i], &i2c->iicds);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900262 ReadWriteByte();
wdenkfc3e2162003-10-08 22:33:00 +0000263 i++;
264 }
wdenk1cb8e982003-03-06 21:55:29 +0000265 }
wdenk1cb8e982003-03-06 21:55:29 +0000266
wdenkfc3e2162003-10-08 22:33:00 +0000267 if (result == I2C_OK)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900268 result = WaitForXfer();
wdenk1cb8e982003-03-06 21:55:29 +0000269
wdenkfc3e2162003-10-08 22:33:00 +0000270 /* send STOP */
C Naumand9abba82010-10-26 23:04:31 +0900271 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900272 ReadWriteByte();
wdenkfc3e2162003-10-08 22:33:00 +0000273 break;
wdenk1cb8e982003-03-06 21:55:29 +0000274
wdenk48b42612003-06-19 23:01:32 +0000275 case I2C_READ:
wdenkfc3e2162003-10-08 22:33:00 +0000276 if (addr && addr_len) {
C Naumand9abba82010-10-26 23:04:31 +0900277 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
278 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000279 /* send START */
C Naumand9abba82010-10-26 23:04:31 +0900280 writel(readl(&i2c->iicstat) | I2C_START_STOP,
281 &i2c->iicstat);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900282 result = WaitForXfer();
283 if (IsACK()) {
wdenkfc3e2162003-10-08 22:33:00 +0000284 i = 0;
285 while ((i < addr_len) && (result == I2C_OK)) {
C Naumand9abba82010-10-26 23:04:31 +0900286 writel(addr[i], &i2c->iicds);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900287 ReadWriteByte();
288 result = WaitForXfer();
wdenkfc3e2162003-10-08 22:33:00 +0000289 i++;
290 }
wdenk1cb8e982003-03-06 21:55:29 +0000291
C Naumand9abba82010-10-26 23:04:31 +0900292 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000293 /* resend START */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900294 writel(I2C_MODE_MR | I2C_TXRX_ENA |
C Naumand9abba82010-10-26 23:04:31 +0900295 I2C_START_STOP, &i2c->iicstat);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900296 ReadWriteByte();
297 result = WaitForXfer();
wdenkfc3e2162003-10-08 22:33:00 +0000298 i = 0;
299 while ((i < data_len) && (result == I2C_OK)) {
300 /* disable ACK for final READ */
301 if (i == data_len - 1)
C Naumand9abba82010-10-26 23:04:31 +0900302 writel(readl(&i2c->iiccon)
303 & ~0x80, &i2c->iiccon);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900304 ReadWriteByte();
305 result = WaitForXfer();
C Naumand9abba82010-10-26 23:04:31 +0900306 data[i] = readl(&i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000307 i++;
308 }
309 } else {
310 result = I2C_NACK;
311 }
312
wdenk1cb8e982003-03-06 21:55:29 +0000313 } else {
C Naumand9abba82010-10-26 23:04:31 +0900314 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
315 writel(chip, &i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000316 /* send START */
C Naumand9abba82010-10-26 23:04:31 +0900317 writel(readl(&i2c->iicstat) | I2C_START_STOP,
318 &i2c->iicstat);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900319 result = WaitForXfer();
wdenkfc3e2162003-10-08 22:33:00 +0000320
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900321 if (IsACK()) {
wdenkfc3e2162003-10-08 22:33:00 +0000322 i = 0;
323 while ((i < data_len) && (result == I2C_OK)) {
324 /* disable ACK for final READ */
325 if (i == data_len - 1)
C Naumand9abba82010-10-26 23:04:31 +0900326 writel(readl(&i2c->iiccon) &
327 ~0x80, &i2c->iiccon);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900328 ReadWriteByte();
329 result = WaitForXfer();
C Naumand9abba82010-10-26 23:04:31 +0900330 data[i] = readl(&i2c->iicds);
wdenkfc3e2162003-10-08 22:33:00 +0000331 i++;
332 }
333 } else {
334 result = I2C_NACK;
335 }
wdenk1cb8e982003-03-06 21:55:29 +0000336 }
337
wdenkfc3e2162003-10-08 22:33:00 +0000338 /* send STOP */
C Naumand9abba82010-10-26 23:04:31 +0900339 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900340 ReadWriteByte();
wdenkfc3e2162003-10-08 22:33:00 +0000341 break;
wdenk1cb8e982003-03-06 21:55:29 +0000342
343 default:
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900344 printf("i2c_transfer: bad call\n");
wdenkfc3e2162003-10-08 22:33:00 +0000345 result = I2C_NOK;
346 break;
347 }
wdenk1cb8e982003-03-06 21:55:29 +0000348
wdenkfc3e2162003-10-08 22:33:00 +0000349 return (result);
wdenk1cb8e982003-03-06 21:55:29 +0000350}
351
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900352int i2c_probe(uchar chip)
wdenk1cb8e982003-03-06 21:55:29 +0000353{
wdenkfc3e2162003-10-08 22:33:00 +0000354 uchar buf[1];
wdenk1cb8e982003-03-06 21:55:29 +0000355
wdenkfc3e2162003-10-08 22:33:00 +0000356 buf[0] = 0;
wdenk1cb8e982003-03-06 21:55:29 +0000357
wdenkfc3e2162003-10-08 22:33:00 +0000358 /*
359 * What is needed is to send the chip address and verify that the
360 * address was <ACK>ed (i.e. there was a chip at that address which
361 * drove the data line low).
362 */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900363 return i2c_transfer(I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
wdenk1cb8e982003-03-06 21:55:29 +0000364}
365
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900366int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1cb8e982003-03-06 21:55:29 +0000367{
wdenkfc3e2162003-10-08 22:33:00 +0000368 uchar xaddr[4];
369 int ret;
wdenk1cb8e982003-03-06 21:55:29 +0000370
wdenkfc3e2162003-10-08 22:33:00 +0000371 if (alen > 4) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900372 printf("I2C read: addr len %d not supported\n", alen);
wdenkfc3e2162003-10-08 22:33:00 +0000373 return 1;
374 }
wdenk1cb8e982003-03-06 21:55:29 +0000375
wdenkfc3e2162003-10-08 22:33:00 +0000376 if (alen > 0) {
377 xaddr[0] = (addr >> 24) & 0xFF;
378 xaddr[1] = (addr >> 16) & 0xFF;
379 xaddr[2] = (addr >> 8) & 0xFF;
380 xaddr[3] = addr & 0xFF;
381 }
wdenk1cb8e982003-03-06 21:55:29 +0000382
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200383#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenkfc3e2162003-10-08 22:33:00 +0000384 /*
385 * EEPROM chips that implement "address overflow" are ones
386 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
387 * address and the extra bits end up in the "chip address"
388 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
389 * four 256 byte chips.
390 *
391 * Note that we consider the length of the address field to
392 * still be one byte because the extra address bits are
393 * hidden in the chip address.
394 */
395 if (alen > 0)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900396 chip |= ((addr >> (alen * 8)) &
397 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1cb8e982003-03-06 21:55:29 +0000398#endif
wdenkfc3e2162003-10-08 22:33:00 +0000399 if ((ret =
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900400 i2c_transfer(I2C_READ, chip << 1, &xaddr[4 - alen], alen,
401 buffer, len)) != 0) {
402 printf("I2c read: failed %d\n", ret);
wdenkfc3e2162003-10-08 22:33:00 +0000403 return 1;
404 }
405 return 0;
wdenk1cb8e982003-03-06 21:55:29 +0000406}
407
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900408int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1cb8e982003-03-06 21:55:29 +0000409{
wdenkfc3e2162003-10-08 22:33:00 +0000410 uchar xaddr[4];
wdenk1cb8e982003-03-06 21:55:29 +0000411
wdenkfc3e2162003-10-08 22:33:00 +0000412 if (alen > 4) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900413 printf("I2C write: addr len %d not supported\n", alen);
wdenkfc3e2162003-10-08 22:33:00 +0000414 return 1;
415 }
wdenk1cb8e982003-03-06 21:55:29 +0000416
wdenkfc3e2162003-10-08 22:33:00 +0000417 if (alen > 0) {
418 xaddr[0] = (addr >> 24) & 0xFF;
419 xaddr[1] = (addr >> 16) & 0xFF;
420 xaddr[2] = (addr >> 8) & 0xFF;
421 xaddr[3] = addr & 0xFF;
422 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200423#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenkfc3e2162003-10-08 22:33:00 +0000424 /*
425 * EEPROM chips that implement "address overflow" are ones
426 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
427 * address and the extra bits end up in the "chip address"
428 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
429 * four 256 byte chips.
430 *
431 * Note that we consider the length of the address field to
432 * still be one byte because the extra address bits are
433 * hidden in the chip address.
434 */
435 if (alen > 0)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900436 chip |= ((addr >> (alen * 8)) &
437 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1cb8e982003-03-06 21:55:29 +0000438#endif
wdenkfc3e2162003-10-08 22:33:00 +0000439 return (i2c_transfer
440 (I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
441 len) != 0);
wdenk1cb8e982003-03-06 21:55:29 +0000442}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900443#endif /* CONFIG_HARD_I2C */