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 /*****************************************************************************/
35 * @file xqspips_options.c
36 * @addtogroup qspips_v3_2
39 * Contains functions for the configuration of the XQspiPs driver component.
42 * MODIFICATION HISTORY:
44 * Ver Who Date Changes
45 * ----- --- -------- -----------------------------------------------
46 * 1.00 sdm 11/25/10 First release
47 * 2.00a kka 07/25/12 Removed the selection for the following options:
48 * Master mode (XQSPIPS_MASTER_OPTION) and
49 * Flash interface mode (XQSPIPS_FLASH_MODE_OPTION) option
50 * as the QSPI driver supports the Master mode
51 * and Flash Interface mode. The driver doesnot support
52 * Slave mode or the legacy mode.
53 * Added the option for setting the Holdb_dr bit in the
54 * configuration options, XQSPIPS_HOLD_B_DRIVE_OPTION
55 * is the option to be used for setting this bit in the
56 * configuration register.
57 * 2.01a sg 02/03/13 SetDelays and GetDelays API's include DelayNss parameter.
59 * 2.02a hk 26/03/13 Removed XQspi_Reset() in Set_Options() function when
60 * LQSPI_MODE_OPTION is set. Moved Enable() to XQpsiPs_LqspiRead().
61 * 3.3 sk 11/07/15 Modified the API prototypes according to MISRAC standards
62 * to remove compilation warnings. CR# 868893.
65 ******************************************************************************/
67 /***************************** Include Files *********************************/
71 /************************** Constant Definitions *****************************/
73 /**************************** Type Definitions *******************************/
75 /***************** Macros (Inline Functions) Definitions *********************/
77 /************************** Function Prototypes ******************************/
79 /************************** Variable Definitions *****************************/
82 * Create the table of options which are processed to get/set the device
83 * options. These options are table driven to allow easy maintenance and
84 * expansion of the options.
91 static OptionsMap OptionsTable[] = {
92 {XQSPIPS_CLK_ACTIVE_LOW_OPTION, XQSPIPS_CR_CPOL_MASK},
93 {XQSPIPS_CLK_PHASE_1_OPTION, XQSPIPS_CR_CPHA_MASK},
94 {XQSPIPS_FORCE_SSELECT_OPTION, XQSPIPS_CR_SSFORCE_MASK},
95 {XQSPIPS_MANUAL_START_OPTION, XQSPIPS_CR_MANSTRTEN_MASK},
96 {XQSPIPS_HOLD_B_DRIVE_OPTION, XQSPIPS_CR_HOLD_B_MASK},
99 #define XQSPIPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(OptionsMap))
101 /*****************************************************************************/
104 * This function sets the options for the QSPI device driver. The options control
105 * how the device behaves relative to the QSPI bus. The device must be idle
106 * rather than busy transferring data before setting these device options.
108 * @param InstancePtr is a pointer to the XQspiPs instance.
109 * @param Options contains the specified options to be set. This is a bit
110 * mask where a 1 means to turn the option on, and a 0 means to
111 * turn the option off. One or more bit values may be contained in
112 * the mask. See the bit definitions named XQSPIPS_*_OPTIONS in
113 * the file xqspips.h.
116 * - XST_SUCCESS if options are successfully set.
117 * - XST_DEVICE_BUSY if the device is currently transferring data.
118 * The transfer must complete or be aborted before setting options.
121 * This function is not thread-safe.
123 ******************************************************************************/
124 s32 XQspiPs_SetOptions(XQspiPs *InstancePtr, u32 Options)
130 Xil_AssertNonvoid(InstancePtr != NULL);
131 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
134 * Do not allow to modify the Control Register while a transfer is in
135 * progress. Not thread-safe.
137 if (InstancePtr->IsBusy) {
138 return XST_DEVICE_BUSY;
141 QspiOptions = Options & XQSPIPS_LQSPI_MODE_OPTION;
142 Options &= ~XQSPIPS_LQSPI_MODE_OPTION;
144 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
148 * Loop through the options table, turning the option on or off
149 * depending on whether the bit is set in the incoming options flag.
151 for (Index = 0; Index < XQSPIPS_NUM_OPTIONS; Index++) {
152 if (Options & OptionsTable[Index].Option) {
154 ConfigReg |= OptionsTable[Index].Mask;
157 ConfigReg &= ~(OptionsTable[Index].Mask);
162 * Now write the control register. Leave it to the upper layers
163 * to restart the device.
165 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress, XQSPIPS_CR_OFFSET,
169 * Check for the LQSPI configuration options.
171 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
172 XQSPIPS_LQSPI_CR_OFFSET);
175 if (QspiOptions & XQSPIPS_LQSPI_MODE_OPTION) {
176 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
177 XQSPIPS_LQSPI_CR_OFFSET,
178 XQSPIPS_LQSPI_CR_RST_STATE);
179 XQspiPs_SetSlaveSelect(InstancePtr);
181 ConfigReg &= ~XQSPIPS_LQSPI_CR_LINEAR_MASK;
182 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
183 XQSPIPS_LQSPI_CR_OFFSET, ConfigReg);
189 /*****************************************************************************/
192 * This function gets the options for the QSPI device. The options control how
193 * the device behaves relative to the QSPI bus.
195 * @param InstancePtr is a pointer to the XQspiPs instance.
199 * Options contains the specified options currently set. This is a bit value
200 * where a 1 means the option is on, and a 0 means the option is off.
201 * See the bit definitions named XQSPIPS_*_OPTIONS in file xqspips.h.
205 ******************************************************************************/
206 u32 XQspiPs_GetOptions(XQspiPs *InstancePtr)
212 Xil_AssertNonvoid(InstancePtr != NULL);
213 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
216 * Get the current options from QSPI configuration register.
218 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
222 * Loop through the options table to grab options
224 for (Index = 0; Index < XQSPIPS_NUM_OPTIONS; Index++) {
225 if (ConfigReg & OptionsTable[Index].Mask) {
226 OptionsFlag |= OptionsTable[Index].Option;
231 * Check for the LQSPI configuration options.
233 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
234 XQSPIPS_LQSPI_CR_OFFSET);
236 if ((ConfigReg & XQSPIPS_LQSPI_CR_LINEAR_MASK) != 0) {
237 OptionsFlag |= XQSPIPS_LQSPI_MODE_OPTION;
243 /*****************************************************************************/
246 * This function sets the clock prescaler for an QSPI device. The device
247 * must be idle rather than busy transferring data before setting these device
250 * @param InstancePtr is a pointer to the XQspiPs instance.
251 * @param Prescaler is the value that determine how much the clock should
252 * be divided by. Use the XQSPIPS_CLK_PRESCALE_* constants defined
253 * in xqspips.h for this setting.
256 * - XST_SUCCESS if options are successfully set.
257 * - XST_DEVICE_BUSY if the device is currently transferring data.
258 * The transfer must complete or be aborted before setting options.
261 * This function is not thread-safe.
263 ******************************************************************************/
264 s32 XQspiPs_SetClkPrescaler(XQspiPs *InstancePtr, u8 Prescaler)
268 Xil_AssertNonvoid(InstancePtr != NULL);
269 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
270 Xil_AssertNonvoid(Prescaler <= XQSPIPS_CR_PRESC_MAXIMUM);
273 * Do not allow the slave select to change while a transfer is in
274 * progress. Not thread-safe.
276 if (InstancePtr->IsBusy) {
277 return XST_DEVICE_BUSY;
281 * Read the configuration register, mask out the interesting bits, and set
282 * them with the shifted value passed into the function. Write the
283 * results back to the configuration register.
285 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
288 ConfigReg &= ~XQSPIPS_CR_PRESC_MASK;
289 ConfigReg |= (u32) (Prescaler & XQSPIPS_CR_PRESC_MAXIMUM) <<
290 XQSPIPS_CR_PRESC_SHIFT;
292 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
299 /*****************************************************************************/
302 * This function gets the clock prescaler of an QSPI device.
304 * @param InstancePtr is a pointer to the XQspiPs instance.
306 * @return The prescaler value.
311 ******************************************************************************/
312 u8 XQspiPs_GetClkPrescaler(XQspiPs *InstancePtr)
316 Xil_AssertNonvoid(InstancePtr != NULL);
317 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
319 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
322 ConfigReg &= XQSPIPS_CR_PRESC_MASK;
324 return (u8)(ConfigReg >> XQSPIPS_CR_PRESC_SHIFT);
327 /*****************************************************************************/
330 * This function sets the delay register for the QSPI device driver.
331 * The delay register controls the Delay Between Transfers, Delay After
332 * Transfers, and the Delay Initially. The default value is 0x0. The range of
333 * each delay value is 0-255.
335 * @param InstancePtr is a pointer to the XQspiPs instance.
336 * @param DelayNss is the delay to de-assert slave select between
337 * two word transfers.
338 * @param DelayBtwn is the delay between one Slave Select being
339 * de-activated and the activation of another slave. The delay is
340 * the number of master clock periods given by DelayBtwn + 2.
341 * @param DelayAfter define the delay between the last bit of the current
342 * byte transfer and the first bit of the next byte transfer.
343 * The delay in number of master clock periods is given as:
344 * CHPA=0:DelayInit+DelayAfter+3
345 * CHPA=1:DelayAfter+1
346 * @param DelayInit is the delay between asserting the slave select signal
347 * and the first bit transfer. The delay int number of master clock
348 * periods is DelayInit+1.
351 * - XST_SUCCESS if delays are successfully set.
352 * - XST_DEVICE_BUSY if the device is currently transferring data.
353 * The transfer must complete or be aborted before setting options.
357 ******************************************************************************/
358 int XQspiPs_SetDelays(XQspiPs *InstancePtr, u8 DelayNss, u8 DelayBtwn,
359 u8 DelayAfter, u8 DelayInit)
363 Xil_AssertNonvoid(InstancePtr != NULL);
364 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
367 * Do not allow the delays to change while a transfer is in
368 * progress. Not thread-safe.
370 if (InstancePtr->IsBusy) {
371 return XST_DEVICE_BUSY;
374 /* Shift, Mask and OR the values to build the register settings */
375 DelayRegister = (u32) DelayNss << XQSPIPS_DR_NSS_SHIFT;
376 DelayRegister |= (u32) DelayBtwn << XQSPIPS_DR_BTWN_SHIFT;
377 DelayRegister |= (u32) DelayAfter << XQSPIPS_DR_AFTER_SHIFT;
378 DelayRegister |= (u32) DelayInit;
380 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
381 XQSPIPS_DR_OFFSET, DelayRegister);
386 /*****************************************************************************/
389 * This function gets the delay settings for an QSPI device.
390 * The delay register controls the Delay Between Transfers, Delay After
391 * Transfers, and the Delay Initially. The default value is 0x0.
393 * @param InstancePtr is a pointer to the XQspiPs instance.
394 * @param DelayNss is a pointer to the Delay to de-assert slave select
395 * between two word transfers.
396 * @param DelayBtwn is a pointer to the Delay Between transfers value.
397 * This is a return parameter.
398 * @param DelayAfter is a pointer to the Delay After transfer value.
399 * This is a return parameter.
400 * @param DelayInit is a pointer to the Delay Initially value. This is
401 * a return parameter.
407 ******************************************************************************/
408 void XQspiPs_GetDelays(XQspiPs *InstancePtr, u8 *DelayNss, u8 *DelayBtwn,
409 u8 *DelayAfter, u8 *DelayInit)
413 Xil_AssertVoid(InstancePtr != NULL);
414 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
416 DelayRegister = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
419 *DelayInit = (u8)(DelayRegister & XQSPIPS_DR_INIT_MASK);
421 *DelayAfter = (u8)((DelayRegister & XQSPIPS_DR_AFTER_MASK) >>
422 XQSPIPS_DR_AFTER_SHIFT);
424 *DelayBtwn = (u8)((DelayRegister & XQSPIPS_DR_BTWN_MASK) >>
425 XQSPIPS_DR_BTWN_SHIFT);
427 *DelayNss = (u8)((DelayRegister & XQSPIPS_DR_NSS_MASK) >>
428 XQSPIPS_DR_NSS_SHIFT);