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 xqspips_options.c
36 * @addtogroup qspips_v3_0
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().
63 ******************************************************************************/
65 /***************************** Include Files *********************************/
69 /************************** Constant Definitions *****************************/
71 /**************************** Type Definitions *******************************/
73 /***************** Macros (Inline Functions) Definitions *********************/
75 /************************** Function Prototypes ******************************/
77 /************************** Variable Definitions *****************************/
80 * Create the table of options which are processed to get/set the device
81 * options. These options are table driven to allow easy maintenance and
82 * expansion of the options.
89 static OptionsMap OptionsTable[] = {
90 {XQSPIPS_CLK_ACTIVE_LOW_OPTION, XQSPIPS_CR_CPOL_MASK},
91 {XQSPIPS_CLK_PHASE_1_OPTION, XQSPIPS_CR_CPHA_MASK},
92 {XQSPIPS_FORCE_SSELECT_OPTION, XQSPIPS_CR_SSFORCE_MASK},
93 {XQSPIPS_MANUAL_START_OPTION, XQSPIPS_CR_MANSTRTEN_MASK},
94 {XQSPIPS_HOLD_B_DRIVE_OPTION, XQSPIPS_CR_HOLD_B_MASK},
97 #define XQSPIPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(OptionsMap))
99 /*****************************************************************************/
102 * This function sets the options for the QSPI device driver. The options control
103 * how the device behaves relative to the QSPI bus. The device must be idle
104 * rather than busy transferring data before setting these device options.
106 * @param InstancePtr is a pointer to the XQspiPs instance.
107 * @param Options contains the specified options to be set. This is a bit
108 * mask where a 1 means to turn the option on, and a 0 means to
109 * turn the option off. One or more bit values may be contained in
110 * the mask. See the bit definitions named XQSPIPS_*_OPTIONS in
111 * the file xqspips.h.
114 * - XST_SUCCESS if options are successfully set.
115 * - XST_DEVICE_BUSY if the device is currently transferring data.
116 * The transfer must complete or be aborted before setting options.
119 * This function is not thread-safe.
121 ******************************************************************************/
122 int XQspiPs_SetOptions(XQspiPs *InstancePtr, u32 Options)
128 Xil_AssertNonvoid(InstancePtr != NULL);
129 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
132 * Do not allow to modify the Control Register while a transfer is in
133 * progress. Not thread-safe.
135 if (InstancePtr->IsBusy) {
136 return XST_DEVICE_BUSY;
139 QspiOptions = Options & XQSPIPS_LQSPI_MODE_OPTION;
140 Options &= ~XQSPIPS_LQSPI_MODE_OPTION;
142 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
146 * Loop through the options table, turning the option on or off
147 * depending on whether the bit is set in the incoming options flag.
149 for (Index = 0; Index < XQSPIPS_NUM_OPTIONS; Index++) {
150 if (Options & OptionsTable[Index].Option) {
152 ConfigReg |= OptionsTable[Index].Mask;
155 ConfigReg &= ~(OptionsTable[Index].Mask);
160 * Now write the control register. Leave it to the upper layers
161 * to restart the device.
163 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress, XQSPIPS_CR_OFFSET,
167 * Check for the LQSPI configuration options.
169 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
170 XQSPIPS_LQSPI_CR_OFFSET);
173 if (QspiOptions & XQSPIPS_LQSPI_MODE_OPTION) {
174 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
175 XQSPIPS_LQSPI_CR_OFFSET,
176 XQSPIPS_LQSPI_CR_RST_STATE);
177 XQspiPs_SetSlaveSelect(InstancePtr);
179 ConfigReg &= ~XQSPIPS_LQSPI_CR_LINEAR_MASK;
180 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
181 XQSPIPS_LQSPI_CR_OFFSET, ConfigReg);
187 /*****************************************************************************/
190 * This function gets the options for the QSPI device. The options control how
191 * the device behaves relative to the QSPI bus.
193 * @param InstancePtr is a pointer to the XQspiPs instance.
197 * Options contains the specified options currently set. This is a bit value
198 * where a 1 means the option is on, and a 0 means the option is off.
199 * See the bit definitions named XQSPIPS_*_OPTIONS in file xqspips.h.
203 ******************************************************************************/
204 u32 XQspiPs_GetOptions(XQspiPs *InstancePtr)
210 Xil_AssertNonvoid(InstancePtr != NULL);
211 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
214 * Get the current options from QSPI configuration register.
216 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
220 * Loop through the options table to grab options
222 for (Index = 0; Index < XQSPIPS_NUM_OPTIONS; Index++) {
223 if (ConfigReg & OptionsTable[Index].Mask) {
224 OptionsFlag |= OptionsTable[Index].Option;
229 * Check for the LQSPI configuration options.
231 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
232 XQSPIPS_LQSPI_CR_OFFSET);
234 if ((ConfigReg & XQSPIPS_LQSPI_CR_LINEAR_MASK) != 0) {
235 OptionsFlag |= XQSPIPS_LQSPI_MODE_OPTION;
241 /*****************************************************************************/
244 * This function sets the clock prescaler for an QSPI device. The device
245 * must be idle rather than busy transferring data before setting these device
248 * @param InstancePtr is a pointer to the XQspiPs instance.
249 * @param Prescaler is the value that determine how much the clock should
250 * be divided by. Use the XQSPIPS_CLK_PRESCALE_* constants defined
251 * in xqspips.h for this setting.
254 * - XST_SUCCESS if options are successfully set.
255 * - XST_DEVICE_BUSY if the device is currently transferring data.
256 * The transfer must complete or be aborted before setting options.
259 * This function is not thread-safe.
261 ******************************************************************************/
262 int XQspiPs_SetClkPrescaler(XQspiPs *InstancePtr, u8 Prescaler)
266 Xil_AssertNonvoid(InstancePtr != NULL);
267 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
268 Xil_AssertNonvoid(Prescaler <= XQSPIPS_CR_PRESC_MAXIMUM);
271 * Do not allow the slave select to change while a transfer is in
272 * progress. Not thread-safe.
274 if (InstancePtr->IsBusy) {
275 return XST_DEVICE_BUSY;
279 * Read the configuration register, mask out the interesting bits, and set
280 * them with the shifted value passed into the function. Write the
281 * results back to the configuration register.
283 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
286 ConfigReg &= ~XQSPIPS_CR_PRESC_MASK;
287 ConfigReg |= (u32) (Prescaler & XQSPIPS_CR_PRESC_MAXIMUM) <<
288 XQSPIPS_CR_PRESC_SHIFT;
290 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
297 /*****************************************************************************/
300 * This function gets the clock prescaler of an QSPI device.
302 * @param InstancePtr is a pointer to the XQspiPs instance.
304 * @return The prescaler value.
309 ******************************************************************************/
310 u8 XQspiPs_GetClkPrescaler(XQspiPs *InstancePtr)
314 Xil_AssertNonvoid(InstancePtr != NULL);
315 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
317 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
320 ConfigReg &= XQSPIPS_CR_PRESC_MASK;
322 return (u8)(ConfigReg >> XQSPIPS_CR_PRESC_SHIFT);
325 /*****************************************************************************/
328 * This function sets the delay register for the QSPI device driver.
329 * The delay register controls the Delay Between Transfers, Delay After
330 * Transfers, and the Delay Initially. The default value is 0x0. The range of
331 * each delay value is 0-255.
333 * @param InstancePtr is a pointer to the XQspiPs instance.
334 * @param DelayNss is the delay to de-assert slave select between
335 * two word transfers.
336 * @param DelayBtwn is the delay between one Slave Select being
337 * de-activated and the activation of another slave. The delay is
338 * the number of master clock periods given by DelayBtwn + 2.
339 * @param DelayAfter define the delay between the last bit of the current
340 * byte transfer and the first bit of the next byte transfer.
341 * The delay in number of master clock periods is given as:
342 * CHPA=0:DelayInit+DelayAfter+3
343 * CHPA=1:DelayAfter+1
344 * @param DelayInit is the delay between asserting the slave select signal
345 * and the first bit transfer. The delay int number of master clock
346 * periods is DelayInit+1.
349 * - XST_SUCCESS if delays are successfully set.
350 * - XST_DEVICE_BUSY if the device is currently transferring data.
351 * The transfer must complete or be aborted before setting options.
355 ******************************************************************************/
356 int XQspiPs_SetDelays(XQspiPs *InstancePtr, u8 DelayNss, u8 DelayBtwn,
357 u8 DelayAfter, u8 DelayInit)
361 Xil_AssertNonvoid(InstancePtr != NULL);
362 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
365 * Do not allow the delays to change while a transfer is in
366 * progress. Not thread-safe.
368 if (InstancePtr->IsBusy) {
369 return XST_DEVICE_BUSY;
372 /* Shift, Mask and OR the values to build the register settings */
373 DelayRegister = (u32) DelayNss << XQSPIPS_DR_NSS_SHIFT;
374 DelayRegister |= (u32) DelayBtwn << XQSPIPS_DR_BTWN_SHIFT;
375 DelayRegister |= (u32) DelayAfter << XQSPIPS_DR_AFTER_SHIFT;
376 DelayRegister |= (u32) DelayInit;
378 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
379 XQSPIPS_DR_OFFSET, DelayRegister);
384 /*****************************************************************************/
387 * This function gets the delay settings for an QSPI device.
388 * The delay register controls the Delay Between Transfers, Delay After
389 * Transfers, and the Delay Initially. The default value is 0x0.
391 * @param InstancePtr is a pointer to the XQspiPs instance.
392 * @param DelayNss is a pointer to the Delay to de-assert slave select
393 * between two word transfers.
394 * @param DelayBtwn is a pointer to the Delay Between transfers value.
395 * This is a return parameter.
396 * @param DelayAfter is a pointer to the Delay After transfer value.
397 * This is a return parameter.
398 * @param DelayInit is a pointer to the Delay Initially value. This is
399 * a return parameter.
405 ******************************************************************************/
406 void XQspiPs_GetDelays(XQspiPs *InstancePtr, u8 *DelayNss, u8 *DelayBtwn,
407 u8 *DelayAfter, u8 *DelayInit)
411 Xil_AssertVoid(InstancePtr != NULL);
412 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
414 DelayRegister = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
417 *DelayInit = (u8)(DelayRegister & XQSPIPS_DR_INIT_MASK);
419 *DelayAfter = (u8)((DelayRegister & XQSPIPS_DR_AFTER_MASK) >>
420 XQSPIPS_DR_AFTER_SHIFT);
422 *DelayBtwn = (u8)((DelayRegister & XQSPIPS_DR_BTWN_MASK) >>
423 XQSPIPS_DR_BTWN_SHIFT);
425 *DelayNss = (u8)((DelayRegister & XQSPIPS_DR_NSS_MASK) >>
426 XQSPIPS_DR_NSS_SHIFT);