1 /******************************************************************************
3 * Copyright (C) 2010 - 2014 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 /*****************************************************************************/
35 * @file xiicps_options.c
37 * Contains functions for the configuration of the XIccPs driver.
40 * MODIFICATION HISTORY:
42 * Ver Who Date Changes
43 * ----- ------ -------- -----------------------------------------------
44 * 1.00a drg/jz 01/30/10 First release
45 * 1.02a sg 08/29/12 Updated the logic to arrive at the best divisors
46 * to achieve I2C clock with minimum error.
47 * This is a fix for CR #674195
48 * 1.03a hk 05/04/13 Initialized BestDivA and BestDivB to 0.
49 * This is fix for CR#704398 to remove warning.
50 * 2.0 hk 03/07/14 Limited frequency set when 100KHz or 400KHz is
51 * selected. This is a hardware limitation. CR#779290.
52 * 2.1 hk 04/24/14 Fix for CR# 761060 - provision for repeated start.
56 ******************************************************************************/
58 /***************************** Include Files *********************************/
62 /************************** Constant Definitions *****************************/
65 /**************************** Type Definitions *******************************/
68 /***************** Macros (Inline Functions) Definitions *********************/
71 /************************** Function Prototypes ******************************/
74 /************************** Variable Definitions *****************************/
76 * Create the table of options which are processed to get/set the device
77 * options. These options are table driven to allow easy maintenance and
78 * expansion of the options.
85 static OptionsMap OptionsTable[] = {
86 {XIICPS_7_BIT_ADDR_OPTION, XIICPS_CR_NEA_MASK},
87 {XIICPS_10_BIT_ADDR_OPTION, XIICPS_CR_NEA_MASK},
88 {XIICPS_SLAVE_MON_OPTION, XIICPS_CR_SLVMON_MASK},
89 {XIICPS_REP_START_OPTION, XIICPS_CR_HOLD_MASK},
92 #define XIICPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(OptionsMap))
94 /*****************************************************************************/
97 * This function sets the options for the IIC device driver. The options control
98 * how the device behaves relative to the IIC bus. The device must be idle
99 * rather than busy transferring data before setting these device options.
101 * @param InstancePtr is a pointer to the XIicPs instance.
102 * @param Options contains the specified options to be set. This is a bit
103 * mask where a 1 means to turn the option on. One or more bit
104 * values may be contained in the mask. See the bit definitions
105 * named XIICPS_*_OPTION in xiicps.h.
108 * - XST_SUCCESS if options are successfully set.
109 * - XST_DEVICE_IS_STARTED if the device is currently transferring
110 * data. The transfer must complete or be aborted before setting
115 ******************************************************************************/
116 int XIicPs_SetOptions(XIicPs *InstancePtr, u32 Options)
121 Xil_AssertNonvoid(InstancePtr != NULL);
122 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
124 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
128 * If repeated start option is requested, set the flag.
129 * The hold bit in CR will be written by driver when the next transfer
132 if (Options & XIICPS_REP_START_OPTION) {
133 InstancePtr->IsRepeatedStart = 1;
134 Options = Options & (~XIICPS_REP_START_OPTION);
138 * Loop through the options table, turning the option on.
140 for (Index = 0; Index < XIICPS_NUM_OPTIONS; Index++) {
141 if (Options & OptionsTable[Index].Option) {
143 * 10-bit option is specially treated, because it is
144 * using the 7-bit option, so turning it on means
145 * turning 7-bit option off.
147 if (OptionsTable[Index].Option &
148 XIICPS_10_BIT_ADDR_OPTION) {
150 ControlReg &= ~OptionsTable[Index].Mask;
153 ControlReg |= OptionsTable[Index].Mask;
159 * Now write to the control register. Leave it to the upper layers
160 * to restart the device.
162 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
166 * Keep a copy of what options this instance has.
168 InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
173 /*****************************************************************************/
176 * This function clears the options for the IIC device driver. The options
177 * control how the device behaves relative to the IIC bus. The device must be
178 * idle rather than busy transferring data before setting these device options.
180 * @param InstancePtr is a pointer to the XIicPs instance.
181 * @param Options contains the specified options to be cleared. This is a
182 * bit mask where a 1 means to turn the option off. One or more bit
183 * values may be contained in the mask. See the bit definitions
184 * named XIICPS_*_OPTION in xiicps.h.
187 * - XST_SUCCESS if options are successfully set.
188 * - XST_DEVICE_IS_STARTED if the device is currently transferring
189 * data. The transfer must complete or be aborted before setting
194 ******************************************************************************/
195 int XIicPs_ClearOptions(XIicPs *InstancePtr, u32 Options)
200 Xil_AssertNonvoid(InstancePtr != NULL);
201 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
203 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
207 * If repeated start option is cleared, set the flag.
208 * The hold bit in CR will be cleared by driver when the
209 * following transfer ends.
211 if (Options & XIICPS_REP_START_OPTION) {
212 InstancePtr->IsRepeatedStart = 0;
213 Options = Options & (~XIICPS_REP_START_OPTION);
217 * Loop through the options table and clear the specified options.
219 for (Index = 0; Index < XIICPS_NUM_OPTIONS; Index++) {
220 if (Options & OptionsTable[Index].Option) {
223 * 10-bit option is specially treated, because it is
224 * using the 7-bit option, so clearing it means turning
227 if (OptionsTable[Index].Option &
228 XIICPS_10_BIT_ADDR_OPTION) {
231 ControlReg |= OptionsTable[Index].Mask;
235 ControlReg &= ~OptionsTable[Index].Mask;
242 * Now write the control register. Leave it to the upper layers
243 * to restart the device.
245 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
249 * Keep a copy of what options this instance has.
251 InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
256 /*****************************************************************************/
259 * This function gets the options for the IIC device. The options control how
260 * the device behaves relative to the IIC bus.
262 * @param InstancePtr is a pointer to the XIicPs instance.
264 * @return 32 bit mask of the options, where a 1 means the option is on,
265 * and a 0 means to the option is off. One or more bit values may
266 * be contained in the mask. See the bit definitions named
267 * XIICPS_*_OPTION in the file xiicps.h.
271 ******************************************************************************/
272 u32 XIicPs_GetOptions(XIicPs *InstancePtr)
278 Xil_AssertNonvoid(InstancePtr != NULL);
279 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
282 * Read control register to find which options are currently set.
284 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
288 * Loop through the options table to determine which options are set.
290 for (Index = 0; Index < XIICPS_NUM_OPTIONS; Index++) {
291 if (ControlReg & OptionsTable[Index].Mask) {
292 OptionsFlag |= OptionsTable[Index].Option;
294 if ((ControlReg & XIICPS_CR_NEA_MASK) == 0) {
295 OptionsFlag |= XIICPS_10_BIT_ADDR_OPTION;
299 if (InstancePtr->IsRepeatedStart) {
300 OptionsFlag |= XIICPS_REP_START_OPTION;
305 /*****************************************************************************/
308 * This function sets the serial clock rate for the IIC device. The device
309 * must be idle rather than busy transferring data before setting these device
312 * The data rate is set by values in the control register. The formula for
313 * determining the correct register values is:
314 * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
315 * See the hardware data sheet for a full explanation of setting the serial
318 * @param InstancePtr is a pointer to the XIicPs instance.
319 * @param FsclHz is the clock frequency in Hz. The two most common clock
320 * rates are 100KHz and 400KHz.
323 * - XST_SUCCESS if options are successfully set.
324 * - XST_DEVICE_IS_STARTED if the device is currently transferring
325 * data. The transfer must complete or be aborted before setting
327 * - XST_FAILURE if the Fscl frequency can not be set.
329 * @note The clock can not be faster than the input clock divide by 22.
331 ******************************************************************************/
332 int XIicPs_SetSClk(XIicPs *InstancePtr, u32 FsclHz)
348 Xil_AssertNonvoid(InstancePtr != NULL);
349 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
350 Xil_AssertNonvoid(FsclHz > 0);
352 if (0 != XIicPs_In32((InstancePtr->Config.BaseAddress) +
353 XIICPS_TRANS_SIZE_OFFSET)) {
354 return XST_DEVICE_IS_STARTED;
358 * Assume Div_a is 0 and calculate (divisor_a+1) x (divisor_b+1).
360 Temp = (InstancePtr->Config.InputClockHz) / (22 * FsclHz);
363 * If the answer is negative or 0, the Fscl input is out of range.
370 * If frequency 400KHz is selected, 384.6KHz should be set.
371 * If frequency 100KHz is selected, 90KHz should be set.
372 * This is due to a hardware limitation.
374 if(FsclHz > 384600) {
378 if((FsclHz <= 100000) && (FsclHz > 90000)) {
383 * TempLimit helps in iterating over the consecutive value of Temp to
384 * find the closest clock rate achievable with divisors.
385 * Iterate over the next value only if fractional part is involved.
387 TempLimit = ((InstancePtr->Config.InputClockHz) % (22 * FsclHz)) ?
391 for ( ; Temp <= TempLimit ; Temp++)
398 for (Div_b = 0; Div_b < 64; Div_b++) {
400 Div_a = Temp / (Div_b + 1);
408 ActualFscl = (InstancePtr->Config.InputClockHz) /
409 (22 * (Div_a + 1) * (Div_b + 1));
411 if (ActualFscl > FsclHz)
412 CurrentError = (ActualFscl - FsclHz);
414 CurrentError = (FsclHz - ActualFscl);
416 if (LastError > CurrentError) {
419 LastError = CurrentError;
424 * Used to capture the best divisors.
426 if (LastError < BestError) {
427 BestError = LastError;
435 * Read the control register and mask the Divisors.
437 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
439 ControlReg &= ~(XIICPS_CR_DIV_A_MASK | XIICPS_CR_DIV_B_MASK);
440 ControlReg |= (BestDivA << XIICPS_CR_DIV_A_SHIFT) |
441 (BestDivB << XIICPS_CR_DIV_B_SHIFT);
443 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
449 /*****************************************************************************/
452 * This function gets the serial clock rate for the IIC device. The device
453 * must be idle rather than busy transferring data before setting these device
456 * @param InstancePtr is a pointer to the XIicPs instance.
458 * @return The value of the IIC clock to the nearest Hz based on the
459 * control register settings. The actual value may not be exact to
460 * to integer math rounding errors.
464 ******************************************************************************/
465 u32 XIicPs_GetSClk(XIicPs *InstancePtr)
472 Xil_AssertNonvoid(InstancePtr != NULL);
473 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
475 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
478 Div_a = (ControlReg & XIICPS_CR_DIV_A_MASK) >> XIICPS_CR_DIV_A_SHIFT;
479 Div_b = (ControlReg & XIICPS_CR_DIV_B_MASK) >> XIICPS_CR_DIV_B_SHIFT;
481 ActualFscl = (InstancePtr->Config.InputClockHz) /
482 (22 * (Div_a + 1) * (Div_b + 1));