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 // Time until we consider ourselves selected
33 #define SCSI_DEFAULT_SELECTION 43
34 #define SCSI_FAST_SELECTION 5
37 // Assumes a 108MHz fpga clock.
38 // 2:0 Deskew count, 55ns
39 // 6:4 Hold count, 53ns
40 // 3:0 Assertion count, 80ns
41 #define SCSI_DEFAULT_DESKEW 0x6
42 #define SCSI_DEFAULT_TIMING ((0x6 << 4) | 0x9)
44 // 3.125MB/s (80 period) to < 10MB/s sync
45 // Assumes a 108MHz fpga clock. (9 ns)
46 // (((period * 4) / 2) * 0.8) / 9
47 // Done using 3 fixed point math.
48 // 2:0 Deskew count, 55ns normal, or 25ns if faster than 5.5MB/s
49 // 6:4 Hold count, 53ns normal, or 33ns if faster than 5.5MB/s
50 // 3:0 Assertion count, variable
51 #define SCSI_SYNC_DESKEW(period) (period < 45 ? SCSI_FAST10_DESKEW : SCSI_DEFAULT_DESKEW)
52 #define SCSI_SYNC_TIMING(period) (((period < 45 ? 0x4 : 0x6) << 4) | ((((((int)period) * 177) + 750)/1000) & 0xF))
55 // 2:0 Deskew count, 25ns
56 // 6:4 Hold count, 33ns
57 // 3:0 Assertion count, 30ns
58 // We want deskew + hold + assert + 3 to add up to 11 clocks
59 // the fpga code has 1 clock of overhead when transitioning from deskew to
61 #define SCSI_FAST10_DESKEW 2
62 #define SCSI_FAST10_TIMING ((0x3 << 4) | 0x3)
65 // 2:0 Deskew count, 12ns
66 // 6:4 Hold count, 17ns
67 // 3:0 Assertion count, 15ns
68 #define SCSI_FAST20_DESKEW 1
69 #define SCSI_FAST20_TIMING ((0x2 << 4) | 0x2)
71 // Private DMA variables.
72 static int dmaInProgress
= 0;
74 static DMA_HandleTypeDef memToFSMC
;
75 static DMA_HandleTypeDef fsmcToMem
;
78 volatile uint8_t scsiRxDMAComplete
;
79 volatile uint8_t scsiTxDMAComplete
;
81 uint8_t scsiPhyFifoSel
= 0; // global
83 // scsi IRQ handler is initialised by the STM32 HAL. Connected to
85 // Note: naming is important to ensure this function is listed in the
87 void EXTI4_IRQHandler()
89 traceIrq(trace_scsiResetISR
);
91 // Make sure that interrupt flag is set
92 if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_4
) != RESET
) {
94 // Clear interrupt flag
95 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4
);
97 scsiDev
.resetFlag
= scsiDev
.resetFlag
|| scsiStatusRST();
99 // selFlag is required for Philips P2000C which releases it after 600ns
100 // without waiting for BSY.
101 // Also required for some early Mac Plus roms
102 scsiDev
.selFlag
= *SCSI_STS_SELECTED
;
105 __SEV(); // Set event. See corresponding __WFE() calls.
108 static void assertFail()
120 scsiSetDataCount(uint32_t count
)
122 *SCSI_DATA_CNT_HI
= count
>> 8;
123 *SCSI_DATA_CNT_LO
= count
& 0xff;
124 *SCSI_DATA_CNT_SET
= 1;
131 if (!scsiPhyFifoAltEmpty()) {
138 trace(trace_spinPhyRxFifo
);
139 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
141 __WFE(); // Wait for event
144 uint8_t val
= scsiPhyRx();
145 // TODO scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
148 if (!scsiPhyFifoEmpty()) {
150 uint8_t k
__attribute((unused
));
151 while (!scsiPhyFifoEmpty()) { k
= scsiPhyRx(); ++j
; }
162 scsiReadPIO(uint8_t* data
, uint32_t count
)
164 uint16_t* fifoData
= (uint16_t*)data
;
166 for (int i
= 0; i
< (count
+ 1) / 2; ++i
)
168 fifoData
[i
] = scsiPhyRx(); // TODO ASSUMES LITTLE ENDIAN
173 scsiReadDMA(uint8_t* data
, uint32_t count
)
175 // Prepare DMA transfer
177 trace(trace_doRxSingleDMA
);
179 scsiTxDMAComplete
= 1; // TODO not used much
180 scsiRxDMAComplete
= 0; // TODO not used much
184 (uint32_t) SCSI_FIFO_DATA
,
192 int complete
= __HAL_DMA_GET_COUNTER(&fsmcToMem
) == 0;
193 complete
= complete
&& (HAL_DMA_PollForTransfer(&fsmcToMem
, HAL_DMA_FULL_TRANSFER
, 0xffffffff) == HAL_OK
);
196 scsiTxDMAComplete
= 1; // TODO MM FIX IRQ
197 scsiRxDMAComplete
= 1;
201 // TODO MM scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
213 scsiRead(uint8_t* data
, uint32_t count
, int* parityError
)
219 uint32_t chunk
= ((count
- i
) > SCSI_FIFO_DEPTH
)
220 ? SCSI_FIFO_DEPTH
: (count
- i
);
224 // DMA is doing 32bit transfers.
225 chunk
= chunk
& 0xFFFFFFF8;
228 scsiSetDataCount(chunk
);
230 while (i
< count
&& likely(!scsiDev
.resetFlag
))
232 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
234 __WFE(); // Wait for event
236 *parityError
|= scsiParityError();
239 uint32_t nextChunk
= ((count
- i
- chunk
) > SCSI_FIFO_DEPTH
)
240 ? SCSI_FIFO_DEPTH
: (count
- i
- chunk
);
244 nextChunk
= nextChunk
& 0xFFFFFFF8;
249 scsiSetDataCount(nextChunk
);
256 scsiReadPIO(data
+ i
, chunk
);
261 scsiReadDMA(data
+ i
, chunk
);
263 trace(trace_spinReadDMAPoll
);
265 while (!scsiReadDMAPoll() && likely(!scsiDev
.resetFlag
))
276 if (!scsiPhyFifoEmpty() || !scsiPhyFifoAltEmpty()) {
278 while (!scsiPhyFifoEmpty()) { scsiPhyRx(); ++j
; }
281 while (!scsiPhyFifoEmpty()) { scsiPhyRx(); ++k
; }
289 scsiWriteByte(uint8_t value
)
292 if (!scsiPhyFifoEmpty()) {
297 trace(trace_spinPhyTxFifo
);
303 trace(trace_spinTxComplete
);
304 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
306 __WFE(); // Wait for event
310 if (!scsiPhyFifoAltEmpty()) {
318 scsiWritePIO(const uint8_t* data
, uint32_t count
)
320 uint16_t* fifoData
= (uint16_t*)data
;
321 for (int i
= 0; i
< (count
+ 1) / 2; ++i
)
323 scsiPhyTx(fifoData
[i
]);
328 scsiWriteDMA(const uint8_t* data
, uint32_t count
)
330 // Prepare DMA transfer
332 trace(trace_doTxSingleDMA
);
334 scsiTxDMAComplete
= 0;
335 scsiRxDMAComplete
= 1;
340 (uint32_t) SCSI_FIFO_DATA
,
347 int complete
= __HAL_DMA_GET_COUNTER(&memToFSMC
) == 0;
348 complete
= complete
&& (HAL_DMA_PollForTransfer(&memToFSMC
, HAL_DMA_FULL_TRANSFER
, 0xffffffff) == HAL_OK
);
351 scsiTxDMAComplete
= 1; // TODO MM FIX IRQ
352 scsiRxDMAComplete
= 1;
364 scsiWrite(const uint8_t* data
, uint32_t count
)
367 while (i
< count
&& likely(!scsiDev
.resetFlag
))
369 uint32_t chunk
= ((count
- i
) > SCSI_FIFO_DEPTH
)
370 ? SCSI_FIFO_DEPTH
: (count
- i
);
373 if (!scsiPhyFifoEmpty()) {
383 scsiWritePIO(data
+ i
, chunk
);
388 // DMA is doing 32bit transfers.
389 chunk
= chunk
& 0xFFFFFFF8;
390 scsiWriteDMA(data
+ i
, chunk
);
392 trace(trace_spinReadDMAPoll
);
394 while (!scsiWriteDMAPoll() && likely(!scsiDev
.resetFlag
))
400 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
402 __WFE(); // Wait for event
406 if (!scsiPhyFifoAltEmpty()) {
413 scsiSetDataCount(chunk
);
416 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
418 __WFE(); // Wait for event
422 if (!scsiPhyFifoAltEmpty()) {
429 static inline void busSettleDelay(void)
431 // Data Release time (switching IO) = 400ns
432 // + Bus Settle time (switching phase) = 400ns.
433 s2s_delay_us(1); // Close enough.
436 void scsiEnterBusFree()
438 *SCSI_CTRL_BSY
= 0x00;
439 // We now have a Bus Clear Delay of 800ns to release remaining signals.
440 *SCSI_CTRL_PHASE
= 0;
443 void scsiEnterPhase(int phase
)
445 // ANSI INCITS 362-2002 SPI-3 10.7.1:
446 // Phase changes are not allowed while REQ or ACK is asserted.
447 while (likely(!scsiDev
.resetFlag
) && scsiStatusACK()) {}
449 int newPhase
= phase
> 0 ? phase
: 0;
450 int oldPhase
= *SCSI_CTRL_PHASE
;
452 if (!scsiDev
.resetFlag
&& (!scsiPhyFifoEmpty() || !scsiPhyFifoAltEmpty())) {
456 if (newPhase
!= oldPhase
)
458 if ((newPhase
== DATA_IN
|| newPhase
== DATA_OUT
) &&
459 scsiDev
.target
->syncOffset
)
461 if (scsiDev
.target
->syncPeriod
== 12)
463 // SCSI2 FAST-20 Timing. 20MB/s.
464 *SCSI_CTRL_DESKEW
= SCSI_FAST20_DESKEW
;
465 *SCSI_CTRL_TIMING
= SCSI_FAST20_TIMING
;
467 else if (scsiDev
.target
->syncPeriod
== 25)
469 // SCSI2 FAST Timing. 10MB/s.
470 *SCSI_CTRL_DESKEW
= SCSI_FAST10_DESKEW
;
471 *SCSI_CTRL_TIMING
= SCSI_FAST10_TIMING
;
475 *SCSI_CTRL_DESKEW
= SCSI_SYNC_DESKEW(scsiDev
.target
->syncPeriod
);
476 *SCSI_CTRL_TIMING
= SCSI_SYNC_TIMING(scsiDev
.target
->syncPeriod
);
479 *SCSI_CTRL_SYNC_OFFSET
= scsiDev
.target
->syncOffset
;
481 *SCSI_CTRL_SYNC_OFFSET
= 0;
484 *SCSI_CTRL_DESKEW
= SCSI_DEFAULT_DESKEW
;
485 *SCSI_CTRL_TIMING
= SCSI_DEFAULT_TIMING
;
488 *SCSI_CTRL_PHASE
= newPhase
;
491 if (scsiDev
.compatMode
< COMPAT_SCSI2
)
501 trace(trace_scsiPhyReset
);
504 trace(trace_spinDMAReset
);
505 HAL_DMA_Abort(&memToFSMC
);
506 HAL_DMA_Abort(&fsmcToMem
);
511 *SCSI_CTRL_PHASE
= 0x00;
512 *SCSI_CTRL_BSY
= 0x00;
513 s2s_fpgaReset(); // Clears fifos etc.
519 *SCSI_CTRL_SYNC_OFFSET
= 0;
520 *SCSI_CTRL_DESKEW
= SCSI_DEFAULT_DESKEW
;
521 *SCSI_CTRL_TIMING
= SCSI_DEFAULT_TIMING
;
523 // DMA Benchmark code
530 for (int i
= 0; i
< (100LL * 1024 * 1024 / SCSI_FIFO_DEPTH
); ++i
)
534 (uint32_t) &scsiDev
.data
[0],
535 (uint32_t) SCSI_FIFO_DATA
,
536 SCSI_FIFO_DEPTH
/ 4);
538 HAL_DMA_PollForTransfer(
540 HAL_DMA_FULL_TRANSFER
,
547 for(int i
= 0; i
< 10; ++i
) s2s_delay_ms(1000);
551 // FPGA comms test code
555 for (int j
= 0; j
< SCSI_FIFO_DEPTH
; ++j
)
560 if (!scsiPhyFifoEmpty())
565 *SCSI_CTRL_PHASE
= DATA_IN
;
568 (uint32_t) &scsiDev
.data
[0],
569 (uint32_t) SCSI_FIFO_DATA
,
570 SCSI_FIFO_DEPTH
/ 4);
572 HAL_DMA_PollForTransfer(
574 HAL_DMA_FULL_TRANSFER
,
577 if (!scsiPhyFifoFull())
582 memset(&scsiDev
.data
[0], 0, SCSI_FIFO_DEPTH
);
584 *SCSI_CTRL_PHASE
= DATA_OUT
;
587 (uint32_t) SCSI_FIFO_DATA
,
588 (uint32_t) &scsiDev
.data
[0],
589 SCSI_FIFO_DEPTH
/ 2);
591 HAL_DMA_PollForTransfer(
593 HAL_DMA_FULL_TRANSFER
,
596 if (!scsiPhyFifoEmpty())
602 for (int j
= 0; j
< SCSI_FIFO_DEPTH
; ++j
)
604 if (scsiDev
.data
[j
] != (uint8_t) j
)
615 #ifdef SCSI_FREQ_TEST
618 *SCSI_CTRL_DBX
= 0xAA;
619 *SCSI_CTRL_DBX
= 0x55;
625 static void scsiPhyInitDMA()
627 // One-time init only.
628 static uint8_t init
= 0;
633 // Memory to memory transfers can only be done using DMA2
636 // Transmit SCSI data. The source data is treated as the
637 // peripheral (even though this is memory-to-memory)
638 memToFSMC
.Instance
= DMA2_Stream0
;
639 memToFSMC
.Init
.Channel
= DMA_CHANNEL_0
;
640 memToFSMC
.Init
.Direction
= DMA_MEMORY_TO_MEMORY
;
641 memToFSMC
.Init
.PeriphInc
= DMA_PINC_ENABLE
;
642 memToFSMC
.Init
.MemInc
= DMA_MINC_DISABLE
;
643 memToFSMC
.Init
.PeriphDataAlignment
= DMA_PDATAALIGN_WORD
;
644 memToFSMC
.Init
.MemDataAlignment
= DMA_MDATAALIGN_HALFWORD
;
645 memToFSMC
.Init
.Mode
= DMA_NORMAL
;
646 memToFSMC
.Init
.Priority
= DMA_PRIORITY_LOW
;
647 // FIFO mode is needed to allow conversion from 32bit words to the
648 // 16bit FSMC interface.
649 memToFSMC
.Init
.FIFOMode
= DMA_FIFOMODE_ENABLE
;
651 // We only use 1 word (4 bytes) in the fifo at a time. Normally it's
652 // better to let the DMA fifo fill up then do burst transfers, but
653 // bursting out the FSMC interface will be very slow and may starve
654 // other (faster) transfers. We don't want to risk the SDIO transfers
655 // from overrun/underrun conditions.
656 memToFSMC
.Init
.FIFOThreshold
= DMA_FIFO_THRESHOLD_1QUARTERFULL
;
657 memToFSMC
.Init
.MemBurst
= DMA_MBURST_SINGLE
;
658 memToFSMC
.Init
.PeriphBurst
= DMA_PBURST_SINGLE
;
659 HAL_DMA_Init(&memToFSMC
);
661 // Receive SCSI data. The source data (fsmc) is treated as the
662 // peripheral (even though this is memory-to-memory)
663 fsmcToMem
.Instance
= DMA2_Stream1
;
664 fsmcToMem
.Init
.Channel
= DMA_CHANNEL_0
;
665 fsmcToMem
.Init
.Direction
= DMA_MEMORY_TO_MEMORY
;
666 fsmcToMem
.Init
.PeriphInc
= DMA_PINC_DISABLE
;
667 fsmcToMem
.Init
.MemInc
= DMA_MINC_ENABLE
;
668 fsmcToMem
.Init
.PeriphDataAlignment
= DMA_PDATAALIGN_HALFWORD
;
669 fsmcToMem
.Init
.MemDataAlignment
= DMA_MDATAALIGN_WORD
;
670 fsmcToMem
.Init
.Mode
= DMA_NORMAL
;
671 fsmcToMem
.Init
.Priority
= DMA_PRIORITY_LOW
;
672 fsmcToMem
.Init
.FIFOMode
= DMA_FIFOMODE_ENABLE
;
673 fsmcToMem
.Init
.FIFOThreshold
= DMA_FIFO_THRESHOLD_1QUARTERFULL
;
674 fsmcToMem
.Init
.MemBurst
= DMA_MBURST_SINGLE
;
675 fsmcToMem
.Init
.PeriphBurst
= DMA_PBURST_SINGLE
;
676 HAL_DMA_Init(&fsmcToMem
);
678 // TODO configure IRQs
687 *SCSI_CTRL_IDMASK
= 0x00; // Reset in scsiPhyConfig
688 *SCSI_CTRL_PHASE
= 0x00;
689 *SCSI_CTRL_BSY
= 0x00;
694 *SCSI_CTRL_SYNC_OFFSET
= 0;
695 *SCSI_CTRL_DESKEW
= SCSI_DEFAULT_DESKEW
;
696 *SCSI_CTRL_TIMING
= SCSI_DEFAULT_TIMING
;
698 *SCSI_CTRL_SEL_TIMING
= SCSI_DEFAULT_SELECTION
;
704 if (scsiDev
.boardCfg
.flags6
& S2S_CFG_ENABLE_TERMINATOR
)
706 HAL_GPIO_WritePin(nTERM_EN_GPIO_Port
, nTERM_EN_Pin
, GPIO_PIN_RESET
);
710 HAL_GPIO_WritePin(nTERM_EN_GPIO_Port
, nTERM_EN_Pin
, GPIO_PIN_SET
);
715 for (int i
= 0; i
< 8; ++i
)
717 const S2S_TargetCfg
* cfg
= s2s_getConfigById(i
);
718 if (cfg
&& (cfg
->scsiId
& S2S_CFG_TARGET_ENABLED
))
723 *SCSI_CTRL_IDMASK
= idMask
;
726 ((scsiDev
.boardCfg
.flags
& S2S_CFG_DISABLE_GLITCH
) ?
727 SCSI_CTRL_FLAGS_DISABLE_GLITCH
: 0) |
728 ((scsiDev
.boardCfg
.flags
& S2S_CFG_ENABLE_PARITY
) ?
729 SCSI_CTRL_FLAGS_ENABLE_PARITY
: 0);
731 *SCSI_CTRL_SEL_TIMING
=
732 (scsiDev
.boardCfg
.flags
& S2S_CFG_ENABLE_SEL_LATCH
) ?
733 SCSI_FAST_SELECTION
: SCSI_DEFAULT_SELECTION
;
745 if (scsiDev
.phase
!= BUS_FREE
)
750 // Acquire the SCSI bus.
751 for (int i
= 0; i
< 100; ++i
)
760 // Error, couldn't acquire scsi bus
764 if (! scsiStatusBSY())
766 // Error, BSY doesn't work.
770 // Should be safe to use the bus now.
777 for (i
= 0; i
< 256; ++i
)
781 if (*SCSI_STS_DBX
!= (i
& 0xff))
785 /*if (Lookup_OddParity[i & 0xff] != SCSI_ReadPin(SCSI_In_DBP))
794 for (i = 0; i < 8; ++i)
796 SCSI_CTL_PHASE_Write(i);
799 if (SCSI_ReadPin(SCSI_In_MSG) != !!(i & __scsiphase_msg))
803 if (SCSI_ReadPin(SCSI_In_CD) != !!(i & __scsiphase_cd))
807 if (SCSI_ReadPin(SCSI_In_IO) != !!(i & __scsiphase_io))
812 SCSI_CTL_PHASE_Write(0);
814 uint32_t signalsOut[] = { SCSI_Out_ATN, SCSI_Out_BSY, SCSI_Out_RST, SCSI_Out_SEL };
815 uint32_t signalsIn[] = { SCSI_Filt_ATN, SCSI_Filt_BSY, SCSI_Filt_RST, SCSI_Filt_SEL };
817 for (i = 0; i < 4; ++i)
819 SCSI_SetPin(signalsOut[i]);
823 for (j = 0; j < 4; ++j)
827 if (! SCSI_ReadFilt(signalsIn[j]))
834 if (SCSI_ReadFilt(signalsIn[j]))
840 SCSI_ClearPin(signalsOut[i]);