1 /******************************************************************************** 2 Copyright (C) 2016 Marvell International Ltd. 3 4 Marvell BSD License Option 5 6 If you received this File from Marvell, you may opt to use, redistribute and/or 7 modify this File under the following licensing terms. 8 Redistribution and use in source and binary forms, with or without modification, 9 are permitted provided that the following conditions are met: 10 11 * Redistributions of source code must retain the above copyright notice, 12 this list of conditions and the following disclaimer. 13 14 * Redistributions in binary form must reproduce the above copyright 15 notice, this list of conditions and the following disclaimer in the 16 documentation and/or other materials provided with the distribution. 17 18 * Neither the name of Marvell nor the names of its contributors may be 19 used to endorse or promote products derived from this software without 20 specific prior written permission. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 33 *******************************************************************************/ 34 35 #ifndef __MV_I2C_H__ 36 #define __MV_I2C_H__ 37 38 #include <Uefi.h> 39 40 #define I2C_BASE_ADDRESS 0xf0511000 41 42 #define I2C_SLAVE_ADDR 0x00 43 #define I2C_EXT_SLAVE_ADDR 0x10 44 #define I2C_DATA 0x04 45 46 #define I2C_CONTROL 0x08 47 #define I2C_CONTROL_ACK (1 << 2) 48 #define I2C_CONTROL_IFLG (1 << 3) 49 #define I2C_CONTROL_STOP (1 << 4) 50 #define I2C_CONTROL_START (1 << 5) 51 #define I2C_CONTROL_I2CEN (1 << 6) 52 #define I2C_CONTROL_INTEN (1 << 7) 53 54 #define I2C_STATUS 0x0c 55 #define I2C_STATUS_START 0x08 56 #define I2C_STATUS_RPTD_START 0x10 57 #define I2C_STATUS_ADDR_W_ACK 0x18 58 #define I2C_STATUS_DATA_WR_ACK 0x28 59 #define I2C_STATUS_ADDR_R_ACK 0x40 60 #define I2C_STATUS_DATA_RD_ACK 0x50 61 #define I2C_STATUS_DATA_RD_NOACK 0x58 62 63 #define I2C_BAUD_RATE 0x0c 64 #define I2C_BAUD_RATE_PARAM(M,N) ((((M) << 3) | ((N) & 0x7)) & 0x7f) 65 #define I2C_BAUD_RATE_RAW(C,M,N) ((C)/((10*(M+1))<<(N+1))) 66 #define I2C_M_FROM_BAUD(baud) (((baud) >> 3) & 0xf) 67 #define I2C_N_FROM_BAUD(baud) ((baud) & 0x7) 68 69 #define I2C_SOFT_RESET 0x1c 70 #define I2C_TRANSFER_TIMEOUT 10000 71 #define I2C_OPERATION_TIMEOUT 1000 72 73 #define I2C_UNKNOWN 0x0 74 #define I2C_SLOW 0x1 75 #define I2C_FAST 0x2 76 #define I2C_FASTEST 0x3 77 78 /* 79 * I2C_FLAG_NORESTART is not part of PI spec, it allows to continue 80 * transmission without repeated start operation. 81 * FIXME: This flag is also defined in 82 * Platforms/Marvell/Include/Protocol/Eeprom.h and it's important to have both 83 * version synced. This solution is temporary and shared flag should be used by 84 * both files. 85 * Situation is analogous with I2C_GUID, which also should be common, but is 86 * for now defined same way in two header files. 87 */ 88 #define I2C_FLAG_NORESTART 0x00000002 89 #define I2C_GUID \ 90 { \ 91 0xadc1901b, 0xb83c, 0x4831, { 0x8f, 0x59, 0x70, 0x89, 0x8f, 0x26, 0x57, 0x1e } \ 92 } 93 94 #define I2C_MASTER_SIGNATURE SIGNATURE_32 ('I', '2', 'C', 'M') 95 96 typedef struct { 97 UINT32 Signature; 98 EFI_HANDLE Controller; 99 EFI_LOCK Lock; 100 UINTN TclkFrequency; 101 UINTN BaseAddress; 102 INTN Bus; 103 EFI_I2C_MASTER_PROTOCOL I2cMaster; 104 EFI_I2C_ENUMERATE_PROTOCOL I2cEnumerate; 105 EFI_I2C_BUS_CONFIGURATION_MANAGEMENT_PROTOCOL I2cBusConf; 106 } I2C_MASTER_CONTEXT; 107 108 #define I2C_SC_FROM_MASTER(a) CR (a, I2C_MASTER_CONTEXT, I2cMaster, I2C_MASTER_SIGNATURE) 109 #define I2C_SC_FROM_ENUMERATE(a) CR (a, I2C_MASTER_CONTEXT, I2cEnumerate, I2C_MASTER_SIGNATURE) 110 #define I2C_SC_FROM_BUSCONF(a) CR (a, I2C_MASTER_CONTEXT, I2cBusConf, I2C_MASTER_SIGNATURE) 111 112 typedef struct { 113 UINT32 raw; 114 UINTN param; 115 UINTN m; 116 UINTN n; 117 } MV_I2C_BAUD_RATE; 118 119 typedef struct { 120 VENDOR_DEVICE_PATH Guid; 121 EFI_DEVICE_PATH_PROTOCOL End; 122 } MV_I2C_DEVICE_PATH; 123 124 STATIC 125 UINT32 126 I2C_READ( 127 IN I2C_MASTER_CONTEXT *I2cMasterContext, 128 IN UINTN off 129 ); 130 131 STATIC 132 EFI_STATUS 133 I2C_WRITE ( 134 IN I2C_MASTER_CONTEXT *I2cMasterContext, 135 IN UINTN off, 136 IN UINT32 val 137 ); 138 139 EFI_STATUS 140 EFIAPI 141 MvI2cInitialise ( 142 IN EFI_HANDLE ImageHandle, 143 IN EFI_SYSTEM_TABLE *SystemTable 144 ); 145 146 STATIC 147 VOID 148 MvI2cControlClear ( 149 IN I2C_MASTER_CONTEXT *I2cMasterContext, 150 IN UINT32 mask 151 ); 152 153 STATIC 154 VOID 155 MvI2cControlSet ( 156 IN I2C_MASTER_CONTEXT *I2cMasterContext, 157 IN UINT32 mask 158 ); 159 160 STATIC 161 VOID 162 MvI2cClearIflg ( 163 IN I2C_MASTER_CONTEXT *I2cMasterContext 164 ); 165 STATIC 166 UINTN 167 MvI2cPollCtrl ( 168 IN I2C_MASTER_CONTEXT *I2cMasterContext, 169 IN UINTN timeout, 170 IN UINT32 mask 171 ); 172 173 STATIC 174 EFI_STATUS 175 MvI2cLockedStart ( 176 IN I2C_MASTER_CONTEXT *I2cMasterContext, 177 IN INT32 mask, 178 IN UINT8 slave, 179 IN UINTN timeout 180 ); 181 182 STATIC 183 VOID 184 MvI2cCalBaudRate ( 185 IN I2C_MASTER_CONTEXT *I2cMasterContext, 186 IN CONST UINT32 target, 187 IN OUT MV_I2C_BAUD_RATE *rate, 188 UINT32 clk 189 ); 190 191 EFI_STATUS 192 EFIAPI 193 MvI2cReset ( 194 IN CONST EFI_I2C_MASTER_PROTOCOL *This 195 ); 196 197 STATIC 198 EFI_STATUS 199 MvI2cRepeatedStart ( 200 IN I2C_MASTER_CONTEXT *I2cMasterContext, 201 IN UINT8 slave, 202 IN UINTN timeout 203 ); 204 205 STATIC 206 EFI_STATUS 207 MvI2cStart ( 208 IN I2C_MASTER_CONTEXT *I2cMasterContext, 209 IN UINT8 slave, 210 IN UINTN timeout 211 ); 212 213 STATIC 214 EFI_STATUS 215 MvI2cStop ( 216 IN I2C_MASTER_CONTEXT *I2cMasterContext 217 ); 218 219 STATIC 220 EFI_STATUS 221 MvI2cRead ( 222 IN I2C_MASTER_CONTEXT *I2cMasterContext, 223 IN OUT UINT8 *buf, 224 IN UINTN len, 225 IN OUT UINTN *read, 226 IN UINTN last, 227 IN UINTN delay 228 ); 229 230 STATIC 231 EFI_STATUS 232 MvI2cWrite ( 233 IN I2C_MASTER_CONTEXT *I2cMasterContext, 234 IN OUT CONST UINT8 *buf, 235 IN UINTN len, 236 IN OUT UINTN *sent, 237 IN UINTN timeout 238 ); 239 240 STATIC 241 EFI_STATUS 242 EFIAPI 243 MvI2cStartRequest ( 244 IN CONST EFI_I2C_MASTER_PROTOCOL *This, 245 IN UINTN SlaveAddress, 246 IN EFI_I2C_REQUEST_PACKET *RequestPacket, 247 IN EFI_EVENT Event OPTIONAL, 248 OUT EFI_STATUS *I2cStatus OPTIONAL 249 ); 250 251 STATIC 252 EFI_STATUS 253 EFIAPI 254 MvI2cEnumerate ( 255 IN CONST EFI_I2C_ENUMERATE_PROTOCOL *This, 256 IN OUT CONST EFI_I2C_DEVICE **Device 257 ); 258 259 STATIC 260 EFI_STATUS 261 EFIAPI 262 MvI2cEnableConf ( 263 IN CONST EFI_I2C_BUS_CONFIGURATION_MANAGEMENT_PROTOCOL *This, 264 IN UINTN I2cBusConfiguration, 265 IN EFI_EVENT Event OPTIONAL, 266 IN EFI_STATUS *I2cStatus OPTIONAL 267 ); 268 269 #endif // __MV_I2C_H__ 270