]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/qspips_v3_3/src/xqspips_options.c
xTaskGenericNotify() now sets xYieldPending to pdTRUE even when the 'higher priority...
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo_bsp / ps7_cortexa9_0 / libsrc / qspips_v3_3 / src / xqspips_options.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2015 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_2
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 * 3.3   sk  11/07/15 Modified the API prototypes according to MISRAC standards
62 *                    to remove compilation warnings. CR# 868893.
63 *</pre>
64 *
65 ******************************************************************************/
66
67 /***************************** Include Files *********************************/
68
69 #include "xqspips.h"
70
71 /************************** Constant Definitions *****************************/
72
73 /**************************** Type Definitions *******************************/
74
75 /***************** Macros (Inline Functions) Definitions *********************/
76
77 /************************** Function Prototypes ******************************/
78
79 /************************** Variable Definitions *****************************/
80
81 /*
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.
85  */
86 typedef struct {
87         u32 Option;
88         u32 Mask;
89 } OptionsMap;
90
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},
97 };
98
99 #define XQSPIPS_NUM_OPTIONS     (sizeof(OptionsTable) / sizeof(OptionsMap))
100
101 /*****************************************************************************/
102 /**
103 *
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.
107 *
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.
114 *
115 * @return
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.
119 *
120 * @note
121 * This function is not thread-safe.
122 *
123 ******************************************************************************/
124 s32 XQspiPs_SetOptions(XQspiPs *InstancePtr, u32 Options)
125 {
126         u32 ConfigReg;
127         unsigned int Index;
128         u32 QspiOptions;
129
130         Xil_AssertNonvoid(InstancePtr != NULL);
131         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
132
133         /*
134          * Do not allow to modify the Control Register while a transfer is in
135          * progress. Not thread-safe.
136          */
137         if (InstancePtr->IsBusy) {
138                 return XST_DEVICE_BUSY;
139         }
140
141         QspiOptions = Options & XQSPIPS_LQSPI_MODE_OPTION;
142         Options &= ~XQSPIPS_LQSPI_MODE_OPTION;
143
144         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
145                                       XQSPIPS_CR_OFFSET);
146
147         /*
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.
150          */
151         for (Index = 0; Index < XQSPIPS_NUM_OPTIONS; Index++) {
152                 if (Options & OptionsTable[Index].Option) {
153                         /* Turn it on */
154                         ConfigReg |= OptionsTable[Index].Mask;
155                 } else {
156                         /* Turn it off */
157                         ConfigReg &= ~(OptionsTable[Index].Mask);
158                 }
159         }
160
161         /*
162          * Now write the control register. Leave it to the upper layers
163          * to restart the device.
164          */
165         XQspiPs_WriteReg(InstancePtr->Config.BaseAddress, XQSPIPS_CR_OFFSET,
166                          ConfigReg);
167
168         /*
169          * Check for the LQSPI configuration options.
170          */
171         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
172                                       XQSPIPS_LQSPI_CR_OFFSET);
173
174
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);
180         } else {
181                 ConfigReg &= ~XQSPIPS_LQSPI_CR_LINEAR_MASK;
182                 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
183                                   XQSPIPS_LQSPI_CR_OFFSET, ConfigReg);
184         }
185
186         return XST_SUCCESS;
187 }
188
189 /*****************************************************************************/
190 /**
191 *
192 * This function gets the options for the QSPI device. The options control how
193 * the device behaves relative to the QSPI bus.
194 *
195 * @param        InstancePtr is a pointer to the XQspiPs instance.
196 *
197 * @return
198 *
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.
202 *
203 * @note         None.
204 *
205 ******************************************************************************/
206 u32 XQspiPs_GetOptions(XQspiPs *InstancePtr)
207 {
208         u32 OptionsFlag = 0;
209         u32 ConfigReg;
210         unsigned int Index;
211
212         Xil_AssertNonvoid(InstancePtr != NULL);
213         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
214
215         /*
216          * Get the current options from QSPI configuration register.
217          */
218         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
219                                       XQSPIPS_CR_OFFSET);
220
221         /*
222          * Loop through the options table to grab options
223          */
224         for (Index = 0; Index < XQSPIPS_NUM_OPTIONS; Index++) {
225                 if (ConfigReg & OptionsTable[Index].Mask) {
226                         OptionsFlag |= OptionsTable[Index].Option;
227                 }
228         }
229
230         /*
231          * Check for the LQSPI configuration options.
232          */
233         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
234                                       XQSPIPS_LQSPI_CR_OFFSET);
235
236         if ((ConfigReg & XQSPIPS_LQSPI_CR_LINEAR_MASK) != 0) {
237                 OptionsFlag |= XQSPIPS_LQSPI_MODE_OPTION;
238         }
239
240         return OptionsFlag;
241 }
242
243 /*****************************************************************************/
244 /**
245 *
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
248 * options.
249 *
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.
254 *
255 * @return
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.
259 *
260 * @note
261 * This function is not thread-safe.
262 *
263 ******************************************************************************/
264 s32 XQspiPs_SetClkPrescaler(XQspiPs *InstancePtr, u8 Prescaler)
265 {
266         u32 ConfigReg;
267
268         Xil_AssertNonvoid(InstancePtr != NULL);
269         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
270         Xil_AssertNonvoid(Prescaler <= XQSPIPS_CR_PRESC_MAXIMUM);
271
272         /*
273          * Do not allow the slave select to change while a transfer is in
274          * progress. Not thread-safe.
275          */
276         if (InstancePtr->IsBusy) {
277                 return XST_DEVICE_BUSY;
278         }
279
280         /*
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.
284          */
285         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
286                                       XQSPIPS_CR_OFFSET);
287
288         ConfigReg &= ~XQSPIPS_CR_PRESC_MASK;
289         ConfigReg |= (u32) (Prescaler & XQSPIPS_CR_PRESC_MAXIMUM) <<
290                             XQSPIPS_CR_PRESC_SHIFT;
291
292         XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
293                           XQSPIPS_CR_OFFSET,
294                           ConfigReg);
295
296         return XST_SUCCESS;
297 }
298
299 /*****************************************************************************/
300 /**
301 *
302 * This function gets the clock prescaler of an QSPI device.
303 *
304 * @param        InstancePtr is a pointer to the XQspiPs instance.
305 *
306 * @return       The prescaler value.
307 *
308 * @note         None.
309 *
310 *
311 ******************************************************************************/
312 u8 XQspiPs_GetClkPrescaler(XQspiPs *InstancePtr)
313 {
314         u32 ConfigReg;
315
316         Xil_AssertNonvoid(InstancePtr != NULL);
317         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
318
319         ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
320                                       XQSPIPS_CR_OFFSET);
321
322         ConfigReg &= XQSPIPS_CR_PRESC_MASK;
323
324         return (u8)(ConfigReg >> XQSPIPS_CR_PRESC_SHIFT);
325 }
326
327 /*****************************************************************************/
328 /**
329 *
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.
334 *
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.
349 *
350 * @return
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.
354 *
355 * @note         None.
356 *
357 ******************************************************************************/
358 int XQspiPs_SetDelays(XQspiPs *InstancePtr, u8 DelayNss, u8 DelayBtwn,
359                          u8 DelayAfter, u8 DelayInit)
360 {
361         u32 DelayRegister;
362
363         Xil_AssertNonvoid(InstancePtr != NULL);
364         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
365
366         /*
367          * Do not allow the delays to change while a transfer is in
368          * progress. Not thread-safe.
369          */
370         if (InstancePtr->IsBusy) {
371                 return XST_DEVICE_BUSY;
372         }
373
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;
379
380         XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
381                           XQSPIPS_DR_OFFSET, DelayRegister);
382
383         return XST_SUCCESS;
384 }
385
386 /*****************************************************************************/
387 /**
388 *
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.
392 *
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.
402 *
403 * @return       None.
404 *
405 * @note         None.
406 *
407 ******************************************************************************/
408 void XQspiPs_GetDelays(XQspiPs *InstancePtr, u8 *DelayNss, u8 *DelayBtwn,
409                          u8 *DelayAfter, u8 *DelayInit)
410 {
411         u32 DelayRegister;
412
413         Xil_AssertVoid(InstancePtr != NULL);
414         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
415
416         DelayRegister = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
417                                          XQSPIPS_DR_OFFSET);
418
419         *DelayInit = (u8)(DelayRegister & XQSPIPS_DR_INIT_MASK);
420
421         *DelayAfter = (u8)((DelayRegister & XQSPIPS_DR_AFTER_MASK) >>
422                            XQSPIPS_DR_AFTER_SHIFT);
423
424         *DelayBtwn = (u8)((DelayRegister & XQSPIPS_DR_BTWN_MASK) >>
425                           XQSPIPS_DR_BTWN_SHIFT);
426
427         *DelayNss = (u8)((DelayRegister & XQSPIPS_DR_NSS_MASK) >>
428                           XQSPIPS_DR_NSS_SHIFT);
429 }
430 /** @} */