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_SDIO_H_
31 #define _DRIVER_CHIP_SDMMC_SDIO_H_
32
33 #include "card.h"
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 #undef readl
40 #undef writel
41
42 #ifndef printk
43 #define printk rt_kprintf
44 #endif
45
46 #define readl hal_readl
47 #define writel hal_writel
48
49 #ifdef CONFIG_USE_SDIO
50 /*
51 * SDIO function CIS tuple (unknown to the core)
52 */
53 struct sdio_func_tuple {
54 struct sdio_func_tuple *next;
55 unsigned char code;
56 unsigned char size;
57 unsigned char data[0];
58 };
59
60 //#define SDIO_DEBUG
61
62 #define SDIO_EXCLUSIVE_HOST
63
64 /* SDIO commands type argument response */
65 #define SD_IO_SEND_OP_COND 5 /* bcr [23:0] OCR R4 */
66 #define SD_IO_RW_DIRECT 52 /* ac [31:0] See below R5 */
67 #define SD_IO_RW_EXTENDED 53 /* adtc [31:0] See below R5 */
68
69 /*
70 * SD_IO_RW_DIRECT argument format:
71 *
72 * [31] R/W flag
73 * [30:28] Function number
74 * [27] RAW flag
75 * [25:9] Register address
76 * [7:0] Data
77 */
78
79 /*
80 * SD_IO_RW_EXTENDED argument format:
81 *
82 * [31] R/W flag
83 * [30:28] Function number
84 * [27] Block mode
85 * [26] Increment address
86 * [25:9] Register address
87 * [8:0] Byte/block count
88 */
89
90 #define R4_18V_PRESENT (1<<24)
91 #define R4_MEMORY_PRESENT (1 << 27)
92
93 //#define CONFIG_SDIO_IRQ_SUPPORT
94
95 /*
96 SDIO status in R5
97 Type
98 e : error bit
99 s : status bit
100 r : detected and set for the actual command response
101 x : detected and set during command execution. the host must poll
102 the card by sending status command in order to read these bits.
103 Clear condition
104 a : according to the card state
105 b : always related to the previous command. Reception of
106 a valid command will clear it (with a delay of one command)
107 c : clear by read
108 */
109
110 #define R5_COM_CRC_ERROR (1 << 15) /* er, b */
111 #define R5_ILLEGAL_COMMAND (1 << 14) /* er, b */
112 #define R5_ERROR (1 << 11) /* erx, c */
113 #define R5_FUNCTION_NUMBER (1 << 9) /* er, c */
114 #define R5_OUT_OF_RANGE (1 << 8) /* er, c */
115 #define R5_STATUS(x) (x & 0xCB00)
116 #define R5_IO_CURRENT_STATE(x) ((x & 0x3000) >> 12) /* s, b */
117
118 /*
119 * Card Common Control Registers (CCCR)
120 */
121
122 #define SDIO_CCCR_CCCR 0x00
123
124 #define SDIO_CCCR_REV_1_00 0 /* CCCR/FBR Version 1.00 */
125 #define SDIO_CCCR_REV_1_10 1 /* CCCR/FBR Version 1.10 */
126 #define SDIO_CCCR_REV_1_20 2 /* CCCR/FBR Version 1.20 */
127 #define SDIO_CCCR_REV_3_00 3 /* CCCR/FBR Version 3.00 */
128
129 #define SDIO_SDIO_REV_1_00 0 /* SDIO Spec Version 1.00 */
130 #define SDIO_SDIO_REV_1_10 1 /* SDIO Spec Version 1.10 */
131 #define SDIO_SDIO_REV_1_20 2 /* SDIO Spec Version 1.20 */
132 #define SDIO_SDIO_REV_2_00 3 /* SDIO Spec Version 2.00 */
133 #define SDIO_SDIO_REV_3_00 4 /* SDIO Spec Version 3.00 */
134
135 #define SDIO_CCCR_SD 0x01
136
137 #define SDIO_SD_REV_1_01 0 /* SD Physical Spec Version 1.01 */
138 #define SDIO_SD_REV_1_10 1 /* SD Physical Spec Version 1.10 */
139 #define SDIO_SD_REV_2_00 2 /* SD Physical Spec Version 2.00 */
140 #define SDIO_SD_REV_3_00 3 /* SD Physical Spev Version 3.00 */
141
142 #define SDIO_CCCR_IOEx 0x02
143 #define SDIO_CCCR_IORx 0x03
144 #define SDIO_CCCR_IENx 0x04 /* Function/Master Interrupt Enable */
145 #define SDIO_CCCR_INTx 0x05 /* Function Interrupt Pending */
146 #define SDIO_CCCR_ABORT 0x06 /* function abort/card reset */
147 #define SDIO_CCCR_IF 0x07 /* bus interface controls */
148
149 #define SDIO_BUS_WIDTH_1BIT 0x00
150 #define SDIO_BUS_WIDTH_4BIT 0x02
151 #define SDIO_BUS_ECSI 0x20 /* Enable continuous SPI interrupt */
152 #define SDIO_BUS_SCSI 0x40 /* Support continuous SPI interrupt */
153
154 #define SDIO_BUS_ASYNC_INT 0x20
155
156 #define SDIO_BUS_CD_DISABLE 0x80 /* disable pull-up on DAT3 (pin 1) */
157
158 #define SDIO_CCCR_CAPS 0x08
159
160 #define SDIO_CCCR_CAP_SDC 0x01 /* can do CMD52 while data transfer */
161 #define SDIO_CCCR_CAP_SMB 0x02 /* can do multi-block xfers (CMD53) */
162 #define SDIO_CCCR_CAP_SRW 0x04 /* supports read-wait protocol */
163 #define SDIO_CCCR_CAP_SBS 0x08 /* supports suspend/resume */
164 #define SDIO_CCCR_CAP_S4MI 0x10 /* interrupt during 4-bit CMD53 */
165 #define SDIO_CCCR_CAP_E4MI 0x20 /* enable ints during 4-bit CMD53 */
166 #define SDIO_CCCR_CAP_LSC 0x40 /* low speed card */
167 #define SDIO_CCCR_CAP_4BLS 0x80 /* 4 bit low speed card */
168
169 #define SDIO_CCCR_CIS 0x09 /* common CIS pointer (3 bytes) */
170
171 /* Following 4 regs are valid only if SBS is set */
172 #define SDIO_CCCR_SUSPEND 0x0c
173 #define SDIO_CCCR_SELx 0x0d
174 #define SDIO_CCCR_EXECx 0x0e
175 #define SDIO_CCCR_READYx 0x0f
176
177 #define SDIO_CCCR_BLKSIZE 0x10
178
179 #define SDIO_CCCR_POWER 0x12
180
181 #define SDIO_POWER_SMPC 0x01 /* Supports Master Power Control */
182 #define SDIO_POWER_EMPC 0x02 /* Enable Master Power Control */
183
184 #define SDIO_CCCR_SPEED 0x13
185
186 #define SDIO_SPEED_SHS 0x01 /* Supports High-Speed mode */
187 #define SDIO_SPEED_BSS_SHIFT 1
188 #define SDIO_SPEED_BSS_MASK (7<<SDIO_SPEED_BSS_SHIFT)
189 #define SDIO_SPEED_SDR12 (0<<SDIO_SPEED_BSS_SHIFT)
190 #define SDIO_SPEED_SDR25 (1<<SDIO_SPEED_BSS_SHIFT)
191 #define SDIO_SPEED_SDR50 (2<<SDIO_SPEED_BSS_SHIFT)
192 #define SDIO_SPEED_SDR104 (3<<SDIO_SPEED_BSS_SHIFT)
193 #define SDIO_SPEED_DDR50 (4<<SDIO_SPEED_BSS_SHIFT)
194 #define SDIO_SPEED_EHS SDIO_SPEED_SDR25 /* Enable High-Speed */
195
196 #define SDIO_CCCR_UHS 0x14
197 #define SDIO_UHS_SDR50 0x01
198 #define SDIO_UHS_SDR104 0x02
199 #define SDIO_UHS_DDR50 0x04
200
201 #define SDIO_CCCR_DRIVE_STRENGTH 0x15
202 #define SDIO_SDTx_MASK 0x07
203 #define SDIO_DRIVE_SDTA (1<<0)
204 #define SDIO_DRIVE_SDTC (1<<1)
205 #define SDIO_DRIVE_SDTD (1<<2)
206 #define SDIO_DRIVE_DTSx_MASK 0x03
207 #define SDIO_DRIVE_DTSx_SHIFT 4
208 #define SDIO_DTSx_SET_TYPE_B (0 << SDIO_DRIVE_DTSx_SHIFT)
209 #define SDIO_DTSx_SET_TYPE_A (1 << SDIO_DRIVE_DTSx_SHIFT)
210 #define SDIO_DTSx_SET_TYPE_C (2 << SDIO_DRIVE_DTSx_SHIFT)
211 #define SDIO_DTSx_SET_TYPE_D (3 << SDIO_DRIVE_DTSx_SHIFT)
212
213 /*
214 * Function Basic Registers (FBR)
215 */
216 #define SDIO_FBR_BASE(f) ((f) * 0x100) /* base of function f's FBRs */
217
218 #define SDIO_FBR_STD_IF 0x00
219
220 #define SDIO_FBR_SUPPORTS_CSA 0x40 /* supports Code Storage Area */
221 #define SDIO_FBR_ENABLE_CSA 0x80 /* enable Code Storage Area */
222
223 #define SDIO_FBR_STD_IF_EXT 0x01
224 #define SDIO_FBR_POWER 0x02
225 #define SDIO_FBR_POWER_SPS 0x01 /* Supports Power Selection */
226 #define SDIO_FBR_POWER_EPS 0x02 /* Enable (low) Power Selection */
227 #define SDIO_FBR_CIS 0x09 /* CIS pointer (3 bytes) */
228 #define SDIO_FBR_CSA 0x0C /* CSA pointer (3 bytes) */
229 #define SDIO_FBR_CSA_DATA 0x0F
230 #define SDIO_FBR_BLKSIZE 0x10 /* block size (2 bytes) */
231
232 /* Misc. helper definitions */
233 #define FN(x) (x)
234 #define FN0 FN(0)
235 #define FN1 FN(1)
236 #define FN2 FN(2)
237 #define FN3 FN(3)
238 #define FN4 FN(4)
239 #define FN5 FN(5)
240 #define FN6 FN(6)
241 #define FN7 FN(7)
242
243 struct mmc_card;
244
245 typedef struct mmc_card sdio_t;
246
247 /**
248 * sdio_readb - read a single byte from a SDIO function
249 * @card: SDIO to access
250 * @addr: address to read
251 * @err_ret: optional status value from transfer
252 *
253 * Reads a single byte from the address space of a given SDIO
254 * function. If there is a problem reading the address, 0xff
255 * is returned and @err_ret will contain the error code.
256 */
257 extern uint8_t
258 sdio_readb(struct mmc_card *card, uint32_t func_num, uint32_t addr,
259 int32_t *err_ret);
260
261 /**
262 * sdio_writeb - write a single byte to a SDIO function
263 * @card: SDIO to access
264 * @b: byte to write
265 * @addr: address to write to
266 * @err_ret: optional status value from transfer
267 *
268 * Writes a single byte to the address space of a given SDIO
269 * function. @err_ret will contain the status of the actual
270 * transfer.
271 */
272 extern void
273 sdio_writeb(struct mmc_card *card, uint32_t func_num, const uint8_t b,
274 uint32_t addr, int32_t *err_ret);
275
276 /**
277 * sdio_readw - read a 16 bit integer from a SDIO function
278 * @func: SDIO function to access
279 * @addr: address to read
280 * @err_ret: optional status value from transfer
281 *
282 * Reads a 16 bit integer from the address space of a given SDIO
283 * function. If there is a problem reading the address, 0xffff
284 * is returned and @err_ret will contain the error code.
285 */
286 extern uint16_t sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret);
287 /**
288 * sdio_writew - write a 16 bit integer to a SDIO function
289 * @func: SDIO function to access
290 * @b: integer to write
291 * @addr: address to write to
292 * @err_ret: optional status value from transfer
293 *
294 * Writes a 16 bit integer to the address space of a given SDIO
295 * function. @err_ret will contain the status of the actual
296 * transfer.
297 */
298 extern void sdio_writew(struct sdio_func *func, uint16_t b, unsigned int addr, int *err_ret);
299 /**
300 * sdio_readl - read a 32 bit integer from a SDIO function
301 * @func: SDIO function to access
302 * @addr: address to read
303 * @err_ret: optional status value from transfer
304 *
305 * Reads a 32 bit integer from the address space of a given SDIO
306 * function. If there is a problem reading the address,
307 * 0xffffffff is returned and @err_ret will contain the error
308 * code.
309 */
310 extern uint32_t sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret);
311 /**
312 * sdio_writel - write a 32 bit integer to a SDIO function
313 * @func: SDIO function to access
314 * @b: integer to write
315 * @addr: address to write to
316 * @err_ret: optional status value from transfer
317 *
318 * Writes a 32 bit integer to the address space of a given SDIO
319 * function. @err_ret will contain the status of the actual
320 * transfer.
321 */
322 extern void sdio_writel(struct sdio_func *func, uint32_t b, unsigned int addr, int *err_ret);
323
324 /**
325 * memcpy_fromio - read a chunk of memory from a SDIO function
326 * @dst: buffer to store the data
327 * @addr: address to begin reading from
328 * @count: number of bytes to read
329 *
330 * Reads from the address space of a given SDIO function. Return
331 * value indicates if the transfer succeeded or not.
332 */
333 extern int
334 sdio_memcpy_fromio(struct mmc_card *card, unsigned int func_num, void *dst,
335 unsigned int addr, int count);
336
337 /**
338 * memcpy_toio - write a chunk of memory to a SDIO function
339 * @addr: address to start writing to
340 * @src: buffer that contains the data to write
341 * @count: number of bytes to write
342 *
343 * Writes to the address space of a given SDIO function. Return
344 * value indicates if the transfer succeeded or not.
345 */
346 extern int
347 sdio_memcpy_toio(struct mmc_card *card, unsigned int func_num, unsigned int addr,
348 const void *src, int count);
349
350 /**
351 * sdio_claim_irq - claim the IRQ for a SDIO function
352 * @card: SDIO card
353 * @func_num: function num
354 * @handler: IRQ handler callback
355 *
356 * Claim and activate the IRQ for the given SDIO function. The provided
357 * handler will be called when that IRQ is asserted. The host is always
358 * claimed already when the handler is called so the handler must not
359 * call sdio_claim_host() nor sdio_release_host().
360 */
361 //extern int sdio_claim_irq(struct mmc_card *card, unsigned int func_num,
362 // sdio_irq_handler_t *handler);
363 extern int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler);
364
365 /**
366 * sdio_release_irq - release the IRQ for a SDIO function
367 * @card: SDIO card
368 * @func_num: function num
369 *
370 * Disable and release the IRQ for the given SDIO function.
371 */
372 //extern int sdio_release_irq(struct mmc_card *card, unsigned int func_num);
373 extern int sdio_release_irq(struct sdio_func *func);
374
375 /**
376 * sdio_align_size - pads a transfer size to a more optimal value
377 * @func: SDIO function
378 * @sz: original transfer size
379 *
380 * Pads the original data size with a number of extra bytes in
381 * order to avoid controller bugs and/or performance hits
382 * (e.g. some controllers revert to PIO for certain sizes).
383 *
384 * If possible, it will also adjust the size so that it can be
385 * handled in just a single request.
386 *
387 * Returns the improved size, which might be unmodified.
388 */
389 //unsigned int sdio_align_size(struct mmc_card *card, unsigned int sz);
390 unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz);
391
392 /**
393 * claim_host - exclusively claim a bus for a certain SDIO function
394 * @func: SDIO function that will be accessed
395 *
396 * Claim a bus for a set of operations. The SDIO function given
397 * is used to figure out which bus is relevant.
398 */
399 #ifdef SDIO_EXCLUSIVE_HOST
400 extern void sdio_claim_host(struct mmc_card *card);
401 #else
sdio_claim_host(struct mmc_card * card)402 static inline void sdio_claim_host(struct mmc_card *card) {
403 #ifdef SDIO_DEBUG
404 if (card->_bus_refs)
405 BUG_ON(1); /* check when need debug */
406 card->_bus_refs++;
407 #endif
408 }
409 #endif
410
411 /**
412 * release_host - release a bus for a certain SDIO function
413 * @func: SDIO function that was accessed
414 *
415 * Release a bus, allowing others to claim the bus for their
416 * operations.
417 */
418 #ifdef SDIO_EXCLUSIVE_HOST
419 extern void sdio_release_host(struct mmc_card *card);
420 #else
sdio_release_host(struct mmc_card * card)421 static inline void sdio_release_host(struct mmc_card *card) {
422 #ifdef SDIO_DEBUG
423 card->_bus_refs--;
424 if (card->_bus_refs)
425 BUG_ON(1); /* check when need debug */
426 #endif
427 }
428 #endif
429
sdio_lock(struct mmc_card * card)430 static inline void sdio_lock(struct mmc_card *card) {
431 sdio_claim_host(card);
432 }
433
sdio_unlock(struct mmc_card * card)434 static inline void sdio_unlock(struct mmc_card *card) {
435 sdio_release_host(card);
436 }
437
438 extern int32_t sdio_set_block_size(struct mmc_card *card, uint32_t fn_num,
439 uint32_t blksz);
440 extern int32_t sdio_enable_func(struct mmc_card *card, uint32_t func_num);
441 extern int32_t sdio_disable_func(struct mmc_card *card, uint32_t func_num);
442 extern int32_t sdio_pm(sdio_t *card, int32_t suspend);
443
444 extern void sdio_test(void);
445 extern int mmc_io_rw_direct(struct mmc_card *card, int32_t write, uint32_t fn, uint32_t addr, uint8_t in, uint8_t *out);
446
447 extern struct sdio_func ** get_mmc_card_func(uint8_t card_id);
448
449 #endif /* CONFIG_USE_SDIO */
450
451 #ifdef __cplusplus
452 }
453 #endif
454
455 #endif /* _DRIVER_CHIP_SDMMC_SDIO_H_ */
456