source: trunk/softs/tsar_boot/src/reset_ioc.c @ 653

Last change on this file since 653 was 653, checked in by cfuguet, 10 years ago

Introducing a RAMDISK driver in the preloader.

When using RAMDISK, execute the make command with the flags
SOCLIB=1 and RAMDISK=1. The RDK_PADDR_BASE variable must
also be set on the conf/<platform>/defs_platform.h

These modifications are backward compatibles. Therefore,
when no using RAMDISK, none modifications applied on the
platform configuration file.

File size: 5.8 KB
RevLine 
[586]1#include <reset_ioc.h>
[292]2
[653]3#if USE_SPI
4static struct sdcard_dev     _sdcard_device;
5static struct spi_dev *const _spi_device = (struct spi_dev*) IOC_PADDR_BASE;
[586]6#endif
[302]7
[586]8#define SDCARD_RESET_ITER_MAX   4
[302]9
[586]10///////////////////////////////////
11inline void reset_sleep(int cycles)
[388]12{
13    int i;
14    for (i = 0; i < cycles; i++);
15}
16
[653]17#if USE_SPI
18///////////////////////////////////////////////////////////////////////////////
[586]19//     reset_ioc_init
20// This function initializes the SDCARD / required for FPGA.
[653]21///////////////////////////////////////////////////////////////////////////////
[586]22int reset_ioc_init()
[292]23{
24    unsigned char sdcard_rsp;
25
[586]26    reset_puts("Initializing block device\n\r");
[292]27
28    /**
29     * Initializing the SPI controller
30     */
31    spi_dev_config (
32      _spi_device   ,
33      200000        , /**< SPI_clk: 200 Khz */
34      SYSCLK_FREQ   , /**< Sys_clk          */
35      8             , /**< Charlen: 8       */
36      SPI_TX_NEGEDGE,
37      SPI_RX_POSEDGE
[412]38    );
[292]39
40    /**
41     * Initializing the SD Card
42     */
[388]43    unsigned int iter = 0;
[412]44    while(1)
[388]45    {
[586]46        reset_puts("Trying to initialize SD card... ");
[292]47
[388]48        sdcard_rsp = sdcard_dev_open(&_sdcard_device, _spi_device, 0);
49        if (sdcard_rsp == 0)
50        {
[586]51            reset_puts("OK\n");
[388]52            break;
53        }
[292]54
[586]55        reset_puts("KO\n");
56        reset_sleep(1000);
[388]57        if (++iter >= SDCARD_RESET_ITER_MAX)
58        {
[586]59            reset_puts("\nERROR: During SD card reset to IDLE state\n"
[388]60                      "/ card response = ");
[586]61            reset_putx(sdcard_rsp);
62            reset_puts("\n");
63            reset_exit();
[388]64        }
65    }
66
[292]67    /**
[388]68     * Set the block length of the SD Card
69     */
70    sdcard_rsp = sdcard_dev_set_blocklen(&_sdcard_device, 512);
71    if (sdcard_rsp)
72    {
[586]73        reset_puts("ERROR: During SD card blocklen initialization\n");
74        reset_exit();
[388]75    }
76
77    /**
[292]78     * Incrementing SDCARD clock frequency for normal function
79     */
80    spi_dev_config (
81        _spi_device ,
[388]82        10000000    , /**< SPI_clk 10 Mhz */
83        SYSCLK_FREQ , /**< Sys_clk        */
84        -1          , /**< Charlen: 8     */
[292]85        -1          ,
86        -1
87    );
88
[586]89    reset_puts("Finish block device initialization\n\r");
[292]90
91    return 0;
[586]92} // end reset_ioc_init()
93#endif
[292]94
[653]95//////////////////////////////////////////////////////////////////////////////
96// reset_bdv_read()
97/////////////////////////////////////////////////////////////////////////////
98#if USE_BDV
99int reset_bdv_read( unsigned int lba, 
100                    void*        buffer, 
101                    unsigned int count )
[292]102{
[653]103    unsigned int * ioc_address = (unsigned int*)IOC_PADDR_BASE;
[292]104
[653]105    // block_device configuration
106    iowrite32( &ioc_address[BLOCK_DEVICE_BUFFER], (unsigned int) buffer );
107    iowrite32( &ioc_address[BLOCK_DEVICE_COUNT], count );
108    iowrite32( &ioc_address[BLOCK_DEVICE_LBA], lba );
109    iowrite32( &ioc_address[BLOCK_DEVICE_IRQ_ENABLE], 0 );
[292]110
[653]111    // block_device trigger transfer
112    iowrite32( &ioc_address[BLOCK_DEVICE_OP], ( unsigned int )
113               BLOCK_DEVICE_READ );
114
115    unsigned int status = 0;
[292]116    while ( 1 )
[412]117    {
[292]118        status = ioread32(&ioc_address[BLOCK_DEVICE_STATUS]);
[653]119        if ( status == BLOCK_DEVICE_READ_SUCCESS )
120        {
121            break;
122        }
123        if ( status == BLOCK_DEVICE_READ_ERROR   ) {
124            reset_puts("ERROR during read on the BLK device\n");
125            return 1;
126        }
[292]127    }
[653]128#if (CACHE_COHERENCE == 0) || USE_IOB
[586]129    reset_buf_invalidate(buffer, CACHE_LINE_SIZE, count * 512);
[347]130#endif
[653]131    return 0;
132}
[568]133#endif
134
[653]135//////////////////////////////////////////////////////////////////////////////
136// reset_spi_read()
137/////////////////////////////////////////////////////////////////////////////
138#if USE_SPI
139static int reset_spi_read( unsigned int lba, 
140                           void*        buffer, 
141                           unsigned int count )
[347]142{
[292]143    unsigned int sdcard_rsp;
[388]144    unsigned int i;
[292]145
146    sdcard_dev_lseek(&_sdcard_device, lba);
147    for(i = 0; i < count; i++)
148    {
[653]149        unsigned char* buf = (unsigned char *) buffer + (512 * i);
150        if (( sdcard_rsp = sdcard_dev_read ( &_sdcard_device, buf, 512 ) ))
[292]151        {
[586]152            reset_puts("ERROR during read on the SDCARD device. Code: ");
153            reset_putx(sdcard_rsp);
[653]154            reset_puts("\n");
[292]155            return 1;
[388]156        }
[292]157    }
158    return 0;
[653]159}
[347]160#endif
[292]161
[586]162//////////////////////////////////////////////////////////////////////////////
[653]163// reset_rdk_read()
[586]164/////////////////////////////////////////////////////////////////////////////
[653]165#if USE_RDK
166static int reset_rdk_read( unsigned int lba,
167                           void*        buffer,
168                           unsigned int count )
[347]169{
[653]170    unsigned int* rdk_address = (unsigned int*) RDK_PADDR_BASE;
171    char* src = (char*) rdk_address + (lba * 512);
[292]172
[653]173    memcpy(buffer, (void*) src, count * 512);
174    return 0;
[347]175}
176#endif
177
[653]178///////////////////////////////////////////////////////////////////////////////
179//      reset_ioc_read()
180// Transfer data from disk to a memory buffer
181// - param lba    : first block index on the disk
182// - param buffer : base address of the memory buffer
183// - param count  : number of blocks to be transfered
184// This is a blocking function. The function returns once the transfer is
185// completed.
186//
187// The USE_BDV, USE_SPI and USE_RDK variables signal if the disk is accessed
188// through a BLOCK DEVICE, SPI or RAMDISK respectively
189///////////////////////////////////////////////////////////////////////////////
190int reset_ioc_read( unsigned int lba, 
191                    void*        buffer, 
192                    unsigned int count )
[568]193{
[653]194    int status;
195#if USE_BDV
196    status = reset_bdv_read(lba, buffer, count);
197#endif
198#if USE_SPI
199    status = reset_spi_read(lba, buffer, count);
200#endif
201#if USE_RDK
202    status = reset_rdk_read(lba, buffer, count);
203#endif
204    return status;
[568]205}
206
[292]207/*
208 * vim: tabstop=4 : shiftwidth=4 : expandtab
209 */
Note: See TracBrowser for help on using the repository browser.