1 /*
2  * Copyright (c) 2016 - 2020, Broadcom
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef CSL_SD_CMD_H
8 #define CSL_SD_CMD_H
9 
10 #define SD_CMD_OK      0
11 #define SD_CMD_ERROR  -1
12 
13 #define SD_CMD_ERR_NO_IO_FUNC           5
14 #define SD_CMD_ERR_INVALID_PARAMETER    6
15 #define SD_CMD_ERR_R1_ILLEGAL_COMMAND   7
16 #define SD_CMD_ERR_R1_COM_CRC_ERROR     8
17 #define SD_CMD_ERR_R1_FUNC_NUM_ERROR    9
18 #define SD_CMD_ERR_R1_ADDRESS_ERROR    10
19 #define SD_CMD_ERR_R1_PARAMETER_ERROR  11
20 #define SD_CMD_ERR_DATA_ERROR_TOKEN    12
21 #define SD_CMD_ERR_DATA_NOT_ACCEPTED   13
22 #define SD_CMD7_ARG_RCA_SHIFT          16
23 
24 #define SD_CARD_STATUS_PENDING                 0x01
25 #define SD_CARD_STATUS_BUFFER_OVERFLOW         0x01
26 #define SD_CARD_STATUS_DEVICE_BUSY             0x02
27 #define SD_CARD_STATUS_UNSUCCESSFUL            0x03
28 #define SD_CARD_STATUS_NOT_IMPLEMENTED         0x04
29 #define SD_CARD_STATUS_ACCESS_VIOLATION        0x05
30 #define SD_CARD_STATUS_INVALID_HANDLE          0x06
31 #define SD_CARD_STATUS_INVALID_PARAMETER       0x07
32 #define SD_CARD_STATUS_NO_SUCH_DEVICE          0x08
33 #define SD_CARD_STATUS_INVALID_DEVICE_REQUEST  0x09
34 #define SD_CARD_STATUS_NO_MEMORY               0x0A
35 #define SD_CARD_STATUS_BUS_DRIVER_NOT_READY    0x0B
36 #define SD_CARD_STATUS_DATA_ERROR              0x0C
37 #define SD_CARD_STATUS_CRC_ERROR               0x0D
38 #define SD_CARD_STATUS_INSUFFICIENT_RESOURCES  0x0E
39 #define SD_CARD_STATUS_DEVICE_NOT_CONNECTED    0x10
40 #define SD_CARD_STATUS_DEVICE_REMOVED          0x11
41 #define SD_CARD_STATUS_DEVICE_NOT_RESPONDING   0x12
42 #define SD_CARD_STATUS_CANCELED                0x13
43 #define SD_CARD_STATUS_RESPONSE_TIMEOUT        0x14
44 #define SD_CARD_STATUS_DATA_TIMEOUT            0x15
45 #define SD_CARD_STATUS_DEVICE_RESPONSE_ERROR   0x16
46 #define SD_CARD_STATUS_DEVICE_UNSUPPORTED      0x17
47 
48 /* Response structure */
49 struct sd_r2_resp {
50 	uint32_t rsp4;	/* 127:96 */
51 	uint32_t rsp3;	/* 95:64 */
52 	uint32_t rsp2;	/* 63:32 */
53 	uint32_t rsp1;	/* 31:0 */
54 };
55 
56 struct sd_r3_resp {
57 	uint32_t ocr;
58 };
59 
60 struct sd_r4_resp {
61 	uint8_t cardReady;
62 	uint8_t funcs;
63 	uint8_t memPresent;
64 	uint32_t ocr;
65 };
66 
67 struct  sd_r5_resp {
68 	uint8_t data;
69 };
70 
71 struct sd_r6_resp {
72 	uint16_t rca;
73 	uint16_t cardStatus;
74 };
75 
76 struct sd_r7_resp {
77 	uint16_t rca;
78 };
79 
80 struct sd_resp {
81 	uint8_t r1;
82 	uint32_t cardStatus;
83 	uint32_t rawData[4];
84 	union {
85 		struct sd_r2_resp r2;
86 		struct sd_r3_resp r3;
87 		struct sd_r4_resp r4;
88 		struct sd_r5_resp r5;
89 		struct sd_r6_resp r6;
90 		struct sd_r7_resp r7;
91 	} data;
92 };
93 
94 struct sd_card_info {
95 	uint32_t type;	/* card type SD, MMC or SDIO */
96 	uint64_t size;	/* card size */
97 	uint32_t speed;	/* card speed */
98 	uint32_t voltage;	/* voltage supported */
99 	uint32_t mId;	/* manufacturer ID */
100 	uint32_t oId;	/* OEM ID */
101 	uint32_t classes;	/* card class */
102 	uint32_t name1;	/* product name part 1 */
103 	uint32_t name2;	/* product name part 2 */
104 	uint32_t revision;	/* revison */
105 	uint32_t sn;	/* serial number */
106 	uint32_t numIoFuns;	/* total I/O function number */
107 	uint32_t maxRdBlkLen;	/* max read block length */
108 	uint32_t maxWtBlkLen;	/* max write block length */
109 	uint32_t blkMode;	/* sdio card block mode support */
110 	uint32_t f0Cis;	/* sdio card block mode support */
111 	uint32_t f1Cis;	/* sdio card block mode support */
112 
113 	uint8_t partRead;	/* partial block read allowed */
114 	uint8_t partWrite;	/* partial block write allowed */
115 	uint8_t dsr;	/* card DSR */
116 	uint8_t rdCurMin;	/* min current for read */
117 	uint8_t rdCurMax;	/* max current for read */
118 	uint8_t wtCurMin;	/* min current for write */
119 	uint8_t wtCurMax;	/* max current for write */
120 	uint8_t erase;	/* erase enable */
121 	uint8_t eraseSecSize;	/* erase sector size */
122 	uint8_t proGrpSize;	/* write protection group size */
123 	uint8_t protect;	/* permanent write protection or not */
124 	uint8_t tmpProt;	/* temp write protection or not */
125 	uint8_t wtSpeed;	/* write speed relatively to read */
126 	uint8_t version;	/* card version 0:1.0 - 1.01, 1:1.10, 2:2.0 */
127 	uint8_t eraseState;	/* if the data will be 0 or 1 after erase */
128 	uint8_t bus;	/* data with supported */
129 	uint8_t security;	/* security support 0, 2:1.01 3:2.0 */
130 	uint8_t format;	/* file format */
131 	uint8_t fileGrp;	/* file group */
132 	char pwd[20];	/* password */
133 };
134 
135 struct sd_handle {
136 	struct sd_dev *device;
137 	struct sd_card_info *card;
138 };
139 
140 int sd_cmd0(struct sd_handle *handle);
141 int sd_cmd1(struct sd_handle *handle, uint32_t initOcr, uint32_t *ocr);
142 int sd_cmd2(struct sd_handle *handle);
143 int sd_cmd3(struct sd_handle *handle);
144 int sd_cmd7(struct sd_handle *handle, uint32_t rca);
145 int sd_cmd9(struct sd_handle *handle, struct sd_card_data *card);
146 int sd_cmd13(struct sd_handle *handle, uint32_t *status);
147 int sd_cmd16(struct sd_handle *handle, uint32_t blockLen);
148 int sd_cmd17(struct sd_handle *handle,
149 	     uint32_t addr, uint32_t len, uint8_t *buffer);
150 int sd_cmd18(struct sd_handle *handle,
151 	     uint32_t addr, uint32_t len, uint8_t *buffer);
152 #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE
153 int sd_cmd24(struct sd_handle *handle,
154 	     uint32_t addr, uint32_t len, uint8_t *buffer);
155 int sd_cmd25(struct sd_handle *handle,
156 	     uint32_t addr, uint32_t len, uint8_t *buffer);
157 #endif
158 #ifdef INCLUDE_EMMC_DRIVER_ERASE_CODE
159 int sd_cmd35(struct sd_handle *handle, uint32_t start);
160 int sd_cmd36(struct sd_handle *handle, uint32_t end);
161 int sd_cmd38(struct sd_handle *handle);
162 #endif
163 int mmc_cmd6(struct sd_handle *handle, uint32_t argument);
164 int mmc_cmd8(struct sd_handle *handle, uint8_t *extCsdReg);
165 
166 int send_cmd(struct sd_handle *handle, uint32_t cmdIndex,
167 	     uint32_t argument, uint32_t options, struct sd_resp *resp);
168 #endif /* CSL_SD_CMD_H */
169