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
37 * Contains functions for the configuration of the XQspiPs driver component.
40 * MODIFICATION HISTORY:
42 * Ver Who Date Changes
43 * ----- --- -------- -----------------------------------------------
44 * 1.00 sdm 11/25/10 First release
45 * 2.00a kka 07/25/12 Removed the selection for the following options:
46 * Master mode (XQSPIPS_MASTER_OPTION) and
47 * Flash interface mode (XQSPIPS_FLASH_MODE_OPTION) option
48 * as the QSPI driver supports the Master mode
49 * and Flash Interface mode. The driver doesnot support
50 * Slave mode or the legacy mode.
51 * Added the option for setting the Holdb_dr bit in the
52 * configuration options, XQSPIPS_HOLD_B_DRIVE_OPTION
53 * is the option to be used for setting this bit in the
54 * configuration register.
55 * 2.01a sg 02/03/13 SetDelays and GetDelays API's include DelayNss parameter.
57 * 2.02a hk 26/03/13 Removed XQspi_Reset() in Set_Options() function when
58 * LQSPI_MODE_OPTION is set. Moved Enable() to XQpsiPs_LqspiRead().
61 ******************************************************************************/
63 /***************************** Include Files *********************************/
67 /************************** Constant Definitions *****************************/
69 /**************************** Type Definitions *******************************/
71 /***************** Macros (Inline Functions) Definitions *********************/
73 /************************** Function Prototypes ******************************/
75 /************************** Variable Definitions *****************************/
78 * Create the table of options which are processed to get/set the device
79 * options. These options are table driven to allow easy maintenance and
80 * expansion of the options.
87 static OptionsMap OptionsTable[] = {
88 {XQSPIPS_CLK_ACTIVE_LOW_OPTION, XQSPIPS_CR_CPOL_MASK},
89 {XQSPIPS_CLK_PHASE_1_OPTION, XQSPIPS_CR_CPHA_MASK},
90 {XQSPIPS_FORCE_SSELECT_OPTION, XQSPIPS_CR_SSFORCE_MASK},
91 {XQSPIPS_MANUAL_START_OPTION, XQSPIPS_CR_MANSTRTEN_MASK},
92 {XQSPIPS_HOLD_B_DRIVE_OPTION, XQSPIPS_CR_HOLD_B_MASK},
95 #define XQSPIPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(OptionsMap))
97 /*****************************************************************************/
100 * This function sets the options for the QSPI device driver. The options control
101 * how the device behaves relative to the QSPI bus. The device must be idle
102 * rather than busy transferring data before setting these device options.
104 * @param InstancePtr is a pointer to the XQspiPs instance.
105 * @param Options contains the specified options to be set. This is a bit
106 * mask where a 1 means to turn the option on, and a 0 means to
107 * turn the option off. One or more bit values may be contained in
108 * the mask. See the bit definitions named XQSPIPS_*_OPTIONS in
109 * the file xqspips.h.
112 * - XST_SUCCESS if options are successfully set.
113 * - XST_DEVICE_BUSY if the device is currently transferring data.
114 * The transfer must complete or be aborted before setting options.
117 * This function is not thread-safe.
119 ******************************************************************************/
120 int XQspiPs_SetOptions(XQspiPs *InstancePtr, u32 Options)
126 Xil_AssertNonvoid(InstancePtr != NULL);
127 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
130 * Do not allow to modify the Control Register while a transfer is in
131 * progress. Not thread-safe.
133 if (InstancePtr->IsBusy) {
134 return XST_DEVICE_BUSY;
137 QspiOptions = Options & XQSPIPS_LQSPI_MODE_OPTION;
138 Options &= ~XQSPIPS_LQSPI_MODE_OPTION;
140 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
144 * Loop through the options table, turning the option on or off
145 * depending on whether the bit is set in the incoming options flag.
147 for (Index = 0; Index < XQSPIPS_NUM_OPTIONS; Index++) {
148 if (Options & OptionsTable[Index].Option) {
150 ConfigReg |= OptionsTable[Index].Mask;
153 ConfigReg &= ~(OptionsTable[Index].Mask);
158 * Now write the control register. Leave it to the upper layers
159 * to restart the device.
161 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress, XQSPIPS_CR_OFFSET,
165 * Check for the LQSPI configuration options.
167 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
168 XQSPIPS_LQSPI_CR_OFFSET);
171 if (QspiOptions & XQSPIPS_LQSPI_MODE_OPTION) {
172 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
173 XQSPIPS_LQSPI_CR_OFFSET,
174 XQSPIPS_LQSPI_CR_RST_STATE);
175 XQspiPs_SetSlaveSelect(InstancePtr);
177 ConfigReg &= ~XQSPIPS_LQSPI_CR_LINEAR_MASK;
178 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
179 XQSPIPS_LQSPI_CR_OFFSET, ConfigReg);
185 /*****************************************************************************/
188 * This function gets the options for the QSPI device. The options control how
189 * the device behaves relative to the QSPI bus.
191 * @param InstancePtr is a pointer to the XQspiPs instance.
195 * Options contains the specified options currently set. This is a bit value
196 * where a 1 means the option is on, and a 0 means the option is off.
197 * See the bit definitions named XQSPIPS_*_OPTIONS in file xqspips.h.
201 ******************************************************************************/
202 u32 XQspiPs_GetOptions(XQspiPs *InstancePtr)
208 Xil_AssertNonvoid(InstancePtr != NULL);
209 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
212 * Get the current options from QSPI configuration register.
214 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
218 * Loop through the options table to grab options
220 for (Index = 0; Index < XQSPIPS_NUM_OPTIONS; Index++) {
221 if (ConfigReg & OptionsTable[Index].Mask) {
222 OptionsFlag |= OptionsTable[Index].Option;
227 * Check for the LQSPI configuration options.
229 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
230 XQSPIPS_LQSPI_CR_OFFSET);
232 if ((ConfigReg & XQSPIPS_LQSPI_CR_LINEAR_MASK) != 0) {
233 OptionsFlag |= XQSPIPS_LQSPI_MODE_OPTION;
239 /*****************************************************************************/
242 * This function sets the clock prescaler for an QSPI device. The device
243 * must be idle rather than busy transferring data before setting these device
246 * @param InstancePtr is a pointer to the XQspiPs instance.
247 * @param Prescaler is the value that determine how much the clock should
248 * be divided by. Use the XQSPIPS_CLK_PRESCALE_* constants defined
249 * in xqspips.h for this setting.
252 * - XST_SUCCESS if options are successfully set.
253 * - XST_DEVICE_BUSY if the device is currently transferring data.
254 * The transfer must complete or be aborted before setting options.
257 * This function is not thread-safe.
259 ******************************************************************************/
260 int XQspiPs_SetClkPrescaler(XQspiPs *InstancePtr, u8 Prescaler)
264 Xil_AssertNonvoid(InstancePtr != NULL);
265 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
266 Xil_AssertNonvoid(Prescaler <= XQSPIPS_CR_PRESC_MAXIMUM);
269 * Do not allow the slave select to change while a transfer is in
270 * progress. Not thread-safe.
272 if (InstancePtr->IsBusy) {
273 return XST_DEVICE_BUSY;
277 * Read the configuration register, mask out the interesting bits, and set
278 * them with the shifted value passed into the function. Write the
279 * results back to the configuration register.
281 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
284 ConfigReg &= ~XQSPIPS_CR_PRESC_MASK;
285 ConfigReg |= (u32) (Prescaler & XQSPIPS_CR_PRESC_MAXIMUM) <<
286 XQSPIPS_CR_PRESC_SHIFT;
288 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
295 /*****************************************************************************/
298 * This function gets the clock prescaler of an QSPI device.
300 * @param InstancePtr is a pointer to the XQspiPs instance.
302 * @return The prescaler value.
307 ******************************************************************************/
308 u8 XQspiPs_GetClkPrescaler(XQspiPs *InstancePtr)
312 Xil_AssertNonvoid(InstancePtr != NULL);
313 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
315 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
318 ConfigReg &= XQSPIPS_CR_PRESC_MASK;
320 return (u8)(ConfigReg >> XQSPIPS_CR_PRESC_SHIFT);
323 /*****************************************************************************/
326 * This function sets the delay register for the QSPI device driver.
327 * The delay register controls the Delay Between Transfers, Delay After
328 * Transfers, and the Delay Initially. The default value is 0x0. The range of
329 * each delay value is 0-255.
331 * @param InstancePtr is a pointer to the XQspiPs instance.
332 * @param DelayNss is the delay to de-assert slave select between
333 * two word transfers.
334 * @param DelayBtwn is the delay between one Slave Select being
335 * de-activated and the activation of another slave. The delay is
336 * the number of master clock periods given by DelayBtwn + 2.
337 * @param DelayAfter define the delay between the last bit of the current
338 * byte transfer and the first bit of the next byte transfer.
339 * The delay in number of master clock periods is given as:
340 * CHPA=0:DelayInit+DelayAfter+3
341 * CHPA=1:DelayAfter+1
342 * @param DelayInit is the delay between asserting the slave select signal
343 * and the first bit transfer. The delay int number of master clock
344 * periods is DelayInit+1.
347 * - XST_SUCCESS if delays are successfully set.
348 * - XST_DEVICE_BUSY if the device is currently transferring data.
349 * The transfer must complete or be aborted before setting options.
353 ******************************************************************************/
354 int XQspiPs_SetDelays(XQspiPs *InstancePtr, u8 DelayNss, u8 DelayBtwn,
355 u8 DelayAfter, u8 DelayInit)
359 Xil_AssertNonvoid(InstancePtr != NULL);
360 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
363 * Do not allow the delays to change while a transfer is in
364 * progress. Not thread-safe.
366 if (InstancePtr->IsBusy) {
367 return XST_DEVICE_BUSY;
370 /* Shift, Mask and OR the values to build the register settings */
371 DelayRegister = (u32) DelayNss << XQSPIPS_DR_NSS_SHIFT;
372 DelayRegister |= (u32) DelayBtwn << XQSPIPS_DR_BTWN_SHIFT;
373 DelayRegister |= (u32) DelayAfter << XQSPIPS_DR_AFTER_SHIFT;
374 DelayRegister |= (u32) DelayInit;
376 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
377 XQSPIPS_DR_OFFSET, DelayRegister);
382 /*****************************************************************************/
385 * This function gets the delay settings for an QSPI device.
386 * The delay register controls the Delay Between Transfers, Delay After
387 * Transfers, and the Delay Initially. The default value is 0x0.
389 * @param InstancePtr is a pointer to the XQspiPs instance.
390 * @param DelayNss is a pointer to the Delay to de-assert slave select
391 * between two word transfers.
392 * @param DelayBtwn is a pointer to the Delay Between transfers value.
393 * This is a return parameter.
394 * @param DelayAfter is a pointer to the Delay After transfer value.
395 * This is a return parameter.
396 * @param DelayInit is a pointer to the Delay Initially value. This is
397 * a return parameter.
403 ******************************************************************************/
404 void XQspiPs_GetDelays(XQspiPs *InstancePtr, u8 *DelayNss, u8 *DelayBtwn,
405 u8 *DelayAfter, u8 *DelayInit)
409 Xil_AssertVoid(InstancePtr != NULL);
410 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
412 DelayRegister = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
415 *DelayInit = (u8)(DelayRegister & XQSPIPS_DR_INIT_MASK);
417 *DelayAfter = (u8)((DelayRegister & XQSPIPS_DR_AFTER_MASK) >>
418 XQSPIPS_DR_AFTER_SHIFT);
420 *DelayBtwn = (u8)((DelayRegister & XQSPIPS_DR_BTWN_MASK) >>
421 XQSPIPS_DR_BTWN_SHIFT);
423 *DelayNss = (u8)((DelayRegister & XQSPIPS_DR_NSS_MASK) >>
424 XQSPIPS_DR_NSS_SHIFT);