1 // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
3 // This file is part of SCSI2SD.
5 // SCSI2SD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // SCSI2SD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
18 #include "stm32f2xx.h"
19 #include "stm32f2xx_hal.h"
20 #include "stm32f2xx_hal_dma.h"
31 // Assumes a 60MHz fpga clock.
32 // 7:6 Hold count, 45ns
33 // 5:3 Assertion count, 90ns
34 // 2:0 Deskew count, 55ns
35 #define SCSI_DEFAULT_TIMING ((0x3 << 6) | (0x6 << 3) | 0x4)
37 // 7:6 Hold count, 10ns
38 // 5:3 Assertion count, 30ns
39 // 2:0 Deskew count, 25ns
40 #define SCSI_FAST_TIMING ((0x1 << 6) | (0x2 << 3) | 0x2)
42 // Private DMA variables.
43 static int dmaInProgress
= 0;
45 static DMA_HandleTypeDef memToFSMC
;
46 static DMA_HandleTypeDef fsmcToMem
;
49 volatile uint8_t scsiRxDMAComplete
;
50 volatile uint8_t scsiTxDMAComplete
;
53 CY_ISR_PROTO(scsiRxCompleteISR
);
54 CY_ISR(scsiRxCompleteISR
)
56 traceIrq(trace_scsiRxCompleteISR
);
57 scsiRxDMAComplete
= 1;
60 CY_ISR_PROTO(scsiTxCompleteISR
);
61 CY_ISR(scsiTxCompleteISR
)
63 traceIrq(trace_scsiTxCompleteISR
);
64 scsiTxDMAComplete
= 1;
68 uint8_t scsiPhyFifoSel
= 0; // global
70 // scsi IRQ handler is initialised by the STM32 HAL. Connected to
72 // Note: naming is important to ensure this function is listed in the
74 void EXTI4_IRQHandler()
76 traceIrq(trace_scsiResetISR
);
78 // Make sure that interrupt flag is set
79 if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_4
) != RESET
) {
81 // Clear interrupt flag
82 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4
);
84 scsiDev
.resetFlag
= scsiDev
.resetFlag
|| scsiStatusRST();
85 // TODO grab SEL status as well
90 static void assertFail()
102 startScsiRx(uint32_t count
)
104 *SCSI_DATA_CNT_HI
= count
>> 8;
105 *SCSI_DATA_CNT_LO
= count
& 0xff;
106 *SCSI_DATA_CNT_SET
= 1;
113 if (!scsiPhyFifoAltEmpty()) {
120 trace(trace_spinPhyRxFifo
);
121 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
)) {}
123 uint8_t val
= scsiPhyRx();
124 // TODO scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
127 if (!scsiPhyFifoEmpty()) {
129 uint8_t k
__attribute((unused
));
130 while (!scsiPhyFifoEmpty()) { k
= scsiPhyRx(); ++j
; }
141 scsiReadPIO(uint8_t* data
, uint32_t count
)
143 for (int i
= 0; i
< count
; ++i
)
145 data
[i
] = scsiPhyRx();
147 // TODO scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
151 scsiReadDMA(uint8_t* data
, uint32_t count
)
153 // Prepare DMA transfer
155 trace(trace_doRxSingleDMA
);
157 scsiTxDMAComplete
= 1; // TODO not used much
158 scsiRxDMAComplete
= 0; // TODO not used much
160 HAL_DMA_Start(&fsmcToMem
, (uint32_t) SCSI_FIFO_DATA
, (uint32_t) data
, count
);
166 int complete
= __HAL_DMA_GET_COUNTER(&fsmcToMem
) == 0;
167 complete
= complete
&& (HAL_DMA_PollForTransfer(&fsmcToMem
, HAL_DMA_FULL_TRANSFER
, 0xffffffff) == HAL_OK
);
170 scsiTxDMAComplete
= 1; // TODO MM FIX IRQ
171 scsiRxDMAComplete
= 1;
175 // TODO MM scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
187 scsiRead(uint8_t* data
, uint32_t count
)
192 uint32_t chunk
= ((count
- i
) > SCSI_FIFO_DEPTH
)
193 ? SCSI_FIFO_DEPTH
: (count
- i
);
196 // DMA is doing 32bit transfers.
197 chunk
= chunk
& 0xFFFFFFF8;
201 while (i
< count
&& likely(!scsiDev
.resetFlag
))
203 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
)) {}
206 uint32_t nextChunk
= ((count
- i
- chunk
) > SCSI_FIFO_DEPTH
)
207 ? SCSI_FIFO_DEPTH
: (count
- i
- chunk
);
210 nextChunk
= nextChunk
& 0xFFFFFFF8;
214 startScsiRx(nextChunk
);
219 scsiReadPIO(data
+ i
, chunk
);
223 scsiReadDMA(data
+ i
, chunk
);
225 trace(trace_spinReadDMAPoll
);
227 while (!scsiReadDMAPoll() && likely(!scsiDev
.resetFlag
))
237 if (!scsiPhyFifoEmpty() || !scsiPhyFifoAltEmpty()) {
239 while (!scsiPhyFifoEmpty()) { scsiPhyRx(); ++j
; }
242 while (!scsiPhyFifoEmpty()) { scsiPhyRx(); ++k
; }
250 scsiWriteByte(uint8_t value
)
253 if (!scsiPhyFifoEmpty()) {
258 trace(trace_spinPhyTxFifo
);
262 trace(trace_spinTxComplete
);
263 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
)) {}
266 if (!scsiPhyFifoAltEmpty()) {
274 scsiWritePIO(const uint8_t* data
, uint32_t count
)
276 for (int i
= 0; i
< count
; ++i
)
283 scsiWriteDMA(const uint8_t* data
, uint32_t count
)
285 // Prepare DMA transfer
287 trace(trace_doTxSingleDMA
);
289 scsiTxDMAComplete
= 0;
290 scsiRxDMAComplete
= 1;
295 (uint32_t) SCSI_FIFO_DATA
,
302 int complete
= __HAL_DMA_GET_COUNTER(&memToFSMC
) == 0;
303 complete
= complete
&& (HAL_DMA_PollForTransfer(&memToFSMC
, HAL_DMA_FULL_TRANSFER
, 0xffffffff) == HAL_OK
);
306 scsiTxDMAComplete
= 1; // TODO MM FIX IRQ
307 scsiRxDMAComplete
= 1;
319 scsiWrite(const uint8_t* data
, uint32_t count
)
322 while (i
< count
&& likely(!scsiDev
.resetFlag
))
324 uint32_t chunk
= ((count
- i
) > SCSI_FIFO_DEPTH
)
325 ? SCSI_FIFO_DEPTH
: (count
- i
);
328 if (!scsiPhyFifoEmpty()) {
336 scsiWritePIO(data
+ i
, chunk
);
340 // DMA is doing 32bit transfers.
341 chunk
= chunk
& 0xFFFFFFF8;
342 scsiWriteDMA(data
+ i
, chunk
);
344 trace(trace_spinReadDMAPoll
);
346 while (!scsiWriteDMAPoll() && likely(!scsiDev
.resetFlag
))
351 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
356 if (!scsiPhyFifoAltEmpty()) {
365 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
370 if (!scsiPhyFifoAltEmpty()) {
377 static inline void busSettleDelay(void)
379 // Data Release time (switching IO) = 400ns
380 // + Bus Settle time (switching phase) = 400ns.
381 s2s_delay_us(1); // Close enough.
384 void scsiEnterBusFree()
386 *SCSI_CTRL_BSY
= 0x00;
387 // We now have a Bus Clear Delay of 800ns to release remaining signals.
388 *SCSI_CTRL_PHASE
= 0;
391 void scsiEnterPhase(int phase
)
393 // ANSI INCITS 362-2002 SPI-3 10.7.1:
394 // Phase changes are not allowed while REQ or ACK is asserted.
395 while (likely(!scsiDev
.resetFlag
) && scsiStatusACK()) {}
397 int newPhase
= phase
> 0 ? phase
: 0;
398 int oldPhase
= *SCSI_CTRL_PHASE
;
400 if (!scsiDev
.resetFlag
&& (!scsiPhyFifoEmpty() || !scsiPhyFifoAltEmpty())) {
404 if (newPhase
!= oldPhase
)
406 if ((newPhase
== DATA_IN
|| newPhase
== DATA_OUT
) &&
407 scsiDev
.target
->syncOffset
)
409 if (scsiDev
.target
->syncPeriod
== 25)
411 // SCSI2 FAST Timing. 10MB/s.
412 *SCSI_CTRL_TIMING
= SCSI_FAST_TIMING
;
415 *SCSI_CTRL_TIMING
= SCSI_DEFAULT_TIMING
;
417 *SCSI_CTRL_SYNC_OFFSET
= scsiDev
.target
->syncOffset
;
419 *SCSI_CTRL_SYNC_OFFSET
= 0;
420 *SCSI_CTRL_TIMING
= SCSI_DEFAULT_TIMING
;
423 *SCSI_CTRL_PHASE
= newPhase
;
426 if (scsiDev
.compatMode
< COMPAT_SCSI2
)
436 trace(trace_scsiPhyReset
);
439 trace(trace_spinDMAReset
);
440 HAL_DMA_Abort(&memToFSMC
);
441 HAL_DMA_Abort(&fsmcToMem
);
447 // Set the Clear bits for both SCSI device FIFOs
448 scsiTarget_AUX_CTL
= scsiTarget_AUX_CTL
| 0x03;
450 // Trigger RST outselves. It is connected to the datapath and will
451 // ensure it returns to the idle state. The datapath runs at the BUS clk
452 // speed (ie. same as the CPU), so we can be sure it is active for a sufficient
454 SCSI_RST_ISR_Disable();
455 SCSI_SetPin(SCSI_Out_RST
);
457 SCSI_CTL_PHASE_Write(0);
458 SCSI_ClearPin(SCSI_Out_ATN
);
459 SCSI_ClearPin(SCSI_Out_BSY
);
460 SCSI_ClearPin(SCSI_Out_ACK
);
461 SCSI_ClearPin(SCSI_Out_RST
);
462 SCSI_ClearPin(SCSI_Out_SEL
);
463 SCSI_ClearPin(SCSI_Out_REQ
);
465 // Allow the FIFOs to fill up again.
466 SCSI_ClearPin(SCSI_Out_RST
);
467 SCSI_RST_ISR_Enable();
468 scsiTarget_AUX_CTL
= scsiTarget_AUX_CTL
& ~(0x03);
470 SCSI_Parity_Error_Read(); // clear sticky bits
473 *SCSI_CTRL_PHASE
= 0x00;
474 *SCSI_CTRL_BSY
= 0x00;
475 s2s_fpgaReset(); // Clears fifos etc.
481 *SCSI_CTRL_SYNC_OFFSET
= 0;
482 *SCSI_CTRL_TIMING
= SCSI_DEFAULT_TIMING
;
484 // DMA Benchmark code
485 // Currently 10MB/s. Assume 20MB/s is achievable with 16 bits.
491 for (int i
= 0; i
< (100LL * 1024 * 1024 / SCSI_FIFO_DEPTH
); ++i
)
495 (uint32_t) &scsiDev
.data
[0],
496 (uint32_t) SCSI_FIFO_DATA
,
497 SCSI_FIFO_DEPTH
/ 4);
499 HAL_DMA_PollForTransfer(
501 HAL_DMA_FULL_TRANSFER
,
508 for(int i
= 0; i
< 10; ++i
) s2s_delay_ms(1000);
512 // FPGA comms test code
516 for (int j
= 0; j
< SCSI_FIFO_DEPTH
; ++j
)
521 if (!scsiPhyFifoEmpty())
526 *SCSI_CTRL_PHASE
= DATA_IN
;
529 (uint32_t) &scsiDev
.data
[0],
530 (uint32_t) SCSI_FIFO_DATA
,
531 SCSI_FIFO_DEPTH
/ 4);
533 HAL_DMA_PollForTransfer(
535 HAL_DMA_FULL_TRANSFER
,
538 if (!scsiPhyFifoFull())
543 memset(&scsiDev
.data
[0], 0, SCSI_FIFO_DEPTH
);
545 *SCSI_CTRL_PHASE
= DATA_OUT
;
548 (uint32_t) SCSI_FIFO_DATA
,
549 (uint32_t) &scsiDev
.data
[0],
552 HAL_DMA_PollForTransfer(
554 HAL_DMA_FULL_TRANSFER
,
557 if (!scsiPhyFifoEmpty())
563 for (int j
= 0; j
< SCSI_FIFO_DEPTH
; ++j
)
565 if (scsiDev
.data
[j
] != (uint8_t) j
)
578 static void scsiPhyInitDMA()
580 // One-time init only.
581 static uint8_t init
= 0;
586 // Memory to memory transfers can only be done using DMA2
589 // Transmit SCSI data. The source data is treated as the
590 // peripheral (even though this is memory-to-memory)
591 memToFSMC
.Instance
= DMA2_Stream0
;
592 memToFSMC
.Init
.Channel
= DMA_CHANNEL_0
;
593 memToFSMC
.Init
.Direction
= DMA_MEMORY_TO_MEMORY
;
594 memToFSMC
.Init
.PeriphInc
= DMA_PINC_ENABLE
;
595 memToFSMC
.Init
.MemInc
= DMA_MINC_DISABLE
;
596 memToFSMC
.Init
.PeriphDataAlignment
= DMA_PDATAALIGN_WORD
;
597 memToFSMC
.Init
.MemDataAlignment
= DMA_MDATAALIGN_BYTE
;
598 memToFSMC
.Init
.Mode
= DMA_NORMAL
;
599 memToFSMC
.Init
.Priority
= DMA_PRIORITY_LOW
;
600 // FIFO mode is needed to allow conversion from 32bit words to the
601 // 8bit FSMC interface.
602 memToFSMC
.Init
.FIFOMode
= DMA_FIFOMODE_ENABLE
;
604 // We only use 1 word (4 bytes) in the fifo at a time. Normally it's
605 // better to let the DMA fifo fill up then do burst transfers, but
606 // bursting out the FSMC interface will be very slow and may starve
607 // other (faster) transfers. We don't want to risk the SDIO transfers
608 // from overrun/underrun conditions.
609 memToFSMC
.Init
.FIFOThreshold
= DMA_FIFO_THRESHOLD_1QUARTERFULL
;
610 memToFSMC
.Init
.MemBurst
= DMA_MBURST_SINGLE
;
611 memToFSMC
.Init
.PeriphBurst
= DMA_PBURST_SINGLE
;
612 HAL_DMA_Init(&memToFSMC
);
614 // Receive SCSI data. The source data (fsmc) is treated as the
615 // peripheral (even though this is memory-to-memory)
616 fsmcToMem
.Instance
= DMA2_Stream1
;
617 fsmcToMem
.Init
.Channel
= DMA_CHANNEL_0
;
618 fsmcToMem
.Init
.Direction
= DMA_MEMORY_TO_MEMORY
;
619 fsmcToMem
.Init
.PeriphInc
= DMA_PINC_DISABLE
;
620 fsmcToMem
.Init
.MemInc
= DMA_MINC_ENABLE
;
621 fsmcToMem
.Init
.PeriphDataAlignment
= DMA_PDATAALIGN_BYTE
;
622 fsmcToMem
.Init
.MemDataAlignment
= DMA_MDATAALIGN_WORD
;
623 fsmcToMem
.Init
.Mode
= DMA_NORMAL
;
624 fsmcToMem
.Init
.Priority
= DMA_PRIORITY_LOW
;
625 fsmcToMem
.Init
.FIFOMode
= DMA_FIFOMODE_ENABLE
;
626 fsmcToMem
.Init
.FIFOThreshold
= DMA_FIFO_THRESHOLD_1QUARTERFULL
;
627 fsmcToMem
.Init
.MemBurst
= DMA_MBURST_SINGLE
;
628 fsmcToMem
.Init
.PeriphBurst
= DMA_PBURST_SINGLE
;
629 HAL_DMA_Init(&fsmcToMem
);
631 // TODO configure IRQs
640 *SCSI_CTRL_IDMASK
= 0x00; // Reset in scsiPhyConfig
641 *SCSI_CTRL_PHASE
= 0x00;
642 *SCSI_CTRL_BSY
= 0x00;
647 *SCSI_CTRL_SYNC_OFFSET
= 0;
648 *SCSI_CTRL_TIMING
= SCSI_DEFAULT_TIMING
;
654 if (scsiDev
.boardCfg
.flags6
& S2S_CFG_ENABLE_TERMINATOR
)
656 HAL_GPIO_WritePin(nTERM_EN_GPIO_Port
, nTERM_EN_Pin
, GPIO_PIN_RESET
);
660 HAL_GPIO_WritePin(nTERM_EN_GPIO_Port
, nTERM_EN_Pin
, GPIO_PIN_SET
);
665 for (int i
= 0; i
< 8; ++i
)
667 const S2S_TargetCfg
* cfg
= s2s_getConfigById(i
);
668 if (cfg
&& (cfg
->scsiId
& S2S_CFG_TARGET_ENABLED
))
673 *SCSI_CTRL_IDMASK
= idMask
;
685 if (scsiDev
.phase
!= BUS_FREE
)
690 // Acquire the SCSI bus.
691 for (int i
= 0; i
< 100; ++i
)
700 // Error, couldn't acquire scsi bus
704 if (! scsiStatusBSY())
706 // Error, BSY doesn't work.
710 // Should be safe to use the bus now.
717 for (i
= 0; i
< 256; ++i
)
721 if (*SCSI_STS_DBX
!= (i
& 0xff))
725 /*if (Lookup_OddParity[i & 0xff] != SCSI_ReadPin(SCSI_In_DBP))
734 for (i = 0; i < 8; ++i)
736 SCSI_CTL_PHASE_Write(i);
739 if (SCSI_ReadPin(SCSI_In_MSG) != !!(i & __scsiphase_msg))
743 if (SCSI_ReadPin(SCSI_In_CD) != !!(i & __scsiphase_cd))
747 if (SCSI_ReadPin(SCSI_In_IO) != !!(i & __scsiphase_io))
752 SCSI_CTL_PHASE_Write(0);
754 uint32_t signalsOut[] = { SCSI_Out_ATN, SCSI_Out_BSY, SCSI_Out_RST, SCSI_Out_SEL };
755 uint32_t signalsIn[] = { SCSI_Filt_ATN, SCSI_Filt_BSY, SCSI_Filt_RST, SCSI_Filt_SEL };
757 for (i = 0; i < 4; ++i)
759 SCSI_SetPin(signalsOut[i]);
763 for (j = 0; j < 4; ++j)
767 if (! SCSI_ReadFilt(signalsIn[j]))
774 if (SCSI_ReadFilt(signalsIn[j]))
780 SCSI_ClearPin(signalsOut[i]);