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