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/>.
20 #define SCSI_CTRL_IDMASK ((volatile uint8_t*)0x60000000)
21 #define SCSI_CTRL_PHASE ((volatile uint8_t*)0x60000002)
22 #define SCSI_CTRL_BSY ((volatile uint8_t*)0x60000004)
23 #define SCSI_FIFO_SEL ((volatile uint8_t*)0x60000006)
24 #define SCSI_DATA_CNT_HI ((volatile uint8_t*)0x60000008)
25 #define SCSI_DATA_CNT_LO ((volatile uint8_t*)0x6000000A)
26 #define SCSI_DATA_CNT_SET ((volatile uint8_t*)0x6000000C)
27 #define SCSI_CTRL_DBX ((volatile uint8_t*)0x6000000E)
28 #define SCSI_CTRL_SYNC_OFFSET ((volatile uint8_t*)0x60000010)
29 #define SCSI_CTRL_DESKEW ((volatile uint8_t*)0x60000012)// Timing
30 #define SCSI_CTRL_TIMING ((volatile uint8_t*)0x60000014)//Timing2
31 #define SCSI_CTRL_TIMING3 ((volatile uint8_t*)0x6000001A)//Timing3
32 #define SCSI_CTRL_FLAGS ((volatile uint8_t*)0x60000016)
33 #define SCSI_CTRL_FLAGS_DISABLE_GLITCH 0x1
34 #define SCSI_CTRL_FLAGS_ENABLE_PARITY 0x2
35 #define SCSI_CTRL_SEL_TIMING ((volatile uint8_t*)0x60000018)
37 #define SCSI_STS_FIFO ((volatile uint8_t*)0x60000020)
38 #define SCSI_STS_ALTFIFO ((volatile uint8_t*)0x60000022)
39 #define SCSI_STS_FIFO_COMPLETE ((volatile uint8_t*)0x60000024)
40 #define SCSI_STS_SELECTED ((volatile uint8_t*)0x60000026)
41 #define SCSI_STS_SCSI ((volatile uint8_t*)0x60000028)
43 // top 8 bits = data we're writing.
44 // bottom 8 bits = data we're reading
45 #define SCSI_STS_DBX ((volatile uint16_t*)0x6000002A)
47 #define SCSI_STS_PARITY_ERR ((volatile uint8_t*)0x6000002C)
49 #define SCSI_FIFO_DATA ((volatile uint16_t*)0x60000040)
50 #define SCSI_FIFO_DEPTH 512
53 #define scsiPhyFifoFull() ((*SCSI_STS_FIFO & 0x01) == 0x01)
54 #define scsiPhyFifoEmpty() ((*SCSI_STS_FIFO & 0x02) == 0x02)
55 #define scsiPhyFifoAltEmpty() ((*SCSI_STS_ALTFIFO & 0x02) == 0x02)
57 #define scsiPhyFifoFlip() \
59 scsiPhyFifoSel ^= 1; \
60 *SCSI_FIFO_SEL = scsiPhyFifoSel; \
63 #define scsiPhyTx(val) *SCSI_FIFO_DATA = (val)
65 // little endian specific !. Also relies on the fsmc outputting the lower
67 #define scsiPhyTx32(a,b) *((volatile uint32_t*)SCSI_FIFO_DATA) = (((uint32_t)(b)) << 16) | (a)
69 #define scsiPhyRx() *SCSI_FIFO_DATA
70 #define scsiPhyComplete() ((*SCSI_STS_FIFO_COMPLETE & 0x01) == 0x01)
72 #define scsiStatusATN() ((*SCSI_STS_SCSI & 0x01) == 0x01)
73 #define scsiStatusBSY() ((*SCSI_STS_SCSI & 0x02) == 0x02)
74 #define scsiStatusRST() ((*SCSI_STS_SCSI & 0x04) == 0x04)
75 #define scsiStatusSEL() ((*SCSI_STS_SCSI & 0x08) == 0x08)
76 #define scsiStatusACK() ((*SCSI_STS_SCSI & 0x10) == 0x10)
78 #define scsiParityError() ((*SCSI_STS_PARITY_ERR & 0x1) == 0x1)
80 // Disable DMA due to errate with the STM32F205 DMA2 controller when
81 // concurrently transferring FSMC (with FIFO) and APB (ie. sdio)
85 extern uint8_t scsiPhyFifoSel;
87 void scsiPhyInit(void);
88 void scsiPhyConfig(void);
89 void scsiPhyReset(void);
91 void scsiEnterPhase(int phase);
92 void scsiEnterBusFree(void);
94 void scsiSetDataCount(uint32_t count);
96 void scsiWrite(const uint8_t* data, uint32_t count);
97 void scsiRead(uint8_t* data, uint32_t count, int* parityError);
98 void scsiWriteByte(uint8_t value);
99 uint8_t scsiReadByte(void);
102 void sdTmpRead(uint8_t* data, uint32_t lba, int sectors);
103 void sdTmpWrite(uint8_t* data, uint32_t lba, int sectors);
105 extern volatile uint8_t scsiRxDMAComplete;
106 extern volatile uint8_t scsiTxDMAComplete;
107 #define scsiDMABusy() (!(scsiRxDMAComplete && scsiTxDMAComplete))
109 void scsiReadDMA(uint8_t* data, uint32_t count);
110 int scsiReadDMAPoll();
112 void scsiWriteDMA(const uint8_t* data, uint32_t count);
113 int scsiWriteDMAPoll();
115 int scsiSelfTest(void);