Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 1 | /* |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 2 | * Driver for the TWSI (i2c) controller found on the Marvell |
| 3 | * orion5x and kirkwood SoC families. |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 4 | * |
Albert ARIBAUD | 340983d | 2011-04-22 19:41:02 +0200 | [diff] [blame] | 5 | * Author: Albert Aribaud <albert.u.boot@aribaud.net> |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 6 | * Copyright (c) 2010 Albert Aribaud. |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 7 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 9 | */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 10 | |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <i2c.h> |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 13 | #include <asm/errno.h> |
| 14 | #include <asm/io.h> |
| 15 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 16 | /* |
| 17 | * include a file that will provide CONFIG_I2C_MVTWSI_BASE |
| 18 | * and possibly other settings |
| 19 | */ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 20 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 21 | #if defined(CONFIG_ORION5X) |
| 22 | #include <asm/arch/orion5x.h> |
Stefan Roese | 54cd2b2 | 2014-10-22 12:13:16 +0200 | [diff] [blame] | 23 | #elif (defined(CONFIG_KIRKWOOD) || defined(CONFIG_ARMADA_XP)) |
Stefan Roese | c243784 | 2014-10-22 12:13:06 +0200 | [diff] [blame] | 24 | #include <asm/arch/soc.h> |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 25 | #elif defined(CONFIG_SUNXI) |
| 26 | #include <asm/arch/i2c.h> |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 27 | #else |
| 28 | #error Driver mvtwsi not supported by SoC or board |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 29 | #endif |
| 30 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 31 | /* |
| 32 | * TWSI register structure |
| 33 | */ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 34 | |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 35 | #ifdef CONFIG_SUNXI |
| 36 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 37 | struct mvtwsi_registers { |
| 38 | u32 slave_address; |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 39 | u32 xtnd_slave_addr; |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 40 | u32 data; |
| 41 | u32 control; |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 42 | u32 status; |
| 43 | u32 baudrate; |
| 44 | u32 soft_reset; |
| 45 | }; |
| 46 | |
| 47 | #else |
| 48 | |
| 49 | struct mvtwsi_registers { |
| 50 | u32 slave_address; |
| 51 | u32 data; |
| 52 | u32 control; |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 53 | union { |
| 54 | u32 status; /* when reading */ |
| 55 | u32 baudrate; /* when writing */ |
| 56 | }; |
| 57 | u32 xtnd_slave_addr; |
| 58 | u32 reserved[2]; |
| 59 | u32 soft_reset; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 62 | #endif |
| 63 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 64 | /* |
| 65 | * Control register fields |
| 66 | */ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 67 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 68 | #define MVTWSI_CONTROL_ACK 0x00000004 |
| 69 | #define MVTWSI_CONTROL_IFLG 0x00000008 |
| 70 | #define MVTWSI_CONTROL_STOP 0x00000010 |
| 71 | #define MVTWSI_CONTROL_START 0x00000020 |
| 72 | #define MVTWSI_CONTROL_TWSIEN 0x00000040 |
| 73 | #define MVTWSI_CONTROL_INTEN 0x00000080 |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 74 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 75 | /* |
| 76 | * Status register values -- only those expected in normal master |
| 77 | * operation on non-10-bit-address devices; whatever status we don't |
| 78 | * expect in nominal conditions (bus errors, arbitration losses, |
| 79 | * missing ACKs...) we just pass back to the caller as an error |
| 80 | * code. |
| 81 | */ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 82 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 83 | #define MVTWSI_STATUS_START 0x08 |
| 84 | #define MVTWSI_STATUS_REPEATED_START 0x10 |
| 85 | #define MVTWSI_STATUS_ADDR_W_ACK 0x18 |
| 86 | #define MVTWSI_STATUS_DATA_W_ACK 0x28 |
| 87 | #define MVTWSI_STATUS_ADDR_R_ACK 0x40 |
| 88 | #define MVTWSI_STATUS_ADDR_R_NAK 0x48 |
| 89 | #define MVTWSI_STATUS_DATA_R_ACK 0x50 |
| 90 | #define MVTWSI_STATUS_DATA_R_NAK 0x58 |
| 91 | #define MVTWSI_STATUS_IDLE 0xF8 |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 92 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 93 | /* |
| 94 | * The single instance of the controller we'll be dealing with |
| 95 | */ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 96 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 97 | static struct mvtwsi_registers *twsi = |
| 98 | (struct mvtwsi_registers *) CONFIG_I2C_MVTWSI_BASE; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 99 | |
| 100 | /* |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 101 | * Returned statuses are 0 for success and nonzero otherwise. |
| 102 | * Currently, cmd_i2c and cmd_eeprom do not interpret an error status. |
| 103 | * Thus to ease debugging, the return status contains some debug info: |
| 104 | * - bits 31..24 are error class: 1 is timeout, 2 is 'status mismatch'. |
| 105 | * - bits 23..16 are the last value of the control register. |
| 106 | * - bits 15..8 are the last value of the status register. |
| 107 | * - bits 7..0 are the expected value of the status register. |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 108 | */ |
| 109 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 110 | #define MVTWSI_ERROR_WRONG_STATUS 0x01 |
| 111 | #define MVTWSI_ERROR_TIMEOUT 0x02 |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 112 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 113 | #define MVTWSI_ERROR(ec, lc, ls, es) (((ec << 24) & 0xFF000000) | \ |
| 114 | ((lc << 16) & 0x00FF0000) | ((ls<<8) & 0x0000FF00) | (es & 0xFF)) |
| 115 | |
| 116 | /* |
| 117 | * Wait for IFLG to raise, or return 'timeout'; then if status is as expected, |
| 118 | * return 0 (ok) or return 'wrong status'. |
| 119 | */ |
| 120 | static int twsi_wait(int expected_status) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 121 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 122 | int control, status; |
| 123 | int timeout = 1000; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 124 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 125 | do { |
| 126 | control = readl(&twsi->control); |
| 127 | if (control & MVTWSI_CONTROL_IFLG) { |
| 128 | status = readl(&twsi->status); |
| 129 | if (status == expected_status) |
| 130 | return 0; |
| 131 | else |
| 132 | return MVTWSI_ERROR( |
| 133 | MVTWSI_ERROR_WRONG_STATUS, |
| 134 | control, status, expected_status); |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 135 | } |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 136 | udelay(10); /* one clock cycle at 100 kHz */ |
| 137 | } while (timeout--); |
| 138 | status = readl(&twsi->status); |
| 139 | return MVTWSI_ERROR( |
| 140 | MVTWSI_ERROR_TIMEOUT, control, status, expected_status); |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 143 | /* |
| 144 | * These flags are ORed to any write to the control register |
| 145 | * They allow global setting of TWSIEN and ACK. |
| 146 | * By default none are set. |
| 147 | * twsi_start() sets TWSIEN (in case the controller was disabled) |
| 148 | * twsi_recv() sets ACK or resets it depending on expected status. |
| 149 | */ |
| 150 | static u8 twsi_control_flags = MVTWSI_CONTROL_TWSIEN; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 151 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 152 | /* |
| 153 | * Assert the START condition, either in a single I2C transaction |
| 154 | * or inside back-to-back ones (repeated starts). |
| 155 | */ |
| 156 | static int twsi_start(int expected_status) |
| 157 | { |
| 158 | /* globally set TWSIEN in case it was not */ |
| 159 | twsi_control_flags |= MVTWSI_CONTROL_TWSIEN; |
| 160 | /* assert START */ |
| 161 | writel(twsi_control_flags | MVTWSI_CONTROL_START, &twsi->control); |
| 162 | /* wait for controller to process START */ |
| 163 | return twsi_wait(expected_status); |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 166 | /* |
| 167 | * Send a byte (i2c address or data). |
| 168 | */ |
| 169 | static int twsi_send(u8 byte, int expected_status) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 170 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 171 | /* put byte in data register for sending */ |
| 172 | writel(byte, &twsi->data); |
| 173 | /* clear any pending interrupt -- that'll cause sending */ |
| 174 | writel(twsi_control_flags, &twsi->control); |
| 175 | /* wait for controller to receive byte and check ACK */ |
| 176 | return twsi_wait(expected_status); |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 179 | /* |
| 180 | * Receive a byte. |
| 181 | * Global mvtwsi_control_flags variable says if we should ack or nak. |
| 182 | */ |
| 183 | static int twsi_recv(u8 *byte) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 184 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 185 | int expected_status, status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 186 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 187 | /* compute expected status based on ACK bit in global control flags */ |
| 188 | if (twsi_control_flags & MVTWSI_CONTROL_ACK) |
| 189 | expected_status = MVTWSI_STATUS_DATA_R_ACK; |
| 190 | else |
| 191 | expected_status = MVTWSI_STATUS_DATA_R_NAK; |
| 192 | /* acknowledge *previous state* and launch receive */ |
| 193 | writel(twsi_control_flags, &twsi->control); |
| 194 | /* wait for controller to receive byte and assert ACK or NAK */ |
| 195 | status = twsi_wait(expected_status); |
| 196 | /* if we did receive expected byte then store it */ |
| 197 | if (status == 0) |
| 198 | *byte = readl(&twsi->data); |
| 199 | /* return status */ |
| 200 | return status; |
| 201 | } |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 202 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 203 | /* |
| 204 | * Assert the STOP condition. |
| 205 | * This is also used to force the bus back in idle (SDA=SCL=1). |
| 206 | */ |
| 207 | static int twsi_stop(int status) |
| 208 | { |
| 209 | int control, stop_status; |
| 210 | int timeout = 1000; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 211 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 212 | /* assert STOP */ |
| 213 | control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP; |
| 214 | writel(control, &twsi->control); |
| 215 | /* wait for IDLE; IFLG won't rise so twsi_wait() is no use. */ |
| 216 | do { |
| 217 | stop_status = readl(&twsi->status); |
| 218 | if (stop_status == MVTWSI_STATUS_IDLE) |
| 219 | break; |
| 220 | udelay(10); /* one clock cycle at 100 kHz */ |
| 221 | } while (timeout--); |
| 222 | control = readl(&twsi->control); |
| 223 | if (stop_status != MVTWSI_STATUS_IDLE) |
| 224 | if (status == 0) |
| 225 | status = MVTWSI_ERROR( |
| 226 | MVTWSI_ERROR_TIMEOUT, |
| 227 | control, status, MVTWSI_STATUS_IDLE); |
| 228 | return status; |
| 229 | } |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 230 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 231 | /* |
| 232 | * Ugly formula to convert m and n values to a frequency comes from |
| 233 | * TWSI specifications |
| 234 | */ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 235 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 236 | #define TWSI_FREQUENCY(m, n) \ |
Hans de Goede | 162aab0 | 2014-05-03 17:46:26 +0200 | [diff] [blame] | 237 | (CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n))) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 238 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 239 | /* |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 240 | * Reset controller. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 241 | * Controller reset also resets the baud rate and slave address, so |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 242 | * they must be re-established afterwards. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 243 | */ |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 244 | static void twsi_reset(struct i2c_adapter *adap) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 245 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 246 | /* ensure controller will be enabled by any twsi*() function */ |
| 247 | twsi_control_flags = MVTWSI_CONTROL_TWSIEN; |
| 248 | /* reset controller */ |
| 249 | writel(0, &twsi->soft_reset); |
| 250 | /* wait 2 ms -- this is what the Marvell LSP does */ |
| 251 | udelay(20000); |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 254 | /* |
| 255 | * I2C init called by cmd_i2c when doing 'i2c reset'. |
| 256 | * Sets baud to the highest possible value not exceeding requested one. |
| 257 | */ |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 258 | static unsigned int twsi_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 259 | unsigned int requested_speed) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 260 | { |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 261 | unsigned int tmp_speed, highest_speed, n, m; |
| 262 | unsigned int baud = 0x44; /* baudrate at controller reset */ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 263 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 264 | /* use actual speed to collect progressively higher values */ |
| 265 | highest_speed = 0; |
| 266 | /* compute m, n setting for highest speed not above requested speed */ |
| 267 | for (n = 0; n < 8; n++) { |
| 268 | for (m = 0; m < 16; m++) { |
| 269 | tmp_speed = TWSI_FREQUENCY(m, n); |
| 270 | if ((tmp_speed <= requested_speed) |
| 271 | && (tmp_speed > highest_speed)) { |
| 272 | highest_speed = tmp_speed; |
| 273 | baud = (m << 3) | n; |
| 274 | } |
| 275 | } |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 276 | } |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 277 | writel(baud, &twsi->baudrate); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static void twsi_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd) |
| 282 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 283 | /* reset controller */ |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 284 | twsi_reset(adap); |
| 285 | /* set speed */ |
| 286 | twsi_i2c_set_bus_speed(adap, speed); |
| 287 | /* set slave address even though we don't use it */ |
| 288 | writel(slaveadd, &twsi->slave_address); |
| 289 | writel(0, &twsi->xtnd_slave_addr); |
| 290 | /* assert STOP but don't care for the result */ |
| 291 | (void) twsi_stop(0); |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 294 | /* |
| 295 | * Begin I2C transaction with expected start status, at given address. |
| 296 | * Common to i2c_probe, i2c_read and i2c_write. |
| 297 | * Expected address status will derive from direction bit (bit 0) in addr. |
| 298 | */ |
| 299 | static int i2c_begin(int expected_start_status, u8 addr) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 300 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 301 | int status, expected_addr_status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 302 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 303 | /* compute expected address status from direction bit in addr */ |
| 304 | if (addr & 1) /* reading */ |
| 305 | expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK; |
| 306 | else /* writing */ |
| 307 | expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK; |
| 308 | /* assert START */ |
| 309 | status = twsi_start(expected_start_status); |
| 310 | /* send out the address if the start went well */ |
| 311 | if (status == 0) |
| 312 | status = twsi_send(addr, expected_addr_status); |
| 313 | /* return ok or status of first failure to caller */ |
| 314 | return status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 315 | } |
| 316 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 317 | /* |
| 318 | * I2C probe called by cmd_i2c when doing 'i2c probe'. |
| 319 | * Begin read, nak data byte, end. |
| 320 | */ |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 321 | static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 322 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 323 | u8 dummy_byte; |
| 324 | int status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 325 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 326 | /* begin i2c read */ |
| 327 | status = i2c_begin(MVTWSI_STATUS_START, (chip << 1) | 1); |
| 328 | /* dummy read was accepted: receive byte but NAK it. */ |
| 329 | if (status == 0) |
| 330 | status = twsi_recv(&dummy_byte); |
| 331 | /* Stop transaction */ |
| 332 | twsi_stop(0); |
| 333 | /* return 0 or status of first failure */ |
| 334 | return status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 337 | /* |
| 338 | * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c |
| 339 | * Begin write, send address byte(s), begin read, receive data bytes, end. |
| 340 | * |
| 341 | * NOTE: some EEPROMS want a stop right before the second start, while |
| 342 | * some will choke if it is there. Deciding which we should do is eeprom |
| 343 | * stuff, not i2c, but at the moment the APIs won't let us put it in |
| 344 | * cmd_eeprom, so we have to choose here, and for the moment that'll be |
| 345 | * a repeated start without a preceding stop. |
| 346 | */ |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 347 | static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr, |
| 348 | int alen, uchar *data, int length) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 349 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 350 | int status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 351 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 352 | /* begin i2c write to send the address bytes */ |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 353 | status = i2c_begin(MVTWSI_STATUS_START, (chip << 1)); |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 354 | /* send addr bytes */ |
| 355 | while ((status == 0) && alen--) |
| 356 | status = twsi_send(addr >> (8*alen), |
| 357 | MVTWSI_STATUS_DATA_W_ACK); |
| 358 | /* begin i2c read to receive eeprom data bytes */ |
| 359 | if (status == 0) |
| 360 | status = i2c_begin( |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 361 | MVTWSI_STATUS_REPEATED_START, (chip << 1) | 1); |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 362 | /* prepare ACK if at least one byte must be received */ |
| 363 | if (length > 0) |
| 364 | twsi_control_flags |= MVTWSI_CONTROL_ACK; |
| 365 | /* now receive actual bytes */ |
| 366 | while ((status == 0) && length--) { |
| 367 | /* reset NAK if we if no more to read now */ |
| 368 | if (length == 0) |
| 369 | twsi_control_flags &= ~MVTWSI_CONTROL_ACK; |
| 370 | /* read current byte */ |
| 371 | status = twsi_recv(data++); |
| 372 | } |
| 373 | /* Stop transaction */ |
| 374 | status = twsi_stop(status); |
| 375 | /* return 0 or status of first failure */ |
| 376 | return status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 379 | /* |
| 380 | * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c |
| 381 | * Begin write, send address byte(s), send data bytes, end. |
| 382 | */ |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 383 | static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr, |
| 384 | int alen, uchar *data, int length) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 385 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 386 | int status; |
| 387 | |
| 388 | /* begin i2c write to send the eeprom adress bytes then data bytes */ |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 389 | status = i2c_begin(MVTWSI_STATUS_START, (chip << 1)); |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 390 | /* send addr bytes */ |
| 391 | while ((status == 0) && alen--) |
| 392 | status = twsi_send(addr >> (8*alen), |
| 393 | MVTWSI_STATUS_DATA_W_ACK); |
| 394 | /* send data bytes */ |
| 395 | while ((status == 0) && (length-- > 0)) |
| 396 | status = twsi_send(*(data++), MVTWSI_STATUS_DATA_W_ACK); |
| 397 | /* Stop transaction */ |
| 398 | status = twsi_stop(status); |
| 399 | /* return 0 or status of first failure */ |
| 400 | return status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 403 | U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe, |
| 404 | twsi_i2c_read, twsi_i2c_write, |
| 405 | twsi_i2c_set_bus_speed, |
| 406 | CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0) |