Fix bug in using nor flash storage without SD
[SCSI2SD.git] / software / SCSI2SD / src / sd.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 SD_H
18 #define SD_H
19
20 #include "storedevice.h"
21
22 #define SD_SECTOR_SIZE 512
23
24 typedef enum
25 {
26         SD_GO_IDLE_STATE = 0,
27         SD_SEND_OP_COND = 1,
28         SD_SEND_IF_COND = 8, // SD V2
29         SD_SEND_CSD = 9,
30         SD_SEND_CID = 10,
31         SD_STOP_TRANSMISSION = 12,
32         SD_SEND_STATUS = 13,
33         SD_SET_BLOCKLEN = 16,
34         SD_READ_SINGLE_BLOCK = 17,
35         SD_READ_MULTIPLE_BLOCK = 18,
36         SD_APP_SET_WR_BLK_ERASE_COUNT = 23,
37         SD_WRITE_MULTIPLE_BLOCK = 25,
38         SD_APP_SEND_OP_COND = 41,
39         SD_APP_CMD = 55,
40         SD_READ_OCR = 58,
41         SD_CRC_ON_OFF = 59
42 } SD_CMD;
43
44 typedef enum
45 {
46         SD_R1_IDLE = 1,
47         SD_R1_ERASE_RESET = 2,
48         SD_R1_ILLEGAL = 4,
49         SD_R1_CRC = 8,
50         SD_R1_ERASE_SEQ = 0x10,
51         SD_R1_ADDRESS = 0x20,
52         SD_R1_PARAMETER = 0x40
53 } SD_R1;
54
55 typedef struct
56 {
57         S2S_Device dev;
58
59         S2S_Target targets[MAX_SCSI_TARGETS];
60
61         int version; // SDHC = version 2.
62         int ccs; // Card Capacity Status. 1 = SDHC or SDXC
63         uint32_t capacity; // in 512 byte blocks
64
65         uint8_t csd[16]; // Unparsed CSD
66         uint8_t cid[16]; // Unparsed CID
67
68         uint32_t lastPollMediaTime;
69 } SdCard;
70
71 extern SdCard sdCard;
72 extern S2S_Device* sdDevice;
73
74 extern volatile uint8_t sdRxDMAComplete;
75 extern volatile uint8_t sdTxDMAComplete;
76
77 int sdInit(void);
78
79 #define sdDMABusy() (!(sdRxDMAComplete && sdTxDMAComplete))
80
81 void sdWriteMultiSectorPrep(uint32_t sdLBA, uint32_t sdSectors);
82 void sdWriteMultiSectorDMA(uint8_t* outputBuffer);
83 int sdWriteSectorDMAPoll();
84
85 void sdReadMultiSectorPrep(uint32_t sdLBA, uint32_t sdSectors);
86 void sdReadMultiSectorDMA(uint8_t* outputBuffer);
87 void sdReadSingleSectorDMA(uint32_t lba, uint8_t* outputBuffer);
88 int sdReadSectorDMAPoll();
89
90 void sdCompleteTransfer(void);
91 void sdCheckPresent();
92 void sdPoll();
93
94 #endif