1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
37 * This is an implementation of IIC driver in the PS block. The device can
38 * be either a master or a slave on the IIC bus. This implementation supports
39 * both interrupt mode transfer and polled mode transfer. Only 7-bit address
40 * is used in the driver, although the hardware also supports 10-bit address.
42 * IIC is a 2-wire serial interface. The master controls the clock, so it can
43 * regulate when it wants to send or receive data. The slave is under control of
44 * the master, it must respond quickly since it has no control of the clock and
45 * must send/receive data as fast or as slow as the master does.
47 * The higher level software must implement a higher layer protocol to inform
48 * the slave what to send to the master.
50 * <b>Initialization & Configuration</b>
52 * The XIicPs_Config structure is used by the driver to configure itself. This
53 * configuration structure is typically created by the tool-chain based on HW
56 * To support multiple runtime loading and initialization strategies employed by
57 * various operating systems, the driver instance can be initialized in the
60 * - XIicPs_LookupConfig(DeviceId) - Use the device identifier to find
61 * the static configuration structure defined in xiicps_g.c. This is
62 * setup by the tools. For some operating systems the config structure
63 * will be initialized by the software and this call is not needed.
65 * - XIicPs_CfgInitialize(InstancePtr, CfgPtr, EffectiveAddr) - Uses a
66 * configuration structure provided by the caller. If running in a
67 * system with address translation, the provided virtual memory base
68 * address replaces the physical address in the configuration
71 * <b>Multiple Masters</b>
73 * More than one master can exist, bus arbitration is defined in the IIC
74 * standard. Lost of arbitration causes arbitration loss interrupt on the device.
76 * <b>Multiple Slaves</b>
78 * Multiple slaves are supported by selecting them with unique addresses. It is
79 * up to the system designer to be sure all devices on the IIC bus have
84 * The IIC hardware can use 7 or 10 bit addresses. The driver provides the
85 * ability to control which address size is sent in messages as a master to a
89 * The hardware FIFO is 32 bytes deep. The user must know the limitations of
90 * other IIC devices on the bus. Some are only able to receive a limited number
91 * of bytes in a single transfer.
95 * The data rate is set by values in the control register. The formula for
96 * determining the correct register values is:
97 * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
99 * When the device is configured as a slave, the slck setting controls the
100 * sample rate and so must be set to be at least as fast as the fastest scl
101 * expected to be seen in the system.
103 * <b>Polled Mode Operation</b>
105 * This driver supports polled mode transfers.
109 * The user must connect the interrupt handler of the driver,
110 * XIicPs_InterruptHandler to an interrupt system such that it will be called
111 * when an interrupt occurs. This function does not save and restore the
112 * processor context such that the user must provide this processing.
114 * The driver handles the following interrupts:
115 * - Transfer complete
117 * - Transfer not Acknowledged
118 * - Transfer Time out
119 * - Monitored slave ready - master mode only
121 * - Transmit FIFO overflow
122 * - Receive FIFO underflow
127 * Bus busy is checked before the setup of a master mode device, to avoid
128 * unnecessary arbitration loss interrupt.
130 * <b>RTOS Independence</b>
132 * This driver is intended to be RTOS and processor independent. It works with
133 * physical addresses only. Any needs for dynamic memory management, threads or
134 * thread mutual exclusion, virtual memory, or cache control must be satisfied by
135 * the layer above this driver.
137 *<b>Repeated Start</b>
139 * The I2C controller does not indicate completion of a receive transfer if HOLD
140 * bit is set. Due to this errata, repeated start cannot be used if a receive
141 * transfer is followed by any other transfer.
143 * <pre> MODIFICATION HISTORY:
145 * Ver Who Date Changes
146 * ----- ------ -------- -----------------------------------------------
147 * 1.00a drg/jz 01/30/08 First release
148 * 1.00a sdm 09/21/11 Fixed an issue in the XIicPs_SetOptions and
149 * XIicPs_ClearOptions where the InstancePtr->Options
150 * was not updated correctly.
151 * Updated the InstancePtr->Options in the
152 * XIicPs_CfgInitialize by calling XIicPs_GetOptions.
153 * Updated the XIicPs_SetupMaster to not check for
154 * Bus Busy condition when the Hold Bit is set.
155 * Removed some unused variables.
156 * 1.01a sg 03/30/12 Fixed an issue in XIicPs_MasterSendPolled where a
157 * check for transfer completion is added, which indicates
158 * the completion of current transfer.
159 * 1.02a sg 08/29/12 Updated the logic to arrive at the best divisors
160 * to achieve I2C clock with minimum error for
162 * 1.03a hk 05/04/13 Initialized BestDivA and BestDivB to 0.
163 * This is fix for CR#704398 to remove warning.
164 * 2.0 hk 03/07/14 Added check for error status in the while loop that
165 * checks for completion.
166 * (XIicPs_MasterSendPolled function). CR# 762244, 764875.
167 * Limited frequency set when 100KHz or 400KHz is
168 * selected. This is a hardware limitation. CR#779290.
169 * 2.1 hk 04/24/14 Fix for CR# 789821 to handle >14 byte transfers.
170 * Explicitly reset CR and clear FIFO in Abort function
171 * and state the same in the comments. CR# 784254.
172 * Fix for CR# 761060 - provision for repeated start.
173 * 2.2 hk 08/23/14 Slave monitor mode changes - clear FIFO, enable
174 * read mode and clear transfer size register.
175 * Disable NACK to avoid interrupts on each retry.
176 * 2.3 sk 10/07/14 Repeated start feature deleted.
177 * 3.0 sk 11/03/14 Modified TimeOut Register value to 0xFF
179 * 12/06/14 Implemented Repeated start feature.
180 * 01/31/15 Modified the code according to MISRAC 2012 Compliant.
181 * 02/18/15 Implemented larger data transfer using repeated start
182 * in Zynq UltraScale MP.
186 ******************************************************************************/
188 #ifndef XIICPS_H /* prevent circular inclusions */
189 #define XIICPS_H /* by using protection macros */
195 /***************************** Include Files *********************************/
197 #include "xil_types.h"
198 #include "xil_assert.h"
200 #include "xiicps_hw.h"
201 #include "xplatform_info.h"
203 /************************** Constant Definitions *****************************/
205 /** @name Configuration options
207 * The following options may be specified or retrieved for the device and
208 * enable/disable additional features of the IIC. Each of the options
209 * are bit fields, so more than one may be specified.
213 #define XIICPS_7_BIT_ADDR_OPTION 0x01U /**< 7-bit address mode */
214 #define XIICPS_10_BIT_ADDR_OPTION 0x02U /**< 10-bit address mode */
215 #define XIICPS_SLAVE_MON_OPTION 0x04U /**< Slave monitor mode */
216 #define XIICPS_REP_START_OPTION 0x08U /**< Repeated Start */
219 /** @name Callback events
221 * These constants specify the handler events that are passed to an application
222 * event handler from the driver. These constants are bit masks such that
223 * more than one event can be passed to the handler.
227 #define XIICPS_EVENT_COMPLETE_SEND 0x0001U /**< Transmit Complete Event*/
228 #define XIICPS_EVENT_COMPLETE_RECV 0x0002U /**< Receive Complete Event*/
229 #define XIICPS_EVENT_TIME_OUT 0x0004U /**< Transfer timed out */
230 #define XIICPS_EVENT_ERROR 0x0008U /**< Receive error */
231 #define XIICPS_EVENT_ARB_LOST 0x0010U /**< Arbitration lost */
232 #define XIICPS_EVENT_NACK 0x0020U /**< NACK Received */
233 #define XIICPS_EVENT_SLAVE_RDY 0x0040U /**< Slave ready */
234 #define XIICPS_EVENT_RX_OVR 0x0080U /**< RX overflow */
235 #define XIICPS_EVENT_TX_OVR 0x0100U /**< TX overflow */
236 #define XIICPS_EVENT_RX_UNF 0x0200U /**< RX underflow */
239 /** @name Role constants
241 * These constants are used to pass into the device setup routines to
242 * set up the device according to transfer direction.
244 #define SENDING_ROLE 1 /**< Transfer direction is sending */
245 #define RECVING_ROLE 0 /**< Transfer direction is receiving */
247 /* Maximum transfer size */
248 #define XIICPS_MAX_TRANSFER_SIZE (u32)(255U - 3U)
250 /**************************** Type Definitions *******************************/
253 * The handler data type allows the user to define a callback function to
254 * respond to interrupt events in the system. This function is executed
255 * in interrupt context, so amount of processing should be minimized.
257 * @param CallBackRef is the callback reference passed in by the upper
258 * layer when setting the callback functions, and passed back to
259 * the upper layer when the callback is invoked. Its type is
260 * not important to the driver, so it is a void pointer.
261 * @param StatusEvent indicates one or more status events that occurred.
263 typedef void (*XIicPs_IntrHandler) (void *CallBackRef, u32 StatusEvent);
266 * This typedef contains configuration information for the device.
269 u16 DeviceId; /**< Unique ID of device */
270 u32 BaseAddress; /**< Base address of the device */
271 u32 InputClockHz; /**< Input clock frequency */
275 * The XIicPs driver instance data. The user is required to allocate a
276 * variable of this type for each IIC device in the system. A pointer
277 * to a variable of this type is then passed to the driver API functions.
280 XIicPs_Config Config; /* Configuration structure */
281 u32 IsReady; /* Device is initialized and ready */
282 u32 Options; /* Options set in the device */
284 u8 *SendBufferPtr; /* Pointer to send buffer */
285 u8 *RecvBufferPtr; /* Pointer to recv buffer */
286 s32 SendByteCount; /* Number of bytes still expected to send */
287 s32 RecvByteCount; /* Number of bytes still expected to receive */
288 s32 CurrByteCount; /* No. of bytes expected in current transfer */
290 s32 UpdateTxSize; /* If tx size register has to be updated */
291 s32 IsSend; /* Whether master is sending or receiving */
292 s32 IsRepeatedStart; /* Indicates if user set repeated start */
294 XIicPs_IntrHandler StatusHandler; /* Event handler function */
295 void *CallBackRef; /* Callback reference for event handler */
298 /***************** Macros (Inline Functions) Definitions *********************/
299 /****************************************************************************/
302 * Place one byte into the transmit FIFO.
304 * @param InstancePtr is the instance of IIC
308 * @note C-Style signature:
309 * void XIicPs_SendByte(XIicPs *InstancePtr)
311 *****************************************************************************/
312 #define XIicPs_SendByte(InstancePtr) \
315 Data = *((InstancePtr)->SendBufferPtr); \
316 XIicPs_Out32((InstancePtr)->Config.BaseAddress \
317 + (u32)(XIICPS_DATA_OFFSET), \
319 (InstancePtr)->SendBufferPtr += 1; \
320 (InstancePtr)->SendByteCount -= 1;\
323 /****************************************************************************/
326 * Receive one byte from FIFO.
328 * @param InstancePtr is the instance of IIC
332 * @note C-Style signature:
333 * u8 XIicPs_RecvByte(XIicPs *InstancePtr)
335 *****************************************************************************/
336 #define XIicPs_RecvByte(InstancePtr) \
339 Value = (u8)(XIicPs_In32((InstancePtr)->Config.BaseAddress \
340 + (u32)XIICPS_DATA_OFFSET)); \
342 *(InstancePtr)->RecvBufferPtr = *Data; \
343 (InstancePtr)->RecvBufferPtr += 1; \
344 (InstancePtr)->RecvByteCount --; \
347 /************************** Function Prototypes ******************************/
350 * Function for configuration lookup, in xiicps_sinit.c
352 XIicPs_Config *XIicPs_LookupConfig(u16 DeviceId);
355 * Functions for general setup, in xiicps.c
357 s32 XIicPs_CfgInitialize(XIicPs *InstancePtr, XIicPs_Config * ConfigPtr,
360 void XIicPs_Abort(XIicPs *InstancePtr);
361 void XIicPs_Reset(XIicPs *InstancePtr);
363 s32 XIicPs_BusIsBusy(XIicPs *InstancePtr);
364 s32 TransmitFifoFill(XIicPs *InstancePtr);
367 * Functions for interrupts, in xiicps_intr.c
369 void XIicPs_SetStatusHandler(XIicPs *InstancePtr, void *CallBackRef,
370 XIicPs_IntrHandler FunctionPtr);
373 * Functions for device as master, in xiicps_master.c
375 void XIicPs_MasterSend(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
377 void XIicPs_MasterRecv(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
379 s32 XIicPs_MasterSendPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
381 s32 XIicPs_MasterRecvPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount,
383 void XIicPs_EnableSlaveMonitor(XIicPs *InstancePtr, u16 SlaveAddr);
384 void XIicPs_DisableSlaveMonitor(XIicPs *InstancePtr);
385 void XIicPs_MasterInterruptHandler(XIicPs *InstancePtr);
388 * Functions for device as slave, in xiicps_slave.c
390 void XIicPs_SetupSlave(XIicPs *InstancePtr, u16 SlaveAddr);
391 void XIicPs_SlaveSend(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
392 void XIicPs_SlaveRecv(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
393 s32 XIicPs_SlaveSendPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
394 s32 XIicPs_SlaveRecvPolled(XIicPs *InstancePtr, u8 *MsgPtr, s32 ByteCount);
395 void XIicPs_SlaveInterruptHandler(XIicPs *InstancePtr);
398 * Functions for selftest, in xiicps_selftest.c
400 s32 XIicPs_SelfTest(XIicPs *InstancePtr);
403 * Functions for setting and getting data rate, in xiicps_options.c
405 s32 XIicPs_SetOptions(XIicPs *InstancePtr, u32 Options);
406 s32 XIicPs_ClearOptions(XIicPs *InstancePtr, u32 Options);
407 u32 XIicPs_GetOptions(XIicPs *InstancePtr);
409 s32 XIicPs_SetSClk(XIicPs *InstancePtr, u32 FsclHz);
410 u32 XIicPs_GetSClk(XIicPs *InstancePtr);
416 #endif /* end of protection macro */