1 /******************************************************************************
3 * (c) Copyright 2010-14 Xilinx, Inc. All rights reserved.
5 * This file contains confidential and proprietary information of Xilinx, Inc.
6 * and is protected under U.S. and international copyright and other
7 * intellectual property laws.
10 * This disclaimer is not a license and does not grant any rights to the
11 * materials distributed herewith. Except as otherwise provided in a valid
12 * license issued to you by Xilinx, and to the maximum extent permitted by
13 * applicable law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND WITH ALL
14 * FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS,
15 * IMPLIED, OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
16 * MERCHANTABILITY, NON-INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE;
17 * and (2) Xilinx shall not be liable (whether in contract or tort, including
18 * negligence, or under any other theory of liability) for any loss or damage
19 * of any kind or nature related to, arising under or in connection with these
20 * materials, including for any direct, or any indirect, special, incidental,
21 * or consequential loss or damage (including loss of data, profits, goodwill,
22 * or any type of loss or damage suffered as a result of any action brought by
23 * a third party) even if such damage or loss was reasonably foreseeable or
24 * Xilinx had been advised of the possibility of the same.
26 * CRITICAL APPLICATIONS
27 * Xilinx products are not designed or intended to be fail-safe, or for use in
28 * any application requiring fail-safe performance, such as life-support or
29 * safety devices or systems, Class III medical devices, nuclear facilities,
30 * applications related to the deployment of airbags, or any other applications
31 * that could lead to death, personal injury, or severe property or
32 * environmental damage (individually and collectively, "Critical
33 * Applications"). Customer assumes the sole risk and liability of any use of
34 * Xilinx products in Critical Applications, subject only to applicable laws
35 * and regulations governing limitations on product liability.
37 * THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS PART OF THIS FILE
40 ******************************************************************************/
41 /*****************************************************************************/
46 * Contains implementation of required functions for the XIicPs driver.
47 * See xiicps.h for detailed description of the device and driver.
49 * <pre> MODIFICATION HISTORY:
51 * Ver Who Date Changes
52 * ----- ------ -------- --------------------------------------------
53 * 1.00a drg/jz 01/30/10 First release
54 * 1.00a sdm 09/21/11 Updated the InstancePtr->Options in the
55 * XIicPs_CfgInitialize by calling XIicPs_GetOptions.
56 * 2.1 hk 04/25/14 Explicitly reset CR and clear FIFO in Abort function
57 * and state the same in the comments. CR# 784254.
58 * Fix for CR# 761060 - provision for repeated start.
62 ******************************************************************************/
64 /***************************** Include Files *********************************/
68 /************************** Constant Definitions *****************************/
70 /**************************** Type Definitions *******************************/
72 /***************** Macros (Inline Functions) Definitions *********************/
74 /************************** Function Prototypes ******************************/
76 static void StubHandler(void *CallBackRef, u32 StatusEvent);
78 /************************** Variable Definitions *****************************/
81 /*****************************************************************************/
84 * Initializes a specific XIicPs instance such that the driver is ready to use.
86 * The state of the device after initialization is:
87 * - Device is disabled
90 * @param InstancePtr is a pointer to the XIicPs instance.
91 * @param ConfigPtr is a reference to a structure containing information
92 * about a specific IIC device. This function initializes an
93 * InstancePtr object for a specific device specified by the
95 * @param EffectiveAddr is the device base address in the virtual memory
96 * address space. The caller is responsible for keeping the address
97 * mapping from EffectiveAddr to the device physical base address
98 * unchanged once this function is invoked. Unexpected errors may
99 * occur if the address mapping changes after this function is
100 * called. If address translation is not used, use
101 * ConfigPtr->BaseAddress for this parameter, passing the physical
104 * @return The return value is XST_SUCCESS if successful.
108 ******************************************************************************/
109 int XIicPs_CfgInitialize(XIicPs *InstancePtr, XIicPs_Config *ConfigPtr,
113 * Assert validates the input arguments.
115 Xil_AssertNonvoid(InstancePtr != NULL);
116 Xil_AssertNonvoid(ConfigPtr != NULL);
119 * Set some default values.
121 InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
122 InstancePtr->Config.BaseAddress = EffectiveAddr;
123 InstancePtr->Config.InputClockHz = ConfigPtr->InputClockHz;
124 InstancePtr->StatusHandler = StubHandler;
125 InstancePtr->CallBackRef = NULL;
127 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
130 * Reset the IIC device to get it into its initial state. It is expected
131 * that device configuration will take place after this initialization
132 * is done, but before the device is started.
134 XIicPs_Reset(InstancePtr);
137 * Keep a copy of what options this instance has.
139 InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
141 /* Initialize repeated start flag to 0 */
142 InstancePtr->IsRepeatedStart = 0;
147 /*****************************************************************************/
149 * Check whether the I2C bus is busy
151 * @param InstancePtr is a pointer to the XIicPs instance.
154 * - TRUE if the bus is busy.
155 * - FALSE if the bus is not busy.
159 ******************************************************************************/
160 int XIicPs_BusIsBusy(XIicPs *InstancePtr)
164 StatusReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
166 if (StatusReg & XIICPS_SR_BA_MASK) {
173 /*****************************************************************************/
176 * This is a stub for the status callback. The stub is here in case the upper
177 * layers forget to set the handler.
179 * @param CallBackRef is a pointer to the upper layer callback reference.
180 * @param StatusEvent is the event that just occurred.
181 * @param ByteCount is the number of bytes transferred up until the event
188 ******************************************************************************/
189 static void StubHandler(void *CallBackRef, u32 StatusEvent)
193 Xil_AssertVoidAlways();
197 /*****************************************************************************/
200 * Aborts a transfer in progress by resetting the FIFOs. The byte counts are
203 * @param InstancePtr is a pointer to the XIicPs instance.
209 ******************************************************************************/
210 void XIicPs_Abort(XIicPs *InstancePtr)
215 Xil_AssertVoid(InstancePtr != NULL);
216 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
219 * Enter a critical section, so disable the interrupts while we clear
220 * the FIFO and the status register.
222 IntrMaskReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
224 XIicPs_WriteReg(InstancePtr->Config.BaseAddress,
225 XIICPS_IDR_OFFSET, XIICPS_IXR_ALL_INTR_MASK);
228 * Reset the settings in config register and clear the FIFOs.
230 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
231 XIICPS_CR_RESET_VALUE | XIICPS_CR_CLR_FIFO_MASK);
234 * Read, then write the interrupt status to make sure there are no
235 * pending interrupts.
237 IntrStatusReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
239 XIicPs_WriteReg(InstancePtr->Config.BaseAddress,
240 XIICPS_ISR_OFFSET, IntrStatusReg);
243 * Restore the interrupt state.
245 IntrMaskReg = XIICPS_IXR_ALL_INTR_MASK & (~IntrMaskReg);
246 XIicPs_WriteReg(InstancePtr->Config.BaseAddress,
247 XIICPS_IER_OFFSET, IntrMaskReg);
251 /*****************************************************************************/
254 * Resets the IIC device. Reset must only be called after the driver has been
255 * initialized. The configuration of the device after reset is the same as its
256 * configuration after initialization. Any data transfer that is in progress is
259 * The upper layer software is responsible for re-configuring (if necessary)
260 * and reenabling interrupts for the IIC device after the reset.
262 * @param InstancePtr is a pointer to the XIicPs instance.
268 ******************************************************************************/
269 void XIicPs_Reset(XIicPs *InstancePtr)
272 Xil_AssertVoid(InstancePtr != NULL);
273 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
276 * Abort any transfer that is in progress.
278 XIicPs_Abort(InstancePtr);
281 * Reset any values so the software state matches the hardware device.
283 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
284 XIICPS_CR_RESET_VALUE);
285 XIicPs_WriteReg(InstancePtr->Config.BaseAddress,
286 XIICPS_TIME_OUT_OFFSET, XIICPS_TO_RESET_VALUE);
287 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_IDR_OFFSET,
288 XIICPS_IXR_ALL_INTR_MASK);
291 /*****************************************************************************/
293 * Put more data into the transmit FIFO, number of bytes is ether expected
294 * number of bytes for this transfer or available space in FIFO, which ever
297 * @param InstancePtr is a pointer to the XIicPs instance.
299 * @return Number of bytes left for this instance.
301 * @note This is function is shared by master and slave.
303 ******************************************************************************/
304 int TransmitFifoFill(XIicPs *InstancePtr)
311 * Determine number of bytes to write to FIFO.
313 AvailBytes = XIICPS_FIFO_DEPTH -
314 XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
315 XIICPS_TRANS_SIZE_OFFSET);
317 if (InstancePtr->SendByteCount > AvailBytes) {
318 NumBytesToSend = AvailBytes;
320 NumBytesToSend = InstancePtr->SendByteCount;
324 * Fill FIFO with amount determined above.
326 for (LoopCnt = 0; LoopCnt < NumBytesToSend; LoopCnt++) {
327 XIicPs_SendByte(InstancePtr);
330 return InstancePtr->SendByteCount;