- Timestamp:
- Sep 21, 2018, 10:26:16 PM (6 years ago)
- Location:
- trunk/hal/tsar_mips32/drivers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/hal/tsar_mips32/drivers/soclib_sdc.c
r550 r558 19 19 #define SDCARD_RESET_ITER_MAX 4 20 20 21 extern chdev_directory_t chdev_dir; // allocated in the kernel_init.c file. 22 21 23 /////////////////////////////////////////////////////////////////////////////// 22 24 // Global variables … … 24 26 25 27 __attribute__((section(".kdata"))) 26 static struct sdcard_dev sdcard;28 static struct sdcard_dev global_sdcard; 27 29 28 30 __attribute__((section(".kdata"))) … … 32 34 // This function enables SD Card select signal 33 35 /////////////////////////////////////////////////////////////////////////////// 34 static void _sdc_enable( )35 { 36 spi_ss_assert(sdcard .spi, sdcard.slave_id);36 static void _sdc_enable(sdcard_dev_t *sdcard) 37 { 38 spi_ss_assert(sdcard->spi, sdcard->slave_id); 37 39 } 38 40 … … 40 42 // This function disables SD Card select signal 41 43 /////////////////////////////////////////////////////////////////////////////// 42 static void _sdc_disable( )43 { 44 spi_ss_deassert(sdcard .spi, sdcard.slave_id);44 static void _sdc_disable(sdcard_dev_t *sdcard) 45 { 46 spi_ss_deassert(sdcard->spi, sdcard->slave_id); 45 47 } 46 48 … … 49 51 // - tick_count: number of ticks to generate (1 tick -> 8 clocks) 50 52 /////////////////////////////////////////////////////////////////////////////// 51 static void _sdc_gen_tick( unsigned int tick_count)53 static void _sdc_gen_tick(sdcard_dev_t *sdcard, unsigned int tick_count) 52 54 { 53 55 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); 55 57 } 56 58 … … 60 62 // - lba: number of logical block to move the pointer 61 63 /////////////////////////////////////////////////////////////////////////////// 62 void _sdc_lseek( unsigned int lba)63 { 64 sdcard .access_pointer = sdcard.block_length * lba;64 void _sdc_lseek(sdcard_dev_t *sdcard, unsigned int lba) 65 { 66 sdcard->access_pointer = sdcard->block_length * lba; 65 67 } 66 68 … … 68 70 // This function gets a byte from the SD card 69 71 /////////////////////////////////////////////////////////////////////////////// 70 static unsigned char _sdc_receive_char( )71 { 72 _sdc_gen_tick( 1);73 74 return spi_get_rx(sdcard .spi, 0);72 static 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); 75 77 } 76 78 … … 80 82 // Returns the SD card response value 81 83 /////////////////////////////////////////////////////////////////////////////// 82 static unsigned char _sdc_wait_response( )84 static unsigned char _sdc_wait_response(sdcard_dev_t *sdcard) 83 85 { 84 86 unsigned char sdcard_rsp; … … 86 88 87 89 iter = 0; 88 sdcard_rsp = _sdc_receive_char( );90 sdcard_rsp = _sdc_receive_char(sdcard); 89 91 while ( 90 92 (iter < SDCARD_COMMAND_TIMEOUT) && … … 92 94 ) 93 95 { 94 sdcard_rsp = _sdc_receive_char( );96 sdcard_rsp = _sdc_receive_char(sdcard); 95 97 iter++; 96 98 } … … 104 106 // It must be called after a read command 105 107 /////////////////////////////////////////////////////////////////////////////// 106 static void _sdc_wait_data_block( )107 { 108 while (_sdc_receive_char( ) != 0xFE);108 static void _sdc_wait_data_block(sdcard_dev_t *sdcard) 109 { 110 while (_sdc_receive_char(sdcard) != 0xFE); 109 111 } 110 112 … … 116 118 // - crc7: CRC (7 bits) to send 117 119 /////////////////////////////////////////////////////////////////////////////// 118 static int _sdc_send_command ( int index, 120 static int _sdc_send_command ( sdcard_dev_t *sdcard, 121 int index, 119 122 int app , 120 123 void * args , … … 124 127 unsigned char * _args; 125 128 126 _sdc_gen_tick( 5);129 _sdc_gen_tick(sdcard, 5); 127 130 128 131 if (app == SDCARD_ACMD) 129 132 { 130 spi_put_tx(sdcard .spi, 0x40 | 55 , 0 );// CMD and START bit131 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 bit136 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); 138 141 if (SDCARD_CHECK_R1_ERROR(sdcard_rsp)) 139 142 { … … 144 147 _args = (unsigned char *) args; 145 148 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); 156 159 } 157 160 … … 161 164 // Returns 0 if success, other value if failure 162 165 /////////////////////////////////////////////////////////////////////////////// 163 static int _sdc_open( unsigned int channel )166 static int _sdc_open( sdcard_dev_t *sdcard, xptr_t base, unsigned int channel ) 164 167 { 165 168 unsigned char args[4]; … … 167 170 unsigned int iter, ersp; 168 171 169 sdcard .spi = spi;170 sdcard .slave_id = channel;172 sdcard->spi = base; 173 sdcard->slave_id = channel; 171 174 172 175 // supply SD card ramp up time (min 74 cycles) 173 _sdc_gen_tick( 10);176 _sdc_gen_tick(sdcard, 10); 174 177 175 178 // Assert slave select signal 176 179 // Send CMD0 (Reset Command) 177 180 // Deassert slave select signal 178 _sdc_enable( );181 _sdc_enable(sdcard); 179 182 180 183 args[0] = 0; … … 182 185 args[2] = 0; 183 186 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); 185 188 186 189 if ( sdcard_rsp != 0x01 ) … … 190 193 } 191 194 192 _sdc_disable( );195 _sdc_disable(sdcard); 193 196 194 197 // send CMD8. If card is pre-v2, It will reply with illegal command. 195 198 // Otherwise we announce sdhc support. 196 _sdc_enable( );199 _sdc_enable(sdcard); 197 200 args[0] = 0; 198 201 args[1] = 0; 199 202 args[2] = 0x01; 200 203 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); 202 205 if (!SDCARD_CHECK_R1_VALID(sdcard_rsp)) 203 206 { … … 208 211 { 209 212 // 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); 214 217 if ((ersp & 0xffff) != 0x0101) 215 218 { … … 219 222 } 220 223 printk("[SDC WARNING] v2 or later "); 221 sdcard .sdhc = 1;224 sdcard->sdhc = 1; 222 225 } 223 226 else if ((sdcard_rsp & SDCARD_R1_ILLEGAL_CMD) == 0) … … 229 232 else 230 233 { 231 sdcard .sdhc = 0;232 } 233 _sdc_disable( );234 sdcard->sdhc = 0; 235 } 236 _sdc_disable(sdcard); 234 237 235 238 // 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; 238 241 args[1] = 0; 239 242 args[2] = 0; … … 243 246 while( iter++ < SDCARD_COMMAND_TIMEOUT ) 244 247 { 245 sdcard_rsp = _sdc_send_command( 41, SDCARD_ACMD, args, 0x00);248 sdcard_rsp = _sdc_send_command(sdcard, 41, SDCARD_ACMD, args, 0x00); 246 249 if( sdcard_rsp == 0x01 ) 247 250 { … … 252 255 } 253 256 254 _sdc_disable( );257 _sdc_disable(sdcard); 255 258 if (sdcard_rsp) 256 259 { … … 258 261 return sdcard_rsp; 259 262 } 260 if (sdcard .sdhc != 0)263 if (sdcard->sdhc != 0) 261 264 { 262 265 // 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; 265 268 args[1] = 0; 266 269 args[2] = 0; 267 270 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); 269 272 if (sdcard_rsp) 270 273 { … … 272 275 return sdcard_rsp; 273 276 } 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); 278 281 if (ersp & 0x40000000) 279 282 { … … 282 285 else 283 286 { 284 sdcard .sdhc = 0;287 sdcard->sdhc = 0; 285 288 } 286 _sdc_disable( );289 _sdc_disable(sdcard); 287 290 } 288 291 printk("card detected\n"); … … 295 298 // Returns 0 if success, other value if failure 296 299 /////////////////////////////////////////////////////////////////////////////// 297 static unsigned int _sdc_set_block_size( unsigned int len)300 static unsigned int _sdc_set_block_size(sdcard_dev_t *sdcard, unsigned int len) 298 301 { 299 302 unsigned char args[4]; … … 306 309 // When using high capacity SDCARD, the block_length is not a number of bytes 307 310 // 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; 311 314 return 0; 312 315 } … … 317 320 } 318 321 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); 322 325 if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) ) 323 326 { 324 _sdc_disable( );327 _sdc_disable(sdcard); 325 328 return sdcard_rsp; 326 329 } 327 330 328 _sdc_disable( );329 330 sdcard .block_length = len;331 _sdc_disable(sdcard); 332 333 sdcard->block_length = len; 331 334 332 335 return 0; … … 340 343 void soclib_sdc_init( chdev_t* chdev ) 341 344 { 342 spi = (struct spi_dev*)SEG_IOC_BASE;343 344 345 // initializing the SPI controller 345 346 _spi_init ( 346 spi,347 chdev->base , 347 348 200000 , /**< SPI_clk: 200 Khz */ 348 349 SYSCLK_FREQ , /**< Sys_clk */ … … 361 362 printk("[SDC WARNING] Trying to initialize SD card...\n"); 362 363 363 sdcard_rsp = _sdc_open( 0 ); // only channel 0364 sdcard_rsp = _sdc_open(&global_sdcard, chdev->base, 0 ); // only channel 0 364 365 if (sdcard_rsp == 0) 365 366 { … … 379 380 380 381 // 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); 382 383 if (sdcard_rsp) 383 384 { … … 387 388 // incrementing SDCARD clock frequency for normal function 388 389 _spi_init ( 389 spi,390 chdev->base , 390 391 10000000 , // SPI_clk 10 Mhz 391 392 SYSCLK_FREQ , // Sys_clk … … 429 430 for ( ; curr < last ; curr++ ) 430 431 { 431 _sdc_lseek( curr);432 _sdc_lseek(&global_sdcard, curr); 432 433 433 434 for (i = 0; i < 4; i++) 434 435 { 435 args[i] = ( sdcard.access_pointer >> (32 - (i+1)*8)) & 0xFF;436 args[i] = (global_sdcard.access_pointer >> (32 - (i+1)*8)) & 0xFF; 436 437 } 437 438 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); 441 442 if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) ) 442 443 { 443 _sdc_disable( );444 _sdc_disable(&global_sdcard); 444 445 return sdcard_rsp; 445 446 } 446 447 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 )) 450 451 { 451 _sdc_disable( );452 _sdc_disable(&global_sdcard); 452 453 return 1; 453 454 } 454 455 455 456 // Get the CRC16 (comes at the end of the data block) 456 _sdc_receive_char( ); // first byte457 _sdc_receive_char( ); // second byte458 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); 460 461 461 462 buf_xp += 512; -
trunk/hal/tsar_mips32/drivers/soclib_sdc.h
r550 r558 17 17 // SD card structure definition 18 18 /////////////////////////////////////////////////////////////////////////////// 19 struct sdcard_dev19 typedef struct sdcard_dev 20 20 { 21 21 // SPI controller pointer … … 36 36 // is the card high capacity ? 37 37 int sdhc; 38 }; 38 } 39 sdcard_dev_t; 39 40 40 41 ///////////////////////////////////////////////////////////////////////////////
Note: See TracChangeset
for help on using the changeset viewer.