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