1 /*
2  * Copyright (c) 2006-2024, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author        Notes
8  * 2011-07-25     weety         first version
9  * 2024-05-24     HPMicro       add HS400 support
10  * 2024-05-26     HPMicro       add UHS-I support for SD card
11  */
12 
13 #ifndef __MMCSD_CARD_H__
14 #define __MMCSD_CARD_H__
15 
16 #include <drivers/mmcsd_host.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #define SD_SCR_BUS_WIDTH_1  (1 << 0)
23 #define SD_SCR_BUS_WIDTH_4  (1 << 2)
24 
25 struct rt_mmcsd_cid {
26     rt_uint8_t  mid;       /* ManufacturerID */
27     rt_uint8_t  prv;       /* Product Revision */
28     rt_uint16_t oid;       /* OEM/Application ID */
29     rt_uint32_t psn;       /* Product Serial Number */
30     rt_uint8_t  pnm[5];    /* Product Name */
31     rt_uint8_t  reserved1;/* reserved */
32     rt_uint16_t mdt;       /* Manufacturing Date */
33     rt_uint8_t  crc;       /* CID CRC */
34     rt_uint8_t  reserved2;/* not used, always 1 */
35 };
36 
37 struct rt_mmcsd_csd {
38     rt_uint8_t      csd_structure;  /* CSD register version */
39     rt_uint8_t      taac;
40     rt_uint8_t      nsac;
41     rt_uint8_t      tran_speed; /* max data transfer rate */
42     rt_uint16_t     card_cmd_class; /* card command classes */
43     rt_uint8_t      rd_blk_len; /* max read data block length */
44     rt_uint8_t      rd_blk_part;
45     rt_uint8_t      wr_blk_misalign;
46     rt_uint8_t      rd_blk_misalign;
47     rt_uint8_t      dsr_imp;    /* DSR implemented */
48     rt_uint8_t      c_size_mult;    /* CSD 1.0 , device size multiplier */
49     rt_uint32_t     c_size;     /* device size */
50     rt_uint8_t      r2w_factor;
51     rt_uint8_t      wr_blk_len; /* max wtire data block length */
52     rt_uint8_t      wr_blk_partial;
53     rt_uint8_t      csd_crc;
54 
55 };
56 
57 struct rt_sd_scr {
58     rt_uint8_t      sd_version;
59     rt_uint8_t      sd_bus_widths;
60 };
61 
62 struct rt_sdio_cccr {
63     rt_uint8_t      sdio_version;
64     rt_uint8_t      sd_version;
65     rt_uint8_t      direct_cmd:1,     /*  Card Supports Direct Commands during data transfer
66                                                    only SD mode, not used for SPI mode */
67                 multi_block:1,    /*  Card Supports Multi-Block */
68                 read_wait:1,      /*  Card Supports Read Wait
69                                        only SD mode, not used for SPI mode */
70                 suspend_resume:1, /*  Card supports Suspend/Resume
71                                        only SD mode, not used for SPI mode */
72                 s4mi:1,            /* generate interrupts during a 4-bit
73                                       multi-block data transfer */
74                 e4mi:1,            /*  Enable the multi-block IRQ during
75                                        4-bit transfer for the SDIO card */
76                 low_speed:1,      /*  Card  is  a  Low-Speed  card */
77                 low_speed_4:1;    /*  4-bit support for Low-Speed cards */
78 
79     rt_uint8_t      bus_width:1,     /* Support SDIO bus width, 1:4bit, 0:1bit */
80                 cd_disable:1,    /*  Connect[0]/Disconnect[1] the 10K-90K ohm pull-up
81                                      resistor on CD/DAT[3] (pin 1) of the card */
82                 power_ctrl:1,    /* Support Master Power Control */
83                 high_speed:1;    /* Support High-Speed  */
84 
85 
86 };
87 
88 /*
89  * SD Status
90  */
91 union rt_sd_status {
92     rt_uint32_t status_words[16];
93     struct {
94         rt_uint32_t reserved[12];
95         rt_uint64_t : 8;
96         rt_uint64_t uhs_au_size: 4;
97         rt_uint64_t uhs_speed_grade: 4;
98         rt_uint64_t erase_offset: 2;
99         rt_uint64_t erase_timeout: 6;
100         rt_uint64_t erase_size: 16;
101         rt_uint64_t : 4;
102         rt_uint64_t au_size: 4;
103         rt_uint64_t performance_move: 8;
104         rt_uint64_t speed_class: 8;
105 
106         rt_uint32_t size_of_protected_area;
107 
108         rt_uint32_t sd_card_type: 16;
109         rt_uint32_t : 6;
110         rt_uint32_t : 7;
111         rt_uint32_t secured_mode: 1;
112         rt_uint32_t data_bus_width: 2;
113     };
114 };
115 
116 /*
117  * SD Speed Class
118  */
119 #define SD_SPEED_CLASS_0    0
120 #define SD_SPEED_CLASS_2    1
121 #define SD_SPEED_CLASS_4    2
122 #define SD_SPEED_CLASS_6    3
123 #define SD_SPEED_CLASS_10   4
124 
125 /*
126  * UHS Speed Grade
127  */
128 #define UHS_SPEED_GRADE_0   0
129 #define UHS_SPEED_GRADE_1   1
130 #define UHS_SPEED_GRADE_3   3
131 
132 
133 struct rt_sdio_cis {
134     rt_uint16_t     manufacturer;
135     rt_uint16_t     product;
136     rt_uint16_t     func0_blk_size;
137     rt_uint32_t     max_tran_speed;
138 };
139 
140 /*
141  * SDIO function CIS tuple (unknown to the core)
142  */
143 struct rt_sdio_function_tuple {
144     struct rt_sdio_function_tuple *next;
145     rt_uint8_t code;
146     rt_uint8_t size;
147     rt_uint8_t *data;
148 };
149 
150 struct rt_sdio_function;
151 typedef void (rt_sdio_irq_handler_t)(struct rt_sdio_function *);
152 
153 /*
154  * SDIO function devices
155  */
156 struct rt_sdio_function {
157     struct rt_mmcsd_card        *card;      /* the card this device belongs to */
158     rt_sdio_irq_handler_t   *irq_handler;   /* IRQ callback */
159     rt_uint8_t      num;        /* function number */
160 
161     rt_uint8_t      func_code;   /*  Standard SDIO Function interface code  */
162     rt_uint16_t     manufacturer;       /* manufacturer id */
163     rt_uint16_t     product;        /* product id */
164 
165     rt_uint32_t     max_blk_size;   /* maximum block size */
166     rt_uint32_t     cur_blk_size;   /* current block size */
167 
168     rt_uint32_t     enable_timeout_val; /* max enable timeout in msec */
169 
170     struct rt_sdio_function_tuple *tuples;
171 
172     void            *priv;
173 };
174 
175 #define SDIO_MAX_FUNCTIONS      7
176 
177 struct rt_mmc_ext_csd
178 {
179     rt_uint32_t cache_size;
180     rt_uint32_t enhanced_data_strobe;
181 };
182 
183 struct rt_mmcsd_card {
184     struct rt_mmcsd_host *host;
185     rt_uint32_t rca;        /* card addr */
186     rt_uint32_t resp_cid[4];    /* card CID register */
187     rt_uint32_t resp_csd[4];    /* card CSD register */
188     rt_uint32_t resp_scr[2];    /* card SCR register */
189 
190     rt_uint16_t tacc_clks;  /* data access time by ns */
191     rt_uint32_t tacc_ns;    /* data access time by clk cycles */
192     rt_uint32_t max_data_rate;  /* max data transfer rate */
193     rt_uint32_t card_capacity;  /* card capacity, unit:KB */
194     rt_uint32_t card_blksize;   /* card block size */
195     rt_uint32_t card_sec_cnt;   /* card sector count*/
196     rt_uint32_t erase_size; /* erase size in sectors */
197     rt_uint16_t card_type;
198 #define CARD_TYPE_MMC                   0 /* MMC card */
199 #define CARD_TYPE_SD                    1 /* SD card */
200 #define CARD_TYPE_SDIO                  2 /* SDIO card */
201 #define CARD_TYPE_SDIO_COMBO            3 /* SD combo (IO+mem) card */
202 
203     rt_uint16_t flags;
204 #define CARD_FLAG_HIGHSPEED  (1 << 0)   /* SDIO bus speed 50MHz */
205 #define CARD_FLAG_SDHC       (1 << 1)   /* SDHC card */
206 #define CARD_FLAG_SDXC       (1 << 2)   /* SDXC card */
207 #define CARD_FLAG_HIGHSPEED_DDR  (1 << 3)   /* HIGH SPEED DDR */
208 #define CARD_FLAG_HS200      (1 << 4)   /* BUS SPEED 200MHz */
209 #define CARD_FLAG_HS400      (1 << 5)   /* BUS SPEED 400MHz */
210 #define CARD_FLAG_SDR50      (1 << 6)   /* BUS SPEED 100MHz */
211 #define CARD_FLAG_SDR104     (1 << 7)   /* BUS SPEED 200MHz */
212 #define CARD_FLAG_DDR50      (1 << 8)   /* DDR50, works on 1.8V only */
213     struct rt_sd_scr    scr;
214     struct rt_mmcsd_csd csd;
215     rt_uint32_t     hs_max_data_rate;  /* max data transfer rate in high speed mode */
216 
217     rt_uint8_t      sdio_function_num;  /* total number of SDIO functions */
218     struct rt_sdio_cccr    cccr;  /* common card info */
219     struct rt_sdio_cis     cis;  /* common tuple info */
220     struct rt_sdio_function *sdio_function[SDIO_MAX_FUNCTIONS + 1]; /* SDIO functions (devices) */
221     void *blk_dev;
222 
223     struct rt_mmc_ext_csd ext_csd;
224 };
225 
226 #ifdef __cplusplus
227 }
228 #endif
229 
230 #endif
231