2 I2C Bus implementation upon CirrusLogic.
4 Copyright (c) 2008 - 2009, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "CirrusLogic5430.h"
16 #include "CirrusLogic5430I2c.h"
18 #define SEQ_ADDRESS_REGISTER 0x3c4
19 #define SEQ_DATA_REGISTER 0x3c5
21 #define I2C_CONTROL 0x08
27 #define I2C_BUS_SPEED 100 //100kbps
30 PCI I/O byte write function.
32 @param PciIo The pointer to PCI_IO_PROTOCOL.
33 @param Address The bit map of I2C Data or I2C Clock pins.
34 @param Data The date to write.
39 EFI_PCI_IO_PROTOCOL
*PciIo
,
47 EFI_PCI_IO_PASS_THROUGH_BAR
,
54 PCI I/O byte read function.
56 @param PciIo The pointer to PCI_IO_PROTOCOL.
57 @param Address The bit map of I2C Data or I2C Clock pins.
59 return byte value read from PCI I/O space.
64 EFI_PCI_IO_PROTOCOL
*PciIo
,
73 EFI_PCI_IO_PASS_THROUGH_BAR
,
82 Read status of I2C Data and I2C Clock Pins.
84 @param PciIo The pointer to PCI_IO_PROTOCOL.
85 @param Blt The bit map of I2C Data or I2C Clock pins.
87 @retval 0 Low on I2C Data or I2C Clock Pin.
88 @retval 1 High on I2C Data or I2C Clock Pin.
93 EFI_PCI_IO_PROTOCOL
*PciIo
,
97 I2cOutb (PciIo
, SEQ_ADDRESS_REGISTER
, I2C_CONTROL
);
98 return (UINT8
) ((I2cInb (PciIo
, SEQ_DATA_REGISTER
) >> Bit
) & 0xfe);
103 Set/Clear I2C Data and I2C Clock Pins.
105 @param PciIo The pointer to PCI_IO_PROTOCOL.
106 @param Blt The bit map to controller I2C Data or I2C Clock pins.
107 @param Value 1 or 0 stands for Set or Clear I2C Data and I2C Clock Pins.
112 EFI_PCI_IO_PROTOCOL
*PciIo
,
118 I2cOutb (PciIo
, SEQ_ADDRESS_REGISTER
, I2C_CONTROL
);
119 Byte
= (UINT8
) (I2cInb (PciIo
, SEQ_DATA_REGISTER
) & (UINT8
) ~(1 << Bit
)) ;
120 Byte
= (UINT8
) (Byte
| ((Value
& 0x01) << Bit
));
121 I2cOutb (PciIo
, SEQ_DATA_REGISTER
, (UINT8
) (Byte
| 0x40));
126 Read/write delay acoording to I2C Bus Speed.
134 MicroSecondDelay (1000 / I2C_BUS_SPEED
);
138 Write a 8-bit data onto I2C Data Pin.
140 @param PciIo The pointer to PCI_IO_PROTOCOL.
141 @param Data The byte data to write.
146 EFI_PCI_IO_PROTOCOL
*PciIo
,
152 // Send byte data onto I2C Bus
154 for (Index
= 0; Index
< 8; Index
--) {
155 I2cPinWrite (PciIo
, I2CDAT_OUT
, (UINT8
) (Data
>> (7 - Index
)));
156 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
158 I2cPinWrite (PciIo
, I2CCLK_OUT
, 0);
163 Read a 8-bit data from I2C Data Pin.
165 @param PciIo The pointer to PCI_IO_PROTOCOL.
167 Return the byte data read from I2C Data Pin.
171 EFI_PCI_IO_PROTOCOL
*PciIo
179 // Read byte data from I2C Bus
181 for (Index
= 0; Index
< 8; Index
--) {
182 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
184 Data
= (UINT8
) (Data
<< 1);
185 Data
= (UINT8
) (Data
| I2cPinRead (PciIo
, I2CDAT_IN
));
186 I2cPinWrite (PciIo
, I2CCLK_OUT
, 0);
193 Receive an ACK signal from I2C Bus.
195 @param PciIo The pointer to PCI_IO_PROTOCOL.
200 EFI_PCI_IO_PROTOCOL
*PciIo
204 // Wait for ACK signal
206 I2cPinWrite (PciIo
, I2CDAT_OUT
, 1);
207 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
209 if (I2cPinRead (PciIo
, I2CDAT_IN
) == 0) {
210 I2cPinWrite (PciIo
, I2CDAT_OUT
, 1);
218 Send an ACK signal onto I2C Bus.
220 @param PciIo The pointer to PCI_IO_PROTOCOL.
225 EFI_PCI_IO_PROTOCOL
*PciIo
228 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
229 I2cPinWrite (PciIo
, I2CDAT_OUT
, 1);
230 I2cPinWrite (PciIo
, I2CDAT_OUT
, 0);
231 I2cPinWrite (PciIo
, I2CCLK_OUT
, 0);
235 Start a I2C transfer on I2C Bus.
237 @param PciIo The pointer to PCI_IO_PROTOCOL.
242 EFI_PCI_IO_PROTOCOL
*PciIo
246 // Init CLK and DAT pins
248 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
249 I2cPinWrite (PciIo
, I2CDAT_OUT
, 1);
251 // Start a I2C transfer, set SDA low from high, when SCL is high
253 I2cPinWrite (PciIo
, I2CDAT_OUT
, 0);
254 I2cPinWrite (PciIo
, I2CCLK_OUT
, 0);
258 Stop a I2C transfer on I2C Bus.
260 @param PciIo The pointer to PCI_IO_PROTOCOL.
265 EFI_PCI_IO_PROTOCOL
*PciIo
269 // Stop a I2C transfer, set SDA high from low, when SCL is high
271 I2cPinWrite (PciIo
, I2CDAT_OUT
, 0);
272 I2cPinWrite (PciIo
, I2CCLK_OUT
, 1);
273 I2cPinWrite (PciIo
, I2CDAT_OUT
, 1);
277 Read one byte data on I2C Bus.
279 Read one byte data from the slave device connectet to I2C Bus.
280 If Data is NULL, then ASSERT().
282 @param PciIo The pointer to PCI_IO_PROTOCOL.
283 @param DeviceAddress Slave device's address.
284 @param RegisterAddress The register address on slave device.
285 @param Data The pointer to returned data if EFI_SUCCESS returned.
287 @retval EFI_DEVICE_ERROR
294 EFI_PCI_IO_PROTOCOL
*PciIo
,
296 UINT8 RegisterAddress
,
300 ASSERT (Data
!= NULL
);
303 // Start I2C transfer
308 // Send slave address with enabling write flag
310 I2cSendByte (PciIo
, (UINT8
) (DeviceAddress
& 0xfe));
313 // Wait for ACK signal
315 if (I2cWaitAck (PciIo
) == FALSE
) {
316 return EFI_DEVICE_ERROR
;
320 // Send register address
322 I2cSendByte (PciIo
, RegisterAddress
);
325 // Wait for ACK signal
327 if (I2cWaitAck (PciIo
) == FALSE
) {
328 return EFI_DEVICE_ERROR
;
332 // Send slave address with enabling read flag
334 I2cSendByte (PciIo
, (UINT8
) (DeviceAddress
| 0x01));
337 // Wait for ACK signal
339 if (I2cWaitAck (PciIo
) == FALSE
) {
340 return EFI_DEVICE_ERROR
;
344 // Read byte data from I2C Bus
346 *Data
= I2cReceiveByte (PciIo
);
349 // Send ACK signal onto I2C Bus
354 // Stop a I2C transfer
362 Write one byte data onto I2C Bus.
364 Write one byte data to the slave device connectet to I2C Bus.
365 If Data is NULL, then ASSERT().
367 @param PciIo The pointer to PCI_IO_PROTOCOL.
368 @param DeviceAddress Slave device's address.
369 @param RegisterAddress The register address on slave device.
370 @param Data The pointer to write data.
372 @retval EFI_DEVICE_ERROR
379 EFI_PCI_IO_PROTOCOL
*PciIo
,
381 UINT8 RegisterAddress
,
385 ASSERT (Data
!= NULL
);
389 // Send slave address with enabling write flag
391 I2cSendByte (PciIo
, (UINT8
) (DeviceAddress
& 0xfe));
394 // Wait for ACK signal
396 if (I2cWaitAck (PciIo
) == FALSE
) {
397 return EFI_DEVICE_ERROR
;
401 // Send register address
403 I2cSendByte (PciIo
, RegisterAddress
);
406 // Wait for ACK signal
408 if (I2cWaitAck (PciIo
) == FALSE
) {
409 return EFI_DEVICE_ERROR
;
413 // Send byte data onto I2C Bus
415 I2cSendByte (PciIo
, *Data
);
418 // Wait for ACK signal
420 if (I2cWaitAck (PciIo
) == FALSE
) {
421 return EFI_DEVICE_ERROR
;
425 // Stop a I2C transfer