1 /*
2  * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the
12  *       distribution.
13  *    3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
14  *       its contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _DRIVER_CHIP_SDMMC_CARD_H_
31 #define _DRIVER_CHIP_SDMMC_CARD_H_
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #include "osal/os_mutex.h"
38 
39 #define CONFIG_SDIO_USE_FUNS
40 //#define CONFIG_USE_SDIO_COMBO
41 
42 //#define SD_SUPPORT_VERSION3           /* not support for not support 1V8 */ #error !!
43 //#define CONFIG_USE_MMC_QUIRK          /*  not support now */ #error !!
44 //#define CONFIG_SDIO_USE_FUNS          /* close to save code. and not support now */ #error !!
45 
46 #define SYSTEM_SIMULATION
47 
48 #ifdef CONFIG_USE_SDIO
49 struct sdio_func;
50 typedef void (sdio_irq_handler_t)(struct sdio_func *);
51 
52 /*
53  * SDIO function devices
54  */
55 struct sdio_func {
56     struct mmc_card         *card;      /* the card this device belongs to */
57     sdio_irq_handler_t      *irq_handler;   /* IRQ callback */
58     uint32_t                num;        /* function number */
59 
60     unsigned char           class;      /* standard interface class */
61     unsigned short          vendor;     /* vendor id */
62     unsigned short          device;     /* device id */
63 
64     uint32_t                max_blksize;    /* maximum block size */
65     uint32_t                cur_blksize;    /* current block size */
66 
67     uint32_t                enable_timeout; /* max enable timeout in msec */
68 
69     uint32_t                state;      /* function state */
70 #define SDIO_STATE_PRESENT  (1<<0)      /* present in sysfs */
71 
72     uint8_t                 tmpbuf[4];  /* DMA:able scratch buffer */
73 
74     unsigned                num_info;   /* number of info strings */
75     const char              **info;     /* info strings */
76 
77     struct sdio_func_tuple  *tuples;
78     /*for rtl*/
79     void *drv_prv;
80 };
81 
82 #define sdio_func_present(f)    ((f)->state & SDIO_STATE_PRESENT)
83 #define sdio_func_set_present(f) ((f)->state |= SDIO_STATE_PRESENT)
84 
85 #endif
86 
87 struct mmc_ocr {
88     union {
89         uint32_t vol_window      : 24,
90                  to_1v8_acpt     : 1,
91                                  : 5,
92                  high_capacity   : 1,
93                                  : 1;
94         uint32_t ocr;
95     };
96 };
97 
98 struct mmc_cid {
99     uint16_t   oemid;
100     uint8_t    manfid;
101     uint32_t   serial;
102     uint16_t   year;
103     uint8_t    month;
104     uint8_t    hwrev;
105     uint8_t    fwrev;
106     uint8_t    prod_name[6];
107 };
108 
109 struct mmc_csd {
110     uint8_t    csd_ver;
111     //uint8_t    c_size_mult;
112     //uint16_t   c_size;
113     uint32_t   max_dtr;     /* max transfer speed */
114     uint16_t    read_blk_len;
115     uint16_t   cmdclass;
116     uint32_t capacity;
117 };
118 
119 struct sd_scr {
120     uint8_t sda_vsn;
121     uint8_t sda_spec3;
122     uint8_t sda_spec4;
123     uint8_t sda_spec5;
124     uint8_t bus_widths;
125     uint8_t security_sup;
126 #define SD_SCR_BUS_WIDTH_1      (1<<0)
127 #define SD_SCR_BUS_WIDTH_4      (1<<2)
128     uint8_t cmds;
129 #define SD_SCR_CMD20_SUPPORT    (1<<0)
130 #define SD_SCR_CMD23_SUPPORT    (1<<1)
131 };
132 
133 struct sd_ssr {
134     uint32_t au;                    /* In sectors */
135     uint32_t erase_timeout;         /* In milliseconds */
136     uint32_t erase_offset;          /* In milliseconds */
137 };
138 
139 struct sd_switch_caps {
140     uint32_t                hs_max_dtr;
141     uint32_t                uhs_max_dtr;
142 #define HIGH_SPEED_MAX_DTR      50000000
143 #define UHS_SDR104_MAX_DTR      208000000
144 #define UHS_SDR50_MAX_DTR       100000000
145 #define UHS_DDR50_MAX_DTR       50000000
146 #define UHS_SDR25_MAX_DTR       UHS_DDR50_MAX_DTR
147 #define UHS_SDR12_MAX_DTR       25000000
148     uint32_t                sd3_bus_mode;
149 #define UHS_SDR12_BUS_SPEED     0
150 #define HIGH_SPEED_BUS_SPEED    1
151 #define UHS_SDR25_BUS_SPEED     1
152 #define UHS_SDR50_BUS_SPEED     2
153 #define UHS_SDR104_BUS_SPEED    3
154 #define UHS_DDR50_BUS_SPEED     4
155 
156 #define SD_MODE_HIGH_SPEED      (1 << HIGH_SPEED_BUS_SPEED)
157 #define SD_MODE_UHS_SDR12       (1 << UHS_SDR12_BUS_SPEED)
158 #define SD_MODE_UHS_SDR25       (1 << UHS_SDR25_BUS_SPEED)
159 #define SD_MODE_UHS_SDR50       (1 << UHS_SDR50_BUS_SPEED)
160 #define SD_MODE_UHS_SDR104      (1 << UHS_SDR104_BUS_SPEED)
161 #define SD_MODE_UHS_DDR50       (1 << UHS_DDR50_BUS_SPEED)
162     uint32_t                sd3_drv_type;
163 #define SD_DRIVER_TYPE_B        0x01
164 #define SD_DRIVER_TYPE_A        0x02
165 #define SD_DRIVER_TYPE_C        0x04
166 #define SD_DRIVER_TYPE_D        0x08
167     uint32_t                sd3_curr_limit;
168 #define SD_SET_CURRENT_LIMIT_200        0
169 #define SD_SET_CURRENT_LIMIT_400        1
170 #define SD_SET_CURRENT_LIMIT_600        2
171 #define SD_SET_CURRENT_LIMIT_800        3
172 
173 #define SD_MAX_CURRENT_200      (1 << SD_SET_CURRENT_LIMIT_200)
174 #define SD_MAX_CURRENT_400      (1 << SD_SET_CURRENT_LIMIT_400)
175 #define SD_MAX_CURRENT_600      (1 << SD_SET_CURRENT_LIMIT_600)
176 #define SD_MAX_CURRENT_800      (1 << SD_SET_CURRENT_LIMIT_800)
177 };
178 
179 struct mmc_ext_csd {
180     uint8_t    version;
181     uint8_t    card_type;
182     uint8_t    csd_struc;
183     uint8_t    hs_timing;
184     uint8_t    bus_width;
185     uint8_t    part_config;
186     uint8_t    boot_bus_cond;
187 };
188 
189 struct sdio_cccr {
190     uint32_t        sdio_vsn;
191     uint32_t        sd_vsn;
192     uint32_t        multi_block:1,
193                     low_speed:1,
194                     wide_bus:1,
195                     high_power:1,
196                     high_speed:1,
197                     disable_cd:1;
198 };
199 
200 struct sdio_cis {
201     uint16_t        vendor;
202     uint16_t        device;
203     uint16_t        blksize;
204     uint32_t        max_dtr;
205 };
206 
207 struct mmc_host;
208 
209 #define SDIO_MAX_FUNCS      7
210 
211 /** @bried SD Card Init Structure definition. */
212 typedef struct {
213     uint16_t                debug_mask;
214     uint16_t                type;           /* set card type if we know to speed up scan card, MMC_TYPE_xx */
215 } SDCard_InitTypeDef;
216 
217 struct mmc_card {
218     uint16_t                debug_mask;
219     uint16_t                suspend;
220 //#if ((defined CONFIG_USE_SD) || (defined CONFIG_USE_MMC))
221     /*  register info. */
222     struct mmc_cid          cid;
223     struct mmc_csd          csd;
224     struct sd_scr           scr;
225     struct sd_ssr           ssr;
226     struct mmc_ext_csd      extcsd;
227     struct sd_switch_caps   sw_caps;                /* switch (CMD6) caps */
228 //#endif
229     /*  card information  */
230     uint32_t                id;
231     uint32_t                type;                   /* card type */
232 #define MMC_TYPE_MMC                    1               /* MMC card */
233 #define MMC_TYPE_SD                     2               /* SD card */
234 #define MMC_TYPE_SDIO                   3               /* SDIO card */
235 #define MMC_TYPE_SD_COMBO               4               /* SD combo (IO+mem) card */
236 #define MMC_TYPE_MAX                    5
237 
238     uint32_t                sd_bus_speed;           /* Bus Speed Mode set for the card */
239     uint32_t                state;                  /* (our) card state */
240 #define MMC_STATE_PRESENT               (1 << 0)        /* present */
241 #define MMC_STATE_READONLY              (1 << 1)        /* card is read-only */
242 #define MMC_STATE_HIGHSPEED             (1 << 2)        /* card is in high speed mode */
243 #define MMC_STATE_BLOCKADDR             (1 << 3)        /* card uses block-addressing */
244 #define MMC_STATE_HIGHSPEED_DDR         (1 << 4)        /* card is in high speed mode */
245 
246 #define MMC_STATE_ULTRAHIGHSPEED        (1<<5)          /* card is in ultra high speed mode */
247 #define MMC_CARD_SDXC                   (1<<6)          /* card is SDXC */
248 #define MMC_CARD_REMOVED                (1<<7)          /* card has been removed */
249 #define MMC_STATE_HIGHSPEED_200         (1<<8)          /* card is in HS200 mode */
250 #define MMC_STATE_SLEEP                 (1<<9)          /* card is in sleep state */
251 //#ifdef CONFIG_USE_MMC_QUIRK
252     uint32_t quirks;                                /* card quirks */
253 #define MMC_QUIRK_LENIENT_FN0           (1 << 0)        /* allow SDIO FN0 writes outside of the VS CCCR range */
254 #define MMC_QUIRK_BLKSZ_FOR_BYTE_MODE   (1 << 1)        /* use func->cur_blksize */
255 /* for byte mode */
256 #define MMC_QUIRK_NONSTD_SDIO           (1 << 2)        /* non-standard SDIO card attached */
257                                                         /* (missing CIA registers) */
258 #define MMC_QUIRK_BROKEN_CLK_GATING     (1<<3)          /* clock gating the sdio bus will make card fail */
259 #define MMC_QUIRK_NONSTD_FUNC_IF        (1<<4)          /* SDIO card has nonstd function interfaces */
260 #define MMC_QUIRK_DISABLE_CD            (1<<5)          /* disconnect CD/DAT[3] resistor */
261 #define MMC_QUIRK_INAND_CMD38           (1<<6)          /* iNAND devices have broken CMD38 */
262 #define MMC_QUIRK_BLK_NO_CMD23          (1<<7)          /* Avoid CMD23 for regular multiblock */
263 #define MMC_QUIRK_BROKEN_BYTE_MODE_512  (1<<8)          /* Avoid sending 512 bytes in */
264 #define MMC_QUIRK_LONG_READ_TIME        (1<<9)          /* Data read time > CSD says */
265 #define MMC_QUIRK_SEC_ERASE_TRIM_BROKEN (1<<10)         /* Skip secure for erase/trim */
266 //#endif
267 /* missing CIA registers */
268 
269 //#ifdef CONFIG_SDIO_USE_FUNS
270     uint32_t erase_size;                            /* erase size in sectors */
271     uint32_t erase_shift;                           /* if erase unit is power 2 */
272     uint32_t pref_erase;                            /* in sectors */
273     uint8_t erased_byte;                            /* value of erased bytes */
274 
275     uint32_t raw_cid[4];                            /* raw card CID */
276     uint32_t raw_csd[4];                            /* raw card CSD */
277     uint32_t raw_scr[2];                            /* raw card SCR */
278     uint32_t sdio_funcs;                            /* number of SDIO functions */
279     struct sdio_func *sdio_func[SDIO_MAX_FUNCS];    /* SDIO functions (devices) */
280     uint32_t num_info;                              /* number of info strings */
281     const int8_t            **info;                 /* info strings */
282 //#ifdef CONFIG_SDIO_IRQ_SUPPORT
283 //  sdio_irq_handler_t      *sdio_single_irq;                /* IRQ callback, all funs only support one for efficiency! */
284     struct sdio_func    *sdio_single_irq; /* SDIO function when only one IRQ active */
285 //#endif
286 //#endif
287 #ifdef CONFIG_USE_SDIO
288     uint32_t       manfid;
289     uint32_t       cisptr[8];
290     uint16_t       fn_bsize[8];
291     void           *mem_info_p;  //if with memory, to store information about memory portion
292     //struct sdio_func        *sdio_func[SDIO_MAX_FUNCS]; /* SDIO functions (devices) */
293     struct sdio_cccr        cccr;           /* common card info */
294     struct sdio_cis         cis;            /* common tuple info */
295 
296     struct sdio_func_tuple  *tuples;        /* unknown common tuples */
297 #endif
298     uint8_t         bus_width;
299     uint8_t         speed_class;
300     uint16_t        ref;
301     OS_Mutex_t      mutex;
302     uint32_t        cidno[4];
303 
304     uint32_t        rca;                           /* relative card address of device */
305     struct mmc_ocr  ocr;
306     struct mmc_host *host;                         /* the host this device belongs to */
307 };
308 
309 struct mmc_card_info {
310     struct mmc_card card;
311     struct sdio_func sdio_func[SDIO_MAX_FUNCS];    /* SDIO functions (devices) */
312     //const int8_t            **info;                 /* info strings */
313     //struct sdio_func  *sdio_single_irq; /* SDIO function when only one IRQ active */
314     //void           *mem_info_p;  //if with memory, to store information about memory portion
315 #ifdef CONFIG_USE_SDIO
316     //struct sdio_func_tuple  *tuples;        /* unknown common tuples */
317 #endif
318     uint32_t    sdc_id;
319 };
320 
321 #define mmc_card_mmc(c)         ((c)->type == MMC_TYPE_MMC)
322 #define mmc_card_sd(c)          ((c)->type == MMC_TYPE_SD)
323 #define mmc_card_sdio(c)        ((c)->type == MMC_TYPE_SDIO)
324 #define mmc_card_sd_combo(c)    ((c)->type == MMC_TYPE_SD_COMBO)
325 
326 #define mmc_card_present(c)     ((c)->state & MMC_STATE_PRESENT)
327 #define mmc_card_readonly(c)    ((c)->state & MMC_STATE_READONLY)
328 #define mmc_card_highspeed(c)   ((c)->state & MMC_STATE_HIGHSPEED)
329 #define mmc_card_hs200(c)       ((c)->state & MMC_STATE_HIGHSPEED_200)
330 #define mmc_card_blockaddr(c)   ((c)->state & MMC_STATE_BLOCKADDR)
331 #define mmc_card_ddr_mode(c)    ((c)->state & MMC_STATE_HIGHSPEED_DDR)
332 #define mmc_card_uhs(c)         ((c)->state & MMC_STATE_ULTRAHIGHSPEED)
333 #define mmc_sd_card_uhs(c)      ((c)->state & MMC_STATE_ULTRAHIGHSPEED)
334 #define mmc_card_ext_capacity(c) ((c)->state & MMC_CARD_SDXC)
335 #define mmc_card_removed(c)     ((c) && ((c)->state & MMC_CARD_REMOVED))
336 #define mmc_card_is_sleep(c)    ((c)->state & MMC_STATE_SLEEP)
337 
338 #define mmc_card_set_present(c) ((c)->state |= MMC_STATE_PRESENT)
339 #define mmc_card_set_readonly(c) ((c)->state |= MMC_STATE_READONLY)
340 #define mmc_card_set_highspeed(c) ((c)->state |= MMC_STATE_HIGHSPEED)
341 #define mmc_card_set_hs200(c)   ((c)->state |= MMC_STATE_HIGHSPEED_200)
342 #define mmc_card_set_blockaddr(c) ((c)->state |= MMC_STATE_BLOCKADDR)
343 #define mmc_card_set_ddr_mode(c) ((c)->state |= MMC_STATE_HIGHSPEED_DDR)
344 #define mmc_card_set_uhs(c) ((c)->state |= MMC_STATE_ULTRAHIGHSPEED)
345 #define mmc_sd_card_set_uhs(c) ((c)->state |= MMC_STATE_ULTRAHIGHSPEED)
346 #define mmc_card_set_ext_capacity(c) ((c)->state |= MMC_CARD_SDXC)
347 #define mmc_card_set_removed(c) ((c)->state |= MMC_CARD_REMOVED)
348 #define mmc_card_set_sleep(c)   ((c)->state |= MMC_STATE_SLEEP)
349 
350 #define mmc_card_clr_sleep(c)   ((c)->state &= ~MMC_STATE_SLEEP)
351 
352 #ifdef __cplusplus
353 }
354 #endif
355 
356 #endif /* _DRIVER_CHIP_SDMMC_CARD_H_ */
357