Parity checking
[SCSI2SD-V6.git] / src / firmware / scsiPhy.h
1 // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
2 //
3 // This file is part of SCSI2SD.
4 //
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.
9 //
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.
14 //
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/>.
17 #ifndef SCSIPHY_H
18 #define SCSIPHY_H
19
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)
30 #define SCSI_CTRL_TIMING ((volatile uint8_t*)0x60000014)
31 #define SCSI_CTRL_FLAGS ((volatile uint8_t*)0x60000016)
32 #define SCSI_CTRL_FLAGS_DISABLE_GLITCH 0x1
33 #define SCSI_CTRL_FLAGS_ENABLE_PARITY 0x2
34
35 #define SCSI_STS_FIFO ((volatile uint8_t*)0x60000020)
36 #define SCSI_STS_ALTFIFO ((volatile uint8_t*)0x60000022)
37 #define SCSI_STS_FIFO_COMPLETE ((volatile uint8_t*)0x60000024)
38 #define SCSI_STS_SELECTED ((volatile uint8_t*)0x60000026)
39 #define SCSI_STS_SCSI ((volatile uint8_t*)0x60000028)
40 #define SCSI_STS_DBX ((volatile uint8_t*)0x6000002A)
41 #define SCSI_STS_PARITY_ERR ((volatile uint8_t*)0x6000002C)
42
43 #define SCSI_FIFO_DATA ((volatile uint16_t*)0x60000040)
44 #define SCSI_FIFO_DEPTH 512
45
46
47 #define scsiPhyFifoFull() ((*SCSI_STS_FIFO & 0x01) == 0x01)
48 #define scsiPhyFifoEmpty() ((*SCSI_STS_FIFO & 0x02) == 0x02)
49 #define scsiPhyFifoAltEmpty() ((*SCSI_STS_ALTFIFO & 0x02) == 0x02)
50
51 #define scsiPhyFifoFlip() \
52 {\
53 scsiPhyFifoSel ^= 1; \
54 *SCSI_FIFO_SEL = scsiPhyFifoSel; \
55 }
56
57 #define scsiPhyTx(val) *SCSI_FIFO_DATA = (val)
58 #define scsiPhyRx() *SCSI_FIFO_DATA
59 #define scsiPhyComplete() ((*SCSI_STS_FIFO_COMPLETE & 0x01) == 0x01)
60
61 #define scsiStatusATN() ((*SCSI_STS_SCSI & 0x01) == 0x01)
62 #define scsiStatusBSY() ((*SCSI_STS_SCSI & 0x02) == 0x02)
63 #define scsiStatusRST() ((*SCSI_STS_SCSI & 0x04) == 0x04)
64 #define scsiStatusSEL() ((*SCSI_STS_SCSI & 0x08) == 0x08)
65 #define scsiStatusACK() ((*SCSI_STS_SCSI & 0x10) == 0x10)
66
67 #define scsiParityError() ((*SCSI_STS_PARITY_ERR & 0x1) == 0x1)
68
69 // Disable DMA due to errate with the STM32F205 DMA2 controller when
70 // concurrently transferring FSMC (with FIFO) and APB (ie. sdio)
71 // peripherals.
72 #undef SCSI_FSMC_DMA
73
74 extern uint8_t scsiPhyFifoSel;
75
76 void scsiPhyInit(void);
77 void scsiPhyConfig(void);
78 void scsiPhyReset(void);
79
80 void scsiEnterPhase(int phase);
81 void scsiEnterBusFree(void);
82
83 void scsiSetDataCount(uint32_t count);
84
85 void scsiWrite(const uint8_t* data, uint32_t count);
86 void scsiRead(uint8_t* data, uint32_t count, int* parityError);
87 void scsiWriteByte(uint8_t value);
88 uint8_t scsiReadByte(void);
89
90
91 void sdTmpRead(uint8_t* data, uint32_t lba, int sectors);
92 void sdTmpWrite(uint8_t* data, uint32_t lba, int sectors);
93
94 extern volatile uint8_t scsiRxDMAComplete;
95 extern volatile uint8_t scsiTxDMAComplete;
96 #define scsiDMABusy() (!(scsiRxDMAComplete && scsiTxDMAComplete))
97
98 void scsiReadDMA(uint8_t* data, uint32_t count);
99 int scsiReadDMAPoll();
100
101 void scsiWriteDMA(const uint8_t* data, uint32_t count);
102 int scsiWriteDMAPoll();
103
104 int scsiSelfTest(void);
105
106 #endif