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> |
kevin.morfitt@fearnside-systems.co.uk | e0d8131 | 2009-11-17 18:30:34 +0900 | [diff] [blame] | 30 | #include <asm/arch/s3c24x0_cpu.h> |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 31 | |
| 32 | #include <asm/io.h> |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 33 | #include <i2c.h> |
| 34 | |
| 35 | #ifdef CONFIG_HARD_I2C |
| 36 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 37 | #define I2C_WRITE 0 |
| 38 | #define I2C_READ 1 |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 39 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 40 | #define I2C_OK 0 |
| 41 | #define I2C_NOK 1 |
| 42 | #define I2C_NACK 2 |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 43 | #define I2C_NOK_LA 3 /* Lost arbitration */ |
| 44 | #define I2C_NOK_TOUT 4 /* time out */ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 45 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 46 | #define I2CSTAT_BSY 0x20 /* Busy bit */ |
| 47 | #define I2CSTAT_NACK 0x01 /* Nack bit */ |
| 48 | #define I2CCON_IRPND 0x10 /* Interrupt pending bit */ |
| 49 | #define I2C_MODE_MT 0xC0 /* Master Transmit Mode */ |
| 50 | #define I2C_MODE_MR 0x80 /* Master Receive Mode */ |
| 51 | #define I2C_START_STOP 0x20 /* START / STOP */ |
| 52 | #define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 53 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 54 | #define I2C_TIMEOUT 1 /* 1 second */ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 55 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 56 | static int GetI2CSDA(void) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 57 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 58 | struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 59 | |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 60 | #ifdef CONFIG_S3C2410 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 61 | return (readl(&gpio->gpedat) & 0x8000) >> 15; |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 62 | #endif |
| 63 | #ifdef CONFIG_S3C2400 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 64 | return (readl(&gpio->pgdat) & 0x0020) >> 5; |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 65 | #endif |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 66 | } |
| 67 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 68 | #if 0 |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 69 | static void SetI2CSDA(int x) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 70 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 71 | rGPEDAT = (rGPEDAT & ~0x8000) | (x & 1) << 15; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 72 | } |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 73 | #endif |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 74 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 75 | static void SetI2CSCL(int x) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 76 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 77 | struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 78 | |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 79 | #ifdef CONFIG_S3C2410 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 80 | writel((readl(&gpio->gpedat) & ~0x4000) | (x & 1) << 14, &gpio->gpedat); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 81 | #endif |
| 82 | #ifdef CONFIG_S3C2400 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 83 | writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 84 | #endif |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 85 | } |
| 86 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 87 | static int WaitForXfer(void) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 88 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 89 | struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c(); |
| 90 | int i; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 91 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 92 | i = I2C_TIMEOUT * 10000; |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 93 | while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 94 | udelay(100); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 95 | i--; |
| 96 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 97 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 98 | return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 99 | } |
| 100 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 101 | static int IsACK(void) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 102 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 103 | struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 104 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 105 | return !(readl(&i2c->iicstat) & I2CSTAT_NACK); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 106 | } |
| 107 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 108 | static void ReadWriteByte(void) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 109 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 110 | struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c(); |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 111 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 112 | writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 113 | } |
| 114 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 115 | void i2c_init(int speed, int slaveadd) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 116 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 117 | struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c(); |
| 118 | struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 119 | ulong freq, pres = 16, div; |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 120 | int i; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 121 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 122 | /* wait for some time to give previous transfer a chance to finish */ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 123 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 124 | i = I2C_TIMEOUT * 1000; |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 125 | while ((readl(&i2c->iicstat) && I2CSTAT_BSY) && (i > 0)) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 126 | udelay(1000); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 127 | i--; |
| 128 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 129 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 130 | if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) { |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 131 | #ifdef CONFIG_S3C2410 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 132 | ulong old_gpecon = readl(&gpio->gpecon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 133 | #endif |
| 134 | #ifdef CONFIG_S3C2400 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 135 | ulong old_gpecon = readl(&gpio->pgcon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 136 | #endif |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 137 | /* bus still busy probably by (most) previously interrupted |
| 138 | transfer */ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 139 | |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 140 | #ifdef CONFIG_S3C2410 |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 141 | /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 142 | writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000, |
| 143 | &gpio->gpecon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 144 | #endif |
| 145 | #ifdef CONFIG_S3C2400 |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 146 | /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 147 | writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000, |
| 148 | &gpio->pgcon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 149 | #endif |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 150 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 151 | /* toggle I2CSCL until bus idle */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 152 | SetI2CSCL(0); |
| 153 | udelay(1000); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 154 | i = 10; |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 155 | while ((i > 0) && (GetI2CSDA() != 1)) { |
| 156 | SetI2CSCL(1); |
| 157 | udelay(1000); |
| 158 | SetI2CSCL(0); |
| 159 | udelay(1000); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 160 | i--; |
| 161 | } |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 162 | SetI2CSCL(1); |
| 163 | udelay(1000); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 164 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 165 | /* restore pin functions */ |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 166 | #ifdef CONFIG_S3C2410 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 167 | writel(old_gpecon, &gpio->gpecon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 168 | #endif |
| 169 | #ifdef CONFIG_S3C2400 |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 170 | writel(old_gpecon, &gpio->pgcon); |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 171 | #endif |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 172 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 173 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 174 | /* calculate prescaler and divisor values */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 175 | freq = get_PCLK(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 176 | if ((freq / pres / (16 + 1)) > speed) |
| 177 | /* set prescaler to 512 */ |
| 178 | pres = 512; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 179 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 180 | div = 0; |
| 181 | while ((freq / pres / (div + 1)) > speed) |
| 182 | div++; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 183 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 184 | /* set prescaler, divisor according to freq, also set |
| 185 | * ACKGEN, IRQ */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 186 | writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 187 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 188 | /* init to SLAVE REVEIVE and set slaveaddr */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 189 | writel(0, &i2c->iicstat); |
| 190 | writel(slaveadd, &i2c->iicadd); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 191 | /* program Master Transmit (and implicit STOP) */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 192 | writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 193 | |
| 194 | } |
| 195 | |
| 196 | /* |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 197 | * cmd_type is 0 for write, 1 for read. |
| 198 | * |
| 199 | * addr_len can take any value from 0-255, it is only limited |
| 200 | * by the char, we could make it larger if needed. If it is |
| 201 | * 0 we skip the address write cycle. |
| 202 | */ |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 203 | static |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 204 | int i2c_transfer(unsigned char cmd_type, |
| 205 | unsigned char chip, |
| 206 | unsigned char addr[], |
| 207 | unsigned char addr_len, |
| 208 | unsigned char data[], unsigned short data_len) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 209 | { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 210 | struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c(); |
| 211 | int i, result; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 212 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 213 | if (data == 0 || data_len == 0) { |
| 214 | /*Don't support data transfer of no length or to address 0 */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 215 | printf("i2c_transfer: bad call\n"); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 216 | return I2C_NOK; |
| 217 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 218 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 219 | /* Check I2C bus idle */ |
| 220 | i = I2C_TIMEOUT * 1000; |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 221 | while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 222 | udelay(1000); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 223 | i--; |
| 224 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 225 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 226 | if (readl(&i2c->iicstat) & I2CSTAT_BSY) |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 227 | return I2C_NOK_TOUT; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 228 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 229 | writel(readl(&i2c->iiccon) | 0x80, &i2c->iiccon); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 230 | result = I2C_OK; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 231 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 232 | switch (cmd_type) { |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 233 | case I2C_WRITE: |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 234 | if (addr && addr_len) { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 235 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 236 | /* send START */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 237 | writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP, |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 238 | &i2c->iicstat); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 239 | i = 0; |
| 240 | while ((i < addr_len) && (result == I2C_OK)) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 241 | result = WaitForXfer(); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 242 | writel(addr[i], &i2c->iicds); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 243 | ReadWriteByte(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 244 | i++; |
| 245 | } |
| 246 | i = 0; |
| 247 | while ((i < data_len) && (result == I2C_OK)) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 248 | result = WaitForXfer(); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 249 | writel(data[i], &i2c->iicds); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 250 | ReadWriteByte(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 251 | i++; |
| 252 | } |
| 253 | } else { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 254 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 255 | /* send START */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 256 | writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP, |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 257 | &i2c->iicstat); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 258 | i = 0; |
| 259 | while ((i < data_len) && (result = I2C_OK)) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 260 | result = WaitForXfer(); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 261 | writel(data[i], &i2c->iicds); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 262 | ReadWriteByte(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 263 | i++; |
| 264 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 265 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 266 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 267 | if (result == I2C_OK) |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 268 | result = WaitForXfer(); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 269 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 270 | /* send STOP */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 271 | writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 272 | ReadWriteByte(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 273 | break; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 274 | |
wdenk | 7539dea | 2003-06-19 23:01:32 +0000 | [diff] [blame] | 275 | case I2C_READ: |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 276 | if (addr && addr_len) { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 277 | writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat); |
| 278 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 279 | /* send START */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 280 | writel(readl(&i2c->iicstat) | I2C_START_STOP, |
| 281 | &i2c->iicstat); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 282 | result = WaitForXfer(); |
| 283 | if (IsACK()) { |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 284 | i = 0; |
| 285 | while ((i < addr_len) && (result == I2C_OK)) { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 286 | writel(addr[i], &i2c->iicds); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 287 | ReadWriteByte(); |
| 288 | result = WaitForXfer(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 289 | i++; |
| 290 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 291 | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 292 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 293 | /* resend START */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 294 | writel(I2C_MODE_MR | I2C_TXRX_ENA | |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 295 | I2C_START_STOP, &i2c->iicstat); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 296 | ReadWriteByte(); |
| 297 | result = WaitForXfer(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 298 | i = 0; |
| 299 | while ((i < data_len) && (result == I2C_OK)) { |
| 300 | /* disable ACK for final READ */ |
| 301 | if (i == data_len - 1) |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 302 | writel(readl(&i2c->iiccon) |
| 303 | & ~0x80, &i2c->iiccon); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 304 | ReadWriteByte(); |
| 305 | result = WaitForXfer(); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 306 | data[i] = readl(&i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 307 | i++; |
| 308 | } |
| 309 | } else { |
| 310 | result = I2C_NACK; |
| 311 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 312 | |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 313 | } else { |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 314 | writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat); |
| 315 | writel(chip, &i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 316 | /* send START */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 317 | writel(readl(&i2c->iicstat) | I2C_START_STOP, |
| 318 | &i2c->iicstat); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 319 | result = WaitForXfer(); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 320 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 321 | if (IsACK()) { |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 322 | i = 0; |
| 323 | while ((i < data_len) && (result == I2C_OK)) { |
| 324 | /* disable ACK for final READ */ |
| 325 | if (i == data_len - 1) |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 326 | writel(readl(&i2c->iiccon) & |
| 327 | ~0x80, &i2c->iiccon); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 328 | ReadWriteByte(); |
| 329 | result = WaitForXfer(); |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 330 | data[i] = readl(&i2c->iicds); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 331 | i++; |
| 332 | } |
| 333 | } else { |
| 334 | result = I2C_NACK; |
| 335 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 336 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 337 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 338 | /* send STOP */ |
C Nauman | 383c43e | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 339 | writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat); |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 340 | ReadWriteByte(); |
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 | |
| 343 | default: |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 344 | printf("i2c_transfer: bad call\n"); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 345 | result = I2C_NOK; |
| 346 | break; |
| 347 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 348 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 349 | return (result); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 350 | } |
| 351 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 352 | int i2c_probe(uchar chip) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 353 | { |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 354 | uchar buf[1]; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 355 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 356 | buf[0] = 0; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 357 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 358 | /* |
| 359 | * What is needed is to send the chip address and verify that the |
| 360 | * address was <ACK>ed (i.e. there was a chip at that address which |
| 361 | * drove the data line low). |
| 362 | */ |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 363 | return i2c_transfer(I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 364 | } |
| 365 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 366 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 367 | { |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 368 | uchar xaddr[4]; |
| 369 | int ret; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 370 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 371 | if (alen > 4) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 372 | printf("I2C read: addr len %d not supported\n", alen); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 373 | return 1; |
| 374 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 375 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 376 | if (alen > 0) { |
| 377 | xaddr[0] = (addr >> 24) & 0xFF; |
| 378 | xaddr[1] = (addr >> 16) & 0xFF; |
| 379 | xaddr[2] = (addr >> 8) & 0xFF; |
| 380 | xaddr[3] = addr & 0xFF; |
| 381 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 382 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 383 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 384 | /* |
| 385 | * EEPROM chips that implement "address overflow" are ones |
| 386 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 387 | * address and the extra bits end up in the "chip address" |
| 388 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 389 | * four 256 byte chips. |
| 390 | * |
| 391 | * Note that we consider the length of the address field to |
| 392 | * still be one byte because the extra address bits are |
| 393 | * hidden in the chip address. |
| 394 | */ |
| 395 | if (alen > 0) |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 396 | chip |= ((addr >> (alen * 8)) & |
| 397 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 398 | #endif |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 399 | if ((ret = |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 400 | i2c_transfer(I2C_READ, chip << 1, &xaddr[4 - alen], alen, |
| 401 | buffer, len)) != 0) { |
| 402 | printf("I2c read: failed %d\n", ret); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 403 | return 1; |
| 404 | } |
| 405 | return 0; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 406 | } |
| 407 | |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 408 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len) |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 409 | { |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 410 | uchar xaddr[4]; |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 411 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 412 | if (alen > 4) { |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 413 | printf("I2C write: addr len %d not supported\n", alen); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 414 | return 1; |
| 415 | } |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 416 | |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 417 | if (alen > 0) { |
| 418 | xaddr[0] = (addr >> 24) & 0xFF; |
| 419 | xaddr[1] = (addr >> 16) & 0xFF; |
| 420 | xaddr[2] = (addr >> 8) & 0xFF; |
| 421 | xaddr[3] = addr & 0xFF; |
| 422 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 423 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 424 | /* |
| 425 | * EEPROM chips that implement "address overflow" are ones |
| 426 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 427 | * address and the extra bits end up in the "chip address" |
| 428 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 429 | * four 256 byte chips. |
| 430 | * |
| 431 | * Note that we consider the length of the address field to |
| 432 | * still be one byte because the extra address bits are |
| 433 | * hidden in the chip address. |
| 434 | */ |
| 435 | if (alen > 0) |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 436 | chip |= ((addr >> (alen * 8)) & |
| 437 | CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 438 | #endif |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 439 | return (i2c_transfer |
| 440 | (I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer, |
| 441 | len) != 0); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 442 | } |
kevin.morfitt@fearnside-systems.co.uk | 1464f4d | 2009-10-10 13:33:11 +0900 | [diff] [blame] | 443 | #endif /* CONFIG_HARD_I2C */ |