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"
32 // Assumes a 96MHz fpga clock.
33 // 2:0 Deskew count, 55ns
34 // 6:4 Hold count, 53ns
35 // 3:0 Assertion count, 80ns
36 #define SCSI_DEFAULT_DESKEW 0x6
37 #define SCSI_DEFAULT_TIMING ((0x5 << 4) | 0x8)
40 // 2:0 Deskew count, 25ns
41 // 6:4 Hold count, 33ns
42 // 3:0 Assertion count, 30ns
43 #define SCSI_FAST10_DESKEW 3
44 #define SCSI_FAST10_TIMING ((0x3 << 4) | 0x3)
47 // 2:0 Deskew count, 12ns
48 // 6:4 Hold count, 17ns
49 // 3:0 Assertion count, 15ns
50 #define SCSI_FAST20_DESKEW 2
51 #define SCSI_FAST20_TIMING ((0x2 << 4) | 0x2)
53 // Private DMA variables.
54 static int dmaInProgress
= 0;
56 static DMA_HandleTypeDef memToFSMC
;
57 static DMA_HandleTypeDef fsmcToMem
;
60 volatile uint8_t scsiRxDMAComplete
;
61 volatile uint8_t scsiTxDMAComplete
;
64 CY_ISR_PROTO(scsiRxCompleteISR
);
65 CY_ISR(scsiRxCompleteISR
)
67 traceIrq(trace_scsiRxCompleteISR
);
68 scsiRxDMAComplete
= 1;
71 CY_ISR_PROTO(scsiTxCompleteISR
);
72 CY_ISR(scsiTxCompleteISR
)
74 traceIrq(trace_scsiTxCompleteISR
);
75 scsiTxDMAComplete
= 1;
79 uint8_t scsiPhyFifoSel
= 0; // global
81 // scsi IRQ handler is initialised by the STM32 HAL. Connected to
83 // Note: naming is important to ensure this function is listed in the
85 void EXTI4_IRQHandler()
87 traceIrq(trace_scsiResetISR
);
89 // Make sure that interrupt flag is set
90 if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_4
) != RESET
) {
92 // Clear interrupt flag
93 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4
);
95 scsiDev
.resetFlag
= scsiDev
.resetFlag
|| scsiStatusRST();
96 // TODO grab SEL status as well
101 static void assertFail()
113 startScsiRx(uint32_t count
)
115 *SCSI_DATA_CNT_HI
= count
>> 8;
116 *SCSI_DATA_CNT_LO
= count
& 0xff;
117 *SCSI_DATA_CNT_SET
= 1;
124 if (!scsiPhyFifoAltEmpty()) {
131 trace(trace_spinPhyRxFifo
);
132 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
)) {}
134 uint8_t val
= scsiPhyRx();
135 // TODO scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
138 if (!scsiPhyFifoEmpty()) {
140 uint8_t k
__attribute((unused
));
141 while (!scsiPhyFifoEmpty()) { k
= scsiPhyRx(); ++j
; }
152 scsiReadPIO(uint8_t* data
, uint32_t count
)
154 for (int i
= 0; i
< count
; ++i
)
156 data
[i
] = scsiPhyRx();
158 // TODO scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
162 scsiReadDMA(uint8_t* data
, uint32_t count
)
164 // Prepare DMA transfer
166 trace(trace_doRxSingleDMA
);
168 scsiTxDMAComplete
= 1; // TODO not used much
169 scsiRxDMAComplete
= 0; // TODO not used much
171 HAL_DMA_Start(&fsmcToMem
, (uint32_t) SCSI_FIFO_DATA
, (uint32_t) data
, count
);
177 int complete
= __HAL_DMA_GET_COUNTER(&fsmcToMem
) == 0;
178 complete
= complete
&& (HAL_DMA_PollForTransfer(&fsmcToMem
, HAL_DMA_FULL_TRANSFER
, 0xffffffff) == HAL_OK
);
181 scsiTxDMAComplete
= 1; // TODO MM FIX IRQ
182 scsiRxDMAComplete
= 1;
186 // TODO MM scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
198 scsiRead(uint8_t* data
, uint32_t count
)
203 uint32_t chunk
= ((count
- i
) > SCSI_FIFO_DEPTH
)
204 ? SCSI_FIFO_DEPTH
: (count
- i
);
208 // DMA is doing 32bit transfers.
209 chunk
= chunk
& 0xFFFFFFF8;
214 while (i
< count
&& likely(!scsiDev
.resetFlag
))
216 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
)) {}
219 uint32_t nextChunk
= ((count
- i
- chunk
) > SCSI_FIFO_DEPTH
)
220 ? SCSI_FIFO_DEPTH
: (count
- i
- chunk
);
224 nextChunk
= nextChunk
& 0xFFFFFFF8;
229 startScsiRx(nextChunk
);
236 scsiReadPIO(data
+ i
, chunk
);
241 scsiReadDMA(data
+ i
, chunk
);
243 trace(trace_spinReadDMAPoll
);
245 while (!scsiReadDMAPoll() && likely(!scsiDev
.resetFlag
))
256 if (!scsiPhyFifoEmpty() || !scsiPhyFifoAltEmpty()) {
258 while (!scsiPhyFifoEmpty()) { scsiPhyRx(); ++j
; }
261 while (!scsiPhyFifoEmpty()) { scsiPhyRx(); ++k
; }
269 scsiWriteByte(uint8_t value
)
272 if (!scsiPhyFifoEmpty()) {
277 trace(trace_spinPhyTxFifo
);
281 trace(trace_spinTxComplete
);
282 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
)) {}
285 if (!scsiPhyFifoAltEmpty()) {
293 scsiWritePIO(const uint8_t* data
, uint32_t count
)
295 for (int i
= 0; i
< count
; ++i
)
302 scsiWriteDMA(const uint8_t* data
, uint32_t count
)
304 // Prepare DMA transfer
306 trace(trace_doTxSingleDMA
);
308 scsiTxDMAComplete
= 0;
309 scsiRxDMAComplete
= 1;
314 (uint32_t) SCSI_FIFO_DATA
,
321 int complete
= __HAL_DMA_GET_COUNTER(&memToFSMC
) == 0;
322 complete
= complete
&& (HAL_DMA_PollForTransfer(&memToFSMC
, HAL_DMA_FULL_TRANSFER
, 0xffffffff) == HAL_OK
);
325 scsiTxDMAComplete
= 1; // TODO MM FIX IRQ
326 scsiRxDMAComplete
= 1;
338 scsiWrite(const uint8_t* data
, uint32_t count
)
341 while (i
< count
&& likely(!scsiDev
.resetFlag
))
343 uint32_t chunk
= ((count
- i
) > SCSI_FIFO_DEPTH
)
344 ? SCSI_FIFO_DEPTH
: (count
- i
);
347 if (!scsiPhyFifoEmpty()) {
357 scsiWritePIO(data
+ i
, chunk
);
362 // DMA is doing 32bit transfers.
363 chunk
= chunk
& 0xFFFFFFF8;
364 scsiWriteDMA(data
+ i
, chunk
);
366 trace(trace_spinReadDMAPoll
);
368 while (!scsiWriteDMAPoll() && likely(!scsiDev
.resetFlag
))
374 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
379 if (!scsiPhyFifoAltEmpty()) {
388 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
393 if (!scsiPhyFifoAltEmpty()) {
400 static inline void busSettleDelay(void)
402 // Data Release time (switching IO) = 400ns
403 // + Bus Settle time (switching phase) = 400ns.
404 s2s_delay_us(1); // Close enough.
407 void scsiEnterBusFree()
409 *SCSI_CTRL_BSY
= 0x00;
410 // We now have a Bus Clear Delay of 800ns to release remaining signals.
411 *SCSI_CTRL_PHASE
= 0;
414 void scsiEnterPhase(int phase
)
416 // ANSI INCITS 362-2002 SPI-3 10.7.1:
417 // Phase changes are not allowed while REQ or ACK is asserted.
418 while (likely(!scsiDev
.resetFlag
) && scsiStatusACK()) {}
420 int newPhase
= phase
> 0 ? phase
: 0;
421 int oldPhase
= *SCSI_CTRL_PHASE
;
423 if (!scsiDev
.resetFlag
&& (!scsiPhyFifoEmpty() || !scsiPhyFifoAltEmpty())) {
427 if (newPhase
!= oldPhase
)
429 if ((newPhase
== DATA_IN
|| newPhase
== DATA_OUT
) &&
430 scsiDev
.target
->syncOffset
)
432 if (scsiDev
.target
->syncPeriod
== 12)
434 // SCSI2 FAST-20 Timing. 20MB/s.
435 *SCSI_CTRL_TIMING
= SCSI_FAST20_DESKEW
;
436 *SCSI_CTRL_TIMING2
= SCSI_FAST20_TIMING
;
438 else if (scsiDev
.target
->syncPeriod
== 25)
440 // SCSI2 FAST Timing. 10MB/s.
441 *SCSI_CTRL_TIMING
= SCSI_FAST10_DESKEW
;
442 *SCSI_CTRL_TIMING2
= SCSI_FAST10_TIMING
;
445 *SCSI_CTRL_TIMING
= SCSI_DEFAULT_DESKEW
;
446 *SCSI_CTRL_TIMING2
= SCSI_DEFAULT_TIMING
;
448 *SCSI_CTRL_SYNC_OFFSET
= scsiDev
.target
->syncOffset
;
450 *SCSI_CTRL_SYNC_OFFSET
= 0;
451 *SCSI_CTRL_TIMING
= SCSI_DEFAULT_TIMING
;
454 *SCSI_CTRL_PHASE
= newPhase
;
457 if (scsiDev
.compatMode
< COMPAT_SCSI2
)
467 trace(trace_scsiPhyReset
);
470 trace(trace_spinDMAReset
);
471 HAL_DMA_Abort(&memToFSMC
);
472 HAL_DMA_Abort(&fsmcToMem
);
477 *SCSI_CTRL_PHASE
= 0x00;
478 *SCSI_CTRL_BSY
= 0x00;
479 s2s_fpgaReset(); // Clears fifos etc.
485 *SCSI_CTRL_SYNC_OFFSET
= 0;
486 *SCSI_CTRL_TIMING
= SCSI_DEFAULT_TIMING
;
488 // DMA Benchmark code
489 // Currently 6.6MB/s. Assume 13MB/s is achievable with 16 bits
495 for (int i
= 0; i
< (100LL * 1024 * 1024 / SCSI_FIFO_DEPTH
); ++i
)
499 (uint32_t) &scsiDev
.data
[0],
500 (uint32_t) SCSI_FIFO_DATA
,
501 SCSI_FIFO_DEPTH
/ 4);
503 HAL_DMA_PollForTransfer(
505 HAL_DMA_FULL_TRANSFER
,
512 for(int i
= 0; i
< 10; ++i
) s2s_delay_ms(1000);
516 // FPGA comms test code
520 for (int j
= 0; j
< SCSI_FIFO_DEPTH
; ++j
)
525 if (!scsiPhyFifoEmpty())
530 *SCSI_CTRL_PHASE
= DATA_IN
;
533 (uint32_t) &scsiDev
.data
[0],
534 (uint32_t) SCSI_FIFO_DATA
,
535 SCSI_FIFO_DEPTH
/ 4);
537 HAL_DMA_PollForTransfer(
539 HAL_DMA_FULL_TRANSFER
,
542 if (!scsiPhyFifoFull())
547 memset(&scsiDev
.data
[0], 0, SCSI_FIFO_DEPTH
);
549 *SCSI_CTRL_PHASE
= DATA_OUT
;
552 (uint32_t) SCSI_FIFO_DATA
,
553 (uint32_t) &scsiDev
.data
[0],
556 HAL_DMA_PollForTransfer(
558 HAL_DMA_FULL_TRANSFER
,
561 if (!scsiPhyFifoEmpty())
567 for (int j
= 0; j
< SCSI_FIFO_DEPTH
; ++j
)
569 if (scsiDev
.data
[j
] != (uint8_t) j
)
582 static void scsiPhyInitDMA()
584 // One-time init only.
585 static uint8_t init
= 0;
590 // Memory to memory transfers can only be done using DMA2
593 // Transmit SCSI data. The source data is treated as the
594 // peripheral (even though this is memory-to-memory)
595 memToFSMC
.Instance
= DMA2_Stream0
;
596 memToFSMC
.Init
.Channel
= DMA_CHANNEL_0
;
597 memToFSMC
.Init
.Direction
= DMA_MEMORY_TO_MEMORY
;
598 memToFSMC
.Init
.PeriphInc
= DMA_PINC_ENABLE
;
599 memToFSMC
.Init
.MemInc
= DMA_MINC_DISABLE
;
600 memToFSMC
.Init
.PeriphDataAlignment
= DMA_PDATAALIGN_WORD
;
601 memToFSMC
.Init
.MemDataAlignment
= DMA_MDATAALIGN_BYTE
;
602 memToFSMC
.Init
.Mode
= DMA_NORMAL
;
603 memToFSMC
.Init
.Priority
= DMA_PRIORITY_LOW
;
604 // FIFO mode is needed to allow conversion from 32bit words to the
605 // 8bit FSMC interface.
606 memToFSMC
.Init
.FIFOMode
= DMA_FIFOMODE_ENABLE
;
608 // We only use 1 word (4 bytes) in the fifo at a time. Normally it's
609 // better to let the DMA fifo fill up then do burst transfers, but
610 // bursting out the FSMC interface will be very slow and may starve
611 // other (faster) transfers. We don't want to risk the SDIO transfers
612 // from overrun/underrun conditions.
613 memToFSMC
.Init
.FIFOThreshold
= DMA_FIFO_THRESHOLD_1QUARTERFULL
;
614 memToFSMC
.Init
.MemBurst
= DMA_MBURST_SINGLE
;
615 memToFSMC
.Init
.PeriphBurst
= DMA_PBURST_SINGLE
;
616 HAL_DMA_Init(&memToFSMC
);
618 // Receive SCSI data. The source data (fsmc) is treated as the
619 // peripheral (even though this is memory-to-memory)
620 fsmcToMem
.Instance
= DMA2_Stream1
;
621 fsmcToMem
.Init
.Channel
= DMA_CHANNEL_0
;
622 fsmcToMem
.Init
.Direction
= DMA_MEMORY_TO_MEMORY
;
623 fsmcToMem
.Init
.PeriphInc
= DMA_PINC_DISABLE
;
624 fsmcToMem
.Init
.MemInc
= DMA_MINC_ENABLE
;
625 fsmcToMem
.Init
.PeriphDataAlignment
= DMA_PDATAALIGN_BYTE
;
626 fsmcToMem
.Init
.MemDataAlignment
= DMA_MDATAALIGN_WORD
;
627 fsmcToMem
.Init
.Mode
= DMA_NORMAL
;
628 fsmcToMem
.Init
.Priority
= DMA_PRIORITY_LOW
;
629 fsmcToMem
.Init
.FIFOMode
= DMA_FIFOMODE_ENABLE
;
630 fsmcToMem
.Init
.FIFOThreshold
= DMA_FIFO_THRESHOLD_1QUARTERFULL
;
631 fsmcToMem
.Init
.MemBurst
= DMA_MBURST_SINGLE
;
632 fsmcToMem
.Init
.PeriphBurst
= DMA_PBURST_SINGLE
;
633 HAL_DMA_Init(&fsmcToMem
);
635 // TODO configure IRQs
644 *SCSI_CTRL_IDMASK
= 0x00; // Reset in scsiPhyConfig
645 *SCSI_CTRL_PHASE
= 0x00;
646 *SCSI_CTRL_BSY
= 0x00;
651 *SCSI_CTRL_SYNC_OFFSET
= 0;
652 *SCSI_CTRL_TIMING
= SCSI_DEFAULT_TIMING
;
658 if (scsiDev
.boardCfg
.flags6
& S2S_CFG_ENABLE_TERMINATOR
)
660 HAL_GPIO_WritePin(nTERM_EN_GPIO_Port
, nTERM_EN_Pin
, GPIO_PIN_RESET
);
664 HAL_GPIO_WritePin(nTERM_EN_GPIO_Port
, nTERM_EN_Pin
, GPIO_PIN_SET
);
669 for (int i
= 0; i
< 8; ++i
)
671 const S2S_TargetCfg
* cfg
= s2s_getConfigById(i
);
672 if (cfg
&& (cfg
->scsiId
& S2S_CFG_TARGET_ENABLED
))
677 *SCSI_CTRL_IDMASK
= idMask
;
689 if (scsiDev
.phase
!= BUS_FREE
)
694 // Acquire the SCSI bus.
695 for (int i
= 0; i
< 100; ++i
)
704 // Error, couldn't acquire scsi bus
708 if (! scsiStatusBSY())
710 // Error, BSY doesn't work.
714 // Should be safe to use the bus now.
721 for (i
= 0; i
< 256; ++i
)
725 if (*SCSI_STS_DBX
!= (i
& 0xff))
729 /*if (Lookup_OddParity[i & 0xff] != SCSI_ReadPin(SCSI_In_DBP))
738 for (i = 0; i < 8; ++i)
740 SCSI_CTL_PHASE_Write(i);
743 if (SCSI_ReadPin(SCSI_In_MSG) != !!(i & __scsiphase_msg))
747 if (SCSI_ReadPin(SCSI_In_CD) != !!(i & __scsiphase_cd))
751 if (SCSI_ReadPin(SCSI_In_IO) != !!(i & __scsiphase_io))
756 SCSI_CTL_PHASE_Write(0);
758 uint32_t signalsOut[] = { SCSI_Out_ATN, SCSI_Out_BSY, SCSI_Out_RST, SCSI_Out_SEL };
759 uint32_t signalsIn[] = { SCSI_Filt_ATN, SCSI_Filt_BSY, SCSI_Filt_RST, SCSI_Filt_SEL };
761 for (i = 0; i < 4; ++i)
763 SCSI_SetPin(signalsOut[i]);
767 for (j = 0; j < 4; ++j)
771 if (! SCSI_ReadFilt(signalsIn[j]))
778 if (SCSI_ReadFilt(signalsIn[j]))
784 SCSI_ClearPin(signalsOut[i]);