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