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"
30 static uint8_t asyncTimings
[][4] =
32 /* Speed, Assert, Deskew, Hold, Glitch */
33 {/*1.5MB/s*/ 28, 18, 13, 6},
34 {/*3.3MB/s*/ 13, 6, 6, 6},
35 {/*5MB/s*/ 9, 6, 6, 6}, // 80ns
36 {/*safe*/ 3, 6, 6, 6}, // Probably safe
37 {/*turbo*/ 3, 3, 3, 2}
40 #define SCSI_ASYNC_15 0
41 #define SCSI_ASYNC_33 1
42 #define SCSI_ASYNC_50 2
43 #define SCSI_ASYNC_SAFE 3
44 #define SCSI_ASYNC_TURBO 4
46 // 5MB/s synchronous timing
47 #define SCSI_FAST5_DESKEW 6 // 55ns
48 #define SCSI_FAST5_HOLD 6 // 53ns
50 // 10MB/s synchronous timing
51 // 2:0 Deskew count, 25ns
52 // 6:4 Hold count, 33ns
53 // 3:0 Assertion count, 30ns
54 // We want deskew + hold + assert + 3 to add up to 11 clocks
55 // the fpga code has 1 clock of overhead when transitioning from deskew to
58 #define SCSI_FAST10_DESKEW 2 // 25ns
59 #define SCSI_FAST10_HOLD 3 // 33ns
60 #define SCSI_FAST10_WRITE_ASSERT 3 // 30ns. Overall clocks only works if fpga overhead is 3.
62 // Slow down the cycle to be valid. 2x assert period is TOO FAST when
63 // reading data. It's ok when writing due to the deskew.
64 // 50ns. ie. 100ns / 2. Rounded down because there's likely a few extra cycles
66 #define SCSI_FAST10_READ_ASSERT 5
68 // Fastest possible timing, probably not 20MB/s
69 #define SCSI_FAST20_DESKEW 1
70 #define SCSI_FAST20_HOLD 2
71 #define SCSI_FAST20_ASSERT 2
74 #define syncDeskew(period) ((period) < 35 ? \
75 SCSI_FAST10_DESKEW : SCSI_FAST5_DESKEW)
77 #define syncHold(period) ((period) < 35 ? \
78 ((period) == 25 ? SCSI_FAST10_HOLD : 4) /* 25ns/33ns */\
82 // Number of overhead cycles per period.
83 #define FPGA_OVERHEAD 2
84 #define FPGA_CYCLES_PER_NS 9
85 #define SCSI_PERIOD_CLKS(period) ((((int)period * 4) + (FPGA_CYCLES_PER_NS/2)) / FPGA_CYCLES_PER_NS)
87 // 3.125MB/s (80 period) to < 10MB/s sync
88 // Assumes a 108MHz fpga clock. (9 ns)
89 // 3:0 Assertion count, variable
90 #define syncAssertionWrite(period,deskew) ((SCSI_PERIOD_CLKS(period) - deskew - FPGA_OVERHEAD + 1) / 2)
91 #define syncAssertionRead(period) syncAssertionWrite(period,0)
94 // Time until we consider ourselves selected
96 #define SCSI_DEFAULT_SELECTION 43
97 #define SCSI_FAST_SELECTION 5
99 // Private DMA variables.
100 static int dmaInProgress
= 0;
102 static DMA_HandleTypeDef memToFSMC
;
103 static DMA_HandleTypeDef fsmcToMem
;
106 volatile uint8_t scsiRxDMAComplete
;
107 volatile uint8_t scsiTxDMAComplete
;
109 uint8_t scsiPhyFifoSel
= 0; // global
111 // scsi IRQ handler is initialised by the STM32 HAL. Connected to
113 // Note: naming is important to ensure this function is listed in the
115 void EXTI4_IRQHandler()
117 // Make sure that interrupt flag is set
118 if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_4
) != RESET
) {
120 // Clear interrupt flag
121 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4
);
123 scsiDev
.resetFlag
= scsiDev
.resetFlag
|| scsiStatusRST();
125 // selFlag is required for Philips P2000C which releases it after 600ns
126 // without waiting for BSY.
127 // Also required for some early Mac Plus roms
128 scsiDev
.selFlag
= *SCSI_STS_SELECTED
;
131 __SEV(); // Set event. See corresponding __WFE() calls.
134 static void assertFail()
146 scsiSetDataCount(uint32_t count
)
148 *SCSI_DATA_CNT_HI
= count
>> 8;
149 *SCSI_DATA_CNT_LO
= count
& 0xff;
150 *SCSI_DATA_CNT_SET
= 1;
157 if (!scsiPhyFifoAltEmpty()) {
164 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
166 __WFE(); // Wait for event
169 uint8_t val
= scsiPhyRx();
170 // TODO scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
173 if (!scsiPhyFifoEmpty()) {
175 uint8_t k
__attribute((unused
));
176 while (!scsiPhyFifoEmpty()) { k
= scsiPhyRx(); ++j
; }
187 scsiReadPIO(uint8_t* data
, uint32_t count
)
189 uint16_t* fifoData
= (uint16_t*)data
;
191 for (int i
= 0; i
< (count
+ 1) / 2; ++i
)
193 fifoData
[i
] = scsiPhyRx(); // TODO ASSUMES LITTLE ENDIAN
198 scsiReadDMA(uint8_t* data
, uint32_t count
)
200 // Prepare DMA transfer
203 scsiTxDMAComplete
= 1; // TODO not used much
204 scsiRxDMAComplete
= 0; // TODO not used much
208 (uint32_t) SCSI_FIFO_DATA
,
216 int complete
= __HAL_DMA_GET_COUNTER(&fsmcToMem
) == 0;
217 complete
= complete
&& (HAL_DMA_PollForTransfer(&fsmcToMem
, HAL_DMA_FULL_TRANSFER
, 0xffffffff) == HAL_OK
);
220 scsiTxDMAComplete
= 1; // TODO MM FIX IRQ
221 scsiRxDMAComplete
= 1;
225 // TODO MM scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
237 scsiRead(uint8_t* data
, uint32_t count
, int* parityError
)
243 uint32_t chunk
= ((count
- i
) > SCSI_FIFO_DEPTH
)
244 ? SCSI_FIFO_DEPTH
: (count
- i
);
248 // DMA is doing 32bit transfers.
249 chunk
= chunk
& 0xFFFFFFF8;
252 scsiSetDataCount(chunk
);
254 while (i
< count
&& likely(!scsiDev
.resetFlag
))
256 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
258 __WFE(); // Wait for event
260 *parityError
|= scsiParityError();
263 uint32_t nextChunk
= ((count
- i
- chunk
) > SCSI_FIFO_DEPTH
)
264 ? SCSI_FIFO_DEPTH
: (count
- i
- chunk
);
268 nextChunk
= nextChunk
& 0xFFFFFFF8;
273 scsiSetDataCount(nextChunk
);
280 scsiReadPIO(data
+ i
, chunk
);
285 scsiReadDMA(data
+ i
, chunk
);
287 while (!scsiReadDMAPoll() && likely(!scsiDev
.resetFlag
))
298 if (!scsiPhyFifoEmpty() || !scsiPhyFifoAltEmpty()) {
300 while (!scsiPhyFifoEmpty()) { scsiPhyRx(); ++j
; }
303 while (!scsiPhyFifoEmpty()) { scsiPhyRx(); ++k
; }
311 scsiWriteByte(uint8_t value
)
314 if (!scsiPhyFifoEmpty()) {
324 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
326 __WFE(); // Wait for event
330 if (!scsiPhyFifoAltEmpty()) {
338 scsiWritePIO(const uint8_t* data
, uint32_t count
)
340 uint16_t* fifoData
= (uint16_t*)data
;
341 for (int i
= 0; i
< (count
+ 1) / 2; ++i
)
343 scsiPhyTx(fifoData
[i
]);
348 scsiWriteDMA(const uint8_t* data
, uint32_t count
)
350 // Prepare DMA transfer
353 scsiTxDMAComplete
= 0;
354 scsiRxDMAComplete
= 1;
359 (uint32_t) SCSI_FIFO_DATA
,
366 int complete
= __HAL_DMA_GET_COUNTER(&memToFSMC
) == 0;
367 complete
= complete
&& (HAL_DMA_PollForTransfer(&memToFSMC
, HAL_DMA_FULL_TRANSFER
, 0xffffffff) == HAL_OK
);
370 scsiTxDMAComplete
= 1; // TODO MM FIX IRQ
371 scsiRxDMAComplete
= 1;
383 scsiWrite(const uint8_t* data
, uint32_t count
)
386 while (i
< count
&& likely(!scsiDev
.resetFlag
))
388 uint32_t chunk
= ((count
- i
) > SCSI_FIFO_DEPTH
)
389 ? SCSI_FIFO_DEPTH
: (count
- i
);
392 if (!scsiPhyFifoEmpty()) {
402 scsiWritePIO(data
+ i
, chunk
);
407 // DMA is doing 32bit transfers.
408 chunk
= chunk
& 0xFFFFFFF8;
409 scsiWriteDMA(data
+ i
, chunk
);
411 while (!scsiWriteDMAPoll() && likely(!scsiDev
.resetFlag
))
417 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
419 __WFE(); // Wait for event
423 if (!scsiPhyFifoAltEmpty()) {
430 scsiSetDataCount(chunk
);
433 while (!scsiPhyComplete() && likely(!scsiDev
.resetFlag
))
435 __WFE(); // Wait for event
439 if (!scsiPhyFifoAltEmpty()) {
446 static inline void busSettleDelay(void)
448 // Data Release time (switching IO) = 400ns
449 // + Bus Settle time (switching phase) = 400ns.
450 s2s_delay_us(1); // Close enough.
453 void scsiEnterBusFree()
455 *SCSI_CTRL_BSY
= 0x00;
456 // We now have a Bus Clear Delay of 800ns to release remaining signals.
457 *SCSI_CTRL_PHASE
= 0;
462 uint8_t assertClocks
,
467 *SCSI_CTRL_DESKEW
= ((hold
& 7) << 5) | (deskew
& 0x1F);
468 *SCSI_CTRL_TIMING
= (assertClocks
& 0x3F);
469 *SCSI_CTRL_TIMING3
= (glitch
& 0xF);
473 scsiSetDefaultTiming()
475 const uint8_t* asyncTiming
= asyncTimings
[0];
483 void scsiEnterPhase(int newPhase
)
485 // ANSI INCITS 362-2002 SPI-3 10.7.1:
486 // Phase changes are not allowed while REQ or ACK is asserted.
487 while (likely(!scsiDev
.resetFlag
) && scsiStatusACK()) {}
489 int oldPhase
= *SCSI_CTRL_PHASE
;
491 if (!scsiDev
.resetFlag
&& (!scsiPhyFifoEmpty() || !scsiPhyFifoAltEmpty())) {
495 if (newPhase
!= oldPhase
)
497 if ((newPhase
== DATA_IN
|| newPhase
== DATA_OUT
) &&
498 scsiDev
.target
->syncOffset
)
500 if (scsiDev
.target
->syncPeriod
< 23)
502 scsiSetTiming(SCSI_FAST20_ASSERT
, SCSI_FAST20_DESKEW
, SCSI_FAST20_HOLD
, 1);
504 else if (scsiDev
.target
->syncPeriod
<= 25)
506 if (newPhase
== DATA_IN
)
508 scsiSetTiming(SCSI_FAST10_WRITE_ASSERT
, SCSI_FAST10_DESKEW
, SCSI_FAST10_HOLD
, 1);
512 scsiSetTiming(SCSI_FAST10_READ_ASSERT
, SCSI_FAST10_DESKEW
, SCSI_FAST10_HOLD
, 1);
517 // Amiga A3000 OS3.9 sets period to 35 and fails with
520 scsiDev
.target
->syncPeriod
< 35 ? 1 :
521 (scsiDev
.target
->syncPeriod
< 45 ? 2 : 5);
522 int deskew
= syncDeskew(scsiDev
.target
->syncPeriod
);
524 if (newPhase
== DATA_IN
)
526 assertion
= syncAssertionWrite(scsiDev
.target
->syncPeriod
, deskew
);
530 assertion
= syncAssertionRead(scsiDev
.target
->syncPeriod
);
535 syncHold(scsiDev
.target
->syncPeriod
),
539 *SCSI_CTRL_SYNC_OFFSET
= scsiDev
.target
->syncOffset
;
541 else if (newPhase
>= 0)
544 *SCSI_CTRL_SYNC_OFFSET
= 0;
545 const uint8_t* asyncTiming
;
547 if (scsiDev
.boardCfg
.scsiSpeed
== S2S_CFG_SPEED_NoLimit
)
549 asyncTiming
= asyncTimings
[SCSI_ASYNC_SAFE
];
551 else if (scsiDev
.boardCfg
.scsiSpeed
>= S2S_CFG_SPEED_TURBO
)
553 asyncTiming
= asyncTimings
[SCSI_ASYNC_TURBO
];
555 else if (scsiDev
.boardCfg
.scsiSpeed
>= S2S_CFG_SPEED_ASYNC_50
)
557 asyncTiming
= asyncTimings
[SCSI_ASYNC_50
];
558 } else if (scsiDev
.boardCfg
.scsiSpeed
>= S2S_CFG_SPEED_ASYNC_33
) {
560 asyncTiming
= asyncTimings
[SCSI_ASYNC_33
];
563 asyncTiming
= asyncTimings
[SCSI_ASYNC_15
];
574 *SCSI_CTRL_PHASE
= newPhase
;
577 if (scsiDev
.compatMode
< COMPAT_SCSI2
)
579 // EMU EMAX needs 100uS ! 10uS is not enough.
585 *SCSI_CTRL_PHASE
= 0;
590 uint32_t s2s_getScsiRateMBs()
592 if (scsiDev
.target
->syncOffset
)
594 if (scsiDev
.target
->syncPeriod
< 23)
598 else if (scsiDev
.target
->syncPeriod
<= 25)
604 return 1000 / (scsiDev
.target
->syncPeriod
* 4);
617 HAL_DMA_Abort(&memToFSMC
);
618 HAL_DMA_Abort(&fsmcToMem
);
623 s2s_fpgaReset(); // Clears fifos etc.
625 *SCSI_CTRL_PHASE
= 0x00;
626 *SCSI_CTRL_BSY
= 0x00;
631 *SCSI_CTRL_SYNC_OFFSET
= 0;
632 scsiSetDefaultTiming();
634 // DMA Benchmark code
635 // Currently 14.9MB/s.
641 for (int i
= 0; i
< (100LL * 1024 * 1024 / SCSI_FIFO_DEPTH
); ++i
)
645 (uint32_t) &scsiDev
.data
[0],
646 (uint32_t) SCSI_FIFO_DATA
,
647 SCSI_FIFO_DEPTH
/ 4);
649 HAL_DMA_PollForTransfer(
651 HAL_DMA_FULL_TRANSFER
,
658 for(int i
= 0; i
< 10; ++i
) s2s_delay_ms(1000);
662 #ifdef SCSI_FREQ_TEST
665 *SCSI_CTRL_DBX
= 0xAA;
666 *SCSI_CTRL_DBX
= 0x55;
672 static void scsiPhyInitDMA()
674 // One-time init only.
675 static uint8_t init
= 0;
680 // Memory to memory transfers can only be done using DMA2
683 // Transmit SCSI data. The source data is treated as the
684 // peripheral (even though this is memory-to-memory)
685 memToFSMC
.Instance
= DMA2_Stream0
;
686 memToFSMC
.Init
.Channel
= DMA_CHANNEL_0
;
687 memToFSMC
.Init
.Direction
= DMA_MEMORY_TO_MEMORY
;
688 memToFSMC
.Init
.PeriphInc
= DMA_PINC_ENABLE
;
689 memToFSMC
.Init
.MemInc
= DMA_MINC_DISABLE
;
690 memToFSMC
.Init
.PeriphDataAlignment
= DMA_PDATAALIGN_WORD
;
691 memToFSMC
.Init
.MemDataAlignment
= DMA_MDATAALIGN_HALFWORD
;
692 memToFSMC
.Init
.Mode
= DMA_NORMAL
;
693 memToFSMC
.Init
.Priority
= DMA_PRIORITY_LOW
;
694 // FIFO mode is needed to allow conversion from 32bit words to the
695 // 16bit FSMC interface.
696 memToFSMC
.Init
.FIFOMode
= DMA_FIFOMODE_ENABLE
;
698 // We only use 1 word (4 bytes) in the fifo at a time. Normally it's
699 // better to let the DMA fifo fill up then do burst transfers, but
700 // bursting out the FSMC interface will be very slow and may starve
701 // other (faster) transfers. We don't want to risk the SDIO transfers
702 // from overrun/underrun conditions.
703 memToFSMC
.Init
.FIFOThreshold
= DMA_FIFO_THRESHOLD_1QUARTERFULL
;
704 memToFSMC
.Init
.MemBurst
= DMA_MBURST_SINGLE
;
705 memToFSMC
.Init
.PeriphBurst
= DMA_PBURST_SINGLE
;
706 HAL_DMA_Init(&memToFSMC
);
708 // Receive SCSI data. The source data (fsmc) is treated as the
709 // peripheral (even though this is memory-to-memory)
710 fsmcToMem
.Instance
= DMA2_Stream1
;
711 fsmcToMem
.Init
.Channel
= DMA_CHANNEL_0
;
712 fsmcToMem
.Init
.Direction
= DMA_MEMORY_TO_MEMORY
;
713 fsmcToMem
.Init
.PeriphInc
= DMA_PINC_DISABLE
;
714 fsmcToMem
.Init
.MemInc
= DMA_MINC_ENABLE
;
715 fsmcToMem
.Init
.PeriphDataAlignment
= DMA_PDATAALIGN_HALFWORD
;
716 fsmcToMem
.Init
.MemDataAlignment
= DMA_MDATAALIGN_WORD
;
717 fsmcToMem
.Init
.Mode
= DMA_NORMAL
;
718 fsmcToMem
.Init
.Priority
= DMA_PRIORITY_LOW
;
719 fsmcToMem
.Init
.FIFOMode
= DMA_FIFOMODE_ENABLE
;
720 fsmcToMem
.Init
.FIFOThreshold
= DMA_FIFO_THRESHOLD_1QUARTERFULL
;
721 fsmcToMem
.Init
.MemBurst
= DMA_MBURST_SINGLE
;
722 fsmcToMem
.Init
.PeriphBurst
= DMA_PBURST_SINGLE
;
723 HAL_DMA_Init(&fsmcToMem
);
725 // TODO configure IRQs
734 *SCSI_CTRL_IDMASK
= 0x00; // Reset in scsiPhyConfig
735 *SCSI_CTRL_PHASE
= 0x00;
736 *SCSI_CTRL_BSY
= 0x00;
741 *SCSI_CTRL_SYNC_OFFSET
= 0;
742 scsiSetDefaultTiming();
744 *SCSI_CTRL_SEL_TIMING
= SCSI_DEFAULT_SELECTION
;
750 if (scsiDev
.boardCfg
.flags6
& S2S_CFG_ENABLE_TERMINATOR
)
752 HAL_GPIO_WritePin(nTERM_EN_GPIO_Port
, nTERM_EN_Pin
, GPIO_PIN_RESET
);
756 HAL_GPIO_WritePin(nTERM_EN_GPIO_Port
, nTERM_EN_Pin
, GPIO_PIN_SET
);
761 for (int i
= 0; i
< 8; ++i
)
763 const S2S_TargetCfg
* cfg
= s2s_getConfigById(i
);
764 if (cfg
&& (cfg
->scsiId
& S2S_CFG_TARGET_ENABLED
))
769 *SCSI_CTRL_IDMASK
= idMask
;
772 ((scsiDev
.boardCfg
.flags
& S2S_CFG_DISABLE_GLITCH
) ?
773 SCSI_CTRL_FLAGS_DISABLE_GLITCH
: 0) |
774 ((scsiDev
.boardCfg
.flags
& S2S_CFG_ENABLE_PARITY
) ?
775 SCSI_CTRL_FLAGS_ENABLE_PARITY
: 0);
777 *SCSI_CTRL_SEL_TIMING
=
778 (scsiDev
.boardCfg
.flags
& S2S_CFG_ENABLE_SEL_LATCH
) ?
779 SCSI_FAST_SELECTION
: SCSI_DEFAULT_SELECTION
;
789 // 64 = fpga comms error
792 if (scsiDev
.phase
!= BUS_FREE
)
797 // Acquire the SCSI bus.
798 for (int i
= 0; i
< 100; ++i
)
807 // Error, couldn't acquire scsi bus
812 if (! scsiStatusBSY())
816 // Error, BSY doesn't work.
820 // Should be safe to use the bus now.
826 if ((*SCSI_STS_DBX
& 0xff) != 0)
833 for (i
= 0; i
< 8; ++i
)
835 uint8_t data
= 1 << i
;
838 *SCSI_CTRL_DBX
= data
;
840 // STS_DBX is 16 bit!
841 if ((*SCSI_STS_DBX
& 0xff) != data
)
850 // FPGA comms test code
851 for(i
= 0; i
< 10000; ++i
)
853 for (int j
= 0; j
< SCSI_FIFO_DEPTH
; ++j
)
858 if (!scsiPhyFifoEmpty())
863 *SCSI_CTRL_PHASE
= DATA_IN
;
866 (uint32_t) &scsiDev
.data
[0],
867 (uint32_t) SCSI_FIFO_DATA
,
868 SCSI_FIFO_DEPTH
/ 4);
870 HAL_DMA_PollForTransfer(
872 HAL_DMA_FULL_TRANSFER
,
875 if (!scsiPhyFifoFull())
880 memset(&scsiDev
.data
[0], 0, SCSI_FIFO_DEPTH
);
882 *SCSI_CTRL_PHASE
= DATA_OUT
;
885 (uint32_t) SCSI_FIFO_DATA
,
886 (uint32_t) &scsiDev
.data
[0],
887 SCSI_FIFO_DEPTH
/ 2);
889 HAL_DMA_PollForTransfer(
891 HAL_DMA_FULL_TRANSFER
,
894 if (!scsiPhyFifoEmpty())
900 for (int j
= 0; j
< SCSI_FIFO_DEPTH
; ++j
)
902 if (scsiDev
.data
[j
] != (uint8_t) j
)