blob: c65360d0ffce51b5447cf51d0a3b0eb9722c133a [file] [log] [blame]
wdenk1cb8e982003-03-06 21:55:29 +00001/*
2 * (C) Copyright 2002
3 * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk1cb8e982003-03-06 21:55:29 +00006 */
7
8/* This code should work for both the S3C2400 and the S3C2410
9 * as they seem to have the same I2C controller inside.
10 * The different address mapping is handled by the s3c24xx.h files below.
11 */
12
13#include <common.h>
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +000014#include <fdtdec.h>
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +000015#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000016#include <asm/arch/clk.h>
17#include <asm/arch/cpu.h>
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +000018#include <asm/arch/pinmux.h>
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000019#else
kevin.morfitt@fearnside-systems.co.ukac678042009-11-17 18:30:34 +090020#include <asm/arch/s3c24x0_cpu.h>
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000021#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090022#include <asm/io.h>
wdenk1cb8e982003-03-06 21:55:29 +000023#include <i2c.h>
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000024#include "s3c24x0_i2c.h"
wdenk1cb8e982003-03-06 21:55:29 +000025
26#ifdef CONFIG_HARD_I2C
27
wdenk48b42612003-06-19 23:01:32 +000028#define I2C_WRITE 0
29#define I2C_READ 1
wdenk1cb8e982003-03-06 21:55:29 +000030
wdenk48b42612003-06-19 23:01:32 +000031#define I2C_OK 0
32#define I2C_NOK 1
33#define I2C_NACK 2
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090034#define I2C_NOK_LA 3 /* Lost arbitration */
35#define I2C_NOK_TOUT 4 /* time out */
wdenk1cb8e982003-03-06 21:55:29 +000036
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090037#define I2CSTAT_BSY 0x20 /* Busy bit */
38#define I2CSTAT_NACK 0x01 /* Nack bit */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000039#define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090040#define I2CCON_IRPND 0x10 /* Interrupt pending bit */
41#define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
42#define I2C_MODE_MR 0x80 /* Master Receive Mode */
43#define I2C_START_STOP 0x20 /* START / STOP */
44#define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
wdenk1cb8e982003-03-06 21:55:29 +000045
Naveen Krishna Che4e24022013-10-15 16:01:43 +053046#define I2C_TIMEOUT_MS 1000 /* 1 second */
wdenk1cb8e982003-03-06 21:55:29 +000047
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000048
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +000049/*
50 * For SPL boot some boards need i2c before SDRAM is initialised so force
51 * variables to live in SRAM
52 */
53static unsigned int g_current_bus __attribute__((section(".data")));
Rajeshwari Shinded04df3c2013-01-13 19:49:36 +000054#ifdef CONFIG_OF_CONTROL
55static int i2c_busses __attribute__((section(".data")));
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +000056static struct s3c24x0_i2c_bus i2c_bus[CONFIG_MAX_I2C_NUM]
57 __attribute__((section(".data")));
Rajeshwari Shinded04df3c2013-01-13 19:49:36 +000058#endif
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000059
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +000060#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
wdenk48b42612003-06-19 23:01:32 +000061static int GetI2CSDA(void)
wdenk1cb8e982003-03-06 21:55:29 +000062{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090063 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk48b42612003-06-19 23:01:32 +000064
wdenk6dff5522003-07-15 07:45:49 +000065#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +090066 return (readl(&gpio->gpedat) & 0x8000) >> 15;
wdenk6dff5522003-07-15 07:45:49 +000067#endif
68#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +090069 return (readl(&gpio->pgdat) & 0x0020) >> 5;
wdenk6dff5522003-07-15 07:45:49 +000070#endif
wdenk1cb8e982003-03-06 21:55:29 +000071}
72
wdenk48b42612003-06-19 23:01:32 +000073static void SetI2CSCL(int x)
wdenk1cb8e982003-03-06 21:55:29 +000074{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090075 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk48b42612003-06-19 23:01:32 +000076
wdenk6dff5522003-07-15 07:45:49 +000077#ifdef CONFIG_S3C2410
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000078 writel((readl(&gpio->gpedat) & ~0x4000) |
79 (x & 1) << 14, &gpio->gpedat);
wdenk6dff5522003-07-15 07:45:49 +000080#endif
81#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +090082 writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
wdenk6dff5522003-07-15 07:45:49 +000083#endif
wdenk1cb8e982003-03-06 21:55:29 +000084}
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000085#endif
wdenk1cb8e982003-03-06 21:55:29 +000086
Naveen Krishna Che4e24022013-10-15 16:01:43 +053087/*
88 * Wait til the byte transfer is completed.
89 *
90 * @param i2c- pointer to the appropriate i2c register bank.
91 * @return I2C_OK, if transmission was ACKED
92 * I2C_NACK, if transmission was NACKED
93 * I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS
94 */
95
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +000096static int WaitForXfer(struct s3c24x0_i2c *i2c)
wdenk1cb8e982003-03-06 21:55:29 +000097{
Naveen Krishna Che4e24022013-10-15 16:01:43 +053098 ulong start_time = get_timer(0);
wdenk1cb8e982003-03-06 21:55:29 +000099
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530100 do {
101 if (readl(&i2c->iiccon) & I2CCON_IRPND)
102 return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
103 I2C_NACK : I2C_OK;
104 } while (get_timer(start_time) < I2C_TIMEOUT_MS);
wdenk1cb8e982003-03-06 21:55:29 +0000105
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530106 return I2C_NOK_TOUT;
wdenk1cb8e982003-03-06 21:55:29 +0000107}
108
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000109static void ReadWriteByte(struct s3c24x0_i2c *i2c)
wdenk1cb8e982003-03-06 21:55:29 +0000110{
C Naumand9abba82010-10-26 23:04:31 +0900111 writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
wdenk1cb8e982003-03-06 21:55:29 +0000112}
113
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000114static struct s3c24x0_i2c *get_base_i2c(void)
115{
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +0000116#ifdef CONFIG_EXYNOS4
117 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
118 + (EXYNOS4_I2C_SPACING
119 * g_current_bus));
120 return i2c;
121#elif defined CONFIG_EXYNOS5
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000122 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
123 + (EXYNOS5_I2C_SPACING
124 * g_current_bus));
125 return i2c;
126#else
127 return s3c24x0_get_base_i2c();
128#endif
129}
130
131static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
132{
133 ulong freq, pres = 16, div;
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +0000134#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000135 freq = get_i2c_clk();
136#else
137 freq = get_PCLK();
138#endif
139 /* calculate prescaler and divisor values */
140 if ((freq / pres / (16 + 1)) > speed)
141 /* set prescaler to 512 */
142 pres = 512;
143
144 div = 0;
145 while ((freq / pres / (div + 1)) > speed)
146 div++;
147
148 /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
149 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
150
151 /* init to SLAVE REVEIVE and set slaveaddr */
152 writel(0, &i2c->iicstat);
153 writel(slaveadd, &i2c->iicadd);
154 /* program Master Transmit (and implicit STOP) */
155 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
156}
157
Rajeshwari Shinde178239d2012-07-23 21:23:54 +0000158/*
159 * MULTI BUS I2C support
160 */
161
162#ifdef CONFIG_I2C_MULTI_BUS
163int i2c_set_bus_num(unsigned int bus)
164{
165 struct s3c24x0_i2c *i2c;
166
167 if ((bus < 0) || (bus >= CONFIG_MAX_I2C_NUM)) {
168 debug("Bad bus: %d\n", bus);
169 return -1;
170 }
171
172 g_current_bus = bus;
173 i2c = get_base_i2c();
174 i2c_ch_init(i2c, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
175
176 return 0;
177}
178
179unsigned int i2c_get_bus_num(void)
180{
181 return g_current_bus;
182}
183#endif
184
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900185void i2c_init(int speed, int slaveadd)
wdenk1cb8e982003-03-06 21:55:29 +0000186{
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530187 int i;
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000188 struct s3c24x0_i2c *i2c;
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +0000189#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900190 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000191#endif
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530192 ulong start_time = get_timer(0);
wdenk1cb8e982003-03-06 21:55:29 +0000193
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000194 /* By default i2c channel 0 is the current bus */
195 g_current_bus = 0;
196 i2c = get_base_i2c();
wdenk1cb8e982003-03-06 21:55:29 +0000197
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530198 /*
199 * In case the previous transfer is still going, wait to give it a
200 * chance to finish.
201 */
202 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
203 if (get_timer(start_time) > I2C_TIMEOUT_MS) {
204 printf("%s: I2C bus busy for %p\n", __func__,
205 &i2c->iicstat);
206 return;
207 }
wdenk1cb8e982003-03-06 21:55:29 +0000208 }
wdenk1cb8e982003-03-06 21:55:29 +0000209
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +0000210#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
C Naumand9abba82010-10-26 23:04:31 +0900211 if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
wdenk6dff5522003-07-15 07:45:49 +0000212#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +0900213 ulong old_gpecon = readl(&gpio->gpecon);
wdenk6dff5522003-07-15 07:45:49 +0000214#endif
215#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +0900216 ulong old_gpecon = readl(&gpio->pgcon);
wdenk6dff5522003-07-15 07:45:49 +0000217#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900218 /* bus still busy probably by (most) previously interrupted
219 transfer */
wdenk1cb8e982003-03-06 21:55:29 +0000220
wdenkfc3e2162003-10-08 22:33:00 +0000221#ifdef CONFIG_S3C2410
222 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
C Naumand9abba82010-10-26 23:04:31 +0900223 writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
224 &gpio->gpecon);
wdenkfc3e2162003-10-08 22:33:00 +0000225#endif
226#ifdef CONFIG_S3C2400
227 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
C Naumand9abba82010-10-26 23:04:31 +0900228 writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
229 &gpio->pgcon);
wdenkfc3e2162003-10-08 22:33:00 +0000230#endif
wdenk1cb8e982003-03-06 21:55:29 +0000231
wdenkfc3e2162003-10-08 22:33:00 +0000232 /* toggle I2CSCL until bus idle */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900233 SetI2CSCL(0);
234 udelay(1000);
wdenkfc3e2162003-10-08 22:33:00 +0000235 i = 10;
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900236 while ((i > 0) && (GetI2CSDA() != 1)) {
237 SetI2CSCL(1);
238 udelay(1000);
239 SetI2CSCL(0);
240 udelay(1000);
wdenkfc3e2162003-10-08 22:33:00 +0000241 i--;
242 }
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900243 SetI2CSCL(1);
244 udelay(1000);
wdenk1cb8e982003-03-06 21:55:29 +0000245
wdenkfc3e2162003-10-08 22:33:00 +0000246 /* restore pin functions */
247#ifdef CONFIG_S3C2410
C Naumand9abba82010-10-26 23:04:31 +0900248 writel(old_gpecon, &gpio->gpecon);
wdenkfc3e2162003-10-08 22:33:00 +0000249#endif
250#ifdef CONFIG_S3C2400
C Naumand9abba82010-10-26 23:04:31 +0900251 writel(old_gpecon, &gpio->pgcon);
wdenkfc3e2162003-10-08 22:33:00 +0000252#endif
253 }
Piotr Wilczekc86d9ed2012-11-20 02:19:05 +0000254#endif /* #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000255 i2c_ch_init(i2c, speed, slaveadd);
wdenk1cb8e982003-03-06 21:55:29 +0000256}
257
258/*
wdenkfc3e2162003-10-08 22:33:00 +0000259 * cmd_type is 0 for write, 1 for read.
260 *
261 * addr_len can take any value from 0-255, it is only limited
262 * by the char, we could make it larger if needed. If it is
263 * 0 we skip the address write cycle.
264 */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000265static int i2c_transfer(struct s3c24x0_i2c *i2c,
266 unsigned char cmd_type,
267 unsigned char chip,
268 unsigned char addr[],
269 unsigned char addr_len,
270 unsigned char data[],
271 unsigned short data_len)
wdenk1cb8e982003-03-06 21:55:29 +0000272{
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530273 int i = 0, result;
274 ulong start_time = get_timer(0);
wdenk1cb8e982003-03-06 21:55:29 +0000275
wdenkfc3e2162003-10-08 22:33:00 +0000276 if (data == 0 || data_len == 0) {
277 /*Don't support data transfer of no length or to address 0 */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000278 debug("i2c_transfer: bad call\n");
wdenkfc3e2162003-10-08 22:33:00 +0000279 return I2C_NOK;
280 }
wdenk1cb8e982003-03-06 21:55:29 +0000281
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530282 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
283 if (get_timer(start_time) > I2C_TIMEOUT_MS)
284 return I2C_NOK_TOUT;
wdenkfc3e2162003-10-08 22:33:00 +0000285 }
wdenk1cb8e982003-03-06 21:55:29 +0000286
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000287 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530288
289 /* Get the slave chip address going */
290 writel(chip, &i2c->iicds);
291 if ((cmd_type == I2C_WRITE) || (addr && addr_len))
292 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
293 &i2c->iicstat);
294 else
295 writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
296 &i2c->iicstat);
297
298 /* Wait for chip address to transmit. */
299 result = WaitForXfer(i2c);
300 if (result != I2C_OK)
301 goto bailout;
302
303 /* If register address needs to be transmitted - do it now. */
304 if (addr && addr_len) {
305 while ((i < addr_len) && (result == I2C_OK)) {
306 writel(addr[i++], &i2c->iicds);
307 ReadWriteByte(i2c);
308 result = WaitForXfer(i2c);
309 }
310 i = 0;
311 if (result != I2C_OK)
312 goto bailout;
313 }
wdenk1cb8e982003-03-06 21:55:29 +0000314
wdenkfc3e2162003-10-08 22:33:00 +0000315 switch (cmd_type) {
wdenk48b42612003-06-19 23:01:32 +0000316 case I2C_WRITE:
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530317 while ((i < data_len) && (result == I2C_OK)) {
318 writel(data[i++], &i2c->iicds);
319 ReadWriteByte(i2c);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000320 result = WaitForXfer(i2c);
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530321 }
wdenkfc3e2162003-10-08 22:33:00 +0000322 break;
wdenk1cb8e982003-03-06 21:55:29 +0000323
wdenk48b42612003-06-19 23:01:32 +0000324 case I2C_READ:
wdenkfc3e2162003-10-08 22:33:00 +0000325 if (addr && addr_len) {
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530326 /*
327 * Register address has been sent, now send slave chip
328 * address again to start the actual read transaction.
329 */
C Naumand9abba82010-10-26 23:04:31 +0900330 writel(chip, &i2c->iicds);
wdenk1cb8e982003-03-06 21:55:29 +0000331
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530332 /* Generate a re-START. */
Rajeshwari Shindecb466c02013-02-19 02:19:45 +0000333 writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
334 &i2c->iicstat);
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530335 ReadWriteByte(i2c);
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000336 result = WaitForXfer(i2c);
wdenkfc3e2162003-10-08 22:33:00 +0000337
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530338 if (result != I2C_OK)
339 goto bailout;
wdenk1cb8e982003-03-06 21:55:29 +0000340 }
341
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530342 while ((i < data_len) && (result == I2C_OK)) {
343 /* disable ACK for final READ */
344 if (i == data_len - 1)
345 writel(readl(&i2c->iiccon)
346 & ~I2CCON_ACKGEN,
347 &i2c->iiccon);
348 ReadWriteByte(i2c);
349 result = WaitForXfer(i2c);
350 data[i++] = readl(&i2c->iicds);
351 }
352 if (result == I2C_NACK)
353 result = I2C_OK; /* Normal terminated read. */
wdenkfc3e2162003-10-08 22:33:00 +0000354 break;
wdenk1cb8e982003-03-06 21:55:29 +0000355
356 default:
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000357 debug("i2c_transfer: bad call\n");
wdenkfc3e2162003-10-08 22:33:00 +0000358 result = I2C_NOK;
359 break;
360 }
wdenk1cb8e982003-03-06 21:55:29 +0000361
Naveen Krishna Che4e24022013-10-15 16:01:43 +0530362bailout:
363 /* Send STOP. */
364 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
365 ReadWriteByte(i2c);
366
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000367 return result;
wdenk1cb8e982003-03-06 21:55:29 +0000368}
369
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900370int i2c_probe(uchar chip)
wdenk1cb8e982003-03-06 21:55:29 +0000371{
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000372 struct s3c24x0_i2c *i2c;
wdenkfc3e2162003-10-08 22:33:00 +0000373 uchar buf[1];
wdenk1cb8e982003-03-06 21:55:29 +0000374
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000375 i2c = get_base_i2c();
wdenkfc3e2162003-10-08 22:33:00 +0000376 buf[0] = 0;
wdenk1cb8e982003-03-06 21:55:29 +0000377
wdenkfc3e2162003-10-08 22:33:00 +0000378 /*
379 * What is needed is to send the chip address and verify that the
380 * address was <ACK>ed (i.e. there was a chip at that address which
381 * drove the data line low).
382 */
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000383 return i2c_transfer(i2c, I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
wdenk1cb8e982003-03-06 21:55:29 +0000384}
385
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900386int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
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 xaddr[4];
390 int ret;
wdenk1cb8e982003-03-06 21:55:29 +0000391
wdenkfc3e2162003-10-08 22:33:00 +0000392 if (alen > 4) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000393 debug("I2C read: addr len %d not supported\n", alen);
wdenkfc3e2162003-10-08 22:33:00 +0000394 return 1;
395 }
wdenk1cb8e982003-03-06 21:55:29 +0000396
wdenkfc3e2162003-10-08 22:33:00 +0000397 if (alen > 0) {
398 xaddr[0] = (addr >> 24) & 0xFF;
399 xaddr[1] = (addr >> 16) & 0xFF;
400 xaddr[2] = (addr >> 8) & 0xFF;
401 xaddr[3] = addr & 0xFF;
402 }
wdenk1cb8e982003-03-06 21:55:29 +0000403
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200404#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenkfc3e2162003-10-08 22:33:00 +0000405 /*
406 * EEPROM chips that implement "address overflow" are ones
407 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
408 * address and the extra bits end up in the "chip address"
409 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
410 * four 256 byte chips.
411 *
412 * Note that we consider the length of the address field to
413 * still be one byte because the extra address bits are
414 * hidden in the chip address.
415 */
416 if (alen > 0)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900417 chip |= ((addr >> (alen * 8)) &
418 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1cb8e982003-03-06 21:55:29 +0000419#endif
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000420 i2c = get_base_i2c();
421 ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen,
422 buffer, len);
423 if (ret != 0) {
424 debug("I2c read: failed %d\n", ret);
wdenkfc3e2162003-10-08 22:33:00 +0000425 return 1;
426 }
427 return 0;
wdenk1cb8e982003-03-06 21:55:29 +0000428}
429
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900430int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1cb8e982003-03-06 21:55:29 +0000431{
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000432 struct s3c24x0_i2c *i2c;
wdenkfc3e2162003-10-08 22:33:00 +0000433 uchar xaddr[4];
wdenk1cb8e982003-03-06 21:55:29 +0000434
wdenkfc3e2162003-10-08 22:33:00 +0000435 if (alen > 4) {
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000436 debug("I2C write: addr len %d not supported\n", alen);
wdenkfc3e2162003-10-08 22:33:00 +0000437 return 1;
438 }
wdenk1cb8e982003-03-06 21:55:29 +0000439
wdenkfc3e2162003-10-08 22:33:00 +0000440 if (alen > 0) {
441 xaddr[0] = (addr >> 24) & 0xFF;
442 xaddr[1] = (addr >> 16) & 0xFF;
443 xaddr[2] = (addr >> 8) & 0xFF;
444 xaddr[3] = addr & 0xFF;
445 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200446#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenkfc3e2162003-10-08 22:33:00 +0000447 /*
448 * EEPROM chips that implement "address overflow" are ones
449 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
450 * address and the extra bits end up in the "chip address"
451 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
452 * four 256 byte chips.
453 *
454 * Note that we consider the length of the address field to
455 * still be one byte because the extra address bits are
456 * hidden in the chip address.
457 */
458 if (alen > 0)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900459 chip |= ((addr >> (alen * 8)) &
460 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1cb8e982003-03-06 21:55:29 +0000461#endif
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000462 i2c = get_base_i2c();
wdenkfc3e2162003-10-08 22:33:00 +0000463 return (i2c_transfer
Rajeshwari Shindeab7e52b2012-07-23 21:23:53 +0000464 (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
wdenkfc3e2162003-10-08 22:33:00 +0000465 len) != 0);
wdenk1cb8e982003-03-06 21:55:29 +0000466}
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000467
Amar1ae76d42013-07-10 10:42:29 +0530468#ifdef CONFIG_OF_CONTROL
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000469void board_i2c_init(const void *blob)
470{
Amar2c07bb92013-04-04 02:27:06 -0400471 int i;
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000472 int node_list[CONFIG_MAX_I2C_NUM];
Amar2c07bb92013-04-04 02:27:06 -0400473 int count;
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000474
475 count = fdtdec_find_aliases_for_id(blob, "i2c",
476 COMPAT_SAMSUNG_S3C2440_I2C, node_list,
477 CONFIG_MAX_I2C_NUM);
478
479 for (i = 0; i < count; i++) {
480 struct s3c24x0_i2c_bus *bus;
481 int node = node_list[i];
482
483 if (node <= 0)
484 continue;
485 bus = &i2c_bus[i];
486 bus->regs = (struct s3c24x0_i2c *)
487 fdtdec_get_addr(blob, node, "reg");
488 bus->id = pinmux_decode_periph_id(blob, node);
489 bus->node = node;
490 bus->bus_num = i2c_busses++;
491 exynos_pinmux_config(bus->id, 0);
492 }
493}
494
495static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx)
496{
497 if (bus_idx < i2c_busses)
498 return &i2c_bus[bus_idx];
499
500 debug("Undefined bus: %d\n", bus_idx);
501 return NULL;
502}
503
504int i2c_get_bus_num_fdt(int node)
505{
506 int i;
507
508 for (i = 0; i < i2c_busses; i++) {
509 if (node == i2c_bus[i].node)
510 return i;
511 }
512
513 debug("%s: Can't find any matched I2C bus\n", __func__);
514 return -1;
515}
516
517int i2c_reset_port_fdt(const void *blob, int node)
518{
519 struct s3c24x0_i2c_bus *i2c;
520 int bus;
521
522 bus = i2c_get_bus_num_fdt(node);
523 if (bus < 0) {
524 debug("could not get bus for node %d\n", node);
525 return -1;
526 }
527
528 i2c = get_bus(bus);
529 if (!i2c) {
530 debug("get_bus() failed for node node %d\n", node);
531 return -1;
532 }
533
534 i2c_ch_init(i2c->regs, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
535
536 return 0;
537}
538#endif
539
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900540#endif /* CONFIG_HARD_I2C */