LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sdmmc_18xx_43xx.h
Go to the documentation of this file.
1 /*
2  * @brief LPC18xx/43xx SD/SDIO driver
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #ifndef __SDMMC_18XX_43XX_H_
33 #define __SDMMC_18XX_43XX_H_
34 
35 #include "chip.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
46 /* SD/MMC commands - this matrix shows the command, response types, and
47  supported card type for that command.
48  Command Number Resp SD MMC
49  ----------------------- ------ ----- --- ---
50  Reset (go idle) CMD0 NA x x
51  Send op condition CMD1 R3 x
52  All send CID CMD2 R2 x x
53  Send relative address CMD3 R1 x
54  Send relative address CMD3 R6 x
55  Program DSR CMD4 NA x
56  Select/deselect card CMD7 R1b x
57  Select/deselect card CMD7 R1 x
58  Send CSD CMD9 R2 x x
59  Send CID CMD10 R2 x x
60  Read data until stop CMD11 R1 x x
61  Stop transmission CMD12 R1/b x x
62  Send status CMD13 R1 x x
63  Go inactive state CMD15 NA x x
64  Set block length CMD16 R1 x x
65  Read single block CMD17 R1 x x
66  Read multiple blocks CMD18 R1 x x
67  Write data until stop CMD20 R1 x
68  Setblock count CMD23 R1 x
69  Write single block CMD24 R1 x x
70  Write multiple blocks CMD25 R1 x x
71  Program CID CMD26 R1 x
72  Program CSD CMD27 R1 x x
73  Set write protection CMD28 R1b x x
74  Clear write protection CMD29 R1b x x
75  Send write protection CMD30 R1 x x
76  Erase block start CMD32 R1 x
77  Erase block end CMD33 R1 x
78  Erase block start CMD35 R1 x
79  Erase block end CMD36 R1 x
80  Erase blocks CMD38 R1b x
81  Fast IO CMD39 R4 x
82  Go IRQ state CMD40 R5 x
83  Lock/unlock CMD42 R1b x
84  Application command CMD55 R1 x
85  General command CMD56 R1b x
86 
87  *** SD card application commands - these must be preceded with ***
88  *** MMC CMD55 application specific command first ***
89  Set bus width ACMD6 R1 x
90  Send SD status ACMD13 R1 x
91  Send number WR blocks ACMD22 R1 x
92  Set WR block erase cnt ACMD23 R1 x
93  Send op condition ACMD41 R3 x
94  Set clear card detect ACMD42 R1 x
95  Send CSR ACMD51 R1 x */
96 
99 typedef enum {
136 
140 typedef enum {
149 } SD_APP_CMD_T;
150 
153 typedef enum {
163 
166 typedef enum {
176 } SDMMC_STATE_T;
177 
178 /* Standard MMC commands (3.1) type argument response */
179 /* class 1 */
180 #define MMC_GO_IDLE_STATE 0 /* bc */
181 #define MMC_SEND_OP_COND 1 /* bcr [31:0] OCR R3 */
182 #define MMC_ALL_SEND_CID 2 /* bcr R2 */
183 #define MMC_SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */
184 #define MMC_SET_DSR 4 /* bc [31:16] RCA */
185 #define MMC_SELECT_CARD 7 /* ac [31:16] RCA R1 */
186 #define MMC_SEND_EXT_CSD 8 /* bc R1 */
187 #define MMC_SEND_CSD 9 /* ac [31:16] RCA R2 */
188 #define MMC_SEND_CID 10 /* ac [31:16] RCA R2 */
189 #define MMC_STOP_TRANSMISSION 12 /* ac R1b */
190 #define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
191 #define MMC_GO_INACTIVE_STATE 15 /* ac [31:16] RCA */
192 
193 /* class 2 */
194 #define MMC_SET_BLOCKLEN 16 /* ac [31:0] block len R1 */
195 #define MMC_READ_SINGLE_BLOCK 17 /* adtc [31:0] data addr R1 */
196 #define MMC_READ_MULTIPLE_BLOCK 18 /* adtc [31:0] data addr R1 */
197 
198 /* class 3 */
199 #define MMC_WRITE_DAT_UNTIL_STOP 20 /* adtc [31:0] data addr R1 */
200 
201 /* class 4 */
202 #define MMC_SET_BLOCK_COUNT 23 /* adtc [31:0] data addr R1 */
203 #define MMC_WRITE_BLOCK 24 /* adtc [31:0] data addr R1 */
204 #define MMC_WRITE_MULTIPLE_BLOCK 25 /* adtc R1 */
205 #define MMC_PROGRAM_CID 26 /* adtc R1 */
206 #define MMC_PROGRAM_CSD 27 /* adtc R1 */
207 
208 /* class 6 */
209 #define MMC_SET_WRITE_PROT 28 /* ac [31:0] data addr R1b */
210 #define MMC_CLR_WRITE_PROT 29 /* ac [31:0] data addr R1b */
211 #define MMC_SEND_WRITE_PROT 30 /* adtc [31:0] wpdata addr R1 */
212 
213 /* class 5 */
214 #define MMC_ERASE_GROUP_START 35 /* ac [31:0] data addr R1 */
215 #define MMC_ERASE_GROUP_END 36 /* ac [31:0] data addr R1 */
216 #define MMC_ERASE 37 /* ac R1b */
217 
218 /* class 9 */
219 #define MMC_FAST_IO 39 /* ac <Complex> R4 */
220 #define MMC_GO_IRQ_STATE 40 /* bcr R5 */
221 
222 /* class 7 */
223 #define MMC_LOCK_UNLOCK 42 /* adtc R1b */
224 
225 /* class 8 */
226 #define MMC_APP_CMD 55 /* ac [31:16] RCA R1 */
227 #define MMC_GEN_CMD 56 /* adtc [0] RD/WR R1b */
228 
229 /* SD commands type argument response */
230 /* class 8 */
231 /* This is basically the same command as for MMC with some quirks. */
232 #define SD_SEND_RELATIVE_ADDR 3 /* ac R6 */
233 #define SD_CMD8 8 /* bcr [31:0] OCR R3 */
234 
235 /* Application commands */
236 #define SD_APP_SET_BUS_WIDTH 6 /* ac [1:0] bus width R1 */
237 #define SD_APP_OP_COND 41 /* bcr [31:0] OCR R1 (R4) */
238 #define SD_APP_SEND_SCR 51 /* adtc R1 */
239 
240 /*
241  MMC status in R1
242  Type
243  e : error bit
244  s : status bit
245  r : detected and set for the actual command response
246  x : detected and set during command execution. the host must poll
247  the card by sending status command in order to read these bits.
248  Clear condition
249  a : according to the card state
250  b : always related to the previous command. Reception of
251  a valid command will clear it (with a delay of one command)
252  c : clear by read
253  */
254 
255 #define R1_OUT_OF_RANGE (1UL << 31) /* er, c */
256 #define R1_ADDRESS_ERROR (1 << 30) /* erx, c */
257 #define R1_BLOCK_LEN_ERROR (1 << 29) /* er, c */
258 #define R1_ERASE_SEQ_ERROR (1 << 28) /* er, c */
259 #define R1_ERASE_PARAM (1 << 27) /* ex, c */
260 #define R1_WP_VIOLATION (1 << 26) /* erx, c */
261 #define R1_CARD_IS_LOCKED (1 << 25) /* sx, a */
262 #define R1_LOCK_UNLOCK_FAILED (1 << 24) /* erx, c */
263 #define R1_COM_CRC_ERROR (1 << 23) /* er, b */
264 #define R1_ILLEGAL_COMMAND (1 << 22) /* er, b */
265 #define R1_CARD_ECC_FAILED (1 << 21) /* ex, c */
266 #define R1_CC_ERROR (1 << 20) /* erx, c */
267 #define R1_ERROR (1 << 19) /* erx, c */
268 #define R1_UNDERRUN (1 << 18) /* ex, c */
269 #define R1_OVERRUN (1 << 17) /* ex, c */
270 #define R1_CID_CSD_OVERWRITE (1 << 16) /* erx, c, CID/CSD overwrite */
271 #define R1_WP_ERASE_SKIP (1 << 15) /* sx, c */
272 #define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
273 #define R1_ERASE_RESET (1 << 13) /* sr, c */
274 #define R1_STATUS(x) (x & 0xFFFFE000)
275 #define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
276 #define R1_READY_FOR_DATA (1 << 8)/* sx, a */
277 #define R1_APP_CMD (1 << 5)/* sr, c */
278 
279 #define OCR_ALL_READY (1UL << 31) /* Card Power up status bit */
280 #define OCR_HC_CCS (1 << 30) /* High capacity card */
281 #define OCR_VOLTAGE_RANGE_MSK 0x00ff8000
282 
283 #define SD_SEND_IF_ARG 0x000001AA
284 #define SD_SEND_IF_ECHO_MSK 0x000000FF
285 #define SD_SEND_IF_RESP 0x000000AA
286 
287 #define CMD_MASK_RESP (0x3UL << 28)
288 #define CMD_RESP(r) (((r) & 0x3) << 28)
289 #define CMD_RESP_R0 (0 << 28)
290 #define CMD_RESP_R1 (1 << 28)
291 #define CMD_RESP_R2 (2 << 28)
292 #define CMD_RESP_R3 (3 << 28)
293 #define CMD_BIT_AUTO_STOP (1 << 24)
294 #define CMD_BIT_APP (1 << 23)
295 #define CMD_BIT_INIT (1 << 22)
296 #define CMD_BIT_BUSY (1 << 21)
297 #define CMD_BIT_LS (1 << 20) /* Low speed, used during acquire */
298 #define CMD_BIT_DATA (1 << 19)
299 #define CMD_BIT_WRITE (1 << 18)
300 #define CMD_BIT_STREAM (1 << 17)
301 #define CMD_MASK_CMD (0xff)
302 #define CMD_SHIFT_CMD (0)
303 
304 #define CMD(c, r) ( ((c) & CMD_MASK_CMD) | CMD_RESP((r)) )
305 
306 #define CMD_IDLE CMD(MMC_GO_IDLE_STATE, 0) | CMD_BIT_LS | CMD_BIT_INIT
307 #define CMD_SD_OP_COND CMD(SD_APP_OP_COND, 1) | CMD_BIT_LS | CMD_BIT_APP
308 #define CMD_SD_SEND_IF_COND CMD(SD_CMD8, 1) | CMD_BIT_LS
309 #define CMD_MMC_OP_COND CMD(MMC_SEND_OP_COND, 3) | CMD_BIT_LS | CMD_BIT_INIT
310 #define CMD_ALL_SEND_CID CMD(MMC_ALL_SEND_CID, 2) | CMD_BIT_LS
311 #define CMD_MMC_SET_RCA CMD(MMC_SET_RELATIVE_ADDR, 1) | CMD_BIT_LS
312 #define CMD_SD_SEND_RCA CMD(SD_SEND_RELATIVE_ADDR, 1) | CMD_BIT_LS
313 #define CMD_SEND_CSD CMD(MMC_SEND_CSD, 2) | CMD_BIT_LS
314 #define CMD_SEND_EXT_CSD CMD(MMC_SEND_EXT_CSD, 1) | CMD_BIT_LS | CMD_BIT_DATA
315 #define CMD_DESELECT_CARD CMD(MMC_SELECT_CARD, 0)
316 #define CMD_SELECT_CARD CMD(MMC_SELECT_CARD, 1)
317 #define CMD_SET_BLOCKLEN CMD(MMC_SET_BLOCKLEN, 1)
318 #define CMD_SEND_STATUS CMD(MMC_SEND_STATUS, 1)
319 #define CMD_READ_SINGLE CMD(MMC_READ_SINGLE_BLOCK, 1) | CMD_BIT_DATA
320 #define CMD_READ_MULTIPLE CMD(MMC_READ_MULTIPLE_BLOCK, 1) | CMD_BIT_DATA | CMD_BIT_AUTO_STOP
321 #define CMD_SD_SET_WIDTH CMD(SD_APP_SET_BUS_WIDTH, 1) | CMD_BIT_APP
322 #define CMD_STOP CMD(MMC_STOP_TRANSMISSION, 1) | CMD_BIT_BUSY
323 #define CMD_WRITE_SINGLE CMD(MMC_WRITE_BLOCK, 1) | CMD_BIT_DATA | CMD_BIT_WRITE
324 #define CMD_WRITE_MULTIPLE CMD(MMC_WRITE_MULTIPLE_BLOCK, 1) | CMD_BIT_DATA | CMD_BIT_WRITE | CMD_BIT_AUTO_STOP
325 
328 #define CARD_TYPE_SD (1 << 0)
329 #define CARD_TYPE_4BIT (1 << 1)
330 #define CARD_TYPE_8BIT (1 << 2)
331 #define CARD_TYPE_HC (OCR_HC_CCS)
333 #define MMC_SECTOR_SIZE 512
334 
337 #define US_TIMEOUT 1000000
338 #define MS_ACQUIRE_DELAY (10)
339 #define INIT_OP_RETRIES 50
340 #define SET_OP_RETRIES 1000
341 #define SDIO_BUS_WIDTH 4
342 #define SD_MMC_ENUM_CLOCK 400000
343 #define MMC_MAX_CLOCK 20000000
344 #define MMC_LOW_BUS_MAX_CLOCK 26000000
345 #define MMC_HIGH_BUS_MAX_CLOCK 52000000
346 #define SD_MAX_CLOCK 25000000
348 /* Function prototype for event setup function */
349 typedef void (*MCI_EVSETUP_FUNC_T)(uint32_t);
350 
351 /* Function prototype for wait (for IRQ) function */
352 typedef uint32_t (*MCI_WAIT_CB_FUNC_T)(void);
353 
354 /* Function prototype for milliSecond delay function */
355 typedef void (*MCI_MSDELAY_FUNC_T)(uint32_t);
356 
357 /* Card specific setup data */
358 typedef struct _mci_card_struct {
359  uint32_t response[4];
360  uint32_t cid[4];
361  uint32_t csd[4];
362  uint32_t ext_csd[512 / 4];
375 
383 {
385 }
386 
394 {
396 }
397 
403 {
405 }
406 
414 STATIC INLINE void Chip_SDMMC_PowerOnOff(int32_t enable)
415 {
417 }
418 
425 {
427 }
428 
434 
439 int32_t Chip_SDMMC_GetState(void);
440 
447 
452 int32_t Chip_SDMMC_GetDeviceSize(void);
453 
461 int32_t Chip_SDMMC_ReadBlocks(void *buffer, int32_t start_block, int32_t num_blocks);
462 
470 int32_t Chip_SDMMC_WriteBlocks(void *buffer, int32_t start_block, int32_t num_blocks);
471 
476 #ifdef __cplusplus
477 }
478 #endif
479 
480 #endif /* __SDMMC_18XX_43XX_H_ */