1 // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
\r
3 // This file is part of SCSI2SD.
\r
5 // SCSI2SD is free software: you can redistribute it and/or modify
\r
6 // it under the terms of the GNU General Public License as published by
\r
7 // the Free Software Foundation, either version 3 of the License, or
\r
8 // (at your option) any later version.
\r
10 // SCSI2SD is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
\r
17 #pragma GCC push_options
\r
18 #pragma GCC optimize("-flto")
\r
22 #include "scsiPhy.h"
\r
26 #define scsiTarget_AUX_CTL (* (reg8 *) scsiTarget_datapath__DP_AUX_CTL_REG)
\r
28 // DMA controller can't handle any more bytes.
\r
29 #define MAX_DMA_BYTES 4095
\r
31 // Private DMA variables.
\r
32 static int dmaInProgress = 0;
\r
33 // used when transferring > MAX_DMA_BYTES.
\r
34 static uint8_t* dmaBuffer = NULL;
\r
35 static uint32_t dmaSentCount = 0;
\r
36 static uint32_t dmaTotalCount = 0;
\r
38 static uint8 scsiDmaRxChan = CY_DMA_INVALID_CHANNEL;
\r
39 static uint8 scsiDmaTxChan = CY_DMA_INVALID_CHANNEL;
\r
42 static uint8 scsiDmaRxTd[1] = { CY_DMA_INVALID_TD };
\r
43 static uint8 scsiDmaTxTd[1] = { CY_DMA_INVALID_TD };
\r
45 // Source of dummy bytes for DMA reads
\r
46 static uint8 dummyBuffer = 0xFF;
\r
48 volatile uint8_t scsiRxDMAComplete;
\r
49 volatile uint8_t scsiTxDMAComplete;
\r
51 CY_ISR_PROTO(scsiRxCompleteISR);
\r
52 CY_ISR(scsiRxCompleteISR)
\r
54 traceIrq(trace_scsiRxCompleteISR);
\r
55 scsiRxDMAComplete = 1;
\r
58 CY_ISR_PROTO(scsiTxCompleteISR);
\r
59 CY_ISR(scsiTxCompleteISR)
\r
61 traceIrq(trace_scsiTxCompleteISR);
\r
62 scsiTxDMAComplete = 1;
\r
65 CY_ISR_PROTO(scsiResetISR);
\r
66 CY_ISR(scsiResetISR)
\r
68 traceIrq(trace_scsiResetISR);
\r
69 scsiDev.resetFlag = 1;
\r
76 (SCSI_ReadPin(SCSI_In_DBx_DB7) << 7) |
\r
77 (SCSI_ReadPin(SCSI_In_DBx_DB6) << 6) |
\r
78 (SCSI_ReadPin(SCSI_In_DBx_DB5) << 5) |
\r
79 (SCSI_ReadPin(SCSI_In_DBx_DB4) << 4) |
\r
80 (SCSI_ReadPin(SCSI_In_DBx_DB3) << 3) |
\r
81 (SCSI_ReadPin(SCSI_In_DBx_DB2) << 2) |
\r
82 (SCSI_ReadPin(SCSI_In_DBx_DB1) << 1) |
\r
83 SCSI_ReadPin(SCSI_In_DBx_DB0);
\r
89 trace(trace_spinPhyTxFifo);
\r
90 while (unlikely(scsiPhyTxFifoFull()) && likely(!scsiDev.resetFlag)) {}
\r
93 trace(trace_spinPhyRxFifo);
\r
94 while (scsiPhyRxFifoEmpty() && likely(!scsiDev.resetFlag)) {}
\r
95 uint8_t val = scsiPhyRx();
\r
96 scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
\r
98 trace(trace_spinTxComplete);
\r
99 while (!(scsiPhyStatus() & SCSI_PHY_TX_COMPLETE) && likely(!scsiDev.resetFlag)) {}
\r
105 scsiReadPIO(uint8* data, uint32 count)
\r
110 while (i < count && likely(!scsiDev.resetFlag))
\r
112 uint8_t status = scsiPhyStatus();
\r
114 if (prep < count && (status & SCSI_PHY_TX_FIFO_NOT_FULL))
\r
119 if (status & SCSI_PHY_RX_FIFO_NOT_EMPTY)
\r
121 data[i] = scsiPhyRx();
\r
125 scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
\r
126 while (!(scsiPhyStatus() & SCSI_PHY_TX_COMPLETE) && likely(!scsiDev.resetFlag)) {}
\r
130 doRxSingleDMA(uint8* data, uint32 count)
\r
132 // Prepare DMA transfer
\r
134 trace(trace_doRxSingleDMA);
\r
136 CyDmaTdSetConfiguration(
\r
139 CY_DMA_DISABLE_TD, // Disable the DMA channel when TD completes count bytes
\r
140 SCSI_TX_DMA__TD_TERMOUT_EN // Trigger interrupt when complete
\r
142 CyDmaTdSetConfiguration(
\r
145 CY_DMA_DISABLE_TD, // Disable the DMA channel when TD completes count bytes
\r
147 SCSI_RX_DMA__TD_TERMOUT_EN // Trigger interrupt when complete
\r
152 LO16((uint32)&dummyBuffer),
\r
153 LO16((uint32)scsiTarget_datapath__F0_REG));
\r
156 LO16((uint32)scsiTarget_datapath__F1_REG),
\r
160 CyDmaChSetInitialTd(scsiDmaTxChan, scsiDmaTxTd[0]);
\r
161 CyDmaChSetInitialTd(scsiDmaRxChan, scsiDmaRxTd[0]);
\r
163 // The DMA controller is a bit trigger-happy. It will retain
\r
164 // a drq request that was triggered while the channel was
\r
166 CyDmaClearPendingDrq(scsiDmaTxChan);
\r
167 CyDmaClearPendingDrq(scsiDmaRxChan);
\r
169 scsiTxDMAComplete = 0;
\r
170 scsiRxDMAComplete = 0;
\r
172 CyDmaChEnable(scsiDmaRxChan, 1);
\r
173 CyDmaChEnable(scsiDmaTxChan, 1);
\r
177 scsiReadDMA(uint8* data, uint32 count)
\r
180 dmaTotalCount = count;
\r
183 uint32_t singleCount = (count > MAX_DMA_BYTES) ? MAX_DMA_BYTES : count;
\r
184 doRxSingleDMA(data, singleCount);
\r
185 dmaSentCount += count;
\r
191 if (scsiTxDMAComplete && scsiRxDMAComplete)
\r
193 // Wait until our scsi signals are consistent. This should only be
\r
195 trace(trace_spinTxComplete);
\r
196 while (!(scsiPhyStatus() & SCSI_PHY_TX_COMPLETE)) {}
\r
198 if (likely(dmaSentCount == dmaTotalCount))
\r
201 scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
\r
206 // Transfer was too large for a single DMA transfer. Continue
\r
207 // to send remaining bytes.
\r
208 uint32_t count = dmaTotalCount - dmaSentCount;
\r
209 if (unlikely(count > MAX_DMA_BYTES)) count = MAX_DMA_BYTES;
\r
210 doRxSingleDMA(dmaBuffer + dmaSentCount, count);
\r
211 dmaSentCount += count;
\r
222 scsiRead(uint8_t* data, uint32_t count)
\r
226 scsiReadPIO(data, count);
\r
230 scsiReadDMA(data, count);
\r
232 // Wait for the next DMA interrupt (or the 1ms systick)
\r
233 // It's beneficial to halt the processor to
\r
234 // give the DMA controller more memory bandwidth to work with.
\r
237 trace(trace_spinReadDMAPoll);
\r
238 while (!scsiReadDMAPoll() && likely(!scsiDev.resetFlag)) {};
\r
243 scsiWriteByte(uint8 value)
\r
245 trace(trace_spinPhyTxFifo);
\r
246 while (unlikely(scsiPhyTxFifoFull()) && likely(!scsiDev.resetFlag)) {}
\r
249 trace(trace_spinTxComplete);
\r
250 while (!(scsiPhyStatus() & SCSI_PHY_TX_COMPLETE) && likely(!scsiDev.resetFlag)) {}
\r
251 scsiPhyRxFifoClear();
\r
255 scsiWritePIO(const uint8_t* data, uint32_t count)
\r
259 while (i < count && likely(!scsiDev.resetFlag))
\r
261 if (!scsiPhyTxFifoFull())
\r
263 scsiPhyTx(data[i]);
\r
268 trace(trace_spinTxComplete);
\r
269 while (!(scsiPhyStatus() & SCSI_PHY_TX_COMPLETE) && likely(!scsiDev.resetFlag)) {}
\r
270 scsiPhyRxFifoClear();
\r
274 doTxSingleDMA(const uint8* data, uint32 count)
\r
276 // Prepare DMA transfer
\r
278 trace(trace_doTxSingleDMA);
\r
280 CyDmaTdSetConfiguration(
\r
283 CY_DMA_DISABLE_TD, // Disable the DMA channel when TD completes count bytes
\r
285 SCSI_TX_DMA__TD_TERMOUT_EN // Trigger interrupt when complete
\r
289 LO16((uint32)data),
\r
290 LO16((uint32)scsiTarget_datapath__F0_REG));
\r
291 CyDmaChSetInitialTd(scsiDmaTxChan, scsiDmaTxTd[0]);
\r
293 // The DMA controller is a bit trigger-happy. It will retain
\r
294 // a drq request that was triggered while the channel was
\r
296 CyDmaClearPendingDrq(scsiDmaTxChan);
\r
298 scsiTxDMAComplete = 0;
\r
299 scsiRxDMAComplete = 1;
\r
301 CyDmaChEnable(scsiDmaTxChan, 1);
\r
305 scsiWriteDMA(const uint8* data, uint32 count)
\r
308 dmaTotalCount = count;
\r
311 uint32_t singleCount = (count > MAX_DMA_BYTES) ? MAX_DMA_BYTES : count;
\r
312 doTxSingleDMA(data, singleCount);
\r
313 dmaSentCount += count;
\r
319 if (scsiTxDMAComplete)
\r
321 // Wait until our scsi signals are consistent. This should only be
\r
323 trace(trace_spinTxComplete);
\r
324 while (!(scsiPhyStatus() & SCSI_PHY_TX_COMPLETE)) {}
\r
326 if (likely(dmaSentCount == dmaTotalCount))
\r
328 scsiPhyRxFifoClear();
\r
334 // Transfer was too large for a single DMA transfer. Continue
\r
335 // to send remaining bytes.
\r
336 uint32_t count = dmaTotalCount - dmaSentCount;
\r
337 if (unlikely(count > MAX_DMA_BYTES)) count = MAX_DMA_BYTES;
\r
338 doTxSingleDMA(dmaBuffer + dmaSentCount, count);
\r
339 dmaSentCount += count;
\r
350 scsiWrite(const uint8_t* data, uint32_t count)
\r
354 scsiWritePIO(data, count);
\r
358 scsiWriteDMA(data, count);
\r
360 // Wait for the next DMA interrupt (or the 1ms systick)
\r
361 // It's beneficial to halt the processor to
\r
362 // give the DMA controller more memory bandwidth to work with.
\r
365 trace(trace_spinWriteDMAPoll);
\r
366 while (!scsiWriteDMAPoll() && likely(!scsiDev.resetFlag)) {};
\r
370 static inline void busSettleDelay(void)
\r
372 // Data Release time (switching IO) = 400ns
\r
373 // + Bus Settle time (switching phase) = 400ns.
\r
374 CyDelayUs(1); // Close enough.
\r
377 void scsiEnterPhase(int phase)
\r
379 int newPhase = phase > 0 ? phase : 0;
\r
380 if (newPhase != SCSI_CTL_PHASE_Read())
\r
382 SCSI_CTL_PHASE_Write(phase > 0 ? phase : 0);
\r
387 void scsiPhyReset()
\r
389 trace(trace_scsiPhyReset);
\r
396 CyDmaChSetRequest(scsiDmaTxChan, CY_DMA_CPU_TERM_CHAIN);
\r
397 CyDmaChSetRequest(scsiDmaRxChan, CY_DMA_CPU_TERM_CHAIN);
\r
398 trace(trace_spinDMAReset);
\r
399 while (!(scsiTxDMAComplete && scsiRxDMAComplete)) {}
\r
401 CyDmaChDisable(scsiDmaTxChan);
\r
402 CyDmaChDisable(scsiDmaRxChan);
\r
405 // Set the Clear bits for both SCSI device FIFOs
\r
406 scsiTarget_AUX_CTL = scsiTarget_AUX_CTL | 0x03;
\r
408 // Trigger RST outselves. It is connected to the datapath and will
\r
409 // ensure it returns to the idle state. The datapath runs at the BUS clk
\r
410 // speed (ie. same as the CPU), so we can be sure it is active for a sufficient
\r
412 SCSI_SetPin(SCSI_Out_RST);
\r
414 SCSI_CTL_PHASE_Write(0);
\r
415 SCSI_ClearPin(SCSI_Out_ATN);
\r
416 SCSI_ClearPin(SCSI_Out_BSY);
\r
417 SCSI_ClearPin(SCSI_Out_ACK);
\r
418 SCSI_ClearPin(SCSI_Out_RST);
\r
419 SCSI_ClearPin(SCSI_Out_SEL);
\r
420 SCSI_ClearPin(SCSI_Out_REQ);
\r
422 // Allow the FIFOs to fill up again.
\r
423 SCSI_ClearPin(SCSI_Out_RST);
\r
424 scsiTarget_AUX_CTL = scsiTarget_AUX_CTL & ~(0x03);
\r
426 SCSI_Parity_Error_Read(); // clear sticky bits
\r
429 static void scsiPhyInitDMA()
\r
431 // One-time init only.
\r
432 if (scsiDmaTxChan == CY_DMA_INVALID_CHANNEL)
\r
435 SCSI_RX_DMA_DmaInitialize(
\r
436 1, // Bytes per burst
\r
437 1, // request per burst
\r
438 HI16(CYDEV_PERIPH_BASE),
\r
439 HI16(CYDEV_SRAM_BASE)
\r
443 SCSI_TX_DMA_DmaInitialize(
\r
444 1, // Bytes per burst
\r
445 1, // request per burst
\r
446 HI16(CYDEV_SRAM_BASE),
\r
447 HI16(CYDEV_PERIPH_BASE)
\r
450 CyDmaChDisable(scsiDmaRxChan);
\r
451 CyDmaChDisable(scsiDmaTxChan);
\r
453 scsiDmaRxTd[0] = CyDmaTdAllocate();
\r
454 scsiDmaTxTd[0] = CyDmaTdAllocate();
\r
456 SCSI_RX_DMA_COMPLETE_StartEx(scsiRxCompleteISR);
\r
457 SCSI_TX_DMA_COMPLETE_StartEx(scsiTxCompleteISR);
\r
466 SCSI_RST_ISR_StartEx(scsiResetISR);
\r
470 // 2 = Parity error
\r
474 // 32 = other error
\r
479 // TEST DBx and DBp
\r
481 SCSI_Out_Ctl_Write(1); // Write bits manually.
\r
482 SCSI_CTL_PHASE_Write(__scsiphase_io); // Needed for parity generation
\r
483 for (i = 0; i < 256; ++i)
\r
485 SCSI_Out_Bits_Write(i);
\r
487 if (scsiReadDBxPins() != (i & 0xff))
\r
491 if (Lookup_OddParity[i & 0xff] != SCSI_ReadPin(SCSI_In_DBP))
\r
496 SCSI_Out_Ctl_Write(0); // Write bits normally.
\r
498 // TEST MSG, CD, IO
\r
499 for (i = 0; i < 8; ++i)
\r
501 SCSI_CTL_PHASE_Write(i);
\r
504 if (SCSI_ReadPin(SCSI_In_MSG) != !!(i & __scsiphase_msg))
\r
508 if (SCSI_ReadPin(SCSI_In_CD) != !!(i & __scsiphase_cd))
\r
512 if (SCSI_ReadPin(SCSI_In_IO) != !!(i & __scsiphase_io))
\r
517 SCSI_CTL_PHASE_Write(0);
\r
519 uint32_t signalsOut[] = { SCSI_Out_ATN, SCSI_Out_BSY, SCSI_Out_RST, SCSI_Out_SEL };
\r
520 uint32_t signalsIn[] = { SCSI_Filt_ATN, SCSI_Filt_BSY, SCSI_Filt_RST, SCSI_Filt_SEL };
\r
522 for (i = 0; i < 4; ++i)
\r
524 SCSI_SetPin(signalsOut[i]);
\r
528 for (j = 0; j < 4; ++j)
\r
532 if (! SCSI_ReadFilt(signalsIn[j]))
\r
539 if (SCSI_ReadFilt(signalsIn[j]))
\r
545 SCSI_ClearPin(signalsOut[i]);
\r
551 #pragma GCC pop_options
\r