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