Ignore:
Timestamp:
Sep 21, 2018, 10:26:16 PM (6 years ago)
Author:
nicolas.van.phan@…
Message:

Make soclib_sdc internal function not use globals

Location:
trunk/hal/tsar_mips32/drivers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/hal/tsar_mips32/drivers/soclib_sdc.c

    r550 r558  
    1919#define SDCARD_RESET_ITER_MAX 4
    2020
     21extern   chdev_directory_t    chdev_dir;  // allocated in the kernel_init.c file.
     22
    2123///////////////////////////////////////////////////////////////////////////////
    2224//   Global variables
     
    2426
    2527__attribute__((section(".kdata")))
    26 static struct sdcard_dev sdcard;
     28static struct sdcard_dev global_sdcard;
    2729
    2830__attribute__((section(".kdata")))
     
    3234// This function enables SD Card select signal
    3335///////////////////////////////////////////////////////////////////////////////
    34 static void _sdc_enable()
    35 {
    36     spi_ss_assert(sdcard.spi, sdcard.slave_id);
     36static void _sdc_enable(sdcard_dev_t *sdcard)
     37{
     38    spi_ss_assert(sdcard->spi, sdcard->slave_id);
    3739}
    3840
     
    4042// This function disables SD Card select signal
    4143///////////////////////////////////////////////////////////////////////////////
    42 static void _sdc_disable()
    43 {
    44     spi_ss_deassert(sdcard.spi, sdcard.slave_id);
     44static void _sdc_disable(sdcard_dev_t *sdcard)
     45{
     46    spi_ss_deassert(sdcard->spi, sdcard->slave_id);
    4547}
    4648
     
    4951// - tick_count: number of ticks to generate (1 tick -> 8 clocks)
    5052///////////////////////////////////////////////////////////////////////////////
    51 static void _sdc_gen_tick(unsigned int tick_count)
     53static void _sdc_gen_tick(sdcard_dev_t *sdcard, unsigned int tick_count)
    5254{
    5355    register int i = 0;
    54     while(i++ < tick_count) spi_put_tx(sdcard.spi, 0xFF, 0);
     56    while(i++ < tick_count) spi_put_tx(sdcard->spi, 0xFF, 0);
    5557}
    5658
     
    6062// - lba: number of logical block to move the pointer
    6163///////////////////////////////////////////////////////////////////////////////
    62 void _sdc_lseek(unsigned int lba)
    63 {
    64     sdcard.access_pointer = sdcard.block_length * lba;
     64void _sdc_lseek(sdcard_dev_t *sdcard, unsigned int lba)
     65{
     66    sdcard->access_pointer = sdcard->block_length * lba;
    6567}
    6668
     
    6870// This function gets a byte from the SD card
    6971///////////////////////////////////////////////////////////////////////////////
    70 static unsigned char _sdc_receive_char()
    71 {
    72     _sdc_gen_tick(1);
    73 
    74     return spi_get_rx(sdcard.spi, 0);
     72static unsigned char _sdc_receive_char(sdcard_dev_t *sdcard)
     73{
     74    _sdc_gen_tick(sdcard, 1);
     75
     76    return spi_get_rx(sdcard->spi, 0);
    7577}
    7678
     
    8082// Returns the SD card response value
    8183///////////////////////////////////////////////////////////////////////////////
    82 static unsigned char _sdc_wait_response()
     84static unsigned char _sdc_wait_response(sdcard_dev_t *sdcard)
    8385{
    8486    unsigned char sdcard_rsp;
     
    8688
    8789    iter       = 0;
    88     sdcard_rsp = _sdc_receive_char();
     90    sdcard_rsp = _sdc_receive_char(sdcard);
    8991    while (
    9092            (iter < SDCARD_COMMAND_TIMEOUT) &&
     
    9294          )
    9395    {
    94         sdcard_rsp = _sdc_receive_char();
     96        sdcard_rsp = _sdc_receive_char(sdcard);
    9597        iter++;
    9698    }
     
    104106// It must be called after a read command
    105107///////////////////////////////////////////////////////////////////////////////
    106 static void _sdc_wait_data_block()
    107 {
    108         while (_sdc_receive_char() != 0xFE);
     108static void _sdc_wait_data_block(sdcard_dev_t *sdcard)
     109{
     110        while (_sdc_receive_char(sdcard) != 0xFE);
    109111}
    110112
     
    116118// - crc7: CRC (7 bits) to send
    117119///////////////////////////////////////////////////////////////////////////////
    118 static int _sdc_send_command ( int      index,
     120static int _sdc_send_command ( sdcard_dev_t *sdcard,
     121                               int      index,
    119122                               int      app  ,
    120123                               void *   args ,
     
    124127    unsigned char * _args;
    125128
    126     _sdc_gen_tick(5); 
     129    _sdc_gen_tick(sdcard, 5); 
    127130
    128131    if (app == SDCARD_ACMD)
    129132    {
    130         spi_put_tx(sdcard.spi, 0x40 | 55         , 0 );// CMD and START bit
    131         spi_put_tx(sdcard.spi, 0x00              , 0 );// Argument[0]
    132         spi_put_tx(sdcard.spi, 0x00              , 0 );// Argument[1]
    133         spi_put_tx(sdcard.spi, 0x00              , 0 );// Argument[2]
    134         spi_put_tx(sdcard.spi, 0x00              , 0 );// Argument[3]
    135         spi_put_tx(sdcard.spi, 0x01 | (crc7 << 1), 0 );// END bit
    136 
    137         sdcard_rsp = _sdc_wait_response();
     133        spi_put_tx(sdcard->spi, 0x40 | 55         , 0 );// CMD and START bit
     134        spi_put_tx(sdcard->spi, 0x00              , 0 );// Argument[0]
     135        spi_put_tx(sdcard->spi, 0x00              , 0 );// Argument[1]
     136        spi_put_tx(sdcard->spi, 0x00              , 0 );// Argument[2]
     137        spi_put_tx(sdcard->spi, 0x00              , 0 );// Argument[3]
     138        spi_put_tx(sdcard->spi, 0x01 | (crc7 << 1), 0 );// END bit
     139
     140        sdcard_rsp = _sdc_wait_response(sdcard);
    138141        if (SDCARD_CHECK_R1_ERROR(sdcard_rsp))
    139142        {
     
    144147    _args = (unsigned char *) args;
    145148
    146     _sdc_gen_tick(1); 
    147 
    148     spi_put_tx(sdcard.spi, 0x40 | index      , 0 );
    149     spi_put_tx(sdcard.spi, _args[0]          , 0 );
    150     spi_put_tx(sdcard.spi, _args[1]          , 0 );
    151     spi_put_tx(sdcard.spi, _args[2]          , 0 );
    152     spi_put_tx(sdcard.spi, _args[3]          , 0 );
    153     spi_put_tx(sdcard.spi, 0x01 | (crc7 << 1), 0 );
    154 
    155     return _sdc_wait_response();
     149    _sdc_gen_tick(sdcard, 1); 
     150
     151    spi_put_tx(sdcard->spi, 0x40 | index      , 0 );
     152    spi_put_tx(sdcard->spi, _args[0]          , 0 );
     153    spi_put_tx(sdcard->spi, _args[1]          , 0 );
     154    spi_put_tx(sdcard->spi, _args[2]          , 0 );
     155    spi_put_tx(sdcard->spi, _args[3]          , 0 );
     156    spi_put_tx(sdcard->spi, 0x01 | (crc7 << 1), 0 );
     157
     158    return _sdc_wait_response(sdcard);
    156159}
    157160
     
    161164// Returns 0 if success, other value if failure
    162165///////////////////////////////////////////////////////////////////////////////
    163 static int _sdc_open( unsigned int channel )
     166static int _sdc_open( sdcard_dev_t *sdcard, xptr_t base, unsigned int channel )
    164167{
    165168        unsigned char args[4];
     
    167170        unsigned int  iter, ersp;
    168171
    169         sdcard.spi      = spi;
    170         sdcard.slave_id = channel;
     172        sdcard->spi      = base;
     173        sdcard->slave_id = channel;
    171174
    172175        // supply SD card ramp up time (min 74 cycles)
    173         _sdc_gen_tick(10);
     176        _sdc_gen_tick(sdcard, 10);
    174177
    175178        // Assert slave select signal
    176179        // Send CMD0 (Reset Command)
    177180        // Deassert slave select signal
    178         _sdc_enable();
     181        _sdc_enable(sdcard);
    179182
    180183        args[0] = 0;
     
    182185        args[2] = 0;
    183186        args[3] = 0;
    184         sdcard_rsp = _sdc_send_command(0, SDCARD_CMD, args, 0x4A);
     187        sdcard_rsp = _sdc_send_command(sdcard, 0, SDCARD_CMD, args, 0x4A);
    185188
    186189        if ( sdcard_rsp != 0x01 )
     
    190193        }
    191194
    192         _sdc_disable();
     195        _sdc_disable(sdcard);
    193196
    194197        // send CMD8. If card is pre-v2, It will reply with illegal command.
    195198        // Otherwise we announce sdhc support.
    196         _sdc_enable();
     199        _sdc_enable(sdcard);
    197200        args[0] = 0;
    198201        args[1] = 0;
    199202        args[2] = 0x01;
    200203        args[3] = 0x01;
    201         sdcard_rsp = _sdc_send_command(8, SDCARD_CMD, args, 0x63);
     204        sdcard_rsp = _sdc_send_command(sdcard, 8, SDCARD_CMD, args, 0x63);
    202205        if (!SDCARD_CHECK_R1_VALID(sdcard_rsp))
    203206    {
     
    208211    {
    209212                // no error, command accepted. get whole reply
    210                 ersp = _sdc_receive_char();
    211                 ersp = (ersp << 8) | _sdc_receive_char();
    212                 ersp = (ersp << 8) | _sdc_receive_char();
    213                 ersp = (ersp << 8) | _sdc_receive_char();
     213                ersp = _sdc_receive_char(sdcard);
     214                ersp = (ersp << 8) | _sdc_receive_char(sdcard);
     215                ersp = (ersp << 8) | _sdc_receive_char(sdcard);
     216                ersp = (ersp << 8) | _sdc_receive_char(sdcard);
    214217                if ((ersp & 0xffff) != 0x0101)
    215218        {
     
    219222                }
    220223                printk("[SDC WARNING] v2 or later ");
    221                 sdcard.sdhc = 1;
     224                sdcard->sdhc = 1;
    222225        }
    223226    else if ((sdcard_rsp & SDCARD_R1_ILLEGAL_CMD) == 0)
     
    229232    else
    230233    {
    231                 sdcard.sdhc = 0;
    232         }
    233         _sdc_disable();
     234                sdcard->sdhc = 0;
     235        }
     236        _sdc_disable(sdcard);
    234237
    235238        // send CMD41, enabling the card
    236         _sdc_enable();
    237         args[0] = sdcard.sdhc ? 0x40: 0;
     239        _sdc_enable(sdcard);
     240        args[0] = sdcard->sdhc ? 0x40: 0;
    238241        args[1] = 0;
    239242        args[2] = 0;
     
    243246        while( iter++ < SDCARD_COMMAND_TIMEOUT )
    244247        {
    245                 sdcard_rsp = _sdc_send_command(41, SDCARD_ACMD, args, 0x00);
     248                sdcard_rsp = _sdc_send_command(sdcard, 41, SDCARD_ACMD, args, 0x00);
    246249                if( sdcard_rsp == 0x01 )
    247250                {
     
    252255        }
    253256
    254         _sdc_disable();
     257        _sdc_disable(sdcard);
    255258        if (sdcard_rsp)
    256259    {
     
    258261                return sdcard_rsp;
    259262        }
    260         if (sdcard.sdhc != 0)
     263        if (sdcard->sdhc != 0)
    261264    {
    262265                // get the card capacity to see if it's really HC
    263                 _sdc_enable();
    264                 args[0] = sdcard.sdhc ? 0x40: 0;
     266                _sdc_enable(sdcard);
     267                args[0] = sdcard->sdhc ? 0x40: 0;
    265268                args[1] = 0;
    266269                args[2] = 0;
    267270                args[3] = 0;
    268         sdcard_rsp = _sdc_send_command(58, SDCARD_CMD, args, 0x00);
     271        sdcard_rsp = _sdc_send_command(sdcard, 58, SDCARD_CMD, args, 0x00);
    269272                if (sdcard_rsp)
    270273        {
     
    272275                        return sdcard_rsp;
    273276                }
    274                 ersp = _sdc_receive_char();
    275                 ersp = (ersp << 8) | _sdc_receive_char();
    276                 ersp = (ersp << 8) | _sdc_receive_char();
    277                 ersp = (ersp << 8) | _sdc_receive_char();
     277                ersp = _sdc_receive_char(sdcard);
     278                ersp = (ersp << 8) | _sdc_receive_char(sdcard);
     279                ersp = (ersp << 8) | _sdc_receive_char(sdcard);
     280                ersp = (ersp << 8) | _sdc_receive_char(sdcard);
    278281                if (ersp & 0x40000000)
    279282        {
     
    282285        else
    283286        {
    284                         sdcard.sdhc = 0;
     287                        sdcard->sdhc = 0;
    285288                }
    286                 _sdc_disable();
     289                _sdc_disable(sdcard);
    287290        }
    288291        printk("card detected\n");
     
    295298// Returns 0 if success, other value if failure
    296299///////////////////////////////////////////////////////////////////////////////
    297 static unsigned int _sdc_set_block_size(unsigned int len)
     300static unsigned int _sdc_set_block_size(sdcard_dev_t *sdcard, unsigned int len)
    298301{
    299302    unsigned char args[4];
     
    306309    // When using high capacity SDCARD, the block_length is not a number of bytes
    307310    // but a number of blocks (transfer unit)
    308     if (sdcard.sdhc)
    309     {
    310         sdcard.block_length = len / 512;
     311    if (sdcard->sdhc)
     312    {
     313        sdcard->block_length = len / 512;
    311314        return 0;
    312315    }
     
    317320    }
    318321
    319     _sdc_enable();
    320 
    321     sdcard_rsp = _sdc_send_command(16, SDCARD_CMD, args, 0x00);
     322    _sdc_enable(sdcard);
     323
     324    sdcard_rsp = _sdc_send_command(sdcard, 16, SDCARD_CMD, args, 0x00);
    322325    if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) )
    323326    {
    324         _sdc_disable();
     327        _sdc_disable(sdcard);
    325328        return sdcard_rsp;
    326329    }
    327330
    328     _sdc_disable();
    329 
    330     sdcard.block_length = len;
     331    _sdc_disable(sdcard);
     332
     333    sdcard->block_length = len;
    331334
    332335        return 0;
     
    340343void soclib_sdc_init( chdev_t* chdev )
    341344{
    342     spi = (struct spi_dev*)SEG_IOC_BASE;
    343 
    344345    // initializing the SPI controller
    345346    _spi_init (
    346         spi           ,
     347        chdev->base   ,
    347348        200000        , /**< SPI_clk: 200 Khz */
    348349        SYSCLK_FREQ   , /**< Sys_clk          */
     
    361362        printk("[SDC WARNING] Trying to initialize SD card...\n");
    362363
    363         sdcard_rsp = _sdc_open( 0 );  // only channel 0
     364        sdcard_rsp = _sdc_open(&global_sdcard, chdev->base, 0 );  // only channel 0
    364365        if (sdcard_rsp == 0)
    365366        {
     
    379380
    380381    // set the block length of the SD Card
    381     sdcard_rsp = _sdc_set_block_size(512);
     382    sdcard_rsp = _sdc_set_block_size(&global_sdcard, 512);
    382383    if (sdcard_rsp)
    383384    {
     
    387388    // incrementing SDCARD clock frequency for normal function
    388389    _spi_init (
    389         spi        ,
     390        chdev->base ,
    390391        10000000    , // SPI_clk 10 Mhz
    391392        SYSCLK_FREQ , // Sys_clk
     
    429430        for ( ; curr < last ; curr++ )
    430431        {
    431             _sdc_lseek(curr);
     432            _sdc_lseek(&global_sdcard, curr);
    432433
    433434            for (i = 0; i < 4; i++)
    434435            {
    435                 args[i] = (sdcard.access_pointer >> (32 - (i+1)*8)) & 0xFF;
     436                args[i] = (global_sdcard.access_pointer >> (32 - (i+1)*8)) & 0xFF;
    436437            }
    437438
    438             _sdc_enable();
    439 
    440             sdcard_rsp = _sdc_send_command(17, SDCARD_CMD, args, 0x00);
     439            _sdc_enable(&global_sdcard);
     440
     441            sdcard_rsp = _sdc_send_command(&global_sdcard, 17, SDCARD_CMD, args, 0x00);
    441442            if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) )
    442443            {
    443                 _sdc_disable();
     444                _sdc_disable(&global_sdcard);
    444445                return sdcard_rsp;
    445446            }
    446447
    447             _sdc_wait_data_block();
    448 
    449             if (spi_get_data(sdcard.spi, buf_xp, 512 ))
     448            _sdc_wait_data_block(&global_sdcard);
     449
     450            if (spi_get_data(global_sdcard.spi, buf_xp, 512 ))
    450451            {
    451                 _sdc_disable();
     452                _sdc_disable(&global_sdcard);
    452453                return 1;
    453454            }
    454455
    455456            // Get the CRC16 (comes at the end of the data block)
    456             _sdc_receive_char(); // first byte
    457             _sdc_receive_char(); // second byte
    458 
    459             _sdc_disable();
     457            _sdc_receive_char(&global_sdcard); // first byte
     458            _sdc_receive_char(&global_sdcard); // second byte
     459
     460            _sdc_disable(&global_sdcard);
    460461
    461462            buf_xp += 512;
  • trunk/hal/tsar_mips32/drivers/soclib_sdc.h

    r550 r558  
    1717// SD card structure definition
    1818///////////////////////////////////////////////////////////////////////////////
    19 struct sdcard_dev
     19typedef struct sdcard_dev
    2020{
    2121    // SPI controller pointer
     
    3636    // is the card high capacity ?
    3737    int sdhc;
    38 };
     38}
     39sdcard_dev_t;
    3940
    4041///////////////////////////////////////////////////////////////////////////////
Note: See TracChangeset for help on using the changeset viewer.