123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- /*++
- Copyright (c) 2014 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- i2c.c
- Abstract:
- This module implements I2C support for the TI AM335x SoC in UEFI.
- Author:
- Evan Green 5-Jan-2015
- Environment:
- Firmware
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <uefifw.h>
- #include <minoca/soc/am335x.h>
- #include "bbonefw.h"
- //
- // --------------------------------------------------------------------- Macros
- //
- #define AM335_I2C_READ(_Register) \
- EfiReadRegister32((VOID *)(AM335_I2C_0_BASE + _Register))
- #define AM335_I2C_WRITE(_Register, _Value) \
- EfiWriteRegister32((VOID *)(AM335_I2C_0_BASE + _Register), _Value)
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- VOID
- EfipAm335I2cConfigureBusClock (
- VOID
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- VOID
- EfipAm335I2c0Initialize (
- VOID
- )
- /*++
- Routine Description:
- This routine initializes the I2c bus.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- UINT32 Value;
- //
- // Set the pin muxing on I2C 0.
- //
- Value = (1 << AM335_SOC_CONF_MUX_PUTYPESEL_SHIFT) |
- (1 << AM335_SOC_CONF_MUX_RXACTIVE_SHIFT) |
- (1 << AM335_SOC_CONF_MUX_SLEWCTRL_SHIFT);
- EfiWriteRegister32(
- (VOID *)(AM335_SOC_CONTROL_REGISTERS + AM335_SOC_CONTROL_I2C0_SDA),
- Value);
- Value = (1 << AM335_SOC_CONF_MUX_PUTYPESEL_SHIFT) |
- (1 << AM335_SOC_CONF_MUX_RXACTIVE_SHIFT) |
- (1 << AM335_SOC_CONF_MUX_SLEWCTRL_SHIFT);
- EfiWriteRegister32(
- (VOID *)(AM335_SOC_CONTROL_REGISTERS + AM335_SOC_CONTROL_I2C0_SCL),
- Value);
- //
- // Disable the I2C controller.
- //
- Value = AM335_I2C_READ(Am3I2cControl);
- Value &= ~AM335_I2C_CONTROL_ENABLE;
- AM335_I2C_WRITE(Am3I2cControl, Value);
- //
- // Reset the controller.
- //
- Value = AM335_I2C_READ(Am3I2cSysControl);
- Value |= AM335_I2C_SYSTEM_CONTROL_SOFT_RESET;
- AM335_I2C_WRITE(Am3I2cSysControl, Value);
- //
- // Disable auto idle.
- //
- Value = AM335_I2C_READ(Am3I2cSysControl);
- Value &= ~AM335_I2C_SYSTEM_CONTROL_AUTO_IDLE;
- AM335_I2C_WRITE(Am3I2cSysControl, Value);
- //
- // Configure the bus speed to be 100kHz.
- //
- EfipAm335I2cConfigureBusClock();
- //
- // Enable the I2C controller.
- //
- Value = AM335_I2C_READ(Am3I2cControl);
- Value |= AM335_I2C_CONTROL_ENABLE;
- AM335_I2C_WRITE(Am3I2cControl, Value);
- //
- // Wait for the system status to indicate the controller is ready.
- //
- do {
- Value = AM335_I2C_READ(Am3I2cSysStatus);
- } while ((Value & AM335_I2C_SYSTEM_STATUS_RESET_DONE) == 0);
- return;
- }
- VOID
- EfipAm335I2c0SetSlaveAddress (
- UINT8 SlaveAddress
- )
- /*++
- Routine Description:
- This routine sets which address on the I2C bus to talk to.
- Arguments:
- SlaveAddress - Supplies the slave address to communicate with.
- Return Value:
- None.
- --*/
- {
- AM335_I2C_WRITE(Am3I2cSlaveAddress, SlaveAddress);
- return;
- }
- VOID
- EfipAm335I2c0Read (
- UINT32 Register,
- UINT32 Size,
- UINT8 *Data
- )
- /*++
- Routine Description:
- This routine performs a read from the I2C bus. This routine assumes the
- slave address has already been set.
- Arguments:
- Register - Supplies the register to read from. Supply -1 to skip
- transmitting a register number.
- Size - Supplies the number of data bytes to read.
- Data - Supplies a pointer where the data will be returned on success.
- Return Value:
- None.
- --*/
- {
- UINT32 Value;
- //
- // Transmit the register number. Set the size to 1, clear all interrupts,
- // start the transfer, write the byte, clear the transmit ready interrupt,
- // and wait for the ready bit.
- //
- if (Register <= MAX_UINT8) {
- AM335_I2C_WRITE(Am3I2cCount, 1);
- AM335_I2C_WRITE(Am3I2cInterruptStatus,
- AM335_I2C_INTERRUPT_STATUS_MASK);
- Value = AM335_I2C_CONTROL_MASTER | AM335_I2C_CONTROL_TRANSMIT |
- AM335_I2C_CONTROL_ENABLE;
- AM335_I2C_WRITE(Am3I2cControl, Value);
- Value |= AM335_I2C_CONTROL_START;
- AM335_I2C_WRITE(Am3I2cControl, Value);
- do {
- Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
- } while ((Value & AM335_I2C_INTERRUPT_BUS_BUSY) == 0);
- AM335_I2C_WRITE(Am3I2cData, Register);
- AM335_I2C_WRITE(Am3I2cInterruptStatus,
- AM335_I2C_INTERRUPT_TX_READY);
- do {
- Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
- } while ((Value & AM335_I2C_INTERRUPT_ACCESS_READY) == 0);
- }
- //
- // Now set the data count to the number of bytes, and set up the receive.
- //
- AM335_I2C_WRITE(Am3I2cCount, Size);
- AM335_I2C_WRITE(Am3I2cInterruptStatus, AM335_I2C_INTERRUPT_STATUS_MASK);
- Value = AM335_I2C_CONTROL_MASTER | AM335_I2C_CONTROL_ENABLE;
- AM335_I2C_WRITE(Am3I2cControl, Value);
- Value |= AM335_I2C_CONTROL_START;
- AM335_I2C_WRITE(Am3I2cControl, Value);
- do {
- Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
- } while ((Value & AM335_I2C_INTERRUPT_BUS_BUSY) == 0);
- //
- // Loop reading the data bytes.
- //
- while (Size != 0) {
- do {
- Value = AM335_I2C_READ(Am3I2cBufferStatus);
- Value = (Value & AM335_I2C_BUFFER_STATUS_RX_MASK) >>
- AM335_I2C_BUFFER_STATUS_RX_SHIFT;
- } while (Value == 0);
- *Data = AM335_I2C_READ(Am3I2cData);
- Data += 1;
- Size -= 1;
- }
- //
- // Make it stop.
- //
- Value = AM335_I2C_READ(Am3I2cControl);
- Value |= AM335_I2C_CONTROL_STOP;
- AM335_I2C_WRITE(Am3I2cControl, Value);
- do {
- Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
- } while ((Value & AM335_I2C_INTERRUPT_BUS_FREE) == 0);
- AM335_I2C_WRITE(Am3I2cInterruptStatus,
- AM335_I2C_INTERRUPT_BUS_FREE);
- return;
- }
- VOID
- EfipAm335I2c0Write (
- UINT32 Register,
- UINT32 Size,
- UINT8 *Data
- )
- /*++
- Routine Description:
- This routine performs a write to the I2C bus. This routine assumes the
- slave address has already been set.
- Arguments:
- Register - Supplies the register to write to. Supply -1 to skip transmitting
- a register number.
- Size - Supplies the number of data bytes to write (not including the
- register byte itself).
- Data - Supplies a pointer to the data to write.
- Return Value:
- None.
- --*/
- {
- UINT32 Index;
- UINT32 Value;
- //
- // Transmit the register number. Set the size to the register plus the
- // data, clear all interrupts, start the transfer, write the byte, clear
- // the transmit ready interrupt, and wait for the ready bit.
- //
- if (Register <= MAX_UINT8) {
- Size += 1;
- }
- AM335_I2C_WRITE(Am3I2cCount, Size);
- AM335_I2C_WRITE(Am3I2cInterruptStatus, AM335_I2C_INTERRUPT_STATUS_MASK);
- Value = AM335_I2C_CONTROL_MASTER | AM335_I2C_CONTROL_TRANSMIT |
- AM335_I2C_CONTROL_ENABLE;
- AM335_I2C_WRITE(Am3I2cControl, Value);
- Value |= AM335_I2C_CONTROL_START;
- AM335_I2C_WRITE(Am3I2cControl, Value);
- do {
- Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
- } while ((Value & AM335_I2C_INTERRUPT_BUS_BUSY) == 0);
- Index = 0;
- while (Index < Size) {
- Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
- if ((Value & AM335_I2C_INTERRUPT_TX_READY) == 0) {
- break;
- }
- //
- // The first time around write the register number, all the others
- // write the data.
- //
- if (Register <= MAX_UINT8) {
- if (Index == 0) {
- Value = Register;
- } else {
- Value = Data[Index - 1];
- }
- //
- // No register, just transmit the data.
- //
- } else {
- Value = Data[Index];
- }
- AM335_I2C_WRITE(Am3I2cData, Value);
- AM335_I2C_WRITE(Am3I2cInterruptStatus,
- AM335_I2C_INTERRUPT_TX_READY);
- Index += 1;
- }
- //
- // Make it stop.
- //
- Value = AM335_I2C_READ(Am3I2cControl);
- Value |= AM335_I2C_CONTROL_STOP;
- AM335_I2C_WRITE(Am3I2cControl, Value);
- do {
- Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
- } while ((Value & AM335_I2C_INTERRUPT_BUS_FREE) == 0);
- AM335_I2C_WRITE(Am3I2cInterruptStatus,
- AM335_I2C_INTERRUPT_BUS_FREE);
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- VOID
- EfipAm335I2cConfigureBusClock (
- VOID
- )
- /*++
- Routine Description:
- This routine initializes the bus clock of the I2C module.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- UINT32 Divider;
- UINT32 Prescaler;
- Prescaler = (AM335_I2C_SYSTEM_CLOCK_SPEED /
- AM335_I2C_INTERNAL_CLOCK_SPEED) - 1;
- AM335_I2C_WRITE(Am3I2cPrescale, Prescaler);
- Divider = (AM335_I2C_INTERNAL_CLOCK_SPEED /
- AM335_I2C_OUTPUT_CLOCK_SPEED) / 2;
- AM335_I2C_WRITE(Am3I2cSclLowTime, Divider - 7);
- AM335_I2C_WRITE(Am3I2cSclHighTime, Divider - 5);
- return;
- }
|