PS2SDK
PS2 Homebrew Libraries
spi_sdcard_driver.h
1 #ifndef SPI_SDCARD_DRIVER_H
2 #define SPI_SDCARD_DRIVER_H
3 
4 #include <bdm.h>
5 #include <stdint.h>
6 
7 #define READ_TOKEN_TIMEOUT 4000
8 #define SECTOR_SIZE 512
9 
10 #define CMD_ABORT_NO_READ_TOKEN 0x1
11 #define CMD_ABORT_CRC16_INVALID 0x2
12 #define CMD_ABORT_WRITE_ERROR 0x3
13 #define CMD_ABORT_UKNOWN_ERROR 0x7
14 
15 typedef enum card_type_e {
16  CARD_TYPE_MMC = 0x00,
17  CARD_TYPE_SDV1 = 0x01,
18  CARD_TYPE_SDV2 = 0x02,
19  CARD_TYPE_SDV2HC = 0x04
20 } card_type_t;
21 
22 typedef enum spisd_result_s {
23  SPISD_RESULT_OK = 0,
24  SPISD_RESULT_ERROR,
25  SPISD_RESULT_NO_CARD,
26  SPISD_RESULT_TIMEOUT,
27 } spisd_result_t;
28 
29 /*
30  * START: structures copied from wisi's implementation
31  */
32 struct t_csdVer1
33 {
34  u8 reserved1 : 6;
35  u8 csd_structure : 2;
36  u8 taac : 8;
37  u8 nsac : 8;
38  u8 tran_speed : 8;
39 
40  u16 cccHi : 8;
41  u16 read_bl_len : 4;
42  u16 cccLo : 4;
43 
44  u32 c_sizeHi : 2;
45  u8 read_bl_partial : 1;
46  u8 write_blk_misalign : 1;
47  u8 read_blk_misalign : 1;
48  u8 dsr_imp : 1;
49  u8 reserved2 : 2;
50 
51  u32 c_sizeMd : 8;
52 
53  u32 c_sizeLo : 2;
54  u8 vdd_r_curr_max : 3;
55  u8 vdd_r_curr_min : 3;
56 
57  u8 c_size_multHi : 2;
58  u8 vdd_w_curr_max : 3;
59  u8 vdd_w_curr_min : 3;
60 
61  u8 c_size_multLo : 1;
62  // XXX: The folowing are not fixed to correct endianness!
63  u8 erase_blk_en : 1;
64  u8 sector_size_high : 6;
65  u8 sector_size_low : 1;
66  u8 wp_grp_size : 7;
67  u8 ep_grp_enable : 1;
68  u8 reserved5 : 2;
69  u8 r2w_factor : 3;
70  u8 write_bl_len_high : 2;
71  u8 write_bl_len_low : 2;
72  u8 write_bl_partial : 1;
73  u8 reserved6 : 5;
74  u8 file_format_group : 1;
75  u8 copy : 1;
76  u8 perm_write_protect : 1;
77  u8 tmp_write_protect : 1;
78  u8 file_format : 2;
79  u8 reserved7 : 2;
80  u8 crc : 7;
81  u8 fixedTo1 : 1;
82 } __attribute__((packed));
83 
84 struct t_csdVer2
85 {
86  u8 reserved1 : 6;
87  u8 csd_structure : 2;
88  u8 taac : 8;
89  u8 nsac : 8;
90  u8 tran_speed : 8;
91 
92  u16 cccHi : 8;
93  u16 read_bl_len : 4;
94  u16 cccLo : 4;
95 
96  // Not fixed!:
97  u8 read_bl_partial : 1;
98  u8 write_blk_misalign : 1;
99  u8 read_blk_misalign : 1;
100  u8 dsr_imp : 1;
101  u8 reserved2 : 4;
102 
103  // fixed:
104  u32 c_sizeHi : 6;
105  u8 reserved3 : 2; // I give up - this is far too unaligned...
106  // 79:58
107  u32 c_sizeMd : 8;
108  u32 c_sizeLo : 8; // 16 22;
109 
110  // Not fixed!:
111  u32 reserved4 : 1;
112  u8 erase_blk_en : 1;
113  u8 sector_size_high : 6;
114  u8 sector_size_low : 1;
115  u8 wp_grp_size : 7;
116  u8 ep_grp_enable : 1;
117  u8 reserved5 : 2;
118  u8 r2w_factor : 3;
119  u8 write_bl_len_high : 2;
120  u8 write_bl_len_low : 2;
121  u8 write_bl_partial : 1;
122  u8 reserved6 : 5;
123  u8 file_format_group : 1;
124  u8 copy : 1;
125  u8 perm_write_protect : 1;
126  u8 tmp_write_protect : 1;
127  u8 file_format : 2;
128  u8 reserved7 : 2;
129  u8 crc : 7;
130  u8 fixedTo1 : 1;
131 } __attribute__((packed));
132 /*
133  * END: structures copied from wisi's implementation
134  */
135 
136 typedef struct spisd_cid_t
137 { /* Card Identification Data */
138  uint8_t ManufacturerID; /* ManufacturerID */
139  uint16_t OEM_AppliID; /* OEM/Application ID */
140  uint32_t ProdName1; /* Product Name part1 */
141  uint8_t ProdName2; /* Product Name part2*/
142  uint8_t ProdRev; /* Product Revision */
143  uint32_t ProdSN; /* Product Serial Number */
144  uint8_t Reserved1; /* Reserved1 */
145  uint16_t ManufactDate; /* Manufacturing Date */
146  uint8_t CID_CRC; /* CID CRC */
147 }spisd_cid_t;
148 
149 /* global struct for everything sdcard */
150 typedef struct spisd_t
151 {
152  uint8_t csd[16];
153  spisd_cid_t cid;
154  card_type_t card_type;
155  uint8_t initialized;
156  uint8_t used;
157 }spisd_t;
158 
159 /* globals */
160 extern spisd_t sdcard;
161 extern struct block_device bd;
162 
163 extern uint16_t spisd_read_status_register();
164 
165 /* init */
166 extern int spisd_init_card(); /* bring card up from identification mode to data-transfer mode */
167 extern int spisd_get_card_info(); /* get card info and reattach to bdm if card capacity changed */
168 extern int spisd_recover();
169 
170 /* BDM functions */
171 extern int spisd_read(struct block_device *bd, uint64_t sector, void *buffer, uint16_t count);
172 extern int spisd_write(struct block_device *bd, uint64_t sector, const void *buffer, uint16_t count);
173 extern void spisd_flush(struct block_device *bd);
174 extern int spisd_stop(struct block_device *bd);
175 
176 
177 #endif
bdm.h
t_csdVer1
Definition: spi_sdcard_driver.h:32
t_csdVer2
Definition: spi_sdcard_driver.h:84
count
u32 count
start sector of fragmented bd/file
Definition: usbhdfsd-common.h:3
__attribute__
typedef __attribute__
Definition: tlbfunc.c:60
spisd_t
Definition: spi_sdcard_driver.h:150
spisd_cid_t
Definition: spi_sdcard_driver.h:136
block_device
Definition: bdm.h:21