Changeset 6e0ae1b in umon


Ignore:
Timestamp:
Aug 3, 2015, 4:16:41 AM (4 years ago)
Author:
Jarielle Catbagan <jcatbagan93@…>
Branches:
master
Children:
9df0e66
Parents:
0cd5b7d
git-author:
Jarielle Catbagan <jcatbagan93@…> (08/03/15 04:16:41)
git-committer:
Ed Sutter <edsutterjr@…> (08/03/15 23:30:46)
Message:

BBB: am335x_sd.c: Integrate reading and writing functionality in "sd" command

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ports/beagleboneblack/am335x_sd.c

    r0cd5b7d r6e0ae1b  
    243243sdRead(int interface, char *buf, int blknum, int blkcount)
    244244{
     245        uint32_t cmd, arg, resp[4];
     246        uint32_t *wordptr = (uint32_t *) buf;
     247        int byteindex;
     248
     249        /* Get the SD card's status via CMD13 */
     250        arg = (sdrca << 16) & 0xFFFF0000;
     251        cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT |
     252                SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1;
     253        if (sdcmd(cmd, arg, resp) == -1)
     254                return(-1);
     255
     256        /* Ensure that the card is in Transfer State before proceeding */
     257        if ((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_TRANSFER) {
     258                arg = (sdrca << 16) & 0xFFFF0000;
     259                cmd = SD_CMD_CMD7_SELECT_DESELECT_CARD | SD_CMD_CMD_TYPE_NORMAL |
     260                        SD_CMD_DP_NO_DATA_PRESENT | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE |
     261                        SD_CMD_RSP_TYPE_R1B;
     262                if (sdcmd(cmd, arg, resp) == -1)
     263                        return(-1);
     264
     265                /* Wait for the SD card to enter Transfer State */
     266                do {
     267                        arg = (sdrca << 16) & 0xFFFF0000;
     268                        cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL |
     269                                SD_CMD_DP_NO_DATA_PRESENT | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE |
     270                                SD_CMD_RSP_TYPE_R1;
     271                        if (sdcmd(cmd, arg, resp) == -1)
     272                                return(-1);
     273                } while ((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_TRANSFER);
     274        }
     275
     276        /* Set the block length and the number of blocks to read */
     277        MMCHS0_REG(SD_BLK) = SD_BLKSIZE | (blkcount << 16);
     278        /* Send CMD18, i.e. read multiple blocks */
     279        arg = blknum;
     280        cmd = SD_CMD_CMD18_READ_MULTIPLE_BLOCK | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_DATA_PRESENT |
     281                SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1 | SD_CMD_MSBS_MULTIPLE |
     282                SD_CMD_DDIR_READ | SD_CMD_ACEN_CMD12_ENABLE | SD_CMD_BCE_ENABLE;
     283        if (sdcmd(cmd, arg, resp) == -1)
     284                return(-1);
     285
     286        /* Check the data buffer to see if there is data to be read */
     287        do {
     288                while (!(MMCHS0_REG(SD_STAT) & SD_STAT_BRR));
     289
     290                /* Clear the BRR status bit in SD_STAT */
     291                MMCHS0_REG(SD_STAT) |= SD_STAT_BRR;
     292
     293                for (byteindex = 0; byteindex < (SD_BLKSIZE / 4); byteindex++) {
     294                        *wordptr = (MMCHS0_REG(SD_DATA));
     295                        wordptr++;
     296                }
     297        } while (!(MMCHS0_REG(SD_STAT) & SD_STAT_TC));
     298
     299        /* Put the SD card into Stand-by State */
     300        arg = 0;
     301        cmd = SD_CMD_CMD7_SELECT_DESELECT_CARD | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT |
     302                SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_NO_RESPONSE;
     303        if (sdcmd(cmd, arg, resp) == -1)
     304                return(-1);
     305
     306        /* Wait for the SD card to enter Stand-by State */
     307        do {
     308                arg = (sdrca << 16) & 0xFFFF0000;
     309                cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT |
     310                        SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1;
     311                if (sdcmd(cmd, arg, resp) == -1)
     312                        return(-1);
     313        } while ((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_STANDBY);
     314
     315        return(0);
     316}
     317
     318int
     319sdWrite(int interface, char *buf, int blknum, int blkcount)
     320{
     321        uint32_t cmd, arg, resp[4];
     322        uint32_t *wordptr = (uint32_t *) buf;
     323        int byteindex;
     324
     325        /* Get the SD card's status by sending CMD13 */
     326        arg = (sdrca << 16) & 0xFFFF0000;
     327        cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT |
     328                SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1;
     329        if (sdcmd(cmd, arg, resp) == -1)
     330                return(-1);
     331
     332        /* Ensure that the card is in the Transfer State before proceeding */
     333        if ((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_TRANSFER) {
     334                arg = (sdrca << 16) & 0xFFFF0000;
     335                cmd  = SD_CMD_CMD7_SELECT_DESELECT_CARD | SD_CMD_CMD_TYPE_NORMAL |
     336                        SD_CMD_DP_NO_DATA_PRESENT | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE |
     337                        SD_CMD_RSP_TYPE_R1B;
     338                if (sdcmd(cmd, arg, resp) == -1)
     339                        return(-1);
     340
     341                /* Wait for SD card to enter Transfer State */
     342                do {
     343                        arg = (sdrca << 16) & 0xFFFF0000;
     344                        cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL |
     345                                SD_CMD_DP_NO_DATA_PRESENT | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE |
     346                                SD_CMD_RSP_TYPE_R1;
     347                        if (sdcmd(cmd, arg, resp) == -1)
     348                                return(-1);
     349                } while ((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_TRANSFER);
     350        }
     351
     352        /* Set the block length in bytes and the number of blocks to write to the SD card */
     353        MMCHS0_REG(SD_BLK) = SD_BLKSIZE | ( blkcount << 16);
     354        /* Send CMD25, that is write the number of blocks specified in 'blkcount' from 'buf' to the
     355         * location that is 512 byte aligned in the SD card specified by the block number 'blknum'
     356         */
     357        arg = blknum;
     358        cmd = SD_CMD_CMD25_WRITE_MULTIPLE_BLOCK | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_DATA_PRESENT |
     359                SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1 | SD_CMD_MSBS_MULTIPLE |
     360                SD_CMD_DDIR_WRITE | SD_CMD_ACEN_CMD12_ENABLE | SD_CMD_BCE_ENABLE;
     361        if (sdcmd(cmd, arg, resp) == -1)
     362                return(-1);
     363
     364        /* Write the data */
     365        do {
     366                /* Wait until data is ready to be written */
     367                while (!(MMCHS0_REG(SD_STAT) & (SD_STAT_BWR | SD_STAT_TC)));
     368
     369                if (MMCHS0_REG(SD_STAT) & SD_STAT_TC)
     370                        break;
     371
     372                /* Clear the BWR status bit in SD_STAT */
     373                MMCHS0_REG(SD_STAT) |= SD_STAT_BWR;
     374
     375                for (byteindex = 0; byteindex < (SD_BLKSIZE / 4); byteindex++)
     376                        MMCHS0_REG(SD_DATA) = *wordptr++;
     377        } while (!(MMCHS0_REG(SD_STAT) & SD_STAT_TC));
     378
     379        /* Put the SD card into Stand-by State */
     380        arg = 0x00000000;
     381        cmd = SD_CMD_CMD7_SELECT_DESELECT_CARD | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT |
     382                SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_NO_RESPONSE;
     383        if (sdcmd(cmd, arg, resp) == -1)
     384                return(-1);
     385
     386        /* Wait for SD card to enter Stand-by State */
     387        do {
     388                arg= (sdrca << 16) & 0xFFFF0000;
     389                cmd  = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT |
     390                        SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1;
     391                if (sdcmd(cmd, arg, resp) == -1)
     392                        return(-1);
     393        } while ((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_STANDBY);
     394
     395        return(0);
     396}
     397
     398int
     399sdInstalled(int interface)
     400{
     401        if ((MMCHS0_REG(SD_CON) & SD_CON_CDP) == SD_CON_CDP_ACTIVE_HIGH)
     402                if (MMCHS0_REG(SD_PSTATE) & SD_PSTATE_CINS)
     403                        return(1);
     404                else
     405                        return(0);
     406        else
     407                if (MMCHS0_REG(SD_PSTATE) & SD_PSTATE_CINS)
     408                        return(0);
     409                else
     410                        return(1);
     411}
     412
     413int
     414sdPowerup(int tot)
     415{
    245416        return(-1);
    246417}
    247418
    248419int
    249 sdWrite(int interface, char *buf, int blknum, int blkcount)
     420sdReadCxD(int interface, unsigned char *buf, int cmd)
    250421{
    251422        return(-1);
    252423}
    253 
    254 int
    255 sdInstalled(int interface)
    256 {
    257         return(-1);
    258 }
    259 
    260 int
    261 sdPowerup(int tot)
    262 {
    263         return(-1);
    264 }
    265 
    266 int
    267 sdReadCxD(int interface, unsigned char *buf, int cmd)
    268 {
    269         return(-1);
    270 }
Note: See TracChangeset for help on using the changeset viewer.