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