Changeset 758 for trunk/softs/tsar_boot/src/reset_ioc.c
- Timestamp:
- Jul 24, 2014, 3:19:18 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/softs/tsar_boot/src/reset_ioc.c
r654 r758 1 /** 2 * \file reset_ioc.c 3 * \date December 2013 4 * \author Cesar Fuguet 5 * 6 * \brief API for accessing the disk controller 7 * 8 * \note These functions call the specific disk controller driver depending 9 * on the USE_IOC_BDV, USE_IOC_SPI or USE_RAMDISK constants 10 */ 11 1 12 #include <reset_ioc.h> 13 #include <defs.h> 2 14 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; 15 #if !defined(USE_IOC_BDV) && !defined(USE_IOC_SPI) && !defined(USE_RAMDISK) 16 # error "One of the USE_IOC_* constants must be defined in the hard_config.h" 6 17 #endif 7 18 8 #define SDCARD_RESET_ITER_MAX 4 19 #if (USE_IOC_BDV + USE_IOC_SPI + USE_RAMDISK) != 1 20 # error "Only one disk controller must be used" 21 #endif 9 22 10 /////////////////////////////////// 11 inline void reset_sleep(int cycles) 23 #if USE_IOC_SPI 24 #include <reset_sdc.h> 25 #endif 26 27 #if USE_IOC_BDV 28 #include <reset_bdv.h> 29 #endif 30 31 #if USE_RAMDISK 32 #include <reset_rdk.h> 33 #endif 34 35 /** 36 * \brief Initialize the disk controller 37 */ 38 int reset_ioc_init() 12 39 { 13 int i; 14 for (i = 0; i < cycles; i++); 40 #if USE_IOC_BDV 41 return reset_bdv_init(); 42 #elif USE_IOC_SPI 43 return reset_sdc_init(); 44 #elif USE_RAMDISK 45 return reset_rdk_init(); 46 #else 47 # error "reset_ioc_init() : Not supported disk controller chosen" 48 #endif 15 49 } 16 50 17 #if USE_SPI 18 /////////////////////////////////////////////////////////////////////////////// 19 // reset_ioc_init 20 // This function initializes the SDCARD / required for FPGA. 21 /////////////////////////////////////////////////////////////////////////////// 22 int reset_ioc_init() 51 /** 52 * \param lba : first block index on the disk 53 * \param buffer: base address of the memory buffer 54 * \param count : number of blocks to be transfered 55 * 56 * \brief Transfer data from disk to a memory buffer 57 * 58 * \note This is a blocking function. The function returns once the transfer 59 * is completed. 60 */ 61 int reset_ioc_read( unsigned int lba, void* buffer, unsigned int count ) 23 62 { 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 63 #if USE_IOC_BDV 195 64 return reset_bdv_read(lba, buffer, count); 196 #elif USE_ SPI197 return reset_s pi_read(lba, buffer, count);198 #elif USE_R DK65 #elif USE_IOC_SPI 66 return reset_sdc_read(lba, buffer, count); 67 #elif USE_RAMDISK 199 68 return reset_rdk_read(lba, buffer, count); 200 69 #else 201 # error "reset_ioc_read() : No supported disk controller chosen"70 # error "reset_ioc_read() : Not supported disk controller chosen" 202 71 #endif 203 72 } 204 73 205 74 /* 206 * vim: tabstop=4 : s hiftwidth=4 : expandtab75 * vim: tabstop=4 : softtabstop=4 : shiftwidth=4 : expandtab 207 76 */
Note: See TracChangeset
for help on using the changeset viewer.