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 // Private DMA variables.
32 static int dmaInProgress
= 0;
34 static DMA_HandleTypeDef memToFSMC
;
35 static DMA_HandleTypeDef fsmcToMem
;
38 volatile uint8_t scsiRxDMAComplete
;
39 volatile uint8_t scsiTxDMAComplete
;
42 CY_ISR_PROTO(scsiRxCompleteISR
);
43 CY_ISR(scsiRxCompleteISR
)
45 traceIrq(trace_scsiRxCompleteISR
);
46 scsiRxDMAComplete
= 1;
49 CY_ISR_PROTO(scsiTxCompleteISR
);
50 CY_ISR(scsiTxCompleteISR
)
52 traceIrq(trace_scsiTxCompleteISR
);
53 scsiTxDMAComplete
= 1;
57 uint8_t scsiPhyFifoSel
= 0; // global
59 // scsi IRQ handler is initialised by the STM32 HAL. Connected to
61 // Note: naming is important to ensure this function is listed in the
63 void EXTI4_IRQHandler()
65 traceIrq(trace_scsiResetISR
);
67 // Make sure that interrupt flag is set
68 if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_4
) != RESET
) {
70 // Clear interrupt flag
71 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4
);
73 scsiDev
.resetFlag
= scsiDev
.resetFlag
|| scsiStatusRST();
74 // TODO grab SEL status as well
79 static void assertFail()
91 startScsiRx(uint32_t count
)
93 *SCSI_DATA_CNT_HI
= count
>> 8;
94 *SCSI_DATA_CNT_LO
= count
& 0xff;
95 *SCSI_DATA_CNT_SET
= 1;
102 if (!scsiPhyFifoAltEmpty()) {
109 trace(trace_spinPhyRxFifo
);
110 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
)) {}
112 uint8_t val
= scsiPhyRx();
113 // TODO scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
116 if (!scsiPhyFifoEmpty()) {
118 uint8_t k
__attribute((unused
));
119 while (!scsiPhyFifoEmpty()) { k
= scsiPhyRx(); ++j
; }
130 scsiReadPIO(uint8_t* data
, uint32_t count
)
132 for (int i
= 0; i
< count
; ++i
)
134 data
[i
] = scsiPhyRx();
136 // TODO scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
140 scsiReadDMA(uint8_t* data
, uint32_t count
)
142 // Prepare DMA transfer
144 trace(trace_doRxSingleDMA
);
146 scsiTxDMAComplete
= 1; // TODO not used much
147 scsiRxDMAComplete
= 0; // TODO not used much
149 HAL_DMA_Start(&fsmcToMem
, (uint32_t) SCSI_FIFO_DATA
, (uint32_t) data
, count
);
155 int complete
= __HAL_DMA_GET_COUNTER(&fsmcToMem
) == 0;
156 complete
= complete
&& (HAL_DMA_PollForTransfer(&fsmcToMem
, HAL_DMA_FULL_TRANSFER
, 0xffffffff) == HAL_OK
);
159 scsiTxDMAComplete
= 1; // TODO MM FIX IRQ
160 scsiRxDMAComplete
= 1;
164 // TODO MM scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
176 scsiRead(uint8_t* data
, uint32_t count
)
181 uint32_t chunk
= ((count
- i
) > SCSI_FIFO_DEPTH
)
182 ? SCSI_FIFO_DEPTH
: (count
- i
);
185 // DMA is doing 32bit transfers.
186 chunk
= chunk
& 0xFFFFFFF8;
190 while (i
< count
&& likely(!scsiDev
.resetFlag
))
192 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
)) {}
195 uint32_t nextChunk
= ((count
- i
- chunk
) > SCSI_FIFO_DEPTH
)
196 ? SCSI_FIFO_DEPTH
: (count
- i
- chunk
);
199 nextChunk
= nextChunk
& 0xFFFFFFF8;
203 startScsiRx(nextChunk
);
208 scsiReadPIO(data
+ i
, chunk
);
212 scsiReadDMA(data
+ i
, chunk
);
214 trace(trace_spinReadDMAPoll
);
216 while (!scsiReadDMAPoll() && likely(!scsiDev
.resetFlag
))
222 if (!scsiPhyFifoEmpty()) {
224 while (!scsiPhyFifoEmpty()) { scsiPhyRx(); ++j
; }
235 scsiWriteByte(uint8_t value
)
238 if (!scsiPhyFifoEmpty()) {
243 trace(trace_spinPhyTxFifo
);
247 trace(trace_spinTxComplete
);
248 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
)) {}
251 if (!scsiPhyFifoAltEmpty()) {
259 scsiWritePIO(const uint8_t* data
, uint32_t count
)
261 for (int i
= 0; i
< count
; ++i
)
268 scsiWriteDMA(const uint8_t* data
, uint32_t count
)
270 // Prepare DMA transfer
272 trace(trace_doTxSingleDMA
);
274 scsiTxDMAComplete
= 0;
275 scsiRxDMAComplete
= 1;
280 (uint32_t) SCSI_FIFO_DATA
,
287 int complete
= __HAL_DMA_GET_COUNTER(&memToFSMC
) == 0;
288 complete
= complete
&& (HAL_DMA_PollForTransfer(&memToFSMC
, HAL_DMA_FULL_TRANSFER
, 0xffffffff) == HAL_OK
);
291 scsiTxDMAComplete
= 1; // TODO MM FIX IRQ
292 scsiRxDMAComplete
= 1;
304 scsiWrite(const uint8_t* data
, uint32_t count
)
307 while (i
< count
&& likely(!scsiDev
.resetFlag
))
309 uint32_t chunk
= ((count
- i
) > SCSI_FIFO_DEPTH
)
310 ? SCSI_FIFO_DEPTH
: (count
- i
);
313 if (!scsiPhyFifoEmpty()) {
321 scsiWritePIO(data
+ i
, chunk
);
325 // DMA is doing 32bit transfers.
326 chunk
= chunk
& 0xFFFFFFF8;
327 scsiWriteDMA(data
+ i
, chunk
);
329 trace(trace_spinReadDMAPoll
);
331 while (!scsiWriteDMAPoll() && likely(!scsiDev
.resetFlag
))
336 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
341 if (!scsiPhyFifoAltEmpty()) {
350 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
355 if (!scsiPhyFifoAltEmpty()) {
362 static inline void busSettleDelay(void)
364 // Data Release time (switching IO) = 400ns
365 // + Bus Settle time (switching phase) = 400ns.
366 s2s_delay_us(1); // Close enough.
369 void scsiEnterBusFree()
371 *SCSI_CTRL_BSY
= 0x00;
372 // We now have a Bus Clear Delay of 800ns to release remaining signals.
373 *SCSI_CTRL_PHASE
= 0;
376 void scsiEnterPhase(int phase
)
378 // ANSI INCITS 362-2002 SPI-3 10.7.1:
379 // Phase changes are not allowed while REQ or ACK is asserted.
380 while (likely(!scsiDev
.resetFlag
) && scsiStatusACK()) {}
382 int newPhase
= phase
> 0 ? phase
: 0;
383 int oldPhase
= *SCSI_CTRL_PHASE
;
385 if (!scsiPhyFifoEmpty() || !scsiPhyFifoAltEmpty()) {
389 if (newPhase
!= oldPhase
)
391 *SCSI_CTRL_PHASE
= newPhase
;
394 if (scsiDev
.compatMode
< COMPAT_SCSI2
)
404 trace(trace_scsiPhyReset
);
407 trace(trace_spinDMAReset
);
408 HAL_DMA_Abort(&memToFSMC
);
409 HAL_DMA_Abort(&fsmcToMem
);
415 // Set the Clear bits for both SCSI device FIFOs
416 scsiTarget_AUX_CTL
= scsiTarget_AUX_CTL
| 0x03;
418 // Trigger RST outselves. It is connected to the datapath and will
419 // ensure it returns to the idle state. The datapath runs at the BUS clk
420 // speed (ie. same as the CPU), so we can be sure it is active for a sufficient
422 SCSI_RST_ISR_Disable();
423 SCSI_SetPin(SCSI_Out_RST
);
425 SCSI_CTL_PHASE_Write(0);
426 SCSI_ClearPin(SCSI_Out_ATN
);
427 SCSI_ClearPin(SCSI_Out_BSY
);
428 SCSI_ClearPin(SCSI_Out_ACK
);
429 SCSI_ClearPin(SCSI_Out_RST
);
430 SCSI_ClearPin(SCSI_Out_SEL
);
431 SCSI_ClearPin(SCSI_Out_REQ
);
433 // Allow the FIFOs to fill up again.
434 SCSI_ClearPin(SCSI_Out_RST
);
435 SCSI_RST_ISR_Enable();
436 scsiTarget_AUX_CTL
= scsiTarget_AUX_CTL
& ~(0x03);
438 SCSI_Parity_Error_Read(); // clear sticky bits
441 *SCSI_CTRL_PHASE
= 0x00;
442 *SCSI_CTRL_BSY
= 0x00;
443 s2s_fpgaReset(); // Clears fifos etc.
449 // DMA Benchmark code
450 // Currently 10MB/s. Assume 20MB/s is achievable with 16 bits.
456 for (int i
= 0; i
< (100LL * 1024 * 1024 / SCSI_FIFO_DEPTH
); ++i
)
460 (uint32_t) &scsiDev
.data
[0],
461 (uint32_t) SCSI_FIFO_DATA
,
462 SCSI_FIFO_DEPTH
/ 4);
464 HAL_DMA_PollForTransfer(
466 HAL_DMA_FULL_TRANSFER
,
473 for(int i
= 0; i
< 10; ++i
) s2s_delay_ms(1000);
477 // FPGA comms test code
481 for (int j
= 0; j
< SCSI_FIFO_DEPTH
; ++j
)
486 if (!scsiPhyFifoEmpty())
491 *SCSI_CTRL_PHASE
= DATA_IN
;
494 (uint32_t) &scsiDev
.data
[0],
495 (uint32_t) SCSI_FIFO_DATA
,
496 SCSI_FIFO_DEPTH
/ 4);
498 HAL_DMA_PollForTransfer(
500 HAL_DMA_FULL_TRANSFER
,
503 if (!scsiPhyFifoFull())
508 memset(&scsiDev
.data
[0], 0, SCSI_FIFO_DEPTH
);
510 *SCSI_CTRL_PHASE
= DATA_OUT
;
513 (uint32_t) SCSI_FIFO_DATA
,
514 (uint32_t) &scsiDev
.data
[0],
517 HAL_DMA_PollForTransfer(
519 HAL_DMA_FULL_TRANSFER
,
522 if (!scsiPhyFifoEmpty())
528 for (int j
= 0; j
< SCSI_FIFO_DEPTH
; ++j
)
530 if (scsiDev
.data
[j
] != (uint8_t) j
)
543 static void scsiPhyInitDMA()
545 // One-time init only.
546 static uint8_t init
= 0;
551 // Memory to memory transfers can only be done using DMA2
554 // Transmit SCSI data. The source data is treated as the
555 // peripheral (even though this is memory-to-memory)
556 memToFSMC
.Instance
= DMA2_Stream0
;
557 memToFSMC
.Init
.Channel
= DMA_CHANNEL_0
;
558 memToFSMC
.Init
.Direction
= DMA_MEMORY_TO_MEMORY
;
559 memToFSMC
.Init
.PeriphInc
= DMA_PINC_ENABLE
;
560 memToFSMC
.Init
.MemInc
= DMA_MINC_DISABLE
;
561 memToFSMC
.Init
.PeriphDataAlignment
= DMA_PDATAALIGN_WORD
;
562 memToFSMC
.Init
.MemDataAlignment
= DMA_MDATAALIGN_BYTE
;
563 memToFSMC
.Init
.Mode
= DMA_NORMAL
;
564 memToFSMC
.Init
.Priority
= DMA_PRIORITY_LOW
;
565 // FIFO mode is needed to allow conversion from 32bit words to the
566 // 8bit FSMC interface.
567 memToFSMC
.Init
.FIFOMode
= DMA_FIFOMODE_ENABLE
;
569 // We only use 1 word (4 bytes) in the fifo at a time. Normally it's
570 // better to let the DMA fifo fill up then do burst transfers, but
571 // bursting out the FSMC interface will be very slow and may starve
572 // other (faster) transfers. We don't want to risk the SDIO transfers
573 // from overrun/underrun conditions.
574 memToFSMC
.Init
.FIFOThreshold
= DMA_FIFO_THRESHOLD_1QUARTERFULL
;
575 memToFSMC
.Init
.MemBurst
= DMA_MBURST_SINGLE
;
576 memToFSMC
.Init
.PeriphBurst
= DMA_PBURST_SINGLE
;
577 HAL_DMA_Init(&memToFSMC
);
579 // Receive SCSI data. The source data (fsmc) is treated as the
580 // peripheral (even though this is memory-to-memory)
581 fsmcToMem
.Instance
= DMA2_Stream1
;
582 fsmcToMem
.Init
.Channel
= DMA_CHANNEL_0
;
583 fsmcToMem
.Init
.Direction
= DMA_MEMORY_TO_MEMORY
;
584 fsmcToMem
.Init
.PeriphInc
= DMA_PINC_DISABLE
;
585 fsmcToMem
.Init
.MemInc
= DMA_MINC_ENABLE
;
586 fsmcToMem
.Init
.PeriphDataAlignment
= DMA_PDATAALIGN_BYTE
;
587 fsmcToMem
.Init
.MemDataAlignment
= DMA_MDATAALIGN_WORD
;
588 fsmcToMem
.Init
.Mode
= DMA_NORMAL
;
589 fsmcToMem
.Init
.Priority
= DMA_PRIORITY_LOW
;
590 fsmcToMem
.Init
.FIFOMode
= DMA_FIFOMODE_ENABLE
;
591 fsmcToMem
.Init
.FIFOThreshold
= DMA_FIFO_THRESHOLD_1QUARTERFULL
;
592 fsmcToMem
.Init
.MemBurst
= DMA_MBURST_SINGLE
;
593 fsmcToMem
.Init
.PeriphBurst
= DMA_PBURST_SINGLE
;
594 HAL_DMA_Init(&fsmcToMem
);
596 // TODO configure IRQs
605 *SCSI_CTRL_IDMASK
= 0x00; // Reset in scsiPhyConfig
606 *SCSI_CTRL_PHASE
= 0x00;
607 *SCSI_CTRL_BSY
= 0x00;
616 if (scsiDev
.boardCfg
.flags6
& S2S_CFG_ENABLE_TERMINATOR
)
618 HAL_GPIO_WritePin(nTERM_EN_GPIO_Port
, nTERM_EN_Pin
, GPIO_PIN_RESET
);
622 HAL_GPIO_WritePin(nTERM_EN_GPIO_Port
, nTERM_EN_Pin
, GPIO_PIN_SET
);
627 for (int i
= 0; i
< 8; ++i
)
629 const S2S_TargetCfg
* cfg
= s2s_getConfigById(i
);
630 if (cfg
&& (cfg
->scsiId
& S2S_CFG_TARGET_ENABLED
))
635 *SCSI_CTRL_IDMASK
= idMask
;
647 if (scsiDev
.phase
!= BUS_FREE
)
652 // Acquire the SCSI bus.
653 for (int i
= 0; i
< 100; ++i
)
662 // Error, couldn't acquire scsi bus
666 if (! scsiStatusBSY())
668 // Error, BSY doesn't work.
672 // Should be safe to use the bus now.
679 for (i
= 0; i
< 256; ++i
)
683 if (*SCSI_STS_DBX
!= (i
& 0xff))
687 /*if (Lookup_OddParity[i & 0xff] != SCSI_ReadPin(SCSI_In_DBP))
696 for (i = 0; i < 8; ++i)
698 SCSI_CTL_PHASE_Write(i);
701 if (SCSI_ReadPin(SCSI_In_MSG) != !!(i & __scsiphase_msg))
705 if (SCSI_ReadPin(SCSI_In_CD) != !!(i & __scsiphase_cd))
709 if (SCSI_ReadPin(SCSI_In_IO) != !!(i & __scsiphase_io))
714 SCSI_CTL_PHASE_Write(0);
716 uint32_t signalsOut[] = { SCSI_Out_ATN, SCSI_Out_BSY, SCSI_Out_RST, SCSI_Out_SEL };
717 uint32_t signalsIn[] = { SCSI_Filt_ATN, SCSI_Filt_BSY, SCSI_Filt_RST, SCSI_Filt_SEL };
719 for (i = 0; i < 4; ++i)
721 SCSI_SetPin(signalsOut[i]);
725 for (j = 0; j < 4; ++j)
729 if (! SCSI_ReadFilt(signalsIn[j]))
736 if (SCSI_ReadFilt(signalsIn[j]))
742 SCSI_ClearPin(signalsOut[i]);