LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sdmmc_18xx_43xx.c
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 #include "sdmmc_18xx_43xx.h"
33 #include "string.h"
34 
35 /*****************************************************************************
36  * Private types/enumerations/variables
37  ****************************************************************************/
38 
39 /* Global instance of the current card */
41 
42 /* Helper definition: all SD error conditions in the status word */
43 #define SD_INT_ERROR (MCI_INT_RESP_ERR | MCI_INT_RCRC | MCI_INT_DCRC | \
44  MCI_INT_RTO | MCI_INT_DTO | MCI_INT_HTO | MCI_INT_FRUN | MCI_INT_HLE | \
45  MCI_INT_SBE | MCI_INT_EBE)
46 
47 /*****************************************************************************
48  * Public types/enumerations/variables
49  ****************************************************************************/
50 
51 /*****************************************************************************
52  * Private functions
53  ****************************************************************************/
54 
55 /* Function to execute a command */
56 static int32_t sdmmc_execute_command(uint32_t cmd, uint32_t arg, uint32_t wait_status)
57 {
58  int32_t step = (cmd & CMD_BIT_APP) ? 2 : 1;
59  int32_t status = 0;
60  uint32_t cmd_reg = 0;
61 
62  if (!wait_status) {
63  wait_status = (cmd & CMD_MASK_RESP) ? MCI_INT_CMD_DONE : MCI_INT_DATA_OVER;
64  }
65 
66  /* Clear the interrupts & FIFOs*/
67  if (cmd & CMD_BIT_DATA) {
69  }
70 
71  /* also check error conditions */
73  if (wait_status & MCI_INT_DATA_OVER) {
74  wait_status |= MCI_INT_FRUN | MCI_INT_HTO | MCI_INT_DTO | MCI_INT_DCRC;
75  }
76 
77  while (step) {
78  IP_SDMMC_SetClock(LPC_SDMMC, g_card_info->clk_rate, g_card_info->speed);
79 
80  /* Clear the interrupts */
82 
83  g_card_info->evsetup_cb(wait_status);
84 
85  switch (step) {
86  case 1: /* Execute command */
87  cmd_reg = ((cmd & CMD_MASK_CMD) >> CMD_SHIFT_CMD) |
88  ((cmd & CMD_BIT_INIT) ? MCI_CMD_INIT : 0) |
90  (((cmd & CMD_MASK_RESP) == CMD_RESP_R2) ? MCI_CMD_RESP_LONG : 0) |
91  ((cmd & CMD_MASK_RESP) ? MCI_CMD_RESP_EXP : 0) |
92  ((cmd & CMD_BIT_WRITE) ? MCI_CMD_DAT_WR : 0) |
93  ((cmd & CMD_BIT_STREAM) ? MCI_CMD_STRM_MODE : 0) |
94  ((cmd & CMD_BIT_BUSY) ? MCI_CMD_STOP : 0) |
95  ((cmd & CMD_BIT_AUTO_STOP) ? MCI_CMD_SEND_STOP : 0) |
97 
98  /* wait for previos data finsh for select/deselect commands */
99  if (((cmd & CMD_MASK_CMD) >> CMD_SHIFT_CMD) == MMC_SELECT_CARD) {
100  cmd_reg |= MCI_CMD_PRV_DAT_WAIT;
101  }
102 
103  /* wait for command to be accepted by CIU */
104  if (IP_SDMMC_SendCmd(LPC_SDMMC, cmd_reg, arg) == 0) {
105  --step;
106  }
107  break;
108 
109  case 0:
110  return 0;
111 
112  case 2: /* APP prefix */
113  cmd_reg = MMC_APP_CMD | MCI_CMD_RESP_EXP |
114  ((cmd & CMD_BIT_INIT) ? MCI_CMD_INIT : 0) |
116 
117  if (IP_SDMMC_SendCmd(LPC_SDMMC, cmd_reg, g_card_info->rca << 16) == 0) {
118  --step;
119  }
120  break;
121  }
122 
123  /* wait for command response */
124  status = g_card_info->waitfunc_cb();
125 
126  /* We return an error if there is a timeout, even if we've fetched a response */
127  if (status & SD_INT_ERROR) {
128  return status;
129  }
130 
131  if (status & MCI_INT_CMD_DONE) {
132  switch (cmd & CMD_MASK_RESP) {
133  case 0:
134  break;
135 
136  case CMD_RESP_R1:
137  case CMD_RESP_R3:
138  case CMD_RESP_R2:
139  IP_SDMMC_GetResponse(LPC_SDMMC, &g_card_info->response[0]);
140  break;
141  }
142  }
143  }
144 
145  return 0;
146 }
147 
148 /* Checks whether card is acquired properly or not */
149 static int32_t prv_card_acquired(void)
150 {
151  return g_card_info->cid[0] != 0;
152 }
153 
154 /* Helper function to get a bit field withing multi-word buffer. Used to get
155  fields with-in CSD & EXT-CSD */
156 static uint32_t prv_get_bits(int32_t start, int32_t end, uint32_t *data)
157 {
158  uint32_t v;
159  uint32_t i = end >> 5;
160  uint32_t j = start & 0x1f;
161 
162  if (i == (start >> 5)) {
163  v = (data[i] >> j);
164  }
165  else {
166  v = ((data[i] << (32 - j)) | (data[start >> 5] >> j));
167  }
168 
169  return v & ((1 << (end - start + 1)) - 1);
170 }
171 
172 /* Function to process the CSD & EXT-CSD of the card */
173 static void prv_process_csd(void)
174 {
175  int32_t status = 0;
176  int32_t c_size = 0;
177  int32_t c_size_mult = 0;
178  int32_t mult = 0;
179 
180  /* compute block length based on CSD response */
181  g_card_info->block_len = 1 << prv_get_bits(80, 83, g_card_info->csd);
182 
183  if ((g_card_info->card_type & CARD_TYPE_HC) && (g_card_info->card_type & CARD_TYPE_SD)) {
184  /* See section 5.3.3 CSD Register (CSD Version 2.0) of SD2.0 spec an explanation for the calculation of these values */
185  c_size = prv_get_bits(48, 63, (uint32_t *) g_card_info->csd) + 1;
186  g_card_info->blocknr = c_size << 10;/* 512 byte blocks */
187  }
188  else {
189  /* See section 5.3 of the 4.1 revision of the MMC specs for an explanation for the calculation of these values */
190  c_size = prv_get_bits(62, 73, (uint32_t *) g_card_info->csd);
191  c_size_mult = prv_get_bits(47, 49, (uint32_t *) g_card_info->csd);
192  mult = 1 << (c_size_mult + 2);
193  g_card_info->blocknr = (c_size + 1) * mult;
194 
195  /* adjust blocknr to 512/block */
196  if (g_card_info->block_len > MMC_SECTOR_SIZE) {
197  g_card_info->blocknr = g_card_info->blocknr * (g_card_info->block_len >> 9);
198  }
199 
200  /* get extended CSD for newer MMC cards CSD spec >= 4.0*/
201  if (((g_card_info->card_type & CARD_TYPE_SD) == 0) &&
202  (prv_get_bits(122, 125, (uint32_t *) g_card_info->csd) >= 4)) {
203  /* put card in trans state */
204  status = sdmmc_execute_command(CMD_SELECT_CARD, g_card_info->rca << 16, 0);
205 
206  /* set block size and byte count */
208 
209  /* send EXT_CSD command */
210  IP_SDMMC_DmaSetup(LPC_SDMMC, &g_card_info->sdif_dev, (uint32_t) g_card_info->ext_csd, MMC_SECTOR_SIZE);
211 
213  if ((status & SD_INT_ERROR) == 0) {
214  /* check EXT_CSD_VER is greater than 1.1 */
215  if ((g_card_info->ext_csd[48] & 0xFF) > 1) {
216  g_card_info->blocknr = g_card_info->ext_csd[53];/* bytes 212:215 represent sec count */
217 
218  }
219  /* switch to 52MHz clock if card type is set to 1 or else set to 26MHz */
220  if ((g_card_info->ext_csd[49] & 0xFF) == 1) {
221  /* for type 1 MMC cards high speed is 52MHz */
222  g_card_info->speed = MMC_HIGH_BUS_MAX_CLOCK;
223  }
224  else {
225  /* for type 0 MMC cards high speed is 26MHz */
226  g_card_info->speed = MMC_LOW_BUS_MAX_CLOCK;
227  }
228  }
229  }
230  }
231 
232  g_card_info->device_size = g_card_info->blocknr << 9; /* blocknr * 512 */
233 }
234 
235 /* Puts current selected card in trans state */
236 static int32_t prv_set_trans_state(void)
237 {
239 
240  /* get current state of the card */
241  status = sdmmc_execute_command(CMD_SEND_STATUS, g_card_info->rca << 16, 0);
242  if (status & MCI_INT_RTO) {
243  /* unable to get the card state. So return immediatly. */
244  return -1;
245  }
246 
247  /* check card state in response */
248  status = R1_CURRENT_STATE(g_card_info->response[0]);
249  switch (status) {
250  case SDMMC_STBY_ST:
251  /* put card in 'Trans' state */
252  status = sdmmc_execute_command(CMD_SELECT_CARD, g_card_info->rca << 16, 0);
253  if (status != 0) {
254  /* unable to put the card in Trans state. So return immediatly. */
255  return -1;
256  }
257  break;
258 
259  case SDMMC_TRAN_ST:
260  /*do nothing */
261  break;
262 
263  default:
264  /* card shouldn't be in other states so return */
265  return -1;
266  }
267 
268  return 0;
269 }
270 
271 /* Sets card data width and block size */
272 static int32_t prv_set_card_params(void)
273 {
274  int32_t status;
275 
276 #if SDIO_BUS_WIDTH > 1
277  if (g_card_info->card_type & CARD_TYPE_SD) {
278  status = sdmmc_execute_command(CMD_SD_SET_WIDTH, 2, 0);
279  if (status != 0) {
280  return -1;
281  }
282 
283  /* if positive response */
285  LPC_SDMMC->CTYPE = MCI_CTYPE_4BIT;
286  }
287 #elif SDIO_BUS_WIDTH > 4
288 #error 8-bit mode not supported yet!
289 #endif
290 
291  /* set block length */
294  if (status != 0) {
295  return -1;
296  }
297 
298  return 0;
299 }
300 
301 /*****************************************************************************
302  * Public functions
303  ****************************************************************************/
304 
305 /* Returns the current SD status, clears pending ints, and disables all ints */
307 {
309 
310  /* Get status and clear interrupts */
314 
315  return status;
316 }
317 
318 /* Get card's current state (idle, transfer, program, etc.) */
319 int32_t Chip_SDMMC_GetState(void)
320 {
322 
323  /* get current state of the card */
324  status = sdmmc_execute_command(CMD_SEND_STATUS, g_card_info->rca << 16, 0);
325  if (status & MCI_INT_RTO) {
326  return -1;
327  }
328 
329  /* check card state in response */
330  return (int32_t) R1_CURRENT_STATE(g_card_info->response[0]);
331 }
332 
333 /* Function to enumerate the SD/MMC/SDHC/MMC+ cards */
335 {
336  int32_t status;
337  int32_t tries = 0;
339  uint32_t r;
340  int32_t state = 0;
341  uint32_t command = 0;
342 
343  g_card_info = pcardinfo;
344 
345  /* clear card type */
347 
348  /* set high speed for the card as 20MHz */
349  g_card_info->speed = MMC_MAX_CLOCK;
350 
352 
353  while (state < 100) {
354  switch (state) {
355  case 0: /* Setup for SD */
356  /* check if it is SDHC card */
358  if (!(status & MCI_INT_RTO)) {
359  /* check response has same echo pattern */
360  if ((g_card_info->response[0] & SD_SEND_IF_ECHO_MSK) == SD_SEND_IF_RESP) {
361  ocr |= OCR_HC_CCS;
362  }
363  }
364 
365  ++state;
366  command = CMD_SD_OP_COND;
367  tries = INIT_OP_RETRIES;
368 
369  /* assume SD card */
370  g_card_info->card_type |= CARD_TYPE_SD;
371  g_card_info->speed = SD_MAX_CLOCK;
372  break;
373 
374  case 10: /* Setup for MMC */
375  /* start fresh for MMC crds */
376  g_card_info->card_type &= ~CARD_TYPE_SD;
378  command = CMD_MMC_OP_COND;
379  tries = INIT_OP_RETRIES;
380  ocr |= OCR_HC_CCS;
381  ++state;
382 
383  /* for MMC cards high speed is 20MHz */
384  g_card_info->speed = MMC_MAX_CLOCK;
385  break;
386 
387  case 1:
388  case 11:
389  status = sdmmc_execute_command(command, 0, 0);
390  if (status & MCI_INT_RTO) {
391  state += 9; /* Mode unavailable */
392  }
393  else {
394  ++state;
395  }
396  break;
397 
398  case 2: /* Initial OCR check */
399  case 12:
400  ocr = g_card_info->response[0] | (ocr & OCR_HC_CCS);
401  if (ocr & OCR_ALL_READY) {
402  ++state;
403  }
404  else {
405  state += 2;
406  }
407  break;
408 
409  case 3: /* Initial wait for OCR clear */
410  case 13:
411  while ((ocr & OCR_ALL_READY) && --tries > 0) {
412  g_card_info->msdelay_func(MS_ACQUIRE_DELAY);
413  status = sdmmc_execute_command(command, 0, 0);
414  ocr = g_card_info->response[0] | (ocr & OCR_HC_CCS);
415  }
416  if (ocr & OCR_ALL_READY) {
417  state += 7;
418  }
419  else {
420  ++state;
421  }
422  break;
423 
424  case 14:
425  /* for MMC cards set high capacity bit */
426  ocr |= OCR_HC_CCS;
427 
428  case 4: /* Assign OCR */
429  tries = SET_OP_RETRIES;
430  ocr &= OCR_VOLTAGE_RANGE_MSK | OCR_HC_CCS; /* Mask for the bits we care about */
431  do {
432  g_card_info->msdelay_func(MS_ACQUIRE_DELAY);
433  status = sdmmc_execute_command(command, ocr, 0);
434  r = g_card_info->response[0];
435  } while (!(r & OCR_ALL_READY) && --tries > 0);
436 
437  if (r & OCR_ALL_READY) {
438  /* is it high capacity card */
439  g_card_info->card_type |= (r & OCR_HC_CCS);
440  ++state;
441  }
442  else {
443  state += 6;
444  }
445  break;
446 
447  case 5: /* CID polling */
448  case 15:
449  status = sdmmc_execute_command(CMD_ALL_SEND_CID, 0, 0);
450  memcpy(&g_card_info->cid, &g_card_info->response[0], 16);
451  ++state;
452  break;
453 
454  case 6: /* RCA send, for SD get RCA */
455  status = sdmmc_execute_command(CMD_SD_SEND_RCA, 0, 0);
456  g_card_info->rca = (g_card_info->response[0]) >> 16;
457  ++state;
458  break;
459 
460  case 16: /* RCA assignment for MMC set to 1 */
461  g_card_info->rca = 1;
462  status = sdmmc_execute_command(CMD_MMC_SET_RCA, g_card_info->rca << 16, 0);
463  ++state;
464  break;
465 
466  case 7:
467  case 17:
468  status = sdmmc_execute_command(CMD_SEND_CSD, g_card_info->rca << 16, 0);
469  memcpy(&g_card_info->csd, &g_card_info->response[0], 16);
470  state = 100;
471  break;
472 
473  default:
474  state += 100; /* break from while loop */
475  break;
476  }
477  }
478 
479  /* Compute card size, block size and no. of blocks based on CSD response recived. */
480  if (prv_card_acquired()) {
481  prv_process_csd();
482 
483  /* Setup card data width and block size (once) */
484  if (prv_set_trans_state() != 0) {
485  return 0;
486  }
487  if (prv_set_card_params() != 0) {
488  return 0;
489  }
490  }
491 
492  return prv_card_acquired();
493 }
494 
495 /* Get the device size of SD/MMC card (after enumeration) */
497 {
498  return g_card_info->device_size;
499 }
500 
501 /* Performs the read of data from the SD/MMC card */
502 int32_t Chip_SDMMC_ReadBlocks(void *buffer, int32_t start_block, int32_t num_blocks)
503 {
504  int32_t cbRead = (num_blocks) * MMC_SECTOR_SIZE;
505  int32_t status = 0;
506  int32_t index;
507 
508  /* if card is not acquired return immediately */
509  if (( start_block < 0) || ( (start_block + num_blocks) > g_card_info->blocknr) ) {
510  return 0;
511  }
512 
513  /* put card in trans state */
514  if (prv_set_trans_state() != 0) {
515  return 0;
516  }
517 
518  /* set number of bytes to read */
519  LPC_SDMMC->BYTCNT = cbRead;
520 
521  /* if high capacity card use block indexing */
522  if (g_card_info->card_type & CARD_TYPE_HC) {
523  index = start_block;
524  }
525  else { /*fix at 512 bytes*/
526  index = start_block << 9; // \* g_card_info->block_len;
527 
528  }
529  IP_SDMMC_DmaSetup(LPC_SDMMC, &g_card_info->sdif_dev, (uint32_t) buffer, cbRead);
530 
531  /* Select single or multiple read based on number of blocks */
532  if (num_blocks == 1) {
534  }
535  else {
537  }
538 
539  if (status != 0) {
540  cbRead = 0;
541  }
542  /*Wait for card program to finish*/
543  while (Chip_SDMMC_GetState() != SDMMC_TRAN_ST) ;
544 
545  return cbRead;
546 }
547 
548 /* Performs write of data to the SD/MMC card */
549 int32_t Chip_SDMMC_WriteBlocks(void *buffer, int32_t start_block, int32_t num_blocks)
550 {
551  int32_t cbWrote = num_blocks * MMC_SECTOR_SIZE;
552  int32_t status;
553  int32_t index;
554 
555  /* if card is not acquired return immediately */
556  if (( start_block < 0) || ( (start_block + num_blocks) > g_card_info->blocknr) ) {
557  return 0;
558  }
559 
560  /*Wait for card program to finish*/
561  while (Chip_SDMMC_GetState() != SDMMC_TRAN_ST) ;
562 
563  /* put card in trans state */
564  if (prv_set_trans_state() != 0) {
565  return 0;
566  }
567 
568  /* set number of bytes to write */
569  LPC_SDMMC->BYTCNT = cbWrote;
570 
571  /* if high capacity card use block indexing */
572  if (g_card_info->card_type & CARD_TYPE_HC) {
573  index = start_block;
574  }
575  else { /*fix at 512 bytes*/
576  index = start_block << 9; // * g_card_info->block_len;
577 
578  }
579  IP_SDMMC_DmaSetup(LPC_SDMMC, &g_card_info->sdif_dev, (uint32_t) buffer, cbWrote);
580 
581  /* Select single or multiple write based on number of blocks */
582  if (num_blocks == 1) {
584  }
585  else {
587  }
588 
589  /*Wait for card program to finish*/
590  while (Chip_SDMMC_GetState() != SDMMC_TRAN_ST) ;
591 
592  if (status != 0) {
593  cbWrote = 0;
594  }
595 
596  return cbWrote;
597 }