2 I2C Bus implementation upon CirrusLogic.
4 Copyright (c) 2008 - 2009, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "CirrusLogic5430.h"
10 #include "CirrusLogic5430I2c.h"
12 #define SEQ_ADDRESS_REGISTER 0x3c4
13 #define SEQ_DATA_REGISTER 0x3c5
15 #define I2C_CONTROL 0x08
21 #define I2C_BUS_SPEED 100 //100kbps
24 PCI I/O byte write function.
26 @param PciIo The pointer to PCI_IO_PROTOCOL.
27 @param Address The bit map of I2C Data or I2C Clock pins.
28 @param Data The date to write.
33 EFI_PCI_IO_PROTOCOL
*PciIo
,
41 EFI_PCI_IO_PASS_THROUGH_BAR
,
48 PCI I/O byte read function.
50 @param PciIo The pointer to PCI_IO_PROTOCOL.
51 @param Address The bit map of I2C Data or I2C Clock pins.
53 return byte value read from PCI I/O space.
58 EFI_PCI_IO_PROTOCOL
*PciIo
,
67 EFI_PCI_IO_PASS_THROUGH_BAR
,
76 Read status of I2C Data and I2C Clock Pins.
78 @param PciIo The pointer to PCI_IO_PROTOCOL.
79 @param Blt The bit map of I2C Data or I2C Clock pins.
81 @retval 0 Low on I2C Data or I2C Clock Pin.
82 @retval 1 High on I2C Data or I2C Clock Pin.
87 EFI_PCI_IO_PROTOCOL
*PciIo
,
91 I2cOutb (PciIo
, SEQ_ADDRESS_REGISTER
, I2C_CONTROL
);
92 return (UINT8
) ((I2cInb (PciIo
, SEQ_DATA_REGISTER
) >> Bit
) & 0xfe);
97 Set/Clear I2C Data and I2C Clock Pins.
99 @param PciIo The pointer to PCI_IO_PROTOCOL.
100 @param Blt The bit map to controller I2C Data or I2C Clock pins.
101 @param Value 1 or 0 stands for Set or Clear I2C Data and I2C Clock Pins.
106 EFI_PCI_IO_PROTOCOL
*PciIo
,
112 I2cOutb (PciIo
, SEQ_ADDRESS_REGISTER
, I2C_CONTROL
);
113 Byte
= (UINT8
) (I2cInb (PciIo
, SEQ_DATA_REGISTER
) & (UINT8
) ~(1 << Bit
)) ;
114 Byte
= (UINT8
) (Byte
| ((Value
& 0x01) << Bit
));
115 I2cOutb (PciIo
, SEQ_DATA_REGISTER
, (UINT8
) (Byte
| 0x40));
120 Read/write delay acoording to I2C Bus Speed.
128 MicroSecondDelay (1000 / I2C_BUS_SPEED
);
132 Write a 8-bit data onto I2C Data Pin.
134 @param PciIo The pointer to PCI_IO_PROTOCOL.
135 @param Data The byte data to write.
140 EFI_PCI_IO_PROTOCOL
*PciIo
,
146 // Send byte data onto I2C Bus
148 for (Index
= 0; Index
< 8; Index
--) {
149 I2cPinWrite (PciIo
, I2CDAT_OUT
, (UINT8
) (Data
>> (7 - Index
)));
150 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
152 I2cPinWrite (PciIo
, I2CCLK_OUT
, 0);
157 Read a 8-bit data from I2C Data Pin.
159 @param PciIo The pointer to PCI_IO_PROTOCOL.
161 Return the byte data read from I2C Data Pin.
165 EFI_PCI_IO_PROTOCOL
*PciIo
173 // Read byte data from I2C Bus
175 for (Index
= 0; Index
< 8; Index
--) {
176 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
178 Data
= (UINT8
) (Data
<< 1);
179 Data
= (UINT8
) (Data
| I2cPinRead (PciIo
, I2CDAT_IN
));
180 I2cPinWrite (PciIo
, I2CCLK_OUT
, 0);
187 Receive an ACK signal from I2C Bus.
189 @param PciIo The pointer to PCI_IO_PROTOCOL.
194 EFI_PCI_IO_PROTOCOL
*PciIo
198 // Wait for ACK signal
200 I2cPinWrite (PciIo
, I2CDAT_OUT
, 1);
201 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
203 if (I2cPinRead (PciIo
, I2CDAT_IN
) == 0) {
204 I2cPinWrite (PciIo
, I2CDAT_OUT
, 1);
212 Send an ACK signal onto I2C Bus.
214 @param PciIo The pointer to PCI_IO_PROTOCOL.
219 EFI_PCI_IO_PROTOCOL
*PciIo
222 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
223 I2cPinWrite (PciIo
, I2CDAT_OUT
, 1);
224 I2cPinWrite (PciIo
, I2CDAT_OUT
, 0);
225 I2cPinWrite (PciIo
, I2CCLK_OUT
, 0);
229 Start a I2C transfer on I2C Bus.
231 @param PciIo The pointer to PCI_IO_PROTOCOL.
236 EFI_PCI_IO_PROTOCOL
*PciIo
240 // Init CLK and DAT pins
242 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
243 I2cPinWrite (PciIo
, I2CDAT_OUT
, 1);
245 // Start a I2C transfer, set SDA low from high, when SCL is high
247 I2cPinWrite (PciIo
, I2CDAT_OUT
, 0);
248 I2cPinWrite (PciIo
, I2CCLK_OUT
, 0);
252 Stop a I2C transfer on I2C Bus.
254 @param PciIo The pointer to PCI_IO_PROTOCOL.
259 EFI_PCI_IO_PROTOCOL
*PciIo
263 // Stop a I2C transfer, set SDA high from low, when SCL is high
265 I2cPinWrite (PciIo
, I2CDAT_OUT
, 0);
266 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
267 I2cPinWrite (PciIo
, I2CDAT_OUT
, 1);
271 Read one byte data on I2C Bus.
273 Read one byte data from the slave device connectet to I2C Bus.
274 If Data is NULL, then ASSERT().
276 @param PciIo The pointer to PCI_IO_PROTOCOL.
277 @param DeviceAddress Slave device's address.
278 @param RegisterAddress The register address on slave device.
279 @param Data The pointer to returned data if EFI_SUCCESS returned.
281 @retval EFI_DEVICE_ERROR
288 EFI_PCI_IO_PROTOCOL
*PciIo
,
290 UINT8 RegisterAddress
,
294 ASSERT (Data
!= NULL
);
297 // Start I2C transfer
302 // Send slave address with enabling write flag
304 I2cSendByte (PciIo
, (UINT8
) (DeviceAddress
& 0xfe));
307 // Wait for ACK signal
309 if (I2cWaitAck (PciIo
) == FALSE
) {
310 return EFI_DEVICE_ERROR
;
314 // Send register address
316 I2cSendByte (PciIo
, RegisterAddress
);
319 // Wait for ACK signal
321 if (I2cWaitAck (PciIo
) == FALSE
) {
322 return EFI_DEVICE_ERROR
;
326 // Send slave address with enabling read flag
328 I2cSendByte (PciIo
, (UINT8
) (DeviceAddress
| 0x01));
331 // Wait for ACK signal
333 if (I2cWaitAck (PciIo
) == FALSE
) {
334 return EFI_DEVICE_ERROR
;
338 // Read byte data from I2C Bus
340 *Data
= I2cReceiveByte (PciIo
);
343 // Send ACK signal onto I2C Bus
348 // Stop a I2C transfer
356 Write one byte data onto I2C Bus.
358 Write one byte data to the slave device connectet to I2C Bus.
359 If Data is NULL, then ASSERT().
361 @param PciIo The pointer to PCI_IO_PROTOCOL.
362 @param DeviceAddress Slave device's address.
363 @param RegisterAddress The register address on slave device.
364 @param Data The pointer to write data.
366 @retval EFI_DEVICE_ERROR
373 EFI_PCI_IO_PROTOCOL
*PciIo
,
375 UINT8 RegisterAddress
,
379 ASSERT (Data
!= NULL
);
383 // Send slave address with enabling write flag
385 I2cSendByte (PciIo
, (UINT8
) (DeviceAddress
& 0xfe));
388 // Wait for ACK signal
390 if (I2cWaitAck (PciIo
) == FALSE
) {
391 return EFI_DEVICE_ERROR
;
395 // Send register address
397 I2cSendByte (PciIo
, RegisterAddress
);
400 // Wait for ACK signal
402 if (I2cWaitAck (PciIo
) == FALSE
) {
403 return EFI_DEVICE_ERROR
;
407 // Send byte data onto I2C Bus
409 I2cSendByte (PciIo
, *Data
);
412 // Wait for ACK signal
414 if (I2cWaitAck (PciIo
) == FALSE
) {
415 return EFI_DEVICE_ERROR
;
419 // Stop a I2C transfer