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