[724] | 1 | #include <reset_ioc.h> |
---|
| 2 | |
---|
| 3 | #if USE_SPI |
---|
| 4 | static struct sdcard_dev _sdcard_device; |
---|
| 5 | static struct spi_dev *const _spi_device = (struct spi_dev*) IOC_PADDR_BASE; |
---|
| 6 | #endif |
---|
| 7 | |
---|
| 8 | #define SDCARD_RESET_ITER_MAX 4 |
---|
| 9 | |
---|
| 10 | /////////////////////////////////// |
---|
| 11 | inline void reset_sleep(int cycles) |
---|
| 12 | { |
---|
| 13 | int i; |
---|
| 14 | for (i = 0; i < cycles; i++); |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | #if USE_SPI |
---|
| 18 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 19 | // reset_ioc_init |
---|
| 20 | // This function initializes the SDCARD / required for FPGA. |
---|
| 21 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 22 | int reset_ioc_init() |
---|
| 23 | { |
---|
| 24 | unsigned char sdcard_rsp; |
---|
| 25 | |
---|
| 26 | reset_puts("Initializing block device\n\r"); |
---|
| 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 |
---|
| 38 | ); |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * Initializing the SD Card |
---|
| 42 | */ |
---|
| 43 | unsigned int iter = 0; |
---|
| 44 | while(1) |
---|
| 45 | { |
---|
| 46 | reset_puts("Trying to initialize SD card... "); |
---|
| 47 | |
---|
| 48 | sdcard_rsp = sdcard_dev_open(&_sdcard_device, _spi_device, 0); |
---|
| 49 | if (sdcard_rsp == 0) |
---|
| 50 | { |
---|
| 51 | reset_puts("OK\n"); |
---|
| 52 | break; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | reset_puts("KO\n"); |
---|
| 56 | reset_sleep(1000); |
---|
| 57 | if (++iter >= SDCARD_RESET_ITER_MAX) |
---|
| 58 | { |
---|
| 59 | reset_puts("\nERROR: During SD card reset to IDLE state\n" |
---|
| 60 | "/ card response = "); |
---|
| 61 | reset_putx(sdcard_rsp); |
---|
| 62 | reset_puts("\n"); |
---|
| 63 | reset_exit(); |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
| 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 | { |
---|
| 73 | reset_puts("ERROR: During SD card blocklen initialization\n"); |
---|
| 74 | reset_exit(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * Incrementing SDCARD clock frequency for normal function |
---|
| 79 | */ |
---|
| 80 | spi_dev_config ( |
---|
| 81 | _spi_device , |
---|
| 82 | 10000000 , /**< SPI_clk 10 Mhz */ |
---|
| 83 | SYSCLK_FREQ , /**< Sys_clk */ |
---|
| 84 | -1 , /**< Charlen: 8 */ |
---|
| 85 | -1 , |
---|
| 86 | -1 |
---|
| 87 | ); |
---|
| 88 | |
---|
| 89 | reset_puts("Finish block device initialization\n\r"); |
---|
| 90 | |
---|
| 91 | return 0; |
---|
| 92 | } // end reset_ioc_init() |
---|
| 93 | #endif |
---|
| 94 | |
---|
| 95 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 96 | // reset_bdv_read() |
---|
| 97 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 98 | #if USE_BDV |
---|
| 99 | int reset_bdv_read( unsigned int lba, |
---|
| 100 | void* buffer, |
---|
| 101 | unsigned int count ) |
---|
| 102 | { |
---|
| 103 | unsigned int * ioc_address = (unsigned int*)IOC_PADDR_BASE; |
---|
| 104 | |
---|
| 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 ); |
---|
| 110 | |
---|
| 111 | // block_device trigger transfer |
---|
| 112 | iowrite32( &ioc_address[BLOCK_DEVICE_OP], ( unsigned int ) |
---|
| 113 | BLOCK_DEVICE_READ ); |
---|
| 114 | |
---|
| 115 | unsigned int status = 0; |
---|
| 116 | while ( 1 ) |
---|
| 117 | { |
---|
| 118 | status = ioread32(&ioc_address[BLOCK_DEVICE_STATUS]); |
---|
| 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 | } |
---|
| 127 | } |
---|
| 128 | #if (CACHE_COHERENCE == 0) || USE_IOB |
---|
| 129 | reset_buf_invalidate(buffer, CACHE_LINE_SIZE, count * 512); |
---|
| 130 | #endif |
---|
| 131 | return 0; |
---|
| 132 | } |
---|
| 133 | #endif |
---|
| 134 | |
---|
| 135 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 136 | // reset_spi_read() |
---|
| 137 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 138 | #if USE_SPI |
---|
| 139 | static int reset_spi_read( unsigned int lba, |
---|
| 140 | void* buffer, |
---|
| 141 | unsigned int count ) |
---|
| 142 | { |
---|
| 143 | unsigned int sdcard_rsp; |
---|
| 144 | unsigned int i; |
---|
| 145 | |
---|
| 146 | sdcard_dev_lseek(&_sdcard_device, lba); |
---|
| 147 | for(i = 0; i < count; i++) |
---|
| 148 | { |
---|
| 149 | unsigned char* buf = (unsigned char *) buffer + (512 * i); |
---|
| 150 | if (( sdcard_rsp = sdcard_dev_read ( &_sdcard_device, buf, 512 ) )) |
---|
| 151 | { |
---|
| 152 | reset_puts("ERROR during read on the SDCARD device. Code: "); |
---|
| 153 | reset_putx(sdcard_rsp); |
---|
| 154 | reset_puts("\n"); |
---|
| 155 | return 1; |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | return 0; |
---|
| 159 | } |
---|
| 160 | #endif |
---|
| 161 | |
---|
| 162 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 163 | // reset_rdk_read() |
---|
| 164 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 165 | #if USE_RDK |
---|
| 166 | static int reset_rdk_read( unsigned int lba, |
---|
| 167 | void* buffer, |
---|
| 168 | unsigned int count ) |
---|
| 169 | { |
---|
| 170 | unsigned int* rdk_address = (unsigned int*) RDK_PADDR_BASE; |
---|
| 171 | char* src = (char*) rdk_address + (lba * 512); |
---|
| 172 | |
---|
| 173 | memcpy(buffer, (void*) src, count * 512); |
---|
| 174 | return 0; |
---|
| 175 | } |
---|
| 176 | #endif |
---|
| 177 | |
---|
| 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 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 190 | int reset_ioc_read( unsigned int lba, |
---|
| 191 | void* buffer, |
---|
| 192 | unsigned int count ) |
---|
| 193 | { |
---|
| 194 | #if USE_BDV |
---|
| 195 | return reset_bdv_read(lba, buffer, count); |
---|
| 196 | #elif USE_SPI |
---|
| 197 | return reset_spi_read(lba, buffer, count); |
---|
| 198 | #elif USE_RDK |
---|
| 199 | return reset_rdk_read(lba, buffer, count); |
---|
| 200 | #else |
---|
| 201 | # error "reset_ioc_read() : No supported disk controller chosen" |
---|
| 202 | #endif |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /* |
---|
| 206 | * vim: tabstop=4 : shiftwidth=4 : expandtab |
---|
| 207 | */ |
---|