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