]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/qspips_v3_0/src/xqspips_options.c
Remove obsolete MPU demos.
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo_bsp / ps7_cortexa9_0 / libsrc / qspips_v3_0 / src / xqspips_options.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2014 Xilinx, Inc.  All rights reserved.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
18 *
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
25 * SOFTWARE.
26 *
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.
30 *
31 ******************************************************************************/
32 /*****************************************************************************/
33 /**
34 *
35 * @file xqspips_options.c
36 * @addtogroup qspips_v3_0
37 * @{
38 *
39 * Contains functions for the configuration of the XQspiPs driver component.
40 *
41 * <pre>
42 * MODIFICATION HISTORY:
43 *
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.
58 *
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 *</pre>
62 *
63 ******************************************************************************/
64
65 /***************************** Include Files *********************************/
66
67 #include "xqspips.h"
68
69 /************************** Constant Definitions *****************************/
70
71 /**************************** Type Definitions *******************************/
72
73 /***************** Macros (Inline Functions) Definitions *********************/
74
75 /************************** Function Prototypes ******************************/
76
77 /************************** Variable Definitions *****************************/
78
79 /*
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.
83  */
84 typedef struct {
85         u32 Option;
86         u32 Mask;
87 } OptionsMap;
88
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},
95 };
96
97 #define XQSPIPS_NUM_OPTIONS     (sizeof(OptionsTable) / sizeof(OptionsMap))
98
99 /*****************************************************************************/
100 /**
101 *
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.
105 *
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.
112 *
113 * @return
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.
117 *
118 * @note
119 * This function is not thread-safe.
120 *
121 ******************************************************************************/
122 int XQspiPs_SetOptions(XQspiPs *InstancePtr, u32 Options)
123 {
124         u32 ConfigReg;
125         unsigned int Index;
126         u32 QspiOptions;
127
128         Xil_AssertNonvoid(InstancePtr != NULL);
129         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
130
131         /*
132          * Do not allow to modify the Control Register while a transfer is in
133          * progress. Not thread-safe.
134          */
135         if (InstancePtr->IsBusy) {
136                 return XST_DEVICE_BUSY;
137         }
138
139         QspiOptions = Options & XQSPIPS_LQSPI_MODE_OPTION;
140         Options &= ~XQSPIPS_LQSPI_MODE_OPTION;
141
142         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
143                                       XQSPIPS_CR_OFFSET);
144
145         /*
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.
148          */
149         for (Index = 0; Index < XQSPIPS_NUM_OPTIONS; Index++) {
150                 if (Options & OptionsTable[Index].Option) {
151                         /* Turn it on */
152                         ConfigReg |= OptionsTable[Index].Mask;
153                 } else {
154                         /* Turn it off */
155                         ConfigReg &= ~(OptionsTable[Index].Mask);
156                 }
157         }
158
159         /*
160          * Now write the control register. Leave it to the upper layers
161          * to restart the device.
162          */
163         XQspiPs_WriteReg(InstancePtr->Config.BaseAddress, XQSPIPS_CR_OFFSET,
164                          ConfigReg);
165
166         /*
167          * Check for the LQSPI configuration options.
168          */
169         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
170                                       XQSPIPS_LQSPI_CR_OFFSET);
171
172
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);
178         } else {
179                 ConfigReg &= ~XQSPIPS_LQSPI_CR_LINEAR_MASK;
180                 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
181                                   XQSPIPS_LQSPI_CR_OFFSET, ConfigReg);
182         }
183
184         return XST_SUCCESS;
185 }
186
187 /*****************************************************************************/
188 /**
189 *
190 * This function gets the options for the QSPI device. The options control how
191 * the device behaves relative to the QSPI bus.
192 *
193 * @param        InstancePtr is a pointer to the XQspiPs instance.
194 *
195 * @return
196 *
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.
200 *
201 * @note         None.
202 *
203 ******************************************************************************/
204 u32 XQspiPs_GetOptions(XQspiPs *InstancePtr)
205 {
206         u32 OptionsFlag = 0;
207         u32 ConfigReg;
208         unsigned int Index;
209
210         Xil_AssertNonvoid(InstancePtr != NULL);
211         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
212
213         /*
214          * Get the current options from QSPI configuration register.
215          */
216         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
217                                       XQSPIPS_CR_OFFSET);
218
219         /*
220          * Loop through the options table to grab options
221          */
222         for (Index = 0; Index < XQSPIPS_NUM_OPTIONS; Index++) {
223                 if (ConfigReg & OptionsTable[Index].Mask) {
224                         OptionsFlag |= OptionsTable[Index].Option;
225                 }
226         }
227
228         /*
229          * Check for the LQSPI configuration options.
230          */
231         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
232                                       XQSPIPS_LQSPI_CR_OFFSET);
233
234         if ((ConfigReg & XQSPIPS_LQSPI_CR_LINEAR_MASK) != 0) {
235                 OptionsFlag |= XQSPIPS_LQSPI_MODE_OPTION;
236         }
237
238         return OptionsFlag;
239 }
240
241 /*****************************************************************************/
242 /**
243 *
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
246 * options.
247 *
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.
252 *
253 * @return
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.
257 *
258 * @note
259 * This function is not thread-safe.
260 *
261 ******************************************************************************/
262 int XQspiPs_SetClkPrescaler(XQspiPs *InstancePtr, u8 Prescaler)
263 {
264         u32 ConfigReg;
265
266         Xil_AssertNonvoid(InstancePtr != NULL);
267         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
268         Xil_AssertNonvoid(Prescaler <= XQSPIPS_CR_PRESC_MAXIMUM);
269
270         /*
271          * Do not allow the slave select to change while a transfer is in
272          * progress. Not thread-safe.
273          */
274         if (InstancePtr->IsBusy) {
275                 return XST_DEVICE_BUSY;
276         }
277
278         /*
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.
282          */
283         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
284                                       XQSPIPS_CR_OFFSET);
285
286         ConfigReg &= ~XQSPIPS_CR_PRESC_MASK;
287         ConfigReg |= (u32) (Prescaler & XQSPIPS_CR_PRESC_MAXIMUM) <<
288                             XQSPIPS_CR_PRESC_SHIFT;
289
290         XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
291                           XQSPIPS_CR_OFFSET,
292                           ConfigReg);
293
294         return XST_SUCCESS;
295 }
296
297 /*****************************************************************************/
298 /**
299 *
300 * This function gets the clock prescaler of an QSPI device.
301 *
302 * @param        InstancePtr is a pointer to the XQspiPs instance.
303 *
304 * @return       The prescaler value.
305 *
306 * @note         None.
307 *
308 *
309 ******************************************************************************/
310 u8 XQspiPs_GetClkPrescaler(XQspiPs *InstancePtr)
311 {
312         u32 ConfigReg;
313
314         Xil_AssertNonvoid(InstancePtr != NULL);
315         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
316
317         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
318                                       XQSPIPS_CR_OFFSET);
319
320         ConfigReg &= XQSPIPS_CR_PRESC_MASK;
321
322         return (u8)(ConfigReg >> XQSPIPS_CR_PRESC_SHIFT);
323 }
324
325 /*****************************************************************************/
326 /**
327 *
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.
332 *
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.
347 *
348 * @return
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.
352 *
353 * @note         None.
354 *
355 ******************************************************************************/
356 int XQspiPs_SetDelays(XQspiPs *InstancePtr, u8 DelayNss, u8 DelayBtwn,
357                          u8 DelayAfter, u8 DelayInit)
358 {
359         u32 DelayRegister;
360
361         Xil_AssertNonvoid(InstancePtr != NULL);
362         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
363
364         /*
365          * Do not allow the delays to change while a transfer is in
366          * progress. Not thread-safe.
367          */
368         if (InstancePtr->IsBusy) {
369                 return XST_DEVICE_BUSY;
370         }
371
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;
377
378         XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
379                           XQSPIPS_DR_OFFSET, DelayRegister);
380
381         return XST_SUCCESS;
382 }
383
384 /*****************************************************************************/
385 /**
386 *
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.
390 *
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.
400 *
401 * @return       None.
402 *
403 * @note         None.
404 *
405 ******************************************************************************/
406 void XQspiPs_GetDelays(XQspiPs *InstancePtr, u8 *DelayNss, u8 *DelayBtwn,
407                          u8 *DelayAfter, u8 *DelayInit)
408 {
409         u32 DelayRegister;
410
411         Xil_AssertVoid(InstancePtr != NULL);
412         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
413
414         DelayRegister = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
415                                          XQSPIPS_DR_OFFSET);
416
417         *DelayInit = (u8)(DelayRegister & XQSPIPS_DR_INIT_MASK);
418
419         *DelayAfter = (u8)((DelayRegister & XQSPIPS_DR_AFTER_MASK) >>
420                            XQSPIPS_DR_AFTER_SHIFT);
421
422         *DelayBtwn = (u8)((DelayRegister & XQSPIPS_DR_BTWN_MASK) >>
423                           XQSPIPS_DR_BTWN_SHIFT);
424
425         *DelayNss = (u8)((DelayRegister & XQSPIPS_DR_NSS_MASK) >>
426                           XQSPIPS_DR_NSS_SHIFT);
427 }
428 /** @} */