Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 2 | /* |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 3 | * Driver for the TWSI (i2c) controller found on the Marvell |
| 4 | * orion5x and kirkwood SoC families. |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 5 | * |
Albert ARIBAUD | 340983d | 2011-04-22 19:41:02 +0200 | [diff] [blame] | 6 | * Author: Albert Aribaud <albert.u.boot@aribaud.net> |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 7 | * Copyright (c) 2010 Albert Aribaud. |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 8 | */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 9 | |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 13 | #include <linux/errno.h> |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Baruch Siach | 91006c7 | 2018-06-07 12:38:10 +0300 | [diff] [blame] | 15 | #include <linux/bitops.h> |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 16 | #include <linux/compat.h> |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 17 | #ifdef CONFIG_DM_I2C |
| 18 | #include <dm.h> |
| 19 | #endif |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 22 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 23 | /* |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 24 | * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other |
| 25 | * settings |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 26 | */ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 27 | |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 28 | #ifndef CONFIG_DM_I2C |
Trevor Woerner | f995375 | 2020-05-06 08:02:38 -0400 | [diff] [blame] | 29 | #if defined(CONFIG_ARCH_ORION5X) |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 30 | #include <asm/arch/orion5x.h> |
Trevor Woerner | bb7ab07 | 2020-05-06 08:02:40 -0400 | [diff] [blame] | 31 | #elif (defined(CONFIG_ARCH_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU)) |
Stefan Roese | c243784 | 2014-10-22 12:13:06 +0200 | [diff] [blame] | 32 | #include <asm/arch/soc.h> |
Jagan Teki | 68078f7 | 2016-10-13 14:19:35 +0530 | [diff] [blame] | 33 | #elif defined(CONFIG_ARCH_SUNXI) |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 34 | #include <asm/arch/i2c.h> |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 35 | #else |
| 36 | #error Driver mvtwsi not supported by SoC or board |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 37 | #endif |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 38 | #endif /* CONFIG_DM_I2C */ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 39 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 40 | /* |
Jernej Skrabec | 9220d50 | 2017-04-27 00:03:36 +0200 | [diff] [blame] | 41 | * On SUNXI, we get CONFIG_SYS_TCLK from this include, so we want to |
| 42 | * always have it. |
| 43 | */ |
| 44 | #if defined(CONFIG_DM_I2C) && defined(CONFIG_ARCH_SUNXI) |
| 45 | #include <asm/arch/i2c.h> |
| 46 | #endif |
| 47 | |
| 48 | /* |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 49 | * TWSI register structure |
| 50 | */ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 51 | |
Jagan Teki | 68078f7 | 2016-10-13 14:19:35 +0530 | [diff] [blame] | 52 | #ifdef CONFIG_ARCH_SUNXI |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 53 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 54 | struct mvtwsi_registers { |
| 55 | u32 slave_address; |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 56 | u32 xtnd_slave_addr; |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 57 | u32 data; |
| 58 | u32 control; |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 59 | u32 status; |
| 60 | u32 baudrate; |
| 61 | u32 soft_reset; |
Baruch Siach | 91006c7 | 2018-06-07 12:38:10 +0300 | [diff] [blame] | 62 | u32 debug; /* Dummy field for build compatibility with mvebu */ |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | #else |
| 66 | |
| 67 | struct mvtwsi_registers { |
| 68 | u32 slave_address; |
| 69 | u32 data; |
| 70 | u32 control; |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 71 | union { |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 72 | u32 status; /* When reading */ |
| 73 | u32 baudrate; /* When writing */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 74 | }; |
| 75 | u32 xtnd_slave_addr; |
Baruch Siach | 91006c7 | 2018-06-07 12:38:10 +0300 | [diff] [blame] | 76 | u32 reserved0[2]; |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 77 | u32 soft_reset; |
Baruch Siach | 91006c7 | 2018-06-07 12:38:10 +0300 | [diff] [blame] | 78 | u32 reserved1[27]; |
| 79 | u32 debug; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 82 | #endif |
| 83 | |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 84 | #ifdef CONFIG_DM_I2C |
| 85 | struct mvtwsi_i2c_dev { |
| 86 | /* TWSI Register base for the device */ |
| 87 | struct mvtwsi_registers *base; |
| 88 | /* Number of the device (determined from cell-index property) */ |
| 89 | int index; |
| 90 | /* The I2C slave address for the device */ |
| 91 | u8 slaveadd; |
| 92 | /* The configured I2C speed in Hz */ |
| 93 | uint speed; |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 94 | /* The current length of a clock period (depending on speed) */ |
| 95 | uint tick; |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 96 | }; |
| 97 | #endif /* CONFIG_DM_I2C */ |
| 98 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 99 | /* |
mario.six@gdsys.cc | f43d3e9 | 2016-07-21 11:57:02 +0200 | [diff] [blame] | 100 | * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control |
| 101 | * register |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 102 | */ |
mario.six@gdsys.cc | f43d3e9 | 2016-07-21 11:57:02 +0200 | [diff] [blame] | 103 | enum mvtwsi_ctrl_register_fields { |
| 104 | /* Acknowledge bit */ |
| 105 | MVTWSI_CONTROL_ACK = 0x00000004, |
| 106 | /* Interrupt flag */ |
| 107 | MVTWSI_CONTROL_IFLG = 0x00000008, |
| 108 | /* Stop bit */ |
| 109 | MVTWSI_CONTROL_STOP = 0x00000010, |
| 110 | /* Start bit */ |
| 111 | MVTWSI_CONTROL_START = 0x00000020, |
| 112 | /* I2C enable */ |
| 113 | MVTWSI_CONTROL_TWSIEN = 0x00000040, |
| 114 | /* Interrupt enable */ |
| 115 | MVTWSI_CONTROL_INTEN = 0x00000080, |
| 116 | }; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 117 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 118 | /* |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 119 | * On sun6i and newer, IFLG is a write-clear bit, which is cleared by writing 1; |
| 120 | * on other platforms, it is a normal r/w bit, which is cleared by writing 0. |
Hans de Goede | 6b703e0 | 2016-01-14 14:06:25 +0100 | [diff] [blame] | 121 | */ |
| 122 | |
| 123 | #ifdef CONFIG_SUNXI_GEN_SUN6I |
| 124 | #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008 |
| 125 | #else |
| 126 | #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000 |
| 127 | #endif |
| 128 | |
| 129 | /* |
mario.six@gdsys.cc | f43d3e9 | 2016-07-21 11:57:02 +0200 | [diff] [blame] | 130 | * enum mvstwsi_status_values - Possible values of I2C controller's status |
| 131 | * register |
| 132 | * |
| 133 | * Only those statuses expected in normal master operation on |
| 134 | * non-10-bit-address devices are specified. |
| 135 | * |
| 136 | * Every status that's unexpected during normal operation (bus errors, |
| 137 | * arbitration losses, missing ACKs...) is passed back to the caller as an error |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 138 | * code. |
| 139 | */ |
mario.six@gdsys.cc | f43d3e9 | 2016-07-21 11:57:02 +0200 | [diff] [blame] | 140 | enum mvstwsi_status_values { |
| 141 | /* START condition transmitted */ |
| 142 | MVTWSI_STATUS_START = 0x08, |
| 143 | /* Repeated START condition transmitted */ |
| 144 | MVTWSI_STATUS_REPEATED_START = 0x10, |
| 145 | /* Address + write bit transmitted, ACK received */ |
| 146 | MVTWSI_STATUS_ADDR_W_ACK = 0x18, |
| 147 | /* Data transmitted, ACK received */ |
| 148 | MVTWSI_STATUS_DATA_W_ACK = 0x28, |
| 149 | /* Address + read bit transmitted, ACK received */ |
| 150 | MVTWSI_STATUS_ADDR_R_ACK = 0x40, |
| 151 | /* Address + read bit transmitted, ACK not received */ |
| 152 | MVTWSI_STATUS_ADDR_R_NAK = 0x48, |
| 153 | /* Data received, ACK transmitted */ |
| 154 | MVTWSI_STATUS_DATA_R_ACK = 0x50, |
| 155 | /* Data received, ACK not transmitted */ |
| 156 | MVTWSI_STATUS_DATA_R_NAK = 0x58, |
| 157 | /* No relevant status */ |
| 158 | MVTWSI_STATUS_IDLE = 0xF8, |
| 159 | }; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 160 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 161 | /* |
mario.six@gdsys.cc | 1cc2c28 | 2016-07-21 11:57:04 +0200 | [diff] [blame] | 162 | * enum mvstwsi_ack_flags - Determine whether a read byte should be |
| 163 | * acknowledged or not. |
| 164 | */ |
| 165 | enum mvtwsi_ack_flags { |
| 166 | /* Send NAK after received byte */ |
| 167 | MVTWSI_READ_NAK = 0, |
| 168 | /* Send ACK after received byte */ |
| 169 | MVTWSI_READ_ACK = 1, |
| 170 | }; |
| 171 | |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 172 | /* |
| 173 | * calc_tick() - Calculate the duration of a clock cycle from the I2C speed |
| 174 | * |
| 175 | * @speed: The speed in Hz to calculate the clock cycle duration for. |
| 176 | * @return The duration of a clock cycle in ns. |
| 177 | */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 178 | inline uint calc_tick(uint speed) |
| 179 | { |
| 180 | /* One tick = the duration of a period at the specified speed in ns (we |
| 181 | * add 100 ns to be on the safe side) */ |
| 182 | return (1000000000u / speed) + 100; |
| 183 | } |
| 184 | |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 185 | #ifndef CONFIG_DM_I2C |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 186 | |
mario.six@gdsys.cc | 1cc2c28 | 2016-07-21 11:57:04 +0200 | [diff] [blame] | 187 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 188 | * twsi_get_base() - Get controller register base for specified adapter |
| 189 | * |
| 190 | * @adap: Adapter to get the register base for. |
| 191 | * @return Register base for the specified adapter. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 192 | */ |
Paul Kocialkowski | 2fae3e7 | 2015-04-10 23:09:51 +0200 | [diff] [blame] | 193 | static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap) |
| 194 | { |
| 195 | switch (adap->hwadapnr) { |
| 196 | #ifdef CONFIG_I2C_MVTWSI_BASE0 |
| 197 | case 0: |
mario.six@gdsys.cc | 2b656eb | 2016-07-21 11:57:01 +0200 | [diff] [blame] | 198 | return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0; |
Paul Kocialkowski | 2fae3e7 | 2015-04-10 23:09:51 +0200 | [diff] [blame] | 199 | #endif |
| 200 | #ifdef CONFIG_I2C_MVTWSI_BASE1 |
| 201 | case 1: |
mario.six@gdsys.cc | 2b656eb | 2016-07-21 11:57:01 +0200 | [diff] [blame] | 202 | return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1; |
Paul Kocialkowski | 2fae3e7 | 2015-04-10 23:09:51 +0200 | [diff] [blame] | 203 | #endif |
| 204 | #ifdef CONFIG_I2C_MVTWSI_BASE2 |
| 205 | case 2: |
mario.six@gdsys.cc | 2b656eb | 2016-07-21 11:57:01 +0200 | [diff] [blame] | 206 | return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2; |
Paul Kocialkowski | 2fae3e7 | 2015-04-10 23:09:51 +0200 | [diff] [blame] | 207 | #endif |
| 208 | #ifdef CONFIG_I2C_MVTWSI_BASE3 |
| 209 | case 3: |
mario.six@gdsys.cc | 2b656eb | 2016-07-21 11:57:01 +0200 | [diff] [blame] | 210 | return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3; |
Paul Kocialkowski | 2fae3e7 | 2015-04-10 23:09:51 +0200 | [diff] [blame] | 211 | #endif |
| 212 | #ifdef CONFIG_I2C_MVTWSI_BASE4 |
| 213 | case 4: |
mario.six@gdsys.cc | 2b656eb | 2016-07-21 11:57:01 +0200 | [diff] [blame] | 214 | return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4; |
Paul Kocialkowski | 2fae3e7 | 2015-04-10 23:09:51 +0200 | [diff] [blame] | 215 | #endif |
Jelle van der Waa | 8d3d7c1 | 2016-01-14 14:06:26 +0100 | [diff] [blame] | 216 | #ifdef CONFIG_I2C_MVTWSI_BASE5 |
| 217 | case 5: |
mario.six@gdsys.cc | 2b656eb | 2016-07-21 11:57:01 +0200 | [diff] [blame] | 218 | return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5; |
Jelle van der Waa | 8d3d7c1 | 2016-01-14 14:06:26 +0100 | [diff] [blame] | 219 | #endif |
Paul Kocialkowski | 2fae3e7 | 2015-04-10 23:09:51 +0200 | [diff] [blame] | 220 | default: |
| 221 | printf("Missing mvtwsi controller %d base\n", adap->hwadapnr); |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | return NULL; |
| 226 | } |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 227 | #endif |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 228 | |
| 229 | /* |
mario.six@gdsys.cc | f43d3e9 | 2016-07-21 11:57:02 +0200 | [diff] [blame] | 230 | * enum mvtwsi_error_class - types of I2C errors |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 231 | */ |
mario.six@gdsys.cc | f43d3e9 | 2016-07-21 11:57:02 +0200 | [diff] [blame] | 232 | enum mvtwsi_error_class { |
| 233 | /* The controller returned a different status than expected */ |
| 234 | MVTWSI_ERROR_WRONG_STATUS = 0x01, |
| 235 | /* The controller timed out */ |
| 236 | MVTWSI_ERROR_TIMEOUT = 0x02, |
| 237 | }; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 238 | |
mario.six@gdsys.cc | f43d3e9 | 2016-07-21 11:57:02 +0200 | [diff] [blame] | 239 | /* |
| 240 | * mvtwsi_error() - Build I2C return code from error information |
| 241 | * |
| 242 | * For debugging purposes, this function packs some information of an occurred |
| 243 | * error into a return code. These error codes are returned from I2C API |
| 244 | * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.). |
| 245 | * |
| 246 | * @ec: The error class of the error (enum mvtwsi_error_class). |
| 247 | * @lc: The last value of the control register. |
| 248 | * @ls: The last value of the status register. |
| 249 | * @es: The expected value of the status register. |
| 250 | * @return The generated error code. |
| 251 | */ |
| 252 | inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es) |
| 253 | { |
| 254 | return ((ec << 24) & 0xFF000000) |
| 255 | | ((lc << 16) & 0x00FF0000) |
| 256 | | ((ls << 8) & 0x0000FF00) |
| 257 | | (es & 0xFF); |
| 258 | } |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 259 | |
| 260 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 261 | * twsi_wait() - Wait for I2C bus interrupt flag and check status, or time out. |
| 262 | * |
| 263 | * @return Zero if status is as expected, or a non-zero code if either a time |
| 264 | * out occurred, or the status was not the expected one. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 265 | */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 266 | static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status, |
| 267 | uint tick) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 268 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 269 | int control, status; |
| 270 | int timeout = 1000; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 271 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 272 | do { |
| 273 | control = readl(&twsi->control); |
| 274 | if (control & MVTWSI_CONTROL_IFLG) { |
Marek Behún | b9739da | 2019-05-02 16:53:38 +0200 | [diff] [blame] | 275 | /* |
| 276 | * On Armada 38x it seems that the controller works as |
| 277 | * if it first set the MVTWSI_CONTROL_IFLAG in the |
| 278 | * control register and only after that it changed the |
| 279 | * status register. |
| 280 | * This sometimes caused weird bugs which only appeared |
| 281 | * on selected I2C speeds and even then only sometimes. |
| 282 | * We therefore add here a simple ndealy(100), which |
| 283 | * seems to fix this weird bug. |
| 284 | */ |
| 285 | ndelay(100); |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 286 | status = readl(&twsi->status); |
| 287 | if (status == expected_status) |
| 288 | return 0; |
| 289 | else |
mario.six@gdsys.cc | f43d3e9 | 2016-07-21 11:57:02 +0200 | [diff] [blame] | 290 | return mvtwsi_error( |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 291 | MVTWSI_ERROR_WRONG_STATUS, |
| 292 | control, status, expected_status); |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 293 | } |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 294 | ndelay(tick); /* One clock cycle */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 295 | } while (timeout--); |
| 296 | status = readl(&twsi->status); |
mario.six@gdsys.cc | f43d3e9 | 2016-07-21 11:57:02 +0200 | [diff] [blame] | 297 | return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status, |
| 298 | expected_status); |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 301 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 302 | * twsi_start() - Assert a START condition on the bus. |
| 303 | * |
| 304 | * This function is used in both single I2C transactions and inside |
| 305 | * back-to-back transactions (repeated starts). |
| 306 | * |
| 307 | * @twsi: The MVTWSI register structure to use. |
| 308 | * @expected_status: The I2C bus status expected to be asserted after the |
| 309 | * operation completion. |
| 310 | * @tick: The duration of a clock cycle at the current I2C speed. |
| 311 | * @return Zero if status is as expected, or a non-zero code if either a time |
| 312 | * out occurred or the status was not the expected one. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 313 | */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 314 | static int twsi_start(struct mvtwsi_registers *twsi, int expected_status, |
| 315 | uint tick) |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 316 | { |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 317 | /* Assert START */ |
mario.six@gdsys.cc | 1cc2c28 | 2016-07-21 11:57:04 +0200 | [diff] [blame] | 318 | writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START | |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 319 | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control); |
| 320 | /* Wait for controller to process START */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 321 | return twsi_wait(twsi, expected_status, tick); |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 324 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 325 | * twsi_send() - Send a byte on the I2C bus. |
| 326 | * |
| 327 | * The byte may be part of an address byte or data. |
| 328 | * |
| 329 | * @twsi: The MVTWSI register structure to use. |
| 330 | * @byte: The byte to send. |
| 331 | * @expected_status: The I2C bus status expected to be asserted after the |
| 332 | * operation completion. |
| 333 | * @tick: The duration of a clock cycle at the current I2C speed. |
| 334 | * @return Zero if status is as expected, or a non-zero code if either a time |
| 335 | * out occurred or the status was not the expected one. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 336 | */ |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 337 | static int twsi_send(struct mvtwsi_registers *twsi, u8 byte, |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 338 | int expected_status, uint tick) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 339 | { |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 340 | /* Write byte to data register for sending */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 341 | writel(byte, &twsi->data); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 342 | /* Clear any pending interrupt -- that will cause sending */ |
mario.six@gdsys.cc | 1cc2c28 | 2016-07-21 11:57:04 +0200 | [diff] [blame] | 343 | writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG, |
| 344 | &twsi->control); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 345 | /* Wait for controller to receive byte, and check ACK */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 346 | return twsi_wait(twsi, expected_status, tick); |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 349 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 350 | * twsi_recv() - Receive a byte on the I2C bus. |
| 351 | * |
| 352 | * The static variable mvtwsi_control_flags controls whether we ack or nak. |
| 353 | * |
| 354 | * @twsi: The MVTWSI register structure to use. |
| 355 | * @byte: The byte to send. |
| 356 | * @ack_flag: Flag that determines whether the received byte should |
| 357 | * be acknowledged by the controller or not (sent ACK/NAK). |
| 358 | * @tick: The duration of a clock cycle at the current I2C speed. |
| 359 | * @return Zero if status is as expected, or a non-zero code if either a time |
| 360 | * out occurred or the status was not the expected one. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 361 | */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 362 | static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag, |
| 363 | uint tick) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 364 | { |
mario.six@gdsys.cc | 1cc2c28 | 2016-07-21 11:57:04 +0200 | [diff] [blame] | 365 | int expected_status, status, control; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 366 | |
mario.six@gdsys.cc | 1cc2c28 | 2016-07-21 11:57:04 +0200 | [diff] [blame] | 367 | /* Compute expected status based on passed ACK flag */ |
| 368 | expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK : |
| 369 | MVTWSI_STATUS_DATA_R_NAK; |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 370 | /* Acknowledge *previous state*, and launch receive */ |
mario.six@gdsys.cc | 1cc2c28 | 2016-07-21 11:57:04 +0200 | [diff] [blame] | 371 | control = MVTWSI_CONTROL_TWSIEN; |
| 372 | control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0; |
| 373 | writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 374 | /* Wait for controller to receive byte, and assert ACK or NAK */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 375 | status = twsi_wait(twsi, expected_status, tick); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 376 | /* If we did receive the expected byte, store it */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 377 | if (status == 0) |
| 378 | *byte = readl(&twsi->data); |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 379 | return status; |
| 380 | } |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 381 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 382 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 383 | * twsi_stop() - Assert a STOP condition on the bus. |
| 384 | * |
| 385 | * This function is also used to force the bus back to idle state (SDA = |
| 386 | * SCL = 1). |
| 387 | * |
| 388 | * @twsi: The MVTWSI register structure to use. |
| 389 | * @tick: The duration of a clock cycle at the current I2C speed. |
| 390 | * @return Zero if the operation succeeded, or a non-zero code if a time out |
| 391 | * occurred. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 392 | */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 393 | static int twsi_stop(struct mvtwsi_registers *twsi, uint tick) |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 394 | { |
| 395 | int control, stop_status; |
mario.six@gdsys.cc | bdf0f66 | 2016-07-21 11:57:05 +0200 | [diff] [blame] | 396 | int status = 0; |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 397 | int timeout = 1000; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 398 | |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 399 | /* Assert STOP */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 400 | control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP; |
Hans de Goede | 6b703e0 | 2016-01-14 14:06:25 +0100 | [diff] [blame] | 401 | writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 402 | /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 403 | do { |
| 404 | stop_status = readl(&twsi->status); |
| 405 | if (stop_status == MVTWSI_STATUS_IDLE) |
| 406 | break; |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 407 | ndelay(tick); /* One clock cycle */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 408 | } while (timeout--); |
| 409 | control = readl(&twsi->control); |
| 410 | if (stop_status != MVTWSI_STATUS_IDLE) |
mario.six@gdsys.cc | bdf0f66 | 2016-07-21 11:57:05 +0200 | [diff] [blame] | 411 | status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT, |
| 412 | control, status, MVTWSI_STATUS_IDLE); |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 413 | return status; |
| 414 | } |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 415 | |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 416 | /* |
| 417 | * twsi_calc_freq() - Compute I2C frequency depending on m and n parameters. |
| 418 | * |
| 419 | * @n: Parameter 'n' for the frequency calculation algorithm. |
| 420 | * @m: Parameter 'm' for the frequency calculation algorithm. |
| 421 | * @return The I2C frequency corresponding to the passed m and n parameters. |
| 422 | */ |
mario.six@gdsys.cc | 9e118b3 | 2016-07-21 11:57:06 +0200 | [diff] [blame] | 423 | static uint twsi_calc_freq(const int n, const int m) |
Stefan Roese | cca56a7 | 2015-03-18 09:30:54 +0100 | [diff] [blame] | 424 | { |
Jagan Teki | 68078f7 | 2016-10-13 14:19:35 +0530 | [diff] [blame] | 425 | #ifdef CONFIG_ARCH_SUNXI |
Stefan Roese | cca56a7 | 2015-03-18 09:30:54 +0100 | [diff] [blame] | 426 | return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n)); |
| 427 | #else |
| 428 | return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n)); |
| 429 | #endif |
| 430 | } |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 431 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 432 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 433 | * twsi_reset() - Reset the I2C controller. |
| 434 | * |
| 435 | * Resetting the controller also resets the baud rate and slave address, hence |
| 436 | * they must be re-established after the reset. |
| 437 | * |
| 438 | * @twsi: The MVTWSI register structure to use. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 439 | */ |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 440 | static void twsi_reset(struct mvtwsi_registers *twsi) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 441 | { |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 442 | /* Reset controller */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 443 | writel(0, &twsi->soft_reset); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 444 | /* Wait 2 ms -- this is what the Marvell LSP does */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 445 | udelay(20000); |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 448 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 449 | * __twsi_i2c_set_bus_speed() - Set the speed of the I2C controller. |
| 450 | * |
| 451 | * This function sets baud rate to the highest possible value that does not |
| 452 | * exceed the requested rate. |
| 453 | * |
| 454 | * @twsi: The MVTWSI register structure to use. |
| 455 | * @requested_speed: The desired frequency the controller should run at |
| 456 | * in Hz. |
| 457 | * @return The actual frequency the controller was configured to. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 458 | */ |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 459 | static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi, |
mario.six@gdsys.cc | a4ac8b7 | 2016-07-21 11:57:07 +0200 | [diff] [blame] | 460 | uint requested_speed) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 461 | { |
mario.six@gdsys.cc | 9e118b3 | 2016-07-21 11:57:06 +0200 | [diff] [blame] | 462 | uint tmp_speed, highest_speed, n, m; |
| 463 | uint baud = 0x44; /* Baud rate after controller reset */ |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 464 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 465 | highest_speed = 0; |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 466 | /* Successively try m, n combinations, and use the combination |
| 467 | * resulting in the largest speed that's not above the requested |
| 468 | * speed */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 469 | for (n = 0; n < 8; n++) { |
| 470 | for (m = 0; m < 16; m++) { |
Stefan Roese | cca56a7 | 2015-03-18 09:30:54 +0100 | [diff] [blame] | 471 | tmp_speed = twsi_calc_freq(n, m); |
mario.six@gdsys.cc | 2b656eb | 2016-07-21 11:57:01 +0200 | [diff] [blame] | 472 | if ((tmp_speed <= requested_speed) && |
| 473 | (tmp_speed > highest_speed)) { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 474 | highest_speed = tmp_speed; |
| 475 | baud = (m << 3) | n; |
| 476 | } |
| 477 | } |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 478 | } |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 479 | writel(baud, &twsi->baudrate); |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 480 | |
| 481 | /* Wait for controller for one tick */ |
| 482 | #ifdef CONFIG_DM_I2C |
| 483 | ndelay(calc_tick(highest_speed)); |
| 484 | #else |
| 485 | ndelay(10000); |
| 486 | #endif |
| 487 | return highest_speed; |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 488 | } |
| 489 | |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 490 | /* |
| 491 | * __twsi_i2c_init() - Initialize the I2C controller. |
| 492 | * |
| 493 | * @twsi: The MVTWSI register structure to use. |
| 494 | * @speed: The initial frequency the controller should run at |
| 495 | * in Hz. |
| 496 | * @slaveadd: The I2C address to be set for the I2C master. |
| 497 | * @actual_speed: A output parameter that receives the actual frequency |
| 498 | * in Hz the controller was set to by the function. |
| 499 | * @return Zero if the operation succeeded, or a non-zero code if a time out |
| 500 | * occurred. |
| 501 | */ |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 502 | static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed, |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 503 | int slaveadd, uint *actual_speed) |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 504 | { |
Stefan Mavrodiev | 6425ebd | 2018-02-13 09:27:40 +0200 | [diff] [blame] | 505 | uint tmp_speed; |
| 506 | |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 507 | /* Reset controller */ |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 508 | twsi_reset(twsi); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 509 | /* Set speed */ |
Stefan Mavrodiev | 6425ebd | 2018-02-13 09:27:40 +0200 | [diff] [blame] | 510 | tmp_speed = __twsi_i2c_set_bus_speed(twsi, speed); |
Heinrich Schuchardt | 5e0fd54 | 2018-01-31 00:57:17 +0100 | [diff] [blame] | 511 | if (actual_speed) |
Stefan Mavrodiev | 6425ebd | 2018-02-13 09:27:40 +0200 | [diff] [blame] | 512 | *actual_speed = tmp_speed; |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 513 | /* Set slave address; even though we don't use it */ |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 514 | writel(slaveadd, &twsi->slave_address); |
| 515 | writel(0, &twsi->xtnd_slave_addr); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 516 | /* Assert STOP, but don't care for the result */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 517 | #ifdef CONFIG_DM_I2C |
| 518 | (void) twsi_stop(twsi, calc_tick(*actual_speed)); |
| 519 | #else |
| 520 | (void) twsi_stop(twsi, 10000); |
| 521 | #endif |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 524 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 525 | * i2c_begin() - Start a I2C transaction. |
| 526 | * |
| 527 | * Begin a I2C transaction with a given expected start status and chip address. |
| 528 | * A START is asserted, and the address byte is sent to the I2C controller. The |
| 529 | * expected address status will be derived from the direction bit (bit 0) of |
| 530 | * the address byte. |
| 531 | * |
| 532 | * @twsi: The MVTWSI register structure to use. |
| 533 | * @expected_start_status: The I2C status the controller is expected to |
| 534 | * assert after the address byte was sent. |
| 535 | * @addr: The address byte to be sent. |
| 536 | * @tick: The duration of a clock cycle at the current |
| 537 | * I2C speed. |
| 538 | * @return Zero if the operation succeeded, or a non-zero code if a time out or |
| 539 | * unexpected I2C status occurred. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 540 | */ |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 541 | static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status, |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 542 | u8 addr, uint tick) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 543 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 544 | int status, expected_addr_status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 545 | |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 546 | /* Compute the expected address status from the direction bit in |
| 547 | * the address byte */ |
| 548 | if (addr & 1) /* Reading */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 549 | expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK; |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 550 | else /* Writing */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 551 | expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK; |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 552 | /* Assert START */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 553 | status = twsi_start(twsi, expected_start_status, tick); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 554 | /* Send out the address if the start went well */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 555 | if (status == 0) |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 556 | status = twsi_send(twsi, addr, expected_addr_status, tick); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 557 | /* Return 0, or the status of the first failure */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 558 | return status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 561 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 562 | * __twsi_i2c_probe_chip() - Probe the given I2C chip address. |
| 563 | * |
| 564 | * This function begins a I2C read transaction, does a dummy read and NAKs; if |
| 565 | * the procedure succeeds, the chip is considered to be present. |
| 566 | * |
| 567 | * @twsi: The MVTWSI register structure to use. |
| 568 | * @chip: The chip address to probe. |
| 569 | * @tick: The duration of a clock cycle at the current I2C speed. |
| 570 | * @return Zero if the operation succeeded, or a non-zero code if a time out or |
| 571 | * unexpected I2C status occurred. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 572 | */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 573 | static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip, |
| 574 | uint tick) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 575 | { |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 576 | u8 dummy_byte; |
| 577 | int status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 578 | |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 579 | /* Begin i2c read */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 580 | status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 581 | /* Dummy read was accepted: receive byte, but NAK it. */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 582 | if (status == 0) |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 583 | status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK, tick); |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 584 | /* Stop transaction */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 585 | twsi_stop(twsi, tick); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 586 | /* Return 0, or the status of the first failure */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 587 | return status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 590 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 591 | * __twsi_i2c_read() - Read data from a I2C chip. |
| 592 | * |
| 593 | * This function begins a I2C write transaction, and transmits the address |
| 594 | * bytes; then begins a I2C read transaction, and receives the data bytes. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 595 | * |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 596 | * NOTE: Some devices want a stop right before the second start, while some |
| 597 | * will choke if it is there. Since deciding this is not yet supported in |
| 598 | * higher level APIs, we need to make a decision here, and for the moment that |
| 599 | * will be a repeated start without a preceding stop. |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 600 | * |
| 601 | * @twsi: The MVTWSI register structure to use. |
| 602 | * @chip: The chip address to read from. |
| 603 | * @addr: The address bytes to send. |
| 604 | * @alen: The length of the address bytes in bytes. |
| 605 | * @data: The buffer to receive the data read from the chip (has to have |
| 606 | * a size of at least 'length' bytes). |
| 607 | * @length: The amount of data to be read from the chip in bytes. |
| 608 | * @tick: The duration of a clock cycle at the current I2C speed. |
| 609 | * @return Zero if the operation succeeded, or a non-zero code if a time out or |
| 610 | * unexpected I2C status occurred. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 611 | */ |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 612 | static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip, |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 613 | u8 *addr, int alen, uchar *data, int length, |
| 614 | uint tick) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 615 | { |
mario.six@gdsys.cc | bdf0f66 | 2016-07-21 11:57:05 +0200 | [diff] [blame] | 616 | int status = 0; |
| 617 | int stop_status; |
mario.six@gdsys.cc | 029a84b | 2016-07-21 11:57:11 +0200 | [diff] [blame] | 618 | int expected_start = MVTWSI_STATUS_START; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 619 | |
mario.six@gdsys.cc | 029a84b | 2016-07-21 11:57:11 +0200 | [diff] [blame] | 620 | if (alen > 0) { |
| 621 | /* Begin i2c write to send the address bytes */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 622 | status = i2c_begin(twsi, expected_start, (chip << 1), tick); |
mario.six@gdsys.cc | 029a84b | 2016-07-21 11:57:11 +0200 | [diff] [blame] | 623 | /* Send address bytes */ |
| 624 | while ((status == 0) && alen--) |
Stefan Roese | abd7d31 | 2016-08-25 15:20:01 +0200 | [diff] [blame] | 625 | status = twsi_send(twsi, addr[alen], |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 626 | MVTWSI_STATUS_DATA_W_ACK, tick); |
mario.six@gdsys.cc | 029a84b | 2016-07-21 11:57:11 +0200 | [diff] [blame] | 627 | /* Send repeated STARTs after the initial START */ |
| 628 | expected_start = MVTWSI_STATUS_REPEATED_START; |
| 629 | } |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 630 | /* Begin i2c read to receive data bytes */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 631 | if (status == 0) |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 632 | status = i2c_begin(twsi, expected_start, (chip << 1) | 1, tick); |
mario.six@gdsys.cc | 1cc2c28 | 2016-07-21 11:57:04 +0200 | [diff] [blame] | 633 | /* Receive actual data bytes; set NAK if we if we have nothing more to |
| 634 | * read */ |
| 635 | while ((status == 0) && length--) |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 636 | status = twsi_recv(twsi, data++, |
mario.six@gdsys.cc | 1cc2c28 | 2016-07-21 11:57:04 +0200 | [diff] [blame] | 637 | length > 0 ? |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 638 | MVTWSI_READ_ACK : MVTWSI_READ_NAK, tick); |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 639 | /* Stop transaction */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 640 | stop_status = twsi_stop(twsi, tick); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 641 | /* Return 0, or the status of the first failure */ |
mario.six@gdsys.cc | bdf0f66 | 2016-07-21 11:57:05 +0200 | [diff] [blame] | 642 | return status != 0 ? status : stop_status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 643 | } |
| 644 | |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 645 | /* |
mario.six@gdsys.cc | efd1fb8 | 2016-07-21 11:57:13 +0200 | [diff] [blame] | 646 | * __twsi_i2c_write() - Send data to a I2C chip. |
| 647 | * |
| 648 | * This function begins a I2C write transaction, and transmits the address |
| 649 | * bytes; then begins a new I2C write transaction, and sends the data bytes. |
| 650 | * |
| 651 | * @twsi: The MVTWSI register structure to use. |
| 652 | * @chip: The chip address to read from. |
| 653 | * @addr: The address bytes to send. |
| 654 | * @alen: The length of the address bytes in bytes. |
| 655 | * @data: The buffer containing the data to be sent to the chip. |
| 656 | * @length: The length of data to be sent to the chip in bytes. |
| 657 | * @tick: The duration of a clock cycle at the current I2C speed. |
| 658 | * @return Zero if the operation succeeded, or a non-zero code if a time out or |
| 659 | * unexpected I2C status occurred. |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 660 | */ |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 661 | static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip, |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 662 | u8 *addr, int alen, uchar *data, int length, |
| 663 | uint tick) |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 664 | { |
mario.six@gdsys.cc | bdf0f66 | 2016-07-21 11:57:05 +0200 | [diff] [blame] | 665 | int status, stop_status; |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 666 | |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 667 | /* Begin i2c write to send first the address bytes, then the |
| 668 | * data bytes */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 669 | status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 670 | /* Send address bytes */ |
mario.six@gdsys.cc | c4eceb5 | 2016-07-21 11:57:09 +0200 | [diff] [blame] | 671 | while ((status == 0) && (alen-- > 0)) |
Stefan Roese | abd7d31 | 2016-08-25 15:20:01 +0200 | [diff] [blame] | 672 | status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK, |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 673 | tick); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 674 | /* Send data bytes */ |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 675 | while ((status == 0) && (length-- > 0)) |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 676 | status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK, |
| 677 | tick); |
Albert Aribaud | 04280c4 | 2010-08-27 18:26:05 +0200 | [diff] [blame] | 678 | /* Stop transaction */ |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 679 | stop_status = twsi_stop(twsi, tick); |
mario.six@gdsys.cc | 7b0c431 | 2016-07-21 11:57:03 +0200 | [diff] [blame] | 680 | /* Return 0, or the status of the first failure */ |
mario.six@gdsys.cc | bdf0f66 | 2016-07-21 11:57:05 +0200 | [diff] [blame] | 681 | return status != 0 ? status : stop_status; |
Heiko Schocher | 5cd2a24 | 2009-07-20 09:59:37 +0200 | [diff] [blame] | 682 | } |
| 683 | |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 684 | #ifndef CONFIG_DM_I2C |
mario.six@gdsys.cc | a4ac8b7 | 2016-07-21 11:57:07 +0200 | [diff] [blame] | 685 | static void twsi_i2c_init(struct i2c_adapter *adap, int speed, |
| 686 | int slaveadd) |
| 687 | { |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 688 | struct mvtwsi_registers *twsi = twsi_get_base(adap); |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 689 | __twsi_i2c_init(twsi, speed, slaveadd, NULL); |
mario.six@gdsys.cc | a4ac8b7 | 2016-07-21 11:57:07 +0200 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 693 | uint requested_speed) |
| 694 | { |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 695 | struct mvtwsi_registers *twsi = twsi_get_base(adap); |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 696 | __twsi_i2c_set_bus_speed(twsi, requested_speed); |
| 697 | return 0; |
mario.six@gdsys.cc | a4ac8b7 | 2016-07-21 11:57:07 +0200 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip) |
| 701 | { |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 702 | struct mvtwsi_registers *twsi = twsi_get_base(adap); |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 703 | return __twsi_i2c_probe_chip(twsi, chip, 10000); |
mario.six@gdsys.cc | a4ac8b7 | 2016-07-21 11:57:07 +0200 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr, |
| 707 | int alen, uchar *data, int length) |
| 708 | { |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 709 | struct mvtwsi_registers *twsi = twsi_get_base(adap); |
mario.six@gdsys.cc | c4eceb5 | 2016-07-21 11:57:09 +0200 | [diff] [blame] | 710 | u8 addr_bytes[4]; |
| 711 | |
| 712 | addr_bytes[0] = (addr >> 0) & 0xFF; |
| 713 | addr_bytes[1] = (addr >> 8) & 0xFF; |
| 714 | addr_bytes[2] = (addr >> 16) & 0xFF; |
| 715 | addr_bytes[3] = (addr >> 24) & 0xFF; |
| 716 | |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 717 | return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length, |
| 718 | 10000); |
mario.six@gdsys.cc | a4ac8b7 | 2016-07-21 11:57:07 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr, |
| 722 | int alen, uchar *data, int length) |
| 723 | { |
mario.six@gdsys.cc | 9f7f689 | 2016-07-21 11:57:08 +0200 | [diff] [blame] | 724 | struct mvtwsi_registers *twsi = twsi_get_base(adap); |
mario.six@gdsys.cc | c4eceb5 | 2016-07-21 11:57:09 +0200 | [diff] [blame] | 725 | u8 addr_bytes[4]; |
| 726 | |
| 727 | addr_bytes[0] = (addr >> 0) & 0xFF; |
| 728 | addr_bytes[1] = (addr >> 8) & 0xFF; |
| 729 | addr_bytes[2] = (addr >> 16) & 0xFF; |
| 730 | addr_bytes[3] = (addr >> 24) & 0xFF; |
| 731 | |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 732 | return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length, |
| 733 | 10000); |
mario.six@gdsys.cc | a4ac8b7 | 2016-07-21 11:57:07 +0200 | [diff] [blame] | 734 | } |
| 735 | |
Paul Kocialkowski | 2fae3e7 | 2015-04-10 23:09:51 +0200 | [diff] [blame] | 736 | #ifdef CONFIG_I2C_MVTWSI_BASE0 |
Hans de Goede | 9830f1c | 2014-06-13 22:55:48 +0200 | [diff] [blame] | 737 | U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe, |
| 738 | twsi_i2c_read, twsi_i2c_write, |
| 739 | twsi_i2c_set_bus_speed, |
| 740 | CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0) |
Paul Kocialkowski | 2fae3e7 | 2015-04-10 23:09:51 +0200 | [diff] [blame] | 741 | #endif |
| 742 | #ifdef CONFIG_I2C_MVTWSI_BASE1 |
| 743 | U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe, |
| 744 | twsi_i2c_read, twsi_i2c_write, |
| 745 | twsi_i2c_set_bus_speed, |
| 746 | CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1) |
| 747 | |
| 748 | #endif |
| 749 | #ifdef CONFIG_I2C_MVTWSI_BASE2 |
| 750 | U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe, |
| 751 | twsi_i2c_read, twsi_i2c_write, |
| 752 | twsi_i2c_set_bus_speed, |
| 753 | CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2) |
| 754 | |
| 755 | #endif |
| 756 | #ifdef CONFIG_I2C_MVTWSI_BASE3 |
| 757 | U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe, |
| 758 | twsi_i2c_read, twsi_i2c_write, |
| 759 | twsi_i2c_set_bus_speed, |
| 760 | CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3) |
| 761 | |
| 762 | #endif |
| 763 | #ifdef CONFIG_I2C_MVTWSI_BASE4 |
| 764 | U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe, |
| 765 | twsi_i2c_read, twsi_i2c_write, |
| 766 | twsi_i2c_set_bus_speed, |
| 767 | CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4) |
| 768 | |
| 769 | #endif |
Jelle van der Waa | 8d3d7c1 | 2016-01-14 14:06:26 +0100 | [diff] [blame] | 770 | #ifdef CONFIG_I2C_MVTWSI_BASE5 |
| 771 | U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe, |
| 772 | twsi_i2c_read, twsi_i2c_write, |
| 773 | twsi_i2c_set_bus_speed, |
| 774 | CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5) |
| 775 | |
| 776 | #endif |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 777 | #else /* CONFIG_DM_I2C */ |
| 778 | |
| 779 | static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr, |
| 780 | u32 chip_flags) |
| 781 | { |
| 782 | struct mvtwsi_i2c_dev *dev = dev_get_priv(bus); |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 783 | return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick); |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed) |
| 787 | { |
| 788 | struct mvtwsi_i2c_dev *dev = dev_get_priv(bus); |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 789 | |
| 790 | dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed); |
| 791 | dev->tick = calc_tick(dev->speed); |
| 792 | |
| 793 | return 0; |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | static int mvtwsi_i2c_ofdata_to_platdata(struct udevice *bus) |
| 797 | { |
| 798 | struct mvtwsi_i2c_dev *dev = dev_get_priv(bus); |
| 799 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 800 | dev->base = devfdt_get_addr_ptr(bus); |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 801 | |
| 802 | if (!dev->base) |
| 803 | return -ENOMEM; |
| 804 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 805 | dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus), |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 806 | "cell-index", -1); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 807 | dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus), |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 808 | "u-boot,i2c-slave-addr", 0x0); |
Simon Glass | f0c99c5 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 809 | dev->speed = dev_read_u32_default(bus, "clock-frequency", |
| 810 | I2C_SPEED_STANDARD_RATE); |
| 811 | |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 812 | return 0; |
| 813 | } |
| 814 | |
Baruch Siach | 91006c7 | 2018-06-07 12:38:10 +0300 | [diff] [blame] | 815 | static void twsi_disable_i2c_slave(struct mvtwsi_registers *twsi) |
| 816 | { |
| 817 | clrbits_le32(&twsi->debug, BIT(18)); |
| 818 | } |
| 819 | |
| 820 | static int mvtwsi_i2c_bind(struct udevice *bus) |
| 821 | { |
| 822 | struct mvtwsi_registers *twsi = devfdt_get_addr_ptr(bus); |
| 823 | |
| 824 | /* Disable the hidden slave in i2c0 of these platforms */ |
Trevor Woerner | bb7ab07 | 2020-05-06 08:02:40 -0400 | [diff] [blame] | 825 | if ((IS_ENABLED(CONFIG_ARMADA_38X) || IS_ENABLED(CONFIG_ARCH_KIRKWOOD)) |
Baruch Siach | 91006c7 | 2018-06-07 12:38:10 +0300 | [diff] [blame] | 826 | && bus->req_seq == 0) |
| 827 | twsi_disable_i2c_slave(twsi); |
| 828 | |
| 829 | return 0; |
| 830 | } |
| 831 | |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 832 | static int mvtwsi_i2c_probe(struct udevice *bus) |
| 833 | { |
| 834 | struct mvtwsi_i2c_dev *dev = dev_get_priv(bus); |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 835 | uint actual_speed; |
| 836 | |
| 837 | __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed); |
| 838 | dev->speed = actual_speed; |
| 839 | dev->tick = calc_tick(dev->speed); |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) |
| 844 | { |
| 845 | struct mvtwsi_i2c_dev *dev = dev_get_priv(bus); |
| 846 | struct i2c_msg *dmsg, *omsg, dummy; |
| 847 | |
| 848 | memset(&dummy, 0, sizeof(struct i2c_msg)); |
| 849 | |
| 850 | /* We expect either two messages (one with an offset and one with the |
| 851 | * actual data) or one message (just data or offset/data combined) */ |
| 852 | if (nmsgs > 2 || nmsgs == 0) { |
| 853 | debug("%s: Only one or two messages are supported.", __func__); |
| 854 | return -1; |
| 855 | } |
| 856 | |
| 857 | omsg = nmsgs == 1 ? &dummy : msg; |
| 858 | dmsg = nmsgs == 1 ? msg : msg + 1; |
| 859 | |
| 860 | if (dmsg->flags & I2C_M_RD) |
| 861 | return __twsi_i2c_read(dev->base, dmsg->addr, omsg->buf, |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 862 | omsg->len, dmsg->buf, dmsg->len, |
| 863 | dev->tick); |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 864 | else |
| 865 | return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf, |
mario.six@gdsys.cc | e841b58 | 2016-07-21 11:57:12 +0200 | [diff] [blame] | 866 | omsg->len, dmsg->buf, dmsg->len, |
| 867 | dev->tick); |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static const struct dm_i2c_ops mvtwsi_i2c_ops = { |
| 871 | .xfer = mvtwsi_i2c_xfer, |
| 872 | .probe_chip = mvtwsi_i2c_probe_chip, |
| 873 | .set_bus_speed = mvtwsi_i2c_set_bus_speed, |
| 874 | }; |
| 875 | |
| 876 | static const struct udevice_id mvtwsi_i2c_ids[] = { |
| 877 | { .compatible = "marvell,mv64xxx-i2c", }, |
Stefan Roese | 58e58d8 | 2016-09-16 15:07:55 +0200 | [diff] [blame] | 878 | { .compatible = "marvell,mv78230-i2c", }, |
Jernej Skrabec | 9220d50 | 2017-04-27 00:03:36 +0200 | [diff] [blame] | 879 | { .compatible = "allwinner,sun6i-a31-i2c", }, |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 880 | { /* sentinel */ } |
| 881 | }; |
| 882 | |
| 883 | U_BOOT_DRIVER(i2c_mvtwsi) = { |
| 884 | .name = "i2c_mvtwsi", |
| 885 | .id = UCLASS_I2C, |
| 886 | .of_match = mvtwsi_i2c_ids, |
Baruch Siach | 91006c7 | 2018-06-07 12:38:10 +0300 | [diff] [blame] | 887 | .bind = mvtwsi_i2c_bind, |
mario.six@gdsys.cc | 355a127 | 2016-07-21 11:57:10 +0200 | [diff] [blame] | 888 | .probe = mvtwsi_i2c_probe, |
| 889 | .ofdata_to_platdata = mvtwsi_i2c_ofdata_to_platdata, |
| 890 | .priv_auto_alloc_size = sizeof(struct mvtwsi_i2c_dev), |
| 891 | .ops = &mvtwsi_i2c_ops, |
| 892 | }; |
| 893 | #endif /* CONFIG_DM_I2C */ |