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; |
Rajeshwari Shinde | e076adf | 2013-02-19 02:19:45 +0000 | [diff] [blame] | 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 */ |
Rajeshwari Shinde | e076adf | 2013-02-19 02:19:45 +0000 | [diff] [blame] | 339 | writel(I2C_MODE_MT | 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(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 346 | /* send START */ |
Rajeshwari Shinde | e076adf | 2013-02-19 02:19:45 +0000 | [diff] [blame] | 347 | writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP, |
| 348 | &i2c->iicstat); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 349 | result = WaitForXfer(i2c); |
| 350 | if (IsACK(i2c)) { |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 351 | i = 0; |
| 352 | while ((i < addr_len) && (result == I2C_OK)) { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 353 | writel(addr[i], &i2c->iicds); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 354 | ReadWriteByte(i2c); |
| 355 | result = WaitForXfer(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 356 | i++; |
| 357 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 358 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 359 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 360 | /* resend START */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 361 | writel(I2C_MODE_MR | I2C_TXRX_ENA | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 362 | I2C_START_STOP, &i2c->iicstat); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 363 | ReadWriteByte(i2c); |
| 364 | result = WaitForXfer(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 365 | i = 0; |
| 366 | while ((i < data_len) && (result == I2C_OK)) { |
| 367 | /* disable ACK for final READ */ |
| 368 | if (i == data_len - 1) |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 369 | writel(readl(&i2c->iiccon) |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 370 | & ~I2CCON_ACKGEN, |
| 371 | &i2c->iiccon); |
| 372 | ReadWriteByte(i2c); |
| 373 | result = WaitForXfer(i2c); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 374 | data[i] = readl(&i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 375 | i++; |
| 376 | } |
| 377 | } else { |
| 378 | result = I2C_NACK; |
| 379 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 380 | |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 381 | } else { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 382 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 383 | /* send START */ |
Rajeshwari Shinde | e076adf | 2013-02-19 02:19:45 +0000 | [diff] [blame] | 384 | writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP, |
| 385 | &i2c->iicstat); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 386 | result = WaitForXfer(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 387 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 388 | if (IsACK(i2c)) { |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 389 | i = 0; |
| 390 | while ((i < data_len) && (result == I2C_OK)) { |
| 391 | /* disable ACK for final READ */ |
| 392 | if (i == data_len - 1) |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 393 | writel(readl(&i2c->iiccon) & |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 394 | ~I2CCON_ACKGEN, |
| 395 | &i2c->iiccon); |
| 396 | ReadWriteByte(i2c); |
| 397 | result = WaitForXfer(i2c); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 398 | data[i] = readl(&i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 399 | i++; |
| 400 | } |
| 401 | } else { |
| 402 | result = I2C_NACK; |
| 403 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 404 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 405 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 406 | /* send STOP */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 407 | writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat); |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 408 | ReadWriteByte(i2c); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 409 | break; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 410 | |
| 411 | default: |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 412 | debug("i2c_transfer: bad call\n"); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 413 | result = I2C_NOK; |
| 414 | break; |
| 415 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 416 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 417 | return result; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 418 | } |
| 419 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 420 | int i2c_probe(uchar chip) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 421 | { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 422 | struct s3c24x0_i2c *i2c; |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 423 | uchar buf[1]; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 424 | |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 425 | i2c = get_base_i2c(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 426 | buf[0] = 0; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 427 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 428 | /* |
| 429 | * What is needed is to send the chip address and verify that the |
| 430 | * address was <ACK>ed (i.e. there was a chip at that address which |
| 431 | * drove the data line low). |
| 432 | */ |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 433 | 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] | 434 | } |
| 435 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 436 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 437 | { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 438 | struct s3c24x0_i2c *i2c; |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 439 | uchar xaddr[4]; |
| 440 | int ret; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 441 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 442 | if (alen > 4) { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 443 | debug("I2C read: addr len %d not supported\n", alen); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 444 | return 1; |
| 445 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 446 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 447 | if (alen > 0) { |
| 448 | xaddr[0] = (addr >> 24) & 0xFF; |
| 449 | xaddr[1] = (addr >> 16) & 0xFF; |
| 450 | xaddr[2] = (addr >> 8) & 0xFF; |
| 451 | xaddr[3] = addr & 0xFF; |
| 452 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 453 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 454 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 455 | /* |
| 456 | * EEPROM chips that implement "address overflow" are ones |
| 457 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 458 | * address and the extra bits end up in the "chip address" |
| 459 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 460 | * four 256 byte chips. |
| 461 | * |
| 462 | * Note that we consider the length of the address field to |
| 463 | * still be one byte because the extra address bits are |
| 464 | * hidden in the chip address. |
| 465 | */ |
| 466 | if (alen > 0) |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 467 | chip |= ((addr >> (alen * 8)) & |
| 468 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 469 | #endif |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 470 | i2c = get_base_i2c(); |
| 471 | ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen, |
| 472 | buffer, len); |
| 473 | if (ret != 0) { |
| 474 | debug("I2c read: failed %d\n", ret); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 475 | return 1; |
| 476 | } |
| 477 | return 0; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 478 | } |
| 479 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 480 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 481 | { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 482 | struct s3c24x0_i2c *i2c; |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 483 | uchar xaddr[4]; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 484 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 485 | if (alen > 4) { |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 486 | debug("I2C write: addr len %d not supported\n", alen); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 487 | return 1; |
| 488 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 489 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 490 | if (alen > 0) { |
| 491 | xaddr[0] = (addr >> 24) & 0xFF; |
| 492 | xaddr[1] = (addr >> 16) & 0xFF; |
| 493 | xaddr[2] = (addr >> 8) & 0xFF; |
| 494 | xaddr[3] = addr & 0xFF; |
| 495 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 496 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 497 | /* |
| 498 | * EEPROM chips that implement "address overflow" are ones |
| 499 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 500 | * address and the extra bits end up in the "chip address" |
| 501 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 502 | * four 256 byte chips. |
| 503 | * |
| 504 | * Note that we consider the length of the address field to |
| 505 | * still be one byte because the extra address bits are |
| 506 | * hidden in the chip address. |
| 507 | */ |
| 508 | if (alen > 0) |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 509 | chip |= ((addr >> (alen * 8)) & |
| 510 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 511 | #endif |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 512 | i2c = get_base_i2c(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 513 | return (i2c_transfer |
Rajeshwari Shinde | 4b4480a | 2012-07-23 21:23:53 +0000 | [diff] [blame] | 514 | (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer, |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 515 | len) != 0); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 516 | } |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 517 | |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 518 | void board_i2c_init(const void *blob) |
| 519 | { |
Amar | 8733d75 | 2013-04-04 02:27:06 -0400 | [diff] [blame] | 520 | int i; |
| 521 | #ifdef CONFIG_OF_CONTROL |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 522 | int node_list[CONFIG_MAX_I2C_NUM]; |
Amar | 8733d75 | 2013-04-04 02:27:06 -0400 | [diff] [blame] | 523 | int count; |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 524 | |
| 525 | count = fdtdec_find_aliases_for_id(blob, "i2c", |
| 526 | COMPAT_SAMSUNG_S3C2440_I2C, node_list, |
| 527 | CONFIG_MAX_I2C_NUM); |
| 528 | |
| 529 | for (i = 0; i < count; i++) { |
| 530 | struct s3c24x0_i2c_bus *bus; |
| 531 | int node = node_list[i]; |
| 532 | |
| 533 | if (node <= 0) |
| 534 | continue; |
| 535 | bus = &i2c_bus[i]; |
| 536 | bus->regs = (struct s3c24x0_i2c *) |
| 537 | fdtdec_get_addr(blob, node, "reg"); |
| 538 | bus->id = pinmux_decode_periph_id(blob, node); |
| 539 | bus->node = node; |
| 540 | bus->bus_num = i2c_busses++; |
| 541 | exynos_pinmux_config(bus->id, 0); |
| 542 | } |
Amar | 8733d75 | 2013-04-04 02:27:06 -0400 | [diff] [blame] | 543 | #else |
| 544 | for (i = 0; i < CONFIG_MAX_I2C_NUM; i++) { |
| 545 | exynos_pinmux_config((PERIPH_ID_I2C0 + i), |
| 546 | PINMUX_FLAG_NONE); |
| 547 | } |
| 548 | #endif |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Amar | 8733d75 | 2013-04-04 02:27:06 -0400 | [diff] [blame] | 551 | #ifdef CONFIG_OF_CONTROL |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 552 | static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx) |
| 553 | { |
| 554 | if (bus_idx < i2c_busses) |
| 555 | return &i2c_bus[bus_idx]; |
| 556 | |
| 557 | debug("Undefined bus: %d\n", bus_idx); |
| 558 | return NULL; |
| 559 | } |
| 560 | |
| 561 | int i2c_get_bus_num_fdt(int node) |
| 562 | { |
| 563 | int i; |
| 564 | |
| 565 | for (i = 0; i < i2c_busses; i++) { |
| 566 | if (node == i2c_bus[i].node) |
| 567 | return i; |
| 568 | } |
| 569 | |
| 570 | debug("%s: Can't find any matched I2C bus\n", __func__); |
| 571 | return -1; |
| 572 | } |
| 573 | |
| 574 | int i2c_reset_port_fdt(const void *blob, int node) |
| 575 | { |
| 576 | struct s3c24x0_i2c_bus *i2c; |
| 577 | int bus; |
| 578 | |
| 579 | bus = i2c_get_bus_num_fdt(node); |
| 580 | if (bus < 0) { |
| 581 | debug("could not get bus for node %d\n", node); |
| 582 | return -1; |
| 583 | } |
| 584 | |
| 585 | i2c = get_bus(bus); |
| 586 | if (!i2c) { |
| 587 | debug("get_bus() failed for node node %d\n", node); |
| 588 | return -1; |
| 589 | } |
| 590 | |
| 591 | i2c_ch_init(i2c->regs, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | #endif |
| 596 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 597 | #endif /* CONFIG_HARD_I2C */ |