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 /*****************************************************************************/
36 * @addtogroup qspips_v3_2
39 * Contains implements the interface functions of the XQspiPs driver.
40 * See xqspips.h for a detailed description of the device and driver.
43 * MODIFICATION HISTORY:
45 * Ver Who Date Changes
46 * ----- --- -------- -----------------------------------------------
47 * 1.00 sdm 11/25/10 First release
48 * 2.00a kka 07/25/12 Removed XQspiPs_GetWriteData API.
49 * The XQspiPs_SetSlaveSelect has been modified to remove
50 * the argument of the slave select as the QSPI controller
51 * only supports one slave.
52 * XQspiPs_GetSlaveSelect API has been removed
53 * Added logic to XQspiPs_GetReadData to handle data
54 * shift for normal data reads and instruction/status
55 * reads differently based on the ShiftReadData flag.
56 * Removed the selection for the following options:
57 * Master mode (XQSPIPS_MASTER_OPTION) and
58 * Flash interface mode (XQSPIPS_FLASH_MODE_OPTION) option
59 * as the QSPI driver supports the Master mode
60 * and Flash Interface mode and doesnot support
61 * Slave mode or the legacy mode.
62 * Modified the XQspiPs_PolledTransfer and XQspiPs_Transfer
63 * APIs so that the last argument (IsInst) specifying whether
64 * it is instruction or data has been removed. The first byte
65 * in the SendBufPtr argument of these APIs specify the
66 * instruction to be sent to the Flash Device.
67 * The XQspiPs_PolledTransfer function has been updated
68 * to fill the data to fifo depth.
69 * This version of the driver fixes CRs 670197/663787.
70 * 2.01a sg 02/03/13 Added flash opcodes for DUAL_IO_READ,QUAD_IO_READ.
71 * Created macros XQspiPs_IsManualStart and
72 * XQspiPs_IsManualChipSelect.
73 * Changed QSPI transfer logic for polled and interrupt
74 * modes to be based on filled tx fifo count and receive
75 * based on it. RXNEMPTY interrupt is not used.
76 * Added assertions to XQspiPs_LqspiRead function.
78 * 2.02a hk 05/14/13 Added enable and disable to the XQspiPs_LqspiRead()
80 * Added instructions for bank selection, die erase and
81 * flag status register to the flash instruction table
82 * Handling for instructions not in flash instruction
83 * table added. Checking for Tx FIFO empty when switching from
84 * TXD1/2/3 to TXD0 added. If WRSR instruction is sent with
85 * byte count 3 (spansion), instruction size and TXD register
86 * changed accordingly. CR# 712502 and 703869.
87 * Added (#ifdef linear base address) in the Linear read function.
88 * Changed XPAR_XQSPIPS_0_LINEAR_BASEADDR to
89 * XPAR_PS7_QSPI_LINEAR_0_S_AXI_BASEADDR in
90 * XQspiPs_LqspiRead function. Fix for CR#718141
92 * 2.03a hk 09/05/13 Modified polled and interrupt transfers to make use of
93 * thresholds. This is to improve performance.
94 * Added RX and TX threshold reset to one in XQspiPs_Abort.
95 * Added RX threshold reset(1) after transfer in polled and
96 * interrupt transfers. Made changes to make sure threshold
97 * change is done only when no transfer is in progress.
98 * 3.1 hk 08/13/14 When writing to the configuration register, set/reset
99 * required bits leaving reserved bits untouched. CR# 796813.
100 * 3.2 sk 02/05/15 Add SLCR reset in abort function as a workaround because
101 * controller does not update FIFO status flags as expected
102 * when thresholds are used.
103 * 3.3 sk 11/07/15 Modified the API prototypes according to MISRAC standards
104 * to remove compilation warnings. CR# 868893.
108 ******************************************************************************/
110 /***************************** Include Files *********************************/
114 /************************** Constant Definitions *****************************/
117 /**************************** Type Definitions *******************************/
120 * This typedef defines qspi flash instruction format
123 u8 OpCode; /**< Operational code of the instruction */
124 u8 InstSize; /**< Size of the instruction including address bytes */
125 u8 TxOffset; /**< Register address where instruction has to be
129 /***************** Macros (Inline Functions) Definitions *********************/
131 #define ARRAY_SIZE(Array) (sizeof(Array) / sizeof((Array)[0]))
133 /************************** Function Prototypes ******************************/
134 static void XQspiPs_GetReadData(XQspiPs *InstancePtr, u32 Data, u8 Size);
135 static void StubStatusHandler(void *CallBackRef, u32 StatusEvent,
138 /************************** Variable Definitions *****************************/
141 * List of all the QSPI instructions and its format
143 static XQspiPsInstFormat FlashInst[] = {
144 { XQSPIPS_FLASH_OPCODE_WREN, 1, XQSPIPS_TXD_01_OFFSET },
145 { XQSPIPS_FLASH_OPCODE_WRDS, 1, XQSPIPS_TXD_01_OFFSET },
146 { XQSPIPS_FLASH_OPCODE_RDSR1, 2, XQSPIPS_TXD_10_OFFSET },
147 { XQSPIPS_FLASH_OPCODE_RDSR2, 2, XQSPIPS_TXD_10_OFFSET },
148 { XQSPIPS_FLASH_OPCODE_WRSR, 2, XQSPIPS_TXD_10_OFFSET },
149 { XQSPIPS_FLASH_OPCODE_PP, 4, XQSPIPS_TXD_00_OFFSET },
150 { XQSPIPS_FLASH_OPCODE_SE, 4, XQSPIPS_TXD_00_OFFSET },
151 { XQSPIPS_FLASH_OPCODE_BE_32K, 4, XQSPIPS_TXD_00_OFFSET },
152 { XQSPIPS_FLASH_OPCODE_BE_4K, 4, XQSPIPS_TXD_00_OFFSET },
153 { XQSPIPS_FLASH_OPCODE_BE, 1, XQSPIPS_TXD_01_OFFSET },
154 { XQSPIPS_FLASH_OPCODE_ERASE_SUS, 1, XQSPIPS_TXD_01_OFFSET },
155 { XQSPIPS_FLASH_OPCODE_ERASE_RES, 1, XQSPIPS_TXD_01_OFFSET },
156 { XQSPIPS_FLASH_OPCODE_RDID, 4, XQSPIPS_TXD_00_OFFSET },
157 { XQSPIPS_FLASH_OPCODE_NORM_READ, 4, XQSPIPS_TXD_00_OFFSET },
158 { XQSPIPS_FLASH_OPCODE_FAST_READ, 4, XQSPIPS_TXD_00_OFFSET },
159 { XQSPIPS_FLASH_OPCODE_DUAL_READ, 4, XQSPIPS_TXD_00_OFFSET },
160 { XQSPIPS_FLASH_OPCODE_QUAD_READ, 4, XQSPIPS_TXD_00_OFFSET },
161 { XQSPIPS_FLASH_OPCODE_DUAL_IO_READ, 4, XQSPIPS_TXD_00_OFFSET },
162 { XQSPIPS_FLASH_OPCODE_QUAD_IO_READ, 4, XQSPIPS_TXD_00_OFFSET },
163 { XQSPIPS_FLASH_OPCODE_BRWR, 2, XQSPIPS_TXD_10_OFFSET },
164 { XQSPIPS_FLASH_OPCODE_BRRD, 2, XQSPIPS_TXD_10_OFFSET },
165 { XQSPIPS_FLASH_OPCODE_EARWR, 2, XQSPIPS_TXD_10_OFFSET },
166 { XQSPIPS_FLASH_OPCODE_EARRD, 2, XQSPIPS_TXD_10_OFFSET },
167 { XQSPIPS_FLASH_OPCODE_DIE_ERASE, 4, XQSPIPS_TXD_00_OFFSET },
168 { XQSPIPS_FLASH_OPCODE_READ_FLAG_SR, 2, XQSPIPS_TXD_10_OFFSET },
169 { XQSPIPS_FLASH_OPCODE_CLEAR_FLAG_SR, 1, XQSPIPS_TXD_01_OFFSET },
170 /* Add all the instructions supported by the flash device */
173 /*****************************************************************************/
176 * Initializes a specific XQspiPs instance such that the driver is ready to use.
178 * The state of the device after initialization is:
180 * - Active high clock polarity
182 * - Baud rate divisor 2
183 * - Transfer width 32
184 * - Master reference clock = pclk
185 * - No chip select active
186 * - Manual CS and Manual Start disabled
188 * @param InstancePtr is a pointer to the XQspiPs instance.
189 * @param ConfigPtr is a reference to a structure containing information
190 * about a specific QSPI device. This function initializes an
191 * InstancePtr object for a specific device specified by the
192 * contents of Config.
193 * @param EffectiveAddr is the device base address in the virtual memory
194 * address space. The caller is responsible for keeping the address
195 * mapping from EffectiveAddr to the device physical base address
196 * unchanged once this function is invoked. Unexpected errors may
197 * occur if the address mapping changes after this function is
198 * called. If address translation is not used, use
199 * ConfigPtr->Config.BaseAddress for this device.
202 * - XST_SUCCESS if successful.
203 * - XST_DEVICE_IS_STARTED if the device is already started.
204 * It must be stopped to re-initialize.
208 ******************************************************************************/
209 int XQspiPs_CfgInitialize(XQspiPs *InstancePtr, XQspiPs_Config *ConfigPtr,
212 Xil_AssertNonvoid(InstancePtr != NULL);
213 Xil_AssertNonvoid(ConfigPtr != NULL);
216 * If the device is busy, disallow the initialize and return a status
217 * indicating it is already started. This allows the user to stop the
218 * device and re-initialize, but prevents a user from inadvertently
219 * initializing. This assumes the busy flag is cleared at startup.
221 if (InstancePtr->IsBusy == TRUE) {
222 return XST_DEVICE_IS_STARTED;
226 * Set some default values.
228 InstancePtr->IsBusy = FALSE;
230 InstancePtr->Config.BaseAddress = EffectiveAddr;
231 InstancePtr->StatusHandler = StubStatusHandler;
233 InstancePtr->SendBufferPtr = NULL;
234 InstancePtr->RecvBufferPtr = NULL;
235 InstancePtr->RequestedBytes = 0;
236 InstancePtr->RemainingBytes = 0;
237 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
239 InstancePtr->Config.ConnectionMode = ConfigPtr->ConnectionMode;
242 * Reset the QSPI device to get it into its initial state. It is
243 * expected that device configuration will take place after this
244 * initialization is done, but before the device is started.
246 XQspiPs_Reset(InstancePtr);
251 /*****************************************************************************/
254 * Resets the QSPI device. Reset must only be called after the driver has been
255 * initialized. Any data transfer that is in progress is aborted.
257 * The upper layer software is responsible for re-configuring (if necessary)
258 * and restarting the QSPI device after the reset.
260 * @param InstancePtr is a pointer to the XQspiPs instance.
266 ******************************************************************************/
267 void XQspiPs_Reset(XQspiPs *InstancePtr)
271 Xil_AssertVoid(InstancePtr != NULL);
272 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
275 * Abort any transfer that is in progress
277 XQspiPs_Abort(InstancePtr);
280 * Write default value to configuration register.
281 * Do not modify reserved bits.
283 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
285 ConfigReg |= XQSPIPS_CR_RESET_MASK_SET;
286 ConfigReg &= ~XQSPIPS_CR_RESET_MASK_CLR;
287 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress, XQSPIPS_CR_OFFSET,
291 /*****************************************************************************/
294 * Aborts a transfer in progress by disabling the device and flush the RxFIFO.
295 * The byte counts are cleared, the busy flag is cleared.
297 * @param InstancePtr is a pointer to the XQspiPs instance.
303 * This function does a read/modify/write of the config register. The user of
304 * this function needs to take care of critical sections.
306 ******************************************************************************/
307 void XQspiPs_Abort(XQspiPs *InstancePtr)
312 XQspiPs_Disable(InstancePtr);
315 * De-assert slave select lines.
317 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
319 ConfigReg |= (XQSPIPS_CR_SSCTRL_MASK | XQSPIPS_CR_SSFORCE_MASK);
320 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
321 XQSPIPS_CR_OFFSET, ConfigReg);
324 * QSPI Software Reset
326 IsLock = XQspiPs_ReadReg(XPAR_XSLCR_0_BASEADDR, SLCR_LOCKSTA);
328 XQspiPs_WriteReg(XPAR_XSLCR_0_BASEADDR, SLCR_UNLOCK,
331 XQspiPs_WriteReg(XPAR_XSLCR_0_BASEADDR, LQSPI_RST_CTRL,
332 LQSPI_RST_CTRL_MASK);
333 XQspiPs_WriteReg(XPAR_XSLCR_0_BASEADDR, LQSPI_RST_CTRL, 0x0);
335 XQspiPs_WriteReg(XPAR_XSLCR_0_BASEADDR, SLCR_LOCK,
340 * Set the RX and TX FIFO threshold to reset value (one)
342 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
343 XQSPIPS_RXWR_OFFSET, XQSPIPS_RXWR_RESET_VALUE);
345 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
346 XQSPIPS_TXWR_OFFSET, XQSPIPS_TXWR_RESET_VALUE);
348 InstancePtr->RemainingBytes = 0;
349 InstancePtr->RequestedBytes = 0;
350 InstancePtr->IsBusy = FALSE;
353 /*****************************************************************************/
356 * Transfers specified data on the QSPI bus. Initiates bus communication and
357 * sends/receives data to/from the selected QSPI slave. For every byte sent,
358 * a byte is received.
360 * The caller has the option of providing two different buffers for send and
361 * receive, or one buffer for both send and receive, or no buffer for receive.
362 * The receive buffer must be at least as big as the send buffer to prevent
363 * unwanted memory writes. This implies that the byte count passed in as an
364 * argument must be the smaller of the two buffers if they differ in size.
365 * Here are some sample usages:
367 * XQspiPs_Transfer(InstancePtr, SendBuf, RecvBuf, ByteCount)
368 * The caller wishes to send and receive, and provides two different
369 * buffers for send and receive.
371 * XQspiPs_Transfer(InstancePtr, SendBuf, NULL, ByteCount)
372 * The caller wishes only to send and does not care about the received
373 * data. The driver ignores the received data in this case.
375 * XQspiPs_Transfer(InstancePtr, SendBuf, SendBuf, ByteCount)
376 * The caller wishes to send and receive, but provides the same buffer
377 * for doing both. The driver sends the data and overwrites the send
378 * buffer with received data as it transfers the data.
380 * XQspiPs_Transfer(InstancePtr, RecvBuf, RecvBuf, ByteCount)
381 * The caller wishes to only receive and does not care about sending
382 * data. In this case, the caller must still provide a send buffer, but
383 * it can be the same as the receive buffer if the caller does not care
384 * what it sends. The device must send N bytes of data if it wishes to
385 * receive N bytes of data.
387 * Although this function takes entire buffers as arguments, the driver can only
388 * transfer a limited number of bytes at a time, limited by the size of the
389 * FIFO. A call to this function only starts the transfer, then subsequent
390 * transfers of the data is performed by the interrupt service routine until
391 * the entire buffer has been transferred. The status callback function is
392 * called when the entire buffer has been sent/received.
394 * This function is non-blocking. The SetSlaveSelect function must be called
395 * prior to this function.
397 * @param InstancePtr is a pointer to the XQspiPs instance.
398 * @param SendBufPtr is a pointer to a data buffer that needs to be
399 * transmitted. This buffer must not be NULL.
400 * @param RecvBufPtr is a pointer to a buffer for received data.
401 * This argument can be NULL if do not care about receiving.
402 * @param ByteCount contains the number of bytes to send/receive.
403 * The number of bytes received always equals the number of bytes
407 * - XST_SUCCESS if the buffers are successfully handed off to the
408 * device for transfer.
409 * - XST_DEVICE_BUSY indicates that a data transfer is already in
410 * progress. This is determined by the driver.
414 * This function is not thread-safe. The higher layer software must ensure that
415 * no two threads are transferring data on the QSPI bus at the same time.
417 ******************************************************************************/
418 s32 XQspiPs_Transfer(XQspiPs *InstancePtr, u8 *SendBufPtr, u8 *RecvBufPtr,
427 XQspiPsInstFormat *CurrInst;
428 XQspiPsInstFormat NewInst[2];
431 CurrInst = &NewInst[0];
434 * The RecvBufPtr argument can be null
436 Xil_AssertNonvoid(InstancePtr != NULL);
437 Xil_AssertNonvoid(SendBufPtr != NULL);
438 Xil_AssertNonvoid(ByteCount > 0);
439 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
442 * Check whether there is another transfer in progress. Not thread-safe.
444 if (InstancePtr->IsBusy) {
445 return XST_DEVICE_BUSY;
449 * Set the busy flag, which will be cleared in the ISR when the
450 * transfer is entirely done.
452 InstancePtr->IsBusy = TRUE;
455 * Set up buffer pointers.
457 InstancePtr->SendBufferPtr = SendBufPtr;
458 InstancePtr->RecvBufferPtr = RecvBufPtr;
460 InstancePtr->RequestedBytes = ByteCount;
461 InstancePtr->RemainingBytes = ByteCount;
464 * The first byte with every chip-select assertion is always
465 * expected to be an instruction for flash interface mode
467 Instruction = *InstancePtr->SendBufferPtr;
469 for (Index = 0 ; Index < ARRAY_SIZE(FlashInst); Index++) {
470 if (Instruction == FlashInst[Index].OpCode) {
476 * Set the RX FIFO threshold
478 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
479 XQSPIPS_RXWR_OFFSET, XQSPIPS_RXFIFO_THRESHOLD_OPT);
482 * If the slave select is "Forced" or under manual control,
483 * set the slave select now, before beginning the transfer.
485 if (XQspiPs_IsManualChipSelect(InstancePtr)) {
486 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
488 ConfigReg &= ~XQSPIPS_CR_SSCTRL_MASK;
489 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
497 XQspiPs_Enable(InstancePtr);
500 * Clear all the interrrupts.
502 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress, XQSPIPS_SR_OFFSET,
503 XQSPIPS_IXR_WR_TO_CLR_MASK);
505 if (Index < ARRAY_SIZE(FlashInst)) {
506 CurrInst = &FlashInst[Index];
508 * Check for WRSR instruction which has different size for
509 * Spansion (3 bytes) and Micron (2 bytes)
511 if( (CurrInst->OpCode == XQSPIPS_FLASH_OPCODE_WRSR) &&
513 CurrInst->InstSize = 3;
514 CurrInst->TxOffset = XQSPIPS_TXD_11_OFFSET;
519 * If instruction not present in table
521 if (Index == ARRAY_SIZE(FlashInst)) {
523 * Assign current instruction, size and TXD register to be used
524 * The InstSize mentioned in case of instructions greater than
525 * 4 bytes is not the actual size, but is indicative of
526 * the TXD register used.
527 * The remaining bytes of the instruction will be transmitted
528 * through TXD0 below.
532 case XQSPIPS_SIZE_ONE:
533 CurrInst->OpCode = Instruction;
534 CurrInst->InstSize = XQSPIPS_SIZE_ONE;
535 CurrInst->TxOffset = XQSPIPS_TXD_01_OFFSET;
540 case XQSPIPS_SIZE_TWO:
541 CurrInst->OpCode = Instruction;
542 CurrInst->InstSize = XQSPIPS_SIZE_TWO;
543 CurrInst->TxOffset = XQSPIPS_TXD_10_OFFSET;
548 case XQSPIPS_SIZE_THREE:
549 CurrInst->OpCode = Instruction;
550 CurrInst->InstSize = XQSPIPS_SIZE_THREE;
551 CurrInst->TxOffset = XQSPIPS_TXD_11_OFFSET;
557 CurrInst->OpCode = Instruction;
558 CurrInst->InstSize = XQSPIPS_SIZE_FOUR;
559 CurrInst->TxOffset = XQSPIPS_TXD_00_OFFSET;
565 * If the instruction size in not 4 bytes then the data received needs
568 if( CurrInst->InstSize != 4 ) {
569 InstancePtr->ShiftReadData = 1;
571 InstancePtr->ShiftReadData = 0;
574 /* Get the complete command (flash inst + address/data) */
575 Data = *((u32 *)InstancePtr->SendBufferPtr);
576 InstancePtr->SendBufferPtr += CurrInst->InstSize;
577 InstancePtr->RemainingBytes -= CurrInst->InstSize;
578 if (InstancePtr->RemainingBytes < 0) {
579 InstancePtr->RemainingBytes = 0;
582 /* Write the command to the FIFO */
583 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
584 CurrInst->TxOffset, Data);
588 * If switching from TXD1/2/3 to TXD0, then start transfer and
589 * check for FIFO empty
591 if(SwitchFlag == 1) {
594 * If, in Manual Start mode, start the transfer.
596 if (XQspiPs_IsManualStart(InstancePtr)) {
597 ConfigReg = XQspiPs_ReadReg(
598 InstancePtr->Config.BaseAddress,
600 ConfigReg |= XQSPIPS_CR_MANSTRT_MASK;
601 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
602 XQSPIPS_CR_OFFSET, ConfigReg);
605 * Wait for the transfer to finish by polling Tx fifo status.
608 StatusReg = XQspiPs_ReadReg(
609 InstancePtr->Config.BaseAddress,
611 } while ((StatusReg & XQSPIPS_IXR_TXOW_MASK) == 0);
616 * Fill the Tx FIFO with as many bytes as it takes (or as many as
619 while ((InstancePtr->RemainingBytes > 0) &&
620 (TransCount < XQSPIPS_FIFO_DEPTH)) {
621 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
622 XQSPIPS_TXD_00_OFFSET,
623 *((u32 *)InstancePtr->SendBufferPtr));
624 InstancePtr->SendBufferPtr += 4;
625 InstancePtr->RemainingBytes -= 4;
626 if (InstancePtr->RemainingBytes < 0) {
627 InstancePtr->RemainingBytes = 0;
633 * Enable QSPI interrupts (connecting to the interrupt controller and
634 * enabling interrupts should have been done by the caller).
636 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
637 XQSPIPS_IER_OFFSET, XQSPIPS_IXR_RXNEMPTY_MASK |
638 XQSPIPS_IXR_TXOW_MASK | XQSPIPS_IXR_RXOVR_MASK |
639 XQSPIPS_IXR_TXUF_MASK);
642 * If, in Manual Start mode, Start the transfer.
644 if (XQspiPs_IsManualStart(InstancePtr)) {
645 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
647 ConfigReg |= XQSPIPS_CR_MANSTRT_MASK;
648 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
649 XQSPIPS_CR_OFFSET, ConfigReg);
655 /*****************************************************************************/
657 * Transfers specified data on the QSPI bus in polled mode.
659 * The caller has the option of providing two different buffers for send and
660 * receive, or one buffer for both send and receive, or no buffer for receive.
661 * The receive buffer must be at least as big as the send buffer to prevent
662 * unwanted memory writes. This implies that the byte count passed in as an
663 * argument must be the smaller of the two buffers if they differ in size.
664 * Here are some sample usages:
666 * XQspiPs_PolledTransfer(InstancePtr, SendBuf, RecvBuf, ByteCount)
667 * The caller wishes to send and receive, and provides two different
668 * buffers for send and receive.
670 * XQspiPs_PolledTransfer(InstancePtr, SendBuf, NULL, ByteCount)
671 * The caller wishes only to send and does not care about the received
672 * data. The driver ignores the received data in this case.
674 * XQspiPs_PolledTransfer(InstancePtr, SendBuf, SendBuf, ByteCount)
675 * The caller wishes to send and receive, but provides the same buffer
676 * for doing both. The driver sends the data and overwrites the send
677 * buffer with received data as it transfers the data.
679 * XQspiPs_PolledTransfer(InstancePtr, RecvBuf, RecvBuf, ByteCount)
680 * The caller wishes to only receive and does not care about sending
681 * data. In this case, the caller must still provide a send buffer, but
682 * it can be the same as the receive buffer if the caller does not care
683 * what it sends. The device must send N bytes of data if it wishes to
684 * receive N bytes of data.
688 * @param InstancePtr is a pointer to the XQspiPs instance.
689 * @param SendBufPtr is a pointer to a data buffer that needs to be
690 * transmitted. This buffer must not be NULL.
691 * @param RecvBufPtr is a pointer to a buffer for received data.
692 * This argument can be NULL if do not care about receiving.
693 * @param ByteCount contains the number of bytes to send/receive.
694 * The number of bytes received always equals the number of bytes
697 * - XST_SUCCESS if the buffers are successfully handed off to the
698 * device for transfer.
699 * - XST_DEVICE_BUSY indicates that a data transfer is already in
700 * progress. This is determined by the driver.
704 * This function is not thread-safe. The higher layer software must ensure that
705 * no two threads are transferring data on the QSPI bus at the same time.
707 ******************************************************************************/
708 s32 XQspiPs_PolledTransfer(XQspiPs *InstancePtr, u8 *SendBufPtr,
709 u8 *RecvBufPtr, u32 ByteCount)
717 XQspiPsInstFormat *CurrInst;
718 XQspiPsInstFormat NewInst[2];
720 u8 IsManualStart = FALSE;
723 CurrInst = &NewInst[0];
725 * The RecvBufPtr argument can be NULL.
727 Xil_AssertNonvoid(InstancePtr != NULL);
728 Xil_AssertNonvoid(SendBufPtr != NULL);
729 Xil_AssertNonvoid(ByteCount > 0);
730 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
733 * Check whether there is another transfer in progress. Not thread-safe.
735 if (InstancePtr->IsBusy) {
736 return XST_DEVICE_BUSY;
740 * Set the busy flag, which will be cleared when the transfer is
743 InstancePtr->IsBusy = TRUE;
746 * Set up buffer pointers.
748 InstancePtr->SendBufferPtr = SendBufPtr;
749 InstancePtr->RecvBufferPtr = RecvBufPtr;
751 InstancePtr->RequestedBytes = ByteCount;
752 InstancePtr->RemainingBytes = ByteCount;
755 * The first byte with every chip-select assertion is always
756 * expected to be an instruction for flash interface mode
758 Instruction = *InstancePtr->SendBufferPtr;
760 for (Index = 0 ; Index < ARRAY_SIZE(FlashInst); Index++) {
761 if (Instruction == FlashInst[Index].OpCode) {
767 * Set the RX FIFO threshold
769 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
770 XQSPIPS_RXWR_OFFSET, XQSPIPS_RXFIFO_THRESHOLD_OPT);
773 * If the slave select is "Forced" or under manual control,
774 * set the slave select now, before beginning the transfer.
776 if (XQspiPs_IsManualChipSelect(InstancePtr)) {
777 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
779 ConfigReg &= ~XQSPIPS_CR_SSCTRL_MASK;
780 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
788 XQspiPs_Enable(InstancePtr);
790 if (Index < ARRAY_SIZE(FlashInst)) {
792 CurrInst = &FlashInst[Index];
794 * Check for WRSR instruction which has different size for
795 * Spansion (3 bytes) and Micron (2 bytes)
797 if( (CurrInst->OpCode == XQSPIPS_FLASH_OPCODE_WRSR) &&
799 CurrInst->InstSize = 3;
800 CurrInst->TxOffset = XQSPIPS_TXD_11_OFFSET;
805 * If instruction not present in table
807 if (Index == ARRAY_SIZE(FlashInst)) {
809 * Assign current instruction, size and TXD register to be used.
810 * The InstSize mentioned in case of instructions greater than 4 bytes
811 * is not the actual size, but is indicative of the TXD register used.
812 * The remaining bytes of the instruction will be transmitted
813 * through TXD0 below.
817 case XQSPIPS_SIZE_ONE:
818 CurrInst->OpCode = Instruction;
819 CurrInst->InstSize = XQSPIPS_SIZE_ONE;
820 CurrInst->TxOffset = XQSPIPS_TXD_01_OFFSET;
825 case XQSPIPS_SIZE_TWO:
826 CurrInst->OpCode = Instruction;
827 CurrInst->InstSize = XQSPIPS_SIZE_TWO;
828 CurrInst->TxOffset = XQSPIPS_TXD_10_OFFSET;
833 case XQSPIPS_SIZE_THREE:
834 CurrInst->OpCode = Instruction;
835 CurrInst->InstSize = XQSPIPS_SIZE_THREE;
836 CurrInst->TxOffset = XQSPIPS_TXD_11_OFFSET;
842 CurrInst->OpCode = Instruction;
843 CurrInst->InstSize = XQSPIPS_SIZE_FOUR;
844 CurrInst->TxOffset = XQSPIPS_TXD_00_OFFSET;
850 * If the instruction size in not 4 bytes then the data received needs
853 if( CurrInst->InstSize != 4 ) {
854 InstancePtr->ShiftReadData = 1;
856 InstancePtr->ShiftReadData = 0;
859 /* Get the complete command (flash inst + address/data) */
860 Data = *((u32 *)InstancePtr->SendBufferPtr);
861 InstancePtr->SendBufferPtr += CurrInst->InstSize;
862 InstancePtr->RemainingBytes -= CurrInst->InstSize;
863 if (InstancePtr->RemainingBytes < 0) {
864 InstancePtr->RemainingBytes = 0;
867 /* Write the command to the FIFO */
868 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
869 CurrInst->TxOffset, Data);
873 * If switching from TXD1/2/3 to TXD0, then start transfer and
874 * check for FIFO empty
876 if(SwitchFlag == 1) {
879 * If, in Manual Start mode, start the transfer.
881 if (XQspiPs_IsManualStart(InstancePtr)) {
882 ConfigReg = XQspiPs_ReadReg(
883 InstancePtr->Config.BaseAddress,
885 ConfigReg |= XQSPIPS_CR_MANSTRT_MASK;
886 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
887 XQSPIPS_CR_OFFSET, ConfigReg);
890 * Wait for the transfer to finish by polling Tx fifo status.
893 StatusReg = XQspiPs_ReadReg(
894 InstancePtr->Config.BaseAddress,
896 } while ((StatusReg & XQSPIPS_IXR_TXOW_MASK) == 0);
901 * Check if manual start is selected and store it in a
902 * local varibale for reference. This is to avoid reading
903 * the config register everytime.
905 IsManualStart = XQspiPs_IsManualStart(InstancePtr);
908 * Fill the DTR/FIFO with as many bytes as it will take (or as
909 * many as we have to send).
911 while ((InstancePtr->RemainingBytes > 0) &&
912 (TransCount < XQSPIPS_FIFO_DEPTH)) {
913 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
914 XQSPIPS_TXD_00_OFFSET,
915 *((u32 *)InstancePtr->SendBufferPtr));
916 InstancePtr->SendBufferPtr += 4;
917 InstancePtr->RemainingBytes -= 4;
918 if (InstancePtr->RemainingBytes < 0) {
919 InstancePtr->RemainingBytes = 0;
924 while((InstancePtr->RemainingBytes > 0) ||
925 (InstancePtr->RequestedBytes > 0)) {
928 * Fill the TX FIFO with RX threshold no. of entries (or as
929 * many as we have to send, in case that's less).
931 while ((InstancePtr->RemainingBytes > 0) &&
932 (TransCount < XQSPIPS_RXFIFO_THRESHOLD_OPT)) {
933 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
934 XQSPIPS_TXD_00_OFFSET,
935 *((u32 *)InstancePtr->SendBufferPtr));
936 InstancePtr->SendBufferPtr += 4;
937 InstancePtr->RemainingBytes -= 4;
938 if (InstancePtr->RemainingBytes < 0) {
939 InstancePtr->RemainingBytes = 0;
945 * If, in Manual Start mode, start the transfer.
947 if (IsManualStart == TRUE) {
948 ConfigReg = XQspiPs_ReadReg(
949 InstancePtr->Config.BaseAddress,
951 ConfigReg |= XQSPIPS_CR_MANSTRT_MASK;
952 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
953 XQSPIPS_CR_OFFSET, ConfigReg);
957 * Reset TransCount - this is only used to fill TX FIFO
959 * RxCount is used to keep track of data received
964 * Wait for RX FIFO to reach threshold (or)
965 * TX FIFO to become empty.
966 * The latter check is required for
967 * small transfers (<32 words) and
968 * when the last chunk in a large data transfer is < 32 words.
972 StatusReg = XQspiPs_ReadReg(
973 InstancePtr->Config.BaseAddress,
975 } while ( ((StatusReg & XQSPIPS_IXR_TXOW_MASK) == 0) &&
976 ((StatusReg & XQSPIPS_IXR_RXNEMPTY_MASK) == 0) );
979 * A transmit has just completed. Process received data
980 * and check for more data to transmit.
981 * First get the data received as a result of the
982 * transmit that just completed. Receive data based on the
983 * count obtained while filling tx fifo. Always get
984 * the received data, but only fill the receive
985 * buffer if it points to something (the upper layer
986 * software may not care to receive data).
988 while ((InstancePtr->RequestedBytes > 0) &&
989 (RxCount < XQSPIPS_RXFIFO_THRESHOLD_OPT )) {
994 if (InstancePtr->RecvBufferPtr != NULL) {
995 if (InstancePtr->RequestedBytes < 4) {
996 Data = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
998 XQspiPs_GetReadData(InstancePtr, Data,
999 InstancePtr->RequestedBytes);
1001 (*(u32 *)InstancePtr->RecvBufferPtr) =
1002 XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
1003 XQSPIPS_RXD_OFFSET);
1004 InstancePtr->RecvBufferPtr += 4;
1005 InstancePtr->RequestedBytes -= 4;
1006 if (InstancePtr->RequestedBytes < 0) {
1007 InstancePtr->RequestedBytes = 0;
1011 Data = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
1012 XQSPIPS_RXD_OFFSET);
1013 InstancePtr->RequestedBytes -= 4;
1020 * If the Slave select lines are being manually controlled, disable
1021 * them because the transfer is complete.
1023 if (XQspiPs_IsManualChipSelect(InstancePtr)) {
1024 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
1026 ConfigReg |= XQSPIPS_CR_SSCTRL_MASK;
1027 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
1028 XQSPIPS_CR_OFFSET, ConfigReg);
1032 * Clear the busy flag.
1034 InstancePtr->IsBusy = FALSE;
1037 * Disable the device.
1039 XQspiPs_Disable(InstancePtr);
1042 * Reset the RX FIFO threshold to one
1044 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
1045 XQSPIPS_RXWR_OFFSET, XQSPIPS_RXWR_RESET_VALUE);
1050 /*****************************************************************************/
1053 * Read the flash in Linear QSPI mode.
1055 * @param InstancePtr is a pointer to the XQspiPs instance.
1056 * @param RecvBufPtr is a pointer to a buffer for received data.
1057 * @param Address is the starting address within the flash from
1058 * from where data needs to be read.
1059 * @param ByteCount contains the number of bytes to receive.
1062 * - XST_SUCCESS if read is performed
1063 * - XST_FAILURE if Linear mode is not set
1068 ******************************************************************************/
1069 int XQspiPs_LqspiRead(XQspiPs *InstancePtr, u8 *RecvBufPtr,
1070 u32 Address, unsigned ByteCount)
1072 Xil_AssertNonvoid(InstancePtr != NULL);
1073 Xil_AssertNonvoid(RecvBufPtr != NULL);
1074 Xil_AssertNonvoid(ByteCount > 0);
1075 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
1077 #ifndef XPAR_PS7_QSPI_LINEAR_0_S_AXI_BASEADDR
1078 #define XPAR_PS7_QSPI_LINEAR_0_S_AXI_BASEADDR 0xFC000000
1081 * Enable the controller
1083 XQspiPs_Enable(InstancePtr);
1085 if (XQspiPs_GetLqspiConfigReg(InstancePtr) &
1086 XQSPIPS_LQSPI_CR_LINEAR_MASK) {
1087 memcpy((void*)RecvBufPtr,
1088 (const void*)(XPAR_PS7_QSPI_LINEAR_0_S_AXI_BASEADDR +
1097 * Disable the controller
1099 XQspiPs_Disable(InstancePtr);
1103 /*****************************************************************************/
1106 * Selects the slave with which the master communicates.
1108 * The user is not allowed to select the slave while a transfer is in progress.
1110 * @param InstancePtr is a pointer to the XQspiPs instance.
1113 * - XST_SUCCESS if the slave is selected or deselected
1115 * - XST_DEVICE_BUSY if a transfer is in progress, slave cannot be
1120 * This function only sets the slave which will be selected when a transfer
1121 * occurs. The slave is not selected when the QSPI is idle.
1123 ******************************************************************************/
1124 int XQspiPs_SetSlaveSelect(XQspiPs *InstancePtr)
1128 Xil_AssertNonvoid(InstancePtr != NULL);
1129 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
1132 * Do not allow the slave select to change while a transfer is in
1133 * progress. Not thread-safe.
1135 if (InstancePtr->IsBusy) {
1136 return XST_DEVICE_BUSY;
1142 ConfigReg = XQspiPs_ReadReg(InstancePtr->Config.BaseAddress,
1144 ConfigReg &= ~XQSPIPS_CR_SSCTRL_MASK;
1145 XQspiPs_WriteReg(InstancePtr->Config.BaseAddress,
1146 XQSPIPS_CR_OFFSET, ConfigReg);
1151 /*****************************************************************************/
1154 * Sets the status callback function, the status handler, which the driver
1155 * calls when it encounters conditions that should be reported to upper
1156 * layer software. The handler executes in an interrupt context, so it must
1157 * minimize the amount of processing performed. One of the following status
1158 * events is passed to the status handler.
1162 * XST_SPI_TRANSFER_DONE The requested data transfer is done
1164 * XST_SPI_TRANSMIT_UNDERRUN As a slave device, the master clocked data
1165 * but there were none available in the transmit
1166 * register/FIFO. This typically means the slave
1167 * application did not issue a transfer request
1168 * fast enough, or the processor/driver could not
1169 * fill the transmit register/FIFO fast enough.
1171 * XST_SPI_RECEIVE_OVERRUN The QSPI device lost data. Data was received
1172 * but the receive data register/FIFO was full.
1175 * @param InstancePtr is a pointer to the XQspiPs instance.
1176 * @param CallBackRef is the upper layer callback reference passed back
1177 * when the callback function is invoked.
1178 * @param FuncPtr is the pointer to the callback function.
1184 * The handler is called within interrupt context, so it should do its work
1185 * quickly and queue potentially time-consuming work to a task-level thread.
1187 ******************************************************************************/
1188 void XQspiPs_SetStatusHandler(XQspiPs *InstancePtr, void *CallBackRef,
1189 XQspiPs_StatusHandler FuncPtr)
1191 Xil_AssertVoid(InstancePtr != NULL);
1192 Xil_AssertVoid(FuncPtr != NULL);
1193 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
1195 InstancePtr->StatusHandler = FuncPtr;
1196 InstancePtr->StatusRef = CallBackRef;
1199 /*****************************************************************************/
1202 * This is a stub for the status callback. The stub is here in case the upper
1203 * layers forget to set the handler.
1205 * @param CallBackRef is a pointer to the upper layer callback reference
1206 * @param StatusEvent is the event that just occurred.
1207 * @param ByteCount is the number of bytes transferred up until the event
1214 ******************************************************************************/
1215 static void StubStatusHandler(void *CallBackRef, u32 StatusEvent,
1222 Xil_AssertVoidAlways();
1225 /*****************************************************************************/
1228 * The interrupt handler for QSPI interrupts. This function must be connected
1229 * by the user to an interrupt controller.
1231 * The interrupts that are handled are:
1234 * - Data Transmit Register (FIFO) Empty. This interrupt is generated when the
1235 * transmit register or FIFO is empty. The driver uses this interrupt during a
1236 * transmission to continually send/receive data until the transfer is done.
1238 * - Data Transmit Register (FIFO) Underflow. This interrupt is generated when
1239 * the QSPI device, when configured as a slave, attempts to read an empty
1240 * DTR/FIFO. An empty DTR/FIFO usually means that software is not giving the
1241 * device data in a timely manner. No action is taken by the driver other than
1242 * to inform the upper layer software of the error.
1244 * - Data Receive Register (FIFO) Overflow. This interrupt is generated when the
1245 * QSPI device attempts to write a received byte to an already full DRR/FIFO.
1246 * A full DRR/FIFO usually means software is not emptying the data in a timely
1247 * manner. No action is taken by the driver other than to inform the upper
1248 * layer software of the error.
1250 * @param InstancePtr is a pointer to the XQspiPs instance.
1256 * The slave select register is being set to deselect the slave when a transfer
1259 ******************************************************************************/
1260 void XQspiPs_InterruptHandler(void *InstancePtr)
1262 XQspiPs *QspiPtr = (XQspiPs *)InstancePtr;
1268 unsigned BytesDone; /* Number of bytes done so far. */
1270 Xil_AssertVoid(InstancePtr != NULL);
1271 Xil_AssertVoid(QspiPtr->IsReady == XIL_COMPONENT_IS_READY);
1274 * Immediately clear the interrupts in case the ISR causes another
1275 * interrupt to be generated. If we clear at the end of the ISR,
1276 * we may miss newly generated interrupts. This occurs because we
1277 * transmit from within the ISR, which could potentially cause another
1278 * TX_EMPTY interrupt.
1280 IntrStatus = XQspiPs_ReadReg(QspiPtr->Config.BaseAddress,
1282 XQspiPs_WriteReg(QspiPtr->Config.BaseAddress, XQSPIPS_SR_OFFSET,
1283 (IntrStatus & XQSPIPS_IXR_WR_TO_CLR_MASK));
1284 XQspiPs_WriteReg(QspiPtr->Config.BaseAddress, XQSPIPS_IDR_OFFSET,
1285 XQSPIPS_IXR_TXOW_MASK | XQSPIPS_IXR_RXNEMPTY_MASK |
1286 XQSPIPS_IXR_RXOVR_MASK | XQSPIPS_IXR_TXUF_MASK);
1288 if ((IntrStatus & XQSPIPS_IXR_TXOW_MASK) ||
1289 (IntrStatus & XQSPIPS_IXR_RXNEMPTY_MASK)) {
1292 * Rx FIFO has just reached threshold no. of entries.
1293 * Read threshold no. of entries from RX FIFO
1294 * Another possiblity of entering this loop is when
1295 * the last byte has been transmitted and TX FIFO is empty,
1296 * in which case, read all the data from RX FIFO.
1297 * Always get the received data, but only fill the
1298 * receive buffer if it is not null (it can be null when
1299 * the device does not care to receive data).
1301 TransCount = QspiPtr->RequestedBytes - QspiPtr->RemainingBytes;
1302 if (TransCount % 4) {
1303 TransCount = TransCount/4 + 1;
1305 TransCount = TransCount/4;
1308 while ((Count < TransCount) &&
1309 (Count < XQSPIPS_RXFIFO_THRESHOLD_OPT)) {
1311 if (QspiPtr->RecvBufferPtr != NULL) {
1312 if (QspiPtr->RequestedBytes < 4) {
1313 Data = XQspiPs_ReadReg(QspiPtr->Config.BaseAddress,
1314 XQSPIPS_RXD_OFFSET);
1315 XQspiPs_GetReadData(QspiPtr, Data,
1316 QspiPtr->RequestedBytes);
1318 (*(u32 *)QspiPtr->RecvBufferPtr) =
1319 XQspiPs_ReadReg(QspiPtr->Config.BaseAddress,
1320 XQSPIPS_RXD_OFFSET);
1321 QspiPtr->RecvBufferPtr += 4;
1322 QspiPtr->RequestedBytes -= 4;
1323 if (QspiPtr->RequestedBytes < 0) {
1324 QspiPtr->RequestedBytes = 0;
1328 XQspiPs_ReadReg(QspiPtr->Config.BaseAddress,
1329 XQSPIPS_RXD_OFFSET);
1330 QspiPtr->RequestedBytes -= 4;
1331 if (QspiPtr->RequestedBytes < 0) {
1332 QspiPtr->RequestedBytes = 0;
1340 * Interrupt asserted as TX_OW got asserted
1341 * See if there is more data to send.
1342 * Fill TX FIFO with RX threshold no. of entries or
1343 * remaining entries (in case that is less than threshold)
1345 while ((QspiPtr->RemainingBytes > 0) &&
1346 (Count < XQSPIPS_RXFIFO_THRESHOLD_OPT)) {
1350 XQspiPs_WriteReg(QspiPtr->Config.BaseAddress,
1351 XQSPIPS_TXD_00_OFFSET,
1352 *((u32 *)QspiPtr->SendBufferPtr));
1353 QspiPtr->SendBufferPtr += 4;
1354 QspiPtr->RemainingBytes -= 4;
1355 if (QspiPtr->RemainingBytes < 0) {
1356 QspiPtr->RemainingBytes = 0;
1362 if ((QspiPtr->RemainingBytes == 0) &&
1363 (QspiPtr->RequestedBytes == 0)) {
1365 * No more data to send. Disable the interrupt
1366 * and inform the upper layer software that the
1367 * transfer is done. The interrupt will be re-enabled
1368 * when another transfer is initiated.
1370 XQspiPs_WriteReg(QspiPtr->Config.BaseAddress,
1372 XQSPIPS_IXR_RXNEMPTY_MASK |
1373 XQSPIPS_IXR_TXOW_MASK |
1374 XQSPIPS_IXR_RXOVR_MASK |
1375 XQSPIPS_IXR_TXUF_MASK);
1378 * If the Slave select is being manually controlled,
1379 * disable it because the transfer is complete.
1381 if (XQspiPs_IsManualChipSelect(InstancePtr)) {
1382 ConfigReg = XQspiPs_ReadReg(
1383 QspiPtr->Config.BaseAddress,
1385 ConfigReg |= XQSPIPS_CR_SSCTRL_MASK;
1386 XQspiPs_WriteReg(QspiPtr->Config.BaseAddress,
1392 * Clear the busy flag.
1394 QspiPtr->IsBusy = FALSE;
1397 * Disable the device.
1399 XQspiPs_Disable(QspiPtr);
1402 * Reset the RX FIFO threshold to one
1404 XQspiPs_WriteReg(QspiPtr->Config.BaseAddress,
1405 XQSPIPS_RXWR_OFFSET, XQSPIPS_RXWR_RESET_VALUE);
1407 QspiPtr->StatusHandler(QspiPtr->StatusRef,
1408 XST_SPI_TRANSFER_DONE,
1409 QspiPtr->RequestedBytes);
1412 * Enable the TXOW interrupt.
1414 XQspiPs_WriteReg(QspiPtr->Config.BaseAddress,
1416 XQSPIPS_IXR_RXNEMPTY_MASK |
1417 XQSPIPS_IXR_TXOW_MASK |
1418 XQSPIPS_IXR_RXOVR_MASK |
1419 XQSPIPS_IXR_TXUF_MASK);
1421 * If, in Manual Start mode, start the transfer.
1423 if (XQspiPs_IsManualStart(QspiPtr)) {
1424 ConfigReg = XQspiPs_ReadReg(
1425 QspiPtr->Config.BaseAddress,
1427 ConfigReg |= XQSPIPS_CR_MANSTRT_MASK;
1429 QspiPtr->Config.BaseAddress,
1430 XQSPIPS_CR_OFFSET, ConfigReg);
1436 * Check for overflow and underflow errors.
1438 if (IntrStatus & XQSPIPS_IXR_RXOVR_MASK) {
1439 BytesDone = QspiPtr->RequestedBytes - QspiPtr->RemainingBytes;
1440 QspiPtr->IsBusy = FALSE;
1443 * If the Slave select lines is being manually controlled,
1444 * disable it because the transfer is complete.
1446 if (XQspiPs_IsManualChipSelect(InstancePtr)) {
1447 ConfigReg = XQspiPs_ReadReg(
1448 QspiPtr->Config.BaseAddress,
1450 ConfigReg |= XQSPIPS_CR_SSCTRL_MASK;
1451 XQspiPs_WriteReg(QspiPtr->Config.BaseAddress,
1452 XQSPIPS_CR_OFFSET, ConfigReg);
1456 * Disable the device.
1458 XQspiPs_Disable(QspiPtr);
1461 * Reset the RX FIFO threshold to one
1463 XQspiPs_WriteReg(QspiPtr->Config.BaseAddress,
1464 XQSPIPS_RXWR_OFFSET, XQSPIPS_RXWR_RESET_VALUE);
1466 QspiPtr->StatusHandler(QspiPtr->StatusRef,
1467 XST_SPI_RECEIVE_OVERRUN, BytesDone);
1470 if (IntrStatus & XQSPIPS_IXR_TXUF_MASK) {
1471 BytesDone = QspiPtr->RequestedBytes - QspiPtr->RemainingBytes;
1473 QspiPtr->IsBusy = FALSE;
1475 * If the Slave select lines is being manually controlled,
1476 * disable it because the transfer is complete.
1478 if (XQspiPs_IsManualChipSelect(InstancePtr)) {
1479 ConfigReg = XQspiPs_ReadReg(
1480 QspiPtr->Config.BaseAddress,
1482 ConfigReg |= XQSPIPS_CR_SSCTRL_MASK;
1483 XQspiPs_WriteReg(QspiPtr->Config.BaseAddress,
1484 XQSPIPS_CR_OFFSET, ConfigReg);
1488 * Disable the device.
1490 XQspiPs_Disable(QspiPtr);
1493 * Reset the RX FIFO threshold to one
1495 XQspiPs_WriteReg(QspiPtr->Config.BaseAddress,
1496 XQSPIPS_RXWR_OFFSET, XQSPIPS_RXWR_RESET_VALUE);
1498 QspiPtr->StatusHandler(QspiPtr->StatusRef,
1499 XST_SPI_TRANSMIT_UNDERRUN, BytesDone);
1504 /*****************************************************************************/
1507 * Copies data from Data to the Receive buffer.
1509 * @param InstancePtr is a pointer to the XQspiPs instance.
1510 * @param Data is the data which needs to be copied to the Rx buffer.
1511 * @param Size is the number of bytes to be copied to the Receive buffer.
1517 ******************************************************************************/
1518 static void XQspiPs_GetReadData(XQspiPs *InstancePtr, u32 Data, u8 Size)
1522 if (InstancePtr->RecvBufferPtr) {
1525 if (InstancePtr->ShiftReadData == 1) {
1526 *((u8 *)InstancePtr->RecvBufferPtr) =
1527 ((Data & 0xFF000000) >> 24);
1529 *((u8 *)InstancePtr->RecvBufferPtr) =
1532 InstancePtr->RecvBufferPtr += 1;
1535 if (InstancePtr->ShiftReadData == 1) {
1536 *((u16 *)InstancePtr->RecvBufferPtr) =
1537 ((Data & 0xFFFF0000) >> 16);
1539 *((u16 *)InstancePtr->RecvBufferPtr) =
1542 InstancePtr->RecvBufferPtr += 2;
1545 if (InstancePtr->ShiftReadData == 1) {
1546 *((u16 *)InstancePtr->RecvBufferPtr) =
1547 ((Data & 0x00FFFF00) >> 8);
1548 InstancePtr->RecvBufferPtr += 2;
1549 DataByte3 = ((Data & 0xFF000000) >> 24);
1550 *((u8 *)InstancePtr->RecvBufferPtr) = DataByte3;
1552 *((u16 *)InstancePtr->RecvBufferPtr) =
1554 InstancePtr->RecvBufferPtr += 2;
1555 DataByte3 = ((Data & 0x00FF0000) >> 16);
1556 *((u8 *)InstancePtr->RecvBufferPtr) = DataByte3;
1558 InstancePtr->RecvBufferPtr += 1;
1561 /* This will never execute */
1565 InstancePtr->ShiftReadData = 0;
1566 InstancePtr->RequestedBytes -= Size;
1567 if (InstancePtr->RequestedBytes < 0) {
1568 InstancePtr->RequestedBytes = 0;