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 /*****************************************************************************/
44 * @file xiicps_options.c
46 * Contains functions for the configuration of the XIccPs driver.
49 * MODIFICATION HISTORY:
51 * Ver Who Date Changes
52 * ----- ------ -------- -----------------------------------------------
53 * 1.00a drg/jz 01/30/10 First release
54 * 1.02a sg 08/29/12 Updated the logic to arrive at the best divisors
55 * to achieve I2C clock with minimum error.
56 * This is a fix for CR #674195
57 * 1.03a hk 05/04/13 Initialized BestDivA and BestDivB to 0.
58 * This is fix for CR#704398 to remove warning.
59 * 2.0 hk 03/07/14 Limited frequency set when 100KHz or 400KHz is
60 * selected. This is a hardware limitation. CR#779290.
61 * 2.1 hk 04/24/14 Fix for CR# 761060 - provision for repeated start.
65 ******************************************************************************/
67 /***************************** Include Files *********************************/
71 /************************** Constant Definitions *****************************/
74 /**************************** Type Definitions *******************************/
77 /***************** Macros (Inline Functions) Definitions *********************/
80 /************************** Function Prototypes ******************************/
83 /************************** Variable Definitions *****************************/
85 * Create the table of options which are processed to get/set the device
86 * options. These options are table driven to allow easy maintenance and
87 * expansion of the options.
94 static OptionsMap OptionsTable[] = {
95 {XIICPS_7_BIT_ADDR_OPTION, XIICPS_CR_NEA_MASK},
96 {XIICPS_10_BIT_ADDR_OPTION, XIICPS_CR_NEA_MASK},
97 {XIICPS_SLAVE_MON_OPTION, XIICPS_CR_SLVMON_MASK},
98 {XIICPS_REP_START_OPTION, XIICPS_CR_HOLD_MASK},
101 #define XIICPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(OptionsMap))
103 /*****************************************************************************/
106 * This function sets the options for the IIC device driver. The options control
107 * how the device behaves relative to the IIC bus. The device must be idle
108 * rather than busy transferring data before setting these device options.
110 * @param InstancePtr is a pointer to the XIicPs instance.
111 * @param Options contains the specified options to be set. This is a bit
112 * mask where a 1 means to turn the option on. One or more bit
113 * values may be contained in the mask. See the bit definitions
114 * named XIICPS_*_OPTION in xiicps.h.
117 * - XST_SUCCESS if options are successfully set.
118 * - XST_DEVICE_IS_STARTED if the device is currently transferring
119 * data. The transfer must complete or be aborted before setting
124 ******************************************************************************/
125 int XIicPs_SetOptions(XIicPs *InstancePtr, u32 Options)
130 Xil_AssertNonvoid(InstancePtr != NULL);
131 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
133 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
137 * If repeated start option is requested, set the flag.
138 * The hold bit in CR will be written by driver when the next transfer
141 if (Options & XIICPS_REP_START_OPTION) {
142 InstancePtr->IsRepeatedStart = 1;
143 Options = Options & (~XIICPS_REP_START_OPTION);
147 * Loop through the options table, turning the option on.
149 for (Index = 0; Index < XIICPS_NUM_OPTIONS; Index++) {
150 if (Options & OptionsTable[Index].Option) {
152 * 10-bit option is specially treated, because it is
153 * using the 7-bit option, so turning it on means
154 * turning 7-bit option off.
156 if (OptionsTable[Index].Option &
157 XIICPS_10_BIT_ADDR_OPTION) {
159 ControlReg &= ~OptionsTable[Index].Mask;
162 ControlReg |= OptionsTable[Index].Mask;
168 * Now write to the control register. Leave it to the upper layers
169 * to restart the device.
171 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
175 * Keep a copy of what options this instance has.
177 InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
182 /*****************************************************************************/
185 * This function clears the options for the IIC device driver. The options
186 * control how the device behaves relative to the IIC bus. The device must be
187 * idle rather than busy transferring data before setting these device options.
189 * @param InstancePtr is a pointer to the XIicPs instance.
190 * @param Options contains the specified options to be cleared. This is a
191 * bit mask where a 1 means to turn the option off. One or more bit
192 * values may be contained in the mask. See the bit definitions
193 * named XIICPS_*_OPTION in xiicps.h.
196 * - XST_SUCCESS if options are successfully set.
197 * - XST_DEVICE_IS_STARTED if the device is currently transferring
198 * data. The transfer must complete or be aborted before setting
203 ******************************************************************************/
204 int XIicPs_ClearOptions(XIicPs *InstancePtr, u32 Options)
209 Xil_AssertNonvoid(InstancePtr != NULL);
210 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
212 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
216 * If repeated start option is cleared, set the flag.
217 * The hold bit in CR will be cleared by driver when the
218 * following transfer ends.
220 if (Options & XIICPS_REP_START_OPTION) {
221 InstancePtr->IsRepeatedStart = 0;
222 Options = Options & (~XIICPS_REP_START_OPTION);
226 * Loop through the options table and clear the specified options.
228 for (Index = 0; Index < XIICPS_NUM_OPTIONS; Index++) {
229 if (Options & OptionsTable[Index].Option) {
232 * 10-bit option is specially treated, because it is
233 * using the 7-bit option, so clearing it means turning
236 if (OptionsTable[Index].Option &
237 XIICPS_10_BIT_ADDR_OPTION) {
240 ControlReg |= OptionsTable[Index].Mask;
244 ControlReg &= ~OptionsTable[Index].Mask;
251 * Now write the control register. Leave it to the upper layers
252 * to restart the device.
254 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
258 * Keep a copy of what options this instance has.
260 InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
265 /*****************************************************************************/
268 * This function gets the options for the IIC device. The options control how
269 * the device behaves relative to the IIC bus.
271 * @param InstancePtr is a pointer to the XIicPs instance.
273 * @return 32 bit mask of the options, where a 1 means the option is on,
274 * and a 0 means to the option is off. One or more bit values may
275 * be contained in the mask. See the bit definitions named
276 * XIICPS_*_OPTION in the file xiicps.h.
280 ******************************************************************************/
281 u32 XIicPs_GetOptions(XIicPs *InstancePtr)
287 Xil_AssertNonvoid(InstancePtr != NULL);
288 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
291 * Read control register to find which options are currently set.
293 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
297 * Loop through the options table to determine which options are set.
299 for (Index = 0; Index < XIICPS_NUM_OPTIONS; Index++) {
300 if (ControlReg & OptionsTable[Index].Mask) {
301 OptionsFlag |= OptionsTable[Index].Option;
303 if ((ControlReg & XIICPS_CR_NEA_MASK) == 0) {
304 OptionsFlag |= XIICPS_10_BIT_ADDR_OPTION;
308 if (InstancePtr->IsRepeatedStart) {
309 OptionsFlag |= XIICPS_REP_START_OPTION;
314 /*****************************************************************************/
317 * This function sets the serial clock rate for the IIC device. The device
318 * must be idle rather than busy transferring data before setting these device
321 * The data rate is set by values in the control register. The formula for
322 * determining the correct register values is:
323 * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
324 * See the hardware data sheet for a full explanation of setting the serial
327 * @param InstancePtr is a pointer to the XIicPs instance.
328 * @param FsclHz is the clock frequency in Hz. The two most common clock
329 * rates are 100KHz and 400KHz.
332 * - XST_SUCCESS if options are successfully set.
333 * - XST_DEVICE_IS_STARTED if the device is currently transferring
334 * data. The transfer must complete or be aborted before setting
336 * - XST_FAILURE if the Fscl frequency can not be set.
338 * @note The clock can not be faster than the input clock divide by 22.
340 ******************************************************************************/
341 int XIicPs_SetSClk(XIicPs *InstancePtr, u32 FsclHz)
357 Xil_AssertNonvoid(InstancePtr != NULL);
358 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
359 Xil_AssertNonvoid(FsclHz > 0);
361 if (0 != XIicPs_In32((InstancePtr->Config.BaseAddress) +
362 XIICPS_TRANS_SIZE_OFFSET)) {
363 return XST_DEVICE_IS_STARTED;
367 * Assume Div_a is 0 and calculate (divisor_a+1) x (divisor_b+1).
369 Temp = (InstancePtr->Config.InputClockHz) / (22 * FsclHz);
372 * If the answer is negative or 0, the Fscl input is out of range.
379 * If frequency 400KHz is selected, 384.6KHz should be set.
380 * If frequency 100KHz is selected, 90KHz should be set.
381 * This is due to a hardware limitation.
383 if(FsclHz > 384600) {
387 if((FsclHz <= 100000) && (FsclHz > 90000)) {
392 * TempLimit helps in iterating over the consecutive value of Temp to
393 * find the closest clock rate achievable with divisors.
394 * Iterate over the next value only if fractional part is involved.
396 TempLimit = ((InstancePtr->Config.InputClockHz) % (22 * FsclHz)) ?
400 for ( ; Temp <= TempLimit ; Temp++)
407 for (Div_b = 0; Div_b < 64; Div_b++) {
409 Div_a = Temp / (Div_b + 1);
417 ActualFscl = (InstancePtr->Config.InputClockHz) /
418 (22 * (Div_a + 1) * (Div_b + 1));
420 if (ActualFscl > FsclHz)
421 CurrentError = (ActualFscl - FsclHz);
423 CurrentError = (FsclHz - ActualFscl);
425 if (LastError > CurrentError) {
428 LastError = CurrentError;
433 * Used to capture the best divisors.
435 if (LastError < BestError) {
436 BestError = LastError;
444 * Read the control register and mask the Divisors.
446 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
448 ControlReg &= ~(XIICPS_CR_DIV_A_MASK | XIICPS_CR_DIV_B_MASK);
449 ControlReg |= (BestDivA << XIICPS_CR_DIV_A_SHIFT) |
450 (BestDivB << XIICPS_CR_DIV_B_SHIFT);
452 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
458 /*****************************************************************************/
461 * This function gets the serial clock rate for the IIC device. The device
462 * must be idle rather than busy transferring data before setting these device
465 * @param InstancePtr is a pointer to the XIicPs instance.
467 * @return The value of the IIC clock to the nearest Hz based on the
468 * control register settings. The actual value may not be exact to
469 * to integer math rounding errors.
473 ******************************************************************************/
474 u32 XIicPs_GetSClk(XIicPs *InstancePtr)
481 Xil_AssertNonvoid(InstancePtr != NULL);
482 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
484 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
487 Div_a = (ControlReg & XIICPS_CR_DIV_A_MASK) >> XIICPS_CR_DIV_A_SHIFT;
488 Div_b = (ControlReg & XIICPS_CR_DIV_B_MASK) >> XIICPS_CR_DIV_B_SHIFT;
490 ActualFscl = (InstancePtr->Config.InputClockHz) /
491 (22 * (Div_a + 1) * (Div_b + 1));