[292] | 1 | /** |
---|
| 2 | * \file : sdcard.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 | |
---|
| 9 | #include <sdcard.h> |
---|
[586] | 10 | #include <reset_tty.h> |
---|
[292] | 11 | |
---|
| 12 | /** |
---|
| 13 | * \param sdcard: Initialized pointer to the block device |
---|
| 14 | * |
---|
| 15 | * \return void |
---|
| 16 | * |
---|
| 17 | * \brief Enable SD Card select signal |
---|
| 18 | */ |
---|
| 19 | static void _sdcard_enable(struct sdcard_dev * sdcard) |
---|
| 20 | { |
---|
| 21 | spi_ss_assert(sdcard->spi, sdcard->slave_id); |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * \param sdcard: Initialized pointer to the block device |
---|
| 26 | * |
---|
| 27 | * \return void |
---|
| 28 | * |
---|
| 29 | * \brief Disable SD Card select signal |
---|
| 30 | */ |
---|
| 31 | static void _sdcard_disable(struct sdcard_dev * sdcard) |
---|
| 32 | { |
---|
| 33 | spi_ss_deassert(sdcard->spi, sdcard->slave_id); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * \param tick_count: SD Card clock ticks number |
---|
| 38 | * |
---|
| 39 | * \return void |
---|
| 40 | * |
---|
| 41 | * \brief Enable SD Card clock |
---|
| 42 | * The tick count is byte measured (1 tick, 8 clock) |
---|
| 43 | */ |
---|
| 44 | static void _sdcard_gen_tick(struct sdcard_dev * sdcard, unsigned int tick_count) |
---|
| 45 | { |
---|
[388] | 46 | register int i = 0; |
---|
[292] | 47 | while(i++ < tick_count) spi_put_tx(sdcard->spi, 0xFF, 0); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * \param sdcard: Initialized pointer to the block device |
---|
| 52 | * |
---|
| 53 | * \return char from the SD card |
---|
| 54 | * |
---|
| 55 | * \brief Get a byte from the SD Card |
---|
| 56 | */ |
---|
| 57 | static unsigned char _sdcard_receive_char(struct sdcard_dev * sdcard) |
---|
| 58 | { |
---|
| 59 | _sdcard_gen_tick(sdcard, 1); |
---|
| 60 | |
---|
| 61 | return spi_get_rx(sdcard->spi, 0); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * \param sdcard: Initialized pointer to the block device |
---|
| 66 | * |
---|
| 67 | * \return sdcard response |
---|
| 68 | * |
---|
| 69 | * \brief Wait for a valid response after the send of a command |
---|
| 70 | * This function can return if one of the next two conditions are true: |
---|
| 71 | * 1. Bit valid received |
---|
| 72 | * 2. Timeout (not valid bit received after SDCARD_COMMAND_TIMEOUT |
---|
| 73 | * wait ticks) |
---|
| 74 | */ |
---|
| 75 | static unsigned char _sdcard_wait_response(struct sdcard_dev * sdcard) |
---|
| 76 | { |
---|
| 77 | unsigned char sdcard_rsp; |
---|
| 78 | register int iter; |
---|
| 79 | |
---|
| 80 | iter = 0; |
---|
| 81 | sdcard_rsp = _sdcard_receive_char(sdcard); |
---|
| 82 | while ( |
---|
| 83 | (iter < SDCARD_COMMAND_TIMEOUT) && |
---|
| 84 | !SDCARD_CHECK_R1_VALID(sdcard_rsp) |
---|
| 85 | ) |
---|
| 86 | { |
---|
| 87 | sdcard_rsp = _sdcard_receive_char(sdcard); |
---|
| 88 | iter++; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | return sdcard_rsp; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * \params sdcard: Initialized pointer to the block device |
---|
| 96 | * |
---|
| 97 | * \return void |
---|
| 98 | * |
---|
| 99 | * \brief Wait data block start marker |
---|
| 100 | */ |
---|
| 101 | static void _sdcard_wait_data_block(struct sdcard_dev * sdcard) |
---|
| 102 | { |
---|
| 103 | while (_sdcard_receive_char(sdcard) != 0xFE); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * \param sdcard : Initialized pointer to block device |
---|
| 108 | * \param index : SD card CMD index |
---|
[694] | 109 | * \param app : Type of command, 0 for normal command or 1 for application |
---|
| 110 | * specific |
---|
[292] | 111 | * \param args : SD card CMD arguments |
---|
| 112 | * |
---|
| 113 | * \return response first byte |
---|
| 114 | * |
---|
| 115 | * \brief Send command to the SD card |
---|
| 116 | */ |
---|
| 117 | static int _sdcard_send_command ( |
---|
| 118 | struct sdcard_dev * sdcard , |
---|
| 119 | int index , |
---|
| 120 | int app , |
---|
| 121 | void * args , |
---|
| 122 | unsigned crc7 ) |
---|
| 123 | { |
---|
| 124 | unsigned char sdcard_rsp; |
---|
| 125 | unsigned char * _args; |
---|
| 126 | |
---|
| 127 | _sdcard_gen_tick(sdcard, 5); |
---|
| 128 | |
---|
| 129 | if (app == SDCARD_ACMD) |
---|
| 130 | { |
---|
| 131 | spi_put_tx(sdcard->spi, 0x40 | 55 , 0 );/* CMD and START bit */ |
---|
| 132 | spi_put_tx(sdcard->spi, 0x00 , 0 );/* Argument[0] */ |
---|
| 133 | spi_put_tx(sdcard->spi, 0x00 , 0 );/* Argument[1] */ |
---|
| 134 | spi_put_tx(sdcard->spi, 0x00 , 0 );/* Argument[2] */ |
---|
| 135 | spi_put_tx(sdcard->spi, 0x00 , 0 );/* Argument[3] */ |
---|
| 136 | spi_put_tx(sdcard->spi, 0x01 | (crc7 << 1), 0 );/* END bit */ |
---|
| 137 | |
---|
| 138 | sdcard_rsp = _sdcard_wait_response(sdcard); |
---|
| 139 | if (SDCARD_CHECK_R1_ERROR(sdcard_rsp)) |
---|
| 140 | { |
---|
| 141 | return sdcard_rsp; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | _args = (unsigned char *) args; |
---|
| 146 | |
---|
| 147 | _sdcard_gen_tick(sdcard, 1); |
---|
| 148 | |
---|
| 149 | spi_put_tx(sdcard->spi, 0x40 | index , 0 ); |
---|
| 150 | spi_put_tx(sdcard->spi, _args[0] , 0 ); |
---|
| 151 | spi_put_tx(sdcard->spi, _args[1] , 0 ); |
---|
| 152 | spi_put_tx(sdcard->spi, _args[2] , 0 ); |
---|
| 153 | spi_put_tx(sdcard->spi, _args[3] , 0 ); |
---|
| 154 | spi_put_tx(sdcard->spi, 0x01 | (crc7 << 1), 0 ); |
---|
| 155 | |
---|
| 156 | return _sdcard_wait_response(sdcard); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | int sdcard_dev_open(struct sdcard_dev * sdcard, struct spi_dev * spi, int ss) |
---|
| 160 | { |
---|
[501] | 161 | unsigned char args[4]; |
---|
[292] | 162 | unsigned char sdcard_rsp; |
---|
[501] | 163 | unsigned int iter, ersp; |
---|
[292] | 164 | |
---|
| 165 | sdcard->spi = spi; |
---|
[501] | 166 | sdcard->slave_id = ss; |
---|
[292] | 167 | |
---|
| 168 | /* |
---|
[501] | 169 | * Supply SD card ramp up time (min 74 cycles) |
---|
| 170 | */ |
---|
[292] | 171 | _sdcard_gen_tick(sdcard, 10); |
---|
| 172 | |
---|
[501] | 173 | /* |
---|
| 174 | * Assert slave select signal |
---|
| 175 | * Send CMD0 (Reset Command) |
---|
| 176 | * Deassert slave select signal |
---|
| 177 | */ |
---|
[292] | 178 | _sdcard_enable(sdcard); |
---|
| 179 | |
---|
[501] | 180 | args[0] = 0; |
---|
| 181 | args[1] = 0; |
---|
| 182 | args[2] = 0; |
---|
| 183 | args[3] = 0; |
---|
[292] | 184 | sdcard_rsp = _sdcard_send_command(sdcard, 0, SDCARD_CMD, args, 0x4A); |
---|
| 185 | if ( sdcard_rsp != 0x01 ) |
---|
| 186 | { |
---|
[586] | 187 | reset_puts("card CMD0 failed "); |
---|
[292] | 188 | return sdcard_rsp; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | _sdcard_disable(sdcard); |
---|
[501] | 192 | /* |
---|
| 193 | * send CMD8. If card is pre-v2, It will reply with illegal command. |
---|
| 194 | * Otherwise we announce sdhc support. |
---|
| 195 | */ |
---|
[292] | 196 | _sdcard_enable(sdcard); |
---|
[501] | 197 | args[0] = 0; |
---|
| 198 | args[1] = 0; |
---|
| 199 | args[2] = 0x01; |
---|
| 200 | args[3] = 0x01; |
---|
| 201 | sdcard_rsp = _sdcard_send_command(sdcard, 8, SDCARD_CMD, args, 0x63); |
---|
| 202 | if (!SDCARD_CHECK_R1_VALID(sdcard_rsp)) { |
---|
[586] | 203 | reset_puts("card CMD8 failed "); |
---|
[501] | 204 | return sdcard_rsp; |
---|
| 205 | } |
---|
| 206 | if (!SDCARD_CHECK_R1_ERROR(sdcard_rsp)) { |
---|
| 207 | /* no error, command accepted. get whole reply */ |
---|
| 208 | ersp = _sdcard_receive_char(sdcard); |
---|
| 209 | ersp = (ersp << 8) | _sdcard_receive_char(sdcard); |
---|
| 210 | ersp = (ersp << 8) | _sdcard_receive_char(sdcard); |
---|
| 211 | ersp = (ersp << 8) | _sdcard_receive_char(sdcard); |
---|
| 212 | if ((ersp & 0xffff) != 0x0101) { |
---|
| 213 | /* voltage mismatch */ |
---|
[586] | 214 | reset_puts("card CMD8 mismatch: "); |
---|
| 215 | reset_putx(ersp); |
---|
[501] | 216 | return sdcard_rsp; |
---|
| 217 | } |
---|
[586] | 218 | reset_puts("v2 or later "); |
---|
[501] | 219 | sdcard->sdhc = 1; |
---|
| 220 | } else if ((sdcard_rsp & SDCARD_R1_ILLEGAL_CMD) == 0) { |
---|
| 221 | /* other error */ |
---|
[586] | 222 | reset_puts("card CMD8 error "); |
---|
[501] | 223 | return sdcard_rsp; |
---|
| 224 | } else { |
---|
| 225 | sdcard->sdhc = 0; |
---|
| 226 | } |
---|
| 227 | _sdcard_disable(sdcard); |
---|
| 228 | /* send CMD41, enabling the card */ |
---|
| 229 | _sdcard_enable(sdcard); |
---|
| 230 | args[0] = sdcard->sdhc ? 0x40: 0; |
---|
| 231 | args[1] = 0; |
---|
| 232 | args[2] = 0; |
---|
| 233 | args[3] = 0; |
---|
[292] | 234 | |
---|
| 235 | iter = 0; |
---|
| 236 | while( iter++ < SDCARD_COMMAND_TIMEOUT ) |
---|
| 237 | { |
---|
| 238 | sdcard_rsp = _sdcard_send_command(sdcard, 41, SDCARD_ACMD, args, 0x00); |
---|
| 239 | if( sdcard_rsp == 0x01 ) |
---|
| 240 | { |
---|
| 241 | continue; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | break; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | _sdcard_disable(sdcard); |
---|
[501] | 248 | if (sdcard_rsp) { |
---|
[586] | 249 | reset_puts("SD ACMD41 failed "); |
---|
[501] | 250 | return sdcard_rsp; |
---|
| 251 | } |
---|
| 252 | if (sdcard->sdhc != 0) { |
---|
| 253 | /* get the card capacity to see if it's really HC */ |
---|
| 254 | _sdcard_enable(sdcard); |
---|
| 255 | args[0] = sdcard->sdhc ? 0x40: 0; |
---|
| 256 | args[1] = 0; |
---|
| 257 | args[2] = 0; |
---|
| 258 | args[3] = 0; |
---|
| 259 | sdcard_rsp = _sdcard_send_command(sdcard, 58, SDCARD_CMD, |
---|
| 260 | args, 0x00); |
---|
| 261 | if (sdcard_rsp) { |
---|
[586] | 262 | reset_puts("SD CMD58 failed "); |
---|
[501] | 263 | return sdcard_rsp; |
---|
| 264 | } |
---|
| 265 | ersp = _sdcard_receive_char(sdcard); |
---|
| 266 | ersp = (ersp << 8) | _sdcard_receive_char(sdcard); |
---|
| 267 | ersp = (ersp << 8) | _sdcard_receive_char(sdcard); |
---|
| 268 | ersp = (ersp << 8) | _sdcard_receive_char(sdcard); |
---|
| 269 | if (ersp & 0x40000000) { |
---|
[586] | 270 | reset_puts("SDHC "); |
---|
[501] | 271 | } else { |
---|
| 272 | sdcard->sdhc = 0; |
---|
| 273 | } |
---|
| 274 | _sdcard_disable(sdcard); |
---|
| 275 | } |
---|
[586] | 276 | reset_puts("card detected "); |
---|
[501] | 277 | return 0; |
---|
[292] | 278 | } |
---|
| 279 | |
---|
| 280 | int sdcard_dev_read(struct sdcard_dev * sdcard, void * buf, unsigned int count) |
---|
| 281 | { |
---|
| 282 | unsigned char args[4]; |
---|
| 283 | unsigned char sdcard_rsp; |
---|
| 284 | register int i; |
---|
| 285 | |
---|
| 286 | for (i = 0; i < 4; i++) |
---|
[388] | 287 | { |
---|
[292] | 288 | args[i] = (sdcard->access_pointer >> (32 - (i+1)*8)) & 0xFF; |
---|
[388] | 289 | } |
---|
[292] | 290 | |
---|
| 291 | _sdcard_enable(sdcard); |
---|
| 292 | |
---|
| 293 | sdcard_rsp = _sdcard_send_command(sdcard, 17, SDCARD_CMD, args, 0x00); |
---|
| 294 | if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) ) |
---|
| 295 | { |
---|
| 296 | _sdcard_disable(sdcard); |
---|
| 297 | return sdcard_rsp; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | _sdcard_wait_data_block(sdcard); |
---|
| 301 | |
---|
[398] | 302 | spi_get_data(sdcard->spi, buf, count); |
---|
[292] | 303 | |
---|
| 304 | /* |
---|
| 305 | * Get the remainder of the block bytes and the CRC16 (comes |
---|
| 306 | * at the end of the data block) |
---|
| 307 | */ |
---|
[398] | 308 | i = count; |
---|
[501] | 309 | while( i++ < (512 + 2) ) _sdcard_receive_char(sdcard); |
---|
[292] | 310 | |
---|
| 311 | _sdcard_disable(sdcard); |
---|
| 312 | |
---|
| 313 | /* |
---|
| 314 | * Move the access pointer to the next block |
---|
| 315 | */ |
---|
| 316 | sdcard->access_pointer += sdcard->block_length; |
---|
| 317 | |
---|
| 318 | return 0; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | unsigned int sdcard_dev_write(struct sdcard_dev *sdcard, void * buf, unsigned int count) |
---|
| 322 | { |
---|
| 323 | return 0; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | void sdcard_dev_lseek(struct sdcard_dev * sdcard, unsigned int blk_pos) |
---|
| 327 | { |
---|
| 328 | sdcard->access_pointer = sdcard->block_length * blk_pos; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | int sdcard_dev_set_blocklen(struct sdcard_dev * sdcard, unsigned int len) |
---|
| 332 | { |
---|
| 333 | unsigned char args[4]; |
---|
| 334 | unsigned char sdcard_rsp; |
---|
| 335 | register int i; |
---|
| 336 | |
---|
[501] | 337 | if (len != 512) |
---|
| 338 | return 1; |
---|
| 339 | |
---|
| 340 | if (sdcard->sdhc) { |
---|
| 341 | sdcard->block_length = 1; |
---|
| 342 | return 0; |
---|
| 343 | } |
---|
| 344 | |
---|
[292] | 345 | for (i = 0; i < 4; i++) |
---|
| 346 | args[i] = (len >> (32 - (i+1)*8)) & 0xFF; |
---|
| 347 | |
---|
| 348 | _sdcard_enable(sdcard); |
---|
| 349 | |
---|
| 350 | sdcard_rsp = _sdcard_send_command(sdcard, 16, SDCARD_CMD, args, 0x00); |
---|
| 351 | if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) ) |
---|
| 352 | { |
---|
| 353 | _sdcard_disable(sdcard); |
---|
| 354 | return sdcard_rsp; |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | _sdcard_disable(sdcard); |
---|
| 358 | |
---|
| 359 | sdcard->block_length = len; |
---|
| 360 | |
---|
| 361 | return 0; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | /* |
---|
| 365 | * vim: tabstop=4 : shiftwidth=4 : expandtab : softtabstop=4 |
---|
| 366 | */ |
---|