wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 1 | /* |
| 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 Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 30 | #include <fdtdec.h> |
Piotr Wilczek | b35cd1c | 2012-11-20 02:19:05 +0000 | [diff] [blame] | 31 | #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 32 | #include <asm/arch/clk.h> |
| 33 | #include <asm/arch/cpu.h> |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 34 | #include <asm/arch/pinmux.h> |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 35 | #else |
kevin.morfitt@fearnside-systems.co.uk | e0d8131 | 2009-11-17 18:30:34 +0900 | [diff] [blame] | 36 | #include <asm/arch/s3c24x0_cpu.h> |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 37 | #endif |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 38 | #include <asm/io.h> |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 39 | #include <i2c.h> |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 40 | #include "s3c24x0_i2c.h" |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 41 | |
| 42 | #ifdef CONFIG_HARD_I2C |
| 43 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 44 | #define I2C_WRITE 0 |
| 45 | #define I2C_READ 1 |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 46 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 47 | #define I2C_OK 0 |
| 48 | #define I2C_NOK 1 |
| 49 | #define I2C_NACK 2 |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 50 | #define I2C_NOK_LA 3 /* Lost arbitration */ |
| 51 | #define I2C_NOK_TOUT 4 /* time out */ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 52 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 53 | #define I2CSTAT_BSY 0x20 /* Busy bit */ |
| 54 | #define I2CSTAT_NACK 0x01 /* Nack bit */ |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 55 | #define I2CCON_ACKGEN 0x80 /* Acknowledge generation */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 56 | #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 */ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 61 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 62 | #define I2C_TIMEOUT 1 /* 1 second */ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 63 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 64 | |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 65 | /* |
| 66 | * For SPL boot some boards need i2c before SDRAM is initialised so force |
| 67 | * variables to live in SRAM |
| 68 | */ |
| 69 | static unsigned int g_current_bus __attribute__((section(".data"))); |
Rajeshwari Shinde | 9a611f6 | 2013-01-13 19:49:36 +0000 | [diff] [blame] | 70 | #ifdef CONFIG_OF_CONTROL |
| 71 | static int i2c_busses __attribute__((section(".data"))); |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 72 | static struct s3c24x0_i2c_bus i2c_bus[CONFIG_MAX_I2C_NUM] |
| 73 | __attribute__((section(".data"))); |
Rajeshwari Shinde | 9a611f6 | 2013-01-13 19:49:36 +0000 | [diff] [blame] | 74 | #endif |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 75 | |
Piotr Wilczek | b35cd1c | 2012-11-20 02:19:05 +0000 | [diff] [blame] | 76 | #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 77 | static int GetI2CSDA(void) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 78 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 79 | struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 80 | |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 81 | #ifdef CONFIG_S3C2410 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 82 | return (readl(&gpio->gpedat) & 0x8000) >> 15; |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 83 | #endif |
| 84 | #ifdef CONFIG_S3C2400 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 85 | return (readl(&gpio->pgdat) & 0x0020) >> 5; |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 86 | #endif |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 87 | } |
| 88 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 89 | #if 0 |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 90 | static void SetI2CSDA(int x) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 91 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 92 | rGPEDAT = (rGPEDAT & ~0x8000) | (x & 1) << 15; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 93 | } |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 94 | #endif |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 95 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 96 | static void SetI2CSCL(int x) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 97 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 98 | struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 99 | |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 100 | #ifdef CONFIG_S3C2410 |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 101 | writel((readl(&gpio->gpedat) & ~0x4000) | |
| 102 | (x & 1) << 14, &gpio->gpedat); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 103 | #endif |
| 104 | #ifdef CONFIG_S3C2400 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 105 | writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 106 | #endif |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 107 | } |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 108 | #endif |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 109 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 110 | static int WaitForXfer(struct s3c24x0_i2c *i2c) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 111 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 112 | int i; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 113 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 114 | i = I2C_TIMEOUT * 10000; |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 115 | while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 116 | udelay(100); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 117 | i--; |
| 118 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 119 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 120 | return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 123 | static int IsACK(struct s3c24x0_i2c *i2c) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 124 | { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 125 | return !(readl(&i2c->iicstat) & I2CSTAT_NACK); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 128 | static void ReadWriteByte(struct s3c24x0_i2c *i2c) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 129 | { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 130 | writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 133 | static struct s3c24x0_i2c *get_base_i2c(void) |
| 134 | { |
Piotr Wilczek | b35cd1c | 2012-11-20 02:19:05 +0000 | [diff] [blame] | 135 | #ifdef CONFIG_EXYNOS4 |
| 136 | struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c() |
| 137 | + (EXYNOS4_I2C_SPACING |
| 138 | * g_current_bus)); |
| 139 | return i2c; |
| 140 | #elif defined CONFIG_EXYNOS5 |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 141 | struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c() |
| 142 | + (EXYNOS5_I2C_SPACING |
| 143 | * g_current_bus)); |
| 144 | return i2c; |
| 145 | #else |
| 146 | return s3c24x0_get_base_i2c(); |
| 147 | #endif |
| 148 | } |
| 149 | |
| 150 | static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd) |
| 151 | { |
| 152 | ulong freq, pres = 16, div; |
Piotr Wilczek | b35cd1c | 2012-11-20 02:19:05 +0000 | [diff] [blame] | 153 | #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 154 | freq = get_i2c_clk(); |
| 155 | #else |
| 156 | freq = get_PCLK(); |
| 157 | #endif |
| 158 | /* calculate prescaler and divisor values */ |
| 159 | if ((freq / pres / (16 + 1)) > speed) |
| 160 | /* set prescaler to 512 */ |
| 161 | pres = 512; |
| 162 | |
| 163 | div = 0; |
| 164 | while ((freq / pres / (div + 1)) > speed) |
| 165 | div++; |
| 166 | |
| 167 | /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */ |
| 168 | writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon); |
| 169 | |
| 170 | /* init to SLAVE REVEIVE and set slaveaddr */ |
| 171 | writel(0, &i2c->iicstat); |
| 172 | writel(slaveadd, &i2c->iicadd); |
| 173 | /* program Master Transmit (and implicit STOP) */ |
| 174 | writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat); |
| 175 | } |
| 176 | |
Rajeshwari Shinde | c07c11d | 2012-07-23 21:23:54 +0000 | [diff] [blame] | 177 | /* |
| 178 | * MULTI BUS I2C support |
| 179 | */ |
| 180 | |
| 181 | #ifdef CONFIG_I2C_MULTI_BUS |
| 182 | int i2c_set_bus_num(unsigned int bus) |
| 183 | { |
| 184 | struct s3c24x0_i2c *i2c; |
| 185 | |
| 186 | if ((bus < 0) || (bus >= CONFIG_MAX_I2C_NUM)) { |
| 187 | debug("Bad bus: %d\n", bus); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | g_current_bus = bus; |
| 192 | i2c = get_base_i2c(); |
| 193 | i2c_ch_init(i2c, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | unsigned int i2c_get_bus_num(void) |
| 199 | { |
| 200 | return g_current_bus; |
| 201 | } |
| 202 | #endif |
| 203 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 204 | void i2c_init(int speed, int slaveadd) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 205 | { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 206 | struct s3c24x0_i2c *i2c; |
Piotr Wilczek | b35cd1c | 2012-11-20 02:19:05 +0000 | [diff] [blame] | 207 | #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 208 | struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio(); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 209 | #endif |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 210 | int i; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 211 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 212 | /* By default i2c channel 0 is the current bus */ |
| 213 | g_current_bus = 0; |
| 214 | i2c = get_base_i2c(); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 215 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 216 | /* wait for some time to give previous transfer a chance to finish */ |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 217 | i = I2C_TIMEOUT * 1000; |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 218 | while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 219 | udelay(1000); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 220 | i--; |
| 221 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 222 | |
Piotr Wilczek | b35cd1c | 2012-11-20 02:19:05 +0000 | [diff] [blame] | 223 | #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 224 | if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) { |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 225 | #ifdef CONFIG_S3C2410 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 226 | ulong old_gpecon = readl(&gpio->gpecon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 227 | #endif |
| 228 | #ifdef CONFIG_S3C2400 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 229 | ulong old_gpecon = readl(&gpio->pgcon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 230 | #endif |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 231 | /* bus still busy probably by (most) previously interrupted |
| 232 | transfer */ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 233 | |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 234 | #ifdef CONFIG_S3C2410 |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 235 | /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 236 | writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000, |
| 237 | &gpio->gpecon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 238 | #endif |
| 239 | #ifdef CONFIG_S3C2400 |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 240 | /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 241 | writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000, |
| 242 | &gpio->pgcon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 243 | #endif |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 244 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 245 | /* toggle I2CSCL until bus idle */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 246 | SetI2CSCL(0); |
| 247 | udelay(1000); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 248 | i = 10; |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 249 | while ((i > 0) && (GetI2CSDA() != 1)) { |
| 250 | SetI2CSCL(1); |
| 251 | udelay(1000); |
| 252 | SetI2CSCL(0); |
| 253 | udelay(1000); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 254 | i--; |
| 255 | } |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 256 | SetI2CSCL(1); |
| 257 | udelay(1000); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 258 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 259 | /* restore pin functions */ |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 260 | #ifdef CONFIG_S3C2410 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 261 | writel(old_gpecon, &gpio->gpecon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 262 | #endif |
| 263 | #ifdef CONFIG_S3C2400 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 264 | writel(old_gpecon, &gpio->pgcon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 265 | #endif |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 266 | } |
Piotr Wilczek | b35cd1c | 2012-11-20 02:19:05 +0000 | [diff] [blame] | 267 | #endif /* #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) */ |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 268 | i2c_ch_init(i2c, speed, slaveadd); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 272 | * cmd_type is 0 for write, 1 for read. |
| 273 | * |
| 274 | * addr_len can take any value from 0-255, it is only limited |
| 275 | * by the char, we could make it larger if needed. If it is |
| 276 | * 0 we skip the address write cycle. |
| 277 | */ |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 278 | static int i2c_transfer(struct s3c24x0_i2c *i2c, |
| 279 | unsigned char cmd_type, |
| 280 | unsigned char chip, |
| 281 | unsigned char addr[], |
| 282 | unsigned char addr_len, |
| 283 | unsigned char data[], |
| 284 | unsigned short data_len) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 285 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 286 | int i, result; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 287 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 288 | if (data == 0 || data_len == 0) { |
| 289 | /*Don't support data transfer of no length or to address 0 */ |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 290 | debug("i2c_transfer: bad call\n"); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 291 | return I2C_NOK; |
| 292 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 293 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 294 | /* Check I2C bus idle */ |
| 295 | i = I2C_TIMEOUT * 1000; |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 296 | while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 297 | udelay(1000); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 298 | i--; |
| 299 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 300 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 301 | if (readl(&i2c->iicstat) & I2CSTAT_BSY) |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 302 | return I2C_NOK_TOUT; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 303 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 304 | writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 305 | result = I2C_OK; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 306 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 307 | switch (cmd_type) { |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 308 | case I2C_WRITE: |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 309 | if (addr && addr_len) { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 310 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 311 | /* send START */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 312 | writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP, |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 313 | &i2c->iicstat); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 314 | i = 0; |
| 315 | while ((i < addr_len) && (result == I2C_OK)) { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 316 | result = WaitForXfer(i2c); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 317 | writel(addr[i], &i2c->iicds); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 318 | ReadWriteByte(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 319 | i++; |
| 320 | } |
| 321 | i = 0; |
| 322 | while ((i < data_len) && (result == I2C_OK)) { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 323 | result = WaitForXfer(i2c); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 324 | writel(data[i], &i2c->iicds); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 325 | ReadWriteByte(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 326 | i++; |
| 327 | } |
| 328 | } else { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 329 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 330 | /* send START */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 331 | writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP, |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 332 | &i2c->iicstat); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 333 | i = 0; |
| 334 | while ((i < data_len) && (result = I2C_OK)) { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 335 | result = WaitForXfer(i2c); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 336 | writel(data[i], &i2c->iicds); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 337 | ReadWriteByte(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 338 | i++; |
| 339 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 340 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 341 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 342 | if (result == I2C_OK) |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 343 | result = WaitForXfer(i2c); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 344 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 345 | /* send STOP */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 346 | writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 347 | ReadWriteByte(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 348 | break; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 349 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 350 | case I2C_READ: |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 351 | if (addr && addr_len) { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 352 | writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat); |
| 353 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 354 | /* send START */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 355 | writel(readl(&i2c->iicstat) | I2C_START_STOP, |
| 356 | &i2c->iicstat); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 357 | result = WaitForXfer(i2c); |
| 358 | if (IsACK(i2c)) { |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 359 | i = 0; |
| 360 | while ((i < addr_len) && (result == I2C_OK)) { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 361 | writel(addr[i], &i2c->iicds); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 362 | ReadWriteByte(i2c); |
| 363 | result = WaitForXfer(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 364 | i++; |
| 365 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 366 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 367 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 368 | /* resend START */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 369 | writel(I2C_MODE_MR | I2C_TXRX_ENA | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 370 | I2C_START_STOP, &i2c->iicstat); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 371 | ReadWriteByte(i2c); |
| 372 | result = WaitForXfer(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 373 | i = 0; |
| 374 | while ((i < data_len) && (result == I2C_OK)) { |
| 375 | /* disable ACK for final READ */ |
| 376 | if (i == data_len - 1) |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 377 | writel(readl(&i2c->iiccon) |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 378 | & ~I2CCON_ACKGEN, |
| 379 | &i2c->iiccon); |
| 380 | ReadWriteByte(i2c); |
| 381 | result = WaitForXfer(i2c); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 382 | data[i] = readl(&i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 383 | i++; |
| 384 | } |
| 385 | } else { |
| 386 | result = I2C_NACK; |
| 387 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 388 | |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 389 | } else { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 390 | writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat); |
| 391 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 392 | /* send START */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 393 | writel(readl(&i2c->iicstat) | I2C_START_STOP, |
| 394 | &i2c->iicstat); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 395 | result = WaitForXfer(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 396 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 397 | if (IsACK(i2c)) { |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 398 | i = 0; |
| 399 | while ((i < data_len) && (result == I2C_OK)) { |
| 400 | /* disable ACK for final READ */ |
| 401 | if (i == data_len - 1) |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 402 | writel(readl(&i2c->iiccon) & |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 403 | ~I2CCON_ACKGEN, |
| 404 | &i2c->iiccon); |
| 405 | ReadWriteByte(i2c); |
| 406 | result = WaitForXfer(i2c); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 407 | data[i] = readl(&i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 408 | i++; |
| 409 | } |
| 410 | } else { |
| 411 | result = I2C_NACK; |
| 412 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 413 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 414 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 415 | /* send STOP */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 416 | writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 417 | ReadWriteByte(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 418 | break; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 419 | |
| 420 | default: |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 421 | debug("i2c_transfer: bad call\n"); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 422 | result = I2C_NOK; |
| 423 | break; |
| 424 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 425 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 426 | return result; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 427 | } |
| 428 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 429 | int i2c_probe(uchar chip) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 430 | { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 431 | struct s3c24x0_i2c *i2c; |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 432 | uchar buf[1]; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 433 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 434 | i2c = get_base_i2c(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 435 | buf[0] = 0; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 436 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 437 | /* |
| 438 | * What is needed is to send the chip address and verify that the |
| 439 | * address was <ACK>ed (i.e. there was a chip at that address which |
| 440 | * drove the data line low). |
| 441 | */ |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 442 | return i2c_transfer(i2c, I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 443 | } |
| 444 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 445 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 446 | { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 447 | struct s3c24x0_i2c *i2c; |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 448 | uchar xaddr[4]; |
| 449 | int ret; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 450 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 451 | if (alen > 4) { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 452 | debug("I2C read: addr len %d not supported\n", alen); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 453 | return 1; |
| 454 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 455 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 456 | 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 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 462 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 463 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 464 | /* |
| 465 | * EEPROM chips that implement "address overflow" are ones |
| 466 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 467 | * address and the extra bits end up in the "chip address" |
| 468 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 469 | * four 256 byte chips. |
| 470 | * |
| 471 | * Note that we consider the length of the address field to |
| 472 | * still be one byte because the extra address bits are |
| 473 | * hidden in the chip address. |
| 474 | */ |
| 475 | if (alen > 0) |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 476 | chip |= ((addr >> (alen * 8)) & |
| 477 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 478 | #endif |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 479 | i2c = get_base_i2c(); |
| 480 | ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen, |
| 481 | buffer, len); |
| 482 | if (ret != 0) { |
| 483 | debug("I2c read: failed %d\n", ret); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 484 | return 1; |
| 485 | } |
| 486 | return 0; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 487 | } |
| 488 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 489 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 490 | { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 491 | struct s3c24x0_i2c *i2c; |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 492 | uchar xaddr[4]; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 493 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 494 | if (alen > 4) { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 495 | debug("I2C write: addr len %d not supported\n", alen); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 496 | return 1; |
| 497 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 498 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 499 | if (alen > 0) { |
| 500 | xaddr[0] = (addr >> 24) & 0xFF; |
| 501 | xaddr[1] = (addr >> 16) & 0xFF; |
| 502 | xaddr[2] = (addr >> 8) & 0xFF; |
| 503 | xaddr[3] = addr & 0xFF; |
| 504 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 505 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 506 | /* |
| 507 | * EEPROM chips that implement "address overflow" are ones |
| 508 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 509 | * address and the extra bits end up in the "chip address" |
| 510 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 511 | * four 256 byte chips. |
| 512 | * |
| 513 | * Note that we consider the length of the address field to |
| 514 | * still be one byte because the extra address bits are |
| 515 | * hidden in the chip address. |
| 516 | */ |
| 517 | if (alen > 0) |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 518 | chip |= ((addr >> (alen * 8)) & |
| 519 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 520 | #endif |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 521 | i2c = get_base_i2c(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 522 | return (i2c_transfer |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 523 | (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer, |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 524 | len) != 0); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 525 | } |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 526 | |
| 527 | #ifdef CONFIG_OF_CONTROL |
| 528 | void board_i2c_init(const void *blob) |
| 529 | { |
| 530 | int node_list[CONFIG_MAX_I2C_NUM]; |
| 531 | int count, i; |
| 532 | |
| 533 | count = fdtdec_find_aliases_for_id(blob, "i2c", |
| 534 | COMPAT_SAMSUNG_S3C2440_I2C, node_list, |
| 535 | CONFIG_MAX_I2C_NUM); |
| 536 | |
| 537 | for (i = 0; i < count; i++) { |
| 538 | struct s3c24x0_i2c_bus *bus; |
| 539 | int node = node_list[i]; |
| 540 | |
| 541 | if (node <= 0) |
| 542 | continue; |
| 543 | bus = &i2c_bus[i]; |
| 544 | bus->regs = (struct s3c24x0_i2c *) |
| 545 | fdtdec_get_addr(blob, node, "reg"); |
| 546 | bus->id = pinmux_decode_periph_id(blob, node); |
| 547 | bus->node = node; |
| 548 | bus->bus_num = i2c_busses++; |
| 549 | exynos_pinmux_config(bus->id, 0); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx) |
| 554 | { |
| 555 | if (bus_idx < i2c_busses) |
| 556 | return &i2c_bus[bus_idx]; |
| 557 | |
| 558 | debug("Undefined bus: %d\n", bus_idx); |
| 559 | return NULL; |
| 560 | } |
| 561 | |
| 562 | int i2c_get_bus_num_fdt(int node) |
| 563 | { |
| 564 | int i; |
| 565 | |
| 566 | for (i = 0; i < i2c_busses; i++) { |
| 567 | if (node == i2c_bus[i].node) |
| 568 | return i; |
| 569 | } |
| 570 | |
| 571 | debug("%s: Can't find any matched I2C bus\n", __func__); |
| 572 | return -1; |
| 573 | } |
| 574 | |
| 575 | int i2c_reset_port_fdt(const void *blob, int node) |
| 576 | { |
| 577 | struct s3c24x0_i2c_bus *i2c; |
| 578 | int bus; |
| 579 | |
| 580 | bus = i2c_get_bus_num_fdt(node); |
| 581 | if (bus < 0) { |
| 582 | debug("could not get bus for node %d\n", node); |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | i2c = get_bus(bus); |
| 587 | if (!i2c) { |
| 588 | debug("get_bus() failed for node node %d\n", node); |
| 589 | return -1; |
| 590 | } |
| 591 | |
| 592 | i2c_ch_init(i2c->regs, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | #endif |
| 597 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 598 | #endif /* CONFIG_HARD_I2C */ |