Glitch filter configuration options and update to PSoC Creator v3.2
[SCSI2SD.git] / software / include / scsi2sd.h
1 //      Copyright (C) 2014 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 scsi2sd_h
18 #define scsi2sd_h
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 /* Common type definitions shared between the firmware and config tools
25
26         The configuration data is stored in flash.
27
28         The flash is organised as 2 arrays of 256 rows, with each row
29         having 256 bytes. Total of 128kb.
30
31         Linear flash memory map:
32         -----------------------------------------
33         Array 1 |Row 255 | Bootloader metadata
34                         ---------------------------------
35                         |Row 254 |
36                         |Row 252 | Blank
37                         ---------------------------------
38                         |Row 251 |
39                         | ...    |
40                         |Row 236 | Config target 3
41                         | ...    |
42                         |Row 220 | Config target 2
43                         | ...    |
44                         |Row 204 | Config target 1
45                         | ...    |
46                         |Row 188 | Config target 0
47                         ---------------------------------
48                         |Row 235 |
49                         | ...    |
50                         |Row 0   |
51         --------|        |
52         Array 0 |Row 255 | Blank
53                         ---------------------------------
54                         |Row 121 |
55                         | ...    |
56                         |Row 37  | Application
57                         ---------------------------------
58                         |Row 36  |
59                         | ...    |
60                         |Row 0   | Bootloader
61
62 */
63
64 #include "stdint.h"
65
66 #define MAX_SCSI_TARGETS 4
67 #define SCSI_CONFIG_ARRAY 1
68 #define SCSI_CONFIG_ROWS 16
69
70 // 256 bytes data, 32 bytes ECC
71 #define SCSI_CONFIG_ROW_SIZE 256
72 #define SCSI_CONFIG_ROW_ECC 288
73 #define SCSI_CONFIG_0_ROW 188
74 #define SCSI_CONFIG_1_ROW 204
75 #define SCSI_CONFIG_2_ROW 220
76 #define SCSI_CONFIG_3_ROW 236
77
78 typedef enum
79 {
80         CONFIG_TARGET_ID_BITS = 0x07,
81         CONFIG_TARGET_ENABLED = 0x80
82 } CONFIG_TARGET_FLAGS;
83
84 typedef enum
85 {
86         CONFIG_ENABLE_UNIT_ATTENTION = 1,
87         CONFIG_ENABLE_PARITY = 2,
88         CONFIG_ENABLE_SCSI2 = 4,
89         CONFIG_DISABLE_GLITCH = 8
90 } CONFIG_FLAGS;
91
92 typedef enum
93 {
94         CONFIG_FIXED,
95         CONFIG_REMOVEABLE,
96         CONFIG_OPTICAL,
97         CONFIG_FLOPPY_14MB
98 } CONFIG_TYPE;
99
100 typedef enum
101 {
102         CONFIG_QUIRKS_NONE,
103         CONFIG_QUIRKS_APPLE
104 } CONFIG_QUIRKS;
105
106 typedef struct __attribute__((packed))
107 {
108         uint8_t deviceType;
109         uint8_t pageCode;
110         uint8_t reserved;
111         uint8_t pageLength;
112         uint8_t data[0]; // pageLength bytes.
113 } VPD;
114
115 typedef struct __attribute__((packed))
116 {
117         // bits 7 -> 3 = CONFIG_TARGET_FLAGS
118         // bits 2 -> 0 = target SCSI ID.
119         uint8_t scsiId;
120
121         uint8_t deviceType; // CONFIG_TYPE
122         uint8_t flags; // CONFIG_FLAGS
123         uint8_t deviceTypeModifier; // Used in INQUIRY response.
124
125         uint32_t sdSectorStart;
126         uint32_t scsiSectors;
127
128         uint16_t bytesPerSector;
129
130         // Max allowed by legacy IBM-PC bios is 6 bits (63)
131         uint16_t sectorsPerTrack;
132
133         // MS-Dos up to 7.10 will crash on >= 256 heads.
134         uint16_t headsPerCylinder;
135
136
137         char vendor[8];
138         char prodId[16];
139         char revision[4];
140         char serial[16];
141
142         uint16_t quirks; // CONFIG_QUIRKS
143
144         uint8_t reserved[960]; // Pad out to 1024 bytes for main section.
145
146         uint8_t vpd[3072]; // Total size is 4k.
147 } TargetConfig;
148
149 typedef enum
150 {
151         CONFIG_NONE, // Invalid
152
153         // Command content:
154         // uint8_t CONFIG_PING
155         // Response:
156         // CONFIG_STATUS
157         CONFIG_PING,
158
159         // Command content:
160         // uint8_t CONFIG_WRITEFLASH
161         // uint8_t[256] flashData
162         // uint8_t flashArray
163         // uint8_t flashRow
164         // Response:
165         // CONFIG_STATUS
166         CONFIG_WRITEFLASH,
167
168         // Command content:
169         // uint8_t CONFIG_READFLASH
170         // uint8_t flashArray
171         // uint8_t flashRow
172         // Response:
173         // 256 bytes of flash
174         CONFIG_READFLASH,
175
176         // Command content:
177         // uint8_t CONFIG_REBOOT
178         // Response: None.
179         CONFIG_REBOOT,
180
181         // Command content:
182         // uint8_t CONFIG_INFO
183         // Response:
184         // uint8_t[16] CSD
185         // uint8_t[16] CID
186         CONFIG_SDINFO,
187
188         // Command content:
189         // uint8_t CONFIG_SCSITEST
190         // Response:
191         // CONFIG_STATUS
192         // uint8_t result code (0 = passed)
193         CONFIG_SCSITEST
194 } CONFIG_COMMAND;
195
196 typedef enum
197 {
198         CONFIG_STATUS_GOOD,
199         CONFIG_STATUS_ERR
200 } CONFIG_STATUS;
201
202
203
204
205 #ifdef __cplusplus
206 } // extern "C"
207
208         #include <type_traits>
209         static_assert(
210                 std::is_pod<TargetConfig>::value, "Misuse of TargetConfig struct"
211                 );
212         static_assert(
213                 sizeof(TargetConfig) == 4096,
214                 "TargetConfig struct size mismatch"
215                 );
216
217 #endif
218
219 #endif