Changeset 284 for branch/giet_vm_ioc_drivers/giet_drivers/sdc_driver.c
- Timestamp:
- Jan 31, 2014, 2:37:38 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branch/giet_vm_ioc_drivers/giet_drivers/sdc_driver.c
r283 r284 1 /** 2 * \file : sdc_driver.c 3 * \date : 30 August 2012 4 * \author : Cesar Fuguet 5 * 6 * This file defines the driver of a SD Card device using an SPI controller 7 */ 8 1 /////////////////////////////////////////////////////////////////////////////////// 2 // File : sdc_driver.c 3 // Date : 31/08/2012 4 // Author : cesar fuguet 5 // Copyright (c) UPMC-LIP6 6 /////////////////////////////////////////////////////////////////////////////////// 9 7 #include <sdc_driver.h> 10 8 #include <utils.h> … … 16 14 static struct spi_dev* spi; 17 15 18 /** 19 * \return void 20 * 21 * \brief Enable SD Card select signal 22 */ 16 /////////////////////////////////////////////////////////////////////////////// 17 // _sdc_enable() 18 // This function enables SD Card select signal 19 /////////////////////////////////////////////////////////////////////////////// 23 20 static void _sdc_enable() 24 21 { … … 26 23 } 27 24 28 /** 29 * \return void 30 * 31 * \brief Disable SD Card select signal 32 */ 25 /////////////////////////////////////////////////////////////////////////////// 26 // _sdc_enable() 27 // This function disables SD Card select signal 28 /////////////////////////////////////////////////////////////////////////////// 33 29 static void _sdc_disable() 34 30 { … … 36 32 } 37 33 38 /** 39 * \param tick_count: SD Card clock ticks number 40 * 41 * \return void 42 * 43 * \brief Enable SD Card clock 44 * The tick count is byte measured (1 tick, 8 clock) 45 */ 34 /////////////////////////////////////////////////////////////////////////////// 35 // _sdc_gen_tick() 36 // This function writes on the SPI tx register to generate SD card clock ticks 37 // - tick_count: number of ticks to generate (1 tick -> 8 clocks) 38 /////////////////////////////////////////////////////////////////////////////// 46 39 static void _sdc_gen_tick(unsigned int tick_count) 47 40 { … … 50 43 } 51 44 52 /** 53 * \param lba : Position where the block device access 54 * pointer must be move 55 * 56 * \return void 57 * 58 * \brief Change block device access pointer position 59 * 60 * The block device access pointer is relocated in terms of blocks 61 */ 45 /////////////////////////////////////////////////////////////////////////////// 46 // _sdc_lseek() 47 // This function changes the SD card access pointer position in terms of 48 // blocks 49 // - lba: number of logical block to move the pointer 50 /////////////////////////////////////////////////////////////////////////////// 62 51 void _sdc_lseek(unsigned int lba) 63 52 { … … 65 54 } 66 55 67 /** 68 * \return char from the SD card 69 * 70 * \brief Get a byte from the SD Card 71 */ 56 /////////////////////////////////////////////////////////////////////////////// 57 // _sdc_receive_char() 58 // This function gets a byte from the SD card 59 /////////////////////////////////////////////////////////////////////////////// 72 60 static unsigned char _sdc_receive_char() 73 61 { … … 77 65 } 78 66 79 /** 80 * \return sdcard response 81 * 82 * \brief Wait for a valid response after the send of a command 83 * This function can return if one of the next two conditions are true: 84 * 1. Bit valid received 85 * 2. Timeout (not valid bit received after SDCARD_COMMAND_TIMEOUT 86 * wait ticks) 87 */ 67 /////////////////////////////////////////////////////////////////////////////// 68 // _sdc_wait_response() 69 // This function returns when a valid response from the SD card is received or 70 // a timeout has been triggered 71 // Returns the SD card response value 72 /////////////////////////////////////////////////////////////////////////////// 88 73 static unsigned char _sdc_wait_response() 89 74 { … … 105 90 } 106 91 107 /** 108 * \return void 109 * 110 * \brief Wait data block start marker 111 */ 92 /////////////////////////////////////////////////////////////////////////////// 93 // _sdc_wait_data_block() 94 // This function returns when a data block from the SD card is received (data 95 // block start marker received). 96 // It must be called after a read command 97 /////////////////////////////////////////////////////////////////////////////// 112 98 static void _sdc_wait_data_block() 113 99 { … … 115 101 } 116 102 117 /** 118 * \param index : SD card CMD index 119 * \param app : Type of command, 0 for normal command or 1 for 120 * application specific 121 * \param args : SD card CMD arguments 122 * 123 * \return response first byte 124 * 125 * \brief Send command to the SD card 126 */ 103 /////////////////////////////////////////////////////////////////////////////// 104 // _sdc_send_command() 105 // This function sends a command to the SD card 106 // - index: CMD index 107 // - app: type of command 108 // - args: CMD arguments vector 109 // - crc7: CRC (7 bits) to send 110 /////////////////////////////////////////////////////////////////////////////// 127 111 static int _sdc_send_command ( int index, 128 112 int app , … … 137 121 if (app == SDCARD_ACMD) 138 122 { 139 spi_put_tx(sdcard.spi, 0x40 | 55 , 0 );/ * CMD and START bit */140 spi_put_tx(sdcard.spi, 0x00 , 0 );/ * Argument[0] */141 spi_put_tx(sdcard.spi, 0x00 , 0 );/ * Argument[1] */142 spi_put_tx(sdcard.spi, 0x00 , 0 );/ * Argument[2] */143 spi_put_tx(sdcard.spi, 0x00 , 0 );/ * Argument[3] */144 spi_put_tx(sdcard.spi, 0x01 | (crc7 << 1), 0 );/ * END bit */123 spi_put_tx(sdcard.spi, 0x40 | 55 , 0 );// CMD and START bit 124 spi_put_tx(sdcard.spi, 0x00 , 0 );// Argument[0] 125 spi_put_tx(sdcard.spi, 0x00 , 0 );// Argument[1] 126 spi_put_tx(sdcard.spi, 0x00 , 0 );// Argument[2] 127 spi_put_tx(sdcard.spi, 0x00 , 0 );// Argument[3] 128 spi_put_tx(sdcard.spi, 0x01 | (crc7 << 1), 0 );// END bit 145 129 146 130 sdcard_rsp = _sdc_wait_response(); … … 165 149 } 166 150 151 /////////////////////////////////////////////////////////////////////////////// 152 // _sdc_open() 153 // This function initializes the SD card (reset procedure) 154 // - channel: channel index (only channel 0 is supported) 155 // Returns 0 if success, other value if failure 156 /////////////////////////////////////////////////////////////////////////////// 167 157 static int _sdc_open( unsigned int channel ) 168 158 { … … 174 164 sdcard.slave_id = channel; 175 165 176 /* 177 * Supply SD card ramp up time (min 74 cycles) 178 */ 166 // supply SD card ramp up time (min 74 cycles) 179 167 _sdc_gen_tick(10); 180 168 181 /* 182 * Assert slave select signal 183 * Send CMD0 (Reset Command) 184 * Deassert slave select signal 185 */ 169 // Assert slave select signal 170 // Send CMD0 (Reset Command) 171 // Deassert slave select signal 186 172 _sdc_enable(); 187 173 … … 199 185 200 186 _sdc_disable(); 201 /* 202 * send CMD8. If card is pre-v2, It will reply with illegal command. 203 * Otherwise we announce sdhc support. 204 */ 187 188 // send CMD8. If card is pre-v2, It will reply with illegal command. 189 // Otherwise we announce sdhc support. 205 190 _sdc_enable(); 206 191 args[0] = 0; … … 209 194 args[3] = 0x01; 210 195 sdcard_rsp = _sdc_send_command(8, SDCARD_CMD, args, 0x63); 211 if (!SDCARD_CHECK_R1_VALID(sdcard_rsp)) { 196 if (!SDCARD_CHECK_R1_VALID(sdcard_rsp)) 197 { 212 198 _puts("card CMD8 failed "); 213 199 return sdcard_rsp; 214 200 } 215 if (!SDCARD_CHECK_R1_ERROR(sdcard_rsp)) { 216 /* no error, command accepted. get whole reply */ 201 if (!SDCARD_CHECK_R1_ERROR(sdcard_rsp)) 202 { 203 // no error, command accepted. get whole reply 217 204 ersp = _sdc_receive_char(); 218 205 ersp = (ersp << 8) | _sdc_receive_char(); … … 220 207 ersp = (ersp << 8) | _sdc_receive_char(); 221 208 if ((ersp & 0xffff) != 0x0101) { 222 / * voltage mismatch */209 // voltage mismatch 223 210 _puts("card CMD8 mismatch: "); 224 211 _putx(ersp); … … 227 214 _puts("v2 or later "); 228 215 sdcard.sdhc = 1; 229 } else if ((sdcard_rsp & SDCARD_R1_ILLEGAL_CMD) == 0) { 230 /* other error */ 216 } 217 else if ((sdcard_rsp & SDCARD_R1_ILLEGAL_CMD) == 0) 218 { 219 // other error 231 220 _puts("card CMD8 error "); 232 221 return sdcard_rsp; 233 } else { 222 } 223 else 224 { 234 225 sdcard.sdhc = 0; 235 226 } 236 227 _sdc_disable(); 237 /* send CMD41, enabling the card */ 228 229 // send CMD41, enabling the card 238 230 _sdc_enable(); 239 231 args[0] = sdcard.sdhc ? 0x40: 0; … … 255 247 256 248 _sdc_disable(); 257 if (sdcard_rsp) { 249 if (sdcard_rsp) 250 { 258 251 _puts("SD ACMD41 failed "); 259 252 return sdcard_rsp; 260 253 } 261 if (sdcard.sdhc != 0) { 262 /* get the card capacity to see if it's really HC */ 254 if (sdcard.sdhc != 0) 255 { 256 // get the card capacity to see if it's really HC 263 257 _sdc_enable(); 264 258 args[0] = sdcard.sdhc ? 0x40: 0; … … 267 261 args[3] = 0; 268 262 sdcard_rsp = _sdc_send_command(58, SDCARD_CMD, args, 0x00); 269 if (sdcard_rsp) { 263 if (sdcard_rsp) 264 { 270 265 _puts("SD CMD58 failed "); 271 266 return sdcard_rsp; … … 275 270 ersp = (ersp << 8) | _sdc_receive_char(); 276 271 ersp = (ersp << 8) | _sdc_receive_char(); 277 if (ersp & 0x40000000) { 272 if (ersp & 0x40000000) 273 { 278 274 _puts("SDHC "); 279 } else { 275 } else 276 { 280 277 sdcard.sdhc = 0; 281 278 } … … 286 283 } 287 284 285 /////////////////////////////////////////////////////////////////////////////// 286 // _sdc_set_block_size() 287 // This function sets the block size in bytes of the SD card 288 // - len: block size in bytes (only 512 bytes supported) 289 // Returns 0 if success, other value if failure 290 /////////////////////////////////////////////////////////////////////////////// 288 291 static unsigned int _sdc_set_block_size(unsigned int len) 289 292 { … … 292 295 register int i; 293 296 294 /* 295 * For now, supported block size is 512 bytes 296 */ 297 // For now, supported block size is 512 bytes 297 298 if (len != 512) return 1; 298 299 299 /* 300 * When using high capacity SDCARD, the block_length is not a number of bytes 301 * but a number of blocks (transfer unit) 302 */ 300 // When using high capacity SDCARD, the block_length is not a number of bytes 301 // but a number of blocks (transfer unit) 303 302 if (sdcard.sdhc) 304 303 { … … 328 327 } 329 328 329 /////////////////////////////////////////////////////////////////////////////// 330 // _sdc_init() 331 // This function initializes the SPI controller and call sdc_open to 332 // initializes SD card 333 // - channel: channel to initialize (only channel 0 supported) 334 // Returns 0 if success, other value if failure 335 /////////////////////////////////////////////////////////////////////////////// 330 336 unsigned int _sdc_init( unsigned int channel ) 331 337 { 332 338 spi = (struct spi_dev*) &seg_ioc_base; 333 339 334 /** 335 * Initializing the SPI controller 336 */ 340 // initializing the SPI controller 337 341 _spi_init ( 338 342 spi , … … 344 348 ); 345 349 346 /** 347 * Initializing the SD Card 348 */ 350 // initializing the SD Card 349 351 unsigned int iter = 0; 350 352 unsigned char sdcard_rsp; … … 376 378 } 377 379 378 /** 379 * Set the block length of the SD Card 380 */ 380 // set the block length of the SD Card 381 381 sdcard_rsp = _sdc_set_block_size(512); 382 382 if (sdcard_rsp) … … 386 386 } 387 387 388 /** 389 * Incrementing SDCARD clock frequency for normal function 390 */ 388 // incrementing SDCARD clock frequency for normal function 391 389 _spi_init ( 392 390 spi , 393 10000000 , / **< SPI_clk 10 Mhz */394 SYSCLK_FREQ , / **< Sys_clk */395 -1 , / **< Charlen: 8 */391 10000000 , // SPI_clk 10 Mhz 392 SYSCLK_FREQ , // Sys_clk 393 -1 , // Charlen: 8 396 394 -1 , 397 395 -1 … … 450 448 } 451 449 452 /* 453 * Get the CRC16 (comes at the end of the data block) 454 */ 450 // Get the CRC16 (comes at the end of the data block) 455 451 _sdc_receive_char(); // first byte 456 452 _sdc_receive_char(); // second byte … … 464 460 } 465 461 462 /////////////////////////////////////////////////////////////////////////////// 463 // _sdc_write() (not supported for now) 464 // Transfer data from memory buffer to SD card device. 465 // - mode : BOOT / KERNEL / USER 466 // - lba : destination first block index on the SD card 467 // - buffer : base address of the memory buffer (must be word aligned) 468 // - count : number of blocks to be transfered. 469 // Returns 0 if success, > 0 if error. 470 /////////////////////////////////////////////////////////////////////////////// 466 471 unsigned int _sdc_write( unsigned int mode, 467 472 unsigned int lba, … … 472 477 } 473 478 479 /////////////////////////////////////////////////////////////////////////////// 480 // _sdc_get_status() 481 // Transfer data from memory buffer to SD card device. 482 // - channel: channel index 483 // - status: this pointer is used to transmit the status value to caller. 484 // Returns 0 if success, > 0 if error. 485 /////////////////////////////////////////////////////////////////////////////// 474 486 unsigned int _sdc_get_status( unsigned int channel , 475 487 unsigned int* status ) 476 488 { 477 return BLOCK_DEVICE_IDLE; 478 } 479 489 *status = BLOCK_DEVICE_IDLE; 490 491 return 0; 492 } 493 494 /////////////////////////////////////////////////////////////////////////////// 495 // _sdc_get_block_size() 496 // Returns the block size in bytes of the SD card 497 /////////////////////////////////////////////////////////////////////////////// 480 498 unsigned int _sdc_get_block_size() 481 499 { … … 484 502 } 485 503 486 487 488 /* 489 * vim: tabstop=4 : shiftwidth=4 : expandtab : softtabstop=4 490 */ 504 // Local Variables: 505 // tab-width: 4 506 // c-basic-offset: 4 507 // c-file-offsets:((innamespace . 0)(inline-open . 0)) 508 // indent-tabs-mode: nil 509 // End: 510 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
Note: See TracChangeset
for help on using the changeset viewer.